xref: /linux/drivers/usb/serial/aircable.c (revision 25985edcedea6396277003854657b5f3cb31a628)
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.
133fe70ba2SManuel Francisco Naranjo  * The protocol is very simply, there are two posibilities reading or writing.
14beb7dd86SRobert P. J. Day  * When writing the first urb must have a Header that starts with 0x20 0x29 the
153fe70ba2SManuel Francisco Naranjo  * next two bytes must say how much data will be sended.
163fe70ba2SManuel Francisco Naranjo  * When reading the process is almost equal except that the header starts with
173fe70ba2SManuel Francisco Naranjo  * 0x00 0x20.
183fe70ba2SManuel Francisco Naranjo  *
19*25985edcSLucas 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
33*25985edcSLucas De Marchi  * to implement a probe function against USB-serial, because other way, the
343fe70ba2SManuel Francisco Naranjo  * driver was attaching himself to both interfaces. I have tryed 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
413fe70ba2SManuel Francisco Naranjo  * the work to recompile lots of information an knowladge in drivers development
423fe70ba2SManuel Francisco Naranjo  * and made it all avaible 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>
503fe70ba2SManuel Francisco Naranjo #include <linux/tty_flip.h>
513fe70ba2SManuel Francisco Naranjo #include <linux/usb.h>
523fe70ba2SManuel Francisco Naranjo #include <linux/usb/serial.h>
533fe70ba2SManuel Francisco Naranjo 
543fe70ba2SManuel Francisco Naranjo static int debug;
553fe70ba2SManuel Francisco Naranjo 
563fe70ba2SManuel Francisco Naranjo /* Vendor and Product ID */
573fe70ba2SManuel Francisco Naranjo #define AIRCABLE_VID		0x16CA
583fe70ba2SManuel Francisco Naranjo #define AIRCABLE_USB_PID	0x1502
593fe70ba2SManuel Francisco Naranjo 
603fe70ba2SManuel Francisco Naranjo /* Protocol Stuff */
613fe70ba2SManuel Francisco Naranjo #define HCI_HEADER_LENGTH	0x4
623fe70ba2SManuel Francisco Naranjo #define TX_HEADER_0		0x20
633fe70ba2SManuel Francisco Naranjo #define TX_HEADER_1		0x29
643fe70ba2SManuel Francisco Naranjo #define RX_HEADER_0		0x00
653fe70ba2SManuel Francisco Naranjo #define RX_HEADER_1		0x20
663fe70ba2SManuel Francisco Naranjo #define HCI_COMPLETE_FRAME	64
673fe70ba2SManuel Francisco Naranjo 
683fe70ba2SManuel Francisco Naranjo /* rx_flags */
693fe70ba2SManuel Francisco Naranjo #define THROTTLED		0x01
703fe70ba2SManuel Francisco Naranjo #define ACTUALLY_THROTTLED	0x02
713fe70ba2SManuel Francisco Naranjo 
723fe70ba2SManuel Francisco Naranjo /*
733fe70ba2SManuel Francisco Naranjo  * Version Information
743fe70ba2SManuel Francisco Naranjo  */
754272568bSJohan Hovold #define DRIVER_VERSION "v2.0"
764272568bSJohan Hovold #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
773fe70ba2SManuel Francisco Naranjo #define DRIVER_DESC "AIRcable USB Driver"
783fe70ba2SManuel Francisco Naranjo 
793fe70ba2SManuel Francisco Naranjo /* ID table that will be registered with USB core */
807d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
813fe70ba2SManuel Francisco Naranjo 	{ USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
823fe70ba2SManuel Francisco Naranjo 	{ },
833fe70ba2SManuel Francisco Naranjo };
843fe70ba2SManuel Francisco Naranjo MODULE_DEVICE_TABLE(usb, id_table);
853fe70ba2SManuel Francisco Naranjo 
864272568bSJohan Hovold static int aircable_prepare_write_buffer(struct usb_serial_port *port,
87c23e5fc1SJohan Hovold 						void *dest, size_t size)
883fe70ba2SManuel Francisco Naranjo {
89c23e5fc1SJohan Hovold 	int count;
90c23e5fc1SJohan Hovold 	unsigned char *buf = dest;
913fe70ba2SManuel Francisco Naranjo 
924272568bSJohan Hovold 	count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
934272568bSJohan Hovold 					size - HCI_HEADER_LENGTH, &port->lock);
943fe70ba2SManuel Francisco Naranjo 	buf[0] = TX_HEADER_0;
953fe70ba2SManuel Francisco Naranjo 	buf[1] = TX_HEADER_1;
964272568bSJohan Hovold 	put_unaligned_le16(count, &buf[2]);
973fe70ba2SManuel Francisco Naranjo 
98f26c2889SJohan Hovold 	return count + HCI_HEADER_LENGTH;
993fe70ba2SManuel Francisco Naranjo }
1003fe70ba2SManuel Francisco Naranjo 
1013fe70ba2SManuel Francisco Naranjo static int aircable_probe(struct usb_serial *serial,
1023fe70ba2SManuel Francisco Naranjo 			  const struct usb_device_id *id)
1033fe70ba2SManuel Francisco Naranjo {
104c4d0f8cbSAlan Cox 	struct usb_host_interface *iface_desc = serial->interface->
105c4d0f8cbSAlan Cox 								cur_altsetting;
1063fe70ba2SManuel Francisco Naranjo 	struct usb_endpoint_descriptor *endpoint;
1073fe70ba2SManuel Francisco Naranjo 	int num_bulk_out = 0;
1083fe70ba2SManuel Francisco Naranjo 	int i;
1093fe70ba2SManuel Francisco Naranjo 
1103fe70ba2SManuel Francisco Naranjo 	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1113fe70ba2SManuel Francisco Naranjo 		endpoint = &iface_desc->endpoint[i].desc;
112377f13bfSLuiz Fernando N. Capitulino 		if (usb_endpoint_is_bulk_out(endpoint)) {
1133fe70ba2SManuel Francisco Naranjo 			dbg("found bulk out on endpoint %d", i);
1143fe70ba2SManuel Francisco Naranjo 			++num_bulk_out;
1153fe70ba2SManuel Francisco Naranjo 		}
1163fe70ba2SManuel Francisco Naranjo 	}
1173fe70ba2SManuel Francisco Naranjo 
1183fe70ba2SManuel Francisco Naranjo 	if (num_bulk_out == 0) {
1193fe70ba2SManuel Francisco Naranjo 		dbg("Invalid interface, discarding");
1203fe70ba2SManuel Francisco Naranjo 		return -ENODEV;
1213fe70ba2SManuel Francisco Naranjo 	}
1223fe70ba2SManuel Francisco Naranjo 
1233fe70ba2SManuel Francisco Naranjo 	return 0;
1243fe70ba2SManuel Francisco Naranjo }
1253fe70ba2SManuel Francisco Naranjo 
1264272568bSJohan Hovold static int aircable_process_packet(struct tty_struct *tty,
1274272568bSJohan Hovold 			struct usb_serial_port *port, int has_headers,
1284272568bSJohan Hovold 			char *packet, int len)
1293fe70ba2SManuel Francisco Naranjo {
1304272568bSJohan Hovold 	if (has_headers) {
1314272568bSJohan Hovold 		len -= HCI_HEADER_LENGTH;
1324272568bSJohan Hovold 		packet += HCI_HEADER_LENGTH;
1333fe70ba2SManuel Francisco Naranjo 	}
1344272568bSJohan Hovold 	if (len <= 0) {
1354272568bSJohan Hovold 		dbg("%s - malformed packet", __func__);
1363fe70ba2SManuel Francisco Naranjo 		return 0;
1373fe70ba2SManuel Francisco Naranjo 	}
1383fe70ba2SManuel Francisco Naranjo 
1394272568bSJohan Hovold 	tty_insert_flip_string(tty, packet, len);
1403fe70ba2SManuel Francisco Naranjo 
1414272568bSJohan Hovold 	return len;
1423fe70ba2SManuel Francisco Naranjo }
1433fe70ba2SManuel Francisco Naranjo 
1444272568bSJohan Hovold static void aircable_process_read_urb(struct urb *urb)
1453fe70ba2SManuel Francisco Naranjo {
1463fe70ba2SManuel Francisco Naranjo 	struct usb_serial_port *port = urb->context;
1474272568bSJohan Hovold 	char *data = (char *)urb->transfer_buffer;
1483fe70ba2SManuel Francisco Naranjo 	struct tty_struct *tty;
1494272568bSJohan Hovold 	int has_headers;
1504272568bSJohan Hovold 	int count;
1514272568bSJohan Hovold 	int len;
1524272568bSJohan Hovold 	int i;
1533fe70ba2SManuel Francisco Naranjo 
1544a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
1554272568bSJohan Hovold 	if (!tty)
1564272568bSJohan Hovold 		return;
1573fe70ba2SManuel Francisco Naranjo 
1584272568bSJohan Hovold 	has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
1593fe70ba2SManuel Francisco Naranjo 
1604272568bSJohan Hovold 	count = 0;
1614272568bSJohan Hovold 	for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
1624272568bSJohan Hovold 		len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
1634272568bSJohan Hovold 		count += aircable_process_packet(tty, port, has_headers,
1644272568bSJohan Hovold 								&data[i], len);
1653fe70ba2SManuel Francisco Naranjo 	}
1664272568bSJohan Hovold 
1674272568bSJohan Hovold 	if (count)
1684272568bSJohan Hovold 		tty_flip_buffer_push(tty);
1694a90f09bSAlan Cox 	tty_kref_put(tty);
1703fe70ba2SManuel Francisco Naranjo }
1713fe70ba2SManuel Francisco Naranjo 
172d9b1b787SJohannes Hölzl static struct usb_driver aircable_driver = {
173d9b1b787SJohannes Hölzl 	.name =		"aircable",
174d9b1b787SJohannes Hölzl 	.probe =	usb_serial_probe,
175d9b1b787SJohannes Hölzl 	.disconnect =	usb_serial_disconnect,
176d9b1b787SJohannes Hölzl 	.id_table =	id_table,
177d9b1b787SJohannes Hölzl 	.no_dynamic_id =	1,
178d9b1b787SJohannes Hölzl };
179d9b1b787SJohannes Hölzl 
1803fe70ba2SManuel Francisco Naranjo static struct usb_serial_driver aircable_device = {
18152d67f0bSJohannes Hölzl 	.driver = {
18252d67f0bSJohannes Hölzl 		.owner =	THIS_MODULE,
18352d67f0bSJohannes Hölzl 		.name =		"aircable",
18452d67f0bSJohannes Hölzl 	},
185d9b1b787SJohannes Hölzl 	.usb_driver = 		&aircable_driver,
1863fe70ba2SManuel Francisco Naranjo 	.id_table = 		id_table,
1873fe70ba2SManuel Francisco Naranjo 	.num_ports =		1,
1884272568bSJohan Hovold 	.bulk_out_size =	HCI_COMPLETE_FRAME,
1893fe70ba2SManuel Francisco Naranjo 	.probe =		aircable_probe,
1904272568bSJohan Hovold 	.process_read_urb =	aircable_process_read_urb,
1914272568bSJohan Hovold 	.prepare_write_buffer =	aircable_prepare_write_buffer,
1924272568bSJohan Hovold 	.throttle =		usb_serial_generic_throttle,
1934272568bSJohan Hovold 	.unthrottle =		usb_serial_generic_unthrottle,
1943fe70ba2SManuel Francisco Naranjo };
1953fe70ba2SManuel Francisco Naranjo 
1963fe70ba2SManuel Francisco Naranjo static int __init aircable_init(void)
1973fe70ba2SManuel Francisco Naranjo {
1983fe70ba2SManuel Francisco Naranjo 	int retval;
1993fe70ba2SManuel Francisco Naranjo 	retval = usb_serial_register(&aircable_device);
2003fe70ba2SManuel Francisco Naranjo 	if (retval)
2013fe70ba2SManuel Francisco Naranjo 		goto failed_serial_register;
2023fe70ba2SManuel Francisco Naranjo 	retval = usb_register(&aircable_driver);
2033fe70ba2SManuel Francisco Naranjo 	if (retval)
2043fe70ba2SManuel Francisco Naranjo 		goto failed_usb_register;
2053fe70ba2SManuel Francisco Naranjo 	return 0;
2063fe70ba2SManuel Francisco Naranjo 
2073fe70ba2SManuel Francisco Naranjo failed_usb_register:
20878c8fb37SDave Young 	usb_serial_deregister(&aircable_device);
20978c8fb37SDave Young failed_serial_register:
2103fe70ba2SManuel Francisco Naranjo 	return retval;
2113fe70ba2SManuel Francisco Naranjo }
2123fe70ba2SManuel Francisco Naranjo 
2133fe70ba2SManuel Francisco Naranjo static void __exit aircable_exit(void)
2143fe70ba2SManuel Francisco Naranjo {
2153fe70ba2SManuel Francisco Naranjo 	usb_deregister(&aircable_driver);
2163fe70ba2SManuel Francisco Naranjo 	usb_serial_deregister(&aircable_device);
2173fe70ba2SManuel Francisco Naranjo }
2183fe70ba2SManuel Francisco Naranjo 
2193fe70ba2SManuel Francisco Naranjo MODULE_AUTHOR(DRIVER_AUTHOR);
2203fe70ba2SManuel Francisco Naranjo MODULE_DESCRIPTION(DRIVER_DESC);
2213fe70ba2SManuel Francisco Naranjo MODULE_VERSION(DRIVER_VERSION);
2223fe70ba2SManuel Francisco Naranjo MODULE_LICENSE("GPL");
2233fe70ba2SManuel Francisco Naranjo 
2243fe70ba2SManuel Francisco Naranjo module_init(aircable_init);
2253fe70ba2SManuel Francisco Naranjo module_exit(aircable_exit);
2263fe70ba2SManuel Francisco Naranjo 
2273fe70ba2SManuel Francisco Naranjo module_param(debug, bool, S_IRUGO | S_IWUSR);
2283fe70ba2SManuel Francisco Naranjo MODULE_PARM_DESC(debug, "Debug enabled or not");
229