ir-usb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This driver allows a USB IrDA device to be used as a "dumb" serial device.
  13. * This can be useful if you do not have access to a full IrDA stack on the
  14. * other side of the connection. If you do have an IrDA stack on both devices,
  15. * please use the usb-irda driver, as it contains the proper error checking and
  16. * other goodness of a full IrDA stack.
  17. *
  18. * Portions of this driver were taken from drivers/net/irda/irda-usb.c, which
  19. * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli
  20. * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com>
  21. *
  22. * See Documentation/usb/usb-serial.txt for more information on using this
  23. * driver
  24. *
  25. * 2008_Jun_02 Felipe Balbi <me@felipebalbi.com>
  26. * Introduced common header to be used also in USB Gadget Framework.
  27. * Still needs some other style fixes.
  28. *
  29. * 2007_Jun_21 Alan Cox <alan@lxorguk.ukuu.org.uk>
  30. * Minimal cleanups for some of the driver problens and tty layer abuse.
  31. * Still needs fixing to allow multiple dongles.
  32. *
  33. * 2002_Mar_07 greg kh
  34. * moved some needed structures and #define values from the
  35. * net/irda/irda-usb.h file into our file, as we don't want to depend on
  36. * that codebase compiling correctly :)
  37. *
  38. * 2002_Jan_14 gb
  39. * Added module parameter to force specific number of XBOFs.
  40. * Added ir_xbof_change().
  41. * Reorganized read_bulk_callback error handling.
  42. * Switched from FILL_BULK_URB() to usb_fill_bulk_urb().
  43. *
  44. * 2001_Nov_08 greg kh
  45. * Changed the irda_usb_find_class_desc() function based on comments and
  46. * code from Martin Diehl.
  47. *
  48. * 2001_Nov_01 greg kh
  49. * Added support for more IrDA USB devices.
  50. * Added support for zero packet. Added buffer override paramater, so
  51. * users can transfer larger packets at once if they wish. Both patches
  52. * came from Dag Brattli <dag@obexcode.com>.
  53. *
  54. * 2001_Oct_07 greg kh
  55. * initial version released.
  56. */
  57. #include <linux/kernel.h>
  58. #include <linux/errno.h>
  59. #include <linux/init.h>
  60. #include <linux/slab.h>
  61. #include <linux/tty.h>
  62. #include <linux/tty_driver.h>
  63. #include <linux/tty_flip.h>
  64. #include <linux/module.h>
  65. #include <linux/spinlock.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/usb.h>
  68. #include <linux/usb/serial.h>
  69. #include <linux/usb/irda.h>
  70. /*
  71. * Version Information
  72. */
  73. #define DRIVER_VERSION "v0.4"
  74. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  75. #define DRIVER_DESC "USB IR Dongle driver"
  76. static int debug;
  77. /* if overridden by the user, then use their value for the size of the read and
  78. * write urbs */
  79. static int buffer_size;
  80. /* if overridden by the user, then use the specified number of XBOFs */
  81. static int xbof = -1;
  82. static int ir_startup (struct usb_serial *serial);
  83. static int ir_open(struct tty_struct *tty, struct usb_serial_port *port,
  84. struct file *filep);
  85. static void ir_close(struct tty_struct *tty, struct usb_serial_port *port,
  86. struct file *filep);
  87. static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
  88. const unsigned char *buf, int count);
  89. static void ir_write_bulk_callback (struct urb *urb);
  90. static void ir_read_bulk_callback (struct urb *urb);
  91. static void ir_set_termios(struct tty_struct *tty,
  92. struct usb_serial_port *port, struct ktermios *old_termios);
  93. /* Not that this lot means you can only have one per system */
  94. static u8 ir_baud;
  95. static u8 ir_xbof;
  96. static u8 ir_add_bof;
  97. static struct usb_device_id ir_id_table[] = {
  98. { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
  99. { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
  100. { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
  101. { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
  102. { } /* Terminating entry */
  103. };
  104. MODULE_DEVICE_TABLE(usb, ir_id_table);
  105. static struct usb_driver ir_driver = {
  106. .name = "ir-usb",
  107. .probe = usb_serial_probe,
  108. .disconnect = usb_serial_disconnect,
  109. .id_table = ir_id_table,
  110. .no_dynamic_id = 1,
  111. };
  112. static struct usb_serial_driver ir_device = {
  113. .driver = {
  114. .owner = THIS_MODULE,
  115. .name = "ir-usb",
  116. },
  117. .description = "IR Dongle",
  118. .usb_driver = &ir_driver,
  119. .id_table = ir_id_table,
  120. .num_ports = 1,
  121. .set_termios = ir_set_termios,
  122. .attach = ir_startup,
  123. .open = ir_open,
  124. .close = ir_close,
  125. .write = ir_write,
  126. .write_bulk_callback = ir_write_bulk_callback,
  127. .read_bulk_callback = ir_read_bulk_callback,
  128. };
  129. static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
  130. {
  131. dbg("bLength=%x", desc->bLength);
  132. dbg("bDescriptorType=%x", desc->bDescriptorType);
  133. dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
  134. dbg("bmDataSize=%x", desc->bmDataSize);
  135. dbg("bmWindowSize=%x", desc->bmWindowSize);
  136. dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
  137. dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
  138. dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
  139. dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
  140. dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
  141. }
  142. /*------------------------------------------------------------------*/
  143. /*
  144. * Function irda_usb_find_class_desc(dev, ifnum)
  145. *
  146. * Returns instance of IrDA class descriptor, or NULL if not found
  147. *
  148. * The class descriptor is some extra info that IrDA USB devices will
  149. * offer to us, describing their IrDA characteristics. We will use that in
  150. * irda_usb_init_qos()
  151. *
  152. * Based on the same function in drivers/net/irda/irda-usb.c
  153. */
  154. static struct usb_irda_cs_descriptor *
  155. irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
  156. {
  157. struct usb_irda_cs_descriptor *desc;
  158. int ret;
  159. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  160. if (!desc)
  161. return NULL;
  162. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  163. USB_REQ_CS_IRDA_GET_CLASS_DESC,
  164. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  165. 0, ifnum, desc, sizeof(*desc), 1000);
  166. dbg("%s - ret=%d", __func__, ret);
  167. if (ret < sizeof(*desc)) {
  168. dbg("%s - class descriptor read %s (%d)",
  169. __func__,
  170. (ret < 0) ? "failed" : "too short",
  171. ret);
  172. goto error;
  173. }
  174. if (desc->bDescriptorType != USB_DT_CS_IRDA) {
  175. dbg("%s - bad class descriptor type", __func__);
  176. goto error;
  177. }
  178. irda_usb_dump_class_desc(desc);
  179. return desc;
  180. error:
  181. kfree(desc);
  182. return NULL;
  183. }
  184. static u8 ir_xbof_change(u8 xbof)
  185. {
  186. u8 result;
  187. /* reference irda-usb.c */
  188. switch (xbof) {
  189. case 48:
  190. result = 0x10;
  191. break;
  192. case 28:
  193. case 24:
  194. result = 0x20;
  195. break;
  196. default:
  197. case 12:
  198. result = 0x30;
  199. break;
  200. case 5:
  201. case 6:
  202. result = 0x40;
  203. break;
  204. case 3:
  205. result = 0x50;
  206. break;
  207. case 2:
  208. result = 0x60;
  209. break;
  210. case 1:
  211. result = 0x70;
  212. break;
  213. case 0:
  214. result = 0x80;
  215. break;
  216. }
  217. return(result);
  218. }
  219. static int ir_startup(struct usb_serial *serial)
  220. {
  221. struct usb_irda_cs_descriptor *irda_desc;
  222. irda_desc = irda_usb_find_class_desc(serial->dev, 0);
  223. if (!irda_desc) {
  224. dev_err(&serial->dev->dev,
  225. "IRDA class descriptor not found, device not bound\n");
  226. return -ENODEV;
  227. }
  228. dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
  229. __func__,
  230. (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
  231. (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
  232. (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
  233. (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
  234. (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
  235. (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
  236. (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
  237. (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
  238. (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
  239. switch (irda_desc->bmAdditionalBOFs) {
  240. case USB_IRDA_AB_48:
  241. ir_add_bof = 48;
  242. break;
  243. case USB_IRDA_AB_24:
  244. ir_add_bof = 24;
  245. break;
  246. case USB_IRDA_AB_12:
  247. ir_add_bof = 12;
  248. break;
  249. case USB_IRDA_AB_6:
  250. ir_add_bof = 6;
  251. break;
  252. case USB_IRDA_AB_3:
  253. ir_add_bof = 3;
  254. break;
  255. case USB_IRDA_AB_2:
  256. ir_add_bof = 2;
  257. break;
  258. case USB_IRDA_AB_1:
  259. ir_add_bof = 1;
  260. break;
  261. case USB_IRDA_AB_0:
  262. ir_add_bof = 0;
  263. break;
  264. default:
  265. break;
  266. }
  267. kfree(irda_desc);
  268. return 0;
  269. }
  270. static int ir_open(struct tty_struct *tty,
  271. struct usb_serial_port *port, struct file *filp)
  272. {
  273. char *buffer;
  274. int result = 0;
  275. dbg("%s - port %d", __func__, port->number);
  276. if (buffer_size) {
  277. /* override the default buffer sizes */
  278. buffer = kmalloc(buffer_size, GFP_KERNEL);
  279. if (!buffer) {
  280. dev_err(&port->dev, "%s - out of memory.\n", __func__);
  281. return -ENOMEM;
  282. }
  283. kfree(port->read_urb->transfer_buffer);
  284. port->read_urb->transfer_buffer = buffer;
  285. port->read_urb->transfer_buffer_length = buffer_size;
  286. buffer = kmalloc(buffer_size, GFP_KERNEL);
  287. if (!buffer) {
  288. dev_err(&port->dev, "%s - out of memory.\n", __func__);
  289. return -ENOMEM;
  290. }
  291. kfree(port->write_urb->transfer_buffer);
  292. port->write_urb->transfer_buffer = buffer;
  293. port->write_urb->transfer_buffer_length = buffer_size;
  294. port->bulk_out_size = buffer_size;
  295. }
  296. /* Start reading from the device */
  297. usb_fill_bulk_urb(
  298. port->read_urb,
  299. port->serial->dev,
  300. usb_rcvbulkpipe(port->serial->dev,
  301. port->bulk_in_endpointAddress),
  302. port->read_urb->transfer_buffer,
  303. port->read_urb->transfer_buffer_length,
  304. ir_read_bulk_callback,
  305. port);
  306. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  307. if (result)
  308. dev_err(&port->dev,
  309. "%s - failed submitting read urb, error %d\n",
  310. __func__, result);
  311. return result;
  312. }
  313. static void ir_close(struct tty_struct *tty,
  314. struct usb_serial_port *port, struct file * filp)
  315. {
  316. dbg("%s - port %d", __func__, port->number);
  317. /* shutdown our bulk read */
  318. usb_kill_urb(port->read_urb);
  319. }
  320. static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
  321. const unsigned char *buf, int count)
  322. {
  323. unsigned char *transfer_buffer;
  324. int result;
  325. int transfer_size;
  326. dbg("%s - port = %d, count = %d", __func__, port->number, count);
  327. if (count == 0)
  328. return 0;
  329. spin_lock_bh(&port->lock);
  330. if (port->write_urb_busy) {
  331. spin_unlock_bh(&port->lock);
  332. dbg("%s - already writing", __func__);
  333. return 0;
  334. }
  335. port->write_urb_busy = 1;
  336. spin_unlock_bh(&port->lock);
  337. transfer_buffer = port->write_urb->transfer_buffer;
  338. transfer_size = min(count, port->bulk_out_size - 1);
  339. /*
  340. * The first byte of the packet we send to the device contains an
  341. * inbound header which indicates an additional number of BOFs and
  342. * a baud rate change.
  343. *
  344. * See section 5.4.2.2 of the USB IrDA spec.
  345. */
  346. *transfer_buffer = ir_xbof | ir_baud;
  347. ++transfer_buffer;
  348. memcpy(transfer_buffer, buf, transfer_size);
  349. usb_fill_bulk_urb(
  350. port->write_urb,
  351. port->serial->dev,
  352. usb_sndbulkpipe(port->serial->dev,
  353. port->bulk_out_endpointAddress),
  354. port->write_urb->transfer_buffer,
  355. transfer_size + 1,
  356. ir_write_bulk_callback,
  357. port);
  358. port->write_urb->transfer_flags = URB_ZERO_PACKET;
  359. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  360. if (result) {
  361. port->write_urb_busy = 0;
  362. dev_err(&port->dev,
  363. "%s - failed submitting write urb, error %d\n",
  364. __func__, result);
  365. } else
  366. result = transfer_size;
  367. return result;
  368. }
  369. static void ir_write_bulk_callback(struct urb *urb)
  370. {
  371. struct usb_serial_port *port = urb->context;
  372. int status = urb->status;
  373. dbg("%s - port %d", __func__, port->number);
  374. port->write_urb_busy = 0;
  375. if (status) {
  376. dbg("%s - nonzero write bulk status received: %d",
  377. __func__, status);
  378. return;
  379. }
  380. usb_serial_debug_data(
  381. debug,
  382. &port->dev,
  383. __func__,
  384. urb->actual_length,
  385. urb->transfer_buffer);
  386. usb_serial_port_softint(port);
  387. }
  388. static void ir_read_bulk_callback(struct urb *urb)
  389. {
  390. struct usb_serial_port *port = urb->context;
  391. struct tty_struct *tty;
  392. unsigned char *data = urb->transfer_buffer;
  393. int result;
  394. int status = urb->status;
  395. dbg("%s - port %d", __func__, port->number);
  396. if (!port->port.count) {
  397. dbg("%s - port closed.", __func__);
  398. return;
  399. }
  400. switch (status) {
  401. case 0: /* Successful */
  402. /*
  403. * The first byte of the packet we get from the device
  404. * contains a busy indicator and baud rate change.
  405. * See section 5.4.1.2 of the USB IrDA spec.
  406. */
  407. if ((*data & 0x0f) > 0)
  408. ir_baud = *data & 0x0f;
  409. usb_serial_debug_data(debug, &port->dev, __func__,
  410. urb->actual_length, data);
  411. tty = tty_port_tty_get(&port->port);
  412. if (tty_buffer_request_room(tty, urb->actual_length - 1)) {
  413. tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
  414. tty_flip_buffer_push(tty);
  415. }
  416. tty_kref_put(tty);
  417. /*
  418. * No break here.
  419. * We want to resubmit the urb so we can read
  420. * again.
  421. */
  422. case -EPROTO: /* taking inspiration from pl2303.c */
  423. /* Continue trying to always read */
  424. usb_fill_bulk_urb(
  425. port->read_urb,
  426. port->serial->dev,
  427. usb_rcvbulkpipe(port->serial->dev,
  428. port->bulk_in_endpointAddress),
  429. port->read_urb->transfer_buffer,
  430. port->read_urb->transfer_buffer_length,
  431. ir_read_bulk_callback,
  432. port);
  433. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  434. if (result)
  435. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
  436. __func__, result);
  437. break ;
  438. default:
  439. dbg("%s - nonzero read bulk status received: %d",
  440. __func__, status);
  441. break ;
  442. }
  443. return;
  444. }
  445. static void ir_set_termios(struct tty_struct *tty,
  446. struct usb_serial_port *port, struct ktermios *old_termios)
  447. {
  448. unsigned char *transfer_buffer;
  449. int result;
  450. speed_t baud;
  451. int ir_baud;
  452. dbg("%s - port %d", __func__, port->number);
  453. baud = tty_get_baud_rate(tty);
  454. /*
  455. * FIXME, we should compare the baud request against the
  456. * capability stated in the IR header that we got in the
  457. * startup function.
  458. */
  459. switch (baud) {
  460. case 2400:
  461. ir_baud = USB_IRDA_BR_2400;
  462. break;
  463. case 9600:
  464. ir_baud = USB_IRDA_BR_9600;
  465. break;
  466. case 19200:
  467. ir_baud = USB_IRDA_BR_19200;
  468. break;
  469. case 38400:
  470. ir_baud = USB_IRDA_BR_38400;
  471. break;
  472. case 57600:
  473. ir_baud = USB_IRDA_BR_57600;
  474. break;
  475. case 115200:
  476. ir_baud = USB_IRDA_BR_115200;
  477. break;
  478. case 576000:
  479. ir_baud = USB_IRDA_BR_576000;
  480. break;
  481. case 1152000:
  482. ir_baud = USB_IRDA_BR_1152000;
  483. break;
  484. case 4000000:
  485. ir_baud = USB_IRDA_BR_4000000;
  486. break;
  487. default:
  488. ir_baud = USB_IRDA_BR_9600;
  489. baud = 9600;
  490. }
  491. if (xbof == -1)
  492. ir_xbof = ir_xbof_change(ir_add_bof);
  493. else
  494. ir_xbof = ir_xbof_change(xbof) ;
  495. /* FIXME need to check to see if our write urb is busy right
  496. * now, or use a urb pool.
  497. *
  498. * send the baud change out on an "empty" data packet
  499. */
  500. transfer_buffer = port->write_urb->transfer_buffer;
  501. *transfer_buffer = ir_xbof | ir_baud;
  502. usb_fill_bulk_urb(
  503. port->write_urb,
  504. port->serial->dev,
  505. usb_sndbulkpipe(port->serial->dev,
  506. port->bulk_out_endpointAddress),
  507. port->write_urb->transfer_buffer,
  508. 1,
  509. ir_write_bulk_callback,
  510. port);
  511. port->write_urb->transfer_flags = URB_ZERO_PACKET;
  512. result = usb_submit_urb(port->write_urb, GFP_KERNEL);
  513. if (result)
  514. dev_err(&port->dev,
  515. "%s - failed submitting write urb, error %d\n",
  516. __func__, result);
  517. /* Only speed changes are supported */
  518. tty_termios_copy_hw(tty->termios, old_termios);
  519. tty_encode_baud_rate(tty, baud, baud);
  520. }
  521. static int __init ir_init(void)
  522. {
  523. int retval;
  524. retval = usb_serial_register(&ir_device);
  525. if (retval)
  526. goto failed_usb_serial_register;
  527. retval = usb_register(&ir_driver);
  528. if (retval)
  529. goto failed_usb_register;
  530. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  531. DRIVER_DESC "\n");
  532. return 0;
  533. failed_usb_register:
  534. usb_serial_deregister(&ir_device);
  535. failed_usb_serial_register:
  536. return retval;
  537. }
  538. static void __exit ir_exit(void)
  539. {
  540. usb_deregister(&ir_driver);
  541. usb_serial_deregister(&ir_device);
  542. }
  543. module_init(ir_init);
  544. module_exit(ir_exit);
  545. MODULE_AUTHOR(DRIVER_AUTHOR);
  546. MODULE_DESCRIPTION(DRIVER_DESC);
  547. MODULE_LICENSE("GPL");
  548. module_param(debug, bool, S_IRUGO | S_IWUSR);
  549. MODULE_PARM_DESC(debug, "Debug enabled or not");
  550. module_param(xbof, int, 0);
  551. MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
  552. module_param(buffer_size, int, 0);
  553. MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");