blob: 569c2200ba42a915902385ed61f71bd8e0f124cc [file] [log] [blame]
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03001/*
2 * AIRcable USB Bluetooth Dongle Driver.
3 *
Johan Hovold42725682010-05-05 23:45:24 +02004 * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03005 * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
Johan Hovold42725682010-05-05 23:45:24 +02006 *
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03007 * 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.
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +053013 * The protocol is very simply, there are two possibilities reading or writing.
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +020014 * When writing the first urb must have a Header that starts with 0x20 0x29 the
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +053015 * next two bytes must say how much data will be sent.
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030016 * When reading the process is almost equal except that the header starts with
17 * 0x00 0x20.
18 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019 * The device simply need some stuff to understand data coming from the usb
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030020 * 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.
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +020023 * When writing to the device the first two bytes of the header are 0x20 0x29
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030024 * 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 *
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030032 * I have taken some info from a Greg Kroah-Hartman article:
33 * http://www.linuxjournal.com/article/6573
34 * And from Linux Device Driver Kit CD, which is a great work, the authors taken
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +053035 * the work to recompile lots of information an knowledge in drivers development
36 * and made it all available inside a cd.
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030037 * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
38 *
39 */
40
Johan Hovold42725682010-05-05 23:45:24 +020041#include <asm/unaligned.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030042#include <linux/tty.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040044#include <linux/module.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030045#include <linux/tty_flip.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030046#include <linux/usb.h>
47#include <linux/usb/serial.h>
48
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030049/* Vendor and Product ID */
50#define AIRCABLE_VID 0x16CA
51#define AIRCABLE_USB_PID 0x1502
52
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030053/* Protocol Stuff */
54#define HCI_HEADER_LENGTH 0x4
55#define TX_HEADER_0 0x20
56#define TX_HEADER_1 0x29
57#define RX_HEADER_0 0x00
58#define RX_HEADER_1 0x20
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030059#define HCI_COMPLETE_FRAME 64
60
61/* rx_flags */
62#define THROTTLED 0x01
63#define ACTUALLY_THROTTLED 0x02
64
Johan Hovold42725682010-05-05 23:45:24 +020065#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030066#define DRIVER_DESC "AIRcable USB Driver"
67
68/* ID table that will be registered with USB core */
Németh Márton7d40d7e2010-01-10 15:34:24 +010069static const struct usb_device_id id_table[] = {
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030070 { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
71 { },
72};
73MODULE_DEVICE_TABLE(usb, id_table);
74
Johan Hovold42725682010-05-05 23:45:24 +020075static int aircable_prepare_write_buffer(struct usb_serial_port *port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +020076 void *dest, size_t size)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030077{
Johan Hovoldc23e5fc2010-05-05 23:58:13 +020078 int count;
79 unsigned char *buf = dest;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030080
Johan Hovold42725682010-05-05 23:45:24 +020081 count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
82 size - HCI_HEADER_LENGTH, &port->lock);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030083 buf[0] = TX_HEADER_0;
84 buf[1] = TX_HEADER_1;
Johan Hovold42725682010-05-05 23:45:24 +020085 put_unaligned_le16(count, &buf[2]);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030086
Johan Hovoldf26c2882010-05-19 00:01:33 +020087 return count + HCI_HEADER_LENGTH;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030088}
89
Johan Hovold5f391972017-03-16 17:13:36 +010090static int aircable_calc_num_ports(struct usb_serial *serial,
91 struct usb_serial_endpoints *epds)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030092{
Johan Hovold5f391972017-03-16 17:13:36 +010093 /* Ignore the first interface, which has no bulk endpoints. */
94 if (epds->num_bulk_out == 0) {
95 dev_dbg(&serial->interface->dev,
96 "ignoring interface with no bulk-out endpoints\n");
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030097 return -ENODEV;
98 }
99
Johan Hovold5f391972017-03-16 17:13:36 +0100100 return 1;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300101}
102
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100103static int aircable_process_packet(struct usb_serial_port *port,
104 int has_headers, char *packet, int len)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300105{
Johan Hovold42725682010-05-05 23:45:24 +0200106 if (has_headers) {
107 len -= HCI_HEADER_LENGTH;
108 packet += HCI_HEADER_LENGTH;
109 }
110 if (len <= 0) {
Greg Kroah-Hartman66afb5b2012-05-15 16:27:08 -0700111 dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
Johan Hovold42725682010-05-05 23:45:24 +0200112 return 0;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300113 }
114
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100115 tty_insert_flip_string(&port->port, packet, len);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300116
Johan Hovold42725682010-05-05 23:45:24 +0200117 return len;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300118}
119
Johan Hovold42725682010-05-05 23:45:24 +0200120static void aircable_process_read_urb(struct urb *urb)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300121{
122 struct usb_serial_port *port = urb->context;
Johan Hovold42725682010-05-05 23:45:24 +0200123 char *data = (char *)urb->transfer_buffer;
Johan Hovold42725682010-05-05 23:45:24 +0200124 int has_headers;
125 int count;
126 int len;
127 int i;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300128
Johan Hovold42725682010-05-05 23:45:24 +0200129 has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300130
Johan Hovold42725682010-05-05 23:45:24 +0200131 count = 0;
132 for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
133 len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100134 count += aircable_process_packet(port, has_headers,
Johan Hovold42725682010-05-05 23:45:24 +0200135 &data[i], len);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300136 }
Johan Hovold42725682010-05-05 23:45:24 +0200137
138 if (count)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100139 tty_flip_buffer_push(&port->port);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300140}
141
142static struct usb_serial_driver aircable_device = {
Johannes Hölzl52d67f02006-12-17 22:05:09 +0100143 .driver = {
144 .owner = THIS_MODULE,
145 .name = "aircable",
146 },
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300147 .id_table = id_table,
Johan Hovold42725682010-05-05 23:45:24 +0200148 .bulk_out_size = HCI_COMPLETE_FRAME,
Johan Hovold5f391972017-03-16 17:13:36 +0100149 .calc_num_ports = aircable_calc_num_ports,
Johan Hovold42725682010-05-05 23:45:24 +0200150 .process_read_urb = aircable_process_read_urb,
151 .prepare_write_buffer = aircable_prepare_write_buffer,
152 .throttle = usb_serial_generic_throttle,
153 .unthrottle = usb_serial_generic_unthrottle,
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300154};
155
Alan Stern08a4f6b2012-02-23 14:56:17 -0500156static struct usb_serial_driver * const serial_drivers[] = {
157 &aircable_device, NULL
158};
159
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700160module_usb_serial_driver(serial_drivers, id_table);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300161
162MODULE_AUTHOR(DRIVER_AUTHOR);
163MODULE_DESCRIPTION(DRIVER_DESC);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300164MODULE_LICENSE("GPL");