as102_usb_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Abilis Systems Single DVB-T Receiver
  3. * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
  4. * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
  5. *
  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, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/mm.h>
  20. #include <linux/usb.h>
  21. #include "as102_drv.h"
  22. #include "as102_usb_drv.h"
  23. #include "as102_fw.h"
  24. static void as102_usb_disconnect(struct usb_interface *interface);
  25. static int as102_usb_probe(struct usb_interface *interface,
  26. const struct usb_device_id *id);
  27. static int as102_usb_start_stream(struct as102_dev_t *dev);
  28. static void as102_usb_stop_stream(struct as102_dev_t *dev);
  29. static int as102_open(struct inode *inode, struct file *file);
  30. static int as102_release(struct inode *inode, struct file *file);
  31. static struct usb_device_id as102_usb_id_table[] = {
  32. { USB_DEVICE(AS102_USB_DEVICE_VENDOR_ID, AS102_USB_DEVICE_PID_0001) },
  33. { USB_DEVICE(PCTV_74E_USB_VID, PCTV_74E_USB_PID) },
  34. { USB_DEVICE(ELGATO_EYETV_DTT_USB_VID, ELGATO_EYETV_DTT_USB_PID) },
  35. { USB_DEVICE(NBOX_DVBT_DONGLE_USB_VID, NBOX_DVBT_DONGLE_USB_PID) },
  36. { USB_DEVICE(SKY_IT_DIGITAL_KEY_USB_VID, SKY_IT_DIGITAL_KEY_USB_PID) },
  37. { } /* Terminating entry */
  38. };
  39. /* Note that this table must always have the same number of entries as the
  40. as102_usb_id_table struct */
  41. static const char * const as102_device_names[] = {
  42. AS102_REFERENCE_DESIGN,
  43. AS102_PCTV_74E,
  44. AS102_ELGATO_EYETV_DTT_NAME,
  45. AS102_NBOX_DVBT_DONGLE_NAME,
  46. AS102_SKY_IT_DIGITAL_KEY_NAME,
  47. NULL /* Terminating entry */
  48. };
  49. /* eLNA configuration: devices built on the reference design work best
  50. with 0xA0, while custom designs seem to require 0xC0 */
  51. static uint8_t const as102_elna_cfg[] = {
  52. 0xA0,
  53. 0xC0,
  54. 0xC0,
  55. 0xA0,
  56. 0xA0,
  57. 0x00 /* Terminating entry */
  58. };
  59. struct usb_driver as102_usb_driver = {
  60. .name = DRIVER_FULL_NAME,
  61. .probe = as102_usb_probe,
  62. .disconnect = as102_usb_disconnect,
  63. .id_table = as102_usb_id_table
  64. };
  65. static const struct file_operations as102_dev_fops = {
  66. .owner = THIS_MODULE,
  67. .open = as102_open,
  68. .release = as102_release,
  69. };
  70. static struct usb_class_driver as102_usb_class_driver = {
  71. .name = "aton2-%d",
  72. .fops = &as102_dev_fops,
  73. .minor_base = AS102_DEVICE_MAJOR,
  74. };
  75. static int as102_usb_xfer_cmd(struct as10x_bus_adapter_t *bus_adap,
  76. unsigned char *send_buf, int send_buf_len,
  77. unsigned char *recv_buf, int recv_buf_len)
  78. {
  79. int ret = 0;
  80. if (send_buf != NULL) {
  81. ret = usb_control_msg(bus_adap->usb_dev,
  82. usb_sndctrlpipe(bus_adap->usb_dev, 0),
  83. AS102_USB_DEVICE_TX_CTRL_CMD,
  84. USB_DIR_OUT | USB_TYPE_VENDOR |
  85. USB_RECIP_DEVICE,
  86. bus_adap->cmd_xid, /* value */
  87. 0, /* index */
  88. send_buf, send_buf_len,
  89. USB_CTRL_SET_TIMEOUT /* 200 */);
  90. if (ret < 0) {
  91. dev_dbg(&bus_adap->usb_dev->dev,
  92. "usb_control_msg(send) failed, err %i\n", ret);
  93. return ret;
  94. }
  95. if (ret != send_buf_len) {
  96. dev_dbg(&bus_adap->usb_dev->dev,
  97. "only wrote %d of %d bytes\n", ret, send_buf_len);
  98. return -1;
  99. }
  100. }
  101. if (recv_buf != NULL) {
  102. #ifdef TRACE
  103. dev_dbg(bus_adap->usb_dev->dev,
  104. "want to read: %d bytes\n", recv_buf_len);
  105. #endif
  106. ret = usb_control_msg(bus_adap->usb_dev,
  107. usb_rcvctrlpipe(bus_adap->usb_dev, 0),
  108. AS102_USB_DEVICE_RX_CTRL_CMD,
  109. USB_DIR_IN | USB_TYPE_VENDOR |
  110. USB_RECIP_DEVICE,
  111. bus_adap->cmd_xid, /* value */
  112. 0, /* index */
  113. recv_buf, recv_buf_len,
  114. USB_CTRL_GET_TIMEOUT /* 200 */);
  115. if (ret < 0) {
  116. dev_dbg(&bus_adap->usb_dev->dev,
  117. "usb_control_msg(recv) failed, err %i\n", ret);
  118. return ret;
  119. }
  120. #ifdef TRACE
  121. dev_dbg(bus_adap->usb_dev->dev,
  122. "read %d bytes\n", recv_buf_len);
  123. #endif
  124. }
  125. return ret;
  126. }
  127. static int as102_send_ep1(struct as10x_bus_adapter_t *bus_adap,
  128. unsigned char *send_buf,
  129. int send_buf_len,
  130. int swap32)
  131. {
  132. int ret, actual_len;
  133. ret = usb_bulk_msg(bus_adap->usb_dev,
  134. usb_sndbulkpipe(bus_adap->usb_dev, 1),
  135. send_buf, send_buf_len, &actual_len, 200);
  136. if (ret) {
  137. dev_dbg(&bus_adap->usb_dev->dev,
  138. "usb_bulk_msg(send) failed, err %i\n", ret);
  139. return ret;
  140. }
  141. if (actual_len != send_buf_len) {
  142. dev_dbg(&bus_adap->usb_dev->dev, "only wrote %d of %d bytes\n",
  143. actual_len, send_buf_len);
  144. return -1;
  145. }
  146. return actual_len;
  147. }
  148. static int as102_read_ep2(struct as10x_bus_adapter_t *bus_adap,
  149. unsigned char *recv_buf, int recv_buf_len)
  150. {
  151. int ret, actual_len;
  152. if (recv_buf == NULL)
  153. return -EINVAL;
  154. ret = usb_bulk_msg(bus_adap->usb_dev,
  155. usb_rcvbulkpipe(bus_adap->usb_dev, 2),
  156. recv_buf, recv_buf_len, &actual_len, 200);
  157. if (ret) {
  158. dev_dbg(&bus_adap->usb_dev->dev,
  159. "usb_bulk_msg(recv) failed, err %i\n", ret);
  160. return ret;
  161. }
  162. if (actual_len != recv_buf_len) {
  163. dev_dbg(&bus_adap->usb_dev->dev, "only read %d of %d bytes\n",
  164. actual_len, recv_buf_len);
  165. return -1;
  166. }
  167. return actual_len;
  168. }
  169. static struct as102_priv_ops_t as102_priv_ops = {
  170. .upload_fw_pkt = as102_send_ep1,
  171. .xfer_cmd = as102_usb_xfer_cmd,
  172. .as102_read_ep2 = as102_read_ep2,
  173. .start_stream = as102_usb_start_stream,
  174. .stop_stream = as102_usb_stop_stream,
  175. };
  176. static int as102_submit_urb_stream(struct as102_dev_t *dev, struct urb *urb)
  177. {
  178. int err;
  179. usb_fill_bulk_urb(urb,
  180. dev->bus_adap.usb_dev,
  181. usb_rcvbulkpipe(dev->bus_adap.usb_dev, 0x2),
  182. urb->transfer_buffer,
  183. AS102_USB_BUF_SIZE,
  184. as102_urb_stream_irq,
  185. dev);
  186. err = usb_submit_urb(urb, GFP_ATOMIC);
  187. if (err)
  188. dev_dbg(&urb->dev->dev,
  189. "%s: usb_submit_urb failed\n", __func__);
  190. return err;
  191. }
  192. void as102_urb_stream_irq(struct urb *urb)
  193. {
  194. struct as102_dev_t *as102_dev = urb->context;
  195. if (urb->actual_length > 0) {
  196. dvb_dmx_swfilter(&as102_dev->dvb_dmx,
  197. urb->transfer_buffer,
  198. urb->actual_length);
  199. } else {
  200. if (urb->actual_length == 0)
  201. memset(urb->transfer_buffer, 0, AS102_USB_BUF_SIZE);
  202. }
  203. /* is not stopped, re-submit urb */
  204. if (as102_dev->streaming)
  205. as102_submit_urb_stream(as102_dev, urb);
  206. }
  207. static void as102_free_usb_stream_buffer(struct as102_dev_t *dev)
  208. {
  209. int i;
  210. for (i = 0; i < MAX_STREAM_URB; i++)
  211. usb_free_urb(dev->stream_urb[i]);
  212. usb_free_coherent(dev->bus_adap.usb_dev,
  213. MAX_STREAM_URB * AS102_USB_BUF_SIZE,
  214. dev->stream,
  215. dev->dma_addr);
  216. }
  217. static int as102_alloc_usb_stream_buffer(struct as102_dev_t *dev)
  218. {
  219. int i;
  220. dev->stream = usb_alloc_coherent(dev->bus_adap.usb_dev,
  221. MAX_STREAM_URB * AS102_USB_BUF_SIZE,
  222. GFP_KERNEL,
  223. &dev->dma_addr);
  224. if (!dev->stream) {
  225. dev_dbg(&dev->bus_adap.usb_dev->dev,
  226. "%s: usb_buffer_alloc failed\n", __func__);
  227. return -ENOMEM;
  228. }
  229. memset(dev->stream, 0, MAX_STREAM_URB * AS102_USB_BUF_SIZE);
  230. /* init urb buffers */
  231. for (i = 0; i < MAX_STREAM_URB; i++) {
  232. struct urb *urb;
  233. urb = usb_alloc_urb(0, GFP_ATOMIC);
  234. if (urb == NULL) {
  235. dev_dbg(&dev->bus_adap.usb_dev->dev,
  236. "%s: usb_alloc_urb failed\n", __func__);
  237. as102_free_usb_stream_buffer(dev);
  238. return -ENOMEM;
  239. }
  240. urb->transfer_buffer = dev->stream + (i * AS102_USB_BUF_SIZE);
  241. urb->transfer_dma = dev->dma_addr + (i * AS102_USB_BUF_SIZE);
  242. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  243. urb->transfer_buffer_length = AS102_USB_BUF_SIZE;
  244. dev->stream_urb[i] = urb;
  245. }
  246. return 0;
  247. }
  248. static void as102_usb_stop_stream(struct as102_dev_t *dev)
  249. {
  250. int i;
  251. for (i = 0; i < MAX_STREAM_URB; i++)
  252. usb_kill_urb(dev->stream_urb[i]);
  253. }
  254. static int as102_usb_start_stream(struct as102_dev_t *dev)
  255. {
  256. int i, ret = 0;
  257. for (i = 0; i < MAX_STREAM_URB; i++) {
  258. ret = as102_submit_urb_stream(dev, dev->stream_urb[i]);
  259. if (ret) {
  260. as102_usb_stop_stream(dev);
  261. return ret;
  262. }
  263. }
  264. return 0;
  265. }
  266. static void as102_usb_release(struct kref *kref)
  267. {
  268. struct as102_dev_t *as102_dev;
  269. as102_dev = container_of(kref, struct as102_dev_t, kref);
  270. if (as102_dev != NULL) {
  271. usb_put_dev(as102_dev->bus_adap.usb_dev);
  272. kfree(as102_dev);
  273. }
  274. }
  275. static void as102_usb_disconnect(struct usb_interface *intf)
  276. {
  277. struct as102_dev_t *as102_dev;
  278. /* extract as102_dev_t from usb_device private data */
  279. as102_dev = usb_get_intfdata(intf);
  280. /* unregister dvb layer */
  281. as102_dvb_unregister(as102_dev);
  282. /* free usb buffers */
  283. as102_free_usb_stream_buffer(as102_dev);
  284. usb_set_intfdata(intf, NULL);
  285. /* usb unregister device */
  286. usb_deregister_dev(intf, &as102_usb_class_driver);
  287. /* decrement usage counter */
  288. kref_put(&as102_dev->kref, as102_usb_release);
  289. pr_info("%s: device has been disconnected\n", DRIVER_NAME);
  290. }
  291. static int as102_usb_probe(struct usb_interface *intf,
  292. const struct usb_device_id *id)
  293. {
  294. int ret;
  295. struct as102_dev_t *as102_dev;
  296. int i;
  297. /* This should never actually happen */
  298. if (ARRAY_SIZE(as102_usb_id_table) !=
  299. (sizeof(as102_device_names) / sizeof(const char *))) {
  300. pr_err("Device names table invalid size");
  301. return -EINVAL;
  302. }
  303. as102_dev = kzalloc(sizeof(struct as102_dev_t), GFP_KERNEL);
  304. if (as102_dev == NULL)
  305. return -ENOMEM;
  306. /* Assign the user-friendly device name */
  307. for (i = 0; i < ARRAY_SIZE(as102_usb_id_table); i++) {
  308. if (id == &as102_usb_id_table[i]) {
  309. as102_dev->name = as102_device_names[i];
  310. as102_dev->elna_cfg = as102_elna_cfg[i];
  311. }
  312. }
  313. if (as102_dev->name == NULL)
  314. as102_dev->name = "Unknown AS102 device";
  315. /* set private callback functions */
  316. as102_dev->bus_adap.ops = &as102_priv_ops;
  317. /* init cmd token for usb bus */
  318. as102_dev->bus_adap.cmd = &as102_dev->bus_adap.token.usb.c;
  319. as102_dev->bus_adap.rsp = &as102_dev->bus_adap.token.usb.r;
  320. /* init kernel device reference */
  321. kref_init(&as102_dev->kref);
  322. /* store as102 device to usb_device private data */
  323. usb_set_intfdata(intf, (void *) as102_dev);
  324. /* store in as102 device the usb_device pointer */
  325. as102_dev->bus_adap.usb_dev = usb_get_dev(interface_to_usbdev(intf));
  326. /* we can register the device now, as it is ready */
  327. ret = usb_register_dev(intf, &as102_usb_class_driver);
  328. if (ret < 0) {
  329. /* something prevented us from registering this driver */
  330. dev_err(&intf->dev,
  331. "%s: usb_register_dev() failed (errno = %d)\n",
  332. __func__, ret);
  333. goto failed;
  334. }
  335. pr_info("%s: device has been detected\n", DRIVER_NAME);
  336. /* request buffer allocation for streaming */
  337. ret = as102_alloc_usb_stream_buffer(as102_dev);
  338. if (ret != 0)
  339. goto failed_stream;
  340. /* register dvb layer */
  341. ret = as102_dvb_register(as102_dev);
  342. if (ret != 0)
  343. goto failed_dvb;
  344. return ret;
  345. failed_dvb:
  346. as102_free_usb_stream_buffer(as102_dev);
  347. failed_stream:
  348. usb_deregister_dev(intf, &as102_usb_class_driver);
  349. failed:
  350. usb_put_dev(as102_dev->bus_adap.usb_dev);
  351. usb_set_intfdata(intf, NULL);
  352. kfree(as102_dev);
  353. return ret;
  354. }
  355. static int as102_open(struct inode *inode, struct file *file)
  356. {
  357. int ret = 0, minor = 0;
  358. struct usb_interface *intf = NULL;
  359. struct as102_dev_t *dev = NULL;
  360. /* read minor from inode */
  361. minor = iminor(inode);
  362. /* fetch device from usb interface */
  363. intf = usb_find_interface(&as102_usb_driver, minor);
  364. if (intf == NULL) {
  365. pr_err("%s: can't find device for minor %d\n",
  366. __func__, minor);
  367. ret = -ENODEV;
  368. goto exit;
  369. }
  370. /* get our device */
  371. dev = usb_get_intfdata(intf);
  372. if (dev == NULL) {
  373. ret = -EFAULT;
  374. goto exit;
  375. }
  376. /* save our device object in the file's private structure */
  377. file->private_data = dev;
  378. /* increment our usage count for the device */
  379. kref_get(&dev->kref);
  380. exit:
  381. return ret;
  382. }
  383. static int as102_release(struct inode *inode, struct file *file)
  384. {
  385. struct as102_dev_t *dev = NULL;
  386. dev = file->private_data;
  387. if (dev != NULL) {
  388. /* decrement the count on our device */
  389. kref_put(&dev->kref, as102_usb_release);
  390. }
  391. return 0;
  392. }
  393. MODULE_DEVICE_TABLE(usb, as102_usb_id_table);