15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23fe70ba2SManuel Francisco Naranjo /*
33fe70ba2SManuel Francisco Naranjo * AIRcable USB Bluetooth Dongle Driver.
43fe70ba2SManuel Francisco Naranjo *
54272568bSJohan Hovold * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
63fe70ba2SManuel Francisco Naranjo * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
74272568bSJohan Hovold *
83fe70ba2SManuel Francisco Naranjo * The device works as an standard CDC device, it has 2 interfaces, the first
93fe70ba2SManuel Francisco Naranjo * one is for firmware access and the second is the serial one.
10cd8c5053SRahul Bedarkar * The protocol is very simply, there are two possibilities reading or writing.
11beb7dd86SRobert P. J. Day * When writing the first urb must have a Header that starts with 0x20 0x29 the
12cd8c5053SRahul Bedarkar * next two bytes must say how much data will be sent.
133fe70ba2SManuel Francisco Naranjo * When reading the process is almost equal except that the header starts with
143fe70ba2SManuel Francisco Naranjo * 0x00 0x20.
153fe70ba2SManuel Francisco Naranjo *
1625985edcSLucas De Marchi * The device simply need some stuff to understand data coming from the usb
173fe70ba2SManuel Francisco Naranjo * buffer: The First and Second byte is used for a Header, the Third and Fourth
183fe70ba2SManuel Francisco Naranjo * tells the device the amount of information the package holds.
193fe70ba2SManuel Francisco Naranjo * Packages are 60 bytes long Header Stuff.
20beb7dd86SRobert P. J. Day * When writing to the device the first two bytes of the header are 0x20 0x29
213fe70ba2SManuel Francisco Naranjo * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
223fe70ba2SManuel Francisco Naranjo * situation, when too much data arrives to the device because it sends the data
233fe70ba2SManuel Francisco Naranjo * but with out the header. I will use a simply hack to override this situation,
243fe70ba2SManuel Francisco Naranjo * if there is data coming that does not contain any header, then that is data
253fe70ba2SManuel Francisco Naranjo * that must go directly to the tty, as there is no documentation about if there
263fe70ba2SManuel Francisco Naranjo * is any other control code, I will simply check for the first
273fe70ba2SManuel Francisco Naranjo * one.
283fe70ba2SManuel Francisco Naranjo *
293fe70ba2SManuel Francisco Naranjo * I have taken some info from a Greg Kroah-Hartman article:
303fe70ba2SManuel Francisco Naranjo * http://www.linuxjournal.com/article/6573
313fe70ba2SManuel Francisco Naranjo * And from Linux Device Driver Kit CD, which is a great work, the authors taken
32cd8c5053SRahul Bedarkar * the work to recompile lots of information an knowledge in drivers development
33cd8c5053SRahul Bedarkar * and made it all available inside a cd.
343fe70ba2SManuel Francisco Naranjo * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
353fe70ba2SManuel Francisco Naranjo *
363fe70ba2SManuel Francisco Naranjo */
373fe70ba2SManuel Francisco Naranjo
38*5f60d5f6SAl Viro #include <linux/unaligned.h>
393fe70ba2SManuel Francisco Naranjo #include <linux/tty.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
416eb0de82SPaul Gortmaker #include <linux/module.h>
423fe70ba2SManuel Francisco Naranjo #include <linux/tty_flip.h>
433fe70ba2SManuel Francisco Naranjo #include <linux/usb.h>
443fe70ba2SManuel Francisco Naranjo #include <linux/usb/serial.h>
453fe70ba2SManuel Francisco Naranjo
463fe70ba2SManuel Francisco Naranjo /* Vendor and Product ID */
473fe70ba2SManuel Francisco Naranjo #define AIRCABLE_VID 0x16CA
483fe70ba2SManuel Francisco Naranjo #define AIRCABLE_USB_PID 0x1502
493fe70ba2SManuel Francisco Naranjo
503fe70ba2SManuel Francisco Naranjo /* Protocol Stuff */
513fe70ba2SManuel Francisco Naranjo #define HCI_HEADER_LENGTH 0x4
523fe70ba2SManuel Francisco Naranjo #define TX_HEADER_0 0x20
533fe70ba2SManuel Francisco Naranjo #define TX_HEADER_1 0x29
543fe70ba2SManuel Francisco Naranjo #define RX_HEADER_0 0x00
553fe70ba2SManuel Francisco Naranjo #define RX_HEADER_1 0x20
563fe70ba2SManuel Francisco Naranjo #define HCI_COMPLETE_FRAME 64
573fe70ba2SManuel Francisco Naranjo
583fe70ba2SManuel Francisco Naranjo /* rx_flags */
593fe70ba2SManuel Francisco Naranjo #define THROTTLED 0x01
603fe70ba2SManuel Francisco Naranjo #define ACTUALLY_THROTTLED 0x02
613fe70ba2SManuel Francisco Naranjo
624272568bSJohan Hovold #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
633fe70ba2SManuel Francisco Naranjo #define DRIVER_DESC "AIRcable USB Driver"
643fe70ba2SManuel Francisco Naranjo
653fe70ba2SManuel Francisco Naranjo /* ID table that will be registered with USB core */
667d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
673fe70ba2SManuel Francisco Naranjo { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
683fe70ba2SManuel Francisco Naranjo { },
693fe70ba2SManuel Francisco Naranjo };
703fe70ba2SManuel Francisco Naranjo MODULE_DEVICE_TABLE(usb, id_table);
713fe70ba2SManuel Francisco Naranjo
aircable_prepare_write_buffer(struct usb_serial_port * port,void * dest,size_t size)724272568bSJohan Hovold static int aircable_prepare_write_buffer(struct usb_serial_port *port,
73c23e5fc1SJohan Hovold void *dest, size_t size)
743fe70ba2SManuel Francisco Naranjo {
75c23e5fc1SJohan Hovold int count;
76c23e5fc1SJohan Hovold unsigned char *buf = dest;
773fe70ba2SManuel Francisco Naranjo
784272568bSJohan Hovold count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
794272568bSJohan Hovold size - HCI_HEADER_LENGTH, &port->lock);
803fe70ba2SManuel Francisco Naranjo buf[0] = TX_HEADER_0;
813fe70ba2SManuel Francisco Naranjo buf[1] = TX_HEADER_1;
824272568bSJohan Hovold put_unaligned_le16(count, &buf[2]);
833fe70ba2SManuel Francisco Naranjo
84f26c2889SJohan Hovold return count + HCI_HEADER_LENGTH;
853fe70ba2SManuel Francisco Naranjo }
863fe70ba2SManuel Francisco Naranjo
aircable_calc_num_ports(struct usb_serial * serial,struct usb_serial_endpoints * epds)875f391979SJohan Hovold static int aircable_calc_num_ports(struct usb_serial *serial,
885f391979SJohan Hovold struct usb_serial_endpoints *epds)
893fe70ba2SManuel Francisco Naranjo {
905f391979SJohan Hovold /* Ignore the first interface, which has no bulk endpoints. */
915f391979SJohan Hovold if (epds->num_bulk_out == 0) {
925f391979SJohan Hovold dev_dbg(&serial->interface->dev,
935f391979SJohan Hovold "ignoring interface with no bulk-out endpoints\n");
943fe70ba2SManuel Francisco Naranjo return -ENODEV;
953fe70ba2SManuel Francisco Naranjo }
963fe70ba2SManuel Francisco Naranjo
975f391979SJohan Hovold return 1;
983fe70ba2SManuel Francisco Naranjo }
993fe70ba2SManuel Francisco Naranjo
aircable_process_packet(struct usb_serial_port * port,int has_headers,char * packet,int len)10005c7cd39SJiri Slaby static int aircable_process_packet(struct usb_serial_port *port,
10105c7cd39SJiri Slaby int has_headers, char *packet, int len)
1023fe70ba2SManuel Francisco Naranjo {
1034272568bSJohan Hovold if (has_headers) {
1044272568bSJohan Hovold len -= HCI_HEADER_LENGTH;
1054272568bSJohan Hovold packet += HCI_HEADER_LENGTH;
1063fe70ba2SManuel Francisco Naranjo }
1074272568bSJohan Hovold if (len <= 0) {
10866afb5b5SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
1093fe70ba2SManuel Francisco Naranjo return 0;
1103fe70ba2SManuel Francisco Naranjo }
1113fe70ba2SManuel Francisco Naranjo
11205c7cd39SJiri Slaby tty_insert_flip_string(&port->port, packet, len);
1133fe70ba2SManuel Francisco Naranjo
1144272568bSJohan Hovold return len;
1153fe70ba2SManuel Francisco Naranjo }
1163fe70ba2SManuel Francisco Naranjo
aircable_process_read_urb(struct urb * urb)1174272568bSJohan Hovold static void aircable_process_read_urb(struct urb *urb)
1183fe70ba2SManuel Francisco Naranjo {
1193fe70ba2SManuel Francisco Naranjo struct usb_serial_port *port = urb->context;
120eb0c68eaSJohan Hovold char *data = urb->transfer_buffer;
1214272568bSJohan Hovold int has_headers;
1224272568bSJohan Hovold int count;
1234272568bSJohan Hovold int len;
1244272568bSJohan Hovold int i;
1253fe70ba2SManuel Francisco Naranjo
1264272568bSJohan Hovold has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
1273fe70ba2SManuel Francisco Naranjo
1284272568bSJohan Hovold count = 0;
1294272568bSJohan Hovold for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
1304272568bSJohan Hovold len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
13105c7cd39SJiri Slaby count += aircable_process_packet(port, has_headers,
1324272568bSJohan Hovold &data[i], len);
1333fe70ba2SManuel Francisco Naranjo }
1344272568bSJohan Hovold
1354272568bSJohan Hovold if (count)
1362e124b4aSJiri Slaby tty_flip_buffer_push(&port->port);
1373fe70ba2SManuel Francisco Naranjo }
1383fe70ba2SManuel Francisco Naranjo
1393fe70ba2SManuel Francisco Naranjo static struct usb_serial_driver aircable_device = {
14052d67f0bSJohannes Hölzl .driver = {
14152d67f0bSJohannes Hölzl .name = "aircable",
14252d67f0bSJohannes Hölzl },
1433fe70ba2SManuel Francisco Naranjo .id_table = id_table,
1444272568bSJohan Hovold .bulk_out_size = HCI_COMPLETE_FRAME,
1455f391979SJohan Hovold .calc_num_ports = aircable_calc_num_ports,
1464272568bSJohan Hovold .process_read_urb = aircable_process_read_urb,
1474272568bSJohan Hovold .prepare_write_buffer = aircable_prepare_write_buffer,
1484272568bSJohan Hovold .throttle = usb_serial_generic_throttle,
1494272568bSJohan Hovold .unthrottle = usb_serial_generic_unthrottle,
1503fe70ba2SManuel Francisco Naranjo };
1513fe70ba2SManuel Francisco Naranjo
15208a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
15308a4f6bcSAlan Stern &aircable_device, NULL
15408a4f6bcSAlan Stern };
15508a4f6bcSAlan Stern
15668e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
1573fe70ba2SManuel Francisco Naranjo
1583fe70ba2SManuel Francisco Naranjo MODULE_AUTHOR(DRIVER_AUTHOR);
1593fe70ba2SManuel Francisco Naranjo MODULE_DESCRIPTION(DRIVER_DESC);
160627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
161