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