18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * AIRcable USB Bluetooth Dongle Driver. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com> 68c2ecf20Sopenharmony_ci * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com) 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * The device works as an standard CDC device, it has 2 interfaces, the first 98c2ecf20Sopenharmony_ci * one is for firmware access and the second is the serial one. 108c2ecf20Sopenharmony_ci * The protocol is very simply, there are two possibilities reading or writing. 118c2ecf20Sopenharmony_ci * When writing the first urb must have a Header that starts with 0x20 0x29 the 128c2ecf20Sopenharmony_ci * next two bytes must say how much data will be sent. 138c2ecf20Sopenharmony_ci * When reading the process is almost equal except that the header starts with 148c2ecf20Sopenharmony_ci * 0x00 0x20. 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * The device simply need some stuff to understand data coming from the usb 178c2ecf20Sopenharmony_ci * buffer: The First and Second byte is used for a Header, the Third and Fourth 188c2ecf20Sopenharmony_ci * tells the device the amount of information the package holds. 198c2ecf20Sopenharmony_ci * Packages are 60 bytes long Header Stuff. 208c2ecf20Sopenharmony_ci * When writing to the device the first two bytes of the header are 0x20 0x29 218c2ecf20Sopenharmony_ci * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange 228c2ecf20Sopenharmony_ci * situation, when too much data arrives to the device because it sends the data 238c2ecf20Sopenharmony_ci * but with out the header. I will use a simply hack to override this situation, 248c2ecf20Sopenharmony_ci * if there is data coming that does not contain any header, then that is data 258c2ecf20Sopenharmony_ci * that must go directly to the tty, as there is no documentation about if there 268c2ecf20Sopenharmony_ci * is any other control code, I will simply check for the first 278c2ecf20Sopenharmony_ci * one. 288c2ecf20Sopenharmony_ci * 298c2ecf20Sopenharmony_ci * I have taken some info from a Greg Kroah-Hartman article: 308c2ecf20Sopenharmony_ci * http://www.linuxjournal.com/article/6573 318c2ecf20Sopenharmony_ci * And from Linux Device Driver Kit CD, which is a great work, the authors taken 328c2ecf20Sopenharmony_ci * the work to recompile lots of information an knowledge in drivers development 338c2ecf20Sopenharmony_ci * and made it all available inside a cd. 348c2ecf20Sopenharmony_ci * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/ 358c2ecf20Sopenharmony_ci * 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 398c2ecf20Sopenharmony_ci#include <linux/tty.h> 408c2ecf20Sopenharmony_ci#include <linux/slab.h> 418c2ecf20Sopenharmony_ci#include <linux/module.h> 428c2ecf20Sopenharmony_ci#include <linux/tty_flip.h> 438c2ecf20Sopenharmony_ci#include <linux/usb.h> 448c2ecf20Sopenharmony_ci#include <linux/usb/serial.h> 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* Vendor and Product ID */ 478c2ecf20Sopenharmony_ci#define AIRCABLE_VID 0x16CA 488c2ecf20Sopenharmony_ci#define AIRCABLE_USB_PID 0x1502 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* Protocol Stuff */ 518c2ecf20Sopenharmony_ci#define HCI_HEADER_LENGTH 0x4 528c2ecf20Sopenharmony_ci#define TX_HEADER_0 0x20 538c2ecf20Sopenharmony_ci#define TX_HEADER_1 0x29 548c2ecf20Sopenharmony_ci#define RX_HEADER_0 0x00 558c2ecf20Sopenharmony_ci#define RX_HEADER_1 0x20 568c2ecf20Sopenharmony_ci#define HCI_COMPLETE_FRAME 64 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* rx_flags */ 598c2ecf20Sopenharmony_ci#define THROTTLED 0x01 608c2ecf20Sopenharmony_ci#define ACTUALLY_THROTTLED 0x02 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>" 638c2ecf20Sopenharmony_ci#define DRIVER_DESC "AIRcable USB Driver" 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* ID table that will be registered with USB core */ 668c2ecf20Sopenharmony_cistatic const struct usb_device_id id_table[] = { 678c2ecf20Sopenharmony_ci { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) }, 688c2ecf20Sopenharmony_ci { }, 698c2ecf20Sopenharmony_ci}; 708c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic int aircable_prepare_write_buffer(struct usb_serial_port *port, 738c2ecf20Sopenharmony_ci void *dest, size_t size) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci int count; 768c2ecf20Sopenharmony_ci unsigned char *buf = dest; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH, 798c2ecf20Sopenharmony_ci size - HCI_HEADER_LENGTH, &port->lock); 808c2ecf20Sopenharmony_ci buf[0] = TX_HEADER_0; 818c2ecf20Sopenharmony_ci buf[1] = TX_HEADER_1; 828c2ecf20Sopenharmony_ci put_unaligned_le16(count, &buf[2]); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci return count + HCI_HEADER_LENGTH; 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic int aircable_calc_num_ports(struct usb_serial *serial, 888c2ecf20Sopenharmony_ci struct usb_serial_endpoints *epds) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci /* Ignore the first interface, which has no bulk endpoints. */ 918c2ecf20Sopenharmony_ci if (epds->num_bulk_out == 0) { 928c2ecf20Sopenharmony_ci dev_dbg(&serial->interface->dev, 938c2ecf20Sopenharmony_ci "ignoring interface with no bulk-out endpoints\n"); 948c2ecf20Sopenharmony_ci return -ENODEV; 958c2ecf20Sopenharmony_ci } 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci return 1; 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic int aircable_process_packet(struct usb_serial_port *port, 1018c2ecf20Sopenharmony_ci int has_headers, char *packet, int len) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci if (has_headers) { 1048c2ecf20Sopenharmony_ci len -= HCI_HEADER_LENGTH; 1058c2ecf20Sopenharmony_ci packet += HCI_HEADER_LENGTH; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci if (len <= 0) { 1088c2ecf20Sopenharmony_ci dev_dbg(&port->dev, "%s - malformed packet\n", __func__); 1098c2ecf20Sopenharmony_ci return 0; 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci tty_insert_flip_string(&port->port, packet, len); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci return len; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic void aircable_process_read_urb(struct urb *urb) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci struct usb_serial_port *port = urb->context; 1208c2ecf20Sopenharmony_ci char *data = urb->transfer_buffer; 1218c2ecf20Sopenharmony_ci int has_headers; 1228c2ecf20Sopenharmony_ci int count; 1238c2ecf20Sopenharmony_ci int len; 1248c2ecf20Sopenharmony_ci int i; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci count = 0; 1298c2ecf20Sopenharmony_ci for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) { 1308c2ecf20Sopenharmony_ci len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME); 1318c2ecf20Sopenharmony_ci count += aircable_process_packet(port, has_headers, 1328c2ecf20Sopenharmony_ci &data[i], len); 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (count) 1368c2ecf20Sopenharmony_ci tty_flip_buffer_push(&port->port); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic struct usb_serial_driver aircable_device = { 1408c2ecf20Sopenharmony_ci .driver = { 1418c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 1428c2ecf20Sopenharmony_ci .name = "aircable", 1438c2ecf20Sopenharmony_ci }, 1448c2ecf20Sopenharmony_ci .id_table = id_table, 1458c2ecf20Sopenharmony_ci .bulk_out_size = HCI_COMPLETE_FRAME, 1468c2ecf20Sopenharmony_ci .calc_num_ports = aircable_calc_num_ports, 1478c2ecf20Sopenharmony_ci .process_read_urb = aircable_process_read_urb, 1488c2ecf20Sopenharmony_ci .prepare_write_buffer = aircable_prepare_write_buffer, 1498c2ecf20Sopenharmony_ci .throttle = usb_serial_generic_throttle, 1508c2ecf20Sopenharmony_ci .unthrottle = usb_serial_generic_unthrottle, 1518c2ecf20Sopenharmony_ci}; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = { 1548c2ecf20Sopenharmony_ci &aircable_device, NULL 1558c2ecf20Sopenharmony_ci}; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ciMODULE_AUTHOR(DRIVER_AUTHOR); 1608c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC); 1618c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 162