Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 1 | /* |
| 2 | * AIRcable USB Bluetooth Dongle Driver. |
| 3 | * |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 4 | * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com> |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 5 | * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com) |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 6 | * |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify it under |
| 8 | * the terms of the GNU General Public License version 2 as published by the |
| 9 | * Free Software Foundation. |
| 10 | * |
| 11 | * The device works as an standard CDC device, it has 2 interfaces, the first |
| 12 | * one is for firmware access and the second is the serial one. |
Rahul Bedarkar | cd8c505 | 2014-01-02 19:29:24 +0530 | [diff] [blame] | 13 | * The protocol is very simply, there are two possibilities reading or writing. |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 14 | * When writing the first urb must have a Header that starts with 0x20 0x29 the |
Rahul Bedarkar | cd8c505 | 2014-01-02 19:29:24 +0530 | [diff] [blame] | 15 | * next two bytes must say how much data will be sent. |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 16 | * When reading the process is almost equal except that the header starts with |
| 17 | * 0x00 0x20. |
| 18 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 19 | * The device simply need some stuff to understand data coming from the usb |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 20 | * buffer: The First and Second byte is used for a Header, the Third and Fourth |
| 21 | * tells the device the amount of information the package holds. |
| 22 | * Packages are 60 bytes long Header Stuff. |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 23 | * When writing to the device the first two bytes of the header are 0x20 0x29 |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 24 | * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange |
| 25 | * situation, when too much data arrives to the device because it sends the data |
| 26 | * but with out the header. I will use a simply hack to override this situation, |
| 27 | * if there is data coming that does not contain any header, then that is data |
| 28 | * that must go directly to the tty, as there is no documentation about if there |
| 29 | * is any other control code, I will simply check for the first |
| 30 | * one. |
| 31 | * |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 32 | * I have taken some info from a Greg Kroah-Hartman article: |
| 33 | * http://www.linuxjournal.com/article/6573 |
| 34 | * And from Linux Device Driver Kit CD, which is a great work, the authors taken |
Rahul Bedarkar | cd8c505 | 2014-01-02 19:29:24 +0530 | [diff] [blame] | 35 | * the work to recompile lots of information an knowledge in drivers development |
| 36 | * and made it all available inside a cd. |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 37 | * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/ |
| 38 | * |
| 39 | */ |
| 40 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 41 | #include <asm/unaligned.h> |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 42 | #include <linux/tty.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Paul Gortmaker | 6eb0de8 | 2011-07-03 16:09:31 -0400 | [diff] [blame] | 44 | #include <linux/module.h> |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 45 | #include <linux/tty_flip.h> |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 46 | #include <linux/usb.h> |
| 47 | #include <linux/usb/serial.h> |
| 48 | |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 49 | /* Vendor and Product ID */ |
| 50 | #define AIRCABLE_VID 0x16CA |
| 51 | #define AIRCABLE_USB_PID 0x1502 |
| 52 | |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 53 | /* Protocol Stuff */ |
| 54 | #define HCI_HEADER_LENGTH 0x4 |
| 55 | #define TX_HEADER_0 0x20 |
| 56 | #define TX_HEADER_1 0x29 |
| 57 | #define RX_HEADER_0 0x00 |
| 58 | #define RX_HEADER_1 0x20 |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 59 | #define HCI_COMPLETE_FRAME 64 |
| 60 | |
| 61 | /* rx_flags */ |
| 62 | #define THROTTLED 0x01 |
| 63 | #define ACTUALLY_THROTTLED 0x02 |
| 64 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 65 | #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>" |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 66 | #define DRIVER_DESC "AIRcable USB Driver" |
| 67 | |
| 68 | /* ID table that will be registered with USB core */ |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame] | 69 | static const struct usb_device_id id_table[] = { |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 70 | { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) }, |
| 71 | { }, |
| 72 | }; |
| 73 | MODULE_DEVICE_TABLE(usb, id_table); |
| 74 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 75 | static int aircable_prepare_write_buffer(struct usb_serial_port *port, |
Johan Hovold | c23e5fc | 2010-05-05 23:58:13 +0200 | [diff] [blame] | 76 | void *dest, size_t size) |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 77 | { |
Johan Hovold | c23e5fc | 2010-05-05 23:58:13 +0200 | [diff] [blame] | 78 | int count; |
| 79 | unsigned char *buf = dest; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 80 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 81 | count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH, |
| 82 | size - HCI_HEADER_LENGTH, &port->lock); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 83 | buf[0] = TX_HEADER_0; |
| 84 | buf[1] = TX_HEADER_1; |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 85 | put_unaligned_le16(count, &buf[2]); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 86 | |
Johan Hovold | f26c288 | 2010-05-19 00:01:33 +0200 | [diff] [blame] | 87 | return count + HCI_HEADER_LENGTH; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 88 | } |
| 89 | |
Johan Hovold | 5f39197 | 2017-03-16 17:13:36 +0100 | [diff] [blame^] | 90 | static int aircable_calc_num_ports(struct usb_serial *serial, |
| 91 | struct usb_serial_endpoints *epds) |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 92 | { |
Johan Hovold | 5f39197 | 2017-03-16 17:13:36 +0100 | [diff] [blame^] | 93 | /* Ignore the first interface, which has no bulk endpoints. */ |
| 94 | if (epds->num_bulk_out == 0) { |
| 95 | dev_dbg(&serial->interface->dev, |
| 96 | "ignoring interface with no bulk-out endpoints\n"); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 97 | return -ENODEV; |
| 98 | } |
| 99 | |
Johan Hovold | 5f39197 | 2017-03-16 17:13:36 +0100 | [diff] [blame^] | 100 | return 1; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 101 | } |
| 102 | |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 103 | static int aircable_process_packet(struct usb_serial_port *port, |
| 104 | int has_headers, char *packet, int len) |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 105 | { |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 106 | if (has_headers) { |
| 107 | len -= HCI_HEADER_LENGTH; |
| 108 | packet += HCI_HEADER_LENGTH; |
| 109 | } |
| 110 | if (len <= 0) { |
Greg Kroah-Hartman | 66afb5b | 2012-05-15 16:27:08 -0700 | [diff] [blame] | 111 | dev_dbg(&port->dev, "%s - malformed packet\n", __func__); |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 112 | return 0; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 113 | } |
| 114 | |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 115 | tty_insert_flip_string(&port->port, packet, len); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 116 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 117 | return len; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 118 | } |
| 119 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 120 | static void aircable_process_read_urb(struct urb *urb) |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 121 | { |
| 122 | struct usb_serial_port *port = urb->context; |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 123 | char *data = (char *)urb->transfer_buffer; |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 124 | int has_headers; |
| 125 | int count; |
| 126 | int len; |
| 127 | int i; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 128 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 129 | has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 130 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 131 | count = 0; |
| 132 | for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) { |
| 133 | len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME); |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 134 | count += aircable_process_packet(port, has_headers, |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 135 | &data[i], len); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 136 | } |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 137 | |
| 138 | if (count) |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 139 | tty_flip_buffer_push(&port->port); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static struct usb_serial_driver aircable_device = { |
Johannes Hölzl | 52d67f0 | 2006-12-17 22:05:09 +0100 | [diff] [blame] | 143 | .driver = { |
| 144 | .owner = THIS_MODULE, |
| 145 | .name = "aircable", |
| 146 | }, |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 147 | .id_table = id_table, |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 148 | .bulk_out_size = HCI_COMPLETE_FRAME, |
Johan Hovold | 5f39197 | 2017-03-16 17:13:36 +0100 | [diff] [blame^] | 149 | .calc_num_ports = aircable_calc_num_ports, |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 150 | .process_read_urb = aircable_process_read_urb, |
| 151 | .prepare_write_buffer = aircable_prepare_write_buffer, |
| 152 | .throttle = usb_serial_generic_throttle, |
| 153 | .unthrottle = usb_serial_generic_unthrottle, |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 154 | }; |
| 155 | |
Alan Stern | 08a4f6b | 2012-02-23 14:56:17 -0500 | [diff] [blame] | 156 | static struct usb_serial_driver * const serial_drivers[] = { |
| 157 | &aircable_device, NULL |
| 158 | }; |
| 159 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 160 | module_usb_serial_driver(serial_drivers, id_table); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 161 | |
| 162 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 163 | MODULE_DESCRIPTION(DRIVER_DESC); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 164 | MODULE_LICENSE("GPL"); |