aircable.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. count = min(64, serial_buf_data_avail(priv->rx_buf));
  247. if (count <= 0)
  248. return; //We have finished sending everything.
  249. tty_prepare_flip_string(tty, &data, count);
  250. if (!data){
  251. err("%s- kzalloc(%d) failed.", __FUNCTION__, count);
  252. return;
  253. }
  254. serial_buf_get(priv->rx_buf, data, count);
  255. tty_flip_buffer_push(tty);
  256. if (serial_buf_data_avail(priv->rx_buf))
  257. schedule_work(&priv->rx_work);
  258. return;
  259. }
  260. /* End of private methods */
  261. static int aircable_probe(struct usb_serial *serial,
  262. const struct usb_device_id *id)
  263. {
  264. struct usb_host_interface *iface_desc = serial->interface->cur_altsetting;
  265. struct usb_endpoint_descriptor *endpoint;
  266. int num_bulk_out=0;
  267. int i;
  268. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  269. endpoint = &iface_desc->endpoint[i].desc;
  270. if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
  271. ((endpoint->bmAttributes & 3) == 0x02)) {
  272. /* we found our bulk out endpoint */
  273. dbg("found bulk out on endpoint %d", i);
  274. ++num_bulk_out;
  275. }
  276. }
  277. if (num_bulk_out == 0) {
  278. dbg("Invalid interface, discarding");
  279. return -ENODEV;
  280. }
  281. return 0;
  282. }
  283. static int aircable_attach (struct usb_serial *serial)
  284. {
  285. struct usb_serial_port *port = serial->port[0];
  286. struct aircable_private *priv;
  287. priv = kzalloc(sizeof(struct aircable_private), GFP_KERNEL);
  288. if (!priv){
  289. err("%s- kmalloc(%Zd) failed.", __FUNCTION__,
  290. sizeof(struct aircable_private));
  291. return -ENOMEM;
  292. }
  293. /* Allocation of Circular Buffers */
  294. priv->tx_buf = serial_buf_alloc();
  295. if (priv->tx_buf == NULL) {
  296. kfree(priv);
  297. return -ENOMEM;
  298. }
  299. priv->rx_buf = serial_buf_alloc();
  300. if (priv->rx_buf == NULL) {
  301. kfree(priv->tx_buf);
  302. kfree(priv);
  303. return -ENOMEM;
  304. }
  305. priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  306. INIT_WORK(&priv->rx_work, aircable_read, port);
  307. usb_set_serial_port_data(serial->port[0], priv);
  308. return 0;
  309. }
  310. static void aircable_shutdown(struct usb_serial *serial)
  311. {
  312. struct usb_serial_port *port = serial->port[0];
  313. struct aircable_private *priv = usb_get_serial_port_data(port);
  314. dbg("%s", __FUNCTION__);
  315. if (priv) {
  316. serial_buf_free(priv->tx_buf);
  317. serial_buf_free(priv->rx_buf);
  318. usb_set_serial_port_data(port, NULL);
  319. kfree(priv);
  320. }
  321. }
  322. static int aircable_write_room(struct usb_serial_port *port)
  323. {
  324. struct aircable_private *priv = usb_get_serial_port_data(port);
  325. return serial_buf_data_avail(priv->tx_buf);
  326. }
  327. static int aircable_write(struct usb_serial_port *port,
  328. const unsigned char *source, int count)
  329. {
  330. struct aircable_private *priv = usb_get_serial_port_data(port);
  331. int temp;
  332. dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
  333. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, source);
  334. if (!count){
  335. dbg("%s - write request of 0 bytes", __FUNCTION__);
  336. return count;
  337. }
  338. temp = serial_buf_put(priv->tx_buf, source, count);
  339. aircable_send(port);
  340. if (count > AIRCABLE_BUF_SIZE)
  341. count = AIRCABLE_BUF_SIZE;
  342. return count;
  343. }
  344. static void aircable_write_bulk_callback(struct urb *urb)
  345. {
  346. struct usb_serial_port *port = urb->context;
  347. int result;
  348. dbg("%s - urb->status: %d", __FUNCTION__ , urb->status);
  349. /* This has been taken from cypress_m8.c cypress_write_int_callback */
  350. switch (urb->status) {
  351. case 0:
  352. /* success */
  353. break;
  354. case -ECONNRESET:
  355. case -ENOENT:
  356. case -ESHUTDOWN:
  357. /* this urb is terminated, clean up */
  358. dbg("%s - urb shutting down with status: %d",
  359. __FUNCTION__, urb->status);
  360. port->write_urb_busy = 0;
  361. return;
  362. default:
  363. /* error in the urb, so we have to resubmit it */
  364. dbg("%s - Overflow in write", __FUNCTION__);
  365. dbg("%s - nonzero write bulk status received: %d",
  366. __FUNCTION__, urb->status);
  367. port->write_urb->transfer_buffer_length = 1;
  368. port->write_urb->dev = port->serial->dev;
  369. result = usb_submit_urb(port->write_urb, GFP_KERNEL);
  370. if (result)
  371. dev_err(&urb->dev->dev,
  372. "%s - failed resubmitting write urb, error %d\n",
  373. __FUNCTION__, result);
  374. else
  375. return;
  376. }
  377. port->write_urb_busy = 0;
  378. aircable_send(port);
  379. }
  380. static void aircable_read_bulk_callback(struct urb *urb)
  381. {
  382. struct usb_serial_port *port = urb->context;
  383. struct aircable_private *priv = usb_get_serial_port_data(port);
  384. struct tty_struct *tty;
  385. unsigned long no_packages, remaining, package_length, i;
  386. int result, shift = 0;
  387. unsigned char *temp;
  388. dbg("%s - port %d", __FUNCTION__, port->number);
  389. if (urb->status) {
  390. dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
  391. if (!port->open_count) {
  392. dbg("%s - port is closed, exiting.", __FUNCTION__);
  393. return;
  394. }
  395. if (urb->status == -EPROTO) {
  396. dbg("%s - caught -EPROTO, resubmitting the urb",
  397. __FUNCTION__);
  398. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  399. usb_rcvbulkpipe(port->serial->dev,
  400. port->bulk_in_endpointAddress),
  401. port->read_urb->transfer_buffer,
  402. port->read_urb->transfer_buffer_length,
  403. aircable_read_bulk_callback, port);
  404. result = usb_submit_urb(urb, GFP_ATOMIC);
  405. if (result)
  406. dev_err(&urb->dev->dev,
  407. "%s - failed resubmitting read urb, error %d\n",
  408. __FUNCTION__, result);
  409. return;
  410. }
  411. dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
  412. return;
  413. }
  414. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  415. urb->actual_length,urb->transfer_buffer);
  416. tty = port->tty;
  417. if (tty && urb->actual_length) {
  418. if (urb->actual_length <= 2) {
  419. /* This is an incomplete package */
  420. serial_buf_put(priv->rx_buf, urb->transfer_buffer,
  421. urb->actual_length);
  422. } else {
  423. temp = urb->transfer_buffer;
  424. if (temp[0] == RX_HEADER_0)
  425. shift = HCI_HEADER_LENGTH;
  426. remaining = urb->actual_length;
  427. no_packages = urb->actual_length / (HCI_COMPLETE_FRAME);
  428. if (urb->actual_length % HCI_COMPLETE_FRAME != 0)
  429. no_packages+=1;
  430. for (i = 0; i < no_packages ;i++) {
  431. if (remaining > (HCI_COMPLETE_FRAME))
  432. package_length = HCI_COMPLETE_FRAME;
  433. else
  434. package_length = remaining;
  435. remaining -= package_length;
  436. serial_buf_put(priv->rx_buf,
  437. urb->transfer_buffer + shift +
  438. (HCI_COMPLETE_FRAME) * (i),
  439. package_length - shift);
  440. }
  441. }
  442. aircable_read(port);
  443. }
  444. /* Schedule the next read _if_ we are still open */
  445. if (port->open_count) {
  446. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  447. usb_rcvbulkpipe(port->serial->dev,
  448. port->bulk_in_endpointAddress),
  449. port->read_urb->transfer_buffer,
  450. port->read_urb->transfer_buffer_length,
  451. aircable_read_bulk_callback, port);
  452. result = usb_submit_urb(urb, GFP_ATOMIC);
  453. if (result)
  454. dev_err(&urb->dev->dev,
  455. "%s - failed resubmitting read urb, error %d\n",
  456. __FUNCTION__, result);
  457. }
  458. return;
  459. }
  460. /* Based on ftdi_sio.c throttle */
  461. static void aircable_throttle(struct usb_serial_port *port)
  462. {
  463. struct aircable_private *priv = usb_get_serial_port_data(port);
  464. unsigned long flags;
  465. dbg("%s - port %d", __FUNCTION__, port->number);
  466. spin_lock_irqsave(&priv->rx_lock, flags);
  467. priv->rx_flags |= THROTTLED;
  468. spin_unlock_irqrestore(&priv->rx_lock, flags);
  469. }
  470. /* Based on ftdi_sio.c unthrottle */
  471. static void aircable_unthrottle(struct usb_serial_port *port)
  472. {
  473. struct aircable_private *priv = usb_get_serial_port_data(port);
  474. int actually_throttled;
  475. unsigned long flags;
  476. dbg("%s - port %d", __FUNCTION__, port->number);
  477. spin_lock_irqsave(&priv->rx_lock, flags);
  478. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  479. priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  480. spin_unlock_irqrestore(&priv->rx_lock, flags);
  481. if (actually_throttled)
  482. schedule_work(&priv->rx_work);
  483. }
  484. static struct usb_serial_driver aircable_device = {
  485. .description = "aircable",
  486. .id_table = id_table,
  487. .num_ports = 1,
  488. .attach = aircable_attach,
  489. .probe = aircable_probe,
  490. .shutdown = aircable_shutdown,
  491. .write = aircable_write,
  492. .write_room = aircable_write_room,
  493. .write_bulk_callback = aircable_write_bulk_callback,
  494. .read_bulk_callback = aircable_read_bulk_callback,
  495. .throttle = aircable_throttle,
  496. .unthrottle = aircable_unthrottle,
  497. };
  498. static struct usb_driver aircable_driver = {
  499. .name = "aircable",
  500. .probe = usb_serial_probe,
  501. .disconnect = usb_serial_disconnect,
  502. .id_table = id_table,
  503. };
  504. static int __init aircable_init (void)
  505. {
  506. int retval;
  507. retval = usb_serial_register(&aircable_device);
  508. if (retval)
  509. goto failed_serial_register;
  510. retval = usb_register(&aircable_driver);
  511. if (retval)
  512. goto failed_usb_register;
  513. return 0;
  514. failed_serial_register:
  515. usb_serial_deregister(&aircable_device);
  516. failed_usb_register:
  517. return retval;
  518. }
  519. static void __exit aircable_exit (void)
  520. {
  521. usb_deregister(&aircable_driver);
  522. usb_serial_deregister(&aircable_device);
  523. }
  524. MODULE_AUTHOR(DRIVER_AUTHOR);
  525. MODULE_DESCRIPTION(DRIVER_DESC);
  526. MODULE_VERSION(DRIVER_VERSION);
  527. MODULE_LICENSE("GPL");
  528. module_init(aircable_init);
  529. module_exit(aircable_exit);
  530. module_param(debug, bool, S_IRUGO | S_IWUSR);
  531. MODULE_PARM_DESC(debug, "Debug enabled or not");