ir-usb.c 12 KB

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