cx231xx-cards.c 24 KB

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