162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * AIRcable USB Bluetooth Dongle Driver.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
662306a36Sopenharmony_ci * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * The device works as an standard CDC device, it has 2 interfaces, the first
962306a36Sopenharmony_ci * one is for firmware access and the second is the serial one.
1062306a36Sopenharmony_ci * The protocol is very simply, there are two possibilities reading or writing.
1162306a36Sopenharmony_ci * When writing the first urb must have a Header that starts with 0x20 0x29 the
1262306a36Sopenharmony_ci * next two bytes must say how much data will be sent.
1362306a36Sopenharmony_ci * When reading the process is almost equal except that the header starts with
1462306a36Sopenharmony_ci * 0x00 0x20.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * The device simply need some stuff to understand data coming from the usb
1762306a36Sopenharmony_ci * buffer: The First and Second byte is used for a Header, the Third and Fourth
1862306a36Sopenharmony_ci * tells the  device the amount of information the package holds.
1962306a36Sopenharmony_ci * Packages are 60 bytes long Header Stuff.
2062306a36Sopenharmony_ci * When writing to the device the first two bytes of the header are 0x20 0x29
2162306a36Sopenharmony_ci * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
2262306a36Sopenharmony_ci * situation, when too much data arrives to the device because it sends the data
2362306a36Sopenharmony_ci * but with out the header. I will use a simply hack to override this situation,
2462306a36Sopenharmony_ci * if there is data coming that does not contain any header, then that is data
2562306a36Sopenharmony_ci * that must go directly to the tty, as there is no documentation about if there
2662306a36Sopenharmony_ci * is any other control code, I will simply check for the first
2762306a36Sopenharmony_ci * one.
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * I have taken some info from a Greg Kroah-Hartman article:
3062306a36Sopenharmony_ci * http://www.linuxjournal.com/article/6573
3162306a36Sopenharmony_ci * And from Linux Device Driver Kit CD, which is a great work, the authors taken
3262306a36Sopenharmony_ci * the work to recompile lots of information an knowledge in drivers development
3362306a36Sopenharmony_ci * and made it all available inside a cd.
3462306a36Sopenharmony_ci * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#include <asm/unaligned.h>
3962306a36Sopenharmony_ci#include <linux/tty.h>
4062306a36Sopenharmony_ci#include <linux/slab.h>
4162306a36Sopenharmony_ci#include <linux/module.h>
4262306a36Sopenharmony_ci#include <linux/tty_flip.h>
4362306a36Sopenharmony_ci#include <linux/usb.h>
4462306a36Sopenharmony_ci#include <linux/usb/serial.h>
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/* Vendor and Product ID */
4762306a36Sopenharmony_ci#define AIRCABLE_VID		0x16CA
4862306a36Sopenharmony_ci#define AIRCABLE_USB_PID	0x1502
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/* Protocol Stuff */
5162306a36Sopenharmony_ci#define HCI_HEADER_LENGTH	0x4
5262306a36Sopenharmony_ci#define TX_HEADER_0		0x20
5362306a36Sopenharmony_ci#define TX_HEADER_1		0x29
5462306a36Sopenharmony_ci#define RX_HEADER_0		0x00
5562306a36Sopenharmony_ci#define RX_HEADER_1		0x20
5662306a36Sopenharmony_ci#define HCI_COMPLETE_FRAME	64
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/* rx_flags */
5962306a36Sopenharmony_ci#define THROTTLED		0x01
6062306a36Sopenharmony_ci#define ACTUALLY_THROTTLED	0x02
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
6362306a36Sopenharmony_ci#define DRIVER_DESC "AIRcable USB Driver"
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/* ID table that will be registered with USB core */
6662306a36Sopenharmony_cistatic const struct usb_device_id id_table[] = {
6762306a36Sopenharmony_ci	{ USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
6862306a36Sopenharmony_ci	{ },
6962306a36Sopenharmony_ci};
7062306a36Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistatic int aircable_prepare_write_buffer(struct usb_serial_port *port,
7362306a36Sopenharmony_ci						void *dest, size_t size)
7462306a36Sopenharmony_ci{
7562306a36Sopenharmony_ci	int count;
7662306a36Sopenharmony_ci	unsigned char *buf = dest;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
7962306a36Sopenharmony_ci					size - HCI_HEADER_LENGTH, &port->lock);
8062306a36Sopenharmony_ci	buf[0] = TX_HEADER_0;
8162306a36Sopenharmony_ci	buf[1] = TX_HEADER_1;
8262306a36Sopenharmony_ci	put_unaligned_le16(count, &buf[2]);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	return count + HCI_HEADER_LENGTH;
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_cistatic int aircable_calc_num_ports(struct usb_serial *serial,
8862306a36Sopenharmony_ci					struct usb_serial_endpoints *epds)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	/* Ignore the first interface, which has no bulk endpoints. */
9162306a36Sopenharmony_ci	if (epds->num_bulk_out == 0) {
9262306a36Sopenharmony_ci		dev_dbg(&serial->interface->dev,
9362306a36Sopenharmony_ci			"ignoring interface with no bulk-out endpoints\n");
9462306a36Sopenharmony_ci		return -ENODEV;
9562306a36Sopenharmony_ci	}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	return 1;
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cistatic int aircable_process_packet(struct usb_serial_port *port,
10162306a36Sopenharmony_ci		int has_headers, char *packet, int len)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	if (has_headers) {
10462306a36Sopenharmony_ci		len -= HCI_HEADER_LENGTH;
10562306a36Sopenharmony_ci		packet += HCI_HEADER_LENGTH;
10662306a36Sopenharmony_ci	}
10762306a36Sopenharmony_ci	if (len <= 0) {
10862306a36Sopenharmony_ci		dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
10962306a36Sopenharmony_ci		return 0;
11062306a36Sopenharmony_ci	}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	tty_insert_flip_string(&port->port, packet, len);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	return len;
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_cistatic void aircable_process_read_urb(struct urb *urb)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	struct usb_serial_port *port = urb->context;
12062306a36Sopenharmony_ci	char *data = urb->transfer_buffer;
12162306a36Sopenharmony_ci	int has_headers;
12262306a36Sopenharmony_ci	int count;
12362306a36Sopenharmony_ci	int len;
12462306a36Sopenharmony_ci	int i;
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	count = 0;
12962306a36Sopenharmony_ci	for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
13062306a36Sopenharmony_ci		len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
13162306a36Sopenharmony_ci		count += aircable_process_packet(port, has_headers,
13262306a36Sopenharmony_ci								&data[i], len);
13362306a36Sopenharmony_ci	}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	if (count)
13662306a36Sopenharmony_ci		tty_flip_buffer_push(&port->port);
13762306a36Sopenharmony_ci}
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistatic struct usb_serial_driver aircable_device = {
14062306a36Sopenharmony_ci	.driver = {
14162306a36Sopenharmony_ci		.owner =	THIS_MODULE,
14262306a36Sopenharmony_ci		.name =		"aircable",
14362306a36Sopenharmony_ci	},
14462306a36Sopenharmony_ci	.id_table = 		id_table,
14562306a36Sopenharmony_ci	.bulk_out_size =	HCI_COMPLETE_FRAME,
14662306a36Sopenharmony_ci	.calc_num_ports =	aircable_calc_num_ports,
14762306a36Sopenharmony_ci	.process_read_urb =	aircable_process_read_urb,
14862306a36Sopenharmony_ci	.prepare_write_buffer =	aircable_prepare_write_buffer,
14962306a36Sopenharmony_ci	.throttle =		usb_serial_generic_throttle,
15062306a36Sopenharmony_ci	.unthrottle =		usb_serial_generic_unthrottle,
15162306a36Sopenharmony_ci};
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = {
15462306a36Sopenharmony_ci	&aircable_device, NULL
15562306a36Sopenharmony_ci};
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciMODULE_AUTHOR(DRIVER_AUTHOR);
16062306a36Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
16162306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
162