]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/cx18/cx18-driver.c
V4L/DVB (13430): cx18: Fix YUV capture so that encoder passes a single frame per...
[net-next-2.6.git] / drivers / media / video / cx18 / cx18-driver.c
CommitLineData
1c1e45d1
HV
1/*
2 * cx18 driver initialization and card probing
3 *
4 * Derived from ivtv-driver.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
c641d09c 7 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
1c1e45d1
HV
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
b1526421 26#include "cx18-io.h"
1c1e45d1
HV
27#include "cx18-version.h"
28#include "cx18-cards.h"
29#include "cx18-i2c.h"
30#include "cx18-irq.h"
31#include "cx18-gpio.h"
32#include "cx18-firmware.h"
21a278b8 33#include "cx18-queue.h"
1c1e45d1
HV
34#include "cx18-streams.h"
35#include "cx18-av-core.h"
36#include "cx18-scb.h"
37#include "cx18-mailbox.h"
38#include "cx18-ioctl.h"
39#include "tuner-xc2028.h"
40
41#include <media/tveeprom.h>
42
1c1e45d1
HV
43/* If you have already X v4l cards, then set this to X. This way
44 the device numbers stay matched. Example: you have a WinTV card
45 without radio and a Compro H900 with. Normally this would give a
46 video1 device together with a radio0 device for the Compro. By
47 setting this to 1 you ensure that radio0 is now also radio1. */
48int cx18_first_minor;
49
1c1e45d1
HV
50/* add your revision and whatnot here */
51static struct pci_device_id cx18_pci_tbl[] __devinitdata = {
52 {PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,
53 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
54 {0,}
55};
56
57MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);
58
5811cf99
AW
59static atomic_t cx18_instance = ATOMIC_INIT(0);
60
1c1e45d1
HV
61/* Parameter declarations */
62static int cardtype[CX18_MAX_CARDS];
63static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, -1, -1, -1, -1, -1, -1, -1,
66 -1, -1, -1, -1, -1, -1, -1, -1 };
67static int radio[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
68 -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1 };
c6eb8eaf
HV
71static unsigned cardtype_c = 1;
72static unsigned tuner_c = 1;
73static unsigned radio_c = 1;
1c1e45d1
HV
74static char pal[] = "--";
75static char secam[] = "--";
76static char ntsc[] = "-";
77
78/* Buffers */
1c1e45d1 79static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
6ecd86dc
AW
80static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
81static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;
1c1e45d1
HV
82static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
83static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
84static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;
85
6ecd86dc
AW
86static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
87static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
88static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
89static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
90/* VBI bufsize based on standards supported by card tuner for now */
91static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;
92
93static int enc_ts_bufs = -1;
94static int enc_mpg_bufs = -1;
95static int enc_idx_bufs = -1;
96static int enc_yuv_bufs = -1;
97static int enc_vbi_bufs = -1;
98static int enc_pcm_bufs = -1;
99
100
1c1e45d1
HV
101static int cx18_pci_latency = 1;
102
3f75c616
AW
103static int mmio_ndelay;
104static int retry_mmio = 1;
105
1c1e45d1
HV
106int cx18_debug;
107
108module_param_array(tuner, int, &tuner_c, 0644);
109module_param_array(radio, bool, &radio_c, 0644);
110module_param_array(cardtype, int, &cardtype_c, 0644);
111module_param_string(pal, pal, sizeof(pal), 0644);
112module_param_string(secam, secam, sizeof(secam), 0644);
113module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
114module_param_named(debug, cx18_debug, int, 0644);
3f75c616
AW
115module_param(mmio_ndelay, int, 0644);
116module_param(retry_mmio, int, 0644);
1c1e45d1
HV
117module_param(cx18_pci_latency, int, 0644);
118module_param(cx18_first_minor, int, 0644);
119
1c1e45d1 120module_param(enc_ts_buffers, int, 0644);
6ecd86dc
AW
121module_param(enc_mpg_buffers, int, 0644);
122module_param(enc_idx_buffers, int, 0644);
1c1e45d1
HV
123module_param(enc_yuv_buffers, int, 0644);
124module_param(enc_vbi_buffers, int, 0644);
125module_param(enc_pcm_buffers, int, 0644);
126
6ecd86dc
AW
127module_param(enc_ts_bufsize, int, 0644);
128module_param(enc_mpg_bufsize, int, 0644);
129module_param(enc_idx_bufsize, int, 0644);
130module_param(enc_yuv_bufsize, int, 0644);
131/* VBI bufsize based on standards supported by card tuner for now */
132module_param(enc_pcm_bufsize, int, 0644);
133
134module_param(enc_ts_bufs, int, 0644);
135module_param(enc_mpg_bufs, int, 0644);
136module_param(enc_idx_bufs, int, 0644);
137module_param(enc_yuv_bufs, int, 0644);
138module_param(enc_vbi_bufs, int, 0644);
139module_param(enc_pcm_bufs, int, 0644);
140
1c1e45d1
HV
141MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
142 "\t\t\tsee tuner.h for values");
143MODULE_PARM_DESC(radio,
144 "Enable or disable the radio. Use only if autodetection\n"
145 "\t\t\tfails. 0 = disable, 1 = enable");
146MODULE_PARM_DESC(cardtype,
147 "Only use this option if your card is not detected properly.\n"
148 "\t\tSpecify card type:\n"
149 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
150 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
151 "\t\t\t 3 = Compro VideoMate H900\n"
152 "\t\t\t 4 = Yuan MPC718\n"
03c28085 153 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
9eee4fb6 154 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
9d5af862
AW
155 "\t\t\t 7 = Leadtek WinFast PVR2100\n"
156 "\t\t\t 8 = Leadtek WinFast DVR3100 H\n"
1c1e45d1
HV
157 "\t\t\t 0 = Autodetect (default)\n"
158 "\t\t\t-1 = Ignore this card\n\t\t");
159MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
160MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
161MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
162MODULE_PARM_DESC(debug,
163 "Debug level (bitmask). Default: 0\n"
164 "\t\t\t 1/0x0001: warning\n"
165 "\t\t\t 2/0x0002: info\n"
166 "\t\t\t 4/0x0004: mailbox\n"
167 "\t\t\t 8/0x0008: dma\n"
168 "\t\t\t 16/0x0010: ioctl\n"
169 "\t\t\t 32/0x0020: file\n"
170 "\t\t\t 64/0x0040: i2c\n"
171 "\t\t\t128/0x0080: irq\n"
172 "\t\t\t256/0x0100: high volume\n");
173MODULE_PARM_DESC(cx18_pci_latency,
174 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
175 "\t\t\tDefault: Yes");
d267d851 176MODULE_PARM_DESC(retry_mmio,
3f75c616
AW
177 "(Deprecated) MMIO writes are now always checked and retried\n"
178 "\t\t\tEffectively: 1 [Yes]");
c641d09c 179MODULE_PARM_DESC(mmio_ndelay,
3f75c616
AW
180 "(Deprecated) MMIO accesses are now never purposely delayed\n"
181 "\t\t\tEffectively: 0 ns");
1c1e45d1 182MODULE_PARM_DESC(enc_ts_buffers,
6ecd86dc 183 "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"
1c1e45d1 184 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
6ecd86dc
AW
185MODULE_PARM_DESC(enc_ts_bufsize,
186 "Size of an encoder TS buffer (kB)\n"
187 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));
188MODULE_PARM_DESC(enc_ts_bufs,
189 "Number of encoder TS buffers\n"
190 "\t\t\tDefault is computed from other enc_ts_* parameters");
191MODULE_PARM_DESC(enc_mpg_buffers,
192 "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"
193 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
194MODULE_PARM_DESC(enc_mpg_bufsize,
195 "Size of an encoder MPG buffer (kB)\n"
196 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));
197MODULE_PARM_DESC(enc_mpg_bufs,
198 "Number of encoder MPG buffers\n"
199 "\t\t\tDefault is computed from other enc_mpg_* parameters");
200MODULE_PARM_DESC(enc_idx_buffers,
201 "Encoder IDX buffer memory (MB). (enc_idx_bufs can override)\n"
202 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFFERS));
203MODULE_PARM_DESC(enc_idx_bufsize,
204 "Size of an encoder IDX buffer (kB)\n"
205 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFSIZE));
206MODULE_PARM_DESC(enc_idx_bufs,
207 "Number of encoder IDX buffers\n"
208 "\t\t\tDefault is computed from other enc_idx_* parameters");
1c1e45d1 209MODULE_PARM_DESC(enc_yuv_buffers,
6ecd86dc 210 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
1c1e45d1 211 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
6ecd86dc
AW
212MODULE_PARM_DESC(enc_yuv_bufsize,
213 "Size of an encoder YUV buffer (kB)\n"
22dce188
AW
214 "\t\t\tAllowed values are multiples of 33.75 kB rounded up\n"
215 "\t\t\t(multiples of size required for 32 screen lines)\n"
216 "\t\t\tDefault: 102");
6ecd86dc
AW
217MODULE_PARM_DESC(enc_yuv_bufs,
218 "Number of encoder YUV buffers\n"
219 "\t\t\tDefault is computed from other enc_yuv_* parameters");
1c1e45d1 220MODULE_PARM_DESC(enc_vbi_buffers,
6ecd86dc 221 "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"
1c1e45d1 222 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
6ecd86dc
AW
223MODULE_PARM_DESC(enc_vbi_bufs,
224 "Number of encoder VBI buffers\n"
225 "\t\t\tDefault is computed from enc_vbi_buffers & tuner std");
1c1e45d1 226MODULE_PARM_DESC(enc_pcm_buffers,
6ecd86dc 227 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
1c1e45d1 228 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
6ecd86dc
AW
229MODULE_PARM_DESC(enc_pcm_bufsize,
230 "Size of an encoder PCM buffer (kB)\n"
231 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));
232MODULE_PARM_DESC(enc_pcm_bufs,
233 "Number of encoder PCM buffers\n"
234 "\t\t\tDefault is computed from other enc_pcm_* parameters");
1c1e45d1 235
581644d9 236MODULE_PARM_DESC(cx18_first_minor, "Set device node number assigned to first card");
1c1e45d1
HV
237
238MODULE_AUTHOR("Hans Verkuil");
239MODULE_DESCRIPTION("CX23418 driver");
240MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
241MODULE_LICENSE("GPL");
242
243MODULE_VERSION(CX18_VERSION);
244
1c1e45d1
HV
245/* Generic utility functions */
246int cx18_msleep_timeout(unsigned int msecs, int intr)
247{
330c6ec8 248 long int timeout = msecs_to_jiffies(msecs);
1c1e45d1
HV
249 int sig;
250
251 do {
252 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
253 timeout = schedule_timeout(timeout);
254 sig = intr ? signal_pending(current) : 0;
255 } while (!sig && timeout);
256 return sig;
257}
258
259/* Release ioremapped memory */
260static void cx18_iounmap(struct cx18 *cx)
261{
262 if (cx == NULL)
263 return;
264
265 /* Release io memory */
266 if (cx->enc_mem != NULL) {
267 CX18_DEBUG_INFO("releasing enc_mem\n");
268 iounmap(cx->enc_mem);
269 cx->enc_mem = NULL;
270 }
271}
272
25a42e4d
AW
273static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len)
274{
275 int i;
276
277 CX18_INFO("eeprom dump:\n");
278 for (i = 0; i < len; i++) {
279 if (0 == (i % 16))
280 CX18_INFO("eeprom %02x:", i);
281 printk(KERN_CONT " %02x", eedata[i]);
282 if (15 == (i % 16))
283 printk(KERN_CONT "\n");
284 }
285}
286
1c1e45d1
HV
287/* Hauppauge card? get values from tveeprom */
288void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
289{
ff2a2001 290 struct i2c_client c;
1c1e45d1
HV
291 u8 eedata[256];
292
6246d4e1 293 memset(&c, 0, sizeof(c));
098003d8 294 strlcpy(c.name, "cx18 tveeprom tmp", sizeof(c.name));
ff2a2001
AW
295 c.adapter = &cx->i2c_adap[0];
296 c.addr = 0xA0 >> 1;
297
25a42e4d
AW
298 memset(tv, 0, sizeof(*tv));
299 if (tveeprom_read(&c, eedata, sizeof(eedata)))
300 return;
301
302 switch (cx->card->type) {
303 case CX18_CARD_HVR_1600_ESMT:
304 case CX18_CARD_HVR_1600_SAMSUNG:
305 tveeprom_hauppauge_analog(&c, tv, eedata);
306 break;
307 case CX18_CARD_YUAN_MPC718:
308 tv->model = 0x718;
309 cx18_eeprom_dump(cx, eedata, sizeof(eedata));
310 CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n",
311 eedata[2], eedata[1], eedata[4], eedata[3]);
312 break;
313 default:
314 tv->model = 0xffffffff;
315 cx18_eeprom_dump(cx, eedata, sizeof(eedata));
316 break;
317 }
1c1e45d1
HV
318}
319
320static void cx18_process_eeprom(struct cx18 *cx)
321{
322 struct tveeprom tv;
323
324 cx18_read_eeprom(cx, &tv);
325
326 /* Many thanks to Steven Toth from Hauppauge for providing the
327 model numbers */
1d081601
HV
328 /* Note: the Samsung memory models cannot be reliably determined
329 from the model number. Use the cardtype module option if you
330 have one of these preproduction models. */
1c1e45d1 331 switch (tv.model) {
1d081601 332 case 74000 ... 74999:
1c1e45d1
HV
333 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
334 break;
25a42e4d
AW
335 case 0x718:
336 return;
337 case 0xffffffff:
338 CX18_INFO("Unknown EEPROM encoding\n");
339 return;
1c1e45d1
HV
340 case 0:
341 CX18_ERR("Invalid EEPROM\n");
342 return;
343 default:
344 CX18_ERR("Unknown model %d, defaulting to HVR-1600\n", tv.model);
345 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
346 break;
347 }
348
349 cx->v4l2_cap = cx->card->v4l2_capabilities;
350 cx->card_name = cx->card->name;
351 cx->card_i2c = cx->card->i2c;
352
353 CX18_INFO("Autodetected %s\n", cx->card_name);
354
355 if (tv.tuner_type == TUNER_ABSENT)
4442ee8b 356 CX18_ERR("tveeprom cannot autodetect tuner!\n");
1c1e45d1
HV
357
358 if (cx->options.tuner == -1)
359 cx->options.tuner = tv.tuner_type;
360 if (cx->options.radio == -1)
361 cx->options.radio = (tv.has_radio != 0);
362
363 if (cx->std != 0)
364 /* user specified tuner standard */
365 return;
366
367 /* autodetect tuner standard */
368 if (tv.tuner_formats & V4L2_STD_PAL) {
369 CX18_DEBUG_INFO("PAL tuner detected\n");
370 cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
371 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
372 CX18_DEBUG_INFO("NTSC tuner detected\n");
373 cx->std |= V4L2_STD_NTSC_M;
374 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
375 CX18_DEBUG_INFO("SECAM tuner detected\n");
376 cx->std |= V4L2_STD_SECAM_L;
377 } else {
378 CX18_INFO("No tuner detected, default to NTSC-M\n");
379 cx->std |= V4L2_STD_NTSC_M;
380 }
381}
382
383static v4l2_std_id cx18_parse_std(struct cx18 *cx)
384{
385 switch (pal[0]) {
386 case '6':
387 return V4L2_STD_PAL_60;
388 case 'b':
389 case 'B':
390 case 'g':
391 case 'G':
392 return V4L2_STD_PAL_BG;
393 case 'h':
394 case 'H':
395 return V4L2_STD_PAL_H;
396 case 'n':
397 case 'N':
398 if (pal[1] == 'c' || pal[1] == 'C')
399 return V4L2_STD_PAL_Nc;
400 return V4L2_STD_PAL_N;
401 case 'i':
402 case 'I':
403 return V4L2_STD_PAL_I;
404 case 'd':
405 case 'D':
406 case 'k':
407 case 'K':
408 return V4L2_STD_PAL_DK;
409 case 'M':
410 case 'm':
411 return V4L2_STD_PAL_M;
412 case '-':
413 break;
414 default:
415 CX18_WARN("pal= argument not recognised\n");
416 return 0;
417 }
418
419 switch (secam[0]) {
420 case 'b':
421 case 'B':
422 case 'g':
423 case 'G':
424 case 'h':
425 case 'H':
426 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
427 case 'd':
428 case 'D':
429 case 'k':
430 case 'K':
431 return V4L2_STD_SECAM_DK;
432 case 'l':
433 case 'L':
434 if (secam[1] == 'C' || secam[1] == 'c')
435 return V4L2_STD_SECAM_LC;
436 return V4L2_STD_SECAM_L;
437 case '-':
438 break;
439 default:
440 CX18_WARN("secam= argument not recognised\n");
441 return 0;
442 }
443
444 switch (ntsc[0]) {
445 case 'm':
446 case 'M':
447 return V4L2_STD_NTSC_M;
448 case 'j':
449 case 'J':
450 return V4L2_STD_NTSC_M_JP;
451 case 'k':
452 case 'K':
453 return V4L2_STD_NTSC_M_KR;
454 case '-':
455 break;
456 default:
457 CX18_WARN("ntsc= argument not recognised\n");
458 return 0;
459 }
460
461 /* no match found */
462 return 0;
463}
464
465static void cx18_process_options(struct cx18 *cx)
466{
467 int i, j;
468
1c1e45d1 469 cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
6ecd86dc
AW
470 cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
471 cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;
1c1e45d1
HV
472 cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
473 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
474 cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
6ecd86dc
AW
475 cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */
476
477 cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;
478 cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;
479 cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;
480 cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;
481 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;
482 cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;
483 cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */
484
485 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;
486 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
487 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
488 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
466df464 489 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36;
6ecd86dc
AW
490 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
491 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
492
466df464 493 /* Ensure stream_buffers & stream_buf_size are valid */
6ecd86dc 494 for (i = 0; i < CX18_MAX_STREAMS; i++) {
466df464
AW
495 if (cx->stream_buffers[i] == 0 || /* User said 0 buffers */
496 cx->options.megabytes[i] <= 0 || /* User said 0 MB total */
497 cx->stream_buf_size[i] <= 0) { /* User said buf size 0 */
6ecd86dc
AW
498 cx->options.megabytes[i] = 0;
499 cx->stream_buffers[i] = 0;
500 cx->stream_buf_size[i] = 0;
501 continue;
502 }
466df464 503 /*
22dce188
AW
504 * YUV is a special case where the stream_buf_size needs to be
505 * an integral multiple of 33.75 kB (storage for 32 screens
506 * lines to maintain alignment in case of lost buffers
507 */
508 if (i == CX18_ENC_STREAM_TYPE_YUV) {
509 cx->stream_buf_size[i] *= 1024;
510 cx->stream_buf_size[i] -=
511 (cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE);
512
513 if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE)
514 cx->stream_buf_size[i] =
515 CX18_UNIT_ENC_YUV_BUFSIZE;
516 }
517 /*
518 * YUV is a special case where the stream_buf_size is
519 * now in bytes.
466df464
AW
520 * VBI is a special case where the stream_buf_size is fixed
521 * and already in bytes
522 */
22dce188
AW
523 if (i == CX18_ENC_STREAM_TYPE_VBI ||
524 i == CX18_ENC_STREAM_TYPE_YUV) {
466df464
AW
525 if (cx->stream_buffers[i] < 0) {
526 cx->stream_buffers[i] =
527 cx->options.megabytes[i] * 1024 * 1024
528 / cx->stream_buf_size[i];
529 } else {
530 /* N.B. This might round down to 0 */
531 cx->options.megabytes[i] =
532 cx->stream_buffers[i]
533 * cx->stream_buf_size[i]/(1024 * 1024);
6ecd86dc 534 }
6ecd86dc 535 } else {
22dce188
AW
536 /* All other streams have stream_buf_size in kB here */
537 if (cx->stream_buffers[i] < 0) {
538 cx->stream_buffers[i] =
539 cx->options.megabytes[i] * 1024
540 / cx->stream_buf_size[i];
541 } else {
542 /* N.B. This might round down to 0 */
543 cx->options.megabytes[i] =
544 cx->stream_buffers[i]
545 * cx->stream_buf_size[i] / 1024;
546 }
547 /* convert from kB to bytes */
548 cx->stream_buf_size[i] *= 1024;
6ecd86dc 549 }
22dce188
AW
550 CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, "
551 "%d bytes\n", i, cx->options.megabytes[i],
552 cx->stream_buffers[i], cx->stream_buf_size[i]);
6ecd86dc
AW
553 }
554
5811cf99
AW
555 cx->options.cardtype = cardtype[cx->instance];
556 cx->options.tuner = tuner[cx->instance];
557 cx->options.radio = radio[cx->instance];
1c1e45d1
HV
558
559 cx->std = cx18_parse_std(cx);
560 if (cx->options.cardtype == -1) {
561 CX18_INFO("Ignore card\n");
562 return;
563 }
564 cx->card = cx18_get_card(cx->options.cardtype - 1);
565 if (cx->card)
566 CX18_INFO("User specified %s card\n", cx->card->name);
567 else if (cx->options.cardtype != 0)
568 CX18_ERR("Unknown user specified type, trying to autodetect card\n");
569 if (cx->card == NULL) {
3d05913d 570 if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
1c1e45d1
HV
571 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
572 CX18_INFO("Autodetected Hauppauge card\n");
573 }
574 }
575 if (cx->card == NULL) {
576 for (i = 0; (cx->card = cx18_get_card(i)); i++) {
577 if (cx->card->pci_list == NULL)
578 continue;
579 for (j = 0; cx->card->pci_list[j].device; j++) {
3d05913d 580 if (cx->pci_dev->device !=
1c1e45d1
HV
581 cx->card->pci_list[j].device)
582 continue;
3d05913d 583 if (cx->pci_dev->subsystem_vendor !=
1c1e45d1
HV
584 cx->card->pci_list[j].subsystem_vendor)
585 continue;
3d05913d 586 if (cx->pci_dev->subsystem_device !=
1c1e45d1
HV
587 cx->card->pci_list[j].subsystem_device)
588 continue;
589 CX18_INFO("Autodetected %s card\n", cx->card->name);
590 goto done;
591 }
592 }
593 }
594done:
595
596 if (cx->card == NULL) {
597 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
29e66a6c 598 CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
3d05913d 599 cx->pci_dev->vendor, cx->pci_dev->device);
29e66a6c 600 CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n",
3d05913d
AW
601 cx->pci_dev->subsystem_vendor,
602 cx->pci_dev->subsystem_device);
1c1e45d1
HV
603 CX18_ERR("Defaulting to %s card\n", cx->card->name);
604 CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
605 CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
606 CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
607 }
608 cx->v4l2_cap = cx->card->v4l2_capabilities;
609 cx->card_name = cx->card->name;
610 cx->card_i2c = cx->card->i2c;
611}
612
87116159
AW
613static int __devinit cx18_create_in_workq(struct cx18 *cx)
614{
615 snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in",
616 cx->v4l2_dev.name);
617 cx->in_work_queue = create_singlethread_workqueue(cx->in_workq_name);
618 if (cx->in_work_queue == NULL) {
619 CX18_ERR("Unable to create incoming mailbox handler thread\n");
620 return -ENOMEM;
621 }
622 return 0;
623}
624
625static int __devinit cx18_create_out_workq(struct cx18 *cx)
626{
627 snprintf(cx->out_workq_name, sizeof(cx->out_workq_name), "%s-out",
628 cx->v4l2_dev.name);
629 cx->out_work_queue = create_workqueue(cx->out_workq_name);
630 if (cx->out_work_queue == NULL) {
631 CX18_ERR("Unable to create outgoing mailbox handler threads\n");
632 return -ENOMEM;
633 }
634 return 0;
635}
636
637static void __devinit cx18_init_in_work_orders(struct cx18 *cx)
638{
639 int i;
640 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
641 cx->in_work_order[i].cx = cx;
642 cx->in_work_order[i].str = cx->epu_debug_str;
643 INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler);
644 }
645}
646
1c1e45d1 647/* Precondition: the cx18 structure has been memset to 0. Only
5811cf99 648 the dev and instance fields have been filled in.
1c1e45d1
HV
649 No assumptions on the card type may be made here (see cx18_init_struct2
650 for that).
651 */
652static int __devinit cx18_init_struct1(struct cx18 *cx)
653{
87116159 654 int ret;
ee2d64f5 655
3d05913d 656 cx->base_addr = pci_resource_start(cx->pci_dev, 0);
1c1e45d1
HV
657
658 mutex_init(&cx->serialize_lock);
8abdd00d 659 mutex_init(&cx->gpio_lock);
72c2d6d3
AW
660 mutex_init(&cx->epu2apu_mb_lock);
661 mutex_init(&cx->epu2cpu_mb_lock);
1c1e45d1 662
87116159
AW
663 ret = cx18_create_out_workq(cx);
664 if (ret)
665 return ret;
572bfea7 666
87116159
AW
667 ret = cx18_create_in_workq(cx);
668 if (ret) {
669 destroy_workqueue(cx->out_work_queue);
670 return ret;
ee2d64f5 671 }
1d6782bd 672
87116159
AW
673 cx18_init_in_work_orders(cx);
674
1c1e45d1
HV
675 /* start counting open_id at 1 */
676 cx->open_id = 1;
677
678 /* Initial settings */
679 cx2341x_fill_defaults(&cx->params);
680 cx->temporal_strength = cx->params.video_temporal_filter;
681 cx->spatial_strength = cx->params.video_spatial_filter;
682 cx->filter_mode = cx->params.video_spatial_filter_mode |
683 (cx->params.video_temporal_filter_mode << 1) |
684 (cx->params.video_median_filter_type << 2);
685 cx->params.port = CX2341X_PORT_MEMORY;
302df970 686 cx->params.capabilities =
006e7179 687 CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI;
1c1e45d1
HV
688 init_waitqueue_head(&cx->cap_w);
689 init_waitqueue_head(&cx->mb_apu_waitq);
690 init_waitqueue_head(&cx->mb_cpu_waitq);
1c1e45d1
HV
691 init_waitqueue_head(&cx->dma_waitq);
692
693 /* VBI */
dd073434 694 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
1c1e45d1 695 cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
af009cf6 696
52fcb3ec
AW
697 /* IVTV style VBI insertion into MPEG streams */
698 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list);
699 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list);
700 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list);
701 list_add(&cx->vbi.sliced_mpeg_buf.list,
702 &cx->vbi.sliced_mpeg_mdl.buf_list);
1c1e45d1
HV
703 return 0;
704}
705
706/* Second initialization part. Here the card type has been
707 autodetected. */
708static void __devinit cx18_init_struct2(struct cx18 *cx)
709{
710 int i;
711
712 for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
713 if (cx->card->video_inputs[i].video_type == 0)
714 break;
715 cx->nof_inputs = i;
716 for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
717 if (cx->card->audio_inputs[i].audio_type == 0)
718 break;
719 cx->nof_audio_inputs = i;
720
721 /* Find tuner input */
722 for (i = 0; i < cx->nof_inputs; i++) {
723 if (cx->card->video_inputs[i].video_type ==
724 CX18_CARD_INPUT_VID_TUNER)
725 break;
726 }
727 if (i == cx->nof_inputs)
728 i = 0;
729 cx->active_input = i;
730 cx->audio_input = cx->card->video_inputs[i].audio_index;
1c1e45d1
HV
731}
732
3d05913d 733static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
1c1e45d1
HV
734 const struct pci_device_id *pci_id)
735{
736 u16 cmd;
737 unsigned char pci_latency;
738
739 CX18_DEBUG_INFO("Enabling pci device\n");
740
3d05913d 741 if (pci_enable_device(pci_dev)) {
5811cf99 742 CX18_ERR("Can't enable device %d!\n", cx->instance);
1c1e45d1
HV
743 return -EIO;
744 }
3d05913d 745 if (pci_set_dma_mask(pci_dev, 0xffffffff)) {
5811cf99 746 CX18_ERR("No suitable DMA available, card %d\n", cx->instance);
1c1e45d1
HV
747 return -EIO;
748 }
749 if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
5811cf99
AW
750 CX18_ERR("Cannot request encoder memory region, card %d\n",
751 cx->instance);
1c1e45d1
HV
752 return -EIO;
753 }
754
4519064c 755 /* Enable bus mastering and memory mapped IO for the CX23418 */
3d05913d 756 pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);
4519064c 757 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
3d05913d 758 pci_write_config_word(pci_dev, PCI_COMMAND, cmd);
1c1e45d1 759
3d05913d
AW
760 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &cx->card_rev);
761 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
1c1e45d1
HV
762
763 if (pci_latency < 64 && cx18_pci_latency) {
764 CX18_INFO("Unreasonably low latency timer, "
765 "setting to 64 (was %d)\n", pci_latency);
3d05913d
AW
766 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);
767 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
1c1e45d1 768 }
1c1e45d1
HV
769
770 CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
771 "irq: %d, latency: %d, memory: 0x%lx\n",
3d05913d
AW
772 cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,
773 PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),
774 cx->pci_dev->irq, pci_latency, (unsigned long)cx->base_addr);
1c1e45d1
HV
775
776 return 0;
777}
778
ff2a2001 779static void cx18_init_subdevs(struct cx18 *cx)
1c1e45d1
HV
780{
781 u32 hw = cx->card->hw_all;
ff2a2001 782 u32 device;
1c1e45d1
HV
783 int i;
784
ff2a2001 785 for (i = 0, device = 1; i < 32; i++, device <<= 1) {
1c1e45d1
HV
786
787 if (!(device & hw))
788 continue;
ff2a2001
AW
789
790 switch (device) {
ff2a2001
AW
791 case CX18_HW_DVB:
792 case CX18_HW_TVEEPROM:
793 /* These subordinate devices do not use probing */
1c1e45d1 794 cx->hw_flags |= device;
ff2a2001
AW
795 break;
796 case CX18_HW_418_AV:
797 /* The A/V decoder gets probed earlier to set PLLs */
798 /* Just note that the card uses it (i.e. has analog) */
1c1e45d1 799 cx->hw_flags |= device;
ff2a2001 800 break;
eefe1010
AW
801 case CX18_HW_GPIO_RESET_CTRL:
802 /*
803 * The Reset Controller gets probed and added to
804 * hw_flags earlier for i2c adapter/bus initialization
805 */
806 break;
807 case CX18_HW_GPIO_MUX:
808 if (cx18_gpio_register(cx, device) == 0)
809 cx->hw_flags |= device;
810 break;
ff2a2001
AW
811 default:
812 if (cx18_i2c_register(cx, i) == 0)
813 cx->hw_flags |= device;
814 break;
815 }
1c1e45d1
HV
816 }
817
ff2a2001
AW
818 if (cx->hw_flags & CX18_HW_418_AV)
819 cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV);
820
821 if (cx->card->hw_muxer != 0)
822 cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer);
1c1e45d1
HV
823}
824
3d05913d 825static int __devinit cx18_probe(struct pci_dev *pci_dev,
1c1e45d1
HV
826 const struct pci_device_id *pci_id)
827{
828 int retval = 0;
ff086575 829 int i;
1c1e45d1
HV
830 u32 devtype;
831 struct cx18 *cx;
832
5811cf99
AW
833 /* FIXME - module parameter arrays constrain max instances */
834 i = atomic_inc_return(&cx18_instance) - 1;
835 if (i >= CX18_MAX_CARDS) {
836 printk(KERN_ERR "cx18: cannot manage card %d, driver has a "
837 "limit of 0 - %d\n", i, CX18_MAX_CARDS - 1);
1c1e45d1
HV
838 return -ENOMEM;
839 }
840
841 cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
5811cf99
AW
842 if (cx == NULL) {
843 printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n",
844 i);
1c1e45d1
HV
845 return -ENOMEM;
846 }
888cdb07 847 cx->pci_dev = pci_dev;
5811cf99
AW
848 cx->instance = i;
849
888cdb07
AW
850 retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);
851 if (retval) {
5811cf99
AW
852 printk(KERN_ERR "cx18: v4l2_device_register of card %d failed"
853 "\n", cx->instance);
854 kfree(cx);
855 return retval;
888cdb07 856 }
5811cf99
AW
857 snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d",
858 cx->instance);
859 CX18_INFO("Initializing card %d\n", cx->instance);
888cdb07 860
1c1e45d1
HV
861 cx18_process_options(cx);
862 if (cx->options.cardtype == -1) {
863 retval = -ENODEV;
5811cf99 864 goto err;
1c1e45d1 865 }
87116159
AW
866
867 retval = cx18_init_struct1(cx);
868 if (retval)
5811cf99 869 goto err;
1c1e45d1
HV
870
871 CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);
872
873 /* PCI Device Setup */
3d05913d 874 retval = cx18_setup_pci(cx, pci_dev, pci_id);
572bfea7 875 if (retval != 0)
87116159 876 goto free_workqueues;
572bfea7 877
1c1e45d1
HV
878 /* map io memory */
879 CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
880 cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
881 cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
882 CX18_MEM_SIZE);
883 if (!cx->enc_mem) {
884 CX18_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
885 CX18_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
886 retval = -ENOMEM;
887 goto free_mem;
888 }
889 cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
b1526421 890 devtype = cx18_read_reg(cx, 0xC72028);
1c1e45d1
HV
891 switch (devtype & 0xff000000) {
892 case 0xff000000:
893 CX18_INFO("cx23418 revision %08x (A)\n", devtype);
894 break;
895 case 0x01000000:
896 CX18_INFO("cx23418 revision %08x (B)\n", devtype);
897 break;
898 default:
899 CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
900 break;
901 }
902
903 cx18_init_power(cx, 1);
904 cx18_init_memory(cx);
905
990c81c8 906 cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
1c1e45d1
HV
907 cx18_init_scb(cx);
908
909 cx18_gpio_init(cx);
910
ff2a2001
AW
911 /* Initialize integrated A/V decoder early to set PLLs, just in case */
912 retval = cx18_av_probe(cx);
fa3e7036
AW
913 if (retval) {
914 CX18_ERR("Could not register A/V decoder subdevice\n");
915 goto free_map;
916 }
cc26b076 917 cx18_call_hw(cx, CX18_HW_418_AV, core, init, 0);
fa3e7036 918
eefe1010
AW
919 /* Initialize GPIO Reset Controller to do chip resets during i2c init */
920 if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) {
921 if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0)
922 CX18_WARN("Could not register GPIO reset controller"
923 "subdevice; proceeding anyway.\n");
924 else
925 cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL;
926 }
927
1c1e45d1
HV
928 /* active i2c */
929 CX18_DEBUG_INFO("activating i2c...\n");
9b4a7c8a
AW
930 retval = init_cx18_i2c(cx);
931 if (retval) {
1c1e45d1
HV
932 CX18_ERR("Could not initialize i2c\n");
933 goto free_map;
934 }
935
1c1e45d1
HV
936 if (cx->card->hw_all & CX18_HW_TVEEPROM) {
937 /* Based on the model number the cardtype may be changed.
938 The PCI IDs are not always reliable. */
939 cx18_process_eeprom(cx);
940 }
941 if (cx->card->comment)
942 CX18_INFO("%s", cx->card->comment);
943 if (cx->card->v4l2_capabilities == 0) {
944 retval = -ENODEV;
945 goto free_i2c;
946 }
947 cx18_init_memory(cx);
fd6b9c97 948 cx18_init_scb(cx);
1c1e45d1
HV
949
950 /* Register IRQ */
3d05913d 951 retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
5811cf99
AW
952 IRQF_SHARED | IRQF_DISABLED,
953 cx->v4l2_dev.name, (void *)cx);
1c1e45d1
HV
954 if (retval) {
955 CX18_ERR("Failed to register irq %d\n", retval);
956 goto free_i2c;
957 }
958
959 if (cx->std == 0)
960 cx->std = V4L2_STD_NTSC_M;
961
962 if (cx->options.tuner == -1) {
1c1e45d1
HV
963 for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
964 if ((cx->std & cx->card->tuners[i].std) == 0)
965 continue;
966 cx->options.tuner = cx->card->tuners[i].tuner;
967 break;
968 }
969 }
970 /* if no tuner was found, then pick the first tuner in the card list */
971 if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
972 cx->std = cx->card->tuners[0].std;
c3cb4d95
HV
973 if (cx->std & V4L2_STD_PAL)
974 cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
975 else if (cx->std & V4L2_STD_NTSC)
976 cx->std = V4L2_STD_NTSC_M;
977 else if (cx->std & V4L2_STD_SECAM)
978 cx->std = V4L2_STD_SECAM_L;
1c1e45d1
HV
979 cx->options.tuner = cx->card->tuners[0].tuner;
980 }
981 if (cx->options.radio == -1)
982 cx->options.radio = (cx->card->radio_input.audio_type != 0);
983
984 /* The card is now fully identified, continue with card-specific
985 initialization. */
986 cx18_init_struct2(cx);
987
ff2a2001 988 cx18_init_subdevs(cx);
1c1e45d1 989
8d037ed1 990 if (cx->std & V4L2_STD_525_60)
1c1e45d1 991 cx->is_60hz = 1;
8d037ed1 992 else
1c1e45d1 993 cx->is_50hz = 1;
8d037ed1 994
1c1e45d1
HV
995 cx->params.video_gop_size = cx->is_60hz ? 15 : 12;
996
1c1e45d1
HV
997 if (cx->options.radio > 0)
998 cx->v4l2_cap |= V4L2_CAP_RADIO;
999
1c1e45d1
HV
1000 if (cx->options.tuner > -1) {
1001 struct tuner_setup setup;
1002
1003 setup.addr = ADDR_UNSET;
1004 setup.type = cx->options.tuner;
1005 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
1006 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
1007 cx18_reset_tuner_gpio : NULL;
ff2a2001 1008 cx18_call_all(cx, tuner, s_type_addr, &setup);
1c1e45d1
HV
1009 if (setup.type == TUNER_XC2028) {
1010 static struct xc2028_ctrl ctrl = {
1011 .fname = XC2028_DEFAULT_FIRMWARE,
1012 .max_len = 64,
1013 };
1014 struct v4l2_priv_tun_config cfg = {
1015 .tuner = cx->options.tuner,
1016 .priv = &ctrl,
1017 };
ff2a2001 1018 cx18_call_all(cx, tuner, s_config, &cfg);
1c1e45d1
HV
1019 }
1020 }
1021
1022 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
1023 are not. */
1024 cx->tuner_std = cx->std;
1025
5e7fdc5e
HV
1026 retval = cx18_streams_setup(cx);
1027 if (retval) {
1028 CX18_ERR("Error %d setting up streams\n", retval);
1029 goto free_irq;
1030 }
1031 retval = cx18_streams_register(cx);
1032 if (retval) {
1033 CX18_ERR("Error %d registering devices\n", retval);
1034 goto free_streams;
1035 }
1c1e45d1 1036
5811cf99 1037 CX18_INFO("Initialized card: %s\n", cx->card_name);
1c1e45d1
HV
1038 return 0;
1039
1040free_streams:
3f98387e 1041 cx18_streams_cleanup(cx, 1);
1c1e45d1 1042free_irq:
3d05913d 1043 free_irq(cx->pci_dev->irq, (void *)cx);
1c1e45d1
HV
1044free_i2c:
1045 exit_cx18_i2c(cx);
1046free_map:
1047 cx18_iounmap(cx);
1048free_mem:
1049 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
87116159 1050free_workqueues:
deed75ed 1051 destroy_workqueue(cx->in_work_queue);
87116159 1052 destroy_workqueue(cx->out_work_queue);
1c1e45d1
HV
1053err:
1054 if (retval == 0)
1055 retval = -ENODEV;
1056 CX18_ERR("Error %d on initialization\n", retval);
1057
5811cf99
AW
1058 v4l2_device_unregister(&cx->v4l2_dev);
1059 kfree(cx);
1c1e45d1
HV
1060 return retval;
1061}
1062
1063int cx18_init_on_first_open(struct cx18 *cx)
1064{
1065 int video_input;
1066 int fw_retry_count = 3;
1067 struct v4l2_frequency vf;
3b6fe58f
AW
1068 struct cx18_open_id fh;
1069
1070 fh.cx = cx;
1c1e45d1
HV
1071
1072 if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
1073 return -ENXIO;
1074
1075 if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
1076 return 0;
1077
1078 while (--fw_retry_count > 0) {
1079 /* load firmware */
1080 if (cx18_firmware_init(cx) == 0)
1081 break;
1082 if (fw_retry_count > 1)
1083 CX18_WARN("Retry loading firmware\n");
1084 }
1085
1086 if (fw_retry_count == 0) {
1087 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1088 return -ENXIO;
1089 }
1090 set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
1091
350145a4
AW
1092 /*
1093 * Init the firmware twice to work around a silicon bug
1094 * with the digital TS.
1095 *
1096 * The second firmware load requires us to normalize the APU state,
1097 * or the audio for the first analog capture will be badly incorrect.
1098 *
1099 * I can't seem to call APU_RESETAI and have it succeed without the
1100 * APU capturing audio, so we start and stop it here to do the reset
1101 */
1102
1103 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1104 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1105 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1106 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1c1e45d1
HV
1107
1108 fw_retry_count = 3;
1109 while (--fw_retry_count > 0) {
1110 /* load firmware */
1111 if (cx18_firmware_init(cx) == 0)
1112 break;
1113 if (fw_retry_count > 1)
1114 CX18_WARN("Retry loading firmware\n");
1115 }
1116
1117 if (fw_retry_count == 0) {
1118 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1119 return -ENXIO;
1120 }
1121
d5c02f6b
AW
1122 /*
1123 * The second firmware load requires us to normalize the APU state,
1124 * or the audio for the first analog capture will be badly incorrect.
1125 *
1126 * I can't seem to call APU_RESETAI and have it succeed without the
1127 * APU capturing audio, so we start and stop it here to do the reset
1128 */
1129
1130 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1131 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1132 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1133 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1134
fa3e7036 1135 /* Init the A/V decoder, if it hasn't been already */
cc26b076 1136 v4l2_subdev_call(cx->sd_av, core, load_fw);
fa3e7036 1137
1c1e45d1
HV
1138 vf.tuner = 0;
1139 vf.type = V4L2_TUNER_ANALOG_TV;
1140 vf.frequency = 6400; /* the tuner 'baseline' frequency */
1141
1142 /* Set initial frequency. For PAL/SECAM broadcasts no
1143 'default' channel exists AFAIK. */
1144 if (cx->std == V4L2_STD_NTSC_M_JP)
1145 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
1146 else if (cx->std & V4L2_STD_NTSC_M)
1147 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
1148
1149 video_input = cx->active_input;
1150 cx->active_input++; /* Force update of input */
3b6fe58f 1151 cx18_s_input(NULL, &fh, video_input);
1c1e45d1
HV
1152
1153 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1154 in one place. */
1155 cx->std++; /* Force full standard initialization */
3b6fe58f
AW
1156 cx18_s_std(NULL, &fh, &cx->tuner_std);
1157 cx18_s_frequency(NULL, &fh, &vf);
1c1e45d1
HV
1158 return 0;
1159}
1160
deed75ed 1161static void cx18_cancel_in_work_orders(struct cx18 *cx)
18b5dc2e
AW
1162{
1163 int i;
deed75ed
AW
1164 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++)
1165 cancel_work_sync(&cx->in_work_order[i].work);
18b5dc2e
AW
1166}
1167
21a278b8
AW
1168static void cx18_cancel_out_work_orders(struct cx18 *cx)
1169{
1170 int i;
1171 for (i = 0; i < CX18_MAX_STREAMS; i++)
1172 if (&cx->streams[i].video_dev != NULL)
1173 cancel_work_sync(&cx->streams[i].out_work_order);
1174}
1175
1c1e45d1
HV
1176static void cx18_remove(struct pci_dev *pci_dev)
1177{
888cdb07 1178 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
5811cf99 1179 struct cx18 *cx = to_cx18(v4l2_dev);
6da6bf5e 1180 int i;
1c1e45d1 1181
5811cf99 1182 CX18_DEBUG_INFO("Removing Card\n");
1c1e45d1
HV
1183
1184 /* Stop all captures */
1185 CX18_DEBUG_INFO("Stopping all streams\n");
31554ae5 1186 if (atomic_read(&cx->tot_capturing) > 0)
1c1e45d1
HV
1187 cx18_stop_all_captures(cx);
1188
87116159 1189 /* Stop interrupts that cause incoming work to be queued */
b1526421 1190 cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
87116159
AW
1191
1192 /* Incoming work can cause outgoing work, so clean up incoming first */
1193 cx18_cancel_in_work_orders(cx);
21a278b8 1194 cx18_cancel_out_work_orders(cx);
87116159
AW
1195
1196 /* Stop ack interrupts that may have been needed for work to finish */
b1526421 1197 cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
1c1e45d1
HV
1198
1199 cx18_halt_firmware(cx);
1200
deed75ed 1201 destroy_workqueue(cx->in_work_queue);
87116159 1202 destroy_workqueue(cx->out_work_queue);
572bfea7 1203
3f98387e 1204 cx18_streams_cleanup(cx, 1);
1c1e45d1
HV
1205
1206 exit_cx18_i2c(cx);
1207
3d05913d 1208 free_irq(cx->pci_dev->irq, (void *)cx);
1c1e45d1 1209
cba627a5 1210 cx18_iounmap(cx);
1c1e45d1
HV
1211
1212 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1213
3d05913d 1214 pci_disable_device(cx->pci_dev);
6da6bf5e
AW
1215
1216 if (cx->vbi.sliced_mpeg_data[0] != NULL)
1217 for (i = 0; i < CX18_VBI_FRAMES; i++)
1218 kfree(cx->vbi.sliced_mpeg_data[i]);
1c1e45d1 1219
5811cf99 1220 CX18_INFO("Removed %s\n", cx->card_name);
888cdb07 1221
5811cf99
AW
1222 v4l2_device_unregister(v4l2_dev);
1223 kfree(cx);
1c1e45d1
HV
1224}
1225
1226/* define a pci_driver for card detection */
1227static struct pci_driver cx18_pci_driver = {
1228 .name = "cx18",
1229 .id_table = cx18_pci_tbl,
1230 .probe = cx18_probe,
1231 .remove = cx18_remove,
1232};
1233
9710e7a7 1234static int __init module_start(void)
1c1e45d1
HV
1235{
1236 printk(KERN_INFO "cx18: Start initialization, version %s\n", CX18_VERSION);
1237
1c1e45d1
HV
1238 /* Validate parameters */
1239 if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
dd89601d 1240 printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n",
1c1e45d1
HV
1241 CX18_MAX_CARDS - 1);
1242 return -1;
1243 }
1244
1245 if (cx18_debug < 0 || cx18_debug > 511) {
1246 cx18_debug = 0;
1247 printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");
1248 }
1249
1250 if (pci_register_driver(&cx18_pci_driver)) {
1251 printk(KERN_ERR "cx18: Error detecting PCI card\n");
1252 return -ENODEV;
1253 }
1254 printk(KERN_INFO "cx18: End initialization\n");
1255 return 0;
1256}
1257
9710e7a7 1258static void __exit module_cleanup(void)
1c1e45d1 1259{
1c1e45d1 1260 pci_unregister_driver(&cx18_pci_driver);
1c1e45d1
HV
1261}
1262
1263module_init(module_start);
1264module_exit(module_cleanup);