ir-usb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 usb_serial_port *port);
  86. static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
  87. const unsigned char *buf, int count);
  88. static void ir_write_bulk_callback (struct urb *urb);
  89. static void ir_read_bulk_callback (struct urb *urb);
  90. static void ir_set_termios(struct tty_struct *tty,
  91. struct usb_serial_port *port, struct ktermios *old_termios);
  92. /* Not that this lot means you can only have one per system */
  93. static u8 ir_baud;
  94. static u8 ir_xbof;
  95. static u8 ir_add_bof;
  96. static struct usb_device_id ir_id_table[] = {
  97. { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
  98. { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
  99. { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
  100. { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
  101. { } /* Terminating entry */
  102. };
  103. MODULE_DEVICE_TABLE(usb, ir_id_table);
  104. static struct usb_driver ir_driver = {
  105. .name = "ir-usb",
  106. .probe = usb_serial_probe,
  107. .disconnect = usb_serial_disconnect,
  108. .id_table = ir_id_table,
  109. .no_dynamic_id = 1,
  110. };
  111. static struct usb_serial_driver ir_device = {
  112. .driver = {
  113. .owner = THIS_MODULE,
  114. .name = "ir-usb",
  115. },
  116. .description = "IR Dongle",
  117. .usb_driver = &ir_driver,
  118. .id_table = ir_id_table,
  119. .num_ports = 1,
  120. .set_termios = ir_set_termios,
  121. .attach = ir_startup,
  122. .open = ir_open,
  123. .close = ir_close,
  124. .write = ir_write,
  125. .write_bulk_callback = ir_write_bulk_callback,
  126. .read_bulk_callback = ir_read_bulk_callback,
  127. };
  128. static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
  129. {
  130. dbg("bLength=%x", desc->bLength);
  131. dbg("bDescriptorType=%x", desc->bDescriptorType);
  132. dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
  133. dbg("bmDataSize=%x", desc->bmDataSize);
  134. dbg("bmWindowSize=%x", desc->bmWindowSize);
  135. dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
  136. dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
  137. dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
  138. dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
  139. dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
  140. }
  141. /*------------------------------------------------------------------*/
  142. /*
  143. * Function irda_usb_find_class_desc(dev, ifnum)
  144. *
  145. * Returns instance of IrDA class descriptor, or NULL if not found
  146. *
  147. * The class descriptor is some extra info that IrDA USB devices will
  148. * offer to us, describing their IrDA characteristics. We will use that in
  149. * irda_usb_init_qos()
  150. *
  151. * Based on the same function in drivers/net/irda/irda-usb.c
  152. */
  153. static struct usb_irda_cs_descriptor *
  154. irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
  155. {
  156. struct usb_irda_cs_descriptor *desc;
  157. int ret;
  158. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  159. if (!desc)
  160. return NULL;
  161. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  162. USB_REQ_CS_IRDA_GET_CLASS_DESC,
  163. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  164. 0, ifnum, desc, sizeof(*desc), 1000);
  165. dbg("%s - ret=%d", __func__, ret);
  166. if (ret < sizeof(*desc)) {
  167. dbg("%s - class descriptor read %s (%d)",
  168. __func__,
  169. (ret < 0) ? "failed" : "too short",
  170. ret);
  171. goto error;
  172. }
  173. if (desc->bDescriptorType != USB_DT_CS_IRDA) {
  174. dbg("%s - bad class descriptor type", __func__);
  175. goto error;
  176. }
  177. irda_usb_dump_class_desc(desc);
  178. return desc;
  179. error:
  180. kfree(desc);
  181. return NULL;
  182. }
  183. static u8 ir_xbof_change(u8 xbof)
  184. {
  185. u8 result;
  186. /* reference irda-usb.c */
  187. switch (xbof) {
  188. case 48:
  189. result = 0x10;
  190. break;
  191. case 28:
  192. case 24:
  193. result = 0x20;
  194. break;
  195. default:
  196. case 12:
  197. result = 0x30;
  198. break;
  199. case 5:
  200. case 6:
  201. result = 0x40;
  202. break;
  203. case 3:
  204. result = 0x50;
  205. break;
  206. case 2:
  207. result = 0x60;
  208. break;
  209. case 1:
  210. result = 0x70;
  211. break;
  212. case 0:
  213. result = 0x80;
  214. break;
  215. }
  216. return(result);
  217. }
  218. static int ir_startup(struct usb_serial *serial)
  219. {
  220. struct usb_irda_cs_descriptor *irda_desc;
  221. irda_desc = irda_usb_find_class_desc(serial->dev, 0);
  222. if (!irda_desc) {
  223. dev_err(&serial->dev->dev,
  224. "IRDA class descriptor not found, device not bound\n");
  225. return -ENODEV;
  226. }
  227. dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
  228. __func__,
  229. (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
  230. (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
  231. (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
  232. (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
  233. (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
  234. (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
  235. (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
  236. (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
  237. (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
  238. switch (irda_desc->bmAdditionalBOFs) {
  239. case USB_IRDA_AB_48:
  240. ir_add_bof = 48;
  241. break;
  242. case USB_IRDA_AB_24:
  243. ir_add_bof = 24;
  244. break;
  245. case USB_IRDA_AB_12:
  246. ir_add_bof = 12;
  247. break;
  248. case USB_IRDA_AB_6:
  249. ir_add_bof = 6;
  250. break;
  251. case USB_IRDA_AB_3:
  252. ir_add_bof = 3;
  253. break;
  254. case USB_IRDA_AB_2:
  255. ir_add_bof = 2;
  256. break;
  257. case USB_IRDA_AB_1:
  258. ir_add_bof = 1;
  259. break;
  260. case USB_IRDA_AB_0:
  261. ir_add_bof = 0;
  262. break;
  263. default:
  264. break;
  265. }
  266. kfree(irda_desc);
  267. return 0;
  268. }
  269. static int ir_open(struct tty_struct *tty,
  270. struct usb_serial_port *port, struct file *filp)
  271. {
  272. char *buffer;
  273. int result = 0;
  274. dbg("%s - port %d", __func__, port->number);
  275. if (buffer_size) {
  276. /* override the default buffer sizes */
  277. buffer = kmalloc(buffer_size, GFP_KERNEL);
  278. if (!buffer) {
  279. dev_err(&port->dev, "%s - out of memory.\n", __func__);
  280. return -ENOMEM;
  281. }
  282. kfree(port->read_urb->transfer_buffer);
  283. port->read_urb->transfer_buffer = buffer;
  284. port->read_urb->transfer_buffer_length = buffer_size;
  285. buffer = kmalloc(buffer_size, GFP_KERNEL);
  286. if (!buffer) {
  287. dev_err(&port->dev, "%s - out of memory.\n", __func__);
  288. return -ENOMEM;
  289. }
  290. kfree(port->write_urb->transfer_buffer);
  291. port->write_urb->transfer_buffer = buffer;
  292. port->write_urb->transfer_buffer_length = buffer_size;
  293. port->bulk_out_size = buffer_size;
  294. }
  295. /* Start reading from the device */
  296. usb_fill_bulk_urb(
  297. port->read_urb,
  298. port->serial->dev,
  299. usb_rcvbulkpipe(port->serial->dev,
  300. port->bulk_in_endpointAddress),
  301. port->read_urb->transfer_buffer,
  302. port->read_urb->transfer_buffer_length,
  303. ir_read_bulk_callback,
  304. port);
  305. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  306. if (result)
  307. dev_err(&port->dev,
  308. "%s - failed submitting read urb, error %d\n",
  309. __func__, result);
  310. return result;
  311. }
  312. static void ir_close(struct usb_serial_port *port)
  313. {
  314. dbg("%s - port %d", __func__, port->number);
  315. /* shutdown our bulk read */
  316. usb_kill_urb(port->read_urb);
  317. }
  318. static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
  319. const unsigned char *buf, int count)
  320. {
  321. unsigned char *transfer_buffer;
  322. int result;
  323. int transfer_size;
  324. dbg("%s - port = %d, count = %d", __func__, port->number, count);
  325. if (count == 0)
  326. return 0;
  327. spin_lock_bh(&port->lock);
  328. if (port->write_urb_busy) {
  329. spin_unlock_bh(&port->lock);
  330. dbg("%s - already writing", __func__);
  331. return 0;
  332. }
  333. port->write_urb_busy = 1;
  334. spin_unlock_bh(&port->lock);
  335. transfer_buffer = port->write_urb->transfer_buffer;
  336. transfer_size = min(count, port->bulk_out_size - 1);
  337. /*
  338. * The first byte of the packet we send to the device contains an
  339. * inbound header which indicates an additional number of BOFs and
  340. * a baud rate change.
  341. *
  342. * See section 5.4.2.2 of the USB IrDA spec.
  343. */
  344. *transfer_buffer = ir_xbof | ir_baud;
  345. ++transfer_buffer;
  346. memcpy(transfer_buffer, buf, transfer_size);
  347. usb_fill_bulk_urb(
  348. port->write_urb,
  349. port->serial->dev,
  350. usb_sndbulkpipe(port->serial->dev,
  351. port->bulk_out_endpointAddress),
  352. port->write_urb->transfer_buffer,
  353. transfer_size + 1,
  354. ir_write_bulk_callback,
  355. port);
  356. port->write_urb->transfer_flags = URB_ZERO_PACKET;
  357. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  358. if (result) {
  359. port->write_urb_busy = 0;
  360. dev_err(&port->dev,
  361. "%s - failed submitting write urb, error %d\n",
  362. __func__, result);
  363. } else
  364. result = transfer_size;
  365. return result;
  366. }
  367. static void ir_write_bulk_callback(struct urb *urb)
  368. {
  369. struct usb_serial_port *port = urb->context;
  370. int status = urb->status;
  371. dbg("%s - port %d", __func__, port->number);
  372. port->write_urb_busy = 0;
  373. if (status) {
  374. dbg("%s - nonzero write bulk status received: %d",
  375. __func__, status);
  376. return;
  377. }
  378. usb_serial_debug_data(
  379. debug,
  380. &port->dev,
  381. __func__,
  382. urb->actual_length,
  383. urb->transfer_buffer);
  384. usb_serial_port_softint(port);
  385. }
  386. static void ir_read_bulk_callback(struct urb *urb)
  387. {
  388. struct usb_serial_port *port = urb->context;
  389. struct tty_struct *tty;
  390. unsigned char *data = urb->transfer_buffer;
  391. int result;
  392. int status = urb->status;
  393. dbg("%s - port %d", __func__, port->number);
  394. if (!port->port.count) {
  395. dbg("%s - port closed.", __func__);
  396. return;
  397. }
  398. switch (status) {
  399. case 0: /* Successful */
  400. /*
  401. * The first byte of the packet we get from the device
  402. * contains a busy indicator and baud rate change.
  403. * See section 5.4.1.2 of the USB IrDA spec.
  404. */
  405. if ((*data & 0x0f) > 0)
  406. ir_baud = *data & 0x0f;
  407. usb_serial_debug_data(debug, &port->dev, __func__,
  408. urb->actual_length, data);
  409. tty = tty_port_tty_get(&port->port);
  410. if (tty_buffer_request_room(tty, urb->actual_length - 1)) {
  411. tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
  412. tty_flip_buffer_push(tty);
  413. }
  414. tty_kref_put(tty);
  415. /*
  416. * No break here.
  417. * We want to resubmit the urb so we can read
  418. * again.
  419. */
  420. case -EPROTO: /* taking inspiration from pl2303.c */
  421. /* Continue trying to always read */
  422. usb_fill_bulk_urb(
  423. port->read_urb,
  424. port->serial->dev,
  425. usb_rcvbulkpipe(port->serial->dev,
  426. port->bulk_in_endpointAddress),
  427. port->read_urb->transfer_buffer,
  428. port->read_urb->transfer_buffer_length,
  429. ir_read_bulk_callback,
  430. port);
  431. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  432. if (result)
  433. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
  434. __func__, result);
  435. break ;
  436. default:
  437. dbg("%s - nonzero read bulk status received: %d",
  438. __func__, status);
  439. break ;
  440. }
  441. return;
  442. }
  443. static void ir_set_termios(struct tty_struct *tty,
  444. struct usb_serial_port *port, struct ktermios *old_termios)
  445. {
  446. unsigned char *transfer_buffer;
  447. int result;
  448. speed_t baud;
  449. int ir_baud;
  450. dbg("%s - port %d", __func__, port->number);
  451. baud = tty_get_baud_rate(tty);
  452. /*
  453. * FIXME, we should compare the baud request against the
  454. * capability stated in the IR header that we got in the
  455. * startup function.
  456. */
  457. switch (baud) {
  458. case 2400:
  459. ir_baud = USB_IRDA_BR_2400;
  460. break;
  461. case 9600:
  462. ir_baud = USB_IRDA_BR_9600;
  463. break;
  464. case 19200:
  465. ir_baud = USB_IRDA_BR_19200;
  466. break;
  467. case 38400:
  468. ir_baud = USB_IRDA_BR_38400;
  469. break;
  470. case 57600:
  471. ir_baud = USB_IRDA_BR_57600;
  472. break;
  473. case 115200:
  474. ir_baud = USB_IRDA_BR_115200;
  475. break;
  476. case 576000:
  477. ir_baud = USB_IRDA_BR_576000;
  478. break;
  479. case 1152000:
  480. ir_baud = USB_IRDA_BR_1152000;
  481. break;
  482. case 4000000:
  483. ir_baud = USB_IRDA_BR_4000000;
  484. break;
  485. default:
  486. ir_baud = USB_IRDA_BR_9600;
  487. baud = 9600;
  488. }
  489. if (xbof == -1)
  490. ir_xbof = ir_xbof_change(ir_add_bof);
  491. else
  492. ir_xbof = ir_xbof_change(xbof) ;
  493. /* FIXME need to check to see if our write urb is busy right
  494. * now, or use a urb pool.
  495. *
  496. * send the baud change out on an "empty" data packet
  497. */
  498. transfer_buffer = port->write_urb->transfer_buffer;
  499. *transfer_buffer = ir_xbof | ir_baud;
  500. usb_fill_bulk_urb(
  501. port->write_urb,
  502. port->serial->dev,
  503. usb_sndbulkpipe(port->serial->dev,
  504. port->bulk_out_endpointAddress),
  505. port->write_urb->transfer_buffer,
  506. 1,
  507. ir_write_bulk_callback,
  508. port);
  509. port->write_urb->transfer_flags = URB_ZERO_PACKET;
  510. result = usb_submit_urb(port->write_urb, GFP_KERNEL);
  511. if (result)
  512. dev_err(&port->dev,
  513. "%s - failed submitting write urb, error %d\n",
  514. __func__, result);
  515. /* Only speed changes are supported */
  516. tty_termios_copy_hw(tty->termios, old_termios);
  517. tty_encode_baud_rate(tty, baud, baud);
  518. }
  519. static int __init ir_init(void)
  520. {
  521. int retval;
  522. retval = usb_serial_register(&ir_device);
  523. if (retval)
  524. goto failed_usb_serial_register;
  525. retval = usb_register(&ir_driver);
  526. if (retval)
  527. goto failed_usb_register;
  528. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  529. DRIVER_DESC "\n");
  530. return 0;
  531. failed_usb_register:
  532. usb_serial_deregister(&ir_device);
  533. failed_usb_serial_register:
  534. return retval;
  535. }
  536. static void __exit ir_exit(void)
  537. {
  538. usb_deregister(&ir_driver);
  539. usb_serial_deregister(&ir_device);
  540. }
  541. module_init(ir_init);
  542. module_exit(ir_exit);
  543. MODULE_AUTHOR(DRIVER_AUTHOR);
  544. MODULE_DESCRIPTION(DRIVER_DESC);
  545. MODULE_LICENSE("GPL");
  546. module_param(debug, bool, S_IRUGO | S_IWUSR);
  547. MODULE_PARM_DESC(debug, "Debug enabled or not");
  548. module_param(xbof, int, 0);
  549. MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
  550. module_param(buffer_size, int, 0);
  551. MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");