Linux Audio

Check our new training course

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