xref: /openbmc/linux/drivers/usb/serial/ir-usb.c (revision 55fd7e02)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * USB IR Dongle driver
4  *
5  *	Copyright (C) 2001-2002	Greg Kroah-Hartman (greg@kroah.com)
6  *	Copyright (C) 2002	Gary Brubaker (xavyer@ix.netcom.com)
7  *	Copyright (C) 2010	Johan Hovold (jhovold@gmail.com)
8  *
9  * This driver allows a USB IrDA device to be used as a "dumb" serial device.
10  * This can be useful if you do not have access to a full IrDA stack on the
11  * other side of the connection.  If you do have an IrDA stack on both devices,
12  * please use the usb-irda driver, as it contains the proper error checking and
13  * other goodness of a full IrDA stack.
14  *
15  * Portions of this driver were taken from drivers/net/irda/irda-usb.c, which
16  * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli
17  * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com>
18  *
19  * See Documentation/usb/usb-serial.rst for more information on using this
20  * driver
21  */
22 
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/tty.h>
28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h>
30 #include <linux/module.h>
31 #include <linux/spinlock.h>
32 #include <linux/uaccess.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/usb/irda.h>
36 
37 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Johan Hovold <jhovold@gmail.com>"
38 #define DRIVER_DESC "USB IR Dongle driver"
39 
40 /* if overridden by the user, then use their value for the size of the read and
41  * write urbs */
42 static int buffer_size;
43 
44 /* if overridden by the user, then use the specified number of XBOFs */
45 static int xbof = -1;
46 
47 static int  ir_startup (struct usb_serial *serial);
48 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
49 		const unsigned char *buf, int count);
50 static int ir_write_room(struct tty_struct *tty);
51 static void ir_write_bulk_callback(struct urb *urb);
52 static void ir_process_read_urb(struct urb *urb);
53 static void ir_set_termios(struct tty_struct *tty,
54 		struct usb_serial_port *port, struct ktermios *old_termios);
55 
56 /* Not that this lot means you can only have one per system */
57 static u8 ir_baud;
58 static u8 ir_xbof;
59 static u8 ir_add_bof;
60 
61 static const struct usb_device_id ir_id_table[] = {
62 	{ USB_DEVICE(0x050f, 0x0180) },		/* KC Technology, KC-180 */
63 	{ USB_DEVICE(0x08e9, 0x0100) },		/* XTNDAccess */
64 	{ USB_DEVICE(0x09c4, 0x0011) },		/* ACTiSys ACT-IR2000U */
65 	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
66 	{ }					/* Terminating entry */
67 };
68 
69 MODULE_DEVICE_TABLE(usb, ir_id_table);
70 
71 static struct usb_serial_driver ir_device = {
72 	.driver	= {
73 		.owner	= THIS_MODULE,
74 		.name	= "ir-usb",
75 	},
76 	.description		= "IR Dongle",
77 	.id_table		= ir_id_table,
78 	.num_ports		= 1,
79 	.num_bulk_in		= 1,
80 	.num_bulk_out		= 1,
81 	.set_termios		= ir_set_termios,
82 	.attach			= ir_startup,
83 	.write			= ir_write,
84 	.write_room		= ir_write_room,
85 	.write_bulk_callback	= ir_write_bulk_callback,
86 	.process_read_urb	= ir_process_read_urb,
87 };
88 
89 static struct usb_serial_driver * const serial_drivers[] = {
90 	&ir_device, NULL
91 };
92 
93 static inline void irda_usb_dump_class_desc(struct usb_serial *serial,
94 					    struct usb_irda_cs_descriptor *desc)
95 {
96 	struct device *dev = &serial->dev->dev;
97 
98 	dev_dbg(dev, "bLength=%x\n", desc->bLength);
99 	dev_dbg(dev, "bDescriptorType=%x\n", desc->bDescriptorType);
100 	dev_dbg(dev, "bcdSpecRevision=%x\n", __le16_to_cpu(desc->bcdSpecRevision));
101 	dev_dbg(dev, "bmDataSize=%x\n", desc->bmDataSize);
102 	dev_dbg(dev, "bmWindowSize=%x\n", desc->bmWindowSize);
103 	dev_dbg(dev, "bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
104 	dev_dbg(dev, "wBaudRate=%x\n", __le16_to_cpu(desc->wBaudRate));
105 	dev_dbg(dev, "bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
106 	dev_dbg(dev, "bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
107 	dev_dbg(dev, "bMaxUnicastList=%x\n", desc->bMaxUnicastList);
108 }
109 
110 /*------------------------------------------------------------------*/
111 /*
112  * Function irda_usb_find_class_desc(dev, ifnum)
113  *
114  *    Returns instance of IrDA class descriptor, or NULL if not found
115  *
116  * The class descriptor is some extra info that IrDA USB devices will
117  * offer to us, describing their IrDA characteristics. We will use that in
118  * irda_usb_init_qos()
119  *
120  * Based on the same function in drivers/net/irda/irda-usb.c
121  */
122 static struct usb_irda_cs_descriptor *
123 irda_usb_find_class_desc(struct usb_serial *serial, unsigned int ifnum)
124 {
125 	struct usb_device *dev = serial->dev;
126 	struct usb_irda_cs_descriptor *desc;
127 	int ret;
128 
129 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
130 	if (!desc)
131 		return NULL;
132 
133 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
134 			USB_REQ_CS_IRDA_GET_CLASS_DESC,
135 			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
136 			0, ifnum, desc, sizeof(*desc), 1000);
137 
138 	dev_dbg(&serial->dev->dev, "%s -  ret=%d\n", __func__, ret);
139 	if (ret < (int)sizeof(*desc)) {
140 		dev_dbg(&serial->dev->dev,
141 			"%s - class descriptor read %s (%d)\n", __func__,
142 			(ret < 0) ? "failed" : "too short", ret);
143 		goto error;
144 	}
145 	if (desc->bDescriptorType != USB_DT_CS_IRDA) {
146 		dev_dbg(&serial->dev->dev, "%s - bad class descriptor type\n",
147 			__func__);
148 		goto error;
149 	}
150 
151 	irda_usb_dump_class_desc(serial, desc);
152 	return desc;
153 
154 error:
155 	kfree(desc);
156 	return NULL;
157 }
158 
159 static u8 ir_xbof_change(u8 xbof)
160 {
161 	u8 result;
162 
163 	/* reference irda-usb.c */
164 	switch (xbof) {
165 	case 48:
166 		result = 0x10;
167 		break;
168 	case 28:
169 	case 24:
170 		result = 0x20;
171 		break;
172 	default:
173 	case 12:
174 		result = 0x30;
175 		break;
176 	case  5:
177 	case  6:
178 		result = 0x40;
179 		break;
180 	case  3:
181 		result = 0x50;
182 		break;
183 	case  2:
184 		result = 0x60;
185 		break;
186 	case  1:
187 		result = 0x70;
188 		break;
189 	case  0:
190 		result = 0x80;
191 		break;
192 	}
193 
194 	return(result);
195 }
196 
197 static int ir_startup(struct usb_serial *serial)
198 {
199 	struct usb_irda_cs_descriptor *irda_desc;
200 	int rates;
201 
202 	irda_desc = irda_usb_find_class_desc(serial, 0);
203 	if (!irda_desc) {
204 		dev_err(&serial->dev->dev,
205 			"IRDA class descriptor not found, device not bound\n");
206 		return -ENODEV;
207 	}
208 
209 	rates = le16_to_cpu(irda_desc->wBaudRate);
210 
211 	dev_dbg(&serial->dev->dev,
212 		"%s - Baud rates supported:%s%s%s%s%s%s%s%s%s\n",
213 		__func__,
214 		(rates & USB_IRDA_BR_2400) ? " 2400" : "",
215 		(rates & USB_IRDA_BR_9600) ? " 9600" : "",
216 		(rates & USB_IRDA_BR_19200) ? " 19200" : "",
217 		(rates & USB_IRDA_BR_38400) ? " 38400" : "",
218 		(rates & USB_IRDA_BR_57600) ? " 57600" : "",
219 		(rates & USB_IRDA_BR_115200) ? " 115200" : "",
220 		(rates & USB_IRDA_BR_576000) ? " 576000" : "",
221 		(rates & USB_IRDA_BR_1152000) ? " 1152000" : "",
222 		(rates & USB_IRDA_BR_4000000) ? " 4000000" : "");
223 
224 	switch (irda_desc->bmAdditionalBOFs) {
225 	case USB_IRDA_AB_48:
226 		ir_add_bof = 48;
227 		break;
228 	case USB_IRDA_AB_24:
229 		ir_add_bof = 24;
230 		break;
231 	case USB_IRDA_AB_12:
232 		ir_add_bof = 12;
233 		break;
234 	case USB_IRDA_AB_6:
235 		ir_add_bof = 6;
236 		break;
237 	case USB_IRDA_AB_3:
238 		ir_add_bof = 3;
239 		break;
240 	case USB_IRDA_AB_2:
241 		ir_add_bof = 2;
242 		break;
243 	case USB_IRDA_AB_1:
244 		ir_add_bof = 1;
245 		break;
246 	case USB_IRDA_AB_0:
247 		ir_add_bof = 0;
248 		break;
249 	default:
250 		break;
251 	}
252 
253 	kfree(irda_desc);
254 
255 	return 0;
256 }
257 
258 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
259 		const unsigned char *buf, int count)
260 {
261 	struct urb *urb = NULL;
262 	unsigned long flags;
263 	int ret;
264 
265 	if (port->bulk_out_size == 0)
266 		return -EINVAL;
267 
268 	if (count == 0)
269 		return 0;
270 
271 	count = min(count, port->bulk_out_size - 1);
272 
273 	spin_lock_irqsave(&port->lock, flags);
274 	if (__test_and_clear_bit(0, &port->write_urbs_free)) {
275 		urb = port->write_urbs[0];
276 		port->tx_bytes += count;
277 	}
278 	spin_unlock_irqrestore(&port->lock, flags);
279 
280 	if (!urb)
281 		return 0;
282 
283 	/*
284 	 * The first byte of the packet we send to the device contains an
285 	 * outbound header which indicates an additional number of BOFs and
286 	 * a baud rate change.
287 	 *
288 	 * See section 5.4.2.2 of the USB IrDA spec.
289 	 */
290 	*(u8 *)urb->transfer_buffer = ir_xbof | ir_baud;
291 
292 	memcpy(urb->transfer_buffer + 1, buf, count);
293 
294 	urb->transfer_buffer_length = count + 1;
295 	urb->transfer_flags = URB_ZERO_PACKET;
296 
297 	ret = usb_submit_urb(urb, GFP_ATOMIC);
298 	if (ret) {
299 		dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
300 
301 		spin_lock_irqsave(&port->lock, flags);
302 		__set_bit(0, &port->write_urbs_free);
303 		port->tx_bytes -= count;
304 		spin_unlock_irqrestore(&port->lock, flags);
305 
306 		return ret;
307 	}
308 
309 	return count;
310 }
311 
312 static void ir_write_bulk_callback(struct urb *urb)
313 {
314 	struct usb_serial_port *port = urb->context;
315 	int status = urb->status;
316 	unsigned long flags;
317 
318 	spin_lock_irqsave(&port->lock, flags);
319 	__set_bit(0, &port->write_urbs_free);
320 	port->tx_bytes -= urb->transfer_buffer_length - 1;
321 	spin_unlock_irqrestore(&port->lock, flags);
322 
323 	switch (status) {
324 	case 0:
325 		break;
326 	case -ENOENT:
327 	case -ECONNRESET:
328 	case -ESHUTDOWN:
329 		dev_dbg(&port->dev, "write urb stopped: %d\n", status);
330 		return;
331 	case -EPIPE:
332 		dev_err(&port->dev, "write urb stopped: %d\n", status);
333 		return;
334 	default:
335 		dev_err(&port->dev, "nonzero write-urb status: %d\n", status);
336 		break;
337 	}
338 
339 	usb_serial_port_softint(port);
340 }
341 
342 static int ir_write_room(struct tty_struct *tty)
343 {
344 	struct usb_serial_port *port = tty->driver_data;
345 	int count = 0;
346 
347 	if (port->bulk_out_size == 0)
348 		return 0;
349 
350 	if (test_bit(0, &port->write_urbs_free))
351 		count = port->bulk_out_size - 1;
352 
353 	return count;
354 }
355 
356 static void ir_process_read_urb(struct urb *urb)
357 {
358 	struct usb_serial_port *port = urb->context;
359 	unsigned char *data = urb->transfer_buffer;
360 
361 	if (!urb->actual_length)
362 		return;
363 	/*
364 	 * The first byte of the packet we get from the device
365 	 * contains a busy indicator and baud rate change.
366 	 * See section 5.4.1.2 of the USB IrDA spec.
367 	 */
368 	if (*data & 0x0f)
369 		ir_baud = *data & 0x0f;
370 
371 	if (urb->actual_length == 1)
372 		return;
373 
374 	tty_insert_flip_string(&port->port, data + 1, urb->actual_length - 1);
375 	tty_flip_buffer_push(&port->port);
376 }
377 
378 static void ir_set_termios(struct tty_struct *tty,
379 		struct usb_serial_port *port, struct ktermios *old_termios)
380 {
381 	struct usb_device *udev = port->serial->dev;
382 	unsigned char *transfer_buffer;
383 	int actual_length;
384 	speed_t baud;
385 	int ir_baud;
386 	int ret;
387 
388 	baud = tty_get_baud_rate(tty);
389 
390 	/*
391 	 * FIXME, we should compare the baud request against the
392 	 * capability stated in the IR header that we got in the
393 	 * startup function.
394 	 */
395 
396 	switch (baud) {
397 	case 2400:
398 		ir_baud = USB_IRDA_LS_2400;
399 		break;
400 	case 9600:
401 		ir_baud = USB_IRDA_LS_9600;
402 		break;
403 	case 19200:
404 		ir_baud = USB_IRDA_LS_19200;
405 		break;
406 	case 38400:
407 		ir_baud = USB_IRDA_LS_38400;
408 		break;
409 	case 57600:
410 		ir_baud = USB_IRDA_LS_57600;
411 		break;
412 	case 115200:
413 		ir_baud = USB_IRDA_LS_115200;
414 		break;
415 	case 576000:
416 		ir_baud = USB_IRDA_LS_576000;
417 		break;
418 	case 1152000:
419 		ir_baud = USB_IRDA_LS_1152000;
420 		break;
421 	case 4000000:
422 		ir_baud = USB_IRDA_LS_4000000;
423 		break;
424 	default:
425 		ir_baud = USB_IRDA_LS_9600;
426 		baud = 9600;
427 	}
428 
429 	if (xbof == -1)
430 		ir_xbof = ir_xbof_change(ir_add_bof);
431 	else
432 		ir_xbof = ir_xbof_change(xbof) ;
433 
434 	/* Only speed changes are supported */
435 	tty_termios_copy_hw(&tty->termios, old_termios);
436 	tty_encode_baud_rate(tty, baud, baud);
437 
438 	/*
439 	 * send the baud change out on an "empty" data packet
440 	 */
441 	transfer_buffer = kmalloc(1, GFP_KERNEL);
442 	if (!transfer_buffer)
443 		return;
444 
445 	*transfer_buffer = ir_xbof | ir_baud;
446 
447 	ret = usb_bulk_msg(udev,
448 			usb_sndbulkpipe(udev, port->bulk_out_endpointAddress),
449 			transfer_buffer, 1, &actual_length, 5000);
450 	if (ret || actual_length != 1) {
451 		if (!ret)
452 			ret = -EIO;
453 		dev_err(&port->dev, "failed to change line speed: %d\n", ret);
454 	}
455 
456 	kfree(transfer_buffer);
457 }
458 
459 static int __init ir_init(void)
460 {
461 	if (buffer_size) {
462 		ir_device.bulk_in_size = buffer_size;
463 		ir_device.bulk_out_size = buffer_size;
464 	}
465 
466 	return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ir_id_table);
467 }
468 
469 static void __exit ir_exit(void)
470 {
471 	usb_serial_deregister_drivers(serial_drivers);
472 }
473 
474 
475 module_init(ir_init);
476 module_exit(ir_exit);
477 
478 MODULE_AUTHOR(DRIVER_AUTHOR);
479 MODULE_DESCRIPTION(DRIVER_DESC);
480 MODULE_LICENSE("GPL");
481 
482 module_param(xbof, int, 0);
483 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
484 module_param(buffer_size, int, 0);
485 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
486 
487