- Andries Brouwer: final isofs pieces.
[davej-history.git] / drivers / usb / bluetooth.c
blob616058acc0afa81e50e19970b2ab754897e2e155
1 /*
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
15 * core needs it.
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
31 * stack.
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
43 * function.
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>
85 #define DEBUG
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 */
109 #define CMD_PKT 0x01
110 #define ACL_PKT 0x02
111 #define SCO_PKT 0x03
112 #define EVENT_PKT 0x04
113 #define ERROR_PKT 0x05
114 #define NEG_PKT 0x06
116 /* Message sizes */
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 {
135 int magic;
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 = {
207 name: "bluetooth",
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)
223 if (!bluetooth) {
224 dbg("%s - bluetooth == NULL", function);
225 return -1;
227 if (bluetooth->magic != USB_BLUETOOTH_MAGIC) {
228 dbg("%s - bad magic number for bluetooth", function);
229 return -1;
232 return 0;
236 static inline struct usb_bluetooth* get_usb_bluetooth (struct usb_bluetooth *bluetooth, const char *function)
238 if (!bluetooth ||
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 */
242 return NULL;
245 return bluetooth;
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;
259 int i;
260 int status;
262 dbg (__FUNCTION__);
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];
269 break;
272 if (urb == NULL) {
273 dbg (__FUNCTION__ " - no free urbs");
274 return -ENOMEM;
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);
294 if (status)
295 dbg(__FUNCTION__ " - usb_submit_urb(control) failed with status = %d", status);
297 return 0;
304 /*****************************************************************************
305 * Driver tty interface functions
306 *****************************************************************************/
307 static int bluetooth_open (struct tty_struct *tty, struct file * filp)
309 struct usb_bluetooth *bluetooth;
310 int result;
312 dbg(__FUNCTION__);
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__)) {
321 return -ENODEV;
324 if (bluetooth->active) {
325 dbg (__FUNCTION__ " - device already open");
326 return -EINVAL;
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
335 * can get lost. */
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);
351 if (result)
352 dbg(__FUNCTION__ " - usb_submit_urb(read bulk) failed with status %d", result);
353 #endif
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);
359 if (result)
360 dbg(__FUNCTION__ " - usb_submit_urb(interrupt in) failed with status %d", result);
362 return 0;
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__);
369 int i;
371 if (!bluetooth) {
372 return;
375 dbg(__FUNCTION__);
377 if (!bluetooth->active) {
378 dbg (__FUNCTION__ " - device not opened");
379 return;
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;
397 int status;
398 int bytes_sent;
399 int buffer_size;
400 int i;
402 if (!bluetooth) {
403 return -ENODEV;
406 dbg(__FUNCTION__ " - %d byte(s)", count);
408 if (!bluetooth->active) {
409 dbg (__FUNCTION__ " - device not opened");
410 return -EINVAL;
413 if (count == 0) {
414 dbg(__FUNCTION__ " - write request of 0 bytes");
415 return 0;
417 if (count == 1) {
418 dbg(__FUNCTION__ " - write request only included type %d", buf[0]);
419 return 1;
422 #ifdef DEBUG
423 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ " - length = %d, data = ", count);
424 for (i = 0; i < count; ++i) {
425 printk ("%.2x ", buf[i]);
427 printk ("\n");
428 #endif
430 switch (*buf) {
431 /* First byte indicates the type of packet */
432 case CMD_PKT:
433 /* dbg(__FUNCTION__ "- Send cmd_pkt len:%d", count);*/
435 if (in_interrupt()){
436 printk("cmd_pkt from interrupt!\n");
437 return count;
440 new_buffer = kmalloc (count-1, GFP_KERNEL);
442 if (!new_buffer) {
443 err (__FUNCTION__ "- out of memory.");
444 return -ENOMEM;
447 if (from_user)
448 copy_from_user (new_buffer, buf+1, count-1);
449 else
450 memcpy (new_buffer, buf+1, count-1);
452 if (bluetooth_ctrl_msg (bluetooth, 0x00, 0x00, new_buffer, count-1) != 0) {
453 kfree (new_buffer);
454 return 0;
457 /* need to free new_buffer somehow... FIXME */
458 return count;
460 case ACL_PKT:
461 current_position = buf;
462 ++current_position;
463 --count;
464 bytes_sent = 0;
466 while (count > 0) {
467 urb = NULL;
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];
473 break;
476 if (urb == NULL) {
477 dbg (__FUNCTION__ " - no free urbs");
478 return bytes_sent;
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...");
492 return bytes_sent;
495 if (from_user)
496 copy_from_user(new_buffer, current_position, buffer_size);
497 else
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);
507 if (status)
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...*/
512 if (count != 0) {
513 udelay(500);
515 #endif
516 current_position += buffer_size;
517 bytes_sent += buffer_size;
518 count -= buffer_size;
521 return bytes_sent + 1;
523 default :
524 dbg(__FUNCTION__" - unsupported (at this time) write type");
527 return 0;
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__);
534 int room = 0;
535 int i;
537 if (!bluetooth) {
538 return -ENODEV;
541 dbg(__FUNCTION__);
543 if (!bluetooth->active) {
544 dbg (__FUNCTION__ " - device not open");
545 return -EINVAL;
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);
555 return 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__);
562 int chars = 0;
563 int i;
565 if (!bluetooth) {
566 return -ENODEV;
569 if (!bluetooth->active) {
570 dbg (__FUNCTION__ " - device not open");
571 return -EINVAL;
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);
581 return 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__);
589 if (!bluetooth) {
590 return;
593 dbg(__FUNCTION__);
595 if (!bluetooth->active) {
596 dbg (__FUNCTION__ " - device not open");
597 return;
600 dbg(__FUNCTION__ " unsupported (at this time)");
602 return;
606 static void bluetooth_unthrottle (struct tty_struct * tty)
608 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
610 if (!bluetooth) {
611 return;
614 dbg(__FUNCTION__);
616 if (!bluetooth->active) {
617 dbg (__FUNCTION__ " - device not open");
618 return;
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__);
629 if (!bluetooth) {
630 return -ENODEV;
633 dbg(__FUNCTION__ " - cmd 0x%.4x", cmd);
635 if (!bluetooth->active) {
636 dbg (__FUNCTION__ " - device not open");
637 return -ENODEV;
640 /* FIXME!!! */
641 return -ENOIOCTLCMD;
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__);
649 if (!bluetooth) {
650 return;
653 dbg(__FUNCTION__);
655 if (!bluetooth->active) {
656 dbg (__FUNCTION__ " - device not open");
657 return;
660 /* FIXME!!! */
662 return;
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__);
669 int result;
671 if (!bluetooth) {
672 return;
675 dbg(__FUNCTION__);
677 if (!bluetooth->active) {
678 dbg (__FUNCTION__ " - device not open");
679 return;
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);
688 if (result)
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__);
696 if (!bluetooth) {
697 return;
700 dbg(__FUNCTION__);
702 if (!bluetooth->active) {
703 dbg (__FUNCTION__ " - device not open");
704 return;
707 if ((bluetooth->read_urb) && (bluetooth->read_urb->actual_length))
708 usb_unlink_urb(bluetooth->read_urb);
710 #endif
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;
722 unsigned int i;
723 unsigned int count = urb->actual_length;
724 unsigned int packet_size;
726 dbg(__FUNCTION__);
728 if (!bluetooth) {
729 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
730 return;
733 if (urb->status) {
734 dbg(__FUNCTION__ " - nonzero int status received: %d", urb->status);
735 return;
738 if (!count) {
739 dbg(__FUNCTION__ " - zero length int");
740 return;
744 #ifdef DEBUG
745 if (count) {
746 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ "- length = %d, data = ", count);
747 for (i = 0; i < count; ++i) {
748 printk ("%.2x ", data[i]);
750 printk ("\n");
752 #endif
754 #ifdef BTBUGGYHARDWARE
755 if ((count >= 2) && (data[0] == 0xFF) && (data[1] == 0x00)) {
756 data += 2;
757 count -= 2;
759 if (count == 0) {
760 urb->actual_length = 0;
761 return;
763 #endif
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
768 packet */
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;
778 return;
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];
788 else
789 return;
791 if (packet_size + EVENT_HDR_SIZE < bluetooth->int_packet_pos) {
792 err(__FUNCTION__ " - packet was too long");
793 bluetooth->int_packet_pos = 0;
794 return;
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__);
816 dbg(__FUNCTION__);
818 if (!bluetooth) {
819 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
820 return;
823 if (urb->status) {
824 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
825 return;
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;
835 unsigned int i;
836 unsigned int packet_size;
837 int result;
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);
848 if (result)
849 err (__FUNCTION__ " - failed resubmitting read urb, error %d", result);
851 return;
853 #endif
855 dbg(__FUNCTION__);
857 if (!bluetooth) {
858 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
859 goto exit;
862 if (urb->status) {
863 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
864 goto exit;
867 if (!count) {
868 dbg(__FUNCTION__ " - zero length read bulk");
869 goto exit;
872 #ifdef DEBUG
873 if (count) {
874 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ "- length = %d, data = ", count);
875 for (i = 0; i < count; ++i) {
876 printk ("%.2x ", data[i]);
878 printk ("\n");
880 #endif
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
885 packet */
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;
895 goto exit;
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]);
905 } else {
906 goto exit;
909 if (packet_size + ACL_HDR_SIZE < bluetooth->bulk_packet_pos) {
910 err(__FUNCTION__ " - packet was too long");
911 bluetooth->bulk_packet_pos = 0;
912 goto exit;
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;
927 exit:
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);
933 if (result)
934 err (__FUNCTION__ " - failed resubmitting read urb, error %d", result);
936 return;
940 static void bluetooth_write_bulk_callback (struct urb *urb)
942 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
944 dbg(__FUNCTION__);
946 if (!bluetooth) {
947 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
948 return;
951 if (urb->status) {
952 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
953 return;
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);
959 return;
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;
968 dbg(__FUNCTION__);
970 if (!bluetooth) {
971 return;
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;
995 int minor;
996 int buffer_size;
997 int i;
998 int num_interrupt_in = 0;
999 int num_bulk_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;
1014 ++num_bulk_in;
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;
1022 ++num_bulk_out;
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;
1030 ++num_interrupt_in;
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.");
1039 return NULL;
1042 MOD_INC_USE_COUNT;
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");
1049 MOD_DEC_USE_COUNT;
1050 return NULL;
1053 if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) {
1054 err("Out of memory");
1055 MOD_DEC_USE_COUNT;
1056 return NULL;
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);
1073 if (urb == NULL) {
1074 err("No free urbs available");
1075 goto probe_error;
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");
1086 goto probe_error;
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");
1093 goto probe_error;
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);
1104 if (urb == NULL) {
1105 err("No free urbs available");
1106 goto probe_error;
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");
1118 goto probe_error;
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");
1126 goto probe_error;
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 */
1140 probe_error:
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 */
1159 kfree (bluetooth);
1160 MOD_DEC_USE_COUNT;
1161 return NULL;
1165 static void usb_bluetooth_disconnect(struct usb_device *dev, void *ptr)
1167 struct usb_bluetooth *bluetooth = (struct usb_bluetooth *) ptr;
1168 int i;
1170 if (bluetooth) {
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 */
1214 kfree (bluetooth);
1216 } else {
1217 info("device disconnected");
1220 MOD_DEC_USE_COUNT;
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,
1229 minor_start: 0,
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)
1254 int i;
1255 int result;
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");
1269 return -1;
1272 /* register the USB driver */
1273 result = usb_register(&usb_bluetooth_driver);
1274 if (result < 0) {
1275 tty_unregister_driver(&bluetooth_tty_driver);
1276 err("usb_register failed for the USB bluetooth driver. Error number %d", result);
1277 return -1;
1280 return 0;
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);