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