4 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2002 Gary Brubaker (xavyer@ix.netcom.com)
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.
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.
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>
22 * See Documentation/usb/usb-serial.txt for more information on using this driver
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 :)
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().
36 * Changed the irda_usb_find_class_desc() function based on comments and
37 * code from Martin Diehl.
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>.
46 * initial version released.
49 #include <linux/config.h>
50 #include <linux/kernel.h>
51 #include <linux/errno.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/tty.h>
55 #include <linux/tty_driver.h>
56 #include <linux/tty_flip.h>
57 #include <linux/module.h>
58 #include <linux/spinlock.h>
59 #include <asm/uaccess.h>
60 #include <linux/usb.h>
62 #ifdef CONFIG_USB_SERIAL_DEBUG
68 #include "usb-serial.h"
73 #define DRIVER_VERSION "v0.4"
74 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
75 #define DRIVER_DESC "USB IR Dongle driver"
77 /* USB IrDA class spec information */
78 #define USB_CLASS_IRDA 0x02
79 #define USB_DT_IRDA 0x21
80 #define IU_REQ_GET_CLASS_DESC 0x06
81 #define SPEED_2400 0x01
82 #define SPEED_9600 0x02
83 #define SPEED_19200 0x03
84 #define SPEED_38400 0x04
85 #define SPEED_57600 0x05
86 #define SPEED_115200 0x06
87 #define SPEED_576000 0x07
88 #define SPEED_1152000 0x08
89 #define SPEED_4000000 0x09
91 struct irda_class_desc {
97 u8 bmMinTurnaroundTime;
102 } __attribute__ ((packed));
104 /* if overridden by the user, then use their value for the size of the read and
106 static int buffer_size = 0;
107 /* if overridden by the user, then use the specified number of XBOFs */
108 static int xbof = -1;
110 static int ir_startup (struct usb_serial *serial);
111 static int ir_open (struct usb_serial_port *port, struct file *filep);
112 static void ir_close (struct usb_serial_port *port, struct file *filep);
113 static int ir_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
114 static void ir_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
115 static void ir_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
116 static void ir_set_termios (struct usb_serial_port *port, struct termios *old_termios);
118 static u8 ir_baud = 0;
119 static u8 ir_xbof = 0;
120 static u8 ir_add_bof = 0;
122 static struct usb_device_id id_table [] = {
123 { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
124 { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
125 { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
126 { USB_INTERFACE_INFO (USB_CLASS_APP_SPEC, USB_CLASS_IRDA, 0) },
127 { } /* Terminating entry */
130 MODULE_DEVICE_TABLE (usb, id_table);
132 static struct usb_driver ir_driver = {
133 .owner = THIS_MODULE,
135 .probe = usb_serial_probe,
136 .disconnect = usb_serial_disconnect,
137 .id_table = id_table,
141 struct usb_serial_device_type ir_device = {
142 .owner = THIS_MODULE,
144 .id_table = id_table,
145 .num_interrupt_in = 1,
149 .set_termios = ir_set_termios,
150 .attach = ir_startup,
154 .write_bulk_callback = ir_write_bulk_callback,
155 .read_bulk_callback = ir_read_bulk_callback,
158 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
160 dbg("bLength=%x", desc->bLength);
161 dbg("bDescriptorType=%x", desc->bDescriptorType);
162 dbg("bcdSpecRevision=%x", desc->bcdSpecRevision);
163 dbg("bmDataSize=%x", desc->bmDataSize);
164 dbg("bmWindowSize=%x", desc->bmWindowSize);
165 dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
166 dbg("wBaudRate=%x", desc->wBaudRate);
167 dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
168 dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
169 dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
172 /*------------------------------------------------------------------*/
174 * Function irda_usb_find_class_desc(dev, ifnum)
176 * Returns instance of IrDA class descriptor, or NULL if not found
178 * The class descriptor is some extra info that IrDA USB devices will
179 * offer to us, describing their IrDA characteristics. We will use that in
180 * irda_usb_init_qos()
182 * Based on the same function in drivers/net/irda/irda-usb.c
184 static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
186 struct irda_class_desc *desc;
189 desc = kmalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
192 memset(desc, 0, sizeof(struct irda_class_desc));
194 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
195 IU_REQ_GET_CLASS_DESC,
196 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
197 0, ifnum, desc, sizeof(*desc), HZ);
199 dbg("%s - ret=%d", __FUNCTION__, ret);
200 if (ret < sizeof(*desc)) {
201 dbg("%s - class descriptor read %s (%d)",
203 (ret<0) ? "failed" : "too short",
207 if (desc->bDescriptorType != USB_DT_IRDA) {
208 dbg("%s - bad class descriptor type", __FUNCTION__);
212 irda_usb_dump_class_desc(desc);
220 static u8 ir_xbof_change(u8 xbof)
223 /* reference irda-usb.c */
225 case 48: result = 0x10; break;
227 case 24: result = 0x20; break;
229 case 12: result = 0x30; break;
231 case 6: result = 0x40; break;
232 case 3: result = 0x50; break;
233 case 2: result = 0x60; break;
234 case 1: result = 0x70; break;
235 case 0: result = 0x80; break;
241 static int ir_startup (struct usb_serial *serial)
243 struct irda_class_desc *irda_desc;
245 irda_desc = irda_usb_find_class_desc (serial->dev, 0);
246 if (irda_desc == NULL) {
247 dev_err (&serial->dev->dev, "IRDA class descriptor not found, device not bound\n");
251 dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
253 (irda_desc->wBaudRate & 0x0001) ? " 2400" : "",
254 (irda_desc->wBaudRate & 0x0002) ? " 9600" : "",
255 (irda_desc->wBaudRate & 0x0004) ? " 19200" : "",
256 (irda_desc->wBaudRate & 0x0008) ? " 38400" : "",
257 (irda_desc->wBaudRate & 0x0010) ? " 57600" : "",
258 (irda_desc->wBaudRate & 0x0020) ? " 115200" : "",
259 (irda_desc->wBaudRate & 0x0040) ? " 576000" : "",
260 (irda_desc->wBaudRate & 0x0080) ? " 1152000" : "",
261 (irda_desc->wBaudRate & 0x0100) ? " 4000000" : "");
263 switch( irda_desc->bmAdditionalBOFs ) {
264 case 0x01: ir_add_bof = 48; break;
265 case 0x02: ir_add_bof = 24; break;
266 case 0x04: ir_add_bof = 12; break;
267 case 0x08: ir_add_bof = 6; break;
268 case 0x10: ir_add_bof = 3; break;
269 case 0x20: ir_add_bof = 2; break;
270 case 0x40: ir_add_bof = 1; break;
271 case 0x80: ir_add_bof = 0; break;
280 static int ir_open (struct usb_serial_port *port, struct file *filp)
282 struct usb_serial *serial = port->serial;
286 if (port_paranoia_check (port, __FUNCTION__))
289 dbg("%s - port %d", __FUNCTION__, port->number);
292 /* override the default buffer sizes */
293 buffer = kmalloc (buffer_size, GFP_KERNEL);
295 dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
298 kfree (port->read_urb->transfer_buffer);
299 port->read_urb->transfer_buffer = buffer;
300 port->read_urb->transfer_buffer_length = buffer_size;
302 buffer = kmalloc (buffer_size, GFP_KERNEL);
304 dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
307 kfree (port->write_urb->transfer_buffer);
308 port->write_urb->transfer_buffer = buffer;
309 port->write_urb->transfer_buffer_length = buffer_size;
310 port->bulk_out_size = buffer_size;
313 /* Start reading from the device */
317 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
318 port->read_urb->transfer_buffer,
319 port->read_urb->transfer_buffer_length,
320 ir_read_bulk_callback,
322 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
324 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
329 static void ir_close (struct usb_serial_port *port, struct file * filp)
331 struct usb_serial *serial;
333 if (port_paranoia_check (port, __FUNCTION__))
336 dbg("%s - port %d", __FUNCTION__, port->number);
338 serial = get_usb_serial (port, __FUNCTION__);
343 /* shutdown our bulk read */
344 usb_unlink_urb (port->read_urb);
348 static int ir_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
350 unsigned char *transfer_buffer;
354 dbg("%s - port = %d, count = %d", __FUNCTION__, port->number, count);
357 dev_err (&port->dev, "%s - no tty???\n", __FUNCTION__);
364 if (port->write_urb->status == -EINPROGRESS) {
365 dbg ("%s - already writing", __FUNCTION__);
369 transfer_buffer = port->write_urb->transfer_buffer;
370 transfer_size = min(count, port->bulk_out_size - 1);
373 * The first byte of the packet we send to the device contains an
374 * inband header which indicates an additional number of BOFs and
375 * a baud rate change.
377 * See section 5.4.2.2 of the USB IrDA spec.
379 *transfer_buffer = ir_xbof | ir_baud;
383 if (copy_from_user (transfer_buffer, buf, transfer_size))
386 memcpy (transfer_buffer, buf, transfer_size);
392 usb_sndbulkpipe(port->serial->dev,
393 port->bulk_out_endpointAddress),
394 port->write_urb->transfer_buffer,
396 ir_write_bulk_callback,
399 port->write_urb->transfer_flags = URB_ZERO_PACKET;
401 result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
403 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
405 result = transfer_size;
410 static void ir_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
412 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
414 if (port_paranoia_check (port, __FUNCTION__))
417 dbg("%s - port %d", __FUNCTION__, port->number);
420 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
424 usb_serial_debug_data (
428 urb->transfer_buffer);
430 schedule_work(&port->work);
433 static void ir_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
435 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
436 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
437 struct tty_struct *tty;
438 unsigned char *data = urb->transfer_buffer;
441 if (port_paranoia_check (port, __FUNCTION__))
444 dbg("%s - port %d", __FUNCTION__, port->number);
447 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
451 if (!port->open_count) {
452 dbg("%s - port closed.", __FUNCTION__);
456 switch (urb->status) {
458 case 0: /* Successful */
461 * The first byte of the packet we get from the device
462 * contains a busy indicator and baud rate change.
463 * See section 5.4.1.2 of the USB IrDA spec.
465 if ((*data & 0x0f) > 0)
466 ir_baud = *data & 0x0f;
468 usb_serial_debug_data (
475 * Bypass flip-buffers, and feed the ldisc directly
476 * due to our potentially large buffer size. Since we
477 * used to set low_latency, this is exactly what the
478 * tty layer did anyway :)
482 tty->ldisc.receive_buf(
486 urb->actual_length-1);
490 * We want to resubmit the urb so we can read
494 case -EPROTO: /* taking inspiration from pl2303.c */
496 /* Continue trying to always read */
500 usb_rcvbulkpipe(serial->dev,
501 port->bulk_in_endpointAddress),
502 port->read_urb->transfer_buffer,
503 port->read_urb->transfer_buffer_length,
504 ir_read_bulk_callback,
507 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
509 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
510 __FUNCTION__, result);
515 dbg("%s - nonzero read bulk status received: %d",
525 static void ir_set_termios (struct usb_serial_port *port, struct termios *old_termios)
527 unsigned char *transfer_buffer;
531 dbg("%s - port %d", __FUNCTION__, port->number);
533 if ((!port->tty) || (!port->tty->termios)) {
534 dbg("%s - no tty structures", __FUNCTION__);
538 cflag = port->tty->termios->c_cflag;
539 /* check that they really want us to change something */
541 if ((cflag == old_termios->c_cflag) &&
542 (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
543 dbg("%s - nothing to change...", __FUNCTION__);
548 /* All we can change is the baud rate */
551 dbg ("%s - asking for baud %d",
553 tty_get_baud_rate(port->tty));
556 * FIXME, we should compare the baud request against the
557 * capability stated in the IR header that we got in the
560 switch (cflag & CBAUD) {
561 case B2400: ir_baud = SPEED_2400; break;
563 case B9600: ir_baud = SPEED_9600; break;
564 case B19200: ir_baud = SPEED_19200; break;
565 case B38400: ir_baud = SPEED_38400; break;
566 case B57600: ir_baud = SPEED_57600; break;
567 case B115200: ir_baud = SPEED_115200; break;
568 case B576000: ir_baud = SPEED_576000; break;
569 case B1152000: ir_baud = SPEED_1152000; break;
571 case B4000000: ir_baud = SPEED_4000000; break;
576 ir_xbof = ir_xbof_change(ir_add_bof);
578 ir_xbof = ir_xbof_change(xbof) ;
581 /* Notify the tty driver that the termios have changed. */
582 port->tty->ldisc.set_termios(port->tty, NULL);
584 /* FIXME need to check to see if our write urb is busy right
585 * now, or use a urb pool.
587 * send the baud change out on an "empty" data packet
589 transfer_buffer = port->write_urb->transfer_buffer;
590 *transfer_buffer = ir_xbof | ir_baud;
595 usb_sndbulkpipe(port->serial->dev,
596 port->bulk_out_endpointAddress),
597 port->write_urb->transfer_buffer,
599 ir_write_bulk_callback,
602 port->write_urb->transfer_flags = URB_ZERO_PACKET;
604 result = usb_submit_urb (port->write_urb, GFP_KERNEL);
606 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
612 static int __init ir_init (void)
615 retval = usb_serial_register(&ir_device);
617 goto failed_usb_serial_register;
618 retval = usb_register(&ir_driver);
620 goto failed_usb_register;
621 info(DRIVER_DESC " " DRIVER_VERSION);
624 usb_serial_deregister(&ir_device);
625 failed_usb_serial_register:
630 static void __exit ir_exit (void)
632 usb_deregister (&ir_driver);
633 usb_serial_deregister (&ir_device);
637 module_init(ir_init);
638 module_exit(ir_exit);
640 MODULE_AUTHOR(DRIVER_AUTHOR);
641 MODULE_DESCRIPTION(DRIVER_DESC);
642 MODULE_LICENSE("GPL");
644 MODULE_PARM(debug, "i");
645 MODULE_PARM_DESC(debug, "Debug enabled or not");
646 MODULE_PARM(xbof, "i");
647 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
648 MODULE_PARM(buffer_size, "i");
649 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");