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