ir-usb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 driver
  23. *
  24. * 2002_Mar_07 greg kh
  25. * moved some needed structures and #define values from the
  26. * net/irda/irda-usb.h file into our file, as we don't want to depend on
  27. * that codebase compiling correctly :)
  28. *
  29. * 2002_Jan_14 gb
  30. * Added module parameter to force specific number of XBOFs.
  31. * Added ir_xbof_change().
  32. * Reorganized read_bulk_callback error handling.
  33. * Switched from FILL_BULK_URB() to usb_fill_bulk_urb().
  34. *
  35. * 2001_Nov_08 greg kh
  36. * Changed the irda_usb_find_class_desc() function based on comments and
  37. * code from Martin Diehl.
  38. *
  39. * 2001_Nov_01 greg kh
  40. * Added support for more IrDA USB devices.
  41. * Added support for zero packet. Added buffer override paramater, so
  42. * users can transfer larger packets at once if they wish. Both patches
  43. * came from Dag Brattli <dag@obexcode.com>.
  44. *
  45. * 2001_Oct_07 greg kh
  46. * initial version released.
  47. */
  48. #include <linux/config.h>
  49. #include <linux/kernel.h>
  50. #include <linux/errno.h>
  51. #include <linux/init.h>
  52. #include <linux/slab.h>
  53. #include <linux/tty.h>
  54. #include <linux/tty_driver.h>
  55. #include <linux/tty_flip.h>
  56. #include <linux/module.h>
  57. #include <linux/spinlock.h>
  58. #include <asm/uaccess.h>
  59. #include <linux/usb.h>
  60. #include "usb-serial.h"
  61. /*
  62. * Version Information
  63. */
  64. #define DRIVER_VERSION "v0.4"
  65. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  66. #define DRIVER_DESC "USB IR Dongle driver"
  67. /* USB IrDA class spec information */
  68. #define USB_CLASS_IRDA 0x02
  69. #define USB_DT_IRDA 0x21
  70. #define IU_REQ_GET_CLASS_DESC 0x06
  71. #define SPEED_2400 0x01
  72. #define SPEED_9600 0x02
  73. #define SPEED_19200 0x03
  74. #define SPEED_38400 0x04
  75. #define SPEED_57600 0x05
  76. #define SPEED_115200 0x06
  77. #define SPEED_576000 0x07
  78. #define SPEED_1152000 0x08
  79. #define SPEED_4000000 0x09
  80. struct irda_class_desc {
  81. u8 bLength;
  82. u8 bDescriptorType;
  83. u16 bcdSpecRevision;
  84. u8 bmDataSize;
  85. u8 bmWindowSize;
  86. u8 bmMinTurnaroundTime;
  87. u16 wBaudRate;
  88. u8 bmAdditionalBOFs;
  89. u8 bIrdaRateSniff;
  90. u8 bMaxUnicastList;
  91. } __attribute__ ((packed));
  92. static int debug;
  93. /* if overridden by the user, then use their value for the size of the read and
  94. * write urbs */
  95. static int buffer_size;
  96. /* if overridden by the user, then use the specified number of XBOFs */
  97. static int xbof = -1;
  98. static int ir_startup (struct usb_serial *serial);
  99. static int ir_open (struct usb_serial_port *port, struct file *filep);
  100. static void ir_close (struct usb_serial_port *port, struct file *filep);
  101. static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  102. static void ir_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
  103. static void ir_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
  104. static void ir_set_termios (struct usb_serial_port *port, struct termios *old_termios);
  105. static u8 ir_baud = 0;
  106. static u8 ir_xbof = 0;
  107. static u8 ir_add_bof = 0;
  108. static struct usb_device_id id_table [] = {
  109. { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
  110. { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
  111. { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
  112. { USB_INTERFACE_INFO (USB_CLASS_APP_SPEC, USB_CLASS_IRDA, 0) },
  113. { } /* Terminating entry */
  114. };
  115. MODULE_DEVICE_TABLE (usb, id_table);
  116. static struct usb_driver ir_driver = {
  117. .name = "ir-usb",
  118. .probe = usb_serial_probe,
  119. .disconnect = usb_serial_disconnect,
  120. .id_table = id_table,
  121. .no_dynamic_id = 1,
  122. };
  123. static struct usb_serial_driver ir_device = {
  124. .driver = {
  125. .owner = THIS_MODULE,
  126. .name = "ir-usb",
  127. },
  128. .description = "IR Dongle",
  129. .id_table = id_table,
  130. .num_interrupt_in = 1,
  131. .num_bulk_in = 1,
  132. .num_bulk_out = 1,
  133. .num_ports = 1,
  134. .set_termios = ir_set_termios,
  135. .attach = ir_startup,
  136. .open = ir_open,
  137. .close = ir_close,
  138. .write = ir_write,
  139. .write_bulk_callback = ir_write_bulk_callback,
  140. .read_bulk_callback = ir_read_bulk_callback,
  141. };
  142. static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
  143. {
  144. dbg("bLength=%x", desc->bLength);
  145. dbg("bDescriptorType=%x", desc->bDescriptorType);
  146. dbg("bcdSpecRevision=%x", desc->bcdSpecRevision);
  147. dbg("bmDataSize=%x", desc->bmDataSize);
  148. dbg("bmWindowSize=%x", desc->bmWindowSize);
  149. dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
  150. dbg("wBaudRate=%x", desc->wBaudRate);
  151. dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
  152. dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
  153. dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
  154. }
  155. /*------------------------------------------------------------------*/
  156. /*
  157. * Function irda_usb_find_class_desc(dev, ifnum)
  158. *
  159. * Returns instance of IrDA class descriptor, or NULL if not found
  160. *
  161. * The class descriptor is some extra info that IrDA USB devices will
  162. * offer to us, describing their IrDA characteristics. We will use that in
  163. * irda_usb_init_qos()
  164. *
  165. * Based on the same function in drivers/net/irda/irda-usb.c
  166. */
  167. static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
  168. {
  169. struct irda_class_desc *desc;
  170. int ret;
  171. desc = kmalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
  172. if (desc == NULL)
  173. return NULL;
  174. memset(desc, 0, sizeof(struct irda_class_desc));
  175. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
  176. IU_REQ_GET_CLASS_DESC,
  177. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  178. 0, ifnum, desc, sizeof(*desc), 1000);
  179. dbg("%s - ret=%d", __FUNCTION__, ret);
  180. if (ret < sizeof(*desc)) {
  181. dbg("%s - class descriptor read %s (%d)",
  182. __FUNCTION__,
  183. (ret<0) ? "failed" : "too short",
  184. ret);
  185. goto error;
  186. }
  187. if (desc->bDescriptorType != USB_DT_IRDA) {
  188. dbg("%s - bad class descriptor type", __FUNCTION__);
  189. goto error;
  190. }
  191. irda_usb_dump_class_desc(desc);
  192. return desc;
  193. error:
  194. kfree(desc);
  195. return NULL;
  196. }
  197. static u8 ir_xbof_change(u8 xbof)
  198. {
  199. u8 result;
  200. /* reference irda-usb.c */
  201. switch(xbof) {
  202. case 48: result = 0x10; break;
  203. case 28:
  204. case 24: result = 0x20; break;
  205. default:
  206. case 12: result = 0x30; break;
  207. case 5:
  208. case 6: result = 0x40; break;
  209. case 3: result = 0x50; break;
  210. case 2: result = 0x60; break;
  211. case 1: result = 0x70; break;
  212. case 0: result = 0x80; break;
  213. }
  214. return(result);
  215. }
  216. static int ir_startup (struct usb_serial *serial)
  217. {
  218. struct irda_class_desc *irda_desc;
  219. irda_desc = irda_usb_find_class_desc (serial->dev, 0);
  220. if (irda_desc == NULL) {
  221. dev_err (&serial->dev->dev, "IRDA class descriptor not found, device not bound\n");
  222. return -ENODEV;
  223. }
  224. dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
  225. __FUNCTION__,
  226. (irda_desc->wBaudRate & 0x0001) ? " 2400" : "",
  227. (irda_desc->wBaudRate & 0x0002) ? " 9600" : "",
  228. (irda_desc->wBaudRate & 0x0004) ? " 19200" : "",
  229. (irda_desc->wBaudRate & 0x0008) ? " 38400" : "",
  230. (irda_desc->wBaudRate & 0x0010) ? " 57600" : "",
  231. (irda_desc->wBaudRate & 0x0020) ? " 115200" : "",
  232. (irda_desc->wBaudRate & 0x0040) ? " 576000" : "",
  233. (irda_desc->wBaudRate & 0x0080) ? " 1152000" : "",
  234. (irda_desc->wBaudRate & 0x0100) ? " 4000000" : "");
  235. switch( irda_desc->bmAdditionalBOFs ) {
  236. case 0x01: ir_add_bof = 48; break;
  237. case 0x02: ir_add_bof = 24; break;
  238. case 0x04: ir_add_bof = 12; break;
  239. case 0x08: ir_add_bof = 6; break;
  240. case 0x10: ir_add_bof = 3; break;
  241. case 0x20: ir_add_bof = 2; break;
  242. case 0x40: ir_add_bof = 1; break;
  243. case 0x80: ir_add_bof = 0; break;
  244. default:;
  245. }
  246. kfree (irda_desc);
  247. return 0;
  248. }
  249. static int ir_open (struct usb_serial_port *port, struct file *filp)
  250. {
  251. char *buffer;
  252. int result = 0;
  253. dbg("%s - port %d", __FUNCTION__, port->number);
  254. if (buffer_size) {
  255. /* override the default buffer sizes */
  256. buffer = kmalloc (buffer_size, GFP_KERNEL);
  257. if (!buffer) {
  258. dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
  259. return -ENOMEM;
  260. }
  261. kfree (port->read_urb->transfer_buffer);
  262. port->read_urb->transfer_buffer = buffer;
  263. port->read_urb->transfer_buffer_length = buffer_size;
  264. buffer = kmalloc (buffer_size, GFP_KERNEL);
  265. if (!buffer) {
  266. dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
  267. return -ENOMEM;
  268. }
  269. kfree (port->write_urb->transfer_buffer);
  270. port->write_urb->transfer_buffer = buffer;
  271. port->write_urb->transfer_buffer_length = buffer_size;
  272. port->bulk_out_size = buffer_size;
  273. }
  274. /* Start reading from the device */
  275. usb_fill_bulk_urb (
  276. port->read_urb,
  277. port->serial->dev,
  278. usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
  279. port->read_urb->transfer_buffer,
  280. port->read_urb->transfer_buffer_length,
  281. ir_read_bulk_callback,
  282. port);
  283. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  284. if (result)
  285. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  286. return result;
  287. }
  288. static void ir_close (struct usb_serial_port *port, struct file * filp)
  289. {
  290. dbg("%s - port %d", __FUNCTION__, port->number);
  291. /* shutdown our bulk read */
  292. usb_kill_urb(port->read_urb);
  293. }
  294. static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  295. {
  296. unsigned char *transfer_buffer;
  297. int result;
  298. int transfer_size;
  299. dbg("%s - port = %d, count = %d", __FUNCTION__, port->number, count);
  300. if (!port->tty) {
  301. dev_err (&port->dev, "%s - no tty???\n", __FUNCTION__);
  302. return 0;
  303. }
  304. if (count == 0)
  305. return 0;
  306. spin_lock(&port->lock);
  307. if (port->write_urb_busy) {
  308. spin_unlock(&port->lock);
  309. dbg("%s - already writing", __FUNCTION__);
  310. return 0;
  311. }
  312. port->write_urb_busy = 1;
  313. spin_unlock(&port->lock);
  314. transfer_buffer = port->write_urb->transfer_buffer;
  315. transfer_size = min(count, port->bulk_out_size - 1);
  316. /*
  317. * The first byte of the packet we send to the device contains an
  318. * inband header which indicates an additional number of BOFs and
  319. * a baud rate change.
  320. *
  321. * See section 5.4.2.2 of the USB IrDA spec.
  322. */
  323. *transfer_buffer = ir_xbof | ir_baud;
  324. ++transfer_buffer;
  325. memcpy (transfer_buffer, buf, transfer_size);
  326. usb_fill_bulk_urb (
  327. port->write_urb,
  328. port->serial->dev,
  329. usb_sndbulkpipe(port->serial->dev,
  330. port->bulk_out_endpointAddress),
  331. port->write_urb->transfer_buffer,
  332. transfer_size + 1,
  333. ir_write_bulk_callback,
  334. port);
  335. port->write_urb->transfer_flags = URB_ZERO_PACKET;
  336. result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
  337. if (result) {
  338. port->write_urb_busy = 0;
  339. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  340. } else
  341. result = transfer_size;
  342. return result;
  343. }
  344. static void ir_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
  345. {
  346. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  347. dbg("%s - port %d", __FUNCTION__, port->number);
  348. port->write_urb_busy = 0;
  349. if (urb->status) {
  350. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  351. return;
  352. }
  353. usb_serial_debug_data (
  354. debug,
  355. &port->dev,
  356. __FUNCTION__,
  357. urb->actual_length,
  358. urb->transfer_buffer);
  359. schedule_work(&port->work);
  360. }
  361. static void ir_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  362. {
  363. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  364. struct tty_struct *tty;
  365. unsigned char *data = urb->transfer_buffer;
  366. int result;
  367. dbg("%s - port %d", __FUNCTION__, port->number);
  368. if (!port->open_count) {
  369. dbg("%s - port closed.", __FUNCTION__);
  370. return;
  371. }
  372. switch (urb->status) {
  373. case 0: /* Successful */
  374. /*
  375. * The first byte of the packet we get from the device
  376. * contains a busy indicator and baud rate change.
  377. * See section 5.4.1.2 of the USB IrDA spec.
  378. */
  379. if ((*data & 0x0f) > 0)
  380. ir_baud = *data & 0x0f;
  381. usb_serial_debug_data (
  382. debug,
  383. &port->dev,
  384. __FUNCTION__,
  385. urb->actual_length,
  386. data);
  387. /*
  388. * Bypass flip-buffers, and feed the ldisc directly
  389. * due to our potentially large buffer size. Since we
  390. * used to set low_latency, this is exactly what the
  391. * tty layer did anyway :)
  392. */
  393. tty = port->tty;
  394. /*
  395. * FIXME: must not do this in IRQ context,
  396. * must honour TTY_DONT_FLIP
  397. */
  398. tty->ldisc.receive_buf(
  399. tty,
  400. data+1,
  401. NULL,
  402. urb->actual_length-1);
  403. /*
  404. * No break here.
  405. * We want to resubmit the urb so we can read
  406. * again.
  407. */
  408. case -EPROTO: /* taking inspiration from pl2303.c */
  409. /* Continue trying to always read */
  410. usb_fill_bulk_urb (
  411. port->read_urb,
  412. port->serial->dev,
  413. usb_rcvbulkpipe(port->serial->dev,
  414. port->bulk_in_endpointAddress),
  415. port->read_urb->transfer_buffer,
  416. port->read_urb->transfer_buffer_length,
  417. ir_read_bulk_callback,
  418. port);
  419. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  420. if (result)
  421. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
  422. __FUNCTION__, result);
  423. break ;
  424. default:
  425. dbg("%s - nonzero read bulk status received: %d",
  426. __FUNCTION__,
  427. urb->status);
  428. break ;
  429. }
  430. return;
  431. }
  432. static void ir_set_termios (struct usb_serial_port *port, struct termios *old_termios)
  433. {
  434. unsigned char *transfer_buffer;
  435. unsigned int cflag;
  436. int result;
  437. dbg("%s - port %d", __FUNCTION__, port->number);
  438. if ((!port->tty) || (!port->tty->termios)) {
  439. dbg("%s - no tty structures", __FUNCTION__);
  440. return;
  441. }
  442. cflag = port->tty->termios->c_cflag;
  443. /* check that they really want us to change something */
  444. if (old_termios) {
  445. if ((cflag == old_termios->c_cflag) &&
  446. (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
  447. dbg("%s - nothing to change...", __FUNCTION__);
  448. return;
  449. }
  450. }
  451. /* All we can change is the baud rate */
  452. if (cflag & CBAUD) {
  453. dbg ("%s - asking for baud %d",
  454. __FUNCTION__,
  455. tty_get_baud_rate(port->tty));
  456. /*
  457. * FIXME, we should compare the baud request against the
  458. * capability stated in the IR header that we got in the
  459. * startup function.
  460. */
  461. switch (cflag & CBAUD) {
  462. case B2400: ir_baud = SPEED_2400; break;
  463. default:
  464. case B9600: ir_baud = SPEED_9600; break;
  465. case B19200: ir_baud = SPEED_19200; break;
  466. case B38400: ir_baud = SPEED_38400; break;
  467. case B57600: ir_baud = SPEED_57600; break;
  468. case B115200: ir_baud = SPEED_115200; break;
  469. case B576000: ir_baud = SPEED_576000; break;
  470. case B1152000: ir_baud = SPEED_1152000; break;
  471. #ifdef B4000000
  472. case B4000000: ir_baud = SPEED_4000000; break;
  473. #endif
  474. }
  475. if (xbof == -1) {
  476. ir_xbof = ir_xbof_change(ir_add_bof);
  477. } else {
  478. ir_xbof = ir_xbof_change(xbof) ;
  479. }
  480. /* Notify the tty driver that the termios have changed. */
  481. port->tty->ldisc.set_termios(port->tty, NULL);
  482. /* FIXME need to check to see if our write urb is busy right
  483. * now, or use a urb pool.
  484. *
  485. * send the baud change out on an "empty" data packet
  486. */
  487. transfer_buffer = port->write_urb->transfer_buffer;
  488. *transfer_buffer = ir_xbof | ir_baud;
  489. usb_fill_bulk_urb (
  490. port->write_urb,
  491. port->serial->dev,
  492. usb_sndbulkpipe(port->serial->dev,
  493. port->bulk_out_endpointAddress),
  494. port->write_urb->transfer_buffer,
  495. 1,
  496. ir_write_bulk_callback,
  497. port);
  498. port->write_urb->transfer_flags = URB_ZERO_PACKET;
  499. result = usb_submit_urb (port->write_urb, GFP_KERNEL);
  500. if (result)
  501. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  502. }
  503. return;
  504. }
  505. static int __init ir_init (void)
  506. {
  507. int retval;
  508. retval = usb_serial_register(&ir_device);
  509. if (retval)
  510. goto failed_usb_serial_register;
  511. retval = usb_register(&ir_driver);
  512. if (retval)
  513. goto failed_usb_register;
  514. info(DRIVER_DESC " " DRIVER_VERSION);
  515. return 0;
  516. failed_usb_register:
  517. usb_serial_deregister(&ir_device);
  518. failed_usb_serial_register:
  519. return retval;
  520. }
  521. static void __exit ir_exit (void)
  522. {
  523. usb_deregister (&ir_driver);
  524. usb_serial_deregister (&ir_device);
  525. }
  526. module_init(ir_init);
  527. module_exit(ir_exit);
  528. MODULE_AUTHOR(DRIVER_AUTHOR);
  529. MODULE_DESCRIPTION(DRIVER_DESC);
  530. MODULE_LICENSE("GPL");
  531. module_param(debug, bool, S_IRUGO | S_IWUSR);
  532. MODULE_PARM_DESC(debug, "Debug enabled or not");
  533. module_param(xbof, int, 0);
  534. MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
  535. module_param(buffer_size, int, 0);
  536. MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");