bluetooth.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:38k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  * This program is free software; you can redistribute it and/or modify
  90.  * it under the terms of the GNU General Public License as published by
  91.  * the Free Software Foundation; either version 2 of the License, or
  92.  * (at your option) any later version.
  93.  *
  94.  * This program is distributed in the hope that it will be useful,
  95.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  96.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  97.  * GNU General Public License for more details.
  98.  *
  99.  * You should have received a copy of the GNU General Public License
  100.  * along with this program; if not, write to the Free Software
  101.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  102.  */
  103. #include <linux/kernel.h>
  104. #include <linux/errno.h>
  105. #include <linux/init.h>
  106. #include <linux/slab.h>
  107. #include <linux/tty.h>
  108. #include <linux/tty_driver.h>
  109. #include <linux/tty_flip.h>
  110. #include <linux/module.h>
  111. #include <asm/uaccess.h>
  112. #define DEBUG
  113. #include <linux/usb.h>
  114. /*
  115.  * Version Information
  116.  */
  117. #define DRIVER_VERSION "v0.13"
  118. #define DRIVER_AUTHOR "Greg Kroah-Hartman, Mark Douglas Corner"
  119. #define DRIVER_DESC "USB Bluetooth tty driver"
  120. /* define this if you have hardware that is not good */
  121. /*#define BTBUGGYHARDWARE */
  122. /* Class, SubClass, and Protocol codes that describe a Bluetooth device */
  123. #define WIRELESS_CLASS_CODE 0xe0
  124. #define RF_SUBCLASS_CODE 0x01
  125. #define BLUETOOTH_PROGRAMMING_PROTOCOL_CODE 0x01
  126. #define BLUETOOTH_TTY_MAJOR 216 /* real device node major id */
  127. #define BLUETOOTH_TTY_MINORS 256 /* whole lotta bluetooth devices */
  128. #define USB_BLUETOOTH_MAGIC 0x6d02 /* magic number for bluetooth struct */
  129. #define BLUETOOTH_CONTROL_REQUEST_TYPE 0x20
  130. /* Bluetooth packet types */
  131. #define CMD_PKT 0x01
  132. #define ACL_PKT 0x02
  133. #define SCO_PKT 0x03
  134. #define EVENT_PKT 0x04
  135. #define ERROR_PKT 0x05
  136. #define NEG_PKT 0x06
  137. /* Message sizes */
  138. #define MAX_EVENT_SIZE 0xFF
  139. #define EVENT_HDR_SIZE 3 /* 2 for the header + 1 for the type indicator */
  140. #define EVENT_BUFFER_SIZE (MAX_EVENT_SIZE + EVENT_HDR_SIZE)
  141. #define MAX_ACL_SIZE 0xFFFF
  142. #define ACL_HDR_SIZE 5 /* 4 for the header + 1 for the type indicator */
  143. #define ACL_BUFFER_SIZE (MAX_ACL_SIZE + ACL_HDR_SIZE)
  144. /* parity check flag */
  145. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  146. #define CHAR2INT16(c1,c0) (((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
  147. #define NUM_BULK_URBS 24
  148. #define NUM_CONTROL_URBS 16
  149. struct usb_bluetooth {
  150. int magic;
  151. struct usb_device * dev;
  152. struct tty_driver * tty_driver; /* the tty_driver for this device */
  153. struct tty_struct * tty; /* the coresponding tty for this port */
  154. unsigned char minor; /* the starting minor number for this device */
  155. int throttle; /* throttled by tty layer */
  156. int open_count;
  157. __u8 control_out_bInterfaceNum;
  158. struct urb * control_urb_pool[NUM_CONTROL_URBS];
  159. struct usb_ctrlrequest dr[NUM_CONTROL_URBS];
  160. unsigned char * interrupt_in_buffer;
  161. struct urb * interrupt_in_urb;
  162. __u8 interrupt_in_endpointAddress;
  163. __u8 interrupt_in_interval;
  164. int interrupt_in_buffer_size;
  165. unsigned char * bulk_in_buffer;
  166. struct urb * read_urb;
  167. __u8 bulk_in_endpointAddress;
  168. int bulk_in_buffer_size;
  169. int bulk_out_buffer_size;
  170. struct urb * write_urb_pool[NUM_BULK_URBS];
  171. __u8 bulk_out_endpointAddress;
  172. wait_queue_head_t write_wait;
  173. struct tq_struct tqueue; /* task queue for line discipline waking up */
  174. unsigned int int_packet_pos;
  175. unsigned char int_buffer[EVENT_BUFFER_SIZE];
  176. unsigned int bulk_packet_pos;
  177. unsigned char bulk_buffer[ACL_BUFFER_SIZE]; /* 64k preallocated, fix? */
  178. struct semaphore lock;
  179. };
  180. /* local function prototypes */
  181. static int  bluetooth_open (struct tty_struct *tty, struct file *filp);
  182. static void bluetooth_close (struct tty_struct *tty, struct file *filp);
  183. static int  bluetooth_write (struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
  184. static int  bluetooth_write_room (struct tty_struct *tty);
  185. static int  bluetooth_chars_in_buffer (struct tty_struct *tty);
  186. static void bluetooth_throttle (struct tty_struct *tty);
  187. static void bluetooth_unthrottle (struct tty_struct *tty);
  188. static int  bluetooth_ioctl (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  189. static void bluetooth_set_termios (struct tty_struct *tty, struct termios *old);
  190. static void bluetooth_int_callback (struct urb *urb);
  191. static void bluetooth_ctrl_callback (struct urb *urb);
  192. static void bluetooth_read_bulk_callback (struct urb *urb);
  193. static void bluetooth_write_bulk_callback (struct urb *urb);
  194. static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
  195.     const struct usb_device_id *id);
  196. static void usb_bluetooth_disconnect (struct usb_device *dev, void *ptr);
  197. static struct usb_device_id usb_bluetooth_ids [] = {
  198. { USB_DEVICE_INFO(WIRELESS_CLASS_CODE, RF_SUBCLASS_CODE, BLUETOOTH_PROGRAMMING_PROTOCOL_CODE) },
  199. { } /* Terminating entry */
  200. };
  201. MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
  202. static struct usb_driver usb_bluetooth_driver = {
  203. .name = "bluetty",
  204. .probe = usb_bluetooth_probe,
  205. .disconnect = usb_bluetooth_disconnect,
  206. .id_table = usb_bluetooth_ids,
  207. };
  208. static int bluetooth_refcount;
  209. static struct tty_driver bluetooth_tty_driver;
  210. static struct tty_struct * bluetooth_tty[BLUETOOTH_TTY_MINORS];
  211. static struct termios * bluetooth_termios[BLUETOOTH_TTY_MINORS];
  212. static struct termios * bluetooth_termios_locked[BLUETOOTH_TTY_MINORS];
  213. static struct usb_bluetooth *bluetooth_table[BLUETOOTH_TTY_MINORS];
  214. static inline int bluetooth_paranoia_check (struct usb_bluetooth *bluetooth, const char *function)
  215. {
  216. if (!bluetooth) {
  217. dbg("%s - bluetooth == NULL", function);
  218. return -1;
  219. }
  220. if (bluetooth->magic != USB_BLUETOOTH_MAGIC) {
  221. dbg("%s - bad magic number for bluetooth", function);
  222. return -1;
  223. }
  224. return 0;
  225. }
  226. static inline struct usb_bluetooth* get_usb_bluetooth (struct usb_bluetooth *bluetooth, const char *function)
  227. {
  228. if (!bluetooth || 
  229.     bluetooth_paranoia_check (bluetooth, function)) { 
  230. /* then say that we dont have a valid usb_bluetooth thing, which will
  231.  * end up generating -ENODEV return values */
  232. return NULL;
  233. }
  234. return bluetooth;
  235. }
  236. static inline struct usb_bluetooth *get_bluetooth_by_minor (int minor)
  237. {
  238. return bluetooth_table[minor];
  239. }
  240. static int bluetooth_ctrl_msg (struct usb_bluetooth *bluetooth, int request, int value, const unsigned char *buf, int len)
  241. {
  242. struct urb *urb = NULL;
  243. struct usb_ctrlrequest *dr = NULL;
  244. int i;
  245. int status;
  246. dbg ("%s", __FUNCTION__);
  247. /* try to find a free urb in our list */
  248. for (i = 0; i < NUM_CONTROL_URBS; ++i) {
  249. if (bluetooth->control_urb_pool[i]->status != -EINPROGRESS) {
  250. urb = bluetooth->control_urb_pool[i];
  251. dr = &bluetooth->dr[i];
  252. break;
  253. }
  254. }
  255. if (urb == NULL) {
  256. dbg ("%s - no free urbs", __FUNCTION__);
  257. return -ENOMEM;
  258. }
  259. /* keep increasing the urb transfer buffer to fit the size of the message */
  260. if (urb->transfer_buffer == NULL) {
  261. urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
  262. if (urb->transfer_buffer == NULL) {
  263. err ("%s - out of memory", __FUNCTION__);
  264. return -ENOMEM;
  265. }
  266. }
  267. if (urb->transfer_buffer_length < len) {
  268. kfree (urb->transfer_buffer);
  269. urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
  270. if (urb->transfer_buffer == NULL) {
  271. err ("%s - out of memory", __FUNCTION__);
  272. return -ENOMEM;
  273. }
  274. }
  275. memcpy (urb->transfer_buffer, buf, len);
  276. dr->bRequestType= BLUETOOTH_CONTROL_REQUEST_TYPE;
  277. dr->bRequest = request;
  278. dr->wValue = cpu_to_le16((u16) value);
  279. dr->wIndex = cpu_to_le16((u16) bluetooth->control_out_bInterfaceNum);
  280. dr->wLength = cpu_to_le16((u16) len);
  281. FILL_CONTROL_URB (urb, bluetooth->dev, usb_sndctrlpipe(bluetooth->dev, 0),
  282.   (unsigned char*)dr, urb->transfer_buffer, len, bluetooth_ctrl_callback, bluetooth);
  283. /* send it down the pipe */
  284. status = usb_submit_urb(urb);
  285. if (status)
  286. dbg("%s - usb_submit_urb(control) failed with status = %d", __FUNCTION__, status);
  287. return status;
  288. }
  289. /*****************************************************************************
  290.  * Driver tty interface functions
  291.  *****************************************************************************/
  292. static int bluetooth_open (struct tty_struct *tty, struct file * filp)
  293. {
  294. struct usb_bluetooth *bluetooth;
  295. int result;
  296. dbg("%s", __FUNCTION__);
  297. /* initialize the pointer incase something fails */
  298. tty->driver_data = NULL;
  299. /* get the bluetooth object associated with this tty pointer */
  300. bluetooth = get_bluetooth_by_minor (MINOR(tty->device));
  301. if (bluetooth_paranoia_check (bluetooth, __FUNCTION__)) {
  302. return -ENODEV;
  303. }
  304. down (&bluetooth->lock);
  305.  
  306. ++bluetooth->open_count;
  307. if (bluetooth->open_count == 1) {
  308. /* set up our structure making the tty driver remember our object, and us it */
  309. tty->driver_data = bluetooth;
  310. bluetooth->tty = tty;
  311. /* force low_latency on so that our tty_push actually forces the data through, 
  312.  * otherwise it is scheduled, and with high data rates (like with OHCI) data
  313.  * can get lost. */
  314. bluetooth->tty->low_latency = 1;
  315. /* Reset the packet position counters */
  316. bluetooth->int_packet_pos = 0;
  317. bluetooth->bulk_packet_pos = 0;
  318. #ifndef BTBUGGYHARDWARE
  319. /* Start reading from the device */
  320. FILL_BULK_URB (bluetooth->read_urb, bluetooth->dev, 
  321.        usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
  322.        bluetooth->bulk_in_buffer,
  323.        bluetooth->bulk_in_buffer_size,
  324.        bluetooth_read_bulk_callback, bluetooth);
  325. result = usb_submit_urb(bluetooth->read_urb);
  326. if (result)
  327. dbg("%s - usb_submit_urb(read bulk) failed with status %d", __FUNCTION__, result);
  328. #endif
  329. FILL_INT_URB (bluetooth->interrupt_in_urb, bluetooth->dev, 
  330.       usb_rcvintpipe(bluetooth->dev, bluetooth->interrupt_in_endpointAddress),
  331.       bluetooth->interrupt_in_buffer,
  332.       bluetooth->interrupt_in_buffer_size,
  333.       bluetooth_int_callback, bluetooth,
  334.       bluetooth->interrupt_in_interval);
  335. result = usb_submit_urb(bluetooth->interrupt_in_urb);
  336. if (result)
  337. dbg("%s - usb_submit_urb(interrupt in) failed with status %d", __FUNCTION__, result);
  338. }
  339. up(&bluetooth->lock);
  340. return 0;
  341. }
  342. static void bluetooth_close (struct tty_struct *tty, struct file * filp)
  343. {
  344. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  345. int i;
  346. if (!bluetooth) {
  347. return;
  348. }
  349. dbg("%s", __FUNCTION__);
  350. if (!bluetooth->open_count) {
  351. dbg ("%s - device not opened", __FUNCTION__);
  352. return;
  353. }
  354. down (&bluetooth->lock);
  355.  
  356. --bluetooth->open_count;
  357. if (bluetooth->open_count <= 0) {
  358. bluetooth->open_count = 0;
  359. /* shutdown any bulk reads and writes that might be going on */
  360. for (i = 0; i < NUM_BULK_URBS; ++i)
  361. usb_unlink_urb (bluetooth->write_urb_pool[i]);
  362. usb_unlink_urb (bluetooth->read_urb);
  363. usb_unlink_urb (bluetooth->interrupt_in_urb);
  364. }
  365. up(&bluetooth->lock);
  366. }
  367. static int bluetooth_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
  368. {
  369. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  370. struct urb *urb = NULL;
  371. unsigned char *temp_buffer = NULL;
  372. const unsigned char *current_buffer;
  373. const unsigned char *current_position;
  374. int bytes_sent;
  375. int buffer_size;
  376. int i;
  377. int retval = 0;
  378. if (!bluetooth) {
  379. return -ENODEV;
  380. }
  381. dbg("%s - %d byte(s)", __FUNCTION__, count);
  382. if (!bluetooth->open_count) {
  383. dbg ("%s - device not opened", __FUNCTION__);
  384. return -EINVAL;
  385. }
  386. if (count == 0) {
  387. dbg("%s - write request of 0 bytes", __FUNCTION__);
  388. return 0;
  389. }
  390. if (count == 1) {
  391. dbg("%s - write request only included type %d", __FUNCTION__, buf[0]);
  392. return 1;
  393. }
  394. #ifdef DEBUG
  395. printk (KERN_DEBUG __FILE__ ": %s - length = %d, data = ", __FUNCTION__, count);
  396. for (i = 0; i < count; ++i) {
  397. printk ("%.2x ", buf[i]);
  398. }
  399. printk ("n");
  400. #endif
  401. if (from_user) {
  402. temp_buffer = kmalloc (count, GFP_KERNEL);
  403. if (temp_buffer == NULL) {
  404. err ("%s - out of memory.", __FUNCTION__);
  405. retval = -ENOMEM;
  406. goto exit;
  407. }
  408. if (copy_from_user (temp_buffer, buf, count)) {
  409. retval = -EFAULT;
  410. goto exit;
  411. }
  412. current_buffer = temp_buffer;
  413. } else {
  414. current_buffer = buf;
  415. }
  416. switch (*current_buffer) {
  417. /* First byte indicates the type of packet */
  418. case CMD_PKT:
  419. /* dbg("%s- Send cmd_pkt len:%d", __FUNCTION__, count);*/
  420. retval = bluetooth_ctrl_msg (bluetooth, 0x00, 0x00, &current_buffer[1], count-1);
  421. if (retval) {
  422. goto exit;
  423. }
  424. retval = count;
  425. break;
  426. case ACL_PKT:
  427. current_position = current_buffer;
  428. ++current_position;
  429. --count;
  430. bytes_sent = 0;
  431. while (count > 0) {
  432. urb = NULL;
  433. /* try to find a free urb in our list */
  434. for (i = 0; i < NUM_BULK_URBS; ++i) {
  435. if (bluetooth->write_urb_pool[i]->status != -EINPROGRESS) {
  436. urb = bluetooth->write_urb_pool[i];
  437. break;
  438. }
  439. }
  440. if (urb == NULL) {
  441. dbg ("%s - no free urbs", __FUNCTION__);
  442. retval = bytes_sent;
  443. goto exit;
  444. }
  445. buffer_size = min (count, bluetooth->bulk_out_buffer_size);
  446. memcpy (urb->transfer_buffer, current_position, buffer_size);
  447. /* build up our urb */
  448. FILL_BULK_URB (urb, bluetooth->dev, usb_sndbulkpipe(bluetooth->dev, bluetooth->bulk_out_endpointAddress),
  449. urb->transfer_buffer, buffer_size, bluetooth_write_bulk_callback, bluetooth);
  450. urb->transfer_flags |= USB_QUEUE_BULK;
  451. /* send it down the pipe */
  452. retval = usb_submit_urb(urb);
  453. if (retval) {
  454. dbg("%s - usb_submit_urb(write bulk) failed with error = %d", __FUNCTION__, retval);
  455. goto exit;
  456. }
  457. #ifdef BTBUGGYHARDWARE
  458. /* A workaround for the stalled data bug */
  459. /* May or may not be needed...*/
  460. if (count != 0) {
  461. udelay(500);
  462. }
  463. #endif
  464. current_position += buffer_size;
  465. bytes_sent += buffer_size;
  466. count -= buffer_size;
  467. }
  468. retval = bytes_sent + 1;
  469. break;
  470. default :
  471. dbg("%s - unsupported (at this time) write type", __FUNCTION__);
  472. retval = -EINVAL;
  473. break;
  474. }
  475. exit:
  476. if (temp_buffer != NULL)
  477. kfree (temp_buffer);
  478. return retval;
  479. static int bluetooth_write_room (struct tty_struct *tty) 
  480. {
  481. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  482. int room = 0;
  483. int i;
  484. if (!bluetooth) {
  485. return -ENODEV;
  486. }
  487. dbg("%s", __FUNCTION__);
  488. if (!bluetooth->open_count) {
  489. dbg ("%s - device not open", __FUNCTION__);
  490. return -EINVAL;
  491. }
  492. for (i = 0; i < NUM_BULK_URBS; ++i) {
  493. if (bluetooth->write_urb_pool[i]->status != -EINPROGRESS) {
  494. room += bluetooth->bulk_out_buffer_size;
  495. }
  496. }
  497. dbg("%s - returns %d", __FUNCTION__, room);
  498. return room;
  499. }
  500. static int bluetooth_chars_in_buffer (struct tty_struct *tty) 
  501. {
  502. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  503. int chars = 0;
  504. int i;
  505. if (!bluetooth) {
  506. return -ENODEV;
  507. }
  508. if (!bluetooth->open_count) {
  509. dbg ("%s - device not open", __FUNCTION__);
  510. return -EINVAL;
  511. }
  512. for (i = 0; i < NUM_BULK_URBS; ++i) {
  513. if (bluetooth->write_urb_pool[i]->status == -EINPROGRESS) {
  514. chars += bluetooth->write_urb_pool[i]->transfer_buffer_length;
  515. }
  516. }
  517. dbg ("%s - returns %d", __FUNCTION__, chars);
  518. return chars;
  519. }
  520. static void bluetooth_throttle (struct tty_struct * tty)
  521. {
  522. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  523. if (!bluetooth) {
  524. return;
  525. }
  526. dbg("%s", __FUNCTION__);
  527. if (!bluetooth->open_count) {
  528. dbg ("%s - device not open", __FUNCTION__);
  529. return;
  530. }
  531. dbg("%s unsupported (at this time)", __FUNCTION__);
  532. return;
  533. }
  534. static void bluetooth_unthrottle (struct tty_struct * tty)
  535. {
  536. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  537. if (!bluetooth) {
  538. return;
  539. }
  540. dbg("%s", __FUNCTION__);
  541. if (!bluetooth->open_count) {
  542. dbg ("%s - device not open", __FUNCTION__);
  543. return;
  544. }
  545. dbg("%s unsupported (at this time)", __FUNCTION__);
  546. }
  547. static int bluetooth_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
  548. {
  549. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  550. if (!bluetooth) {
  551. return -ENODEV;
  552. }
  553. dbg("%s - cmd 0x%.4x", __FUNCTION__, cmd);
  554. if (!bluetooth->open_count) {
  555. dbg ("%s - device not open", __FUNCTION__);
  556. return -ENODEV;
  557. }
  558. /* FIXME!!! */
  559. return -ENOIOCTLCMD;
  560. }
  561. static void bluetooth_set_termios (struct tty_struct *tty, struct termios * old)
  562. {
  563. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  564. if (!bluetooth) {
  565. return;
  566. }
  567. dbg("%s", __FUNCTION__);
  568. if (!bluetooth->open_count) {
  569. dbg ("%s - device not open", __FUNCTION__);
  570. return;
  571. }
  572. /* FIXME!!! */
  573. return;
  574. }
  575. #ifdef BTBUGGYHARDWARE
  576. void btusb_enable_bulk_read(struct tty_struct *tty){
  577. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  578. int result;
  579. if (!bluetooth) {
  580. return;
  581. }
  582. dbg("%s", __FUNCTION__);
  583. if (!bluetooth->open_count) {
  584. dbg ("%s - device not open", __FUNCTION__);
  585. return;
  586. }
  587. if (bluetooth->read_urb) {
  588. FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev, 
  589.       usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
  590.       bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, 
  591.       bluetooth_read_bulk_callback, bluetooth);
  592. result = usb_submit_urb(bluetooth->read_urb);
  593. if (result)
  594. err ("%s - failed submitting read urb, error %d", __FUNCTION__, result);
  595. }
  596. }
  597. void btusb_disable_bulk_read(struct tty_struct *tty){
  598. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  599. if (!bluetooth) {
  600. return;
  601. }
  602. dbg("%s", __FUNCTION__);
  603. if (!bluetooth->open_count) {
  604. dbg ("%s - device not open", __FUNCTION__);
  605. return;
  606. }
  607. if ((bluetooth->read_urb) && (bluetooth->read_urb->actual_length))
  608. usb_unlink_urb(bluetooth->read_urb);
  609. }
  610. #endif
  611. /*****************************************************************************
  612.  * urb callback functions
  613.  *****************************************************************************/
  614. static void bluetooth_int_callback (struct urb *urb)
  615. {
  616. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
  617. unsigned char *data = urb->transfer_buffer;
  618. unsigned int i;
  619. unsigned int count = urb->actual_length;
  620. unsigned int packet_size;
  621. dbg("%s", __FUNCTION__);
  622. if (!bluetooth) {
  623. dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
  624. return;
  625. }
  626. if (urb->status) {
  627. dbg("%s - nonzero int status received: %d", __FUNCTION__, urb->status);
  628. return;
  629. }
  630. if (!count) {
  631. dbg("%s - zero length int", __FUNCTION__);
  632. return;
  633. }
  634. #ifdef DEBUG
  635. if (count) {
  636. printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count);
  637. for (i = 0; i < count; ++i) {
  638. printk ("%.2x ", data[i]);
  639. }
  640. printk ("n");
  641. }
  642. #endif
  643. #ifdef BTBUGGYHARDWARE
  644. if ((count >= 2) && (data[0] == 0xFF) && (data[1] == 0x00)) {
  645. data += 2;
  646. count -= 2;
  647. }
  648. if (count == 0) {
  649. urb->actual_length = 0;
  650. return;
  651. }
  652. #endif
  653. /* We add  a packet type identifier to the beginning of each
  654.    HCI frame.  This makes the data in the tty look like a
  655.    serial USB devices.  Each HCI frame can be broken across
  656.    multiple URBs so we buffer them until we have a full hci
  657.    packet */
  658. if (!bluetooth->int_packet_pos) {
  659. bluetooth->int_buffer[0] = EVENT_PKT;
  660. bluetooth->int_packet_pos++;
  661. }
  662. if (bluetooth->int_packet_pos + count > EVENT_BUFFER_SIZE) {
  663. err("%s - exceeded EVENT_BUFFER_SIZE", __FUNCTION__);
  664. bluetooth->int_packet_pos = 0;
  665. return;
  666. }
  667. memcpy (&bluetooth->int_buffer[bluetooth->int_packet_pos],
  668. urb->transfer_buffer, count);
  669. bluetooth->int_packet_pos += count;
  670. urb->actual_length = 0;
  671. if (bluetooth->int_packet_pos >= EVENT_HDR_SIZE)
  672. packet_size = bluetooth->int_buffer[2];
  673. else
  674. return;
  675. if (packet_size + EVENT_HDR_SIZE < bluetooth->int_packet_pos) {
  676. err("%s - packet was too long", __FUNCTION__);
  677. bluetooth->int_packet_pos = 0;
  678. return;
  679. }
  680. if (packet_size + EVENT_HDR_SIZE == bluetooth->int_packet_pos) {
  681. for (i = 0; i < bluetooth->int_packet_pos; ++i) {
  682. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them */
  683. if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
  684. tty_flip_buffer_push(bluetooth->tty);
  685. }
  686. tty_insert_flip_char(bluetooth->tty, bluetooth->int_buffer[i], 0);
  687. }
  688. tty_flip_buffer_push(bluetooth->tty);
  689. bluetooth->int_packet_pos = 0;
  690. }
  691. }
  692. static void bluetooth_ctrl_callback (struct urb *urb)
  693. {
  694. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
  695. dbg("%s", __FUNCTION__);
  696. if (!bluetooth) {
  697. dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
  698. return;
  699. }
  700. if (urb->status) {
  701. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  702. return;
  703. }
  704. }
  705. static void bluetooth_read_bulk_callback (struct urb *urb)
  706. {
  707. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
  708. unsigned char *data = urb->transfer_buffer;
  709. unsigned int count = urb->actual_length;
  710. unsigned int i;
  711. unsigned int packet_size;
  712. int result;
  713. dbg("%s", __FUNCTION__);
  714. if (!bluetooth) {
  715. dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
  716. return;
  717. }
  718. if (urb->status) {
  719. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  720. if (urb->status == -ENOENT) {                   
  721. dbg("%s - URB canceled, won't reschedule", __FUNCTION__);
  722. return;
  723. }
  724. goto exit;
  725. }
  726. if (!count) {
  727. dbg("%s - zero length read bulk", __FUNCTION__);
  728. goto exit;
  729. }
  730. #ifdef DEBUG
  731. if (count) {
  732. printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count);
  733. for (i = 0; i < count; ++i) {
  734. printk ("%.2x ", data[i]);
  735. }
  736. printk ("n");
  737. }
  738. #endif
  739. #ifdef BTBUGGYHARDWARE
  740. if ((count == 4) && (data[0] == 0x00) && (data[1] == 0x00)
  741.     && (data[2] == 0x00) && (data[3] == 0x00)) {
  742. urb->actual_length = 0;
  743. FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev, 
  744.       usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
  745.       bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, 
  746.       bluetooth_read_bulk_callback, bluetooth);
  747. result = usb_submit_urb(bluetooth->read_urb);
  748. if (result)
  749. err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  750. return;
  751. }
  752. #endif
  753. /* We add  a packet type identifier to the beginning of each
  754.    HCI frame.  This makes the data in the tty look like a
  755.    serial USB devices.  Each HCI frame can be broken across
  756.    multiple URBs so we buffer them until we have a full hci
  757.    packet */
  758. if (!bluetooth->bulk_packet_pos) {
  759. bluetooth->bulk_buffer[0] = ACL_PKT;
  760. bluetooth->bulk_packet_pos++;
  761. }
  762. if (bluetooth->bulk_packet_pos + count > ACL_BUFFER_SIZE) {
  763. err("%s - exceeded ACL_BUFFER_SIZE", __FUNCTION__);
  764. bluetooth->bulk_packet_pos = 0;
  765. goto exit;
  766. }
  767. memcpy (&bluetooth->bulk_buffer[bluetooth->bulk_packet_pos],
  768. urb->transfer_buffer, count);
  769. bluetooth->bulk_packet_pos += count;
  770. urb->actual_length = 0;
  771. if (bluetooth->bulk_packet_pos >= ACL_HDR_SIZE) {
  772. packet_size = CHAR2INT16(bluetooth->bulk_buffer[4],bluetooth->bulk_buffer[3]);
  773. } else {
  774. goto exit;
  775. }
  776. if (packet_size + ACL_HDR_SIZE < bluetooth->bulk_packet_pos) {
  777. err("%s - packet was too long", __FUNCTION__);
  778. bluetooth->bulk_packet_pos = 0;
  779. goto exit;
  780. }
  781. if (packet_size + ACL_HDR_SIZE == bluetooth->bulk_packet_pos) {
  782. for (i = 0; i < bluetooth->bulk_packet_pos; ++i) {
  783. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  784. if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
  785. tty_flip_buffer_push(bluetooth->tty);
  786. }
  787. tty_insert_flip_char(bluetooth->tty, bluetooth->bulk_buffer[i], 0);
  788. }
  789. tty_flip_buffer_push(bluetooth->tty);
  790. bluetooth->bulk_packet_pos = 0;
  791. }
  792. exit:
  793. if (!bluetooth || !bluetooth->open_count)
  794. return;
  795. FILL_BULK_URB(bluetooth->read_urb, bluetooth->dev, 
  796.       usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
  797.       bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, 
  798.       bluetooth_read_bulk_callback, bluetooth);
  799. result = usb_submit_urb(bluetooth->read_urb);
  800. if (result)
  801. err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  802. return;
  803. }
  804. static void bluetooth_write_bulk_callback (struct urb *urb)
  805. {
  806. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
  807. dbg("%s", __FUNCTION__);
  808. if (!bluetooth) {
  809. dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
  810. return;
  811. }
  812. if (urb->status) {
  813. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  814. return;
  815. }
  816. /* wake up our little function to let the tty layer know that something happened */
  817. queue_task(&bluetooth->tqueue, &tq_immediate);
  818. mark_bh(IMMEDIATE_BH);
  819. return;
  820. }
  821. static void bluetooth_softint(void *private)
  822. {
  823. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)private, __FUNCTION__);
  824. struct tty_struct *tty;
  825. dbg("%s", __FUNCTION__);
  826. if (!bluetooth) {
  827. return;
  828. }
  829. tty = bluetooth->tty;
  830. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
  831. dbg("%s - write wakeup call.", __FUNCTION__);
  832. (tty->ldisc.write_wakeup)(tty);
  833. }
  834. wake_up_interruptible(&tty->write_wait);
  835. }
  836. static void * usb_bluetooth_probe(struct usb_device *dev, unsigned int ifnum,
  837.     const struct usb_device_id *id)
  838. {
  839. struct usb_bluetooth *bluetooth = NULL;
  840. struct usb_interface_descriptor *interface;
  841. struct usb_endpoint_descriptor *endpoint;
  842. struct usb_endpoint_descriptor *interrupt_in_endpoint[8];
  843. struct usb_endpoint_descriptor *bulk_in_endpoint[8];
  844. struct usb_endpoint_descriptor *bulk_out_endpoint[8];
  845. int control_out_endpoint;
  846. int minor;
  847. int buffer_size;
  848. int i;
  849. int num_interrupt_in = 0;
  850. int num_bulk_in = 0;
  851. int num_bulk_out = 0;
  852. interface = &dev->actconfig->interface[ifnum].altsetting[0];
  853. control_out_endpoint = interface->bInterfaceNumber;
  854. /* find the endpoints that we need */
  855. for (i = 0; i < interface->bNumEndpoints; ++i) {
  856. endpoint = &interface->endpoint[i];
  857. if ((endpoint->bEndpointAddress & 0x80) &&
  858.     ((endpoint->bmAttributes & 3) == 0x02)) {
  859. /* we found a bulk in endpoint */
  860. dbg("found bulk in");
  861. bulk_in_endpoint[num_bulk_in] = endpoint;
  862. ++num_bulk_in;
  863. }
  864. if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
  865.     ((endpoint->bmAttributes & 3) == 0x02)) {
  866. /* we found a bulk out endpoint */
  867. dbg("found bulk out");
  868. bulk_out_endpoint[num_bulk_out] = endpoint;
  869. ++num_bulk_out;
  870. }
  871. if ((endpoint->bEndpointAddress & 0x80) &&
  872.     ((endpoint->bmAttributes & 3) == 0x03)) {
  873. /* we found a interrupt in endpoint */
  874. dbg("found interrupt in");
  875. interrupt_in_endpoint[num_interrupt_in] = endpoint;
  876. ++num_interrupt_in;
  877. }
  878. }
  879. /* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */
  880. if ((num_bulk_in != 1) ||
  881.     (num_bulk_out != 1) ||
  882.     (num_interrupt_in != 1)) {
  883. dbg ("%s - improper number of endpoints. Bluetooth driver not bound.", __FUNCTION__);
  884. return NULL;
  885. }
  886. MOD_INC_USE_COUNT;
  887. info("USB Bluetooth converter detected");
  888. for (minor = 0; minor < BLUETOOTH_TTY_MINORS && bluetooth_table[minor]; ++minor)
  889. ;
  890. if (bluetooth_table[minor]) {
  891. err("No more free Bluetooth devices");
  892. MOD_DEC_USE_COUNT;
  893. return NULL;
  894. }
  895. if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) {
  896. err("Out of memory");
  897. MOD_DEC_USE_COUNT;
  898. return NULL;
  899. }
  900. memset(bluetooth, 0, sizeof(struct usb_bluetooth));
  901. bluetooth->magic = USB_BLUETOOTH_MAGIC;
  902. bluetooth->dev = dev;
  903. bluetooth->minor = minor;
  904. bluetooth->tqueue.routine = bluetooth_softint;
  905. bluetooth->tqueue.data = bluetooth;
  906. init_MUTEX(&bluetooth->lock);
  907. /* record the interface number for the control out */
  908. bluetooth->control_out_bInterfaceNum = control_out_endpoint;
  909. /* create our control out urb pool */ 
  910. for (i = 0; i < NUM_CONTROL_URBS; ++i) {
  911. struct urb  *urb = usb_alloc_urb(0);
  912. if (urb == NULL) {
  913. err("No free urbs available");
  914. goto probe_error;
  915. }
  916. urb->transfer_buffer = NULL;
  917. bluetooth->control_urb_pool[i] = urb;
  918. }
  919. /* set up the endpoint information */
  920. endpoint = bulk_in_endpoint[0];
  921. bluetooth->read_urb = usb_alloc_urb (0);
  922. if (!bluetooth->read_urb) {
  923. err("No free urbs available");
  924. goto probe_error;
  925. }
  926. bluetooth->bulk_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
  927. bluetooth->bulk_in_endpointAddress = endpoint->bEndpointAddress;
  928. bluetooth->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  929. if (!bluetooth->bulk_in_buffer) {
  930. err("Couldn't allocate bulk_in_buffer");
  931. goto probe_error;
  932. }
  933. FILL_BULK_URB(bluetooth->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
  934.       bluetooth->bulk_in_buffer, buffer_size, bluetooth_read_bulk_callback, bluetooth);
  935. endpoint = bulk_out_endpoint[0];
  936. bluetooth->bulk_out_endpointAddress = endpoint->bEndpointAddress;
  937. bluetooth->bulk_out_buffer_size = endpoint->wMaxPacketSize * 2;
  938. /* create our write urb pool */ 
  939. for (i = 0; i < NUM_BULK_URBS; ++i) {
  940. struct urb  *urb = usb_alloc_urb(0);
  941. if (urb == NULL) {
  942. err("No free urbs available");
  943. goto probe_error;
  944. }
  945. urb->transfer_buffer = kmalloc (bluetooth->bulk_out_buffer_size, GFP_KERNEL);
  946. if (urb->transfer_buffer == NULL) {
  947. err("out of memory");
  948. goto probe_error;
  949. }
  950. bluetooth->write_urb_pool[i] = urb;
  951. }
  952. endpoint = interrupt_in_endpoint[0];
  953. bluetooth->interrupt_in_urb = usb_alloc_urb(0);
  954. if (!bluetooth->interrupt_in_urb) {
  955. err("No free urbs available");
  956. goto probe_error;
  957. }
  958. bluetooth->interrupt_in_buffer_size = buffer_size = endpoint->wMaxPacketSize;
  959. bluetooth->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
  960. bluetooth->interrupt_in_interval = endpoint->bInterval;
  961. bluetooth->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  962. if (!bluetooth->interrupt_in_buffer) {
  963. err("Couldn't allocate interrupt_in_buffer");
  964. goto probe_error;
  965. }
  966. FILL_INT_URB(bluetooth->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  967.      bluetooth->interrupt_in_buffer, buffer_size, bluetooth_int_callback,
  968.      bluetooth, endpoint->bInterval);
  969. /* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */
  970. tty_register_devfs (&bluetooth_tty_driver, 0, minor);
  971. info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor, minor);
  972. bluetooth_table[minor] = bluetooth;
  973. return bluetooth; /* success */
  974. probe_error:
  975. if (bluetooth->read_urb)
  976. usb_free_urb (bluetooth->read_urb);
  977. if (bluetooth->bulk_in_buffer)
  978. kfree (bluetooth->bulk_in_buffer);
  979. if (bluetooth->interrupt_in_urb)
  980. usb_free_urb (bluetooth->interrupt_in_urb);
  981. if (bluetooth->interrupt_in_buffer)
  982. kfree (bluetooth->interrupt_in_buffer);
  983. for (i = 0; i < NUM_BULK_URBS; ++i)
  984. if (bluetooth->write_urb_pool[i]) {
  985. if (bluetooth->write_urb_pool[i]->transfer_buffer)
  986. kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
  987. usb_free_urb (bluetooth->write_urb_pool[i]);
  988. }
  989. for (i = 0; i < NUM_CONTROL_URBS; ++i) 
  990. if (bluetooth->control_urb_pool[i]) {
  991. if (bluetooth->control_urb_pool[i]->transfer_buffer)
  992. kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
  993. usb_free_urb (bluetooth->control_urb_pool[i]);
  994. }
  995. bluetooth_table[minor] = NULL;
  996. /* free up any memory that we allocated */
  997. kfree (bluetooth);
  998. MOD_DEC_USE_COUNT;
  999. return NULL;
  1000. }
  1001. static void usb_bluetooth_disconnect(struct usb_device *dev, void *ptr)
  1002. {
  1003. struct usb_bluetooth *bluetooth = (struct usb_bluetooth *) ptr;
  1004. int i;
  1005. if (bluetooth) {
  1006. if ((bluetooth->open_count) && (bluetooth->tty))
  1007. tty_hangup(bluetooth->tty);
  1008. bluetooth->open_count = 0;
  1009. if (bluetooth->read_urb) {
  1010. usb_unlink_urb (bluetooth->read_urb);
  1011. usb_free_urb (bluetooth->read_urb);
  1012. }
  1013. if (bluetooth->bulk_in_buffer)
  1014. kfree (bluetooth->bulk_in_buffer);
  1015. if (bluetooth->interrupt_in_urb) {
  1016. usb_unlink_urb (bluetooth->interrupt_in_urb);
  1017. usb_free_urb (bluetooth->interrupt_in_urb);
  1018. }
  1019. if (bluetooth->interrupt_in_buffer)
  1020. kfree (bluetooth->interrupt_in_buffer);
  1021. tty_unregister_devfs (&bluetooth_tty_driver, bluetooth->minor);
  1022. for (i = 0; i < NUM_BULK_URBS; ++i) {
  1023. if (bluetooth->write_urb_pool[i]) {
  1024. usb_unlink_urb (bluetooth->write_urb_pool[i]);
  1025. if (bluetooth->write_urb_pool[i]->transfer_buffer)
  1026. kfree (bluetooth->write_urb_pool[i]->transfer_buffer);
  1027. usb_free_urb (bluetooth->write_urb_pool[i]);
  1028. }
  1029. }
  1030. for (i = 0; i < NUM_CONTROL_URBS; ++i) {
  1031. if (bluetooth->control_urb_pool[i]) {
  1032. usb_unlink_urb (bluetooth->control_urb_pool[i]);
  1033. if (bluetooth->control_urb_pool[i]->transfer_buffer)
  1034. kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
  1035. usb_free_urb (bluetooth->control_urb_pool[i]);
  1036. }
  1037. }
  1038. info("Bluetooth converter now disconnected from ttyUB%d", bluetooth->minor);
  1039. bluetooth_table[bluetooth->minor] = NULL;
  1040. /* free up any memory that we allocated */
  1041. kfree (bluetooth);
  1042. } else {
  1043. info("device disconnected");
  1044. }
  1045. MOD_DEC_USE_COUNT;
  1046. }
  1047. static struct tty_driver bluetooth_tty_driver = {
  1048. .magic = TTY_DRIVER_MAGIC,
  1049. .driver_name = "usb-bluetooth",
  1050. .name = "usb/ttub/%d",
  1051. .major = BLUETOOTH_TTY_MAJOR,
  1052. .minor_start = 0,
  1053. .num = BLUETOOTH_TTY_MINORS,
  1054. .type = TTY_DRIVER_TYPE_SERIAL,
  1055. .subtype = SERIAL_TYPE_NORMAL,
  1056. .flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
  1057. .refcount = &bluetooth_refcount,
  1058. .table = bluetooth_tty,
  1059. .termios = bluetooth_termios,
  1060. .termios_locked = bluetooth_termios_locked,
  1061. .open = bluetooth_open,
  1062. .close = bluetooth_close,
  1063. .write = bluetooth_write,
  1064. .write_room = bluetooth_write_room,
  1065. .ioctl = bluetooth_ioctl,
  1066. .set_termios = bluetooth_set_termios,
  1067. .throttle = bluetooth_throttle,
  1068. .unthrottle = bluetooth_unthrottle,
  1069. .chars_in_buffer = bluetooth_chars_in_buffer,
  1070. };
  1071. int usb_bluetooth_init(void)
  1072. {
  1073. int i;
  1074. int result;
  1075. /* Initalize our global data */
  1076. for (i = 0; i < BLUETOOTH_TTY_MINORS; ++i) {
  1077. bluetooth_table[i] = NULL;
  1078. }
  1079. info ("USB Bluetooth support registered");
  1080. /* register the tty driver */
  1081. bluetooth_tty_driver.init_termios          = tty_std_termios;
  1082. bluetooth_tty_driver.init_termios.c_cflag  = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1083. if (tty_register_driver (&bluetooth_tty_driver)) {
  1084. err("%s - failed to register tty driver", __FUNCTION__);
  1085. return -1;
  1086. }
  1087. /* register the USB driver */
  1088. result = usb_register(&usb_bluetooth_driver);
  1089. if (result < 0) {
  1090. tty_unregister_driver(&bluetooth_tty_driver);
  1091. err("usb_register failed for the USB bluetooth driver. Error number %d", result);
  1092. return -1;
  1093. }
  1094. info(DRIVER_DESC " " DRIVER_VERSION);
  1095. return 0;
  1096. }
  1097. void usb_bluetooth_exit(void)
  1098. {
  1099. usb_deregister(&usb_bluetooth_driver);
  1100. tty_unregister_driver(&bluetooth_tty_driver);
  1101. }
  1102. module_init(usb_bluetooth_init);
  1103. module_exit(usb_bluetooth_exit);
  1104. /* Module information */
  1105. MODULE_AUTHOR( DRIVER_AUTHOR );
  1106. MODULE_DESCRIPTION( DRIVER_DESC );
  1107. MODULE_LICENSE("GPL");