cx231xx-cards.c 23 KB

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