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
24 * 2007_Jun_21 Alan Cox <alan@redhat.com>
25 * Minimal cleanups for some of the driver problens and tty layer abuse.
26 * Still needs fixing to allow multiple dongles.
29 * moved some needed structures and #define values from the
30 * net/irda/irda-usb.h file into our file, as we don't want to depend on
31 * that codebase compiling correctly :)
34 * Added module parameter to force specific number of XBOFs.
35 * Added ir_xbof_change().
36 * Reorganized read_bulk_callback error handling.
37 * Switched from FILL_BULK_URB() to usb_fill_bulk_urb().
40 * Changed the irda_usb_find_class_desc() function based on comments and
41 * code from Martin Diehl.
44 * Added support for more IrDA USB devices.
45 * Added support for zero packet. Added buffer override paramater, so
46 * users can transfer larger packets at once if they wish. Both patches
47 * came from Dag Brattli <dag@obexcode.com>.
50 * initial version released.
53 #include <linux/kernel.h>
54 #include <linux/errno.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/tty.h>
58 #include <linux/tty_driver.h>
59 #include <linux/tty_flip.h>
60 #include <linux/module.h>
61 #include <linux/spinlock.h>
62 #include <asm/uaccess.h>
63 #include <linux/usb.h>
64 #include <linux/usb/serial.h>
69 #define DRIVER_VERSION "v0.4"
70 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
71 #define DRIVER_DESC "USB IR Dongle driver"
73 /* USB IrDA class spec information */
74 #define USB_CLASS_IRDA 0x02
75 #define USB_DT_IRDA 0x21
76 #define IU_REQ_GET_CLASS_DESC 0x06
77 #define SPEED_2400 0x01
78 #define SPEED_9600 0x02
79 #define SPEED_19200 0x03
80 #define SPEED_38400 0x04
81 #define SPEED_57600 0x05
82 #define SPEED_115200 0x06
83 #define SPEED_576000 0x07
84 #define SPEED_1152000 0x08
85 #define SPEED_4000000 0x09
87 struct irda_class_desc {
93 u8 bmMinTurnaroundTime;
98 } __attribute__ ((packed));
102 /* if overridden by the user, then use their value for the size of the read and
104 static int buffer_size;
105 /* if overridden by the user, then use the specified number of XBOFs */
106 static int xbof = -1;
108 static int ir_startup (struct usb_serial *serial);
109 static int ir_open (struct usb_serial_port *port, struct file *filep);
110 static void ir_close (struct usb_serial_port *port, struct file *filep);
111 static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count);
112 static void ir_write_bulk_callback (struct urb *urb);
113 static void ir_read_bulk_callback (struct urb *urb);
114 static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
116 /* Not that this lot means you can only have one per system */
117 static u8 ir_baud = 0;
118 static u8 ir_xbof = 0;
119 static u8 ir_add_bof = 0;
121 static struct usb_device_id id_table [] = {
122 { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
123 { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
124 { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
125 { USB_INTERFACE_INFO (USB_CLASS_APP_SPEC, USB_CLASS_IRDA, 0) },
126 { } /* Terminating entry */
129 MODULE_DEVICE_TABLE (usb, id_table);
131 static struct usb_driver ir_driver = {
133 .probe = usb_serial_probe,
134 .disconnect = usb_serial_disconnect,
135 .id_table = id_table,
140 static struct usb_serial_driver ir_device = {
142 .owner = THIS_MODULE,
145 .description = "IR Dongle",
146 .usb_driver = &ir_driver,
147 .id_table = id_table,
148 .num_interrupt_in = 1,
152 .set_termios = ir_set_termios,
153 .attach = ir_startup,
157 .write_bulk_callback = ir_write_bulk_callback,
158 .read_bulk_callback = ir_read_bulk_callback,
161 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
163 dbg("bLength=%x", desc->bLength);
164 dbg("bDescriptorType=%x", desc->bDescriptorType);
165 dbg("bcdSpecRevision=%x", desc->bcdSpecRevision);
166 dbg("bmDataSize=%x", desc->bmDataSize);
167 dbg("bmWindowSize=%x", desc->bmWindowSize);
168 dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
169 dbg("wBaudRate=%x", desc->wBaudRate);
170 dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
171 dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
172 dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
175 /*------------------------------------------------------------------*/
177 * Function irda_usb_find_class_desc(dev, ifnum)
179 * Returns instance of IrDA class descriptor, or NULL if not found
181 * The class descriptor is some extra info that IrDA USB devices will
182 * offer to us, describing their IrDA characteristics. We will use that in
183 * irda_usb_init_qos()
185 * Based on the same function in drivers/net/irda/irda-usb.c
187 static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
189 struct irda_class_desc *desc;
192 desc = kzalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
196 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
197 IU_REQ_GET_CLASS_DESC,
198 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
199 0, ifnum, desc, sizeof(*desc), 1000);
201 dbg("%s - ret=%d", __FUNCTION__, ret);
202 if (ret < sizeof(*desc)) {
203 dbg("%s - class descriptor read %s (%d)",
205 (ret<0) ? "failed" : "too short",
209 if (desc->bDescriptorType != USB_DT_IRDA) {
210 dbg("%s - bad class descriptor type", __FUNCTION__);
214 irda_usb_dump_class_desc(desc);
222 static u8 ir_xbof_change(u8 xbof)
225 /* reference irda-usb.c */
227 case 48: result = 0x10; break;
229 case 24: result = 0x20; break;
231 case 12: result = 0x30; break;
233 case 6: result = 0x40; break;
234 case 3: result = 0x50; break;
235 case 2: result = 0x60; break;
236 case 1: result = 0x70; break;
237 case 0: result = 0x80; break;
243 static int ir_startup (struct usb_serial *serial)
245 struct irda_class_desc *irda_desc;
247 irda_desc = irda_usb_find_class_desc (serial->dev, 0);
248 if (irda_desc == NULL) {
249 dev_err (&serial->dev->dev, "IRDA class descriptor not found, device not bound\n");
253 dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
255 (irda_desc->wBaudRate & 0x0001) ? " 2400" : "",
256 (irda_desc->wBaudRate & 0x0002) ? " 9600" : "",
257 (irda_desc->wBaudRate & 0x0004) ? " 19200" : "",
258 (irda_desc->wBaudRate & 0x0008) ? " 38400" : "",
259 (irda_desc->wBaudRate & 0x0010) ? " 57600" : "",
260 (irda_desc->wBaudRate & 0x0020) ? " 115200" : "",
261 (irda_desc->wBaudRate & 0x0040) ? " 576000" : "",
262 (irda_desc->wBaudRate & 0x0080) ? " 1152000" : "",
263 (irda_desc->wBaudRate & 0x0100) ? " 4000000" : "");
265 switch( irda_desc->bmAdditionalBOFs ) {
266 case 0x01: ir_add_bof = 48; break;
267 case 0x02: ir_add_bof = 24; break;
268 case 0x04: ir_add_bof = 12; break;
269 case 0x08: ir_add_bof = 6; break;
270 case 0x10: ir_add_bof = 3; break;
271 case 0x20: ir_add_bof = 2; break;
272 case 0x40: ir_add_bof = 1; break;
273 case 0x80: ir_add_bof = 0; break;
282 static int ir_open (struct usb_serial_port *port, struct file *filp)
287 dbg("%s - port %d", __FUNCTION__, port->number);
290 /* override the default buffer sizes */
291 buffer = kmalloc (buffer_size, GFP_KERNEL);
293 dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
296 kfree (port->read_urb->transfer_buffer);
297 port->read_urb->transfer_buffer = buffer;
298 port->read_urb->transfer_buffer_length = buffer_size;
300 buffer = kmalloc (buffer_size, GFP_KERNEL);
302 dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
305 kfree (port->write_urb->transfer_buffer);
306 port->write_urb->transfer_buffer = buffer;
307 port->write_urb->transfer_buffer_length = buffer_size;
308 port->bulk_out_size = buffer_size;
311 /* Start reading from the device */
315 usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
316 port->read_urb->transfer_buffer,
317 port->read_urb->transfer_buffer_length,
318 ir_read_bulk_callback,
320 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
322 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
327 static void ir_close (struct usb_serial_port *port, struct file * filp)
329 dbg("%s - port %d", __FUNCTION__, port->number);
331 /* shutdown our bulk read */
332 usb_kill_urb(port->read_urb);
335 static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count)
337 unsigned char *transfer_buffer;
341 dbg("%s - port = %d, count = %d", __FUNCTION__, port->number, count);
344 dev_err (&port->dev, "%s - no tty???\n", __FUNCTION__);
351 spin_lock_bh(&port->lock);
352 if (port->write_urb_busy) {
353 spin_unlock_bh(&port->lock);
354 dbg("%s - already writing", __FUNCTION__);
357 port->write_urb_busy = 1;
358 spin_unlock_bh(&port->lock);
360 transfer_buffer = port->write_urb->transfer_buffer;
361 transfer_size = min(count, port->bulk_out_size - 1);
364 * The first byte of the packet we send to the device contains an
365 * inband header which indicates an additional number of BOFs and
366 * a baud rate change.
368 * See section 5.4.2.2 of the USB IrDA spec.
370 *transfer_buffer = ir_xbof | ir_baud;
373 memcpy (transfer_buffer, buf, transfer_size);
378 usb_sndbulkpipe(port->serial->dev,
379 port->bulk_out_endpointAddress),
380 port->write_urb->transfer_buffer,
382 ir_write_bulk_callback,
385 port->write_urb->transfer_flags = URB_ZERO_PACKET;
387 result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
389 port->write_urb_busy = 0;
390 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
392 result = transfer_size;
397 static void ir_write_bulk_callback (struct urb *urb)
399 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
400 int status = urb->status;
402 dbg("%s - port %d", __FUNCTION__, port->number);
404 port->write_urb_busy = 0;
406 dbg("%s - nonzero write bulk status received: %d",
407 __FUNCTION__, status);
411 usb_serial_debug_data (
416 urb->transfer_buffer);
418 usb_serial_port_softint(port);
421 static void ir_read_bulk_callback (struct urb *urb)
423 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
424 struct tty_struct *tty;
425 unsigned char *data = urb->transfer_buffer;
427 int status = urb->status;
429 dbg("%s - port %d", __FUNCTION__, port->number);
431 if (!port->open_count) {
432 dbg("%s - port closed.", __FUNCTION__);
437 case 0: /* Successful */
440 * The first byte of the packet we get from the device
441 * contains a busy indicator and baud rate change.
442 * See section 5.4.1.2 of the USB IrDA spec.
444 if ((*data & 0x0f) > 0)
445 ir_baud = *data & 0x0f;
447 usb_serial_debug_data (
456 if (tty_buffer_request_room(tty, urb->actual_length - 1)) {
457 tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
458 tty_flip_buffer_push(tty);
463 * We want to resubmit the urb so we can read
467 case -EPROTO: /* taking inspiration from pl2303.c */
469 /* Continue trying to always read */
473 usb_rcvbulkpipe(port->serial->dev,
474 port->bulk_in_endpointAddress),
475 port->read_urb->transfer_buffer,
476 port->read_urb->transfer_buffer_length,
477 ir_read_bulk_callback,
480 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
482 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
483 __FUNCTION__, result);
488 dbg("%s - nonzero read bulk status received: %d",
498 static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
500 unsigned char *transfer_buffer;
505 dbg("%s - port %d", __FUNCTION__, port->number);
507 if ((!port->tty) || (!port->tty->termios)) {
508 dbg("%s - no tty structures", __FUNCTION__);
512 baud = tty_get_baud_rate(port->tty);
515 * FIXME, we should compare the baud request against the
516 * capability stated in the IR header that we got in the
521 case 2400: ir_baud = SPEED_2400; break;
522 case 9600: ir_baud = SPEED_9600; break;
523 case 19200: ir_baud = SPEED_19200; break;
524 case 38400: ir_baud = SPEED_38400; break;
525 case 57600: ir_baud = SPEED_57600; break;
526 case 115200: ir_baud = SPEED_115200; break;
527 case 576000: ir_baud = SPEED_576000; break;
528 case 1152000: ir_baud = SPEED_1152000; break;
529 case 4000000: ir_baud = SPEED_4000000; break;
532 ir_baud = SPEED_9600;
534 /* And once the new tty stuff is all done we need to
535 call back to correct the baud bits */
539 ir_xbof = ir_xbof_change(ir_add_bof);
541 ir_xbof = ir_xbof_change(xbof) ;
543 /* FIXME need to check to see if our write urb is busy right
544 * now, or use a urb pool.
546 * send the baud change out on an "empty" data packet
548 transfer_buffer = port->write_urb->transfer_buffer;
549 *transfer_buffer = ir_xbof | ir_baud;
554 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
555 port->write_urb->transfer_buffer,
557 ir_write_bulk_callback,
560 port->write_urb->transfer_flags = URB_ZERO_PACKET;
562 result = usb_submit_urb (port->write_urb, GFP_KERNEL);
564 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
568 static int __init ir_init (void)
571 retval = usb_serial_register(&ir_device);
573 goto failed_usb_serial_register;
574 retval = usb_register(&ir_driver);
576 goto failed_usb_register;
577 info(DRIVER_DESC " " DRIVER_VERSION);
580 usb_serial_deregister(&ir_device);
581 failed_usb_serial_register:
586 static void __exit ir_exit (void)
588 usb_deregister (&ir_driver);
589 usb_serial_deregister (&ir_device);
593 module_init(ir_init);
594 module_exit(ir_exit);
596 MODULE_AUTHOR(DRIVER_AUTHOR);
597 MODULE_DESCRIPTION(DRIVER_DESC);
598 MODULE_LICENSE("GPL");
600 module_param(debug, bool, S_IRUGO | S_IWUSR);
601 MODULE_PARM_DESC(debug, "Debug enabled or not");
602 module_param(xbof, int, 0);
603 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
604 module_param(buffer_size, int, 0);
605 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");