v2.5.0.7 -> v2.5.0.8
[opensuse:kernel.git] / drivers / usb / bluetooth.c
1 /*
2  * bluetooth.c   Version 0.13
3  *
4  * Copyright (c) 2000, 2001 Greg Kroah-Hartman  <greg@kroah.com>
5  * Copyright (c) 2000 Mark Douglas Corner       <mcorner@umich.edu>
6  *
7  * USB Bluetooth driver, based on the Bluetooth Spec version 1.0B
8  * 
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.
12  *
13  * (2001/07/09) Version 0.12 gkh
14  *      - removed in_interrupt() call, as it doesn't make sense to do 
15  *        that anymore.
16  *
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.
20  *
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
26  *        up the logic a bit.
27  *      - Added a buffer to the control_urb_pool which fixes a memory leak
28  *        when the device is removed from the system.
29  *
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.
33  *
34  * (08/04/2001) gb
35  *      Identify version on module load.
36  *
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
41  *      fixes.
42  *
43  * (11/29/2000) Version 0.7 gkh
44  *      Fixed problem with overrunning the tty flip buffer.
45  *      Removed unneeded NULL pointer initialization.
46  *
47  * (10/05/2000) Version 0.6 gkh
48  *      Fixed bug with urb->dev not being set properly, now that the usb
49  *      core needs it.
50  *      Got a real major id number and name.
51  *
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.
55  *
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.
62  *
63  * (08/03/2000) Version 0.3 gkh mdc
64  *      Merged in Mark's changes to make the driver play nice with the Axis
65  *      stack.
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.
74  *
75  * (07/11/2000) Version 0.2 gkh
76  *      Fixed a small bug found by Nils Faerber in the usb_bluetooth_probe 
77  *      function.
78  *
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 :)
86  * 
87  */
88
89 /*
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.
94  *
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.
99  *
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
103  */
104
105
106 #include <linux/kernel.h>
107 #include <linux/sched.h>
108 #include <linux/signal.h>
109 #include <linux/errno.h>
110 #include <linux/poll.h>
111 #include <linux/init.h>
112 #include <linux/slab.h>
113 #include <linux/fcntl.h>
114 #include <linux/tty.h>
115 #include <linux/tty_driver.h>
116 #include <linux/tty_flip.h>
117 #include <linux/module.h>
118
119 #define DEBUG
120 #include <linux/usb.h>
121
122 /*
123  * Version Information
124  */
125 #define DRIVER_VERSION "v0.13"
126 #define DRIVER_AUTHOR "Greg Kroah-Hartman, Mark Douglas Corner"
127 #define DRIVER_DESC "USB Bluetooth tty driver"
128
129 /* define this if you have hardware that is not good */
130 /*#define       BTBUGGYHARDWARE */
131
132 /* Class, SubClass, and Protocol codes that describe a Bluetooth device */
133 #define WIRELESS_CLASS_CODE                     0xe0
134 #define RF_SUBCLASS_CODE                        0x01
135 #define BLUETOOTH_PROGRAMMING_PROTOCOL_CODE     0x01
136
137
138 #define BLUETOOTH_TTY_MAJOR     216     /* real device node major id */
139 #define BLUETOOTH_TTY_MINORS    256     /* whole lotta bluetooth devices */
140
141 #define USB_BLUETOOTH_MAGIC     0x6d02  /* magic number for bluetooth struct */
142
143 #define BLUETOOTH_CONTROL_REQUEST_TYPE  0x20
144
145 /* Bluetooth packet types */
146 #define CMD_PKT                 0x01
147 #define ACL_PKT                 0x02
148 #define SCO_PKT                 0x03
149 #define EVENT_PKT               0x04
150 #define ERROR_PKT               0x05
151 #define NEG_PKT                 0x06
152
153 /* Message sizes */
154 #define MAX_EVENT_SIZE          0xFF
155 #define EVENT_HDR_SIZE          3       /* 2 for the header + 1 for the type indicator */
156 #define EVENT_BUFFER_SIZE       (MAX_EVENT_SIZE + EVENT_HDR_SIZE)
157
158 #define MAX_ACL_SIZE            0xFFFF
159 #define ACL_HDR_SIZE            5       /* 4 for the header + 1 for the type indicator */
160 #define ACL_BUFFER_SIZE         (MAX_ACL_SIZE + ACL_HDR_SIZE)
161
162 /* parity check flag */
163 #define RELEVANT_IFLAG(iflag)   (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
164
165 #define CHAR2INT16(c1,c0)       (((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
166
167 #define NUM_BULK_URBS           24
168 #define NUM_CONTROL_URBS        16
169
170 struct usb_bluetooth {
171         int                     magic;
172         struct usb_device *     dev;
173         struct tty_driver *     tty_driver;     /* the tty_driver for this device */
174         struct tty_struct *     tty;            /* the coresponding tty for this port */
175
176         unsigned char           minor;          /* the starting minor number for this device */
177         int                     throttle;       /* throttled by tty layer */
178         int                     open_count;
179         
180         __u8                    control_out_bInterfaceNum;
181         struct urb *            control_urb_pool[NUM_CONTROL_URBS];
182         devrequest              dr[NUM_CONTROL_URBS];
183
184         unsigned char *         interrupt_in_buffer;
185         struct urb *            interrupt_in_urb;
186         __u8                    interrupt_in_endpointAddress;
187         __u8                    interrupt_in_interval;
188         int                     interrupt_in_buffer_size;
189
190         unsigned char *         bulk_in_buffer;
191         struct urb *            read_urb;
192         __u8                    bulk_in_endpointAddress;
193         int                     bulk_in_buffer_size;
194
195         int                     bulk_out_buffer_size;
196         struct urb *            write_urb_pool[NUM_BULK_URBS];
197         __u8                    bulk_out_endpointAddress;
198
199         wait_queue_head_t       write_wait;
200
201         struct tq_struct        tqueue;         /* task queue for line discipline waking up */
202         
203         unsigned int            int_packet_pos;
204         unsigned char           int_buffer[EVENT_BUFFER_SIZE];
205         unsigned int            bulk_packet_pos;
206         unsigned char           bulk_buffer[ACL_BUFFER_SIZE];   /* 64k preallocated, fix? */
207         struct semaphore        lock;
208 };
209
210
211 /* local function prototypes */
212 static int  bluetooth_open              (struct tty_struct *tty, struct file *filp);
213 static void bluetooth_close             (struct tty_struct *tty, struct file *filp);
214 static int  bluetooth_write             (struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
215 static int  bluetooth_write_room        (struct tty_struct *tty);
216 static int  bluetooth_chars_in_buffer   (struct tty_struct *tty);
217 static void bluetooth_throttle          (struct tty_struct *tty);
218 static void bluetooth_unthrottle        (struct tty_struct *tty);
219 static int  bluetooth_ioctl             (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
220 static void bluetooth_set_termios       (struct tty_struct *tty, struct termios *old);
221
222 static void bluetooth_int_callback              (struct urb *urb);
223 static void bluetooth_ctrl_callback             (struct urb *urb);
224 static void bluetooth_read_bulk_callback        (struct urb *urb);
225 static void bluetooth_write_bulk_callback       (struct urb *urb);
226
227 static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
228                                   const struct usb_device_id *id);
229 static void usb_bluetooth_disconnect    (struct usb_device *dev, void *ptr);
230
231
232 static struct usb_device_id usb_bluetooth_ids [] = {
233         { USB_DEVICE_INFO(WIRELESS_CLASS_CODE, RF_SUBCLASS_CODE, BLUETOOTH_PROGRAMMING_PROTOCOL_CODE) },
234         { }                                             /* Terminating entry */
235 };
236
237 MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
238
239 static struct usb_driver usb_bluetooth_driver = {
240         name:           "bluetooth",
241         probe:          usb_bluetooth_probe,
242         disconnect:     usb_bluetooth_disconnect,
243         id_table:       usb_bluetooth_ids,
244 };
245
246 static int                      bluetooth_refcount;
247 static struct tty_driver        bluetooth_tty_driver;
248 static struct tty_struct *      bluetooth_tty[BLUETOOTH_TTY_MINORS];
249 static struct termios *         bluetooth_termios[BLUETOOTH_TTY_MINORS];
250 static struct termios *         bluetooth_termios_locked[BLUETOOTH_TTY_MINORS];
251 static struct usb_bluetooth     *bluetooth_table[BLUETOOTH_TTY_MINORS];
252
253
254 static inline int bluetooth_paranoia_check (struct usb_bluetooth *bluetooth, const char *function)
255 {
256         if (!bluetooth) {
257                 dbg("%s - bluetooth == NULL", function);
258                 return -1;
259         }
260         if (bluetooth->magic != USB_BLUETOOTH_MAGIC) {
261                 dbg("%s - bad magic number for bluetooth", function);
262                 return -1;
263         }
264
265         return 0;
266 }
267
268
269 static inline struct usb_bluetooth* get_usb_bluetooth (struct usb_bluetooth *bluetooth, const char *function)
270 {
271         if (!bluetooth || 
272             bluetooth_paranoia_check (bluetooth, function)) { 
273                 /* then say that we dont have a valid usb_bluetooth thing, which will
274                  * end up generating -ENODEV return values */
275                 return NULL;
276         }
277
278         return bluetooth;
279 }
280
281
282 static inline struct usb_bluetooth *get_bluetooth_by_minor (int minor)
283 {
284         return bluetooth_table[minor];
285 }
286
287
288 static int bluetooth_ctrl_msg (struct usb_bluetooth *bluetooth, int request, int value, const unsigned char *buf, int len)
289 {
290         struct urb *urb = NULL;
291         devrequest *dr = NULL;
292         int i;
293         int status;
294
295         dbg (__FUNCTION__);
296
297         /* try to find a free urb in our list */
298         for (i = 0; i < NUM_CONTROL_URBS; ++i) {
299                 if (bluetooth->control_urb_pool[i]->status != -EINPROGRESS) {
300                         urb = bluetooth->control_urb_pool[i];
301                         dr = &bluetooth->dr[i];
302                         break;
303                 }
304         }
305         if (urb == NULL) {
306                 dbg (__FUNCTION__ " - no free urbs");
307                 return -ENOMEM;
308         }
309
310         /* keep increasing the urb transfer buffer to fit the size of the message */
311         if (urb->transfer_buffer == NULL) {
312                 urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
313                 if (urb->transfer_buffer == NULL) {
314                         err (__FUNCTION__" - out of memory");
315                         return -ENOMEM;
316                 }
317         }
318         if (urb->transfer_buffer_length < len) {
319                 kfree (urb->transfer_buffer);
320                 urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
321                 if (urb->transfer_buffer == NULL) {
322                         err (__FUNCTION__" - out of memory");
323                         return -ENOMEM;
324                 }
325         }
326         memcpy (urb->transfer_buffer, buf, len);
327
328         dr->requesttype = BLUETOOTH_CONTROL_REQUEST_TYPE;
329         dr->request = request;
330         dr->value = cpu_to_le16((u16) value);
331         dr->index = cpu_to_le16((u16) bluetooth->control_out_bInterfaceNum);
332         dr->length = cpu_to_le16((u16) len);
333         
334         FILL_CONTROL_URB (urb, bluetooth->dev, usb_sndctrlpipe(bluetooth->dev, 0),
335                           (unsigned char*)dr, urb->transfer_buffer, len, bluetooth_ctrl_callback, bluetooth);
336
337         /* send it down the pipe */
338         status = usb_submit_urb(urb);
339         if (status)
340                 dbg(__FUNCTION__ " - usb_submit_urb(control) failed with status = %d", status);
341         
342         return status;
343 }
344
345
346
347
348
349 /*****************************************************************************
350  * Driver tty interface functions
351  *****************************************************************************/
352 static int bluetooth_open (struct tty_struct *tty, struct file * filp)
353 {
354         struct usb_bluetooth *bluetooth;
355         int result;
356
357         dbg(__FUNCTION__);
358
359         /* initialize the pointer incase something fails */
360         tty->driver_data = NULL;
361
362         /* get the bluetooth object associated with this tty pointer */
363         bluetooth = get_bluetooth_by_minor (MINOR(tty->device));
364
365         if (bluetooth_paranoia_check (bluetooth, __FUNCTION__)) {
366                 return -ENODEV;
367         }
368
369         down (&bluetooth->lock);
370  
371         ++bluetooth->open_count;
372         if (bluetooth->open_count == 1) {
373                 /* set up our structure making the tty driver remember our object, and us it */
374                 tty->driver_data = bluetooth;
375                 bluetooth->tty = tty;
376
377                 /* force low_latency on so that our tty_push actually forces the data through, 
378                 * otherwise it is scheduled, and with high data rates (like with OHCI) data
379                 * can get lost. */
380                 bluetooth->tty->low_latency = 1;
381         
382                 /* Reset the packet position counters */
383                 bluetooth->int_packet_pos = 0;
384                 bluetooth->bulk_packet_pos = 0;
385
386 #ifndef BTBUGGYHARDWARE
387                 /* Start reading from the device */
388                 FILL_BULK_URB (bluetooth->read_urb, bluetooth->dev, 
389                                usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
390                                bluetooth->bulk_in_buffer,
391                                bluetooth->bulk_in_buffer_size,
392                                bluetooth_read_bulk_callback, bluetooth);
393                 result = usb_submit_urb(bluetooth->read_urb);
394                 if (result)
395                         dbg(__FUNCTION__ " - usb_submit_urb(read bulk) failed with status %d", result);
396 #endif
397                 FILL_INT_URB (bluetooth->interrupt_in_urb, bluetooth->dev,
398                               usb_rcvintpipe(bluetooth->dev, bluetooth->interrupt_in_endpointAddress),
399                               bluetooth->interrupt_in_buffer,
400                               bluetooth->interrupt_in_buffer_size,
401                               bluetooth_int_callback, bluetooth,
402                               bluetooth->interrupt_in_interval);
403                 result = usb_submit_urb(bluetooth->interrupt_in_urb);
404                 if (result)
405                         dbg(__FUNCTION__ " - usb_submit_urb(interrupt in) failed with status %d", result);
406         }
407         
408         up(&bluetooth->lock);
409
410         return 0;
411 }
412
413
414 static void bluetooth_close (struct tty_struct *tty, struct file * filp)
415 {
416         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
417         int i;
418
419         if (!bluetooth) {
420                 return;
421         }
422
423         dbg(__FUNCTION__);
424
425         if (!bluetooth->open_count) {
426                 dbg (__FUNCTION__ " - device not opened");
427                 return;
428         }
429
430         down (&bluetooth->lock);
431  
432         --bluetooth->open_count;
433         if (bluetooth->open_count <= 0) {
434                 bluetooth->open_count = 0;
435
436                 /* shutdown any bulk reads and writes that might be going on */
437                 for (i = 0; i < NUM_BULK_URBS; ++i)
438                         usb_unlink_urb (bluetooth->write_urb_pool[i]);
439                 usb_unlink_urb (bluetooth->read_urb);
440                 usb_unlink_urb (bluetooth->interrupt_in_urb);
441         }
442         up(&bluetooth->lock);
443 }
444
445
446 static int bluetooth_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
447 {
448         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
449         struct urb *urb = NULL;
450         unsigned char *temp_buffer = NULL;
451         const unsigned char *current_buffer;
452         const unsigned char *current_position;
453         int bytes_sent;
454         int buffer_size;
455         int i;
456         int retval = 0;
457
458         if (!bluetooth) {
459                 return -ENODEV;
460         }
461
462         dbg(__FUNCTION__ " - %d byte(s)", count);
463
464         if (!bluetooth->open_count) {
465                 dbg (__FUNCTION__ " - device not opened");
466                 return -EINVAL;
467         }
468
469         if (count == 0) {
470                 dbg(__FUNCTION__ " - write request of 0 bytes");
471                 return 0;
472         }
473         if (count == 1) {
474                 dbg(__FUNCTION__ " - write request only included type %d", buf[0]);
475                 return 1;
476         }
477
478 #ifdef DEBUG
479         printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ " - length = %d, data = ", count);
480         for (i = 0; i < count; ++i) {
481                 printk ("%.2x ", buf[i]);
482         }
483         printk ("\n");
484 #endif
485
486         if (from_user) {
487                 temp_buffer = kmalloc (count, GFP_KERNEL);
488                 if (temp_buffer == NULL) {
489                         err (__FUNCTION__ "- out of memory.");
490                         retval = -ENOMEM;
491                         goto exit;
492                 }
493                 copy_from_user (temp_buffer, buf, count);
494                 current_buffer = temp_buffer;
495         } else {
496                 current_buffer = buf;
497         }
498
499         switch (*current_buffer) {
500                 /* First byte indicates the type of packet */
501                 case CMD_PKT:
502                         /* dbg(__FUNCTION__ "- Send cmd_pkt len:%d", count);*/
503
504                         retval = bluetooth_ctrl_msg (bluetooth, 0x00, 0x00, &current_buffer[1], count-1);
505                         if (retval) {
506                                 goto exit;
507                         }
508                         retval = count;
509                         break;
510
511                 case ACL_PKT:
512                         current_position = current_buffer;
513                         ++current_position;
514                         --count;
515                         bytes_sent = 0;
516
517                         while (count > 0) {
518                                 urb = NULL;
519
520                                 /* try to find a free urb in our list */
521                                 for (i = 0; i < NUM_BULK_URBS; ++i) {
522                                         if (bluetooth->write_urb_pool[i]->status != -EINPROGRESS) {
523                                                 urb = bluetooth->write_urb_pool[i];
524                                                 break;
525                                         }
526                                 }
527                                 if (urb == NULL) {
528                                         dbg (__FUNCTION__ " - no free urbs");
529                                         retval = bytes_sent;
530                                         goto exit;
531                                 }
532                                 
533
534                                 buffer_size = min (count, bluetooth->bulk_out_buffer_size);
535                                 memcpy (urb->transfer_buffer, current_position, buffer_size);
536
537                                 /* build up our urb */
538                                 FILL_BULK_URB (urb, bluetooth->dev, usb_sndbulkpipe(bluetooth->dev, bluetooth->bulk_out_endpointAddress),
539                                                 urb->transfer_buffer, buffer_size, bluetooth_write_bulk_callback, bluetooth);
540                                 urb->transfer_flags |= USB_QUEUE_BULK;
541
542                                 /* send it down the pipe */
543                                 retval = usb_submit_urb(urb);
544                                 if (retval) {
545                                         dbg(__FUNCTION__ " - usb_submit_urb(write bulk) failed with error = %d", retval);
546                                         goto exit;
547                                 }
548 #ifdef BTBUGGYHARDWARE
549                                 /* A workaround for the stalled data bug */
550                                 /* May or may not be needed...*/
551                                 if (count != 0) {
552                                         udelay(500);
553                                 }
554 #endif
555                                 current_position += buffer_size;
556                                 bytes_sent += buffer_size;
557                                 count -= buffer_size;
558                         }
559
560                         retval = bytes_sent + 1;
561                         break;
562                 
563                 default :
564                         dbg(__FUNCTION__" - unsupported (at this time) write type");
565                         retval = -EINVAL;
566                         break;
567         }
568
569 exit:
570         if (temp_buffer != NULL)
571                 kfree (temp_buffer);
572
573         return retval;
574
575
576
577 static int bluetooth_write_room (struct tty_struct *tty) 
578 {
579         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
580         int room = 0;
581         int i;
582
583         if (!bluetooth) {
584                 return -ENODEV;
585         }
586
587         dbg(__FUNCTION__);
588
589         if (!bluetooth->open_count) {
590                 dbg (__FUNCTION__ " - device not open");
591                 return -EINVAL;
592         }
593
594         for (i = 0; i < NUM_BULK_URBS; ++i) {
595                 if (bluetooth->write_urb_pool[i]->status != -EINPROGRESS) {
596                         room += bluetooth->bulk_out_buffer_size;
597                 }
598         }
599
600         dbg(__FUNCTION__ " - returns %d", room);
601         return room;
602 }
603
604
605 static int bluetooth_chars_in_buffer (struct tty_struct *tty) 
606 {
607         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
608         int chars = 0;
609         int i;
610
611         if (!bluetooth) {
612                 return -ENODEV;
613         }
614
615         if (!bluetooth->open_count) {
616                 dbg (__FUNCTION__ " - device not open");
617                 return -EINVAL;
618         }
619
620         for (i = 0; i < NUM_BULK_URBS; ++i) {
621                 if (bluetooth->write_urb_pool[i]->status == -EINPROGRESS) {
622                         chars += bluetooth->write_urb_pool[i]->transfer_buffer_length;
623                 }
624         }
625
626         dbg (__FUNCTION__ " - returns %d", chars);
627         return chars;
628 }
629
630
631 static void bluetooth_throttle (struct tty_struct * tty)
632 {
633         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
634
635         if (!bluetooth) {
636                 return;
637         }
638
639         dbg(__FUNCTION__);
640
641         if (!bluetooth->open_count) {
642                 dbg (__FUNCTION__ " - device not open");
643                 return;
644         }
645         
646         dbg(__FUNCTION__ " unsupported (at this time)");
647
648         return;
649 }
650
651
652 static void bluetooth_unthrottle (struct tty_struct * tty)
653 {
654         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
655
656         if (!bluetooth) {
657                 return;
658         }
659
660         dbg(__FUNCTION__);
661
662         if (!bluetooth->open_count) {
663                 dbg (__FUNCTION__ " - device not open");
664                 return;
665         }
666
667         dbg(__FUNCTION__ " unsupported (at this time)");
668 }
669
670
671 static int bluetooth_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
672 {
673         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
674
675         if (!bluetooth) {
676                 return -ENODEV;
677         }
678
679         dbg(__FUNCTION__ " - cmd 0x%.4x", cmd);
680
681         if (!bluetooth->open_count) {
682                 dbg (__FUNCTION__ " - device not open");
683                 return -ENODEV;
684         }
685
686         /* FIXME!!! */
687         return -ENOIOCTLCMD;
688 }
689
690
691 static void bluetooth_set_termios (struct tty_struct *tty, struct termios * old)
692 {
693         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
694
695         if (!bluetooth) {
696                 return;
697         }
698
699         dbg(__FUNCTION__);
700
701         if (!bluetooth->open_count) {
702                 dbg (__FUNCTION__ " - device not open");
703                 return;
704         }
705
706         /* FIXME!!! */
707
708         return;
709 }
710
711
712 #ifdef BTBUGGYHARDWARE
713 void btusb_enable_bulk_read(struct tty_struct *tty){
714         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
715         int result;
716
717         if (!bluetooth) {
718                 return;
719         }
720
721         dbg(__FUNCTION__);
722
723         if (!bluetooth->open_count) {
724                 dbg (__FUNCTION__ " - device not open");
725                 return;
726         }
727
728         if (bluetooth->read_urb) {
729                 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev, 
730                               usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
731                               bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, 
732                               bluetooth_read_bulk_callback, bluetooth);
733                 result = usb_submit_urb(bluetooth->read_urb);
734                 if (result)
735                         err (__FUNCTION__ " - failed submitting read urb, error %d", result);
736         }
737 }
738
739 void btusb_disable_bulk_read(struct tty_struct *tty){
740         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
741
742         if (!bluetooth) {
743                 return;
744         }
745
746         dbg(__FUNCTION__);
747
748         if (!bluetooth->open_count) {
749                 dbg (__FUNCTION__ " - device not open");
750                 return;
751         }
752
753         if ((bluetooth->read_urb) && (bluetooth->read_urb->actual_length))
754                 usb_unlink_urb(bluetooth->read_urb);
755 }
756 #endif
757
758
759 /*****************************************************************************
760  * urb callback functions
761  *****************************************************************************/
762
763
764 static void bluetooth_int_callback (struct urb *urb)
765 {
766         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
767         unsigned char *data = urb->transfer_buffer;
768         unsigned int i;
769         unsigned int count = urb->actual_length;
770         unsigned int packet_size;
771
772         dbg(__FUNCTION__);
773
774         if (!bluetooth) {
775                 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
776                 return;
777         }
778
779         if (urb->status) {
780                 dbg(__FUNCTION__ " - nonzero int status received: %d", urb->status);
781                 return;
782         }
783
784         if (!count) {
785                 dbg(__FUNCTION__ " - zero length int");
786                 return;
787         }
788
789
790 #ifdef DEBUG
791         if (count) {
792                 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ "- length = %d, data = ", count);
793                 for (i = 0; i < count; ++i) {
794                         printk ("%.2x ", data[i]);
795                 }
796                 printk ("\n");
797         }
798 #endif
799
800 #ifdef BTBUGGYHARDWARE
801         if ((count >= 2) && (data[0] == 0xFF) && (data[1] == 0x00)) {
802                 data += 2;
803                 count -= 2;
804         }
805         if (count == 0) {
806                 urb->actual_length = 0;
807                 return;
808         }
809 #endif
810         /* We add  a packet type identifier to the beginning of each
811            HCI frame.  This makes the data in the tty look like a
812            serial USB devices.  Each HCI frame can be broken across
813            multiple URBs so we buffer them until we have a full hci
814            packet */
815
816         if (!bluetooth->int_packet_pos) {
817                 bluetooth->int_buffer[0] = EVENT_PKT;
818                 bluetooth->int_packet_pos++;
819         }
820         
821         if (bluetooth->int_packet_pos + count > EVENT_BUFFER_SIZE) {
822                 err(__FUNCTION__ " - exceeded EVENT_BUFFER_SIZE");
823                 bluetooth->int_packet_pos = 0;
824                 return;
825         }
826
827         memcpy (&bluetooth->int_buffer[bluetooth->int_packet_pos],
828                 urb->transfer_buffer, count);
829         bluetooth->int_packet_pos += count;
830         urb->actual_length = 0;
831
832         if (bluetooth->int_packet_pos >= EVENT_HDR_SIZE)
833                 packet_size = bluetooth->int_buffer[2];
834         else
835                 return;
836
837         if (packet_size + EVENT_HDR_SIZE < bluetooth->int_packet_pos) {
838                 err(__FUNCTION__ " - packet was too long");
839                 bluetooth->int_packet_pos = 0;
840                 return;
841         }
842
843         if (packet_size + EVENT_HDR_SIZE == bluetooth->int_packet_pos) {
844                 for (i = 0; i < bluetooth->int_packet_pos; ++i) {
845                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them */
846                         if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
847                                 tty_flip_buffer_push(bluetooth->tty);
848                         }
849                         tty_insert_flip_char(bluetooth->tty, bluetooth->int_buffer[i], 0);
850                 }
851                 tty_flip_buffer_push(bluetooth->tty);
852
853                 bluetooth->int_packet_pos = 0;
854         }
855 }
856
857
858 static void bluetooth_ctrl_callback (struct urb *urb)
859 {
860         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
861
862         dbg(__FUNCTION__);
863
864         if (!bluetooth) {
865                 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
866                 return;
867         }
868
869         if (urb->status) {
870                 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
871                 return;
872         }
873 }
874
875
876 static void bluetooth_read_bulk_callback (struct urb *urb)
877 {
878         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
879         unsigned char *data = urb->transfer_buffer;
880         unsigned int count = urb->actual_length;
881         unsigned int i;
882         unsigned int packet_size;
883         int result;
884
885
886         dbg(__FUNCTION__);
887
888         if (!bluetooth) {
889                 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
890                 return;
891         }
892
893         if (urb->status) {
894                 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
895                 if (urb->status == -ENOENT) {                   
896                         dbg(__FUNCTION__ " - URB canceled, won't reschedule");
897                         return;
898                 }
899                 goto exit;
900         }
901
902         if (!count) {
903                 dbg(__FUNCTION__ " - zero length read bulk");
904                 goto exit;
905         }
906
907 #ifdef DEBUG
908         if (count) {
909                 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ "- length = %d, data = ", count);
910                 for (i = 0; i < count; ++i) {
911                         printk ("%.2x ", data[i]);
912                 }
913                 printk ("\n");
914         }
915 #endif
916 #ifdef BTBUGGYHARDWARE
917         if ((count == 4) && (data[0] == 0x00) && (data[1] == 0x00)
918             && (data[2] == 0x00) && (data[3] == 0x00)) {
919                 urb->actual_length = 0;
920                 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev, 
921                               usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
922                               bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, 
923                               bluetooth_read_bulk_callback, bluetooth);
924                 result = usb_submit_urb(bluetooth->read_urb);
925                 if (result)
926                         err (__FUNCTION__ " - failed resubmitting read urb, error %d", result);
927
928                 return;
929         }
930 #endif
931         /* We add  a packet type identifier to the beginning of each
932            HCI frame.  This makes the data in the tty look like a
933            serial USB devices.  Each HCI frame can be broken across
934            multiple URBs so we buffer them until we have a full hci
935            packet */
936         
937         if (!bluetooth->bulk_packet_pos) {
938                 bluetooth->bulk_buffer[0] = ACL_PKT;
939                 bluetooth->bulk_packet_pos++;
940         }
941
942         if (bluetooth->bulk_packet_pos + count > ACL_BUFFER_SIZE) {
943                 err(__FUNCTION__ " - exceeded ACL_BUFFER_SIZE");
944                 bluetooth->bulk_packet_pos = 0;
945                 goto exit;
946         }
947
948         memcpy (&bluetooth->bulk_buffer[bluetooth->bulk_packet_pos],
949                 urb->transfer_buffer, count);
950         bluetooth->bulk_packet_pos += count;
951         urb->actual_length = 0;
952
953         if (bluetooth->bulk_packet_pos >= ACL_HDR_SIZE) {
954                 packet_size = CHAR2INT16(bluetooth->bulk_buffer[4],bluetooth->bulk_buffer[3]);
955         } else {
956                 goto exit;
957         }
958
959         if (packet_size + ACL_HDR_SIZE < bluetooth->bulk_packet_pos) {
960                 err(__FUNCTION__ " - packet was too long");
961                 bluetooth->bulk_packet_pos = 0;
962                 goto exit;
963         }
964
965         if (packet_size + ACL_HDR_SIZE == bluetooth->bulk_packet_pos) {
966                 for (i = 0; i < bluetooth->bulk_packet_pos; ++i) {
967                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
968                         if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
969                                 tty_flip_buffer_push(bluetooth->tty);
970                         }
971                         tty_insert_flip_char(bluetooth->tty, bluetooth->bulk_buffer[i], 0);
972                 }
973                 tty_flip_buffer_push(bluetooth->tty);
974                 bluetooth->bulk_packet_pos = 0;
975         }       
976
977 exit:
978         if (!bluetooth || !bluetooth->open_count)
979                 return;
980
981         FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev, 
982                       usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
983                       bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, 
984                       bluetooth_read_bulk_callback, bluetooth);
985         result = usb_submit_urb(bluetooth->read_urb);
986         if (result)
987                 err (__FUNCTION__ " - failed resubmitting read urb, error %d", result);
988
989         return;
990 }
991
992
993 static void bluetooth_write_bulk_callback (struct urb *urb)
994 {
995         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
996
997         dbg(__FUNCTION__);
998
999         if (!bluetooth) {
1000                 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
1001                 return;
1002         }
1003
1004         if (urb->status) {
1005                 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
1006                 return;
1007         }
1008
1009         /* wake up our little function to let the tty layer know that something happened */
1010         queue_task(&bluetooth->tqueue, &tq_immediate);
1011         mark_bh(IMMEDIATE_BH);
1012         return;
1013 }
1014
1015
1016 static void bluetooth_softint(void *private)
1017 {
1018         struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)private, __FUNCTION__);
1019         struct tty_struct *tty;
1020
1021         dbg(__FUNCTION__);
1022
1023         if (!bluetooth) {
1024                 return;
1025         }
1026
1027         tty = bluetooth->tty;
1028         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
1029                 dbg(__FUNCTION__ " - write wakeup call.");
1030                 (tty->ldisc.write_wakeup)(tty);
1031         }
1032
1033         wake_up_interruptible(&tty->write_wait);
1034 }
1035
1036
1037 static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
1038                                   const struct usb_device_id *id)
1039 {
1040         struct usb_bluetooth *bluetooth = NULL;
1041         struct usb_interface_descriptor *interface;
1042         struct usb_endpoint_descriptor *endpoint;
1043         struct usb_endpoint_descriptor *interrupt_in_endpoint[8];
1044         struct usb_endpoint_descriptor *bulk_in_endpoint[8];
1045         struct usb_endpoint_descriptor *bulk_out_endpoint[8];
1046         int control_out_endpoint;
1047
1048         int minor;
1049         int buffer_size;
1050         int i;
1051         int num_interrupt_in = 0;
1052         int num_bulk_in = 0;
1053         int num_bulk_out = 0;
1054
1055         interface = &dev->actconfig->interface[ifnum].altsetting[0];
1056         control_out_endpoint = interface->bInterfaceNumber;
1057
1058         /* find the endpoints that we need */
1059         for (i = 0; i < interface->bNumEndpoints; ++i) {
1060                 endpoint = &interface->endpoint[i];
1061
1062                 if ((endpoint->bEndpointAddress & 0x80) &&
1063                     ((endpoint->bmAttributes & 3) == 0x02)) {
1064                         /* we found a bulk in endpoint */
1065                         dbg("found bulk in");
1066                         bulk_in_endpoint[num_bulk_in] = endpoint;
1067                         ++num_bulk_in;
1068                 }
1069
1070                 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1071                     ((endpoint->bmAttributes & 3) == 0x02)) {
1072                         /* we found a bulk out endpoint */
1073                         dbg("found bulk out");
1074                         bulk_out_endpoint[num_bulk_out] = endpoint;
1075                         ++num_bulk_out;
1076                 }
1077
1078                 if ((endpoint->bEndpointAddress & 0x80) &&
1079                     ((endpoint->bmAttributes & 3) == 0x03)) {
1080                         /* we found a interrupt in endpoint */
1081                         dbg("found interrupt in");
1082                         interrupt_in_endpoint[num_interrupt_in] = endpoint;
1083                         ++num_interrupt_in;
1084                 }
1085         }
1086
1087         /* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */
1088         if ((num_bulk_in != 1) ||
1089             (num_bulk_out != 1) ||
1090             (num_interrupt_in != 1)) {
1091                 dbg (__FUNCTION__ " - improper number of endpoints. Bluetooth driver not bound.");
1092                 return NULL;
1093         }
1094
1095         MOD_INC_USE_COUNT;
1096         info("USB Bluetooth converter detected");
1097
1098         for (minor = 0; minor < BLUETOOTH_TTY_MINORS && bluetooth_table[minor]; ++minor)
1099                 ;
1100         if (bluetooth_table[minor]) {
1101                 err("No more free Bluetooth devices");
1102                 MOD_DEC_USE_COUNT;
1103                 return NULL;
1104         }
1105
1106         if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) {
1107                 err("Out of memory");
1108                 MOD_DEC_USE_COUNT;
1109                 return NULL;
1110         }
1111
1112         memset(bluetooth, 0, sizeof(struct usb_bluetooth));
1113
1114         bluetooth->magic = USB_BLUETOOTH_MAGIC;
1115         bluetooth->dev = dev;
1116         bluetooth->minor = minor;
1117         bluetooth->tqueue.routine = bluetooth_softint;
1118         bluetooth->tqueue.data = bluetooth;
1119         init_MUTEX(&bluetooth->lock);
1120
1121         /* record the interface number for the control out */
1122         bluetooth->control_out_bInterfaceNum = control_out_endpoint;
1123         
1124         /* create our control out urb pool */ 
1125         for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1126                 struct urb  *urb = usb_alloc_urb(0);
1127                 if (urb == NULL) {
1128                         err("No free urbs available");
1129                         goto probe_error;
1130                 }
1131                 urb->transfer_buffer = NULL;
1132                 bluetooth->control_urb_pool[i] = urb;
1133         }
1134
1135         /* set up the endpoint information */
1136         endpoint = bulk_in_endpoint[0];
1137         bluetooth->read_urb = usb_alloc_urb (0);
1138         if (!bluetooth->read_urb) {
1139                 err("No free urbs available");
1140                 goto probe_error;
1141         }
1142         bluetooth->bulk_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1143         bluetooth->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1144         bluetooth->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1145         if (!bluetooth->bulk_in_buffer) {
1146                 err("Couldn't allocate bulk_in_buffer");
1147                 goto probe_error;
1148         }
1149         FILL_BULK_URB(bluetooth->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
1150                       bluetooth->bulk_in_buffer, buffer_size, bluetooth_read_bulk_callback, bluetooth);
1151
1152         endpoint = bulk_out_endpoint[0];
1153         bluetooth->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1154         
1155         /* create our write urb pool */ 
1156         for (i = 0; i < NUM_BULK_URBS; ++i) {
1157                 struct urb  *urb = usb_alloc_urb(0);
1158                 if (urb == NULL) {
1159                         err("No free urbs available");
1160                         goto probe_error;
1161                 }
1162                 urb->transfer_buffer = kmalloc (bluetooth->bulk_out_buffer_size, GFP_KERNEL);
1163                 if (urb->transfer_buffer == NULL) {
1164                         err("out of memory");
1165                         goto probe_error;
1166                 }
1167                 bluetooth->write_urb_pool[i] = urb;
1168         }
1169         
1170         bluetooth->bulk_out_buffer_size = endpoint->wMaxPacketSize * 2;
1171
1172         endpoint = interrupt_in_endpoint[0];
1173         bluetooth->interrupt_in_urb = usb_alloc_urb(0);
1174         if (!bluetooth->interrupt_in_urb) {
1175                 err("No free urbs available");
1176                 goto probe_error;
1177         }
1178         bluetooth->interrupt_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1179         bluetooth->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1180         bluetooth->interrupt_in_interval = endpoint->bInterval;
1181         bluetooth->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1182         if (!bluetooth->interrupt_in_buffer) {
1183                 err("Couldn't allocate interrupt_in_buffer");
1184                 goto probe_error;
1185         }
1186         FILL_INT_URB(bluetooth->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1187                      bluetooth->interrupt_in_buffer, buffer_size, bluetooth_int_callback,
1188                      bluetooth, endpoint->bInterval);
1189
1190         /* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */
1191         tty_register_devfs (&bluetooth_tty_driver, 0, minor);
1192         info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor, minor);
1193
1194         bluetooth_table[minor] = bluetooth;
1195
1196         return bluetooth; /* success */
1197
1198 probe_error:
1199         if (bluetooth->read_urb)
1200                 usb_free_urb (bluetooth->read_urb);
1201         if (bluetooth->bulk_in_buffer)
1202                 kfree (bluetooth->bulk_in_buffer);
1203         if (bluetooth->interrupt_in_urb)
1204                 usb_free_urb (bluetooth->interrupt_in_urb);
1205         if (bluetooth->interrupt_in_buffer)
1206                 kfree (bluetooth->interrupt_in_buffer);
1207         for (i = 0; i < NUM_BULK_URBS; ++i)
1208                 if (bluetooth->write_urb_pool[i]) {
1209                         if (bluetooth->write_urb_pool[i]->transfer_buffer)
1210                                 kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
1211                         usb_free_urb (bluetooth->write_urb_pool[i]);
1212                 }
1213         for (i = 0; i < NUM_CONTROL_URBS; ++i) 
1214                 if (bluetooth->control_urb_pool[i]) {
1215                         if (bluetooth->control_urb_pool[i]->transfer_buffer)
1216                                 kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1217                         usb_free_urb (bluetooth->control_urb_pool[i]);
1218                 }
1219
1220         bluetooth_table[minor] = NULL;
1221
1222         /* free up any memory that we allocated */
1223         kfree (bluetooth);
1224         MOD_DEC_USE_COUNT;
1225         return NULL;
1226 }
1227
1228
1229 static void usb_bluetooth_disconnect(struct usb_device *dev, void *ptr)
1230 {
1231         struct usb_bluetooth *bluetooth = (struct usb_bluetooth *) ptr;
1232         int i;
1233
1234         if (bluetooth) {
1235                 if ((bluetooth->open_count) && (bluetooth->tty))
1236                         tty_hangup(bluetooth->tty);
1237
1238                 bluetooth->open_count = 0;
1239
1240                 if (bluetooth->read_urb) {
1241                         usb_unlink_urb (bluetooth->read_urb);
1242                         usb_free_urb (bluetooth->read_urb);
1243                 }
1244                 if (bluetooth->bulk_in_buffer)
1245                         kfree (bluetooth->bulk_in_buffer);
1246
1247                 if (bluetooth->interrupt_in_urb) {
1248                         usb_unlink_urb (bluetooth->interrupt_in_urb);
1249                         usb_free_urb (bluetooth->interrupt_in_urb);
1250                 }
1251                 if (bluetooth->interrupt_in_buffer)
1252                         kfree (bluetooth->interrupt_in_buffer);
1253
1254                 tty_unregister_devfs (&bluetooth_tty_driver, bluetooth->minor);
1255
1256                 for (i = 0; i < NUM_BULK_URBS; ++i) {
1257                         if (bluetooth->write_urb_pool[i]) {
1258                                 usb_unlink_urb (bluetooth->write_urb_pool[i]);
1259                                 if (bluetooth->write_urb_pool[i]->transfer_buffer)
1260                                         kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
1261                                 usb_free_urb (bluetooth->write_urb_pool[i]);
1262                         }
1263                 }
1264                 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1265                         if (bluetooth->control_urb_pool[i]) {
1266                                 usb_unlink_urb (bluetooth->control_urb_pool[i]);
1267                                 if (bluetooth->control_urb_pool[i]->transfer_buffer)
1268                                         kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1269                                 usb_free_urb (bluetooth->control_urb_pool[i]);
1270                         }
1271                 }
1272                 
1273                 info("Bluetooth converter now disconnected from ttyUB%d", bluetooth->minor);
1274
1275                 bluetooth_table[bluetooth->minor] = NULL;
1276
1277                 /* free up any memory that we allocated */
1278                 kfree (bluetooth);
1279
1280         } else {
1281                 info("device disconnected");
1282         }
1283
1284         MOD_DEC_USE_COUNT;
1285 }
1286
1287
1288 static struct tty_driver bluetooth_tty_driver = {
1289         magic:                  TTY_DRIVER_MAGIC,
1290         driver_name:            "usb-bluetooth",
1291         name:                   "usb/ttub/%d",
1292         major:                  BLUETOOTH_TTY_MAJOR,
1293         minor_start:            0,
1294         num:                    BLUETOOTH_TTY_MINORS,
1295         type:                   TTY_DRIVER_TYPE_SERIAL,
1296         subtype:                SERIAL_TYPE_NORMAL,
1297         flags:                  TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1298
1299         refcount:               &bluetooth_refcount,
1300         table:                  bluetooth_tty,
1301         termios:                bluetooth_termios,
1302         termios_locked:         bluetooth_termios_locked,
1303
1304         open:                   bluetooth_open,
1305         close:                  bluetooth_close,
1306         write:                  bluetooth_write,
1307         write_room:             bluetooth_write_room,
1308         ioctl:                  bluetooth_ioctl,
1309         set_termios:            bluetooth_set_termios,
1310         throttle:               bluetooth_throttle,
1311         unthrottle:             bluetooth_unthrottle,
1312         chars_in_buffer:        bluetooth_chars_in_buffer,
1313 };
1314
1315
1316 int usb_bluetooth_init(void)
1317 {
1318         int i;
1319         int result;
1320
1321         /* Initalize our global data */
1322         for (i = 0; i < BLUETOOTH_TTY_MINORS; ++i) {
1323                 bluetooth_table[i] = NULL;
1324         }
1325
1326         info ("USB Bluetooth support registered");
1327
1328         /* register the tty driver */
1329         bluetooth_tty_driver.init_termios          = tty_std_termios;
1330         bluetooth_tty_driver.init_termios.c_cflag  = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1331         if (tty_register_driver (&bluetooth_tty_driver)) {
1332                 err(__FUNCTION__ " - failed to register tty driver");
1333                 return -1;
1334         }
1335
1336         /* register the USB driver */
1337         result = usb_register(&usb_bluetooth_driver);
1338         if (result < 0) {
1339                 tty_unregister_driver(&bluetooth_tty_driver);
1340                 err("usb_register failed for the USB bluetooth driver. Error number %d", result);
1341                 return -1;
1342         }
1343
1344         info(DRIVER_DESC " " DRIVER_VERSION);
1345
1346         return 0;
1347 }
1348
1349
1350 void usb_bluetooth_exit(void)
1351 {
1352         usb_deregister(&usb_bluetooth_driver);
1353         tty_unregister_driver(&bluetooth_tty_driver);
1354 }
1355
1356
1357 module_init(usb_bluetooth_init);
1358 module_exit(usb_bluetooth_exit);
1359
1360 /* Module information */
1361 MODULE_AUTHOR( DRIVER_AUTHOR );
1362 MODULE_DESCRIPTION( DRIVER_DESC );
1363 MODULE_LICENSE("GPL");
1364