aircable.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * AIRcable USB Bluetooth Dongle Driver.
  3. *
  4. * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
  5. * This program is free software; you can redistribute it and/or modify it under
  6. * the terms of the GNU General Public License version 2 as published by the
  7. * Free Software Foundation.
  8. *
  9. * The device works as an standard CDC device, it has 2 interfaces, the first
  10. * one is for firmware access and the second is the serial one.
  11. * The protocol is very simply, there are two posibilities reading or writing.
  12. * When writting the first urb must have a Header that starts with 0x20 0x29 the
  13. * next two bytes must say how much data will be sended.
  14. * When reading the process is almost equal except that the header starts with
  15. * 0x00 0x20.
  16. *
  17. * The device simply need some stuff to understand data comming from the usb
  18. * buffer: The First and Second byte is used for a Header, the Third and Fourth
  19. * tells the device the amount of information the package holds.
  20. * Packages are 60 bytes long Header Stuff.
  21. * When writting to the device the first two bytes of the header are 0x20 0x29
  22. * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
  23. * situation, when too much data arrives to the device because it sends the data
  24. * but with out the header. I will use a simply hack to override this situation,
  25. * if there is data coming that does not contain any header, then that is data
  26. * that must go directly to the tty, as there is no documentation about if there
  27. * is any other control code, I will simply check for the first
  28. * one.
  29. *
  30. * The driver registers himself with the USB-serial core and the USB Core. I had
  31. * to implement a probe function agains USB-serial, because other way, the
  32. * driver was attaching himself to both interfaces. I have tryed with different
  33. * configurations of usb_serial_driver with out exit, only the probe function
  34. * could handle this correctly.
  35. *
  36. * I have taken some info from a Greg Kroah-Hartman article:
  37. * http://www.linuxjournal.com/article/6573
  38. * And from Linux Device Driver Kit CD, which is a great work, the authors taken
  39. * the work to recompile lots of information an knowladge in drivers development
  40. * and made it all avaible inside a cd.
  41. * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
  42. *
  43. */
  44. #include <linux/tty.h>
  45. #include <linux/tty_flip.h>
  46. #include <linux/circ_buf.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/serial.h>
  49. static int debug;
  50. /* Vendor and Product ID */
  51. #define AIRCABLE_VID 0x16CA
  52. #define AIRCABLE_USB_PID 0x1502
  53. /* write buffer size defines */
  54. #define AIRCABLE_BUF_SIZE 2048
  55. /* Protocol Stuff */
  56. #define HCI_HEADER_LENGTH 0x4
  57. #define TX_HEADER_0 0x20
  58. #define TX_HEADER_1 0x29
  59. #define RX_HEADER_0 0x00
  60. #define RX_HEADER_1 0x20
  61. #define MAX_HCI_FRAMESIZE 60
  62. #define HCI_COMPLETE_FRAME 64
  63. /* rx_flags */
  64. #define THROTTLED 0x01
  65. #define ACTUALLY_THROTTLED 0x02
  66. /*
  67. * Version Information
  68. */
  69. #define DRIVER_VERSION "v1.0b2"
  70. #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>"
  71. #define DRIVER_DESC "AIRcable USB Driver"
  72. /* ID table that will be registered with USB core */
  73. static struct usb_device_id id_table [] = {
  74. { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
  75. { },
  76. };
  77. MODULE_DEVICE_TABLE(usb, id_table);
  78. /* Internal Structure */
  79. struct aircable_private {
  80. spinlock_t rx_lock; /* spinlock for the receive lines */
  81. struct circ_buf *tx_buf; /* write buffer */
  82. struct circ_buf *rx_buf; /* read buffer */
  83. int rx_flags; /* for throttilng */
  84. struct work_struct rx_work; /* work cue for the receiving line */
  85. };
  86. /* Private methods */
  87. /* Circular Buffer Methods, code from ti_usb_3410_5052 used */
  88. /*
  89. * serial_buf_clear
  90. *
  91. * Clear out all data in the circular buffer.
  92. */
  93. static void serial_buf_clear(struct circ_buf *cb)
  94. {
  95. cb->head = cb->tail = 0;
  96. }
  97. /*
  98. * serial_buf_alloc
  99. *
  100. * Allocate a circular buffer and all associated memory.
  101. */
  102. static struct circ_buf *serial_buf_alloc(void)
  103. {
  104. struct circ_buf *cb;
  105. cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
  106. if (cb == NULL)
  107. return NULL;
  108. cb->buf = kmalloc(AIRCABLE_BUF_SIZE, GFP_KERNEL);
  109. if (cb->buf == NULL) {
  110. kfree(cb);
  111. return NULL;
  112. }
  113. serial_buf_clear(cb);
  114. return cb;
  115. }
  116. /*
  117. * serial_buf_free
  118. *
  119. * Free the buffer and all associated memory.
  120. */
  121. static void serial_buf_free(struct circ_buf *cb)
  122. {
  123. kfree(cb->buf);
  124. kfree(cb);
  125. }
  126. /*
  127. * serial_buf_data_avail
  128. *
  129. * Return the number of bytes of data available in the circular
  130. * buffer.
  131. */
  132. static int serial_buf_data_avail(struct circ_buf *cb)
  133. {
  134. return CIRC_CNT(cb->head,cb->tail,AIRCABLE_BUF_SIZE);
  135. }
  136. /*
  137. * serial_buf_put
  138. *
  139. * Copy data data from a user buffer and put it into the circular buffer.
  140. * Restrict to the amount of space available.
  141. *
  142. * Return the number of bytes copied.
  143. */
  144. static int serial_buf_put(struct circ_buf *cb, const char *buf, int count)
  145. {
  146. int c, ret = 0;
  147. while (1) {
  148. c = CIRC_SPACE_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
  149. if (count < c)
  150. c = count;
  151. if (c <= 0)
  152. break;
  153. memcpy(cb->buf + cb->head, buf, c);
  154. cb->head = (cb->head + c) & (AIRCABLE_BUF_SIZE-1);
  155. buf += c;
  156. count -= c;
  157. ret= c;
  158. }
  159. return ret;
  160. }
  161. /*
  162. * serial_buf_get
  163. *
  164. * Get data from the circular buffer and copy to the given buffer.
  165. * Restrict to the amount of data available.
  166. *
  167. * Return the number of bytes copied.
  168. */
  169. static int serial_buf_get(struct circ_buf *cb, char *buf, int count)
  170. {
  171. int c, ret = 0;
  172. while (1) {
  173. c = CIRC_CNT_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
  174. if (count < c)
  175. c = count;
  176. if (c <= 0)
  177. break;
  178. memcpy(buf, cb->buf + cb->tail, c);
  179. cb->tail = (cb->tail + c) & (AIRCABLE_BUF_SIZE-1);
  180. buf += c;
  181. count -= c;
  182. ret= c;
  183. }
  184. return ret;
  185. }
  186. /* End of circula buffer methods */
  187. static void aircable_send(struct usb_serial_port *port)
  188. {
  189. int count, result;
  190. struct aircable_private *priv = usb_get_serial_port_data(port);
  191. unsigned char* buf;
  192. dbg("%s - port %d", __FUNCTION__, port->number);
  193. if (port->write_urb_busy)
  194. return;
  195. count = min(serial_buf_data_avail(priv->tx_buf), MAX_HCI_FRAMESIZE);
  196. if (count == 0)
  197. return;
  198. buf = kzalloc(count + HCI_HEADER_LENGTH, GFP_ATOMIC);
  199. if (!buf) {
  200. err("%s- kzalloc(%d) failed.", __FUNCTION__,
  201. count + HCI_HEADER_LENGTH);
  202. return;
  203. }
  204. buf[0] = TX_HEADER_0;
  205. buf[1] = TX_HEADER_1;
  206. buf[2] = (unsigned char)count;
  207. buf[3] = (unsigned char)(count >> 8);
  208. serial_buf_get(priv->tx_buf,buf + HCI_HEADER_LENGTH, MAX_HCI_FRAMESIZE);
  209. memcpy(port->write_urb->transfer_buffer, buf,
  210. count + HCI_HEADER_LENGTH);
  211. kfree(buf);
  212. port->write_urb_busy = 1;
  213. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  214. count + HCI_HEADER_LENGTH,
  215. port->write_urb->transfer_buffer);
  216. port->write_urb->transfer_buffer_length = count + HCI_HEADER_LENGTH;
  217. port->write_urb->dev = port->serial->dev;
  218. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  219. if (result) {
  220. dev_err(&port->dev,
  221. "%s - failed submitting write urb, error %d\n",
  222. __FUNCTION__, result);
  223. port->write_urb_busy = 0;
  224. }
  225. schedule_work(&port->work);
  226. }
  227. static void aircable_read(void *params)
  228. {
  229. struct usb_serial_port *port = params;
  230. struct aircable_private *priv = usb_get_serial_port_data(port);
  231. struct tty_struct *tty;
  232. unsigned char *data;
  233. int count;
  234. if (priv->rx_flags & THROTTLED){
  235. if (priv->rx_flags & ACTUALLY_THROTTLED)
  236. schedule_work(&priv->rx_work);
  237. return;
  238. }
  239. /* By now I will flush data to the tty in packages of no more than
  240. * 64 bytes, to ensure I do not get throttled.
  241. * Ask USB mailing list for better aproach.
  242. */
  243. tty = port->tty;
  244. if (!tty) {
  245. schedule_work(&priv->rx_work);
  246. err("%s - No tty available", __FUNCTION__);
  247. return ;
  248. }
  249. count = min(64, serial_buf_data_avail(priv->rx_buf));
  250. if (count <= 0)
  251. return; //We have finished sending everything.
  252. tty_prepare_flip_string(tty, &data, count);
  253. if (!data){
  254. err("%s- kzalloc(%d) failed.", __FUNCTION__, count);
  255. return;
  256. }
  257. serial_buf_get(priv->rx_buf, data, count);
  258. tty_flip_buffer_push(tty);
  259. if (serial_buf_data_avail(priv->rx_buf))
  260. schedule_work(&priv->rx_work);
  261. return;
  262. }
  263. /* End of private methods */
  264. static int aircable_probe(struct usb_serial *serial,
  265. const struct usb_device_id *id)
  266. {
  267. struct usb_host_interface *iface_desc = serial->interface->cur_altsetting;
  268. struct usb_endpoint_descriptor *endpoint;
  269. int num_bulk_out=0;
  270. int i;
  271. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  272. endpoint = &iface_desc->endpoint[i].desc;
  273. if (usb_endpoint_is_bulk_out(endpoint)) {
  274. dbg("found bulk out on endpoint %d", i);
  275. ++num_bulk_out;
  276. }
  277. }
  278. if (num_bulk_out == 0) {
  279. dbg("Invalid interface, discarding");
  280. return -ENODEV;
  281. }
  282. return 0;
  283. }
  284. static int aircable_attach (struct usb_serial *serial)
  285. {
  286. struct usb_serial_port *port = serial->port[0];
  287. struct aircable_private *priv;
  288. priv = kzalloc(sizeof(struct aircable_private), GFP_KERNEL);
  289. if (!priv){
  290. err("%s- kmalloc(%Zd) failed.", __FUNCTION__,
  291. sizeof(struct aircable_private));
  292. return -ENOMEM;
  293. }
  294. /* Allocation of Circular Buffers */
  295. priv->tx_buf = serial_buf_alloc();
  296. if (priv->tx_buf == NULL) {
  297. kfree(priv);
  298. return -ENOMEM;
  299. }
  300. priv->rx_buf = serial_buf_alloc();
  301. if (priv->rx_buf == NULL) {
  302. kfree(priv->tx_buf);
  303. kfree(priv);
  304. return -ENOMEM;
  305. }
  306. priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  307. INIT_WORK(&priv->rx_work, aircable_read, port);
  308. usb_set_serial_port_data(serial->port[0], priv);
  309. return 0;
  310. }
  311. static void aircable_shutdown(struct usb_serial *serial)
  312. {
  313. struct usb_serial_port *port = serial->port[0];
  314. struct aircable_private *priv = usb_get_serial_port_data(port);
  315. dbg("%s", __FUNCTION__);
  316. if (priv) {
  317. serial_buf_free(priv->tx_buf);
  318. serial_buf_free(priv->rx_buf);
  319. usb_set_serial_port_data(port, NULL);
  320. kfree(priv);
  321. }
  322. }
  323. static int aircable_write_room(struct usb_serial_port *port)
  324. {
  325. struct aircable_private *priv = usb_get_serial_port_data(port);
  326. return serial_buf_data_avail(priv->tx_buf);
  327. }
  328. static int aircable_write(struct usb_serial_port *port,
  329. const unsigned char *source, int count)
  330. {
  331. struct aircable_private *priv = usb_get_serial_port_data(port);
  332. int temp;
  333. dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
  334. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, source);
  335. if (!count){
  336. dbg("%s - write request of 0 bytes", __FUNCTION__);
  337. return count;
  338. }
  339. temp = serial_buf_put(priv->tx_buf, source, count);
  340. aircable_send(port);
  341. if (count > AIRCABLE_BUF_SIZE)
  342. count = AIRCABLE_BUF_SIZE;
  343. return count;
  344. }
  345. static void aircable_write_bulk_callback(struct urb *urb)
  346. {
  347. struct usb_serial_port *port = urb->context;
  348. int result;
  349. dbg("%s - urb->status: %d", __FUNCTION__ , urb->status);
  350. /* This has been taken from cypress_m8.c cypress_write_int_callback */
  351. switch (urb->status) {
  352. case 0:
  353. /* success */
  354. break;
  355. case -ECONNRESET:
  356. case -ENOENT:
  357. case -ESHUTDOWN:
  358. /* this urb is terminated, clean up */
  359. dbg("%s - urb shutting down with status: %d",
  360. __FUNCTION__, urb->status);
  361. port->write_urb_busy = 0;
  362. return;
  363. default:
  364. /* error in the urb, so we have to resubmit it */
  365. dbg("%s - Overflow in write", __FUNCTION__);
  366. dbg("%s - nonzero write bulk status received: %d",
  367. __FUNCTION__, urb->status);
  368. port->write_urb->transfer_buffer_length = 1;
  369. port->write_urb->dev = port->serial->dev;
  370. result = usb_submit_urb(port->write_urb, GFP_KERNEL);
  371. if (result)
  372. dev_err(&urb->dev->dev,
  373. "%s - failed resubmitting write urb, error %d\n",
  374. __FUNCTION__, result);
  375. else
  376. return;
  377. }
  378. port->write_urb_busy = 0;
  379. aircable_send(port);
  380. }
  381. static void aircable_read_bulk_callback(struct urb *urb)
  382. {
  383. struct usb_serial_port *port = urb->context;
  384. struct aircable_private *priv = usb_get_serial_port_data(port);
  385. struct tty_struct *tty;
  386. unsigned long no_packages, remaining, package_length, i;
  387. int result, shift = 0;
  388. unsigned char *temp;
  389. dbg("%s - port %d", __FUNCTION__, port->number);
  390. if (urb->status) {
  391. dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
  392. if (!port->open_count) {
  393. dbg("%s - port is closed, exiting.", __FUNCTION__);
  394. return;
  395. }
  396. if (urb->status == -EPROTO) {
  397. dbg("%s - caught -EPROTO, resubmitting the urb",
  398. __FUNCTION__);
  399. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  400. usb_rcvbulkpipe(port->serial->dev,
  401. port->bulk_in_endpointAddress),
  402. port->read_urb->transfer_buffer,
  403. port->read_urb->transfer_buffer_length,
  404. aircable_read_bulk_callback, port);
  405. result = usb_submit_urb(urb, GFP_ATOMIC);
  406. if (result)
  407. dev_err(&urb->dev->dev,
  408. "%s - failed resubmitting read urb, error %d\n",
  409. __FUNCTION__, result);
  410. return;
  411. }
  412. dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
  413. return;
  414. }
  415. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  416. urb->actual_length,urb->transfer_buffer);
  417. tty = port->tty;
  418. if (tty && urb->actual_length) {
  419. if (urb->actual_length <= 2) {
  420. /* This is an incomplete package */
  421. serial_buf_put(priv->rx_buf, urb->transfer_buffer,
  422. urb->actual_length);
  423. } else {
  424. temp = urb->transfer_buffer;
  425. if (temp[0] == RX_HEADER_0)
  426. shift = HCI_HEADER_LENGTH;
  427. remaining = urb->actual_length;
  428. no_packages = urb->actual_length / (HCI_COMPLETE_FRAME);
  429. if (urb->actual_length % HCI_COMPLETE_FRAME != 0)
  430. no_packages+=1;
  431. for (i = 0; i < no_packages ;i++) {
  432. if (remaining > (HCI_COMPLETE_FRAME))
  433. package_length = HCI_COMPLETE_FRAME;
  434. else
  435. package_length = remaining;
  436. remaining -= package_length;
  437. serial_buf_put(priv->rx_buf,
  438. urb->transfer_buffer + shift +
  439. (HCI_COMPLETE_FRAME) * (i),
  440. package_length - shift);
  441. }
  442. }
  443. aircable_read(port);
  444. }
  445. /* Schedule the next read _if_ we are still open */
  446. if (port->open_count) {
  447. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  448. usb_rcvbulkpipe(port->serial->dev,
  449. port->bulk_in_endpointAddress),
  450. port->read_urb->transfer_buffer,
  451. port->read_urb->transfer_buffer_length,
  452. aircable_read_bulk_callback, port);
  453. result = usb_submit_urb(urb, GFP_ATOMIC);
  454. if (result)
  455. dev_err(&urb->dev->dev,
  456. "%s - failed resubmitting read urb, error %d\n",
  457. __FUNCTION__, result);
  458. }
  459. return;
  460. }
  461. /* Based on ftdi_sio.c throttle */
  462. static void aircable_throttle(struct usb_serial_port *port)
  463. {
  464. struct aircable_private *priv = usb_get_serial_port_data(port);
  465. unsigned long flags;
  466. dbg("%s - port %d", __FUNCTION__, port->number);
  467. spin_lock_irqsave(&priv->rx_lock, flags);
  468. priv->rx_flags |= THROTTLED;
  469. spin_unlock_irqrestore(&priv->rx_lock, flags);
  470. }
  471. /* Based on ftdi_sio.c unthrottle */
  472. static void aircable_unthrottle(struct usb_serial_port *port)
  473. {
  474. struct aircable_private *priv = usb_get_serial_port_data(port);
  475. int actually_throttled;
  476. unsigned long flags;
  477. dbg("%s - port %d", __FUNCTION__, port->number);
  478. spin_lock_irqsave(&priv->rx_lock, flags);
  479. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  480. priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  481. spin_unlock_irqrestore(&priv->rx_lock, flags);
  482. if (actually_throttled)
  483. schedule_work(&priv->rx_work);
  484. }
  485. static struct usb_serial_driver aircable_device = {
  486. .description = "aircable",
  487. .id_table = id_table,
  488. .num_ports = 1,
  489. .attach = aircable_attach,
  490. .probe = aircable_probe,
  491. .shutdown = aircable_shutdown,
  492. .write = aircable_write,
  493. .write_room = aircable_write_room,
  494. .write_bulk_callback = aircable_write_bulk_callback,
  495. .read_bulk_callback = aircable_read_bulk_callback,
  496. .throttle = aircable_throttle,
  497. .unthrottle = aircable_unthrottle,
  498. };
  499. static struct usb_driver aircable_driver = {
  500. .name = "aircable",
  501. .probe = usb_serial_probe,
  502. .disconnect = usb_serial_disconnect,
  503. .id_table = id_table,
  504. };
  505. static int __init aircable_init (void)
  506. {
  507. int retval;
  508. retval = usb_serial_register(&aircable_device);
  509. if (retval)
  510. goto failed_serial_register;
  511. retval = usb_register(&aircable_driver);
  512. if (retval)
  513. goto failed_usb_register;
  514. return 0;
  515. failed_serial_register:
  516. usb_serial_deregister(&aircable_device);
  517. failed_usb_register:
  518. return retval;
  519. }
  520. static void __exit aircable_exit (void)
  521. {
  522. usb_deregister(&aircable_driver);
  523. usb_serial_deregister(&aircable_device);
  524. }
  525. MODULE_AUTHOR(DRIVER_AUTHOR);
  526. MODULE_DESCRIPTION(DRIVER_DESC);
  527. MODULE_VERSION(DRIVER_VERSION);
  528. MODULE_LICENSE("GPL");
  529. module_init(aircable_init);
  530. module_exit(aircable_exit);
  531. module_param(debug, bool, S_IRUGO | S_IWUSR);
  532. MODULE_PARM_DESC(debug, "Debug enabled or not");