aircable.c 16 KB

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