xref: /linux/drivers/usb/serial/aircable.c (revision cd8c50532a42065339be1fe550e66b89d7ffd14f)
13fe70ba2SManuel Francisco Naranjo /*
23fe70ba2SManuel Francisco Naranjo  * AIRcable USB Bluetooth Dongle Driver.
33fe70ba2SManuel Francisco Naranjo  *
44272568bSJohan Hovold  * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
53fe70ba2SManuel Francisco Naranjo  * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
64272568bSJohan Hovold  *
73fe70ba2SManuel Francisco Naranjo  * This program is free software; you can redistribute it and/or modify it under
83fe70ba2SManuel Francisco Naranjo  * the terms of the GNU General Public License version 2 as published by the
93fe70ba2SManuel Francisco Naranjo  * Free Software Foundation.
103fe70ba2SManuel Francisco Naranjo  *
113fe70ba2SManuel Francisco Naranjo  * The device works as an standard CDC device, it has 2 interfaces, the first
123fe70ba2SManuel Francisco Naranjo  * one is for firmware access and the second is the serial one.
13*cd8c5053SRahul Bedarkar  * The protocol is very simply, there are two possibilities reading or writing.
14beb7dd86SRobert P. J. Day  * When writing the first urb must have a Header that starts with 0x20 0x29 the
15*cd8c5053SRahul Bedarkar  * next two bytes must say how much data will be sent.
163fe70ba2SManuel Francisco Naranjo  * When reading the process is almost equal except that the header starts with
173fe70ba2SManuel Francisco Naranjo  * 0x00 0x20.
183fe70ba2SManuel Francisco Naranjo  *
1925985edcSLucas De Marchi  * The device simply need some stuff to understand data coming from the usb
203fe70ba2SManuel Francisco Naranjo  * buffer: The First and Second byte is used for a Header, the Third and Fourth
213fe70ba2SManuel Francisco Naranjo  * tells the  device the amount of information the package holds.
223fe70ba2SManuel Francisco Naranjo  * Packages are 60 bytes long Header Stuff.
23beb7dd86SRobert P. J. Day  * When writing to the device the first two bytes of the header are 0x20 0x29
243fe70ba2SManuel Francisco Naranjo  * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
253fe70ba2SManuel Francisco Naranjo  * situation, when too much data arrives to the device because it sends the data
263fe70ba2SManuel Francisco Naranjo  * but with out the header. I will use a simply hack to override this situation,
273fe70ba2SManuel Francisco Naranjo  * if there is data coming that does not contain any header, then that is data
283fe70ba2SManuel Francisco Naranjo  * that must go directly to the tty, as there is no documentation about if there
293fe70ba2SManuel Francisco Naranjo  * is any other control code, I will simply check for the first
303fe70ba2SManuel Francisco Naranjo  * one.
313fe70ba2SManuel Francisco Naranjo  *
323fe70ba2SManuel Francisco Naranjo  * The driver registers himself with the USB-serial core and the USB Core. I had
3325985edcSLucas De Marchi  * to implement a probe function against USB-serial, because other way, the
34*cd8c5053SRahul Bedarkar  * driver was attaching himself to both interfaces. I have tried with different
353fe70ba2SManuel Francisco Naranjo  * configurations of usb_serial_driver with out exit, only the probe function
363fe70ba2SManuel Francisco Naranjo  * could handle this correctly.
373fe70ba2SManuel Francisco Naranjo  *
383fe70ba2SManuel Francisco Naranjo  * I have taken some info from a Greg Kroah-Hartman article:
393fe70ba2SManuel Francisco Naranjo  * http://www.linuxjournal.com/article/6573
403fe70ba2SManuel Francisco Naranjo  * And from Linux Device Driver Kit CD, which is a great work, the authors taken
41*cd8c5053SRahul Bedarkar  * the work to recompile lots of information an knowledge in drivers development
42*cd8c5053SRahul Bedarkar  * and made it all available inside a cd.
433fe70ba2SManuel Francisco Naranjo  * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
443fe70ba2SManuel Francisco Naranjo  *
453fe70ba2SManuel Francisco Naranjo  */
463fe70ba2SManuel Francisco Naranjo 
474272568bSJohan Hovold #include <asm/unaligned.h>
483fe70ba2SManuel Francisco Naranjo #include <linux/tty.h>
495a0e3ad6STejun Heo #include <linux/slab.h>
506eb0de82SPaul Gortmaker #include <linux/module.h>
513fe70ba2SManuel Francisco Naranjo #include <linux/tty_flip.h>
523fe70ba2SManuel Francisco Naranjo #include <linux/usb.h>
533fe70ba2SManuel Francisco Naranjo #include <linux/usb/serial.h>
543fe70ba2SManuel Francisco Naranjo 
553fe70ba2SManuel Francisco Naranjo /* Vendor and Product ID */
563fe70ba2SManuel Francisco Naranjo #define AIRCABLE_VID		0x16CA
573fe70ba2SManuel Francisco Naranjo #define AIRCABLE_USB_PID	0x1502
583fe70ba2SManuel Francisco Naranjo 
593fe70ba2SManuel Francisco Naranjo /* Protocol Stuff */
603fe70ba2SManuel Francisco Naranjo #define HCI_HEADER_LENGTH	0x4
613fe70ba2SManuel Francisco Naranjo #define TX_HEADER_0		0x20
623fe70ba2SManuel Francisco Naranjo #define TX_HEADER_1		0x29
633fe70ba2SManuel Francisco Naranjo #define RX_HEADER_0		0x00
643fe70ba2SManuel Francisco Naranjo #define RX_HEADER_1		0x20
653fe70ba2SManuel Francisco Naranjo #define HCI_COMPLETE_FRAME	64
663fe70ba2SManuel Francisco Naranjo 
673fe70ba2SManuel Francisco Naranjo /* rx_flags */
683fe70ba2SManuel Francisco Naranjo #define THROTTLED		0x01
693fe70ba2SManuel Francisco Naranjo #define ACTUALLY_THROTTLED	0x02
703fe70ba2SManuel Francisco Naranjo 
714272568bSJohan Hovold #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
723fe70ba2SManuel Francisco Naranjo #define DRIVER_DESC "AIRcable USB Driver"
733fe70ba2SManuel Francisco Naranjo 
743fe70ba2SManuel Francisco Naranjo /* ID table that will be registered with USB core */
757d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
763fe70ba2SManuel Francisco Naranjo 	{ USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
773fe70ba2SManuel Francisco Naranjo 	{ },
783fe70ba2SManuel Francisco Naranjo };
793fe70ba2SManuel Francisco Naranjo MODULE_DEVICE_TABLE(usb, id_table);
803fe70ba2SManuel Francisco Naranjo 
814272568bSJohan Hovold static int aircable_prepare_write_buffer(struct usb_serial_port *port,
82c23e5fc1SJohan Hovold 						void *dest, size_t size)
833fe70ba2SManuel Francisco Naranjo {
84c23e5fc1SJohan Hovold 	int count;
85c23e5fc1SJohan Hovold 	unsigned char *buf = dest;
863fe70ba2SManuel Francisco Naranjo 
874272568bSJohan Hovold 	count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
884272568bSJohan Hovold 					size - HCI_HEADER_LENGTH, &port->lock);
893fe70ba2SManuel Francisco Naranjo 	buf[0] = TX_HEADER_0;
903fe70ba2SManuel Francisco Naranjo 	buf[1] = TX_HEADER_1;
914272568bSJohan Hovold 	put_unaligned_le16(count, &buf[2]);
923fe70ba2SManuel Francisco Naranjo 
93f26c2889SJohan Hovold 	return count + HCI_HEADER_LENGTH;
943fe70ba2SManuel Francisco Naranjo }
953fe70ba2SManuel Francisco Naranjo 
963fe70ba2SManuel Francisco Naranjo static int aircable_probe(struct usb_serial *serial,
973fe70ba2SManuel Francisco Naranjo 			  const struct usb_device_id *id)
983fe70ba2SManuel Francisco Naranjo {
99c4d0f8cbSAlan Cox 	struct usb_host_interface *iface_desc = serial->interface->
100c4d0f8cbSAlan Cox 								cur_altsetting;
1013fe70ba2SManuel Francisco Naranjo 	struct usb_endpoint_descriptor *endpoint;
1023fe70ba2SManuel Francisco Naranjo 	int num_bulk_out = 0;
1033fe70ba2SManuel Francisco Naranjo 	int i;
1043fe70ba2SManuel Francisco Naranjo 
1053fe70ba2SManuel Francisco Naranjo 	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1063fe70ba2SManuel Francisco Naranjo 		endpoint = &iface_desc->endpoint[i].desc;
107377f13bfSLuiz Fernando N. Capitulino 		if (usb_endpoint_is_bulk_out(endpoint)) {
10866afb5b5SGreg Kroah-Hartman 			dev_dbg(&serial->dev->dev,
10966afb5b5SGreg Kroah-Hartman 				"found bulk out on endpoint %d\n", i);
1103fe70ba2SManuel Francisco Naranjo 			++num_bulk_out;
1113fe70ba2SManuel Francisco Naranjo 		}
1123fe70ba2SManuel Francisco Naranjo 	}
1133fe70ba2SManuel Francisco Naranjo 
1143fe70ba2SManuel Francisco Naranjo 	if (num_bulk_out == 0) {
11566afb5b5SGreg Kroah-Hartman 		dev_dbg(&serial->dev->dev, "Invalid interface, discarding\n");
1163fe70ba2SManuel Francisco Naranjo 		return -ENODEV;
1173fe70ba2SManuel Francisco Naranjo 	}
1183fe70ba2SManuel Francisco Naranjo 
1193fe70ba2SManuel Francisco Naranjo 	return 0;
1203fe70ba2SManuel Francisco Naranjo }
1213fe70ba2SManuel Francisco Naranjo 
12205c7cd39SJiri Slaby static int aircable_process_packet(struct usb_serial_port *port,
12305c7cd39SJiri Slaby 		int has_headers, char *packet, int len)
1243fe70ba2SManuel Francisco Naranjo {
1254272568bSJohan Hovold 	if (has_headers) {
1264272568bSJohan Hovold 		len -= HCI_HEADER_LENGTH;
1274272568bSJohan Hovold 		packet += HCI_HEADER_LENGTH;
1283fe70ba2SManuel Francisco Naranjo 	}
1294272568bSJohan Hovold 	if (len <= 0) {
13066afb5b5SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
1313fe70ba2SManuel Francisco Naranjo 		return 0;
1323fe70ba2SManuel Francisco Naranjo 	}
1333fe70ba2SManuel Francisco Naranjo 
13405c7cd39SJiri Slaby 	tty_insert_flip_string(&port->port, packet, len);
1353fe70ba2SManuel Francisco Naranjo 
1364272568bSJohan Hovold 	return len;
1373fe70ba2SManuel Francisco Naranjo }
1383fe70ba2SManuel Francisco Naranjo 
1394272568bSJohan Hovold static void aircable_process_read_urb(struct urb *urb)
1403fe70ba2SManuel Francisco Naranjo {
1413fe70ba2SManuel Francisco Naranjo 	struct usb_serial_port *port = urb->context;
1424272568bSJohan Hovold 	char *data = (char *)urb->transfer_buffer;
1434272568bSJohan Hovold 	int has_headers;
1444272568bSJohan Hovold 	int count;
1454272568bSJohan Hovold 	int len;
1464272568bSJohan Hovold 	int i;
1473fe70ba2SManuel Francisco Naranjo 
1484272568bSJohan Hovold 	has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
1493fe70ba2SManuel Francisco Naranjo 
1504272568bSJohan Hovold 	count = 0;
1514272568bSJohan Hovold 	for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
1524272568bSJohan Hovold 		len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
15305c7cd39SJiri Slaby 		count += aircable_process_packet(port, has_headers,
1544272568bSJohan Hovold 								&data[i], len);
1553fe70ba2SManuel Francisco Naranjo 	}
1564272568bSJohan Hovold 
1574272568bSJohan Hovold 	if (count)
1582e124b4aSJiri Slaby 		tty_flip_buffer_push(&port->port);
1593fe70ba2SManuel Francisco Naranjo }
1603fe70ba2SManuel Francisco Naranjo 
1613fe70ba2SManuel Francisco Naranjo static struct usb_serial_driver aircable_device = {
16252d67f0bSJohannes Hölzl 	.driver = {
16352d67f0bSJohannes Hölzl 		.owner =	THIS_MODULE,
16452d67f0bSJohannes Hölzl 		.name =		"aircable",
16552d67f0bSJohannes Hölzl 	},
1663fe70ba2SManuel Francisco Naranjo 	.id_table = 		id_table,
1673fe70ba2SManuel Francisco Naranjo 	.num_ports =		1,
1684272568bSJohan Hovold 	.bulk_out_size =	HCI_COMPLETE_FRAME,
1693fe70ba2SManuel Francisco Naranjo 	.probe =		aircable_probe,
1704272568bSJohan Hovold 	.process_read_urb =	aircable_process_read_urb,
1714272568bSJohan Hovold 	.prepare_write_buffer =	aircable_prepare_write_buffer,
1724272568bSJohan Hovold 	.throttle =		usb_serial_generic_throttle,
1734272568bSJohan Hovold 	.unthrottle =		usb_serial_generic_unthrottle,
1743fe70ba2SManuel Francisco Naranjo };
1753fe70ba2SManuel Francisco Naranjo 
17608a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
17708a4f6bcSAlan Stern 	&aircable_device, NULL
17808a4f6bcSAlan Stern };
17908a4f6bcSAlan Stern 
18068e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
1813fe70ba2SManuel Francisco Naranjo 
1823fe70ba2SManuel Francisco Naranjo MODULE_AUTHOR(DRIVER_AUTHOR);
1833fe70ba2SManuel Francisco Naranjo MODULE_DESCRIPTION(DRIVER_DESC);
1843fe70ba2SManuel Francisco Naranjo MODULE_LICENSE("GPL");
185