blob: cbd4f85da76bc1e495934f03b8d2a697c036c433 [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"
Andy Walls21a278b2009-04-15 20:45:10 -030033#include "cx18-queue.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030034#include "cx18-streams.h"
35#include "cx18-av-core.h"
36#include "cx18-scb.h"
37#include "cx18-mailbox.h"
38#include "cx18-ioctl.h"
39#include "tuner-xc2028.h"
40
41#include <media/tveeprom.h>
42
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030043/* If you have already X v4l cards, then set this to X. This way
44 the device numbers stay matched. Example: you have a WinTV card
45 without radio and a Compro H900 with. Normally this would give a
46 video1 device together with a radio0 device for the Compro. By
47 setting this to 1 you ensure that radio0 is now also radio1. */
48int cx18_first_minor;
49
Devin Heitmuellerd68b6872009-11-20 01:15:54 -030050/* Callback for registering extensions */
51int (*cx18_ext_init)(struct cx18 *);
52EXPORT_SYMBOL(cx18_ext_init);
53
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030054/* add your revision and whatnot here */
55static struct pci_device_id cx18_pci_tbl[] __devinitdata = {
56 {PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,
57 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
58 {0,}
59};
60
61MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);
62
Andy Walls5811cf92009-02-14 17:08:37 -030063static atomic_t cx18_instance = ATOMIC_INIT(0);
64
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030065/* Parameter declarations */
66static int cardtype[CX18_MAX_CARDS];
67static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
68 -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1 };
71static int radio[CX18_MAX_CARDS] = { -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,
74 -1, -1, -1, -1, -1, -1, -1, -1 };
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -030075static unsigned cardtype_c = 1;
76static unsigned tuner_c = 1;
77static unsigned radio_c = 1;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030078static char pal[] = "--";
79static char secam[] = "--";
80static char ntsc[] = "-";
81
82/* Buffers */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030083static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
Andy Walls6ecd86d2008-12-07 23:30:17 -030084static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
85static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030086static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
87static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
88static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;
89
Andy Walls6ecd86d2008-12-07 23:30:17 -030090static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
91static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
92static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
93static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
Andy Walls6ecd86d2008-12-07 23:30:17 -030094static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;
95
96static int enc_ts_bufs = -1;
97static int enc_mpg_bufs = -1;
Andy Wallsefc0b122009-12-30 22:54:53 -030098static int enc_idx_bufs = CX18_MAX_FW_MDLS_PER_STREAM;
Andy Walls6ecd86d2008-12-07 23:30:17 -030099static int enc_yuv_bufs = -1;
100static int enc_vbi_bufs = -1;
101static int enc_pcm_bufs = -1;
102
103
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300104static int cx18_pci_latency = 1;
105
Andy Walls3f75c612008-11-16 23:33:41 -0300106static int mmio_ndelay;
107static int retry_mmio = 1;
108
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300109int cx18_debug;
110
111module_param_array(tuner, int, &tuner_c, 0644);
112module_param_array(radio, bool, &radio_c, 0644);
113module_param_array(cardtype, int, &cardtype_c, 0644);
114module_param_string(pal, pal, sizeof(pal), 0644);
115module_param_string(secam, secam, sizeof(secam), 0644);
116module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
117module_param_named(debug, cx18_debug, int, 0644);
Andy Walls3f75c612008-11-16 23:33:41 -0300118module_param(mmio_ndelay, int, 0644);
119module_param(retry_mmio, int, 0644);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300120module_param(cx18_pci_latency, int, 0644);
121module_param(cx18_first_minor, int, 0644);
122
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300123module_param(enc_ts_buffers, int, 0644);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300124module_param(enc_mpg_buffers, int, 0644);
125module_param(enc_idx_buffers, int, 0644);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300126module_param(enc_yuv_buffers, int, 0644);
127module_param(enc_vbi_buffers, int, 0644);
128module_param(enc_pcm_buffers, int, 0644);
129
Andy Walls6ecd86d2008-12-07 23:30:17 -0300130module_param(enc_ts_bufsize, int, 0644);
131module_param(enc_mpg_bufsize, int, 0644);
132module_param(enc_idx_bufsize, int, 0644);
133module_param(enc_yuv_bufsize, int, 0644);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300134module_param(enc_pcm_bufsize, int, 0644);
135
136module_param(enc_ts_bufs, int, 0644);
137module_param(enc_mpg_bufs, int, 0644);
138module_param(enc_idx_bufs, int, 0644);
139module_param(enc_yuv_bufs, int, 0644);
140module_param(enc_vbi_bufs, int, 0644);
141module_param(enc_pcm_bufs, int, 0644);
142
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300143MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
144 "\t\t\tsee tuner.h for values");
145MODULE_PARM_DESC(radio,
146 "Enable or disable the radio. Use only if autodetection\n"
147 "\t\t\tfails. 0 = disable, 1 = enable");
148MODULE_PARM_DESC(cardtype,
149 "Only use this option if your card is not detected properly.\n"
150 "\t\tSpecify card type:\n"
151 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
152 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
153 "\t\t\t 3 = Compro VideoMate H900\n"
154 "\t\t\t 4 = Yuan MPC718\n"
Sri Deevi03c28082008-06-21 11:06:44 -0300155 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
Andy Walls9eee4fb2008-10-04 20:28:40 -0300156 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
Andy Walls9d5af862009-06-09 20:37:24 -0300157 "\t\t\t 7 = Leadtek WinFast PVR2100\n"
158 "\t\t\t 8 = Leadtek WinFast DVR3100 H\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300159 "\t\t\t 0 = Autodetect (default)\n"
160 "\t\t\t-1 = Ignore this card\n\t\t");
161MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
162MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
163MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
164MODULE_PARM_DESC(debug,
165 "Debug level (bitmask). Default: 0\n"
166 "\t\t\t 1/0x0001: warning\n"
167 "\t\t\t 2/0x0002: info\n"
168 "\t\t\t 4/0x0004: mailbox\n"
169 "\t\t\t 8/0x0008: dma\n"
170 "\t\t\t 16/0x0010: ioctl\n"
171 "\t\t\t 32/0x0020: file\n"
172 "\t\t\t 64/0x0040: i2c\n"
173 "\t\t\t128/0x0080: irq\n"
174 "\t\t\t256/0x0100: high volume\n");
175MODULE_PARM_DESC(cx18_pci_latency,
176 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
177 "\t\t\tDefault: Yes");
Andy Wallsd267d852008-09-28 21:46:02 -0300178MODULE_PARM_DESC(retry_mmio,
Andy Walls3f75c612008-11-16 23:33:41 -0300179 "(Deprecated) MMIO writes are now always checked and retried\n"
180 "\t\t\tEffectively: 1 [Yes]");
Andy Wallsc641d092008-09-01 00:40:41 -0300181MODULE_PARM_DESC(mmio_ndelay,
Andy Walls3f75c612008-11-16 23:33:41 -0300182 "(Deprecated) MMIO accesses are now never purposely delayed\n"
183 "\t\t\tEffectively: 0 ns");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300184MODULE_PARM_DESC(enc_ts_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300185 "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300186 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300187MODULE_PARM_DESC(enc_ts_bufsize,
188 "Size of an encoder TS buffer (kB)\n"
189 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));
190MODULE_PARM_DESC(enc_ts_bufs,
191 "Number of encoder TS buffers\n"
192 "\t\t\tDefault is computed from other enc_ts_* parameters");
193MODULE_PARM_DESC(enc_mpg_buffers,
194 "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"
195 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
196MODULE_PARM_DESC(enc_mpg_bufsize,
197 "Size of an encoder MPG buffer (kB)\n"
198 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));
199MODULE_PARM_DESC(enc_mpg_bufs,
200 "Number of encoder MPG buffers\n"
201 "\t\t\tDefault is computed from other enc_mpg_* parameters");
202MODULE_PARM_DESC(enc_idx_buffers,
Andy Wallsefc0b122009-12-30 22:54:53 -0300203 "(Deprecated) Encoder IDX buffer memory (MB)\n"
204 "\t\t\tIgnored, except 0 disables IDX buffer allocations\n"
205 "\t\t\tDefault: 1 [Enabled]");
Andy Walls6ecd86d2008-12-07 23:30:17 -0300206MODULE_PARM_DESC(enc_idx_bufsize,
207 "Size of an encoder IDX buffer (kB)\n"
Andy Wallsefc0b122009-12-30 22:54:53 -0300208 "\t\t\tAllowed values are multiples of 1.5 kB rounded up\n"
209 "\t\t\t(multiples of size required for 64 index entries)\n"
210 "\t\t\tDefault: 2");
Andy Walls6ecd86d2008-12-07 23:30:17 -0300211MODULE_PARM_DESC(enc_idx_bufs,
212 "Number of encoder IDX buffers\n"
Andy Wallsefc0b122009-12-30 22:54:53 -0300213 "\t\t\tDefault: " __stringify(CX18_MAX_FW_MDLS_PER_STREAM));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300214MODULE_PARM_DESC(enc_yuv_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300215 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300216 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300217MODULE_PARM_DESC(enc_yuv_bufsize,
218 "Size of an encoder YUV buffer (kB)\n"
Andy Walls22dce182009-11-09 23:55:30 -0300219 "\t\t\tAllowed values are multiples of 33.75 kB rounded up\n"
220 "\t\t\t(multiples of size required for 32 screen lines)\n"
221 "\t\t\tDefault: 102");
Andy Walls6ecd86d2008-12-07 23:30:17 -0300222MODULE_PARM_DESC(enc_yuv_bufs,
223 "Number of encoder YUV buffers\n"
224 "\t\t\tDefault is computed from other enc_yuv_* parameters");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300225MODULE_PARM_DESC(enc_vbi_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300226 "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300227 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300228MODULE_PARM_DESC(enc_vbi_bufs,
229 "Number of encoder VBI buffers\n"
Andy Walls127ce5f2009-11-11 00:22:57 -0300230 "\t\t\tDefault is computed from enc_vbi_buffers");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300231MODULE_PARM_DESC(enc_pcm_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300232 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300233 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300234MODULE_PARM_DESC(enc_pcm_bufsize,
235 "Size of an encoder PCM buffer (kB)\n"
236 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));
237MODULE_PARM_DESC(enc_pcm_bufs,
238 "Number of encoder PCM buffers\n"
239 "\t\t\tDefault is computed from other enc_pcm_* parameters");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300240
Devin Heitmuellerf8bd9d22009-12-20 23:29:02 -0300241MODULE_PARM_DESC(cx18_first_minor,
242 "Set device node number assigned to first card");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300243
244MODULE_AUTHOR("Hans Verkuil");
245MODULE_DESCRIPTION("CX23418 driver");
246MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
247MODULE_LICENSE("GPL");
248
249MODULE_VERSION(CX18_VERSION);
250
Devin Heitmuellerd68b6872009-11-20 01:15:54 -0300251/* Forward Declaration */
252static void request_modules(struct cx18 *dev);
253
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300254/* Generic utility functions */
255int cx18_msleep_timeout(unsigned int msecs, int intr)
256{
Andy Walls330c6ec2008-11-08 14:19:37 -0300257 long int timeout = msecs_to_jiffies(msecs);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300258 int sig;
259
260 do {
261 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
262 timeout = schedule_timeout(timeout);
263 sig = intr ? signal_pending(current) : 0;
264 } while (!sig && timeout);
265 return sig;
266}
267
268/* Release ioremapped memory */
269static void cx18_iounmap(struct cx18 *cx)
270{
271 if (cx == NULL)
272 return;
273
274 /* Release io memory */
275 if (cx->enc_mem != NULL) {
276 CX18_DEBUG_INFO("releasing enc_mem\n");
277 iounmap(cx->enc_mem);
278 cx->enc_mem = NULL;
279 }
280}
281
Andy Walls25a42e42009-07-05 17:09:28 -0300282static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len)
283{
284 int i;
285
286 CX18_INFO("eeprom dump:\n");
287 for (i = 0; i < len; i++) {
288 if (0 == (i % 16))
289 CX18_INFO("eeprom %02x:", i);
290 printk(KERN_CONT " %02x", eedata[i]);
291 if (15 == (i % 16))
292 printk(KERN_CONT "\n");
293 }
294}
295
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300296/* Hauppauge card? get values from tveeprom */
297void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
298{
Andy Wallsff2a2002009-02-20 23:52:13 -0300299 struct i2c_client c;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300300 u8 eedata[256];
301
Andy Walls6246d4e2009-02-21 22:27:37 -0300302 memset(&c, 0, sizeof(c));
Andy Walls098003d2009-02-28 13:19:45 -0300303 strlcpy(c.name, "cx18 tveeprom tmp", sizeof(c.name));
Andy Wallsff2a2002009-02-20 23:52:13 -0300304 c.adapter = &cx->i2c_adap[0];
305 c.addr = 0xA0 >> 1;
306
Andy Walls25a42e42009-07-05 17:09:28 -0300307 memset(tv, 0, sizeof(*tv));
308 if (tveeprom_read(&c, eedata, sizeof(eedata)))
309 return;
310
311 switch (cx->card->type) {
312 case CX18_CARD_HVR_1600_ESMT:
313 case CX18_CARD_HVR_1600_SAMSUNG:
314 tveeprom_hauppauge_analog(&c, tv, eedata);
315 break;
316 case CX18_CARD_YUAN_MPC718:
317 tv->model = 0x718;
318 cx18_eeprom_dump(cx, eedata, sizeof(eedata));
319 CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n",
320 eedata[2], eedata[1], eedata[4], eedata[3]);
321 break;
322 default:
323 tv->model = 0xffffffff;
324 cx18_eeprom_dump(cx, eedata, sizeof(eedata));
325 break;
326 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300327}
328
329static void cx18_process_eeprom(struct cx18 *cx)
330{
331 struct tveeprom tv;
332
333 cx18_read_eeprom(cx, &tv);
334
335 /* Many thanks to Steven Toth from Hauppauge for providing the
336 model numbers */
Hans Verkuil1d081602008-05-12 14:45:19 -0300337 /* Note: the Samsung memory models cannot be reliably determined
338 from the model number. Use the cardtype module option if you
339 have one of these preproduction models. */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300340 switch (tv.model) {
Hans Verkuil1d081602008-05-12 14:45:19 -0300341 case 74000 ... 74999:
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300342 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
343 break;
Andy Walls25a42e42009-07-05 17:09:28 -0300344 case 0x718:
345 return;
346 case 0xffffffff:
347 CX18_INFO("Unknown EEPROM encoding\n");
348 return;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300349 case 0:
350 CX18_ERR("Invalid EEPROM\n");
351 return;
352 default:
353 CX18_ERR("Unknown model %d, defaulting to HVR-1600\n", tv.model);
354 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
355 break;
356 }
357
358 cx->v4l2_cap = cx->card->v4l2_capabilities;
359 cx->card_name = cx->card->name;
360 cx->card_i2c = cx->card->i2c;
361
362 CX18_INFO("Autodetected %s\n", cx->card_name);
363
364 if (tv.tuner_type == TUNER_ABSENT)
Andy Walls4442ee82009-05-09 00:34:31 -0300365 CX18_ERR("tveeprom cannot autodetect tuner!\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300366
367 if (cx->options.tuner == -1)
368 cx->options.tuner = tv.tuner_type;
369 if (cx->options.radio == -1)
370 cx->options.radio = (tv.has_radio != 0);
371
372 if (cx->std != 0)
373 /* user specified tuner standard */
374 return;
375
376 /* autodetect tuner standard */
377 if (tv.tuner_formats & V4L2_STD_PAL) {
378 CX18_DEBUG_INFO("PAL tuner detected\n");
379 cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
380 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
381 CX18_DEBUG_INFO("NTSC tuner detected\n");
382 cx->std |= V4L2_STD_NTSC_M;
383 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
384 CX18_DEBUG_INFO("SECAM tuner detected\n");
385 cx->std |= V4L2_STD_SECAM_L;
386 } else {
387 CX18_INFO("No tuner detected, default to NTSC-M\n");
388 cx->std |= V4L2_STD_NTSC_M;
389 }
390}
391
392static v4l2_std_id cx18_parse_std(struct cx18 *cx)
393{
394 switch (pal[0]) {
395 case '6':
396 return V4L2_STD_PAL_60;
397 case 'b':
398 case 'B':
399 case 'g':
400 case 'G':
401 return V4L2_STD_PAL_BG;
402 case 'h':
403 case 'H':
404 return V4L2_STD_PAL_H;
405 case 'n':
406 case 'N':
407 if (pal[1] == 'c' || pal[1] == 'C')
408 return V4L2_STD_PAL_Nc;
409 return V4L2_STD_PAL_N;
410 case 'i':
411 case 'I':
412 return V4L2_STD_PAL_I;
413 case 'd':
414 case 'D':
415 case 'k':
416 case 'K':
417 return V4L2_STD_PAL_DK;
418 case 'M':
419 case 'm':
420 return V4L2_STD_PAL_M;
421 case '-':
422 break;
423 default:
424 CX18_WARN("pal= argument not recognised\n");
425 return 0;
426 }
427
428 switch (secam[0]) {
429 case 'b':
430 case 'B':
431 case 'g':
432 case 'G':
433 case 'h':
434 case 'H':
435 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
436 case 'd':
437 case 'D':
438 case 'k':
439 case 'K':
440 return V4L2_STD_SECAM_DK;
441 case 'l':
442 case 'L':
443 if (secam[1] == 'C' || secam[1] == 'c')
444 return V4L2_STD_SECAM_LC;
445 return V4L2_STD_SECAM_L;
446 case '-':
447 break;
448 default:
449 CX18_WARN("secam= argument not recognised\n");
450 return 0;
451 }
452
453 switch (ntsc[0]) {
454 case 'm':
455 case 'M':
456 return V4L2_STD_NTSC_M;
457 case 'j':
458 case 'J':
459 return V4L2_STD_NTSC_M_JP;
460 case 'k':
461 case 'K':
462 return V4L2_STD_NTSC_M_KR;
463 case '-':
464 break;
465 default:
466 CX18_WARN("ntsc= argument not recognised\n");
467 return 0;
468 }
469
470 /* no match found */
471 return 0;
472}
473
474static void cx18_process_options(struct cx18 *cx)
475{
476 int i, j;
477
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300478 cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300479 cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
480 cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300481 cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
482 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
483 cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300484 cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */
485
486 cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;
487 cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;
488 cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;
489 cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;
490 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;
491 cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;
492 cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */
493
494 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;
495 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
496 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
497 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
Andy Walls466df462009-02-07 15:47:28 -0300498 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300499 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
500 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
501
Andy Walls466df462009-02-07 15:47:28 -0300502 /* Ensure stream_buffers & stream_buf_size are valid */
Andy Walls6ecd86d2008-12-07 23:30:17 -0300503 for (i = 0; i < CX18_MAX_STREAMS; i++) {
Andy Walls466df462009-02-07 15:47:28 -0300504 if (cx->stream_buffers[i] == 0 || /* User said 0 buffers */
505 cx->options.megabytes[i] <= 0 || /* User said 0 MB total */
506 cx->stream_buf_size[i] <= 0) { /* User said buf size 0 */
Andy Walls6ecd86d2008-12-07 23:30:17 -0300507 cx->options.megabytes[i] = 0;
508 cx->stream_buffers[i] = 0;
509 cx->stream_buf_size[i] = 0;
510 continue;
511 }
Andy Walls466df462009-02-07 15:47:28 -0300512 /*
Andy Walls22dce182009-11-09 23:55:30 -0300513 * YUV is a special case where the stream_buf_size needs to be
514 * an integral multiple of 33.75 kB (storage for 32 screens
Andy Wallsefc0b122009-12-30 22:54:53 -0300515 * lines to maintain alignment in case of lost buffers).
516 *
517 * IDX is a special case where the stream_buf_size should be
518 * an integral multiple of 1.5 kB (storage for 64 index entries
519 * to maintain alignment in case of lost buffers).
520 *
Andy Walls22dce182009-11-09 23:55:30 -0300521 */
522 if (i == CX18_ENC_STREAM_TYPE_YUV) {
523 cx->stream_buf_size[i] *= 1024;
524 cx->stream_buf_size[i] -=
525 (cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE);
526
527 if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE)
528 cx->stream_buf_size[i] =
529 CX18_UNIT_ENC_YUV_BUFSIZE;
Andy Wallsefc0b122009-12-30 22:54:53 -0300530 } else if (i == CX18_ENC_STREAM_TYPE_IDX) {
531 cx->stream_buf_size[i] *= 1024;
532 cx->stream_buf_size[i] -=
533 (cx->stream_buf_size[i] % CX18_UNIT_ENC_IDX_BUFSIZE);
534
535 if (cx->stream_buf_size[i] < CX18_UNIT_ENC_IDX_BUFSIZE)
536 cx->stream_buf_size[i] =
537 CX18_UNIT_ENC_IDX_BUFSIZE;
Andy Walls22dce182009-11-09 23:55:30 -0300538 }
539 /*
Andy Wallsefc0b122009-12-30 22:54:53 -0300540 * YUV and IDX are special cases where the stream_buf_size is
Andy Walls22dce182009-11-09 23:55:30 -0300541 * now in bytes.
Andy Walls466df462009-02-07 15:47:28 -0300542 * VBI is a special case where the stream_buf_size is fixed
543 * and already in bytes
544 */
Andy Walls22dce182009-11-09 23:55:30 -0300545 if (i == CX18_ENC_STREAM_TYPE_VBI ||
Andy Wallsefc0b122009-12-30 22:54:53 -0300546 i == CX18_ENC_STREAM_TYPE_YUV ||
547 i == CX18_ENC_STREAM_TYPE_IDX) {
Andy Walls466df462009-02-07 15:47:28 -0300548 if (cx->stream_buffers[i] < 0) {
549 cx->stream_buffers[i] =
550 cx->options.megabytes[i] * 1024 * 1024
551 / cx->stream_buf_size[i];
552 } else {
553 /* N.B. This might round down to 0 */
554 cx->options.megabytes[i] =
555 cx->stream_buffers[i]
556 * cx->stream_buf_size[i]/(1024 * 1024);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300557 }
Andy Walls6ecd86d2008-12-07 23:30:17 -0300558 } else {
Andy Walls22dce182009-11-09 23:55:30 -0300559 /* All other streams have stream_buf_size in kB here */
560 if (cx->stream_buffers[i] < 0) {
561 cx->stream_buffers[i] =
562 cx->options.megabytes[i] * 1024
563 / cx->stream_buf_size[i];
564 } else {
565 /* N.B. This might round down to 0 */
566 cx->options.megabytes[i] =
567 cx->stream_buffers[i]
568 * cx->stream_buf_size[i] / 1024;
569 }
570 /* convert from kB to bytes */
571 cx->stream_buf_size[i] *= 1024;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300572 }
Andy Walls22dce182009-11-09 23:55:30 -0300573 CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, "
574 "%d bytes\n", i, cx->options.megabytes[i],
575 cx->stream_buffers[i], cx->stream_buf_size[i]);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300576 }
577
Andy Walls5811cf92009-02-14 17:08:37 -0300578 cx->options.cardtype = cardtype[cx->instance];
579 cx->options.tuner = tuner[cx->instance];
580 cx->options.radio = radio[cx->instance];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300581
582 cx->std = cx18_parse_std(cx);
583 if (cx->options.cardtype == -1) {
584 CX18_INFO("Ignore card\n");
585 return;
586 }
587 cx->card = cx18_get_card(cx->options.cardtype - 1);
588 if (cx->card)
589 CX18_INFO("User specified %s card\n", cx->card->name);
590 else if (cx->options.cardtype != 0)
591 CX18_ERR("Unknown user specified type, trying to autodetect card\n");
592 if (cx->card == NULL) {
Andy Walls3d059132009-01-10 21:54:39 -0300593 if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300594 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
595 CX18_INFO("Autodetected Hauppauge card\n");
596 }
597 }
598 if (cx->card == NULL) {
599 for (i = 0; (cx->card = cx18_get_card(i)); i++) {
600 if (cx->card->pci_list == NULL)
601 continue;
602 for (j = 0; cx->card->pci_list[j].device; j++) {
Andy Walls3d059132009-01-10 21:54:39 -0300603 if (cx->pci_dev->device !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300604 cx->card->pci_list[j].device)
605 continue;
Andy Walls3d059132009-01-10 21:54:39 -0300606 if (cx->pci_dev->subsystem_vendor !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300607 cx->card->pci_list[j].subsystem_vendor)
608 continue;
Andy Walls3d059132009-01-10 21:54:39 -0300609 if (cx->pci_dev->subsystem_device !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300610 cx->card->pci_list[j].subsystem_device)
611 continue;
612 CX18_INFO("Autodetected %s card\n", cx->card->name);
613 goto done;
614 }
615 }
616 }
617done:
618
619 if (cx->card == NULL) {
620 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
Bjorn Helgaas29e66a62008-09-04 17:24:51 -0300621 CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
Andy Walls3d059132009-01-10 21:54:39 -0300622 cx->pci_dev->vendor, cx->pci_dev->device);
Bjorn Helgaas29e66a62008-09-04 17:24:51 -0300623 CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n",
Andy Walls3d059132009-01-10 21:54:39 -0300624 cx->pci_dev->subsystem_vendor,
625 cx->pci_dev->subsystem_device);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300626 CX18_ERR("Defaulting to %s card\n", cx->card->name);
627 CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
628 CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
629 CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
630 }
631 cx->v4l2_cap = cx->card->v4l2_capabilities;
632 cx->card_name = cx->card->name;
633 cx->card_i2c = cx->card->i2c;
634}
635
Andy Walls87116152009-04-13 22:42:43 -0300636static int __devinit cx18_create_in_workq(struct cx18 *cx)
637{
638 snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in",
639 cx->v4l2_dev.name);
640 cx->in_work_queue = create_singlethread_workqueue(cx->in_workq_name);
641 if (cx->in_work_queue == NULL) {
642 CX18_ERR("Unable to create incoming mailbox handler thread\n");
643 return -ENOMEM;
644 }
645 return 0;
646}
647
648static int __devinit cx18_create_out_workq(struct cx18 *cx)
649{
650 snprintf(cx->out_workq_name, sizeof(cx->out_workq_name), "%s-out",
651 cx->v4l2_dev.name);
652 cx->out_work_queue = create_workqueue(cx->out_workq_name);
653 if (cx->out_work_queue == NULL) {
654 CX18_ERR("Unable to create outgoing mailbox handler threads\n");
655 return -ENOMEM;
656 }
657 return 0;
658}
659
660static void __devinit cx18_init_in_work_orders(struct cx18 *cx)
661{
662 int i;
663 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
664 cx->in_work_order[i].cx = cx;
665 cx->in_work_order[i].str = cx->epu_debug_str;
666 INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler);
667 }
668}
669
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300670/* Precondition: the cx18 structure has been memset to 0. Only
Andy Walls5811cf92009-02-14 17:08:37 -0300671 the dev and instance fields have been filled in.
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300672 No assumptions on the card type may be made here (see cx18_init_struct2
673 for that).
674 */
675static int __devinit cx18_init_struct1(struct cx18 *cx)
676{
Andy Walls87116152009-04-13 22:42:43 -0300677 int ret;
Andy Wallsee2d64f2008-11-16 01:38:19 -0300678
Andy Walls3d059132009-01-10 21:54:39 -0300679 cx->base_addr = pci_resource_start(cx->pci_dev, 0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300680
681 mutex_init(&cx->serialize_lock);
Andy Walls8abdd002008-07-13 19:05:25 -0300682 mutex_init(&cx->gpio_lock);
Andy Walls72c2d6d2008-11-06 01:15:41 -0300683 mutex_init(&cx->epu2apu_mb_lock);
684 mutex_init(&cx->epu2cpu_mb_lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300685
Andy Walls87116152009-04-13 22:42:43 -0300686 ret = cx18_create_out_workq(cx);
687 if (ret)
688 return ret;
689
690 ret = cx18_create_in_workq(cx);
691 if (ret) {
692 destroy_workqueue(cx->out_work_queue);
693 return ret;
Andy Walls572bfea2008-11-25 21:43:05 -0300694 }
695
Andy Walls87116152009-04-13 22:42:43 -0300696 cx18_init_in_work_orders(cx);
Andy Walls1d6782b2008-11-05 00:49:14 -0300697
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300698 /* start counting open_id at 1 */
699 cx->open_id = 1;
700
701 /* Initial settings */
702 cx2341x_fill_defaults(&cx->params);
703 cx->temporal_strength = cx->params.video_temporal_filter;
704 cx->spatial_strength = cx->params.video_spatial_filter;
705 cx->filter_mode = cx->params.video_spatial_filter_mode |
706 (cx->params.video_temporal_filter_mode << 1) |
707 (cx->params.video_median_filter_type << 2);
708 cx->params.port = CX2341X_PORT_MEMORY;
Andy Walls302df972009-01-31 00:33:02 -0300709 cx->params.capabilities =
Andy Walls006e7172009-02-21 22:44:50 -0300710 CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300711 init_waitqueue_head(&cx->cap_w);
712 init_waitqueue_head(&cx->mb_apu_waitq);
713 init_waitqueue_head(&cx->mb_cpu_waitq);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300714 init_waitqueue_head(&cx->dma_waitq);
715
716 /* VBI */
Andy Wallsdd073432008-12-12 16:24:04 -0300717 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300718 cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
Andy Wallsaf009cf2008-12-12 20:00:29 -0300719
Andy Walls52fcb3e2009-11-08 23:45:24 -0300720 /* IVTV style VBI insertion into MPEG streams */
721 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list);
722 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list);
723 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list);
724 list_add(&cx->vbi.sliced_mpeg_buf.list,
725 &cx->vbi.sliced_mpeg_mdl.buf_list);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300726 return 0;
727}
728
729/* Second initialization part. Here the card type has been
730 autodetected. */
731static void __devinit cx18_init_struct2(struct cx18 *cx)
732{
733 int i;
734
735 for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
736 if (cx->card->video_inputs[i].video_type == 0)
737 break;
738 cx->nof_inputs = i;
739 for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
740 if (cx->card->audio_inputs[i].audio_type == 0)
741 break;
742 cx->nof_audio_inputs = i;
743
744 /* Find tuner input */
745 for (i = 0; i < cx->nof_inputs; i++) {
746 if (cx->card->video_inputs[i].video_type ==
747 CX18_CARD_INPUT_VID_TUNER)
748 break;
749 }
750 if (i == cx->nof_inputs)
751 i = 0;
752 cx->active_input = i;
753 cx->audio_input = cx->card->video_inputs[i].audio_index;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300754}
755
Andy Walls3d059132009-01-10 21:54:39 -0300756static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300757 const struct pci_device_id *pci_id)
758{
759 u16 cmd;
760 unsigned char pci_latency;
761
762 CX18_DEBUG_INFO("Enabling pci device\n");
763
Andy Walls3d059132009-01-10 21:54:39 -0300764 if (pci_enable_device(pci_dev)) {
Andy Walls5811cf92009-02-14 17:08:37 -0300765 CX18_ERR("Can't enable device %d!\n", cx->instance);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300766 return -EIO;
767 }
Andy Walls3d059132009-01-10 21:54:39 -0300768 if (pci_set_dma_mask(pci_dev, 0xffffffff)) {
Andy Walls5811cf92009-02-14 17:08:37 -0300769 CX18_ERR("No suitable DMA available, card %d\n", cx->instance);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300770 return -EIO;
771 }
772 if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
Andy Walls5811cf92009-02-14 17:08:37 -0300773 CX18_ERR("Cannot request encoder memory region, card %d\n",
774 cx->instance);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300775 return -EIO;
776 }
777
Andy Walls45190642008-08-29 16:10:21 -0300778 /* Enable bus mastering and memory mapped IO for the CX23418 */
Andy Walls3d059132009-01-10 21:54:39 -0300779 pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);
Andy Walls45190642008-08-29 16:10:21 -0300780 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Andy Walls3d059132009-01-10 21:54:39 -0300781 pci_write_config_word(pci_dev, PCI_COMMAND, cmd);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300782
Andy Walls3d059132009-01-10 21:54:39 -0300783 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &cx->card_rev);
784 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300785
786 if (pci_latency < 64 && cx18_pci_latency) {
787 CX18_INFO("Unreasonably low latency timer, "
788 "setting to 64 (was %d)\n", pci_latency);
Andy Walls3d059132009-01-10 21:54:39 -0300789 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);
790 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300791 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300792
793 CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
794 "irq: %d, latency: %d, memory: 0x%lx\n",
Andy Walls3d059132009-01-10 21:54:39 -0300795 cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,
796 PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),
797 cx->pci_dev->irq, pci_latency, (unsigned long)cx->base_addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300798
799 return 0;
800}
801
Andy Wallsff2a2002009-02-20 23:52:13 -0300802static void cx18_init_subdevs(struct cx18 *cx)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300803{
804 u32 hw = cx->card->hw_all;
Andy Wallsff2a2002009-02-20 23:52:13 -0300805 u32 device;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300806 int i;
807
Andy Wallsff2a2002009-02-20 23:52:13 -0300808 for (i = 0, device = 1; i < 32; i++, device <<= 1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300809
810 if (!(device & hw))
811 continue;
Andy Wallsff2a2002009-02-20 23:52:13 -0300812
813 switch (device) {
Andy Wallsff2a2002009-02-20 23:52:13 -0300814 case CX18_HW_DVB:
815 case CX18_HW_TVEEPROM:
816 /* These subordinate devices do not use probing */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300817 cx->hw_flags |= device;
Andy Wallsff2a2002009-02-20 23:52:13 -0300818 break;
819 case CX18_HW_418_AV:
820 /* The A/V decoder gets probed earlier to set PLLs */
821 /* Just note that the card uses it (i.e. has analog) */
822 cx->hw_flags |= device;
823 break;
Andy Wallseefe1012009-02-21 18:42:49 -0300824 case CX18_HW_GPIO_RESET_CTRL:
825 /*
826 * The Reset Controller gets probed and added to
827 * hw_flags earlier for i2c adapter/bus initialization
828 */
829 break;
830 case CX18_HW_GPIO_MUX:
831 if (cx18_gpio_register(cx, device) == 0)
832 cx->hw_flags |= device;
833 break;
Andy Wallsff2a2002009-02-20 23:52:13 -0300834 default:
835 if (cx18_i2c_register(cx, i) == 0)
836 cx->hw_flags |= device;
837 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300838 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300839 }
840
Andy Wallsff2a2002009-02-20 23:52:13 -0300841 if (cx->hw_flags & CX18_HW_418_AV)
842 cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV);
843
844 if (cx->card->hw_muxer != 0)
845 cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300846}
847
Andy Walls3d059132009-01-10 21:54:39 -0300848static int __devinit cx18_probe(struct pci_dev *pci_dev,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300849 const struct pci_device_id *pci_id)
850{
851 int retval = 0;
Andy Wallsff086572008-10-18 08:51:28 -0300852 int i;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300853 u32 devtype;
854 struct cx18 *cx;
855
Andy Walls5811cf92009-02-14 17:08:37 -0300856 /* FIXME - module parameter arrays constrain max instances */
857 i = atomic_inc_return(&cx18_instance) - 1;
858 if (i >= CX18_MAX_CARDS) {
859 printk(KERN_ERR "cx18: cannot manage card %d, driver has a "
860 "limit of 0 - %d\n", i, CX18_MAX_CARDS - 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300861 return -ENOMEM;
862 }
863
864 cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
Andy Walls5811cf92009-02-14 17:08:37 -0300865 if (cx == NULL) {
866 printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n",
867 i);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300868 return -ENOMEM;
869 }
Andy Walls888cdb02009-01-11 15:08:53 -0300870 cx->pci_dev = pci_dev;
Andy Walls5811cf92009-02-14 17:08:37 -0300871 cx->instance = i;
872
Andy Walls888cdb02009-01-11 15:08:53 -0300873 retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);
874 if (retval) {
Andy Walls5811cf92009-02-14 17:08:37 -0300875 printk(KERN_ERR "cx18: v4l2_device_register of card %d failed"
876 "\n", cx->instance);
877 kfree(cx);
878 return retval;
Andy Walls888cdb02009-01-11 15:08:53 -0300879 }
Andy Walls5811cf92009-02-14 17:08:37 -0300880 snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d",
881 cx->instance);
882 CX18_INFO("Initializing card %d\n", cx->instance);
Andy Walls888cdb02009-01-11 15:08:53 -0300883
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300884 cx18_process_options(cx);
885 if (cx->options.cardtype == -1) {
886 retval = -ENODEV;
Andy Walls5811cf92009-02-14 17:08:37 -0300887 goto err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300888 }
Andy Walls87116152009-04-13 22:42:43 -0300889
890 retval = cx18_init_struct1(cx);
891 if (retval)
Andy Walls5811cf92009-02-14 17:08:37 -0300892 goto err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300893
894 CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);
895
896 /* PCI Device Setup */
Andy Walls3d059132009-01-10 21:54:39 -0300897 retval = cx18_setup_pci(cx, pci_dev, pci_id);
Andy Walls572bfea2008-11-25 21:43:05 -0300898 if (retval != 0)
Andy Walls87116152009-04-13 22:42:43 -0300899 goto free_workqueues;
Andy Walls572bfea2008-11-25 21:43:05 -0300900
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300901 /* map io memory */
902 CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
903 cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
904 cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
905 CX18_MEM_SIZE);
906 if (!cx->enc_mem) {
907 CX18_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n");
908 CX18_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n");
909 retval = -ENOMEM;
910 goto free_mem;
911 }
912 cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
Andy Wallsb1526422008-08-30 16:03:44 -0300913 devtype = cx18_read_reg(cx, 0xC72028);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300914 switch (devtype & 0xff000000) {
915 case 0xff000000:
916 CX18_INFO("cx23418 revision %08x (A)\n", devtype);
917 break;
918 case 0x01000000:
919 CX18_INFO("cx23418 revision %08x (B)\n", devtype);
920 break;
921 default:
922 CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
923 break;
924 }
925
926 cx18_init_power(cx, 1);
927 cx18_init_memory(cx);
928
Al Viro990c81c2008-05-21 00:32:01 -0300929 cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300930 cx18_init_scb(cx);
931
932 cx18_gpio_init(cx);
933
Andy Wallsff2a2002009-02-20 23:52:13 -0300934 /* Initialize integrated A/V decoder early to set PLLs, just in case */
935 retval = cx18_av_probe(cx);
Andy Wallsfa3e7032009-02-16 02:23:25 -0300936 if (retval) {
937 CX18_ERR("Could not register A/V decoder subdevice\n");
938 goto free_map;
939 }
Andy Wallsfa3e7032009-02-16 02:23:25 -0300940
Andy Wallseefe1012009-02-21 18:42:49 -0300941 /* Initialize GPIO Reset Controller to do chip resets during i2c init */
942 if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) {
943 if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0)
944 CX18_WARN("Could not register GPIO reset controller"
945 "subdevice; proceeding anyway.\n");
946 else
947 cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL;
948 }
949
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300950 /* active i2c */
951 CX18_DEBUG_INFO("activating i2c...\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300952 retval = init_cx18_i2c(cx);
953 if (retval) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300954 CX18_ERR("Could not initialize i2c\n");
955 goto free_map;
956 }
957
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300958 if (cx->card->hw_all & CX18_HW_TVEEPROM) {
959 /* Based on the model number the cardtype may be changed.
960 The PCI IDs are not always reliable. */
961 cx18_process_eeprom(cx);
962 }
963 if (cx->card->comment)
964 CX18_INFO("%s", cx->card->comment);
965 if (cx->card->v4l2_capabilities == 0) {
966 retval = -ENODEV;
967 goto free_i2c;
968 }
969 cx18_init_memory(cx);
Andy Wallsfd6b9c92008-12-14 21:26:25 -0300970 cx18_init_scb(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300971
972 /* Register IRQ */
Andy Walls3d059132009-01-10 21:54:39 -0300973 retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
Andy Walls5811cf92009-02-14 17:08:37 -0300974 IRQF_SHARED | IRQF_DISABLED,
975 cx->v4l2_dev.name, (void *)cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300976 if (retval) {
977 CX18_ERR("Failed to register irq %d\n", retval);
978 goto free_i2c;
979 }
980
981 if (cx->std == 0)
982 cx->std = V4L2_STD_NTSC_M;
983
984 if (cx->options.tuner == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300985 for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
986 if ((cx->std & cx->card->tuners[i].std) == 0)
987 continue;
988 cx->options.tuner = cx->card->tuners[i].tuner;
989 break;
990 }
991 }
992 /* if no tuner was found, then pick the first tuner in the card list */
993 if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
994 cx->std = cx->card->tuners[0].std;
Hans Verkuilc3cb4d92008-06-27 23:27:25 -0300995 if (cx->std & V4L2_STD_PAL)
996 cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
997 else if (cx->std & V4L2_STD_NTSC)
998 cx->std = V4L2_STD_NTSC_M;
999 else if (cx->std & V4L2_STD_SECAM)
1000 cx->std = V4L2_STD_SECAM_L;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001001 cx->options.tuner = cx->card->tuners[0].tuner;
1002 }
1003 if (cx->options.radio == -1)
1004 cx->options.radio = (cx->card->radio_input.audio_type != 0);
1005
1006 /* The card is now fully identified, continue with card-specific
1007 initialization. */
1008 cx18_init_struct2(cx);
1009
Andy Wallsff2a2002009-02-20 23:52:13 -03001010 cx18_init_subdevs(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001011
Andy Walls8d037ed2009-02-21 22:35:11 -03001012 if (cx->std & V4L2_STD_525_60)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001013 cx->is_60hz = 1;
Andy Walls8d037ed2009-02-21 22:35:11 -03001014 else
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001015 cx->is_50hz = 1;
Andy Walls8d037ed2009-02-21 22:35:11 -03001016
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001017 cx->params.video_gop_size = cx->is_60hz ? 15 : 12;
1018
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001019 if (cx->options.radio > 0)
1020 cx->v4l2_cap |= V4L2_CAP_RADIO;
1021
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001022 if (cx->options.tuner > -1) {
1023 struct tuner_setup setup;
1024
1025 setup.addr = ADDR_UNSET;
1026 setup.type = cx->options.tuner;
1027 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
1028 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
1029 cx18_reset_tuner_gpio : NULL;
Andy Wallsff2a2002009-02-20 23:52:13 -03001030 cx18_call_all(cx, tuner, s_type_addr, &setup);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001031 if (setup.type == TUNER_XC2028) {
1032 static struct xc2028_ctrl ctrl = {
1033 .fname = XC2028_DEFAULT_FIRMWARE,
1034 .max_len = 64,
1035 };
1036 struct v4l2_priv_tun_config cfg = {
1037 .tuner = cx->options.tuner,
1038 .priv = &ctrl,
1039 };
Andy Wallsff2a2002009-02-20 23:52:13 -03001040 cx18_call_all(cx, tuner, s_config, &cfg);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001041 }
1042 }
1043
1044 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
1045 are not. */
1046 cx->tuner_std = cx->std;
1047
Hans Verkuil5e7fdc52008-05-30 10:51:53 -03001048 retval = cx18_streams_setup(cx);
1049 if (retval) {
1050 CX18_ERR("Error %d setting up streams\n", retval);
1051 goto free_irq;
1052 }
1053 retval = cx18_streams_register(cx);
1054 if (retval) {
1055 CX18_ERR("Error %d registering devices\n", retval);
1056 goto free_streams;
1057 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001058
Andy Walls5811cf92009-02-14 17:08:37 -03001059 CX18_INFO("Initialized card: %s\n", cx->card_name);
Devin Heitmuellerd68b6872009-11-20 01:15:54 -03001060
1061 /* Load cx18 submodules (cx18-alsa) */
1062 request_modules(cx);
1063
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001064 return 0;
1065
1066free_streams:
Hans Verkuil3f983872008-05-01 10:31:12 -03001067 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001068free_irq:
Andy Walls3d059132009-01-10 21:54:39 -03001069 free_irq(cx->pci_dev->irq, (void *)cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001070free_i2c:
1071 exit_cx18_i2c(cx);
1072free_map:
1073 cx18_iounmap(cx);
1074free_mem:
1075 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
Andy Walls87116152009-04-13 22:42:43 -03001076free_workqueues:
Andy Wallsdeed75e2009-04-13 22:22:40 -03001077 destroy_workqueue(cx->in_work_queue);
Andy Walls87116152009-04-13 22:42:43 -03001078 destroy_workqueue(cx->out_work_queue);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001079err:
1080 if (retval == 0)
1081 retval = -ENODEV;
1082 CX18_ERR("Error %d on initialization\n", retval);
1083
Andy Walls5811cf92009-02-14 17:08:37 -03001084 v4l2_device_unregister(&cx->v4l2_dev);
1085 kfree(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001086 return retval;
1087}
1088
1089int cx18_init_on_first_open(struct cx18 *cx)
1090{
1091 int video_input;
1092 int fw_retry_count = 3;
1093 struct v4l2_frequency vf;
Andy Walls3b6fe582008-06-21 08:36:31 -03001094 struct cx18_open_id fh;
1095
1096 fh.cx = cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001097
1098 if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
1099 return -ENXIO;
1100
1101 if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
1102 return 0;
1103
1104 while (--fw_retry_count > 0) {
1105 /* load firmware */
1106 if (cx18_firmware_init(cx) == 0)
1107 break;
1108 if (fw_retry_count > 1)
1109 CX18_WARN("Retry loading firmware\n");
1110 }
1111
1112 if (fw_retry_count == 0) {
1113 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1114 return -ENXIO;
1115 }
1116 set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
1117
Andy Walls350145a2009-01-04 21:51:17 -03001118 /*
1119 * Init the firmware twice to work around a silicon bug
1120 * with the digital TS.
1121 *
1122 * The second firmware load requires us to normalize the APU state,
1123 * or the audio for the first analog capture will be badly incorrect.
1124 *
1125 * I can't seem to call APU_RESETAI and have it succeed without the
1126 * APU capturing audio, so we start and stop it here to do the reset
1127 */
1128
1129 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1130 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1131 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1132 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001133
1134 fw_retry_count = 3;
1135 while (--fw_retry_count > 0) {
1136 /* load firmware */
1137 if (cx18_firmware_init(cx) == 0)
1138 break;
1139 if (fw_retry_count > 1)
1140 CX18_WARN("Retry loading firmware\n");
1141 }
1142
1143 if (fw_retry_count == 0) {
1144 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1145 return -ENXIO;
1146 }
1147
Andy Wallsd5c02f62009-01-30 22:48:40 -03001148 /*
1149 * The second firmware load requires us to normalize the APU state,
1150 * or the audio for the first analog capture will be badly incorrect.
1151 *
1152 * I can't seem to call APU_RESETAI and have it succeed without the
1153 * APU capturing audio, so we start and stop it here to do the reset
1154 */
1155
1156 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1157 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1158 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1159 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1160
Andy Wallsfa3e7032009-02-16 02:23:25 -03001161 /* Init the A/V decoder, if it hasn't been already */
Hans Verkuilcc26b072009-03-29 19:20:26 -03001162 v4l2_subdev_call(cx->sd_av, core, load_fw);
Andy Wallsfa3e7032009-02-16 02:23:25 -03001163
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001164 vf.tuner = 0;
1165 vf.type = V4L2_TUNER_ANALOG_TV;
1166 vf.frequency = 6400; /* the tuner 'baseline' frequency */
1167
1168 /* Set initial frequency. For PAL/SECAM broadcasts no
1169 'default' channel exists AFAIK. */
1170 if (cx->std == V4L2_STD_NTSC_M_JP)
1171 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
1172 else if (cx->std & V4L2_STD_NTSC_M)
1173 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
1174
1175 video_input = cx->active_input;
1176 cx->active_input++; /* Force update of input */
Andy Walls3b6fe582008-06-21 08:36:31 -03001177 cx18_s_input(NULL, &fh, video_input);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001178
1179 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1180 in one place. */
1181 cx->std++; /* Force full standard initialization */
Andy Walls3b6fe582008-06-21 08:36:31 -03001182 cx18_s_std(NULL, &fh, &cx->tuner_std);
1183 cx18_s_frequency(NULL, &fh, &vf);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001184 return 0;
1185}
1186
Andy Wallsdeed75e2009-04-13 22:22:40 -03001187static void cx18_cancel_in_work_orders(struct cx18 *cx)
Andy Walls18b5dc22008-11-16 17:15:01 -03001188{
1189 int i;
Andy Wallsdeed75e2009-04-13 22:22:40 -03001190 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++)
1191 cancel_work_sync(&cx->in_work_order[i].work);
Andy Walls18b5dc22008-11-16 17:15:01 -03001192}
1193
Andy Walls21a278b2009-04-15 20:45:10 -03001194static void cx18_cancel_out_work_orders(struct cx18 *cx)
1195{
1196 int i;
1197 for (i = 0; i < CX18_MAX_STREAMS; i++)
1198 if (&cx->streams[i].video_dev != NULL)
1199 cancel_work_sync(&cx->streams[i].out_work_order);
1200}
1201
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001202static void cx18_remove(struct pci_dev *pci_dev)
1203{
Andy Walls888cdb02009-01-11 15:08:53 -03001204 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
Andy Walls5811cf92009-02-14 17:08:37 -03001205 struct cx18 *cx = to_cx18(v4l2_dev);
Andy Walls6da6bf52009-02-21 19:53:54 -03001206 int i;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001207
Andy Walls5811cf92009-02-14 17:08:37 -03001208 CX18_DEBUG_INFO("Removing Card\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001209
1210 /* Stop all captures */
1211 CX18_DEBUG_INFO("Stopping all streams\n");
Hans Verkuil31554ae2008-05-25 11:21:27 -03001212 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001213 cx18_stop_all_captures(cx);
1214
Andy Walls87116152009-04-13 22:42:43 -03001215 /* Stop interrupts that cause incoming work to be queued */
Andy Wallsb1526422008-08-30 16:03:44 -03001216 cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
Andy Walls87116152009-04-13 22:42:43 -03001217
1218 /* Incoming work can cause outgoing work, so clean up incoming first */
1219 cx18_cancel_in_work_orders(cx);
Andy Walls21a278b2009-04-15 20:45:10 -03001220 cx18_cancel_out_work_orders(cx);
Andy Walls87116152009-04-13 22:42:43 -03001221
1222 /* Stop ack interrupts that may have been needed for work to finish */
Andy Wallsb1526422008-08-30 16:03:44 -03001223 cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001224
1225 cx18_halt_firmware(cx);
1226
Andy Wallsdeed75e2009-04-13 22:22:40 -03001227 destroy_workqueue(cx->in_work_queue);
Andy Walls87116152009-04-13 22:42:43 -03001228 destroy_workqueue(cx->out_work_queue);
Andy Walls572bfea2008-11-25 21:43:05 -03001229
Hans Verkuil3f983872008-05-01 10:31:12 -03001230 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001231
1232 exit_cx18_i2c(cx);
1233
Andy Walls3d059132009-01-10 21:54:39 -03001234 free_irq(cx->pci_dev->irq, (void *)cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001235
Hans Verkuilcba627a2008-05-12 14:48:26 -03001236 cx18_iounmap(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001237
1238 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1239
Andy Walls3d059132009-01-10 21:54:39 -03001240 pci_disable_device(cx->pci_dev);
Andy Walls6da6bf52009-02-21 19:53:54 -03001241
1242 if (cx->vbi.sliced_mpeg_data[0] != NULL)
1243 for (i = 0; i < CX18_VBI_FRAMES; i++)
1244 kfree(cx->vbi.sliced_mpeg_data[i]);
Andy Walls5811cf92009-02-14 17:08:37 -03001245
1246 CX18_INFO("Removed %s\n", cx->card_name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001247
Andy Walls888cdb02009-01-11 15:08:53 -03001248 v4l2_device_unregister(v4l2_dev);
Andy Walls5811cf92009-02-14 17:08:37 -03001249 kfree(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001250}
1251
Devin Heitmuellerd68b6872009-11-20 01:15:54 -03001252
1253#if defined(CONFIG_MODULES) && defined(MODULE)
1254static void request_module_async(struct work_struct *work)
1255{
Devin Heitmuellerf8bd9d22009-12-20 23:29:02 -03001256 struct cx18 *dev = container_of(work, struct cx18, request_module_wk);
Devin Heitmuellerd68b6872009-11-20 01:15:54 -03001257
1258 /* Make sure cx18-alsa module is loaded */
1259 request_module("cx18-alsa");
1260
1261 /* Initialize cx18-alsa for this instance of the cx18 device */
1262 if (cx18_ext_init != NULL)
1263 cx18_ext_init(dev);
1264}
1265
1266static void request_modules(struct cx18 *dev)
1267{
1268 INIT_WORK(&dev->request_module_wk, request_module_async);
1269 schedule_work(&dev->request_module_wk);
1270}
1271#else
1272#define request_modules(dev)
1273#endif /* CONFIG_MODULES */
1274
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001275/* define a pci_driver for card detection */
1276static struct pci_driver cx18_pci_driver = {
1277 .name = "cx18",
1278 .id_table = cx18_pci_tbl,
1279 .probe = cx18_probe,
1280 .remove = cx18_remove,
1281};
1282
Peter Huewe9710e7a2009-11-04 15:28:33 -03001283static int __init module_start(void)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001284{
Devin Heitmuellerf8bd9d22009-12-20 23:29:02 -03001285 printk(KERN_INFO "cx18: Start initialization, version %s\n",
1286 CX18_VERSION);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001287
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001288 /* Validate parameters */
1289 if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
Hans Verkuildd896012008-10-04 08:36:54 -03001290 printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n",
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001291 CX18_MAX_CARDS - 1);
1292 return -1;
1293 }
1294
1295 if (cx18_debug < 0 || cx18_debug > 511) {
1296 cx18_debug = 0;
1297 printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");
1298 }
1299
1300 if (pci_register_driver(&cx18_pci_driver)) {
1301 printk(KERN_ERR "cx18: Error detecting PCI card\n");
1302 return -ENODEV;
1303 }
1304 printk(KERN_INFO "cx18: End initialization\n");
1305 return 0;
1306}
1307
Peter Huewe9710e7a2009-11-04 15:28:33 -03001308static void __exit module_cleanup(void)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001309{
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001310 pci_unregister_driver(&cx18_pci_driver);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001311}
1312
1313module_init(module_start);
1314module_exit(module_cleanup);