Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / usb / serial / ir-usb.c
blob6b803ab98543760d5669f13dced088dee87bb8d4
1 /*
2 * USB IR Dongle driver
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.
28 * 2002_Mar_07 greg kh
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 :)
33 * 2002_Jan_14 gb
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().
39 * 2001_Nov_08 greg kh
40 * Changed the irda_usb_find_class_desc() function based on comments and
41 * code from Martin Diehl.
43 * 2001_Nov_01 greg kh
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>.
49 * 2001_Oct_07 greg kh
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>
67 * Version Information
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 {
88 u8 bLength;
89 u8 bDescriptorType;
90 u16 bcdSpecRevision;
91 u8 bmDataSize;
92 u8 bmWindowSize;
93 u8 bmMinTurnaroundTime;
94 u16 wBaudRate;
95 u8 bmAdditionalBOFs;
96 u8 bIrdaRateSniff;
97 u8 bMaxUnicastList;
98 } __attribute__ ((packed));
100 static int debug;
102 /* if overridden by the user, then use their value for the size of the read and
103 * write urbs */
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 = {
132 .name = "ir-usb",
133 .probe = usb_serial_probe,
134 .disconnect = usb_serial_disconnect,
135 .id_table = id_table,
136 .no_dynamic_id = 1,
140 static struct usb_serial_driver ir_device = {
141 .driver = {
142 .owner = THIS_MODULE,
143 .name = "ir-usb",
145 .description = "IR Dongle",
146 .usb_driver = &ir_driver,
147 .id_table = id_table,
148 .num_interrupt_in = 1,
149 .num_bulk_in = 1,
150 .num_bulk_out = 1,
151 .num_ports = 1,
152 .set_termios = ir_set_termios,
153 .attach = ir_startup,
154 .open = ir_open,
155 .close = ir_close,
156 .write = ir_write,
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;
190 int ret;
192 desc = kzalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
193 if (desc == NULL)
194 return NULL;
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)",
204 __FUNCTION__,
205 (ret<0) ? "failed" : "too short",
206 ret);
207 goto error;
209 if (desc->bDescriptorType != USB_DT_IRDA) {
210 dbg("%s - bad class descriptor type", __FUNCTION__);
211 goto error;
214 irda_usb_dump_class_desc(desc);
215 return desc;
216 error:
217 kfree(desc);
218 return NULL;
222 static u8 ir_xbof_change(u8 xbof)
224 u8 result;
225 /* reference irda-usb.c */
226 switch(xbof) {
227 case 48: result = 0x10; break;
228 case 28:
229 case 24: result = 0x20; break;
230 default:
231 case 12: result = 0x30; break;
232 case 5:
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;
239 return(result);
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");
250 return -ENODEV;
253 dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
254 __FUNCTION__,
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;
274 default:;
277 kfree (irda_desc);
279 return 0;
282 static int ir_open (struct usb_serial_port *port, struct file *filp)
284 char *buffer;
285 int result = 0;
287 dbg("%s - port %d", __FUNCTION__, port->number);
289 if (buffer_size) {
290 /* override the default buffer sizes */
291 buffer = kmalloc (buffer_size, GFP_KERNEL);
292 if (!buffer) {
293 dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
294 return -ENOMEM;
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);
301 if (!buffer) {
302 dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
303 return -ENOMEM;
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 */
312 usb_fill_bulk_urb (
313 port->read_urb,
314 port->serial->dev,
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,
319 port);
320 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
321 if (result)
322 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
324 return 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;
338 int result;
339 int transfer_size;
341 dbg("%s - port = %d, count = %d", __FUNCTION__, port->number, count);
343 if (!port->tty) {
344 dev_err (&port->dev, "%s - no tty???\n", __FUNCTION__);
345 return 0;
348 if (count == 0)
349 return 0;
351 spin_lock_bh(&port->lock);
352 if (port->write_urb_busy) {
353 spin_unlock_bh(&port->lock);
354 dbg("%s - already writing", __FUNCTION__);
355 return 0;
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;
371 ++transfer_buffer;
373 memcpy (transfer_buffer, buf, transfer_size);
375 usb_fill_bulk_urb (
376 port->write_urb,
377 port->serial->dev,
378 usb_sndbulkpipe(port->serial->dev,
379 port->bulk_out_endpointAddress),
380 port->write_urb->transfer_buffer,
381 transfer_size + 1,
382 ir_write_bulk_callback,
383 port);
385 port->write_urb->transfer_flags = URB_ZERO_PACKET;
387 result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
388 if (result) {
389 port->write_urb_busy = 0;
390 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
391 } else
392 result = transfer_size;
394 return result;
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;
405 if (status) {
406 dbg("%s - nonzero write bulk status received: %d",
407 __FUNCTION__, status);
408 return;
411 usb_serial_debug_data (
412 debug,
413 &port->dev,
414 __FUNCTION__,
415 urb->actual_length,
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;
426 int result;
427 int status = urb->status;
429 dbg("%s - port %d", __FUNCTION__, port->number);
431 if (!port->open_count) {
432 dbg("%s - port closed.", __FUNCTION__);
433 return;
436 switch (status) {
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 (
448 debug,
449 &port->dev,
450 __FUNCTION__,
451 urb->actual_length,
452 data);
454 tty = port->tty;
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);
462 * No break here.
463 * We want to resubmit the urb so we can read
464 * again.
467 case -EPROTO: /* taking inspiration from pl2303.c */
469 /* Continue trying to always read */
470 usb_fill_bulk_urb (
471 port->read_urb,
472 port->serial->dev,
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,
478 port);
480 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
481 if (result)
482 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
483 __FUNCTION__, result);
485 break ;
487 default:
488 dbg("%s - nonzero read bulk status received: %d",
489 __FUNCTION__,
490 status);
491 break ;
495 return;
498 static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
500 unsigned char *transfer_buffer;
501 int result;
502 speed_t baud;
503 int ir_baud;
505 dbg("%s - port %d", __FUNCTION__, port->number);
507 baud = tty_get_baud_rate(port->tty);
510 * FIXME, we should compare the baud request against the
511 * capability stated in the IR header that we got in the
512 * startup function.
515 switch (baud) {
516 case 2400: ir_baud = SPEED_2400; break;
517 case 9600: ir_baud = SPEED_9600; break;
518 case 19200: ir_baud = SPEED_19200; break;
519 case 38400: ir_baud = SPEED_38400; break;
520 case 57600: ir_baud = SPEED_57600; break;
521 case 115200: ir_baud = SPEED_115200; break;
522 case 576000: ir_baud = SPEED_576000; break;
523 case 1152000: ir_baud = SPEED_1152000; break;
524 case 4000000: ir_baud = SPEED_4000000; break;
525 break;
526 default:
527 ir_baud = SPEED_9600;
528 baud = 9600;
531 if (xbof == -1)
532 ir_xbof = ir_xbof_change(ir_add_bof);
533 else
534 ir_xbof = ir_xbof_change(xbof) ;
536 /* FIXME need to check to see if our write urb is busy right
537 * now, or use a urb pool.
539 * send the baud change out on an "empty" data packet
541 transfer_buffer = port->write_urb->transfer_buffer;
542 *transfer_buffer = ir_xbof | ir_baud;
544 usb_fill_bulk_urb (
545 port->write_urb,
546 port->serial->dev,
547 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
548 port->write_urb->transfer_buffer,
550 ir_write_bulk_callback,
551 port);
553 port->write_urb->transfer_flags = URB_ZERO_PACKET;
555 result = usb_submit_urb (port->write_urb, GFP_KERNEL);
556 if (result)
557 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
559 /* Only speed changes are supported */
560 tty_termios_copy_hw(port->tty->termios, old_termios);
561 tty_encode_baud_rate(port->tty, baud, baud);
565 static int __init ir_init (void)
567 int retval;
568 retval = usb_serial_register(&ir_device);
569 if (retval)
570 goto failed_usb_serial_register;
571 retval = usb_register(&ir_driver);
572 if (retval)
573 goto failed_usb_register;
574 info(DRIVER_DESC " " DRIVER_VERSION);
575 return 0;
576 failed_usb_register:
577 usb_serial_deregister(&ir_device);
578 failed_usb_serial_register:
579 return retval;
583 static void __exit ir_exit (void)
585 usb_deregister (&ir_driver);
586 usb_serial_deregister (&ir_device);
590 module_init(ir_init);
591 module_exit(ir_exit);
593 MODULE_AUTHOR(DRIVER_AUTHOR);
594 MODULE_DESCRIPTION(DRIVER_DESC);
595 MODULE_LICENSE("GPL");
597 module_param(debug, bool, S_IRUGO | S_IWUSR);
598 MODULE_PARM_DESC(debug, "Debug enabled or not");
599 module_param(xbof, int, 0);
600 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
601 module_param(buffer_size, int, 0);
602 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");