/kernel/2.6.32_froyo_photon_nightly/drivers/media/video/cx231xx/cx231xx-cards.c

http://photon-android.googlecode.com/ · C · 914 lines · 641 code · 153 blank · 120 comment · 72 complexity · adf8cc2c3bd1dc124a45f51d42a248c0 MD5 · raw file

  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_register_i2c_ir(struct cx231xx *dev)
  247. {
  248. if (disable_ir)
  249. return;
  250. /* REVISIT: instantiate IR device */
  251. /* detect & configure */
  252. switch (dev->model) {
  253. case CX231XX_BOARD_CNXT_RDE_250:
  254. break;
  255. case CX231XX_BOARD_CNXT_RDU_250:
  256. break;
  257. default:
  258. break;
  259. }
  260. }
  261. void cx231xx_card_setup(struct cx231xx *dev)
  262. {
  263. cx231xx_set_model(dev);
  264. dev->tuner_type = cx231xx_boards[dev->model].tuner_type;
  265. if (cx231xx_boards[dev->model].tuner_addr)
  266. dev->tuner_addr = cx231xx_boards[dev->model].tuner_addr;
  267. /* request some modules */
  268. if (dev->board.decoder == CX231XX_AVDECODER) {
  269. dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  270. &dev->i2c_bus[0].i2c_adap,
  271. "cx25840", "cx25840", 0x88 >> 1, NULL);
  272. if (dev->sd_cx25840 == NULL)
  273. cx231xx_info("cx25840 subdev registration failure\n");
  274. cx25840_call(dev, core, load_fw);
  275. }
  276. if (dev->board.tuner_type != TUNER_ABSENT) {
  277. dev->sd_tuner = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  278. &dev->i2c_bus[1].i2c_adap,
  279. "tuner", "tuner", 0xc2 >> 1, NULL);
  280. if (dev->sd_tuner == NULL)
  281. cx231xx_info("tuner subdev registration failure\n");
  282. cx231xx_config_tuner(dev);
  283. }
  284. cx231xx_config_tuner(dev);
  285. #if 0
  286. /* TBD IR will be added later */
  287. cx231xx_ir_init(dev);
  288. #endif
  289. }
  290. /*
  291. * cx231xx_config()
  292. * inits registers with sane defaults
  293. */
  294. int cx231xx_config(struct cx231xx *dev)
  295. {
  296. /* TBD need to add cx231xx specific code */
  297. dev->mute = 1; /* maybe not the right place... */
  298. dev->volume = 0x1f;
  299. return 0;
  300. }
  301. /*
  302. * cx231xx_config_i2c()
  303. * configure i2c attached devices
  304. */
  305. void cx231xx_config_i2c(struct cx231xx *dev)
  306. {
  307. /* u32 input = INPUT(dev->video_input)->vmux; */
  308. call_all(dev, video, s_stream, 1);
  309. }
  310. /*
  311. * cx231xx_realease_resources()
  312. * unregisters the v4l2,i2c and usb devices
  313. * called when the device gets disconected or at module unload
  314. */
  315. void cx231xx_release_resources(struct cx231xx *dev)
  316. {
  317. #if 0 /* TBD IR related */
  318. if (dev->ir)
  319. cx231xx_ir_fini(dev);
  320. #endif
  321. cx231xx_release_analog_resources(dev);
  322. cx231xx_remove_from_devlist(dev);
  323. cx231xx_dev_uninit(dev);
  324. usb_put_dev(dev->udev);
  325. /* Mark device as unused */
  326. cx231xx_devused &= ~(1 << dev->devno);
  327. }
  328. /*
  329. * cx231xx_init_dev()
  330. * allocates and inits the device structs, registers i2c bus and v4l device
  331. */
  332. static int cx231xx_init_dev(struct cx231xx **devhandle, struct usb_device *udev,
  333. int minor)
  334. {
  335. struct cx231xx *dev = *devhandle;
  336. int retval = -ENOMEM;
  337. int errCode;
  338. unsigned int maxh, maxw;
  339. dev->udev = udev;
  340. mutex_init(&dev->lock);
  341. mutex_init(&dev->ctrl_urb_lock);
  342. mutex_init(&dev->gpio_i2c_lock);
  343. spin_lock_init(&dev->video_mode.slock);
  344. spin_lock_init(&dev->vbi_mode.slock);
  345. spin_lock_init(&dev->sliced_cc_mode.slock);
  346. init_waitqueue_head(&dev->open);
  347. init_waitqueue_head(&dev->wait_frame);
  348. init_waitqueue_head(&dev->wait_stream);
  349. dev->cx231xx_read_ctrl_reg = cx231xx_read_ctrl_reg;
  350. dev->cx231xx_write_ctrl_reg = cx231xx_write_ctrl_reg;
  351. dev->cx231xx_send_usb_command = cx231xx_send_usb_command;
  352. dev->cx231xx_gpio_i2c_read = cx231xx_gpio_i2c_read;
  353. dev->cx231xx_gpio_i2c_write = cx231xx_gpio_i2c_write;
  354. /* Query cx231xx to find what pcb config it is related to */
  355. initialize_cx231xx(dev);
  356. /* Cx231xx pre card setup */
  357. cx231xx_pre_card_setup(dev);
  358. errCode = cx231xx_config(dev);
  359. if (errCode) {
  360. cx231xx_errdev("error configuring device\n");
  361. return -ENOMEM;
  362. }
  363. /* set default norm */
  364. dev->norm = dev->board.norm;
  365. /* register i2c bus */
  366. errCode = cx231xx_dev_init(dev);
  367. if (errCode < 0) {
  368. cx231xx_errdev("%s: cx231xx_i2c_register - errCode [%d]!\n",
  369. __func__, errCode);
  370. return errCode;
  371. }
  372. /* Do board specific init */
  373. cx231xx_card_setup(dev);
  374. /* configure the device */
  375. cx231xx_config_i2c(dev);
  376. maxw = norm_maxw(dev);
  377. maxh = norm_maxh(dev);
  378. /* set default image size */
  379. dev->width = maxw;
  380. dev->height = maxh;
  381. dev->interlaced = 0;
  382. dev->hscale = 0;
  383. dev->vscale = 0;
  384. dev->video_input = 0;
  385. errCode = cx231xx_config(dev);
  386. if (errCode < 0) {
  387. cx231xx_errdev("%s: cx231xx_config - errCode [%d]!\n",
  388. __func__, errCode);
  389. return errCode;
  390. }
  391. /* init video dma queues */
  392. INIT_LIST_HEAD(&dev->video_mode.vidq.active);
  393. INIT_LIST_HEAD(&dev->video_mode.vidq.queued);
  394. /* init vbi dma queues */
  395. INIT_LIST_HEAD(&dev->vbi_mode.vidq.active);
  396. INIT_LIST_HEAD(&dev->vbi_mode.vidq.queued);
  397. /* Reset other chips required if they are tied up with GPIO pins */
  398. cx231xx_add_into_devlist(dev);
  399. retval = cx231xx_register_analog_devices(dev);
  400. if (retval < 0) {
  401. cx231xx_release_resources(dev);
  402. goto fail_reg_devices;
  403. }
  404. cx231xx_init_extension(dev);
  405. return 0;
  406. fail_reg_devices:
  407. mutex_unlock(&dev->lock);
  408. return retval;
  409. }
  410. #if defined(CONFIG_MODULES) && defined(MODULE)
  411. static void request_module_async(struct work_struct *work)
  412. {
  413. struct cx231xx *dev = container_of(work,
  414. struct cx231xx, request_module_wk);
  415. if (dev->has_alsa_audio)
  416. request_module("cx231xx-alsa");
  417. if (dev->board.has_dvb)
  418. request_module("cx231xx-dvb");
  419. }
  420. static void request_modules(struct cx231xx *dev)
  421. {
  422. INIT_WORK(&dev->request_module_wk, request_module_async);
  423. schedule_work(&dev->request_module_wk);
  424. }
  425. #else
  426. #define request_modules(dev)
  427. #endif /* CONFIG_MODULES */
  428. /*
  429. * cx231xx_usb_probe()
  430. * checks for supported devices
  431. */
  432. static int cx231xx_usb_probe(struct usb_interface *interface,
  433. const struct usb_device_id *id)
  434. {
  435. struct usb_device *udev;
  436. struct usb_interface *uif;
  437. struct cx231xx *dev = NULL;
  438. int retval = -ENODEV;
  439. int nr = 0, ifnum;
  440. int i, isoc_pipe = 0;
  441. char *speed;
  442. char descr[255] = "";
  443. struct usb_interface *lif = NULL;
  444. int skip_interface = 0;
  445. struct usb_interface_assoc_descriptor *assoc_desc;
  446. udev = usb_get_dev(interface_to_usbdev(interface));
  447. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  448. if (!ifnum) {
  449. /*
  450. * Interface number 0 - IR interface
  451. */
  452. /* Check to see next free device and mark as used */
  453. nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
  454. cx231xx_devused |= 1 << nr;
  455. if (nr >= CX231XX_MAXBOARDS) {
  456. cx231xx_err(DRIVER_NAME ": Supports only %i cx231xx boards.\n",
  457. CX231XX_MAXBOARDS);
  458. cx231xx_devused &= ~(1 << nr);
  459. return -ENOMEM;
  460. }
  461. /* allocate memory for our device state and initialize it */
  462. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  463. if (dev == NULL) {
  464. cx231xx_err(DRIVER_NAME ": out of memory!\n");
  465. cx231xx_devused &= ~(1 << nr);
  466. return -ENOMEM;
  467. }
  468. snprintf(dev->name, 29, "cx231xx #%d", nr);
  469. dev->devno = nr;
  470. dev->model = id->driver_info;
  471. dev->video_mode.alt = -1;
  472. dev->interface_count++;
  473. /* reset gpio dir and value */
  474. dev->gpio_dir = 0;
  475. dev->gpio_val = 0;
  476. dev->xc_fw_load_done = 0;
  477. dev->has_alsa_audio = 1;
  478. dev->power_mode = -1;
  479. /* 0 - vbi ; 1 -sliced cc mode */
  480. dev->vbi_or_sliced_cc_mode = 0;
  481. /* get maximum no.of IAD interfaces */
  482. assoc_desc = udev->actconfig->intf_assoc[0];
  483. dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
  484. /* init CIR module TBD */
  485. /* store the current interface */
  486. lif = interface;
  487. switch (udev->speed) {
  488. case USB_SPEED_LOW:
  489. speed = "1.5";
  490. break;
  491. case USB_SPEED_UNKNOWN:
  492. case USB_SPEED_FULL:
  493. speed = "12";
  494. break;
  495. case USB_SPEED_HIGH:
  496. speed = "480";
  497. break;
  498. default:
  499. speed = "unknown";
  500. }
  501. if (udev->manufacturer)
  502. strlcpy(descr, udev->manufacturer, sizeof(descr));
  503. if (udev->product) {
  504. if (*descr)
  505. strlcat(descr, " ", sizeof(descr));
  506. strlcat(descr, udev->product, sizeof(descr));
  507. }
  508. if (*descr)
  509. strlcat(descr, " ", sizeof(descr));
  510. cx231xx_info("New device %s@ %s Mbps "
  511. "(%04x:%04x) with %d interfaces\n",
  512. descr,
  513. speed,
  514. le16_to_cpu(udev->descriptor.idVendor),
  515. le16_to_cpu(udev->descriptor.idProduct),
  516. dev->max_iad_interface_count);
  517. } else {
  518. /* Get dev structure first */
  519. dev = usb_get_intfdata(udev->actconfig->interface[0]);
  520. if (dev == NULL) {
  521. cx231xx_err(DRIVER_NAME ": out of first interface!\n");
  522. return -ENODEV;
  523. }
  524. /* store the interface 0 back */
  525. lif = udev->actconfig->interface[0];
  526. /* increment interface count */
  527. dev->interface_count++;
  528. /* get device number */
  529. nr = dev->devno;
  530. /*
  531. * set skip interface, for all interfaces but
  532. * interface 1 and the last one
  533. */
  534. if ((ifnum != 1) && ((dev->interface_count - 1)
  535. != dev->max_iad_interface_count))
  536. skip_interface = 1;
  537. if (ifnum == 1) {
  538. assoc_desc = udev->actconfig->intf_assoc[0];
  539. if (assoc_desc->bFirstInterface != ifnum) {
  540. cx231xx_err(DRIVER_NAME ": Not found "
  541. "matching IAD interface\n");
  542. return -ENODEV;
  543. }
  544. }
  545. }
  546. if (skip_interface)
  547. return -ENODEV;
  548. cx231xx_info("registering interface %d\n", ifnum);
  549. /* save our data pointer in this interface device */
  550. usb_set_intfdata(lif, dev);
  551. if ((dev->interface_count - 1) != dev->max_iad_interface_count)
  552. return 0;
  553. /*
  554. * AV device initialization - only done at the last interface
  555. */
  556. /* Create v4l2 device */
  557. retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  558. if (retval) {
  559. cx231xx_errdev("v4l2_device_register failed\n");
  560. cx231xx_devused &= ~(1 << nr);
  561. kfree(dev);
  562. return -EIO;
  563. }
  564. /* allocate device struct */
  565. retval = cx231xx_init_dev(&dev, udev, nr);
  566. if (retval) {
  567. cx231xx_devused &= ~(1 << dev->devno);
  568. v4l2_device_unregister(&dev->v4l2_dev);
  569. kfree(dev);
  570. return retval;
  571. }
  572. /* compute alternate max packet sizes for video */
  573. uif = udev->actconfig->interface[dev->current_pcb_config.
  574. hs_config_info[0].interface_info.video_index + 1];
  575. dev->video_mode.end_point_addr = le16_to_cpu(uif->altsetting[0].
  576. endpoint[isoc_pipe].desc.bEndpointAddress);
  577. dev->video_mode.num_alt = uif->num_altsetting;
  578. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  579. dev->video_mode.end_point_addr,
  580. dev->video_mode.num_alt);
  581. dev->video_mode.alt_max_pkt_size =
  582. kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
  583. if (dev->video_mode.alt_max_pkt_size == NULL) {
  584. cx231xx_errdev("out of memory!\n");
  585. cx231xx_devused &= ~(1 << nr);
  586. v4l2_device_unregister(&dev->v4l2_dev);
  587. kfree(dev);
  588. return -ENOMEM;
  589. }
  590. for (i = 0; i < dev->video_mode.num_alt; i++) {
  591. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  592. desc.wMaxPacketSize);
  593. dev->video_mode.alt_max_pkt_size[i] =
  594. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  595. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  596. dev->video_mode.alt_max_pkt_size[i]);
  597. }
  598. /* compute alternate max packet sizes for vbi */
  599. uif = udev->actconfig->interface[dev->current_pcb_config.
  600. hs_config_info[0].interface_info.
  601. vanc_index + 1];
  602. dev->vbi_mode.end_point_addr =
  603. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
  604. bEndpointAddress);
  605. dev->vbi_mode.num_alt = uif->num_altsetting;
  606. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  607. dev->vbi_mode.end_point_addr,
  608. dev->vbi_mode.num_alt);
  609. dev->vbi_mode.alt_max_pkt_size =
  610. kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
  611. if (dev->vbi_mode.alt_max_pkt_size == NULL) {
  612. cx231xx_errdev("out of memory!\n");
  613. cx231xx_devused &= ~(1 << nr);
  614. v4l2_device_unregister(&dev->v4l2_dev);
  615. kfree(dev);
  616. return -ENOMEM;
  617. }
  618. for (i = 0; i < dev->vbi_mode.num_alt; i++) {
  619. u16 tmp =
  620. le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  621. desc.wMaxPacketSize);
  622. dev->vbi_mode.alt_max_pkt_size[i] =
  623. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  624. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  625. dev->vbi_mode.alt_max_pkt_size[i]);
  626. }
  627. /* compute alternate max packet sizes for sliced CC */
  628. uif = udev->actconfig->interface[dev->current_pcb_config.
  629. hs_config_info[0].interface_info.
  630. hanc_index + 1];
  631. dev->sliced_cc_mode.end_point_addr =
  632. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
  633. bEndpointAddress);
  634. dev->sliced_cc_mode.num_alt = uif->num_altsetting;
  635. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  636. dev->sliced_cc_mode.end_point_addr,
  637. dev->sliced_cc_mode.num_alt);
  638. dev->sliced_cc_mode.alt_max_pkt_size =
  639. kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
  640. if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
  641. cx231xx_errdev("out of memory!\n");
  642. cx231xx_devused &= ~(1 << nr);
  643. v4l2_device_unregister(&dev->v4l2_dev);
  644. kfree(dev);
  645. return -ENOMEM;
  646. }
  647. for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
  648. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  649. desc.wMaxPacketSize);
  650. dev->sliced_cc_mode.alt_max_pkt_size[i] =
  651. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  652. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  653. dev->sliced_cc_mode.alt_max_pkt_size[i]);
  654. }
  655. if (dev->current_pcb_config.ts1_source != 0xff) {
  656. /* compute alternate max packet sizes for TS1 */
  657. uif = udev->actconfig->interface[dev->current_pcb_config.
  658. hs_config_info[0].
  659. interface_info.
  660. ts1_index + 1];
  661. dev->ts1_mode.end_point_addr =
  662. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
  663. desc.bEndpointAddress);
  664. dev->ts1_mode.num_alt = uif->num_altsetting;
  665. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  666. dev->ts1_mode.end_point_addr,
  667. dev->ts1_mode.num_alt);
  668. dev->ts1_mode.alt_max_pkt_size =
  669. kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
  670. if (dev->ts1_mode.alt_max_pkt_size == NULL) {
  671. cx231xx_errdev("out of memory!\n");
  672. cx231xx_devused &= ~(1 << nr);
  673. v4l2_device_unregister(&dev->v4l2_dev);
  674. kfree(dev);
  675. return -ENOMEM;
  676. }
  677. for (i = 0; i < dev->ts1_mode.num_alt; i++) {
  678. u16 tmp = le16_to_cpu(uif->altsetting[i].
  679. endpoint[isoc_pipe].desc.
  680. wMaxPacketSize);
  681. dev->ts1_mode.alt_max_pkt_size[i] =
  682. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  683. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  684. dev->ts1_mode.alt_max_pkt_size[i]);
  685. }
  686. }
  687. /* load other modules required */
  688. request_modules(dev);
  689. return 0;
  690. }
  691. /*
  692. * cx231xx_usb_disconnect()
  693. * called when the device gets diconencted
  694. * video device will be unregistered on v4l2_close in case it is still open
  695. */
  696. static void cx231xx_usb_disconnect(struct usb_interface *interface)
  697. {
  698. struct cx231xx *dev;
  699. dev = usb_get_intfdata(interface);
  700. usb_set_intfdata(interface, NULL);
  701. if (!dev)
  702. return;
  703. if (!dev->udev)
  704. return;
  705. /* delete v4l2 device */
  706. v4l2_device_unregister(&dev->v4l2_dev);
  707. /* wait until all current v4l2 io is finished then deallocate
  708. resources */
  709. mutex_lock(&dev->lock);
  710. wake_up_interruptible_all(&dev->open);
  711. if (dev->users) {
  712. cx231xx_warn
  713. ("device /dev/video%d is open! Deregistration and memory "
  714. "deallocation are deferred on close.\n", dev->vdev->num);
  715. dev->state |= DEV_MISCONFIGURED;
  716. cx231xx_uninit_isoc(dev);
  717. dev->state |= DEV_DISCONNECTED;
  718. wake_up_interruptible(&dev->wait_frame);
  719. wake_up_interruptible(&dev->wait_stream);
  720. } else {
  721. dev->state |= DEV_DISCONNECTED;
  722. cx231xx_release_resources(dev);
  723. }
  724. cx231xx_close_extension(dev);
  725. mutex_unlock(&dev->lock);
  726. if (!dev->users) {
  727. kfree(dev->video_mode.alt_max_pkt_size);
  728. kfree(dev->vbi_mode.alt_max_pkt_size);
  729. kfree(dev->sliced_cc_mode.alt_max_pkt_size);
  730. kfree(dev->ts1_mode.alt_max_pkt_size);
  731. kfree(dev);
  732. }
  733. }
  734. static struct usb_driver cx231xx_usb_driver = {
  735. .name = "cx231xx",
  736. .probe = cx231xx_usb_probe,
  737. .disconnect = cx231xx_usb_disconnect,
  738. .id_table = cx231xx_id_table,
  739. };
  740. static int __init cx231xx_module_init(void)
  741. {
  742. int result;
  743. printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
  744. /* register this driver with the USB subsystem */
  745. result = usb_register(&cx231xx_usb_driver);
  746. if (result)
  747. cx231xx_err(DRIVER_NAME
  748. " usb_register failed. Error number %d.\n", result);
  749. return result;
  750. }
  751. static void __exit cx231xx_module_exit(void)
  752. {
  753. /* deregister this driver with the USB subsystem */
  754. usb_deregister(&cx231xx_usb_driver);
  755. }
  756. module_init(cx231xx_module_init);
  757. module_exit(cx231xx_module_exit);