--- /usr/src/linux/drivers/usb/bluetooth.orig Wed Nov 28 18:43:58 2001 +++ /usr/src/linux/drivers/usb/bluetooth.c Thu Nov 29 19:57:26 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,10 +363,11 @@ return -ENODEV; } - if (bluetooth->active) { - dbg (__FUNCTION__ " - device already open"); - return -EINVAL; - } + down (&bluetooth->lock); + + ++bluetooth->open_count; + + if (!bluetooth->active) { /* set up our structure making the tty driver remember our object, and us it */ tty->driver_data = bluetooth; @@ -398,6 +401,9 @@ 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;