ir-usb.c 11 KB

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