cx231xx-cards.c 25 KB

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