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