ir-usb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. * Copyright (C) 2010 Johan Hovold (jhovold@gmail.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This driver allows a USB IrDA device to be used as a "dumb" serial device.
  14. * This can be useful if you do not have access to a full IrDA stack on the
  15. * other side of the connection. If you do have an IrDA stack on both devices,
  16. * please use the usb-irda driver, as it contains the proper error checking and
  17. * other goodness of a full IrDA stack.
  18. *
  19. * Portions of this driver were taken from drivers/net/irda/irda-usb.c, which
  20. * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli
  21. * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com>
  22. *
  23. * See Documentation/usb/usb-serial.txt for more information on using this
  24. * driver
  25. *
  26. * 2008_Jun_02 Felipe Balbi <me@felipebalbi.com>
  27. * Introduced common header to be used also in USB Gadget Framework.
  28. * Still needs some other style fixes.
  29. *
  30. * 2007_Jun_21 Alan Cox <alan@lxorguk.ukuu.org.uk>
  31. * Minimal cleanups for some of the driver problens and tty layer abuse.
  32. * Still needs fixing to allow multiple dongles.
  33. *
  34. * 2002_Mar_07 greg kh
  35. * moved some needed structures and #define values from the
  36. * net/irda/irda-usb.h file into our file, as we don't want to depend on
  37. * that codebase compiling correctly :)
  38. *
  39. * 2002_Jan_14 gb
  40. * Added module parameter to force specific number of XBOFs.
  41. * Added ir_xbof_change().
  42. * Reorganized read_bulk_callback error handling.
  43. * Switched from FILL_BULK_URB() to usb_fill_bulk_urb().
  44. *
  45. * 2001_Nov_08 greg kh
  46. * Changed the irda_usb_find_class_desc() function based on comments and
  47. * code from Martin Diehl.
  48. *
  49. * 2001_Nov_01 greg kh
  50. * Added support for more IrDA USB devices.
  51. * Added support for zero packet. Added buffer override paramater, so
  52. * users can transfer larger packets at once if they wish. Both patches
  53. * came from Dag Brattli <dag@obexcode.com>.
  54. *
  55. * 2001_Oct_07 greg kh
  56. * initial version released.
  57. */
  58. #include <linux/kernel.h>
  59. #include <linux/errno.h>
  60. #include <linux/init.h>
  61. #include <linux/slab.h>
  62. #include <linux/tty.h>
  63. #include <linux/tty_driver.h>
  64. #include <linux/tty_flip.h>
  65. #include <linux/module.h>
  66. #include <linux/spinlock.h>
  67. #include <linux/uaccess.h>
  68. #include <linux/usb.h>
  69. #include <linux/usb/serial.h>
  70. #include <linux/usb/irda.h>
  71. /*
  72. * Version Information
  73. */
  74. #define DRIVER_VERSION "v0.5"
  75. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Johan Hovold <jhovold@gmail.com>"
  76. #define DRIVER_DESC "USB IR Dongle driver"
  77. static int debug;
  78. /* if overridden by the user, then use their value for the size of the read and
  79. * write urbs */
  80. static int buffer_size;
  81. /* if overridden by the user, then use the specified number of XBOFs */
  82. static int xbof = -1;
  83. static int ir_startup (struct usb_serial *serial);
  84. static int ir_open(struct tty_struct *tty, struct usb_serial_port *port);
  85. static int ir_prepare_write_buffer(struct usb_serial_port *port,
  86. void *dest, size_t size);
  87. static void ir_process_read_urb(struct urb *urb);
  88. static void ir_set_termios(struct tty_struct *tty,
  89. struct usb_serial_port *port, struct ktermios *old_termios);
  90. /* Not that this lot means you can only have one per system */
  91. static u8 ir_baud;
  92. static u8 ir_xbof;
  93. static u8 ir_add_bof;
  94. static const struct usb_device_id ir_id_table[] = {
  95. { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
  96. { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
  97. { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
  98. { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
  99. { } /* Terminating entry */
  100. };
  101. MODULE_DEVICE_TABLE(usb, ir_id_table);
  102. static struct usb_driver ir_driver = {
  103. .name = "ir-usb",
  104. .probe = usb_serial_probe,
  105. .disconnect = usb_serial_disconnect,
  106. .id_table = ir_id_table,
  107. .no_dynamic_id = 1,
  108. };
  109. static struct usb_serial_driver ir_device = {
  110. .driver = {
  111. .owner = THIS_MODULE,
  112. .name = "ir-usb",
  113. },
  114. .description = "IR Dongle",
  115. .usb_driver = &ir_driver,
  116. .id_table = ir_id_table,
  117. .num_ports = 1,
  118. .set_termios = ir_set_termios,
  119. .attach = ir_startup,
  120. .open = ir_open,
  121. .prepare_write_buffer = ir_prepare_write_buffer,
  122. .process_read_urb = ir_process_read_urb,
  123. };
  124. static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
  125. {
  126. dbg("bLength=%x", desc->bLength);
  127. dbg("bDescriptorType=%x", desc->bDescriptorType);
  128. dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
  129. dbg("bmDataSize=%x", desc->bmDataSize);
  130. dbg("bmWindowSize=%x", desc->bmWindowSize);
  131. dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
  132. dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
  133. dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
  134. dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
  135. dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
  136. }
  137. /*------------------------------------------------------------------*/
  138. /*
  139. * Function irda_usb_find_class_desc(dev, ifnum)
  140. *
  141. * Returns instance of IrDA class descriptor, or NULL if not found
  142. *
  143. * The class descriptor is some extra info that IrDA USB devices will
  144. * offer to us, describing their IrDA characteristics. We will use that in
  145. * irda_usb_init_qos()
  146. *
  147. * Based on the same function in drivers/net/irda/irda-usb.c
  148. */
  149. static struct usb_irda_cs_descriptor *
  150. irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
  151. {
  152. struct usb_irda_cs_descriptor *desc;
  153. int ret;
  154. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  155. if (!desc)
  156. return NULL;
  157. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  158. USB_REQ_CS_IRDA_GET_CLASS_DESC,
  159. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  160. 0, ifnum, desc, sizeof(*desc), 1000);
  161. dbg("%s - ret=%d", __func__, ret);
  162. if (ret < sizeof(*desc)) {
  163. dbg("%s - class descriptor read %s (%d)",
  164. __func__,
  165. (ret < 0) ? "failed" : "too short",
  166. ret);
  167. goto error;
  168. }
  169. if (desc->bDescriptorType != USB_DT_CS_IRDA) {
  170. dbg("%s - bad class descriptor type", __func__);
  171. goto error;
  172. }
  173. irda_usb_dump_class_desc(desc);
  174. return desc;
  175. error:
  176. kfree(desc);
  177. return NULL;
  178. }
  179. static u8 ir_xbof_change(u8 xbof)
  180. {
  181. u8 result;
  182. /* reference irda-usb.c */
  183. switch (xbof) {
  184. case 48:
  185. result = 0x10;
  186. break;
  187. case 28:
  188. case 24:
  189. result = 0x20;
  190. break;
  191. default:
  192. case 12:
  193. result = 0x30;
  194. break;
  195. case 5:
  196. case 6:
  197. result = 0x40;
  198. break;
  199. case 3:
  200. result = 0x50;
  201. break;
  202. case 2:
  203. result = 0x60;
  204. break;
  205. case 1:
  206. result = 0x70;
  207. break;
  208. case 0:
  209. result = 0x80;
  210. break;
  211. }
  212. return(result);
  213. }
  214. static int ir_startup(struct usb_serial *serial)
  215. {
  216. struct usb_irda_cs_descriptor *irda_desc;
  217. irda_desc = irda_usb_find_class_desc(serial->dev, 0);
  218. if (!irda_desc) {
  219. dev_err(&serial->dev->dev,
  220. "IRDA class descriptor not found, device not bound\n");
  221. return -ENODEV;
  222. }
  223. dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
  224. __func__,
  225. (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
  226. (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
  227. (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
  228. (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
  229. (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
  230. (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
  231. (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
  232. (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
  233. (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
  234. switch (irda_desc->bmAdditionalBOFs) {
  235. case USB_IRDA_AB_48:
  236. ir_add_bof = 48;
  237. break;
  238. case USB_IRDA_AB_24:
  239. ir_add_bof = 24;
  240. break;
  241. case USB_IRDA_AB_12:
  242. ir_add_bof = 12;
  243. break;
  244. case USB_IRDA_AB_6:
  245. ir_add_bof = 6;
  246. break;
  247. case USB_IRDA_AB_3:
  248. ir_add_bof = 3;
  249. break;
  250. case USB_IRDA_AB_2:
  251. ir_add_bof = 2;
  252. break;
  253. case USB_IRDA_AB_1:
  254. ir_add_bof = 1;
  255. break;
  256. case USB_IRDA_AB_0:
  257. ir_add_bof = 0;
  258. break;
  259. default:
  260. break;
  261. }
  262. kfree(irda_desc);
  263. return 0;
  264. }
  265. static int ir_open(struct tty_struct *tty, struct usb_serial_port *port)
  266. {
  267. int i;
  268. dbg("%s - port %d", __func__, port->number);
  269. for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
  270. port->write_urbs[i]->transfer_flags = URB_ZERO_PACKET;
  271. /* Start reading from the device */
  272. return usb_serial_generic_open(tty, port);
  273. }
  274. static int ir_prepare_write_buffer(struct usb_serial_port *port,
  275. void *dest, size_t size)
  276. {
  277. unsigned char *buf = dest;
  278. int count;
  279. /*
  280. * The first byte of the packet we send to the device contains an
  281. * inbound header which indicates an additional number of BOFs and
  282. * a baud rate change.
  283. *
  284. * See section 5.4.2.2 of the USB IrDA spec.
  285. */
  286. *buf = ir_xbof | ir_baud;
  287. count = kfifo_out_locked(&port->write_fifo, buf + 1, size - 1,
  288. &port->lock);
  289. return count + 1;
  290. }
  291. static void ir_process_read_urb(struct urb *urb)
  292. {
  293. struct usb_serial_port *port = urb->context;
  294. unsigned char *data = urb->transfer_buffer;
  295. struct tty_struct *tty;
  296. if (!urb->actual_length)
  297. return;
  298. /*
  299. * The first byte of the packet we get from the device
  300. * contains a busy indicator and baud rate change.
  301. * See section 5.4.1.2 of the USB IrDA spec.
  302. */
  303. if (*data & 0x0f)
  304. ir_baud = *data & 0x0f;
  305. if (urb->actual_length == 1)
  306. return;
  307. tty = tty_port_tty_get(&port->port);
  308. if (!tty)
  309. return;
  310. tty_insert_flip_string(tty, data + 1, urb->actual_length - 1);
  311. tty_flip_buffer_push(tty);
  312. tty_kref_put(tty);
  313. }
  314. static void ir_set_termios_callback(struct urb *urb)
  315. {
  316. struct usb_serial_port *port = urb->context;
  317. int status = urb->status;
  318. dbg("%s - port %d", __func__, port->number);
  319. kfree(urb->transfer_buffer);
  320. if (status)
  321. dbg("%s - non-zero urb status: %d", __func__, status);
  322. }
  323. static void ir_set_termios(struct tty_struct *tty,
  324. struct usb_serial_port *port, struct ktermios *old_termios)
  325. {
  326. struct urb *urb;
  327. unsigned char *transfer_buffer;
  328. int result;
  329. speed_t baud;
  330. int ir_baud;
  331. dbg("%s - port %d", __func__, port->number);
  332. baud = tty_get_baud_rate(tty);
  333. /*
  334. * FIXME, we should compare the baud request against the
  335. * capability stated in the IR header that we got in the
  336. * startup function.
  337. */
  338. switch (baud) {
  339. case 2400:
  340. ir_baud = USB_IRDA_BR_2400;
  341. break;
  342. case 9600:
  343. ir_baud = USB_IRDA_BR_9600;
  344. break;
  345. case 19200:
  346. ir_baud = USB_IRDA_BR_19200;
  347. break;
  348. case 38400:
  349. ir_baud = USB_IRDA_BR_38400;
  350. break;
  351. case 57600:
  352. ir_baud = USB_IRDA_BR_57600;
  353. break;
  354. case 115200:
  355. ir_baud = USB_IRDA_BR_115200;
  356. break;
  357. case 576000:
  358. ir_baud = USB_IRDA_BR_576000;
  359. break;
  360. case 1152000:
  361. ir_baud = USB_IRDA_BR_1152000;
  362. break;
  363. case 4000000:
  364. ir_baud = USB_IRDA_BR_4000000;
  365. break;
  366. default:
  367. ir_baud = USB_IRDA_BR_9600;
  368. baud = 9600;
  369. }
  370. if (xbof == -1)
  371. ir_xbof = ir_xbof_change(ir_add_bof);
  372. else
  373. ir_xbof = ir_xbof_change(xbof) ;
  374. /* Only speed changes are supported */
  375. tty_termios_copy_hw(tty->termios, old_termios);
  376. tty_encode_baud_rate(tty, baud, baud);
  377. /*
  378. * send the baud change out on an "empty" data packet
  379. */
  380. urb = usb_alloc_urb(0, GFP_KERNEL);
  381. if (!urb) {
  382. dev_err(&port->dev, "%s - no more urbs\n", __func__);
  383. return;
  384. }
  385. transfer_buffer = kmalloc(1, GFP_KERNEL);
  386. if (!transfer_buffer) {
  387. dev_err(&port->dev, "%s - out of memory\n", __func__);
  388. goto err_buf;
  389. }
  390. *transfer_buffer = ir_xbof | ir_baud;
  391. usb_fill_bulk_urb(
  392. urb,
  393. port->serial->dev,
  394. usb_sndbulkpipe(port->serial->dev,
  395. port->bulk_out_endpointAddress),
  396. transfer_buffer,
  397. 1,
  398. ir_set_termios_callback,
  399. port);
  400. urb->transfer_flags = URB_ZERO_PACKET;
  401. result = usb_submit_urb(urb, GFP_KERNEL);
  402. if (result) {
  403. dev_err(&port->dev, "%s - failed to submit urb: %d\n",
  404. __func__, result);
  405. goto err_subm;
  406. }
  407. usb_free_urb(urb);
  408. return;
  409. err_subm:
  410. kfree(transfer_buffer);
  411. err_buf:
  412. usb_free_urb(urb);
  413. }
  414. static int __init ir_init(void)
  415. {
  416. int retval;
  417. if (buffer_size) {
  418. ir_device.bulk_in_size = buffer_size;
  419. ir_device.bulk_out_size = buffer_size;
  420. }
  421. retval = usb_serial_register(&ir_device);
  422. if (retval)
  423. goto failed_usb_serial_register;
  424. retval = usb_register(&ir_driver);
  425. if (retval)
  426. goto failed_usb_register;
  427. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  428. DRIVER_DESC "\n");
  429. return 0;
  430. failed_usb_register:
  431. usb_serial_deregister(&ir_device);
  432. failed_usb_serial_register:
  433. return retval;
  434. }
  435. static void __exit ir_exit(void)
  436. {
  437. usb_deregister(&ir_driver);
  438. usb_serial_deregister(&ir_device);
  439. }
  440. module_init(ir_init);
  441. module_exit(ir_exit);
  442. MODULE_AUTHOR(DRIVER_AUTHOR);
  443. MODULE_DESCRIPTION(DRIVER_DESC);
  444. MODULE_LICENSE("GPL");
  445. module_param(debug, bool, S_IRUGO | S_IWUSR);
  446. MODULE_PARM_DESC(debug, "Debug enabled or not");
  447. module_param(xbof, int, 0);
  448. MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
  449. module_param(buffer_size, int, 0);
  450. MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");