cx231xx-cards.c 23 KB

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