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