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