~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/usb/bluetooth.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.