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