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