2 * cx18 driver initialization and card probing
4 * Derived from ivtv-driver.c
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
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.
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.
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
25 #include "cx18-driver.h"
27 #include "cx18-version.h"
28 #include "cx18-cards.h"
31 #include "cx18-gpio.h"
32 #include "cx18-firmware.h"
33 #include "cx18-streams.h"
34 #include "cx18-av-core.h"
36 #include "cx18-mailbox.h"
37 #include "cx18-ioctl.h"
38 #include "tuner-xc2028.h"
40 #include <media/tveeprom.h>
43 /* var to keep track of the number of array elements in use */
44 int cx18_cards_active;
46 /* If you have already X v4l cards, then set this to X. This way
47 the device numbers stay matched. Example: you have a WinTV card
48 without radio and a Compro H900 with. Normally this would give a
49 video1 device together with a radio0 device for the Compro. By
50 setting this to 1 you ensure that radio0 is now also radio1. */
53 /* Master variable for all cx18 info */
54 struct cx18 *cx18_cards[CX18_MAX_CARDS];
56 /* Protects cx18_cards_active */
57 DEFINE_SPINLOCK(cx18_cards_lock);
59 /* add your revision and whatnot here */
60 static struct pci_device_id cx18_pci_tbl[] __devinitdata = {
61 {PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,
62 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
66 MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);
68 /* Parameter declarations */
69 static int cardtype[CX18_MAX_CARDS];
70 static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1 };
74 static int radio[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1,
76 -1, -1, -1, -1, -1, -1, -1, -1,
77 -1, -1, -1, -1, -1, -1, -1, -1 };
78 static unsigned cardtype_c = 1;
79 static unsigned tuner_c = 1;
80 static unsigned radio_c = 1;
81 static char pal[] = "--";
82 static char secam[] = "--";
83 static char ntsc[] = "-";
86 static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
87 static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
88 static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;
89 static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
90 static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
91 static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;
93 static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
94 static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
95 static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
96 static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
97 /* VBI bufsize based on standards supported by card tuner for now */
98 static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;
100 static int enc_ts_bufs = -1;
101 static int enc_mpg_bufs = -1;
102 static int enc_idx_bufs = -1;
103 static int enc_yuv_bufs = -1;
104 static int enc_vbi_bufs = -1;
105 static int enc_pcm_bufs = -1;
108 static int cx18_pci_latency = 1;
110 static int mmio_ndelay;
111 static int retry_mmio = 1;
115 module_param_array(tuner, int, &tuner_c, 0644);
116 module_param_array(radio, bool, &radio_c, 0644);
117 module_param_array(cardtype, int, &cardtype_c, 0644);
118 module_param_string(pal, pal, sizeof(pal), 0644);
119 module_param_string(secam, secam, sizeof(secam), 0644);
120 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
121 module_param_named(debug, cx18_debug, int, 0644);
122 module_param(mmio_ndelay, int, 0644);
123 module_param(retry_mmio, int, 0644);
124 module_param(cx18_pci_latency, int, 0644);
125 module_param(cx18_first_minor, int, 0644);
127 module_param(enc_ts_buffers, int, 0644);
128 module_param(enc_mpg_buffers, int, 0644);
129 module_param(enc_idx_buffers, int, 0644);
130 module_param(enc_yuv_buffers, int, 0644);
131 module_param(enc_vbi_buffers, int, 0644);
132 module_param(enc_pcm_buffers, int, 0644);
134 module_param(enc_ts_bufsize, int, 0644);
135 module_param(enc_mpg_bufsize, int, 0644);
136 module_param(enc_idx_bufsize, int, 0644);
137 module_param(enc_yuv_bufsize, int, 0644);
138 /* VBI bufsize based on standards supported by card tuner for now */
139 module_param(enc_pcm_bufsize, int, 0644);
141 module_param(enc_ts_bufs, int, 0644);
142 module_param(enc_mpg_bufs, int, 0644);
143 module_param(enc_idx_bufs, int, 0644);
144 module_param(enc_yuv_bufs, int, 0644);
145 module_param(enc_vbi_bufs, int, 0644);
146 module_param(enc_pcm_bufs, int, 0644);
148 MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
149 "\t\t\tsee tuner.h for values");
150 MODULE_PARM_DESC(radio,
151 "Enable or disable the radio. Use only if autodetection\n"
152 "\t\t\tfails. 0 = disable, 1 = enable");
153 MODULE_PARM_DESC(cardtype,
154 "Only use this option if your card is not detected properly.\n"
155 "\t\tSpecify card type:\n"
156 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
157 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
158 "\t\t\t 3 = Compro VideoMate H900\n"
159 "\t\t\t 4 = Yuan MPC718\n"
160 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
161 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
162 "\t\t\t 7 = Leadtek WinFast PVR2100/DVR3100 H\n"
163 "\t\t\t 0 = Autodetect (default)\n"
164 "\t\t\t-1 = Ignore this card\n\t\t");
165 MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
166 MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
167 MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
168 MODULE_PARM_DESC(debug,
169 "Debug level (bitmask). Default: 0\n"
170 "\t\t\t 1/0x0001: warning\n"
171 "\t\t\t 2/0x0002: info\n"
172 "\t\t\t 4/0x0004: mailbox\n"
173 "\t\t\t 8/0x0008: dma\n"
174 "\t\t\t 16/0x0010: ioctl\n"
175 "\t\t\t 32/0x0020: file\n"
176 "\t\t\t 64/0x0040: i2c\n"
177 "\t\t\t128/0x0080: irq\n"
178 "\t\t\t256/0x0100: high volume\n");
179 MODULE_PARM_DESC(cx18_pci_latency,
180 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
181 "\t\t\tDefault: Yes");
182 MODULE_PARM_DESC(retry_mmio,
183 "(Deprecated) MMIO writes are now always checked and retried\n"
184 "\t\t\tEffectively: 1 [Yes]");
185 MODULE_PARM_DESC(mmio_ndelay,
186 "(Deprecated) MMIO accesses are now never purposely delayed\n"
187 "\t\t\tEffectively: 0 ns");
188 MODULE_PARM_DESC(enc_ts_buffers,
189 "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"
190 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
191 MODULE_PARM_DESC(enc_ts_bufsize,
192 "Size of an encoder TS buffer (kB)\n"
193 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));
194 MODULE_PARM_DESC(enc_ts_bufs,
195 "Number of encoder TS buffers\n"
196 "\t\t\tDefault is computed from other enc_ts_* parameters");
197 MODULE_PARM_DESC(enc_mpg_buffers,
198 "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"
199 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
200 MODULE_PARM_DESC(enc_mpg_bufsize,
201 "Size of an encoder MPG buffer (kB)\n"
202 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));
203 MODULE_PARM_DESC(enc_mpg_bufs,
204 "Number of encoder MPG buffers\n"
205 "\t\t\tDefault is computed from other enc_mpg_* parameters");
206 MODULE_PARM_DESC(enc_idx_buffers,
207 "Encoder IDX buffer memory (MB). (enc_idx_bufs can override)\n"
208 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFFERS));
209 MODULE_PARM_DESC(enc_idx_bufsize,
210 "Size of an encoder IDX buffer (kB)\n"
211 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFSIZE));
212 MODULE_PARM_DESC(enc_idx_bufs,
213 "Number of encoder IDX buffers\n"
214 "\t\t\tDefault is computed from other enc_idx_* parameters");
215 MODULE_PARM_DESC(enc_yuv_buffers,
216 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
217 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
218 MODULE_PARM_DESC(enc_yuv_bufsize,
219 "Size of an encoder YUV buffer (kB)\n"
220 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFSIZE));
221 MODULE_PARM_DESC(enc_yuv_bufs,
222 "Number of encoder YUV buffers\n"
223 "\t\t\tDefault is computed from other enc_yuv_* parameters");
224 MODULE_PARM_DESC(enc_vbi_buffers,
225 "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"
226 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
227 MODULE_PARM_DESC(enc_vbi_bufs,
228 "Number of encoder VBI buffers\n"
229 "\t\t\tDefault is computed from enc_vbi_buffers & tuner std");
230 MODULE_PARM_DESC(enc_pcm_buffers,
231 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
232 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
233 MODULE_PARM_DESC(enc_pcm_bufsize,
234 "Size of an encoder PCM buffer (kB)\n"
235 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));
236 MODULE_PARM_DESC(enc_pcm_bufs,
237 "Number of encoder PCM buffers\n"
238 "\t\t\tDefault is computed from other enc_pcm_* parameters");
240 MODULE_PARM_DESC(cx18_first_minor, "Set kernel number assigned to first card");
242 MODULE_AUTHOR("Hans Verkuil");
243 MODULE_DESCRIPTION("CX23418 driver");
244 MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
245 MODULE_LICENSE("GPL");
247 MODULE_VERSION(CX18_VERSION);
249 /* Generic utility functions */
250 int cx18_msleep_timeout(unsigned int msecs, int intr)
252 long int timeout = msecs_to_jiffies(msecs);
256 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
257 timeout = schedule_timeout(timeout);
258 sig = intr ? signal_pending(current) : 0;
259 } while (!sig && timeout);
263 /* Release ioremapped memory */
264 static void cx18_iounmap(struct cx18 *cx)
269 /* Release io memory */
270 if (cx->enc_mem != NULL) {
271 CX18_DEBUG_INFO("releasing enc_mem\n");
272 iounmap(cx->enc_mem);
277 /* Hauppauge card? get values from tveeprom */
278 void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
282 cx->i2c_client[0].addr = 0xA0 >> 1;
283 tveeprom_read(&cx->i2c_client[0], eedata, sizeof(eedata));
284 tveeprom_hauppauge_analog(&cx->i2c_client[0], tv, eedata);
287 static void cx18_process_eeprom(struct cx18 *cx)
291 cx18_read_eeprom(cx, &tv);
293 /* Many thanks to Steven Toth from Hauppauge for providing the
295 /* Note: the Samsung memory models cannot be reliably determined
296 from the model number. Use the cardtype module option if you
297 have one of these preproduction models. */
299 case 74000 ... 74999:
300 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
303 CX18_ERR("Invalid EEPROM\n");
306 CX18_ERR("Unknown model %d, defaulting to HVR-1600\n", tv.model);
307 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
311 cx->v4l2_cap = cx->card->v4l2_capabilities;
312 cx->card_name = cx->card->name;
313 cx->card_i2c = cx->card->i2c;
315 CX18_INFO("Autodetected %s\n", cx->card_name);
317 if (tv.tuner_type == TUNER_ABSENT)
318 CX18_ERR("tveeprom cannot autodetect tuner!");
320 if (cx->options.tuner == -1)
321 cx->options.tuner = tv.tuner_type;
322 if (cx->options.radio == -1)
323 cx->options.radio = (tv.has_radio != 0);
326 /* user specified tuner standard */
329 /* autodetect tuner standard */
330 if (tv.tuner_formats & V4L2_STD_PAL) {
331 CX18_DEBUG_INFO("PAL tuner detected\n");
332 cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
333 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
334 CX18_DEBUG_INFO("NTSC tuner detected\n");
335 cx->std |= V4L2_STD_NTSC_M;
336 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
337 CX18_DEBUG_INFO("SECAM tuner detected\n");
338 cx->std |= V4L2_STD_SECAM_L;
340 CX18_INFO("No tuner detected, default to NTSC-M\n");
341 cx->std |= V4L2_STD_NTSC_M;
345 static v4l2_std_id cx18_parse_std(struct cx18 *cx)
349 return V4L2_STD_PAL_60;
354 return V4L2_STD_PAL_BG;
357 return V4L2_STD_PAL_H;
360 if (pal[1] == 'c' || pal[1] == 'C')
361 return V4L2_STD_PAL_Nc;
362 return V4L2_STD_PAL_N;
365 return V4L2_STD_PAL_I;
370 return V4L2_STD_PAL_DK;
373 return V4L2_STD_PAL_M;
377 CX18_WARN("pal= argument not recognised\n");
388 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
393 return V4L2_STD_SECAM_DK;
396 if (secam[1] == 'C' || secam[1] == 'c')
397 return V4L2_STD_SECAM_LC;
398 return V4L2_STD_SECAM_L;
402 CX18_WARN("secam= argument not recognised\n");
409 return V4L2_STD_NTSC_M;
412 return V4L2_STD_NTSC_M_JP;
415 return V4L2_STD_NTSC_M_KR;
419 CX18_WARN("ntsc= argument not recognised\n");
427 static void cx18_process_options(struct cx18 *cx)
431 cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
432 cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
433 cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;
434 cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
435 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
436 cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
437 cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */
439 cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;
440 cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;
441 cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;
442 cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;
443 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;
444 cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;
445 cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */
447 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;
448 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
449 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
450 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
451 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = 0; /* computed later */
452 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
453 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
455 /* Except for VBI ensure stream_buffers & stream_buf_size are valid */
456 for (i = 0; i < CX18_MAX_STREAMS; i++) {
457 /* User said to use 0 buffers */
458 if (cx->stream_buffers[i] == 0) {
459 cx->options.megabytes[i] = 0;
460 cx->stream_buf_size[i] = 0;
463 /* User said to use 0 MB total */
464 if (cx->options.megabytes[i] <= 0) {
465 cx->options.megabytes[i] = 0;
466 cx->stream_buffers[i] = 0;
467 cx->stream_buf_size[i] = 0;
470 /* VBI is computed later or user said buffer has size 0 */
471 if (cx->stream_buf_size[i] <= 0) {
472 if (i != CX18_ENC_STREAM_TYPE_VBI) {
473 cx->options.megabytes[i] = 0;
474 cx->stream_buffers[i] = 0;
475 cx->stream_buf_size[i] = 0;
479 if (cx->stream_buffers[i] < 0) {
480 cx->stream_buffers[i] = cx->options.megabytes[i] * 1024
481 / cx->stream_buf_size[i];
483 /* N.B. This might round down to 0 */
484 cx->options.megabytes[i] =
485 cx->stream_buffers[i] * cx->stream_buf_size[i] / 1024;
487 cx->stream_buf_size[i] *= 1024; /* convert from kB to bytes */
490 cx->options.cardtype = cardtype[cx->num];
491 cx->options.tuner = tuner[cx->num];
492 cx->options.radio = radio[cx->num];
494 cx->std = cx18_parse_std(cx);
495 if (cx->options.cardtype == -1) {
496 CX18_INFO("Ignore card\n");
499 cx->card = cx18_get_card(cx->options.cardtype - 1);
501 CX18_INFO("User specified %s card\n", cx->card->name);
502 else if (cx->options.cardtype != 0)
503 CX18_ERR("Unknown user specified type, trying to autodetect card\n");
504 if (cx->card == NULL) {
505 if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
506 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
507 CX18_INFO("Autodetected Hauppauge card\n");
510 if (cx->card == NULL) {
511 for (i = 0; (cx->card = cx18_get_card(i)); i++) {
512 if (cx->card->pci_list == NULL)
514 for (j = 0; cx->card->pci_list[j].device; j++) {
515 if (cx->pci_dev->device !=
516 cx->card->pci_list[j].device)
518 if (cx->pci_dev->subsystem_vendor !=
519 cx->card->pci_list[j].subsystem_vendor)
521 if (cx->pci_dev->subsystem_device !=
522 cx->card->pci_list[j].subsystem_device)
524 CX18_INFO("Autodetected %s card\n", cx->card->name);
531 if (cx->card == NULL) {
532 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
533 CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
534 cx->pci_dev->vendor, cx->pci_dev->device);
535 CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n",
536 cx->pci_dev->subsystem_vendor,
537 cx->pci_dev->subsystem_device);
538 CX18_ERR("Defaulting to %s card\n", cx->card->name);
539 CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
540 CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
541 CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
543 cx->v4l2_cap = cx->card->v4l2_capabilities;
544 cx->card_name = cx->card->name;
545 cx->card_i2c = cx->card->i2c;
548 /* Precondition: the cx18 structure has been memset to 0. Only
549 the dev and num fields have been filled in.
550 No assumptions on the card type may be made here (see cx18_init_struct2
553 static int __devinit cx18_init_struct1(struct cx18 *cx)
557 cx->base_addr = pci_resource_start(cx->pci_dev, 0);
559 mutex_init(&cx->serialize_lock);
560 mutex_init(&cx->i2c_bus_lock[0]);
561 mutex_init(&cx->i2c_bus_lock[1]);
562 mutex_init(&cx->gpio_lock);
563 mutex_init(&cx->epu2apu_mb_lock);
564 mutex_init(&cx->epu2cpu_mb_lock);
566 cx->work_queue = create_singlethread_workqueue(cx->name);
567 if (cx->work_queue == NULL) {
568 CX18_ERR("Unable to create work hander thread\n");
572 for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++) {
573 cx->epu_work_order[i].cx = cx;
574 cx->epu_work_order[i].str = cx->epu_debug_str;
575 INIT_WORK(&cx->epu_work_order[i].work, cx18_epu_work_handler);
578 /* start counting open_id at 1 */
581 /* Initial settings */
582 cx2341x_fill_defaults(&cx->params);
583 cx->temporal_strength = cx->params.video_temporal_filter;
584 cx->spatial_strength = cx->params.video_spatial_filter;
585 cx->filter_mode = cx->params.video_spatial_filter_mode |
586 (cx->params.video_temporal_filter_mode << 1) |
587 (cx->params.video_median_filter_type << 2);
588 cx->params.port = CX2341X_PORT_MEMORY;
589 cx->params.capabilities =
590 CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_AC3 | CX2341X_CAP_HAS_SLICED_VBI;
591 init_waitqueue_head(&cx->cap_w);
592 init_waitqueue_head(&cx->mb_apu_waitq);
593 init_waitqueue_head(&cx->mb_cpu_waitq);
594 init_waitqueue_head(&cx->dma_waitq);
597 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
598 cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
603 /* Second initialization part. Here the card type has been
605 static void __devinit cx18_init_struct2(struct cx18 *cx)
609 for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
610 if (cx->card->video_inputs[i].video_type == 0)
613 for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
614 if (cx->card->audio_inputs[i].audio_type == 0)
616 cx->nof_audio_inputs = i;
618 /* Find tuner input */
619 for (i = 0; i < cx->nof_inputs; i++) {
620 if (cx->card->video_inputs[i].video_type ==
621 CX18_CARD_INPUT_VID_TUNER)
624 if (i == cx->nof_inputs)
626 cx->active_input = i;
627 cx->audio_input = cx->card->video_inputs[i].audio_index;
628 cx->av_state.vid_input = CX18_AV_COMPOSITE7;
629 cx->av_state.aud_input = CX18_AV_AUDIO8;
630 cx->av_state.audclk_freq = 48000;
631 cx->av_state.audmode = V4L2_TUNER_MODE_LANG1;
632 cx->av_state.vbi_line_offset = 8;
635 static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
636 const struct pci_device_id *pci_id)
639 unsigned char pci_latency;
641 CX18_DEBUG_INFO("Enabling pci device\n");
643 if (pci_enable_device(pci_dev)) {
644 CX18_ERR("Can't enable device %d!\n", cx->num);
647 if (pci_set_dma_mask(pci_dev, 0xffffffff)) {
648 CX18_ERR("No suitable DMA available on card %d.\n", cx->num);
651 if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
652 CX18_ERR("Cannot request encoder memory region on card %d.\n", cx->num);
656 /* Enable bus mastering and memory mapped IO for the CX23418 */
657 pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);
658 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
659 pci_write_config_word(pci_dev, PCI_COMMAND, cmd);
661 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &cx->card_rev);
662 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
664 if (pci_latency < 64 && cx18_pci_latency) {
665 CX18_INFO("Unreasonably low latency timer, "
666 "setting to 64 (was %d)\n", pci_latency);
667 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);
668 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
671 CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
672 "irq: %d, latency: %d, memory: 0x%lx\n",
673 cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,
674 PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),
675 cx->pci_dev->irq, pci_latency, (unsigned long)cx->base_addr);
681 static u32 cx18_request_module(struct cx18 *cx, u32 hw,
682 const char *name, u32 id)
686 if (request_module("%s", name) != 0) {
687 CX18_ERR("Failed to load module %s\n", name);
690 CX18_DEBUG_INFO("Loaded module %s\n", name);
695 static void cx18_load_and_init_modules(struct cx18 *cx)
697 u32 hw = cx->card->hw_all;
702 #ifdef CONFIG_MEDIA_TUNER_MODULE
703 hw = cx18_request_module(cx, hw, "tuner", CX18_HW_TUNER);
705 #ifdef CONFIG_VIDEO_CS5345_MODULE
706 hw = cx18_request_module(cx, hw, "cs5345", CX18_HW_CS5345);
710 /* check which i2c devices are actually found */
711 for (i = 0; i < 32; i++) {
716 if (device == CX18_HW_GPIO || device == CX18_HW_TVEEPROM ||
717 device == CX18_HW_CX23418 || device == CX18_HW_DVB) {
718 /* These 'devices' do not use i2c probing */
719 cx->hw_flags |= device;
722 cx18_i2c_register(cx, i);
723 if (cx18_i2c_hw_addr(cx, device) > 0)
724 cx->hw_flags |= device;
730 static int __devinit cx18_probe(struct pci_dev *pci_dev,
731 const struct pci_device_id *pci_id)
739 spin_lock(&cx18_cards_lock);
741 /* Make sure we've got a place for this card */
742 if (cx18_cards_active == CX18_MAX_CARDS) {
743 printk(KERN_ERR "cx18: Maximum number of cards detected (%d).\n",
745 spin_unlock(&cx18_cards_lock);
749 cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
751 spin_unlock(&cx18_cards_lock);
754 cx18_cards[cx18_cards_active] = cx;
755 cx->num = cx18_cards_active++;
756 snprintf(cx->name, sizeof(cx->name), "cx18-%d", cx->num);
757 CX18_INFO("Initializing card #%d\n", cx->num);
759 spin_unlock(&cx18_cards_lock);
761 cx->pci_dev = pci_dev;
762 retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);
764 CX18_ERR("Call to v4l2_device_register() failed\n");
767 CX18_DEBUG_INFO("registered v4l2_device name: %s\n", cx->v4l2_dev.name);
769 cx18_process_options(cx);
770 if (cx->options.cardtype == -1) {
772 goto unregister_v4l2;
774 if (cx18_init_struct1(cx)) {
776 goto unregister_v4l2;
779 CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);
781 /* PCI Device Setup */
782 retval = cx18_setup_pci(cx, pci_dev, pci_id);
787 CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
788 cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
789 cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
792 CX18_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
793 CX18_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
797 cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
798 devtype = cx18_read_reg(cx, 0xC72028);
799 switch (devtype & 0xff000000) {
801 CX18_INFO("cx23418 revision %08x (A)\n", devtype);
804 CX18_INFO("cx23418 revision %08x (B)\n", devtype);
807 CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
811 cx18_init_power(cx, 1);
812 cx18_init_memory(cx);
814 cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
820 CX18_DEBUG_INFO("activating i2c...\n");
821 retval = init_cx18_i2c(cx);
823 CX18_ERR("Could not initialize i2c\n");
827 CX18_DEBUG_INFO("Active card count: %d.\n", cx18_cards_active);
829 if (cx->card->hw_all & CX18_HW_TVEEPROM) {
830 /* Based on the model number the cardtype may be changed.
831 The PCI IDs are not always reliable. */
832 cx18_process_eeprom(cx);
834 if (cx->card->comment)
835 CX18_INFO("%s", cx->card->comment);
836 if (cx->card->v4l2_capabilities == 0) {
840 cx18_init_memory(cx);
844 retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
845 IRQF_SHARED | IRQF_DISABLED, cx->name, (void *)cx);
847 CX18_ERR("Failed to register irq %d\n", retval);
852 cx->std = V4L2_STD_NTSC_M;
854 if (cx->options.tuner == -1) {
855 for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
856 if ((cx->std & cx->card->tuners[i].std) == 0)
858 cx->options.tuner = cx->card->tuners[i].tuner;
862 /* if no tuner was found, then pick the first tuner in the card list */
863 if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
864 cx->std = cx->card->tuners[0].std;
865 if (cx->std & V4L2_STD_PAL)
866 cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
867 else if (cx->std & V4L2_STD_NTSC)
868 cx->std = V4L2_STD_NTSC_M;
869 else if (cx->std & V4L2_STD_SECAM)
870 cx->std = V4L2_STD_SECAM_L;
871 cx->options.tuner = cx->card->tuners[0].tuner;
873 if (cx->options.radio == -1)
874 cx->options.radio = (cx->card->radio_input.audio_type != 0);
876 /* The card is now fully identified, continue with card-specific
878 cx18_init_struct2(cx);
880 cx18_load_and_init_modules(cx);
882 if (cx->std & V4L2_STD_525_60) {
889 cx->params.video_gop_size = cx->is_60hz ? 15 : 12;
892 * FIXME: setting the buffer size based on the tuner standard is
893 * suboptimal, as the CVBS and SVideo inputs could use a different std
894 * and the buffer could end up being too small in that case.
896 vbi_buf_size = vbi_active_samples * (cx->is_60hz ? 24 : 36) / 2;
897 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
899 if (cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] < 0)
900 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] =
901 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] * 1024 * 1024
904 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] =
905 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] * vbi_buf_size
908 if (cx->options.radio > 0)
909 cx->v4l2_cap |= V4L2_CAP_RADIO;
911 if (cx->options.tuner > -1) {
912 struct tuner_setup setup;
914 setup.addr = ADDR_UNSET;
915 setup.type = cx->options.tuner;
916 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
917 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
918 cx18_reset_tuner_gpio : NULL;
919 cx18_call_i2c_clients(cx, TUNER_SET_TYPE_ADDR, &setup);
920 if (setup.type == TUNER_XC2028) {
921 static struct xc2028_ctrl ctrl = {
922 .fname = XC2028_DEFAULT_FIRMWARE,
925 struct v4l2_priv_tun_config cfg = {
926 .tuner = cx->options.tuner,
929 cx18_call_i2c_clients(cx, TUNER_SET_CONFIG, &cfg);
933 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
935 cx->tuner_std = cx->std;
937 retval = cx18_streams_setup(cx);
939 CX18_ERR("Error %d setting up streams\n", retval);
942 retval = cx18_streams_register(cx);
944 CX18_ERR("Error %d registering devices\n", retval);
948 CX18_INFO("Initialized card #%d: %s\n", cx->num, cx->card_name);
953 cx18_streams_cleanup(cx, 1);
955 free_irq(cx->pci_dev->irq, (void *)cx);
961 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
963 destroy_workqueue(cx->work_queue);
965 v4l2_device_unregister(&cx->v4l2_dev);
969 CX18_ERR("Error %d on initialization\n", retval);
972 spin_lock(&cx18_cards_lock);
973 kfree(cx18_cards[i]);
974 cx18_cards[i] = NULL;
975 spin_unlock(&cx18_cards_lock);
979 int cx18_init_on_first_open(struct cx18 *cx)
982 int fw_retry_count = 3;
983 struct v4l2_frequency vf;
984 struct cx18_open_id fh;
988 if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
991 if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
994 while (--fw_retry_count > 0) {
996 if (cx18_firmware_init(cx) == 0)
998 if (fw_retry_count > 1)
999 CX18_WARN("Retry loading firmware\n");
1002 if (fw_retry_count == 0) {
1003 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1006 set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
1009 * Init the firmware twice to work around a silicon bug
1010 * with the digital TS.
1012 * The second firmware load requires us to normalize the APU state,
1013 * or the audio for the first analog capture will be badly incorrect.
1015 * I can't seem to call APU_RESETAI and have it succeed without the
1016 * APU capturing audio, so we start and stop it here to do the reset
1019 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1020 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1021 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1022 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1025 while (--fw_retry_count > 0) {
1027 if (cx18_firmware_init(cx) == 0)
1029 if (fw_retry_count > 1)
1030 CX18_WARN("Retry loading firmware\n");
1033 if (fw_retry_count == 0) {
1034 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1039 * The second firmware load requires us to normalize the APU state,
1040 * or the audio for the first analog capture will be badly incorrect.
1042 * I can't seem to call APU_RESETAI and have it succeed without the
1043 * APU capturing audio, so we start and stop it here to do the reset
1046 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1047 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1048 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1049 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1052 vf.type = V4L2_TUNER_ANALOG_TV;
1053 vf.frequency = 6400; /* the tuner 'baseline' frequency */
1055 /* Set initial frequency. For PAL/SECAM broadcasts no
1056 'default' channel exists AFAIK. */
1057 if (cx->std == V4L2_STD_NTSC_M_JP)
1058 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
1059 else if (cx->std & V4L2_STD_NTSC_M)
1060 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
1062 video_input = cx->active_input;
1063 cx->active_input++; /* Force update of input */
1064 cx18_s_input(NULL, &fh, video_input);
1066 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1068 cx->std++; /* Force full standard initialization */
1069 cx18_s_std(NULL, &fh, &cx->tuner_std);
1070 cx18_s_frequency(NULL, &fh, &vf);
1074 static void cx18_cancel_epu_work_orders(struct cx18 *cx)
1077 for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++)
1078 cancel_work_sync(&cx->epu_work_order[i].work);
1081 static void cx18_remove(struct pci_dev *pci_dev)
1083 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1084 struct cx18 *cx = container_of(v4l2_dev, struct cx18, v4l2_dev);
1086 CX18_DEBUG_INFO("Removing Card #%d\n", cx->num);
1088 /* Stop all captures */
1089 CX18_DEBUG_INFO("Stopping all streams\n");
1090 if (atomic_read(&cx->tot_capturing) > 0)
1091 cx18_stop_all_captures(cx);
1094 cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
1095 cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
1097 cx18_halt_firmware(cx);
1099 cx18_cancel_epu_work_orders(cx);
1101 destroy_workqueue(cx->work_queue);
1103 cx18_streams_cleanup(cx, 1);
1107 free_irq(cx->pci_dev->irq, (void *)cx);
1111 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1113 pci_disable_device(cx->pci_dev);
1115 v4l2_device_unregister(v4l2_dev);
1117 CX18_INFO("Removed %s, card #%d\n", cx->card_name, cx->num);
1120 /* define a pci_driver for card detection */
1121 static struct pci_driver cx18_pci_driver = {
1123 .id_table = cx18_pci_tbl,
1124 .probe = cx18_probe,
1125 .remove = cx18_remove,
1128 static int module_start(void)
1130 printk(KERN_INFO "cx18: Start initialization, version %s\n", CX18_VERSION);
1132 memset(cx18_cards, 0, sizeof(cx18_cards));
1134 /* Validate parameters */
1135 if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
1136 printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n",
1137 CX18_MAX_CARDS - 1);
1141 if (cx18_debug < 0 || cx18_debug > 511) {
1143 printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");
1146 if (pci_register_driver(&cx18_pci_driver)) {
1147 printk(KERN_ERR "cx18: Error detecting PCI card\n");
1150 printk(KERN_INFO "cx18: End initialization\n");
1154 static void module_cleanup(void)
1158 pci_unregister_driver(&cx18_pci_driver);
1160 for (i = 0; i < cx18_cards_active; i++) {
1161 if (cx18_cards[i] == NULL)
1163 kfree(cx18_cards[i]);
1168 module_init(module_start);
1169 module_exit(module_cleanup);