cx231xx-cards.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. if (!ifnum) {
  468. /*
  469. * Interface number 0 - IR interface
  470. */
  471. /* Check to see next free device and mark as used */
  472. nr = find_first_zero_bit(&cx231xx_devused, CX231XX_MAXBOARDS);
  473. cx231xx_devused |= 1 << nr;
  474. if (nr >= CX231XX_MAXBOARDS) {
  475. cx231xx_err(DRIVER_NAME ": Supports only %i cx231xx boards.\n",
  476. CX231XX_MAXBOARDS);
  477. cx231xx_devused &= ~(1 << nr);
  478. return -ENOMEM;
  479. }
  480. /* allocate memory for our device state and initialize it */
  481. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  482. if (dev == NULL) {
  483. cx231xx_err(DRIVER_NAME ": out of memory!\n");
  484. cx231xx_devused &= ~(1 << nr);
  485. return -ENOMEM;
  486. }
  487. snprintf(dev->name, 29, "cx231xx #%d", nr);
  488. dev->devno = nr;
  489. dev->model = id->driver_info;
  490. dev->video_mode.alt = -1;
  491. dev->interface_count++;
  492. /* reset gpio dir and value */
  493. dev->gpio_dir = 0;
  494. dev->gpio_val = 0;
  495. dev->xc_fw_load_done = 0;
  496. dev->has_alsa_audio = 1;
  497. dev->power_mode = -1;
  498. /* 0 - vbi ; 1 -sliced cc mode */
  499. dev->vbi_or_sliced_cc_mode = 0;
  500. /* get maximum no.of IAD interfaces */
  501. assoc_desc = udev->actconfig->intf_assoc[0];
  502. dev->max_iad_interface_count = assoc_desc->bInterfaceCount;
  503. /* init CIR module TBD */
  504. /* store the current interface */
  505. lif = interface;
  506. switch (udev->speed) {
  507. case USB_SPEED_LOW:
  508. speed = "1.5";
  509. break;
  510. case USB_SPEED_UNKNOWN:
  511. case USB_SPEED_FULL:
  512. speed = "12";
  513. break;
  514. case USB_SPEED_HIGH:
  515. speed = "480";
  516. break;
  517. default:
  518. speed = "unknown";
  519. }
  520. if (udev->manufacturer)
  521. strlcpy(descr, udev->manufacturer, sizeof(descr));
  522. if (udev->product) {
  523. if (*descr)
  524. strlcat(descr, " ", sizeof(descr));
  525. strlcat(descr, udev->product, sizeof(descr));
  526. }
  527. if (*descr)
  528. strlcat(descr, " ", sizeof(descr));
  529. cx231xx_info("New device %s@ %s Mbps "
  530. "(%04x:%04x) with %d interfaces\n",
  531. descr,
  532. speed,
  533. le16_to_cpu(udev->descriptor.idVendor),
  534. le16_to_cpu(udev->descriptor.idProduct),
  535. dev->max_iad_interface_count);
  536. } else {
  537. /* Get dev structure first */
  538. dev = usb_get_intfdata(udev->actconfig->interface[0]);
  539. if (dev == NULL) {
  540. cx231xx_err(DRIVER_NAME ": out of first interface!\n");
  541. return -ENODEV;
  542. }
  543. /* store the interface 0 back */
  544. lif = udev->actconfig->interface[0];
  545. /* increment interface count */
  546. dev->interface_count++;
  547. /* get device number */
  548. nr = dev->devno;
  549. /*
  550. * set skip interface, for all interfaces but
  551. * interface 1 and the last one
  552. */
  553. if ((ifnum != 1) && ((dev->interface_count - 1)
  554. != dev->max_iad_interface_count))
  555. skip_interface = 1;
  556. if (ifnum == 1) {
  557. assoc_desc = udev->actconfig->intf_assoc[0];
  558. if (assoc_desc->bFirstInterface != ifnum) {
  559. cx231xx_err(DRIVER_NAME ": Not found "
  560. "matching IAD interface\n");
  561. return -ENODEV;
  562. }
  563. }
  564. }
  565. if (skip_interface)
  566. return -ENODEV;
  567. cx231xx_info("registering interface %d\n", ifnum);
  568. /* save our data pointer in this interface device */
  569. usb_set_intfdata(lif, dev);
  570. if ((dev->interface_count - 1) != dev->max_iad_interface_count)
  571. return 0;
  572. /*
  573. * AV device initialization - only done at the last interface
  574. */
  575. /* Create v4l2 device */
  576. snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  577. "%s-%03d", "cx231xx", nr);
  578. retval = v4l2_device_register(&udev->dev, &dev->v4l2_dev);
  579. if (retval) {
  580. cx231xx_errdev("v4l2_device_register failed\n");
  581. cx231xx_devused &= ~(1 << nr);
  582. kfree(dev);
  583. return -EIO;
  584. }
  585. /* allocate device struct */
  586. retval = cx231xx_init_dev(&dev, udev, nr);
  587. if (retval) {
  588. cx231xx_devused &= ~(1 << dev->devno);
  589. v4l2_device_unregister(&dev->v4l2_dev);
  590. kfree(dev);
  591. return retval;
  592. }
  593. /* compute alternate max packet sizes for video */
  594. uif = udev->actconfig->interface[dev->current_pcb_config.
  595. hs_config_info[0].interface_info.video_index + 1];
  596. dev->video_mode.end_point_addr = le16_to_cpu(uif->altsetting[0].
  597. endpoint[isoc_pipe].desc.bEndpointAddress);
  598. dev->video_mode.num_alt = uif->num_altsetting;
  599. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  600. dev->video_mode.end_point_addr,
  601. dev->video_mode.num_alt);
  602. dev->video_mode.alt_max_pkt_size =
  603. kmalloc(32 * dev->video_mode.num_alt, GFP_KERNEL);
  604. if (dev->video_mode.alt_max_pkt_size == NULL) {
  605. cx231xx_errdev("out of memory!\n");
  606. cx231xx_devused &= ~(1 << nr);
  607. v4l2_device_unregister(&dev->v4l2_dev);
  608. kfree(dev);
  609. return -ENOMEM;
  610. }
  611. for (i = 0; i < dev->video_mode.num_alt; i++) {
  612. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  613. desc.wMaxPacketSize);
  614. dev->video_mode.alt_max_pkt_size[i] =
  615. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  616. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  617. dev->video_mode.alt_max_pkt_size[i]);
  618. }
  619. /* compute alternate max packet sizes for vbi */
  620. uif = udev->actconfig->interface[dev->current_pcb_config.
  621. hs_config_info[0].interface_info.
  622. vanc_index + 1];
  623. dev->vbi_mode.end_point_addr =
  624. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
  625. bEndpointAddress);
  626. dev->vbi_mode.num_alt = uif->num_altsetting;
  627. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  628. dev->vbi_mode.end_point_addr,
  629. dev->vbi_mode.num_alt);
  630. dev->vbi_mode.alt_max_pkt_size =
  631. kmalloc(32 * dev->vbi_mode.num_alt, GFP_KERNEL);
  632. if (dev->vbi_mode.alt_max_pkt_size == NULL) {
  633. cx231xx_errdev("out of memory!\n");
  634. cx231xx_devused &= ~(1 << nr);
  635. v4l2_device_unregister(&dev->v4l2_dev);
  636. kfree(dev);
  637. return -ENOMEM;
  638. }
  639. for (i = 0; i < dev->vbi_mode.num_alt; i++) {
  640. u16 tmp =
  641. le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  642. desc.wMaxPacketSize);
  643. dev->vbi_mode.alt_max_pkt_size[i] =
  644. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  645. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  646. dev->vbi_mode.alt_max_pkt_size[i]);
  647. }
  648. /* compute alternate max packet sizes for sliced CC */
  649. uif = udev->actconfig->interface[dev->current_pcb_config.
  650. hs_config_info[0].interface_info.
  651. hanc_index + 1];
  652. dev->sliced_cc_mode.end_point_addr =
  653. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
  654. bEndpointAddress);
  655. dev->sliced_cc_mode.num_alt = uif->num_altsetting;
  656. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  657. dev->sliced_cc_mode.end_point_addr,
  658. dev->sliced_cc_mode.num_alt);
  659. dev->sliced_cc_mode.alt_max_pkt_size =
  660. kmalloc(32 * dev->sliced_cc_mode.num_alt, GFP_KERNEL);
  661. if (dev->sliced_cc_mode.alt_max_pkt_size == NULL) {
  662. cx231xx_errdev("out of memory!\n");
  663. cx231xx_devused &= ~(1 << nr);
  664. v4l2_device_unregister(&dev->v4l2_dev);
  665. kfree(dev);
  666. return -ENOMEM;
  667. }
  668. for (i = 0; i < dev->sliced_cc_mode.num_alt; i++) {
  669. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].
  670. desc.wMaxPacketSize);
  671. dev->sliced_cc_mode.alt_max_pkt_size[i] =
  672. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  673. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  674. dev->sliced_cc_mode.alt_max_pkt_size[i]);
  675. }
  676. if (dev->current_pcb_config.ts1_source != 0xff) {
  677. /* compute alternate max packet sizes for TS1 */
  678. uif = udev->actconfig->interface[dev->current_pcb_config.
  679. hs_config_info[0].
  680. interface_info.
  681. ts1_index + 1];
  682. dev->ts1_mode.end_point_addr =
  683. le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].
  684. desc.bEndpointAddress);
  685. dev->ts1_mode.num_alt = uif->num_altsetting;
  686. cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
  687. dev->ts1_mode.end_point_addr,
  688. dev->ts1_mode.num_alt);
  689. dev->ts1_mode.alt_max_pkt_size =
  690. kmalloc(32 * dev->ts1_mode.num_alt, GFP_KERNEL);
  691. if (dev->ts1_mode.alt_max_pkt_size == NULL) {
  692. cx231xx_errdev("out of memory!\n");
  693. cx231xx_devused &= ~(1 << nr);
  694. v4l2_device_unregister(&dev->v4l2_dev);
  695. kfree(dev);
  696. return -ENOMEM;
  697. }
  698. for (i = 0; i < dev->ts1_mode.num_alt; i++) {
  699. u16 tmp = le16_to_cpu(uif->altsetting[i].
  700. endpoint[isoc_pipe].desc.
  701. wMaxPacketSize);
  702. dev->ts1_mode.alt_max_pkt_size[i] =
  703. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  704. cx231xx_info("Alternate setting %i, max size= %i\n", i,
  705. dev->ts1_mode.alt_max_pkt_size[i]);
  706. }
  707. }
  708. /* load other modules required */
  709. request_modules(dev);
  710. return 0;
  711. }
  712. /*
  713. * cx231xx_usb_disconnect()
  714. * called when the device gets diconencted
  715. * video device will be unregistered on v4l2_close in case it is still open
  716. */
  717. static void cx231xx_usb_disconnect(struct usb_interface *interface)
  718. {
  719. struct cx231xx *dev;
  720. dev = usb_get_intfdata(interface);
  721. usb_set_intfdata(interface, NULL);
  722. if (!dev)
  723. return;
  724. if (!dev->udev)
  725. return;
  726. /* delete v4l2 device */
  727. v4l2_device_unregister(&dev->v4l2_dev);
  728. /* wait until all current v4l2 io is finished then deallocate
  729. resources */
  730. mutex_lock(&dev->lock);
  731. wake_up_interruptible_all(&dev->open);
  732. if (dev->users) {
  733. cx231xx_warn
  734. ("device /dev/video%d is open! Deregistration and memory "
  735. "deallocation are deferred on close.\n", dev->vdev->num);
  736. dev->state |= DEV_MISCONFIGURED;
  737. cx231xx_uninit_isoc(dev);
  738. dev->state |= DEV_DISCONNECTED;
  739. wake_up_interruptible(&dev->wait_frame);
  740. wake_up_interruptible(&dev->wait_stream);
  741. } else {
  742. dev->state |= DEV_DISCONNECTED;
  743. cx231xx_release_resources(dev);
  744. }
  745. cx231xx_close_extension(dev);
  746. mutex_unlock(&dev->lock);
  747. if (!dev->users) {
  748. kfree(dev->video_mode.alt_max_pkt_size);
  749. kfree(dev->vbi_mode.alt_max_pkt_size);
  750. kfree(dev->sliced_cc_mode.alt_max_pkt_size);
  751. kfree(dev->ts1_mode.alt_max_pkt_size);
  752. kfree(dev);
  753. }
  754. }
  755. static struct usb_driver cx231xx_usb_driver = {
  756. .name = "cx231xx",
  757. .probe = cx231xx_usb_probe,
  758. .disconnect = cx231xx_usb_disconnect,
  759. .id_table = cx231xx_id_table,
  760. };
  761. static int __init cx231xx_module_init(void)
  762. {
  763. int result;
  764. printk(KERN_INFO DRIVER_NAME " v4l2 driver loaded.\n");
  765. /* register this driver with the USB subsystem */
  766. result = usb_register(&cx231xx_usb_driver);
  767. if (result)
  768. cx231xx_err(DRIVER_NAME
  769. " usb_register failed. Error number %d.\n", result);
  770. return result;
  771. }
  772. static void __exit cx231xx_module_exit(void)
  773. {
  774. /* deregister this driver with the USB subsystem */
  775. usb_deregister(&cx231xx_usb_driver);
  776. }
  777. module_init(cx231xx_module_init);
  778. module_exit(cx231xx_module_exit);