bluetty.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * bluetty.c Version 0.13
  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 corresponding 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. __u8 bulk_out_endpointAddress;
  171. wait_queue_head_t write_wait;
  172. struct work_struct work; /* work queue entry for line discipline waking up */
  173. unsigned int int_packet_pos;
  174. unsigned char int_buffer[EVENT_BUFFER_SIZE];
  175. unsigned int bulk_packet_pos;
  176. unsigned char bulk_buffer[ACL_BUFFER_SIZE]; /* 64k preallocated, fix? */
  177. struct semaphore lock;
  178. };
  179. /* local function prototypes */
  180. static int bluetooth_open (struct tty_struct *tty, struct file *filp);
  181. static void bluetooth_close (struct tty_struct *tty, struct file *filp);
  182. static int bluetooth_write (struct tty_struct *tty, const unsigned char *buf, int count);
  183. static int bluetooth_write_room (struct tty_struct *tty);
  184. static int bluetooth_chars_in_buffer (struct tty_struct *tty);
  185. static void bluetooth_throttle (struct tty_struct *tty);
  186. static void bluetooth_unthrottle (struct tty_struct *tty);
  187. static int bluetooth_ioctl (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  188. static void bluetooth_set_termios (struct tty_struct *tty, struct termios *old);
  189. static void bluetooth_int_callback (struct urb *urb, struct pt_regs *regs);
  190. static void bluetooth_ctrl_callback (struct urb *urb, struct pt_regs *regs);
  191. static void bluetooth_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
  192. static void bluetooth_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
  193. static int usb_bluetooth_probe (struct usb_interface *intf,
  194. const struct usb_device_id *id);
  195. static void usb_bluetooth_disconnect (struct usb_interface *intf);
  196. static struct usb_device_id usb_bluetooth_ids [] = {
  197. { USB_DEVICE_INFO(WIRELESS_CLASS_CODE, RF_SUBCLASS_CODE, BLUETOOTH_PROGRAMMING_PROTOCOL_CODE) },
  198. { } /* Terminating entry */
  199. };
  200. MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
  201. static struct usb_driver usb_bluetooth_driver = {
  202. .owner = THIS_MODULE,
  203. .name = "bluetty",
  204. .probe = usb_bluetooth_probe,
  205. .disconnect = usb_bluetooth_disconnect,
  206. .id_table = usb_bluetooth_ids,
  207. };
  208. static struct tty_driver *bluetooth_tty_driver;
  209. static struct usb_bluetooth *bluetooth_table[BLUETOOTH_TTY_MINORS];
  210. static inline int bluetooth_paranoia_check (struct usb_bluetooth *bluetooth, const char *function)
  211. {
  212. if (!bluetooth) {
  213. dbg("%s - bluetooth == NULL", function);
  214. return -1;
  215. }
  216. if (bluetooth->magic != USB_BLUETOOTH_MAGIC) {
  217. dbg("%s - bad magic number for bluetooth", function);
  218. return -1;
  219. }
  220. return 0;
  221. }
  222. static inline struct usb_bluetooth* get_usb_bluetooth (struct usb_bluetooth *bluetooth, const char *function)
  223. {
  224. if (!bluetooth ||
  225. bluetooth_paranoia_check (bluetooth, function)) {
  226. /* then say that we don't have a valid usb_bluetooth thing, which will
  227. * end up generating -ENODEV return values */
  228. return NULL;
  229. }
  230. return bluetooth;
  231. }
  232. static inline struct usb_bluetooth *get_bluetooth_by_index (int index)
  233. {
  234. return bluetooth_table[index];
  235. }
  236. static int bluetooth_ctrl_msg (struct usb_bluetooth *bluetooth, int request, int value, const unsigned char *buf, int len)
  237. {
  238. struct urb *urb = NULL;
  239. struct usb_ctrlrequest *dr = NULL;
  240. int i;
  241. int status;
  242. dbg ("%s", __FUNCTION__);
  243. /* try to find a free urb in our list */
  244. for (i = 0; i < NUM_CONTROL_URBS; ++i) {
  245. if (bluetooth->control_urb_pool[i]->status != -EINPROGRESS) {
  246. urb = bluetooth->control_urb_pool[i];
  247. dr = &bluetooth->dr[i];
  248. break;
  249. }
  250. }
  251. if (urb == NULL) {
  252. dbg ("%s - no free urbs", __FUNCTION__);
  253. return -ENOMEM;
  254. }
  255. /* keep increasing the urb transfer buffer to fit the size of the message */
  256. if (urb->transfer_buffer == NULL) {
  257. urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
  258. if (urb->transfer_buffer == NULL) {
  259. err ("%s - out of memory", __FUNCTION__);
  260. return -ENOMEM;
  261. }
  262. }
  263. if (urb->transfer_buffer_length < len) {
  264. kfree(urb->transfer_buffer);
  265. urb->transfer_buffer = kmalloc (len, GFP_KERNEL);
  266. if (urb->transfer_buffer == NULL) {
  267. err ("%s - out of memory", __FUNCTION__);
  268. return -ENOMEM;
  269. }
  270. }
  271. memcpy (urb->transfer_buffer, buf, len);
  272. dr->bRequestType= BLUETOOTH_CONTROL_REQUEST_TYPE;
  273. dr->bRequest = request;
  274. dr->wValue = cpu_to_le16((u16) value);
  275. dr->wIndex = cpu_to_le16((u16) bluetooth->control_out_bInterfaceNum);
  276. dr->wLength = cpu_to_le16((u16) len);
  277. usb_fill_control_urb (urb, bluetooth->dev, usb_sndctrlpipe(bluetooth->dev, 0),
  278. (unsigned char*)dr, urb->transfer_buffer, len, bluetooth_ctrl_callback, bluetooth);
  279. /* send it down the pipe */
  280. status = usb_submit_urb(urb, GFP_KERNEL);
  281. if (status)
  282. dbg("%s - usb_submit_urb(control) failed with status = %d", __FUNCTION__, status);
  283. return status;
  284. }
  285. /*****************************************************************************
  286. * Driver tty interface functions
  287. *****************************************************************************/
  288. static int bluetooth_open (struct tty_struct *tty, struct file * filp)
  289. {
  290. struct usb_bluetooth *bluetooth;
  291. int result;
  292. dbg("%s", __FUNCTION__);
  293. /* initialize the pointer incase something fails */
  294. tty->driver_data = NULL;
  295. /* get the bluetooth object associated with this tty pointer */
  296. bluetooth = get_bluetooth_by_index (tty->index);
  297. if (bluetooth_paranoia_check (bluetooth, __FUNCTION__)) {
  298. return -ENODEV;
  299. }
  300. down (&bluetooth->lock);
  301. ++bluetooth->open_count;
  302. if (bluetooth->open_count == 1) {
  303. /* set up our structure making the tty driver remember our object, and us it */
  304. tty->driver_data = bluetooth;
  305. bluetooth->tty = tty;
  306. /* force low_latency on so that our tty_push actually forces the data through,
  307. * otherwise it is scheduled, and with high data rates (like with OHCI) data
  308. * can get lost. */
  309. bluetooth->tty->low_latency = 1;
  310. /* Reset the packet position counters */
  311. bluetooth->int_packet_pos = 0;
  312. bluetooth->bulk_packet_pos = 0;
  313. #ifndef BTBUGGYHARDWARE
  314. /* Start reading from the device */
  315. usb_fill_bulk_urb (bluetooth->read_urb, bluetooth->dev,
  316. usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
  317. bluetooth->bulk_in_buffer,
  318. bluetooth->bulk_in_buffer_size,
  319. bluetooth_read_bulk_callback, bluetooth);
  320. result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL);
  321. if (result)
  322. dbg("%s - usb_submit_urb(read bulk) failed with status %d", __FUNCTION__, result);
  323. #endif
  324. usb_fill_int_urb (bluetooth->interrupt_in_urb, bluetooth->dev,
  325. usb_rcvintpipe(bluetooth->dev, bluetooth->interrupt_in_endpointAddress),
  326. bluetooth->interrupt_in_buffer,
  327. bluetooth->interrupt_in_buffer_size,
  328. bluetooth_int_callback, bluetooth,
  329. bluetooth->interrupt_in_interval);
  330. result = usb_submit_urb(bluetooth->interrupt_in_urb, GFP_KERNEL);
  331. if (result)
  332. dbg("%s - usb_submit_urb(interrupt in) failed with status %d", __FUNCTION__, result);
  333. }
  334. up(&bluetooth->lock);
  335. return 0;
  336. }
  337. static void bluetooth_close (struct tty_struct *tty, struct file * filp)
  338. {
  339. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  340. if (!bluetooth) {
  341. return;
  342. }
  343. dbg("%s", __FUNCTION__);
  344. if (!bluetooth->open_count) {
  345. dbg ("%s - device not opened", __FUNCTION__);
  346. return;
  347. }
  348. down (&bluetooth->lock);
  349. --bluetooth->open_count;
  350. if (bluetooth->open_count <= 0) {
  351. bluetooth->open_count = 0;
  352. /* shutdown any in-flight urbs that we know about */
  353. usb_kill_urb (bluetooth->read_urb);
  354. usb_kill_urb (bluetooth->interrupt_in_urb);
  355. }
  356. up(&bluetooth->lock);
  357. }
  358. static int bluetooth_write (struct tty_struct * tty, const unsigned char *buf, int count)
  359. {
  360. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  361. struct urb *urb = NULL;
  362. unsigned char *temp_buffer = NULL;
  363. const unsigned char *current_buffer;
  364. unsigned char *urb_buffer;
  365. int i;
  366. int retval = 0;
  367. if (!bluetooth) {
  368. return -ENODEV;
  369. }
  370. dbg("%s - %d byte(s)", __FUNCTION__, count);
  371. if (!bluetooth->open_count) {
  372. dbg ("%s - device not opened", __FUNCTION__);
  373. return -EINVAL;
  374. }
  375. if (count == 0) {
  376. dbg("%s - write request of 0 bytes", __FUNCTION__);
  377. return 0;
  378. }
  379. if (count == 1) {
  380. dbg("%s - write request only included type %d", __FUNCTION__, buf[0]);
  381. return 1;
  382. }
  383. #ifdef DEBUG
  384. printk (KERN_DEBUG __FILE__ ": %s - length = %d, data = ", __FUNCTION__, count);
  385. for (i = 0; i < count; ++i) {
  386. printk ("%.2x ", buf[i]);
  387. }
  388. printk ("\n");
  389. #endif
  390. current_buffer = buf;
  391. switch (*current_buffer) {
  392. /* First byte indicates the type of packet */
  393. case CMD_PKT:
  394. /* dbg("%s- Send cmd_pkt len:%d", __FUNCTION__, count);*/
  395. retval = bluetooth_ctrl_msg (bluetooth, 0x00, 0x00, &current_buffer[1], count-1);
  396. if (retval) {
  397. goto exit;
  398. }
  399. retval = count;
  400. break;
  401. case ACL_PKT:
  402. ++current_buffer;
  403. --count;
  404. urb_buffer = kmalloc (count, GFP_ATOMIC);
  405. if (!urb_buffer) {
  406. dev_err(&bluetooth->dev->dev, "out of memory\n");
  407. retval = -ENOMEM;
  408. goto exit;
  409. }
  410. urb = usb_alloc_urb(0, GFP_ATOMIC);
  411. if (!urb) {
  412. dev_err(&bluetooth->dev->dev, "no more free urbs\n");
  413. kfree(urb_buffer);
  414. retval = -ENOMEM;
  415. goto exit;
  416. }
  417. memcpy (urb_buffer, current_buffer, count);
  418. /* build up our urb */
  419. usb_fill_bulk_urb(urb, bluetooth->dev,
  420. usb_sndbulkpipe(bluetooth->dev,
  421. bluetooth->bulk_out_endpointAddress),
  422. urb_buffer,
  423. count,
  424. bluetooth_write_bulk_callback,
  425. bluetooth);
  426. /* send it down the pipe */
  427. retval = usb_submit_urb(urb, GFP_KERNEL);
  428. if (retval) {
  429. dbg("%s - usb_submit_urb(write bulk) failed with error = %d", __FUNCTION__, retval);
  430. goto exit;
  431. }
  432. /* we are done with this urb, so let the host driver
  433. * really free it when it is finished with it */
  434. usb_free_urb (urb);
  435. retval = count + 1;
  436. break;
  437. default :
  438. dbg("%s - unsupported (at this time) write type", __FUNCTION__);
  439. retval = -EINVAL;
  440. break;
  441. }
  442. exit:
  443. kfree(temp_buffer);
  444. return retval;
  445. }
  446. static int bluetooth_write_room (struct tty_struct *tty)
  447. {
  448. dbg("%s", __FUNCTION__);
  449. /*
  450. * We really can take anything the user throws at us
  451. * but let's pick a nice big number to tell the tty
  452. * layer that we have lots of free space
  453. */
  454. return 2048;
  455. }
  456. static int bluetooth_chars_in_buffer (struct tty_struct *tty)
  457. {
  458. dbg("%s", __FUNCTION__);
  459. /*
  460. * We can't really account for how much data we
  461. * have sent out, but hasn't made it through to the
  462. * device, so just tell the tty layer that everything
  463. * is flushed.
  464. */
  465. return 0;
  466. }
  467. static void bluetooth_throttle (struct tty_struct * tty)
  468. {
  469. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  470. if (!bluetooth) {
  471. return;
  472. }
  473. dbg("%s", __FUNCTION__);
  474. if (!bluetooth->open_count) {
  475. dbg ("%s - device not open", __FUNCTION__);
  476. return;
  477. }
  478. dbg("%s unsupported (at this time)", __FUNCTION__);
  479. return;
  480. }
  481. static void bluetooth_unthrottle (struct tty_struct * tty)
  482. {
  483. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  484. if (!bluetooth) {
  485. return;
  486. }
  487. dbg("%s", __FUNCTION__);
  488. if (!bluetooth->open_count) {
  489. dbg ("%s - device not open", __FUNCTION__);
  490. return;
  491. }
  492. dbg("%s unsupported (at this time)", __FUNCTION__);
  493. }
  494. static int bluetooth_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
  495. {
  496. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  497. if (!bluetooth) {
  498. return -ENODEV;
  499. }
  500. dbg("%s - cmd 0x%.4x", __FUNCTION__, cmd);
  501. if (!bluetooth->open_count) {
  502. dbg ("%s - device not open", __FUNCTION__);
  503. return -ENODEV;
  504. }
  505. /* FIXME!!! */
  506. return -ENOIOCTLCMD;
  507. }
  508. static void bluetooth_set_termios (struct tty_struct *tty, struct termios * old)
  509. {
  510. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  511. if (!bluetooth) {
  512. return;
  513. }
  514. dbg("%s", __FUNCTION__);
  515. if (!bluetooth->open_count) {
  516. dbg ("%s - device not open", __FUNCTION__);
  517. return;
  518. }
  519. /* FIXME!!! */
  520. return;
  521. }
  522. #ifdef BTBUGGYHARDWARE
  523. void btusb_enable_bulk_read(struct tty_struct *tty){
  524. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  525. int result;
  526. if (!bluetooth) {
  527. return;
  528. }
  529. dbg("%s", __FUNCTION__);
  530. if (!bluetooth->open_count) {
  531. dbg ("%s - device not open", __FUNCTION__);
  532. return;
  533. }
  534. if (bluetooth->read_urb) {
  535. usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev,
  536. usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
  537. bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
  538. bluetooth_read_bulk_callback, bluetooth);
  539. result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL);
  540. if (result)
  541. err ("%s - failed submitting read urb, error %d", __FUNCTION__, result);
  542. }
  543. }
  544. void btusb_disable_bulk_read(struct tty_struct *tty){
  545. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__);
  546. if (!bluetooth) {
  547. return;
  548. }
  549. dbg("%s", __FUNCTION__);
  550. if (!bluetooth->open_count) {
  551. dbg ("%s - device not open", __FUNCTION__);
  552. return;
  553. }
  554. if ((bluetooth->read_urb) && (bluetooth->read_urb->actual_length))
  555. usb_kill_urb(bluetooth->read_urb);
  556. }
  557. #endif
  558. /*****************************************************************************
  559. * urb callback functions
  560. *****************************************************************************/
  561. static void bluetooth_int_callback (struct urb *urb, struct pt_regs *regs)
  562. {
  563. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
  564. unsigned char *data = urb->transfer_buffer;
  565. unsigned int i;
  566. unsigned int count = urb->actual_length;
  567. unsigned int packet_size;
  568. int status;
  569. dbg("%s", __FUNCTION__);
  570. if (!bluetooth) {
  571. dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
  572. return;
  573. }
  574. switch (urb->status) {
  575. case 0:
  576. /* success */
  577. break;
  578. case -ECONNRESET:
  579. case -ENOENT:
  580. case -ESHUTDOWN:
  581. /* this urb is terminated, clean up */
  582. dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
  583. return;
  584. default:
  585. dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
  586. goto exit;
  587. }
  588. if (!count) {
  589. dbg("%s - zero length int", __FUNCTION__);
  590. goto exit;
  591. }
  592. #ifdef DEBUG
  593. if (count) {
  594. printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count);
  595. for (i = 0; i < count; ++i) {
  596. printk ("%.2x ", data[i]);
  597. }
  598. printk ("\n");
  599. }
  600. #endif
  601. #ifdef BTBUGGYHARDWARE
  602. if ((count >= 2) && (data[0] == 0xFF) && (data[1] == 0x00)) {
  603. data += 2;
  604. count -= 2;
  605. }
  606. if (count == 0) {
  607. urb->actual_length = 0;
  608. goto exit;
  609. }
  610. #endif
  611. /* We add a packet type identifier to the beginning of each
  612. HCI frame. This makes the data in the tty look like a
  613. serial USB devices. Each HCI frame can be broken across
  614. multiple URBs so we buffer them until we have a full hci
  615. packet */
  616. if (!bluetooth->int_packet_pos) {
  617. bluetooth->int_buffer[0] = EVENT_PKT;
  618. bluetooth->int_packet_pos++;
  619. }
  620. if (bluetooth->int_packet_pos + count > EVENT_BUFFER_SIZE) {
  621. err("%s - exceeded EVENT_BUFFER_SIZE", __FUNCTION__);
  622. bluetooth->int_packet_pos = 0;
  623. goto exit;
  624. }
  625. memcpy (&bluetooth->int_buffer[bluetooth->int_packet_pos],
  626. urb->transfer_buffer, count);
  627. bluetooth->int_packet_pos += count;
  628. urb->actual_length = 0;
  629. if (bluetooth->int_packet_pos >= EVENT_HDR_SIZE)
  630. packet_size = bluetooth->int_buffer[2];
  631. else
  632. goto exit;
  633. if (packet_size + EVENT_HDR_SIZE < bluetooth->int_packet_pos) {
  634. err("%s - packet was too long", __FUNCTION__);
  635. bluetooth->int_packet_pos = 0;
  636. goto exit;
  637. }
  638. if (packet_size + EVENT_HDR_SIZE == bluetooth->int_packet_pos) {
  639. for (i = 0; i < bluetooth->int_packet_pos; ++i) {
  640. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them */
  641. if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
  642. tty_flip_buffer_push(bluetooth->tty);
  643. }
  644. tty_insert_flip_char(bluetooth->tty, bluetooth->int_buffer[i], 0);
  645. }
  646. tty_flip_buffer_push(bluetooth->tty);
  647. bluetooth->int_packet_pos = 0;
  648. }
  649. exit:
  650. status = usb_submit_urb (urb, GFP_ATOMIC);
  651. if (status)
  652. err ("%s - usb_submit_urb failed with result %d",
  653. __FUNCTION__, status);
  654. }
  655. static void bluetooth_ctrl_callback (struct urb *urb, struct pt_regs *regs)
  656. {
  657. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
  658. dbg("%s", __FUNCTION__);
  659. if (!bluetooth) {
  660. dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
  661. return;
  662. }
  663. if (urb->status) {
  664. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  665. return;
  666. }
  667. }
  668. static void bluetooth_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  669. {
  670. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
  671. unsigned char *data = urb->transfer_buffer;
  672. unsigned int count = urb->actual_length;
  673. unsigned int i;
  674. unsigned int packet_size;
  675. int result;
  676. dbg("%s", __FUNCTION__);
  677. if (!bluetooth) {
  678. dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
  679. return;
  680. }
  681. if (urb->status) {
  682. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  683. if (urb->status == -ENOENT) {
  684. dbg("%s - URB canceled, won't reschedule", __FUNCTION__);
  685. return;
  686. }
  687. goto exit;
  688. }
  689. if (!count) {
  690. dbg("%s - zero length read bulk", __FUNCTION__);
  691. goto exit;
  692. }
  693. #ifdef DEBUG
  694. if (count) {
  695. printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count);
  696. for (i = 0; i < count; ++i) {
  697. printk ("%.2x ", data[i]);
  698. }
  699. printk ("\n");
  700. }
  701. #endif
  702. #ifdef BTBUGGYHARDWARE
  703. if ((count == 4) && (data[0] == 0x00) && (data[1] == 0x00)
  704. && (data[2] == 0x00) && (data[3] == 0x00)) {
  705. urb->actual_length = 0;
  706. usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev,
  707. usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
  708. bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
  709. bluetooth_read_bulk_callback, bluetooth);
  710. result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL);
  711. if (result)
  712. err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  713. return;
  714. }
  715. #endif
  716. /* We add a packet type identifier to the beginning of each
  717. HCI frame. This makes the data in the tty look like a
  718. serial USB devices. Each HCI frame can be broken across
  719. multiple URBs so we buffer them until we have a full hci
  720. packet */
  721. if (!bluetooth->bulk_packet_pos) {
  722. bluetooth->bulk_buffer[0] = ACL_PKT;
  723. bluetooth->bulk_packet_pos++;
  724. }
  725. if (bluetooth->bulk_packet_pos + count > ACL_BUFFER_SIZE) {
  726. err("%s - exceeded ACL_BUFFER_SIZE", __FUNCTION__);
  727. bluetooth->bulk_packet_pos = 0;
  728. goto exit;
  729. }
  730. memcpy (&bluetooth->bulk_buffer[bluetooth->bulk_packet_pos],
  731. urb->transfer_buffer, count);
  732. bluetooth->bulk_packet_pos += count;
  733. urb->actual_length = 0;
  734. if (bluetooth->bulk_packet_pos >= ACL_HDR_SIZE) {
  735. packet_size = CHAR2INT16(bluetooth->bulk_buffer[4],bluetooth->bulk_buffer[3]);
  736. } else {
  737. goto exit;
  738. }
  739. if (packet_size + ACL_HDR_SIZE < bluetooth->bulk_packet_pos) {
  740. err("%s - packet was too long", __FUNCTION__);
  741. bluetooth->bulk_packet_pos = 0;
  742. goto exit;
  743. }
  744. if (packet_size + ACL_HDR_SIZE == bluetooth->bulk_packet_pos) {
  745. for (i = 0; i < bluetooth->bulk_packet_pos; ++i) {
  746. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  747. if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) {
  748. tty_flip_buffer_push(bluetooth->tty);
  749. }
  750. tty_insert_flip_char(bluetooth->tty, bluetooth->bulk_buffer[i], 0);
  751. }
  752. tty_flip_buffer_push(bluetooth->tty);
  753. bluetooth->bulk_packet_pos = 0;
  754. }
  755. exit:
  756. if (!bluetooth || !bluetooth->open_count)
  757. return;
  758. usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev,
  759. usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress),
  760. bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size,
  761. bluetooth_read_bulk_callback, bluetooth);
  762. result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL);
  763. if (result)
  764. err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  765. return;
  766. }
  767. static void bluetooth_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
  768. {
  769. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__);
  770. dbg("%s", __FUNCTION__);
  771. /* free up the transfer buffer, as usb_free_urb() does not do this */
  772. kfree(urb->transfer_buffer);
  773. if (!bluetooth) {
  774. dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__);
  775. return;
  776. }
  777. if (urb->status) {
  778. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  779. return;
  780. }
  781. /* wake up our little function to let the tty layer know that something happened */
  782. schedule_work(&bluetooth->work);
  783. }
  784. static void bluetooth_softint(void *private)
  785. {
  786. struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)private, __FUNCTION__);
  787. dbg("%s", __FUNCTION__);
  788. if (!bluetooth)
  789. return;
  790. tty_wakeup(bluetooth->tty);
  791. }
  792. static int usb_bluetooth_probe (struct usb_interface *intf,
  793. const struct usb_device_id *id)
  794. {
  795. struct usb_device *dev = interface_to_usbdev (intf);
  796. struct usb_bluetooth *bluetooth = NULL;
  797. struct usb_host_interface *interface;
  798. struct usb_endpoint_descriptor *endpoint;
  799. struct usb_endpoint_descriptor *interrupt_in_endpoint[8];
  800. struct usb_endpoint_descriptor *bulk_in_endpoint[8];
  801. struct usb_endpoint_descriptor *bulk_out_endpoint[8];
  802. int control_out_endpoint;
  803. int minor;
  804. int buffer_size;
  805. int i;
  806. int num_interrupt_in = 0;
  807. int num_bulk_in = 0;
  808. int num_bulk_out = 0;
  809. interface = intf->cur_altsetting;
  810. control_out_endpoint = interface->desc.bInterfaceNumber;
  811. /* find the endpoints that we need */
  812. for (i = 0; i < interface->desc.bNumEndpoints; ++i) {
  813. endpoint = &interface->endpoint[i].desc;
  814. if ((endpoint->bEndpointAddress & 0x80) &&
  815. ((endpoint->bmAttributes & 3) == 0x02)) {
  816. /* we found a bulk in endpoint */
  817. dbg("found bulk in");
  818. bulk_in_endpoint[num_bulk_in] = endpoint;
  819. ++num_bulk_in;
  820. }
  821. if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
  822. ((endpoint->bmAttributes & 3) == 0x02)) {
  823. /* we found a bulk out endpoint */
  824. dbg("found bulk out");
  825. bulk_out_endpoint[num_bulk_out] = endpoint;
  826. ++num_bulk_out;
  827. }
  828. if ((endpoint->bEndpointAddress & 0x80) &&
  829. ((endpoint->bmAttributes & 3) == 0x03)) {
  830. /* we found a interrupt in endpoint */
  831. dbg("found interrupt in");
  832. interrupt_in_endpoint[num_interrupt_in] = endpoint;
  833. ++num_interrupt_in;
  834. }
  835. }
  836. /* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */
  837. if ((num_bulk_in != 1) ||
  838. (num_bulk_out != 1) ||
  839. (num_interrupt_in != 1)) {
  840. dbg ("%s - improper number of endpoints. Bluetooth driver not bound.", __FUNCTION__);
  841. return -EIO;
  842. }
  843. info("USB Bluetooth converter detected");
  844. for (minor = 0; minor < BLUETOOTH_TTY_MINORS && bluetooth_table[minor]; ++minor)
  845. ;
  846. if (bluetooth_table[minor]) {
  847. err("No more free Bluetooth devices");
  848. return -ENODEV;
  849. }
  850. if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) {
  851. err("Out of memory");
  852. return -ENOMEM;
  853. }
  854. memset(bluetooth, 0, sizeof(struct usb_bluetooth));
  855. bluetooth->magic = USB_BLUETOOTH_MAGIC;
  856. bluetooth->dev = dev;
  857. bluetooth->minor = minor;
  858. INIT_WORK(&bluetooth->work, bluetooth_softint, bluetooth);
  859. init_MUTEX(&bluetooth->lock);
  860. /* record the interface number for the control out */
  861. bluetooth->control_out_bInterfaceNum = control_out_endpoint;
  862. /* create our control out urb pool */
  863. for (i = 0; i < NUM_CONTROL_URBS; ++i) {
  864. struct urb *urb = usb_alloc_urb(0, GFP_KERNEL);
  865. if (urb == NULL) {
  866. err("No free urbs available");
  867. goto probe_error;
  868. }
  869. urb->transfer_buffer = NULL;
  870. bluetooth->control_urb_pool[i] = urb;
  871. }
  872. /* set up the endpoint information */
  873. endpoint = bulk_in_endpoint[0];
  874. bluetooth->read_urb = usb_alloc_urb (0, GFP_KERNEL);
  875. if (!bluetooth->read_urb) {
  876. err("No free urbs available");
  877. goto probe_error;
  878. }
  879. bluetooth->bulk_in_buffer_size = buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  880. bluetooth->bulk_in_endpointAddress = endpoint->bEndpointAddress;
  881. bluetooth->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  882. if (!bluetooth->bulk_in_buffer) {
  883. err("Couldn't allocate bulk_in_buffer");
  884. goto probe_error;
  885. }
  886. usb_fill_bulk_urb(bluetooth->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
  887. bluetooth->bulk_in_buffer, buffer_size, bluetooth_read_bulk_callback, bluetooth);
  888. endpoint = bulk_out_endpoint[0];
  889. bluetooth->bulk_out_endpointAddress = endpoint->bEndpointAddress;
  890. bluetooth->bulk_out_buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
  891. endpoint = interrupt_in_endpoint[0];
  892. bluetooth->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  893. if (!bluetooth->interrupt_in_urb) {
  894. err("No free urbs available");
  895. goto probe_error;
  896. }
  897. bluetooth->interrupt_in_buffer_size = buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  898. bluetooth->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
  899. bluetooth->interrupt_in_interval = endpoint->bInterval;
  900. bluetooth->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  901. if (!bluetooth->interrupt_in_buffer) {
  902. err("Couldn't allocate interrupt_in_buffer");
  903. goto probe_error;
  904. }
  905. usb_fill_int_urb(bluetooth->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  906. bluetooth->interrupt_in_buffer, buffer_size, bluetooth_int_callback,
  907. bluetooth, endpoint->bInterval);
  908. /* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */
  909. tty_register_device (bluetooth_tty_driver, minor, &intf->dev);
  910. info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor, minor);
  911. bluetooth_table[minor] = bluetooth;
  912. /* success */
  913. usb_set_intfdata (intf, bluetooth);
  914. return 0;
  915. probe_error:
  916. if (bluetooth->read_urb)
  917. usb_free_urb (bluetooth->read_urb);
  918. if (bluetooth->bulk_in_buffer)
  919. kfree (bluetooth->bulk_in_buffer);
  920. if (bluetooth->interrupt_in_urb)
  921. usb_free_urb (bluetooth->interrupt_in_urb);
  922. if (bluetooth->interrupt_in_buffer)
  923. kfree (bluetooth->interrupt_in_buffer);
  924. for (i = 0; i < NUM_CONTROL_URBS; ++i)
  925. if (bluetooth->control_urb_pool[i]) {
  926. if (bluetooth->control_urb_pool[i]->transfer_buffer)
  927. kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
  928. usb_free_urb (bluetooth->control_urb_pool[i]);
  929. }
  930. bluetooth_table[minor] = NULL;
  931. /* free up any memory that we allocated */
  932. kfree (bluetooth);
  933. return -EIO;
  934. }
  935. static void usb_bluetooth_disconnect(struct usb_interface *intf)
  936. {
  937. struct usb_bluetooth *bluetooth = usb_get_intfdata (intf);
  938. int i;
  939. usb_set_intfdata (intf, NULL);
  940. if (bluetooth) {
  941. if ((bluetooth->open_count) && (bluetooth->tty))
  942. tty_hangup(bluetooth->tty);
  943. bluetooth->open_count = 0;
  944. if (bluetooth->read_urb) {
  945. usb_kill_urb (bluetooth->read_urb);
  946. usb_free_urb (bluetooth->read_urb);
  947. }
  948. if (bluetooth->bulk_in_buffer)
  949. kfree (bluetooth->bulk_in_buffer);
  950. if (bluetooth->interrupt_in_urb) {
  951. usb_kill_urb (bluetooth->interrupt_in_urb);
  952. usb_free_urb (bluetooth->interrupt_in_urb);
  953. }
  954. if (bluetooth->interrupt_in_buffer)
  955. kfree (bluetooth->interrupt_in_buffer);
  956. tty_unregister_device (bluetooth_tty_driver, bluetooth->minor);
  957. for (i = 0; i < NUM_CONTROL_URBS; ++i) {
  958. if (bluetooth->control_urb_pool[i]) {
  959. usb_kill_urb (bluetooth->control_urb_pool[i]);
  960. if (bluetooth->control_urb_pool[i]->transfer_buffer)
  961. kfree (bluetooth->control_urb_pool[i]->transfer_buffer);
  962. usb_free_urb (bluetooth->control_urb_pool[i]);
  963. }
  964. }
  965. info("Bluetooth converter now disconnected from ttyUB%d", bluetooth->minor);
  966. bluetooth_table[bluetooth->minor] = NULL;
  967. /* free up any memory that we allocated */
  968. kfree (bluetooth);
  969. } else {
  970. info("device disconnected");
  971. }
  972. }
  973. static struct tty_operations bluetooth_ops = {
  974. .open = bluetooth_open,
  975. .close = bluetooth_close,
  976. .write = bluetooth_write,
  977. .write_room = bluetooth_write_room,
  978. .ioctl = bluetooth_ioctl,
  979. .set_termios = bluetooth_set_termios,
  980. .throttle = bluetooth_throttle,
  981. .unthrottle = bluetooth_unthrottle,
  982. .chars_in_buffer = bluetooth_chars_in_buffer,
  983. };
  984. static int usb_bluetooth_init(void)
  985. {
  986. int i;
  987. int result;
  988. /* Initialize our global data */
  989. for (i = 0; i < BLUETOOTH_TTY_MINORS; ++i) {
  990. bluetooth_table[i] = NULL;
  991. }
  992. info ("USB Bluetooth support registered");
  993. bluetooth_tty_driver = alloc_tty_driver(BLUETOOTH_TTY_MINORS);
  994. if (!bluetooth_tty_driver)
  995. return -ENOMEM;
  996. bluetooth_tty_driver->owner = THIS_MODULE;
  997. bluetooth_tty_driver->driver_name = "usb-bluetooth";
  998. bluetooth_tty_driver->name = "ttyUB";
  999. bluetooth_tty_driver->devfs_name = "usb/ttub/";
  1000. bluetooth_tty_driver->major = BLUETOOTH_TTY_MAJOR;
  1001. bluetooth_tty_driver->minor_start = 0;
  1002. bluetooth_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1003. bluetooth_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  1004. bluetooth_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
  1005. bluetooth_tty_driver->init_termios = tty_std_termios;
  1006. bluetooth_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1007. tty_set_operations(bluetooth_tty_driver, &bluetooth_ops);
  1008. if (tty_register_driver (bluetooth_tty_driver)) {
  1009. err("%s - failed to register tty driver", __FUNCTION__);
  1010. put_tty_driver(bluetooth_tty_driver);
  1011. return -1;
  1012. }
  1013. /* register the USB driver */
  1014. result = usb_register(&usb_bluetooth_driver);
  1015. if (result < 0) {
  1016. tty_unregister_driver(bluetooth_tty_driver);
  1017. put_tty_driver(bluetooth_tty_driver);
  1018. err("usb_register failed for the USB bluetooth driver. Error number %d", result);
  1019. return -1;
  1020. }
  1021. info(DRIVER_DESC " " DRIVER_VERSION);
  1022. return 0;
  1023. }
  1024. static void usb_bluetooth_exit(void)
  1025. {
  1026. usb_deregister(&usb_bluetooth_driver);
  1027. tty_unregister_driver(bluetooth_tty_driver);
  1028. put_tty_driver(bluetooth_tty_driver);
  1029. }
  1030. module_init(usb_bluetooth_init);
  1031. module_exit(usb_bluetooth_exit);
  1032. /* Module information */
  1033. MODULE_AUTHOR( DRIVER_AUTHOR );
  1034. MODULE_DESCRIPTION( DRIVER_DESC );
  1035. MODULE_LICENSE("GPL");