--- /usr/src/linux/drivers/usb/bluetooth.orig Fri Nov 30 17:21:21 2001 +++ /usr/src/linux/drivers/usb/bluetooth.c Fri Nov 30 17:44:47 2001 @@ -200,6 +200,8 @@ unsigned char int_buffer[EVENT_BUFFER_SIZE]; unsigned int bulk_packet_pos; unsigned char bulk_buffer[ACL_BUFFER_SIZE]; /* 64k preallocated, fix? */ + struct semaphore lock; + int open_count; }; @@ -361,43 +363,47 @@ return -ENODEV; } - if (bluetooth->active) { - dbg (__FUNCTION__ " - device already open"); - return -EINVAL; - } + down (&bluetooth->lock); + + ++bluetooth->open_count; - /* set up our structure making the tty driver remember our object, and us it */ - tty->driver_data = bluetooth; - bluetooth->tty = tty; - - /* force low_latency on so that our tty_push actually forces the data through, - * otherwise it is scheduled, and with high data rates (like with OHCI) data - * can get lost. */ - bluetooth->tty->low_latency = 1; + if (!bluetooth->active) { + + /* set up our structure making the tty driver remember our object, and us it */ + tty->driver_data = bluetooth; + bluetooth->tty = tty; + + /* force low_latency on so that our tty_push actually forces the data through, + * otherwise it is scheduled, and with high data rates (like with OHCI) data + * can get lost. */ + bluetooth->tty->low_latency = 1; - bluetooth->active = 1; + bluetooth->active = 1; - /* Reset the packet position counters */ - bluetooth->int_packet_pos = 0; - bluetooth->bulk_packet_pos = 0; + /* Reset the packet position counters */ + bluetooth->int_packet_pos = 0; + bluetooth->bulk_packet_pos = 0; #ifndef BTBUGGYHARDWARE - /* Start reading from the device */ - FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev, + /* Start reading from the device */ + FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev, usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress), bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, bluetooth_read_bulk_callback, bluetooth); - result = usb_submit_urb(bluetooth->read_urb); - if (result) - dbg(__FUNCTION__ " - usb_submit_urb(read bulk) failed with status %d", result); + result = usb_submit_urb(bluetooth->read_urb); + if (result) + dbg(__FUNCTION__ " - usb_submit_urb(read bulk) failed with status %d", result); #endif - FILL_INT_URB(bluetooth->interrupt_in_urb, bluetooth->dev, + FILL_INT_URB(bluetooth->interrupt_in_urb, bluetooth->dev, usb_rcvintpipe(bluetooth->dev, bluetooth->interrupt_in_endpointAddress), bluetooth->interrupt_in_buffer, bluetooth->interrupt_in_buffer_size, bluetooth_int_callback, bluetooth, bluetooth->interrupt_in_interval); - result = usb_submit_urb(bluetooth->interrupt_in_urb); - if (result) - dbg(__FUNCTION__ " - usb_submit_urb(interrupt in) failed with status %d", result); + result = usb_submit_urb(bluetooth->interrupt_in_urb); + if (result) + dbg(__FUNCTION__ " - usb_submit_urb(interrupt in) failed with status %d", result); + } + + up(&bluetooth->lock); return 0; } @@ -419,13 +425,22 @@ return; } + down (&bluetooth->lock); + + --bluetooth->open_count; + + if (bluetooth->open_count <= 0) { + /* shutdown any bulk reads and writes that might be going on */ - for (i = 0; i < NUM_BULK_URBS; ++i) - usb_unlink_urb (bluetooth->write_urb_pool[i]); - usb_unlink_urb (bluetooth->read_urb); - usb_unlink_urb (bluetooth->interrupt_in_urb); + for (i = 0; i < NUM_BULK_URBS; ++i) + usb_unlink_urb (bluetooth->write_urb_pool[i]); + usb_unlink_urb (bluetooth->read_urb); + usb_unlink_urb (bluetooth->interrupt_in_urb); - bluetooth->active = 0; + bluetooth->active = 0; + bluetooth->open_count = 0; + } + up(&bluetooth->lock); } @@ -1102,6 +1117,7 @@ bluetooth->minor = minor; bluetooth->tqueue.routine = bluetooth_softint; bluetooth->tqueue.data = bluetooth; + init_MUTEX(&bluetooth->lock); /* record the interface number for the control out */ bluetooth->control_out_bInterfaceNum = control_out_endpoint;