blob: 321c1b79794c05c623833467b74a967059740285 [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 Walls6afdeaf2010-05-23 18:53:35 -03007 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.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"
Hans Verkuila75b9be2010-12-31 10:22:52 -030039#include "cx18-controls.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030040#include "tuner-xc2028.h"
41
42#include <media/tveeprom.h>
43
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030044/* If you have already X v4l cards, then set this to X. This way
45 the device numbers stay matched. Example: you have a WinTV card
46 without radio and a Compro H900 with. Normally this would give a
47 video1 device together with a radio0 device for the Compro. By
48 setting this to 1 you ensure that radio0 is now also radio1. */
49int cx18_first_minor;
50
Devin Heitmuellerd68b6872009-11-20 01:15:54 -030051/* Callback for registering extensions */
52int (*cx18_ext_init)(struct cx18 *);
53EXPORT_SYMBOL(cx18_ext_init);
54
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030055/* add your revision and whatnot here */
56static struct pci_device_id cx18_pci_tbl[] __devinitdata = {
57 {PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,
58 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
59 {0,}
60};
61
62MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);
63
Andy Walls5811cf92009-02-14 17:08:37 -030064static atomic_t cx18_instance = ATOMIC_INIT(0);
65
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030066/* Parameter declarations */
67static int cardtype[CX18_MAX_CARDS];
68static int tuner[CX18_MAX_CARDS] = { -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,
71 -1, -1, -1, -1, -1, -1, -1, -1 };
72static int radio[CX18_MAX_CARDS] = { -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,
75 -1, -1, -1, -1, -1, -1, -1, -1 };
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -030076static unsigned cardtype_c = 1;
77static unsigned tuner_c = 1;
78static unsigned radio_c = 1;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030079static char pal[] = "--";
80static char secam[] = "--";
81static char ntsc[] = "-";
82
83/* Buffers */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030084static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
Andy Walls6ecd86d2008-12-07 23:30:17 -030085static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
86static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030087static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
88static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
89static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;
90
Andy Walls6ecd86d2008-12-07 23:30:17 -030091static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
92static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
93static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
94static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
Andy Walls6ecd86d2008-12-07 23:30:17 -030095static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;
96
97static int enc_ts_bufs = -1;
98static int enc_mpg_bufs = -1;
Andy Wallsefc0b122009-12-30 22:54:53 -030099static int enc_idx_bufs = CX18_MAX_FW_MDLS_PER_STREAM;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300100static int enc_yuv_bufs = -1;
101static int enc_vbi_bufs = -1;
102static int enc_pcm_bufs = -1;
103
104
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300105static int cx18_pci_latency = 1;
106
Andy Walls3f75c612008-11-16 23:33:41 -0300107static int mmio_ndelay;
108static int retry_mmio = 1;
109
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300110int cx18_debug;
111
112module_param_array(tuner, int, &tuner_c, 0644);
113module_param_array(radio, bool, &radio_c, 0644);
114module_param_array(cardtype, int, &cardtype_c, 0644);
115module_param_string(pal, pal, sizeof(pal), 0644);
116module_param_string(secam, secam, sizeof(secam), 0644);
117module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
118module_param_named(debug, cx18_debug, int, 0644);
Andy Walls3f75c612008-11-16 23:33:41 -0300119module_param(mmio_ndelay, int, 0644);
120module_param(retry_mmio, int, 0644);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300121module_param(cx18_pci_latency, int, 0644);
122module_param(cx18_first_minor, int, 0644);
123
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300124module_param(enc_ts_buffers, int, 0644);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300125module_param(enc_mpg_buffers, int, 0644);
126module_param(enc_idx_buffers, int, 0644);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300127module_param(enc_yuv_buffers, int, 0644);
128module_param(enc_vbi_buffers, int, 0644);
129module_param(enc_pcm_buffers, int, 0644);
130
Andy Walls6ecd86d2008-12-07 23:30:17 -0300131module_param(enc_ts_bufsize, int, 0644);
132module_param(enc_mpg_bufsize, int, 0644);
133module_param(enc_idx_bufsize, int, 0644);
134module_param(enc_yuv_bufsize, int, 0644);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300135module_param(enc_pcm_bufsize, int, 0644);
136
137module_param(enc_ts_bufs, int, 0644);
138module_param(enc_mpg_bufs, int, 0644);
139module_param(enc_idx_bufs, int, 0644);
140module_param(enc_yuv_bufs, int, 0644);
141module_param(enc_vbi_bufs, int, 0644);
142module_param(enc_pcm_bufs, int, 0644);
143
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300144MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
145 "\t\t\tsee tuner.h for values");
146MODULE_PARM_DESC(radio,
147 "Enable or disable the radio. Use only if autodetection\n"
148 "\t\t\tfails. 0 = disable, 1 = enable");
149MODULE_PARM_DESC(cardtype,
150 "Only use this option if your card is not detected properly.\n"
151 "\t\tSpecify card type:\n"
152 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
153 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
154 "\t\t\t 3 = Compro VideoMate H900\n"
155 "\t\t\t 4 = Yuan MPC718\n"
Sri Deevi03c28082008-06-21 11:06:44 -0300156 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
Andy Walls9eee4fb2008-10-04 20:28:40 -0300157 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
Andy Walls9d5af862009-06-09 20:37:24 -0300158 "\t\t\t 7 = Leadtek WinFast PVR2100\n"
159 "\t\t\t 8 = Leadtek WinFast DVR3100 H\n"
Alexey Chernova3634362010-10-28 18:12:02 -0300160 "\t\t\t 9 = GoTView PCI DVD3 Hybrid\n"
Devin Heitmuellere3bfeab2011-02-26 02:44:38 -0300161 "\t\t\t 10 = Hauppauge HVR 1600 (S5H1411)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300162 "\t\t\t 0 = Autodetect (default)\n"
163 "\t\t\t-1 = Ignore this card\n\t\t");
164MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
165MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
166MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
167MODULE_PARM_DESC(debug,
168 "Debug level (bitmask). Default: 0\n"
169 "\t\t\t 1/0x0001: warning\n"
170 "\t\t\t 2/0x0002: info\n"
171 "\t\t\t 4/0x0004: mailbox\n"
172 "\t\t\t 8/0x0008: dma\n"
173 "\t\t\t 16/0x0010: ioctl\n"
174 "\t\t\t 32/0x0020: file\n"
175 "\t\t\t 64/0x0040: i2c\n"
176 "\t\t\t128/0x0080: irq\n"
177 "\t\t\t256/0x0100: high volume\n");
178MODULE_PARM_DESC(cx18_pci_latency,
179 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
180 "\t\t\tDefault: Yes");
Andy Wallsd267d852008-09-28 21:46:02 -0300181MODULE_PARM_DESC(retry_mmio,
Andy Walls3f75c612008-11-16 23:33:41 -0300182 "(Deprecated) MMIO writes are now always checked and retried\n"
183 "\t\t\tEffectively: 1 [Yes]");
Andy Wallsc641d092008-09-01 00:40:41 -0300184MODULE_PARM_DESC(mmio_ndelay,
Andy Walls3f75c612008-11-16 23:33:41 -0300185 "(Deprecated) MMIO accesses are now never purposely delayed\n"
186 "\t\t\tEffectively: 0 ns");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300187MODULE_PARM_DESC(enc_ts_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300188 "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300189 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300190MODULE_PARM_DESC(enc_ts_bufsize,
191 "Size of an encoder TS buffer (kB)\n"
192 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));
193MODULE_PARM_DESC(enc_ts_bufs,
194 "Number of encoder TS buffers\n"
195 "\t\t\tDefault is computed from other enc_ts_* parameters");
196MODULE_PARM_DESC(enc_mpg_buffers,
197 "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"
198 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
199MODULE_PARM_DESC(enc_mpg_bufsize,
200 "Size of an encoder MPG buffer (kB)\n"
201 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));
202MODULE_PARM_DESC(enc_mpg_bufs,
203 "Number of encoder MPG buffers\n"
204 "\t\t\tDefault is computed from other enc_mpg_* parameters");
205MODULE_PARM_DESC(enc_idx_buffers,
Andy Wallsefc0b122009-12-30 22:54:53 -0300206 "(Deprecated) Encoder IDX buffer memory (MB)\n"
207 "\t\t\tIgnored, except 0 disables IDX buffer allocations\n"
208 "\t\t\tDefault: 1 [Enabled]");
Andy Walls6ecd86d2008-12-07 23:30:17 -0300209MODULE_PARM_DESC(enc_idx_bufsize,
210 "Size of an encoder IDX buffer (kB)\n"
Andy Wallsefc0b122009-12-30 22:54:53 -0300211 "\t\t\tAllowed values are multiples of 1.5 kB rounded up\n"
212 "\t\t\t(multiples of size required for 64 index entries)\n"
213 "\t\t\tDefault: 2");
Andy Walls6ecd86d2008-12-07 23:30:17 -0300214MODULE_PARM_DESC(enc_idx_bufs,
215 "Number of encoder IDX buffers\n"
Andy Wallsefc0b122009-12-30 22:54:53 -0300216 "\t\t\tDefault: " __stringify(CX18_MAX_FW_MDLS_PER_STREAM));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300217MODULE_PARM_DESC(enc_yuv_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300218 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300219 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300220MODULE_PARM_DESC(enc_yuv_bufsize,
221 "Size of an encoder YUV buffer (kB)\n"
Andy Walls22dce182009-11-09 23:55:30 -0300222 "\t\t\tAllowed values are multiples of 33.75 kB rounded up\n"
223 "\t\t\t(multiples of size required for 32 screen lines)\n"
224 "\t\t\tDefault: 102");
Andy Walls6ecd86d2008-12-07 23:30:17 -0300225MODULE_PARM_DESC(enc_yuv_bufs,
226 "Number of encoder YUV buffers\n"
227 "\t\t\tDefault is computed from other enc_yuv_* parameters");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300228MODULE_PARM_DESC(enc_vbi_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300229 "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300230 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300231MODULE_PARM_DESC(enc_vbi_bufs,
232 "Number of encoder VBI buffers\n"
Andy Walls127ce5f2009-11-11 00:22:57 -0300233 "\t\t\tDefault is computed from enc_vbi_buffers");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300234MODULE_PARM_DESC(enc_pcm_buffers,
Andy Walls6ecd86d2008-12-07 23:30:17 -0300235 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300236 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
Andy Walls6ecd86d2008-12-07 23:30:17 -0300237MODULE_PARM_DESC(enc_pcm_bufsize,
238 "Size of an encoder PCM buffer (kB)\n"
239 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));
240MODULE_PARM_DESC(enc_pcm_bufs,
241 "Number of encoder PCM buffers\n"
242 "\t\t\tDefault is computed from other enc_pcm_* parameters");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300243
Devin Heitmuellerf8bd9d22009-12-20 23:29:02 -0300244MODULE_PARM_DESC(cx18_first_minor,
245 "Set device node number assigned to first card");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300246
247MODULE_AUTHOR("Hans Verkuil");
248MODULE_DESCRIPTION("CX23418 driver");
249MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
250MODULE_LICENSE("GPL");
251
252MODULE_VERSION(CX18_VERSION);
253
Mauro Carvalho Chehab583d3382010-01-28 00:19:00 -0200254#if defined(CONFIG_MODULES) && defined(MODULE)
255static void request_module_async(struct work_struct *work)
256{
257 struct cx18 *dev = container_of(work, struct cx18, request_module_wk);
258
259 /* Make sure cx18-alsa module is loaded */
260 request_module("cx18-alsa");
261
262 /* Initialize cx18-alsa for this instance of the cx18 device */
263 if (cx18_ext_init != NULL)
264 cx18_ext_init(dev);
265}
266
267static void request_modules(struct cx18 *dev)
268{
269 INIT_WORK(&dev->request_module_wk, request_module_async);
270 schedule_work(&dev->request_module_wk);
271}
Tejun Heo707bcf32010-12-24 16:14:20 +0100272
273static void flush_request_modules(struct cx18 *dev)
274{
275 flush_work_sync(&dev->request_module_wk);
276}
Mauro Carvalho Chehab583d3382010-01-28 00:19:00 -0200277#else
278#define request_modules(dev)
Tejun Heo707bcf32010-12-24 16:14:20 +0100279#define flush_request_modules(dev)
Mauro Carvalho Chehab583d3382010-01-28 00:19:00 -0200280#endif /* CONFIG_MODULES */
Devin Heitmuellerd68b6872009-11-20 01:15:54 -0300281
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300282/* Generic utility functions */
283int cx18_msleep_timeout(unsigned int msecs, int intr)
284{
Andy Walls330c6ec2008-11-08 14:19:37 -0300285 long int timeout = msecs_to_jiffies(msecs);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300286 int sig;
287
288 do {
289 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
290 timeout = schedule_timeout(timeout);
291 sig = intr ? signal_pending(current) : 0;
292 } while (!sig && timeout);
293 return sig;
294}
295
296/* Release ioremapped memory */
297static void cx18_iounmap(struct cx18 *cx)
298{
299 if (cx == NULL)
300 return;
301
302 /* Release io memory */
303 if (cx->enc_mem != NULL) {
304 CX18_DEBUG_INFO("releasing enc_mem\n");
305 iounmap(cx->enc_mem);
306 cx->enc_mem = NULL;
307 }
308}
309
Andy Walls25a42e42009-07-05 17:09:28 -0300310static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len)
311{
312 int i;
313
314 CX18_INFO("eeprom dump:\n");
315 for (i = 0; i < len; i++) {
316 if (0 == (i % 16))
317 CX18_INFO("eeprom %02x:", i);
318 printk(KERN_CONT " %02x", eedata[i]);
319 if (15 == (i % 16))
320 printk(KERN_CONT "\n");
321 }
322}
323
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300324/* Hauppauge card? get values from tveeprom */
325void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
326{
Andy Wallsff2a2002009-02-20 23:52:13 -0300327 struct i2c_client c;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300328 u8 eedata[256];
329
Andy Walls6246d4e2009-02-21 22:27:37 -0300330 memset(&c, 0, sizeof(c));
Andy Walls098003d2009-02-28 13:19:45 -0300331 strlcpy(c.name, "cx18 tveeprom tmp", sizeof(c.name));
Andy Wallsff2a2002009-02-20 23:52:13 -0300332 c.adapter = &cx->i2c_adap[0];
333 c.addr = 0xA0 >> 1;
334
Andy Walls25a42e42009-07-05 17:09:28 -0300335 memset(tv, 0, sizeof(*tv));
336 if (tveeprom_read(&c, eedata, sizeof(eedata)))
337 return;
338
339 switch (cx->card->type) {
340 case CX18_CARD_HVR_1600_ESMT:
341 case CX18_CARD_HVR_1600_SAMSUNG:
Devin Heitmuellere3bfeab2011-02-26 02:44:38 -0300342 case CX18_CARD_HVR_1600_S5H1411:
Andy Walls25a42e42009-07-05 17:09:28 -0300343 tveeprom_hauppauge_analog(&c, tv, eedata);
344 break;
345 case CX18_CARD_YUAN_MPC718:
Alexey Chernova3634362010-10-28 18:12:02 -0300346 case CX18_CARD_GOTVIEW_PCI_DVD3:
Andy Walls25a42e42009-07-05 17:09:28 -0300347 tv->model = 0x718;
348 cx18_eeprom_dump(cx, eedata, sizeof(eedata));
349 CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n",
350 eedata[2], eedata[1], eedata[4], eedata[3]);
351 break;
352 default:
353 tv->model = 0xffffffff;
354 cx18_eeprom_dump(cx, eedata, sizeof(eedata));
355 break;
356 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300357}
358
359static void cx18_process_eeprom(struct cx18 *cx)
360{
361 struct tveeprom tv;
362
363 cx18_read_eeprom(cx, &tv);
364
365 /* Many thanks to Steven Toth from Hauppauge for providing the
366 model numbers */
Hans Verkuil1d081602008-05-12 14:45:19 -0300367 /* Note: the Samsung memory models cannot be reliably determined
368 from the model number. Use the cardtype module option if you
369 have one of these preproduction models. */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300370 switch (tv.model) {
Devin Heitmuellere3bfeab2011-02-26 02:44:38 -0300371 case 74301: /* Retail models */
372 case 74321:
373 case 74351: /* OEM models */
374 case 74361:
375 /* Digital side is s5h1411/tda18271 */
376 cx->card = cx18_get_card(CX18_CARD_HVR_1600_S5H1411);
377 break;
378 case 74021: /* Retail models */
379 case 74031:
380 case 74041:
381 case 74141:
382 case 74541: /* OEM models */
383 case 74551:
384 case 74591:
385 case 74651:
386 case 74691:
387 case 74751:
388 case 74891:
389 /* Digital side is s5h1409/mxl5005s */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300390 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
391 break;
Andy Walls25a42e42009-07-05 17:09:28 -0300392 case 0x718:
393 return;
394 case 0xffffffff:
395 CX18_INFO("Unknown EEPROM encoding\n");
396 return;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300397 case 0:
398 CX18_ERR("Invalid EEPROM\n");
399 return;
400 default:
Devin Heitmuellere3bfeab2011-02-26 02:44:38 -0300401 CX18_ERR("Unknown model %d, defaulting to original HVR-1600 "
402 "(cardtype=1)\n", tv.model);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300403 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
404 break;
405 }
406
407 cx->v4l2_cap = cx->card->v4l2_capabilities;
408 cx->card_name = cx->card->name;
409 cx->card_i2c = cx->card->i2c;
410
411 CX18_INFO("Autodetected %s\n", cx->card_name);
412
413 if (tv.tuner_type == TUNER_ABSENT)
Andy Walls4442ee82009-05-09 00:34:31 -0300414 CX18_ERR("tveeprom cannot autodetect tuner!\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300415
416 if (cx->options.tuner == -1)
417 cx->options.tuner = tv.tuner_type;
418 if (cx->options.radio == -1)
419 cx->options.radio = (tv.has_radio != 0);
420
421 if (cx->std != 0)
422 /* user specified tuner standard */
423 return;
424
425 /* autodetect tuner standard */
426 if (tv.tuner_formats & V4L2_STD_PAL) {
427 CX18_DEBUG_INFO("PAL tuner detected\n");
428 cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
429 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
430 CX18_DEBUG_INFO("NTSC tuner detected\n");
431 cx->std |= V4L2_STD_NTSC_M;
432 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
433 CX18_DEBUG_INFO("SECAM tuner detected\n");
434 cx->std |= V4L2_STD_SECAM_L;
435 } else {
436 CX18_INFO("No tuner detected, default to NTSC-M\n");
437 cx->std |= V4L2_STD_NTSC_M;
438 }
439}
440
441static v4l2_std_id cx18_parse_std(struct cx18 *cx)
442{
443 switch (pal[0]) {
444 case '6':
445 return V4L2_STD_PAL_60;
446 case 'b':
447 case 'B':
448 case 'g':
449 case 'G':
450 return V4L2_STD_PAL_BG;
451 case 'h':
452 case 'H':
453 return V4L2_STD_PAL_H;
454 case 'n':
455 case 'N':
456 if (pal[1] == 'c' || pal[1] == 'C')
457 return V4L2_STD_PAL_Nc;
458 return V4L2_STD_PAL_N;
459 case 'i':
460 case 'I':
461 return V4L2_STD_PAL_I;
462 case 'd':
463 case 'D':
464 case 'k':
465 case 'K':
466 return V4L2_STD_PAL_DK;
467 case 'M':
468 case 'm':
469 return V4L2_STD_PAL_M;
470 case '-':
471 break;
472 default:
473 CX18_WARN("pal= argument not recognised\n");
474 return 0;
475 }
476
477 switch (secam[0]) {
478 case 'b':
479 case 'B':
480 case 'g':
481 case 'G':
482 case 'h':
483 case 'H':
484 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
485 case 'd':
486 case 'D':
487 case 'k':
488 case 'K':
489 return V4L2_STD_SECAM_DK;
490 case 'l':
491 case 'L':
492 if (secam[1] == 'C' || secam[1] == 'c')
493 return V4L2_STD_SECAM_LC;
494 return V4L2_STD_SECAM_L;
495 case '-':
496 break;
497 default:
498 CX18_WARN("secam= argument not recognised\n");
499 return 0;
500 }
501
502 switch (ntsc[0]) {
503 case 'm':
504 case 'M':
505 return V4L2_STD_NTSC_M;
506 case 'j':
507 case 'J':
508 return V4L2_STD_NTSC_M_JP;
509 case 'k':
510 case 'K':
511 return V4L2_STD_NTSC_M_KR;
512 case '-':
513 break;
514 default:
515 CX18_WARN("ntsc= argument not recognised\n");
516 return 0;
517 }
518
519 /* no match found */
520 return 0;
521}
522
523static void cx18_process_options(struct cx18 *cx)
524{
525 int i, j;
526
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300527 cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300528 cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
529 cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300530 cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
531 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
532 cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300533 cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */
534
535 cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;
536 cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;
537 cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;
538 cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;
539 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;
540 cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;
541 cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */
542
543 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;
544 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
545 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
546 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
Andy Walls466df462009-02-07 15:47:28 -0300547 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300548 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
549 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
550
Andy Walls466df462009-02-07 15:47:28 -0300551 /* Ensure stream_buffers & stream_buf_size are valid */
Andy Walls6ecd86d2008-12-07 23:30:17 -0300552 for (i = 0; i < CX18_MAX_STREAMS; i++) {
Andy Walls466df462009-02-07 15:47:28 -0300553 if (cx->stream_buffers[i] == 0 || /* User said 0 buffers */
554 cx->options.megabytes[i] <= 0 || /* User said 0 MB total */
555 cx->stream_buf_size[i] <= 0) { /* User said buf size 0 */
Andy Walls6ecd86d2008-12-07 23:30:17 -0300556 cx->options.megabytes[i] = 0;
557 cx->stream_buffers[i] = 0;
558 cx->stream_buf_size[i] = 0;
559 continue;
560 }
Andy Walls466df462009-02-07 15:47:28 -0300561 /*
Andy Walls22dce182009-11-09 23:55:30 -0300562 * YUV is a special case where the stream_buf_size needs to be
563 * an integral multiple of 33.75 kB (storage for 32 screens
Andy Wallsefc0b122009-12-30 22:54:53 -0300564 * lines to maintain alignment in case of lost buffers).
565 *
566 * IDX is a special case where the stream_buf_size should be
567 * an integral multiple of 1.5 kB (storage for 64 index entries
568 * to maintain alignment in case of lost buffers).
569 *
Andy Walls22dce182009-11-09 23:55:30 -0300570 */
571 if (i == CX18_ENC_STREAM_TYPE_YUV) {
572 cx->stream_buf_size[i] *= 1024;
573 cx->stream_buf_size[i] -=
574 (cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE);
575
576 if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE)
577 cx->stream_buf_size[i] =
578 CX18_UNIT_ENC_YUV_BUFSIZE;
Andy Wallsefc0b122009-12-30 22:54:53 -0300579 } else if (i == CX18_ENC_STREAM_TYPE_IDX) {
580 cx->stream_buf_size[i] *= 1024;
581 cx->stream_buf_size[i] -=
582 (cx->stream_buf_size[i] % CX18_UNIT_ENC_IDX_BUFSIZE);
583
584 if (cx->stream_buf_size[i] < CX18_UNIT_ENC_IDX_BUFSIZE)
585 cx->stream_buf_size[i] =
586 CX18_UNIT_ENC_IDX_BUFSIZE;
Andy Walls22dce182009-11-09 23:55:30 -0300587 }
588 /*
Andy Wallsefc0b122009-12-30 22:54:53 -0300589 * YUV and IDX are special cases where the stream_buf_size is
Andy Walls22dce182009-11-09 23:55:30 -0300590 * now in bytes.
Andy Walls466df462009-02-07 15:47:28 -0300591 * VBI is a special case where the stream_buf_size is fixed
592 * and already in bytes
593 */
Andy Walls22dce182009-11-09 23:55:30 -0300594 if (i == CX18_ENC_STREAM_TYPE_VBI ||
Andy Wallsefc0b122009-12-30 22:54:53 -0300595 i == CX18_ENC_STREAM_TYPE_YUV ||
596 i == CX18_ENC_STREAM_TYPE_IDX) {
Andy Walls466df462009-02-07 15:47:28 -0300597 if (cx->stream_buffers[i] < 0) {
598 cx->stream_buffers[i] =
599 cx->options.megabytes[i] * 1024 * 1024
600 / cx->stream_buf_size[i];
601 } else {
602 /* N.B. This might round down to 0 */
603 cx->options.megabytes[i] =
604 cx->stream_buffers[i]
605 * cx->stream_buf_size[i]/(1024 * 1024);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300606 }
Andy Walls6ecd86d2008-12-07 23:30:17 -0300607 } else {
Andy Walls22dce182009-11-09 23:55:30 -0300608 /* All other streams have stream_buf_size in kB here */
609 if (cx->stream_buffers[i] < 0) {
610 cx->stream_buffers[i] =
611 cx->options.megabytes[i] * 1024
612 / cx->stream_buf_size[i];
613 } else {
614 /* N.B. This might round down to 0 */
615 cx->options.megabytes[i] =
616 cx->stream_buffers[i]
617 * cx->stream_buf_size[i] / 1024;
618 }
619 /* convert from kB to bytes */
620 cx->stream_buf_size[i] *= 1024;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300621 }
Andy Walls22dce182009-11-09 23:55:30 -0300622 CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, "
623 "%d bytes\n", i, cx->options.megabytes[i],
624 cx->stream_buffers[i], cx->stream_buf_size[i]);
Andy Walls6ecd86d2008-12-07 23:30:17 -0300625 }
626
Andy Walls5811cf92009-02-14 17:08:37 -0300627 cx->options.cardtype = cardtype[cx->instance];
628 cx->options.tuner = tuner[cx->instance];
629 cx->options.radio = radio[cx->instance];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300630
631 cx->std = cx18_parse_std(cx);
632 if (cx->options.cardtype == -1) {
633 CX18_INFO("Ignore card\n");
634 return;
635 }
636 cx->card = cx18_get_card(cx->options.cardtype - 1);
637 if (cx->card)
638 CX18_INFO("User specified %s card\n", cx->card->name);
639 else if (cx->options.cardtype != 0)
640 CX18_ERR("Unknown user specified type, trying to autodetect card\n");
641 if (cx->card == NULL) {
Andy Walls3d059132009-01-10 21:54:39 -0300642 if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300643 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
644 CX18_INFO("Autodetected Hauppauge card\n");
645 }
646 }
647 if (cx->card == NULL) {
648 for (i = 0; (cx->card = cx18_get_card(i)); i++) {
649 if (cx->card->pci_list == NULL)
650 continue;
651 for (j = 0; cx->card->pci_list[j].device; j++) {
Andy Walls3d059132009-01-10 21:54:39 -0300652 if (cx->pci_dev->device !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300653 cx->card->pci_list[j].device)
654 continue;
Andy Walls3d059132009-01-10 21:54:39 -0300655 if (cx->pci_dev->subsystem_vendor !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300656 cx->card->pci_list[j].subsystem_vendor)
657 continue;
Andy Walls3d059132009-01-10 21:54:39 -0300658 if (cx->pci_dev->subsystem_device !=
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300659 cx->card->pci_list[j].subsystem_device)
660 continue;
661 CX18_INFO("Autodetected %s card\n", cx->card->name);
662 goto done;
663 }
664 }
665 }
666done:
667
668 if (cx->card == NULL) {
669 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
Bjorn Helgaas29e66a62008-09-04 17:24:51 -0300670 CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
Andy Walls3d059132009-01-10 21:54:39 -0300671 cx->pci_dev->vendor, cx->pci_dev->device);
Bjorn Helgaas29e66a62008-09-04 17:24:51 -0300672 CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n",
Andy Walls3d059132009-01-10 21:54:39 -0300673 cx->pci_dev->subsystem_vendor,
674 cx->pci_dev->subsystem_device);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300675 CX18_ERR("Defaulting to %s card\n", cx->card->name);
676 CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
677 CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
678 CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
679 }
680 cx->v4l2_cap = cx->card->v4l2_capabilities;
681 cx->card_name = cx->card->name;
682 cx->card_i2c = cx->card->i2c;
683}
684
Andy Walls87116152009-04-13 22:42:43 -0300685static int __devinit cx18_create_in_workq(struct cx18 *cx)
686{
687 snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in",
688 cx->v4l2_dev.name);
Tejun Heoa3bc5e32011-01-03 10:49:34 -0300689 cx->in_work_queue = alloc_ordered_workqueue(cx->in_workq_name, 0);
Andy Walls87116152009-04-13 22:42:43 -0300690 if (cx->in_work_queue == NULL) {
691 CX18_ERR("Unable to create incoming mailbox handler thread\n");
692 return -ENOMEM;
693 }
694 return 0;
695}
696
Andy Walls87116152009-04-13 22:42:43 -0300697static void __devinit cx18_init_in_work_orders(struct cx18 *cx)
698{
699 int i;
700 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
701 cx->in_work_order[i].cx = cx;
702 cx->in_work_order[i].str = cx->epu_debug_str;
703 INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler);
704 }
705}
706
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300707/* Precondition: the cx18 structure has been memset to 0. Only
Andy Walls5811cf92009-02-14 17:08:37 -0300708 the dev and instance fields have been filled in.
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300709 No assumptions on the card type may be made here (see cx18_init_struct2
710 for that).
711 */
712static int __devinit cx18_init_struct1(struct cx18 *cx)
713{
Andy Walls87116152009-04-13 22:42:43 -0300714 int ret;
Andy Wallsee2d64f2008-11-16 01:38:19 -0300715
Andy Walls3d059132009-01-10 21:54:39 -0300716 cx->base_addr = pci_resource_start(cx->pci_dev, 0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300717
718 mutex_init(&cx->serialize_lock);
Andy Walls8abdd002008-07-13 19:05:25 -0300719 mutex_init(&cx->gpio_lock);
Andy Walls72c2d6d2008-11-06 01:15:41 -0300720 mutex_init(&cx->epu2apu_mb_lock);
721 mutex_init(&cx->epu2cpu_mb_lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300722
Tejun Heoa3bc5e32011-01-03 10:49:34 -0300723 ret = cx18_create_in_workq(cx);
Andy Walls87116152009-04-13 22:42:43 -0300724 if (ret)
725 return ret;
726
Andy Walls87116152009-04-13 22:42:43 -0300727 cx18_init_in_work_orders(cx);
Andy Walls1d6782b2008-11-05 00:49:14 -0300728
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300729 /* start counting open_id at 1 */
730 cx->open_id = 1;
731
732 /* Initial settings */
Hans Verkuila75b9be2010-12-31 10:22:52 -0300733 cx->cxhdl.port = CX2341X_PORT_MEMORY;
734 cx->cxhdl.capabilities = CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI;
735 cx->cxhdl.ops = &cx18_cxhdl_ops;
736 cx->cxhdl.func = cx18_api_func;
Hans Verkuil7ee9e642011-01-25 18:25:39 -0300737 cx->cxhdl.priv = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
Hans Verkuila75b9be2010-12-31 10:22:52 -0300738 ret = cx2341x_handler_init(&cx->cxhdl, 50);
739 if (ret)
740 return ret;
741 cx->v4l2_dev.ctrl_handler = &cx->cxhdl.hdl;
742
743 cx->temporal_strength = cx->cxhdl.video_temporal_filter->cur.val;
744 cx->spatial_strength = cx->cxhdl.video_spatial_filter->cur.val;
745 cx->filter_mode = cx->cxhdl.video_spatial_filter_mode->cur.val |
746 (cx->cxhdl.video_temporal_filter_mode->cur.val << 1) |
747 (cx->cxhdl.video_median_filter_type->cur.val << 2);
748
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300749 init_waitqueue_head(&cx->cap_w);
750 init_waitqueue_head(&cx->mb_apu_waitq);
751 init_waitqueue_head(&cx->mb_cpu_waitq);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300752 init_waitqueue_head(&cx->dma_waitq);
753
754 /* VBI */
Andy Wallsdd073432008-12-12 16:24:04 -0300755 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300756 cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
Andy Wallsaf009cf2008-12-12 20:00:29 -0300757
Andy Walls52fcb3e2009-11-08 23:45:24 -0300758 /* IVTV style VBI insertion into MPEG streams */
759 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list);
760 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list);
761 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list);
762 list_add(&cx->vbi.sliced_mpeg_buf.list,
763 &cx->vbi.sliced_mpeg_mdl.buf_list);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300764 return 0;
765}
766
767/* Second initialization part. Here the card type has been
768 autodetected. */
769static void __devinit cx18_init_struct2(struct cx18 *cx)
770{
771 int i;
772
773 for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
774 if (cx->card->video_inputs[i].video_type == 0)
775 break;
776 cx->nof_inputs = i;
777 for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
778 if (cx->card->audio_inputs[i].audio_type == 0)
779 break;
780 cx->nof_audio_inputs = i;
781
782 /* Find tuner input */
783 for (i = 0; i < cx->nof_inputs; i++) {
784 if (cx->card->video_inputs[i].video_type ==
785 CX18_CARD_INPUT_VID_TUNER)
786 break;
787 }
788 if (i == cx->nof_inputs)
789 i = 0;
790 cx->active_input = i;
791 cx->audio_input = cx->card->video_inputs[i].audio_index;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300792}
793
Andy Walls3d059132009-01-10 21:54:39 -0300794static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300795 const struct pci_device_id *pci_id)
796{
797 u16 cmd;
798 unsigned char pci_latency;
799
800 CX18_DEBUG_INFO("Enabling pci device\n");
801
Andy Walls3d059132009-01-10 21:54:39 -0300802 if (pci_enable_device(pci_dev)) {
Andy Walls5811cf92009-02-14 17:08:37 -0300803 CX18_ERR("Can't enable device %d!\n", cx->instance);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300804 return -EIO;
805 }
Andy Walls3d059132009-01-10 21:54:39 -0300806 if (pci_set_dma_mask(pci_dev, 0xffffffff)) {
Andy Walls5811cf92009-02-14 17:08:37 -0300807 CX18_ERR("No suitable DMA available, card %d\n", cx->instance);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300808 return -EIO;
809 }
810 if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
Andy Walls5811cf92009-02-14 17:08:37 -0300811 CX18_ERR("Cannot request encoder memory region, card %d\n",
812 cx->instance);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300813 return -EIO;
814 }
815
Andy Walls45190642008-08-29 16:10:21 -0300816 /* Enable bus mastering and memory mapped IO for the CX23418 */
Andy Walls3d059132009-01-10 21:54:39 -0300817 pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);
Andy Walls45190642008-08-29 16:10:21 -0300818 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
Andy Walls3d059132009-01-10 21:54:39 -0300819 pci_write_config_word(pci_dev, PCI_COMMAND, cmd);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300820
Andy Walls3d059132009-01-10 21:54:39 -0300821 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &cx->card_rev);
822 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300823
824 if (pci_latency < 64 && cx18_pci_latency) {
825 CX18_INFO("Unreasonably low latency timer, "
826 "setting to 64 (was %d)\n", pci_latency);
Andy Walls3d059132009-01-10 21:54:39 -0300827 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);
828 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300829 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300830
831 CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
832 "irq: %d, latency: %d, memory: 0x%lx\n",
Andy Walls3d059132009-01-10 21:54:39 -0300833 cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,
834 PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),
835 cx->pci_dev->irq, pci_latency, (unsigned long)cx->base_addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300836
837 return 0;
838}
839
Andy Wallsff2a2002009-02-20 23:52:13 -0300840static void cx18_init_subdevs(struct cx18 *cx)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300841{
842 u32 hw = cx->card->hw_all;
Andy Wallsff2a2002009-02-20 23:52:13 -0300843 u32 device;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300844 int i;
845
Andy Wallsff2a2002009-02-20 23:52:13 -0300846 for (i = 0, device = 1; i < 32; i++, device <<= 1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300847
848 if (!(device & hw))
849 continue;
Andy Wallsff2a2002009-02-20 23:52:13 -0300850
851 switch (device) {
Andy Wallsff2a2002009-02-20 23:52:13 -0300852 case CX18_HW_DVB:
853 case CX18_HW_TVEEPROM:
854 /* These subordinate devices do not use probing */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300855 cx->hw_flags |= device;
Andy Wallsff2a2002009-02-20 23:52:13 -0300856 break;
857 case CX18_HW_418_AV:
858 /* The A/V decoder gets probed earlier to set PLLs */
859 /* Just note that the card uses it (i.e. has analog) */
860 cx->hw_flags |= device;
861 break;
Andy Wallseefe1012009-02-21 18:42:49 -0300862 case CX18_HW_GPIO_RESET_CTRL:
863 /*
864 * The Reset Controller gets probed and added to
865 * hw_flags earlier for i2c adapter/bus initialization
866 */
867 break;
868 case CX18_HW_GPIO_MUX:
869 if (cx18_gpio_register(cx, device) == 0)
870 cx->hw_flags |= device;
871 break;
Andy Wallsff2a2002009-02-20 23:52:13 -0300872 default:
873 if (cx18_i2c_register(cx, i) == 0)
874 cx->hw_flags |= device;
875 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300876 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300877 }
878
Andy Wallsff2a2002009-02-20 23:52:13 -0300879 if (cx->hw_flags & CX18_HW_418_AV)
880 cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV);
881
882 if (cx->card->hw_muxer != 0)
883 cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300884}
885
Andy Walls3d059132009-01-10 21:54:39 -0300886static int __devinit cx18_probe(struct pci_dev *pci_dev,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300887 const struct pci_device_id *pci_id)
888{
889 int retval = 0;
Andy Wallsff086572008-10-18 08:51:28 -0300890 int i;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300891 u32 devtype;
892 struct cx18 *cx;
893
Andy Walls5811cf92009-02-14 17:08:37 -0300894 /* FIXME - module parameter arrays constrain max instances */
895 i = atomic_inc_return(&cx18_instance) - 1;
896 if (i >= CX18_MAX_CARDS) {
897 printk(KERN_ERR "cx18: cannot manage card %d, driver has a "
898 "limit of 0 - %d\n", i, CX18_MAX_CARDS - 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300899 return -ENOMEM;
900 }
901
902 cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
Andy Walls5811cf92009-02-14 17:08:37 -0300903 if (cx == NULL) {
904 printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n",
905 i);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300906 return -ENOMEM;
907 }
Andy Walls888cdb02009-01-11 15:08:53 -0300908 cx->pci_dev = pci_dev;
Andy Walls5811cf92009-02-14 17:08:37 -0300909 cx->instance = i;
910
Andy Walls888cdb02009-01-11 15:08:53 -0300911 retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);
912 if (retval) {
Andy Walls5811cf92009-02-14 17:08:37 -0300913 printk(KERN_ERR "cx18: v4l2_device_register of card %d failed"
914 "\n", cx->instance);
915 kfree(cx);
916 return retval;
Andy Walls888cdb02009-01-11 15:08:53 -0300917 }
Andy Walls5811cf92009-02-14 17:08:37 -0300918 snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d",
919 cx->instance);
920 CX18_INFO("Initializing card %d\n", cx->instance);
Andy Walls888cdb02009-01-11 15:08:53 -0300921
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300922 cx18_process_options(cx);
923 if (cx->options.cardtype == -1) {
924 retval = -ENODEV;
Andy Walls5811cf92009-02-14 17:08:37 -0300925 goto err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300926 }
Andy Walls87116152009-04-13 22:42:43 -0300927
928 retval = cx18_init_struct1(cx);
929 if (retval)
Andy Walls5811cf92009-02-14 17:08:37 -0300930 goto err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300931
932 CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);
933
934 /* PCI Device Setup */
Andy Walls3d059132009-01-10 21:54:39 -0300935 retval = cx18_setup_pci(cx, pci_dev, pci_id);
Andy Walls572bfea2008-11-25 21:43:05 -0300936 if (retval != 0)
Andy Walls87116152009-04-13 22:42:43 -0300937 goto free_workqueues;
Andy Walls572bfea2008-11-25 21:43:05 -0300938
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300939 /* map io memory */
940 CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
941 cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
942 cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
943 CX18_MEM_SIZE);
944 if (!cx->enc_mem) {
Andy Wallsfa984472010-12-11 12:31:45 -0300945 CX18_ERR("ioremap failed. Can't get a window into CX23418 "
946 "memory and register space\n");
947 CX18_ERR("Each capture card with a CX23418 needs 64 MB of "
948 "vmalloc address space for the window\n");
949 CX18_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
950 CX18_ERR("Use the vmalloc= kernel command line option to set "
951 "VmallocTotal to a larger value\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300952 retval = -ENOMEM;
953 goto free_mem;
954 }
955 cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
Andy Wallsb1526422008-08-30 16:03:44 -0300956 devtype = cx18_read_reg(cx, 0xC72028);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300957 switch (devtype & 0xff000000) {
958 case 0xff000000:
959 CX18_INFO("cx23418 revision %08x (A)\n", devtype);
960 break;
961 case 0x01000000:
962 CX18_INFO("cx23418 revision %08x (B)\n", devtype);
963 break;
964 default:
965 CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
966 break;
967 }
968
969 cx18_init_power(cx, 1);
970 cx18_init_memory(cx);
971
Al Viro990c81c2008-05-21 00:32:01 -0300972 cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300973 cx18_init_scb(cx);
974
975 cx18_gpio_init(cx);
976
Andy Wallsff2a2002009-02-20 23:52:13 -0300977 /* Initialize integrated A/V decoder early to set PLLs, just in case */
978 retval = cx18_av_probe(cx);
Andy Wallsfa3e7032009-02-16 02:23:25 -0300979 if (retval) {
980 CX18_ERR("Could not register A/V decoder subdevice\n");
981 goto free_map;
982 }
Andy Wallsfa3e7032009-02-16 02:23:25 -0300983
Andy Wallseefe1012009-02-21 18:42:49 -0300984 /* Initialize GPIO Reset Controller to do chip resets during i2c init */
985 if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) {
986 if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0)
987 CX18_WARN("Could not register GPIO reset controller"
988 "subdevice; proceeding anyway.\n");
989 else
990 cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL;
991 }
992
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300993 /* active i2c */
994 CX18_DEBUG_INFO("activating i2c...\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300995 retval = init_cx18_i2c(cx);
996 if (retval) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300997 CX18_ERR("Could not initialize i2c\n");
998 goto free_map;
999 }
1000
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001001 if (cx->card->hw_all & CX18_HW_TVEEPROM) {
1002 /* Based on the model number the cardtype may be changed.
1003 The PCI IDs are not always reliable. */
1004 cx18_process_eeprom(cx);
1005 }
1006 if (cx->card->comment)
1007 CX18_INFO("%s", cx->card->comment);
1008 if (cx->card->v4l2_capabilities == 0) {
1009 retval = -ENODEV;
1010 goto free_i2c;
1011 }
1012 cx18_init_memory(cx);
Andy Wallsfd6b9c92008-12-14 21:26:25 -03001013 cx18_init_scb(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001014
1015 /* Register IRQ */
Andy Walls3d059132009-01-10 21:54:39 -03001016 retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
Andy Walls5811cf92009-02-14 17:08:37 -03001017 IRQF_SHARED | IRQF_DISABLED,
1018 cx->v4l2_dev.name, (void *)cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001019 if (retval) {
1020 CX18_ERR("Failed to register irq %d\n", retval);
1021 goto free_i2c;
1022 }
1023
1024 if (cx->std == 0)
1025 cx->std = V4L2_STD_NTSC_M;
1026
1027 if (cx->options.tuner == -1) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001028 for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
1029 if ((cx->std & cx->card->tuners[i].std) == 0)
1030 continue;
1031 cx->options.tuner = cx->card->tuners[i].tuner;
1032 break;
1033 }
1034 }
1035 /* if no tuner was found, then pick the first tuner in the card list */
1036 if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
1037 cx->std = cx->card->tuners[0].std;
Hans Verkuilc3cb4d92008-06-27 23:27:25 -03001038 if (cx->std & V4L2_STD_PAL)
1039 cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
1040 else if (cx->std & V4L2_STD_NTSC)
1041 cx->std = V4L2_STD_NTSC_M;
1042 else if (cx->std & V4L2_STD_SECAM)
1043 cx->std = V4L2_STD_SECAM_L;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001044 cx->options.tuner = cx->card->tuners[0].tuner;
1045 }
1046 if (cx->options.radio == -1)
1047 cx->options.radio = (cx->card->radio_input.audio_type != 0);
1048
1049 /* The card is now fully identified, continue with card-specific
1050 initialization. */
1051 cx18_init_struct2(cx);
1052
Andy Wallsff2a2002009-02-20 23:52:13 -03001053 cx18_init_subdevs(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001054
Andy Walls8d037ed2009-02-21 22:35:11 -03001055 if (cx->std & V4L2_STD_525_60)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001056 cx->is_60hz = 1;
Andy Walls8d037ed2009-02-21 22:35:11 -03001057 else
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001058 cx->is_50hz = 1;
Andy Walls8d037ed2009-02-21 22:35:11 -03001059
Hans Verkuila75b9be2010-12-31 10:22:52 -03001060 cx2341x_handler_set_50hz(&cx->cxhdl, !cx->is_60hz);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001061
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001062 if (cx->options.radio > 0)
1063 cx->v4l2_cap |= V4L2_CAP_RADIO;
1064
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001065 if (cx->options.tuner > -1) {
1066 struct tuner_setup setup;
1067
1068 setup.addr = ADDR_UNSET;
1069 setup.type = cx->options.tuner;
1070 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
1071 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
1072 cx18_reset_tuner_gpio : NULL;
Andy Wallsff2a2002009-02-20 23:52:13 -03001073 cx18_call_all(cx, tuner, s_type_addr, &setup);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001074 if (setup.type == TUNER_XC2028) {
1075 static struct xc2028_ctrl ctrl = {
1076 .fname = XC2028_DEFAULT_FIRMWARE,
1077 .max_len = 64,
1078 };
1079 struct v4l2_priv_tun_config cfg = {
1080 .tuner = cx->options.tuner,
1081 .priv = &ctrl,
1082 };
Andy Wallsff2a2002009-02-20 23:52:13 -03001083 cx18_call_all(cx, tuner, s_config, &cfg);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001084 }
1085 }
1086
1087 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
1088 are not. */
1089 cx->tuner_std = cx->std;
1090
Hans Verkuil5e7fdc52008-05-30 10:51:53 -03001091 retval = cx18_streams_setup(cx);
1092 if (retval) {
1093 CX18_ERR("Error %d setting up streams\n", retval);
1094 goto free_irq;
1095 }
1096 retval = cx18_streams_register(cx);
1097 if (retval) {
1098 CX18_ERR("Error %d registering devices\n", retval);
1099 goto free_streams;
1100 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001101
Andy Walls5811cf92009-02-14 17:08:37 -03001102 CX18_INFO("Initialized card: %s\n", cx->card_name);
Devin Heitmuellerd68b6872009-11-20 01:15:54 -03001103
1104 /* Load cx18 submodules (cx18-alsa) */
1105 request_modules(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001106 return 0;
1107
1108free_streams:
Hans Verkuil3f983872008-05-01 10:31:12 -03001109 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001110free_irq:
Andy Walls3d059132009-01-10 21:54:39 -03001111 free_irq(cx->pci_dev->irq, (void *)cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001112free_i2c:
1113 exit_cx18_i2c(cx);
1114free_map:
1115 cx18_iounmap(cx);
1116free_mem:
1117 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
Andy Walls87116152009-04-13 22:42:43 -03001118free_workqueues:
Andy Wallsdeed75e2009-04-13 22:22:40 -03001119 destroy_workqueue(cx->in_work_queue);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001120err:
1121 if (retval == 0)
1122 retval = -ENODEV;
1123 CX18_ERR("Error %d on initialization\n", retval);
1124
Andy Walls5811cf92009-02-14 17:08:37 -03001125 v4l2_device_unregister(&cx->v4l2_dev);
1126 kfree(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001127 return retval;
1128}
1129
1130int cx18_init_on_first_open(struct cx18 *cx)
1131{
1132 int video_input;
1133 int fw_retry_count = 3;
1134 struct v4l2_frequency vf;
Andy Walls3b6fe582008-06-21 08:36:31 -03001135 struct cx18_open_id fh;
1136
1137 fh.cx = cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001138
1139 if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
1140 return -ENXIO;
1141
1142 if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
1143 return 0;
1144
1145 while (--fw_retry_count > 0) {
1146 /* load firmware */
1147 if (cx18_firmware_init(cx) == 0)
1148 break;
1149 if (fw_retry_count > 1)
1150 CX18_WARN("Retry loading firmware\n");
1151 }
1152
1153 if (fw_retry_count == 0) {
1154 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1155 return -ENXIO;
1156 }
1157 set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
1158
Andy Walls350145a2009-01-04 21:51:17 -03001159 /*
1160 * Init the firmware twice to work around a silicon bug
1161 * with the digital TS.
1162 *
1163 * The second firmware load requires us to normalize the APU state,
1164 * or the audio for the first analog capture will be badly incorrect.
1165 *
1166 * I can't seem to call APU_RESETAI and have it succeed without the
1167 * APU capturing audio, so we start and stop it here to do the reset
1168 */
1169
1170 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1171 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1172 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1173 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001174
1175 fw_retry_count = 3;
1176 while (--fw_retry_count > 0) {
1177 /* load firmware */
1178 if (cx18_firmware_init(cx) == 0)
1179 break;
1180 if (fw_retry_count > 1)
1181 CX18_WARN("Retry loading firmware\n");
1182 }
1183
1184 if (fw_retry_count == 0) {
1185 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1186 return -ENXIO;
1187 }
1188
Andy Wallsd5c02f62009-01-30 22:48:40 -03001189 /*
1190 * The second firmware load requires us to normalize the APU state,
1191 * or the audio for the first analog capture will be badly incorrect.
1192 *
1193 * I can't seem to call APU_RESETAI and have it succeed without the
1194 * APU capturing audio, so we start and stop it here to do the reset
1195 */
1196
1197 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1198 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1199 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1200 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1201
Andy Wallsfa3e7032009-02-16 02:23:25 -03001202 /* Init the A/V decoder, if it hasn't been already */
Hans Verkuilcc26b072009-03-29 19:20:26 -03001203 v4l2_subdev_call(cx->sd_av, core, load_fw);
Andy Wallsfa3e7032009-02-16 02:23:25 -03001204
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001205 vf.tuner = 0;
1206 vf.type = V4L2_TUNER_ANALOG_TV;
1207 vf.frequency = 6400; /* the tuner 'baseline' frequency */
1208
1209 /* Set initial frequency. For PAL/SECAM broadcasts no
1210 'default' channel exists AFAIK. */
1211 if (cx->std == V4L2_STD_NTSC_M_JP)
1212 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
1213 else if (cx->std & V4L2_STD_NTSC_M)
1214 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
1215
1216 video_input = cx->active_input;
1217 cx->active_input++; /* Force update of input */
Andy Walls3b6fe582008-06-21 08:36:31 -03001218 cx18_s_input(NULL, &fh, video_input);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001219
1220 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1221 in one place. */
1222 cx->std++; /* Force full standard initialization */
Andy Walls3b6fe582008-06-21 08:36:31 -03001223 cx18_s_std(NULL, &fh, &cx->tuner_std);
1224 cx18_s_frequency(NULL, &fh, &vf);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001225 return 0;
1226}
1227
Andy Wallsdeed75e2009-04-13 22:22:40 -03001228static void cx18_cancel_in_work_orders(struct cx18 *cx)
Andy Walls18b5dc22008-11-16 17:15:01 -03001229{
1230 int i;
Andy Wallsdeed75e2009-04-13 22:22:40 -03001231 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++)
1232 cancel_work_sync(&cx->in_work_order[i].work);
Andy Walls18b5dc22008-11-16 17:15:01 -03001233}
1234
Andy Walls21a278b2009-04-15 20:45:10 -03001235static void cx18_cancel_out_work_orders(struct cx18 *cx)
1236{
1237 int i;
1238 for (i = 0; i < CX18_MAX_STREAMS; i++)
1239 if (&cx->streams[i].video_dev != NULL)
1240 cancel_work_sync(&cx->streams[i].out_work_order);
1241}
1242
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001243static void cx18_remove(struct pci_dev *pci_dev)
1244{
Andy Walls888cdb02009-01-11 15:08:53 -03001245 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
Andy Walls5811cf92009-02-14 17:08:37 -03001246 struct cx18 *cx = to_cx18(v4l2_dev);
Andy Walls6da6bf52009-02-21 19:53:54 -03001247 int i;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001248
Andy Walls5811cf92009-02-14 17:08:37 -03001249 CX18_DEBUG_INFO("Removing Card\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001250
Tejun Heo707bcf32010-12-24 16:14:20 +01001251 flush_request_modules(cx);
1252
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001253 /* Stop all captures */
1254 CX18_DEBUG_INFO("Stopping all streams\n");
Hans Verkuil31554ae2008-05-25 11:21:27 -03001255 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001256 cx18_stop_all_captures(cx);
1257
Andy Walls87116152009-04-13 22:42:43 -03001258 /* Stop interrupts that cause incoming work to be queued */
Andy Wallsb1526422008-08-30 16:03:44 -03001259 cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
Andy Walls87116152009-04-13 22:42:43 -03001260
1261 /* Incoming work can cause outgoing work, so clean up incoming first */
1262 cx18_cancel_in_work_orders(cx);
Andy Walls21a278b2009-04-15 20:45:10 -03001263 cx18_cancel_out_work_orders(cx);
Andy Walls87116152009-04-13 22:42:43 -03001264
1265 /* Stop ack interrupts that may have been needed for work to finish */
Andy Wallsb1526422008-08-30 16:03:44 -03001266 cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001267
1268 cx18_halt_firmware(cx);
1269
Andy Wallsdeed75e2009-04-13 22:22:40 -03001270 destroy_workqueue(cx->in_work_queue);
Andy Walls572bfea2008-11-25 21:43:05 -03001271
Hans Verkuil3f983872008-05-01 10:31:12 -03001272 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001273
1274 exit_cx18_i2c(cx);
1275
Andy Walls3d059132009-01-10 21:54:39 -03001276 free_irq(cx->pci_dev->irq, (void *)cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001277
Hans Verkuilcba627a2008-05-12 14:48:26 -03001278 cx18_iounmap(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001279
1280 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1281
Andy Walls3d059132009-01-10 21:54:39 -03001282 pci_disable_device(cx->pci_dev);
Andy Walls6da6bf52009-02-21 19:53:54 -03001283
1284 if (cx->vbi.sliced_mpeg_data[0] != NULL)
1285 for (i = 0; i < CX18_VBI_FRAMES; i++)
1286 kfree(cx->vbi.sliced_mpeg_data[i]);
Andy Walls5811cf92009-02-14 17:08:37 -03001287
Hans Verkuila75b9be2010-12-31 10:22:52 -03001288 v4l2_ctrl_handler_free(&cx->av_state.hdl);
1289
Andy Walls5811cf92009-02-14 17:08:37 -03001290 CX18_INFO("Removed %s\n", cx->card_name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001291
Andy Walls888cdb02009-01-11 15:08:53 -03001292 v4l2_device_unregister(v4l2_dev);
Andy Walls5811cf92009-02-14 17:08:37 -03001293 kfree(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001294}
1295
Devin Heitmuellerd68b6872009-11-20 01:15:54 -03001296
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001297/* define a pci_driver for card detection */
1298static struct pci_driver cx18_pci_driver = {
1299 .name = "cx18",
1300 .id_table = cx18_pci_tbl,
1301 .probe = cx18_probe,
1302 .remove = cx18_remove,
1303};
1304
Peter Huewe9710e7a2009-11-04 15:28:33 -03001305static int __init module_start(void)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001306{
Devin Heitmuellerf8bd9d22009-12-20 23:29:02 -03001307 printk(KERN_INFO "cx18: Start initialization, version %s\n",
1308 CX18_VERSION);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001309
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001310 /* Validate parameters */
1311 if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
Hans Verkuildd896012008-10-04 08:36:54 -03001312 printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n",
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001313 CX18_MAX_CARDS - 1);
1314 return -1;
1315 }
1316
1317 if (cx18_debug < 0 || cx18_debug > 511) {
1318 cx18_debug = 0;
1319 printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");
1320 }
1321
1322 if (pci_register_driver(&cx18_pci_driver)) {
1323 printk(KERN_ERR "cx18: Error detecting PCI card\n");
1324 return -ENODEV;
1325 }
1326 printk(KERN_INFO "cx18: End initialization\n");
1327 return 0;
1328}
1329
Peter Huewe9710e7a2009-11-04 15:28:33 -03001330static void __exit module_cleanup(void)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001331{
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001332 pci_unregister_driver(&cx18_pci_driver);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001333}
1334
1335module_init(module_start);
1336module_exit(module_cleanup);