ir-usb.c 11 KB

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