2 * bluetooth.c Version 0.7
4 * Copyright (c) 2000 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (c) 2000 Mark Douglas Corner <mcorner@umich.edu>
7 * USB Bluetooth driver, based on the Bluetooth Spec version 1.0B
9 * (11/29/2000) Version 0.7 gkh
10 * Fixed problem with overrunning the tty flip buffer.
11 * Removed unneeded NULL pointer initialization.
13 * (10/05/2000) Version 0.6 gkh
14 * Fixed bug with urb->dev not being set properly, now that the usb
16 * Got a real major id number and name.
18 * (08/06/2000) Version 0.5 gkh
19 * Fixed problem of not resubmitting the bulk read urb if there is
20 * an error in the callback. Ericsson devices seem to need this.
22 * (07/11/2000) Version 0.4 gkh
23 * Fixed bug in disconnect for when we call tty_hangup
24 * Fixed bug in bluetooth_ctrl_msg where the bluetooth struct was not
25 * getting attached to the control urb properly.
26 * Fixed bug in bluetooth_write where we pay attention to the result
27 * of bluetooth_ctrl_msg.
29 * (08/03/2000) Version 0.3 gkh mdc
30 * Merged in Mark's changes to make the driver play nice with the Axis
32 * Made the write bulk use an urb pool to enable larger transfers with
33 * fewer calls to the driver.
34 * Fixed off by one bug in acl pkt receive
35 * Made packet counters specific to each bluetooth device
36 * Added checks for zero length callbacks
37 * Added buffers for int and bulk packets. Had to do this otherwise
38 * packet types could intermingle.
39 * Made a control urb pool for the control messages.
41 * (07/11/2000) Version 0.2 gkh
42 * Fixed a small bug found by Nils Faerber in the usb_bluetooth_probe
45 * (07/09/2000) Version 0.1 gkh
46 * Initial release. Has support for sending ACL data (which is really just
47 * a HCI frame.) Raw HCI commands and HCI events are not supported.
48 * A ioctl will probably be needed for the HCI commands and events in the
49 * future. All isoch endpoints are ignored at this time also.
50 * This driver should work for all currently shipping USB Bluetooth
51 * devices at this time :)
56 * This program is free software; you can redistribute it and/or modify
57 * it under the terms of the GNU General Public License as published by
58 * the Free Software Foundation; either version 2 of the License, or
59 * (at your option) any later version.
61 * This program is distributed in the hope that it will be useful,
62 * but WITHOUT ANY WARRANTY; without even the implied warranty of
63 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 * GNU General Public License for more details.
66 * You should have received a copy of the GNU General Public License
67 * along with this program; if not, write to the Free Software
68 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
72 #include <linux/kernel.h>
73 #include <linux/sched.h>
74 #include <linux/signal.h>
75 #include <linux/errno.h>
76 #include <linux/poll.h>
77 #include <linux/init.h>
78 #include <linux/malloc.h>
79 #include <linux/fcntl.h>
80 #include <linux/tty_driver.h>
81 #include <linux/tty_flip.h>
82 #include <linux/tty.h>
83 #include <linux/module.h>
86 #include <linux/usb.h>
88 /* Module information */
89 MODULE_AUTHOR("Greg Kroah-Hartman, Mark Douglas Corner");
90 MODULE_DESCRIPTION("USB Bluetooth driver");
92 /* define this if you have hardware that is not good */
93 /*#define BTBUGGYHARDWARE */
95 /* Class, SubClass, and Protocol codes that describe a Bluetooth device */
96 #define WIRELESS_CLASS_CODE 0xe0
97 #define RF_SUBCLASS_CODE 0x01
98 #define BLUETOOTH_PROGRAMMING_PROTOCOL_CODE 0x01
101 #define BLUETOOTH_TTY_MAJOR 216 /* real device node major id */
102 #define BLUETOOTH_TTY_MINORS 256 /* whole lotta bluetooth devices */
104 #define USB_BLUETOOTH_MAGIC 0x6d02 /* magic number for bluetooth struct */
106 #define BLUETOOTH_CONTROL_REQUEST_TYPE 0x20
108 /* Bluetooth packet types */
112 #define EVENT_PKT 0x04
113 #define ERROR_PKT 0x05
117 #define MAX_EVENT_SIZE 0xFF
118 #define EVENT_HDR_SIZE 3 /* 2 for the header + 1 for the type indicator */
119 #define EVENT_BUFFER_SIZE (MAX_EVENT_SIZE + EVENT_HDR_SIZE)
121 #define MAX_ACL_SIZE 0xFFFF
122 #define ACL_HDR_SIZE 5 /* 4 for the header + 1 for the type indicator */
123 #define ACL_BUFFER_SIZE (MAX_ACL_SIZE + ACL_HDR_SIZE)
125 /* parity check flag */
126 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
128 #define CHAR2INT16(c1,c0) (((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
129 #define MIN(a,b) (((a)<(b))?(a):(b))
131 #define NUM_BULK_URBS 24
132 #define NUM_CONTROL_URBS 16
134 struct usb_bluetooth
{
136 struct usb_device
* dev
;
137 struct tty_driver
* tty_driver
; /* the tty_driver for this device */
138 struct tty_struct
* tty
; /* the coresponding tty for this port */
140 unsigned char minor
; /* the starting minor number for this device */
141 char active
; /* someone has this device open */
142 int throttle
; /* throttled by tty layer */
144 __u8 control_out_bInterfaceNum
;
145 struct urb
* control_urb_pool
[NUM_CONTROL_URBS
];
146 devrequest dr
[NUM_CONTROL_URBS
];
148 unsigned char * interrupt_in_buffer
;
149 struct urb
* interrupt_in_urb
;
150 __u8 interrupt_in_endpointAddress
;
151 __u8 interrupt_in_interval
;
152 int interrupt_in_buffer_size
;
154 unsigned char * bulk_in_buffer
;
155 struct urb
* read_urb
;
156 __u8 bulk_in_endpointAddress
;
157 int bulk_in_buffer_size
;
159 int bulk_out_buffer_size
;
160 struct urb
* write_urb_pool
[NUM_BULK_URBS
];
161 __u8 bulk_out_endpointAddress
;
163 wait_queue_head_t write_wait
;
165 struct tq_struct tqueue
; /* task queue for line discipline waking up */
167 unsigned int int_packet_pos
;
168 unsigned char int_buffer
[EVENT_BUFFER_SIZE
];
169 unsigned int bulk_packet_pos
;
170 unsigned char bulk_buffer
[ACL_BUFFER_SIZE
]; /* 64k preallocated, fix? */
174 /* local function prototypes */
175 static int bluetooth_open (struct tty_struct
*tty
, struct file
*filp
);
176 static void bluetooth_close (struct tty_struct
*tty
, struct file
*filp
);
177 static int bluetooth_write (struct tty_struct
*tty
, int from_user
, const unsigned char *buf
, int count
);
178 static int bluetooth_write_room (struct tty_struct
*tty
);
179 static int bluetooth_chars_in_buffer (struct tty_struct
*tty
);
180 static void bluetooth_throttle (struct tty_struct
*tty
);
181 static void bluetooth_unthrottle (struct tty_struct
*tty
);
182 static int bluetooth_ioctl (struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
);
183 static void bluetooth_set_termios (struct tty_struct
*tty
, struct termios
*old
);
185 static void bluetooth_int_callback (struct urb
*urb
);
186 static void bluetooth_ctrl_callback (struct urb
*urb
);
187 static void bluetooth_read_bulk_callback (struct urb
*urb
);
188 static void bluetooth_write_bulk_callback (struct urb
*urb
);
190 static void * usb_bluetooth_probe(struct usb_device
*dev
, unsigned int ifnum
,
191 const struct usb_device_id
*id
);
192 static void usb_bluetooth_disconnect (struct usb_device
*dev
, void *ptr
);
195 static struct usb_device_id usb_bluetooth_ids
[] = {
197 bDeviceClass
: WIRELESS_CLASS_CODE
,
198 bDeviceSubClass
: RF_SUBCLASS_CODE
,
199 bDeviceProtocol
: BLUETOOTH_PROGRAMMING_PROTOCOL_CODE
201 { } /* Terminating entry */
204 MODULE_DEVICE_TABLE (usb
, usb_bluetooth_ids
);
206 static struct usb_driver usb_bluetooth_driver
= {
208 probe
: usb_bluetooth_probe
,
209 disconnect
: usb_bluetooth_disconnect
,
210 id_table
: usb_bluetooth_ids
,
213 static int bluetooth_refcount
;
214 static struct tty_driver bluetooth_tty_driver
;
215 static struct tty_struct
* bluetooth_tty
[BLUETOOTH_TTY_MINORS
];
216 static struct termios
* bluetooth_termios
[BLUETOOTH_TTY_MINORS
];
217 static struct termios
* bluetooth_termios_locked
[BLUETOOTH_TTY_MINORS
];
218 static struct usb_bluetooth
*bluetooth_table
[BLUETOOTH_TTY_MINORS
];
221 static inline int bluetooth_paranoia_check (struct usb_bluetooth
*bluetooth
, const char *function
)
224 dbg("%s - bluetooth == NULL", function
);
227 if (bluetooth
->magic
!= USB_BLUETOOTH_MAGIC
) {
228 dbg("%s - bad magic number for bluetooth", function
);
236 static inline struct usb_bluetooth
* get_usb_bluetooth (struct usb_bluetooth
*bluetooth
, const char *function
)
239 bluetooth_paranoia_check (bluetooth
, function
)) {
240 /* then say that we dont have a valid usb_bluetooth thing, which will
241 * end up generating -ENODEV return values */
249 static inline struct usb_bluetooth
*get_bluetooth_by_minor (int minor
)
251 return bluetooth_table
[minor
];
255 static int bluetooth_ctrl_msg (struct usb_bluetooth
*bluetooth
, int request
, int value
, void *buf
, int len
)
257 struct urb
*urb
= NULL
;
258 devrequest
*dr
= NULL
;
264 /* try to find a free urb in our list */
265 for (i
= 0; i
< NUM_CONTROL_URBS
; ++i
) {
266 if (bluetooth
->control_urb_pool
[i
]->status
!= -EINPROGRESS
) {
267 urb
= bluetooth
->control_urb_pool
[i
];
268 dr
= &bluetooth
->dr
[i
];
273 dbg (__FUNCTION__
" - no free urbs");
277 /* free up the last buffer that this urb used */
278 if (urb
->transfer_buffer
!= NULL
) {
279 kfree(urb
->transfer_buffer
);
280 urb
->transfer_buffer
= NULL
;
283 dr
->requesttype
= BLUETOOTH_CONTROL_REQUEST_TYPE
;
284 dr
->request
= request
;
285 dr
->value
= cpu_to_le16p(&value
);
286 dr
->index
= cpu_to_le16p(&bluetooth
->control_out_bInterfaceNum
);
287 dr
->length
= cpu_to_le16p(&len
);
289 FILL_CONTROL_URB (urb
, bluetooth
->dev
, usb_sndctrlpipe(bluetooth
->dev
, 0),
290 (unsigned char*)dr
, buf
, len
, bluetooth_ctrl_callback
, bluetooth
);
292 /* send it down the pipe */
293 status
= usb_submit_urb(urb
);
295 dbg(__FUNCTION__
" - usb_submit_urb(control) failed with status = %d", status
);
304 /*****************************************************************************
305 * Driver tty interface functions
306 *****************************************************************************/
307 static int bluetooth_open (struct tty_struct
*tty
, struct file
* filp
)
309 struct usb_bluetooth
*bluetooth
;
314 /* initialize the pointer incase something fails */
315 tty
->driver_data
= NULL
;
317 /* get the bluetooth object associated with this tty pointer */
318 bluetooth
= get_bluetooth_by_minor (MINOR(tty
->device
));
320 if (bluetooth_paranoia_check (bluetooth
, __FUNCTION__
)) {
324 if (bluetooth
->active
) {
325 dbg (__FUNCTION__
" - device already open");
329 /* set up our structure making the tty driver remember our object, and us it */
330 tty
->driver_data
= bluetooth
;
331 bluetooth
->tty
= tty
;
333 /* force low_latency on so that our tty_push actually forces the data through,
334 * otherwise it is scheduled, and with high data rates (like with OHCI) data
336 bluetooth
->tty
->low_latency
= 1;
338 bluetooth
->active
= 1;
340 /* Reset the packet position counters */
341 bluetooth
->int_packet_pos
= 0;
342 bluetooth
->bulk_packet_pos
= 0;
344 #ifndef BTBUGGYHARDWARE
345 /* Start reading from the device */
346 FILL_BULK_URB(bluetooth
->read_urb
, bluetooth
->dev
,
347 usb_rcvbulkpipe(bluetooth
->dev
, bluetooth
->bulk_in_endpointAddress
),
348 bluetooth
->bulk_in_buffer
, bluetooth
->bulk_in_buffer_size
,
349 bluetooth_read_bulk_callback
, bluetooth
);
350 result
= usb_submit_urb(bluetooth
->read_urb
);
352 dbg(__FUNCTION__
" - usb_submit_urb(read bulk) failed with status %d", result
);
354 FILL_INT_URB(bluetooth
->interrupt_in_urb
, bluetooth
->dev
,
355 usb_rcvintpipe(bluetooth
->dev
, bluetooth
->interrupt_in_endpointAddress
),
356 bluetooth
->interrupt_in_buffer
, bluetooth
->interrupt_in_buffer_size
,
357 bluetooth_int_callback
, bluetooth
, bluetooth
->interrupt_in_interval
);
358 result
= usb_submit_urb(bluetooth
->interrupt_in_urb
);
360 dbg(__FUNCTION__
" - usb_submit_urb(interrupt in) failed with status %d", result
);
366 static void bluetooth_close (struct tty_struct
*tty
, struct file
* filp
)
368 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)tty
->driver_data
, __FUNCTION__
);
377 if (!bluetooth
->active
) {
378 dbg (__FUNCTION__
" - device not opened");
382 /* shutdown any bulk reads and writes that might be going on */
383 for (i
= 0; i
< NUM_BULK_URBS
; ++i
)
384 usb_unlink_urb (bluetooth
->write_urb_pool
[i
]);
385 usb_unlink_urb (bluetooth
->read_urb
);
387 bluetooth
->active
= 0;
391 static int bluetooth_write (struct tty_struct
* tty
, int from_user
, const unsigned char *buf
, int count
)
393 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)tty
->driver_data
, __FUNCTION__
);
394 struct urb
*urb
= NULL
;
395 unsigned char *new_buffer
;
396 const unsigned char *current_position
;
406 dbg(__FUNCTION__
" - %d byte(s)", count
);
408 if (!bluetooth
->active
) {
409 dbg (__FUNCTION__
" - device not opened");
414 dbg(__FUNCTION__
" - write request of 0 bytes");
418 dbg(__FUNCTION__
" - write request only included type %d", buf
[0]);
423 printk (KERN_DEBUG __FILE__
": " __FUNCTION__
" - length = %d, data = ", count
);
424 for (i
= 0; i
< count
; ++i
) {
425 printk ("%.2x ", buf
[i
]);
431 /* First byte indicates the type of packet */
433 /* dbg(__FUNCTION__ "- Send cmd_pkt len:%d", count);*/
436 printk("cmd_pkt from interrupt!\n");
440 new_buffer
= kmalloc (count
-1, GFP_KERNEL
);
443 err (__FUNCTION__
"- out of memory.");
448 copy_from_user (new_buffer
, buf
+1, count
-1);
450 memcpy (new_buffer
, buf
+1, count
-1);
452 if (bluetooth_ctrl_msg (bluetooth
, 0x00, 0x00, new_buffer
, count
-1) != 0) {
457 /* need to free new_buffer somehow... FIXME */
461 current_position
= buf
;
469 /* try to find a free urb in our list */
470 for (i
= 0; i
< NUM_BULK_URBS
; ++i
) {
471 if (bluetooth
->write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
472 urb
= bluetooth
->write_urb_pool
[i
];
477 dbg (__FUNCTION__
" - no free urbs");
481 /* free up the last buffer that this urb used */
482 if (urb
->transfer_buffer
!= NULL
) {
483 kfree(urb
->transfer_buffer
);
484 urb
->transfer_buffer
= NULL
;
487 buffer_size
= MIN (count
, bluetooth
->bulk_out_buffer_size
);
489 new_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
490 if (new_buffer
== NULL
) {
491 err(__FUNCTION__
" no more kernel memory...");
496 copy_from_user(new_buffer
, current_position
, buffer_size
);
498 memcpy (new_buffer
, current_position
, buffer_size
);
500 /* build up our urb */
501 FILL_BULK_URB (urb
, bluetooth
->dev
, usb_sndbulkpipe(bluetooth
->dev
, bluetooth
->bulk_out_endpointAddress
),
502 new_buffer
, buffer_size
, bluetooth_write_bulk_callback
, bluetooth
);
503 urb
->transfer_flags
|= USB_QUEUE_BULK
;
505 /* send it down the pipe */
506 status
= usb_submit_urb(urb
);
508 dbg(__FUNCTION__
" - usb_submit_urb(write bulk) failed with status = %d", status
);
509 #ifdef BTBUGGYHARDWARE
510 /* A workaround for the stalled data bug */
511 /* May or may not be needed...*/
516 current_position
+= buffer_size
;
517 bytes_sent
+= buffer_size
;
518 count
-= buffer_size
;
521 return bytes_sent
+ 1;
524 dbg(__FUNCTION__
" - unsupported (at this time) write type");
531 static int bluetooth_write_room (struct tty_struct
*tty
)
533 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)tty
->driver_data
, __FUNCTION__
);
543 if (!bluetooth
->active
) {
544 dbg (__FUNCTION__
" - device not open");
548 for (i
= 0; i
< NUM_BULK_URBS
; ++i
) {
549 if (bluetooth
->write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
550 room
+= bluetooth
->bulk_out_buffer_size
;
554 dbg(__FUNCTION__
" - returns %d", room
);
559 static int bluetooth_chars_in_buffer (struct tty_struct
*tty
)
561 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)tty
->driver_data
, __FUNCTION__
);
569 if (!bluetooth
->active
) {
570 dbg (__FUNCTION__
" - device not open");
574 for (i
= 0; i
< NUM_BULK_URBS
; ++i
) {
575 if (bluetooth
->write_urb_pool
[i
]->status
== -EINPROGRESS
) {
576 chars
+= bluetooth
->write_urb_pool
[i
]->transfer_buffer_length
;
580 dbg (__FUNCTION__
" - returns %d", chars
);
585 static void bluetooth_throttle (struct tty_struct
* tty
)
587 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)tty
->driver_data
, __FUNCTION__
);
595 if (!bluetooth
->active
) {
596 dbg (__FUNCTION__
" - device not open");
600 dbg(__FUNCTION__
" unsupported (at this time)");
606 static void bluetooth_unthrottle (struct tty_struct
* tty
)
608 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)tty
->driver_data
, __FUNCTION__
);
616 if (!bluetooth
->active
) {
617 dbg (__FUNCTION__
" - device not open");
621 dbg(__FUNCTION__
" unsupported (at this time)");
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(__FUNCTION__
" - cmd 0x%.4x", cmd
);
635 if (!bluetooth
->active
) {
636 dbg (__FUNCTION__
" - device not open");
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__
);
655 if (!bluetooth
->active
) {
656 dbg (__FUNCTION__
" - device not open");
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__
);
677 if (!bluetooth
->active
) {
678 dbg (__FUNCTION__
" - device not open");
682 if (bluetooth
->read_urb
) {
683 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
);
689 err (__FUNCTION__
" - failed submitting read urb, error %d", 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__
);
702 if (!bluetooth
->active
) {
703 dbg (__FUNCTION__
" - device not open");
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
)
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
;
729 dbg(__FUNCTION__
" - bad bluetooth pointer, exiting");
734 dbg(__FUNCTION__
" - nonzero int status received: %d", urb
->status
);
739 dbg(__FUNCTION__
" - zero length int");
746 printk (KERN_DEBUG __FILE__
": " __FUNCTION__
"- length = %d, data = ", count
);
747 for (i
= 0; i
< count
; ++i
) {
748 printk ("%.2x ", data
[i
]);
754 #ifdef BTBUGGYHARDWARE
755 if ((count
>= 2) && (data
[0] == 0xFF) && (data
[1] == 0x00)) {
760 urb
->actual_length
= 0;
764 /* We add a packet type identifier to the beginning of each
765 HCI frame. This makes the data in the tty look like a
766 serial USB devices. Each HCI frame can be broken across
767 multiple URBs so we buffer them until we have a full hci
770 if (!bluetooth
->int_packet_pos
) {
771 bluetooth
->int_buffer
[0] = EVENT_PKT
;
772 bluetooth
->int_packet_pos
++;
775 if (bluetooth
->int_packet_pos
+ count
> EVENT_BUFFER_SIZE
) {
776 err(__FUNCTION__
" - exceeded EVENT_BUFFER_SIZE");
777 bluetooth
->int_packet_pos
= 0;
781 memcpy (&bluetooth
->int_buffer
[bluetooth
->int_packet_pos
],
782 urb
->transfer_buffer
, count
);
783 bluetooth
->int_packet_pos
+= count
;
784 urb
->actual_length
= 0;
786 if (bluetooth
->int_packet_pos
>= EVENT_HDR_SIZE
)
787 packet_size
= bluetooth
->int_buffer
[2];
791 if (packet_size
+ EVENT_HDR_SIZE
< bluetooth
->int_packet_pos
) {
792 err(__FUNCTION__
" - packet was too long");
793 bluetooth
->int_packet_pos
= 0;
797 if (packet_size
+ EVENT_HDR_SIZE
== bluetooth
->int_packet_pos
) {
798 for (i
= 0; i
< bluetooth
->int_packet_pos
; ++i
) {
799 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them */
800 if (bluetooth
->tty
->flip
.count
>= TTY_FLIPBUF_SIZE
) {
801 tty_flip_buffer_push(bluetooth
->tty
);
803 tty_insert_flip_char(bluetooth
->tty
, bluetooth
->int_buffer
[i
], 0);
805 tty_flip_buffer_push(bluetooth
->tty
);
807 bluetooth
->int_packet_pos
= 0;
812 static void bluetooth_ctrl_callback (struct urb
*urb
)
814 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)urb
->context
, __FUNCTION__
);
819 dbg(__FUNCTION__
" - bad bluetooth pointer, exiting");
824 dbg(__FUNCTION__
" - nonzero read bulk status received: %d", urb
->status
);
830 static void bluetooth_read_bulk_callback (struct urb
*urb
)
832 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)urb
->context
, __FUNCTION__
);
833 unsigned char *data
= urb
->transfer_buffer
;
834 unsigned int count
= urb
->actual_length
;
836 unsigned int packet_size
;
839 #ifdef BTBUGGYHARDWARE
840 if ((count
== 4) && (data
[0] == 0x00) && (data
[1] == 0x00)
841 && (data
[2] == 0x00) && (data
[3] == 0x00)) {
842 urb
->actual_length
= 0;
843 FILL_BULK_URB(bluetooth
->read_urb
, bluetooth
->dev
,
844 usb_rcvbulkpipe(bluetooth
->dev
, bluetooth
->bulk_in_endpointAddress
),
845 bluetooth
->bulk_in_buffer
, bluetooth
->bulk_in_buffer_size
,
846 bluetooth_read_bulk_callback
, bluetooth
);
847 result
= usb_submit_urb(bluetooth
->read_urb
);
849 err (__FUNCTION__
" - failed resubmitting read urb, error %d", result
);
858 dbg(__FUNCTION__
" - bad bluetooth pointer, exiting");
863 dbg(__FUNCTION__
" - nonzero read bulk status received: %d", urb
->status
);
868 dbg(__FUNCTION__
" - zero length read bulk");
874 printk (KERN_DEBUG __FILE__
": " __FUNCTION__
"- length = %d, data = ", count
);
875 for (i
= 0; i
< count
; ++i
) {
876 printk ("%.2x ", data
[i
]);
881 /* We add a packet type identifier to the beginning of each
882 HCI frame. This makes the data in the tty look like a
883 serial USB devices. Each HCI frame can be broken across
884 multiple URBs so we buffer them until we have a full hci
887 if (!bluetooth
->bulk_packet_pos
) {
888 bluetooth
->bulk_buffer
[0] = ACL_PKT
;
889 bluetooth
->bulk_packet_pos
++;
892 if (bluetooth
->bulk_packet_pos
+ count
> ACL_BUFFER_SIZE
) {
893 err(__FUNCTION__
" - exceeded ACL_BUFFER_SIZE");
894 bluetooth
->bulk_packet_pos
= 0;
898 memcpy (&bluetooth
->bulk_buffer
[bluetooth
->bulk_packet_pos
],
899 urb
->transfer_buffer
, count
);
900 bluetooth
->bulk_packet_pos
+= count
;
901 urb
->actual_length
= 0;
903 if (bluetooth
->bulk_packet_pos
>= ACL_HDR_SIZE
) {
904 packet_size
= CHAR2INT16(bluetooth
->bulk_buffer
[4],bluetooth
->bulk_buffer
[3]);
909 if (packet_size
+ ACL_HDR_SIZE
< bluetooth
->bulk_packet_pos
) {
910 err(__FUNCTION__
" - packet was too long");
911 bluetooth
->bulk_packet_pos
= 0;
915 if (packet_size
+ ACL_HDR_SIZE
== bluetooth
->bulk_packet_pos
) {
916 for (i
= 0; i
< bluetooth
->bulk_packet_pos
; ++i
) {
917 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
918 if (bluetooth
->tty
->flip
.count
>= TTY_FLIPBUF_SIZE
) {
919 tty_flip_buffer_push(bluetooth
->tty
);
921 tty_insert_flip_char(bluetooth
->tty
, bluetooth
->bulk_buffer
[i
], 0);
923 tty_flip_buffer_push(bluetooth
->tty
);
924 bluetooth
->bulk_packet_pos
= 0;
928 FILL_BULK_URB(bluetooth
->read_urb
, bluetooth
->dev
,
929 usb_rcvbulkpipe(bluetooth
->dev
, bluetooth
->bulk_in_endpointAddress
),
930 bluetooth
->bulk_in_buffer
, bluetooth
->bulk_in_buffer_size
,
931 bluetooth_read_bulk_callback
, bluetooth
);
932 result
= usb_submit_urb(bluetooth
->read_urb
);
934 err (__FUNCTION__
" - failed resubmitting read urb, error %d", result
);
940 static void bluetooth_write_bulk_callback (struct urb
*urb
)
942 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)urb
->context
, __FUNCTION__
);
947 dbg(__FUNCTION__
" - bad bluetooth pointer, exiting");
952 dbg(__FUNCTION__
" - nonzero write bulk status received: %d", urb
->status
);
956 /* wake up our little function to let the tty layer know that something happened */
957 queue_task(&bluetooth
->tqueue
, &tq_immediate
);
958 mark_bh(IMMEDIATE_BH
);
963 static void bluetooth_softint(void *private)
965 struct usb_bluetooth
*bluetooth
= get_usb_bluetooth ((struct usb_bluetooth
*)private, __FUNCTION__
);
966 struct tty_struct
*tty
;
974 tty
= bluetooth
->tty
;
975 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) && tty
->ldisc
.write_wakeup
) {
976 dbg(__FUNCTION__
" - write wakeup call.");
977 (tty
->ldisc
.write_wakeup
)(tty
);
980 wake_up_interruptible(&tty
->write_wait
);
984 static void * usb_bluetooth_probe(struct usb_device
*dev
, unsigned int ifnum
,
985 const struct usb_device_id
*id
)
987 struct usb_bluetooth
*bluetooth
= NULL
;
988 struct usb_interface_descriptor
*interface
;
989 struct usb_endpoint_descriptor
*endpoint
;
990 struct usb_endpoint_descriptor
*interrupt_in_endpoint
[8];
991 struct usb_endpoint_descriptor
*bulk_in_endpoint
[8];
992 struct usb_endpoint_descriptor
*bulk_out_endpoint
[8];
993 int control_out_endpoint
;
998 int num_interrupt_in
= 0;
1000 int num_bulk_out
= 0;
1002 interface
= &dev
->actconfig
->interface
[ifnum
].altsetting
[0];
1003 control_out_endpoint
= interface
->bInterfaceNumber
;
1005 /* find the endpoints that we need */
1006 for (i
= 0; i
< interface
->bNumEndpoints
; ++i
) {
1007 endpoint
= &interface
->endpoint
[i
];
1009 if ((endpoint
->bEndpointAddress
& 0x80) &&
1010 ((endpoint
->bmAttributes
& 3) == 0x02)) {
1011 /* we found a bulk in endpoint */
1012 dbg("found bulk in");
1013 bulk_in_endpoint
[num_bulk_in
] = endpoint
;
1017 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
1018 ((endpoint
->bmAttributes
& 3) == 0x02)) {
1019 /* we found a bulk out endpoint */
1020 dbg("found bulk out");
1021 bulk_out_endpoint
[num_bulk_out
] = endpoint
;
1025 if ((endpoint
->bEndpointAddress
& 0x80) &&
1026 ((endpoint
->bmAttributes
& 3) == 0x03)) {
1027 /* we found a interrupt in endpoint */
1028 dbg("found interrupt in");
1029 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
1034 /* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */
1035 if ((num_bulk_in
!= 1) ||
1036 (num_bulk_out
!= 1) ||
1037 (num_interrupt_in
!= 1)) {
1038 dbg (__FUNCTION__
" - improper number of endpoints. Bluetooth driver not bound.");
1043 info("USB Bluetooth converter detected");
1045 for (minor
= 0; minor
< BLUETOOTH_TTY_MINORS
&& bluetooth_table
[minor
]; ++minor
)
1047 if (bluetooth_table
[minor
]) {
1048 err("No more free Bluetooth devices");
1053 if (!(bluetooth
= kmalloc(sizeof(struct usb_bluetooth
), GFP_KERNEL
))) {
1054 err("Out of memory");
1059 memset(bluetooth
, 0, sizeof(struct usb_bluetooth
));
1061 bluetooth
->magic
= USB_BLUETOOTH_MAGIC
;
1062 bluetooth
->dev
= dev
;
1063 bluetooth
->minor
= minor
;
1064 bluetooth
->tqueue
.routine
= bluetooth_softint
;
1065 bluetooth
->tqueue
.data
= bluetooth
;
1067 /* record the interface number for the control out */
1068 bluetooth
->control_out_bInterfaceNum
= control_out_endpoint
;
1070 /* create our control out urb pool */
1071 for (i
= 0; i
< NUM_CONTROL_URBS
; ++i
) {
1072 struct urb
*urb
= usb_alloc_urb(0);
1074 err("No free urbs available");
1077 urb
->transfer_buffer
= NULL
;
1078 bluetooth
->control_urb_pool
[i
] = urb
;
1081 /* set up the endpoint information */
1082 endpoint
= bulk_in_endpoint
[0];
1083 bluetooth
->read_urb
= usb_alloc_urb (0);
1084 if (!bluetooth
->read_urb
) {
1085 err("No free urbs available");
1088 bluetooth
->bulk_in_buffer_size
= buffer_size
= endpoint
->wMaxPacketSize
;
1089 bluetooth
->bulk_in_endpointAddress
= endpoint
->bEndpointAddress
;
1090 bluetooth
->bulk_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1091 if (!bluetooth
->bulk_in_buffer
) {
1092 err("Couldn't allocate bulk_in_buffer");
1095 FILL_BULK_URB(bluetooth
->read_urb
, dev
, usb_rcvbulkpipe(dev
, endpoint
->bEndpointAddress
),
1096 bluetooth
->bulk_in_buffer
, buffer_size
, bluetooth_read_bulk_callback
, bluetooth
);
1098 endpoint
= bulk_out_endpoint
[0];
1099 bluetooth
->bulk_out_endpointAddress
= endpoint
->bEndpointAddress
;
1101 /* create our write urb pool */
1102 for (i
= 0; i
< NUM_BULK_URBS
; ++i
) {
1103 struct urb
*urb
= usb_alloc_urb(0);
1105 err("No free urbs available");
1108 urb
->transfer_buffer
= NULL
;
1109 bluetooth
->write_urb_pool
[i
] = urb
;
1112 bluetooth
->bulk_out_buffer_size
= endpoint
->wMaxPacketSize
* 2;
1114 endpoint
= interrupt_in_endpoint
[0];
1115 bluetooth
->interrupt_in_urb
= usb_alloc_urb(0);
1116 if (!bluetooth
->interrupt_in_urb
) {
1117 err("No free urbs available");
1120 bluetooth
->interrupt_in_buffer_size
= buffer_size
= endpoint
->wMaxPacketSize
;
1121 bluetooth
->interrupt_in_endpointAddress
= endpoint
->bEndpointAddress
;
1122 bluetooth
->interrupt_in_interval
= endpoint
->bInterval
;
1123 bluetooth
->interrupt_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1124 if (!bluetooth
->interrupt_in_buffer
) {
1125 err("Couldn't allocate interrupt_in_buffer");
1128 FILL_INT_URB(bluetooth
->interrupt_in_urb
, dev
, usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
),
1129 bluetooth
->interrupt_in_buffer
, buffer_size
, bluetooth_int_callback
,
1130 bluetooth
, endpoint
->bInterval
);
1132 /* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */
1133 tty_register_devfs (&bluetooth_tty_driver
, 0, minor
);
1134 info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor
, minor
);
1136 bluetooth_table
[minor
] = bluetooth
;
1138 return bluetooth
; /* success */
1141 if (bluetooth
->read_urb
)
1142 usb_free_urb (bluetooth
->read_urb
);
1143 if (bluetooth
->bulk_in_buffer
)
1144 kfree (bluetooth
->bulk_in_buffer
);
1145 if (bluetooth
->interrupt_in_urb
)
1146 usb_free_urb (bluetooth
->interrupt_in_urb
);
1147 if (bluetooth
->interrupt_in_buffer
)
1148 kfree (bluetooth
->interrupt_in_buffer
);
1149 for (i
= 0; i
< NUM_BULK_URBS
; ++i
)
1150 if (bluetooth
->write_urb_pool
[i
])
1151 usb_free_urb (bluetooth
->write_urb_pool
[i
]);
1152 for (i
= 0; i
< NUM_CONTROL_URBS
; ++i
)
1153 if (bluetooth
->control_urb_pool
[i
])
1154 usb_free_urb (bluetooth
->control_urb_pool
[i
]);
1156 bluetooth_table
[minor
] = NULL
;
1158 /* free up any memory that we allocated */
1165 static void usb_bluetooth_disconnect(struct usb_device
*dev
, void *ptr
)
1167 struct usb_bluetooth
*bluetooth
= (struct usb_bluetooth
*) ptr
;
1171 if ((bluetooth
->active
) && (bluetooth
->tty
))
1172 tty_hangup(bluetooth
->tty
);
1174 bluetooth
->active
= 0;
1176 if (bluetooth
->read_urb
) {
1177 usb_unlink_urb (bluetooth
->read_urb
);
1178 usb_free_urb (bluetooth
->read_urb
);
1180 if (bluetooth
->bulk_in_buffer
)
1181 kfree (bluetooth
->bulk_in_buffer
);
1183 if (bluetooth
->interrupt_in_urb
) {
1184 usb_unlink_urb (bluetooth
->interrupt_in_urb
);
1185 usb_free_urb (bluetooth
->interrupt_in_urb
);
1187 if (bluetooth
->interrupt_in_buffer
)
1188 kfree (bluetooth
->interrupt_in_buffer
);
1190 tty_unregister_devfs (&bluetooth_tty_driver
, bluetooth
->minor
);
1192 for (i
= 0; i
< NUM_BULK_URBS
; ++i
) {
1193 if (bluetooth
->write_urb_pool
[i
]) {
1194 usb_unlink_urb (bluetooth
->write_urb_pool
[i
]);
1195 if (bluetooth
->write_urb_pool
[i
]->transfer_buffer
)
1196 kfree (bluetooth
->write_urb_pool
[i
]->transfer_buffer
);
1197 usb_free_urb (bluetooth
->write_urb_pool
[i
]);
1200 for (i
= 0; i
< NUM_CONTROL_URBS
; ++i
) {
1201 if (bluetooth
->control_urb_pool
[i
]) {
1202 usb_unlink_urb (bluetooth
->control_urb_pool
[i
]);
1203 if (bluetooth
->control_urb_pool
[i
]->transfer_buffer
)
1204 kfree (bluetooth
->control_urb_pool
[i
]->transfer_buffer
);
1205 usb_free_urb (bluetooth
->control_urb_pool
[i
]);
1209 info("Bluetooth converter now disconnected from ttyUB%d", bluetooth
->minor
);
1211 bluetooth_table
[bluetooth
->minor
] = NULL
;
1213 /* free up any memory that we allocated */
1217 info("device disconnected");
1224 static struct tty_driver bluetooth_tty_driver
= {
1225 magic
: TTY_DRIVER_MAGIC
,
1226 driver_name
: "usb-bluetooth",
1227 name
: "usb/ttub/%d",
1228 major
: BLUETOOTH_TTY_MAJOR
,
1230 num
: BLUETOOTH_TTY_MINORS
,
1231 type
: TTY_DRIVER_TYPE_SERIAL
,
1232 subtype
: SERIAL_TYPE_NORMAL
,
1233 flags
: TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
,
1235 refcount
: &bluetooth_refcount
,
1236 table
: bluetooth_tty
,
1237 termios
: bluetooth_termios
,
1238 termios_locked
: bluetooth_termios_locked
,
1240 open
: bluetooth_open
,
1241 close
: bluetooth_close
,
1242 write
: bluetooth_write
,
1243 write_room
: bluetooth_write_room
,
1244 ioctl
: bluetooth_ioctl
,
1245 set_termios
: bluetooth_set_termios
,
1246 throttle
: bluetooth_throttle
,
1247 unthrottle
: bluetooth_unthrottle
,
1248 chars_in_buffer
: bluetooth_chars_in_buffer
,
1252 int usb_bluetooth_init(void)
1257 /* Initalize our global data */
1258 for (i
= 0; i
< BLUETOOTH_TTY_MINORS
; ++i
) {
1259 bluetooth_table
[i
] = NULL
;
1262 info ("USB Bluetooth support registered");
1264 /* register the tty driver */
1265 bluetooth_tty_driver
.init_termios
= tty_std_termios
;
1266 bluetooth_tty_driver
.init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1267 if (tty_register_driver (&bluetooth_tty_driver
)) {
1268 err(__FUNCTION__
" - failed to register tty driver");
1272 /* register the USB driver */
1273 result
= usb_register(&usb_bluetooth_driver
);
1275 tty_unregister_driver(&bluetooth_tty_driver
);
1276 err("usb_register failed for the USB bluetooth driver. Error number %d", result
);
1284 void usb_bluetooth_exit(void)
1286 usb_deregister(&usb_bluetooth_driver
);
1287 tty_unregister_driver(&bluetooth_tty_driver
);
1291 module_init(usb_bluetooth_init
);
1292 module_exit(usb_bluetooth_exit
);