aircable.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 writing 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 writing 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/slab.h>
  46. #include <linux/tty_flip.h>
  47. #include <linux/circ_buf.h>
  48. #include <linux/usb.h>
  49. #include <linux/usb/serial.h>
  50. static int debug;
  51. /* Vendor and Product ID */
  52. #define AIRCABLE_VID 0x16CA
  53. #define AIRCABLE_USB_PID 0x1502
  54. /* write buffer size defines */
  55. #define AIRCABLE_BUF_SIZE 2048
  56. /* Protocol Stuff */
  57. #define HCI_HEADER_LENGTH 0x4
  58. #define TX_HEADER_0 0x20
  59. #define TX_HEADER_1 0x29
  60. #define RX_HEADER_0 0x00
  61. #define RX_HEADER_1 0x20
  62. #define MAX_HCI_FRAMESIZE 60
  63. #define HCI_COMPLETE_FRAME 64
  64. /* rx_flags */
  65. #define THROTTLED 0x01
  66. #define ACTUALLY_THROTTLED 0x02
  67. /*
  68. * Version Information
  69. */
  70. #define DRIVER_VERSION "v1.0b2"
  71. #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>"
  72. #define DRIVER_DESC "AIRcable USB Driver"
  73. /* ID table that will be registered with USB core */
  74. static const struct usb_device_id id_table[] = {
  75. { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
  76. { },
  77. };
  78. MODULE_DEVICE_TABLE(usb, id_table);
  79. /* Internal Structure */
  80. struct aircable_private {
  81. spinlock_t rx_lock; /* spinlock for the receive lines */
  82. struct circ_buf *tx_buf; /* write buffer */
  83. struct circ_buf *rx_buf; /* read buffer */
  84. int rx_flags; /* for throttilng */
  85. struct work_struct rx_work; /* work cue for the receiving line */
  86. struct usb_serial_port *port; /* USB port with which associated */
  87. };
  88. /* Private methods */
  89. /* Circular Buffer Methods, code from ti_usb_3410_5052 used */
  90. /*
  91. * serial_buf_clear
  92. *
  93. * Clear out all data in the circular buffer.
  94. */
  95. static void serial_buf_clear(struct circ_buf *cb)
  96. {
  97. cb->head = cb->tail = 0;
  98. }
  99. /*
  100. * serial_buf_alloc
  101. *
  102. * Allocate a circular buffer and all associated memory.
  103. */
  104. static struct circ_buf *serial_buf_alloc(void)
  105. {
  106. struct circ_buf *cb;
  107. cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
  108. if (cb == NULL)
  109. return NULL;
  110. cb->buf = kmalloc(AIRCABLE_BUF_SIZE, GFP_KERNEL);
  111. if (cb->buf == NULL) {
  112. kfree(cb);
  113. return NULL;
  114. }
  115. serial_buf_clear(cb);
  116. return cb;
  117. }
  118. /*
  119. * serial_buf_free
  120. *
  121. * Free the buffer and all associated memory.
  122. */
  123. static void serial_buf_free(struct circ_buf *cb)
  124. {
  125. kfree(cb->buf);
  126. kfree(cb);
  127. }
  128. /*
  129. * serial_buf_data_avail
  130. *
  131. * Return the number of bytes of data available in the circular
  132. * buffer.
  133. */
  134. static int serial_buf_data_avail(struct circ_buf *cb)
  135. {
  136. return CIRC_CNT(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
  137. }
  138. /*
  139. * serial_buf_put
  140. *
  141. * Copy data data from a user buffer and put it into the circular buffer.
  142. * Restrict to the amount of space available.
  143. *
  144. * Return the number of bytes copied.
  145. */
  146. static int serial_buf_put(struct circ_buf *cb, const char *buf, int count)
  147. {
  148. int c, ret = 0;
  149. while (1) {
  150. c = CIRC_SPACE_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
  151. if (count < c)
  152. c = count;
  153. if (c <= 0)
  154. break;
  155. memcpy(cb->buf + cb->head, buf, c);
  156. cb->head = (cb->head + c) & (AIRCABLE_BUF_SIZE-1);
  157. buf += c;
  158. count -= c;
  159. ret = c;
  160. }
  161. return ret;
  162. }
  163. /*
  164. * serial_buf_get
  165. *
  166. * Get data from the circular buffer and copy to the given buffer.
  167. * Restrict to the amount of data available.
  168. *
  169. * Return the number of bytes copied.
  170. */
  171. static int serial_buf_get(struct circ_buf *cb, char *buf, int count)
  172. {
  173. int c, ret = 0;
  174. while (1) {
  175. c = CIRC_CNT_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
  176. if (count < c)
  177. c = count;
  178. if (c <= 0)
  179. break;
  180. memcpy(buf, cb->buf + cb->tail, c);
  181. cb->tail = (cb->tail + c) & (AIRCABLE_BUF_SIZE-1);
  182. buf += c;
  183. count -= c;
  184. ret = c;
  185. }
  186. return ret;
  187. }
  188. /* End of circula buffer methods */
  189. static void aircable_send(struct usb_serial_port *port)
  190. {
  191. int count, result;
  192. struct aircable_private *priv = usb_get_serial_port_data(port);
  193. unsigned char *buf;
  194. __le16 *dbuf;
  195. dbg("%s - port %d", __func__, port->number);
  196. if (port->write_urb_busy)
  197. return;
  198. count = min(serial_buf_data_avail(priv->tx_buf), MAX_HCI_FRAMESIZE);
  199. if (count == 0)
  200. return;
  201. buf = kzalloc(count + HCI_HEADER_LENGTH, GFP_ATOMIC);
  202. if (!buf) {
  203. dev_err(&port->dev, "%s- kzalloc(%d) failed.\n",
  204. __func__, count + HCI_HEADER_LENGTH);
  205. return;
  206. }
  207. buf[0] = TX_HEADER_0;
  208. buf[1] = TX_HEADER_1;
  209. dbuf = (__le16 *)&buf[2];
  210. *dbuf = cpu_to_le16((u16)count);
  211. serial_buf_get(priv->tx_buf, buf + HCI_HEADER_LENGTH,
  212. MAX_HCI_FRAMESIZE);
  213. memcpy(port->write_urb->transfer_buffer, buf,
  214. count + HCI_HEADER_LENGTH);
  215. kfree(buf);
  216. port->write_urb_busy = 1;
  217. usb_serial_debug_data(debug, &port->dev, __func__,
  218. count + HCI_HEADER_LENGTH,
  219. port->write_urb->transfer_buffer);
  220. port->write_urb->transfer_buffer_length = count + HCI_HEADER_LENGTH;
  221. port->write_urb->dev = port->serial->dev;
  222. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  223. if (result) {
  224. dev_err(&port->dev,
  225. "%s - failed submitting write urb, error %d\n",
  226. __func__, result);
  227. port->write_urb_busy = 0;
  228. }
  229. schedule_work(&port->work);
  230. }
  231. static void aircable_read(struct work_struct *work)
  232. {
  233. struct aircable_private *priv =
  234. container_of(work, struct aircable_private, rx_work);
  235. struct usb_serial_port *port = priv->port;
  236. struct tty_struct *tty;
  237. unsigned char *data;
  238. int count;
  239. if (priv->rx_flags & THROTTLED) {
  240. if (priv->rx_flags & ACTUALLY_THROTTLED)
  241. schedule_work(&priv->rx_work);
  242. return;
  243. }
  244. /* By now I will flush data to the tty in packages of no more than
  245. * 64 bytes, to ensure I do not get throttled.
  246. * Ask USB mailing list for better aproach.
  247. */
  248. tty = tty_port_tty_get(&port->port);
  249. if (!tty) {
  250. schedule_work(&priv->rx_work);
  251. dev_err(&port->dev, "%s - No tty available\n", __func__);
  252. return ;
  253. }
  254. count = min(64, serial_buf_data_avail(priv->rx_buf));
  255. if (count <= 0)
  256. goto out; /* We have finished sending everything. */
  257. tty_prepare_flip_string(tty, &data, count);
  258. if (!data) {
  259. dev_err(&port->dev, "%s- kzalloc(%d) failed.",
  260. __func__, count);
  261. goto out;
  262. }
  263. serial_buf_get(priv->rx_buf, data, count);
  264. tty_flip_buffer_push(tty);
  265. if (serial_buf_data_avail(priv->rx_buf))
  266. schedule_work(&priv->rx_work);
  267. out:
  268. tty_kref_put(tty);
  269. return;
  270. }
  271. /* End of private methods */
  272. static int aircable_probe(struct usb_serial *serial,
  273. const struct usb_device_id *id)
  274. {
  275. struct usb_host_interface *iface_desc = serial->interface->
  276. cur_altsetting;
  277. struct usb_endpoint_descriptor *endpoint;
  278. int num_bulk_out = 0;
  279. int i;
  280. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  281. endpoint = &iface_desc->endpoint[i].desc;
  282. if (usb_endpoint_is_bulk_out(endpoint)) {
  283. dbg("found bulk out on endpoint %d", i);
  284. ++num_bulk_out;
  285. }
  286. }
  287. if (num_bulk_out == 0) {
  288. dbg("Invalid interface, discarding");
  289. return -ENODEV;
  290. }
  291. return 0;
  292. }
  293. static int aircable_attach(struct usb_serial *serial)
  294. {
  295. struct usb_serial_port *port = serial->port[0];
  296. struct aircable_private *priv;
  297. priv = kzalloc(sizeof(struct aircable_private), GFP_KERNEL);
  298. if (!priv) {
  299. dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
  300. sizeof(struct aircable_private));
  301. return -ENOMEM;
  302. }
  303. /* Allocation of Circular Buffers */
  304. priv->tx_buf = serial_buf_alloc();
  305. if (priv->tx_buf == NULL) {
  306. kfree(priv);
  307. return -ENOMEM;
  308. }
  309. priv->rx_buf = serial_buf_alloc();
  310. if (priv->rx_buf == NULL) {
  311. kfree(priv->tx_buf);
  312. kfree(priv);
  313. return -ENOMEM;
  314. }
  315. priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  316. priv->port = port;
  317. INIT_WORK(&priv->rx_work, aircable_read);
  318. usb_set_serial_port_data(serial->port[0], priv);
  319. return 0;
  320. }
  321. static void aircable_release(struct usb_serial *serial)
  322. {
  323. struct usb_serial_port *port = serial->port[0];
  324. struct aircable_private *priv = usb_get_serial_port_data(port);
  325. dbg("%s", __func__);
  326. if (priv) {
  327. serial_buf_free(priv->tx_buf);
  328. serial_buf_free(priv->rx_buf);
  329. kfree(priv);
  330. }
  331. }
  332. static int aircable_write_room(struct tty_struct *tty)
  333. {
  334. struct usb_serial_port *port = tty->driver_data;
  335. struct aircable_private *priv = usb_get_serial_port_data(port);
  336. return serial_buf_data_avail(priv->tx_buf);
  337. }
  338. static int aircable_write(struct tty_struct *tty, struct usb_serial_port *port,
  339. const unsigned char *source, int count)
  340. {
  341. struct aircable_private *priv = usb_get_serial_port_data(port);
  342. int temp;
  343. dbg("%s - port %d, %d bytes", __func__, port->number, count);
  344. usb_serial_debug_data(debug, &port->dev, __func__, count, source);
  345. if (!count) {
  346. dbg("%s - write request of 0 bytes", __func__);
  347. return count;
  348. }
  349. temp = serial_buf_put(priv->tx_buf, source, count);
  350. aircable_send(port);
  351. if (count > AIRCABLE_BUF_SIZE)
  352. count = AIRCABLE_BUF_SIZE;
  353. return count;
  354. }
  355. static void aircable_write_bulk_callback(struct urb *urb)
  356. {
  357. struct usb_serial_port *port = urb->context;
  358. int status = urb->status;
  359. int result;
  360. dbg("%s - urb status: %d", __func__ , status);
  361. /* This has been taken from cypress_m8.c cypress_write_int_callback */
  362. switch (status) {
  363. case 0:
  364. /* success */
  365. break;
  366. case -ECONNRESET:
  367. case -ENOENT:
  368. case -ESHUTDOWN:
  369. /* this urb is terminated, clean up */
  370. dbg("%s - urb shutting down with status: %d",
  371. __func__, status);
  372. port->write_urb_busy = 0;
  373. return;
  374. default:
  375. /* error in the urb, so we have to resubmit it */
  376. dbg("%s - Overflow in write", __func__);
  377. dbg("%s - nonzero write bulk status received: %d",
  378. __func__, status);
  379. port->write_urb->transfer_buffer_length = 1;
  380. port->write_urb->dev = port->serial->dev;
  381. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  382. if (result)
  383. dev_err(&urb->dev->dev,
  384. "%s - failed resubmitting write urb, error %d\n",
  385. __func__, result);
  386. else
  387. return;
  388. }
  389. port->write_urb_busy = 0;
  390. aircable_send(port);
  391. }
  392. static void aircable_read_bulk_callback(struct urb *urb)
  393. {
  394. struct usb_serial_port *port = urb->context;
  395. struct aircable_private *priv = usb_get_serial_port_data(port);
  396. struct tty_struct *tty;
  397. unsigned long no_packages, remaining, package_length, i;
  398. int result, shift = 0;
  399. unsigned char *temp;
  400. int status = urb->status;
  401. dbg("%s - port %d", __func__, port->number);
  402. if (status) {
  403. dbg("%s - urb status = %d", __func__, status);
  404. if (status == -EPROTO) {
  405. dbg("%s - caught -EPROTO, resubmitting the urb",
  406. __func__);
  407. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  408. usb_rcvbulkpipe(port->serial->dev,
  409. port->bulk_in_endpointAddress),
  410. port->read_urb->transfer_buffer,
  411. port->read_urb->transfer_buffer_length,
  412. aircable_read_bulk_callback, port);
  413. result = usb_submit_urb(urb, GFP_ATOMIC);
  414. if (result)
  415. dev_err(&urb->dev->dev,
  416. "%s - failed resubmitting read urb, error %d\n",
  417. __func__, result);
  418. return;
  419. }
  420. dbg("%s - unable to handle the error, exiting.", __func__);
  421. return;
  422. }
  423. usb_serial_debug_data(debug, &port->dev, __func__,
  424. urb->actual_length, urb->transfer_buffer);
  425. tty = tty_port_tty_get(&port->port);
  426. if (tty && urb->actual_length) {
  427. if (urb->actual_length <= 2) {
  428. /* This is an incomplete package */
  429. serial_buf_put(priv->rx_buf, urb->transfer_buffer,
  430. urb->actual_length);
  431. } else {
  432. temp = urb->transfer_buffer;
  433. if (temp[0] == RX_HEADER_0)
  434. shift = HCI_HEADER_LENGTH;
  435. remaining = urb->actual_length;
  436. no_packages = urb->actual_length / (HCI_COMPLETE_FRAME);
  437. if (urb->actual_length % HCI_COMPLETE_FRAME != 0)
  438. no_packages++;
  439. for (i = 0; i < no_packages; i++) {
  440. if (remaining > (HCI_COMPLETE_FRAME))
  441. package_length = HCI_COMPLETE_FRAME;
  442. else
  443. package_length = remaining;
  444. remaining -= package_length;
  445. serial_buf_put(priv->rx_buf,
  446. urb->transfer_buffer + shift +
  447. (HCI_COMPLETE_FRAME) * (i),
  448. package_length - shift);
  449. }
  450. }
  451. aircable_read(&priv->rx_work);
  452. }
  453. tty_kref_put(tty);
  454. /* Schedule the next read */
  455. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  456. usb_rcvbulkpipe(port->serial->dev,
  457. port->bulk_in_endpointAddress),
  458. port->read_urb->transfer_buffer,
  459. port->read_urb->transfer_buffer_length,
  460. aircable_read_bulk_callback, port);
  461. result = usb_submit_urb(urb, GFP_ATOMIC);
  462. if (result && result != -EPERM)
  463. dev_err(&urb->dev->dev,
  464. "%s - failed resubmitting read urb, error %d\n",
  465. __func__, result);
  466. }
  467. /* Based on ftdi_sio.c throttle */
  468. static void aircable_throttle(struct tty_struct *tty)
  469. {
  470. struct usb_serial_port *port = tty->driver_data;
  471. struct aircable_private *priv = usb_get_serial_port_data(port);
  472. dbg("%s - port %d", __func__, port->number);
  473. spin_lock_irq(&priv->rx_lock);
  474. priv->rx_flags |= THROTTLED;
  475. spin_unlock_irq(&priv->rx_lock);
  476. }
  477. /* Based on ftdi_sio.c unthrottle */
  478. static void aircable_unthrottle(struct tty_struct *tty)
  479. {
  480. struct usb_serial_port *port = tty->driver_data;
  481. struct aircable_private *priv = usb_get_serial_port_data(port);
  482. int actually_throttled;
  483. dbg("%s - port %d", __func__, port->number);
  484. spin_lock_irq(&priv->rx_lock);
  485. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  486. priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  487. spin_unlock_irq(&priv->rx_lock);
  488. if (actually_throttled)
  489. schedule_work(&priv->rx_work);
  490. }
  491. static struct usb_driver aircable_driver = {
  492. .name = "aircable",
  493. .probe = usb_serial_probe,
  494. .disconnect = usb_serial_disconnect,
  495. .id_table = id_table,
  496. .no_dynamic_id = 1,
  497. };
  498. static struct usb_serial_driver aircable_device = {
  499. .driver = {
  500. .owner = THIS_MODULE,
  501. .name = "aircable",
  502. },
  503. .usb_driver = &aircable_driver,
  504. .id_table = id_table,
  505. .num_ports = 1,
  506. .attach = aircable_attach,
  507. .probe = aircable_probe,
  508. .release = aircable_release,
  509. .write = aircable_write,
  510. .write_room = aircable_write_room,
  511. .write_bulk_callback = aircable_write_bulk_callback,
  512. .read_bulk_callback = aircable_read_bulk_callback,
  513. .throttle = aircable_throttle,
  514. .unthrottle = aircable_unthrottle,
  515. };
  516. static int __init aircable_init(void)
  517. {
  518. int retval;
  519. retval = usb_serial_register(&aircable_device);
  520. if (retval)
  521. goto failed_serial_register;
  522. retval = usb_register(&aircable_driver);
  523. if (retval)
  524. goto failed_usb_register;
  525. return 0;
  526. failed_usb_register:
  527. usb_serial_deregister(&aircable_device);
  528. failed_serial_register:
  529. return retval;
  530. }
  531. static void __exit aircable_exit(void)
  532. {
  533. usb_deregister(&aircable_driver);
  534. usb_serial_deregister(&aircable_device);
  535. }
  536. MODULE_AUTHOR(DRIVER_AUTHOR);
  537. MODULE_DESCRIPTION(DRIVER_DESC);
  538. MODULE_VERSION(DRIVER_VERSION);
  539. MODULE_LICENSE("GPL");
  540. module_init(aircable_init);
  541. module_exit(aircable_exit);
  542. module_param(debug, bool, S_IRUGO | S_IWUSR);
  543. MODULE_PARM_DESC(debug, "Debug enabled or not");