cx231xx-cards.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. cx231xx-cards.c - driver for Conexant Cx23100/101/102
  3. USB video capture devices
  4. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  5. Based on em28xx driver
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/i2c.h>
  22. #include <linux/usb.h>
  23. #include <media/tuner.h>
  24. #include <media/tveeprom.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-chip-ident.h>
  27. #include <media/cx25840.h>
  28. #include "xc5000.h"
  29. #include "cx231xx.h"
  30. static int tuner = -1;
  31. module_param(tuner, int, 0444);
  32. MODULE_PARM_DESC(tuner, "tuner type");
  33. static unsigned int disable_ir;
  34. module_param(disable_ir, int, 0444);
  35. MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
  36. /* Bitmask marking allocated devices from 0 to CX231XX_MAXBOARDS */
  37. static unsigned long cx231xx_devused;
  38. /*
  39. * Reset sequences for analog/digital modes
  40. */
  41. static struct cx231xx_reg_seq RDE250_XCV_TUNER[] = {
  42. {0x03, 0x01, 10},
  43. {0x03, 0x00, 30},
  44. {0x03, 0x01, 10},
  45. {-1, -1, -1},
  46. };
  47. /*
  48. * Board definitions
  49. */
  50. struct cx231xx_board cx231xx_boards[] = {
  51. [CX231XX_BOARD_UNKNOWN] = {
  52. .name = "Unknown CX231xx video grabber",
  53. .tuner_type = TUNER_ABSENT,
  54. .input = {{
  55. .type = CX231XX_VMUX_TELEVISION,
  56. .vmux = CX231XX_VIN_3_1,
  57. .amux = CX231XX_AMUX_VIDEO,
  58. .gpio = 0,
  59. }, {
  60. .type = CX231XX_VMUX_COMPOSITE1,
  61. .vmux = CX231XX_VIN_2_1,
  62. .amux = CX231XX_AMUX_LINE_IN,
  63. .gpio = 0,
  64. }, {
  65. .type = CX231XX_VMUX_SVIDEO,
  66. .vmux = CX231XX_VIN_1_1 |
  67. (CX231XX_VIN_1_2 << 8) |
  68. CX25840_SVIDEO_ON,
  69. .amux = CX231XX_AMUX_LINE_IN,
  70. .gpio = 0,
  71. }
  72. },
  73. },
  74. [CX231XX_BOARD_CNXT_RDE_250] = {
  75. .name = "Conexant Hybrid TV - RDE250",
  76. .tuner_type = TUNER_XC5000,
  77. .tuner_addr = 0x61,
  78. .tuner_gpio = RDE250_XCV_TUNER,
  79. .tuner_sif_gpio = 0x05,
  80. .tuner_scl_gpio = 0x1a,
  81. .tuner_sda_gpio = 0x1b,
  82. .decoder = CX231XX_AVDECODER,
  83. .demod_xfer_mode = 0,
  84. .ctl_pin_status_mask = 0xFFFFFFC4,
  85. .agc_analog_digital_select_gpio = 0x0c,
  86. .gpio_pin_status_mask = 0x4001000,
  87. .tuner_i2c_master = 1,
  88. .demod_i2c_master = 2,
  89. .has_dvb = 1,
  90. .demod_addr = 0x02,
  91. .norm = V4L2_STD_PAL,
  92. .input = {{
  93. .type = CX231XX_VMUX_TELEVISION,
  94. .vmux = CX231XX_VIN_3_1,
  95. .amux = CX231XX_AMUX_VIDEO,
  96. .gpio = 0,
  97. }, {
  98. .type = CX231XX_VMUX_COMPOSITE1,
  99. .vmux = CX231XX_VIN_2_1,
  100. .amux = CX231XX_AMUX_LINE_IN,
  101. .gpio = 0,
  102. }, {
  103. .type = CX231XX_VMUX_SVIDEO,
  104. .vmux = CX231XX_VIN_1_1 |
  105. (CX231XX_VIN_1_2 << 8) |
  106. CX25840_SVIDEO_ON,
  107. .amux = CX231XX_AMUX_LINE_IN,
  108. .gpio = 0,
  109. }
  110. },
  111. },
  112. [CX231XX_BOARD_CNXT_RDU_250] = {
  113. .name = "Conexant Hybrid TV - RDU250",
  114. .tuner_type = TUNER_XC5000,
  115. .tuner_addr = 0x61,
  116. .tuner_gpio = RDE250_XCV_TUNER,
  117. .tuner_sif_gpio = 0x05,
  118. .tuner_scl_gpio = 0x1a,
  119. .tuner_sda_gpio = 0x1b,
  120. .decoder = CX231XX_AVDECODER,
  121. .demod_xfer_mode = 0,
  122. .ctl_pin_status_mask = 0xFFFFFFC4,
  123. .agc_analog_digital_select_gpio = 0x0c,
  124. .gpio_pin_status_mask = 0x4001000,
  125. .tuner_i2c_master = 1,
  126. .demod_i2c_master = 2,
  127. .has_dvb = 1,
  128. .demod_addr = 0x32,
  129. .norm = V4L2_STD_NTSC,
  130. .input = {{
  131. .type = CX231XX_VMUX_TELEVISION,
  132. .vmux = CX231XX_VIN_3_1,
  133. .amux = CX231XX_AMUX_VIDEO,
  134. .gpio = 0,
  135. }, {
  136. .type = CX231XX_VMUX_COMPOSITE1,
  137. .vmux = CX231XX_VIN_2_1,
  138. .amux = CX231XX_AMUX_LINE_IN,
  139. .gpio = 0,
  140. }, {
  141. .type = CX231XX_VMUX_SVIDEO,
  142. .vmux = CX231XX_VIN_1_1 |
  143. (CX231XX_VIN_1_2 << 8) |
  144. CX25840_SVIDEO_ON,
  145. .amux = CX231XX_AMUX_LINE_IN,
  146. .gpio = 0,
  147. }
  148. },
  149. },
  150. };
  151. const unsigned int cx231xx_bcount = ARRAY_SIZE(cx231xx_boards);
  152. /* table of devices that work with this driver */
  153. struct usb_device_id cx231xx_id_table[] = {
  154. {USB_DEVICE(0x0572, 0x5A3C),
  155. .driver_info = CX231XX_BOARD_UNKNOWN},
  156. {USB_DEVICE(0x0572, 0x58A2),
  157. .driver_info = CX231XX_BOARD_CNXT_RDE_250},
  158. {USB_DEVICE(0x0572, 0x58A1),
  159. .driver_info = CX231XX_BOARD_CNXT_RDU_250},
  160. {},
  161. };
  162. MODULE_DEVICE_TABLE(usb, cx231xx_id_table);
  163. /* cx231xx_tuner_callback
  164. * will be used to reset XC5000 tuner using GPIO pin
  165. */
  166. int cx231xx_tuner_callback(void *ptr, int component, int command, int arg)
  167. {
  168. int rc = 0;
  169. struct cx231xx *dev = ptr;
  170. if (dev->tuner_type == TUNER_XC5000) {
  171. if (command == XC5000_TUNER_RESET) {
  172. cx231xx_info
  173. ("Tuner CB: RESET: cmd %d : tuner type %d \n",
  174. command, dev->tuner_type);
  175. cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
  176. 1);
  177. msleep(10);
  178. cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
  179. 0);
  180. msleep(330);
  181. cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit,
  182. 1);
  183. msleep(10);
  184. }
  185. }
  186. return rc;
  187. }
  188. EXPORT_SYMBOL_GPL(cx231xx_tuner_callback);
  189. static inline void cx231xx_set_model(struct cx231xx *dev)
  190. {
  191. memcpy(&dev->board, &cx231xx_boards[dev->model], sizeof(dev->board));
  192. }
  193. /* Since cx231xx_pre_card_setup() requires a proper dev->model,
  194. * this won't work for boards with generic PCI IDs
  195. */
  196. void cx231xx_pre_card_setup(struct cx231xx *dev)
  197. {
  198. cx231xx_set_model(dev);
  199. cx231xx_info("Identified as %s (card=%d)\n",
  200. dev->board.name, dev->model);
  201. /* set the direction for GPIO pins */
  202. cx231xx_set_gpio_direction(dev, dev->board.tuner_gpio->bit, 1);
  203. cx231xx_set_gpio_value(dev, dev->board.tuner_gpio->bit, 1);
  204. cx231xx_set_gpio_direction(dev, dev->board.tuner_sif_gpio, 1);
  205. /* request some modules if any required */
  206. /* reset the Tuner */
  207. cx231xx_gpio_set(dev, dev->board.tuner_gpio);
  208. /* set the mode to Analog mode initially */
  209. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  210. /* Unlock device */
  211. /* cx231xx_set_mode(dev, CX231XX_SUSPEND); */
  212. }
  213. static void cx231xx_config_tuner(struct cx231xx *dev)
  214. {
  215. struct tuner_setup tun_setup;
  216. struct v4l2_frequency f;
  217. if (dev->tuner_type == TUNER_ABSENT)
  218. return;
  219. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  220. tun_setup.type = dev->tuner_type;
  221. tun_setup.addr = dev->tuner_addr;
  222. tun_setup.tuner_callback = cx231xx_tuner_callback;
  223. tuner_call(dev, tuner, s_type_addr, &tun_setup);
  224. #if 0
  225. if (tun_setup.type == TUNER_XC5000) {
  226. static struct xc2028_ctrl ctrl = {
  227. .fname = XC5000_DEFAULT_FIRMWARE,
  228. .max_len = 64,
  229. .demod = 0;
  230. };
  231. struct v4l2_priv_tun_config cfg = {
  232. .tuner = dev->tuner_type,
  233. .priv = &ctrl,
  234. };
  235. tuner_call(dev, tuner, s_config, &cfg);
  236. }
  237. #endif
  238. /* configure tuner */
  239. f.tuner = 0;
  240. f.type = V4L2_TUNER_ANALOG_TV;
  241. f.frequency = 9076; /* just a magic number */
  242. dev->ctl_freq = f.frequency;
  243. call_all(dev, tuner, s_frequency, &f);
  244. }
  245. /* ----------------------------------------------------------------------- */
  246. void cx231xx_set_ir(struct cx231xx *dev, struct IR_i2c *ir)
  247. {
  248. if (disable_ir) {
  249. ir->get_key = NULL;
  250. return;
  251. }
  252. /* detect & configure */
  253. switch (dev->model) {
  254. case CX231XX_BOARD_CNXT_RDE_250:
  255. break;
  256. case CX231XX_BOARD_CNXT_RDU_250:
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. void cx231xx_card_setup(struct cx231xx *dev)
  263. {
  264. cx231xx_set_model(dev);
  265. dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
  266. if (cx231xx_boards[dev->model].tuner_addr)
  267. dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
  268. /* request some modules */
  269. if (dev->board.decoder == CX231XX_AVDECODER) {
  270. dev->sd_cx25840 =
  271. v4l2_i2c_new_subdev(&dev->i2c_bus[0].i2c_adap,
  272. "cx25840", "cx25840", 0x88 >> 1);
  273. if (dev->sd_cx25840 == NULL)
  274. cx231xx_info("cx25840 subdev registration failure\n");
  275. cx25840_call(dev, core, init, 0);
  276. }
  277. if (dev->board.tuner_type != TUNER_ABSENT) {
  278. dev->sd_tuner =
  279. v4l2_i2c_new_subdev(&dev->i2c_bus[1].i2c_adap,
  280. "tuner", "tuner", 0xc2 >> 1);
  281. if (dev->sd_tuner == NULL)
  282. cx231xx_info("tuner subdev registration failure\n");
  283. cx231xx_config_tuner(dev);
  284. }
  285. cx231xx_config_tuner(dev);
  286. #if 0
  287. /* TBD IR will be added later */
  288. cx231xx_ir_init(dev);
  289. #endif
  290. }
  291. /*
  292. * cx231xx_config()
  293. * inits registers with sane defaults
  294. */
  295. int cx231xx_config(struct cx231xx *dev)
  296. {
  297. /* TBD need to add cx231xx specific code */
  298. dev->mute = 1; /* maybe not the right place... */
  299. dev->volume = 0x1f;
  300. return 0;
  301. }
  302. /*
  303. * cx231xx_config_i2c()
  304. * configure i2c attached devices
  305. */
  306. void cx231xx_config_i2c(struct cx231xx *dev)
  307. {
  308. struct v4l2_routing route;
  309. route.input = INPUT(dev->video_input)->vmux;
  310. route.output = 0;
  311. call_all(dev, video, s_stream, 1);
  312. }
  313. /*
  314. * cx231xx_realease_resources()
  315. * unregisters the v4l2,i2c and usb devices
  316. * called when the device gets disconected or at module unload
  317. */
  318. void cx231xx_release_resources(struct cx231xx *dev)
  319. {
  320. #if 0 /* TBD IR related */
  321. if (dev->ir)
  322. cx231xx_ir_fini(dev);
  323. #endif
  324. cx231xx_release_analog_resources(dev);
  325. cx231xx_remove_from_devlist(dev);
  326. cx231xx_dev_uninit(dev);
  327. usb_put_dev(dev->udev);
  328. /* Mark device as unused */
  329. cx231xx_devused &= ~(1 << dev->devno);
  330. }
  331. /*
  332. * cx231xx_init_dev()
  333. * allocates and inits the device structs, registers i2c bus and v4l device
  334. */
  335. static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
  336. int minor)
  337. {
  338. struct cx231xx *dev = *devhandle;
  339. int retval = -ENOMEM;
  340. int errCode;
  341. unsigned int maxh, maxw;
  342. dev->udev = udev;
  343. mutex_init(&dev->lock);
  344. mutex_init(&dev->ctrl_urb_lock);
  345. mutex_init(&dev->gpio_i2c_lock);
  346. spin_lock_init(&dev->video_mode.slock);
  347. spin_lock_init(&dev->vbi_mode.slock);
  348. spin_lock_init(&dev->sliced_cc_mode.slock);
  349. init_waitqueue_head(&dev->open);
  350. init_waitqueue_head(&dev->wait_frame);
  351. init_waitqueue_head(&dev->wait_stream);
  352. dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
  353. dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
  354. dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
  355. dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
  356. dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
  357. /* Query cx231xx to find what pcb config it is related to */
  358. initialize_cx231xx(dev);
  359. /* Cx231xx pre card setup */
  360. cx231xx_pre_card_setup(dev);
  361. errCode = cx231xx_config(dev);
  362. if (errCode) {
  363. cx231xx_errdev("error configuring device\n");
  364. return -ENOMEM;
  365. }
  366. /* set default norm */
  367. dev->norm = dev->board.norm;
  368. /* register i2c bus */
  369. errCode = cx231xx_dev_init(dev);
  370. if (errCode < 0) {
  371. cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
  372. __func__, errCode);
  373. return errCode;
  374. }
  375. /* Do board specific init */
  376. cx231xx_card_setup(dev);
  377. /* configure the device */
  378. cx231xx_config_i2c(dev);
  379. maxw = norm_maxw(dev);
  380. maxh = norm_maxh(dev);
  381. /* set default image size */
  382. dev->width = maxw;
  383. dev->height = maxh;
  384. dev->interlaced = 0;
  385. dev->hscale = 0;
  386. dev->vscale = 0;
  387. dev->video_input = 0;
  388. errCode = cx231xx_config(dev);
  389. if (errCode < 0) {
  390. cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
  391. __func__, errCode);
  392. return errCode;
  393. }
  394. /* init video dma queues */
  395. INIT_LIST_HEAD(&dev->video_mode.vidq.active);
  396. INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
  397. /* init vbi dma queues */
  398. INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
  399. INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
  400. /* Reset other chips required if they are tied up with GPIO pins */
  401. cx231xx_add_into_devlist(dev);
  402. retval = cx231xx_register_analog_devices(dev);
  403. if (retval < 0) {
  404. cx231xx_release_resources(dev);
  405. goto fail_reg_devices;
  406. }
  407. cx231xx_init_extension(dev);
  408. return 0;
  409. fail_reg_devices:
  410. mutex_unlock(&dev->lock);
  411. return retval;
  412. }
  413. #if defined(CONFIG_MODULES) && defined(MODULE)
  414. static void request_module_async(struct work_struct *work)
  415. {
  416. struct cx231xx *dev = container_of(work,
  417. struct cx231xx, request_module_wk);
  418. if (dev->has_alsa_audio)
  419. request_module("cx231xx-alsa");
  420. if (dev->board.has_dvb)
  421. request_module("cx231xx-dvb");
  422. }
  423. static void request_modules(struct cx231xx *dev)
  424. {
  425. INIT_WORK(&dev->request_module_wk, request_module_async);
  426. schedule_work(&dev->request_module_wk);
  427. }
  428. #else
  429. #define request_modules(dev)
  430. #endif /* CONFIG_MODULES */
  431. /*
  432. * cx231xx_usb_probe()
  433. * checks for supported devices
  434. */
  435. static int cx231xx_usb_probe(struct usb_interface *interface,
  436. const struct usb_device_id *id)
  437. {
  438. struct usb_device *udev;
  439. struct usb_interface *uif;
  440. struct cx231xx *dev = NULL;
  441. int retval = -ENODEV;
  442. int nr = 0, ifnum;
  443. int i, isoc_pipe = 0;
  444. char *speed;
  445. char descr[255] = "";
  446. struct usb_interface *lif = NULL;
  447. int skip_interface = 0;
  448. struct usb_interface_assoc_descriptor *assoc_desc;
  449. udev = usb_get_dev(interface_to_usbdev(interface));
  450. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  451. if (!ifnum) {
  452. /*
  453. * Interface number 0 - IR interface
  454. */
  455. /* Check to see next free device and mark as used */
  456. nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
  457. cx231xx_devused |= 1 << nr;
  458. if (nr >= CX231XX_MAXBOARDS) {
  459. cx231xx_err(DRIVER_NAME ": Supports only %i cx231xx boards.\n",
  460. CX231XX_MAXBOARDS);
  461. cx231xx_devused &= ~(1 << nr);
  462. return -ENOMEM;
  463. }
  464. /* allocate memory for our device state and initialize it */
  465. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  466. if (dev == NULL) {
  467. cx231xx_err(DRIVER_NAME ": out of memory!\n");
  468. cx231xx_devused &= ~(1 << nr);
  469. return -ENOMEM;
  470. }
  471. snprintf(dev->name, 29, "cx231xx #%d", nr);
  472. dev->devno = nr;
  473. dev->model = id->driver_info;
  474. dev->video_mode.alt = -1;
  475. dev->interface_count++;
  476. /* reset gpio dir and value */
  477. dev->gpio_dir = 0;
  478. dev->gpio_val = 0;
  479. dev->xc_fw_load_done = 0;
  480. dev->has_alsa_audio = 1;
  481. dev->power_mode = -1;
  482. /* 0 - vbi ; 1 -sliced cc mode */
  483. dev->vbi_or_sliced_cc_mode = 0;
  484. /* get maximum no.of IAD interfaces */
  485. assoc_desc = udev->actconfig->intf_assoc[0];
  486. dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
  487. /* init CIR module TBD */
  488. /* store the current interface */
  489. lif = interface;
  490. switch (udev->speed) {
  491. case USB_SPEED_LOW:
  492. speed = "1.5";
  493. break;
  494. case USB_SPEED_UNKNOWN:
  495. case USB_SPEED_FULL:
  496. speed = "12";
  497. break;
  498. case USB_SPEED_HIGH:
  499. speed = "480";
  500. break;
  501. default:
  502. speed = "unknown";
  503. }
  504. if (udev->manufacturer)
  505. strlcpy(descr, udev->manufacturer, sizeof(descr));
  506. if (udev->product) {
  507. if (*descr)
  508. strlcat(descr, " ", sizeof(descr));
  509. strlcat(descr, udev->product, sizeof(descr));
  510. }
  511. if (*descr)
  512. strlcat(descr, " ", sizeof(descr));
  513. cx231xx_info("New device %s@ %s Mbps "
  514. "(%04x:%04x) with %d interfaces\n",
  515. descr,
  516. speed,
  517. le16_to_cpu(udev->descriptor.idVendor),
  518. le16_to_cpu(udev->descriptor.idProduct),
  519. dev->max_iad_interface_count);
  520. } else {
  521. /* Get dev structure first */
  522. dev = usb_get_intfdata(udev->actconfig->interface[0]);
  523. if (dev == NULL) {
  524. cx231xx_err(DRIVER_NAME ": out of first interface!\n");
  525. return -ENODEV;
  526. }
  527. /* store the interface 0 back */
  528. lif = udev->actconfig->interface[0];
  529. /* increment interface count */
  530. dev->interface_count++;
  531. /* get device number */
  532. nr = dev->devno;
  533. /*
  534. * set skip interface, for all interfaces but
  535. * interface 1 and the last one
  536. */
  537. if ((ifnum != 1) && ((dev->interface_count - 1)
  538. != dev->max_iad_interface_count))
  539. skip_interface = 1;
  540. if (ifnum == 1) {
  541. assoc_desc = udev->actconfig->intf_assoc[0];
  542. if (assoc_desc->bFirstInterface != ifnum) {
  543. cx231xx_err(DRIVER_NAME ": Not found "
  544. "matching IAD interface\n");
  545. return -ENODEV;
  546. }
  547. }
  548. }
  549. if (skip_interface)
  550. return -ENODEV;
  551. cx231xx_info("registering interface %d\n", ifnum);
  552. /* save our data pointer in this interface device */
  553. usb_set_intfdata(lif, dev);
  554. if ((dev->interface_count - 1) != dev->max_iad_interface_count)
  555. return 0;
  556. /*
  557. * AV device initialization - only done at the last interface
  558. */
  559. /* Create v4l2 device */
  560. retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  561. if (retval) {
  562. cx231xx_errdev("v4l2_device_register failed\n");
  563. cx231xx_devused &= ~(1 << nr);
  564. kfree(dev);
  565. return -EIO;
  566. }
  567. /* allocate device struct */
  568. retval = cx231xx_init_dev(&dev, udev, nr);
  569. if (retval) {
  570. cx231xx_devused &= ~(1 << dev->devno);
  571. v4l2_device_unregister(&dev->v4l2_dev);
  572. kfree(dev);
  573. return retval;
  574. }
  575. /* compute alternate max packet sizes for video */
  576. uif = udev->actconfig->interface[dev->current_pcb_config.
  577. hs_config_info[0].interface_info.video_index + 1];
  578. dev->video_mode.end_point_addr = le16_to_cpu(uif->altsetting[0].
  579. endpoint[isoc_pipe].desc.bEndpointAddress);
  580. dev->video_mode.num_alt = uif->num_altsetting;
  581. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  582. dev->video_mode.end_point_addr,
  583. dev->video_mode.num_alt);
  584. dev->video_mode.alt_max_pkt_size =
  585. kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
  586. if (dev->video_mode.alt_max_pkt_size == NULL) {
  587. cx231xx_errdev("out of memory!\n");
  588. cx231xx_devused &= ~(1 << nr);
  589. v4l2_device_unregister(&dev->v4l2_dev);
  590. kfree(dev);
  591. return -ENOMEM;
  592. }
  593. for (i = 0; i < dev->video_mode.num_alt; i++) {
  594. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  595. desc.wMaxPacketSize);
  596. dev->video_mode.alt_max_pkt_size[i] =
  597. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  598. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  599. dev->video_mode.alt_max_pkt_size[i]);
  600. }
  601. /* compute alternate max packet sizes for vbi */
  602. uif = udev->actconfig->interface[dev->current_pcb_config.
  603. hs_config_info[0].interface_info.
  604. vanc_index + 1];
  605. dev->vbi_mode.end_point_addr =
  606. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
  607. bEndpointAddress);
  608. dev->vbi_mode.num_alt = uif->num_altsetting;
  609. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  610. dev->vbi_mode.end_point_addr,
  611. dev->vbi_mode.num_alt);
  612. dev->vbi_mode.alt_max_pkt_size =
  613. kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
  614. if (dev->vbi_mode.alt_max_pkt_size == NULL) {
  615. cx231xx_errdev("out of memory!\n");
  616. cx231xx_devused &= ~(1 << nr);
  617. v4l2_device_unregister(&dev->v4l2_dev);
  618. kfree(dev);
  619. return -ENOMEM;
  620. }
  621. for (i = 0; i < dev->vbi_mode.num_alt; i++) {
  622. u16 tmp =
  623. le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  624. desc.wMaxPacketSize);
  625. dev->vbi_mode.alt_max_pkt_size[i] =
  626. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  627. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  628. dev->vbi_mode.alt_max_pkt_size[i]);
  629. }
  630. /* compute alternate max packet sizes for sliced CC */
  631. uif = udev->actconfig->interface[dev->current_pcb_config.
  632. hs_config_info[0].interface_info.
  633. hanc_index + 1];
  634. dev->sliced_cc_mode.end_point_addr =
  635. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
  636. bEndpointAddress);
  637. dev->sliced_cc_mode.num_alt = uif->num_altsetting;
  638. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  639. dev->sliced_cc_mode.end_point_addr,
  640. dev->sliced_cc_mode.num_alt);
  641. dev->sliced_cc_mode.alt_max_pkt_size =
  642. kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
  643. if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
  644. cx231xx_errdev("out of memory!\n");
  645. cx231xx_devused &= ~(1 << nr);
  646. v4l2_device_unregister(&dev->v4l2_dev);
  647. kfree(dev);
  648. return -ENOMEM;
  649. }
  650. for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
  651. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  652. desc.wMaxPacketSize);
  653. dev->sliced_cc_mode.alt_max_pkt_size[i] =
  654. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  655. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  656. dev->sliced_cc_mode.alt_max_pkt_size[i]);
  657. }
  658. if (dev->current_pcb_config.ts1_source != 0xff) {
  659. /* compute alternate max packet sizes for TS1 */
  660. uif = udev->actconfig->interface[dev->current_pcb_config.
  661. hs_config_info[0].
  662. interface_info.
  663. ts1_index + 1];
  664. dev->ts1_mode.end_point_addr =
  665. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
  666. desc.bEndpointAddress);
  667. dev->ts1_mode.num_alt = uif->num_altsetting;
  668. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  669. dev->ts1_mode.end_point_addr,
  670. dev->ts1_mode.num_alt);
  671. dev->ts1_mode.alt_max_pkt_size =
  672. kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
  673. if (dev->ts1_mode.alt_max_pkt_size == NULL) {
  674. cx231xx_errdev("out of memory!\n");
  675. cx231xx_devused &= ~(1 << nr);
  676. v4l2_device_unregister(&dev->v4l2_dev);
  677. kfree(dev);
  678. return -ENOMEM;
  679. }
  680. for (i = 0; i < dev->ts1_mode.num_alt; i++) {
  681. u16 tmp = le16_to_cpu(uif->altsetting[i].
  682. endpoint[isoc_pipe].desc.
  683. wMaxPacketSize);
  684. dev->ts1_mode.alt_max_pkt_size[i] =
  685. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  686. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  687. dev->ts1_mode.alt_max_pkt_size[i]);
  688. }
  689. }
  690. /* load other modules required */
  691. request_modules(dev);
  692. return 0;
  693. }
  694. /*
  695. * cx231xx_usb_disconnect()
  696. * called when the device gets diconencted
  697. * video device will be unregistered on v4l2_close in case it is still open
  698. */
  699. static void cx231xx_usb_disconnect(struct usb_interface *interface)
  700. {
  701. struct cx231xx *dev;
  702. dev = usb_get_intfdata(interface);
  703. usb_set_intfdata(interface, NULL);
  704. if (!dev)
  705. return;
  706. if (!dev->udev)
  707. return;
  708. /* delete v4l2 device */
  709. v4l2_device_unregister(&dev->v4l2_dev);
  710. /* wait until all current v4l2 io is finished then deallocate
  711. resources */
  712. mutex_lock(&dev->lock);
  713. wake_up_interruptible_all(&dev->open);
  714. if (dev->users) {
  715. cx231xx_warn
  716. ("device /dev/video%d is open! Deregistration and memory "
  717. "deallocation are deferred on close.\n", dev->vdev->num);
  718. dev->state |= DEV_MISCONFIGURED;
  719. cx231xx_uninit_isoc(dev);
  720. dev->state |= DEV_DISCONNECTED;
  721. wake_up_interruptible(&dev->wait_frame);
  722. wake_up_interruptible(&dev->wait_stream);
  723. } else {
  724. dev->state |= DEV_DISCONNECTED;
  725. cx231xx_release_resources(dev);
  726. }
  727. cx231xx_close_extension(dev);
  728. mutex_unlock(&dev->lock);
  729. if (!dev->users) {
  730. kfree(dev->video_mode.alt_max_pkt_size);
  731. kfree(dev->vbi_mode.alt_max_pkt_size);
  732. kfree(dev->sliced_cc_mode.alt_max_pkt_size);
  733. kfree(dev->ts1_mode.alt_max_pkt_size);
  734. kfree(dev);
  735. }
  736. }
  737. static struct usb_driver cx231xx_usb_driver = {
  738. .name = "cx231xx",
  739. .probe = cx231xx_usb_probe,
  740. .disconnect = cx231xx_usb_disconnect,
  741. .id_table = cx231xx_id_table,
  742. };
  743. static int __init cx231xx_module_init(void)
  744. {
  745. int result;
  746. printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
  747. /* register this driver with the USB subsystem */
  748. result = usb_register(&cx231xx_usb_driver);
  749. if (result)
  750. cx231xx_err(DRIVER_NAME
  751. " usb_register failed. Error number %d.\n", result);
  752. return result;
  753. }
  754. static void __exit cx231xx_module_exit(void)
  755. {
  756. /* deregister this driver with the USB subsystem */
  757. usb_deregister(&cx231xx_usb_driver);
  758. }
  759. module_init(cx231xx_module_init);
  760. module_exit(cx231xx_module_exit);