xref: /openbmc/linux/drivers/usb/serial/ir-usb.c (revision 78c99ba1)
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
23  * driver
24  *
25  * 2008_Jun_02  Felipe Balbi <me@felipebalbi.com>
26  *	Introduced common header to be used also in USB Gadget Framework.
27  *	Still needs some other style fixes.
28  *
29  * 2007_Jun_21  Alan Cox <alan@lxorguk.ukuu.org.uk>
30  *	Minimal cleanups for some of the driver problens and tty layer abuse.
31  *	Still needs fixing to allow multiple dongles.
32  *
33  * 2002_Mar_07	greg kh
34  *	moved some needed structures and #define values from the
35  *	net/irda/irda-usb.h file into our file, as we don't want to depend on
36  *	that codebase compiling correctly :)
37  *
38  * 2002_Jan_14  gb
39  *	Added module parameter to force specific number of XBOFs.
40  *	Added ir_xbof_change().
41  *	Reorganized read_bulk_callback error handling.
42  *	Switched from FILL_BULK_URB() to usb_fill_bulk_urb().
43  *
44  * 2001_Nov_08  greg kh
45  *	Changed the irda_usb_find_class_desc() function based on comments and
46  *	code from Martin Diehl.
47  *
48  * 2001_Nov_01	greg kh
49  *	Added support for more IrDA USB devices.
50  *	Added support for zero packet.  Added buffer override paramater, so
51  *	users can transfer larger packets at once if they wish.  Both patches
52  *	came from Dag Brattli <dag@obexcode.com>.
53  *
54  * 2001_Oct_07	greg kh
55  *	initial version released.
56  */
57 
58 #include <linux/kernel.h>
59 #include <linux/errno.h>
60 #include <linux/init.h>
61 #include <linux/slab.h>
62 #include <linux/tty.h>
63 #include <linux/tty_driver.h>
64 #include <linux/tty_flip.h>
65 #include <linux/module.h>
66 #include <linux/spinlock.h>
67 #include <linux/uaccess.h>
68 #include <linux/usb.h>
69 #include <linux/usb/serial.h>
70 #include <linux/usb/irda.h>
71 
72 /*
73  * Version Information
74  */
75 #define DRIVER_VERSION "v0.4"
76 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
77 #define DRIVER_DESC "USB IR Dongle driver"
78 
79 static int debug;
80 
81 /* if overridden by the user, then use their value for the size of the read and
82  * write urbs */
83 static int buffer_size;
84 
85 /* if overridden by the user, then use the specified number of XBOFs */
86 static int xbof = -1;
87 
88 static int  ir_startup (struct usb_serial *serial);
89 static int  ir_open(struct tty_struct *tty, struct usb_serial_port *port,
90 					struct file *filep);
91 static void ir_close(struct usb_serial_port *port);
92 static int  ir_write(struct tty_struct *tty, struct usb_serial_port *port,
93 					const unsigned char *buf, int count);
94 static void ir_write_bulk_callback (struct urb *urb);
95 static void ir_read_bulk_callback (struct urb *urb);
96 static void ir_set_termios(struct tty_struct *tty,
97 		struct usb_serial_port *port, struct ktermios *old_termios);
98 
99 /* Not that this lot means you can only have one per system */
100 static u8 ir_baud;
101 static u8 ir_xbof;
102 static u8 ir_add_bof;
103 
104 static struct usb_device_id ir_id_table[] = {
105 	{ USB_DEVICE(0x050f, 0x0180) },		/* KC Technology, KC-180 */
106 	{ USB_DEVICE(0x08e9, 0x0100) },		/* XTNDAccess */
107 	{ USB_DEVICE(0x09c4, 0x0011) },		/* ACTiSys ACT-IR2000U */
108 	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
109 	{ }					/* Terminating entry */
110 };
111 
112 MODULE_DEVICE_TABLE(usb, ir_id_table);
113 
114 static struct usb_driver ir_driver = {
115 	.name		= "ir-usb",
116 	.probe		= usb_serial_probe,
117 	.disconnect	= usb_serial_disconnect,
118 	.id_table	= ir_id_table,
119 	.no_dynamic_id	= 1,
120 };
121 
122 static struct usb_serial_driver ir_device = {
123 	.driver	= {
124 		.owner	= THIS_MODULE,
125 		.name	= "ir-usb",
126 	},
127 	.description		= "IR Dongle",
128 	.usb_driver		= &ir_driver,
129 	.id_table		= ir_id_table,
130 	.num_ports		= 1,
131 	.set_termios		= ir_set_termios,
132 	.attach			= ir_startup,
133 	.open			= ir_open,
134 	.close			= ir_close,
135 	.write			= ir_write,
136 	.write_bulk_callback	= ir_write_bulk_callback,
137 	.read_bulk_callback	= ir_read_bulk_callback,
138 };
139 
140 static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
141 {
142 	dbg("bLength=%x", desc->bLength);
143 	dbg("bDescriptorType=%x", desc->bDescriptorType);
144 	dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
145 	dbg("bmDataSize=%x", desc->bmDataSize);
146 	dbg("bmWindowSize=%x", desc->bmWindowSize);
147 	dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
148 	dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
149 	dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
150 	dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
151 	dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
152 }
153 
154 /*------------------------------------------------------------------*/
155 /*
156  * Function irda_usb_find_class_desc(dev, ifnum)
157  *
158  *    Returns instance of IrDA class descriptor, or NULL if not found
159  *
160  * The class descriptor is some extra info that IrDA USB devices will
161  * offer to us, describing their IrDA characteristics. We will use that in
162  * irda_usb_init_qos()
163  *
164  * Based on the same function in drivers/net/irda/irda-usb.c
165  */
166 static struct usb_irda_cs_descriptor *
167 irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
168 {
169 	struct usb_irda_cs_descriptor *desc;
170 	int ret;
171 
172 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
173 	if (!desc)
174 		return NULL;
175 
176 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
177 			USB_REQ_CS_IRDA_GET_CLASS_DESC,
178 			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
179 			0, ifnum, desc, sizeof(*desc), 1000);
180 
181 	dbg("%s -  ret=%d", __func__, ret);
182 	if (ret < sizeof(*desc)) {
183 		dbg("%s - class descriptor read %s (%d)",
184 				__func__,
185 				(ret < 0) ? "failed" : "too short",
186 				ret);
187 		goto error;
188 	}
189 	if (desc->bDescriptorType != USB_DT_CS_IRDA) {
190 		dbg("%s - bad class descriptor type", __func__);
191 		goto error;
192 	}
193 
194 	irda_usb_dump_class_desc(desc);
195 	return desc;
196 
197 error:
198 	kfree(desc);
199 	return NULL;
200 }
201 
202 
203 static u8 ir_xbof_change(u8 xbof)
204 {
205 	u8 result;
206 
207 	/* reference irda-usb.c */
208 	switch (xbof) {
209 	case 48:
210 		result = 0x10;
211 		break;
212 	case 28:
213 	case 24:
214 		result = 0x20;
215 		break;
216 	default:
217 	case 12:
218 		result = 0x30;
219 		break;
220 	case  5:
221 	case  6:
222 		result = 0x40;
223 		break;
224 	case  3:
225 		result = 0x50;
226 		break;
227 	case  2:
228 		result = 0x60;
229 		break;
230 	case  1:
231 		result = 0x70;
232 		break;
233 	case  0:
234 		result = 0x80;
235 		break;
236 	}
237 
238 	return(result);
239 }
240 
241 
242 static int ir_startup(struct usb_serial *serial)
243 {
244 	struct usb_irda_cs_descriptor *irda_desc;
245 
246 	irda_desc = irda_usb_find_class_desc(serial->dev, 0);
247 	if (!irda_desc) {
248 		dev_err(&serial->dev->dev,
249 			"IRDA class descriptor not found, device not bound\n");
250 		return -ENODEV;
251 	}
252 
253 	dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
254 		__func__,
255 		(irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
256 		(irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
257 		(irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
258 		(irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
259 		(irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
260 		(irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
261 		(irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
262 		(irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
263 		(irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
264 
265 	switch (irda_desc->bmAdditionalBOFs) {
266 	case USB_IRDA_AB_48:
267 		ir_add_bof = 48;
268 		break;
269 	case USB_IRDA_AB_24:
270 		ir_add_bof = 24;
271 		break;
272 	case USB_IRDA_AB_12:
273 		ir_add_bof = 12;
274 		break;
275 	case USB_IRDA_AB_6:
276 		ir_add_bof = 6;
277 		break;
278 	case USB_IRDA_AB_3:
279 		ir_add_bof = 3;
280 		break;
281 	case USB_IRDA_AB_2:
282 		ir_add_bof = 2;
283 		break;
284 	case USB_IRDA_AB_1:
285 		ir_add_bof = 1;
286 		break;
287 	case USB_IRDA_AB_0:
288 		ir_add_bof = 0;
289 		break;
290 	default:
291 		break;
292 	}
293 
294 	kfree(irda_desc);
295 
296 	return 0;
297 }
298 
299 static int ir_open(struct tty_struct *tty,
300 			struct usb_serial_port *port, struct file *filp)
301 {
302 	char *buffer;
303 	int result = 0;
304 
305 	dbg("%s - port %d", __func__, port->number);
306 
307 	if (buffer_size) {
308 		/* override the default buffer sizes */
309 		buffer = kmalloc(buffer_size, GFP_KERNEL);
310 		if (!buffer) {
311 			dev_err(&port->dev, "%s - out of memory.\n", __func__);
312 			return -ENOMEM;
313 		}
314 		kfree(port->read_urb->transfer_buffer);
315 		port->read_urb->transfer_buffer = buffer;
316 		port->read_urb->transfer_buffer_length = buffer_size;
317 
318 		buffer = kmalloc(buffer_size, GFP_KERNEL);
319 		if (!buffer) {
320 			dev_err(&port->dev, "%s - out of memory.\n", __func__);
321 			return -ENOMEM;
322 		}
323 		kfree(port->write_urb->transfer_buffer);
324 		port->write_urb->transfer_buffer = buffer;
325 		port->write_urb->transfer_buffer_length = buffer_size;
326 		port->bulk_out_size = buffer_size;
327 	}
328 
329 	/* Start reading from the device */
330 	usb_fill_bulk_urb(
331 		port->read_urb,
332 		port->serial->dev,
333 		usb_rcvbulkpipe(port->serial->dev,
334 			port->bulk_in_endpointAddress),
335 		port->read_urb->transfer_buffer,
336 		port->read_urb->transfer_buffer_length,
337 		ir_read_bulk_callback,
338 		port);
339 	result = usb_submit_urb(port->read_urb, GFP_KERNEL);
340 	if (result)
341 		dev_err(&port->dev,
342 			"%s - failed submitting read urb, error %d\n",
343 			__func__, result);
344 
345 	return result;
346 }
347 
348 static void ir_close(struct usb_serial_port *port)
349 {
350 	dbg("%s - port %d", __func__, port->number);
351 
352 	/* shutdown our bulk read */
353 	usb_kill_urb(port->read_urb);
354 }
355 
356 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
357 					const unsigned char *buf, int count)
358 {
359 	unsigned char *transfer_buffer;
360 	int result;
361 	int transfer_size;
362 
363 	dbg("%s - port = %d, count = %d", __func__, port->number, count);
364 
365 	if (count == 0)
366 		return 0;
367 
368 	spin_lock_bh(&port->lock);
369 	if (port->write_urb_busy) {
370 		spin_unlock_bh(&port->lock);
371 		dbg("%s - already writing", __func__);
372 		return 0;
373 	}
374 	port->write_urb_busy = 1;
375 	spin_unlock_bh(&port->lock);
376 
377 	transfer_buffer = port->write_urb->transfer_buffer;
378 	transfer_size = min(count, port->bulk_out_size - 1);
379 
380 	/*
381 	 * The first byte of the packet we send to the device contains an
382 	 * inbound header which indicates an additional number of BOFs and
383 	 * a baud rate change.
384 	 *
385 	 * See section 5.4.2.2 of the USB IrDA spec.
386 	 */
387 	*transfer_buffer = ir_xbof | ir_baud;
388 	++transfer_buffer;
389 
390 	memcpy(transfer_buffer, buf, transfer_size);
391 
392 	usb_fill_bulk_urb(
393 		port->write_urb,
394 		port->serial->dev,
395 		usb_sndbulkpipe(port->serial->dev,
396 			port->bulk_out_endpointAddress),
397 		port->write_urb->transfer_buffer,
398 		transfer_size + 1,
399 		ir_write_bulk_callback,
400 		port);
401 
402 	port->write_urb->transfer_flags = URB_ZERO_PACKET;
403 
404 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
405 	if (result) {
406 		port->write_urb_busy = 0;
407 		dev_err(&port->dev,
408 			"%s - failed submitting write urb, error %d\n",
409 			__func__, result);
410 	} else
411 		result = transfer_size;
412 
413 	return result;
414 }
415 
416 static void ir_write_bulk_callback(struct urb *urb)
417 {
418 	struct usb_serial_port *port = urb->context;
419 	int status = urb->status;
420 
421 	dbg("%s - port %d", __func__, port->number);
422 
423 	port->write_urb_busy = 0;
424 	if (status) {
425 		dbg("%s - nonzero write bulk status received: %d",
426 		    __func__, status);
427 		return;
428 	}
429 
430 	usb_serial_debug_data(
431 		debug,
432 		&port->dev,
433 		__func__,
434 		urb->actual_length,
435 		urb->transfer_buffer);
436 
437 	usb_serial_port_softint(port);
438 }
439 
440 static void ir_read_bulk_callback(struct urb *urb)
441 {
442 	struct usb_serial_port *port = urb->context;
443 	struct tty_struct *tty;
444 	unsigned char *data = urb->transfer_buffer;
445 	int result;
446 	int status = urb->status;
447 
448 	dbg("%s - port %d", __func__, port->number);
449 
450 	if (!port->port.count) {
451 		dbg("%s - port closed.", __func__);
452 		return;
453 	}
454 
455 	switch (status) {
456 	case 0: /* Successful */
457 		/*
458 		 * The first byte of the packet we get from the device
459 		 * contains a busy indicator and baud rate change.
460 		 * See section 5.4.1.2 of the USB IrDA spec.
461 		 */
462 		if ((*data & 0x0f) > 0)
463 			ir_baud = *data & 0x0f;
464 		usb_serial_debug_data(debug, &port->dev, __func__,
465 						urb->actual_length, data);
466 		tty = tty_port_tty_get(&port->port);
467 		if (tty_buffer_request_room(tty, urb->actual_length - 1)) {
468 			tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
469 			tty_flip_buffer_push(tty);
470 		}
471 		tty_kref_put(tty);
472 
473 		/*
474 		 * No break here.
475 		 * We want to resubmit the urb so we can read
476 		 * again.
477 		 */
478 
479 	case -EPROTO: /* taking inspiration from pl2303.c */
480 			/* Continue trying to always read */
481 		usb_fill_bulk_urb(
482 			port->read_urb,
483 			port->serial->dev,
484 			usb_rcvbulkpipe(port->serial->dev,
485 				port->bulk_in_endpointAddress),
486 			port->read_urb->transfer_buffer,
487 			port->read_urb->transfer_buffer_length,
488 			ir_read_bulk_callback,
489 			port);
490 
491 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
492 		if (result)
493 			dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
494 				__func__, result);
495 			break ;
496 	default:
497 		dbg("%s - nonzero read bulk status received: %d",
498 			__func__, status);
499 		break ;
500 	}
501 	return;
502 }
503 
504 static void ir_set_termios(struct tty_struct *tty,
505 		struct usb_serial_port *port, struct ktermios *old_termios)
506 {
507 	unsigned char *transfer_buffer;
508 	int result;
509 	speed_t baud;
510 	int ir_baud;
511 
512 	dbg("%s - port %d", __func__, port->number);
513 
514 	baud = tty_get_baud_rate(tty);
515 
516 	/*
517 	 * FIXME, we should compare the baud request against the
518 	 * capability stated in the IR header that we got in the
519 	 * startup function.
520 	 */
521 
522 	switch (baud) {
523 	case 2400:
524 		ir_baud = USB_IRDA_BR_2400;
525 		break;
526 	case 9600:
527 		ir_baud = USB_IRDA_BR_9600;
528 		break;
529 	case 19200:
530 		ir_baud = USB_IRDA_BR_19200;
531 		break;
532 	case 38400:
533 		ir_baud = USB_IRDA_BR_38400;
534 		break;
535 	case 57600:
536 		ir_baud = USB_IRDA_BR_57600;
537 		break;
538 	case 115200:
539 		ir_baud = USB_IRDA_BR_115200;
540 		break;
541 	case 576000:
542 		ir_baud = USB_IRDA_BR_576000;
543 		break;
544 	case 1152000:
545 		ir_baud = USB_IRDA_BR_1152000;
546 		break;
547 	case 4000000:
548 		ir_baud = USB_IRDA_BR_4000000;
549 		break;
550 	default:
551 		ir_baud = USB_IRDA_BR_9600;
552 		baud = 9600;
553 	}
554 
555 	if (xbof == -1)
556 		ir_xbof = ir_xbof_change(ir_add_bof);
557 	else
558 		ir_xbof = ir_xbof_change(xbof) ;
559 
560 	/* FIXME need to check to see if our write urb is busy right
561 	 * now, or use a urb pool.
562 	 *
563 	 * send the baud change out on an "empty" data packet
564 	 */
565 	transfer_buffer = port->write_urb->transfer_buffer;
566 	*transfer_buffer = ir_xbof | ir_baud;
567 
568 	usb_fill_bulk_urb(
569 		port->write_urb,
570 		port->serial->dev,
571 		usb_sndbulkpipe(port->serial->dev,
572 			port->bulk_out_endpointAddress),
573 		port->write_urb->transfer_buffer,
574 		1,
575 		ir_write_bulk_callback,
576 		port);
577 
578 	port->write_urb->transfer_flags = URB_ZERO_PACKET;
579 
580 	result = usb_submit_urb(port->write_urb, GFP_KERNEL);
581 	if (result)
582 		dev_err(&port->dev,
583 				"%s - failed submitting write urb, error %d\n",
584 				__func__, result);
585 
586 	/* Only speed changes are supported */
587 	tty_termios_copy_hw(tty->termios, old_termios);
588 	tty_encode_baud_rate(tty, baud, baud);
589 }
590 
591 static int __init ir_init(void)
592 {
593 	int retval;
594 
595 	retval = usb_serial_register(&ir_device);
596 	if (retval)
597 		goto failed_usb_serial_register;
598 
599 	retval = usb_register(&ir_driver);
600 	if (retval)
601 		goto failed_usb_register;
602 
603 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
604 	       DRIVER_DESC "\n");
605 
606 	return 0;
607 
608 failed_usb_register:
609 	usb_serial_deregister(&ir_device);
610 
611 failed_usb_serial_register:
612 	return retval;
613 }
614 
615 static void __exit ir_exit(void)
616 {
617 	usb_deregister(&ir_driver);
618 	usb_serial_deregister(&ir_device);
619 }
620 
621 
622 module_init(ir_init);
623 module_exit(ir_exit);
624 
625 MODULE_AUTHOR(DRIVER_AUTHOR);
626 MODULE_DESCRIPTION(DRIVER_DESC);
627 MODULE_LICENSE("GPL");
628 
629 module_param(debug, bool, S_IRUGO | S_IWUSR);
630 MODULE_PARM_DESC(debug, "Debug enabled or not");
631 module_param(xbof, int, 0);
632 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
633 module_param(buffer_size, int, 0);
634 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
635 
636