blob: a893caff0aa9fe6d04994a376bcf3d2ad1f864f1 [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 };
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -030078static unsigned cardtype_c = 1;
79static unsigned tuner_c = 1;
80static unsigned radio_c = 1;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030081static char pal[] = "--";
82static char secam[] = "--";
83static char ntsc[] = "-";
84
85/* Buffers */
86static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
87static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
88static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
89static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
90static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;
91
92static int cx18_pci_latency = 1;
93
Andy Walls3f75c612008-11-16 23:33:41 -030094static int mmio_ndelay;
95static int retry_mmio = 1;
96
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030097int cx18_debug;
98
99module_param_array(tuner, int, &tuner_c, 0644);
100module_param_array(radio, bool, &radio_c, 0644);
101module_param_array(cardtype, int, &cardtype_c, 0644);
102module_param_string(pal, pal, sizeof(pal), 0644);
103module_param_string(secam, secam, sizeof(secam), 0644);
104module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
105module_param_named(debug, cx18_debug, int, 0644);
Andy Walls3f75c612008-11-16 23:33:41 -0300106module_param(mmio_ndelay, int, 0644);
107module_param(retry_mmio, int, 0644);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300108module_param(cx18_pci_latency, int, 0644);
109module_param(cx18_first_minor, int, 0644);
110
111module_param(enc_mpg_buffers, int, 0644);
112module_param(enc_ts_buffers, int, 0644);
113module_param(enc_yuv_buffers, int, 0644);
114module_param(enc_vbi_buffers, int, 0644);
115module_param(enc_pcm_buffers, int, 0644);
116
117MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
118 "\t\t\tsee tuner.h for values");
119MODULE_PARM_DESC(radio,
120 "Enable or disable the radio. Use only if autodetection\n"
121 "\t\t\tfails. 0 = disable, 1 = enable");
122MODULE_PARM_DESC(cardtype,
123 "Only use this option if your card is not detected properly.\n"
124 "\t\tSpecify card type:\n"
125 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
126 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
127 "\t\t\t 3 = Compro VideoMate H900\n"
128 "\t\t\t 4 = Yuan MPC718\n"
Sri Deevi03c28082008-06-21 11:06:44 -0300129 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
Andy Walls9eee4fb2008-10-04 20:28:40 -0300130 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
131 "\t\t\t 7 = Leadtek WinFast PVR2100\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300132 "\t\t\t 0 = Autodetect (default)\n"
133 "\t\t\t-1 = Ignore this card\n\t\t");
134MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
135MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
136MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
137MODULE_PARM_DESC(debug,
138 "Debug level (bitmask). Default: 0\n"
139 "\t\t\t 1/0x0001: warning\n"
140 "\t\t\t 2/0x0002: info\n"
141 "\t\t\t 4/0x0004: mailbox\n"
142 "\t\t\t 8/0x0008: dma\n"
143 "\t\t\t 16/0x0010: ioctl\n"
144 "\t\t\t 32/0x0020: file\n"
145 "\t\t\t 64/0x0040: i2c\n"
146 "\t\t\t128/0x0080: irq\n"
147 "\t\t\t256/0x0100: high volume\n");
148MODULE_PARM_DESC(cx18_pci_latency,
149 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
150 "\t\t\tDefault: Yes");
Andy Wallsd267d852008-09-28 21:46:02 -0300151MODULE_PARM_DESC(retry_mmio,
Andy Walls3f75c612008-11-16 23:33:41 -0300152 "(Deprecated) MMIO writes are now always checked and retried\n"
153 "\t\t\tEffectively: 1 [Yes]");
Andy Wallsc641d092008-09-01 00:40:41 -0300154MODULE_PARM_DESC(mmio_ndelay,
Andy Walls3f75c612008-11-16 23:33:41 -0300155 "(Deprecated) MMIO accesses are now never purposely delayed\n"
156 "\t\t\tEffectively: 0 ns");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300157MODULE_PARM_DESC(enc_mpg_buffers,
158 "Encoder MPG Buffers (in MB)\n"
159 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
160MODULE_PARM_DESC(enc_ts_buffers,
161 "Encoder TS Buffers (in MB)\n"
162 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
163MODULE_PARM_DESC(enc_yuv_buffers,
164 "Encoder YUV Buffers (in MB)\n"
165 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
166MODULE_PARM_DESC(enc_vbi_buffers,
167 "Encoder VBI Buffers (in MB)\n"
168 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
169MODULE_PARM_DESC(enc_pcm_buffers,
170 "Encoder PCM buffers (in MB)\n"
171 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
172
Hans Verkuildd896012008-10-04 08:36:54 -0300173MODULE_PARM_DESC(cx18_first_minor, "Set kernel number assigned to first card");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300174
175MODULE_AUTHOR("Hans Verkuil");
176MODULE_DESCRIPTION("CX23418 driver");
177MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
178MODULE_LICENSE("GPL");
179
180MODULE_VERSION(CX18_VERSION);
181
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300182/* Generic utility functions */
183int cx18_msleep_timeout(unsigned int msecs, int intr)
184{
Andy Walls330c6ec2008-11-08 14:19:37 -0300185 long int timeout = msecs_to_jiffies(msecs);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300186 int sig;
187
188 do {
189 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
190 timeout = schedule_timeout(timeout);
191 sig = intr ? signal_pending(current) : 0;
192 } while (!sig && timeout);
193 return sig;
194}
195
196/* Release ioremapped memory */
197static void cx18_iounmap(struct cx18 *cx)
198{
199 if (cx == NULL)
200 return;
201
202 /* Release io memory */
203 if (cx->enc_mem != NULL) {
204 CX18_DEBUG_INFO("releasing enc_mem\n");
205 iounmap(cx->enc_mem);
206 cx->enc_mem = NULL;
207 }
208}
209
210/* Hauppauge card? get values from tveeprom */
211void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
212{
213 u8 eedata[256];
214
215 cx->i2c_client[0].addr = 0xA0 >> 1;
216 tveeprom_read(&cx->i2c_client[0], eedata, sizeof(eedata));
217 tveeprom_hauppauge_analog(&cx->i2c_client[0], tv, eedata);
218}
219
220static void cx18_process_eeprom(struct cx18 *cx)
221{
222 struct tveeprom tv;
223
224 cx18_read_eeprom(cx, &tv);
225
226 /* Many thanks to Steven Toth from Hauppauge for providing the
227 model numbers */
Hans Verkuil1d081602008-05-12 14:45:19 -0300228 /* Note: the Samsung memory models cannot be reliably determined
229 from the model number. Use the cardtype module option if you
230 have one of these preproduction models. */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300231 switch (tv.model) {
Hans Verkuil1d081602008-05-12 14:45:19 -0300232 case 74000 ... 74999:
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300233 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
234 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300235 case 0:
236 CX18_ERR("Invalid EEPROM\n");
237 return;
238 default:
239 CX18_ERR("Unknown model %d, defaulting to HVR-1600\n", tv.model);
240 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
241 break;
242 }
243
244 cx->v4l2_cap = cx->card->v4l2_capabilities;
245 cx->card_name = cx->card->name;
246 cx->card_i2c = cx->card->i2c;
247
248 CX18_INFO("Autodetected %s\n", cx->card_name);
249
250 if (tv.tuner_type == TUNER_ABSENT)
251 CX18_ERR("tveeprom cannot autodetect tuner!");
252
253 if (cx->options.tuner == -1)
254 cx->options.tuner = tv.tuner_type;
255 if (cx->options.radio == -1)
256 cx->options.radio = (tv.has_radio != 0);
257
258 if (cx->std != 0)
259 /* user specified tuner standard */
260 return;
261
262 /* autodetect tuner standard */
263 if (tv.tuner_formats & V4L2_STD_PAL) {
264 CX18_DEBUG_INFO("PAL tuner detected\n");
265 cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
266 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
267 CX18_DEBUG_INFO("NTSC tuner detected\n");
268 cx->std |= V4L2_STD_NTSC_M;
269 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
270 CX18_DEBUG_INFO("SECAM tuner detected\n");
271 cx->std |= V4L2_STD_SECAM_L;
272 } else {
273 CX18_INFO("No tuner detected, default to NTSC-M\n");
274 cx->std |= V4L2_STD_NTSC_M;
275 }
276}
277
278static v4l2_std_id cx18_parse_std(struct cx18 *cx)
279{
280 switch (pal[0]) {
281 case '6':
282 return V4L2_STD_PAL_60;
283 case 'b':
284 case 'B':
285 case 'g':
286 case 'G':
287 return V4L2_STD_PAL_BG;
288 case 'h':
289 case 'H':
290 return V4L2_STD_PAL_H;
291 case 'n':
292 case 'N':
293 if (pal[1] == 'c' || pal[1] == 'C')
294 return V4L2_STD_PAL_Nc;
295 return V4L2_STD_PAL_N;
296 case 'i':
297 case 'I':
298 return V4L2_STD_PAL_I;
299 case 'd':
300 case 'D':
301 case 'k':
302 case 'K':
303 return V4L2_STD_PAL_DK;
304 case 'M':
305 case 'm':
306 return V4L2_STD_PAL_M;
307 case '-':
308 break;
309 default:
310 CX18_WARN("pal= argument not recognised\n");
311 return 0;
312 }
313
314 switch (secam[0]) {
315 case 'b':
316 case 'B':
317 case 'g':
318 case 'G':
319 case 'h':
320 case 'H':
321 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
322 case 'd':
323 case 'D':
324 case 'k':
325 case 'K':
326 return V4L2_STD_SECAM_DK;
327 case 'l':
328 case 'L':
329 if (secam[1] == 'C' || secam[1] == 'c')
330 return V4L2_STD_SECAM_LC;
331 return V4L2_STD_SECAM_L;
332 case '-':
333 break;
334 default:
335 CX18_WARN("secam= argument not recognised\n");
336 return 0;
337 }
338
339 switch (ntsc[0]) {
340 case 'm':
341 case 'M':
342 return V4L2_STD_NTSC_M;
343 case 'j':
344 case 'J':
345 return V4L2_STD_NTSC_M_JP;
346 case 'k':
347 case 'K':
348 return V4L2_STD_NTSC_M_KR;
349 case '-':
350 break;
351 default:
352 CX18_WARN("ntsc= argument not recognised\n");
353 return 0;
354 }
355
356 /* no match found */
357 return 0;
358}
359
360static void cx18_process_options(struct cx18 *cx)
361{
362 int i, j;
363
364 cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
365 cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
366 cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
367 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
368 cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
369 cx->options.cardtype = cardtype[cx->num];
370 cx->options.tuner = tuner[cx->num];
371 cx->options.radio = radio[cx->num];
372
373 cx->std = cx18_parse_std(cx);
374 if (cx->options.cardtype == -1) {
375 CX18_INFO("Ignore card\n");
376 return;
377 }
378 cx->card = cx18_get_card(cx->options.cardtype - 1);
379 if (cx->card)
380 CX18_INFO("User specified %s card\n", cx->card->name);
381 else if (cx->options.cardtype != 0)
382 CX18_ERR("Unknown user specified type, trying to autodetect card\n");
383 if (cx->card == NULL) {
384 if (cx->dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
385 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
386 CX18_INFO("Autodetected Hauppauge card\n");
387 }
388 }
389 if (cx->card == NULL) {
390 for (i = 0; (cx->card = cx18_get_card(i)); i++) {
391 if (cx->card->pci_list == NULL)
392 continue;
393 for (j = 0; cx->card->pci_list[j].device; j++) {
394 if (cx->dev->device !=
395 cx->card->pci_list[j].device)
396 continue;
397 if (cx->dev->subsystem_vendor !=
398 cx->card->pci_list[j].subsystem_vendor)
399 continue;
400 if (cx->dev->subsystem_device !=
401 cx->card->pci_list[j].subsystem_device)
402 continue;
403 CX18_INFO("Autodetected %s card\n", cx->card->name);
404 goto done;
405 }
406 }
407 }
408done:
409
410 if (cx->card == NULL) {
411 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
Bjorn Helgaas29e66a62008-09-04 17:24:51 -0300412 CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300413 cx->dev->vendor, cx->dev->device);
Bjorn Helgaas29e66a62008-09-04 17:24:51 -0300414 CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n",
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300415 cx->dev->subsystem_vendor, cx->dev->subsystem_device);
416 CX18_ERR("Defaulting to %s card\n", cx->card->name);
417 CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
418 CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
419 CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
420 }
421 cx->v4l2_cap = cx->card->v4l2_capabilities;
422 cx->card_name = cx->card->name;
423 cx->card_i2c = cx->card->i2c;
424}
425
426/* Precondition: the cx18 structure has been memset to 0. Only
427 the dev and num fields have been filled in.
428 No assumptions on the card type may be made here (see cx18_init_struct2
429 for that).
430 */
431static int __devinit cx18_init_struct1(struct cx18 *cx)
432{
Andy Wallsee2d64f2008-11-16 01:38:19 -0300433 int i;
434
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300435 cx->base_addr = pci_resource_start(cx->dev, 0);
436
437 mutex_init(&cx->serialize_lock);
438 mutex_init(&cx->i2c_bus_lock[0]);
439 mutex_init(&cx->i2c_bus_lock[1]);
Andy Walls8abdd002008-07-13 19:05:25 -0300440 mutex_init(&cx->gpio_lock);
Andy Walls72c2d6d2008-11-06 01:15:41 -0300441 mutex_init(&cx->epu2apu_mb_lock);
442 mutex_init(&cx->epu2cpu_mb_lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300443
444 spin_lock_init(&cx->lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300445
Andy Walls572bfea2008-11-25 21:43:05 -0300446 cx->work_queue = create_singlethread_workqueue(cx->name);
447 if (cx->work_queue == NULL) {
448 CX18_ERR("Unable to create work hander thread\n");
449 return -ENOMEM;
450 }
451
Andy Wallsee2d64f2008-11-16 01:38:19 -0300452 for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++) {
453 cx->epu_work_order[i].cx = cx;
454 cx->epu_work_order[i].str = cx->epu_debug_str;
455 INIT_WORK(&cx->epu_work_order[i].work, cx18_epu_work_handler);
456 }
Andy Walls1d6782b2008-11-05 00:49:14 -0300457
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300458 /* start counting open_id at 1 */
459 cx->open_id = 1;
460
461 /* Initial settings */
462 cx2341x_fill_defaults(&cx->params);
463 cx->temporal_strength = cx->params.video_temporal_filter;
464 cx->spatial_strength = cx->params.video_spatial_filter;
465 cx->filter_mode = cx->params.video_spatial_filter_mode |
466 (cx->params.video_temporal_filter_mode << 1) |
467 (cx->params.video_median_filter_type << 2);
468 cx->params.port = CX2341X_PORT_MEMORY;
Hans Verkuile8b934d2008-06-28 20:57:56 -0300469 cx->params.capabilities = CX2341X_CAP_HAS_TS;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300470 init_waitqueue_head(&cx->cap_w);
471 init_waitqueue_head(&cx->mb_apu_waitq);
472 init_waitqueue_head(&cx->mb_cpu_waitq);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300473 init_waitqueue_head(&cx->dma_waitq);
474
475 /* VBI */
476 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
477 cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
478 cx->vbi.raw_size = 1456;
479 cx->vbi.raw_decoder_line_size = 1456;
480 cx->vbi.raw_decoder_sav_odd_field = 0x20;
481 cx->vbi.raw_decoder_sav_even_field = 0x60;
482 cx->vbi.sliced_decoder_line_size = 272;
483 cx->vbi.sliced_decoder_sav_odd_field = 0xB0;
484 cx->vbi.sliced_decoder_sav_even_field = 0xF0;
485 return 0;
486}
487
488/* Second initialization part. Here the card type has been
489 autodetected. */
490static void __devinit cx18_init_struct2(struct cx18 *cx)
491{
492 int i;
493
494 for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
495 if (cx->card->video_inputs[i].video_type == 0)
496 break;
497 cx->nof_inputs = i;
498 for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
499 if (cx->card->audio_inputs[i].audio_type == 0)
500 break;
501 cx->nof_audio_inputs = i;
502
503 /* Find tuner input */
504 for (i = 0; i < cx->nof_inputs; i++) {
505 if (cx->card->video_inputs[i].video_type ==
506 CX18_CARD_INPUT_VID_TUNER)
507 break;
508 }
509 if (i == cx->nof_inputs)
510 i = 0;
511 cx->active_input = i;
512 cx->audio_input = cx->card->video_inputs[i].audio_index;
513 cx->av_state.vid_input = CX18_AV_COMPOSITE7;
514 cx->av_state.aud_input = CX18_AV_AUDIO8;
515 cx->av_state.audclk_freq = 48000;
516 cx->av_state.audmode = V4L2_TUNER_MODE_LANG1;
517 cx->av_state.vbi_line_offset = 8;
518}
519
520static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *dev,
521 const struct pci_device_id *pci_id)
522{
523 u16 cmd;
524 unsigned char pci_latency;
525
526 CX18_DEBUG_INFO("Enabling pci device\n");
527
528 if (pci_enable_device(dev)) {
529 CX18_ERR("Can't enable device %d!\n", cx->num);
530 return -EIO;
531 }
532 if (pci_set_dma_mask(dev, 0xffffffff)) {
533 CX18_ERR("No suitable DMA available on card %d.\n", cx->num);
534 return -EIO;
535 }
536 if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
537 CX18_ERR("Cannot request encoder memory region on card %d.\n", cx->num);
538 return -EIO;
539 }
540
Andy Walls45190642008-08-29 16:10:21 -0300541 /* Enable bus mastering and memory mapped IO for the CX23418 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300542 pci_read_config_word(dev, PCI_COMMAND, &cmd);
Andy Walls45190642008-08-29 16:10:21 -0300543 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300544 pci_write_config_word(dev, PCI_COMMAND, cmd);
545
546 pci_read_config_byte(dev, PCI_CLASS_REVISION, &cx->card_rev);
547 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
548
549 if (pci_latency < 64 && cx18_pci_latency) {
550 CX18_INFO("Unreasonably low latency timer, "
551 "setting to 64 (was %d)\n", pci_latency);
552 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
553 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
554 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300555
556 CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
557 "irq: %d, latency: %d, memory: 0x%lx\n",
558 cx->dev->device, cx->card_rev, dev->bus->number,
559 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
560 cx->dev->irq, pci_latency, (unsigned long)cx->base_addr);
561
562 return 0;
563}
564
Hans Verkuil6a4a7932008-05-01 09:34:54 -0300565#ifdef MODULE
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300566static u32 cx18_request_module(struct cx18 *cx, u32 hw,
567 const char *name, u32 id)
568{
569 if ((hw & id) == 0)
570 return hw;
571 if (request_module(name) != 0) {
572 CX18_ERR("Failed to load module %s\n", name);
573 return hw & ~id;
574 }
575 CX18_DEBUG_INFO("Loaded module %s\n", name);
576 return hw;
577}
Hans Verkuil6a4a7932008-05-01 09:34:54 -0300578#endif
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300579
580static void cx18_load_and_init_modules(struct cx18 *cx)
581{
582 u32 hw = cx->card->hw_all;
583 int i;
584
Hans Verkuil6a4a7932008-05-01 09:34:54 -0300585#ifdef MODULE
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300586 /* load modules */
Hans Verkuil58ae1c22008-11-03 08:06:51 -0300587#ifdef CONFIG_MEDIA_TUNER_MODULE
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300588 hw = cx18_request_module(cx, hw, "tuner", CX18_HW_TUNER);
589#endif
Hans Verkuil58ae1c22008-11-03 08:06:51 -0300590#ifdef CONFIG_VIDEO_CS5345_MODULE
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300591 hw = cx18_request_module(cx, hw, "cs5345", CX18_HW_CS5345);
592#endif
Hans Verkuil6a4a7932008-05-01 09:34:54 -0300593#endif
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300594
595 /* check which i2c devices are actually found */
596 for (i = 0; i < 32; i++) {
597 u32 device = 1 << i;
598
599 if (!(device & hw))
600 continue;
601 if (device == CX18_HW_GPIO || device == CX18_HW_TVEEPROM ||
602 device == CX18_HW_CX23418 || device == CX18_HW_DVB) {
603 /* These 'devices' do not use i2c probing */
604 cx->hw_flags |= device;
605 continue;
606 }
607 cx18_i2c_register(cx, i);
608 if (cx18_i2c_hw_addr(cx, device) > 0)
609 cx->hw_flags |= device;
610 }
611
612 hw = cx->hw_flags;
613}
614
615static int __devinit cx18_probe(struct pci_dev *dev,
616 const struct pci_device_id *pci_id)
617{
618 int retval = 0;
Andy Wallsff086572008-10-18 08:51:28 -0300619 int i;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300620 int vbi_buf_size;
621 u32 devtype;
622 struct cx18 *cx;
623
624 spin_lock(&cx18_cards_lock);
625
626 /* Make sure we've got a place for this card */
627 if (cx18_cards_active == CX18_MAX_CARDS) {
628 printk(KERN_ERR "cx18: Maximum number of cards detected (%d).\n",
629 cx18_cards_active);
630 spin_unlock(&cx18_cards_lock);
631 return -ENOMEM;
632 }
633
634 cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700635 if (!cx) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300636 spin_unlock(&cx18_cards_lock);
637 return -ENOMEM;
638 }
639 cx18_cards[cx18_cards_active] = cx;
640 cx->dev = dev;
641 cx->num = cx18_cards_active++;
Jean Delvareb4ac3c82008-05-13 18:27:15 -0300642 snprintf(cx->name, sizeof(cx->name), "cx18-%d", cx->num);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300643 CX18_INFO("Initializing card #%d\n", cx->num);
644
645 spin_unlock(&cx18_cards_lock);
646
647 cx18_process_options(cx);
648 if (cx->options.cardtype == -1) {
649 retval = -ENODEV;
650 goto err;
651 }
652 if (cx18_init_struct1(cx)) {
653 retval = -ENOMEM;
654 goto err;
655 }
656
657 CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);
658
659 /* PCI Device Setup */
660 retval = cx18_setup_pci(cx, dev, pci_id);
Andy Walls572bfea2008-11-25 21:43:05 -0300661 if (retval != 0)
662 goto free_workqueue;
663
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300664 /* save cx in the pci struct for later use */
665 pci_set_drvdata(dev, cx);
666
667 /* map io memory */
668 CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
669 cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
670 cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
671 CX18_MEM_SIZE);
672 if (!cx->enc_mem) {
673 CX18_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
674 CX18_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
675 retval = -ENOMEM;
676 goto free_mem;
677 }
678 cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
Andy Wallsb1526422008-08-30 16:03:44 -0300679 devtype = cx18_read_reg(cx, 0xC72028);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300680 switch (devtype & 0xff000000) {
681 case 0xff000000:
682 CX18_INFO("cx23418 revision %08x (A)\n", devtype);
683 break;
684 case 0x01000000:
685 CX18_INFO("cx23418 revision %08x (B)\n", devtype);
686 break;
687 default:
688 CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
689 break;
690 }
691
692 cx18_init_power(cx, 1);
693 cx18_init_memory(cx);
694
Al Viro990c81c2008-05-21 00:32:01 -0300695 cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300696 cx18_init_scb(cx);
697
698 cx18_gpio_init(cx);
699
700 /* active i2c */
701 CX18_DEBUG_INFO("activating i2c...\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300702 retval = init_cx18_i2c(cx);
703 if (retval) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300704 CX18_ERR("Could not initialize i2c\n");
705 goto free_map;
706 }
707
708 CX18_DEBUG_INFO("Active card count: %d.\n", cx18_cards_active);
709
710 if (cx->card->hw_all & CX18_HW_TVEEPROM) {
711 /* Based on the model number the cardtype may be changed.
712 The PCI IDs are not always reliable. */
713 cx18_process_eeprom(cx);
714 }
715 if (cx->card->comment)
716 CX18_INFO("%s", cx->card->comment);
717 if (cx->card->v4l2_capabilities == 0) {
718 retval = -ENODEV;
719 goto free_i2c;
720 }
721 cx18_init_memory(cx);
722
723 /* Register IRQ */
724 retval = request_irq(cx->dev->irq, cx18_irq_handler,
725 IRQF_SHARED | IRQF_DISABLED, cx->name, (void *)cx);
726 if (retval) {
727 CX18_ERR("Failed to register irq %d\n", retval);
728 goto free_i2c;
729 }
730
731 if (cx->std == 0)
732 cx->std = V4L2_STD_NTSC_M;
733
734 if (cx->options.tuner == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300735 for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
736 if ((cx->std & cx->card->tuners[i].std) == 0)
737 continue;
738 cx->options.tuner = cx->card->tuners[i].tuner;
739 break;
740 }
741 }
742 /* if no tuner was found, then pick the first tuner in the card list */
743 if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
744 cx->std = cx->card->tuners[0].std;
Hans Verkuilc3cb4d92008-06-27 23:27:25 -0300745 if (cx->std & V4L2_STD_PAL)
746 cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
747 else if (cx->std & V4L2_STD_NTSC)
748 cx->std = V4L2_STD_NTSC_M;
749 else if (cx->std & V4L2_STD_SECAM)
750 cx->std = V4L2_STD_SECAM_L;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300751 cx->options.tuner = cx->card->tuners[0].tuner;
752 }
753 if (cx->options.radio == -1)
754 cx->options.radio = (cx->card->radio_input.audio_type != 0);
755
756 /* The card is now fully identified, continue with card-specific
757 initialization. */
758 cx18_init_struct2(cx);
759
760 cx18_load_and_init_modules(cx);
761
762 if (cx->std & V4L2_STD_525_60) {
763 cx->is_60hz = 1;
764 cx->is_out_60hz = 1;
765 } else {
766 cx->is_50hz = 1;
767 cx->is_out_50hz = 1;
768 }
769 cx->params.video_gop_size = cx->is_60hz ? 15 : 12;
770
771 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = 0x08000;
772 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = 0x08000;
773 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = 0x01200;
774 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = 0x20000;
775 vbi_buf_size = cx->vbi.raw_size * (cx->is_60hz ? 24 : 36) / 2;
776 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
777
778 if (cx->options.radio > 0)
779 cx->v4l2_cap |= V4L2_CAP_RADIO;
780
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300781 if (cx->options.tuner > -1) {
782 struct tuner_setup setup;
783
784 setup.addr = ADDR_UNSET;
785 setup.type = cx->options.tuner;
786 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
787 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
788 cx18_reset_tuner_gpio : NULL;
789 cx18_call_i2c_clients(cx, TUNER_SET_TYPE_ADDR, &setup);
790 if (setup.type == TUNER_XC2028) {
791 static struct xc2028_ctrl ctrl = {
792 .fname = XC2028_DEFAULT_FIRMWARE,
793 .max_len = 64,
794 };
795 struct v4l2_priv_tun_config cfg = {
796 .tuner = cx->options.tuner,
797 .priv = &ctrl,
798 };
799 cx18_call_i2c_clients(cx, TUNER_SET_CONFIG, &cfg);
800 }
801 }
802
803 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
804 are not. */
805 cx->tuner_std = cx->std;
806
Hans Verkuil5e7fdc52008-05-30 10:51:53 -0300807 retval = cx18_streams_setup(cx);
808 if (retval) {
809 CX18_ERR("Error %d setting up streams\n", retval);
810 goto free_irq;
811 }
812 retval = cx18_streams_register(cx);
813 if (retval) {
814 CX18_ERR("Error %d registering devices\n", retval);
815 goto free_streams;
816 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300817
818 CX18_INFO("Initialized card #%d: %s\n", cx->num, cx->card_name);
819
820 return 0;
821
822free_streams:
Hans Verkuil3f983872008-05-01 10:31:12 -0300823 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300824free_irq:
825 free_irq(cx->dev->irq, (void *)cx);
826free_i2c:
827 exit_cx18_i2c(cx);
828free_map:
829 cx18_iounmap(cx);
830free_mem:
831 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
832free_workqueue:
Andy Walls572bfea2008-11-25 21:43:05 -0300833 destroy_workqueue(cx->work_queue);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300834err:
835 if (retval == 0)
836 retval = -ENODEV;
837 CX18_ERR("Error %d on initialization\n", retval);
838
Andy Wallsff086572008-10-18 08:51:28 -0300839 i = cx->num;
840 spin_lock(&cx18_cards_lock);
841 kfree(cx18_cards[i]);
842 cx18_cards[i] = NULL;
843 spin_unlock(&cx18_cards_lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300844 return retval;
845}
846
847int cx18_init_on_first_open(struct cx18 *cx)
848{
849 int video_input;
850 int fw_retry_count = 3;
851 struct v4l2_frequency vf;
Andy Walls3b6fe582008-06-21 08:36:31 -0300852 struct cx18_open_id fh;
853
854 fh.cx = cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300855
856 if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
857 return -ENXIO;
858
859 if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
860 return 0;
861
862 while (--fw_retry_count > 0) {
863 /* load firmware */
864 if (cx18_firmware_init(cx) == 0)
865 break;
866 if (fw_retry_count > 1)
867 CX18_WARN("Retry loading firmware\n");
868 }
869
870 if (fw_retry_count == 0) {
871 set_bit(CX18_F_I_FAILED, &cx->i_flags);
872 return -ENXIO;
873 }
874 set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
875
876 /* Init the firmware twice to work around a silicon bug
877 * transport related. */
878
879 fw_retry_count = 3;
880 while (--fw_retry_count > 0) {
881 /* load firmware */
882 if (cx18_firmware_init(cx) == 0)
883 break;
884 if (fw_retry_count > 1)
885 CX18_WARN("Retry loading firmware\n");
886 }
887
888 if (fw_retry_count == 0) {
889 set_bit(CX18_F_I_FAILED, &cx->i_flags);
890 return -ENXIO;
891 }
892
893 vf.tuner = 0;
894 vf.type = V4L2_TUNER_ANALOG_TV;
895 vf.frequency = 6400; /* the tuner 'baseline' frequency */
896
897 /* Set initial frequency. For PAL/SECAM broadcasts no
898 'default' channel exists AFAIK. */
899 if (cx->std == V4L2_STD_NTSC_M_JP)
900 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
901 else if (cx->std & V4L2_STD_NTSC_M)
902 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
903
904 video_input = cx->active_input;
905 cx->active_input++; /* Force update of input */
Andy Walls3b6fe582008-06-21 08:36:31 -0300906 cx18_s_input(NULL, &fh, video_input);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300907
908 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
909 in one place. */
910 cx->std++; /* Force full standard initialization */
Andy Walls3b6fe582008-06-21 08:36:31 -0300911 cx18_s_std(NULL, &fh, &cx->tuner_std);
912 cx18_s_frequency(NULL, &fh, &vf);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300913 return 0;
914}
915
Andy Walls18b5dc22008-11-16 17:15:01 -0300916static void cx18_cancel_epu_work_orders(struct cx18 *cx)
917{
918 int i;
919 for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++)
920 cancel_work_sync(&cx->epu_work_order[i].work);
921}
922
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300923static void cx18_remove(struct pci_dev *pci_dev)
924{
925 struct cx18 *cx = pci_get_drvdata(pci_dev);
926
927 CX18_DEBUG_INFO("Removing Card #%d\n", cx->num);
928
929 /* Stop all captures */
930 CX18_DEBUG_INFO("Stopping all streams\n");
Hans Verkuil31554ae2008-05-25 11:21:27 -0300931 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300932 cx18_stop_all_captures(cx);
933
934 /* Interrupts */
Andy Wallsb1526422008-08-30 16:03:44 -0300935 cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
936 cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300937
938 cx18_halt_firmware(cx);
939
Andy Walls18b5dc22008-11-16 17:15:01 -0300940 cx18_cancel_epu_work_orders(cx);
Andy Walls1d6782b2008-11-05 00:49:14 -0300941
Andy Walls572bfea2008-11-25 21:43:05 -0300942 destroy_workqueue(cx->work_queue);
943
Hans Verkuil3f983872008-05-01 10:31:12 -0300944 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300945
946 exit_cx18_i2c(cx);
947
948 free_irq(cx->dev->irq, (void *)cx);
949
Hans Verkuilcba627a2008-05-12 14:48:26 -0300950 cx18_iounmap(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300951
952 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
953
954 pci_disable_device(cx->dev);
955
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300956 CX18_INFO("Removed %s, card #%d\n", cx->card_name, cx->num);
957}
958
959/* define a pci_driver for card detection */
960static struct pci_driver cx18_pci_driver = {
961 .name = "cx18",
962 .id_table = cx18_pci_tbl,
963 .probe = cx18_probe,
964 .remove = cx18_remove,
965};
966
967static int module_start(void)
968{
969 printk(KERN_INFO "cx18: Start initialization, version %s\n", CX18_VERSION);
970
971 memset(cx18_cards, 0, sizeof(cx18_cards));
972
973 /* Validate parameters */
974 if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
Hans Verkuildd896012008-10-04 08:36:54 -0300975 printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n",
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300976 CX18_MAX_CARDS - 1);
977 return -1;
978 }
979
980 if (cx18_debug < 0 || cx18_debug > 511) {
981 cx18_debug = 0;
982 printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");
983 }
984
985 if (pci_register_driver(&cx18_pci_driver)) {
986 printk(KERN_ERR "cx18: Error detecting PCI card\n");
987 return -ENODEV;
988 }
989 printk(KERN_INFO "cx18: End initialization\n");
990 return 0;
991}
992
993static void module_cleanup(void)
994{
995 int i;
996
997 pci_unregister_driver(&cx18_pci_driver);
998
999 for (i = 0; i < cx18_cards_active; i++) {
1000 if (cx18_cards[i] == NULL)
1001 continue;
1002 kfree(cx18_cards[i]);
1003 }
Andy Walls18b5dc22008-11-16 17:15:01 -03001004
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001005}
1006
1007module_init(module_start);
1008module_exit(module_cleanup);