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