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