blob: 2ed9894672f4ea87078fe3a425cac8e647b33155 [file] [log] [blame]
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001/*
2 ivtv driver initialization and card probing
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/* Main Driver file for the ivtv project:
23 * Driver for the Conexant CX23415/CX23416 chip.
24 * Author: Kevin Thayer (nufan_wfk at yahoo.com)
25 * License: GPL
26 * http://www.ivtvdriver.org
27 *
28 * -----
29 * MPG600/MPG160 support by T.Adachi <tadachi@tadachi-net.com>
30 * and Takeru KOMORIYA<komoriya@paken.org>
31 *
32 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
33 * using information provided by Jiun-Kuei Jung @ AVerMedia.
34 *
35 * Kurouto Sikou CX23416GYC-STVLP tested by K.Ohta <alpha292@bremen.or.jp>
36 * using information from T.Adachi,Takeru KOMORIYA and others :-)
37 *
38 * Nagase TRANSGEAR 5000TV, Aopen VA2000MAX-STN6 and I/O data GV-MVP/RX
39 * version by T.Adachi. Special thanks Mr.Suzuki
40 */
41
42#include "ivtv-driver.h"
43#include "ivtv-version.h"
44#include "ivtv-fileops.h"
45#include "ivtv-i2c.h"
46#include "ivtv-firmware.h"
47#include "ivtv-queue.h"
48#include "ivtv-udma.h"
49#include "ivtv-irq.h"
50#include "ivtv-mailbox.h"
51#include "ivtv-streams.h"
52#include "ivtv-ioctl.h"
53#include "ivtv-cards.h"
54#include "ivtv-vbi.h"
55#include "ivtv-audio.h"
56#include "ivtv-gpio.h"
57#include "ivtv-yuv.h"
58
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030059#include <media/tveeprom.h>
Hans Verkuil469ba042007-07-20 06:51:58 -030060#include <media/saa7115.h>
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030061#include <media/v4l2-chip-ident.h>
62
63/* var to keep track of the number of array elements in use */
64int ivtv_cards_active = 0;
65
66/* If you have already X v4l cards, then set this to X. This way
67 the device numbers stay matched. Example: you have a WinTV card
68 without radio and a PVR-350 with. Normally this would give a
69 video1 device together with a radio0 device for the PVR. By
70 setting this to 1 you ensure that radio0 is now also radio1. */
71int ivtv_first_minor = 0;
72
73/* Master variable for all ivtv info */
74struct ivtv *ivtv_cards[IVTV_MAX_CARDS];
75
76/* Protects ivtv_cards_active */
Thomas Gleixnerb71ed5c2007-05-01 08:34:15 -030077DEFINE_SPINLOCK(ivtv_cards_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030078
79/* add your revision and whatnot here */
80static struct pci_device_id ivtv_pci_tbl[] __devinitdata = {
81 {PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV15,
82 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
83 {PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV16,
84 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
85 {0,}
86};
87
88MODULE_DEVICE_TABLE(pci,ivtv_pci_tbl);
89
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030090/* Parameter declarations */
91static int cardtype[IVTV_MAX_CARDS];
Hans Verkuil32db7752007-07-20 09:29:43 -030092static int tuner[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
93 -1, -1, -1, -1, -1, -1, -1, -1,
94 -1, -1, -1, -1, -1, -1, -1, -1,
95 -1, -1, -1, -1, -1, -1, -1, -1 };
96static int radio[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
97 -1, -1, -1, -1, -1, -1, -1, -1,
98 -1, -1, -1, -1, -1, -1, -1, -1,
99 -1, -1, -1, -1, -1, -1, -1, -1 };
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300100
101static int cardtype_c = 1;
102static int tuner_c = 1;
103static int radio_c = 1;
104static char pal[] = "--";
105static char secam[] = "--";
106static char ntsc[] = "-";
107
108/* Buffers */
109static int enc_mpg_buffers = IVTV_DEFAULT_ENC_MPG_BUFFERS;
110static int enc_yuv_buffers = IVTV_DEFAULT_ENC_YUV_BUFFERS;
111static int enc_vbi_buffers = IVTV_DEFAULT_ENC_VBI_BUFFERS;
112static int enc_pcm_buffers = IVTV_DEFAULT_ENC_PCM_BUFFERS;
113static int dec_mpg_buffers = IVTV_DEFAULT_DEC_MPG_BUFFERS;
114static int dec_yuv_buffers = IVTV_DEFAULT_DEC_YUV_BUFFERS;
115static int dec_vbi_buffers = IVTV_DEFAULT_DEC_VBI_BUFFERS;
116
117static int ivtv_yuv_mode = 0;
Ian Armstrong9e0df402007-03-16 07:44:42 -0300118static int ivtv_yuv_threshold=-1;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300119static int ivtv_pci_latency = 1;
120
121int ivtv_debug = 0;
122
Hans Verkuil31ec1352007-03-03 08:50:42 -0300123static int newi2c = -1;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300124
125module_param_array(tuner, int, &tuner_c, 0644);
126module_param_array(radio, bool, &radio_c, 0644);
127module_param_array(cardtype, int, &cardtype_c, 0644);
128module_param_string(pal, pal, sizeof(pal), 0644);
129module_param_string(secam, secam, sizeof(secam), 0644);
130module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
131module_param_named(debug,ivtv_debug, int, 0644);
132module_param(ivtv_pci_latency, int, 0644);
133module_param(ivtv_yuv_mode, int, 0644);
134module_param(ivtv_yuv_threshold, int, 0644);
135module_param(ivtv_first_minor, int, 0644);
136
137module_param(enc_mpg_buffers, int, 0644);
138module_param(enc_yuv_buffers, int, 0644);
139module_param(enc_vbi_buffers, int, 0644);
140module_param(enc_pcm_buffers, int, 0644);
141module_param(dec_mpg_buffers, int, 0644);
142module_param(dec_yuv_buffers, int, 0644);
143module_param(dec_vbi_buffers, int, 0644);
144
145module_param(newi2c, int, 0644);
146
147MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
148 "\t\t\tsee tuner.h for values");
149MODULE_PARM_DESC(radio,
150 "Enable or disable the radio. Use only if autodetection\n"
151 "\t\t\tfails. 0 = disable, 1 = enable");
152MODULE_PARM_DESC(cardtype,
153 "Only use this option if your card is not detected properly.\n"
154 "\t\tSpecify card type:\n"
155 "\t\t\t 1 = WinTV PVR 250\n"
156 "\t\t\t 2 = WinTV PVR 350\n"
157 "\t\t\t 3 = WinTV PVR-150 or PVR-500\n"
158 "\t\t\t 4 = AVerMedia M179\n"
159 "\t\t\t 5 = YUAN MPG600/Kuroutoshikou iTVC16-STVLP\n"
160 "\t\t\t 6 = YUAN MPG160/Kuroutoshikou iTVC15-STVLP\n"
161 "\t\t\t 7 = YUAN PG600/DIAMONDMM PVR-550 (CX Falcon 2)\n"
162 "\t\t\t 8 = Adaptec AVC-2410\n"
163 "\t\t\t 9 = Adaptec AVC-2010\n"
164 "\t\t\t10 = NAGASE TRANSGEAR 5000TV\n"
165 "\t\t\t11 = AOpen VA2000MAX-STN6\n"
166 "\t\t\t12 = YUAN MPG600GR/Kuroutoshikou CX23416GYC-STVLP\n"
167 "\t\t\t13 = I/O Data GV-MVP/RX\n"
168 "\t\t\t14 = I/O Data GV-MVP/RX2E\n"
169 "\t\t\t15 = GOTVIEW PCI DVD\n"
170 "\t\t\t16 = GOTVIEW PCI DVD2 Deluxe\n"
171 "\t\t\t17 = Yuan MPC622\n"
172 "\t\t\t18 = Digital Cowboy DCT-MTVP1\n"
Hans Verkuil37297802007-09-11 11:59:15 -0300173 "\t\t\t19 = Yuan PG600V2/GotView PCI DVD Lite\n"
174 "\t\t\t20 = Club3D ZAP-TV1x01\n"
175 "\t\t\t21 = AverTV MCE 116 Plus\n"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300176 "\t\t\t 0 = Autodetect (default)\n"
177 "\t\t\t-1 = Ignore this card\n\t\t");
178MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
179MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
180MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
181MODULE_PARM_DESC(debug,
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300182 "Debug level (bitmask). Default: 0\n"
183 "\t\t\t 1/0x0001: warning\n"
184 "\t\t\t 2/0x0002: info\n"
185 "\t\t\t 4/0x0004: mailbox\n"
186 "\t\t\t 8/0x0008: ioctl\n"
187 "\t\t\t 16/0x0010: file\n"
188 "\t\t\t 32/0x0020: dma\n"
189 "\t\t\t 64/0x0040: irq\n"
190 "\t\t\t 128/0x0080: decoder\n"
191 "\t\t\t 256/0x0100: yuv\n"
192 "\t\t\t 512/0x0200: i2c\n"
193 "\t\t\t1024/0x0400: high volume\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300194MODULE_PARM_DESC(ivtv_pci_latency,
195 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
196 "\t\t\tDefault: Yes");
197MODULE_PARM_DESC(ivtv_yuv_mode,
198 "Specify the yuv playback mode:\n"
199 "\t\t\t0 = interlaced\n\t\t\t1 = progressive\n\t\t\t2 = auto\n"
200 "\t\t\tDefault: 0 (interlaced)");
201MODULE_PARM_DESC(ivtv_yuv_threshold,
202 "If ivtv_yuv_mode is 2 (auto) then playback content as\n\t\tprogressive if src height <= ivtv_yuvthreshold\n"
203 "\t\t\tDefault: 480");;
204MODULE_PARM_DESC(enc_mpg_buffers,
205 "Encoder MPG Buffers (in MB)\n"
206 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_MPG_BUFFERS));
207MODULE_PARM_DESC(enc_yuv_buffers,
208 "Encoder YUV Buffers (in MB)\n"
209 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_YUV_BUFFERS));
210MODULE_PARM_DESC(enc_vbi_buffers,
211 "Encoder VBI Buffers (in MB)\n"
212 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_VBI_BUFFERS));
213MODULE_PARM_DESC(enc_pcm_buffers,
Hans Verkuil313e91e2007-08-19 05:32:33 -0300214 "Encoder PCM buffers (in kB)\n"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300215 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_PCM_BUFFERS));
216MODULE_PARM_DESC(dec_mpg_buffers,
217 "Decoder MPG buffers (in MB)\n"
218 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_MPG_BUFFERS));
219MODULE_PARM_DESC(dec_yuv_buffers,
220 "Decoder YUV buffers (in MB)\n"
221 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_YUV_BUFFERS));
222MODULE_PARM_DESC(dec_vbi_buffers,
Hans Verkuil313e91e2007-08-19 05:32:33 -0300223 "Decoder VBI buffers (in kB)\n"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300224 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_VBI_BUFFERS));
225MODULE_PARM_DESC(newi2c,
226 "Use new I2C implementation\n"
227 "\t\t\t-1 is autodetect, 0 is off, 1 is on\n"
228 "\t\t\tDefault is autodetect");
229
230MODULE_PARM_DESC(ivtv_first_minor, "Set minor assigned to first card");
231
232MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil");
233MODULE_DESCRIPTION("CX23415/CX23416 driver");
234MODULE_SUPPORTED_DEVICE
235 ("CX23415/CX23416 MPEG2 encoder (WinTV PVR-150/250/350/500,\n"
236 "\t\t\tYuan MPG series and similar)");
237MODULE_LICENSE("GPL");
238
239MODULE_VERSION(IVTV_VERSION);
240
241void ivtv_clear_irq_mask(struct ivtv *itv, u32 mask)
242{
243 itv->irqmask &= ~mask;
244 write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
245}
246
247void ivtv_set_irq_mask(struct ivtv *itv, u32 mask)
248{
249 itv->irqmask |= mask;
250 write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
251}
252
253int ivtv_set_output_mode(struct ivtv *itv, int mode)
254{
255 int old_mode;
256
257 spin_lock(&itv->lock);
258 old_mode = itv->output_mode;
259 if (old_mode == 0)
260 itv->output_mode = old_mode = mode;
261 spin_unlock(&itv->lock);
262 return old_mode;
263}
264
265struct ivtv_stream *ivtv_get_output_stream(struct ivtv *itv)
266{
267 switch (itv->output_mode) {
268 case OUT_MPG:
269 return &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
270 case OUT_YUV:
271 return &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
272 default:
273 return NULL;
274 }
275}
276
277int ivtv_waitq(wait_queue_head_t *waitq)
278{
279 DEFINE_WAIT(wait);
280
281 prepare_to_wait(waitq, &wait, TASK_INTERRUPTIBLE);
282 schedule();
283 finish_wait(waitq, &wait);
284 return signal_pending(current) ? -EINTR : 0;
285}
286
287/* Generic utility functions */
Mauro Carvalho Chehab201700d2007-07-19 11:21:04 -0300288int ivtv_msleep_timeout(unsigned int msecs, int intr)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300289{
290 int ret;
Mauro Carvalho Chehab201700d2007-07-19 11:21:04 -0300291 int timeout = msecs_to_jiffies(msecs);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300292
293 do {
294 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
295 timeout = schedule_timeout(timeout);
296 if (intr && (ret = signal_pending(current)))
297 return ret;
298 } while (timeout);
299 return 0;
300}
301
302/* Release ioremapped memory */
303static void ivtv_iounmap(struct ivtv *itv)
304{
305 if (itv == NULL)
306 return;
307
308 /* Release registers memory */
309 if (itv->reg_mem != NULL) {
310 IVTV_DEBUG_INFO("releasing reg_mem\n");
311 iounmap(itv->reg_mem);
312 itv->reg_mem = NULL;
313 }
314 /* Release io memory */
315 if (itv->has_cx23415 && itv->dec_mem != NULL) {
316 IVTV_DEBUG_INFO("releasing dec_mem\n");
317 iounmap(itv->dec_mem);
318 }
319 itv->dec_mem = NULL;
320
321 /* Release io memory */
322 if (itv->enc_mem != NULL) {
323 IVTV_DEBUG_INFO("releasing enc_mem\n");
324 iounmap(itv->enc_mem);
325 itv->enc_mem = NULL;
326 }
327}
328
329/* Hauppauge card? get values from tveeprom */
330void ivtv_read_eeprom(struct ivtv *itv, struct tveeprom *tv)
331{
332 u8 eedata[256];
333
334 itv->i2c_client.addr = 0xA0 >> 1;
335 tveeprom_read(&itv->i2c_client, eedata, sizeof(eedata));
336 tveeprom_hauppauge_analog(&itv->i2c_client, tv, eedata);
337}
338
339static void ivtv_process_eeprom(struct ivtv *itv)
340{
341 struct tveeprom tv;
342 int pci_slot = PCI_SLOT(itv->dev->devfn);
343
344 ivtv_read_eeprom(itv, &tv);
345
346 /* Many thanks to Steven Toth from Hauppauge for providing the
347 model numbers */
348 switch (tv.model) {
349 /* In a few cases the PCI subsystem IDs do not correctly
350 identify the card. A better method is to check the
351 model number from the eeprom instead. */
Hans Verkuil1b9d3132007-05-18 16:18:17 -0300352 case 30012 ... 30039: /* Low profile PVR250 */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300353 case 32000 ... 32999:
354 case 48000 ... 48099: /* 48??? range are PVR250s with a cx23415 */
355 case 48400 ... 48599:
356 itv->card = ivtv_get_card(IVTV_CARD_PVR_250);
357 break;
358 case 48100 ... 48399:
359 case 48600 ... 48999:
360 itv->card = ivtv_get_card(IVTV_CARD_PVR_350);
361 break;
362 case 23000 ... 23999: /* PVR500 */
363 case 25000 ... 25999: /* Low profile PVR150 */
364 case 26000 ... 26999: /* Regular PVR150 */
365 itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
366 break;
367 case 0:
368 IVTV_ERR("Invalid EEPROM\n");
369 return;
370 default:
371 IVTV_ERR("Unknown model %d, defaulting to PVR-150\n", tv.model);
372 itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
373 break;
374 }
375
376 switch (tv.model) {
377 /* Old style PVR350 (with an saa7114) uses this input for
378 the tuner. */
379 case 48254:
380 itv->card = ivtv_get_card(IVTV_CARD_PVR_350_V1);
381 break;
382 default:
383 break;
384 }
385
386 itv->v4l2_cap = itv->card->v4l2_capabilities;
387 itv->card_name = itv->card->name;
388
389 /* If this is a PVR500 then it should be possible to detect whether it is the
390 first or second unit by looking at the subsystem device ID: is bit 4 is
391 set, then it is the second unit (according to info from Hauppauge).
392
393 However, while this works for most cards, I have seen a few PVR500 cards
394 where both units have the same subsystem ID.
395
396 So instead I look at the reported 'PCI slot' (which is the slot on the PVR500
397 PCI bridge) and if it is 8, then it is assumed to be the first unit, otherwise
398 it is the second unit. It is possible that it is a different slot when ivtv is
399 used in Xen, in that case I ignore this card here. The worst that can happen
400 is that the card presents itself with a non-working radio device.
401
402 This detection is needed since the eeprom reports incorrectly that a radio is
403 present on the second unit. */
404 if (tv.model / 1000 == 23) {
405 itv->card_name = "WinTV PVR 500";
406 if (pci_slot == 8 || pci_slot == 9) {
407 int is_first = (pci_slot & 1) == 0;
408
409 itv->card_name = is_first ? "WinTV PVR 500 (unit #1)" :
410 "WinTV PVR 500 (unit #2)";
411 if (!is_first) {
412 IVTV_INFO("Correcting tveeprom data: no radio present on second unit\n");
413 tv.has_radio = 0;
414 }
415 }
416 }
417 IVTV_INFO("Autodetected %s\n", itv->card_name);
418
419 switch (tv.tuner_hauppauge_model) {
420 case 85:
421 case 99:
422 case 112:
423 itv->pvr150_workaround = 1;
424 break;
425 default:
426 break;
427 }
428 if (tv.tuner_type == TUNER_ABSENT)
429 IVTV_ERR("tveeprom cannot autodetect tuner!");
430
431 if (itv->options.tuner == -1)
432 itv->options.tuner = tv.tuner_type;
433 if (itv->options.radio == -1)
434 itv->options.radio = (tv.has_radio != 0);
435 /* only enable newi2c if an IR blaster is present */
436 /* FIXME: for 2.6.20 the test against 2 should be removed */
437 if (itv->options.newi2c == -1 && tv.has_ir != -1 && tv.has_ir != 2) {
438 itv->options.newi2c = (tv.has_ir & 2) ? 1 : 0;
439 if (itv->options.newi2c) {
Hans Verkuilae38d932007-07-17 13:42:43 -0300440 IVTV_INFO("Reopen i2c bus for IR-blaster support\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300441 exit_ivtv_i2c(itv);
442 init_ivtv_i2c(itv);
443 }
444 }
445
446 if (itv->std != 0)
447 /* user specified tuner standard */
448 return;
449
450 /* autodetect tuner standard */
451 if (tv.tuner_formats & V4L2_STD_PAL) {
452 IVTV_DEBUG_INFO("PAL tuner detected\n");
453 itv->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
454 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
455 IVTV_DEBUG_INFO("NTSC tuner detected\n");
456 itv->std |= V4L2_STD_NTSC_M;
457 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
458 IVTV_DEBUG_INFO("SECAM tuner detected\n");
459 itv->std |= V4L2_STD_SECAM_L;
460 } else {
461 IVTV_INFO("No tuner detected, default to NTSC-M\n");
462 itv->std |= V4L2_STD_NTSC_M;
463 }
464}
465
466static v4l2_std_id ivtv_parse_std(struct ivtv *itv)
467{
468 switch (pal[0]) {
469 case '6':
470 return V4L2_STD_PAL_60;
471 case 'b':
472 case 'B':
473 case 'g':
474 case 'G':
475 return V4L2_STD_PAL_BG;
476 case 'h':
477 case 'H':
478 return V4L2_STD_PAL_H;
479 case 'n':
480 case 'N':
481 if (pal[1] == 'c' || pal[1] == 'C')
482 return V4L2_STD_PAL_Nc;
483 return V4L2_STD_PAL_N;
484 case 'i':
485 case 'I':
486 return V4L2_STD_PAL_I;
487 case 'd':
488 case 'D':
489 case 'k':
490 case 'K':
491 return V4L2_STD_PAL_DK;
492 case 'M':
493 case 'm':
494 return V4L2_STD_PAL_M;
495 case '-':
496 break;
497 default:
498 IVTV_WARN("pal= argument not recognised\n");
499 return 0;
500 }
501
502 switch (secam[0]) {
503 case 'b':
504 case 'B':
505 case 'g':
506 case 'G':
507 case 'h':
508 case 'H':
509 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
510 case 'd':
511 case 'D':
512 case 'k':
513 case 'K':
514 return V4L2_STD_SECAM_DK;
515 case 'l':
516 case 'L':
517 if (secam[1] == 'C' || secam[1] == 'c')
518 return V4L2_STD_SECAM_LC;
519 return V4L2_STD_SECAM_L;
520 case '-':
521 break;
522 default:
523 IVTV_WARN("secam= argument not recognised\n");
524 return 0;
525 }
526
527 switch (ntsc[0]) {
528 case 'm':
529 case 'M':
530 return V4L2_STD_NTSC_M;
531 case 'j':
532 case 'J':
533 return V4L2_STD_NTSC_M_JP;
534 case 'k':
535 case 'K':
536 return V4L2_STD_NTSC_M_KR;
537 case '-':
538 break;
539 default:
540 IVTV_WARN("ntsc= argument not recognised\n");
541 return 0;
542 }
543
544 /* no match found */
545 return 0;
546}
547
548static void ivtv_process_options(struct ivtv *itv)
549{
550 const char *chipname;
551 int i, j;
552
Hans Verkuil313e91e2007-08-19 05:32:33 -0300553 itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers * 1024;
554 itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers * 1024;
555 itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers * 1024;
556 itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
557 itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_MPG] = dec_mpg_buffers * 1024;
558 itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_YUV] = dec_yuv_buffers * 1024;
559 itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_VBI] = dec_vbi_buffers;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300560 itv->options.cardtype = cardtype[itv->num];
561 itv->options.tuner = tuner[itv->num];
562 itv->options.radio = radio[itv->num];
563 itv->options.newi2c = newi2c;
564
565 itv->std = ivtv_parse_std(itv);
566 itv->has_cx23415 = (itv->dev->device == PCI_DEVICE_ID_IVTV15);
567 chipname = itv->has_cx23415 ? "cx23415" : "cx23416";
568 if (itv->options.cardtype == -1) {
569 IVTV_INFO("Ignore card (detected %s based chip)\n", chipname);
570 return;
571 }
572 if ((itv->card = ivtv_get_card(itv->options.cardtype - 1))) {
573 IVTV_INFO("User specified %s card (detected %s based chip)\n",
574 itv->card->name, chipname);
575 } else if (itv->options.cardtype != 0) {
576 IVTV_ERR("Unknown user specified type, trying to autodetect card\n");
577 }
578 if (itv->card == NULL) {
579 if (itv->dev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE ||
580 itv->dev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT1 ||
581 itv->dev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT2) {
582 itv->card = ivtv_get_card(itv->has_cx23415 ? IVTV_CARD_PVR_350 : IVTV_CARD_PVR_150);
583 IVTV_INFO("Autodetected Hauppauge card (%s based)\n",
584 chipname);
585 }
586 }
587 if (itv->card == NULL) {
588 for (i = 0; (itv->card = ivtv_get_card(i)); i++) {
589 if (itv->card->pci_list == NULL)
590 continue;
591 for (j = 0; itv->card->pci_list[j].device; j++) {
592 if (itv->dev->device !=
593 itv->card->pci_list[j].device)
594 continue;
595 if (itv->dev->subsystem_vendor !=
596 itv->card->pci_list[j].subsystem_vendor)
597 continue;
598 if (itv->dev->subsystem_device !=
599 itv->card->pci_list[j].subsystem_device)
600 continue;
601 IVTV_INFO("Autodetected %s card (%s based)\n",
602 itv->card->name, chipname);
603 goto done;
604 }
605 }
606 }
607done:
608
609 if (itv->card == NULL) {
610 itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
611 IVTV_ERR("Unknown card: vendor/device: %04x/%04x\n",
612 itv->dev->vendor, itv->dev->device);
613 IVTV_ERR(" subsystem vendor/device: %04x/%04x\n",
614 itv->dev->subsystem_vendor, itv->dev->subsystem_device);
615 IVTV_ERR(" %s based\n", chipname);
616 IVTV_ERR("Defaulting to %s card\n", itv->card->name);
617 IVTV_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
618 IVTV_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
619 IVTV_ERR("Prefix your subject line with [UNKNOWN CARD].\n");
620 }
621 itv->v4l2_cap = itv->card->v4l2_capabilities;
622 itv->card_name = itv->card->name;
623}
624
625/* Precondition: the ivtv structure has been memset to 0. Only
626 the dev and num fields have been filled in.
627 No assumptions on the card type may be made here (see ivtv_init_struct2
628 for that).
629 */
630static int __devinit ivtv_init_struct1(struct ivtv *itv)
631{
632 itv->base_addr = pci_resource_start(itv->dev, 0);
633 itv->enc_mbox.max_mbox = 2; /* the encoder has 3 mailboxes (0-2) */
634 itv->dec_mbox.max_mbox = 1; /* the decoder has 2 mailboxes (0-1) */
635
Hans Verkuilf8859692007-07-10 14:58:33 -0300636 mutex_init(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300637 mutex_init(&itv->i2c_bus_lock);
638 mutex_init(&itv->udma.lock);
639
Hans Verkuild6102902007-03-10 20:09:07 -0300640 spin_lock_init(&itv->lock);
641 spin_lock_init(&itv->dma_reg_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300642
Hans Verkuil1e13f9e2007-03-10 06:52:02 -0300643 itv->irq_work_queues = create_workqueue(itv->name);
644 if (itv->irq_work_queues == NULL) {
645 IVTV_ERR("Could not create ivtv workqueue\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300646 return -1;
647 }
648
Hans Verkuil1e13f9e2007-03-10 06:52:02 -0300649 INIT_WORK(&itv->irq_work_queue, ivtv_irq_work_handler);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300650
651 /* start counting open_id at 1 */
652 itv->open_id = 1;
653
654 /* Initial settings */
655 cx2341x_fill_defaults(&itv->params);
656 itv->params.port = CX2341X_PORT_MEMORY;
657 itv->params.capabilities = CX2341X_CAP_HAS_SLICED_VBI;
658 init_waitqueue_head(&itv->cap_w);
659 init_waitqueue_head(&itv->event_waitq);
660 init_waitqueue_head(&itv->vsync_waitq);
661 init_waitqueue_head(&itv->dma_waitq);
662 init_timer(&itv->dma_timer);
663 itv->dma_timer.function = ivtv_unfinished_dma;
664 itv->dma_timer.data = (unsigned long)itv;
665
666 itv->cur_dma_stream = -1;
Hans Verkuildc02d502007-05-19 14:07:16 -0300667 itv->cur_pio_stream = -1;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300668 itv->audio_stereo_mode = AUDIO_STEREO;
669 itv->audio_bilingual_mode = AUDIO_MONO_LEFT;
670
671 /* Ctrls */
672 itv->speed = 1000;
673
674 /* VBI */
675 itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
676 itv->vbi.sliced_in = &itv->vbi.in.fmt.sliced;
677
678 /* OSD */
679 itv->osd_global_alpha_state = 1;
680 itv->osd_global_alpha = 255;
681
682 /* YUV */
683 atomic_set(&itv->yuv_info.next_dma_frame, -1);
684 itv->yuv_info.lace_mode = ivtv_yuv_mode;
685 itv->yuv_info.lace_threshold = ivtv_yuv_threshold;
686 return 0;
687}
688
689/* Second initialization part. Here the card type has been
690 autodetected. */
691static void __devinit ivtv_init_struct2(struct ivtv *itv)
692{
693 int i;
694
695 for (i = 0; i < IVTV_CARD_MAX_VIDEO_INPUTS; i++)
696 if (itv->card->video_inputs[i].video_type == 0)
697 break;
698 itv->nof_inputs = i;
699 for (i = 0; i < IVTV_CARD_MAX_AUDIO_INPUTS; i++)
700 if (itv->card->audio_inputs[i].audio_type == 0)
701 break;
702 itv->nof_audio_inputs = i;
703
704 /* 0x00EF = saa7114(239) 0x00F0 = saa7115(240) 0x0106 = micro */
705 if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X))
706 itv->digitizer = 0xF1;
707 else if (itv->card->hw_all & IVTV_HW_SAA7114)
708 itv->digitizer = 0xEF;
709 else /* cx25840 */
710 itv->digitizer = 0x140;
711
712 if (itv->card->hw_all & IVTV_HW_CX25840) {
713 itv->vbi.sliced_size = 288; /* multiple of 16, real size = 284 */
714 } else {
715 itv->vbi.sliced_size = 64; /* multiple of 16, real size = 52 */
716 }
717
718 /* Find tuner input */
719 for (i = 0; i < itv->nof_inputs; i++) {
720 if (itv->card->video_inputs[i].video_type ==
721 IVTV_CARD_INPUT_VID_TUNER)
722 break;
723 }
724 if (i == itv->nof_inputs)
725 i = 0;
726 itv->active_input = i;
727 itv->audio_input = itv->card->video_inputs[i].audio_index;
728 if (itv->card->hw_all & IVTV_HW_CX25840)
729 itv->video_dec_func = ivtv_cx25840;
730 else if (itv->card->hw_all & IVTV_HW_SAA717X)
731 itv->video_dec_func = ivtv_saa717x;
732 else
733 itv->video_dec_func = ivtv_saa7115;
734}
735
736static int ivtv_setup_pci(struct ivtv *itv, struct pci_dev *dev,
737 const struct pci_device_id *pci_id)
738{
739 u16 cmd;
740 unsigned char pci_latency;
741
742 IVTV_DEBUG_INFO("Enabling pci device\n");
743
744 if (pci_enable_device(dev)) {
745 IVTV_ERR("Can't enable device %d!\n", itv->num);
746 return -EIO;
747 }
748 if (pci_set_dma_mask(dev, 0xffffffff)) {
749 IVTV_ERR("No suitable DMA available on card %d.\n", itv->num);
750 return -EIO;
751 }
752 if (!request_mem_region(itv->base_addr, IVTV_ENCODER_SIZE, "ivtv encoder")) {
753 IVTV_ERR("Cannot request encoder memory region on card %d.\n", itv->num);
754 return -EIO;
755 }
756
757 if (!request_mem_region(itv->base_addr + IVTV_REG_OFFSET,
758 IVTV_REG_SIZE, "ivtv registers")) {
759 IVTV_ERR("Cannot request register memory region on card %d.\n", itv->num);
760 release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
761 return -EIO;
762 }
763
764 if (itv->has_cx23415 &&
765 !request_mem_region(itv->base_addr + IVTV_DECODER_OFFSET,
766 IVTV_DECODER_SIZE, "ivtv decoder")) {
767 IVTV_ERR("Cannot request decoder memory region on card %d.\n", itv->num);
768 release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
769 release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
770 return -EIO;
771 }
772
773 /* Check for bus mastering */
774 pci_read_config_word(dev, PCI_COMMAND, &cmd);
775 if (!(cmd & PCI_COMMAND_MASTER)) {
776 IVTV_DEBUG_INFO("Attempting to enable Bus Mastering\n");
777 pci_set_master(dev);
778 pci_read_config_word(dev, PCI_COMMAND, &cmd);
779 if (!(cmd & PCI_COMMAND_MASTER)) {
780 IVTV_ERR("Bus Mastering is not enabled\n");
781 return -ENXIO;
782 }
783 }
784 IVTV_DEBUG_INFO("Bus Mastering Enabled.\n");
785
786 pci_read_config_byte(dev, PCI_CLASS_REVISION, &itv->card_rev);
787 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
788
789 if (pci_latency < 64 && ivtv_pci_latency) {
790 IVTV_INFO("Unreasonably low latency timer, "
791 "setting to 64 (was %d)\n", pci_latency);
792 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
793 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &pci_latency);
794 }
795 /* This config space value relates to DMA latencies. The
796 default value 0x8080 is too low however and will lead
797 to DMA errors. 0xffff is the max value which solves
798 these problems. */
799 pci_write_config_dword(dev, 0x40, 0xffff);
800
801 IVTV_DEBUG_INFO("%d (rev %d) at %02x:%02x.%x, "
802 "irq: %d, latency: %d, memory: 0x%lx\n",
803 itv->dev->device, itv->card_rev, dev->bus->number,
804 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
805 itv->dev->irq, pci_latency, (unsigned long)itv->base_addr);
806
807 return 0;
808}
809
810static void ivtv_request_module(struct ivtv *itv, const char *name)
811{
812 if (request_module(name) != 0) {
813 IVTV_ERR("Failed to load module %s\n", name);
814 } else {
815 IVTV_DEBUG_INFO("Loaded module %s\n", name);
816 }
817}
818
819static void ivtv_load_and_init_modules(struct ivtv *itv)
820{
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300821 u32 hw = itv->card->hw_all;
822 int i;
823
824 /* load modules */
825#ifndef CONFIG_VIDEO_TUNER
826 if (hw & IVTV_HW_TUNER) {
Hans Verkuil37297802007-09-11 11:59:15 -0300827 if (itv->options.tuner == TUNER_XCEIVE_XC3028) {
828 IVTV_INFO("Xceive tuner not yet supported, only composite and S-Video inputs will be available\n");
829 itv->tunerid = 1;
830 }
831 else {
832 ivtv_request_module(itv, "tuner");
833 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300834 }
835#endif
836#ifndef CONFIG_VIDEO_CX25840
837 if (hw & IVTV_HW_CX25840)
838 ivtv_request_module(itv, "cx25840");
839#endif
840#ifndef CONFIG_VIDEO_SAA711X
841 if (hw & IVTV_HW_SAA711X)
842 ivtv_request_module(itv, "saa7115");
843#endif
844#ifndef CONFIG_VIDEO_SAA7127
845 if (hw & IVTV_HW_SAA7127)
846 ivtv_request_module(itv, "saa7127");
847#endif
848 if (hw & IVTV_HW_SAA717X)
849 ivtv_request_module(itv, "saa717x");
850#ifndef CONFIG_VIDEO_UPD64031A
851 if (hw & IVTV_HW_UPD64031A)
852 ivtv_request_module(itv, "upd64031a");
853#endif
854#ifndef CONFIG_VIDEO_UPD64083
855 if (hw & IVTV_HW_UPD6408X)
856 ivtv_request_module(itv, "upd64083");
857#endif
858#ifndef CONFIG_VIDEO_MSP3400
859 if (hw & IVTV_HW_MSP34XX)
860 ivtv_request_module(itv, "msp3400");
861#endif
Hans Verkuilac247432007-07-27 06:56:50 -0300862#ifndef CONFIG_VIDEO_VP27SMPX
863 if (hw & IVTV_HW_VP27SMPX)
864 ivtv_request_module(itv, "vp27smpx");
865#endif
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300866 if (hw & IVTV_HW_TVAUDIO)
867 ivtv_request_module(itv, "tvaudio");
868#ifndef CONFIG_VIDEO_WM8775
869 if (hw & IVTV_HW_WM8775)
870 ivtv_request_module(itv, "wm8775");
871#endif
872#ifndef CONFIG_VIDEO_WM8739
873 if (hw & IVTV_HW_WM8739)
874 ivtv_request_module(itv, "wm8739");
875#endif
876#ifndef CONFIG_VIDEO_CS53L32A
877 if (hw & IVTV_HW_CS53L32A)
878 ivtv_request_module(itv, "cs53l32a");
879#endif
880
881 /* check which i2c devices are actually found */
882 for (i = 0; i < 32; i++) {
883 u32 device = 1 << i;
884
885 if (!(device & hw))
886 continue;
887 if (device == IVTV_HW_GPIO) {
888 /* GPIO is always available */
889 itv->hw_flags |= IVTV_HW_GPIO;
890 continue;
891 }
892 if (ivtv_i2c_hw_addr(itv, device) > 0)
893 itv->hw_flags |= device;
894 }
895
896 hw = itv->hw_flags;
897
898 if (itv->card->type == IVTV_CARD_CX23416GYC) {
899 /* Several variations of this card exist, detect which card
900 type should be used. */
901 if ((hw & (IVTV_HW_UPD64031A | IVTV_HW_UPD6408X)) == 0)
902 itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGRYCS);
903 else if ((hw & IVTV_HW_UPD64031A) == 0)
904 itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGR);
905 }
Hans Verkuil469ba042007-07-20 06:51:58 -0300906 else if (itv->card->type == IVTV_CARD_GV_MVPRX ||
907 itv->card->type == IVTV_CARD_GV_MVPRX2E) {
908 struct v4l2_crystal_freq crystal_freq;
909
910 /* The crystal frequency of GVMVPRX is 24.576MHz */
911 crystal_freq.freq = SAA7115_FREQ_24_576_MHZ;
912 crystal_freq.flags = SAA7115_FREQ_FL_UCGC;
913 itv->video_dec_func(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq);
914 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300915
916 if (hw & IVTV_HW_CX25840) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300917 itv->vbi.raw_decoder_line_size = 1444;
918 itv->vbi.raw_decoder_sav_odd_field = 0x20;
919 itv->vbi.raw_decoder_sav_even_field = 0x60;
920 itv->vbi.sliced_decoder_line_size = 272;
921 itv->vbi.sliced_decoder_sav_odd_field = 0xB0;
922 itv->vbi.sliced_decoder_sav_even_field = 0xF0;
923 }
924
925 if (hw & IVTV_HW_SAA711X) {
926 struct v4l2_chip_ident v = { V4L2_CHIP_MATCH_I2C_DRIVER, I2C_DRIVERID_SAA711X };
927
928 /* determine the exact saa711x model */
929 itv->hw_flags &= ~IVTV_HW_SAA711X;
930
931 ivtv_saa7115(itv, VIDIOC_G_CHIP_IDENT, &v);
932 if (v.ident == V4L2_IDENT_SAA7114) {
933 itv->hw_flags |= IVTV_HW_SAA7114;
934 /* VBI is not yet supported by the saa7114 driver. */
935 itv->v4l2_cap &= ~(V4L2_CAP_SLICED_VBI_CAPTURE|V4L2_CAP_VBI_CAPTURE);
936 }
937 else {
938 itv->hw_flags |= IVTV_HW_SAA7115;
939 }
940 itv->vbi.raw_decoder_line_size = 1443;
941 itv->vbi.raw_decoder_sav_odd_field = 0x25;
942 itv->vbi.raw_decoder_sav_even_field = 0x62;
943 itv->vbi.sliced_decoder_line_size = 51;
944 itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
945 itv->vbi.sliced_decoder_sav_even_field = 0xEC;
946 }
947
948 if (hw & IVTV_HW_SAA717X) {
949 itv->vbi.raw_decoder_line_size = 1443;
950 itv->vbi.raw_decoder_sav_odd_field = 0x25;
951 itv->vbi.raw_decoder_sav_even_field = 0x62;
952 itv->vbi.sliced_decoder_line_size = 51;
953 itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
954 itv->vbi.sliced_decoder_sav_even_field = 0xEC;
955 }
956}
957
958static int __devinit ivtv_probe(struct pci_dev *dev,
959 const struct pci_device_id *pci_id)
960{
961 int retval = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300962 int yuv_buf_size;
963 int vbi_buf_size;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300964 struct ivtv *itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300965
966 spin_lock(&ivtv_cards_lock);
967
968 /* Make sure we've got a place for this card */
969 if (ivtv_cards_active == IVTV_MAX_CARDS) {
Hans Verkuilae38d932007-07-17 13:42:43 -0300970 printk(KERN_ERR "ivtv: Maximum number of cards detected (%d)\n",
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300971 ivtv_cards_active);
972 spin_unlock(&ivtv_cards_lock);
973 return -ENOMEM;
974 }
975
976 itv = kzalloc(sizeof(struct ivtv), GFP_ATOMIC);
977 if (itv == 0) {
978 spin_unlock(&ivtv_cards_lock);
979 return -ENOMEM;
980 }
981 ivtv_cards[ivtv_cards_active] = itv;
982 itv->dev = dev;
983 itv->num = ivtv_cards_active++;
984 snprintf(itv->name, sizeof(itv->name) - 1, "ivtv%d", itv->num);
Hans Verkuilae38d932007-07-17 13:42:43 -0300985 IVTV_INFO("Initializing card #%d\n", itv->num);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300986
987 spin_unlock(&ivtv_cards_lock);
988
989 ivtv_process_options(itv);
990 if (itv->options.cardtype == -1) {
991 retval = -ENODEV;
992 goto err;
993 }
994 if (ivtv_init_struct1(itv)) {
995 retval = -ENOMEM;
996 goto err;
997 }
998
999 IVTV_DEBUG_INFO("base addr: 0x%08x\n", itv->base_addr);
1000
1001 /* PCI Device Setup */
1002 if ((retval = ivtv_setup_pci(itv, dev, pci_id)) != 0) {
1003 if (retval == -EIO)
1004 goto free_workqueue;
1005 else if (retval == -ENXIO)
1006 goto free_mem;
1007 }
1008 /* save itv in the pci struct for later use */
1009 pci_set_drvdata(dev, itv);
1010
1011 /* map io memory */
1012 IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
1013 itv->base_addr + IVTV_ENCODER_OFFSET, IVTV_ENCODER_SIZE);
1014 itv->enc_mem = ioremap_nocache(itv->base_addr + IVTV_ENCODER_OFFSET,
1015 IVTV_ENCODER_SIZE);
1016 if (!itv->enc_mem) {
1017 IVTV_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
1018 IVTV_ERR("or disabling CONFIG_HIMEM4G into the kernel would help\n");
1019 retval = -ENOMEM;
1020 goto free_mem;
1021 }
1022
1023 if (itv->has_cx23415) {
1024 IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
1025 itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
1026 itv->dec_mem = ioremap_nocache(itv->base_addr + IVTV_DECODER_OFFSET,
1027 IVTV_DECODER_SIZE);
1028 if (!itv->dec_mem) {
1029 IVTV_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
1030 IVTV_ERR("or disabling CONFIG_HIMEM4G into the kernel would help\n");
1031 retval = -ENOMEM;
1032 goto free_mem;
1033 }
1034 }
1035 else {
1036 itv->dec_mem = itv->enc_mem;
1037 }
1038
1039 /* map registers memory */
1040 IVTV_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
1041 itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1042 itv->reg_mem =
1043 ioremap_nocache(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1044 if (!itv->reg_mem) {
1045 IVTV_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
1046 IVTV_ERR("or disabling CONFIG_HIMEM4G into the kernel would help\n");
1047 retval = -ENOMEM;
1048 goto free_io;
1049 }
1050
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001051 /* Check yuv output filter table */
1052 if (itv->has_cx23415) ivtv_yuv_filter_check(itv);
1053
1054 ivtv_gpio_init(itv);
1055
1056 /* active i2c */
1057 IVTV_DEBUG_INFO("activating i2c...\n");
1058 if (init_ivtv_i2c(itv)) {
1059 IVTV_ERR("Could not initialize i2c\n");
1060 goto free_irq;
1061 }
1062
1063 IVTV_DEBUG_INFO("Active card count: %d.\n", ivtv_cards_active);
1064
1065 if (itv->card->hw_all & IVTV_HW_TVEEPROM) {
1066#ifdef CONFIG_VIDEO_TVEEPROM_MODULE
1067 ivtv_request_module(itv, "tveeprom");
1068#endif
1069 /* Based on the model number the cardtype may be changed.
1070 The PCI IDs are not always reliable. */
1071 ivtv_process_eeprom(itv);
1072 }
1073
1074 if (itv->std == 0) {
1075 itv->std = V4L2_STD_NTSC_M;
1076 }
1077
1078 if (itv->options.tuner == -1) {
1079 int i;
1080
1081 for (i = 0; i < IVTV_CARD_MAX_TUNERS; i++) {
1082 if ((itv->std & itv->card->tuners[i].std) == 0)
1083 continue;
1084 itv->options.tuner = itv->card->tuners[i].tuner;
1085 break;
1086 }
1087 }
1088 /* if no tuner was found, then pick the first tuner in the card list */
1089 if (itv->options.tuner == -1 && itv->card->tuners[0].std) {
1090 itv->std = itv->card->tuners[0].std;
1091 itv->options.tuner = itv->card->tuners[0].tuner;
1092 }
1093 if (itv->options.radio == -1)
1094 itv->options.radio = (itv->card->radio_input.audio_type != 0);
1095
1096 /* The card is now fully identified, continue with card-specific
1097 initialization. */
1098 ivtv_init_struct2(itv);
1099
1100 ivtv_load_and_init_modules(itv);
1101
1102 if (itv->std & V4L2_STD_525_60) {
1103 itv->is_60hz = 1;
1104 itv->is_out_60hz = 1;
1105 } else {
1106 itv->is_50hz = 1;
1107 itv->is_out_50hz = 1;
1108 }
1109 itv->params.video_gop_size = itv->is_60hz ? 15 : 12;
1110
1111 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000;
1112 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200;
1113 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_MPG] = 0x10000;
1114
1115 /* 0x15180 == 720 * 480 / 4, 0x19500 == 720 * 576 / 4 */
1116 yuv_buf_size = itv->is_60hz ? 0x15180 : 0x19500;
1117 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_YUV] = yuv_buf_size / 2;
1118 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_YUV] = yuv_buf_size / 8;
1119
1120 /* Setup VBI Raw Size. Should be big enough to hold PAL.
1121 It is possible to switch between PAL and NTSC, so we need to
1122 take the largest size here. */
1123 /* 1456 is multiple of 16, real size = 1444 */
1124 itv->vbi.raw_size = 1456;
1125 /* We use a buffer size of 1/2 of the total size needed for a
1126 frame. This is actually very useful, since we now receive
1127 a field at a time and that makes 'compressing' the raw data
1128 down to size by stripping off the SAV codes a lot easier.
1129 Note: having two different buffer sizes prevents standard
1130 switching on the fly. We need to find a better solution... */
1131 vbi_buf_size = itv->vbi.raw_size * (itv->is_60hz ? 24 : 36) / 2;
1132 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
1133 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_VBI] = sizeof(struct v4l2_sliced_vbi_data) * 36;
1134
1135 if (itv->options.radio > 0)
1136 itv->v4l2_cap |= V4L2_CAP_RADIO;
1137
Hans Verkuil37297802007-09-11 11:59:15 -03001138 if (itv->options.tuner > -1 && itv->tunerid == 0) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001139 struct tuner_setup setup;
1140
1141 setup.addr = ADDR_UNSET;
1142 setup.type = itv->options.tuner;
1143 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001144 ivtv_call_i2c_clients(itv, TUNER_SET_TYPE_ADDR, &setup);
1145 }
1146
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001147 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
1148 are not. */
1149 itv->tuner_std = itv->std;
1150
Hans Verkuil2ce55b62007-08-19 05:09:37 -03001151 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1152 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std);
1153 }
1154
Hans Verkuilfee73162007-03-11 14:16:42 -03001155 retval = ivtv_streams_setup(itv);
1156 if (retval) {
1157 IVTV_ERR("Error %d setting up streams\n", retval);
1158 goto free_i2c;
1159 }
1160
Hans Verkuilfee73162007-03-11 14:16:42 -03001161 IVTV_DEBUG_IRQ("Masking interrupts\n");
1162 /* clear interrupt mask, effectively disabling interrupts */
1163 ivtv_set_irq_mask(itv, 0xffffffff);
1164
1165 /* Register IRQ */
1166 retval = request_irq(itv->dev->irq, ivtv_irq_handler,
1167 IRQF_SHARED | IRQF_DISABLED, itv->name, (void *)itv);
1168 if (retval) {
1169 IVTV_ERR("Failed to register irq %d\n", retval);
1170 goto free_streams;
1171 }
Hans Verkuilae38d932007-07-17 13:42:43 -03001172 IVTV_INFO("Initialized card #%d: %s\n", itv->num, itv->card_name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001173 return 0;
1174
1175 free_irq:
1176 free_irq(itv->dev->irq, (void *)itv);
1177 free_streams:
1178 ivtv_streams_cleanup(itv);
1179 free_i2c:
1180 exit_ivtv_i2c(itv);
1181 free_io:
1182 ivtv_iounmap(itv);
1183 free_mem:
1184 release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
1185 release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1186 if (itv->has_cx23415)
1187 release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
1188 free_workqueue:
Hans Verkuil1e13f9e2007-03-10 06:52:02 -03001189 destroy_workqueue(itv->irq_work_queues);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001190 err:
1191 if (retval == 0)
1192 retval = -ENODEV;
1193 IVTV_ERR("Error %d on initialization\n", retval);
1194
Hans Verkuil32db7752007-07-20 09:29:43 -03001195 spin_lock(&ivtv_cards_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001196 kfree(ivtv_cards[ivtv_cards_active]);
1197 ivtv_cards[ivtv_cards_active] = NULL;
Hans Verkuil32db7752007-07-20 09:29:43 -03001198 spin_unlock(&ivtv_cards_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001199 return retval;
1200}
1201
Hans Verkuilc976bc82007-07-22 12:52:40 -03001202int ivtv_init_on_first_open(struct ivtv *itv)
1203{
1204 struct v4l2_frequency vf;
1205 int fw_retry_count = 3;
1206 int video_input;
1207
Hans Verkuil6e5eb592007-07-25 12:55:52 -03001208 if (test_bit(IVTV_F_I_FAILED, &itv->i_flags))
1209 return -ENXIO;
1210
1211 if (test_and_set_bit(IVTV_F_I_INITED, &itv->i_flags))
1212 return 0;
1213
Hans Verkuilc976bc82007-07-22 12:52:40 -03001214 while (--fw_retry_count > 0) {
1215 /* load firmware */
1216 if (ivtv_firmware_init(itv) == 0)
1217 break;
1218 if (fw_retry_count > 1)
1219 IVTV_WARN("Retry loading firmware\n");
1220 }
Hans Verkuil6e5eb592007-07-25 12:55:52 -03001221
Hans Verkuilc976bc82007-07-22 12:52:40 -03001222 if (fw_retry_count == 0) {
Hans Verkuil6e5eb592007-07-25 12:55:52 -03001223 set_bit(IVTV_F_I_FAILED, &itv->i_flags);
1224 return -ENXIO;
Hans Verkuilc976bc82007-07-22 12:52:40 -03001225 }
1226
1227 /* Try and get firmware versions */
1228 IVTV_DEBUG_INFO("Getting firmware version..\n");
1229 ivtv_firmware_versions(itv);
1230
1231 if (itv->card->hw_all & IVTV_HW_CX25840) {
1232 struct v4l2_control ctrl;
1233
1234 /* CX25840_CID_ENABLE_PVR150_WORKAROUND */
1235 ctrl.id = V4L2_CID_PRIVATE_BASE;
1236 ctrl.value = itv->pvr150_workaround;
1237 itv->video_dec_func(itv, VIDIOC_S_CTRL, &ctrl);
1238 }
1239
1240 vf.tuner = 0;
1241 vf.type = V4L2_TUNER_ANALOG_TV;
1242 vf.frequency = 6400; /* the tuner 'baseline' frequency */
1243
1244 /* Set initial frequency. For PAL/SECAM broadcasts no
1245 'default' channel exists AFAIK. */
1246 if (itv->std == V4L2_STD_NTSC_M_JP) {
1247 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
1248 }
1249 else if (itv->std & V4L2_STD_NTSC_M) {
1250 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
1251 }
1252
1253 video_input = itv->active_input;
1254 itv->active_input++; /* Force update of input */
1255 ivtv_v4l2_ioctls(itv, NULL, VIDIOC_S_INPUT, &video_input);
1256
1257 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1258 in one place. */
1259 itv->std++; /* Force full standard initialization */
1260 itv->std_out = itv->std;
1261 ivtv_v4l2_ioctls(itv, NULL, VIDIOC_S_FREQUENCY, &vf);
1262
1263 if (itv->card->v4l2_capabilities & V4L2_CAP_VIDEO_OUTPUT) {
1264 ivtv_init_mpeg_decoder(itv);
1265 }
1266 ivtv_v4l2_ioctls(itv, NULL, VIDIOC_S_STD, &itv->tuner_std);
1267
1268 /* On a cx23416 this seems to be able to enable DMA to the chip? */
1269 if (!itv->has_cx23415)
1270 write_reg_sync(0x03, IVTV_REG_DMACONTROL);
1271
1272 /* Default interrupts enabled. For the PVR350 this includes the
1273 decoder VSYNC interrupt, which is always on. It is not only used
1274 during decoding but also by the OSD.
1275 Some old PVR250 cards had a cx23415, so testing for that is too
1276 general. Instead test if the card has video output capability. */
1277 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1278 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT | IVTV_IRQ_DEC_VSYNC);
1279 ivtv_set_osd_alpha(itv);
1280 }
1281 else
1282 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT);
1283 return 0;
1284}
1285
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001286static void ivtv_remove(struct pci_dev *pci_dev)
1287{
1288 struct ivtv *itv = pci_get_drvdata(pci_dev);
1289
Hans Verkuilae38d932007-07-17 13:42:43 -03001290 IVTV_DEBUG_INFO("Removing Card #%d\n", itv->num);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001291
Hans Verkuilc976bc82007-07-22 12:52:40 -03001292 if (test_bit(IVTV_F_I_INITED, &itv->i_flags)) {
1293 /* Stop all captures */
1294 IVTV_DEBUG_INFO("Stopping all streams\n");
1295 if (atomic_read(&itv->capturing) > 0)
1296 ivtv_stop_all_captures(itv);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001297
Hans Verkuilc976bc82007-07-22 12:52:40 -03001298 /* Stop all decoding */
1299 IVTV_DEBUG_INFO("Stopping decoding\n");
1300 if (atomic_read(&itv->decoding) > 0) {
1301 int type;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001302
Hans Verkuilc976bc82007-07-22 12:52:40 -03001303 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
1304 type = IVTV_DEC_STREAM_TYPE_YUV;
1305 else
1306 type = IVTV_DEC_STREAM_TYPE_MPG;
1307 ivtv_stop_v4l2_decode_stream(&itv->streams[type],
1308 VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
1309 }
1310 ivtv_halt_firmware(itv);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001311 }
1312
1313 /* Interrupts */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001314 ivtv_set_irq_mask(itv, 0xffffffff);
1315 del_timer_sync(&itv->dma_timer);
1316
1317 /* Stop all Work Queues */
Hans Verkuil1e13f9e2007-03-10 06:52:02 -03001318 flush_workqueue(itv->irq_work_queues);
1319 destroy_workqueue(itv->irq_work_queues);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001320
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001321 ivtv_streams_cleanup(itv);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001322 ivtv_udma_free(itv);
1323
1324 exit_ivtv_i2c(itv);
1325
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001326 free_irq(itv->dev->irq, (void *)itv);
Hans Verkuil27b5a392007-06-16 16:46:56 -03001327 ivtv_iounmap(itv);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001328
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001329 release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
1330 release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1331 if (itv->has_cx23415)
1332 release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
1333
1334 pci_disable_device(itv->dev);
1335
1336 IVTV_INFO("Removed %s, card #%d\n", itv->card_name, itv->num);
1337}
1338
1339/* define a pci_driver for card detection */
1340static struct pci_driver ivtv_pci_driver = {
1341 .name = "ivtv",
1342 .id_table = ivtv_pci_tbl,
1343 .probe = ivtv_probe,
1344 .remove = ivtv_remove,
1345};
1346
1347static int module_start(void)
1348{
Hans Verkuilae38d932007-07-17 13:42:43 -03001349 printk(KERN_INFO "ivtv: Start initialization, version %s\n", IVTV_VERSION);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001350
1351 memset(ivtv_cards, 0, sizeof(ivtv_cards));
1352
1353 /* Validate parameters */
1354 if (ivtv_first_minor < 0 || ivtv_first_minor >= IVTV_MAX_CARDS) {
Hans Verkuilae38d932007-07-17 13:42:43 -03001355 printk(KERN_ERR "ivtv: Exiting, ivtv_first_minor must be between 0 and %d\n",
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001356 IVTV_MAX_CARDS - 1);
1357 return -1;
1358 }
1359
Hans Verkuil1aa32c22007-08-19 06:08:58 -03001360 if (ivtv_debug < 0 || ivtv_debug > 2047) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001361 ivtv_debug = 0;
Hans Verkuil1aa32c22007-08-19 06:08:58 -03001362 printk(KERN_INFO "ivtv: Debug value must be >= 0 and <= 2047\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001363 }
1364
Hans Verkuil6773c1c2007-03-10 20:34:54 -03001365 if (pci_register_driver(&ivtv_pci_driver)) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001366 printk(KERN_ERR "ivtv: Error detecting PCI card\n");
1367 return -ENODEV;
1368 }
Hans Verkuilae38d932007-07-17 13:42:43 -03001369 printk(KERN_INFO "ivtv: End initialization\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001370 return 0;
1371}
1372
1373static void module_cleanup(void)
1374{
1375 int i, j;
1376
Hans Verkuilf40a2912007-03-10 20:50:51 -03001377 pci_unregister_driver(&ivtv_pci_driver);
1378
Hans Verkuil32db7752007-07-20 09:29:43 -03001379 spin_lock(&ivtv_cards_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001380 for (i = 0; i < ivtv_cards_active; i++) {
1381 if (ivtv_cards[i] == NULL)
1382 continue;
1383 for (j = 0; j < IVTV_VBI_FRAMES; j++) {
1384 kfree(ivtv_cards[i]->vbi.sliced_mpeg_data[j]);
1385 }
1386 kfree(ivtv_cards[i]);
1387 }
Hans Verkuil32db7752007-07-20 09:29:43 -03001388 spin_unlock(&ivtv_cards_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001389}
1390
Hans Verkuilb6735ac2007-03-10 06:59:44 -03001391/* Note: These symbols are exported because they are used by the ivtv-fb
1392 framebuffer module and an infrared module for the IR-blaster. */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001393EXPORT_SYMBOL(ivtv_set_irq_mask);
1394EXPORT_SYMBOL(ivtv_cards_active);
1395EXPORT_SYMBOL(ivtv_cards);
Hans Verkuil32db7752007-07-20 09:29:43 -03001396EXPORT_SYMBOL(ivtv_cards_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001397EXPORT_SYMBOL(ivtv_api);
1398EXPORT_SYMBOL(ivtv_vapi);
1399EXPORT_SYMBOL(ivtv_vapi_result);
1400EXPORT_SYMBOL(ivtv_clear_irq_mask);
1401EXPORT_SYMBOL(ivtv_debug);
1402EXPORT_SYMBOL(ivtv_reset_ir_gpio);
1403EXPORT_SYMBOL(ivtv_udma_setup);
1404EXPORT_SYMBOL(ivtv_udma_unmap);
1405EXPORT_SYMBOL(ivtv_udma_alloc);
1406EXPORT_SYMBOL(ivtv_udma_prepare);
Hans Verkuil6e5eb592007-07-25 12:55:52 -03001407EXPORT_SYMBOL(ivtv_init_on_first_open);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001408
1409module_init(module_start);
1410module_exit(module_cleanup);