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