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 
167 struct usb_bluetooth {
168 	int			magic;
169 	struct usb_device *	dev;
170 	struct tty_driver *	tty_driver;	/* the tty_driver for this device */
171 	struct tty_struct *	tty;		/* the coresponding tty for this port */
172 
173 	unsigned char		minor;		/* the starting minor number for this device */
174 	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 */
209 static int  bluetooth_open		(struct tty_struct *tty, struct file *filp);
210 static void bluetooth_close		(struct tty_struct *tty, struct file *filp);
211 static int  bluetooth_write		(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
212 static int  bluetooth_write_room	(struct tty_struct *tty);
213 static int  bluetooth_chars_in_buffer	(struct tty_struct *tty);
214 static void bluetooth_throttle		(struct tty_struct *tty);
215 static void bluetooth_unthrottle	(struct tty_struct *tty);
216 static int  bluetooth_ioctl		(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
217 static void bluetooth_set_termios	(struct tty_struct *tty, struct termios *old);
218 
219 static void bluetooth_int_callback		(struct urb *urb);
220 static void bluetooth_ctrl_callback		(struct urb *urb);
221 static void bluetooth_read_bulk_callback	(struct urb *urb);
222 static void bluetooth_write_bulk_callback	(struct urb *urb);
223 
224 static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
225 			 	  const struct usb_device_id *id);
226 static void usb_bluetooth_disconnect	(struct usb_device *dev, void *ptr);
227 
228 
229 static 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 
234 MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
235 
236 static 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 
243 static int			bluetooth_refcount;
244 static struct tty_driver	bluetooth_tty_driver;
245 static struct tty_struct *	bluetooth_tty[BLUETOOTH_TTY_MINORS];
246 static struct termios *		bluetooth_termios[BLUETOOTH_TTY_MINORS];
247 static struct termios *		bluetooth_termios_locked[BLUETOOTH_TTY_MINORS];
248 static struct usb_bluetooth	*bluetooth_table[BLUETOOTH_TTY_MINORS];
249 
250 
bluetooth_paranoia_check(struct usb_bluetooth * bluetooth,const char * function)251 static 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 
get_usb_bluetooth(struct usb_bluetooth * bluetooth,const char * function)266 static 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 
get_bluetooth_by_minor(int minor)279 static inline struct usb_bluetooth *get_bluetooth_by_minor (int minor)
280 {
281 	return bluetooth_table[minor];
282 }
283 
284 
bluetooth_ctrl_msg(struct usb_bluetooth * bluetooth,int request,int value,const unsigned char * buf,int len)285 static 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  *****************************************************************************/
bluetooth_open(struct tty_struct * tty,struct file * filp)349 static 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 
bluetooth_close(struct tty_struct * tty,struct file * filp)411 static 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 
bluetooth_write(struct tty_struct * tty,int from_user,const unsigned char * buf,int count)443 static 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 
569 exit:
570 	if (temp_buffer != NULL)
571 		kfree (temp_buffer);
572 
573 	return retval;
574 }
575 
576 
bluetooth_write_room(struct tty_struct * tty)577 static 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 
bluetooth_chars_in_buffer(struct tty_struct * tty)605 static 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 
bluetooth_throttle(struct tty_struct * tty)631 static 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 
bluetooth_unthrottle(struct tty_struct * tty)652 static 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 
bluetooth_ioctl(struct tty_struct * tty,struct file * file,unsigned int cmd,unsigned long arg)671 static 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 
bluetooth_set_termios(struct tty_struct * tty,struct termios * old)691 static 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
btusb_enable_bulk_read(struct tty_struct * tty)713 void 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 
btusb_disable_bulk_read(struct tty_struct * tty)739 void 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 
bluetooth_int_callback(struct urb * urb)764 static 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 
bluetooth_ctrl_callback(struct urb * urb)858 static 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 
bluetooth_read_bulk_callback(struct urb * urb)876 static 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 
977 exit:
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 
bluetooth_write_bulk_callback(struct urb * urb)993 static 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 
bluetooth_softint(void * private)1016 static 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 	struct tty_ldisc *ld;
1021 
1022 	dbg("%s", __FUNCTION__);
1023 
1024 	if (!bluetooth) {
1025 		return;
1026 	}
1027 
1028 	tty = bluetooth->tty;
1029 	if (tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) {
1030 		ld = tty_ldisc_ref(tty);
1031 		if(ld) {
1032 			if(ld->write_wakeup) {
1033 				ld->write_wakeup(tty);
1034 				dbg("%s - write wakeup call.", __FUNCTION__);
1035 			}
1036 			tty_ldisc_deref(tty);
1037 		}
1038 	}
1039 
1040 	wake_up_interruptible(&tty->write_wait);
1041 }
1042 
1043 
usb_bluetooth_probe(struct usb_device * dev,unsigned int ifnum,const struct usb_device_id * id)1044 static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
1045 			 	  const struct usb_device_id *id)
1046 {
1047 	struct usb_bluetooth *bluetooth = NULL;
1048 	struct usb_interface_descriptor *interface;
1049 	struct usb_endpoint_descriptor *endpoint;
1050 	struct usb_endpoint_descriptor *interrupt_in_endpoint[8];
1051 	struct usb_endpoint_descriptor *bulk_in_endpoint[8];
1052 	struct usb_endpoint_descriptor *bulk_out_endpoint[8];
1053 	int control_out_endpoint;
1054 
1055 	int minor;
1056 	int buffer_size;
1057 	int i;
1058 	int num_interrupt_in = 0;
1059 	int num_bulk_in = 0;
1060 	int num_bulk_out = 0;
1061 
1062 	interface = &dev->actconfig->interface[ifnum].altsetting[0];
1063 	control_out_endpoint = interface->bInterfaceNumber;
1064 
1065 	/* find the endpoints that we need */
1066 	for (i = 0; i < interface->bNumEndpoints; ++i) {
1067 		endpoint = &interface->endpoint[i];
1068 
1069 		if ((endpoint->bEndpointAddress & 0x80) &&
1070 		    ((endpoint->bmAttributes & 3) == 0x02)) {
1071 			/* we found a bulk in endpoint */
1072 			dbg("found bulk in");
1073 			bulk_in_endpoint[num_bulk_in] = endpoint;
1074 			++num_bulk_in;
1075 		}
1076 
1077 		if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1078 		    ((endpoint->bmAttributes & 3) == 0x02)) {
1079 			/* we found a bulk out endpoint */
1080 			dbg("found bulk out");
1081 			bulk_out_endpoint[num_bulk_out] = endpoint;
1082 			++num_bulk_out;
1083 		}
1084 
1085 		if ((endpoint->bEndpointAddress & 0x80) &&
1086 		    ((endpoint->bmAttributes & 3) == 0x03)) {
1087 			/* we found a interrupt in endpoint */
1088 			dbg("found interrupt in");
1089 			interrupt_in_endpoint[num_interrupt_in] = endpoint;
1090 			++num_interrupt_in;
1091 		}
1092 	}
1093 
1094 	/* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */
1095 	if ((num_bulk_in != 1) ||
1096 	    (num_bulk_out != 1) ||
1097 	    (num_interrupt_in != 1)) {
1098 		dbg ("%s - improper number of endpoints. Bluetooth driver not bound.", __FUNCTION__);
1099 		return NULL;
1100 	}
1101 
1102 	MOD_INC_USE_COUNT;
1103 	info("USB Bluetooth converter detected");
1104 
1105 	for (minor = 0; minor < BLUETOOTH_TTY_MINORS && bluetooth_table[minor]; ++minor)
1106 		;
1107 	if (bluetooth_table[minor]) {
1108 		err("No more free Bluetooth devices");
1109 		MOD_DEC_USE_COUNT;
1110 		return NULL;
1111 	}
1112 
1113 	if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) {
1114 		err("Out of memory");
1115 		MOD_DEC_USE_COUNT;
1116 		return NULL;
1117 	}
1118 
1119 	memset(bluetooth, 0, sizeof(struct usb_bluetooth));
1120 
1121 	bluetooth->magic = USB_BLUETOOTH_MAGIC;
1122 	bluetooth->dev = dev;
1123 	bluetooth->minor = minor;
1124 	bluetooth->tqueue.routine = bluetooth_softint;
1125 	bluetooth->tqueue.data = bluetooth;
1126 	init_MUTEX(&bluetooth->lock);
1127 
1128 	/* record the interface number for the control out */
1129 	bluetooth->control_out_bInterfaceNum = control_out_endpoint;
1130 
1131 	/* create our control out urb pool */
1132 	for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1133 		struct urb  *urb = usb_alloc_urb(0);
1134 		if (urb == NULL) {
1135 			err("No free urbs available");
1136 			goto probe_error;
1137 		}
1138 		urb->transfer_buffer = NULL;
1139 		bluetooth->control_urb_pool[i] = urb;
1140 	}
1141 
1142 	/* set up the endpoint information */
1143 	endpoint = bulk_in_endpoint[0];
1144 	bluetooth->read_urb = usb_alloc_urb (0);
1145 	if (!bluetooth->read_urb) {
1146 		err("No free urbs available");
1147 		goto probe_error;
1148 	}
1149 	bluetooth->bulk_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1150 	bluetooth->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1151 	bluetooth->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1152 	if (!bluetooth->bulk_in_buffer) {
1153 		err("Couldn't allocate bulk_in_buffer");
1154 		goto probe_error;
1155 	}
1156 	FILL_BULK_URB(bluetooth->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
1157 		      bluetooth->bulk_in_buffer, buffer_size, bluetooth_read_bulk_callback, bluetooth);
1158 
1159 	endpoint = bulk_out_endpoint[0];
1160 	bluetooth->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1161 	bluetooth->bulk_out_buffer_size = endpoint->wMaxPacketSize * 2;
1162 
1163 	/* create our write urb pool */
1164 	for (i = 0; i < NUM_BULK_URBS; ++i) {
1165 		struct urb  *urb = usb_alloc_urb(0);
1166 		if (urb == NULL) {
1167 			err("No free urbs available");
1168 			goto probe_error;
1169 		}
1170 		urb->transfer_buffer = kmalloc (bluetooth->bulk_out_buffer_size, GFP_KERNEL);
1171 		if (urb->transfer_buffer == NULL) {
1172 			err("out of memory");
1173 			goto probe_error;
1174 		}
1175 		bluetooth->write_urb_pool[i] = urb;
1176 	}
1177 
1178 	endpoint = interrupt_in_endpoint[0];
1179 	bluetooth->interrupt_in_urb = usb_alloc_urb(0);
1180 	if (!bluetooth->interrupt_in_urb) {
1181 		err("No free urbs available");
1182 		goto probe_error;
1183 	}
1184 	bluetooth->interrupt_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1185 	bluetooth->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1186 	bluetooth->interrupt_in_interval = endpoint->bInterval;
1187 	bluetooth->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1188 	if (!bluetooth->interrupt_in_buffer) {
1189 		err("Couldn't allocate interrupt_in_buffer");
1190 		goto probe_error;
1191 	}
1192 	FILL_INT_URB(bluetooth->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1193 		     bluetooth->interrupt_in_buffer, buffer_size, bluetooth_int_callback,
1194 		     bluetooth, endpoint->bInterval);
1195 
1196 	/* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */
1197 	tty_register_devfs (&bluetooth_tty_driver, 0, minor);
1198 	info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor, minor);
1199 
1200 	bluetooth_table[minor] = bluetooth;
1201 
1202 	return bluetooth; /* success */
1203 
1204 probe_error:
1205 	if (bluetooth->read_urb)
1206 		usb_free_urb (bluetooth->read_urb);
1207 	if (bluetooth->bulk_in_buffer)
1208 		kfree (bluetooth->bulk_in_buffer);
1209 	if (bluetooth->interrupt_in_urb)
1210 		usb_free_urb (bluetooth->interrupt_in_urb);
1211 	if (bluetooth->interrupt_in_buffer)
1212 		kfree (bluetooth->interrupt_in_buffer);
1213 	for (i = 0; i < NUM_BULK_URBS; ++i)
1214 		if (bluetooth->write_urb_pool[i]) {
1215 			if (bluetooth->write_urb_pool[i]->transfer_buffer)
1216 				kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
1217 			usb_free_urb (bluetooth->write_urb_pool[i]);
1218 		}
1219 	for (i = 0; i < NUM_CONTROL_URBS; ++i)
1220 		if (bluetooth->control_urb_pool[i]) {
1221 			if (bluetooth->control_urb_pool[i]->transfer_buffer)
1222 				kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1223 			usb_free_urb (bluetooth->control_urb_pool[i]);
1224 		}
1225 
1226 	bluetooth_table[minor] = NULL;
1227 
1228 	/* free up any memory that we allocated */
1229 	kfree (bluetooth);
1230 	MOD_DEC_USE_COUNT;
1231 	return NULL;
1232 }
1233 
1234 
usb_bluetooth_disconnect(struct usb_device * dev,void * ptr)1235 static void usb_bluetooth_disconnect(struct usb_device *dev, void *ptr)
1236 {
1237 	struct usb_bluetooth *bluetooth = (struct usb_bluetooth *) ptr;
1238 	int i;
1239 
1240 	if (bluetooth) {
1241 		if ((bluetooth->open_count) && (bluetooth->tty))
1242 			tty_hangup(bluetooth->tty);
1243 
1244 		bluetooth->open_count = 0;
1245 
1246 		if (bluetooth->read_urb) {
1247 			usb_unlink_urb (bluetooth->read_urb);
1248 			usb_free_urb (bluetooth->read_urb);
1249 		}
1250 		if (bluetooth->bulk_in_buffer)
1251 			kfree (bluetooth->bulk_in_buffer);
1252 
1253 		if (bluetooth->interrupt_in_urb) {
1254 			usb_unlink_urb (bluetooth->interrupt_in_urb);
1255 			usb_free_urb (bluetooth->interrupt_in_urb);
1256 		}
1257 		if (bluetooth->interrupt_in_buffer)
1258 			kfree (bluetooth->interrupt_in_buffer);
1259 
1260 		tty_unregister_devfs (&bluetooth_tty_driver, bluetooth->minor);
1261 
1262 		for (i = 0; i < NUM_BULK_URBS; ++i) {
1263 			if (bluetooth->write_urb_pool[i]) {
1264 				usb_unlink_urb (bluetooth->write_urb_pool[i]);
1265 				if (bluetooth->write_urb_pool[i]->transfer_buffer)
1266 					kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
1267 				usb_free_urb (bluetooth->write_urb_pool[i]);
1268 			}
1269 		}
1270 		for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1271 			if (bluetooth->control_urb_pool[i]) {
1272 				usb_unlink_urb (bluetooth->control_urb_pool[i]);
1273 				if (bluetooth->control_urb_pool[i]->transfer_buffer)
1274 					kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1275 				usb_free_urb (bluetooth->control_urb_pool[i]);
1276 			}
1277 		}
1278 
1279 		info("Bluetooth converter now disconnected from ttyUB%d", bluetooth->minor);
1280 
1281 		bluetooth_table[bluetooth->minor] = NULL;
1282 
1283 		/* free up any memory that we allocated */
1284 		kfree (bluetooth);
1285 
1286 	} else {
1287 		info("device disconnected");
1288 	}
1289 
1290 	MOD_DEC_USE_COUNT;
1291 }
1292 
1293 
1294 static struct tty_driver bluetooth_tty_driver = {
1295 	.magic =		TTY_DRIVER_MAGIC,
1296 	.driver_name =		"usb-bluetooth",
1297 	.name =			"usb/ttub/%d",
1298 	.major =		BLUETOOTH_TTY_MAJOR,
1299 	.minor_start =		0,
1300 	.num =			BLUETOOTH_TTY_MINORS,
1301 	.type =			TTY_DRIVER_TYPE_SERIAL,
1302 	.subtype =		SERIAL_TYPE_NORMAL,
1303 	.flags =		TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1304 
1305 	.refcount =		&bluetooth_refcount,
1306 	.table =		bluetooth_tty,
1307 	.termios =		bluetooth_termios,
1308 	.termios_locked =	bluetooth_termios_locked,
1309 
1310 	.open =			bluetooth_open,
1311 	.close =		bluetooth_close,
1312 	.write =		bluetooth_write,
1313 	.write_room =		bluetooth_write_room,
1314 	.ioctl =		bluetooth_ioctl,
1315 	.set_termios =		bluetooth_set_termios,
1316 	.throttle =		bluetooth_throttle,
1317 	.unthrottle =		bluetooth_unthrottle,
1318 	.chars_in_buffer =	bluetooth_chars_in_buffer,
1319 };
1320 
1321 
usb_bluetooth_init(void)1322 int usb_bluetooth_init(void)
1323 {
1324 	int i;
1325 	int result;
1326 
1327 	/* Initalize our global data */
1328 	for (i = 0; i < BLUETOOTH_TTY_MINORS; ++i) {
1329 		bluetooth_table[i] = NULL;
1330 	}
1331 
1332 	info ("USB Bluetooth support registered");
1333 
1334 	/* register the tty driver */
1335 	bluetooth_tty_driver.init_termios          = tty_std_termios;
1336 	bluetooth_tty_driver.init_termios.c_cflag  = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1337 	if (tty_register_driver (&bluetooth_tty_driver)) {
1338 		err("%s - failed to register tty driver", __FUNCTION__);
1339 		return -1;
1340 	}
1341 
1342 	/* register the USB driver */
1343 	result = usb_register(&usb_bluetooth_driver);
1344 	if (result < 0) {
1345 		tty_unregister_driver(&bluetooth_tty_driver);
1346 		err("usb_register failed for the USB bluetooth driver. Error number %d", result);
1347 		return -1;
1348 	}
1349 
1350 	info(DRIVER_DESC " " DRIVER_VERSION);
1351 
1352 	return 0;
1353 }
1354 
1355 
usb_bluetooth_exit(void)1356 void usb_bluetooth_exit(void)
1357 {
1358 	usb_deregister(&usb_bluetooth_driver);
1359 	tty_unregister_driver(&bluetooth_tty_driver);
1360 }
1361 
1362 
1363 module_init(usb_bluetooth_init);
1364 module_exit(usb_bluetooth_exit);
1365 
1366 /* Module information */
1367 MODULE_AUTHOR( DRIVER_AUTHOR );
1368 MODULE_DESCRIPTION( DRIVER_DESC );
1369 MODULE_LICENSE("GPL");
1370 
1371