ir-usb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * USB IR Dongle driver
  3. *
  4. * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2002 Gary Brubaker (xavyer@ix.netcom.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This driver allows a USB IrDA device to be used as a "dumb" serial device.
  13. * This can be useful if you do not have access to a full IrDA stack on the
  14. * other side of the connection. If you do have an IrDA stack on both devices,
  15. * please use the usb-irda driver, as it contains the proper error checking and
  16. * other goodness of a full IrDA stack.
  17. *
  18. * Portions of this driver were taken from drivers/net/irda/irda-usb.c, which
  19. * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli
  20. * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com>
  21. *
  22. * See Documentation/usb/usb-serial.txt for more information on using this driver
  23. *
  24. * 2007_Jun_21 Alan Cox <alan@redhat.com>
  25. * Minimal cleanups for some of the driver problens and tty layer abuse.
  26. * Still needs fixing to allow multiple dongles.
  27. *
  28. * 2002_Mar_07 greg kh
  29. * moved some needed structures and #define values from the
  30. * net/irda/irda-usb.h file into our file, as we don't want to depend on
  31. * that codebase compiling correctly :)
  32. *
  33. * 2002_Jan_14 gb
  34. * Added module parameter to force specific number of XBOFs.
  35. * Added ir_xbof_change().
  36. * Reorganized read_bulk_callback error handling.
  37. * Switched from FILL_BULK_URB() to usb_fill_bulk_urb().
  38. *
  39. * 2001_Nov_08 greg kh
  40. * Changed the irda_usb_find_class_desc() function based on comments and
  41. * code from Martin Diehl.
  42. *
  43. * 2001_Nov_01 greg kh
  44. * Added support for more IrDA USB devices.
  45. * Added support for zero packet. Added buffer override paramater, so
  46. * users can transfer larger packets at once if they wish. Both patches
  47. * came from Dag Brattli <dag@obexcode.com>.
  48. *
  49. * 2001_Oct_07 greg kh
  50. * initial version released.
  51. */
  52. #include <linux/kernel.h>
  53. #include <linux/errno.h>
  54. #include <linux/init.h>
  55. #include <linux/slab.h>
  56. #include <linux/tty.h>
  57. #include <linux/tty_driver.h>
  58. #include <linux/tty_flip.h>
  59. #include <linux/module.h>
  60. #include <linux/spinlock.h>
  61. #include <asm/uaccess.h>
  62. #include <linux/usb.h>
  63. #include <linux/usb/serial.h>
  64. /*
  65. * Version Information
  66. */
  67. #define DRIVER_VERSION "v0.4"
  68. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  69. #define DRIVER_DESC "USB IR Dongle driver"
  70. /* USB IrDA class spec information */
  71. #define USB_CLASS_IRDA 0x02
  72. #define USB_DT_IRDA 0x21
  73. #define IU_REQ_GET_CLASS_DESC 0x06
  74. #define SPEED_2400 0x01
  75. #define SPEED_9600 0x02
  76. #define SPEED_19200 0x03
  77. #define SPEED_38400 0x04
  78. #define SPEED_57600 0x05
  79. #define SPEED_115200 0x06
  80. #define SPEED_576000 0x07
  81. #define SPEED_1152000 0x08
  82. #define SPEED_4000000 0x09
  83. struct irda_class_desc {
  84. u8 bLength;
  85. u8 bDescriptorType;
  86. u16 bcdSpecRevision;
  87. u8 bmDataSize;
  88. u8 bmWindowSize;
  89. u8 bmMinTurnaroundTime;
  90. u16 wBaudRate;
  91. u8 bmAdditionalBOFs;
  92. u8 bIrdaRateSniff;
  93. u8 bMaxUnicastList;
  94. } __attribute__ ((packed));
  95. static int debug;
  96. /* if overridden by the user, then use their value for the size of the read and
  97. * write urbs */
  98. static int buffer_size;
  99. /* if overridden by the user, then use the specified number of XBOFs */
  100. static int xbof = -1;
  101. static int ir_startup (struct usb_serial *serial);
  102. static int ir_open (struct usb_serial_port *port, struct file *filep);
  103. static void ir_close (struct usb_serial_port *port, struct file *filep);
  104. static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  105. static void ir_write_bulk_callback (struct urb *urb);
  106. static void ir_read_bulk_callback (struct urb *urb);
  107. static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
  108. /* Not that this lot means you can only have one per system */
  109. static u8 ir_baud = 0;
  110. static u8 ir_xbof = 0;
  111. static u8 ir_add_bof = 0;
  112. static struct usb_device_id id_table [] = {
  113. { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
  114. { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
  115. { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
  116. { USB_INTERFACE_INFO (USB_CLASS_APP_SPEC, USB_CLASS_IRDA, 0) },
  117. { } /* Terminating entry */
  118. };
  119. MODULE_DEVICE_TABLE (usb, id_table);
  120. static struct usb_driver ir_driver = {
  121. .name = "ir-usb",
  122. .probe = usb_serial_probe,
  123. .disconnect = usb_serial_disconnect,
  124. .id_table = id_table,
  125. .no_dynamic_id = 1,
  126. };
  127. static struct usb_serial_driver ir_device = {
  128. .driver = {
  129. .owner = THIS_MODULE,
  130. .name = "ir-usb",
  131. },
  132. .description = "IR Dongle",
  133. .usb_driver = &ir_driver,
  134. .id_table = id_table,
  135. .num_interrupt_in = 1,
  136. .num_bulk_in = 1,
  137. .num_bulk_out = 1,
  138. .num_ports = 1,
  139. .set_termios = ir_set_termios,
  140. .attach = ir_startup,
  141. .open = ir_open,
  142. .close = ir_close,
  143. .write = ir_write,
  144. .write_bulk_callback = ir_write_bulk_callback,
  145. .read_bulk_callback = ir_read_bulk_callback,
  146. };
  147. static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
  148. {
  149. dbg("bLength=%x", desc->bLength);
  150. dbg("bDescriptorType=%x", desc->bDescriptorType);
  151. dbg("bcdSpecRevision=%x", desc->bcdSpecRevision);
  152. dbg("bmDataSize=%x", desc->bmDataSize);
  153. dbg("bmWindowSize=%x", desc->bmWindowSize);
  154. dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
  155. dbg("wBaudRate=%x", desc->wBaudRate);
  156. dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
  157. dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
  158. dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
  159. }
  160. /*------------------------------------------------------------------*/
  161. /*
  162. * Function irda_usb_find_class_desc(dev, ifnum)
  163. *
  164. * Returns instance of IrDA class descriptor, or NULL if not found
  165. *
  166. * The class descriptor is some extra info that IrDA USB devices will
  167. * offer to us, describing their IrDA characteristics. We will use that in
  168. * irda_usb_init_qos()
  169. *
  170. * Based on the same function in drivers/net/irda/irda-usb.c
  171. */
  172. static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
  173. {
  174. struct irda_class_desc *desc;
  175. int ret;
  176. desc = kzalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
  177. if (desc == NULL)
  178. return NULL;
  179. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
  180. IU_REQ_GET_CLASS_DESC,
  181. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  182. 0, ifnum, desc, sizeof(*desc), 1000);
  183. dbg("%s - ret=%d", __FUNCTION__, ret);
  184. if (ret < sizeof(*desc)) {
  185. dbg("%s - class descriptor read %s (%d)",
  186. __FUNCTION__,
  187. (ret<0) ? "failed" : "too short",
  188. ret);
  189. goto error;
  190. }
  191. if (desc->bDescriptorType != USB_DT_IRDA) {
  192. dbg("%s - bad class descriptor type", __FUNCTION__);
  193. goto error;
  194. }
  195. irda_usb_dump_class_desc(desc);
  196. return desc;
  197. error:
  198. kfree(desc);
  199. return NULL;
  200. }
  201. static u8 ir_xbof_change(u8 xbof)
  202. {
  203. u8 result;
  204. /* reference irda-usb.c */
  205. switch(xbof) {
  206. case 48: result = 0x10; break;
  207. case 28:
  208. case 24: result = 0x20; break;
  209. default:
  210. case 12: result = 0x30; break;
  211. case 5:
  212. case 6: result = 0x40; break;
  213. case 3: result = 0x50; break;
  214. case 2: result = 0x60; break;
  215. case 1: result = 0x70; break;
  216. case 0: result = 0x80; break;
  217. }
  218. return(result);
  219. }
  220. static int ir_startup (struct usb_serial *serial)
  221. {
  222. struct irda_class_desc *irda_desc;
  223. irda_desc = irda_usb_find_class_desc (serial->dev, 0);
  224. if (irda_desc == NULL) {
  225. dev_err (&serial->dev->dev, "IRDA class descriptor not found, device not bound\n");
  226. return -ENODEV;
  227. }
  228. dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
  229. __FUNCTION__,
  230. (irda_desc->wBaudRate & 0x0001) ? " 2400" : "",
  231. (irda_desc->wBaudRate & 0x0002) ? " 9600" : "",
  232. (irda_desc->wBaudRate & 0x0004) ? " 19200" : "",
  233. (irda_desc->wBaudRate & 0x0008) ? " 38400" : "",
  234. (irda_desc->wBaudRate & 0x0010) ? " 57600" : "",
  235. (irda_desc->wBaudRate & 0x0020) ? " 115200" : "",
  236. (irda_desc->wBaudRate & 0x0040) ? " 576000" : "",
  237. (irda_desc->wBaudRate & 0x0080) ? " 1152000" : "",
  238. (irda_desc->wBaudRate & 0x0100) ? " 4000000" : "");
  239. switch( irda_desc->bmAdditionalBOFs ) {
  240. case 0x01: ir_add_bof = 48; break;
  241. case 0x02: ir_add_bof = 24; break;
  242. case 0x04: ir_add_bof = 12; break;
  243. case 0x08: ir_add_bof = 6; break;
  244. case 0x10: ir_add_bof = 3; break;
  245. case 0x20: ir_add_bof = 2; break;
  246. case 0x40: ir_add_bof = 1; break;
  247. case 0x80: ir_add_bof = 0; break;
  248. default:;
  249. }
  250. kfree (irda_desc);
  251. return 0;
  252. }
  253. static int ir_open (struct usb_serial_port *port, struct file *filp)
  254. {
  255. char *buffer;
  256. int result = 0;
  257. dbg("%s - port %d", __FUNCTION__, port->number);
  258. if (buffer_size) {
  259. /* override the default buffer sizes */
  260. buffer = kmalloc (buffer_size, GFP_KERNEL);
  261. if (!buffer) {
  262. dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
  263. return -ENOMEM;
  264. }
  265. kfree (port->read_urb->transfer_buffer);
  266. port->read_urb->transfer_buffer = buffer;
  267. port->read_urb->transfer_buffer_length = buffer_size;
  268. buffer = kmalloc (buffer_size, GFP_KERNEL);
  269. if (!buffer) {
  270. dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
  271. return -ENOMEM;
  272. }
  273. kfree (port->write_urb->transfer_buffer);
  274. port->write_urb->transfer_buffer = buffer;
  275. port->write_urb->transfer_buffer_length = buffer_size;
  276. port->bulk_out_size = buffer_size;
  277. }
  278. /* Start reading from the device */
  279. usb_fill_bulk_urb (
  280. port->read_urb,
  281. port->serial->dev,
  282. usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
  283. port->read_urb->transfer_buffer,
  284. port->read_urb->transfer_buffer_length,
  285. ir_read_bulk_callback,
  286. port);
  287. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  288. if (result)
  289. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  290. return result;
  291. }
  292. static void ir_close (struct usb_serial_port *port, struct file * filp)
  293. {
  294. dbg("%s - port %d", __FUNCTION__, port->number);
  295. /* shutdown our bulk read */
  296. usb_kill_urb(port->read_urb);
  297. }
  298. static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  299. {
  300. unsigned char *transfer_buffer;
  301. int result;
  302. int transfer_size;
  303. dbg("%s - port = %d, count = %d", __FUNCTION__, port->number, count);
  304. if (!port->tty) {
  305. dev_err (&port->dev, "%s - no tty???\n", __FUNCTION__);
  306. return 0;
  307. }
  308. if (count == 0)
  309. return 0;
  310. spin_lock_bh(&port->lock);
  311. if (port->write_urb_busy) {
  312. spin_unlock_bh(&port->lock);
  313. dbg("%s - already writing", __FUNCTION__);
  314. return 0;
  315. }
  316. port->write_urb_busy = 1;
  317. spin_unlock_bh(&port->lock);
  318. transfer_buffer = port->write_urb->transfer_buffer;
  319. transfer_size = min(count, port->bulk_out_size - 1);
  320. /*
  321. * The first byte of the packet we send to the device contains an
  322. * inband header which indicates an additional number of BOFs and
  323. * a baud rate change.
  324. *
  325. * See section 5.4.2.2 of the USB IrDA spec.
  326. */
  327. *transfer_buffer = ir_xbof | ir_baud;
  328. ++transfer_buffer;
  329. memcpy (transfer_buffer, buf, transfer_size);
  330. usb_fill_bulk_urb (
  331. port->write_urb,
  332. port->serial->dev,
  333. usb_sndbulkpipe(port->serial->dev,
  334. port->bulk_out_endpointAddress),
  335. port->write_urb->transfer_buffer,
  336. transfer_size + 1,
  337. ir_write_bulk_callback,
  338. port);
  339. port->write_urb->transfer_flags = URB_ZERO_PACKET;
  340. result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
  341. if (result) {
  342. port->write_urb_busy = 0;
  343. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  344. } else
  345. result = transfer_size;
  346. return result;
  347. }
  348. static void ir_write_bulk_callback (struct urb *urb)
  349. {
  350. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  351. int status = urb->status;
  352. dbg("%s - port %d", __FUNCTION__, port->number);
  353. port->write_urb_busy = 0;
  354. if (status) {
  355. dbg("%s - nonzero write bulk status received: %d",
  356. __FUNCTION__, status);
  357. return;
  358. }
  359. usb_serial_debug_data (
  360. debug,
  361. &port->dev,
  362. __FUNCTION__,
  363. urb->actual_length,
  364. urb->transfer_buffer);
  365. usb_serial_port_softint(port);
  366. }
  367. static void ir_read_bulk_callback (struct urb *urb)
  368. {
  369. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  370. struct tty_struct *tty;
  371. unsigned char *data = urb->transfer_buffer;
  372. int result;
  373. int status = urb->status;
  374. dbg("%s - port %d", __FUNCTION__, port->number);
  375. if (!port->open_count) {
  376. dbg("%s - port closed.", __FUNCTION__);
  377. return;
  378. }
  379. switch (status) {
  380. case 0: /* Successful */
  381. /*
  382. * The first byte of the packet we get from the device
  383. * contains a busy indicator and baud rate change.
  384. * See section 5.4.1.2 of the USB IrDA spec.
  385. */
  386. if ((*data & 0x0f) > 0)
  387. ir_baud = *data & 0x0f;
  388. usb_serial_debug_data (
  389. debug,
  390. &port->dev,
  391. __FUNCTION__,
  392. urb->actual_length,
  393. data);
  394. tty = port->tty;
  395. if (tty_buffer_request_room(tty, urb->actual_length - 1)) {
  396. tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
  397. tty_flip_buffer_push(tty);
  398. }
  399. /*
  400. * No break here.
  401. * We want to resubmit the urb so we can read
  402. * again.
  403. */
  404. case -EPROTO: /* taking inspiration from pl2303.c */
  405. /* Continue trying to always read */
  406. usb_fill_bulk_urb (
  407. port->read_urb,
  408. port->serial->dev,
  409. usb_rcvbulkpipe(port->serial->dev,
  410. port->bulk_in_endpointAddress),
  411. port->read_urb->transfer_buffer,
  412. port->read_urb->transfer_buffer_length,
  413. ir_read_bulk_callback,
  414. port);
  415. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  416. if (result)
  417. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
  418. __FUNCTION__, result);
  419. break ;
  420. default:
  421. dbg("%s - nonzero read bulk status received: %d",
  422. __FUNCTION__,
  423. status);
  424. break ;
  425. }
  426. return;
  427. }
  428. static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
  429. {
  430. unsigned char *transfer_buffer;
  431. int result;
  432. speed_t baud;
  433. int ir_baud;
  434. dbg("%s - port %d", __FUNCTION__, port->number);
  435. if ((!port->tty) || (!port->tty->termios)) {
  436. dbg("%s - no tty structures", __FUNCTION__);
  437. return;
  438. }
  439. baud = tty_get_baud_rate(port->tty);
  440. /*
  441. * FIXME, we should compare the baud request against the
  442. * capability stated in the IR header that we got in the
  443. * startup function.
  444. */
  445. switch (baud) {
  446. case 2400: ir_baud = SPEED_2400; break;
  447. case 9600: ir_baud = SPEED_9600; break;
  448. case 19200: ir_baud = SPEED_19200; break;
  449. case 38400: ir_baud = SPEED_38400; break;
  450. case 57600: ir_baud = SPEED_57600; break;
  451. case 115200: ir_baud = SPEED_115200; break;
  452. case 576000: ir_baud = SPEED_576000; break;
  453. case 1152000: ir_baud = SPEED_1152000; break;
  454. case 4000000: ir_baud = SPEED_4000000; break;
  455. break;
  456. default:
  457. ir_baud = SPEED_9600;
  458. baud = 9600;
  459. /* And once the new tty stuff is all done we need to
  460. call back to correct the baud bits */
  461. }
  462. if (xbof == -1)
  463. ir_xbof = ir_xbof_change(ir_add_bof);
  464. else
  465. ir_xbof = ir_xbof_change(xbof) ;
  466. /* FIXME need to check to see if our write urb is busy right
  467. * now, or use a urb pool.
  468. *
  469. * send the baud change out on an "empty" data packet
  470. */
  471. transfer_buffer = port->write_urb->transfer_buffer;
  472. *transfer_buffer = ir_xbof | ir_baud;
  473. usb_fill_bulk_urb (
  474. port->write_urb,
  475. port->serial->dev,
  476. usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
  477. port->write_urb->transfer_buffer,
  478. 1,
  479. ir_write_bulk_callback,
  480. port);
  481. port->write_urb->transfer_flags = URB_ZERO_PACKET;
  482. result = usb_submit_urb (port->write_urb, GFP_KERNEL);
  483. if (result)
  484. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  485. }
  486. static int __init ir_init (void)
  487. {
  488. int retval;
  489. retval = usb_serial_register(&ir_device);
  490. if (retval)
  491. goto failed_usb_serial_register;
  492. retval = usb_register(&ir_driver);
  493. if (retval)
  494. goto failed_usb_register;
  495. info(DRIVER_DESC " " DRIVER_VERSION);
  496. return 0;
  497. failed_usb_register:
  498. usb_serial_deregister(&ir_device);
  499. failed_usb_serial_register:
  500. return retval;
  501. }
  502. static void __exit ir_exit (void)
  503. {
  504. usb_deregister (&ir_driver);
  505. usb_serial_deregister (&ir_device);
  506. }
  507. module_init(ir_init);
  508. module_exit(ir_exit);
  509. MODULE_AUTHOR(DRIVER_AUTHOR);
  510. MODULE_DESCRIPTION(DRIVER_DESC);
  511. MODULE_LICENSE("GPL");
  512. module_param(debug, bool, S_IRUGO | S_IWUSR);
  513. MODULE_PARM_DESC(debug, "Debug enabled or not");
  514. module_param(xbof, int, 0);
  515. MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
  516. module_param(buffer_size, int, 0);
  517. MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");