xref: /openbmc/linux/drivers/media/pci/cx18/cx18-driver.c (revision 318de7911feddb452a92c43fe67cc01dc3878daf)
1b285192aSMauro Carvalho Chehab /*
2b285192aSMauro Carvalho Chehab  *  cx18 driver initialization and card probing
3b285192aSMauro Carvalho Chehab  *
4b285192aSMauro Carvalho Chehab  *  Derived from ivtv-driver.c
5b285192aSMauro Carvalho Chehab  *
6b285192aSMauro Carvalho Chehab  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
7b285192aSMauro Carvalho Chehab  *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
8b285192aSMauro Carvalho Chehab  *
9b285192aSMauro Carvalho Chehab  *  This program is free software; you can redistribute it and/or modify
10b285192aSMauro Carvalho Chehab  *  it under the terms of the GNU General Public License as published by
11b285192aSMauro Carvalho Chehab  *  the Free Software Foundation; either version 2 of the License, or
12b285192aSMauro Carvalho Chehab  *  (at your option) any later version.
13b285192aSMauro Carvalho Chehab  *
14b285192aSMauro Carvalho Chehab  *  This program is distributed in the hope that it will be useful,
15b285192aSMauro Carvalho Chehab  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16b285192aSMauro Carvalho Chehab  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17b285192aSMauro Carvalho Chehab  *  GNU General Public License for more details.
18b285192aSMauro Carvalho Chehab  *
19b285192aSMauro Carvalho Chehab  *  You should have received a copy of the GNU General Public License
20b285192aSMauro Carvalho Chehab  *  along with this program; if not, write to the Free Software
21b285192aSMauro Carvalho Chehab  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22b285192aSMauro Carvalho Chehab  *  02111-1307  USA
23b285192aSMauro Carvalho Chehab  */
24b285192aSMauro Carvalho Chehab 
25b285192aSMauro Carvalho Chehab #include "cx18-driver.h"
26b285192aSMauro Carvalho Chehab #include "cx18-io.h"
27b285192aSMauro Carvalho Chehab #include "cx18-version.h"
28b285192aSMauro Carvalho Chehab #include "cx18-cards.h"
29b285192aSMauro Carvalho Chehab #include "cx18-i2c.h"
30b285192aSMauro Carvalho Chehab #include "cx18-irq.h"
31b285192aSMauro Carvalho Chehab #include "cx18-gpio.h"
32b285192aSMauro Carvalho Chehab #include "cx18-firmware.h"
33b285192aSMauro Carvalho Chehab #include "cx18-queue.h"
34b285192aSMauro Carvalho Chehab #include "cx18-streams.h"
35b285192aSMauro Carvalho Chehab #include "cx18-av-core.h"
36b285192aSMauro Carvalho Chehab #include "cx18-scb.h"
37b285192aSMauro Carvalho Chehab #include "cx18-mailbox.h"
38b285192aSMauro Carvalho Chehab #include "cx18-ioctl.h"
39b285192aSMauro Carvalho Chehab #include "cx18-controls.h"
40b285192aSMauro Carvalho Chehab #include "tuner-xc2028.h"
41b285192aSMauro Carvalho Chehab #include <linux/dma-mapping.h>
42b285192aSMauro Carvalho Chehab #include <media/tveeprom.h>
43b285192aSMauro Carvalho Chehab 
44b285192aSMauro Carvalho Chehab /* If you have already X v4l cards, then set this to X. This way
45b285192aSMauro Carvalho Chehab    the device numbers stay matched. Example: you have a WinTV card
46b285192aSMauro Carvalho Chehab    without radio and a Compro H900 with. Normally this would give a
47b285192aSMauro Carvalho Chehab    video1 device together with a radio0 device for the Compro. By
48b285192aSMauro Carvalho Chehab    setting this to 1 you ensure that radio0 is now also radio1. */
49b285192aSMauro Carvalho Chehab int cx18_first_minor;
50b285192aSMauro Carvalho Chehab 
51b285192aSMauro Carvalho Chehab /* Callback for registering extensions */
52b285192aSMauro Carvalho Chehab int (*cx18_ext_init)(struct cx18 *);
53b285192aSMauro Carvalho Chehab EXPORT_SYMBOL(cx18_ext_init);
54b285192aSMauro Carvalho Chehab 
55b285192aSMauro Carvalho Chehab /* add your revision and whatnot here */
564c62e976SGreg Kroah-Hartman static struct pci_device_id cx18_pci_tbl[] = {
57b285192aSMauro Carvalho Chehab 	{PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,
58b285192aSMauro Carvalho Chehab 	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
59b285192aSMauro Carvalho Chehab 	{0,}
60b285192aSMauro Carvalho Chehab };
61b285192aSMauro Carvalho Chehab 
62b285192aSMauro Carvalho Chehab MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);
63b285192aSMauro Carvalho Chehab 
64b285192aSMauro Carvalho Chehab static atomic_t cx18_instance = ATOMIC_INIT(0);
65b285192aSMauro Carvalho Chehab 
66b285192aSMauro Carvalho Chehab /* Parameter declarations */
67b285192aSMauro Carvalho Chehab static int cardtype[CX18_MAX_CARDS];
68b285192aSMauro Carvalho Chehab static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
69b285192aSMauro Carvalho Chehab 				     -1, -1, -1, -1, -1, -1, -1, -1,
70b285192aSMauro Carvalho Chehab 				     -1, -1, -1, -1, -1, -1, -1, -1,
71b285192aSMauro Carvalho Chehab 				     -1, -1, -1, -1, -1, -1, -1, -1 };
72b285192aSMauro Carvalho Chehab static int radio[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
73b285192aSMauro Carvalho Chehab 				     -1, -1, -1, -1, -1, -1, -1, -1,
74b285192aSMauro Carvalho Chehab 				     -1, -1, -1, -1, -1, -1, -1, -1,
75b285192aSMauro Carvalho Chehab 				     -1, -1, -1, -1, -1, -1, -1, -1 };
76b285192aSMauro Carvalho Chehab static unsigned cardtype_c = 1;
77b285192aSMauro Carvalho Chehab static unsigned tuner_c = 1;
78b285192aSMauro Carvalho Chehab static unsigned radio_c = 1;
79b285192aSMauro Carvalho Chehab static char pal[] = "--";
80b285192aSMauro Carvalho Chehab static char secam[] = "--";
81b285192aSMauro Carvalho Chehab static char ntsc[] = "-";
82b285192aSMauro Carvalho Chehab 
83b285192aSMauro Carvalho Chehab /* Buffers */
84b285192aSMauro Carvalho Chehab static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
85b285192aSMauro Carvalho Chehab static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
86b285192aSMauro Carvalho Chehab static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;
87b285192aSMauro Carvalho Chehab static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
88b285192aSMauro Carvalho Chehab static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
89b285192aSMauro Carvalho Chehab static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;
90b285192aSMauro Carvalho Chehab 
91b285192aSMauro Carvalho Chehab static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
92b285192aSMauro Carvalho Chehab static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
93b285192aSMauro Carvalho Chehab static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
94b285192aSMauro Carvalho Chehab static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
95b285192aSMauro Carvalho Chehab static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;
96b285192aSMauro Carvalho Chehab 
97b285192aSMauro Carvalho Chehab static int enc_ts_bufs = -1;
98b285192aSMauro Carvalho Chehab static int enc_mpg_bufs = -1;
99b285192aSMauro Carvalho Chehab static int enc_idx_bufs = CX18_MAX_FW_MDLS_PER_STREAM;
100b285192aSMauro Carvalho Chehab static int enc_yuv_bufs = -1;
101b285192aSMauro Carvalho Chehab static int enc_vbi_bufs = -1;
102b285192aSMauro Carvalho Chehab static int enc_pcm_bufs = -1;
103b285192aSMauro Carvalho Chehab 
104b285192aSMauro Carvalho Chehab 
105b285192aSMauro Carvalho Chehab static int cx18_pci_latency = 1;
106b285192aSMauro Carvalho Chehab 
107b285192aSMauro Carvalho Chehab static int mmio_ndelay;
108b285192aSMauro Carvalho Chehab static int retry_mmio = 1;
109b285192aSMauro Carvalho Chehab 
110b285192aSMauro Carvalho Chehab int cx18_debug;
111b285192aSMauro Carvalho Chehab 
112b285192aSMauro Carvalho Chehab module_param_array(tuner, int, &tuner_c, 0644);
113b285192aSMauro Carvalho Chehab module_param_array(radio, int, &radio_c, 0644);
114b285192aSMauro Carvalho Chehab module_param_array(cardtype, int, &cardtype_c, 0644);
115b285192aSMauro Carvalho Chehab module_param_string(pal, pal, sizeof(pal), 0644);
116b285192aSMauro Carvalho Chehab module_param_string(secam, secam, sizeof(secam), 0644);
117b285192aSMauro Carvalho Chehab module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
118b285192aSMauro Carvalho Chehab module_param_named(debug, cx18_debug, int, 0644);
119b285192aSMauro Carvalho Chehab module_param(mmio_ndelay, int, 0644);
120b285192aSMauro Carvalho Chehab module_param(retry_mmio, int, 0644);
121b285192aSMauro Carvalho Chehab module_param(cx18_pci_latency, int, 0644);
122b285192aSMauro Carvalho Chehab module_param(cx18_first_minor, int, 0644);
123b285192aSMauro Carvalho Chehab 
124b285192aSMauro Carvalho Chehab module_param(enc_ts_buffers, int, 0644);
125b285192aSMauro Carvalho Chehab module_param(enc_mpg_buffers, int, 0644);
126b285192aSMauro Carvalho Chehab module_param(enc_idx_buffers, int, 0644);
127b285192aSMauro Carvalho Chehab module_param(enc_yuv_buffers, int, 0644);
128b285192aSMauro Carvalho Chehab module_param(enc_vbi_buffers, int, 0644);
129b285192aSMauro Carvalho Chehab module_param(enc_pcm_buffers, int, 0644);
130b285192aSMauro Carvalho Chehab 
131b285192aSMauro Carvalho Chehab module_param(enc_ts_bufsize, int, 0644);
132b285192aSMauro Carvalho Chehab module_param(enc_mpg_bufsize, int, 0644);
133b285192aSMauro Carvalho Chehab module_param(enc_idx_bufsize, int, 0644);
134b285192aSMauro Carvalho Chehab module_param(enc_yuv_bufsize, int, 0644);
135b285192aSMauro Carvalho Chehab module_param(enc_pcm_bufsize, int, 0644);
136b285192aSMauro Carvalho Chehab 
137b285192aSMauro Carvalho Chehab module_param(enc_ts_bufs, int, 0644);
138b285192aSMauro Carvalho Chehab module_param(enc_mpg_bufs, int, 0644);
139b285192aSMauro Carvalho Chehab module_param(enc_idx_bufs, int, 0644);
140b285192aSMauro Carvalho Chehab module_param(enc_yuv_bufs, int, 0644);
141b285192aSMauro Carvalho Chehab module_param(enc_vbi_bufs, int, 0644);
142b285192aSMauro Carvalho Chehab module_param(enc_pcm_bufs, int, 0644);
143b285192aSMauro Carvalho Chehab 
144b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
145b285192aSMauro Carvalho Chehab 			"\t\t\tsee tuner.h for values");
146b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(radio,
147b285192aSMauro Carvalho Chehab 		 "Enable or disable the radio. Use only if autodetection\n"
148b285192aSMauro Carvalho Chehab 		 "\t\t\tfails. 0 = disable, 1 = enable");
149b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(cardtype,
150b285192aSMauro Carvalho Chehab 		 "Only use this option if your card is not detected properly.\n"
151b285192aSMauro Carvalho Chehab 		 "\t\tSpecify card type:\n"
152b285192aSMauro Carvalho Chehab 		 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
153b285192aSMauro Carvalho Chehab 		 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
154b285192aSMauro Carvalho Chehab 		 "\t\t\t 3 = Compro VideoMate H900\n"
155b285192aSMauro Carvalho Chehab 		 "\t\t\t 4 = Yuan MPC718\n"
156b285192aSMauro Carvalho Chehab 		 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
157b285192aSMauro Carvalho Chehab 		 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
158b285192aSMauro Carvalho Chehab 		 "\t\t\t 7 = Leadtek WinFast PVR2100\n"
159b285192aSMauro Carvalho Chehab 		 "\t\t\t 8 = Leadtek WinFast DVR3100 H\n"
160b285192aSMauro Carvalho Chehab 		 "\t\t\t 9 = GoTView PCI DVD3 Hybrid\n"
161b285192aSMauro Carvalho Chehab 		 "\t\t\t 10 = Hauppauge HVR 1600 (S5H1411)\n"
162b285192aSMauro Carvalho Chehab 		 "\t\t\t 0 = Autodetect (default)\n"
163b285192aSMauro Carvalho Chehab 		 "\t\t\t-1 = Ignore this card\n\t\t");
164b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
165b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
166b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
167b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(debug,
168b285192aSMauro Carvalho Chehab 		 "Debug level (bitmask). Default: 0\n"
169b285192aSMauro Carvalho Chehab 		 "\t\t\t  1/0x0001: warning\n"
170b285192aSMauro Carvalho Chehab 		 "\t\t\t  2/0x0002: info\n"
171b285192aSMauro Carvalho Chehab 		 "\t\t\t  4/0x0004: mailbox\n"
172b285192aSMauro Carvalho Chehab 		 "\t\t\t  8/0x0008: dma\n"
173b285192aSMauro Carvalho Chehab 		 "\t\t\t 16/0x0010: ioctl\n"
174b285192aSMauro Carvalho Chehab 		 "\t\t\t 32/0x0020: file\n"
175b285192aSMauro Carvalho Chehab 		 "\t\t\t 64/0x0040: i2c\n"
176b285192aSMauro Carvalho Chehab 		 "\t\t\t128/0x0080: irq\n"
177b285192aSMauro Carvalho Chehab 		 "\t\t\t256/0x0100: high volume\n");
178b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(cx18_pci_latency,
179b285192aSMauro Carvalho Chehab 		 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
180b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: Yes");
181b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(retry_mmio,
182b285192aSMauro Carvalho Chehab 		 "(Deprecated) MMIO writes are now always checked and retried\n"
183b285192aSMauro Carvalho Chehab 		 "\t\t\tEffectively: 1 [Yes]");
184b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(mmio_ndelay,
185b285192aSMauro Carvalho Chehab 		 "(Deprecated) MMIO accesses are now never purposely delayed\n"
186b285192aSMauro Carvalho Chehab 		 "\t\t\tEffectively: 0 ns");
187b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_ts_buffers,
188b285192aSMauro Carvalho Chehab 		 "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"
189b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
190b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_ts_bufsize,
191b285192aSMauro Carvalho Chehab 		 "Size of an encoder TS buffer (kB)\n"
192b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));
193b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_ts_bufs,
194b285192aSMauro Carvalho Chehab 		 "Number of encoder TS buffers\n"
195b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault is computed from other enc_ts_* parameters");
196b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_mpg_buffers,
197b285192aSMauro Carvalho Chehab 		 "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"
198b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
199b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_mpg_bufsize,
200b285192aSMauro Carvalho Chehab 		 "Size of an encoder MPG buffer (kB)\n"
201b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));
202b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_mpg_bufs,
203b285192aSMauro Carvalho Chehab 		 "Number of encoder MPG buffers\n"
204b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault is computed from other enc_mpg_* parameters");
205b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_idx_buffers,
206b285192aSMauro Carvalho Chehab 		 "(Deprecated) Encoder IDX buffer memory (MB)\n"
207b285192aSMauro Carvalho Chehab 		 "\t\t\tIgnored, except 0 disables IDX buffer allocations\n"
208b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: 1 [Enabled]");
209b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_idx_bufsize,
210b285192aSMauro Carvalho Chehab 		 "Size of an encoder IDX buffer (kB)\n"
211b285192aSMauro Carvalho Chehab 		 "\t\t\tAllowed values are multiples of 1.5 kB rounded up\n"
212b285192aSMauro Carvalho Chehab 		 "\t\t\t(multiples of size required for 64 index entries)\n"
213b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: 2");
214b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_idx_bufs,
215b285192aSMauro Carvalho Chehab 		 "Number of encoder IDX buffers\n"
216b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_MAX_FW_MDLS_PER_STREAM));
217b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_yuv_buffers,
218b285192aSMauro Carvalho Chehab 		 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
219b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
220b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_yuv_bufsize,
221b285192aSMauro Carvalho Chehab 		 "Size of an encoder YUV buffer (kB)\n"
222b285192aSMauro Carvalho Chehab 		 "\t\t\tAllowed values are multiples of 33.75 kB rounded up\n"
223b285192aSMauro Carvalho Chehab 		 "\t\t\t(multiples of size required for 32 screen lines)\n"
224b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: 102");
225b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_yuv_bufs,
226b285192aSMauro Carvalho Chehab 		 "Number of encoder YUV buffers\n"
227b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault is computed from other enc_yuv_* parameters");
228b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_vbi_buffers,
229b285192aSMauro Carvalho Chehab 		 "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"
230b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
231b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_vbi_bufs,
232b285192aSMauro Carvalho Chehab 		 "Number of encoder VBI buffers\n"
233b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault is computed from enc_vbi_buffers");
234b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_pcm_buffers,
235b285192aSMauro Carvalho Chehab 		 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
236b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
237b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_pcm_bufsize,
238b285192aSMauro Carvalho Chehab 		 "Size of an encoder PCM buffer (kB)\n"
239b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));
240b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enc_pcm_bufs,
241b285192aSMauro Carvalho Chehab 		 "Number of encoder PCM buffers\n"
242b285192aSMauro Carvalho Chehab 		 "\t\t\tDefault is computed from other enc_pcm_* parameters");
243b285192aSMauro Carvalho Chehab 
244b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(cx18_first_minor,
245b285192aSMauro Carvalho Chehab 		 "Set device node number assigned to first card");
246b285192aSMauro Carvalho Chehab 
247b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Hans Verkuil");
248b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("CX23418 driver");
249b285192aSMauro Carvalho Chehab MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
250b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
251b285192aSMauro Carvalho Chehab 
252b285192aSMauro Carvalho Chehab MODULE_VERSION(CX18_VERSION);
253b285192aSMauro Carvalho Chehab 
254b285192aSMauro Carvalho Chehab #if defined(CONFIG_MODULES) && defined(MODULE)
255b285192aSMauro Carvalho Chehab static void request_module_async(struct work_struct *work)
256b285192aSMauro Carvalho Chehab {
257b285192aSMauro Carvalho Chehab 	struct cx18 *dev = container_of(work, struct cx18, request_module_wk);
258b285192aSMauro Carvalho Chehab 
259b285192aSMauro Carvalho Chehab 	/* Make sure cx18-alsa module is loaded */
260b285192aSMauro Carvalho Chehab 	request_module("cx18-alsa");
261b285192aSMauro Carvalho Chehab 
262b285192aSMauro Carvalho Chehab 	/* Initialize cx18-alsa for this instance of the cx18 device */
263b285192aSMauro Carvalho Chehab 	if (cx18_ext_init != NULL)
264b285192aSMauro Carvalho Chehab 		cx18_ext_init(dev);
265b285192aSMauro Carvalho Chehab }
266b285192aSMauro Carvalho Chehab 
267b285192aSMauro Carvalho Chehab static void request_modules(struct cx18 *dev)
268b285192aSMauro Carvalho Chehab {
269b285192aSMauro Carvalho Chehab 	INIT_WORK(&dev->request_module_wk, request_module_async);
270b285192aSMauro Carvalho Chehab 	schedule_work(&dev->request_module_wk);
271b285192aSMauro Carvalho Chehab }
272b285192aSMauro Carvalho Chehab 
273b285192aSMauro Carvalho Chehab static void flush_request_modules(struct cx18 *dev)
274b285192aSMauro Carvalho Chehab {
2750b8e74c6SLinus Torvalds 	flush_work(&dev->request_module_wk);
276b285192aSMauro Carvalho Chehab }
277b285192aSMauro Carvalho Chehab #else
278b285192aSMauro Carvalho Chehab #define request_modules(dev)
279b285192aSMauro Carvalho Chehab #define flush_request_modules(dev)
280b285192aSMauro Carvalho Chehab #endif /* CONFIG_MODULES */
281b285192aSMauro Carvalho Chehab 
282b285192aSMauro Carvalho Chehab /* Generic utility functions */
283b285192aSMauro Carvalho Chehab int cx18_msleep_timeout(unsigned int msecs, int intr)
284b285192aSMauro Carvalho Chehab {
285b285192aSMauro Carvalho Chehab 	long int timeout = msecs_to_jiffies(msecs);
286b285192aSMauro Carvalho Chehab 	int sig;
287b285192aSMauro Carvalho Chehab 
288b285192aSMauro Carvalho Chehab 	do {
289b285192aSMauro Carvalho Chehab 		set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
290b285192aSMauro Carvalho Chehab 		timeout = schedule_timeout(timeout);
291b285192aSMauro Carvalho Chehab 		sig = intr ? signal_pending(current) : 0;
292b285192aSMauro Carvalho Chehab 	} while (!sig && timeout);
293b285192aSMauro Carvalho Chehab 	return sig;
294b285192aSMauro Carvalho Chehab }
295b285192aSMauro Carvalho Chehab 
296b285192aSMauro Carvalho Chehab /* Release ioremapped memory */
297b285192aSMauro Carvalho Chehab static void cx18_iounmap(struct cx18 *cx)
298b285192aSMauro Carvalho Chehab {
299b285192aSMauro Carvalho Chehab 	if (cx == NULL)
300b285192aSMauro Carvalho Chehab 		return;
301b285192aSMauro Carvalho Chehab 
302b285192aSMauro Carvalho Chehab 	/* Release io memory */
303b285192aSMauro Carvalho Chehab 	if (cx->enc_mem != NULL) {
304b285192aSMauro Carvalho Chehab 		CX18_DEBUG_INFO("releasing enc_mem\n");
305b285192aSMauro Carvalho Chehab 		iounmap(cx->enc_mem);
306b285192aSMauro Carvalho Chehab 		cx->enc_mem = NULL;
307b285192aSMauro Carvalho Chehab 	}
308b285192aSMauro Carvalho Chehab }
309b285192aSMauro Carvalho Chehab 
310b285192aSMauro Carvalho Chehab static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len)
311b285192aSMauro Carvalho Chehab {
312b285192aSMauro Carvalho Chehab 	int i;
313b285192aSMauro Carvalho Chehab 
314b285192aSMauro Carvalho Chehab 	CX18_INFO("eeprom dump:\n");
315b285192aSMauro Carvalho Chehab 	for (i = 0; i < len; i++) {
316b285192aSMauro Carvalho Chehab 		if (0 == (i % 16))
317b285192aSMauro Carvalho Chehab 			CX18_INFO("eeprom %02x:", i);
318b285192aSMauro Carvalho Chehab 		printk(KERN_CONT " %02x", eedata[i]);
319b285192aSMauro Carvalho Chehab 		if (15 == (i % 16))
320b285192aSMauro Carvalho Chehab 			printk(KERN_CONT "\n");
321b285192aSMauro Carvalho Chehab 	}
322b285192aSMauro Carvalho Chehab }
323b285192aSMauro Carvalho Chehab 
324b285192aSMauro Carvalho Chehab /* Hauppauge card? get values from tveeprom */
325b285192aSMauro Carvalho Chehab void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
326b285192aSMauro Carvalho Chehab {
3271d212cf0SMauro Carvalho Chehab 	struct i2c_client *c;
328b285192aSMauro Carvalho Chehab 	u8 eedata[256];
329b285192aSMauro Carvalho Chehab 
330e351bf25SDan Carpenter 	memset(tv, 0, sizeof(*tv));
331e351bf25SDan Carpenter 
3321d212cf0SMauro Carvalho Chehab 	c = kzalloc(sizeof(*c), GFP_KERNEL);
333e351bf25SDan Carpenter 	if (!c)
334e351bf25SDan Carpenter 		return;
3351d212cf0SMauro Carvalho Chehab 
3361d212cf0SMauro Carvalho Chehab 	strlcpy(c->name, "cx18 tveeprom tmp", sizeof(c->name));
3371d212cf0SMauro Carvalho Chehab 	c->adapter = &cx->i2c_adap[0];
3381d212cf0SMauro Carvalho Chehab 	c->addr = 0xa0 >> 1;
339b285192aSMauro Carvalho Chehab 
3401d212cf0SMauro Carvalho Chehab 	if (tveeprom_read(c, eedata, sizeof(eedata)))
3411d212cf0SMauro Carvalho Chehab 		goto ret;
342b285192aSMauro Carvalho Chehab 
343b285192aSMauro Carvalho Chehab 	switch (cx->card->type) {
344b285192aSMauro Carvalho Chehab 	case CX18_CARD_HVR_1600_ESMT:
345b285192aSMauro Carvalho Chehab 	case CX18_CARD_HVR_1600_SAMSUNG:
346b285192aSMauro Carvalho Chehab 	case CX18_CARD_HVR_1600_S5H1411:
3471d212cf0SMauro Carvalho Chehab 		tveeprom_hauppauge_analog(c, tv, eedata);
348b285192aSMauro Carvalho Chehab 		break;
349b285192aSMauro Carvalho Chehab 	case CX18_CARD_YUAN_MPC718:
350b285192aSMauro Carvalho Chehab 	case CX18_CARD_GOTVIEW_PCI_DVD3:
351b285192aSMauro Carvalho Chehab 		tv->model = 0x718;
352b285192aSMauro Carvalho Chehab 		cx18_eeprom_dump(cx, eedata, sizeof(eedata));
353b285192aSMauro Carvalho Chehab 		CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n",
354b285192aSMauro Carvalho Chehab 			  eedata[2], eedata[1], eedata[4], eedata[3]);
355b285192aSMauro Carvalho Chehab 		break;
356b285192aSMauro Carvalho Chehab 	default:
357b285192aSMauro Carvalho Chehab 		tv->model = 0xffffffff;
358b285192aSMauro Carvalho Chehab 		cx18_eeprom_dump(cx, eedata, sizeof(eedata));
359b285192aSMauro Carvalho Chehab 		break;
360b285192aSMauro Carvalho Chehab 	}
3611d212cf0SMauro Carvalho Chehab 
3621d212cf0SMauro Carvalho Chehab ret:
3631d212cf0SMauro Carvalho Chehab 	kfree(c);
364b285192aSMauro Carvalho Chehab }
365b285192aSMauro Carvalho Chehab 
366b285192aSMauro Carvalho Chehab static void cx18_process_eeprom(struct cx18 *cx)
367b285192aSMauro Carvalho Chehab {
368b285192aSMauro Carvalho Chehab 	struct tveeprom tv;
369b285192aSMauro Carvalho Chehab 
370b285192aSMauro Carvalho Chehab 	cx18_read_eeprom(cx, &tv);
371b285192aSMauro Carvalho Chehab 
372b285192aSMauro Carvalho Chehab 	/* Many thanks to Steven Toth from Hauppauge for providing the
373b285192aSMauro Carvalho Chehab 	   model numbers */
374b285192aSMauro Carvalho Chehab 	/* Note: the Samsung memory models cannot be reliably determined
375b285192aSMauro Carvalho Chehab 	   from the model number. Use the cardtype module option if you
376b285192aSMauro Carvalho Chehab 	   have one of these preproduction models. */
377b285192aSMauro Carvalho Chehab 	switch (tv.model) {
378b285192aSMauro Carvalho Chehab 	case 74301: /* Retail models */
379b285192aSMauro Carvalho Chehab 	case 74321:
380b285192aSMauro Carvalho Chehab 	case 74351: /* OEM models */
381b285192aSMauro Carvalho Chehab 	case 74361:
382b285192aSMauro Carvalho Chehab 		/* Digital side is s5h1411/tda18271 */
383b285192aSMauro Carvalho Chehab 		cx->card = cx18_get_card(CX18_CARD_HVR_1600_S5H1411);
384b285192aSMauro Carvalho Chehab 		break;
385b285192aSMauro Carvalho Chehab 	case 74021: /* Retail models */
386b285192aSMauro Carvalho Chehab 	case 74031:
387b285192aSMauro Carvalho Chehab 	case 74041:
388b285192aSMauro Carvalho Chehab 	case 74141:
389b285192aSMauro Carvalho Chehab 	case 74541: /* OEM models */
390b285192aSMauro Carvalho Chehab 	case 74551:
391b285192aSMauro Carvalho Chehab 	case 74591:
392b285192aSMauro Carvalho Chehab 	case 74651:
393b285192aSMauro Carvalho Chehab 	case 74691:
394b285192aSMauro Carvalho Chehab 	case 74751:
395b285192aSMauro Carvalho Chehab 	case 74891:
396b285192aSMauro Carvalho Chehab 		/* Digital side is s5h1409/mxl5005s */
397b285192aSMauro Carvalho Chehab 		cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
398b285192aSMauro Carvalho Chehab 		break;
399b285192aSMauro Carvalho Chehab 	case 0x718:
400b285192aSMauro Carvalho Chehab 		return;
401b285192aSMauro Carvalho Chehab 	case 0xffffffff:
402b285192aSMauro Carvalho Chehab 		CX18_INFO("Unknown EEPROM encoding\n");
403b285192aSMauro Carvalho Chehab 		return;
404b285192aSMauro Carvalho Chehab 	case 0:
405b285192aSMauro Carvalho Chehab 		CX18_ERR("Invalid EEPROM\n");
406b285192aSMauro Carvalho Chehab 		return;
407b285192aSMauro Carvalho Chehab 	default:
408b285192aSMauro Carvalho Chehab 		CX18_ERR("Unknown model %d, defaulting to original HVR-1600 "
409b285192aSMauro Carvalho Chehab 			 "(cardtype=1)\n", tv.model);
410b285192aSMauro Carvalho Chehab 		cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
411b285192aSMauro Carvalho Chehab 		break;
412b285192aSMauro Carvalho Chehab 	}
413b285192aSMauro Carvalho Chehab 
414b285192aSMauro Carvalho Chehab 	cx->v4l2_cap = cx->card->v4l2_capabilities;
415b285192aSMauro Carvalho Chehab 	cx->card_name = cx->card->name;
416b285192aSMauro Carvalho Chehab 	cx->card_i2c = cx->card->i2c;
417b285192aSMauro Carvalho Chehab 
418b285192aSMauro Carvalho Chehab 	CX18_INFO("Autodetected %s\n", cx->card_name);
419b285192aSMauro Carvalho Chehab 
420b285192aSMauro Carvalho Chehab 	if (tv.tuner_type == TUNER_ABSENT)
421b285192aSMauro Carvalho Chehab 		CX18_ERR("tveeprom cannot autodetect tuner!\n");
422b285192aSMauro Carvalho Chehab 
423b285192aSMauro Carvalho Chehab 	if (cx->options.tuner == -1)
424b285192aSMauro Carvalho Chehab 		cx->options.tuner = tv.tuner_type;
425b285192aSMauro Carvalho Chehab 	if (cx->options.radio == -1)
426b285192aSMauro Carvalho Chehab 		cx->options.radio = (tv.has_radio != 0);
427b285192aSMauro Carvalho Chehab 
428b285192aSMauro Carvalho Chehab 	if (cx->std != 0)
429b285192aSMauro Carvalho Chehab 		/* user specified tuner standard */
430b285192aSMauro Carvalho Chehab 		return;
431b285192aSMauro Carvalho Chehab 
432b285192aSMauro Carvalho Chehab 	/* autodetect tuner standard */
433b285192aSMauro Carvalho Chehab #define TVEEPROM_TUNER_FORMAT_ALL (V4L2_STD_B  | V4L2_STD_GH | \
434b285192aSMauro Carvalho Chehab 				   V4L2_STD_MN | \
435b285192aSMauro Carvalho Chehab 				   V4L2_STD_PAL_I | \
436b285192aSMauro Carvalho Chehab 				   V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC | \
437b285192aSMauro Carvalho Chehab 				   V4L2_STD_DK)
438b285192aSMauro Carvalho Chehab 	if ((tv.tuner_formats & TVEEPROM_TUNER_FORMAT_ALL)
439b285192aSMauro Carvalho Chehab 					== TVEEPROM_TUNER_FORMAT_ALL) {
440b285192aSMauro Carvalho Chehab 		CX18_DEBUG_INFO("Worldwide tuner detected\n");
441b285192aSMauro Carvalho Chehab 		cx->std = V4L2_STD_ALL;
442b285192aSMauro Carvalho Chehab 	} else if (tv.tuner_formats & V4L2_STD_PAL) {
443b285192aSMauro Carvalho Chehab 		CX18_DEBUG_INFO("PAL tuner detected\n");
444b285192aSMauro Carvalho Chehab 		cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
445b285192aSMauro Carvalho Chehab 	} else if (tv.tuner_formats & V4L2_STD_NTSC) {
446b285192aSMauro Carvalho Chehab 		CX18_DEBUG_INFO("NTSC tuner detected\n");
447b285192aSMauro Carvalho Chehab 		cx->std |= V4L2_STD_NTSC_M;
448b285192aSMauro Carvalho Chehab 	} else if (tv.tuner_formats & V4L2_STD_SECAM) {
449b285192aSMauro Carvalho Chehab 		CX18_DEBUG_INFO("SECAM tuner detected\n");
450b285192aSMauro Carvalho Chehab 		cx->std |= V4L2_STD_SECAM_L;
451b285192aSMauro Carvalho Chehab 	} else {
452b285192aSMauro Carvalho Chehab 		CX18_INFO("No tuner detected, default to NTSC-M\n");
453b285192aSMauro Carvalho Chehab 		cx->std |= V4L2_STD_NTSC_M;
454b285192aSMauro Carvalho Chehab 	}
455b285192aSMauro Carvalho Chehab }
456b285192aSMauro Carvalho Chehab 
457b285192aSMauro Carvalho Chehab static v4l2_std_id cx18_parse_std(struct cx18 *cx)
458b285192aSMauro Carvalho Chehab {
459b285192aSMauro Carvalho Chehab 	switch (pal[0]) {
460b285192aSMauro Carvalho Chehab 	case '6':
461b285192aSMauro Carvalho Chehab 		return V4L2_STD_PAL_60;
462b285192aSMauro Carvalho Chehab 	case 'b':
463b285192aSMauro Carvalho Chehab 	case 'B':
464b285192aSMauro Carvalho Chehab 	case 'g':
465b285192aSMauro Carvalho Chehab 	case 'G':
466b285192aSMauro Carvalho Chehab 		return V4L2_STD_PAL_BG;
467b285192aSMauro Carvalho Chehab 	case 'h':
468b285192aSMauro Carvalho Chehab 	case 'H':
469b285192aSMauro Carvalho Chehab 		return V4L2_STD_PAL_H;
470b285192aSMauro Carvalho Chehab 	case 'n':
471b285192aSMauro Carvalho Chehab 	case 'N':
472b285192aSMauro Carvalho Chehab 		if (pal[1] == 'c' || pal[1] == 'C')
473b285192aSMauro Carvalho Chehab 			return V4L2_STD_PAL_Nc;
474b285192aSMauro Carvalho Chehab 		return V4L2_STD_PAL_N;
475b285192aSMauro Carvalho Chehab 	case 'i':
476b285192aSMauro Carvalho Chehab 	case 'I':
477b285192aSMauro Carvalho Chehab 		return V4L2_STD_PAL_I;
478b285192aSMauro Carvalho Chehab 	case 'd':
479b285192aSMauro Carvalho Chehab 	case 'D':
480b285192aSMauro Carvalho Chehab 	case 'k':
481b285192aSMauro Carvalho Chehab 	case 'K':
482b285192aSMauro Carvalho Chehab 		return V4L2_STD_PAL_DK;
483b285192aSMauro Carvalho Chehab 	case 'M':
484b285192aSMauro Carvalho Chehab 	case 'm':
485b285192aSMauro Carvalho Chehab 		return V4L2_STD_PAL_M;
486b285192aSMauro Carvalho Chehab 	case '-':
487b285192aSMauro Carvalho Chehab 		break;
488b285192aSMauro Carvalho Chehab 	default:
489b285192aSMauro Carvalho Chehab 		CX18_WARN("pal= argument not recognised\n");
490b285192aSMauro Carvalho Chehab 		return 0;
491b285192aSMauro Carvalho Chehab 	}
492b285192aSMauro Carvalho Chehab 
493b285192aSMauro Carvalho Chehab 	switch (secam[0]) {
494b285192aSMauro Carvalho Chehab 	case 'b':
495b285192aSMauro Carvalho Chehab 	case 'B':
496b285192aSMauro Carvalho Chehab 	case 'g':
497b285192aSMauro Carvalho Chehab 	case 'G':
498b285192aSMauro Carvalho Chehab 	case 'h':
499b285192aSMauro Carvalho Chehab 	case 'H':
500b285192aSMauro Carvalho Chehab 		return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
501b285192aSMauro Carvalho Chehab 	case 'd':
502b285192aSMauro Carvalho Chehab 	case 'D':
503b285192aSMauro Carvalho Chehab 	case 'k':
504b285192aSMauro Carvalho Chehab 	case 'K':
505b285192aSMauro Carvalho Chehab 		return V4L2_STD_SECAM_DK;
506b285192aSMauro Carvalho Chehab 	case 'l':
507b285192aSMauro Carvalho Chehab 	case 'L':
508b285192aSMauro Carvalho Chehab 		if (secam[1] == 'C' || secam[1] == 'c')
509b285192aSMauro Carvalho Chehab 			return V4L2_STD_SECAM_LC;
510b285192aSMauro Carvalho Chehab 		return V4L2_STD_SECAM_L;
511b285192aSMauro Carvalho Chehab 	case '-':
512b285192aSMauro Carvalho Chehab 		break;
513b285192aSMauro Carvalho Chehab 	default:
514b285192aSMauro Carvalho Chehab 		CX18_WARN("secam= argument not recognised\n");
515b285192aSMauro Carvalho Chehab 		return 0;
516b285192aSMauro Carvalho Chehab 	}
517b285192aSMauro Carvalho Chehab 
518b285192aSMauro Carvalho Chehab 	switch (ntsc[0]) {
519b285192aSMauro Carvalho Chehab 	case 'm':
520b285192aSMauro Carvalho Chehab 	case 'M':
521b285192aSMauro Carvalho Chehab 		return V4L2_STD_NTSC_M;
522b285192aSMauro Carvalho Chehab 	case 'j':
523b285192aSMauro Carvalho Chehab 	case 'J':
524b285192aSMauro Carvalho Chehab 		return V4L2_STD_NTSC_M_JP;
525b285192aSMauro Carvalho Chehab 	case 'k':
526b285192aSMauro Carvalho Chehab 	case 'K':
527b285192aSMauro Carvalho Chehab 		return V4L2_STD_NTSC_M_KR;
528b285192aSMauro Carvalho Chehab 	case '-':
529b285192aSMauro Carvalho Chehab 		break;
530b285192aSMauro Carvalho Chehab 	default:
531b285192aSMauro Carvalho Chehab 		CX18_WARN("ntsc= argument not recognised\n");
532b285192aSMauro Carvalho Chehab 		return 0;
533b285192aSMauro Carvalho Chehab 	}
534b285192aSMauro Carvalho Chehab 
535b285192aSMauro Carvalho Chehab 	/* no match found */
536b285192aSMauro Carvalho Chehab 	return 0;
537b285192aSMauro Carvalho Chehab }
538b285192aSMauro Carvalho Chehab 
539b285192aSMauro Carvalho Chehab static void cx18_process_options(struct cx18 *cx)
540b285192aSMauro Carvalho Chehab {
541b285192aSMauro Carvalho Chehab 	int i, j;
542b285192aSMauro Carvalho Chehab 
543b285192aSMauro Carvalho Chehab 	cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
544b285192aSMauro Carvalho Chehab 	cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
545b285192aSMauro Carvalho Chehab 	cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;
546b285192aSMauro Carvalho Chehab 	cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
547b285192aSMauro Carvalho Chehab 	cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
548b285192aSMauro Carvalho Chehab 	cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
549b285192aSMauro Carvalho Chehab 	cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */
550b285192aSMauro Carvalho Chehab 
551b285192aSMauro Carvalho Chehab 	cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;
552b285192aSMauro Carvalho Chehab 	cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;
553b285192aSMauro Carvalho Chehab 	cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;
554b285192aSMauro Carvalho Chehab 	cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;
555b285192aSMauro Carvalho Chehab 	cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;
556b285192aSMauro Carvalho Chehab 	cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;
557b285192aSMauro Carvalho Chehab 	cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */
558b285192aSMauro Carvalho Chehab 
559b285192aSMauro Carvalho Chehab 	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;
560b285192aSMauro Carvalho Chehab 	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
561b285192aSMauro Carvalho Chehab 	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
562b285192aSMauro Carvalho Chehab 	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
563*318de791SMauro Carvalho Chehab 	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = VBI_ACTIVE_SAMPLES * 36;
564b285192aSMauro Carvalho Chehab 	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
565b285192aSMauro Carvalho Chehab 	cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
566b285192aSMauro Carvalho Chehab 
567b285192aSMauro Carvalho Chehab 	/* Ensure stream_buffers & stream_buf_size are valid */
568b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX18_MAX_STREAMS; i++) {
569b285192aSMauro Carvalho Chehab 		if (cx->stream_buffers[i] == 0 ||     /* User said 0 buffers */
570b285192aSMauro Carvalho Chehab 		    cx->options.megabytes[i] <= 0 ||  /* User said 0 MB total */
571b285192aSMauro Carvalho Chehab 		    cx->stream_buf_size[i] <= 0) {    /* User said buf size 0 */
572b285192aSMauro Carvalho Chehab 			cx->options.megabytes[i] = 0;
573b285192aSMauro Carvalho Chehab 			cx->stream_buffers[i] = 0;
574b285192aSMauro Carvalho Chehab 			cx->stream_buf_size[i] = 0;
575b285192aSMauro Carvalho Chehab 			continue;
576b285192aSMauro Carvalho Chehab 		}
577b285192aSMauro Carvalho Chehab 		/*
578b285192aSMauro Carvalho Chehab 		 * YUV is a special case where the stream_buf_size needs to be
579b285192aSMauro Carvalho Chehab 		 * an integral multiple of 33.75 kB (storage for 32 screens
580b285192aSMauro Carvalho Chehab 		 * lines to maintain alignment in case of lost buffers).
581b285192aSMauro Carvalho Chehab 		 *
582b285192aSMauro Carvalho Chehab 		 * IDX is a special case where the stream_buf_size should be
583b285192aSMauro Carvalho Chehab 		 * an integral multiple of 1.5 kB (storage for 64 index entries
584b285192aSMauro Carvalho Chehab 		 * to maintain alignment in case of lost buffers).
585b285192aSMauro Carvalho Chehab 		 *
586b285192aSMauro Carvalho Chehab 		 */
587b285192aSMauro Carvalho Chehab 		if (i == CX18_ENC_STREAM_TYPE_YUV) {
588b285192aSMauro Carvalho Chehab 			cx->stream_buf_size[i] *= 1024;
589b285192aSMauro Carvalho Chehab 			cx->stream_buf_size[i] -=
590b285192aSMauro Carvalho Chehab 			   (cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE);
591b285192aSMauro Carvalho Chehab 
592b285192aSMauro Carvalho Chehab 			if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE)
593b285192aSMauro Carvalho Chehab 				cx->stream_buf_size[i] =
594b285192aSMauro Carvalho Chehab 						CX18_UNIT_ENC_YUV_BUFSIZE;
595b285192aSMauro Carvalho Chehab 		} else if (i == CX18_ENC_STREAM_TYPE_IDX) {
596b285192aSMauro Carvalho Chehab 			cx->stream_buf_size[i] *= 1024;
597b285192aSMauro Carvalho Chehab 			cx->stream_buf_size[i] -=
598b285192aSMauro Carvalho Chehab 			   (cx->stream_buf_size[i] % CX18_UNIT_ENC_IDX_BUFSIZE);
599b285192aSMauro Carvalho Chehab 
600b285192aSMauro Carvalho Chehab 			if (cx->stream_buf_size[i] < CX18_UNIT_ENC_IDX_BUFSIZE)
601b285192aSMauro Carvalho Chehab 				cx->stream_buf_size[i] =
602b285192aSMauro Carvalho Chehab 						CX18_UNIT_ENC_IDX_BUFSIZE;
603b285192aSMauro Carvalho Chehab 		}
604b285192aSMauro Carvalho Chehab 		/*
605b285192aSMauro Carvalho Chehab 		 * YUV and IDX are special cases where the stream_buf_size is
606b285192aSMauro Carvalho Chehab 		 * now in bytes.
607b285192aSMauro Carvalho Chehab 		 * VBI is a special case where the stream_buf_size is fixed
608b285192aSMauro Carvalho Chehab 		 * and already in bytes
609b285192aSMauro Carvalho Chehab 		 */
610b285192aSMauro Carvalho Chehab 		if (i == CX18_ENC_STREAM_TYPE_VBI ||
611b285192aSMauro Carvalho Chehab 		    i == CX18_ENC_STREAM_TYPE_YUV ||
612b285192aSMauro Carvalho Chehab 		    i == CX18_ENC_STREAM_TYPE_IDX) {
613b285192aSMauro Carvalho Chehab 			if (cx->stream_buffers[i] < 0) {
614b285192aSMauro Carvalho Chehab 				cx->stream_buffers[i] =
615b285192aSMauro Carvalho Chehab 					cx->options.megabytes[i] * 1024 * 1024
616b285192aSMauro Carvalho Chehab 					/ cx->stream_buf_size[i];
617b285192aSMauro Carvalho Chehab 			} else {
618b285192aSMauro Carvalho Chehab 				/* N.B. This might round down to 0 */
619b285192aSMauro Carvalho Chehab 				cx->options.megabytes[i] =
620b285192aSMauro Carvalho Chehab 					cx->stream_buffers[i]
621b285192aSMauro Carvalho Chehab 					* cx->stream_buf_size[i]/(1024 * 1024);
622b285192aSMauro Carvalho Chehab 			}
623b285192aSMauro Carvalho Chehab 		} else {
624b285192aSMauro Carvalho Chehab 			/* All other streams have stream_buf_size in kB here */
625b285192aSMauro Carvalho Chehab 			if (cx->stream_buffers[i] < 0) {
626b285192aSMauro Carvalho Chehab 				cx->stream_buffers[i] =
627b285192aSMauro Carvalho Chehab 						cx->options.megabytes[i] * 1024
628b285192aSMauro Carvalho Chehab 						/ cx->stream_buf_size[i];
629b285192aSMauro Carvalho Chehab 			} else {
630b285192aSMauro Carvalho Chehab 				/* N.B. This might round down to 0 */
631b285192aSMauro Carvalho Chehab 				cx->options.megabytes[i] =
632b285192aSMauro Carvalho Chehab 						cx->stream_buffers[i]
633b285192aSMauro Carvalho Chehab 						* cx->stream_buf_size[i] / 1024;
634b285192aSMauro Carvalho Chehab 			}
635b285192aSMauro Carvalho Chehab 			/* convert from kB to bytes */
636b285192aSMauro Carvalho Chehab 			cx->stream_buf_size[i] *= 1024;
637b285192aSMauro Carvalho Chehab 		}
638b285192aSMauro Carvalho Chehab 		CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, "
639b285192aSMauro Carvalho Chehab 				"%d bytes\n", i, cx->options.megabytes[i],
640b285192aSMauro Carvalho Chehab 				cx->stream_buffers[i], cx->stream_buf_size[i]);
641b285192aSMauro Carvalho Chehab 	}
642b285192aSMauro Carvalho Chehab 
643b285192aSMauro Carvalho Chehab 	cx->options.cardtype = cardtype[cx->instance];
644b285192aSMauro Carvalho Chehab 	cx->options.tuner = tuner[cx->instance];
645b285192aSMauro Carvalho Chehab 	cx->options.radio = radio[cx->instance];
646b285192aSMauro Carvalho Chehab 
647b285192aSMauro Carvalho Chehab 	cx->std = cx18_parse_std(cx);
648b285192aSMauro Carvalho Chehab 	if (cx->options.cardtype == -1) {
649b285192aSMauro Carvalho Chehab 		CX18_INFO("Ignore card\n");
650b285192aSMauro Carvalho Chehab 		return;
651b285192aSMauro Carvalho Chehab 	}
652b285192aSMauro Carvalho Chehab 	cx->card = cx18_get_card(cx->options.cardtype - 1);
653b285192aSMauro Carvalho Chehab 	if (cx->card)
654b285192aSMauro Carvalho Chehab 		CX18_INFO("User specified %s card\n", cx->card->name);
655b285192aSMauro Carvalho Chehab 	else if (cx->options.cardtype != 0)
656b285192aSMauro Carvalho Chehab 		CX18_ERR("Unknown user specified type, trying to autodetect card\n");
657b285192aSMauro Carvalho Chehab 	if (cx->card == NULL) {
658b285192aSMauro Carvalho Chehab 		if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
659b285192aSMauro Carvalho Chehab 			cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
660b285192aSMauro Carvalho Chehab 			CX18_INFO("Autodetected Hauppauge card\n");
661b285192aSMauro Carvalho Chehab 		}
662b285192aSMauro Carvalho Chehab 	}
663b285192aSMauro Carvalho Chehab 	if (cx->card == NULL) {
664b285192aSMauro Carvalho Chehab 		for (i = 0; (cx->card = cx18_get_card(i)); i++) {
665b285192aSMauro Carvalho Chehab 			if (cx->card->pci_list == NULL)
666b285192aSMauro Carvalho Chehab 				continue;
667b285192aSMauro Carvalho Chehab 			for (j = 0; cx->card->pci_list[j].device; j++) {
668b285192aSMauro Carvalho Chehab 				if (cx->pci_dev->device !=
669b285192aSMauro Carvalho Chehab 				    cx->card->pci_list[j].device)
670b285192aSMauro Carvalho Chehab 					continue;
671b285192aSMauro Carvalho Chehab 				if (cx->pci_dev->subsystem_vendor !=
672b285192aSMauro Carvalho Chehab 				    cx->card->pci_list[j].subsystem_vendor)
673b285192aSMauro Carvalho Chehab 					continue;
674b285192aSMauro Carvalho Chehab 				if (cx->pci_dev->subsystem_device !=
675b285192aSMauro Carvalho Chehab 				    cx->card->pci_list[j].subsystem_device)
676b285192aSMauro Carvalho Chehab 					continue;
677b285192aSMauro Carvalho Chehab 				CX18_INFO("Autodetected %s card\n", cx->card->name);
678b285192aSMauro Carvalho Chehab 				goto done;
679b285192aSMauro Carvalho Chehab 			}
680b285192aSMauro Carvalho Chehab 		}
681b285192aSMauro Carvalho Chehab 	}
682b285192aSMauro Carvalho Chehab done:
683b285192aSMauro Carvalho Chehab 
684b285192aSMauro Carvalho Chehab 	if (cx->card == NULL) {
685b285192aSMauro Carvalho Chehab 		cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
686b285192aSMauro Carvalho Chehab 		CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
687b285192aSMauro Carvalho Chehab 			 cx->pci_dev->vendor, cx->pci_dev->device);
688b285192aSMauro Carvalho Chehab 		CX18_ERR("              subsystem vendor/device: [%04x:%04x]\n",
689b285192aSMauro Carvalho Chehab 			 cx->pci_dev->subsystem_vendor,
690b285192aSMauro Carvalho Chehab 			 cx->pci_dev->subsystem_device);
691b285192aSMauro Carvalho Chehab 		CX18_ERR("Defaulting to %s card\n", cx->card->name);
692b285192aSMauro Carvalho Chehab 		CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
693b285192aSMauro Carvalho Chehab 		CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
694b285192aSMauro Carvalho Chehab 		CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
695b285192aSMauro Carvalho Chehab 	}
696b285192aSMauro Carvalho Chehab 	cx->v4l2_cap = cx->card->v4l2_capabilities;
697b285192aSMauro Carvalho Chehab 	cx->card_name = cx->card->name;
698b285192aSMauro Carvalho Chehab 	cx->card_i2c = cx->card->i2c;
699b285192aSMauro Carvalho Chehab }
700b285192aSMauro Carvalho Chehab 
7014c62e976SGreg Kroah-Hartman static int cx18_create_in_workq(struct cx18 *cx)
702b285192aSMauro Carvalho Chehab {
703b285192aSMauro Carvalho Chehab 	snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in",
704b285192aSMauro Carvalho Chehab 		 cx->v4l2_dev.name);
705d8537548SKees Cook 	cx->in_work_queue = alloc_ordered_workqueue("%s", 0, cx->in_workq_name);
706b285192aSMauro Carvalho Chehab 	if (cx->in_work_queue == NULL) {
707b285192aSMauro Carvalho Chehab 		CX18_ERR("Unable to create incoming mailbox handler thread\n");
708b285192aSMauro Carvalho Chehab 		return -ENOMEM;
709b285192aSMauro Carvalho Chehab 	}
710b285192aSMauro Carvalho Chehab 	return 0;
711b285192aSMauro Carvalho Chehab }
712b285192aSMauro Carvalho Chehab 
7134c62e976SGreg Kroah-Hartman static void cx18_init_in_work_orders(struct cx18 *cx)
714b285192aSMauro Carvalho Chehab {
715b285192aSMauro Carvalho Chehab 	int i;
716b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
717b285192aSMauro Carvalho Chehab 		cx->in_work_order[i].cx = cx;
718b285192aSMauro Carvalho Chehab 		cx->in_work_order[i].str = cx->epu_debug_str;
719b285192aSMauro Carvalho Chehab 		INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler);
720b285192aSMauro Carvalho Chehab 	}
721b285192aSMauro Carvalho Chehab }
722b285192aSMauro Carvalho Chehab 
723b285192aSMauro Carvalho Chehab /* Precondition: the cx18 structure has been memset to 0. Only
724b285192aSMauro Carvalho Chehab    the dev and instance fields have been filled in.
725b285192aSMauro Carvalho Chehab    No assumptions on the card type may be made here (see cx18_init_struct2
726b285192aSMauro Carvalho Chehab    for that).
727b285192aSMauro Carvalho Chehab  */
7284c62e976SGreg Kroah-Hartman static int cx18_init_struct1(struct cx18 *cx)
729b285192aSMauro Carvalho Chehab {
730b285192aSMauro Carvalho Chehab 	int ret;
731b285192aSMauro Carvalho Chehab 
732b285192aSMauro Carvalho Chehab 	cx->base_addr = pci_resource_start(cx->pci_dev, 0);
733b285192aSMauro Carvalho Chehab 
734b285192aSMauro Carvalho Chehab 	mutex_init(&cx->serialize_lock);
735b285192aSMauro Carvalho Chehab 	mutex_init(&cx->gpio_lock);
736b285192aSMauro Carvalho Chehab 	mutex_init(&cx->epu2apu_mb_lock);
737b285192aSMauro Carvalho Chehab 	mutex_init(&cx->epu2cpu_mb_lock);
738b285192aSMauro Carvalho Chehab 
739b285192aSMauro Carvalho Chehab 	ret = cx18_create_in_workq(cx);
740b285192aSMauro Carvalho Chehab 	if (ret)
741b285192aSMauro Carvalho Chehab 		return ret;
742b285192aSMauro Carvalho Chehab 
743b285192aSMauro Carvalho Chehab 	cx18_init_in_work_orders(cx);
744b285192aSMauro Carvalho Chehab 
745b285192aSMauro Carvalho Chehab 	/* start counting open_id at 1 */
746b285192aSMauro Carvalho Chehab 	cx->open_id = 1;
747b285192aSMauro Carvalho Chehab 
748b285192aSMauro Carvalho Chehab 	/* Initial settings */
749b285192aSMauro Carvalho Chehab 	cx->cxhdl.port = CX2341X_PORT_MEMORY;
750b285192aSMauro Carvalho Chehab 	cx->cxhdl.capabilities = CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI;
751b285192aSMauro Carvalho Chehab 	cx->cxhdl.ops = &cx18_cxhdl_ops;
752b285192aSMauro Carvalho Chehab 	cx->cxhdl.func = cx18_api_func;
753b285192aSMauro Carvalho Chehab 	cx->cxhdl.priv = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
754b285192aSMauro Carvalho Chehab 	ret = cx2341x_handler_init(&cx->cxhdl, 50);
755b285192aSMauro Carvalho Chehab 	if (ret)
756b285192aSMauro Carvalho Chehab 		return ret;
757b285192aSMauro Carvalho Chehab 	cx->v4l2_dev.ctrl_handler = &cx->cxhdl.hdl;
758b285192aSMauro Carvalho Chehab 
759b285192aSMauro Carvalho Chehab 	cx->temporal_strength = cx->cxhdl.video_temporal_filter->cur.val;
760b285192aSMauro Carvalho Chehab 	cx->spatial_strength = cx->cxhdl.video_spatial_filter->cur.val;
761b285192aSMauro Carvalho Chehab 	cx->filter_mode = cx->cxhdl.video_spatial_filter_mode->cur.val |
762b285192aSMauro Carvalho Chehab 		(cx->cxhdl.video_temporal_filter_mode->cur.val << 1) |
763b285192aSMauro Carvalho Chehab 		(cx->cxhdl.video_median_filter_type->cur.val << 2);
764b285192aSMauro Carvalho Chehab 
765b285192aSMauro Carvalho Chehab 	init_waitqueue_head(&cx->cap_w);
766b285192aSMauro Carvalho Chehab 	init_waitqueue_head(&cx->mb_apu_waitq);
767b285192aSMauro Carvalho Chehab 	init_waitqueue_head(&cx->mb_cpu_waitq);
768b285192aSMauro Carvalho Chehab 	init_waitqueue_head(&cx->dma_waitq);
769b285192aSMauro Carvalho Chehab 
770b285192aSMauro Carvalho Chehab 	/* VBI */
771b285192aSMauro Carvalho Chehab 	cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
772b285192aSMauro Carvalho Chehab 	cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
773b285192aSMauro Carvalho Chehab 
774b285192aSMauro Carvalho Chehab 	/* IVTV style VBI insertion into MPEG streams */
775b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list);
776b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list);
777b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list);
778b285192aSMauro Carvalho Chehab 	list_add(&cx->vbi.sliced_mpeg_buf.list,
779b285192aSMauro Carvalho Chehab 		 &cx->vbi.sliced_mpeg_mdl.buf_list);
780b285192aSMauro Carvalho Chehab 	return 0;
781b285192aSMauro Carvalho Chehab }
782b285192aSMauro Carvalho Chehab 
783b285192aSMauro Carvalho Chehab /* Second initialization part. Here the card type has been
784b285192aSMauro Carvalho Chehab    autodetected. */
7854c62e976SGreg Kroah-Hartman static void cx18_init_struct2(struct cx18 *cx)
786b285192aSMauro Carvalho Chehab {
787b285192aSMauro Carvalho Chehab 	int i;
788b285192aSMauro Carvalho Chehab 
7892f9e682dSMauro Carvalho Chehab 	for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS - 1; i++)
790b285192aSMauro Carvalho Chehab 		if (cx->card->video_inputs[i].video_type == 0)
791b285192aSMauro Carvalho Chehab 			break;
792b285192aSMauro Carvalho Chehab 	cx->nof_inputs = i;
7932f9e682dSMauro Carvalho Chehab 	for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS - 1; i++)
794b285192aSMauro Carvalho Chehab 		if (cx->card->audio_inputs[i].audio_type == 0)
795b285192aSMauro Carvalho Chehab 			break;
796b285192aSMauro Carvalho Chehab 	cx->nof_audio_inputs = i;
797b285192aSMauro Carvalho Chehab 
798b285192aSMauro Carvalho Chehab 	/* Find tuner input */
799b285192aSMauro Carvalho Chehab 	for (i = 0; i < cx->nof_inputs; i++) {
800b285192aSMauro Carvalho Chehab 		if (cx->card->video_inputs[i].video_type ==
801b285192aSMauro Carvalho Chehab 				CX18_CARD_INPUT_VID_TUNER)
802b285192aSMauro Carvalho Chehab 			break;
803b285192aSMauro Carvalho Chehab 	}
804b285192aSMauro Carvalho Chehab 	if (i == cx->nof_inputs)
805b285192aSMauro Carvalho Chehab 		i = 0;
806b285192aSMauro Carvalho Chehab 	cx->active_input = i;
807b285192aSMauro Carvalho Chehab 	cx->audio_input = cx->card->video_inputs[i].audio_index;
808b285192aSMauro Carvalho Chehab }
809b285192aSMauro Carvalho Chehab 
810b285192aSMauro Carvalho Chehab static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
811b285192aSMauro Carvalho Chehab 			  const struct pci_device_id *pci_id)
812b285192aSMauro Carvalho Chehab {
813b285192aSMauro Carvalho Chehab 	u16 cmd;
814b285192aSMauro Carvalho Chehab 	unsigned char pci_latency;
815b285192aSMauro Carvalho Chehab 
816b285192aSMauro Carvalho Chehab 	CX18_DEBUG_INFO("Enabling pci device\n");
817b285192aSMauro Carvalho Chehab 
818b285192aSMauro Carvalho Chehab 	if (pci_enable_device(pci_dev)) {
819b285192aSMauro Carvalho Chehab 		CX18_ERR("Can't enable device %d!\n", cx->instance);
820b285192aSMauro Carvalho Chehab 		return -EIO;
821b285192aSMauro Carvalho Chehab 	}
822b285192aSMauro Carvalho Chehab 	if (pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32))) {
823b285192aSMauro Carvalho Chehab 		CX18_ERR("No suitable DMA available, card %d\n", cx->instance);
824b285192aSMauro Carvalho Chehab 		return -EIO;
825b285192aSMauro Carvalho Chehab 	}
826b285192aSMauro Carvalho Chehab 	if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
827b285192aSMauro Carvalho Chehab 		CX18_ERR("Cannot request encoder memory region, card %d\n",
828b285192aSMauro Carvalho Chehab 			 cx->instance);
829b285192aSMauro Carvalho Chehab 		return -EIO;
830b285192aSMauro Carvalho Chehab 	}
831b285192aSMauro Carvalho Chehab 
832b285192aSMauro Carvalho Chehab 	/* Enable bus mastering and memory mapped IO for the CX23418 */
833b285192aSMauro Carvalho Chehab 	pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);
834b285192aSMauro Carvalho Chehab 	cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
835b285192aSMauro Carvalho Chehab 	pci_write_config_word(pci_dev, PCI_COMMAND, cmd);
836b285192aSMauro Carvalho Chehab 
837b285192aSMauro Carvalho Chehab 	cx->card_rev = pci_dev->revision;
838b285192aSMauro Carvalho Chehab 	pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
839b285192aSMauro Carvalho Chehab 
840b285192aSMauro Carvalho Chehab 	if (pci_latency < 64 && cx18_pci_latency) {
841b285192aSMauro Carvalho Chehab 		CX18_INFO("Unreasonably low latency timer, "
842b285192aSMauro Carvalho Chehab 			       "setting to 64 (was %d)\n", pci_latency);
843b285192aSMauro Carvalho Chehab 		pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);
844b285192aSMauro Carvalho Chehab 		pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
845b285192aSMauro Carvalho Chehab 	}
846b285192aSMauro Carvalho Chehab 
847b285192aSMauro Carvalho Chehab 	CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
848b285192aSMauro Carvalho Chehab 		   "irq: %d, latency: %d, memory: 0x%llx\n",
849b285192aSMauro Carvalho Chehab 		   cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,
850b285192aSMauro Carvalho Chehab 		   PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),
851b285192aSMauro Carvalho Chehab 		   cx->pci_dev->irq, pci_latency, (u64)cx->base_addr);
852b285192aSMauro Carvalho Chehab 
853b285192aSMauro Carvalho Chehab 	return 0;
854b285192aSMauro Carvalho Chehab }
855b285192aSMauro Carvalho Chehab 
856b285192aSMauro Carvalho Chehab static void cx18_init_subdevs(struct cx18 *cx)
857b285192aSMauro Carvalho Chehab {
858b285192aSMauro Carvalho Chehab 	u32 hw = cx->card->hw_all;
859b285192aSMauro Carvalho Chehab 	u32 device;
860b285192aSMauro Carvalho Chehab 	int i;
861b285192aSMauro Carvalho Chehab 
862b285192aSMauro Carvalho Chehab 	for (i = 0, device = 1; i < 32; i++, device <<= 1) {
863b285192aSMauro Carvalho Chehab 
864b285192aSMauro Carvalho Chehab 		if (!(device & hw))
865b285192aSMauro Carvalho Chehab 			continue;
866b285192aSMauro Carvalho Chehab 
867b285192aSMauro Carvalho Chehab 		switch (device) {
868b285192aSMauro Carvalho Chehab 		case CX18_HW_DVB:
869b285192aSMauro Carvalho Chehab 		case CX18_HW_TVEEPROM:
870b285192aSMauro Carvalho Chehab 			/* These subordinate devices do not use probing */
871b285192aSMauro Carvalho Chehab 			cx->hw_flags |= device;
872b285192aSMauro Carvalho Chehab 			break;
873b285192aSMauro Carvalho Chehab 		case CX18_HW_418_AV:
874b285192aSMauro Carvalho Chehab 			/* The A/V decoder gets probed earlier to set PLLs */
875b285192aSMauro Carvalho Chehab 			/* Just note that the card uses it (i.e. has analog) */
876b285192aSMauro Carvalho Chehab 			cx->hw_flags |= device;
877b285192aSMauro Carvalho Chehab 			break;
878b285192aSMauro Carvalho Chehab 		case CX18_HW_GPIO_RESET_CTRL:
879b285192aSMauro Carvalho Chehab 			/*
880b285192aSMauro Carvalho Chehab 			 * The Reset Controller gets probed and added to
881b285192aSMauro Carvalho Chehab 			 * hw_flags earlier for i2c adapter/bus initialization
882b285192aSMauro Carvalho Chehab 			 */
883b285192aSMauro Carvalho Chehab 			break;
884b285192aSMauro Carvalho Chehab 		case CX18_HW_GPIO_MUX:
885b285192aSMauro Carvalho Chehab 			if (cx18_gpio_register(cx, device) == 0)
886b285192aSMauro Carvalho Chehab 				cx->hw_flags |= device;
887b285192aSMauro Carvalho Chehab 			break;
888b285192aSMauro Carvalho Chehab 		default:
889b285192aSMauro Carvalho Chehab 			if (cx18_i2c_register(cx, i) == 0)
890b285192aSMauro Carvalho Chehab 				cx->hw_flags |= device;
891b285192aSMauro Carvalho Chehab 			break;
892b285192aSMauro Carvalho Chehab 		}
893b285192aSMauro Carvalho Chehab 	}
894b285192aSMauro Carvalho Chehab 
895b285192aSMauro Carvalho Chehab 	if (cx->hw_flags & CX18_HW_418_AV)
896b285192aSMauro Carvalho Chehab 		cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV);
897b285192aSMauro Carvalho Chehab 
898b285192aSMauro Carvalho Chehab 	if (cx->card->hw_muxer != 0)
899b285192aSMauro Carvalho Chehab 		cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer);
900b285192aSMauro Carvalho Chehab }
901b285192aSMauro Carvalho Chehab 
9024c62e976SGreg Kroah-Hartman static int cx18_probe(struct pci_dev *pci_dev,
903b285192aSMauro Carvalho Chehab 		      const struct pci_device_id *pci_id)
904b285192aSMauro Carvalho Chehab {
905b285192aSMauro Carvalho Chehab 	int retval = 0;
906b285192aSMauro Carvalho Chehab 	int i;
907b285192aSMauro Carvalho Chehab 	u32 devtype;
908b285192aSMauro Carvalho Chehab 	struct cx18 *cx;
909b285192aSMauro Carvalho Chehab 
910b285192aSMauro Carvalho Chehab 	/* FIXME - module parameter arrays constrain max instances */
911b285192aSMauro Carvalho Chehab 	i = atomic_inc_return(&cx18_instance) - 1;
912b285192aSMauro Carvalho Chehab 	if (i >= CX18_MAX_CARDS) {
913b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "cx18: cannot manage card %d, driver has a "
914b285192aSMauro Carvalho Chehab 		       "limit of 0 - %d\n", i, CX18_MAX_CARDS - 1);
915b285192aSMauro Carvalho Chehab 		return -ENOMEM;
916b285192aSMauro Carvalho Chehab 	}
917b285192aSMauro Carvalho Chehab 
918b285192aSMauro Carvalho Chehab 	cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
919b285192aSMauro Carvalho Chehab 	if (cx == NULL) {
920b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n",
921b285192aSMauro Carvalho Chehab 		       i);
922b285192aSMauro Carvalho Chehab 		return -ENOMEM;
923b285192aSMauro Carvalho Chehab 	}
924b285192aSMauro Carvalho Chehab 	cx->pci_dev = pci_dev;
925b285192aSMauro Carvalho Chehab 	cx->instance = i;
926b285192aSMauro Carvalho Chehab 
927b285192aSMauro Carvalho Chehab 	retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);
928b285192aSMauro Carvalho Chehab 	if (retval) {
929b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "cx18: v4l2_device_register of card %d failed"
930b285192aSMauro Carvalho Chehab 		       "\n", cx->instance);
931b285192aSMauro Carvalho Chehab 		kfree(cx);
932b285192aSMauro Carvalho Chehab 		return retval;
933b285192aSMauro Carvalho Chehab 	}
934b285192aSMauro Carvalho Chehab 	snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d",
935b285192aSMauro Carvalho Chehab 		 cx->instance);
936b285192aSMauro Carvalho Chehab 	CX18_INFO("Initializing card %d\n", cx->instance);
937b285192aSMauro Carvalho Chehab 
938b285192aSMauro Carvalho Chehab 	cx18_process_options(cx);
939b285192aSMauro Carvalho Chehab 	if (cx->options.cardtype == -1) {
940b285192aSMauro Carvalho Chehab 		retval = -ENODEV;
941b285192aSMauro Carvalho Chehab 		goto err;
942b285192aSMauro Carvalho Chehab 	}
943b285192aSMauro Carvalho Chehab 
944b285192aSMauro Carvalho Chehab 	retval = cx18_init_struct1(cx);
945b285192aSMauro Carvalho Chehab 	if (retval)
946b285192aSMauro Carvalho Chehab 		goto err;
947b285192aSMauro Carvalho Chehab 
948b285192aSMauro Carvalho Chehab 	CX18_DEBUG_INFO("base addr: 0x%llx\n", (u64)cx->base_addr);
949b285192aSMauro Carvalho Chehab 
950b285192aSMauro Carvalho Chehab 	/* PCI Device Setup */
951b285192aSMauro Carvalho Chehab 	retval = cx18_setup_pci(cx, pci_dev, pci_id);
952b285192aSMauro Carvalho Chehab 	if (retval != 0)
953b285192aSMauro Carvalho Chehab 		goto free_workqueues;
954b285192aSMauro Carvalho Chehab 
955b285192aSMauro Carvalho Chehab 	/* map io memory */
956b285192aSMauro Carvalho Chehab 	CX18_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
957b285192aSMauro Carvalho Chehab 		   (u64)cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
958b285192aSMauro Carvalho Chehab 	cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
959b285192aSMauro Carvalho Chehab 				       CX18_MEM_SIZE);
960b285192aSMauro Carvalho Chehab 	if (!cx->enc_mem) {
961b285192aSMauro Carvalho Chehab 		CX18_ERR("ioremap failed. Can't get a window into CX23418 "
962b285192aSMauro Carvalho Chehab 			 "memory and register space\n");
963b285192aSMauro Carvalho Chehab 		CX18_ERR("Each capture card with a CX23418 needs 64 MB of "
964b285192aSMauro Carvalho Chehab 			 "vmalloc address space for the window\n");
965b285192aSMauro Carvalho Chehab 		CX18_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
966b285192aSMauro Carvalho Chehab 		CX18_ERR("Use the vmalloc= kernel command line option to set "
967b285192aSMauro Carvalho Chehab 			 "VmallocTotal to a larger value\n");
968b285192aSMauro Carvalho Chehab 		retval = -ENOMEM;
969b285192aSMauro Carvalho Chehab 		goto free_mem;
970b285192aSMauro Carvalho Chehab 	}
971b285192aSMauro Carvalho Chehab 	cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
972b285192aSMauro Carvalho Chehab 	devtype = cx18_read_reg(cx, 0xC72028);
973b285192aSMauro Carvalho Chehab 	switch (devtype & 0xff000000) {
974b285192aSMauro Carvalho Chehab 	case 0xff000000:
975b285192aSMauro Carvalho Chehab 		CX18_INFO("cx23418 revision %08x (A)\n", devtype);
976b285192aSMauro Carvalho Chehab 		break;
977b285192aSMauro Carvalho Chehab 	case 0x01000000:
978b285192aSMauro Carvalho Chehab 		CX18_INFO("cx23418 revision %08x (B)\n", devtype);
979b285192aSMauro Carvalho Chehab 		break;
980b285192aSMauro Carvalho Chehab 	default:
981b285192aSMauro Carvalho Chehab 		CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
982b285192aSMauro Carvalho Chehab 		break;
983b285192aSMauro Carvalho Chehab 	}
984b285192aSMauro Carvalho Chehab 
985b285192aSMauro Carvalho Chehab 	cx18_init_power(cx, 1);
986b285192aSMauro Carvalho Chehab 	cx18_init_memory(cx);
987b285192aSMauro Carvalho Chehab 
988b285192aSMauro Carvalho Chehab 	cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
989b285192aSMauro Carvalho Chehab 	cx18_init_scb(cx);
990b285192aSMauro Carvalho Chehab 
991b285192aSMauro Carvalho Chehab 	cx18_gpio_init(cx);
992b285192aSMauro Carvalho Chehab 
993b285192aSMauro Carvalho Chehab 	/* Initialize integrated A/V decoder early to set PLLs, just in case */
994b285192aSMauro Carvalho Chehab 	retval = cx18_av_probe(cx);
995b285192aSMauro Carvalho Chehab 	if (retval) {
996b285192aSMauro Carvalho Chehab 		CX18_ERR("Could not register A/V decoder subdevice\n");
997b285192aSMauro Carvalho Chehab 		goto free_map;
998b285192aSMauro Carvalho Chehab 	}
999b285192aSMauro Carvalho Chehab 
1000b285192aSMauro Carvalho Chehab 	/* Initialize GPIO Reset Controller to do chip resets during i2c init */
1001b285192aSMauro Carvalho Chehab 	if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) {
1002b285192aSMauro Carvalho Chehab 		if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0)
1003b285192aSMauro Carvalho Chehab 			CX18_WARN("Could not register GPIO reset controller"
1004b285192aSMauro Carvalho Chehab 				  "subdevice; proceeding anyway.\n");
1005b285192aSMauro Carvalho Chehab 		else
1006b285192aSMauro Carvalho Chehab 			cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL;
1007b285192aSMauro Carvalho Chehab 	}
1008b285192aSMauro Carvalho Chehab 
1009b285192aSMauro Carvalho Chehab 	/* active i2c  */
1010b285192aSMauro Carvalho Chehab 	CX18_DEBUG_INFO("activating i2c...\n");
1011b285192aSMauro Carvalho Chehab 	retval = init_cx18_i2c(cx);
1012b285192aSMauro Carvalho Chehab 	if (retval) {
1013b285192aSMauro Carvalho Chehab 		CX18_ERR("Could not initialize i2c\n");
1014b285192aSMauro Carvalho Chehab 		goto free_map;
1015b285192aSMauro Carvalho Chehab 	}
1016b285192aSMauro Carvalho Chehab 
1017b285192aSMauro Carvalho Chehab 	if (cx->card->hw_all & CX18_HW_TVEEPROM) {
1018b285192aSMauro Carvalho Chehab 		/* Based on the model number the cardtype may be changed.
1019b285192aSMauro Carvalho Chehab 		   The PCI IDs are not always reliable. */
1020b285192aSMauro Carvalho Chehab 		const struct cx18_card *orig_card = cx->card;
1021b285192aSMauro Carvalho Chehab 		cx18_process_eeprom(cx);
1022b285192aSMauro Carvalho Chehab 
1023b285192aSMauro Carvalho Chehab 		if (cx->card != orig_card) {
1024b285192aSMauro Carvalho Chehab 			/* Changed the cardtype; re-reset the I2C chips */
1025b285192aSMauro Carvalho Chehab 			cx18_gpio_init(cx);
1026b285192aSMauro Carvalho Chehab 			cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL,
1027b285192aSMauro Carvalho Chehab 					core, reset, (u32) CX18_GPIO_RESET_I2C);
1028b285192aSMauro Carvalho Chehab 		}
1029b285192aSMauro Carvalho Chehab 	}
1030b285192aSMauro Carvalho Chehab 	if (cx->card->comment)
1031b285192aSMauro Carvalho Chehab 		CX18_INFO("%s", cx->card->comment);
1032b285192aSMauro Carvalho Chehab 	if (cx->card->v4l2_capabilities == 0) {
1033b285192aSMauro Carvalho Chehab 		retval = -ENODEV;
1034b285192aSMauro Carvalho Chehab 		goto free_i2c;
1035b285192aSMauro Carvalho Chehab 	}
1036b285192aSMauro Carvalho Chehab 	cx18_init_memory(cx);
1037b285192aSMauro Carvalho Chehab 	cx18_init_scb(cx);
1038b285192aSMauro Carvalho Chehab 
1039b285192aSMauro Carvalho Chehab 	/* Register IRQ */
1040b285192aSMauro Carvalho Chehab 	retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
10419a373d17SMichael Opdenacker 			     IRQF_SHARED, cx->v4l2_dev.name, (void *)cx);
1042b285192aSMauro Carvalho Chehab 	if (retval) {
1043b285192aSMauro Carvalho Chehab 		CX18_ERR("Failed to register irq %d\n", retval);
1044b285192aSMauro Carvalho Chehab 		goto free_i2c;
1045b285192aSMauro Carvalho Chehab 	}
1046b285192aSMauro Carvalho Chehab 
1047b285192aSMauro Carvalho Chehab 	if (cx->std == 0)
1048b285192aSMauro Carvalho Chehab 		cx->std = V4L2_STD_NTSC_M;
1049b285192aSMauro Carvalho Chehab 
1050b285192aSMauro Carvalho Chehab 	if (cx->options.tuner == -1) {
1051b285192aSMauro Carvalho Chehab 		for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
1052b285192aSMauro Carvalho Chehab 			if ((cx->std & cx->card->tuners[i].std) == 0)
1053b285192aSMauro Carvalho Chehab 				continue;
1054b285192aSMauro Carvalho Chehab 			cx->options.tuner = cx->card->tuners[i].tuner;
1055b285192aSMauro Carvalho Chehab 			break;
1056b285192aSMauro Carvalho Chehab 		}
1057b285192aSMauro Carvalho Chehab 	}
1058b285192aSMauro Carvalho Chehab 	/* if no tuner was found, then pick the first tuner in the card list */
1059b285192aSMauro Carvalho Chehab 	if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
1060b285192aSMauro Carvalho Chehab 		cx->std = cx->card->tuners[0].std;
1061b285192aSMauro Carvalho Chehab 		if (cx->std & V4L2_STD_PAL)
1062b285192aSMauro Carvalho Chehab 			cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
1063b285192aSMauro Carvalho Chehab 		else if (cx->std & V4L2_STD_NTSC)
1064b285192aSMauro Carvalho Chehab 			cx->std = V4L2_STD_NTSC_M;
1065b285192aSMauro Carvalho Chehab 		else if (cx->std & V4L2_STD_SECAM)
1066b285192aSMauro Carvalho Chehab 			cx->std = V4L2_STD_SECAM_L;
1067b285192aSMauro Carvalho Chehab 		cx->options.tuner = cx->card->tuners[0].tuner;
1068b285192aSMauro Carvalho Chehab 	}
1069b285192aSMauro Carvalho Chehab 	if (cx->options.radio == -1)
1070b285192aSMauro Carvalho Chehab 		cx->options.radio = (cx->card->radio_input.audio_type != 0);
1071b285192aSMauro Carvalho Chehab 
1072b285192aSMauro Carvalho Chehab 	/* The card is now fully identified, continue with card-specific
1073b285192aSMauro Carvalho Chehab 	   initialization. */
1074b285192aSMauro Carvalho Chehab 	cx18_init_struct2(cx);
1075b285192aSMauro Carvalho Chehab 
1076b285192aSMauro Carvalho Chehab 	cx18_init_subdevs(cx);
1077b285192aSMauro Carvalho Chehab 
1078b285192aSMauro Carvalho Chehab 	if (cx->std & V4L2_STD_525_60)
1079b285192aSMauro Carvalho Chehab 		cx->is_60hz = 1;
1080b285192aSMauro Carvalho Chehab 	else
1081b285192aSMauro Carvalho Chehab 		cx->is_50hz = 1;
1082b285192aSMauro Carvalho Chehab 
1083b285192aSMauro Carvalho Chehab 	cx2341x_handler_set_50hz(&cx->cxhdl, !cx->is_60hz);
1084b285192aSMauro Carvalho Chehab 
1085b285192aSMauro Carvalho Chehab 	if (cx->options.radio > 0)
1086b285192aSMauro Carvalho Chehab 		cx->v4l2_cap |= V4L2_CAP_RADIO;
1087b285192aSMauro Carvalho Chehab 
1088b285192aSMauro Carvalho Chehab 	if (cx->options.tuner > -1) {
1089b285192aSMauro Carvalho Chehab 		struct tuner_setup setup;
1090b285192aSMauro Carvalho Chehab 
1091b285192aSMauro Carvalho Chehab 		setup.addr = ADDR_UNSET;
1092b285192aSMauro Carvalho Chehab 		setup.type = cx->options.tuner;
1093b285192aSMauro Carvalho Chehab 		setup.mode_mask = T_ANALOG_TV;  /* matches TV tuners */
10946a03dc92SHans Verkuil 		setup.config = NULL;
1095b285192aSMauro Carvalho Chehab 		if (cx->options.radio > 0)
1096b285192aSMauro Carvalho Chehab 			setup.mode_mask |= T_RADIO;
1097b285192aSMauro Carvalho Chehab 		setup.tuner_callback = (setup.type == TUNER_XC2028) ?
1098b285192aSMauro Carvalho Chehab 			cx18_reset_tuner_gpio : NULL;
1099b285192aSMauro Carvalho Chehab 		cx18_call_all(cx, tuner, s_type_addr, &setup);
1100b285192aSMauro Carvalho Chehab 		if (setup.type == TUNER_XC2028) {
1101b285192aSMauro Carvalho Chehab 			static struct xc2028_ctrl ctrl = {
1102b285192aSMauro Carvalho Chehab 				.fname = XC2028_DEFAULT_FIRMWARE,
1103b285192aSMauro Carvalho Chehab 				.max_len = 64,
1104b285192aSMauro Carvalho Chehab 			};
1105b285192aSMauro Carvalho Chehab 			struct v4l2_priv_tun_config cfg = {
1106b285192aSMauro Carvalho Chehab 				.tuner = cx->options.tuner,
1107b285192aSMauro Carvalho Chehab 				.priv = &ctrl,
1108b285192aSMauro Carvalho Chehab 			};
1109b285192aSMauro Carvalho Chehab 			cx18_call_all(cx, tuner, s_config, &cfg);
1110b285192aSMauro Carvalho Chehab 		}
1111b285192aSMauro Carvalho Chehab 	}
1112b285192aSMauro Carvalho Chehab 
1113b285192aSMauro Carvalho Chehab 	/* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
1114b285192aSMauro Carvalho Chehab 	   are not. */
1115b285192aSMauro Carvalho Chehab 	cx->tuner_std = cx->std;
1116b285192aSMauro Carvalho Chehab 	if (cx->std == V4L2_STD_ALL)
1117b285192aSMauro Carvalho Chehab 		cx->std = V4L2_STD_NTSC_M;
1118b285192aSMauro Carvalho Chehab 
1119b285192aSMauro Carvalho Chehab 	retval = cx18_streams_setup(cx);
1120b285192aSMauro Carvalho Chehab 	if (retval) {
1121b285192aSMauro Carvalho Chehab 		CX18_ERR("Error %d setting up streams\n", retval);
1122b285192aSMauro Carvalho Chehab 		goto free_irq;
1123b285192aSMauro Carvalho Chehab 	}
1124b285192aSMauro Carvalho Chehab 	retval = cx18_streams_register(cx);
1125b285192aSMauro Carvalho Chehab 	if (retval) {
1126b285192aSMauro Carvalho Chehab 		CX18_ERR("Error %d registering devices\n", retval);
1127b285192aSMauro Carvalho Chehab 		goto free_streams;
1128b285192aSMauro Carvalho Chehab 	}
1129b285192aSMauro Carvalho Chehab 
1130b285192aSMauro Carvalho Chehab 	CX18_INFO("Initialized card: %s\n", cx->card_name);
1131b285192aSMauro Carvalho Chehab 
1132b285192aSMauro Carvalho Chehab 	/* Load cx18 submodules (cx18-alsa) */
1133b285192aSMauro Carvalho Chehab 	request_modules(cx);
1134b285192aSMauro Carvalho Chehab 	return 0;
1135b285192aSMauro Carvalho Chehab 
1136b285192aSMauro Carvalho Chehab free_streams:
1137b285192aSMauro Carvalho Chehab 	cx18_streams_cleanup(cx, 1);
1138b285192aSMauro Carvalho Chehab free_irq:
1139b285192aSMauro Carvalho Chehab 	free_irq(cx->pci_dev->irq, (void *)cx);
1140b285192aSMauro Carvalho Chehab free_i2c:
1141b285192aSMauro Carvalho Chehab 	exit_cx18_i2c(cx);
1142b285192aSMauro Carvalho Chehab free_map:
1143b285192aSMauro Carvalho Chehab 	cx18_iounmap(cx);
1144b285192aSMauro Carvalho Chehab free_mem:
1145b285192aSMauro Carvalho Chehab 	release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1146b285192aSMauro Carvalho Chehab free_workqueues:
1147b285192aSMauro Carvalho Chehab 	destroy_workqueue(cx->in_work_queue);
1148b285192aSMauro Carvalho Chehab err:
1149b285192aSMauro Carvalho Chehab 	if (retval == 0)
1150b285192aSMauro Carvalho Chehab 		retval = -ENODEV;
1151b285192aSMauro Carvalho Chehab 	CX18_ERR("Error %d on initialization\n", retval);
1152b285192aSMauro Carvalho Chehab 
1153b285192aSMauro Carvalho Chehab 	v4l2_device_unregister(&cx->v4l2_dev);
1154b285192aSMauro Carvalho Chehab 	kfree(cx);
1155b285192aSMauro Carvalho Chehab 	return retval;
1156b285192aSMauro Carvalho Chehab }
1157b285192aSMauro Carvalho Chehab 
1158b285192aSMauro Carvalho Chehab int cx18_init_on_first_open(struct cx18 *cx)
1159b285192aSMauro Carvalho Chehab {
1160b285192aSMauro Carvalho Chehab 	int video_input;
1161b285192aSMauro Carvalho Chehab 	int fw_retry_count = 3;
1162b285192aSMauro Carvalho Chehab 	struct v4l2_frequency vf;
1163b285192aSMauro Carvalho Chehab 	struct cx18_open_id fh;
1164b285192aSMauro Carvalho Chehab 	v4l2_std_id std;
1165b285192aSMauro Carvalho Chehab 
1166b285192aSMauro Carvalho Chehab 	fh.cx = cx;
1167b285192aSMauro Carvalho Chehab 
1168b285192aSMauro Carvalho Chehab 	if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
1169b285192aSMauro Carvalho Chehab 		return -ENXIO;
1170b285192aSMauro Carvalho Chehab 
1171b285192aSMauro Carvalho Chehab 	if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
1172b285192aSMauro Carvalho Chehab 		return 0;
1173b285192aSMauro Carvalho Chehab 
1174b285192aSMauro Carvalho Chehab 	while (--fw_retry_count > 0) {
1175b285192aSMauro Carvalho Chehab 		/* load firmware */
1176b285192aSMauro Carvalho Chehab 		if (cx18_firmware_init(cx) == 0)
1177b285192aSMauro Carvalho Chehab 			break;
1178b285192aSMauro Carvalho Chehab 		if (fw_retry_count > 1)
1179b285192aSMauro Carvalho Chehab 			CX18_WARN("Retry loading firmware\n");
1180b285192aSMauro Carvalho Chehab 	}
1181b285192aSMauro Carvalho Chehab 
1182b285192aSMauro Carvalho Chehab 	if (fw_retry_count == 0) {
1183b285192aSMauro Carvalho Chehab 		set_bit(CX18_F_I_FAILED, &cx->i_flags);
1184b285192aSMauro Carvalho Chehab 		return -ENXIO;
1185b285192aSMauro Carvalho Chehab 	}
1186b285192aSMauro Carvalho Chehab 	set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
1187b285192aSMauro Carvalho Chehab 
1188b285192aSMauro Carvalho Chehab 	/*
1189b285192aSMauro Carvalho Chehab 	 * Init the firmware twice to work around a silicon bug
1190b285192aSMauro Carvalho Chehab 	 * with the digital TS.
1191b285192aSMauro Carvalho Chehab 	 *
1192b285192aSMauro Carvalho Chehab 	 * The second firmware load requires us to normalize the APU state,
1193b285192aSMauro Carvalho Chehab 	 * or the audio for the first analog capture will be badly incorrect.
1194b285192aSMauro Carvalho Chehab 	 *
1195b285192aSMauro Carvalho Chehab 	 * I can't seem to call APU_RESETAI and have it succeed without the
1196b285192aSMauro Carvalho Chehab 	 * APU capturing audio, so we start and stop it here to do the reset
1197b285192aSMauro Carvalho Chehab 	 */
1198b285192aSMauro Carvalho Chehab 
1199b285192aSMauro Carvalho Chehab 	/* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1200b285192aSMauro Carvalho Chehab 	cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1201b285192aSMauro Carvalho Chehab 	cx18_vapi(cx, CX18_APU_RESETAI, 0);
1202b285192aSMauro Carvalho Chehab 	cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1203b285192aSMauro Carvalho Chehab 
1204b285192aSMauro Carvalho Chehab 	fw_retry_count = 3;
1205b285192aSMauro Carvalho Chehab 	while (--fw_retry_count > 0) {
1206b285192aSMauro Carvalho Chehab 		/* load firmware */
1207b285192aSMauro Carvalho Chehab 		if (cx18_firmware_init(cx) == 0)
1208b285192aSMauro Carvalho Chehab 			break;
1209b285192aSMauro Carvalho Chehab 		if (fw_retry_count > 1)
1210b285192aSMauro Carvalho Chehab 			CX18_WARN("Retry loading firmware\n");
1211b285192aSMauro Carvalho Chehab 	}
1212b285192aSMauro Carvalho Chehab 
1213b285192aSMauro Carvalho Chehab 	if (fw_retry_count == 0) {
1214b285192aSMauro Carvalho Chehab 		set_bit(CX18_F_I_FAILED, &cx->i_flags);
1215b285192aSMauro Carvalho Chehab 		return -ENXIO;
1216b285192aSMauro Carvalho Chehab 	}
1217b285192aSMauro Carvalho Chehab 
1218b285192aSMauro Carvalho Chehab 	/*
1219b285192aSMauro Carvalho Chehab 	 * The second firmware load requires us to normalize the APU state,
1220b285192aSMauro Carvalho Chehab 	 * or the audio for the first analog capture will be badly incorrect.
1221b285192aSMauro Carvalho Chehab 	 *
1222b285192aSMauro Carvalho Chehab 	 * I can't seem to call APU_RESETAI and have it succeed without the
1223b285192aSMauro Carvalho Chehab 	 * APU capturing audio, so we start and stop it here to do the reset
1224b285192aSMauro Carvalho Chehab 	 */
1225b285192aSMauro Carvalho Chehab 
1226b285192aSMauro Carvalho Chehab 	/* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1227b285192aSMauro Carvalho Chehab 	cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1228b285192aSMauro Carvalho Chehab 	cx18_vapi(cx, CX18_APU_RESETAI, 0);
1229b285192aSMauro Carvalho Chehab 	cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1230b285192aSMauro Carvalho Chehab 
1231b285192aSMauro Carvalho Chehab 	/* Init the A/V decoder, if it hasn't been already */
1232b285192aSMauro Carvalho Chehab 	v4l2_subdev_call(cx->sd_av, core, load_fw);
1233b285192aSMauro Carvalho Chehab 
1234b285192aSMauro Carvalho Chehab 	vf.tuner = 0;
1235b285192aSMauro Carvalho Chehab 	vf.type = V4L2_TUNER_ANALOG_TV;
1236b285192aSMauro Carvalho Chehab 	vf.frequency = 6400; /* the tuner 'baseline' frequency */
1237b285192aSMauro Carvalho Chehab 
1238b285192aSMauro Carvalho Chehab 	/* Set initial frequency. For PAL/SECAM broadcasts no
1239b285192aSMauro Carvalho Chehab 	   'default' channel exists AFAIK. */
1240b285192aSMauro Carvalho Chehab 	if (cx->std == V4L2_STD_NTSC_M_JP)
1241b285192aSMauro Carvalho Chehab 		vf.frequency = 1460;	/* ch. 1 91250*16/1000 */
1242b285192aSMauro Carvalho Chehab 	else if (cx->std & V4L2_STD_NTSC_M)
1243b285192aSMauro Carvalho Chehab 		vf.frequency = 1076;	/* ch. 4 67250*16/1000 */
1244b285192aSMauro Carvalho Chehab 
1245b285192aSMauro Carvalho Chehab 	video_input = cx->active_input;
1246b285192aSMauro Carvalho Chehab 	cx->active_input++;	/* Force update of input */
1247b285192aSMauro Carvalho Chehab 	cx18_s_input(NULL, &fh, video_input);
1248b285192aSMauro Carvalho Chehab 
1249b285192aSMauro Carvalho Chehab 	/* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1250b285192aSMauro Carvalho Chehab 	   in one place. */
1251b285192aSMauro Carvalho Chehab 	cx->std++;		/* Force full standard initialization */
1252b285192aSMauro Carvalho Chehab 	std = (cx->tuner_std == V4L2_STD_ALL) ? V4L2_STD_NTSC_M : cx->tuner_std;
1253314527acSHans Verkuil 	cx18_s_std(NULL, &fh, std);
1254b285192aSMauro Carvalho Chehab 	cx18_s_frequency(NULL, &fh, &vf);
1255b285192aSMauro Carvalho Chehab 	return 0;
1256b285192aSMauro Carvalho Chehab }
1257b285192aSMauro Carvalho Chehab 
1258b285192aSMauro Carvalho Chehab static void cx18_cancel_in_work_orders(struct cx18 *cx)
1259b285192aSMauro Carvalho Chehab {
1260b285192aSMauro Carvalho Chehab 	int i;
1261b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++)
1262b285192aSMauro Carvalho Chehab 		cancel_work_sync(&cx->in_work_order[i].work);
1263b285192aSMauro Carvalho Chehab }
1264b285192aSMauro Carvalho Chehab 
1265b285192aSMauro Carvalho Chehab static void cx18_cancel_out_work_orders(struct cx18 *cx)
1266b285192aSMauro Carvalho Chehab {
1267b285192aSMauro Carvalho Chehab 	int i;
1268b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX18_MAX_STREAMS; i++)
1269b285192aSMauro Carvalho Chehab 		if (&cx->streams[i].video_dev != NULL)
1270b285192aSMauro Carvalho Chehab 			cancel_work_sync(&cx->streams[i].out_work_order);
1271b285192aSMauro Carvalho Chehab }
1272b285192aSMauro Carvalho Chehab 
1273b285192aSMauro Carvalho Chehab static void cx18_remove(struct pci_dev *pci_dev)
1274b285192aSMauro Carvalho Chehab {
1275b285192aSMauro Carvalho Chehab 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1276b285192aSMauro Carvalho Chehab 	struct cx18 *cx = to_cx18(v4l2_dev);
1277b285192aSMauro Carvalho Chehab 	int i;
1278b285192aSMauro Carvalho Chehab 
1279b285192aSMauro Carvalho Chehab 	CX18_DEBUG_INFO("Removing Card\n");
1280b285192aSMauro Carvalho Chehab 
1281b285192aSMauro Carvalho Chehab 	flush_request_modules(cx);
1282b285192aSMauro Carvalho Chehab 
1283b285192aSMauro Carvalho Chehab 	/* Stop all captures */
1284b285192aSMauro Carvalho Chehab 	CX18_DEBUG_INFO("Stopping all streams\n");
1285b285192aSMauro Carvalho Chehab 	if (atomic_read(&cx->tot_capturing) > 0)
1286b285192aSMauro Carvalho Chehab 		cx18_stop_all_captures(cx);
1287b285192aSMauro Carvalho Chehab 
1288b285192aSMauro Carvalho Chehab 	/* Stop interrupts that cause incoming work to be queued */
1289b285192aSMauro Carvalho Chehab 	cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
1290b285192aSMauro Carvalho Chehab 
1291b285192aSMauro Carvalho Chehab 	/* Incoming work can cause outgoing work, so clean up incoming first */
1292b285192aSMauro Carvalho Chehab 	cx18_cancel_in_work_orders(cx);
1293b285192aSMauro Carvalho Chehab 	cx18_cancel_out_work_orders(cx);
1294b285192aSMauro Carvalho Chehab 
1295b285192aSMauro Carvalho Chehab 	/* Stop ack interrupts that may have been needed for work to finish */
1296b285192aSMauro Carvalho Chehab 	cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
1297b285192aSMauro Carvalho Chehab 
1298b285192aSMauro Carvalho Chehab 	cx18_halt_firmware(cx);
1299b285192aSMauro Carvalho Chehab 
1300b285192aSMauro Carvalho Chehab 	destroy_workqueue(cx->in_work_queue);
1301b285192aSMauro Carvalho Chehab 
1302b285192aSMauro Carvalho Chehab 	cx18_streams_cleanup(cx, 1);
1303b285192aSMauro Carvalho Chehab 
1304b285192aSMauro Carvalho Chehab 	exit_cx18_i2c(cx);
1305b285192aSMauro Carvalho Chehab 
1306b285192aSMauro Carvalho Chehab 	free_irq(cx->pci_dev->irq, (void *)cx);
1307b285192aSMauro Carvalho Chehab 
1308b285192aSMauro Carvalho Chehab 	cx18_iounmap(cx);
1309b285192aSMauro Carvalho Chehab 
1310b285192aSMauro Carvalho Chehab 	release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1311b285192aSMauro Carvalho Chehab 
1312b285192aSMauro Carvalho Chehab 	pci_disable_device(cx->pci_dev);
1313b285192aSMauro Carvalho Chehab 
1314b285192aSMauro Carvalho Chehab 	if (cx->vbi.sliced_mpeg_data[0] != NULL)
1315b285192aSMauro Carvalho Chehab 		for (i = 0; i < CX18_VBI_FRAMES; i++)
1316b285192aSMauro Carvalho Chehab 			kfree(cx->vbi.sliced_mpeg_data[i]);
1317b285192aSMauro Carvalho Chehab 
1318b285192aSMauro Carvalho Chehab 	v4l2_ctrl_handler_free(&cx->av_state.hdl);
1319b285192aSMauro Carvalho Chehab 
1320b285192aSMauro Carvalho Chehab 	CX18_INFO("Removed %s\n", cx->card_name);
1321b285192aSMauro Carvalho Chehab 
1322b285192aSMauro Carvalho Chehab 	v4l2_device_unregister(v4l2_dev);
1323b285192aSMauro Carvalho Chehab 	kfree(cx);
1324b285192aSMauro Carvalho Chehab }
1325b285192aSMauro Carvalho Chehab 
1326b285192aSMauro Carvalho Chehab 
1327b285192aSMauro Carvalho Chehab /* define a pci_driver for card detection */
1328b285192aSMauro Carvalho Chehab static struct pci_driver cx18_pci_driver = {
1329b285192aSMauro Carvalho Chehab       .name =     "cx18",
1330b285192aSMauro Carvalho Chehab       .id_table = cx18_pci_tbl,
1331b285192aSMauro Carvalho Chehab       .probe =    cx18_probe,
1332b285192aSMauro Carvalho Chehab       .remove =   cx18_remove,
1333b285192aSMauro Carvalho Chehab };
1334b285192aSMauro Carvalho Chehab 
1335b285192aSMauro Carvalho Chehab static int __init module_start(void)
1336b285192aSMauro Carvalho Chehab {
1337b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "cx18:  Start initialization, version %s\n",
1338b285192aSMauro Carvalho Chehab 	       CX18_VERSION);
1339b285192aSMauro Carvalho Chehab 
1340b285192aSMauro Carvalho Chehab 	/* Validate parameters */
1341b285192aSMauro Carvalho Chehab 	if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
1342b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "cx18:  Exiting, cx18_first_minor must be between 0 and %d\n",
1343b285192aSMauro Carvalho Chehab 		     CX18_MAX_CARDS - 1);
1344b285192aSMauro Carvalho Chehab 		return -1;
1345b285192aSMauro Carvalho Chehab 	}
1346b285192aSMauro Carvalho Chehab 
1347b285192aSMauro Carvalho Chehab 	if (cx18_debug < 0 || cx18_debug > 511) {
1348b285192aSMauro Carvalho Chehab 		cx18_debug = 0;
1349b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "cx18:   Debug value must be >= 0 and <= 511!\n");
1350b285192aSMauro Carvalho Chehab 	}
1351b285192aSMauro Carvalho Chehab 
1352b285192aSMauro Carvalho Chehab 	if (pci_register_driver(&cx18_pci_driver)) {
1353b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "cx18:   Error detecting PCI card\n");
1354b285192aSMauro Carvalho Chehab 		return -ENODEV;
1355b285192aSMauro Carvalho Chehab 	}
1356b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "cx18:  End initialization\n");
1357b285192aSMauro Carvalho Chehab 	return 0;
1358b285192aSMauro Carvalho Chehab }
1359b285192aSMauro Carvalho Chehab 
1360b285192aSMauro Carvalho Chehab static void __exit module_cleanup(void)
1361b285192aSMauro Carvalho Chehab {
1362b285192aSMauro Carvalho Chehab 	pci_unregister_driver(&cx18_pci_driver);
1363b285192aSMauro Carvalho Chehab }
1364b285192aSMauro Carvalho Chehab 
1365b285192aSMauro Carvalho Chehab module_init(module_start);
1366b285192aSMauro Carvalho Chehab module_exit(module_cleanup);
1367b285192aSMauro Carvalho Chehab MODULE_FIRMWARE(XC2028_DEFAULT_FIRMWARE);
1368