cx231xx-cards.c 25 KB

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