2 * bluetooth.c Version 0.12
4 * Copyright (C) 2000, 2001 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2000 Mark Douglas Corner <mcorner@umich.edu>
7 * USB Bluetooth TTY driver, based on the Bluetooth Spec version 1.0B
9 * (2001/11/30) Version 0.13 gkh
10 * - added locking patch from Masoodur Rahman <rmasoodu@in.ibm.com>
11 * - removed active variable, as open_count will do.
13 * (2001/07/09) Version 0.12 gkh
14 * - removed in_interrupt() call, as it doesn't make sense to do
17 * (2001/06/05) Version 0.11 gkh
18 * - Fixed problem with read urb status saying that we have shutdown,
19 * and that we shouldn't resubmit the urb. Patch from unknown.
21 * (2001/05/28) Version 0.10 gkh
22 * - Fixed problem with using data from userspace in the bluetooth_write
23 * function as found by the CHECKER project.
24 * - Added a buffer to the write_urb_pool which reduces the number of
25 * buffers being created and destroyed for ever write. Also cleans
27 * - Added a buffer to the control_urb_pool which fixes a memory leak
28 * when the device is removed from the system.
30 * (2001/05/28) Version 0.9 gkh
31 * Fixed problem with bluetooth==NULL for bluetooth_read_bulk_callback
32 * which was found by both the CHECKER project and Mikko Rahkonen.
35 * Identify version on module load.
37 * (2001/03/10) Version 0.8 gkh
38 * Fixed problem with not unlinking interrupt urb on device close
39 * and resubmitting the read urb on error with bluetooth struct.
40 * Thanks to Narayan Mohanram <narayan@RovingNetworks.com> for the
43 * (11/29/2000) Version 0.7 gkh
44 * Fixed problem with overrunning the tty flip buffer.
45 * Removed unneeded NULL pointer initialization.
47 * (10/05/2000) Version 0.6 gkh
48 * Fixed bug with urb->dev not being set properly, now that the usb
50 * Got a real major id number and name.
52 * (08/06/2000) Version 0.5 gkh
53 * Fixed problem of not resubmitting the bulk read urb if there is
54 * an error in the callback. Ericsson devices seem to need this.
56 * (07/11/2000) Version 0.4 gkh
57 * Fixed bug in disconnect for when we call tty_hangup
58 * Fixed bug in bluetooth_ctrl_msg where the bluetooth struct was not
59 * getting attached to the control urb properly.
60 * Fixed bug in bluetooth_write where we pay attention to the result
61 * of bluetooth_ctrl_msg.
63 * (08/03/2000) Version 0.3 gkh mdc
64 * Merged in Mark's changes to make the driver play nice with the Axis
66 * Made the write bulk use an urb pool to enable larger transfers with
67 * fewer calls to the driver.
68 * Fixed off by one bug in acl pkt receive
69 * Made packet counters specific to each bluetooth device
70 * Added checks for zero length callbacks
71 * Added buffers for int and bulk packets. Had to do this otherwise
72 * packet types could intermingle.
73 * Made a control urb pool for the control messages.
75 * (07/11/2000) Version 0.2 gkh
76 * Fixed a small bug found by Nils Faerber in the usb_bluetooth_probe
79 * (07/09/2000) Version 0.1 gkh
80 * Initial release. Has support for sending ACL data (which is really just
81 * a HCI frame.) Raw HCI commands and HCI events are not supported.
82 * A ioctl will probably be needed for the HCI commands and events in the
83 * future. All isoch endpoints are ignored at this time also.
84 * This driver should work for all currently shipping USB Bluetooth
85 * devices at this time :)
90 * This program is free software; you can redistribute it and/or modify
91 * it under the terms of the GNU General Public License as published by
92 * the Free Software Foundation; either version 2 of the License, or
93 * (at your option) any later version.
95 * This program is distributed in the hope that it will be useful,
96 * but WITHOUT ANY WARRANTY; without even the implied warranty of
97 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98 * GNU General Public License for more details.
100 * You should have received a copy of the GNU General Public License
101 * along with this program; if not, write to the Free Software
102 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
106 #include <linux/kernel.h>
107 #include <linux/errno.h>
108 #include <linux/init.h>
109 #include <linux/slab.h>
110 #include <linux/tty.h>
111 #include <linux/tty_driver.h>
112 #include <linux/tty_flip.h>
113 #include <linux/module.h>
114 #include <asm/uaccess.h>
117 #include <linux/usb.h>
120 * Version Information
122 #define DRIVER_VERSION "v0.13"
123 #define DRIVER_AUTHOR "Greg Kroah-Hartman, Mark Douglas Corner"
124 #define DRIVER_DESC "USB Bluetooth tty driver"
126 /* define this if you have hardware that is not good */
127 /*#define BTBUGGYHARDWARE */
129 /* Class, SubClass, and Protocol codes that describe a Bluetooth device */
130 #define WIRELESS_CLASS_CODE 0xe0
131 #define RF_SUBCLASS_CODE 0x01
132 #define BLUETOOTH_PROGRAMMING_PROTOCOL_CODE 0x01
135 #define BLUETOOTH_TTY_MAJOR 216 /* real device node major id */
136 #define BLUETOOTH_TTY_MINORS 256 /* whole lotta bluetooth devices */
138 #define USB_BLUETOOTH_MAGIC 0x6d02 /* magic number for bluetooth struct */
140 #define BLUETOOTH_CONTROL_REQUEST_TYPE 0x20
142 /* Bluetooth packet types */
146 #define EVENT_PKT 0x04
147 #define ERROR_PKT 0x05
151 #define MAX_EVENT_SIZE 0xFF
152 #define EVENT_HDR_SIZE 3 /* 2 for the header + 1 for the type indicator */
153 #define EVENT_BUFFER_SIZE (MAX_EVENT_SIZE + EVENT_HDR_SIZE)
155 #define MAX_ACL_SIZE 0xFFFF
156 #define ACL_HDR_SIZE 5 /* 4 for the header + 1 for the type indicator */
157 #define ACL_BUFFER_SIZE (MAX_ACL_SIZE + ACL_HDR_SIZE)
159 /* parity check flag */
160 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
162 #define CHAR2INT16(c1,c0) (((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
164 #define NUM_BULK_URBS 24
165 #define NUM_CONTROL_URBS 16
167 struct usb_bluetooth {
169 struct usb_device * dev;
170 struct tty_driver * tty_driver; /* the tty_driver for this device */
171 struct tty_struct * tty; /* the coresponding tty for this port */
173 unsigned char minor; /* the starting minor number for this device */
174 int throttle; /* throttled by tty layer */
177 __u8 control_out_bInterfaceNum;
178 struct urb * control_urb_pool[NUM_CONTROL_URBS];
179 struct usb_ctrlrequest dr[NUM_CONTROL_URBS];
181 unsigned char * interrupt_in_buffer;
182 struct urb * interrupt_in_urb;
183 __u8 interrupt_in_endpointAddress;
184 __u8 interrupt_in_interval;
185 int interrupt_in_buffer_size;
187 unsigned char * bulk_in_buffer;
188 struct urb * read_urb;
189 __u8 bulk_in_endpointAddress;
190 int bulk_in_buffer_size;
192 int bulk_out_buffer_size;
193 struct urb * write_urb_pool[NUM_BULK_URBS];
194 __u8 bulk_out_endpointAddress;
196 wait_queue_head_t write_wait;
198 struct tq_struct tqueue; /* task queue for line discipline waking up */
200 unsigned int int_packet_pos;
201 unsigned char int_buffer[EVENT_BUFFER_SIZE];
202 unsigned int bulk_packet_pos;
203 unsigned char bulk_buffer[ACL_BUFFER_SIZE]; /* 64k preallocated, fix? */
204 struct semaphore lock;
208 /* local function prototypes */
209 static int bluetooth_open (struct tty_struct *tty, struct file *filp);
210 static void bluetooth_close (struct tty_struct *tty, struct file *filp);
211 static int bluetooth_write (struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
212 static int bluetooth_write_room (struct tty_struct *tty);
213 static int bluetooth_chars_in_buffer (struct tty_struct *tty);
214 static void bluetooth_throttle (struct tty_struct *tty);
215 static void bluetooth_unthrottle (struct tty_struct *tty);
216 static int bluetooth_ioctl (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
217 static void bluetooth_set_termios (struct tty_struct *tty, struct termios *old);
219 static void bluetooth_int_callback (struct urb *urb);
220 static void bluetooth_ctrl_callback (struct urb *urb);
221 static void bluetooth_read_bulk_callback (struct urb *urb);
222 static void bluetooth_write_bulk_callback (struct urb *urb);
224 static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
225 const struct usb_device_id *id);
226 static void usb_bluetooth_disconnect (struct usb_device *dev, void *ptr);
229 static struct usb_device_id usb_bluetooth_ids [] = {
230 { USB_DEVICE_INFO(WIRELESS_CLASS_CODE, RF_SUBCLASS_CODE, BLUETOOTH_PROGRAMMING_PROTOCOL_CODE) },
231 { } /* Terminating entry */
234 MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
236 static struct usb_driver usb_bluetooth_driver = {
238 .probe = usb_bluetooth_probe,
239 .disconnect = usb_bluetooth_disconnect,
240 .id_table = usb_bluetooth_ids,
243 static int bluetooth_refcount;
244 static struct tty_driver bluetooth_tty_driver;
245 static struct tty_struct * bluetooth_tty[BLUETOOTH_TTY_MINORS];
246 static struct termios * bluetooth_termios[BLUETOOTH_TTY_MINORS];
247 static struct termios * bluetooth_termios_locked[BLUETOOTH_TTY_MINORS];
248 static struct usb_bluetooth *bluetooth_table[BLUETOOTH_TTY_MINORS];
251 static inline int bluetooth_paranoia_check (struct usb_bluetooth *bluetooth, const char *function)
254 dbg("%s - bluetooth == NULL", function);
257 if (bluetooth->magic != USB_BLUETOOTH_MAGIC) {
258 dbg("%s - bad magic number for bluetooth", function);
266 static inline struct usb_bluetooth* get_usb_bluetooth (struct usb_bluetooth *bluetooth, const char *function)
269 bluetooth_paranoia_check (bluetooth, function)) {
270 /* then say that we dont have a valid usb_bluetooth thing, which will
271 * end up generating -ENODEV return values */
279 static inline struct usb_bluetooth *get_bluetooth_by_minor (int minor)
281 return bluetooth_table[minor];
285 static int bluetooth_ctrl_msg (struct usb_bluetooth *bluetooth, int request, int value, const unsigned char *buf, int len)
287 struct urb *urb = NULL;
288 struct usb_ctrlrequest *dr = NULL;
292 dbg ("%s", __FUNCTION__);
294 /* try to find a free urb in our list */
295 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
296 if (bluetooth->control_urb_pool[i]->status != -EINPROGRESS) {
297 urb = bluetooth->control_urb_pool[i];
298 dr = &bluetooth->dr[i];
303 dbg ("%s - no free urbs", __FUNCTION__);
307 /* keep increasing the urb transfer buffer to fit the size of the message */
308 if (urb->transfer_buffer == NULL) {
309 urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
310 if (urb->transfer_buffer == NULL) {
311 err ("%s - out of memory", __FUNCTION__);
315 if (urb->transfer_buffer_length < len) {
316 kfree (urb->transfer_buffer);
317 urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
318 if (urb->transfer_buffer == NULL) {
319 err ("%s - out of memory", __FUNCTION__);
323 memcpy (urb->transfer_buffer, buf, len);
325 dr->bRequestType= BLUETOOTH_CONTROL_REQUEST_TYPE;
326 dr->bRequest = request;
327 dr->wValue = cpu_to_le16((u16) value);
328 dr->wIndex = cpu_to_le16((u16) bluetooth->control_out_bInterfaceNum);
329 dr->wLength = cpu_to_le16((u16) len);
331 FILL_CONTROL_URB (urb, bluetooth->dev, usb_sndctrlpipe(bluetooth->dev, 0),
332 (unsigned char*)dr, urb->transfer_buffer, len, bluetooth_ctrl_callback, bluetooth);
334 /* send it down the pipe */
335 status = usb_submit_urb(urb);
337 dbg("%s - usb_submit_urb(control) failed with status = %d", __FUNCTION__, status);
346 /*****************************************************************************
347 * Driver tty interface functions
348 *****************************************************************************/
349 static int bluetooth_open (struct tty_struct *tty, struct file * filp)
351 struct usb_bluetooth *bluetooth;
354 dbg("%s", __FUNCTION__);
356 /* initialize the pointer incase something fails */
357 tty->driver_data = NULL;
359 /* get the bluetooth object associated with this tty pointer */
360 bluetooth = get_bluetooth_by_minor (MINOR(tty->device));
362 if (bluetooth_paranoia_check (bluetooth, __FUNCTION__)) {
366 down (&bluetooth->lock);
368 ++bluetooth->open_count;
369 if (bluetooth->open_count == 1) {
370 /* set up our structure making the tty driver remember our object, and us it */
371 tty->driver_data = bluetooth;
372 bluetooth->tty = tty;
374 /* force low_latency on so that our tty_push actually forces the data through,
375 * otherwise it is scheduled, and with high data rates (like with OHCI) data
377 bluetooth->tty->low_latency = 1;
379 /* Reset the packet position counters */
380 bluetooth->int_packet_pos = 0;
381 bluetooth->bulk_packet_pos = 0;
383 #ifndef BTBUGGYHARDWARE
384 /* Start reading from the device */
385 FILL_BULK_URB (bluetooth->read_urb, bluetooth->dev,
386 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
387 bluetooth->bulk_in_buffer,
388 bluetooth->bulk_in_buffer_size,
389 bluetooth_read_bulk_callback, bluetooth);
390 result = usb_submit_urb(bluetooth->read_urb);
392 dbg("%s - usb_submit_urb(read bulk) failed with status %d", __FUNCTION__, result);
394 FILL_INT_URB (bluetooth->interrupt_in_urb, bluetooth->dev,
395 usb_rcvintpipe(bluetooth->dev, bluetooth->interrupt_in_endpointAddress),
396 bluetooth->interrupt_in_buffer,
397 bluetooth->interrupt_in_buffer_size,
398 bluetooth_int_callback, bluetooth,
399 bluetooth->interrupt_in_interval);
400 result = usb_submit_urb(bluetooth->interrupt_in_urb);
402 dbg("%s - usb_submit_urb(interrupt in) failed with status %d", __FUNCTION__, result);
405 up(&bluetooth->lock);
411 static void bluetooth_close (struct tty_struct *tty, struct file * filp)
413 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
420 dbg("%s", __FUNCTION__);
422 if (!bluetooth->open_count) {
423 dbg ("%s - device not opened", __FUNCTION__);
427 down (&bluetooth->lock);
429 --bluetooth->open_count;
430 if (bluetooth->open_count <= 0) {
431 bluetooth->open_count = 0;
433 /* shutdown any bulk reads and writes that might be going on */
434 for (i = 0; i < NUM_BULK_URBS; ++i)
435 usb_unlink_urb (bluetooth->write_urb_pool[i]);
436 usb_unlink_urb (bluetooth->read_urb);
437 usb_unlink_urb (bluetooth->interrupt_in_urb);
439 up(&bluetooth->lock);
443 static int bluetooth_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
445 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
446 struct urb *urb = NULL;
447 unsigned char *temp_buffer = NULL;
448 const unsigned char *current_buffer;
449 const unsigned char *current_position;
459 dbg("%s - %d byte(s)", __FUNCTION__, count);
461 if (!bluetooth->open_count) {
462 dbg ("%s - device not opened", __FUNCTION__);
467 dbg("%s - write request of 0 bytes", __FUNCTION__);
471 dbg("%s - write request only included type %d", __FUNCTION__, buf[0]);
476 printk (KERN_DEBUG __FILE__ ": %s - length = %d, data = ", __FUNCTION__, count);
477 for (i = 0; i < count; ++i) {
478 printk ("%.2x ", buf[i]);
484 temp_buffer = kmalloc (count, GFP_KERNEL);
485 if (temp_buffer == NULL) {
486 err ("%s - out of memory.", __FUNCTION__);
490 if (copy_from_user (temp_buffer, buf, count)) {
494 current_buffer = temp_buffer;
496 current_buffer = buf;
499 switch (*current_buffer) {
500 /* First byte indicates the type of packet */
502 /* dbg("%s- Send cmd_pkt len:%d", __FUNCTION__, count);*/
504 retval = bluetooth_ctrl_msg (bluetooth, 0x00, 0x00, ¤t_buffer[1], count-1);
512 current_position = current_buffer;
520 /* try to find a free urb in our list */
521 for (i = 0; i < NUM_BULK_URBS; ++i) {
522 if (bluetooth->write_urb_pool[i]->status != -EINPROGRESS) {
523 urb = bluetooth->write_urb_pool[i];
528 dbg ("%s - no free urbs", __FUNCTION__);
534 buffer_size = min (count, bluetooth->bulk_out_buffer_size);
535 memcpy (urb->transfer_buffer, current_position, buffer_size);
537 /* build up our urb */
538 FILL_BULK_URB (urb, bluetooth->dev, usb_sndbulkpipe(bluetooth->dev, bluetooth->bulk_out_endpointAddress),
539 urb->transfer_buffer, buffer_size, bluetooth_write_bulk_callback, bluetooth);
540 urb->transfer_flags |= USB_QUEUE_BULK;
542 /* send it down the pipe */
543 retval = usb_submit_urb(urb);
545 dbg("%s - usb_submit_urb(write bulk) failed with error = %d", __FUNCTION__, retval);
548 #ifdef BTBUGGYHARDWARE
549 /* A workaround for the stalled data bug */
550 /* May or may not be needed...*/
555 current_position += buffer_size;
556 bytes_sent += buffer_size;
557 count -= buffer_size;
560 retval = bytes_sent + 1;
564 dbg("%s - unsupported (at this time) write type", __FUNCTION__);
570 if (temp_buffer != NULL)
577 static int bluetooth_write_room (struct tty_struct *tty)
579 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
587 dbg("%s", __FUNCTION__);
589 if (!bluetooth->open_count) {
590 dbg ("%s - device not open", __FUNCTION__);
594 for (i = 0; i < NUM_BULK_URBS; ++i) {
595 if (bluetooth->write_urb_pool[i]->status != -EINPROGRESS) {
596 room += bluetooth->bulk_out_buffer_size;
600 dbg("%s - returns %d", __FUNCTION__, room);
605 static int bluetooth_chars_in_buffer (struct tty_struct *tty)
607 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
615 if (!bluetooth->open_count) {
616 dbg ("%s - device not open", __FUNCTION__);
620 for (i = 0; i < NUM_BULK_URBS; ++i) {
621 if (bluetooth->write_urb_pool[i]->status == -EINPROGRESS) {
622 chars += bluetooth->write_urb_pool[i]->transfer_buffer_length;
626 dbg ("%s - returns %d", __FUNCTION__, chars);
631 static void bluetooth_throttle (struct tty_struct * tty)
633 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
639 dbg("%s", __FUNCTION__);
641 if (!bluetooth->open_count) {
642 dbg ("%s - device not open", __FUNCTION__);
646 dbg("%s unsupported (at this time)", __FUNCTION__);
652 static void bluetooth_unthrottle (struct tty_struct * tty)
654 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
660 dbg("%s", __FUNCTION__);
662 if (!bluetooth->open_count) {
663 dbg ("%s - device not open", __FUNCTION__);
667 dbg("%s unsupported (at this time)", __FUNCTION__);
671 static int bluetooth_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
673 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
679 dbg("%s - cmd 0x%.4x", __FUNCTION__, cmd);
681 if (!bluetooth->open_count) {
682 dbg ("%s - device not open", __FUNCTION__);
691 static void bluetooth_set_termios (struct tty_struct *tty, struct termios * old)
693 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
699 dbg("%s", __FUNCTION__);
701 if (!bluetooth->open_count) {
702 dbg ("%s - device not open", __FUNCTION__);
712 #ifdef BTBUGGYHARDWARE
713 void btusb_enable_bulk_read(struct tty_struct *tty){
714 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
721 dbg("%s", __FUNCTION__);
723 if (!bluetooth->open_count) {
724 dbg ("%s - device not open", __FUNCTION__);
728 if (bluetooth->read_urb) {
729 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev,
730 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
731 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
732 bluetooth_read_bulk_callback, bluetooth);
733 result = usb_submit_urb(bluetooth->read_urb);
735 err ("%s - failed submitting read urb, error %d", __FUNCTION__, result);
739 void btusb_disable_bulk_read(struct tty_struct *tty){
740 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
746 dbg("%s", __FUNCTION__);
748 if (!bluetooth->open_count) {
749 dbg ("%s - device not open", __FUNCTION__);
753 if ((bluetooth->read_urb) && (bluetooth->read_urb->actual_length))
754 usb_unlink_urb(bluetooth->read_urb);
759 /*****************************************************************************
760 * urb callback functions
761 *****************************************************************************/
764 static void bluetooth_int_callback (struct urb *urb)
766 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
767 unsigned char *data = urb->transfer_buffer;
769 unsigned int count = urb->actual_length;
770 unsigned int packet_size;
772 dbg("%s", __FUNCTION__);
775 dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
780 dbg("%s - nonzero int status received: %d", __FUNCTION__, urb->status);
785 dbg("%s - zero length int", __FUNCTION__);
792 printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count);
793 for (i = 0; i < count; ++i) {
794 printk ("%.2x ", data[i]);
800 #ifdef BTBUGGYHARDWARE
801 if ((count >= 2) && (data[0] == 0xFF) && (data[1] == 0x00)) {
806 urb->actual_length = 0;
810 /* We add a packet type identifier to the beginning of each
811 HCI frame. This makes the data in the tty look like a
812 serial USB devices. Each HCI frame can be broken across
813 multiple URBs so we buffer them until we have a full hci
816 if (!bluetooth->int_packet_pos) {
817 bluetooth->int_buffer[0] = EVENT_PKT;
818 bluetooth->int_packet_pos++;
821 if (bluetooth->int_packet_pos + count > EVENT_BUFFER_SIZE) {
822 err("%s - exceeded EVENT_BUFFER_SIZE", __FUNCTION__);
823 bluetooth->int_packet_pos = 0;
827 memcpy (&bluetooth->int_buffer[bluetooth->int_packet_pos],
828 urb->transfer_buffer, count);
829 bluetooth->int_packet_pos += count;
830 urb->actual_length = 0;
832 if (bluetooth->int_packet_pos >= EVENT_HDR_SIZE)
833 packet_size = bluetooth->int_buffer[2];
837 if (packet_size + EVENT_HDR_SIZE < bluetooth->int_packet_pos) {
838 err("%s - packet was too long", __FUNCTION__);
839 bluetooth->int_packet_pos = 0;
843 if (packet_size + EVENT_HDR_SIZE == bluetooth->int_packet_pos) {
844 for (i = 0; i < bluetooth->int_packet_pos; ++i) {
845 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them */
846 if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
847 tty_flip_buffer_push(bluetooth->tty);
849 tty_insert_flip_char(bluetooth->tty, bluetooth->int_buffer[i], 0);
851 tty_flip_buffer_push(bluetooth->tty);
853 bluetooth->int_packet_pos = 0;
858 static void bluetooth_ctrl_callback (struct urb *urb)
860 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
862 dbg("%s", __FUNCTION__);
865 dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
870 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
876 static void bluetooth_read_bulk_callback (struct urb *urb)
878 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
879 unsigned char *data = urb->transfer_buffer;
880 unsigned int count = urb->actual_length;
882 unsigned int packet_size;
886 dbg("%s", __FUNCTION__);
889 dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
894 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
895 if (urb->status == -ENOENT) {
896 dbg("%s - URB canceled, won't reschedule", __FUNCTION__);
903 dbg("%s - zero length read bulk", __FUNCTION__);
909 printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count);
910 for (i = 0; i < count; ++i) {
911 printk ("%.2x ", data[i]);
916 #ifdef BTBUGGYHARDWARE
917 if ((count == 4) && (data[0] == 0x00) && (data[1] == 0x00)
918 && (data[2] == 0x00) && (data[3] == 0x00)) {
919 urb->actual_length = 0;
920 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev,
921 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
922 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
923 bluetooth_read_bulk_callback, bluetooth);
924 result = usb_submit_urb(bluetooth->read_urb);
926 err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
931 /* We add a packet type identifier to the beginning of each
932 HCI frame. This makes the data in the tty look like a
933 serial USB devices. Each HCI frame can be broken across
934 multiple URBs so we buffer them until we have a full hci
937 if (!bluetooth->bulk_packet_pos) {
938 bluetooth->bulk_buffer[0] = ACL_PKT;
939 bluetooth->bulk_packet_pos++;
942 if (bluetooth->bulk_packet_pos + count > ACL_BUFFER_SIZE) {
943 err("%s - exceeded ACL_BUFFER_SIZE", __FUNCTION__);
944 bluetooth->bulk_packet_pos = 0;
948 memcpy (&bluetooth->bulk_buffer[bluetooth->bulk_packet_pos],
949 urb->transfer_buffer, count);
950 bluetooth->bulk_packet_pos += count;
951 urb->actual_length = 0;
953 if (bluetooth->bulk_packet_pos >= ACL_HDR_SIZE) {
954 packet_size = CHAR2INT16(bluetooth->bulk_buffer[4],bluetooth->bulk_buffer[3]);
959 if (packet_size + ACL_HDR_SIZE < bluetooth->bulk_packet_pos) {
960 err("%s - packet was too long", __FUNCTION__);
961 bluetooth->bulk_packet_pos = 0;
965 if (packet_size + ACL_HDR_SIZE == bluetooth->bulk_packet_pos) {
966 for (i = 0; i < bluetooth->bulk_packet_pos; ++i) {
967 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
968 if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
969 tty_flip_buffer_push(bluetooth->tty);
971 tty_insert_flip_char(bluetooth->tty, bluetooth->bulk_buffer[i], 0);
973 tty_flip_buffer_push(bluetooth->tty);
974 bluetooth->bulk_packet_pos = 0;
978 if (!bluetooth || !bluetooth->open_count)
981 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev,
982 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
983 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
984 bluetooth_read_bulk_callback, bluetooth);
985 result = usb_submit_urb(bluetooth->read_urb);
987 err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
993 static void bluetooth_write_bulk_callback (struct urb *urb)
995 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
997 dbg("%s", __FUNCTION__);
1000 dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
1005 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1009 /* wake up our little function to let the tty layer know that something happened */
1010 queue_task(&bluetooth->tqueue, &tq_immediate);
1011 mark_bh(IMMEDIATE_BH);
1016 static void bluetooth_softint(void *private)
1018 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)private, __FUNCTION__);
1019 struct tty_struct *tty;
1020 struct tty_ldisc *ld;
1022 dbg("%s", __FUNCTION__);
1028 tty = bluetooth->tty;
1029 if (tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) {
1030 ld = tty_ldisc_ref(tty);
1032 if(ld->write_wakeup) {
1033 ld->write_wakeup(tty);
1034 dbg("%s - write wakeup call.", __FUNCTION__);
1036 tty_ldisc_deref(tty);
1040 wake_up_interruptible(&tty->write_wait);
1044 static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
1045 const struct usb_device_id *id)
1047 struct usb_bluetooth *bluetooth = NULL;
1048 struct usb_interface_descriptor *interface;
1049 struct usb_endpoint_descriptor *endpoint;
1050 struct usb_endpoint_descriptor *interrupt_in_endpoint[8];
1051 struct usb_endpoint_descriptor *bulk_in_endpoint[8];
1052 struct usb_endpoint_descriptor *bulk_out_endpoint[8];
1053 int control_out_endpoint;
1058 int num_interrupt_in = 0;
1059 int num_bulk_in = 0;
1060 int num_bulk_out = 0;
1062 interface = &dev->actconfig->interface[ifnum].altsetting[0];
1063 control_out_endpoint = interface->bInterfaceNumber;
1065 /* find the endpoints that we need */
1066 for (i = 0; i < interface->bNumEndpoints; ++i) {
1067 endpoint = &interface->endpoint[i];
1069 if ((endpoint->bEndpointAddress & 0x80) &&
1070 ((endpoint->bmAttributes & 3) == 0x02)) {
1071 /* we found a bulk in endpoint */
1072 dbg("found bulk in");
1073 bulk_in_endpoint[num_bulk_in] = endpoint;
1077 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1078 ((endpoint->bmAttributes & 3) == 0x02)) {
1079 /* we found a bulk out endpoint */
1080 dbg("found bulk out");
1081 bulk_out_endpoint[num_bulk_out] = endpoint;
1085 if ((endpoint->bEndpointAddress & 0x80) &&
1086 ((endpoint->bmAttributes & 3) == 0x03)) {
1087 /* we found a interrupt in endpoint */
1088 dbg("found interrupt in");
1089 interrupt_in_endpoint[num_interrupt_in] = endpoint;
1094 /* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */
1095 if ((num_bulk_in != 1) ||
1096 (num_bulk_out != 1) ||
1097 (num_interrupt_in != 1)) {
1098 dbg ("%s - improper number of endpoints. Bluetooth driver not bound.", __FUNCTION__);
1103 info("USB Bluetooth converter detected");
1105 for (minor = 0; minor < BLUETOOTH_TTY_MINORS && bluetooth_table[minor]; ++minor)
1107 if (bluetooth_table[minor]) {
1108 err("No more free Bluetooth devices");
1113 if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) {
1114 err("Out of memory");
1119 memset(bluetooth, 0, sizeof(struct usb_bluetooth));
1121 bluetooth->magic = USB_BLUETOOTH_MAGIC;
1122 bluetooth->dev = dev;
1123 bluetooth->minor = minor;
1124 bluetooth->tqueue.routine = bluetooth_softint;
1125 bluetooth->tqueue.data = bluetooth;
1126 init_MUTEX(&bluetooth->lock);
1128 /* record the interface number for the control out */
1129 bluetooth->control_out_bInterfaceNum = control_out_endpoint;
1131 /* create our control out urb pool */
1132 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1133 struct urb *urb = usb_alloc_urb(0);
1135 err("No free urbs available");
1138 urb->transfer_buffer = NULL;
1139 bluetooth->control_urb_pool[i] = urb;
1142 /* set up the endpoint information */
1143 endpoint = bulk_in_endpoint[0];
1144 bluetooth->read_urb = usb_alloc_urb (0);
1145 if (!bluetooth->read_urb) {
1146 err("No free urbs available");
1149 bluetooth->bulk_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1150 bluetooth->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1151 bluetooth->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1152 if (!bluetooth->bulk_in_buffer) {
1153 err("Couldn't allocate bulk_in_buffer");
1156 FILL_BULK_URB(bluetooth->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
1157 bluetooth->bulk_in_buffer, buffer_size, bluetooth_read_bulk_callback, bluetooth);
1159 endpoint = bulk_out_endpoint[0];
1160 bluetooth->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1161 bluetooth->bulk_out_buffer_size = endpoint->wMaxPacketSize * 2;
1163 /* create our write urb pool */
1164 for (i = 0; i < NUM_BULK_URBS; ++i) {
1165 struct urb *urb = usb_alloc_urb(0);
1167 err("No free urbs available");
1170 urb->transfer_buffer = kmalloc (bluetooth->bulk_out_buffer_size, GFP_KERNEL);
1171 if (urb->transfer_buffer == NULL) {
1172 err("out of memory");
1175 bluetooth->write_urb_pool[i] = urb;
1178 endpoint = interrupt_in_endpoint[0];
1179 bluetooth->interrupt_in_urb = usb_alloc_urb(0);
1180 if (!bluetooth->interrupt_in_urb) {
1181 err("No free urbs available");
1184 bluetooth->interrupt_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1185 bluetooth->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1186 bluetooth->interrupt_in_interval = endpoint->bInterval;
1187 bluetooth->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1188 if (!bluetooth->interrupt_in_buffer) {
1189 err("Couldn't allocate interrupt_in_buffer");
1192 FILL_INT_URB(bluetooth->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1193 bluetooth->interrupt_in_buffer, buffer_size, bluetooth_int_callback,
1194 bluetooth, endpoint->bInterval);
1196 /* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */
1197 tty_register_devfs (&bluetooth_tty_driver, 0, minor);
1198 info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor, minor);
1200 bluetooth_table[minor] = bluetooth;
1202 return bluetooth; /* success */
1205 if (bluetooth->read_urb)
1206 usb_free_urb (bluetooth->read_urb);
1207 if (bluetooth->bulk_in_buffer)
1208 kfree (bluetooth->bulk_in_buffer);
1209 if (bluetooth->interrupt_in_urb)
1210 usb_free_urb (bluetooth->interrupt_in_urb);
1211 if (bluetooth->interrupt_in_buffer)
1212 kfree (bluetooth->interrupt_in_buffer);
1213 for (i = 0; i < NUM_BULK_URBS; ++i)
1214 if (bluetooth->write_urb_pool[i]) {
1215 if (bluetooth->write_urb_pool[i]->transfer_buffer)
1216 kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
1217 usb_free_urb (bluetooth->write_urb_pool[i]);
1219 for (i = 0; i < NUM_CONTROL_URBS; ++i)
1220 if (bluetooth->control_urb_pool[i]) {
1221 if (bluetooth->control_urb_pool[i]->transfer_buffer)
1222 kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1223 usb_free_urb (bluetooth->control_urb_pool[i]);
1226 bluetooth_table[minor] = NULL;
1228 /* free up any memory that we allocated */
1235 static void usb_bluetooth_disconnect(struct usb_device *dev, void *ptr)
1237 struct usb_bluetooth *bluetooth = (struct usb_bluetooth *) ptr;
1241 if ((bluetooth->open_count) && (bluetooth->tty))
1242 tty_hangup(bluetooth->tty);
1244 bluetooth->open_count = 0;
1246 if (bluetooth->read_urb) {
1247 usb_unlink_urb (bluetooth->read_urb);
1248 usb_free_urb (bluetooth->read_urb);
1250 if (bluetooth->bulk_in_buffer)
1251 kfree (bluetooth->bulk_in_buffer);
1253 if (bluetooth->interrupt_in_urb) {
1254 usb_unlink_urb (bluetooth->interrupt_in_urb);
1255 usb_free_urb (bluetooth->interrupt_in_urb);
1257 if (bluetooth->interrupt_in_buffer)
1258 kfree (bluetooth->interrupt_in_buffer);
1260 tty_unregister_devfs (&bluetooth_tty_driver, bluetooth->minor);
1262 for (i = 0; i < NUM_BULK_URBS; ++i) {
1263 if (bluetooth->write_urb_pool[i]) {
1264 usb_unlink_urb (bluetooth->write_urb_pool[i]);
1265 if (bluetooth->write_urb_pool[i]->transfer_buffer)
1266 kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
1267 usb_free_urb (bluetooth->write_urb_pool[i]);
1270 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1271 if (bluetooth->control_urb_pool[i]) {
1272 usb_unlink_urb (bluetooth->control_urb_pool[i]);
1273 if (bluetooth->control_urb_pool[i]->transfer_buffer)
1274 kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1275 usb_free_urb (bluetooth->control_urb_pool[i]);
1279 info("Bluetooth converter now disconnected from ttyUB%d", bluetooth->minor);
1281 bluetooth_table[bluetooth->minor] = NULL;
1283 /* free up any memory that we allocated */
1287 info("device disconnected");
1294 static struct tty_driver bluetooth_tty_driver = {
1295 .magic = TTY_DRIVER_MAGIC,
1296 .driver_name = "usb-bluetooth",
1297 .name = "usb/ttub/%d",
1298 .major = BLUETOOTH_TTY_MAJOR,
1300 .num = BLUETOOTH_TTY_MINORS,
1301 .type = TTY_DRIVER_TYPE_SERIAL,
1302 .subtype = SERIAL_TYPE_NORMAL,
1303 .flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1305 .refcount = &bluetooth_refcount,
1306 .table = bluetooth_tty,
1307 .termios = bluetooth_termios,
1308 .termios_locked = bluetooth_termios_locked,
1310 .open = bluetooth_open,
1311 .close = bluetooth_close,
1312 .write = bluetooth_write,
1313 .write_room = bluetooth_write_room,
1314 .ioctl = bluetooth_ioctl,
1315 .set_termios = bluetooth_set_termios,
1316 .throttle = bluetooth_throttle,
1317 .unthrottle = bluetooth_unthrottle,
1318 .chars_in_buffer = bluetooth_chars_in_buffer,
1322 int usb_bluetooth_init(void)
1327 /* Initalize our global data */
1328 for (i = 0; i < BLUETOOTH_TTY_MINORS; ++i) {
1329 bluetooth_table[i] = NULL;
1332 info ("USB Bluetooth support registered");
1334 /* register the tty driver */
1335 bluetooth_tty_driver.init_termios = tty_std_termios;
1336 bluetooth_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1337 if (tty_register_driver (&bluetooth_tty_driver)) {
1338 err("%s - failed to register tty driver", __FUNCTION__);
1342 /* register the USB driver */
1343 result = usb_register(&usb_bluetooth_driver);
1345 tty_unregister_driver(&bluetooth_tty_driver);
1346 err("usb_register failed for the USB bluetooth driver. Error number %d", result);
1350 info(DRIVER_DESC " " DRIVER_VERSION);
1356 void usb_bluetooth_exit(void)
1358 usb_deregister(&usb_bluetooth_driver);
1359 tty_unregister_driver(&bluetooth_tty_driver);
1363 module_init(usb_bluetooth_init);
1364 module_exit(usb_bluetooth_exit);
1366 /* Module information */
1367 MODULE_AUTHOR( DRIVER_AUTHOR );
1368 MODULE_DESCRIPTION( DRIVER_DESC );
1369 MODULE_LICENSE("GPL");