xref: /openbmc/linux/drivers/usb/serial/ir-usb.c (revision 17a0184ca17e288decdca8b2841531e34d49285f)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * USB IR Dongle driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Copyright (C) 2001-2002	Greg Kroah-Hartman (greg@kroah.com)
61da177e4SLinus Torvalds  *	Copyright (C) 2002	Gary Brubaker (xavyer@ix.netcom.com)
7f4a4cbb2SJohan Hovold  *	Copyright (C) 2010	Johan Hovold (jhovold@gmail.com)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This driver allows a USB IrDA device to be used as a "dumb" serial device.
101da177e4SLinus Torvalds  * This can be useful if you do not have access to a full IrDA stack on the
111da177e4SLinus Torvalds  * other side of the connection.  If you do have an IrDA stack on both devices,
121da177e4SLinus Torvalds  * please use the usb-irda driver, as it contains the proper error checking and
131da177e4SLinus Torvalds  * other goodness of a full IrDA stack.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Portions of this driver were taken from drivers/net/irda/irda-usb.c, which
161da177e4SLinus Torvalds  * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli
171da177e4SLinus Torvalds  * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com>
181da177e4SLinus Torvalds  *
19ecefae6dSMauro Carvalho Chehab  * See Documentation/usb/usb-serial.rst for more information on using this
20e0d795e4SFelipe Balbi  * driver
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/kernel.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/tty.h>
281da177e4SLinus Torvalds #include <linux/tty_driver.h>
291da177e4SLinus Torvalds #include <linux/tty_flip.h>
301da177e4SLinus Torvalds #include <linux/module.h>
311da177e4SLinus Torvalds #include <linux/spinlock.h>
32e0d795e4SFelipe Balbi #include <linux/uaccess.h>
331da177e4SLinus Torvalds #include <linux/usb.h>
34a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
35e0d795e4SFelipe Balbi #include <linux/usb/irda.h>
361da177e4SLinus Torvalds 
37f4a4cbb2SJohan Hovold #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Johan Hovold <jhovold@gmail.com>"
381da177e4SLinus Torvalds #define DRIVER_DESC "USB IR Dongle driver"
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /* if overridden by the user, then use their value for the size of the read and
411da177e4SLinus Torvalds  * write urbs */
421da177e4SLinus Torvalds static int buffer_size;
43e0d795e4SFelipe Balbi 
441da177e4SLinus Torvalds /* if overridden by the user, then use the specified number of XBOFs */
451da177e4SLinus Torvalds static int xbof = -1;
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static int  ir_startup (struct usb_serial *serial);
48a509a7e4SAlan Cox static int  ir_open(struct tty_struct *tty, struct usb_serial_port *port);
49f4a4cbb2SJohan Hovold static int ir_prepare_write_buffer(struct usb_serial_port *port,
50f4a4cbb2SJohan Hovold 						void *dest, size_t size);
51f4a4cbb2SJohan Hovold static void ir_process_read_urb(struct urb *urb);
5295da310eSAlan Cox static void ir_set_termios(struct tty_struct *tty,
5395da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios);
541da177e4SLinus Torvalds 
55a6ea438bSAlan Cox /* Not that this lot means you can only have one per system */
56e0d795e4SFelipe Balbi static u8 ir_baud;
57e0d795e4SFelipe Balbi static u8 ir_xbof;
58e0d795e4SFelipe Balbi static u8 ir_add_bof;
591da177e4SLinus Torvalds 
607d40d7e8SNémeth Márton static const struct usb_device_id ir_id_table[] = {
611da177e4SLinus Torvalds 	{ USB_DEVICE(0x050f, 0x0180) },		/* KC Technology, KC-180 */
621da177e4SLinus Torvalds 	{ USB_DEVICE(0x08e9, 0x0100) },		/* XTNDAccess */
631da177e4SLinus Torvalds 	{ USB_DEVICE(0x09c4, 0x0011) },		/* ACTiSys ACT-IR2000U */
64e0d795e4SFelipe Balbi 	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
651da177e4SLinus Torvalds 	{ }					/* Terminating entry */
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
68e0d795e4SFelipe Balbi MODULE_DEVICE_TABLE(usb, ir_id_table);
691da177e4SLinus Torvalds 
70ea65370dSGreg Kroah-Hartman static struct usb_serial_driver ir_device = {
7118fcac35SGreg Kroah-Hartman 	.driver	= {
721da177e4SLinus Torvalds 		.owner	= THIS_MODULE,
73269bda1cSGreg Kroah-Hartman 		.name	= "ir-usb",
7418fcac35SGreg Kroah-Hartman 	},
75269bda1cSGreg Kroah-Hartman 	.description		= "IR Dongle",
76e0d795e4SFelipe Balbi 	.id_table		= ir_id_table,
771da177e4SLinus Torvalds 	.num_ports		= 1,
781da177e4SLinus Torvalds 	.set_termios		= ir_set_termios,
791da177e4SLinus Torvalds 	.attach			= ir_startup,
801da177e4SLinus Torvalds 	.open			= ir_open,
81f4a4cbb2SJohan Hovold 	.prepare_write_buffer	= ir_prepare_write_buffer,
82f4a4cbb2SJohan Hovold 	.process_read_urb	= ir_process_read_urb,
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
857dbe2460SAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
867dbe2460SAlan Stern 	&ir_device, NULL
877dbe2460SAlan Stern };
887dbe2460SAlan Stern 
899f857ae9SGreg Kroah-Hartman static inline void irda_usb_dump_class_desc(struct usb_serial *serial,
909f857ae9SGreg Kroah-Hartman 					    struct usb_irda_cs_descriptor *desc)
911da177e4SLinus Torvalds {
929f857ae9SGreg Kroah-Hartman 	struct device *dev = &serial->dev->dev;
939f857ae9SGreg Kroah-Hartman 
949f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bLength=%x\n", desc->bLength);
959f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bDescriptorType=%x\n", desc->bDescriptorType);
969f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bcdSpecRevision=%x\n", __le16_to_cpu(desc->bcdSpecRevision));
979f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bmDataSize=%x\n", desc->bmDataSize);
989f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bmWindowSize=%x\n", desc->bmWindowSize);
999f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1009f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "wBaudRate=%x\n", __le16_to_cpu(desc->wBaudRate));
1019f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1029f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1039f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /*------------------------------------------------------------------*/
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * Function irda_usb_find_class_desc(dev, ifnum)
1091da177e4SLinus Torvalds  *
1101da177e4SLinus Torvalds  *    Returns instance of IrDA class descriptor, or NULL if not found
1111da177e4SLinus Torvalds  *
1121da177e4SLinus Torvalds  * The class descriptor is some extra info that IrDA USB devices will
1131da177e4SLinus Torvalds  * offer to us, describing their IrDA characteristics. We will use that in
1141da177e4SLinus Torvalds  * irda_usb_init_qos()
1151da177e4SLinus Torvalds  *
1161da177e4SLinus Torvalds  * Based on the same function in drivers/net/irda/irda-usb.c
1171da177e4SLinus Torvalds  */
118e0d795e4SFelipe Balbi static struct usb_irda_cs_descriptor *
1199f857ae9SGreg Kroah-Hartman irda_usb_find_class_desc(struct usb_serial *serial, unsigned int ifnum)
1201da177e4SLinus Torvalds {
1219f857ae9SGreg Kroah-Hartman 	struct usb_device *dev = serial->dev;
122e0d795e4SFelipe Balbi 	struct usb_irda_cs_descriptor *desc;
1231da177e4SLinus Torvalds 	int ret;
1241da177e4SLinus Torvalds 
125e0d795e4SFelipe Balbi 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
126e0d795e4SFelipe Balbi 	if (!desc)
1271da177e4SLinus Torvalds 		return NULL;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
130e0d795e4SFelipe Balbi 			USB_REQ_CS_IRDA_GET_CLASS_DESC,
1311da177e4SLinus Torvalds 			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1321da177e4SLinus Torvalds 			0, ifnum, desc, sizeof(*desc), 1000);
1331da177e4SLinus Torvalds 
1349f857ae9SGreg Kroah-Hartman 	dev_dbg(&serial->dev->dev, "%s -  ret=%d\n", __func__, ret);
1353391ca1dSChengguang Xu 	if (ret < (int)sizeof(*desc)) {
1369f857ae9SGreg Kroah-Hartman 		dev_dbg(&serial->dev->dev,
1379f857ae9SGreg Kroah-Hartman 			"%s - class descriptor read %s (%d)\n", __func__,
1389f857ae9SGreg Kroah-Hartman 			(ret < 0) ? "failed" : "too short", ret);
1391da177e4SLinus Torvalds 		goto error;
1401da177e4SLinus Torvalds 	}
141e0d795e4SFelipe Balbi 	if (desc->bDescriptorType != USB_DT_CS_IRDA) {
1429f857ae9SGreg Kroah-Hartman 		dev_dbg(&serial->dev->dev, "%s - bad class descriptor type\n",
1439f857ae9SGreg Kroah-Hartman 			__func__);
1441da177e4SLinus Torvalds 		goto error;
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 
1479f857ae9SGreg Kroah-Hartman 	irda_usb_dump_class_desc(serial, desc);
1481da177e4SLinus Torvalds 	return desc;
149e0d795e4SFelipe Balbi 
1501da177e4SLinus Torvalds error:
1511da177e4SLinus Torvalds 	kfree(desc);
1521da177e4SLinus Torvalds 	return NULL;
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds static u8 ir_xbof_change(u8 xbof)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	u8 result;
158e0d795e4SFelipe Balbi 
1591da177e4SLinus Torvalds 	/* reference irda-usb.c */
1601da177e4SLinus Torvalds 	switch (xbof) {
161e0d795e4SFelipe Balbi 	case 48:
162e0d795e4SFelipe Balbi 		result = 0x10;
163e0d795e4SFelipe Balbi 		break;
1641da177e4SLinus Torvalds 	case 28:
165e0d795e4SFelipe Balbi 	case 24:
166e0d795e4SFelipe Balbi 		result = 0x20;
167e0d795e4SFelipe Balbi 		break;
1681da177e4SLinus Torvalds 	default:
169e0d795e4SFelipe Balbi 	case 12:
170e0d795e4SFelipe Balbi 		result = 0x30;
171e0d795e4SFelipe Balbi 		break;
1721da177e4SLinus Torvalds 	case  5:
173e0d795e4SFelipe Balbi 	case  6:
174e0d795e4SFelipe Balbi 		result = 0x40;
175e0d795e4SFelipe Balbi 		break;
176e0d795e4SFelipe Balbi 	case  3:
177e0d795e4SFelipe Balbi 		result = 0x50;
178e0d795e4SFelipe Balbi 		break;
179e0d795e4SFelipe Balbi 	case  2:
180e0d795e4SFelipe Balbi 		result = 0x60;
181e0d795e4SFelipe Balbi 		break;
182e0d795e4SFelipe Balbi 	case  1:
183e0d795e4SFelipe Balbi 		result = 0x70;
184e0d795e4SFelipe Balbi 		break;
185e0d795e4SFelipe Balbi 	case  0:
186e0d795e4SFelipe Balbi 		result = 0x80;
187e0d795e4SFelipe Balbi 		break;
1881da177e4SLinus Torvalds 	}
189e0d795e4SFelipe Balbi 
1901da177e4SLinus Torvalds 	return(result);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds static int ir_startup(struct usb_serial *serial)
1941da177e4SLinus Torvalds {
195e0d795e4SFelipe Balbi 	struct usb_irda_cs_descriptor *irda_desc;
196ad0ccac7SJohan Hovold 	int rates;
1971da177e4SLinus Torvalds 
1982988a8aeSJohan Hovold 	if (serial->num_bulk_in < 1 || serial->num_bulk_out < 1)
1992988a8aeSJohan Hovold 		return -ENODEV;
2002988a8aeSJohan Hovold 
2019f857ae9SGreg Kroah-Hartman 	irda_desc = irda_usb_find_class_desc(serial, 0);
202e0d795e4SFelipe Balbi 	if (!irda_desc) {
203e0d795e4SFelipe Balbi 		dev_err(&serial->dev->dev,
204e0d795e4SFelipe Balbi 			"IRDA class descriptor not found, device not bound\n");
2051da177e4SLinus Torvalds 		return -ENODEV;
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 
208ad0ccac7SJohan Hovold 	rates = le16_to_cpu(irda_desc->wBaudRate);
209ad0ccac7SJohan Hovold 
2109f857ae9SGreg Kroah-Hartman 	dev_dbg(&serial->dev->dev,
2119f857ae9SGreg Kroah-Hartman 		"%s - Baud rates supported:%s%s%s%s%s%s%s%s%s\n",
212441b62c1SHarvey Harrison 		__func__,
213ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_2400) ? " 2400" : "",
214ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_9600) ? " 9600" : "",
215ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_19200) ? " 19200" : "",
216ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_38400) ? " 38400" : "",
217ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_57600) ? " 57600" : "",
218ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_115200) ? " 115200" : "",
219ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_576000) ? " 576000" : "",
220ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_1152000) ? " 1152000" : "",
221ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_4000000) ? " 4000000" : "");
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	switch (irda_desc->bmAdditionalBOFs) {
224e0d795e4SFelipe Balbi 	case USB_IRDA_AB_48:
225e0d795e4SFelipe Balbi 		ir_add_bof = 48;
226e0d795e4SFelipe Balbi 		break;
227e0d795e4SFelipe Balbi 	case USB_IRDA_AB_24:
228e0d795e4SFelipe Balbi 		ir_add_bof = 24;
229e0d795e4SFelipe Balbi 		break;
230e0d795e4SFelipe Balbi 	case USB_IRDA_AB_12:
231e0d795e4SFelipe Balbi 		ir_add_bof = 12;
232e0d795e4SFelipe Balbi 		break;
233e0d795e4SFelipe Balbi 	case USB_IRDA_AB_6:
234e0d795e4SFelipe Balbi 		ir_add_bof = 6;
235e0d795e4SFelipe Balbi 		break;
236e0d795e4SFelipe Balbi 	case USB_IRDA_AB_3:
237e0d795e4SFelipe Balbi 		ir_add_bof = 3;
238e0d795e4SFelipe Balbi 		break;
239e0d795e4SFelipe Balbi 	case USB_IRDA_AB_2:
240e0d795e4SFelipe Balbi 		ir_add_bof = 2;
241e0d795e4SFelipe Balbi 		break;
242e0d795e4SFelipe Balbi 	case USB_IRDA_AB_1:
243e0d795e4SFelipe Balbi 		ir_add_bof = 1;
244e0d795e4SFelipe Balbi 		break;
245e0d795e4SFelipe Balbi 	case USB_IRDA_AB_0:
246e0d795e4SFelipe Balbi 		ir_add_bof = 0;
247e0d795e4SFelipe Balbi 		break;
248e0d795e4SFelipe Balbi 	default:
249e0d795e4SFelipe Balbi 		break;
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	kfree(irda_desc);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	return 0;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
257a509a7e4SAlan Cox static int ir_open(struct tty_struct *tty, struct usb_serial_port *port)
2581da177e4SLinus Torvalds {
259f4a4cbb2SJohan Hovold 	int i;
2601da177e4SLinus Torvalds 
261f4a4cbb2SJohan Hovold 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
262f4a4cbb2SJohan Hovold 		port->write_urbs[i]->transfer_flags = URB_ZERO_PACKET;
263f4a4cbb2SJohan Hovold 
2641da177e4SLinus Torvalds 	/* Start reading from the device */
265f4a4cbb2SJohan Hovold 	return usb_serial_generic_open(tty, port);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
268f4a4cbb2SJohan Hovold static int ir_prepare_write_buffer(struct usb_serial_port *port,
269f4a4cbb2SJohan Hovold 						void *dest, size_t size)
2701da177e4SLinus Torvalds {
271f4a4cbb2SJohan Hovold 	unsigned char *buf = dest;
272e421fe97SJohan Hovold 	int count;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/*
2751da177e4SLinus Torvalds 	 * The first byte of the packet we send to the device contains an
276e0d795e4SFelipe Balbi 	 * inbound header which indicates an additional number of BOFs and
2771da177e4SLinus Torvalds 	 * a baud rate change.
2781da177e4SLinus Torvalds 	 *
2791da177e4SLinus Torvalds 	 * See section 5.4.2.2 of the USB IrDA spec.
2801da177e4SLinus Torvalds 	 */
281f4a4cbb2SJohan Hovold 	*buf = ir_xbof | ir_baud;
2821da177e4SLinus Torvalds 
283e421fe97SJohan Hovold 	count = kfifo_out_locked(&port->write_fifo, buf + 1, size - 1,
284f4a4cbb2SJohan Hovold 								&port->lock);
285e421fe97SJohan Hovold 	return count + 1;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
288f4a4cbb2SJohan Hovold static void ir_process_read_urb(struct urb *urb)
2891da177e4SLinus Torvalds {
290cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
2911da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
2921da177e4SLinus Torvalds 
293f4a4cbb2SJohan Hovold 	if (!urb->actual_length)
294f4a4cbb2SJohan Hovold 		return;
2951da177e4SLinus Torvalds 	/*
2961da177e4SLinus Torvalds 	 * The first byte of the packet we get from the device
2971da177e4SLinus Torvalds 	 * contains a busy indicator and baud rate change.
2981da177e4SLinus Torvalds 	 * See section 5.4.1.2 of the USB IrDA spec.
2991da177e4SLinus Torvalds 	 */
300f4a4cbb2SJohan Hovold 	if (*data & 0x0f)
3011da177e4SLinus Torvalds 		ir_baud = *data & 0x0f;
302f4a4cbb2SJohan Hovold 
303f4a4cbb2SJohan Hovold 	if (urb->actual_length == 1)
304f4a4cbb2SJohan Hovold 		return;
305f4a4cbb2SJohan Hovold 
30605c7cd39SJiri Slaby 	tty_insert_flip_string(&port->port, data + 1, urb->actual_length - 1);
3072e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->port);
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
310df66e8a2SJohan Hovold static void ir_set_termios_callback(struct urb *urb)
311df66e8a2SJohan Hovold {
312df66e8a2SJohan Hovold 	kfree(urb->transfer_buffer);
313df66e8a2SJohan Hovold 
314b9783555SGreg Kroah-Hartman 	if (urb->status)
3159f857ae9SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - non-zero urb status: %d\n",
3169f857ae9SGreg Kroah-Hartman 			__func__, urb->status);
317df66e8a2SJohan Hovold }
318df66e8a2SJohan Hovold 
31995da310eSAlan Cox static void ir_set_termios(struct tty_struct *tty,
32095da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
3211da177e4SLinus Torvalds {
322df66e8a2SJohan Hovold 	struct urb *urb;
3231da177e4SLinus Torvalds 	unsigned char *transfer_buffer;
3241da177e4SLinus Torvalds 	int result;
325a6ea438bSAlan Cox 	speed_t baud;
326a6ea438bSAlan Cox 	int ir_baud;
3271da177e4SLinus Torvalds 
32895da310eSAlan Cox 	baud = tty_get_baud_rate(tty);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	/*
3311da177e4SLinus Torvalds 	 * FIXME, we should compare the baud request against the
3321da177e4SLinus Torvalds 	 * capability stated in the IR header that we got in the
3331da177e4SLinus Torvalds 	 * startup function.
3341da177e4SLinus Torvalds 	 */
335a6ea438bSAlan Cox 
336a6ea438bSAlan Cox 	switch (baud) {
337e0d795e4SFelipe Balbi 	case 2400:
338*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_2400;
339e0d795e4SFelipe Balbi 		break;
340e0d795e4SFelipe Balbi 	case 9600:
341*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_9600;
342e0d795e4SFelipe Balbi 		break;
343e0d795e4SFelipe Balbi 	case 19200:
344*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_19200;
345e0d795e4SFelipe Balbi 		break;
346e0d795e4SFelipe Balbi 	case 38400:
347*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_38400;
348e0d795e4SFelipe Balbi 		break;
349e0d795e4SFelipe Balbi 	case 57600:
350*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_57600;
351e0d795e4SFelipe Balbi 		break;
352e0d795e4SFelipe Balbi 	case 115200:
353*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_115200;
354e0d795e4SFelipe Balbi 		break;
355e0d795e4SFelipe Balbi 	case 576000:
356*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_576000;
357e0d795e4SFelipe Balbi 		break;
358e0d795e4SFelipe Balbi 	case 1152000:
359*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_1152000;
360e0d795e4SFelipe Balbi 		break;
361e0d795e4SFelipe Balbi 	case 4000000:
362*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_4000000;
363a6ea438bSAlan Cox 		break;
3641da177e4SLinus Torvalds 	default:
365*17a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_9600;
366a6ea438bSAlan Cox 		baud = 9600;
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds 
369a6ea438bSAlan Cox 	if (xbof == -1)
3701da177e4SLinus Torvalds 		ir_xbof = ir_xbof_change(ir_add_bof);
371a6ea438bSAlan Cox 	else
3721da177e4SLinus Torvalds 		ir_xbof = ir_xbof_change(xbof) ;
3731da177e4SLinus Torvalds 
374560aac22SAlan Cox 	/* Only speed changes are supported */
375adc8d746SAlan Cox 	tty_termios_copy_hw(&tty->termios, old_termios);
37695da310eSAlan Cox 	tty_encode_baud_rate(tty, baud, baud);
377df66e8a2SJohan Hovold 
378df66e8a2SJohan Hovold 	/*
379df66e8a2SJohan Hovold 	 * send the baud change out on an "empty" data packet
380df66e8a2SJohan Hovold 	 */
381df66e8a2SJohan Hovold 	urb = usb_alloc_urb(0, GFP_KERNEL);
38210c642d0SJohan Hovold 	if (!urb)
383df66e8a2SJohan Hovold 		return;
38410c642d0SJohan Hovold 
385df66e8a2SJohan Hovold 	transfer_buffer = kmalloc(1, GFP_KERNEL);
38610c642d0SJohan Hovold 	if (!transfer_buffer)
387df66e8a2SJohan Hovold 		goto err_buf;
388df66e8a2SJohan Hovold 
389df66e8a2SJohan Hovold 	*transfer_buffer = ir_xbof | ir_baud;
390df66e8a2SJohan Hovold 
391df66e8a2SJohan Hovold 	usb_fill_bulk_urb(
392df66e8a2SJohan Hovold 		urb,
393df66e8a2SJohan Hovold 		port->serial->dev,
394df66e8a2SJohan Hovold 		usb_sndbulkpipe(port->serial->dev,
395df66e8a2SJohan Hovold 			port->bulk_out_endpointAddress),
396df66e8a2SJohan Hovold 		transfer_buffer,
397df66e8a2SJohan Hovold 		1,
398df66e8a2SJohan Hovold 		ir_set_termios_callback,
399df66e8a2SJohan Hovold 		port);
400df66e8a2SJohan Hovold 
401df66e8a2SJohan Hovold 	urb->transfer_flags = URB_ZERO_PACKET;
402df66e8a2SJohan Hovold 
403df66e8a2SJohan Hovold 	result = usb_submit_urb(urb, GFP_KERNEL);
404df66e8a2SJohan Hovold 	if (result) {
405df66e8a2SJohan Hovold 		dev_err(&port->dev, "%s - failed to submit urb: %d\n",
406df66e8a2SJohan Hovold 							__func__, result);
407df66e8a2SJohan Hovold 		goto err_subm;
408df66e8a2SJohan Hovold 	}
409df66e8a2SJohan Hovold 
410df66e8a2SJohan Hovold 	usb_free_urb(urb);
411df66e8a2SJohan Hovold 
412df66e8a2SJohan Hovold 	return;
413df66e8a2SJohan Hovold err_subm:
414df66e8a2SJohan Hovold 	kfree(transfer_buffer);
415df66e8a2SJohan Hovold err_buf:
416df66e8a2SJohan Hovold 	usb_free_urb(urb);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds static int __init ir_init(void)
4201da177e4SLinus Torvalds {
4216f6ed696SJohan Hovold 	if (buffer_size) {
4226f6ed696SJohan Hovold 		ir_device.bulk_in_size = buffer_size;
4236f6ed696SJohan Hovold 		ir_device.bulk_out_size = buffer_size;
4246f6ed696SJohan Hovold 	}
4256f6ed696SJohan Hovold 
4267663e308SGreg Kroah-Hartman 	return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ir_id_table);
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds static void __exit ir_exit(void)
4301da177e4SLinus Torvalds {
43168e24113SGreg Kroah-Hartman 	usb_serial_deregister_drivers(serial_drivers);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds module_init(ir_init);
4361da177e4SLinus Torvalds module_exit(ir_exit);
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
4391da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
4401da177e4SLinus Torvalds MODULE_LICENSE("GPL");
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds module_param(xbof, int, 0);
4431da177e4SLinus Torvalds MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
4441da177e4SLinus Torvalds module_param(buffer_size, int, 0);
4451da177e4SLinus Torvalds MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
4461da177e4SLinus Torvalds 
447