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