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