1 /*
2 * bluetooth.c Version 0.7
3 *
4 * Copyright (c) 2000 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (c) 2000 Mark Douglas Corner <mcorner@umich.edu>
6 *
7 * USB Bluetooth driver, based on the Bluetooth Spec version 1.0B
8 *
9 * (11/29/2000) Version 0.7 gkh
10 * Fixed problem with overrunning the tty flip buffer.
11 * Removed unneeded NULL pointer initialization.
12 *
13 * (10/05/2000) Version 0.6 gkh
14 * Fixed bug with urb->dev not being set properly, now that the usb
15 * core needs it.
16 * Got a real major id number and name.
17 *
18 * (08/06/2000) Version 0.5 gkh
19 * Fixed problem of not resubmitting the bulk read urb if there is
20 * an error in the callback. Ericsson devices seem to need this.
21 *
22 * (07/11/2000) Version 0.4 gkh
23 * Fixed bug in disconnect for when we call tty_hangup
24 * Fixed bug in bluetooth_ctrl_msg where the bluetooth struct was not
25 * getting attached to the control urb properly.
26 * Fixed bug in bluetooth_write where we pay attention to the result
27 * of bluetooth_ctrl_msg.
28 *
29 * (08/03/2000) Version 0.3 gkh mdc
30 * Merged in Mark's changes to make the driver play nice with the Axis
31 * stack.
32 * Made the write bulk use an urb pool to enable larger transfers with
33 * fewer calls to the driver.
34 * Fixed off by one bug in acl pkt receive
35 * Made packet counters specific to each bluetooth device
36 * Added checks for zero length callbacks
37 * Added buffers for int and bulk packets. Had to do this otherwise
38 * packet types could intermingle.
39 * Made a control urb pool for the control messages.
40 *
41 * (07/11/2000) Version 0.2 gkh
42 * Fixed a small bug found by Nils Faerber in the usb_bluetooth_probe
43 * function.
44 *
45 * (07/09/2000) Version 0.1 gkh
46 * Initial release. Has support for sending ACL data (which is really just
47 * a HCI frame.) Raw HCI commands and HCI events are not supported.
48 * A ioctl will probably be needed for the HCI commands and events in the
49 * future. All isoch endpoints are ignored at this time also.
50 * This driver should work for all currently shipping USB Bluetooth
51 * devices at this time :)
52 *
53 */
54
55 /*
56 * This program is free software; you can redistribute it and/or modify
57 * it under the terms of the GNU General Public License as published by
58 * the Free Software Foundation; either version 2 of the License, or
59 * (at your option) any later version.
60 *
61 * This program is distributed in the hope that it will be useful,
62 * but WITHOUT ANY WARRANTY; without even the implied warranty of
63 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 * GNU General Public License for more details.
65 *
66 * You should have received a copy of the GNU General Public License
67 * along with this program; if not, write to the Free Software
68 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
69 */
70
71
72 #include <linux/kernel.h>
73 #include <linux/sched.h>
74 #include <linux/signal.h>
75 #include <linux/errno.h>
76 #include <linux/poll.h>
77 #include <linux/init.h>
78 #include <linux/malloc.h>
79 #include <linux/fcntl.h>
80 #include <linux/tty_driver.h>
81 #include <linux/tty_flip.h>
82 #include <linux/tty.h>
83 #include <linux/module.h>
84
85 #define DEBUG
86 #include <linux/usb.h>
87
88 /* Module information */
89 MODULE_AUTHOR("Greg Kroah-Hartman, Mark Douglas Corner");
90 MODULE_DESCRIPTION("USB Bluetooth driver");
91
92 /* define this if you have hardware that is not good */
93 /*#define BTBUGGYHARDWARE */
94
95 /* Class, SubClass, and Protocol codes that describe a Bluetooth device */
96 #define WIRELESS_CLASS_CODE 0xe0
97 #define RF_SUBCLASS_CODE 0x01
98 #define BLUETOOTH_PROGRAMMING_PROTOCOL_CODE 0x01
99
100
101 #define BLUETOOTH_TTY_MAJOR 216 /* real device node major id */
102 #define BLUETOOTH_TTY_MINORS 256 /* whole lotta bluetooth devices */
103
104 #define USB_BLUETOOTH_MAGIC 0x6d02 /* magic number for bluetooth struct */
105
106 #define BLUETOOTH_CONTROL_REQUEST_TYPE 0x20
107
108 /* Bluetooth packet types */
109 #define CMD_PKT 0x01
110 #define ACL_PKT 0x02
111 #define SCO_PKT 0x03
112 #define EVENT_PKT 0x04
113 #define ERROR_PKT 0x05
114 #define NEG_PKT 0x06
115
116 /* Message sizes */
117 #define MAX_EVENT_SIZE 0xFF
118 #define EVENT_HDR_SIZE 3 /* 2 for the header + 1 for the type indicator */
119 #define EVENT_BUFFER_SIZE (MAX_EVENT_SIZE + EVENT_HDR_SIZE)
120
121 #define MAX_ACL_SIZE 0xFFFF
122 #define ACL_HDR_SIZE 5 /* 4 for the header + 1 for the type indicator */
123 #define ACL_BUFFER_SIZE (MAX_ACL_SIZE + ACL_HDR_SIZE)
124
125 /* parity check flag */
126 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
127
128 #define CHAR2INT16(c1,c0) (((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
129 #define MIN(a,b) (((a)<(b))?(a):(b))
130
131 #define NUM_BULK_URBS 24
132 #define NUM_CONTROL_URBS 16
133
134 struct usb_bluetooth {
135 int magic;
136 struct usb_device * dev;
137 struct tty_driver * tty_driver; /* the tty_driver for this device */
138 struct tty_struct * tty; /* the coresponding tty for this port */
139
140 unsigned char minor; /* the starting minor number for this device */
141 char active; /* someone has this device open */
142 int throttle; /* throttled by tty layer */
143
144 __u8 control_out_bInterfaceNum;
145 struct urb * control_urb_pool[NUM_CONTROL_URBS];
146 devrequest dr[NUM_CONTROL_URBS];
147
148 unsigned char * interrupt_in_buffer;
149 struct urb * interrupt_in_urb;
150 __u8 interrupt_in_endpointAddress;
151 __u8 interrupt_in_interval;
152 int interrupt_in_buffer_size;
153
154 unsigned char * bulk_in_buffer;
155 struct urb * read_urb;
156 __u8 bulk_in_endpointAddress;
157 int bulk_in_buffer_size;
158
159 int bulk_out_buffer_size;
160 struct urb * write_urb_pool[NUM_BULK_URBS];
161 __u8 bulk_out_endpointAddress;
162
163 wait_queue_head_t write_wait;
164
165 struct tq_struct tqueue; /* task queue for line discipline waking up */
166
167 unsigned int int_packet_pos;
168 unsigned char int_buffer[EVENT_BUFFER_SIZE];
169 unsigned int bulk_packet_pos;
170 unsigned char bulk_buffer[ACL_BUFFER_SIZE]; /* 64k preallocated, fix? */
171 };
172
173
174 /* local function prototypes */
175 static int bluetooth_open (struct tty_struct *tty, struct file *filp);
176 static void bluetooth_close (struct tty_struct *tty, struct file *filp);
177 static int bluetooth_write (struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
178 static int bluetooth_write_room (struct tty_struct *tty);
179 static int bluetooth_chars_in_buffer (struct tty_struct *tty);
180 static void bluetooth_throttle (struct tty_struct *tty);
181 static void bluetooth_unthrottle (struct tty_struct *tty);
182 static int bluetooth_ioctl (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
183 static void bluetooth_set_termios (struct tty_struct *tty, struct termios *old);
184
185 static void bluetooth_int_callback (struct urb *urb);
186 static void bluetooth_ctrl_callback (struct urb *urb);
187 static void bluetooth_read_bulk_callback (struct urb *urb);
188 static void bluetooth_write_bulk_callback (struct urb *urb);
189
190 static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
191 const struct usb_device_id *id);
192 static void usb_bluetooth_disconnect (struct usb_device *dev, void *ptr);
193
194
195 static struct usb_device_id usb_bluetooth_ids [] = {
196 { USB_DEVICE_INFO(WIRELESS_CLASS_CODE, RF_SUBCLASS_CODE, BLUETOOTH_PROGRAMMING_PROTOCOL_CODE) },
197 { } /* Terminating entry */
198 };
199
200 MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
201
202 static struct usb_driver usb_bluetooth_driver = {
203 name: "bluetooth",
204 probe: usb_bluetooth_probe,
205 disconnect: usb_bluetooth_disconnect,
206 id_table: usb_bluetooth_ids,
207 };
208
209 static int bluetooth_refcount;
210 static struct tty_driver bluetooth_tty_driver;
211 static struct tty_struct * bluetooth_tty[BLUETOOTH_TTY_MINORS];
212 static struct termios * bluetooth_termios[BLUETOOTH_TTY_MINORS];
213 static struct termios * bluetooth_termios_locked[BLUETOOTH_TTY_MINORS];
214 static struct usb_bluetooth *bluetooth_table[BLUETOOTH_TTY_MINORS];
215
216
217 static inline int bluetooth_paranoia_check (struct usb_bluetooth *bluetooth, const char *function)
218 {
219 if (!bluetooth) {
220 dbg("%s - bluetooth == NULL", function);
221 return -1;
222 }
223 if (bluetooth->magic != USB_BLUETOOTH_MAGIC) {
224 dbg("%s - bad magic number for bluetooth", function);
225 return -1;
226 }
227
228 return 0;
229 }
230
231
232 static inline struct usb_bluetooth* get_usb_bluetooth (struct usb_bluetooth *bluetooth, const char *function)
233 {
234 if (!bluetooth ||
235 bluetooth_paranoia_check (bluetooth, function)) {
236 /* then say that we dont have a valid usb_bluetooth thing, which will
237 * end up generating -ENODEV return values */
238 return NULL;
239 }
240
241 return bluetooth;
242 }
243
244
245 static inline struct usb_bluetooth *get_bluetooth_by_minor (int minor)
246 {
247 return bluetooth_table[minor];
248 }
249
250
251 static int bluetooth_ctrl_msg (struct usb_bluetooth *bluetooth, int request, int value, void *buf, int len)
252 {
253 struct urb *urb = NULL;
254 devrequest *dr = NULL;
255 int i;
256 int status;
257
258 dbg (__FUNCTION__);
259
260 /* try to find a free urb in our list */
261 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
262 if (bluetooth->control_urb_pool[i]->status != -EINPROGRESS) {
263 urb = bluetooth->control_urb_pool[i];
264 dr = &bluetooth->dr[i];
265 break;
266 }
267 }
268 if (urb == NULL) {
269 dbg (__FUNCTION__ " - no free urbs");
270 return -ENOMEM;
271 }
272
273 /* free up the last buffer that this urb used */
274 if (urb->transfer_buffer != NULL) {
275 kfree(urb->transfer_buffer);
276 urb->transfer_buffer = NULL;
277 }
278
279 dr->requesttype = BLUETOOTH_CONTROL_REQUEST_TYPE;
280 dr->request = request;
281 dr->value = cpu_to_le16p(&value);
282 dr->index = cpu_to_le16p(&bluetooth->control_out_bInterfaceNum);
283 dr->length = cpu_to_le16p(&len);
284
285 FILL_CONTROL_URB (urb, bluetooth->dev, usb_sndctrlpipe(bluetooth->dev, 0),
286 (unsigned char*)dr, buf, len, bluetooth_ctrl_callback, bluetooth);
287
288 /* send it down the pipe */
289 status = usb_submit_urb(urb);
290 if (status)
291 dbg(__FUNCTION__ " - usb_submit_urb(control) failed with status = %d", status);
292
293 return 0;
294 }
295
296
297
298
299
300 /*****************************************************************************
301 * Driver tty interface functions
302 *****************************************************************************/
303 static int bluetooth_open (struct tty_struct *tty, struct file * filp)
304 {
305 struct usb_bluetooth *bluetooth;
306 int result;
307
308 dbg(__FUNCTION__);
309
310 /* initialize the pointer incase something fails */
311 tty->driver_data = NULL;
312
313 /* get the bluetooth object associated with this tty pointer */
314 bluetooth = get_bluetooth_by_minor (MINOR(tty->device));
315
316 if (bluetooth_paranoia_check (bluetooth, __FUNCTION__)) {
317 return -ENODEV;
318 }
319
320 if (bluetooth->active) {
321 dbg (__FUNCTION__ " - device already open");
322 return -EINVAL;
323 }
324
325 /* set up our structure making the tty driver remember our object, and us it */
326 tty->driver_data = bluetooth;
327 bluetooth->tty = tty;
328
329 /* force low_latency on so that our tty_push actually forces the data through,
330 * otherwise it is scheduled, and with high data rates (like with OHCI) data
331 * can get lost. */
332 bluetooth->tty->low_latency = 1;
333
334 bluetooth->active = 1;
335
336 /* Reset the packet position counters */
337 bluetooth->int_packet_pos = 0;
338 bluetooth->bulk_packet_pos = 0;
339
340 #ifndef BTBUGGYHARDWARE
341 /* Start reading from the device */
342 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev,
343 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
344 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
345 bluetooth_read_bulk_callback, bluetooth);
346 result = usb_submit_urb(bluetooth->read_urb);
347 if (result)
348 dbg(__FUNCTION__ " - usb_submit_urb(read bulk) failed with status %d", result);
349 #endif
350 FILL_INT_URB(bluetooth->interrupt_in_urb, bluetooth->dev,
351 usb_rcvintpipe(bluetooth->dev, bluetooth->interrupt_in_endpointAddress),
352 bluetooth->interrupt_in_buffer, bluetooth->interrupt_in_buffer_size,
353 bluetooth_int_callback, bluetooth, bluetooth->interrupt_in_interval);
354 result = usb_submit_urb(bluetooth->interrupt_in_urb);
355 if (result)
356 dbg(__FUNCTION__ " - usb_submit_urb(interrupt in) failed with status %d", result);
357
358 return 0;
359 }
360
361
362 static void bluetooth_close (struct tty_struct *tty, struct file * filp)
363 {
364 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
365 int i;
366
367 if (!bluetooth) {
368 return;
369 }
370
371 dbg(__FUNCTION__);
372
373 if (!bluetooth->active) {
374 dbg (__FUNCTION__ " - device not opened");
375 return;
376 }
377
378 /* shutdown any bulk reads and writes that might be going on */
379 for (i = 0; i < NUM_BULK_URBS; ++i)
380 usb_unlink_urb (bluetooth->write_urb_pool[i]);
381 usb_unlink_urb (bluetooth->read_urb);
382
383 bluetooth->active = 0;
384 }
385
386
387 static int bluetooth_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
388 {
389 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
390 struct urb *urb = NULL;
391 unsigned char *new_buffer;
392 const unsigned char *current_position;
393 int status;
394 int bytes_sent;
395 int buffer_size;
396 int i;
397
398 if (!bluetooth) {
399 return -ENODEV;
400 }
401
402 dbg(__FUNCTION__ " - %d byte(s)", count);
403
404 if (!bluetooth->active) {
405 dbg (__FUNCTION__ " - device not opened");
406 return -EINVAL;
407 }
408
409 if (count == 0) {
410 dbg(__FUNCTION__ " - write request of 0 bytes");
411 return 0;
412 }
413 if (count == 1) {
414 dbg(__FUNCTION__ " - write request only included type %d", buf[0]);
415 return 1;
416 }
417
418 #ifdef DEBUG
419 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ " - length = %d, data = ", count);
420 for (i = 0; i < count; ++i) {
421 printk ("%.2x ", buf[i]);
422 }
423 printk ("\n");
424 #endif
425
426 switch (*buf) {
427 /* First byte indicates the type of packet */
428 case CMD_PKT:
429 /* dbg(__FUNCTION__ "- Send cmd_pkt len:%d", count);*/
430
431 if (in_interrupt()){
432 printk("cmd_pkt from interrupt!\n");
433 return count;
434 }
435
436 new_buffer = kmalloc (count-1, GFP_KERNEL);
437
438 if (!new_buffer) {
439 err (__FUNCTION__ "- out of memory.");
440 return -ENOMEM;
441 }
442
443 if (from_user)
444 copy_from_user (new_buffer, buf+1, count-1);
445 else
446 memcpy (new_buffer, buf+1, count-1);
447
448 if (bluetooth_ctrl_msg (bluetooth, 0x00, 0x00, new_buffer, count-1) != 0) {
449 kfree (new_buffer);
450 return 0;
451 }
452
453 /* need to free new_buffer somehow... FIXME */
454 return count;
455
456 case ACL_PKT:
457 current_position = buf;
458 ++current_position;
459 --count;
460 bytes_sent = 0;
461
462 while (count > 0) {
463 urb = NULL;
464
465 /* try to find a free urb in our list */
466 for (i = 0; i < NUM_BULK_URBS; ++i) {
467 if (bluetooth->write_urb_pool[i]->status != -EINPROGRESS) {
468 urb = bluetooth->write_urb_pool[i];
469 break;
470 }
471 }
472 if (urb == NULL) {
473 dbg (__FUNCTION__ " - no free urbs");
474 return bytes_sent;
475 }
476
477 /* free up the last buffer that this urb used */
478 if (urb->transfer_buffer != NULL) {
479 kfree(urb->transfer_buffer);
480 urb->transfer_buffer = NULL;
481 }
482
483 buffer_size = MIN (count, bluetooth->bulk_out_buffer_size);
484
485 new_buffer = kmalloc (buffer_size, GFP_KERNEL);
486 if (new_buffer == NULL) {
487 err(__FUNCTION__" no more kernel memory...");
488 return bytes_sent;
489 }
490
491 if (from_user)
492 copy_from_user(new_buffer, current_position, buffer_size);
493 else
494 memcpy (new_buffer, current_position, buffer_size);
495
496 /* build up our urb */
497 FILL_BULK_URB (urb, bluetooth->dev, usb_sndbulkpipe(bluetooth->dev, bluetooth->bulk_out_endpointAddress),
498 new_buffer, buffer_size, bluetooth_write_bulk_callback, bluetooth);
499 urb->transfer_flags |= USB_QUEUE_BULK;
500
501 /* send it down the pipe */
502 status = usb_submit_urb(urb);
503 if (status)
504 dbg(__FUNCTION__ " - usb_submit_urb(write bulk) failed with status = %d", status);
505 #ifdef BTBUGGYHARDWARE
506 /* A workaround for the stalled data bug */
507 /* May or may not be needed...*/
508 if (count != 0) {
509 udelay(500);
510 }
511 #endif
512 current_position += buffer_size;
513 bytes_sent += buffer_size;
514 count -= buffer_size;
515 }
516
517 return bytes_sent + 1;
518
519 default :
520 dbg(__FUNCTION__" - unsupported (at this time) write type");
521 }
522
523 return 0;
524 }
525
526
527 static int bluetooth_write_room (struct tty_struct *tty)
528 {
529 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
530 int room = 0;
531 int i;
532
533 if (!bluetooth) {
534 return -ENODEV;
535 }
536
537 dbg(__FUNCTION__);
538
539 if (!bluetooth->active) {
540 dbg (__FUNCTION__ " - device not open");
541 return -EINVAL;
542 }
543
544 for (i = 0; i < NUM_BULK_URBS; ++i) {
545 if (bluetooth->write_urb_pool[i]->status != -EINPROGRESS) {
546 room += bluetooth->bulk_out_buffer_size;
547 }
548 }
549
550 dbg(__FUNCTION__ " - returns %d", room);
551 return room;
552 }
553
554
555 static int bluetooth_chars_in_buffer (struct tty_struct *tty)
556 {
557 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
558 int chars = 0;
559 int i;
560
561 if (!bluetooth) {
562 return -ENODEV;
563 }
564
565 if (!bluetooth->active) {
566 dbg (__FUNCTION__ " - device not open");
567 return -EINVAL;
568 }
569
570 for (i = 0; i < NUM_BULK_URBS; ++i) {
571 if (bluetooth->write_urb_pool[i]->status == -EINPROGRESS) {
572 chars += bluetooth->write_urb_pool[i]->transfer_buffer_length;
573 }
574 }
575
576 dbg (__FUNCTION__ " - returns %d", chars);
577 return chars;
578 }
579
580
581 static void bluetooth_throttle (struct tty_struct * tty)
582 {
583 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
584
585 if (!bluetooth) {
586 return;
587 }
588
589 dbg(__FUNCTION__);
590
591 if (!bluetooth->active) {
592 dbg (__FUNCTION__ " - device not open");
593 return;
594 }
595
596 dbg(__FUNCTION__ " unsupported (at this time)");
597
598 return;
599 }
600
601
602 static void bluetooth_unthrottle (struct tty_struct * tty)
603 {
604 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
605
606 if (!bluetooth) {
607 return;
608 }
609
610 dbg(__FUNCTION__);
611
612 if (!bluetooth->active) {
613 dbg (__FUNCTION__ " - device not open");
614 return;
615 }
616
617 dbg(__FUNCTION__ " unsupported (at this time)");
618 }
619
620
621 static int bluetooth_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
622 {
623 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
624
625 if (!bluetooth) {
626 return -ENODEV;
627 }
628
629 dbg(__FUNCTION__ " - cmd 0x%.4x", cmd);
630
631 if (!bluetooth->active) {
632 dbg (__FUNCTION__ " - device not open");
633 return -ENODEV;
634 }
635
636 /* FIXME!!! */
637 return -ENOIOCTLCMD;
638 }
639
640
641 static void bluetooth_set_termios (struct tty_struct *tty, struct termios * old)
642 {
643 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
644
645 if (!bluetooth) {
646 return;
647 }
648
649 dbg(__FUNCTION__);
650
651 if (!bluetooth->active) {
652 dbg (__FUNCTION__ " - device not open");
653 return;
654 }
655
656 /* FIXME!!! */
657
658 return;
659 }
660
661
662 #ifdef BTBUGGYHARDWARE
663 void btusb_enable_bulk_read(struct tty_struct *tty){
664 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
665 int result;
666
667 if (!bluetooth) {
668 return;
669 }
670
671 dbg(__FUNCTION__);
672
673 if (!bluetooth->active) {
674 dbg (__FUNCTION__ " - device not open");
675 return;
676 }
677
678 if (bluetooth->read_urb) {
679 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev,
680 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
681 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
682 bluetooth_read_bulk_callback, bluetooth);
683 result = usb_submit_urb(bluetooth->read_urb);
684 if (result)
685 err (__FUNCTION__ " - failed submitting read urb, error %d", result);
686 }
687 }
688
689 void btusb_disable_bulk_read(struct tty_struct *tty){
690 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
691
692 if (!bluetooth) {
693 return;
694 }
695
696 dbg(__FUNCTION__);
697
698 if (!bluetooth->active) {
699 dbg (__FUNCTION__ " - device not open");
700 return;
701 }
702
703 if ((bluetooth->read_urb) && (bluetooth->read_urb->actual_length))
704 usb_unlink_urb(bluetooth->read_urb);
705 }
706 #endif
707
708
709 /*****************************************************************************
710 * urb callback functions
711 *****************************************************************************/
712
713
714 static void bluetooth_int_callback (struct urb *urb)
715 {
716 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
717 unsigned char *data = urb->transfer_buffer;
718 unsigned int i;
719 unsigned int count = urb->actual_length;
720 unsigned int packet_size;
721
722 dbg(__FUNCTION__);
723
724 if (!bluetooth) {
725 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
726 return;
727 }
728
729 if (urb->status) {
730 dbg(__FUNCTION__ " - nonzero int status received: %d", urb->status);
731 return;
732 }
733
734 if (!count) {
735 dbg(__FUNCTION__ " - zero length int");
736 return;
737 }
738
739
740 #ifdef DEBUG
741 if (count) {
742 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ "- length = %d, data = ", count);
743 for (i = 0; i < count; ++i) {
744 printk ("%.2x ", data[i]);
745 }
746 printk ("\n");
747 }
748 #endif
749
750 #ifdef BTBUGGYHARDWARE
751 if ((count >= 2) && (data[0] == 0xFF) && (data[1] == 0x00)) {
752 data += 2;
753 count -= 2;
754 }
755 if (count == 0) {
756 urb->actual_length = 0;
757 return;
758 }
759 #endif
760 /* We add a packet type identifier to the beginning of each
761 HCI frame. This makes the data in the tty look like a
762 serial USB devices. Each HCI frame can be broken across
763 multiple URBs so we buffer them until we have a full hci
764 packet */
765
766 if (!bluetooth->int_packet_pos) {
767 bluetooth->int_buffer[0] = EVENT_PKT;
768 bluetooth->int_packet_pos++;
769 }
770
771 if (bluetooth->int_packet_pos + count > EVENT_BUFFER_SIZE) {
772 err(__FUNCTION__ " - exceeded EVENT_BUFFER_SIZE");
773 bluetooth->int_packet_pos = 0;
774 return;
775 }
776
777 memcpy (&bluetooth->int_buffer[bluetooth->int_packet_pos],
778 urb->transfer_buffer, count);
779 bluetooth->int_packet_pos += count;
780 urb->actual_length = 0;
781
782 if (bluetooth->int_packet_pos >= EVENT_HDR_SIZE)
783 packet_size = bluetooth->int_buffer[2];
784 else
785 return;
786
787 if (packet_size + EVENT_HDR_SIZE < bluetooth->int_packet_pos) {
788 err(__FUNCTION__ " - packet was too long");
789 bluetooth->int_packet_pos = 0;
790 return;
791 }
792
793 if (packet_size + EVENT_HDR_SIZE == bluetooth->int_packet_pos) {
794 for (i = 0; i < bluetooth->int_packet_pos; ++i) {
795 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them */
796 if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
797 tty_flip_buffer_push(bluetooth->tty);
798 }
799 tty_insert_flip_char(bluetooth->tty, bluetooth->int_buffer[i], 0);
800 }
801 tty_flip_buffer_push(bluetooth->tty);
802
803 bluetooth->int_packet_pos = 0;
804 }
805 }
806
807
808 static void bluetooth_ctrl_callback (struct urb *urb)
809 {
810 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
811
812 dbg(__FUNCTION__);
813
814 if (!bluetooth) {
815 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
816 return;
817 }
818
819 if (urb->status) {
820 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
821 return;
822 }
823 }
824
825
826 static void bluetooth_read_bulk_callback (struct urb *urb)
827 {
828 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
829 unsigned char *data = urb->transfer_buffer;
830 unsigned int count = urb->actual_length;
831 unsigned int i;
832 unsigned int packet_size;
833 int result;
834
835 #ifdef BTBUGGYHARDWARE
836 if ((count == 4) && (data[0] == 0x00) && (data[1] == 0x00)
837 && (data[2] == 0x00) && (data[3] == 0x00)) {
838 urb->actual_length = 0;
839 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev,
840 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
841 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
842 bluetooth_read_bulk_callback, bluetooth);
843 result = usb_submit_urb(bluetooth->read_urb);
844 if (result)
845 err (__FUNCTION__ " - failed resubmitting read urb, error %d", result);
846
847 return;
848 }
849 #endif
850
851 dbg(__FUNCTION__);
852
853 if (!bluetooth) {
854 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
855 goto exit;
856 }
857
858 if (urb->status) {
859 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
860 goto exit;
861 }
862
863 if (!count) {
864 dbg(__FUNCTION__ " - zero length read bulk");
865 goto exit;
866 }
867
868 #ifdef DEBUG
869 if (count) {
870 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ "- length = %d, data = ", count);
871 for (i = 0; i < count; ++i) {
872 printk ("%.2x ", data[i]);
873 }
874 printk ("\n");
875 }
876 #endif
877 /* We add a packet type identifier to the beginning of each
878 HCI frame. This makes the data in the tty look like a
879 serial USB devices. Each HCI frame can be broken across
880 multiple URBs so we buffer them until we have a full hci
881 packet */
882
883 if (!bluetooth->bulk_packet_pos) {
884 bluetooth->bulk_buffer[0] = ACL_PKT;
885 bluetooth->bulk_packet_pos++;
886 }
887
888 if (bluetooth->bulk_packet_pos + count > ACL_BUFFER_SIZE) {
889 err(__FUNCTION__ " - exceeded ACL_BUFFER_SIZE");
890 bluetooth->bulk_packet_pos = 0;
891 goto exit;
892 }
893
894 memcpy (&bluetooth->bulk_buffer[bluetooth->bulk_packet_pos],
895 urb->transfer_buffer, count);
896 bluetooth->bulk_packet_pos += count;
897 urb->actual_length = 0;
898
899 if (bluetooth->bulk_packet_pos >= ACL_HDR_SIZE) {
900 packet_size = CHAR2INT16(bluetooth->bulk_buffer[4],bluetooth->bulk_buffer[3]);
901 } else {
902 goto exit;
903 }
904
905 if (packet_size + ACL_HDR_SIZE < bluetooth->bulk_packet_pos) {
906 err(__FUNCTION__ " - packet was too long");
907 bluetooth->bulk_packet_pos = 0;
908 goto exit;
909 }
910
911 if (packet_size + ACL_HDR_SIZE == bluetooth->bulk_packet_pos) {
912 for (i = 0; i < bluetooth->bulk_packet_pos; ++i) {
913 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
914 if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
915 tty_flip_buffer_push(bluetooth->tty);
916 }
917 tty_insert_flip_char(bluetooth->tty, bluetooth->bulk_buffer[i], 0);
918 }
919 tty_flip_buffer_push(bluetooth->tty);
920 bluetooth->bulk_packet_pos = 0;
921 }
922
923 exit:
924 FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev,
925 usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
926 bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
927 bluetooth_read_bulk_callback, bluetooth);
928 result = usb_submit_urb(bluetooth->read_urb);
929 if (result)
930 err (__FUNCTION__ " - failed resubmitting read urb, error %d", result);
931
932 return;
933 }
934
935
936 static void bluetooth_write_bulk_callback (struct urb *urb)
937 {
938 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
939
940 dbg(__FUNCTION__);
941
942 if (!bluetooth) {
943 dbg(__FUNCTION__ " - bad bluetooth pointer, exiting");
944 return;
945 }
946
947 if (urb->status) {
948 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
949 return;
950 }
951
952 /* wake up our little function to let the tty layer know that something happened */
953 queue_task(&bluetooth->tqueue, &tq_immediate);
954 mark_bh(IMMEDIATE_BH);
955 return;
956 }
957
958
959 static void bluetooth_softint(void *private)
960 {
961 struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)private, __FUNCTION__);
962 struct tty_struct *tty;
963
964 dbg(__FUNCTION__);
965
966 if (!bluetooth) {
967 return;
968 }
969
970 tty = bluetooth->tty;
971 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
972 dbg(__FUNCTION__ " - write wakeup call.");
973 (tty->ldisc.write_wakeup)(tty);
974 }
975
976 wake_up_interruptible(&tty->write_wait);
977 }
978
979
980 static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
981 const struct usb_device_id *id)
982 {
983 struct usb_bluetooth *bluetooth = NULL;
984 struct usb_interface_descriptor *interface;
985 struct usb_endpoint_descriptor *endpoint;
986 struct usb_endpoint_descriptor *interrupt_in_endpoint[8];
987 struct usb_endpoint_descriptor *bulk_in_endpoint[8];
988 struct usb_endpoint_descriptor *bulk_out_endpoint[8];
989 int control_out_endpoint;
990
991 int minor;
992 int buffer_size;
993 int i;
994 int num_interrupt_in = 0;
995 int num_bulk_in = 0;
996 int num_bulk_out = 0;
997
998 interface = &dev->actconfig->interface[ifnum].altsetting[0];
999 control_out_endpoint = interface->bInterfaceNumber;
1000
1001 /* find the endpoints that we need */
1002 for (i = 0; i < interface->bNumEndpoints; ++i) {
1003 endpoint = &interface->endpoint[i];
1004
1005 if ((endpoint->bEndpointAddress & 0x80) &&
1006 ((endpoint->bmAttributes & 3) == 0x02)) {
1007 /* we found a bulk in endpoint */
1008 dbg("found bulk in");
1009 bulk_in_endpoint[num_bulk_in] = endpoint;
1010 ++num_bulk_in;
1011 }
1012
1013 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1014 ((endpoint->bmAttributes & 3) == 0x02)) {
1015 /* we found a bulk out endpoint */
1016 dbg("found bulk out");
1017 bulk_out_endpoint[num_bulk_out] = endpoint;
1018 ++num_bulk_out;
1019 }
1020
1021 if ((endpoint->bEndpointAddress & 0x80) &&
1022 ((endpoint->bmAttributes & 3) == 0x03)) {
1023 /* we found a interrupt in endpoint */
1024 dbg("found interrupt in");
1025 interrupt_in_endpoint[num_interrupt_in] = endpoint;
1026 ++num_interrupt_in;
1027 }
1028 }
1029
1030 /* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */
1031 if ((num_bulk_in != 1) ||
1032 (num_bulk_out != 1) ||
1033 (num_interrupt_in != 1)) {
1034 dbg (__FUNCTION__ " - improper number of endpoints. Bluetooth driver not bound.");
1035 return NULL;
1036 }
1037
1038 MOD_INC_USE_COUNT;
1039 info("USB Bluetooth converter detected");
1040
1041 for (minor = 0; minor < BLUETOOTH_TTY_MINORS && bluetooth_table[minor]; ++minor)
1042 ;
1043 if (bluetooth_table[minor]) {
1044 err("No more free Bluetooth devices");
1045 MOD_DEC_USE_COUNT;
1046 return NULL;
1047 }
1048
1049 if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) {
1050 err("Out of memory");
1051 MOD_DEC_USE_COUNT;
1052 return NULL;
1053 }
1054
1055 memset(bluetooth, 0, sizeof(struct usb_bluetooth));
1056
1057 bluetooth->magic = USB_BLUETOOTH_MAGIC;
1058 bluetooth->dev = dev;
1059 bluetooth->minor = minor;
1060 bluetooth->tqueue.routine = bluetooth_softint;
1061 bluetooth->tqueue.data = bluetooth;
1062
1063 /* record the interface number for the control out */
1064 bluetooth->control_out_bInterfaceNum = control_out_endpoint;
1065
1066 /* create our control out urb pool */
1067 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1068 struct urb *urb = usb_alloc_urb(0);
1069 if (urb == NULL) {
1070 err("No free urbs available");
1071 goto probe_error;
1072 }
1073 urb->transfer_buffer = NULL;
1074 bluetooth->control_urb_pool[i] = urb;
1075 }
1076
1077 /* set up the endpoint information */
1078 endpoint = bulk_in_endpoint[0];
1079 bluetooth->read_urb = usb_alloc_urb (0);
1080 if (!bluetooth->read_urb) {
1081 err("No free urbs available");
1082 goto probe_error;
1083 }
1084 bluetooth->bulk_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1085 bluetooth->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1086 bluetooth->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1087 if (!bluetooth->bulk_in_buffer) {
1088 err("Couldn't allocate bulk_in_buffer");
1089 goto probe_error;
1090 }
1091 FILL_BULK_URB(bluetooth->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
1092 bluetooth->bulk_in_buffer, buffer_size, bluetooth_read_bulk_callback, bluetooth);
1093
1094 endpoint = bulk_out_endpoint[0];
1095 bluetooth->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1096
1097 /* create our write urb pool */
1098 for (i = 0; i < NUM_BULK_URBS; ++i) {
1099 struct urb *urb = usb_alloc_urb(0);
1100 if (urb == NULL) {
1101 err("No free urbs available");
1102 goto probe_error;
1103 }
1104 urb->transfer_buffer = NULL;
1105 bluetooth->write_urb_pool[i] = urb;
1106 }
1107
1108 bluetooth->bulk_out_buffer_size = endpoint->wMaxPacketSize * 2;
1109
1110 endpoint = interrupt_in_endpoint[0];
1111 bluetooth->interrupt_in_urb = usb_alloc_urb(0);
1112 if (!bluetooth->interrupt_in_urb) {
1113 err("No free urbs available");
1114 goto probe_error;
1115 }
1116 bluetooth->interrupt_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
1117 bluetooth->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1118 bluetooth->interrupt_in_interval = endpoint->bInterval;
1119 bluetooth->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1120 if (!bluetooth->interrupt_in_buffer) {
1121 err("Couldn't allocate interrupt_in_buffer");
1122 goto probe_error;
1123 }
1124 FILL_INT_URB(bluetooth->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1125 bluetooth->interrupt_in_buffer, buffer_size, bluetooth_int_callback,
1126 bluetooth, endpoint->bInterval);
1127
1128 /* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */
1129 tty_register_devfs (&bluetooth_tty_driver, 0, minor);
1130 info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor, minor);
1131
1132 bluetooth_table[minor] = bluetooth;
1133
1134 return bluetooth; /* success */
1135
1136 probe_error:
1137 if (bluetooth->read_urb)
1138 usb_free_urb (bluetooth->read_urb);
1139 if (bluetooth->bulk_in_buffer)
1140 kfree (bluetooth->bulk_in_buffer);
1141 if (bluetooth->interrupt_in_urb)
1142 usb_free_urb (bluetooth->interrupt_in_urb);
1143 if (bluetooth->interrupt_in_buffer)
1144 kfree (bluetooth->interrupt_in_buffer);
1145 for (i = 0; i < NUM_BULK_URBS; ++i)
1146 if (bluetooth->write_urb_pool[i])
1147 usb_free_urb (bluetooth->write_urb_pool[i]);
1148 for (i = 0; i < NUM_CONTROL_URBS; ++i)
1149 if (bluetooth->control_urb_pool[i])
1150 usb_free_urb (bluetooth->control_urb_pool[i]);
1151
1152 bluetooth_table[minor] = NULL;
1153
1154 /* free up any memory that we allocated */
1155 kfree (bluetooth);
1156 MOD_DEC_USE_COUNT;
1157 return NULL;
1158 }
1159
1160
1161 static void usb_bluetooth_disconnect(struct usb_device *dev, void *ptr)
1162 {
1163 struct usb_bluetooth *bluetooth = (struct usb_bluetooth *) ptr;
1164 int i;
1165
1166 if (bluetooth) {
1167 if ((bluetooth->active) && (bluetooth->tty))
1168 tty_hangup(bluetooth->tty);
1169
1170 bluetooth->active = 0;
1171
1172 if (bluetooth->read_urb) {
1173 usb_unlink_urb (bluetooth->read_urb);
1174 usb_free_urb (bluetooth->read_urb);
1175 }
1176 if (bluetooth->bulk_in_buffer)
1177 kfree (bluetooth->bulk_in_buffer);
1178
1179 if (bluetooth->interrupt_in_urb) {
1180 usb_unlink_urb (bluetooth->interrupt_in_urb);
1181 usb_free_urb (bluetooth->interrupt_in_urb);
1182 }
1183 if (bluetooth->interrupt_in_buffer)
1184 kfree (bluetooth->interrupt_in_buffer);
1185
1186 tty_unregister_devfs (&bluetooth_tty_driver, bluetooth->minor);
1187
1188 for (i = 0; i < NUM_BULK_URBS; ++i) {
1189 if (bluetooth->write_urb_pool[i]) {
1190 usb_unlink_urb (bluetooth->write_urb_pool[i]);
1191 if (bluetooth->write_urb_pool[i]->transfer_buffer)
1192 kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
1193 usb_free_urb (bluetooth->write_urb_pool[i]);
1194 }
1195 }
1196 for (i = 0; i < NUM_CONTROL_URBS; ++i) {
1197 if (bluetooth->control_urb_pool[i]) {
1198 usb_unlink_urb (bluetooth->control_urb_pool[i]);
1199 if (bluetooth->control_urb_pool[i]->transfer_buffer)
1200 kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
1201 usb_free_urb (bluetooth->control_urb_pool[i]);
1202 }
1203 }
1204
1205 info("Bluetooth converter now disconnected from ttyUB%d", bluetooth->minor);
1206
1207 bluetooth_table[bluetooth->minor] = NULL;
1208
1209 /* free up any memory that we allocated */
1210 kfree (bluetooth);
1211
1212 } else {
1213 info("device disconnected");
1214 }
1215
1216 MOD_DEC_USE_COUNT;
1217 }
1218
1219
1220 static struct tty_driver bluetooth_tty_driver = {
1221 magic: TTY_DRIVER_MAGIC,
1222 driver_name: "usb-bluetooth",
1223 name: "usb/ttub/%d",
1224 major: BLUETOOTH_TTY_MAJOR,
1225 minor_start: 0,
1226 num: BLUETOOTH_TTY_MINORS,
1227 type: TTY_DRIVER_TYPE_SERIAL,
1228 subtype: SERIAL_TYPE_NORMAL,
1229 flags: TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1230
1231 refcount: &bluetooth_refcount,
1232 table: bluetooth_tty,
1233 termios: bluetooth_termios,
1234 termios_locked: bluetooth_termios_locked,
1235
1236 open: bluetooth_open,
1237 close: bluetooth_close,
1238 write: bluetooth_write,
1239 write_room: bluetooth_write_room,
1240 ioctl: bluetooth_ioctl,
1241 set_termios: bluetooth_set_termios,
1242 throttle: bluetooth_throttle,
1243 unthrottle: bluetooth_unthrottle,
1244 chars_in_buffer: bluetooth_chars_in_buffer,
1245 };
1246
1247
1248 int usb_bluetooth_init(void)
1249 {
1250 int i;
1251 int result;
1252
1253 /* Initalize our global data */
1254 for (i = 0; i < BLUETOOTH_TTY_MINORS; ++i) {
1255 bluetooth_table[i] = NULL;
1256 }
1257
1258 info ("USB Bluetooth support registered");
1259
1260 /* register the tty driver */
1261 bluetooth_tty_driver.init_termios = tty_std_termios;
1262 bluetooth_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1263 if (tty_register_driver (&bluetooth_tty_driver)) {
1264 err(__FUNCTION__ " - failed to register tty driver");
1265 return -1;
1266 }
1267
1268 /* register the USB driver */
1269 result = usb_register(&usb_bluetooth_driver);
1270 if (result < 0) {
1271 tty_unregister_driver(&bluetooth_tty_driver);
1272 err("usb_register failed for the USB bluetooth driver. Error number %d", result);
1273 return -1;
1274 }
1275
1276 return 0;
1277 }
1278
1279
1280 void usb_bluetooth_exit(void)
1281 {
1282 usb_deregister(&usb_bluetooth_driver);
1283 tty_unregister_driver(&bluetooth_tty_driver);
1284 }
1285
1286
1287 module_init(usb_bluetooth_init);
1288 module_exit(usb_bluetooth_exit);
1289
1290
1291
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.