cx231xx-cards.c 23 KB

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