blob: 52d04f640b73cc506ecb8ee1ebd4b72d5ff07076 [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 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030086static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
Andy Walls6ecd86d2008-12-07 23:30:17 -030087static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
88static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030089static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
90static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
91static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;
92
Andy Walls6ecd86d2008-12-07 23:30:17 -030093static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
94static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
95static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
96static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
97/* VBI bufsize based on standards supported by card tuner for now */
98static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;
99
100static int enc_ts_bufs = -1;
101static int enc_mpg_bufs = -1;
102static int enc_idx_bufs = -1;
103static int enc_yuv_bufs = -1;
104static int enc_vbi_bufs = -1;
105static int enc_pcm_bufs = -1;
106
107
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300108static int cx18_pci_latency = 1;
109
Andy Walls3f75c612008-11-16 23:33:41 -0300110static int mmio_ndelay;
111static int retry_mmio = 1;
112
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300113int cx18_debug;
114
115module_param_array(tuner, int, &tuner_c, 0644);
116module_param_array(radio, bool, &radio_c, 0644);
117module_param_array(cardtype, int, &cardtype_c, 0644);
118module_param_string(pal, pal, sizeof(pal), 0644);
119module_param_string(secam, secam, sizeof(secam), 0644);
120module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
121module_param_named(debug, cx18_debug, int, 0644);
Andy Walls3f75c612008-11-16 23:33:41 -0300122module_param(mmio_ndelay, int, 0644);
123module_param(retry_mmio, int, 0644);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300124module_param(cx18_pci_latency, int, 0644);
125module_param(cx18_first_minor, int, 0644);
126
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300127module_param(enc_ts_buffers, int, 0644);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300128module_param(enc_mpg_buffers, int, 0644);
129module_param(enc_idx_buffers, int, 0644);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300130module_param(enc_yuv_buffers, int, 0644);
131module_param(enc_vbi_buffers, int, 0644);
132module_param(enc_pcm_buffers, int, 0644);
133
Andy Walls6ecd86d2008-12-07 23:30:17 -0300134module_param(enc_ts_bufsize, int, 0644);
135module_param(enc_mpg_bufsize, int, 0644);
136module_param(enc_idx_bufsize, int, 0644);
137module_param(enc_yuv_bufsize, int, 0644);
138/* VBI bufsize based on standards supported by card tuner for now */
139module_param(enc_pcm_bufsize, int, 0644);
140
141module_param(enc_ts_bufs, int, 0644);
142module_param(enc_mpg_bufs, int, 0644);
143module_param(enc_idx_bufs, int, 0644);
144module_param(enc_yuv_bufs, int, 0644);
145module_param(enc_vbi_bufs, int, 0644);
146module_param(enc_pcm_bufs, int, 0644);
147
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300148MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
149 "\t\t\tsee tuner.h for values");
150MODULE_PARM_DESC(radio,
151 "Enable or disable the radio. Use only if autodetection\n"
152 "\t\t\tfails. 0 = disable, 1 = enable");
153MODULE_PARM_DESC(cardtype,
154 "Only use this option if your card is not detected properly.\n"
155 "\t\tSpecify card type:\n"
156 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
157 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
158 "\t\t\t 3 = Compro VideoMate H900\n"
159 "\t\t\t 4 = Yuan MPC718\n"
Sri Deevi03c28082008-06-21 11:06:44 -0300160 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
Andy Walls9eee4fb2008-10-04 20:28:40 -0300161 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
162 "\t\t\t 7 = Leadtek WinFast PVR2100\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300163 "\t\t\t 0 = Autodetect (default)\n"
164 "\t\t\t-1 = Ignore this card\n\t\t");
165MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
166MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
167MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
168MODULE_PARM_DESC(debug,
169 "Debug level (bitmask). Default: 0\n"
170 "\t\t\t 1/0x0001: warning\n"
171 "\t\t\t 2/0x0002: info\n"
172 "\t\t\t 4/0x0004: mailbox\n"
173 "\t\t\t 8/0x0008: dma\n"
174 "\t\t\t 16/0x0010: ioctl\n"
175 "\t\t\t 32/0x0020: file\n"
176 "\t\t\t 64/0x0040: i2c\n"
177 "\t\t\t128/0x0080: irq\n"
178 "\t\t\t256/0x0100: high volume\n");
179MODULE_PARM_DESC(cx18_pci_latency,
180 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
181 "\t\t\tDefault: Yes");
Andy Wallsd267d852008-09-28 21:46:02 -0300182MODULE_PARM_DESC(retry_mmio,
Andy Walls3f75c612008-11-16 23:33:41 -0300183 "(Deprecated) MMIO writes are now always checked and retried\n"
184 "\t\t\tEffectively: 1 [Yes]");
Andy Wallsc641d092008-09-01 00:40:41 -0300185MODULE_PARM_DESC(mmio_ndelay,
Andy Walls3f75c612008-11-16 23:33:41 -0300186 "(Deprecated) MMIO accesses are now never purposely delayed\n"
187 "\t\t\tEffectively: 0 ns");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300188MODULE_PARM_DESC(enc_ts_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300189 "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300190 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300191MODULE_PARM_DESC(enc_ts_bufsize,
192 "Size of an encoder TS buffer (kB)\n"
193 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));
194MODULE_PARM_DESC(enc_ts_bufs,
195 "Number of encoder TS buffers\n"
196 "\t\t\tDefault is computed from other enc_ts_* parameters");
197MODULE_PARM_DESC(enc_mpg_buffers,
198 "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"
199 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
200MODULE_PARM_DESC(enc_mpg_bufsize,
201 "Size of an encoder MPG buffer (kB)\n"
202 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));
203MODULE_PARM_DESC(enc_mpg_bufs,
204 "Number of encoder MPG buffers\n"
205 "\t\t\tDefault is computed from other enc_mpg_* parameters");
206MODULE_PARM_DESC(enc_idx_buffers,
207 "Encoder IDX buffer memory (MB). (enc_idx_bufs can override)\n"
208 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFFERS));
209MODULE_PARM_DESC(enc_idx_bufsize,
210 "Size of an encoder IDX buffer (kB)\n"
211 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFSIZE));
212MODULE_PARM_DESC(enc_idx_bufs,
213 "Number of encoder IDX buffers\n"
214 "\t\t\tDefault is computed from other enc_idx_* parameters");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300215MODULE_PARM_DESC(enc_yuv_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300216 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300217 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300218MODULE_PARM_DESC(enc_yuv_bufsize,
219 "Size of an encoder YUV buffer (kB)\n"
220 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFSIZE));
221MODULE_PARM_DESC(enc_yuv_bufs,
222 "Number of encoder YUV buffers\n"
223 "\t\t\tDefault is computed from other enc_yuv_* parameters");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300224MODULE_PARM_DESC(enc_vbi_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300225 "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300226 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300227MODULE_PARM_DESC(enc_vbi_bufs,
228 "Number of encoder VBI buffers\n"
229 "\t\t\tDefault is computed from enc_vbi_buffers & tuner std");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300230MODULE_PARM_DESC(enc_pcm_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300231 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300232 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300233MODULE_PARM_DESC(enc_pcm_bufsize,
234 "Size of an encoder PCM buffer (kB)\n"
235 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));
236MODULE_PARM_DESC(enc_pcm_bufs,
237 "Number of encoder PCM buffers\n"
238 "\t\t\tDefault is computed from other enc_pcm_* parameters");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300239
Hans Verkuildd896012008-10-04 08:36:54 -0300240MODULE_PARM_DESC(cx18_first_minor, "Set kernel number assigned to first card");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300241
242MODULE_AUTHOR("Hans Verkuil");
243MODULE_DESCRIPTION("CX23418 driver");
244MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
245MODULE_LICENSE("GPL");
246
247MODULE_VERSION(CX18_VERSION);
248
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300249/* Generic utility functions */
250int cx18_msleep_timeout(unsigned int msecs, int intr)
251{
Andy Walls330c6ec2008-11-08 14:19:37 -0300252 long int timeout = msecs_to_jiffies(msecs);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300253 int sig;
254
255 do {
256 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
257 timeout = schedule_timeout(timeout);
258 sig = intr ? signal_pending(current) : 0;
259 } while (!sig && timeout);
260 return sig;
261}
262
263/* Release ioremapped memory */
264static void cx18_iounmap(struct cx18 *cx)
265{
266 if (cx == NULL)
267 return;
268
269 /* Release io memory */
270 if (cx->enc_mem != NULL) {
271 CX18_DEBUG_INFO("releasing enc_mem\n");
272 iounmap(cx->enc_mem);
273 cx->enc_mem = NULL;
274 }
275}
276
277/* Hauppauge card? get values from tveeprom */
278void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
279{
280 u8 eedata[256];
281
282 cx->i2c_client[0].addr = 0xA0 >> 1;
283 tveeprom_read(&cx->i2c_client[0], eedata, sizeof(eedata));
284 tveeprom_hauppauge_analog(&cx->i2c_client[0], tv, eedata);
285}
286
287static void cx18_process_eeprom(struct cx18 *cx)
288{
289 struct tveeprom tv;
290
291 cx18_read_eeprom(cx, &tv);
292
293 /* Many thanks to Steven Toth from Hauppauge for providing the
294 model numbers */
Hans Verkuil1d081602008-05-12 14:45:19 -0300295 /* Note: the Samsung memory models cannot be reliably determined
296 from the model number. Use the cardtype module option if you
297 have one of these preproduction models. */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300298 switch (tv.model) {
Hans Verkuil1d081602008-05-12 14:45:19 -0300299 case 74000 ... 74999:
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300300 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
301 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300302 case 0:
303 CX18_ERR("Invalid EEPROM\n");
304 return;
305 default:
306 CX18_ERR("Unknown model %d, defaulting to HVR-1600\n", tv.model);
307 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
308 break;
309 }
310
311 cx->v4l2_cap = cx->card->v4l2_capabilities;
312 cx->card_name = cx->card->name;
313 cx->card_i2c = cx->card->i2c;
314
315 CX18_INFO("Autodetected %s\n", cx->card_name);
316
317 if (tv.tuner_type == TUNER_ABSENT)
318 CX18_ERR("tveeprom cannot autodetect tuner!");
319
320 if (cx->options.tuner == -1)
321 cx->options.tuner = tv.tuner_type;
322 if (cx->options.radio == -1)
323 cx->options.radio = (tv.has_radio != 0);
324
325 if (cx->std != 0)
326 /* user specified tuner standard */
327 return;
328
329 /* autodetect tuner standard */
330 if (tv.tuner_formats & V4L2_STD_PAL) {
331 CX18_DEBUG_INFO("PAL tuner detected\n");
332 cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
333 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
334 CX18_DEBUG_INFO("NTSC tuner detected\n");
335 cx->std |= V4L2_STD_NTSC_M;
336 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
337 CX18_DEBUG_INFO("SECAM tuner detected\n");
338 cx->std |= V4L2_STD_SECAM_L;
339 } else {
340 CX18_INFO("No tuner detected, default to NTSC-M\n");
341 cx->std |= V4L2_STD_NTSC_M;
342 }
343}
344
345static v4l2_std_id cx18_parse_std(struct cx18 *cx)
346{
347 switch (pal[0]) {
348 case '6':
349 return V4L2_STD_PAL_60;
350 case 'b':
351 case 'B':
352 case 'g':
353 case 'G':
354 return V4L2_STD_PAL_BG;
355 case 'h':
356 case 'H':
357 return V4L2_STD_PAL_H;
358 case 'n':
359 case 'N':
360 if (pal[1] == 'c' || pal[1] == 'C')
361 return V4L2_STD_PAL_Nc;
362 return V4L2_STD_PAL_N;
363 case 'i':
364 case 'I':
365 return V4L2_STD_PAL_I;
366 case 'd':
367 case 'D':
368 case 'k':
369 case 'K':
370 return V4L2_STD_PAL_DK;
371 case 'M':
372 case 'm':
373 return V4L2_STD_PAL_M;
374 case '-':
375 break;
376 default:
377 CX18_WARN("pal= argument not recognised\n");
378 return 0;
379 }
380
381 switch (secam[0]) {
382 case 'b':
383 case 'B':
384 case 'g':
385 case 'G':
386 case 'h':
387 case 'H':
388 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
389 case 'd':
390 case 'D':
391 case 'k':
392 case 'K':
393 return V4L2_STD_SECAM_DK;
394 case 'l':
395 case 'L':
396 if (secam[1] == 'C' || secam[1] == 'c')
397 return V4L2_STD_SECAM_LC;
398 return V4L2_STD_SECAM_L;
399 case '-':
400 break;
401 default:
402 CX18_WARN("secam= argument not recognised\n");
403 return 0;
404 }
405
406 switch (ntsc[0]) {
407 case 'm':
408 case 'M':
409 return V4L2_STD_NTSC_M;
410 case 'j':
411 case 'J':
412 return V4L2_STD_NTSC_M_JP;
413 case 'k':
414 case 'K':
415 return V4L2_STD_NTSC_M_KR;
416 case '-':
417 break;
418 default:
419 CX18_WARN("ntsc= argument not recognised\n");
420 return 0;
421 }
422
423 /* no match found */
424 return 0;
425}
426
427static void cx18_process_options(struct cx18 *cx)
428{
429 int i, j;
430
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300431 cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300432 cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
433 cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300434 cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
435 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
436 cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300437 cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */
438
439 cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;
440 cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;
441 cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;
442 cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;
443 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;
444 cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;
445 cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */
446
447 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;
448 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
449 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
450 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
451 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = 0; /* computed later */
452 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
453 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
454
455 /* Except for VBI ensure stream_buffers & stream_buf_size are valid */
456 for (i = 0; i < CX18_MAX_STREAMS; i++) {
457 /* User said to use 0 buffers */
458 if (cx->stream_buffers[i] == 0) {
459 cx->options.megabytes[i] = 0;
460 cx->stream_buf_size[i] = 0;
461 continue;
462 }
463 /* User said to use 0 MB total */
464 if (cx->options.megabytes[i] <= 0) {
465 cx->options.megabytes[i] = 0;
466 cx->stream_buffers[i] = 0;
467 cx->stream_buf_size[i] = 0;
468 continue;
469 }
470 /* VBI is computed later or user said buffer has size 0 */
471 if (cx->stream_buf_size[i] <= 0) {
472 if (i != CX18_ENC_STREAM_TYPE_VBI) {
473 cx->options.megabytes[i] = 0;
474 cx->stream_buffers[i] = 0;
475 cx->stream_buf_size[i] = 0;
476 }
477 continue;
478 }
479 if (cx->stream_buffers[i] < 0) {
480 cx->stream_buffers[i] = cx->options.megabytes[i] * 1024
481 / cx->stream_buf_size[i];
482 } else {
483 /* N.B. This might round down to 0 */
484 cx->options.megabytes[i] =
485 cx->stream_buffers[i] * cx->stream_buf_size[i] / 1024;
486 }
487 cx->stream_buf_size[i] *= 1024; /* convert from kB to bytes */
488 }
489
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300490 cx->options.cardtype = cardtype[cx->num];
491 cx->options.tuner = tuner[cx->num];
492 cx->options.radio = radio[cx->num];
493
494 cx->std = cx18_parse_std(cx);
495 if (cx->options.cardtype == -1) {
496 CX18_INFO("Ignore card\n");
497 return;
498 }
499 cx->card = cx18_get_card(cx->options.cardtype - 1);
500 if (cx->card)
501 CX18_INFO("User specified %s card\n", cx->card->name);
502 else if (cx->options.cardtype != 0)
503 CX18_ERR("Unknown user specified type, trying to autodetect card\n");
504 if (cx->card == NULL) {
Andy Walls3d059132009-01-10 21:54:39 -0300505 if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300506 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
507 CX18_INFO("Autodetected Hauppauge card\n");
508 }
509 }
510 if (cx->card == NULL) {
511 for (i = 0; (cx->card = cx18_get_card(i)); i++) {
512 if (cx->card->pci_list == NULL)
513 continue;
514 for (j = 0; cx->card->pci_list[j].device; j++) {
Andy Walls3d059132009-01-10 21:54:39 -0300515 if (cx->pci_dev->device !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300516 cx->card->pci_list[j].device)
517 continue;
Andy Walls3d059132009-01-10 21:54:39 -0300518 if (cx->pci_dev->subsystem_vendor !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300519 cx->card->pci_list[j].subsystem_vendor)
520 continue;
Andy Walls3d059132009-01-10 21:54:39 -0300521 if (cx->pci_dev->subsystem_device !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300522 cx->card->pci_list[j].subsystem_device)
523 continue;
524 CX18_INFO("Autodetected %s card\n", cx->card->name);
525 goto done;
526 }
527 }
528 }
529done:
530
531 if (cx->card == NULL) {
532 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
Bjorn Helgaas29e66a62008-09-04 17:24:51 -0300533 CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
Andy Walls3d059132009-01-10 21:54:39 -0300534 cx->pci_dev->vendor, cx->pci_dev->device);
Bjorn Helgaas29e66a62008-09-04 17:24:51 -0300535 CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n",
Andy Walls3d059132009-01-10 21:54:39 -0300536 cx->pci_dev->subsystem_vendor,
537 cx->pci_dev->subsystem_device);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300538 CX18_ERR("Defaulting to %s card\n", cx->card->name);
539 CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
540 CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
541 CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
542 }
543 cx->v4l2_cap = cx->card->v4l2_capabilities;
544 cx->card_name = cx->card->name;
545 cx->card_i2c = cx->card->i2c;
546}
547
548/* Precondition: the cx18 structure has been memset to 0. Only
549 the dev and num fields have been filled in.
550 No assumptions on the card type may be made here (see cx18_init_struct2
551 for that).
552 */
553static int __devinit cx18_init_struct1(struct cx18 *cx)
554{
Andy Wallsee2d64f2008-11-16 01:38:19 -0300555 int i;
556
Andy Walls3d059132009-01-10 21:54:39 -0300557 cx->base_addr = pci_resource_start(cx->pci_dev, 0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300558
559 mutex_init(&cx->serialize_lock);
560 mutex_init(&cx->i2c_bus_lock[0]);
561 mutex_init(&cx->i2c_bus_lock[1]);
Andy Walls8abdd002008-07-13 19:05:25 -0300562 mutex_init(&cx->gpio_lock);
Andy Walls72c2d6d2008-11-06 01:15:41 -0300563 mutex_init(&cx->epu2apu_mb_lock);
564 mutex_init(&cx->epu2cpu_mb_lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300565
566 spin_lock_init(&cx->lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300567
Andy Walls572bfea2008-11-25 21:43:05 -0300568 cx->work_queue = create_singlethread_workqueue(cx->name);
569 if (cx->work_queue == NULL) {
570 CX18_ERR("Unable to create work hander thread\n");
571 return -ENOMEM;
572 }
573
Andy Wallsee2d64f2008-11-16 01:38:19 -0300574 for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++) {
575 cx->epu_work_order[i].cx = cx;
576 cx->epu_work_order[i].str = cx->epu_debug_str;
577 INIT_WORK(&cx->epu_work_order[i].work, cx18_epu_work_handler);
578 }
Andy Walls1d6782b2008-11-05 00:49:14 -0300579
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300580 /* start counting open_id at 1 */
581 cx->open_id = 1;
582
583 /* Initial settings */
584 cx2341x_fill_defaults(&cx->params);
585 cx->temporal_strength = cx->params.video_temporal_filter;
586 cx->spatial_strength = cx->params.video_spatial_filter;
587 cx->filter_mode = cx->params.video_spatial_filter_mode |
588 (cx->params.video_temporal_filter_mode << 1) |
589 (cx->params.video_median_filter_type << 2);
590 cx->params.port = CX2341X_PORT_MEMORY;
Andy Walls31230c52009-01-03 14:21:30 -0300591 cx->params.capabilities = CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_AC3;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300592 init_waitqueue_head(&cx->cap_w);
593 init_waitqueue_head(&cx->mb_apu_waitq);
594 init_waitqueue_head(&cx->mb_cpu_waitq);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300595 init_waitqueue_head(&cx->dma_waitq);
596
597 /* VBI */
Andy Wallsdd073432008-12-12 16:24:04 -0300598 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300599 cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
Andy Wallsaf009cf2008-12-12 20:00:29 -0300600
601 /*
602 * The VBI line sizes depend on the pixel clock and the horiz rate
603 *
604 * (1/Fh)*(2*Fp) = Samples/line
605 * = 4 bytes EAV + Anc data in hblank + 4 bytes SAV + active samples
606 *
607 * Sliced VBI is sent as ancillary data during horizontal blanking
608 * Raw VBI is sent as active video samples during vertcal blanking
609 *
610 * We use a BT.656 pxiel clock of 13.5 MHz and a BT.656 active line
611 * length of 720 pixels @ 4:2:2 sampling. Thus...
612 *
Andy Wallsaafc7702008-12-14 19:21:35 -0300613 * For systems that use a 15.734 kHz horizontal rate, such as
614 * NTSC-M, PAL-M, PAL-60, and other 60 Hz/525 line systems, we have:
Andy Wallsaf009cf2008-12-12 20:00:29 -0300615 *
Andy Wallsaafc7702008-12-14 19:21:35 -0300616 * (1/15.734 kHz) * 2 * 13.5 MHz = 1716 samples/line =
Andy Wallsaf009cf2008-12-12 20:00:29 -0300617 * 4 bytes SAV + 268 bytes anc data + 4 bytes SAV + 1440 active samples
618 *
Andy Wallsaafc7702008-12-14 19:21:35 -0300619 * For systems that use a 15.625 kHz horizontal rate, such as
620 * PAL-B/G/H, PAL-I, SECAM-L and other 50 Hz/625 line systems, we have:
Andy Wallsaf009cf2008-12-12 20:00:29 -0300621 *
Andy Wallsaafc7702008-12-14 19:21:35 -0300622 * (1/15.625 kHz) * 2 * 13.5 MHz = 1728 samples/line =
Andy Wallsaf009cf2008-12-12 20:00:29 -0300623 * 4 bytes SAV + 280 bytes anc data + 4 bytes SAV + 1440 active samples
624 *
625 */
626
Andy Wallsaafc7702008-12-14 19:21:35 -0300627 /* FIXME: init these based on tuner std & modify when std changes */
Andy Wallsaf009cf2008-12-12 20:00:29 -0300628 /* CX18-AV-Core number of VBI samples output per horizontal line */
629 cx->vbi.raw_decoder_line_size = 1444; /* 4 byte SAV + 2 * 720 */
630 cx->vbi.sliced_decoder_line_size = 272; /* 60 Hz: 268+4, 50 Hz: 280+4 */
631
632 /* CX18-AV-Core VBI samples/line possibly rounded up */
633 cx->vbi.raw_size = 1444; /* Real max size is 1444 */
634 cx->vbi.sliced_size = 284; /* Real max size is 284 */
635
636 /*
637 * CX18-AV-Core SAV/EAV RP codes in VIP 1.x mode
638 * Task Field VerticalBlank HorizontalBlank 0 0 0 0
639 */
640 cx->vbi.raw_decoder_sav_odd_field = 0x20; /* V */
641 cx->vbi.raw_decoder_sav_even_field = 0x60; /* FV */
642 cx->vbi.sliced_decoder_sav_odd_field = 0xB0; /* T VH - actually EAV */
643 cx->vbi.sliced_decoder_sav_even_field = 0xF0; /* TFVH - actually EAV */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300644 return 0;
645}
646
647/* Second initialization part. Here the card type has been
648 autodetected. */
649static void __devinit cx18_init_struct2(struct cx18 *cx)
650{
651 int i;
652
653 for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
654 if (cx->card->video_inputs[i].video_type == 0)
655 break;
656 cx->nof_inputs = i;
657 for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
658 if (cx->card->audio_inputs[i].audio_type == 0)
659 break;
660 cx->nof_audio_inputs = i;
661
662 /* Find tuner input */
663 for (i = 0; i < cx->nof_inputs; i++) {
664 if (cx->card->video_inputs[i].video_type ==
665 CX18_CARD_INPUT_VID_TUNER)
666 break;
667 }
668 if (i == cx->nof_inputs)
669 i = 0;
670 cx->active_input = i;
671 cx->audio_input = cx->card->video_inputs[i].audio_index;
672 cx->av_state.vid_input = CX18_AV_COMPOSITE7;
673 cx->av_state.aud_input = CX18_AV_AUDIO8;
674 cx->av_state.audclk_freq = 48000;
675 cx->av_state.audmode = V4L2_TUNER_MODE_LANG1;
Andy Wallsaf009cf2008-12-12 20:00:29 -0300676 /* FIXME - 8 is NTSC value, investigate */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300677 cx->av_state.vbi_line_offset = 8;
678}
679
Andy Walls3d059132009-01-10 21:54:39 -0300680static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300681 const struct pci_device_id *pci_id)
682{
683 u16 cmd;
684 unsigned char pci_latency;
685
686 CX18_DEBUG_INFO("Enabling pci device\n");
687
Andy Walls3d059132009-01-10 21:54:39 -0300688 if (pci_enable_device(pci_dev)) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300689 CX18_ERR("Can't enable device %d!\n", cx->num);
690 return -EIO;
691 }
Andy Walls3d059132009-01-10 21:54:39 -0300692 if (pci_set_dma_mask(pci_dev, 0xffffffff)) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300693 CX18_ERR("No suitable DMA available on card %d.\n", cx->num);
694 return -EIO;
695 }
696 if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
697 CX18_ERR("Cannot request encoder memory region on card %d.\n", cx->num);
698 return -EIO;
699 }
700
Andy Walls45190642008-08-29 16:10:21 -0300701 /* Enable bus mastering and memory mapped IO for the CX23418 */
Andy Walls3d059132009-01-10 21:54:39 -0300702 pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);
Andy Walls45190642008-08-29 16:10:21 -0300703 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Andy Walls3d059132009-01-10 21:54:39 -0300704 pci_write_config_word(pci_dev, PCI_COMMAND, cmd);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300705
Andy Walls3d059132009-01-10 21:54:39 -0300706 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &cx->card_rev);
707 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300708
709 if (pci_latency < 64 && cx18_pci_latency) {
710 CX18_INFO("Unreasonably low latency timer, "
711 "setting to 64 (was %d)\n", pci_latency);
Andy Walls3d059132009-01-10 21:54:39 -0300712 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);
713 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300714 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300715
716 CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
717 "irq: %d, latency: %d, memory: 0x%lx\n",
Andy Walls3d059132009-01-10 21:54:39 -0300718 cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,
719 PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),
720 cx->pci_dev->irq, pci_latency, (unsigned long)cx->base_addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300721
722 return 0;
723}
724
Hans Verkuil6a4a7932008-05-01 09:34:54 -0300725#ifdef MODULE
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300726static u32 cx18_request_module(struct cx18 *cx, u32 hw,
727 const char *name, u32 id)
728{
729 if ((hw & id) == 0)
730 return hw;
731 if (request_module(name) != 0) {
732 CX18_ERR("Failed to load module %s\n", name);
733 return hw & ~id;
734 }
735 CX18_DEBUG_INFO("Loaded module %s\n", name);
736 return hw;
737}
Hans Verkuil6a4a7932008-05-01 09:34:54 -0300738#endif
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300739
740static void cx18_load_and_init_modules(struct cx18 *cx)
741{
742 u32 hw = cx->card->hw_all;
743 int i;
744
Hans Verkuil6a4a7932008-05-01 09:34:54 -0300745#ifdef MODULE
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300746 /* load modules */
Hans Verkuil58ae1c22008-11-03 08:06:51 -0300747#ifdef CONFIG_MEDIA_TUNER_MODULE
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300748 hw = cx18_request_module(cx, hw, "tuner", CX18_HW_TUNER);
749#endif
Hans Verkuil58ae1c22008-11-03 08:06:51 -0300750#ifdef CONFIG_VIDEO_CS5345_MODULE
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300751 hw = cx18_request_module(cx, hw, "cs5345", CX18_HW_CS5345);
752#endif
Hans Verkuil6a4a7932008-05-01 09:34:54 -0300753#endif
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300754
755 /* check which i2c devices are actually found */
756 for (i = 0; i < 32; i++) {
757 u32 device = 1 << i;
758
759 if (!(device & hw))
760 continue;
761 if (device == CX18_HW_GPIO || device == CX18_HW_TVEEPROM ||
762 device == CX18_HW_CX23418 || device == CX18_HW_DVB) {
763 /* These 'devices' do not use i2c probing */
764 cx->hw_flags |= device;
765 continue;
766 }
767 cx18_i2c_register(cx, i);
768 if (cx18_i2c_hw_addr(cx, device) > 0)
769 cx->hw_flags |= device;
770 }
771
772 hw = cx->hw_flags;
773}
774
Andy Walls3d059132009-01-10 21:54:39 -0300775static int __devinit cx18_probe(struct pci_dev *pci_dev,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300776 const struct pci_device_id *pci_id)
777{
778 int retval = 0;
Andy Wallsff086572008-10-18 08:51:28 -0300779 int i;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300780 int vbi_buf_size;
781 u32 devtype;
782 struct cx18 *cx;
783
784 spin_lock(&cx18_cards_lock);
785
786 /* Make sure we've got a place for this card */
787 if (cx18_cards_active == CX18_MAX_CARDS) {
788 printk(KERN_ERR "cx18: Maximum number of cards detected (%d).\n",
789 cx18_cards_active);
790 spin_unlock(&cx18_cards_lock);
791 return -ENOMEM;
792 }
793
794 cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700795 if (!cx) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300796 spin_unlock(&cx18_cards_lock);
797 return -ENOMEM;
798 }
799 cx18_cards[cx18_cards_active] = cx;
Andy Walls3d059132009-01-10 21:54:39 -0300800 cx->pci_dev = pci_dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300801 cx->num = cx18_cards_active++;
Jean Delvareb4ac3c82008-05-13 18:27:15 -0300802 snprintf(cx->name, sizeof(cx->name), "cx18-%d", cx->num);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300803 CX18_INFO("Initializing card #%d\n", cx->num);
804
805 spin_unlock(&cx18_cards_lock);
806
807 cx18_process_options(cx);
808 if (cx->options.cardtype == -1) {
809 retval = -ENODEV;
810 goto err;
811 }
812 if (cx18_init_struct1(cx)) {
813 retval = -ENOMEM;
814 goto err;
815 }
816
817 CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);
818
819 /* PCI Device Setup */
Andy Walls3d059132009-01-10 21:54:39 -0300820 retval = cx18_setup_pci(cx, pci_dev, pci_id);
Andy Walls572bfea2008-11-25 21:43:05 -0300821 if (retval != 0)
822 goto free_workqueue;
823
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300824 /* save cx in the pci struct for later use */
Andy Walls3d059132009-01-10 21:54:39 -0300825 pci_set_drvdata(pci_dev, cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300826
827 /* map io memory */
828 CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
829 cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
830 cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
831 CX18_MEM_SIZE);
832 if (!cx->enc_mem) {
833 CX18_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
834 CX18_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
835 retval = -ENOMEM;
836 goto free_mem;
837 }
838 cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
Andy Wallsb1526422008-08-30 16:03:44 -0300839 devtype = cx18_read_reg(cx, 0xC72028);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300840 switch (devtype & 0xff000000) {
841 case 0xff000000:
842 CX18_INFO("cx23418 revision %08x (A)\n", devtype);
843 break;
844 case 0x01000000:
845 CX18_INFO("cx23418 revision %08x (B)\n", devtype);
846 break;
847 default:
848 CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
849 break;
850 }
851
852 cx18_init_power(cx, 1);
853 cx18_init_memory(cx);
854
Al Viro990c81c2008-05-21 00:32:01 -0300855 cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300856 cx18_init_scb(cx);
857
858 cx18_gpio_init(cx);
859
860 /* active i2c */
861 CX18_DEBUG_INFO("activating i2c...\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300862 retval = init_cx18_i2c(cx);
863 if (retval) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300864 CX18_ERR("Could not initialize i2c\n");
865 goto free_map;
866 }
867
868 CX18_DEBUG_INFO("Active card count: %d.\n", cx18_cards_active);
869
870 if (cx->card->hw_all & CX18_HW_TVEEPROM) {
871 /* Based on the model number the cardtype may be changed.
872 The PCI IDs are not always reliable. */
873 cx18_process_eeprom(cx);
874 }
875 if (cx->card->comment)
876 CX18_INFO("%s", cx->card->comment);
877 if (cx->card->v4l2_capabilities == 0) {
878 retval = -ENODEV;
879 goto free_i2c;
880 }
881 cx18_init_memory(cx);
Andy Wallsfd6b9c92008-12-14 21:26:25 -0300882 cx18_init_scb(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300883
884 /* Register IRQ */
Andy Walls3d059132009-01-10 21:54:39 -0300885 retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300886 IRQF_SHARED | IRQF_DISABLED, cx->name, (void *)cx);
887 if (retval) {
888 CX18_ERR("Failed to register irq %d\n", retval);
889 goto free_i2c;
890 }
891
892 if (cx->std == 0)
893 cx->std = V4L2_STD_NTSC_M;
894
895 if (cx->options.tuner == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300896 for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
897 if ((cx->std & cx->card->tuners[i].std) == 0)
898 continue;
899 cx->options.tuner = cx->card->tuners[i].tuner;
900 break;
901 }
902 }
903 /* if no tuner was found, then pick the first tuner in the card list */
904 if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
905 cx->std = cx->card->tuners[0].std;
Hans Verkuilc3cb4d92008-06-27 23:27:25 -0300906 if (cx->std & V4L2_STD_PAL)
907 cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
908 else if (cx->std & V4L2_STD_NTSC)
909 cx->std = V4L2_STD_NTSC_M;
910 else if (cx->std & V4L2_STD_SECAM)
911 cx->std = V4L2_STD_SECAM_L;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300912 cx->options.tuner = cx->card->tuners[0].tuner;
913 }
914 if (cx->options.radio == -1)
915 cx->options.radio = (cx->card->radio_input.audio_type != 0);
916
917 /* The card is now fully identified, continue with card-specific
918 initialization. */
919 cx18_init_struct2(cx);
920
921 cx18_load_and_init_modules(cx);
922
923 if (cx->std & V4L2_STD_525_60) {
924 cx->is_60hz = 1;
925 cx->is_out_60hz = 1;
926 } else {
927 cx->is_50hz = 1;
928 cx->is_out_50hz = 1;
929 }
930 cx->params.video_gop_size = cx->is_60hz ? 15 : 12;
931
Andy Wallsaafc7702008-12-14 19:21:35 -0300932 /*
933 * FIXME: setting the buffer size based on the tuner standard is
934 * suboptimal, as the CVBS and SVideo inputs could use a different std
935 * and the buffer could end up being too small in that case.
936 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300937 vbi_buf_size = cx->vbi.raw_size * (cx->is_60hz ? 24 : 36) / 2;
938 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
939
Andy Walls6ecd86d2008-12-07 23:30:17 -0300940 if (cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] < 0)
941 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] =
942 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] * 1024 * 1024
943 / vbi_buf_size;
944 else
945 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] =
946 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] * vbi_buf_size
947 / (1024 * 1024);
948
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300949 if (cx->options.radio > 0)
950 cx->v4l2_cap |= V4L2_CAP_RADIO;
951
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300952 if (cx->options.tuner > -1) {
953 struct tuner_setup setup;
954
955 setup.addr = ADDR_UNSET;
956 setup.type = cx->options.tuner;
957 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
958 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
959 cx18_reset_tuner_gpio : NULL;
960 cx18_call_i2c_clients(cx, TUNER_SET_TYPE_ADDR, &setup);
961 if (setup.type == TUNER_XC2028) {
962 static struct xc2028_ctrl ctrl = {
963 .fname = XC2028_DEFAULT_FIRMWARE,
964 .max_len = 64,
965 };
966 struct v4l2_priv_tun_config cfg = {
967 .tuner = cx->options.tuner,
968 .priv = &ctrl,
969 };
970 cx18_call_i2c_clients(cx, TUNER_SET_CONFIG, &cfg);
971 }
972 }
973
974 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
975 are not. */
976 cx->tuner_std = cx->std;
977
Hans Verkuil5e7fdc52008-05-30 10:51:53 -0300978 retval = cx18_streams_setup(cx);
979 if (retval) {
980 CX18_ERR("Error %d setting up streams\n", retval);
981 goto free_irq;
982 }
983 retval = cx18_streams_register(cx);
984 if (retval) {
985 CX18_ERR("Error %d registering devices\n", retval);
986 goto free_streams;
987 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300988
989 CX18_INFO("Initialized card #%d: %s\n", cx->num, cx->card_name);
990
991 return 0;
992
993free_streams:
Hans Verkuil3f983872008-05-01 10:31:12 -0300994 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300995free_irq:
Andy Walls3d059132009-01-10 21:54:39 -0300996 free_irq(cx->pci_dev->irq, (void *)cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300997free_i2c:
998 exit_cx18_i2c(cx);
999free_map:
1000 cx18_iounmap(cx);
1001free_mem:
1002 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1003free_workqueue:
Andy Walls572bfea2008-11-25 21:43:05 -03001004 destroy_workqueue(cx->work_queue);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001005err:
1006 if (retval == 0)
1007 retval = -ENODEV;
1008 CX18_ERR("Error %d on initialization\n", retval);
1009
Andy Wallsff086572008-10-18 08:51:28 -03001010 i = cx->num;
1011 spin_lock(&cx18_cards_lock);
1012 kfree(cx18_cards[i]);
1013 cx18_cards[i] = NULL;
1014 spin_unlock(&cx18_cards_lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001015 return retval;
1016}
1017
1018int cx18_init_on_first_open(struct cx18 *cx)
1019{
1020 int video_input;
1021 int fw_retry_count = 3;
1022 struct v4l2_frequency vf;
Andy Walls3b6fe582008-06-21 08:36:31 -03001023 struct cx18_open_id fh;
1024
1025 fh.cx = cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001026
1027 if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
1028 return -ENXIO;
1029
1030 if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
1031 return 0;
1032
1033 while (--fw_retry_count > 0) {
1034 /* load firmware */
1035 if (cx18_firmware_init(cx) == 0)
1036 break;
1037 if (fw_retry_count > 1)
1038 CX18_WARN("Retry loading firmware\n");
1039 }
1040
1041 if (fw_retry_count == 0) {
1042 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1043 return -ENXIO;
1044 }
1045 set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
1046
Andy Walls350145a2009-01-04 21:51:17 -03001047 /*
1048 * Init the firmware twice to work around a silicon bug
1049 * with the digital TS.
1050 *
1051 * The second firmware load requires us to normalize the APU state,
1052 * or the audio for the first analog capture will be badly incorrect.
1053 *
1054 * I can't seem to call APU_RESETAI and have it succeed without the
1055 * APU capturing audio, so we start and stop it here to do the reset
1056 */
1057
1058 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1059 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1060 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1061 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001062
1063 fw_retry_count = 3;
1064 while (--fw_retry_count > 0) {
1065 /* load firmware */
1066 if (cx18_firmware_init(cx) == 0)
1067 break;
1068 if (fw_retry_count > 1)
1069 CX18_WARN("Retry loading firmware\n");
1070 }
1071
1072 if (fw_retry_count == 0) {
1073 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1074 return -ENXIO;
1075 }
1076
1077 vf.tuner = 0;
1078 vf.type = V4L2_TUNER_ANALOG_TV;
1079 vf.frequency = 6400; /* the tuner 'baseline' frequency */
1080
1081 /* Set initial frequency. For PAL/SECAM broadcasts no
1082 'default' channel exists AFAIK. */
1083 if (cx->std == V4L2_STD_NTSC_M_JP)
1084 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
1085 else if (cx->std & V4L2_STD_NTSC_M)
1086 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
1087
1088 video_input = cx->active_input;
1089 cx->active_input++; /* Force update of input */
Andy Walls3b6fe582008-06-21 08:36:31 -03001090 cx18_s_input(NULL, &fh, video_input);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001091
1092 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1093 in one place. */
1094 cx->std++; /* Force full standard initialization */
Andy Walls3b6fe582008-06-21 08:36:31 -03001095 cx18_s_std(NULL, &fh, &cx->tuner_std);
1096 cx18_s_frequency(NULL, &fh, &vf);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001097 return 0;
1098}
1099
Andy Walls18b5dc22008-11-16 17:15:01 -03001100static void cx18_cancel_epu_work_orders(struct cx18 *cx)
1101{
1102 int i;
1103 for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++)
1104 cancel_work_sync(&cx->epu_work_order[i].work);
1105}
1106
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001107static void cx18_remove(struct pci_dev *pci_dev)
1108{
1109 struct cx18 *cx = pci_get_drvdata(pci_dev);
1110
1111 CX18_DEBUG_INFO("Removing Card #%d\n", cx->num);
1112
1113 /* Stop all captures */
1114 CX18_DEBUG_INFO("Stopping all streams\n");
Hans Verkuil31554ae2008-05-25 11:21:27 -03001115 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001116 cx18_stop_all_captures(cx);
1117
1118 /* Interrupts */
Andy Wallsb1526422008-08-30 16:03:44 -03001119 cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
1120 cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001121
1122 cx18_halt_firmware(cx);
1123
Andy Walls18b5dc22008-11-16 17:15:01 -03001124 cx18_cancel_epu_work_orders(cx);
Andy Walls1d6782b2008-11-05 00:49:14 -03001125
Andy Walls572bfea2008-11-25 21:43:05 -03001126 destroy_workqueue(cx->work_queue);
1127
Hans Verkuil3f983872008-05-01 10:31:12 -03001128 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001129
1130 exit_cx18_i2c(cx);
1131
Andy Walls3d059132009-01-10 21:54:39 -03001132 free_irq(cx->pci_dev->irq, (void *)cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001133
Hans Verkuilcba627a2008-05-12 14:48:26 -03001134 cx18_iounmap(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001135
1136 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1137
Andy Walls3d059132009-01-10 21:54:39 -03001138 pci_disable_device(cx->pci_dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001139
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001140 CX18_INFO("Removed %s, card #%d\n", cx->card_name, cx->num);
1141}
1142
1143/* define a pci_driver for card detection */
1144static struct pci_driver cx18_pci_driver = {
1145 .name = "cx18",
1146 .id_table = cx18_pci_tbl,
1147 .probe = cx18_probe,
1148 .remove = cx18_remove,
1149};
1150
1151static int module_start(void)
1152{
1153 printk(KERN_INFO "cx18: Start initialization, version %s\n", CX18_VERSION);
1154
1155 memset(cx18_cards, 0, sizeof(cx18_cards));
1156
1157 /* Validate parameters */
1158 if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
Hans Verkuildd896012008-10-04 08:36:54 -03001159 printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n",
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001160 CX18_MAX_CARDS - 1);
1161 return -1;
1162 }
1163
1164 if (cx18_debug < 0 || cx18_debug > 511) {
1165 cx18_debug = 0;
1166 printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");
1167 }
1168
1169 if (pci_register_driver(&cx18_pci_driver)) {
1170 printk(KERN_ERR "cx18: Error detecting PCI card\n");
1171 return -ENODEV;
1172 }
1173 printk(KERN_INFO "cx18: End initialization\n");
1174 return 0;
1175}
1176
1177static void module_cleanup(void)
1178{
1179 int i;
1180
1181 pci_unregister_driver(&cx18_pci_driver);
1182
1183 for (i = 0; i < cx18_cards_active; i++) {
1184 if (cx18_cards[i] == NULL)
1185 continue;
1186 kfree(cx18_cards[i]);
1187 }
Andy Walls18b5dc22008-11-16 17:15:01 -03001188
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001189}
1190
1191module_init(module_start);
1192module_exit(module_cleanup);