ir-usb.c 12 KB

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