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