Merge branches 'pxa-ian' and 'pxa-xm270' into pxa
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / cx18 / cx18-driver.c
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24 #include "cx18-driver.h"
25 #include "cx18-version.h"
26 #include "cx18-cards.h"
27 #include "cx18-i2c.h"
28 #include "cx18-irq.h"
29 #include "cx18-gpio.h"
30 #include "cx18-firmware.h"
31 #include "cx18-streams.h"
32 #include "cx18-av-core.h"
33 #include "cx18-scb.h"
34 #include "cx18-mailbox.h"
35 #include "cx18-ioctl.h"
36 #include "tuner-xc2028.h"
37
38 #include <media/tveeprom.h>
39
40
41 /* var to keep track of the number of array elements in use */
42 int cx18_cards_active;
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 /* Master variable for all cx18 info */
52 struct cx18 *cx18_cards[CX18_MAX_CARDS];
53
54 /* Protects cx18_cards_active */
55 DEFINE_SPINLOCK(cx18_cards_lock);
56
57 /* add your revision and whatnot here */
58 static struct pci_device_id cx18_pci_tbl[] __devinitdata = {
59 {PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,
60 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
61 {0,}
62 };
63
64 MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);
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
77 static int cardtype_c = 1;
78 static int tuner_c = 1;
79 static int radio_c = 1;
80 static char pal[] = "--";
81 static char secam[] = "--";
82 static char ntsc[] = "-";
83
84 /* Buffers */
85 static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
86 static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_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 cx18_pci_latency = 1;
92
93 int cx18_debug;
94
95 module_param_array(tuner, int, &tuner_c, 0644);
96 module_param_array(radio, bool, &radio_c, 0644);
97 module_param_array(cardtype, int, &cardtype_c, 0644);
98 module_param_string(pal, pal, sizeof(pal), 0644);
99 module_param_string(secam, secam, sizeof(secam), 0644);
100 module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
101 module_param_named(debug, cx18_debug, int, 0644);
102 module_param(cx18_pci_latency, int, 0644);
103 module_param(cx18_first_minor, int, 0644);
104
105 module_param(enc_mpg_buffers, int, 0644);
106 module_param(enc_ts_buffers, int, 0644);
107 module_param(enc_yuv_buffers, int, 0644);
108 module_param(enc_vbi_buffers, int, 0644);
109 module_param(enc_pcm_buffers, int, 0644);
110
111 MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
112 "\t\t\tsee tuner.h for values");
113 MODULE_PARM_DESC(radio,
114 "Enable or disable the radio. Use only if autodetection\n"
115 "\t\t\tfails. 0 = disable, 1 = enable");
116 MODULE_PARM_DESC(cardtype,
117 "Only use this option if your card is not detected properly.\n"
118 "\t\tSpecify card type:\n"
119 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
120 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
121 "\t\t\t 3 = Compro VideoMate H900\n"
122 "\t\t\t 4 = Yuan MPC718\n"
123 "\t\t\t 0 = Autodetect (default)\n"
124 "\t\t\t-1 = Ignore this card\n\t\t");
125 MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
126 MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
127 MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
128 MODULE_PARM_DESC(debug,
129 "Debug level (bitmask). Default: 0\n"
130 "\t\t\t 1/0x0001: warning\n"
131 "\t\t\t 2/0x0002: info\n"
132 "\t\t\t 4/0x0004: mailbox\n"
133 "\t\t\t 8/0x0008: dma\n"
134 "\t\t\t 16/0x0010: ioctl\n"
135 "\t\t\t 32/0x0020: file\n"
136 "\t\t\t 64/0x0040: i2c\n"
137 "\t\t\t128/0x0080: irq\n"
138 "\t\t\t256/0x0100: high volume\n");
139 MODULE_PARM_DESC(cx18_pci_latency,
140 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
141 "\t\t\tDefault: Yes");
142 MODULE_PARM_DESC(enc_mpg_buffers,
143 "Encoder MPG Buffers (in MB)\n"
144 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
145 MODULE_PARM_DESC(enc_ts_buffers,
146 "Encoder TS Buffers (in MB)\n"
147 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
148 MODULE_PARM_DESC(enc_yuv_buffers,
149 "Encoder YUV Buffers (in MB)\n"
150 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
151 MODULE_PARM_DESC(enc_vbi_buffers,
152 "Encoder VBI Buffers (in MB)\n"
153 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
154 MODULE_PARM_DESC(enc_pcm_buffers,
155 "Encoder PCM buffers (in MB)\n"
156 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
157
158 MODULE_PARM_DESC(cx18_first_minor, "Set minor assigned to first card");
159
160 MODULE_AUTHOR("Hans Verkuil");
161 MODULE_DESCRIPTION("CX23418 driver");
162 MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
163 MODULE_LICENSE("GPL");
164
165 MODULE_VERSION(CX18_VERSION);
166
167 /* Generic utility functions */
168 int cx18_msleep_timeout(unsigned int msecs, int intr)
169 {
170 int timeout = msecs_to_jiffies(msecs);
171 int sig;
172
173 do {
174 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
175 timeout = schedule_timeout(timeout);
176 sig = intr ? signal_pending(current) : 0;
177 } while (!sig && timeout);
178 return sig;
179 }
180
181 /* Release ioremapped memory */
182 static void cx18_iounmap(struct cx18 *cx)
183 {
184 if (cx == NULL)
185 return;
186
187 /* Release io memory */
188 if (cx->enc_mem != NULL) {
189 CX18_DEBUG_INFO("releasing enc_mem\n");
190 iounmap(cx->enc_mem);
191 cx->enc_mem = NULL;
192 }
193 }
194
195 /* Hauppauge card? get values from tveeprom */
196 void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
197 {
198 u8 eedata[256];
199
200 cx->i2c_client[0].addr = 0xA0 >> 1;
201 tveeprom_read(&cx->i2c_client[0], eedata, sizeof(eedata));
202 tveeprom_hauppauge_analog(&cx->i2c_client[0], tv, eedata);
203 }
204
205 static void cx18_process_eeprom(struct cx18 *cx)
206 {
207 struct tveeprom tv;
208
209 cx18_read_eeprom(cx, &tv);
210
211 /* Many thanks to Steven Toth from Hauppauge for providing the
212 model numbers */
213 /* Note: the Samsung memory models cannot be reliably determined
214 from the model number. Use the cardtype module option if you
215 have one of these preproduction models. */
216 switch (tv.model) {
217 case 74000 ... 74999:
218 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
219 break;
220 case 0:
221 CX18_ERR("Invalid EEPROM\n");
222 return;
223 default:
224 CX18_ERR("Unknown model %d, defaulting to HVR-1600\n", tv.model);
225 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
226 break;
227 }
228
229 cx->v4l2_cap = cx->card->v4l2_capabilities;
230 cx->card_name = cx->card->name;
231 cx->card_i2c = cx->card->i2c;
232
233 CX18_INFO("Autodetected %s\n", cx->card_name);
234
235 if (tv.tuner_type == TUNER_ABSENT)
236 CX18_ERR("tveeprom cannot autodetect tuner!");
237
238 if (cx->options.tuner == -1)
239 cx->options.tuner = tv.tuner_type;
240 if (cx->options.radio == -1)
241 cx->options.radio = (tv.has_radio != 0);
242
243 if (cx->std != 0)
244 /* user specified tuner standard */
245 return;
246
247 /* autodetect tuner standard */
248 if (tv.tuner_formats & V4L2_STD_PAL) {
249 CX18_DEBUG_INFO("PAL tuner detected\n");
250 cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
251 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
252 CX18_DEBUG_INFO("NTSC tuner detected\n");
253 cx->std |= V4L2_STD_NTSC_M;
254 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
255 CX18_DEBUG_INFO("SECAM tuner detected\n");
256 cx->std |= V4L2_STD_SECAM_L;
257 } else {
258 CX18_INFO("No tuner detected, default to NTSC-M\n");
259 cx->std |= V4L2_STD_NTSC_M;
260 }
261 }
262
263 static v4l2_std_id cx18_parse_std(struct cx18 *cx)
264 {
265 switch (pal[0]) {
266 case '6':
267 return V4L2_STD_PAL_60;
268 case 'b':
269 case 'B':
270 case 'g':
271 case 'G':
272 return V4L2_STD_PAL_BG;
273 case 'h':
274 case 'H':
275 return V4L2_STD_PAL_H;
276 case 'n':
277 case 'N':
278 if (pal[1] == 'c' || pal[1] == 'C')
279 return V4L2_STD_PAL_Nc;
280 return V4L2_STD_PAL_N;
281 case 'i':
282 case 'I':
283 return V4L2_STD_PAL_I;
284 case 'd':
285 case 'D':
286 case 'k':
287 case 'K':
288 return V4L2_STD_PAL_DK;
289 case 'M':
290 case 'm':
291 return V4L2_STD_PAL_M;
292 case '-':
293 break;
294 default:
295 CX18_WARN("pal= argument not recognised\n");
296 return 0;
297 }
298
299 switch (secam[0]) {
300 case 'b':
301 case 'B':
302 case 'g':
303 case 'G':
304 case 'h':
305 case 'H':
306 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
307 case 'd':
308 case 'D':
309 case 'k':
310 case 'K':
311 return V4L2_STD_SECAM_DK;
312 case 'l':
313 case 'L':
314 if (secam[1] == 'C' || secam[1] == 'c')
315 return V4L2_STD_SECAM_LC;
316 return V4L2_STD_SECAM_L;
317 case '-':
318 break;
319 default:
320 CX18_WARN("secam= argument not recognised\n");
321 return 0;
322 }
323
324 switch (ntsc[0]) {
325 case 'm':
326 case 'M':
327 return V4L2_STD_NTSC_M;
328 case 'j':
329 case 'J':
330 return V4L2_STD_NTSC_M_JP;
331 case 'k':
332 case 'K':
333 return V4L2_STD_NTSC_M_KR;
334 case '-':
335 break;
336 default:
337 CX18_WARN("ntsc= argument not recognised\n");
338 return 0;
339 }
340
341 /* no match found */
342 return 0;
343 }
344
345 static void cx18_process_options(struct cx18 *cx)
346 {
347 int i, j;
348
349 cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
350 cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
351 cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
352 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
353 cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
354 cx->options.cardtype = cardtype[cx->num];
355 cx->options.tuner = tuner[cx->num];
356 cx->options.radio = radio[cx->num];
357
358 cx->std = cx18_parse_std(cx);
359 if (cx->options.cardtype == -1) {
360 CX18_INFO("Ignore card\n");
361 return;
362 }
363 cx->card = cx18_get_card(cx->options.cardtype - 1);
364 if (cx->card)
365 CX18_INFO("User specified %s card\n", cx->card->name);
366 else if (cx->options.cardtype != 0)
367 CX18_ERR("Unknown user specified type, trying to autodetect card\n");
368 if (cx->card == NULL) {
369 if (cx->dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
370 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
371 CX18_INFO("Autodetected Hauppauge card\n");
372 }
373 }
374 if (cx->card == NULL) {
375 for (i = 0; (cx->card = cx18_get_card(i)); i++) {
376 if (cx->card->pci_list == NULL)
377 continue;
378 for (j = 0; cx->card->pci_list[j].device; j++) {
379 if (cx->dev->device !=
380 cx->card->pci_list[j].device)
381 continue;
382 if (cx->dev->subsystem_vendor !=
383 cx->card->pci_list[j].subsystem_vendor)
384 continue;
385 if (cx->dev->subsystem_device !=
386 cx->card->pci_list[j].subsystem_device)
387 continue;
388 CX18_INFO("Autodetected %s card\n", cx->card->name);
389 goto done;
390 }
391 }
392 }
393 done:
394
395 if (cx->card == NULL) {
396 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
397 CX18_ERR("Unknown card: vendor/device: %04x/%04x\n",
398 cx->dev->vendor, cx->dev->device);
399 CX18_ERR(" subsystem vendor/device: %04x/%04x\n",
400 cx->dev->subsystem_vendor, cx->dev->subsystem_device);
401 CX18_ERR("Defaulting to %s card\n", cx->card->name);
402 CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
403 CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
404 CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
405 }
406 cx->v4l2_cap = cx->card->v4l2_capabilities;
407 cx->card_name = cx->card->name;
408 cx->card_i2c = cx->card->i2c;
409 }
410
411 /* Precondition: the cx18 structure has been memset to 0. Only
412 the dev and num fields have been filled in.
413 No assumptions on the card type may be made here (see cx18_init_struct2
414 for that).
415 */
416 static int __devinit cx18_init_struct1(struct cx18 *cx)
417 {
418 cx->base_addr = pci_resource_start(cx->dev, 0);
419
420 mutex_init(&cx->serialize_lock);
421 mutex_init(&cx->i2c_bus_lock[0]);
422 mutex_init(&cx->i2c_bus_lock[1]);
423
424 spin_lock_init(&cx->lock);
425 spin_lock_init(&cx->dma_reg_lock);
426
427 /* start counting open_id at 1 */
428 cx->open_id = 1;
429
430 /* Initial settings */
431 cx2341x_fill_defaults(&cx->params);
432 cx->temporal_strength = cx->params.video_temporal_filter;
433 cx->spatial_strength = cx->params.video_spatial_filter;
434 cx->filter_mode = cx->params.video_spatial_filter_mode |
435 (cx->params.video_temporal_filter_mode << 1) |
436 (cx->params.video_median_filter_type << 2);
437 cx->params.port = CX2341X_PORT_MEMORY;
438 cx->params.capabilities = CX2341X_CAP_HAS_SLICED_VBI;
439 init_waitqueue_head(&cx->cap_w);
440 init_waitqueue_head(&cx->mb_apu_waitq);
441 init_waitqueue_head(&cx->mb_cpu_waitq);
442 init_waitqueue_head(&cx->mb_epu_waitq);
443 init_waitqueue_head(&cx->mb_hpu_waitq);
444 init_waitqueue_head(&cx->dma_waitq);
445
446 /* VBI */
447 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
448 cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
449 cx->vbi.raw_size = 1456;
450 cx->vbi.raw_decoder_line_size = 1456;
451 cx->vbi.raw_decoder_sav_odd_field = 0x20;
452 cx->vbi.raw_decoder_sav_even_field = 0x60;
453 cx->vbi.sliced_decoder_line_size = 272;
454 cx->vbi.sliced_decoder_sav_odd_field = 0xB0;
455 cx->vbi.sliced_decoder_sav_even_field = 0xF0;
456 return 0;
457 }
458
459 /* Second initialization part. Here the card type has been
460 autodetected. */
461 static void __devinit cx18_init_struct2(struct cx18 *cx)
462 {
463 int i;
464
465 for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
466 if (cx->card->video_inputs[i].video_type == 0)
467 break;
468 cx->nof_inputs = i;
469 for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
470 if (cx->card->audio_inputs[i].audio_type == 0)
471 break;
472 cx->nof_audio_inputs = i;
473
474 /* Find tuner input */
475 for (i = 0; i < cx->nof_inputs; i++) {
476 if (cx->card->video_inputs[i].video_type ==
477 CX18_CARD_INPUT_VID_TUNER)
478 break;
479 }
480 if (i == cx->nof_inputs)
481 i = 0;
482 cx->active_input = i;
483 cx->audio_input = cx->card->video_inputs[i].audio_index;
484 cx->av_state.vid_input = CX18_AV_COMPOSITE7;
485 cx->av_state.aud_input = CX18_AV_AUDIO8;
486 cx->av_state.audclk_freq = 48000;
487 cx->av_state.audmode = V4L2_TUNER_MODE_LANG1;
488 cx->av_state.vbi_line_offset = 8;
489 }
490
491 static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *dev,
492 const struct pci_device_id *pci_id)
493 {
494 u16 cmd;
495 unsigned char pci_latency;
496
497 CX18_DEBUG_INFO("Enabling pci device\n");
498
499 if (pci_enable_device(dev)) {
500 CX18_ERR("Can't enable device %d!\n", cx->num);
501 return -EIO;
502 }
503 if (pci_set_dma_mask(dev, 0xffffffff)) {
504 CX18_ERR("No suitable DMA available on card %d.\n", cx->num);
505 return -EIO;
506 }
507 if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
508 CX18_ERR("Cannot request encoder memory region on card %d.\n", cx->num);
509 return -EIO;
510 }
511
512 /* Check for bus mastering */
513 pci_read_config_word(dev, PCI_COMMAND, &cmd);
514 cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
515 pci_write_config_word(dev, PCI_COMMAND, cmd);
516
517 pci_read_config_byte(dev, PCI_CLASS_REVISION, &cx->card_rev);
518 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
519
520 if (pci_latency < 64 && cx18_pci_latency) {
521 CX18_INFO("Unreasonably low latency timer, "
522 "setting to 64 (was %d)\n", pci_latency);
523 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
524 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
525 }
526 /* This config space value relates to DMA latencies. The
527 default value 0x8080 is too low however and will lead
528 to DMA errors. 0xffff is the max value which solves
529 these problems. */
530 pci_write_config_dword(dev, 0x40, 0xffff);
531
532 CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
533 "irq: %d, latency: %d, memory: 0x%lx\n",
534 cx->dev->device, cx->card_rev, dev->bus->number,
535 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
536 cx->dev->irq, pci_latency, (unsigned long)cx->base_addr);
537
538 return 0;
539 }
540
541 #ifdef MODULE
542 static u32 cx18_request_module(struct cx18 *cx, u32 hw,
543 const char *name, u32 id)
544 {
545 if ((hw & id) == 0)
546 return hw;
547 if (request_module(name) != 0) {
548 CX18_ERR("Failed to load module %s\n", name);
549 return hw & ~id;
550 }
551 CX18_DEBUG_INFO("Loaded module %s\n", name);
552 return hw;
553 }
554 #endif
555
556 static void cx18_load_and_init_modules(struct cx18 *cx)
557 {
558 u32 hw = cx->card->hw_all;
559 int i;
560
561 #ifdef MODULE
562 /* load modules */
563 #ifndef CONFIG_MEDIA_TUNER
564 hw = cx18_request_module(cx, hw, "tuner", CX18_HW_TUNER);
565 #endif
566 #ifndef CONFIG_VIDEO_CS5345
567 hw = cx18_request_module(cx, hw, "cs5345", CX18_HW_CS5345);
568 #endif
569 #endif
570
571 /* check which i2c devices are actually found */
572 for (i = 0; i < 32; i++) {
573 u32 device = 1 << i;
574
575 if (!(device & hw))
576 continue;
577 if (device == CX18_HW_GPIO || device == CX18_HW_TVEEPROM ||
578 device == CX18_HW_CX23418 || device == CX18_HW_DVB) {
579 /* These 'devices' do not use i2c probing */
580 cx->hw_flags |= device;
581 continue;
582 }
583 cx18_i2c_register(cx, i);
584 if (cx18_i2c_hw_addr(cx, device) > 0)
585 cx->hw_flags |= device;
586 }
587
588 hw = cx->hw_flags;
589 }
590
591 static int __devinit cx18_probe(struct pci_dev *dev,
592 const struct pci_device_id *pci_id)
593 {
594 int retval = 0;
595 int vbi_buf_size;
596 u32 devtype;
597 struct cx18 *cx;
598
599 spin_lock(&cx18_cards_lock);
600
601 /* Make sure we've got a place for this card */
602 if (cx18_cards_active == CX18_MAX_CARDS) {
603 printk(KERN_ERR "cx18: Maximum number of cards detected (%d).\n",
604 cx18_cards_active);
605 spin_unlock(&cx18_cards_lock);
606 return -ENOMEM;
607 }
608
609 cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
610 if (!cx) {
611 spin_unlock(&cx18_cards_lock);
612 return -ENOMEM;
613 }
614 cx18_cards[cx18_cards_active] = cx;
615 cx->dev = dev;
616 cx->num = cx18_cards_active++;
617 snprintf(cx->name, sizeof(cx->name) - 1, "cx18-%d", cx->num);
618 CX18_INFO("Initializing card #%d\n", cx->num);
619
620 spin_unlock(&cx18_cards_lock);
621
622 cx18_process_options(cx);
623 if (cx->options.cardtype == -1) {
624 retval = -ENODEV;
625 goto err;
626 }
627 if (cx18_init_struct1(cx)) {
628 retval = -ENOMEM;
629 goto err;
630 }
631
632 CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);
633
634 /* PCI Device Setup */
635 retval = cx18_setup_pci(cx, dev, pci_id);
636 if (retval != 0) {
637 if (retval == -EIO)
638 goto free_workqueue;
639 else if (retval == -ENXIO)
640 goto free_mem;
641 }
642 /* save cx in the pci struct for later use */
643 pci_set_drvdata(dev, cx);
644
645 /* map io memory */
646 CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
647 cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
648 cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
649 CX18_MEM_SIZE);
650 if (!cx->enc_mem) {
651 CX18_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
652 CX18_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
653 retval = -ENOMEM;
654 goto free_mem;
655 }
656 cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
657 devtype = read_reg(0xC72028);
658 switch (devtype & 0xff000000) {
659 case 0xff000000:
660 CX18_INFO("cx23418 revision %08x (A)\n", devtype);
661 break;
662 case 0x01000000:
663 CX18_INFO("cx23418 revision %08x (B)\n", devtype);
664 break;
665 default:
666 CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
667 break;
668 }
669
670 cx18_init_power(cx, 1);
671 cx18_init_memory(cx);
672
673 cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
674 cx18_init_scb(cx);
675
676 cx18_gpio_init(cx);
677
678 /* active i2c */
679 CX18_DEBUG_INFO("activating i2c...\n");
680 if (init_cx18_i2c(cx)) {
681 CX18_ERR("Could not initialize i2c\n");
682 goto free_map;
683 }
684
685 CX18_DEBUG_INFO("Active card count: %d.\n", cx18_cards_active);
686
687 if (cx->card->hw_all & CX18_HW_TVEEPROM) {
688 /* Based on the model number the cardtype may be changed.
689 The PCI IDs are not always reliable. */
690 cx18_process_eeprom(cx);
691 }
692 if (cx->card->comment)
693 CX18_INFO("%s", cx->card->comment);
694 if (cx->card->v4l2_capabilities == 0) {
695 retval = -ENODEV;
696 goto free_i2c;
697 }
698 cx18_init_memory(cx);
699
700 /* Register IRQ */
701 retval = request_irq(cx->dev->irq, cx18_irq_handler,
702 IRQF_SHARED | IRQF_DISABLED, cx->name, (void *)cx);
703 if (retval) {
704 CX18_ERR("Failed to register irq %d\n", retval);
705 goto free_i2c;
706 }
707
708 if (cx->std == 0)
709 cx->std = V4L2_STD_NTSC_M;
710
711 if (cx->options.tuner == -1) {
712 int i;
713
714 for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
715 if ((cx->std & cx->card->tuners[i].std) == 0)
716 continue;
717 cx->options.tuner = cx->card->tuners[i].tuner;
718 break;
719 }
720 }
721 /* if no tuner was found, then pick the first tuner in the card list */
722 if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
723 cx->std = cx->card->tuners[0].std;
724 cx->options.tuner = cx->card->tuners[0].tuner;
725 }
726 if (cx->options.radio == -1)
727 cx->options.radio = (cx->card->radio_input.audio_type != 0);
728
729 /* The card is now fully identified, continue with card-specific
730 initialization. */
731 cx18_init_struct2(cx);
732
733 cx18_load_and_init_modules(cx);
734
735 if (cx->std & V4L2_STD_525_60) {
736 cx->is_60hz = 1;
737 cx->is_out_60hz = 1;
738 } else {
739 cx->is_50hz = 1;
740 cx->is_out_50hz = 1;
741 }
742 cx->params.video_gop_size = cx->is_60hz ? 15 : 12;
743
744 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = 0x08000;
745 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = 0x08000;
746 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = 0x01200;
747 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = 0x20000;
748 vbi_buf_size = cx->vbi.raw_size * (cx->is_60hz ? 24 : 36) / 2;
749 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
750
751 if (cx->options.radio > 0)
752 cx->v4l2_cap |= V4L2_CAP_RADIO;
753
754 if (cx->options.tuner > -1) {
755 struct tuner_setup setup;
756
757 setup.addr = ADDR_UNSET;
758 setup.type = cx->options.tuner;
759 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
760 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
761 cx18_reset_tuner_gpio : NULL;
762 cx18_call_i2c_clients(cx, TUNER_SET_TYPE_ADDR, &setup);
763 if (setup.type == TUNER_XC2028) {
764 static struct xc2028_ctrl ctrl = {
765 .fname = XC2028_DEFAULT_FIRMWARE,
766 .max_len = 64,
767 };
768 struct v4l2_priv_tun_config cfg = {
769 .tuner = cx->options.tuner,
770 .priv = &ctrl,
771 };
772 cx18_call_i2c_clients(cx, TUNER_SET_CONFIG, &cfg);
773 }
774 }
775
776 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
777 are not. */
778 cx->tuner_std = cx->std;
779
780 retval = cx18_streams_setup(cx);
781 if (retval) {
782 CX18_ERR("Error %d setting up streams\n", retval);
783 goto free_irq;
784 }
785 retval = cx18_streams_register(cx);
786 if (retval) {
787 CX18_ERR("Error %d registering devices\n", retval);
788 goto free_streams;
789 }
790
791 CX18_INFO("Initialized card #%d: %s\n", cx->num, cx->card_name);
792
793 return 0;
794
795 free_streams:
796 cx18_streams_cleanup(cx, 1);
797 free_irq:
798 free_irq(cx->dev->irq, (void *)cx);
799 free_i2c:
800 exit_cx18_i2c(cx);
801 free_map:
802 cx18_iounmap(cx);
803 free_mem:
804 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
805 free_workqueue:
806 err:
807 if (retval == 0)
808 retval = -ENODEV;
809 CX18_ERR("Error %d on initialization\n", retval);
810
811 kfree(cx18_cards[cx18_cards_active]);
812 cx18_cards[cx18_cards_active] = NULL;
813 return retval;
814 }
815
816 int cx18_init_on_first_open(struct cx18 *cx)
817 {
818 int video_input;
819 int fw_retry_count = 3;
820 struct v4l2_frequency vf;
821
822 if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
823 return -ENXIO;
824
825 if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
826 return 0;
827
828 while (--fw_retry_count > 0) {
829 /* load firmware */
830 if (cx18_firmware_init(cx) == 0)
831 break;
832 if (fw_retry_count > 1)
833 CX18_WARN("Retry loading firmware\n");
834 }
835
836 if (fw_retry_count == 0) {
837 set_bit(CX18_F_I_FAILED, &cx->i_flags);
838 return -ENXIO;
839 }
840 set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
841
842 /* Init the firmware twice to work around a silicon bug
843 * transport related. */
844
845 fw_retry_count = 3;
846 while (--fw_retry_count > 0) {
847 /* load firmware */
848 if (cx18_firmware_init(cx) == 0)
849 break;
850 if (fw_retry_count > 1)
851 CX18_WARN("Retry loading firmware\n");
852 }
853
854 if (fw_retry_count == 0) {
855 set_bit(CX18_F_I_FAILED, &cx->i_flags);
856 return -ENXIO;
857 }
858
859 vf.tuner = 0;
860 vf.type = V4L2_TUNER_ANALOG_TV;
861 vf.frequency = 6400; /* the tuner 'baseline' frequency */
862
863 /* Set initial frequency. For PAL/SECAM broadcasts no
864 'default' channel exists AFAIK. */
865 if (cx->std == V4L2_STD_NTSC_M_JP)
866 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
867 else if (cx->std & V4L2_STD_NTSC_M)
868 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
869
870 video_input = cx->active_input;
871 cx->active_input++; /* Force update of input */
872 cx18_v4l2_ioctls(cx, NULL, VIDIOC_S_INPUT, &video_input);
873
874 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
875 in one place. */
876 cx->std++; /* Force full standard initialization */
877 cx18_v4l2_ioctls(cx, NULL, VIDIOC_S_STD, &cx->tuner_std);
878 cx18_v4l2_ioctls(cx, NULL, VIDIOC_S_FREQUENCY, &vf);
879 return 0;
880 }
881
882 static void cx18_remove(struct pci_dev *pci_dev)
883 {
884 struct cx18 *cx = pci_get_drvdata(pci_dev);
885
886 CX18_DEBUG_INFO("Removing Card #%d\n", cx->num);
887
888 /* Stop all captures */
889 CX18_DEBUG_INFO("Stopping all streams\n");
890 if (atomic_read(&cx->tot_capturing) > 0)
891 cx18_stop_all_captures(cx);
892
893 /* Interrupts */
894 sw1_irq_disable(IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
895 sw2_irq_disable(IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
896
897 cx18_halt_firmware(cx);
898
899 cx18_streams_cleanup(cx, 1);
900
901 exit_cx18_i2c(cx);
902
903 free_irq(cx->dev->irq, (void *)cx);
904
905 cx18_iounmap(cx);
906
907 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
908
909 pci_disable_device(cx->dev);
910
911 CX18_INFO("Removed %s, card #%d\n", cx->card_name, cx->num);
912 }
913
914 /* define a pci_driver for card detection */
915 static struct pci_driver cx18_pci_driver = {
916 .name = "cx18",
917 .id_table = cx18_pci_tbl,
918 .probe = cx18_probe,
919 .remove = cx18_remove,
920 };
921
922 static int module_start(void)
923 {
924 printk(KERN_INFO "cx18: Start initialization, version %s\n", CX18_VERSION);
925
926 memset(cx18_cards, 0, sizeof(cx18_cards));
927
928 /* Validate parameters */
929 if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
930 printk(KERN_ERR "cx18: Exiting, ivtv_first_minor must be between 0 and %d\n",
931 CX18_MAX_CARDS - 1);
932 return -1;
933 }
934
935 if (cx18_debug < 0 || cx18_debug > 511) {
936 cx18_debug = 0;
937 printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");
938 }
939
940 if (pci_register_driver(&cx18_pci_driver)) {
941 printk(KERN_ERR "cx18: Error detecting PCI card\n");
942 return -ENODEV;
943 }
944 printk(KERN_INFO "cx18: End initialization\n");
945 return 0;
946 }
947
948 static void module_cleanup(void)
949 {
950 int i;
951
952 pci_unregister_driver(&cx18_pci_driver);
953
954 for (i = 0; i < cx18_cards_active; i++) {
955 if (cx18_cards[i] == NULL)
956 continue;
957 kfree(cx18_cards[i]);
958 }
959 }
960
961 module_init(module_start);
962 module_exit(module_cleanup);