2 * bluetty.c Version 0.13
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 corresponding 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 __u8 bulk_out_endpointAddress;
195 wait_queue_head_t write_wait;
197 struct work_struct work; /* work queue entry for line discipline waking up */
199 unsigned int int_packet_pos;
200 unsigned char int_buffer[EVENT_BUFFER_SIZE];
201 unsigned int bulk_packet_pos;
202 unsigned char bulk_buffer[ACL_BUFFER_SIZE]; /* 64k preallocated, fix? */
203 struct semaphore lock;
207 /* local function prototypes */
208 static int bluetooth_open (struct tty_struct *tty, struct file *filp);
209 static void bluetooth_close (struct tty_struct *tty, struct file *filp);
210 static int bluetooth_write (struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
211 static int bluetooth_write_room (struct tty_struct *tty);
212 static int bluetooth_chars_in_buffer (struct tty_struct *tty);
213 static void bluetooth_throttle (struct tty_struct *tty);
214 static void bluetooth_unthrottle (struct tty_struct *tty);
215 static int bluetooth_ioctl (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
216 static void bluetooth_set_termios (struct tty_struct *tty, struct termios *old);
218 static void bluetooth_int_callback (struct urb *urb, struct pt_regs *regs);
219 static void bluetooth_ctrl_callback (struct urb *urb, struct pt_regs *regs);
220 static void bluetooth_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
221 static void bluetooth_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
223 static int usb_bluetooth_probe (struct usb_interface *intf,
224 const struct usb_device_id *id);
225 static void usb_bluetooth_disconnect (struct usb_interface *intf);
228 static struct usb_device_id usb_bluetooth_ids [] = {
229 { USB_DEVICE_INFO(WIRELESS_CLASS_CODE, RF_SUBCLASS_CODE, BLUETOOTH_PROGRAMMING_PROTOCOL_CODE) },
230 { } /* Terminating entry */
233 MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
235 static struct usb_driver usb_bluetooth_driver = {
236 .owner = THIS_MODULE,
238 .probe = usb_bluetooth_probe,
239 .disconnect = usb_bluetooth_disconnect,
240 .id_table = usb_bluetooth_ids,
243 static struct tty_driver *bluetooth_tty_driver;
244 static struct usb_bluetooth *bluetooth_table[BLUETOOTH_TTY_MINORS];
247 static inline int bluetooth_paranoia_check (struct usb_bluetooth *bluetooth, const char *function)
250 dbg("%s - bluetooth == NULL", function);
253 if (bluetooth->magic != USB_BLUETOOTH_MAGIC) {
254 dbg("%s - bad magic number for bluetooth", function);
262 static inline struct usb_bluetooth* get_usb_bluetooth (struct usb_bluetooth *bluetooth, const char *function)
265 bluetooth_paranoia_check (bluetooth, function)) {
266 /* then say that we don't have a valid usb_bluetooth thing, which will
267 * end up generating -ENODEV return values */
275 static inline struct usb_bluetooth *get_bluetooth_by_index (int index)
277 return bluetooth_table[index];
281 static int bluetooth_ctrl_msg (struct usb_bluetooth *bluetooth, int request, int value, const unsigned char *buf, int len)
283 struct urb *urb = NULL;
284 struct usb_ctrlrequest *dr = NULL;
288 dbg ("%s", __FUNCTION__);
290 /* try to find a free urb in our list */
291 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
292 if (bluetooth->control_urb_pool[i]->status != -EINPROGRESS) {
293 urb = bluetooth->control_urb_pool[i];
294 dr = &bluetooth->dr[i];
299 dbg ("%s - no free urbs", __FUNCTION__);
303 /* keep increasing the urb transfer buffer to fit the size of the message */
304 if (urb->transfer_buffer == NULL) {
305 urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
306 if (urb->transfer_buffer == NULL) {
307 err ("%s - out of memory", __FUNCTION__);
311 if (urb->transfer_buffer_length < len) {
312 kfree (urb->transfer_buffer);
313 urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
314 if (urb->transfer_buffer == NULL) {
315 err ("%s - out of memory", __FUNCTION__);
319 memcpy (urb->transfer_buffer, buf, len);
321 dr->bRequestType= BLUETOOTH_CONTROL_REQUEST_TYPE;
322 dr->bRequest = request;
323 dr->wValue = cpu_to_le16((u16) value);
324 dr->wIndex = cpu_to_le16((u16) bluetooth->control_out_bInterfaceNum);
325 dr->wLength = cpu_to_le16((u16) len);
327 usb_fill_control_urb (urb, bluetooth->dev, usb_sndctrlpipe(bluetooth->dev, 0),
328 (unsigned char*)dr, urb->transfer_buffer, len, bluetooth_ctrl_callback, bluetooth);
330 /* send it down the pipe */
331 status = usb_submit_urb(urb, GFP_KERNEL);
333 dbg("%s - usb_submit_urb(control) failed with status = %d", __FUNCTION__, status);
342 /*****************************************************************************
343 * Driver tty interface functions
344 *****************************************************************************/
345 static int bluetooth_open (struct tty_struct *tty, struct file * filp)
347 struct usb_bluetooth *bluetooth;
350 dbg("%s", __FUNCTION__);
352 /* initialize the pointer incase something fails */
353 tty->driver_data = NULL;
355 /* get the bluetooth object associated with this tty pointer */
356 bluetooth = get_bluetooth_by_index (tty->index);
358 if (bluetooth_paranoia_check (bluetooth, __FUNCTION__)) {
362 down (&bluetooth->lock);
364 ++bluetooth->open_count;
365 if (bluetooth->open_count == 1) {
366 /* set up our structure making the tty driver remember our object, and us it */
367 tty->driver_data = bluetooth;
368 bluetooth->tty = tty;
370 /* force low_latency on so that our tty_push actually forces the data through,
371 * otherwise it is scheduled, and with high data rates (like with OHCI) data
373 bluetooth->tty->low_latency = 1;
375 /* Reset the packet position counters */
376 bluetooth->int_packet_pos = 0;
377 bluetooth->bulk_packet_pos = 0;
379 #ifndef BTBUGGYHARDWARE
380 /* Start reading from the device */
381 usb_fill_bulk_urb (bluetooth->read_urb, bluetooth->dev,
382 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
383 bluetooth->bulk_in_buffer,
384 bluetooth->bulk_in_buffer_size,
385 bluetooth_read_bulk_callback, bluetooth);
386 result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL);
388 dbg("%s - usb_submit_urb(read bulk) failed with status %d", __FUNCTION__, result);
390 usb_fill_int_urb (bluetooth->interrupt_in_urb, bluetooth->dev,
391 usb_rcvintpipe(bluetooth->dev, bluetooth->interrupt_in_endpointAddress),
392 bluetooth->interrupt_in_buffer,
393 bluetooth->interrupt_in_buffer_size,
394 bluetooth_int_callback, bluetooth,
395 bluetooth->interrupt_in_interval);
396 result = usb_submit_urb(bluetooth->interrupt_in_urb, GFP_KERNEL);
398 dbg("%s - usb_submit_urb(interrupt in) failed with status %d", __FUNCTION__, result);
401 up(&bluetooth->lock);
407 static void bluetooth_close (struct tty_struct *tty, struct file * filp)
409 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
415 dbg("%s", __FUNCTION__);
417 if (!bluetooth->open_count) {
418 dbg ("%s - device not opened", __FUNCTION__);
422 down (&bluetooth->lock);
424 --bluetooth->open_count;
425 if (bluetooth->open_count <= 0) {
426 bluetooth->open_count = 0;
428 /* shutdown any in-flight urbs that we know about */
429 usb_unlink_urb (bluetooth->read_urb);
430 usb_unlink_urb (bluetooth->interrupt_in_urb);
432 up(&bluetooth->lock);
436 static int bluetooth_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
438 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
439 struct urb *urb = NULL;
440 unsigned char *temp_buffer = NULL;
441 const unsigned char *current_buffer;
442 unsigned char *urb_buffer;
450 dbg("%s - %d byte(s)", __FUNCTION__, count);
452 if (!bluetooth->open_count) {
453 dbg ("%s - device not opened", __FUNCTION__);
458 dbg("%s - write request of 0 bytes", __FUNCTION__);
462 dbg("%s - write request only included type %d", __FUNCTION__, buf[0]);
467 printk (KERN_DEBUG __FILE__ ": %s - length = %d, data = ", __FUNCTION__, count);
468 for (i = 0; i < count; ++i) {
469 printk ("%.2x ", buf[i]);
475 temp_buffer = kmalloc (count, GFP_KERNEL);
476 if (temp_buffer == NULL) {
477 err ("%s - out of memory.", __FUNCTION__);
481 if (copy_from_user (temp_buffer, (void __user *)buf, count)) {
485 current_buffer = temp_buffer;
487 current_buffer = buf;
490 switch (*current_buffer) {
491 /* First byte indicates the type of packet */
493 /* dbg("%s- Send cmd_pkt len:%d", __FUNCTION__, count);*/
495 retval = bluetooth_ctrl_msg (bluetooth, 0x00, 0x00, ¤t_buffer[1], count-1);
506 urb_buffer = kmalloc (count, GFP_ATOMIC);
508 dev_err(&bluetooth->dev->dev, "out of memory\n");
513 urb = usb_alloc_urb(0, GFP_ATOMIC);
515 dev_err(&bluetooth->dev->dev, "no more free urbs\n");
520 memcpy (urb_buffer, current_buffer, count);
522 /* build up our urb */
523 usb_fill_bulk_urb(urb, bluetooth->dev,
524 usb_sndbulkpipe(bluetooth->dev,
525 bluetooth->bulk_out_endpointAddress),
528 bluetooth_write_bulk_callback,
532 /* send it down the pipe */
533 retval = usb_submit_urb(urb, GFP_KERNEL);
535 dbg("%s - usb_submit_urb(write bulk) failed with error = %d", __FUNCTION__, retval);
539 /* we are done with this urb, so let the host driver
540 * really free it when it is finished with it */
546 dbg("%s - unsupported (at this time) write type", __FUNCTION__);
558 static int bluetooth_write_room (struct tty_struct *tty)
560 dbg("%s", __FUNCTION__);
563 * We really can take anything the user throws at us
564 * but let's pick a nice big number to tell the tty
565 * layer that we have lots of free space
571 static int bluetooth_chars_in_buffer (struct tty_struct *tty)
573 dbg("%s", __FUNCTION__);
576 * We can't really account for how much data we
577 * have sent out, but hasn't made it through to the
578 * device, so just tell the tty layer that everything
585 static void bluetooth_throttle (struct tty_struct * tty)
587 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
593 dbg("%s", __FUNCTION__);
595 if (!bluetooth->open_count) {
596 dbg ("%s - device not open", __FUNCTION__);
600 dbg("%s unsupported (at this time)", __FUNCTION__);
606 static void bluetooth_unthrottle (struct tty_struct * tty)
608 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
614 dbg("%s", __FUNCTION__);
616 if (!bluetooth->open_count) {
617 dbg ("%s - device not open", __FUNCTION__);
621 dbg("%s unsupported (at this time)", __FUNCTION__);
625 static int bluetooth_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
627 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
633 dbg("%s - cmd 0x%.4x", __FUNCTION__, cmd);
635 if (!bluetooth->open_count) {
636 dbg ("%s - device not open", __FUNCTION__);
645 static void bluetooth_set_termios (struct tty_struct *tty, struct termios * old)
647 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
653 dbg("%s", __FUNCTION__);
655 if (!bluetooth->open_count) {
656 dbg ("%s - device not open", __FUNCTION__);
666 #ifdef BTBUGGYHARDWARE
667 void btusb_enable_bulk_read(struct tty_struct *tty){
668 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
675 dbg("%s", __FUNCTION__);
677 if (!bluetooth->open_count) {
678 dbg ("%s - device not open", __FUNCTION__);
682 if (bluetooth->read_urb) {
683 usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev,
684 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
685 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
686 bluetooth_read_bulk_callback, bluetooth);
687 result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL);
689 err ("%s - failed submitting read urb, error %d", __FUNCTION__, result);
693 void btusb_disable_bulk_read(struct tty_struct *tty){
694 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
700 dbg("%s", __FUNCTION__);
702 if (!bluetooth->open_count) {
703 dbg ("%s - device not open", __FUNCTION__);
707 if ((bluetooth->read_urb) && (bluetooth->read_urb->actual_length))
708 usb_unlink_urb(bluetooth->read_urb);
713 /*****************************************************************************
714 * urb callback functions
715 *****************************************************************************/
718 static void bluetooth_int_callback (struct urb *urb, struct pt_regs *regs)
720 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
721 unsigned char *data = urb->transfer_buffer;
723 unsigned int count = urb->actual_length;
724 unsigned int packet_size;
727 dbg("%s", __FUNCTION__);
730 dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
734 switch (urb->status) {
741 /* this urb is terminated, clean up */
742 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
745 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
750 dbg("%s - zero length int", __FUNCTION__);
757 printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count);
758 for (i = 0; i < count; ++i) {
759 printk ("%.2x ", data[i]);
765 #ifdef BTBUGGYHARDWARE
766 if ((count >= 2) && (data[0] == 0xFF) && (data[1] == 0x00)) {
771 urb->actual_length = 0;
775 /* We add a packet type identifier to the beginning of each
776 HCI frame. This makes the data in the tty look like a
777 serial USB devices. Each HCI frame can be broken across
778 multiple URBs so we buffer them until we have a full hci
781 if (!bluetooth->int_packet_pos) {
782 bluetooth->int_buffer[0] = EVENT_PKT;
783 bluetooth->int_packet_pos++;
786 if (bluetooth->int_packet_pos + count > EVENT_BUFFER_SIZE) {
787 err("%s - exceeded EVENT_BUFFER_SIZE", __FUNCTION__);
788 bluetooth->int_packet_pos = 0;
792 memcpy (&bluetooth->int_buffer[bluetooth->int_packet_pos],
793 urb->transfer_buffer, count);
794 bluetooth->int_packet_pos += count;
795 urb->actual_length = 0;
797 if (bluetooth->int_packet_pos >= EVENT_HDR_SIZE)
798 packet_size = bluetooth->int_buffer[2];
802 if (packet_size + EVENT_HDR_SIZE < bluetooth->int_packet_pos) {
803 err("%s - packet was too long", __FUNCTION__);
804 bluetooth->int_packet_pos = 0;
808 if (packet_size + EVENT_HDR_SIZE == bluetooth->int_packet_pos) {
809 for (i = 0; i < bluetooth->int_packet_pos; ++i) {
810 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them */
811 if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
812 tty_flip_buffer_push(bluetooth->tty);
814 tty_insert_flip_char(bluetooth->tty, bluetooth->int_buffer[i], 0);
816 tty_flip_buffer_push(bluetooth->tty);
818 bluetooth->int_packet_pos = 0;
822 status = usb_submit_urb (urb, GFP_ATOMIC);
824 err ("%s - usb_submit_urb failed with result %d",
825 __FUNCTION__, status);
829 static void bluetooth_ctrl_callback (struct urb *urb, struct pt_regs *regs)
831 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
833 dbg("%s", __FUNCTION__);
836 dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
841 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
847 static void bluetooth_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
849 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
850 unsigned char *data = urb->transfer_buffer;
851 unsigned int count = urb->actual_length;
853 unsigned int packet_size;
857 dbg("%s", __FUNCTION__);
860 dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
865 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
866 if (urb->status == -ENOENT) {
867 dbg("%s - URB canceled, won't reschedule", __FUNCTION__);
874 dbg("%s - zero length read bulk", __FUNCTION__);
880 printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count);
881 for (i = 0; i < count; ++i) {
882 printk ("%.2x ", data[i]);
887 #ifdef BTBUGGYHARDWARE
888 if ((count == 4) && (data[0] == 0x00) && (data[1] == 0x00)
889 && (data[2] == 0x00) && (data[3] == 0x00)) {
890 urb->actual_length = 0;
891 usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev,
892 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
893 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
894 bluetooth_read_bulk_callback, bluetooth);
895 result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL);
897 err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
902 /* We add a packet type identifier to the beginning of each
903 HCI frame. This makes the data in the tty look like a
904 serial USB devices. Each HCI frame can be broken across
905 multiple URBs so we buffer them until we have a full hci
908 if (!bluetooth->bulk_packet_pos) {
909 bluetooth->bulk_buffer[0] = ACL_PKT;
910 bluetooth->bulk_packet_pos++;
913 if (bluetooth->bulk_packet_pos + count > ACL_BUFFER_SIZE) {
914 err("%s - exceeded ACL_BUFFER_SIZE", __FUNCTION__);
915 bluetooth->bulk_packet_pos = 0;
919 memcpy (&bluetooth->bulk_buffer[bluetooth->bulk_packet_pos],
920 urb->transfer_buffer, count);
921 bluetooth->bulk_packet_pos += count;
922 urb->actual_length = 0;
924 if (bluetooth->bulk_packet_pos >= ACL_HDR_SIZE) {
925 packet_size = CHAR2INT16(bluetooth->bulk_buffer[4],bluetooth->bulk_buffer[3]);
930 if (packet_size + ACL_HDR_SIZE < bluetooth->bulk_packet_pos) {
931 err("%s - packet was too long", __FUNCTION__);
932 bluetooth->bulk_packet_pos = 0;
936 if (packet_size + ACL_HDR_SIZE == bluetooth->bulk_packet_pos) {
937 for (i = 0; i < bluetooth->bulk_packet_pos; ++i) {
938 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
939 if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
940 tty_flip_buffer_push(bluetooth->tty);
942 tty_insert_flip_char(bluetooth->tty, bluetooth->bulk_buffer[i], 0);
944 tty_flip_buffer_push(bluetooth->tty);
945 bluetooth->bulk_packet_pos = 0;
949 if (!bluetooth || !bluetooth->open_count)
952 usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev,
953 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
954 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
955 bluetooth_read_bulk_callback, bluetooth);
956 result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL);
958 err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
964 static void bluetooth_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
966 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
968 dbg("%s", __FUNCTION__);
970 /* free up the transfer buffer, as usb_free_urb() does not do this */
971 kfree(urb->transfer_buffer);
974 dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
979 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
983 /* wake up our little function to let the tty layer know that something happened */
984 schedule_work(&bluetooth->work);
988 static void bluetooth_softint(void *private)
990 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)private, __FUNCTION__);
991 struct tty_struct *tty;
993 dbg("%s", __FUNCTION__);
999 tty = bluetooth->tty;
1000 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
1001 dbg("%s - write wakeup call.", __FUNCTION__);
1002 (tty->ldisc.write_wakeup)(tty);
1005 wake_up_interruptible(&tty->write_wait);
1009 static int usb_bluetooth_probe (struct usb_interface *intf,
1010 const struct usb_device_id *id)
1012 struct usb_device *dev = interface_to_usbdev (intf);
1013 struct usb_bluetooth *bluetooth = NULL;
1014 struct usb_host_interface *interface;
1015 struct usb_endpoint_descriptor *endpoint;
1016 struct usb_endpoint_descriptor *interrupt_in_endpoint[8];
1017 struct usb_endpoint_descriptor *bulk_in_endpoint[8];
1018 struct usb_endpoint_descriptor *bulk_out_endpoint[8];
1019 int control_out_endpoint;
1024 int num_interrupt_in = 0;
1025 int num_bulk_in = 0;
1026 int num_bulk_out = 0;
1028 interface = intf->cur_altsetting;
1029 control_out_endpoint = interface->desc.bInterfaceNumber;
1031 /* find the endpoints that we need */
1032 for (i = 0; i < interface->desc.bNumEndpoints; ++i) {
1033 endpoint = &interface->endpoint[i].desc;
1035 if ((endpoint->bEndpointAddress & 0x80) &&
1036 ((endpoint->bmAttributes & 3) == 0x02)) {
1037 /* we found a bulk in endpoint */
1038 dbg("found bulk in");
1039 bulk_in_endpoint[num_bulk_in] = endpoint;
1043 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1044 ((endpoint->bmAttributes & 3) == 0x02)) {
1045 /* we found a bulk out endpoint */
1046 dbg("found bulk out");
1047 bulk_out_endpoint[num_bulk_out] = endpoint;
1051 if ((endpoint->bEndpointAddress & 0x80) &&
1052 ((endpoint->bmAttributes & 3) == 0x03)) {
1053 /* we found a interrupt in endpoint */
1054 dbg("found interrupt in");
1055 interrupt_in_endpoint[num_interrupt_in] = endpoint;
1060 /* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */
1061 if ((num_bulk_in != 1) ||
1062 (num_bulk_out != 1) ||
1063 (num_interrupt_in != 1)) {
1064 dbg ("%s - improper number of endpoints. Bluetooth driver not bound.", __FUNCTION__);
1068 info("USB Bluetooth converter detected");
1070 for (minor = 0; minor < BLUETOOTH_TTY_MINORS && bluetooth_table[minor]; ++minor)
1072 if (bluetooth_table[minor]) {
1073 err("No more free Bluetooth devices");
1077 if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) {
1078 err("Out of memory");
1082 memset(bluetooth, 0, sizeof(struct usb_bluetooth));
1084 bluetooth->magic = USB_BLUETOOTH_MAGIC;
1085 bluetooth->dev = dev;
1086 bluetooth->minor = minor;
1087 INIT_WORK(&bluetooth->work, bluetooth_softint, bluetooth);
1088 init_MUTEX(&bluetooth->lock);
1090 /* record the interface number for the control out */
1091 bluetooth->control_out_bInterfaceNum = control_out_endpoint;
1093 /* create our control out urb pool */
1094 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1095 struct urb *urb = usb_alloc_urb(0, GFP_KERNEL);
1097 err("No free urbs available");
1100 urb->transfer_buffer = NULL;
1101 bluetooth->control_urb_pool[i] = urb;
1104 /* set up the endpoint information */
1105 endpoint = bulk_in_endpoint[0];
1106 bluetooth->read_urb = usb_alloc_urb (0, GFP_KERNEL);
1107 if (!bluetooth->read_urb) {
1108 err("No free urbs available");
1111 bluetooth->bulk_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1112 bluetooth->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1113 bluetooth->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1114 if (!bluetooth->bulk_in_buffer) {
1115 err("Couldn't allocate bulk_in_buffer");
1118 usb_fill_bulk_urb(bluetooth->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
1119 bluetooth->bulk_in_buffer, buffer_size, bluetooth_read_bulk_callback, bluetooth);
1121 endpoint = bulk_out_endpoint[0];
1122 bluetooth->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1123 bluetooth->bulk_out_buffer_size = endpoint->wMaxPacketSize * 2;
1125 endpoint = interrupt_in_endpoint[0];
1126 bluetooth->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1127 if (!bluetooth->interrupt_in_urb) {
1128 err("No free urbs available");
1131 bluetooth->interrupt_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1132 bluetooth->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1133 bluetooth->interrupt_in_interval = endpoint->bInterval;
1134 bluetooth->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1135 if (!bluetooth->interrupt_in_buffer) {
1136 err("Couldn't allocate interrupt_in_buffer");
1139 usb_fill_int_urb(bluetooth->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1140 bluetooth->interrupt_in_buffer, buffer_size, bluetooth_int_callback,
1141 bluetooth, endpoint->bInterval);
1143 /* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */
1144 tty_register_device (bluetooth_tty_driver, minor, &intf->dev);
1145 info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor, minor);
1147 bluetooth_table[minor] = bluetooth;
1150 usb_set_intfdata (intf, bluetooth);
1154 if (bluetooth->read_urb)
1155 usb_free_urb (bluetooth->read_urb);
1156 if (bluetooth->bulk_in_buffer)
1157 kfree (bluetooth->bulk_in_buffer);
1158 if (bluetooth->interrupt_in_urb)
1159 usb_free_urb (bluetooth->interrupt_in_urb);
1160 if (bluetooth->interrupt_in_buffer)
1161 kfree (bluetooth->interrupt_in_buffer);
1162 for (i = 0; i < NUM_CONTROL_URBS; ++i)
1163 if (bluetooth->control_urb_pool[i]) {
1164 if (bluetooth->control_urb_pool[i]->transfer_buffer)
1165 kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1166 usb_free_urb (bluetooth->control_urb_pool[i]);
1169 bluetooth_table[minor] = NULL;
1171 /* free up any memory that we allocated */
1177 static void usb_bluetooth_disconnect(struct usb_interface *intf)
1179 struct usb_bluetooth *bluetooth = usb_get_intfdata (intf);
1182 usb_set_intfdata (intf, NULL);
1184 if ((bluetooth->open_count) && (bluetooth->tty))
1185 tty_hangup(bluetooth->tty);
1187 bluetooth->open_count = 0;
1189 if (bluetooth->read_urb) {
1190 usb_unlink_urb (bluetooth->read_urb);
1191 usb_free_urb (bluetooth->read_urb);
1193 if (bluetooth->bulk_in_buffer)
1194 kfree (bluetooth->bulk_in_buffer);
1196 if (bluetooth->interrupt_in_urb) {
1197 usb_unlink_urb (bluetooth->interrupt_in_urb);
1198 usb_free_urb (bluetooth->interrupt_in_urb);
1200 if (bluetooth->interrupt_in_buffer)
1201 kfree (bluetooth->interrupt_in_buffer);
1203 tty_unregister_device (bluetooth_tty_driver, bluetooth->minor);
1205 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1206 if (bluetooth->control_urb_pool[i]) {
1207 usb_unlink_urb (bluetooth->control_urb_pool[i]);
1208 if (bluetooth->control_urb_pool[i]->transfer_buffer)
1209 kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1210 usb_free_urb (bluetooth->control_urb_pool[i]);
1214 info("Bluetooth converter now disconnected from ttyUB%d", bluetooth->minor);
1216 bluetooth_table[bluetooth->minor] = NULL;
1218 /* free up any memory that we allocated */
1221 info("device disconnected");
1225 static struct tty_operations bluetooth_ops = {
1226 .open = bluetooth_open,
1227 .close = bluetooth_close,
1228 .write = bluetooth_write,
1229 .write_room = bluetooth_write_room,
1230 .ioctl = bluetooth_ioctl,
1231 .set_termios = bluetooth_set_termios,
1232 .throttle = bluetooth_throttle,
1233 .unthrottle = bluetooth_unthrottle,
1234 .chars_in_buffer = bluetooth_chars_in_buffer,
1237 static int usb_bluetooth_init(void)
1242 /* Initialize our global data */
1243 for (i = 0; i < BLUETOOTH_TTY_MINORS; ++i) {
1244 bluetooth_table[i] = NULL;
1247 info ("USB Bluetooth support registered");
1249 bluetooth_tty_driver = alloc_tty_driver(BLUETOOTH_TTY_MINORS);
1250 if (!bluetooth_tty_driver)
1253 bluetooth_tty_driver->owner = THIS_MODULE;
1254 bluetooth_tty_driver->driver_name = "usb-bluetooth";
1255 bluetooth_tty_driver->name = "ttyUB";
1256 bluetooth_tty_driver->devfs_name = "usb/ttub/";
1257 bluetooth_tty_driver->major = BLUETOOTH_TTY_MAJOR;
1258 bluetooth_tty_driver->minor_start = 0;
1259 bluetooth_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1260 bluetooth_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1261 bluetooth_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1262 bluetooth_tty_driver->init_termios = tty_std_termios;
1263 bluetooth_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1264 tty_set_operations(bluetooth_tty_driver, &bluetooth_ops);
1265 if (tty_register_driver (bluetooth_tty_driver)) {
1266 err("%s - failed to register tty driver", __FUNCTION__);
1267 put_tty_driver(bluetooth_tty_driver);
1271 /* register the USB driver */
1272 result = usb_register(&usb_bluetooth_driver);
1274 tty_unregister_driver(bluetooth_tty_driver);
1275 put_tty_driver(bluetooth_tty_driver);
1276 err("usb_register failed for the USB bluetooth driver. Error number %d", result);
1280 info(DRIVER_DESC " " DRIVER_VERSION);
1286 static void usb_bluetooth_exit(void)
1288 usb_deregister(&usb_bluetooth_driver);
1289 tty_unregister_driver(bluetooth_tty_driver);
1290 put_tty_driver(bluetooth_tty_driver);
1294 module_init(usb_bluetooth_init);
1295 module_exit(usb_bluetooth_exit);
1297 /* Module information */
1298 MODULE_AUTHOR( DRIVER_AUTHOR );
1299 MODULE_DESCRIPTION( DRIVER_DESC );
1300 MODULE_LICENSE("GPL");