blob: 319c459459e0c5a0fb0f3323d0780b59a8ec65c6 [file] [log] [blame]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001/*
Sri Deevib9255172009-03-04 17:49:01 -03002 cx231xx-cards.c - driver for Conexant Cx23100/101/102
3 USB video capture devices
Sri Deevie0d3baf2009-03-03 14:37:50 -03004
5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
Sri Deevib9255172009-03-04 17:49:01 -03006 Based on em28xx driver
Sri Deevie0d3baf2009-03-03 14:37:50 -03007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/delay.h>
26#include <linux/i2c.h>
27#include <linux/usb.h>
28#include <media/tuner.h>
29#include <media/tveeprom.h>
30#include <media/v4l2-common.h>
31#include <media/v4l2-chip-ident.h>
32
33#include <media/cx25840.h>
34#include "xc5000.h"
35
36#include "cx231xx.h"
37
38static int tuner = -1;
39module_param(tuner, int, 0444);
40MODULE_PARM_DESC(tuner, "tuner type");
41
42static unsigned int disable_ir;
43module_param(disable_ir, int, 0444);
44MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
45
46/* Bitmask marking allocated devices from 0 to CX231XX_MAXBOARDS */
47static unsigned long cx231xx_devused;
48
49/*
50 * Reset sequences for analog/digital modes
51 */
52
53static struct cx231xx_reg_seq RDE250_XCV_TUNER[] = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030054 {0x03, 0x01, 10},
55 {0x03, 0x00, 30},
56 {0x03, 0x01, 10},
57 {-1, -1, -1},
Sri Deevie0d3baf2009-03-03 14:37:50 -030058};
59
Sri Deevie0d3baf2009-03-03 14:37:50 -030060/*
61 * Board definitions
62 */
63struct cx231xx_board cx231xx_boards[] = {
Sri Deevie0d3baf2009-03-03 14:37:50 -030064 [CX231XX_BOARD_UNKNOWN] = {
Sri Deevi6e4f5742009-03-10 21:16:26 -030065 .name = "Unknown CX231xx video grabber",
66 .tuner_type = TUNER_ABSENT,
67 .input = {{
68 .type = CX231XX_VMUX_TELEVISION,
69 .vmux = CX231XX_VIN_3_1,
70 .amux = CX231XX_AMUX_VIDEO,
71 .gpio = 0,
72 }, {
73 .type = CX231XX_VMUX_COMPOSITE1,
74 .vmux = CX231XX_VIN_2_1,
75 .amux = CX231XX_AMUX_LINE_IN,
76 .gpio = 0,
77 }, {
78 .type = CX231XX_VMUX_SVIDEO,
79 .vmux = CX231XX_VIN_1_1 |
80 (CX231XX_VIN_1_2 << 8) |
81 CX25840_SVIDEO_ON,
82 .amux = CX231XX_AMUX_LINE_IN,
83 .gpio = 0,
84 }
85 },
86 },
Sri Deevie0d3baf2009-03-03 14:37:50 -030087 [CX231XX_BOARD_CNXT_RDE_250] = {
Sri Deevi6e4f5742009-03-10 21:16:26 -030088 .name = "Conexant Hybrid TV - RDE250",
89 .tuner_type = TUNER_XC5000,
90 .tuner_addr = 0x61,
91 .tuner_gpio = RDE250_XCV_TUNER,
92 .tuner_sif_gpio = 0x05,
93 .tuner_scl_gpio = 0x1a,
94 .tuner_sda_gpio = 0x1b,
95 .decoder = CX231XX_AVDECODER,
96 .demod_xfer_mode = 0,
97 .ctl_pin_status_mask = 0xFFFFFFC4,
98 .agc_analog_digital_select_gpio = 0x0c,
99 .gpio_pin_status_mask = 0x4001000,
100 .tuner_i2c_master = 1,
101 .demod_i2c_master = 2,
102 .has_dvb = 1,
103 .demod_addr = 0x02,
104 .norm = V4L2_STD_PAL,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300105
Sri Deevi6e4f5742009-03-10 21:16:26 -0300106 .input = {{
107 .type = CX231XX_VMUX_TELEVISION,
108 .vmux = CX231XX_VIN_3_1,
109 .amux = CX231XX_AMUX_VIDEO,
110 .gpio = 0,
111 }, {
112 .type = CX231XX_VMUX_COMPOSITE1,
113 .vmux = CX231XX_VIN_2_1,
114 .amux = CX231XX_AMUX_LINE_IN,
115 .gpio = 0,
116 }, {
117 .type = CX231XX_VMUX_SVIDEO,
118 .vmux = CX231XX_VIN_1_1 |
119 (CX231XX_VIN_1_2 << 8) |
120 CX25840_SVIDEO_ON,
121 .amux = CX231XX_AMUX_LINE_IN,
122 .gpio = 0,
123 }
124 },
125 },
Sri Deevie0d3baf2009-03-03 14:37:50 -0300126
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300127 [CX231XX_BOARD_CNXT_RDU_250] = {
Sri Deevi6e4f5742009-03-10 21:16:26 -0300128 .name = "Conexant Hybrid TV - RDU250",
129 .tuner_type = TUNER_XC5000,
130 .tuner_addr = 0x61,
131 .tuner_gpio = RDE250_XCV_TUNER,
132 .tuner_sif_gpio = 0x05,
133 .tuner_scl_gpio = 0x1a,
134 .tuner_sda_gpio = 0x1b,
135 .decoder = CX231XX_AVDECODER,
136 .demod_xfer_mode = 0,
137 .ctl_pin_status_mask = 0xFFFFFFC4,
138 .agc_analog_digital_select_gpio = 0x0c,
139 .gpio_pin_status_mask = 0x4001000,
140 .tuner_i2c_master = 1,
141 .demod_i2c_master = 2,
142 .has_dvb = 1,
143 .demod_addr = 0x32,
144 .norm = V4L2_STD_NTSC,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300145
Sri Deevi6e4f5742009-03-10 21:16:26 -0300146 .input = {{
147 .type = CX231XX_VMUX_TELEVISION,
148 .vmux = CX231XX_VIN_3_1,
149 .amux = CX231XX_AMUX_VIDEO,
150 .gpio = 0,
151 }, {
152 .type = CX231XX_VMUX_COMPOSITE1,
153 .vmux = CX231XX_VIN_2_1,
154 .amux = CX231XX_AMUX_LINE_IN,
155 .gpio = 0,
156 }, {
157 .type = CX231XX_VMUX_SVIDEO,
158 .vmux = CX231XX_VIN_1_1 |
159 (CX231XX_VIN_1_2 << 8) |
160 CX25840_SVIDEO_ON,
161 .amux = CX231XX_AMUX_LINE_IN,
162 .gpio = 0,
163 }
164 },
165 },
Sri Deevie0d3baf2009-03-03 14:37:50 -0300166};
167const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
168
169/* table of devices that work with this driver */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300170struct usb_device_id cx231xx_id_table[] = {
Sri Deevied559ed2009-03-13 18:35:14 -0300171 {USB_DEVICE(0x0572, 0x5A3C),
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300172 .driver_info = CX231XX_BOARD_UNKNOWN},
173 {USB_DEVICE(0x0572, 0x58A2),
174 .driver_info = CX231XX_BOARD_CNXT_RDE_250},
Sri Deevied559ed2009-03-13 18:35:14 -0300175 {USB_DEVICE(0x0572, 0x58A1),
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300176 .driver_info = CX231XX_BOARD_CNXT_RDU_250},
177 {},
Sri Deevie0d3baf2009-03-03 14:37:50 -0300178};
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300179
Sri Deevie0d3baf2009-03-03 14:37:50 -0300180MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
181
182/* cx231xx_tuner_callback
183 * will be used to reset XC5000 tuner using GPIO pin
184 */
185
186int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
187{
188 int rc = 0;
189 struct cx231xx *dev = ptr;
190
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300191 if (dev->tuner_type == TUNER_XC5000) {
192 if (command == XC5000_TUNER_RESET) {
193 cx231xx_info
Sri Deevib9255172009-03-04 17:49:01 -0300194 ("Tuner CB: RESET: cmd %d : tuner type %d \n",
195 command, dev->tuner_type);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300196 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
197 1);
198 msleep(10);
199 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
200 0);
201 msleep(330);
202 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
203 1);
204 msleep(10);
205 }
206 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300207 return rc;
208}
209EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
210
Sri Deevib9255172009-03-04 17:49:01 -0300211static inline void cx231xx_set_model(struct cx231xx *dev)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300212{
213 memcpy(&dev->board, &cx231xx_boards[dev->model], sizeof(dev->board));
214}
215
216/* Since cx231xx_pre_card_setup() requires a proper dev->model,
217 * this won't work for boards with generic PCI IDs
218 */
219void cx231xx_pre_card_setup(struct cx231xx *dev)
220{
221
222 cx231xx_set_model(dev);
223
224 cx231xx_info("Identified as %s (card=%d)\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300225 dev->board.name, dev->model);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300226
Sri Deevi6e4f5742009-03-10 21:16:26 -0300227 /* set the direction for GPIO pins */
228 cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
229 cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
230 cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300231
232 /* request some modules if any required */
233
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300234 /* reset the Tuner */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300235 cx231xx_gpio_set(dev, dev->board.tuner_gpio);
236
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300237 /* set the mode to Analog mode initially */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300238 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
239
240 /* Unlock device */
241 /* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
242
243}
244
Sri Deevie0d3baf2009-03-03 14:37:50 -0300245static void cx231xx_config_tuner(struct cx231xx *dev)
246{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300247 struct tuner_setup tun_setup;
248 struct v4l2_frequency f;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300249
250 if (dev->tuner_type == TUNER_ABSENT)
251 return;
252
253 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
254 tun_setup.type = dev->tuner_type;
255 tun_setup.addr = dev->tuner_addr;
256 tun_setup.tuner_callback = cx231xx_tuner_callback;
257
Sri Deevib1196122009-03-20 23:33:48 -0300258 tuner_call(dev, tuner, s_type_addr, &tun_setup);
259
Sri Deevie0d3baf2009-03-03 14:37:50 -0300260#if 0
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300261 if (tun_setup.type == TUNER_XC5000) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300262 static struct xc2028_ctrl ctrl = {
263 .fname = XC5000_DEFAULT_FIRMWARE,
264 .max_len = 64,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300265 .demod = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300266 };
267 struct v4l2_priv_tun_config cfg = {
268 .tuner = dev->tuner_type,
269 .priv = &ctrl,
270 };
Sri Deevib1196122009-03-20 23:33:48 -0300271 tuner_call(dev, tuner, s_config, &cfg);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300272 }
273#endif
Sri Deevie0d3baf2009-03-03 14:37:50 -0300274 /* configure tuner */
275 f.tuner = 0;
276 f.type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300277 f.frequency = 9076; /* just a magic number */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300278 dev->ctl_freq = f.frequency;
Sri Deevib1196122009-03-20 23:33:48 -0300279 call_all(dev, tuner, s_frequency, &f);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300280
Sri Deevib1196122009-03-20 23:33:48 -0300281}
Sri Deevie0d3baf2009-03-03 14:37:50 -0300282
283/* ----------------------------------------------------------------------- */
Jean Delvarec668f322009-05-13 16:48:50 -0300284void cx231xx_register_i2c_ir(struct cx231xx *dev)
285{
286 if (disable_ir)
287 return;
288
289 /* REVISIT: instantiate IR device */
Jean Delvarec668f322009-05-13 16:48:50 -0300290
Sri Deevie0d3baf2009-03-03 14:37:50 -0300291 /* detect & configure */
292 switch (dev->model) {
293
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300294 case CX231XX_BOARD_CNXT_RDE_250:
295 break;
296 case CX231XX_BOARD_CNXT_RDU_250:
297 break;
298 default:
299 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300300 }
301}
302
303void cx231xx_card_setup(struct cx231xx *dev)
304{
Sri Deevib1196122009-03-20 23:33:48 -0300305
Sri Deevie0d3baf2009-03-03 14:37:50 -0300306 cx231xx_set_model(dev);
307
308 dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
309 if (cx231xx_boards[dev->model].tuner_addr)
310 dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
311
Sri Deevie0d3baf2009-03-03 14:37:50 -0300312 /* request some modules */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300313 if (dev->board.decoder == CX231XX_AVDECODER) {
Hans Verkuile6574f22009-04-01 03:57:53 -0300314 dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
315 &dev->i2c_bus[0].i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -0300316 "cx25840", "cx25840", 0x88 >> 1, NULL);
Sri Deevib1196122009-03-20 23:33:48 -0300317 if (dev->sd_cx25840 == NULL)
318 cx231xx_info("cx25840 subdev registration failure\n");
Hans Verkuilcc26b072009-03-29 19:20:26 -0300319 cx25840_call(dev, core, load_fw);
Sri Deevib1196122009-03-20 23:33:48 -0300320
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300321 }
Sri Deevib1196122009-03-20 23:33:48 -0300322
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300323 if (dev->board.tuner_type != TUNER_ABSENT) {
Hans Verkuile6574f22009-04-01 03:57:53 -0300324 dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
325 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -0300326 "tuner", "tuner", 0xc2 >> 1, NULL);
Sri Deevib1196122009-03-20 23:33:48 -0300327 if (dev->sd_tuner == NULL)
328 cx231xx_info("tuner subdev registration failure\n");
329
330 cx231xx_config_tuner(dev);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300331 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300332
333 cx231xx_config_tuner(dev);
334
Sri Deevib1196122009-03-20 23:33:48 -0300335#if 0
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300336 /* TBD IR will be added later */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300337 cx231xx_ir_init(dev);
338#endif
339}
340
Sri Deevie0d3baf2009-03-03 14:37:50 -0300341/*
342 * cx231xx_config()
343 * inits registers with sane defaults
344 */
345int cx231xx_config(struct cx231xx *dev)
346{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300347 /* TBD need to add cx231xx specific code */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300348 dev->mute = 1; /* maybe not the right place... */
349 dev->volume = 0x1f;
350
351 return 0;
352}
353
354/*
355 * cx231xx_config_i2c()
356 * configure i2c attached devices
357 */
358void cx231xx_config_i2c(struct cx231xx *dev)
359{
Hans Verkuil5325b422009-04-02 11:26:22 -0300360 /* u32 input = INPUT(dev->video_input)->vmux; */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300361
Sri Deevib1196122009-03-20 23:33:48 -0300362 call_all(dev, video, s_stream, 1);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300363}
364
365/*
366 * cx231xx_realease_resources()
367 * unregisters the v4l2,i2c and usb devices
368 * called when the device gets disconected or at module unload
369*/
370void cx231xx_release_resources(struct cx231xx *dev)
371{
372
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300373#if 0 /* TBD IR related */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300374 if (dev->ir)
375 cx231xx_ir_fini(dev);
376#endif
377
378 cx231xx_release_analog_resources(dev);
379
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300380 cx231xx_remove_from_devlist(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300381
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300382 cx231xx_dev_uninit(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300383
384 usb_put_dev(dev->udev);
385
386 /* Mark device as unused */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300387 cx231xx_devused &= ~(1 << dev->devno);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300388}
389
Sri Deevie0d3baf2009-03-03 14:37:50 -0300390/*
391 * cx231xx_init_dev()
392 * allocates and inits the device structs, registers i2c bus and v4l device
393 */
394static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300395 int minor)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300396{
397 struct cx231xx *dev = *devhandle;
398 int retval = -ENOMEM;
399 int errCode;
400 unsigned int maxh, maxw;
401
402 dev->udev = udev;
403 mutex_init(&dev->lock);
404 mutex_init(&dev->ctrl_urb_lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300405 mutex_init(&dev->gpio_i2c_lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300406
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300407 spin_lock_init(&dev->video_mode.slock);
408 spin_lock_init(&dev->vbi_mode.slock);
409 spin_lock_init(&dev->sliced_cc_mode.slock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300410
411 init_waitqueue_head(&dev->open);
412 init_waitqueue_head(&dev->wait_frame);
413 init_waitqueue_head(&dev->wait_stream);
414
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300415 dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
416 dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
417 dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
418 dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
419 dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300420
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300421 /* Query cx231xx to find what pcb config it is related to */
422 initialize_cx231xx(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300423
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300424 /* Cx231xx pre card setup */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300425 cx231xx_pre_card_setup(dev);
426
427 errCode = cx231xx_config(dev);
428 if (errCode) {
429 cx231xx_errdev("error configuring device\n");
430 return -ENOMEM;
431 }
432
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300433 /* set default norm */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300434 dev->norm = dev->board.norm;
435
436 /* register i2c bus */
437 errCode = cx231xx_dev_init(dev);
438 if (errCode < 0) {
439 cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300440 __func__, errCode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300441 return errCode;
442 }
443
444 /* Do board specific init */
445 cx231xx_card_setup(dev);
446
447 /* configure the device */
448 cx231xx_config_i2c(dev);
449
450 maxw = norm_maxw(dev);
451 maxh = norm_maxh(dev);
452
453 /* set default image size */
454 dev->width = maxw;
455 dev->height = maxh;
456 dev->interlaced = 0;
457 dev->hscale = 0;
458 dev->vscale = 0;
459 dev->video_input = 0;
460
461 errCode = cx231xx_config(dev);
462 if (errCode < 0) {
463 cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300464 __func__, errCode);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300465 return errCode;
466 }
467
468 /* init video dma queues */
469 INIT_LIST_HEAD(&dev->video_mode.vidq.active);
470 INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
471
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300472 /* init vbi dma queues */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300473 INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
474 INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
475
476 /* Reset other chips required if they are tied up with GPIO pins */
477
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300478 cx231xx_add_into_devlist(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300479
480 retval = cx231xx_register_analog_devices(dev);
481 if (retval < 0) {
482 cx231xx_release_resources(dev);
483 goto fail_reg_devices;
484 }
485
486 cx231xx_init_extension(dev);
487
488 return 0;
489
Sri Deevib9255172009-03-04 17:49:01 -0300490fail_reg_devices:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300491 mutex_unlock(&dev->lock);
492 return retval;
493}
494
495#if defined(CONFIG_MODULES) && defined(MODULE)
496static void request_module_async(struct work_struct *work)
497{
498 struct cx231xx *dev = container_of(work,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300499 struct cx231xx, request_module_wk);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300500
501 if (dev->has_alsa_audio)
502 request_module("cx231xx-alsa");
503
504 if (dev->board.has_dvb)
505 request_module("cx231xx-dvb");
506
507}
508
509static void request_modules(struct cx231xx *dev)
510{
511 INIT_WORK(&dev->request_module_wk, request_module_async);
512 schedule_work(&dev->request_module_wk);
513}
514#else
515#define request_modules(dev)
516#endif /* CONFIG_MODULES */
517
Sri Deevie0d3baf2009-03-03 14:37:50 -0300518/*
519 * cx231xx_usb_probe()
520 * checks for supported devices
521 */
522static int cx231xx_usb_probe(struct usb_interface *interface,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300523 const struct usb_device_id *id)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300524{
525 struct usb_device *udev;
526 struct usb_interface *uif;
527 struct cx231xx *dev = NULL;
528 int retval = -ENODEV;
Hans Verkuild73abcf2009-03-12 18:38:40 -0300529 int nr = 0, ifnum;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300530 int i, isoc_pipe = 0;
531 char *speed;
532 char descr[255] = "";
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300533 struct usb_interface *lif = NULL;
534 int skip_interface = 0;
535 struct usb_interface_assoc_descriptor *assoc_desc;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300536
537 udev = usb_get_dev(interface_to_usbdev(interface));
538 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
539
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300540 if (!ifnum) {
541 /*
542 * Interface number 0 - IR interface
543 */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300544 /* Check to see next free device and mark as used */
545 nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
546 cx231xx_devused |= 1 << nr;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300547
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300548 if (nr >= CX231XX_MAXBOARDS) {
Mauro Carvalho Chehabb905de32009-03-22 05:46:36 -0300549 cx231xx_err(DRIVER_NAME ": Supports only %i cx231xx boards.\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300550 CX231XX_MAXBOARDS);
551 cx231xx_devused &= ~(1 << nr);
552 return -ENOMEM;
553 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300554
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300555 /* allocate memory for our device state and initialize it */
556 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
557 if (dev == NULL) {
558 cx231xx_err(DRIVER_NAME ": out of memory!\n");
559 cx231xx_devused &= ~(1 << nr);
560 return -ENOMEM;
561 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300562
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300563 snprintf(dev->name, 29, "cx231xx #%d", nr);
564 dev->devno = nr;
565 dev->model = id->driver_info;
566 dev->video_mode.alt = -1;
567 dev->interface_count++;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300568
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300569 /* reset gpio dir and value */
570 dev->gpio_dir = 0;
571 dev->gpio_val = 0;
572 dev->xc_fw_load_done = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300573 dev->has_alsa_audio = 1;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300574 dev->power_mode = -1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300575
Sri Deevib9255172009-03-04 17:49:01 -0300576 /* 0 - vbi ; 1 -sliced cc mode */
577 dev->vbi_or_sliced_cc_mode = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300578
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300579 /* get maximum no.of IAD interfaces */
580 assoc_desc = udev->actconfig->intf_assoc[0];
581 dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300582
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300583 /* init CIR module TBD */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300584
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300585 /* store the current interface */
586 lif = interface;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300587
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300588 switch (udev->speed) {
589 case USB_SPEED_LOW:
590 speed = "1.5";
591 break;
592 case USB_SPEED_UNKNOWN:
593 case USB_SPEED_FULL:
594 speed = "12";
595 break;
596 case USB_SPEED_HIGH:
597 speed = "480";
598 break;
599 default:
600 speed = "unknown";
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300601 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300602
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300603 if (udev->manufacturer)
604 strlcpy(descr, udev->manufacturer, sizeof(descr));
Sri Deevie0d3baf2009-03-03 14:37:50 -0300605
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300606 if (udev->product) {
607 if (*descr)
608 strlcat(descr, " ", sizeof(descr));
609 strlcat(descr, udev->product, sizeof(descr));
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300610 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300611 if (*descr)
612 strlcat(descr, " ", sizeof(descr));
Sri Deevie0d3baf2009-03-03 14:37:50 -0300613
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300614 cx231xx_info("New device %s@ %s Mbps "
615 "(%04x:%04x) with %d interfaces\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300616 descr,
617 speed,
618 le16_to_cpu(udev->descriptor.idVendor),
619 le16_to_cpu(udev->descriptor.idProduct),
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300620 dev->max_iad_interface_count);
621 } else {
622 /* Get dev structure first */
623 dev = usb_get_intfdata(udev->actconfig->interface[0]);
624 if (dev == NULL) {
625 cx231xx_err(DRIVER_NAME ": out of first interface!\n");
626 return -ENODEV;
Sri Deevib1196122009-03-20 23:33:48 -0300627 }
628
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300629 /* store the interface 0 back */
630 lif = udev->actconfig->interface[0];
Sri Deevie0d3baf2009-03-03 14:37:50 -0300631
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300632 /* increment interface count */
633 dev->interface_count++;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300634
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300635 /* get device number */
636 nr = dev->devno;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300637
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300638 /*
639 * set skip interface, for all interfaces but
640 * interface 1 and the last one
641 */
642 if ((ifnum != 1) && ((dev->interface_count - 1)
643 != dev->max_iad_interface_count))
644 skip_interface = 1;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300645
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300646 if (ifnum == 1) {
647 assoc_desc = udev->actconfig->intf_assoc[0];
648 if (assoc_desc->bFirstInterface != ifnum) {
649 cx231xx_err(DRIVER_NAME ": Not found "
650 "matching IAD interface\n");
651 return -ENODEV;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300652 }
653 }
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300654 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300655
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300656 if (skip_interface)
657 return -ENODEV;
658
659 cx231xx_info("registering interface %d\n", ifnum);
660
Sri Deevie0d3baf2009-03-03 14:37:50 -0300661 /* save our data pointer in this interface device */
662 usb_set_intfdata(lif, dev);
663
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300664 if ((dev->interface_count - 1) != dev->max_iad_interface_count)
665 return 0;
666
667 /*
668 * AV device initialization - only done at the last interface
669 */
670
671 /* Create v4l2 device */
Janne Grunau72317482009-04-01 08:39:29 -0300672 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300673 if (retval) {
674 cx231xx_errdev("v4l2_device_register failed\n");
675 cx231xx_devused &= ~(1 << nr);
676 kfree(dev);
677 return -EIO;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300678 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300679
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300680 /* allocate device struct */
681 retval = cx231xx_init_dev(&dev, udev, nr);
682 if (retval) {
683 cx231xx_devused &= ~(1 << dev->devno);
684 v4l2_device_unregister(&dev->v4l2_dev);
685 kfree(dev);
686 return retval;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300687 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300688
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300689 /* compute alternate max packet sizes for video */
690 uif = udev->actconfig->interface[dev->current_pcb_config.
691 hs_config_info[0].interface_info.video_index + 1];
692
693 dev->video_mode.end_point_addr = le16_to_cpu(uif->altsetting[0].
694 endpoint[isoc_pipe].desc.bEndpointAddress);
695
696 dev->video_mode.num_alt = uif->num_altsetting;
697 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
698 dev->video_mode.end_point_addr,
699 dev->video_mode.num_alt);
700 dev->video_mode.alt_max_pkt_size =
701 kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
702
703 if (dev->video_mode.alt_max_pkt_size == NULL) {
704 cx231xx_errdev("out of memory!\n");
705 cx231xx_devused &= ~(1 << nr);
706 v4l2_device_unregister(&dev->v4l2_dev);
707 kfree(dev);
708 return -ENOMEM;
709 }
710
711 for (i = 0; i < dev->video_mode.num_alt; i++) {
712 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
713 desc.wMaxPacketSize);
714 dev->video_mode.alt_max_pkt_size[i] =
715 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
716 cx231xx_info("Alternate setting %i, max size= %i\n", i,
717 dev->video_mode.alt_max_pkt_size[i]);
718 }
719
720 /* compute alternate max packet sizes for vbi */
721 uif = udev->actconfig->interface[dev->current_pcb_config.
722 hs_config_info[0].interface_info.
723 vanc_index + 1];
724
725 dev->vbi_mode.end_point_addr =
726 le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
727 bEndpointAddress);
728
729 dev->vbi_mode.num_alt = uif->num_altsetting;
730 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
731 dev->vbi_mode.end_point_addr,
732 dev->vbi_mode.num_alt);
733 dev->vbi_mode.alt_max_pkt_size =
734 kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
735
736 if (dev->vbi_mode.alt_max_pkt_size == NULL) {
737 cx231xx_errdev("out of memory!\n");
738 cx231xx_devused &= ~(1 << nr);
739 v4l2_device_unregister(&dev->v4l2_dev);
740 kfree(dev);
741 return -ENOMEM;
742 }
743
744 for (i = 0; i < dev->vbi_mode.num_alt; i++) {
745 u16 tmp =
746 le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
747 desc.wMaxPacketSize);
748 dev->vbi_mode.alt_max_pkt_size[i] =
749 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
750 cx231xx_info("Alternate setting %i, max size= %i\n", i,
751 dev->vbi_mode.alt_max_pkt_size[i]);
752 }
753
754 /* compute alternate max packet sizes for sliced CC */
755 uif = udev->actconfig->interface[dev->current_pcb_config.
756 hs_config_info[0].interface_info.
757 hanc_index + 1];
758
759 dev->sliced_cc_mode.end_point_addr =
760 le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
761 bEndpointAddress);
762
763 dev->sliced_cc_mode.num_alt = uif->num_altsetting;
764 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
765 dev->sliced_cc_mode.end_point_addr,
766 dev->sliced_cc_mode.num_alt);
767 dev->sliced_cc_mode.alt_max_pkt_size =
768 kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
769
770 if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
771 cx231xx_errdev("out of memory!\n");
772 cx231xx_devused &= ~(1 << nr);
773 v4l2_device_unregister(&dev->v4l2_dev);
774 kfree(dev);
775 return -ENOMEM;
776 }
777
778 for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
779 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
780 desc.wMaxPacketSize);
781 dev->sliced_cc_mode.alt_max_pkt_size[i] =
782 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
783 cx231xx_info("Alternate setting %i, max size= %i\n", i,
784 dev->sliced_cc_mode.alt_max_pkt_size[i]);
785 }
786
787 if (dev->current_pcb_config.ts1_source != 0xff) {
788 /* compute alternate max packet sizes for TS1 */
789 uif = udev->actconfig->interface[dev->current_pcb_config.
790 hs_config_info[0].
791 interface_info.
792 ts1_index + 1];
793
794 dev->ts1_mode.end_point_addr =
795 le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
796 desc.bEndpointAddress);
797
798 dev->ts1_mode.num_alt = uif->num_altsetting;
799 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
800 dev->ts1_mode.end_point_addr,
801 dev->ts1_mode.num_alt);
802 dev->ts1_mode.alt_max_pkt_size =
803 kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
804
805 if (dev->ts1_mode.alt_max_pkt_size == NULL) {
806 cx231xx_errdev("out of memory!\n");
807 cx231xx_devused &= ~(1 << nr);
808 v4l2_device_unregister(&dev->v4l2_dev);
809 kfree(dev);
810 return -ENOMEM;
811 }
812
813 for (i = 0; i < dev->ts1_mode.num_alt; i++) {
814 u16 tmp = le16_to_cpu(uif->altsetting[i].
815 endpoint[isoc_pipe].desc.
816 wMaxPacketSize);
817 dev->ts1_mode.alt_max_pkt_size[i] =
818 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
819 cx231xx_info("Alternate setting %i, max size= %i\n", i,
820 dev->ts1_mode.alt_max_pkt_size[i]);
821 }
822 }
823
824 /* load other modules required */
825 request_modules(dev);
826
Sri Deevie0d3baf2009-03-03 14:37:50 -0300827 return 0;
828}
829
830/*
831 * cx231xx_usb_disconnect()
832 * called when the device gets diconencted
833 * video device will be unregistered on v4l2_close in case it is still open
834 */
835static void cx231xx_usb_disconnect(struct usb_interface *interface)
836{
837 struct cx231xx *dev;
838
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300839 dev = usb_get_intfdata(interface);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300840 usb_set_intfdata(interface, NULL);
841
842 if (!dev)
843 return;
844
Mauro Carvalho Chehab90960742009-03-22 07:52:02 -0300845 if (!dev->udev)
846 return;
847
Sri Deevib1196122009-03-20 23:33:48 -0300848 /* delete v4l2 device */
849 v4l2_device_unregister(&dev->v4l2_dev);
850
Sri Deevie0d3baf2009-03-03 14:37:50 -0300851 /* wait until all current v4l2 io is finished then deallocate
852 resources */
853 mutex_lock(&dev->lock);
854
855 wake_up_interruptible_all(&dev->open);
856
857 if (dev->users) {
858 cx231xx_warn
859 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300860 "deallocation are deferred on close.\n", dev->vdev->num);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300861
862 dev->state |= DEV_MISCONFIGURED;
863 cx231xx_uninit_isoc(dev);
864 dev->state |= DEV_DISCONNECTED;
865 wake_up_interruptible(&dev->wait_frame);
866 wake_up_interruptible(&dev->wait_stream);
867 } else {
868 dev->state |= DEV_DISCONNECTED;
869 cx231xx_release_resources(dev);
870 }
871
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300872 cx231xx_close_extension(dev);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300873
874 mutex_unlock(&dev->lock);
875
876 if (!dev->users) {
877 kfree(dev->video_mode.alt_max_pkt_size);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300878 kfree(dev->vbi_mode.alt_max_pkt_size);
879 kfree(dev->sliced_cc_mode.alt_max_pkt_size);
880 kfree(dev->ts1_mode.alt_max_pkt_size);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300881 kfree(dev);
882 }
883}
884
885static struct usb_driver cx231xx_usb_driver = {
886 .name = "cx231xx",
887 .probe = cx231xx_usb_probe,
888 .disconnect = cx231xx_usb_disconnect,
889 .id_table = cx231xx_id_table,
890};
891
892static int __init cx231xx_module_init(void)
893{
894 int result;
895
Mauro Carvalho Chehab818fdf32009-03-13 07:41:58 -0300896 printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300897
898 /* register this driver with the USB subsystem */
899 result = usb_register(&cx231xx_usb_driver);
900 if (result)
901 cx231xx_err(DRIVER_NAME
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300902 " usb_register failed. Error number %d.\n", result);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300903
904 return result;
905}
906
907static void __exit cx231xx_module_exit(void)
908{
909 /* deregister this driver with the USB subsystem */
910 usb_deregister(&cx231xx_usb_driver);
911}
912
913module_init(cx231xx_module_init);
914module_exit(cx231xx_module_exit);