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