ir-usb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/tty.h>
  31. #include <linux/tty_driver.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/module.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/usb.h>
  37. #include <linux/usb/serial.h>
  38. #include <linux/usb/irda.h>
  39. /*
  40. * Version Information
  41. */
  42. #define DRIVER_VERSION "v0.5"
  43. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Johan Hovold <jhovold@gmail.com>"
  44. #define DRIVER_DESC "USB IR Dongle driver"
  45. static bool debug;
  46. /* if overridden by the user, then use their value for the size of the read and
  47. * write urbs */
  48. static int buffer_size;
  49. /* if overridden by the user, then use the specified number of XBOFs */
  50. static int xbof = -1;
  51. static int ir_startup (struct usb_serial *serial);
  52. static int ir_open(struct tty_struct *tty, struct usb_serial_port *port);
  53. static int ir_prepare_write_buffer(struct usb_serial_port *port,
  54. void *dest, size_t size);
  55. static void ir_process_read_urb(struct urb *urb);
  56. static void ir_set_termios(struct tty_struct *tty,
  57. struct usb_serial_port *port, struct ktermios *old_termios);
  58. /* Not that this lot means you can only have one per system */
  59. static u8 ir_baud;
  60. static u8 ir_xbof;
  61. static u8 ir_add_bof;
  62. static const struct usb_device_id ir_id_table[] = {
  63. { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
  64. { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
  65. { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
  66. { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
  67. { } /* Terminating entry */
  68. };
  69. MODULE_DEVICE_TABLE(usb, ir_id_table);
  70. static struct usb_driver ir_driver = {
  71. .name = "ir-usb",
  72. .probe = usb_serial_probe,
  73. .disconnect = usb_serial_disconnect,
  74. .id_table = ir_id_table,
  75. .no_dynamic_id = 1,
  76. };
  77. static struct usb_serial_driver ir_device = {
  78. .driver = {
  79. .owner = THIS_MODULE,
  80. .name = "ir-usb",
  81. },
  82. .description = "IR Dongle",
  83. .usb_driver = &ir_driver,
  84. .id_table = ir_id_table,
  85. .num_ports = 1,
  86. .set_termios = ir_set_termios,
  87. .attach = ir_startup,
  88. .open = ir_open,
  89. .prepare_write_buffer = ir_prepare_write_buffer,
  90. .process_read_urb = ir_process_read_urb,
  91. };
  92. static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
  93. {
  94. dbg("bLength=%x", desc->bLength);
  95. dbg("bDescriptorType=%x", desc->bDescriptorType);
  96. dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
  97. dbg("bmDataSize=%x", desc->bmDataSize);
  98. dbg("bmWindowSize=%x", desc->bmWindowSize);
  99. dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
  100. dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
  101. dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
  102. dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
  103. dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
  104. }
  105. /*------------------------------------------------------------------*/
  106. /*
  107. * Function irda_usb_find_class_desc(dev, ifnum)
  108. *
  109. * Returns instance of IrDA class descriptor, or NULL if not found
  110. *
  111. * The class descriptor is some extra info that IrDA USB devices will
  112. * offer to us, describing their IrDA characteristics. We will use that in
  113. * irda_usb_init_qos()
  114. *
  115. * Based on the same function in drivers/net/irda/irda-usb.c
  116. */
  117. static struct usb_irda_cs_descriptor *
  118. irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
  119. {
  120. struct usb_irda_cs_descriptor *desc;
  121. int ret;
  122. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  123. if (!desc)
  124. return NULL;
  125. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  126. USB_REQ_CS_IRDA_GET_CLASS_DESC,
  127. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  128. 0, ifnum, desc, sizeof(*desc), 1000);
  129. dbg("%s - ret=%d", __func__, ret);
  130. if (ret < sizeof(*desc)) {
  131. dbg("%s - class descriptor read %s (%d)",
  132. __func__,
  133. (ret < 0) ? "failed" : "too short",
  134. ret);
  135. goto error;
  136. }
  137. if (desc->bDescriptorType != USB_DT_CS_IRDA) {
  138. dbg("%s - bad class descriptor type", __func__);
  139. goto error;
  140. }
  141. irda_usb_dump_class_desc(desc);
  142. return desc;
  143. error:
  144. kfree(desc);
  145. return NULL;
  146. }
  147. static u8 ir_xbof_change(u8 xbof)
  148. {
  149. u8 result;
  150. /* reference irda-usb.c */
  151. switch (xbof) {
  152. case 48:
  153. result = 0x10;
  154. break;
  155. case 28:
  156. case 24:
  157. result = 0x20;
  158. break;
  159. default:
  160. case 12:
  161. result = 0x30;
  162. break;
  163. case 5:
  164. case 6:
  165. result = 0x40;
  166. break;
  167. case 3:
  168. result = 0x50;
  169. break;
  170. case 2:
  171. result = 0x60;
  172. break;
  173. case 1:
  174. result = 0x70;
  175. break;
  176. case 0:
  177. result = 0x80;
  178. break;
  179. }
  180. return(result);
  181. }
  182. static int ir_startup(struct usb_serial *serial)
  183. {
  184. struct usb_irda_cs_descriptor *irda_desc;
  185. irda_desc = irda_usb_find_class_desc(serial->dev, 0);
  186. if (!irda_desc) {
  187. dev_err(&serial->dev->dev,
  188. "IRDA class descriptor not found, device not bound\n");
  189. return -ENODEV;
  190. }
  191. dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
  192. __func__,
  193. (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
  194. (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
  195. (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
  196. (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
  197. (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
  198. (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
  199. (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
  200. (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
  201. (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
  202. switch (irda_desc->bmAdditionalBOFs) {
  203. case USB_IRDA_AB_48:
  204. ir_add_bof = 48;
  205. break;
  206. case USB_IRDA_AB_24:
  207. ir_add_bof = 24;
  208. break;
  209. case USB_IRDA_AB_12:
  210. ir_add_bof = 12;
  211. break;
  212. case USB_IRDA_AB_6:
  213. ir_add_bof = 6;
  214. break;
  215. case USB_IRDA_AB_3:
  216. ir_add_bof = 3;
  217. break;
  218. case USB_IRDA_AB_2:
  219. ir_add_bof = 2;
  220. break;
  221. case USB_IRDA_AB_1:
  222. ir_add_bof = 1;
  223. break;
  224. case USB_IRDA_AB_0:
  225. ir_add_bof = 0;
  226. break;
  227. default:
  228. break;
  229. }
  230. kfree(irda_desc);
  231. return 0;
  232. }
  233. static int ir_open(struct tty_struct *tty, struct usb_serial_port *port)
  234. {
  235. int i;
  236. dbg("%s - port %d", __func__, port->number);
  237. for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
  238. port->write_urbs[i]->transfer_flags = URB_ZERO_PACKET;
  239. /* Start reading from the device */
  240. return usb_serial_generic_open(tty, port);
  241. }
  242. static int ir_prepare_write_buffer(struct usb_serial_port *port,
  243. void *dest, size_t size)
  244. {
  245. unsigned char *buf = dest;
  246. int count;
  247. /*
  248. * The first byte of the packet we send to the device contains an
  249. * inbound header which indicates an additional number of BOFs and
  250. * a baud rate change.
  251. *
  252. * See section 5.4.2.2 of the USB IrDA spec.
  253. */
  254. *buf = ir_xbof | ir_baud;
  255. count = kfifo_out_locked(&port->write_fifo, buf + 1, size - 1,
  256. &port->lock);
  257. return count + 1;
  258. }
  259. static void ir_process_read_urb(struct urb *urb)
  260. {
  261. struct usb_serial_port *port = urb->context;
  262. unsigned char *data = urb->transfer_buffer;
  263. struct tty_struct *tty;
  264. if (!urb->actual_length)
  265. return;
  266. /*
  267. * The first byte of the packet we get from the device
  268. * contains a busy indicator and baud rate change.
  269. * See section 5.4.1.2 of the USB IrDA spec.
  270. */
  271. if (*data & 0x0f)
  272. ir_baud = *data & 0x0f;
  273. if (urb->actual_length == 1)
  274. return;
  275. tty = tty_port_tty_get(&port->port);
  276. if (!tty)
  277. return;
  278. tty_insert_flip_string(tty, data + 1, urb->actual_length - 1);
  279. tty_flip_buffer_push(tty);
  280. tty_kref_put(tty);
  281. }
  282. static void ir_set_termios_callback(struct urb *urb)
  283. {
  284. struct usb_serial_port *port = urb->context;
  285. int status = urb->status;
  286. dbg("%s - port %d", __func__, port->number);
  287. kfree(urb->transfer_buffer);
  288. if (status)
  289. dbg("%s - non-zero urb status: %d", __func__, status);
  290. }
  291. static void ir_set_termios(struct tty_struct *tty,
  292. struct usb_serial_port *port, struct ktermios *old_termios)
  293. {
  294. struct urb *urb;
  295. unsigned char *transfer_buffer;
  296. int result;
  297. speed_t baud;
  298. int ir_baud;
  299. dbg("%s - port %d", __func__, port->number);
  300. baud = tty_get_baud_rate(tty);
  301. /*
  302. * FIXME, we should compare the baud request against the
  303. * capability stated in the IR header that we got in the
  304. * startup function.
  305. */
  306. switch (baud) {
  307. case 2400:
  308. ir_baud = USB_IRDA_BR_2400;
  309. break;
  310. case 9600:
  311. ir_baud = USB_IRDA_BR_9600;
  312. break;
  313. case 19200:
  314. ir_baud = USB_IRDA_BR_19200;
  315. break;
  316. case 38400:
  317. ir_baud = USB_IRDA_BR_38400;
  318. break;
  319. case 57600:
  320. ir_baud = USB_IRDA_BR_57600;
  321. break;
  322. case 115200:
  323. ir_baud = USB_IRDA_BR_115200;
  324. break;
  325. case 576000:
  326. ir_baud = USB_IRDA_BR_576000;
  327. break;
  328. case 1152000:
  329. ir_baud = USB_IRDA_BR_1152000;
  330. break;
  331. case 4000000:
  332. ir_baud = USB_IRDA_BR_4000000;
  333. break;
  334. default:
  335. ir_baud = USB_IRDA_BR_9600;
  336. baud = 9600;
  337. }
  338. if (xbof == -1)
  339. ir_xbof = ir_xbof_change(ir_add_bof);
  340. else
  341. ir_xbof = ir_xbof_change(xbof) ;
  342. /* Only speed changes are supported */
  343. tty_termios_copy_hw(tty->termios, old_termios);
  344. tty_encode_baud_rate(tty, baud, baud);
  345. /*
  346. * send the baud change out on an "empty" data packet
  347. */
  348. urb = usb_alloc_urb(0, GFP_KERNEL);
  349. if (!urb) {
  350. dev_err(&port->dev, "%s - no more urbs\n", __func__);
  351. return;
  352. }
  353. transfer_buffer = kmalloc(1, GFP_KERNEL);
  354. if (!transfer_buffer) {
  355. dev_err(&port->dev, "%s - out of memory\n", __func__);
  356. goto err_buf;
  357. }
  358. *transfer_buffer = ir_xbof | ir_baud;
  359. usb_fill_bulk_urb(
  360. urb,
  361. port->serial->dev,
  362. usb_sndbulkpipe(port->serial->dev,
  363. port->bulk_out_endpointAddress),
  364. transfer_buffer,
  365. 1,
  366. ir_set_termios_callback,
  367. port);
  368. urb->transfer_flags = URB_ZERO_PACKET;
  369. result = usb_submit_urb(urb, GFP_KERNEL);
  370. if (result) {
  371. dev_err(&port->dev, "%s - failed to submit urb: %d\n",
  372. __func__, result);
  373. goto err_subm;
  374. }
  375. usb_free_urb(urb);
  376. return;
  377. err_subm:
  378. kfree(transfer_buffer);
  379. err_buf:
  380. usb_free_urb(urb);
  381. }
  382. static int __init ir_init(void)
  383. {
  384. int retval;
  385. if (buffer_size) {
  386. ir_device.bulk_in_size = buffer_size;
  387. ir_device.bulk_out_size = buffer_size;
  388. }
  389. retval = usb_serial_register(&ir_device);
  390. if (retval)
  391. goto failed_usb_serial_register;
  392. retval = usb_register(&ir_driver);
  393. if (retval)
  394. goto failed_usb_register;
  395. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  396. DRIVER_DESC "\n");
  397. return 0;
  398. failed_usb_register:
  399. usb_serial_deregister(&ir_device);
  400. failed_usb_serial_register:
  401. return retval;
  402. }
  403. static void __exit ir_exit(void)
  404. {
  405. usb_deregister(&ir_driver);
  406. usb_serial_deregister(&ir_device);
  407. }
  408. module_init(ir_init);
  409. module_exit(ir_exit);
  410. MODULE_AUTHOR(DRIVER_AUTHOR);
  411. MODULE_DESCRIPTION(DRIVER_DESC);
  412. MODULE_LICENSE("GPL");
  413. module_param(debug, bool, S_IRUGO | S_IWUSR);
  414. MODULE_PARM_DESC(debug, "Debug enabled or not");
  415. module_param(xbof, int, 0);
  416. MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
  417. module_param(buffer_size, int, 0);
  418. MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");