ir-usb.c 16 KB

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