xref: /openbmc/linux/drivers/usb/serial/ir-usb.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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);
4838c0d5bdSJohan Hovold static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
4938c0d5bdSJohan Hovold 		const unsigned char *buf, int count);
5094cc7aeaSJiri Slaby static unsigned int ir_write_room(struct tty_struct *tty);
5138c0d5bdSJohan Hovold static void ir_write_bulk_callback(struct urb *urb);
52f4a4cbb2SJohan Hovold static void ir_process_read_urb(struct urb *urb);
5395da310eSAlan Cox static void ir_set_termios(struct tty_struct *tty,
54*f6d47fe5SIlpo Järvinen 			   struct usb_serial_port *port,
55*f6d47fe5SIlpo Järvinen 			   const struct ktermios *old_termios);
561da177e4SLinus Torvalds 
57a6ea438bSAlan Cox /* Not that this lot means you can only have one per system */
58e0d795e4SFelipe Balbi static u8 ir_baud;
59e0d795e4SFelipe Balbi static u8 ir_xbof;
60e0d795e4SFelipe Balbi static u8 ir_add_bof;
611da177e4SLinus Torvalds 
627d40d7e8SNémeth Márton static const struct usb_device_id ir_id_table[] = {
631da177e4SLinus Torvalds 	{ USB_DEVICE(0x050f, 0x0180) },		/* KC Technology, KC-180 */
641da177e4SLinus Torvalds 	{ USB_DEVICE(0x08e9, 0x0100) },		/* XTNDAccess */
651da177e4SLinus Torvalds 	{ USB_DEVICE(0x09c4, 0x0011) },		/* ACTiSys ACT-IR2000U */
66e0d795e4SFelipe Balbi 	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
671da177e4SLinus Torvalds 	{ }					/* Terminating entry */
681da177e4SLinus Torvalds };
691da177e4SLinus Torvalds 
70e0d795e4SFelipe Balbi MODULE_DEVICE_TABLE(usb, ir_id_table);
711da177e4SLinus Torvalds 
72ea65370dSGreg Kroah-Hartman static struct usb_serial_driver ir_device = {
7318fcac35SGreg Kroah-Hartman 	.driver	= {
741da177e4SLinus Torvalds 		.owner	= THIS_MODULE,
75269bda1cSGreg Kroah-Hartman 		.name	= "ir-usb",
7618fcac35SGreg Kroah-Hartman 	},
77269bda1cSGreg Kroah-Hartman 	.description		= "IR Dongle",
78e0d795e4SFelipe Balbi 	.id_table		= ir_id_table,
791da177e4SLinus Torvalds 	.num_ports		= 1,
80a1c91c10SJohan Hovold 	.num_bulk_in		= 1,
81a1c91c10SJohan Hovold 	.num_bulk_out		= 1,
821da177e4SLinus Torvalds 	.set_termios		= ir_set_termios,
831da177e4SLinus Torvalds 	.attach			= ir_startup,
8438c0d5bdSJohan Hovold 	.write			= ir_write,
8538c0d5bdSJohan Hovold 	.write_room		= ir_write_room,
8638c0d5bdSJohan Hovold 	.write_bulk_callback	= ir_write_bulk_callback,
87f4a4cbb2SJohan Hovold 	.process_read_urb	= ir_process_read_urb,
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds 
907dbe2460SAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
917dbe2460SAlan Stern 	&ir_device, NULL
927dbe2460SAlan Stern };
937dbe2460SAlan Stern 
irda_usb_dump_class_desc(struct usb_serial * serial,struct usb_irda_cs_descriptor * desc)949f857ae9SGreg Kroah-Hartman static inline void irda_usb_dump_class_desc(struct usb_serial *serial,
959f857ae9SGreg Kroah-Hartman 					    struct usb_irda_cs_descriptor *desc)
961da177e4SLinus Torvalds {
979f857ae9SGreg Kroah-Hartman 	struct device *dev = &serial->dev->dev;
989f857ae9SGreg Kroah-Hartman 
999f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bLength=%x\n", desc->bLength);
1009f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bDescriptorType=%x\n", desc->bDescriptorType);
1019f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bcdSpecRevision=%x\n", __le16_to_cpu(desc->bcdSpecRevision));
1029f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bmDataSize=%x\n", desc->bmDataSize);
1039f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bmWindowSize=%x\n", desc->bmWindowSize);
1049f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
1059f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "wBaudRate=%x\n", __le16_to_cpu(desc->wBaudRate));
1069f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
1079f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
1089f857ae9SGreg Kroah-Hartman 	dev_dbg(dev, "bMaxUnicastList=%x\n", desc->bMaxUnicastList);
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*------------------------------------------------------------------*/
1121da177e4SLinus Torvalds /*
1131da177e4SLinus Torvalds  * Function irda_usb_find_class_desc(dev, ifnum)
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  *    Returns instance of IrDA class descriptor, or NULL if not found
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  * The class descriptor is some extra info that IrDA USB devices will
1181da177e4SLinus Torvalds  * offer to us, describing their IrDA characteristics. We will use that in
1191da177e4SLinus Torvalds  * irda_usb_init_qos()
1201da177e4SLinus Torvalds  *
1211da177e4SLinus Torvalds  * Based on the same function in drivers/net/irda/irda-usb.c
1221da177e4SLinus Torvalds  */
123e0d795e4SFelipe Balbi static struct usb_irda_cs_descriptor *
irda_usb_find_class_desc(struct usb_serial * serial,unsigned int ifnum)1249f857ae9SGreg Kroah-Hartman irda_usb_find_class_desc(struct usb_serial *serial, unsigned int ifnum)
1251da177e4SLinus Torvalds {
1269f857ae9SGreg Kroah-Hartman 	struct usb_device *dev = serial->dev;
127e0d795e4SFelipe Balbi 	struct usb_irda_cs_descriptor *desc;
1281da177e4SLinus Torvalds 	int ret;
1291da177e4SLinus Torvalds 
130e0d795e4SFelipe Balbi 	desc = kzalloc(sizeof(*desc), GFP_KERNEL);
131e0d795e4SFelipe Balbi 	if (!desc)
1321da177e4SLinus Torvalds 		return NULL;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
135e0d795e4SFelipe Balbi 			USB_REQ_CS_IRDA_GET_CLASS_DESC,
1361da177e4SLinus Torvalds 			USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1371da177e4SLinus Torvalds 			0, ifnum, desc, sizeof(*desc), 1000);
1381da177e4SLinus Torvalds 
1399f857ae9SGreg Kroah-Hartman 	dev_dbg(&serial->dev->dev, "%s -  ret=%d\n", __func__, ret);
1403391ca1dSChengguang Xu 	if (ret < (int)sizeof(*desc)) {
1419f857ae9SGreg Kroah-Hartman 		dev_dbg(&serial->dev->dev,
1429f857ae9SGreg Kroah-Hartman 			"%s - class descriptor read %s (%d)\n", __func__,
1439f857ae9SGreg Kroah-Hartman 			(ret < 0) ? "failed" : "too short", ret);
1441da177e4SLinus Torvalds 		goto error;
1451da177e4SLinus Torvalds 	}
146e0d795e4SFelipe Balbi 	if (desc->bDescriptorType != USB_DT_CS_IRDA) {
1479f857ae9SGreg Kroah-Hartman 		dev_dbg(&serial->dev->dev, "%s - bad class descriptor type\n",
1489f857ae9SGreg Kroah-Hartman 			__func__);
1491da177e4SLinus Torvalds 		goto error;
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 
1529f857ae9SGreg Kroah-Hartman 	irda_usb_dump_class_desc(serial, desc);
1531da177e4SLinus Torvalds 	return desc;
154e0d795e4SFelipe Balbi 
1551da177e4SLinus Torvalds error:
1561da177e4SLinus Torvalds 	kfree(desc);
1571da177e4SLinus Torvalds 	return NULL;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
ir_xbof_change(u8 xbof)1601da177e4SLinus Torvalds static u8 ir_xbof_change(u8 xbof)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	u8 result;
163e0d795e4SFelipe Balbi 
1641da177e4SLinus Torvalds 	/* reference irda-usb.c */
1651da177e4SLinus Torvalds 	switch (xbof) {
166e0d795e4SFelipe Balbi 	case 48:
167e0d795e4SFelipe Balbi 		result = 0x10;
168e0d795e4SFelipe Balbi 		break;
1691da177e4SLinus Torvalds 	case 28:
170e0d795e4SFelipe Balbi 	case 24:
171e0d795e4SFelipe Balbi 		result = 0x20;
172e0d795e4SFelipe Balbi 		break;
1731da177e4SLinus Torvalds 	default:
174e0d795e4SFelipe Balbi 	case 12:
175e0d795e4SFelipe Balbi 		result = 0x30;
176e0d795e4SFelipe Balbi 		break;
1771da177e4SLinus Torvalds 	case  5:
178e0d795e4SFelipe Balbi 	case  6:
179e0d795e4SFelipe Balbi 		result = 0x40;
180e0d795e4SFelipe Balbi 		break;
181e0d795e4SFelipe Balbi 	case  3:
182e0d795e4SFelipe Balbi 		result = 0x50;
183e0d795e4SFelipe Balbi 		break;
184e0d795e4SFelipe Balbi 	case  2:
185e0d795e4SFelipe Balbi 		result = 0x60;
186e0d795e4SFelipe Balbi 		break;
187e0d795e4SFelipe Balbi 	case  1:
188e0d795e4SFelipe Balbi 		result = 0x70;
189e0d795e4SFelipe Balbi 		break;
190e0d795e4SFelipe Balbi 	case  0:
191e0d795e4SFelipe Balbi 		result = 0x80;
192e0d795e4SFelipe Balbi 		break;
1931da177e4SLinus Torvalds 	}
194e0d795e4SFelipe Balbi 
1951da177e4SLinus Torvalds 	return(result);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
ir_startup(struct usb_serial * serial)1981da177e4SLinus Torvalds static int ir_startup(struct usb_serial *serial)
1991da177e4SLinus Torvalds {
200e0d795e4SFelipe Balbi 	struct usb_irda_cs_descriptor *irda_desc;
201ad0ccac7SJohan Hovold 	int rates;
2021da177e4SLinus Torvalds 
2039f857ae9SGreg Kroah-Hartman 	irda_desc = irda_usb_find_class_desc(serial, 0);
204e0d795e4SFelipe Balbi 	if (!irda_desc) {
205e0d795e4SFelipe Balbi 		dev_err(&serial->dev->dev,
206e0d795e4SFelipe Balbi 			"IRDA class descriptor not found, device not bound\n");
2071da177e4SLinus Torvalds 		return -ENODEV;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
210ad0ccac7SJohan Hovold 	rates = le16_to_cpu(irda_desc->wBaudRate);
211ad0ccac7SJohan Hovold 
2129f857ae9SGreg Kroah-Hartman 	dev_dbg(&serial->dev->dev,
2139f857ae9SGreg Kroah-Hartman 		"%s - Baud rates supported:%s%s%s%s%s%s%s%s%s\n",
214441b62c1SHarvey Harrison 		__func__,
215ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_2400) ? " 2400" : "",
216ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_9600) ? " 9600" : "",
217ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_19200) ? " 19200" : "",
218ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_38400) ? " 38400" : "",
219ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_57600) ? " 57600" : "",
220ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_115200) ? " 115200" : "",
221ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_576000) ? " 576000" : "",
222ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_1152000) ? " 1152000" : "",
223ad0ccac7SJohan Hovold 		(rates & USB_IRDA_BR_4000000) ? " 4000000" : "");
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	switch (irda_desc->bmAdditionalBOFs) {
226e0d795e4SFelipe Balbi 	case USB_IRDA_AB_48:
227e0d795e4SFelipe Balbi 		ir_add_bof = 48;
228e0d795e4SFelipe Balbi 		break;
229e0d795e4SFelipe Balbi 	case USB_IRDA_AB_24:
230e0d795e4SFelipe Balbi 		ir_add_bof = 24;
231e0d795e4SFelipe Balbi 		break;
232e0d795e4SFelipe Balbi 	case USB_IRDA_AB_12:
233e0d795e4SFelipe Balbi 		ir_add_bof = 12;
234e0d795e4SFelipe Balbi 		break;
235e0d795e4SFelipe Balbi 	case USB_IRDA_AB_6:
236e0d795e4SFelipe Balbi 		ir_add_bof = 6;
237e0d795e4SFelipe Balbi 		break;
238e0d795e4SFelipe Balbi 	case USB_IRDA_AB_3:
239e0d795e4SFelipe Balbi 		ir_add_bof = 3;
240e0d795e4SFelipe Balbi 		break;
241e0d795e4SFelipe Balbi 	case USB_IRDA_AB_2:
242e0d795e4SFelipe Balbi 		ir_add_bof = 2;
243e0d795e4SFelipe Balbi 		break;
244e0d795e4SFelipe Balbi 	case USB_IRDA_AB_1:
245e0d795e4SFelipe Balbi 		ir_add_bof = 1;
246e0d795e4SFelipe Balbi 		break;
247e0d795e4SFelipe Balbi 	case USB_IRDA_AB_0:
248e0d795e4SFelipe Balbi 		ir_add_bof = 0;
249e0d795e4SFelipe Balbi 		break;
250e0d795e4SFelipe Balbi 	default:
251e0d795e4SFelipe Balbi 		break;
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	kfree(irda_desc);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	return 0;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
ir_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * buf,int count)25938c0d5bdSJohan Hovold static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
26038c0d5bdSJohan Hovold 		const unsigned char *buf, int count)
2611da177e4SLinus Torvalds {
26238c0d5bdSJohan Hovold 	struct urb *urb = NULL;
26338c0d5bdSJohan Hovold 	unsigned long flags;
26438c0d5bdSJohan Hovold 	int ret;
2651da177e4SLinus Torvalds 
26638c0d5bdSJohan Hovold 	if (port->bulk_out_size == 0)
26738c0d5bdSJohan Hovold 		return -EINVAL;
268f4a4cbb2SJohan Hovold 
26938c0d5bdSJohan Hovold 	if (count == 0)
27038c0d5bdSJohan Hovold 		return 0;
27138c0d5bdSJohan Hovold 
27238c0d5bdSJohan Hovold 	count = min(count, port->bulk_out_size - 1);
27338c0d5bdSJohan Hovold 
27438c0d5bdSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
27538c0d5bdSJohan Hovold 	if (__test_and_clear_bit(0, &port->write_urbs_free)) {
27638c0d5bdSJohan Hovold 		urb = port->write_urbs[0];
27738c0d5bdSJohan Hovold 		port->tx_bytes += count;
2781da177e4SLinus Torvalds 	}
27938c0d5bdSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
2801da177e4SLinus Torvalds 
28138c0d5bdSJohan Hovold 	if (!urb)
28238c0d5bdSJohan Hovold 		return 0;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/*
2851da177e4SLinus Torvalds 	 * The first byte of the packet we send to the device contains an
28638c0d5bdSJohan Hovold 	 * outbound header which indicates an additional number of BOFs and
2871da177e4SLinus Torvalds 	 * a baud rate change.
2881da177e4SLinus Torvalds 	 *
2891da177e4SLinus Torvalds 	 * See section 5.4.2.2 of the USB IrDA spec.
2901da177e4SLinus Torvalds 	 */
29138c0d5bdSJohan Hovold 	*(u8 *)urb->transfer_buffer = ir_xbof | ir_baud;
2921da177e4SLinus Torvalds 
29338c0d5bdSJohan Hovold 	memcpy(urb->transfer_buffer + 1, buf, count);
29438c0d5bdSJohan Hovold 
29538c0d5bdSJohan Hovold 	urb->transfer_buffer_length = count + 1;
29638c0d5bdSJohan Hovold 	urb->transfer_flags = URB_ZERO_PACKET;
29738c0d5bdSJohan Hovold 
29838c0d5bdSJohan Hovold 	ret = usb_submit_urb(urb, GFP_ATOMIC);
29938c0d5bdSJohan Hovold 	if (ret) {
30038c0d5bdSJohan Hovold 		dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
30138c0d5bdSJohan Hovold 
30238c0d5bdSJohan Hovold 		spin_lock_irqsave(&port->lock, flags);
30338c0d5bdSJohan Hovold 		__set_bit(0, &port->write_urbs_free);
30438c0d5bdSJohan Hovold 		port->tx_bytes -= count;
30538c0d5bdSJohan Hovold 		spin_unlock_irqrestore(&port->lock, flags);
30638c0d5bdSJohan Hovold 
30738c0d5bdSJohan Hovold 		return ret;
30838c0d5bdSJohan Hovold 	}
30938c0d5bdSJohan Hovold 
31038c0d5bdSJohan Hovold 	return count;
31138c0d5bdSJohan Hovold }
31238c0d5bdSJohan Hovold 
ir_write_bulk_callback(struct urb * urb)31338c0d5bdSJohan Hovold static void ir_write_bulk_callback(struct urb *urb)
31438c0d5bdSJohan Hovold {
31538c0d5bdSJohan Hovold 	struct usb_serial_port *port = urb->context;
31638c0d5bdSJohan Hovold 	int status = urb->status;
31738c0d5bdSJohan Hovold 	unsigned long flags;
31838c0d5bdSJohan Hovold 
31938c0d5bdSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
32038c0d5bdSJohan Hovold 	__set_bit(0, &port->write_urbs_free);
32138c0d5bdSJohan Hovold 	port->tx_bytes -= urb->transfer_buffer_length - 1;
32238c0d5bdSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
32338c0d5bdSJohan Hovold 
32438c0d5bdSJohan Hovold 	switch (status) {
32538c0d5bdSJohan Hovold 	case 0:
32638c0d5bdSJohan Hovold 		break;
32738c0d5bdSJohan Hovold 	case -ENOENT:
32838c0d5bdSJohan Hovold 	case -ECONNRESET:
32938c0d5bdSJohan Hovold 	case -ESHUTDOWN:
33038c0d5bdSJohan Hovold 		dev_dbg(&port->dev, "write urb stopped: %d\n", status);
33138c0d5bdSJohan Hovold 		return;
33238c0d5bdSJohan Hovold 	case -EPIPE:
33338c0d5bdSJohan Hovold 		dev_err(&port->dev, "write urb stopped: %d\n", status);
33438c0d5bdSJohan Hovold 		return;
33538c0d5bdSJohan Hovold 	default:
33638c0d5bdSJohan Hovold 		dev_err(&port->dev, "nonzero write-urb status: %d\n", status);
33738c0d5bdSJohan Hovold 		break;
33838c0d5bdSJohan Hovold 	}
33938c0d5bdSJohan Hovold 
34038c0d5bdSJohan Hovold 	usb_serial_port_softint(port);
34138c0d5bdSJohan Hovold }
34238c0d5bdSJohan Hovold 
ir_write_room(struct tty_struct * tty)34394cc7aeaSJiri Slaby static unsigned int ir_write_room(struct tty_struct *tty)
34438c0d5bdSJohan Hovold {
34538c0d5bdSJohan Hovold 	struct usb_serial_port *port = tty->driver_data;
34694cc7aeaSJiri Slaby 	unsigned int count = 0;
34738c0d5bdSJohan Hovold 
34838c0d5bdSJohan Hovold 	if (port->bulk_out_size == 0)
34938c0d5bdSJohan Hovold 		return 0;
35038c0d5bdSJohan Hovold 
35138c0d5bdSJohan Hovold 	if (test_bit(0, &port->write_urbs_free))
35238c0d5bdSJohan Hovold 		count = port->bulk_out_size - 1;
35338c0d5bdSJohan Hovold 
35438c0d5bdSJohan Hovold 	return count;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
ir_process_read_urb(struct urb * urb)357f4a4cbb2SJohan Hovold static void ir_process_read_urb(struct urb *urb)
3581da177e4SLinus Torvalds {
359cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
3601da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
3611da177e4SLinus Torvalds 
362f4a4cbb2SJohan Hovold 	if (!urb->actual_length)
363f4a4cbb2SJohan Hovold 		return;
3641da177e4SLinus Torvalds 	/*
3651da177e4SLinus Torvalds 	 * The first byte of the packet we get from the device
3661da177e4SLinus Torvalds 	 * contains a busy indicator and baud rate change.
3671da177e4SLinus Torvalds 	 * See section 5.4.1.2 of the USB IrDA spec.
3681da177e4SLinus Torvalds 	 */
369f4a4cbb2SJohan Hovold 	if (*data & 0x0f)
3701da177e4SLinus Torvalds 		ir_baud = *data & 0x0f;
371f4a4cbb2SJohan Hovold 
372f4a4cbb2SJohan Hovold 	if (urb->actual_length == 1)
373f4a4cbb2SJohan Hovold 		return;
374f4a4cbb2SJohan Hovold 
37505c7cd39SJiri Slaby 	tty_insert_flip_string(&port->port, data + 1, urb->actual_length - 1);
3762e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->port);
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
ir_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)37995da310eSAlan Cox static void ir_set_termios(struct tty_struct *tty,
380*f6d47fe5SIlpo Järvinen 			   struct usb_serial_port *port,
381*f6d47fe5SIlpo Järvinen 			   const struct ktermios *old_termios)
3821da177e4SLinus Torvalds {
383e7542bc3SJohan Hovold 	struct usb_device *udev = port->serial->dev;
3841da177e4SLinus Torvalds 	unsigned char *transfer_buffer;
385e7542bc3SJohan Hovold 	int actual_length;
386a6ea438bSAlan Cox 	speed_t baud;
387a6ea438bSAlan Cox 	int ir_baud;
388e7542bc3SJohan Hovold 	int ret;
3891da177e4SLinus Torvalds 
39095da310eSAlan Cox 	baud = tty_get_baud_rate(tty);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/*
3931da177e4SLinus Torvalds 	 * FIXME, we should compare the baud request against the
3941da177e4SLinus Torvalds 	 * capability stated in the IR header that we got in the
3951da177e4SLinus Torvalds 	 * startup function.
3961da177e4SLinus Torvalds 	 */
397a6ea438bSAlan Cox 
398a6ea438bSAlan Cox 	switch (baud) {
399e0d795e4SFelipe Balbi 	case 2400:
40017a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_2400;
401e0d795e4SFelipe Balbi 		break;
402e0d795e4SFelipe Balbi 	case 9600:
40317a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_9600;
404e0d795e4SFelipe Balbi 		break;
405e0d795e4SFelipe Balbi 	case 19200:
40617a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_19200;
407e0d795e4SFelipe Balbi 		break;
408e0d795e4SFelipe Balbi 	case 38400:
40917a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_38400;
410e0d795e4SFelipe Balbi 		break;
411e0d795e4SFelipe Balbi 	case 57600:
41217a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_57600;
413e0d795e4SFelipe Balbi 		break;
414e0d795e4SFelipe Balbi 	case 115200:
41517a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_115200;
416e0d795e4SFelipe Balbi 		break;
417e0d795e4SFelipe Balbi 	case 576000:
41817a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_576000;
419e0d795e4SFelipe Balbi 		break;
420e0d795e4SFelipe Balbi 	case 1152000:
42117a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_1152000;
422e0d795e4SFelipe Balbi 		break;
423e0d795e4SFelipe Balbi 	case 4000000:
42417a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_4000000;
425a6ea438bSAlan Cox 		break;
4261da177e4SLinus Torvalds 	default:
42717a0184cSJohan Hovold 		ir_baud = USB_IRDA_LS_9600;
428a6ea438bSAlan Cox 		baud = 9600;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 
431a6ea438bSAlan Cox 	if (xbof == -1)
4321da177e4SLinus Torvalds 		ir_xbof = ir_xbof_change(ir_add_bof);
433a6ea438bSAlan Cox 	else
4341da177e4SLinus Torvalds 		ir_xbof = ir_xbof_change(xbof) ;
4351da177e4SLinus Torvalds 
436560aac22SAlan Cox 	/* Only speed changes are supported */
437adc8d746SAlan Cox 	tty_termios_copy_hw(&tty->termios, old_termios);
43895da310eSAlan Cox 	tty_encode_baud_rate(tty, baud, baud);
439df66e8a2SJohan Hovold 
440df66e8a2SJohan Hovold 	/*
441df66e8a2SJohan Hovold 	 * send the baud change out on an "empty" data packet
442df66e8a2SJohan Hovold 	 */
443df66e8a2SJohan Hovold 	transfer_buffer = kmalloc(1, GFP_KERNEL);
44410c642d0SJohan Hovold 	if (!transfer_buffer)
445e7542bc3SJohan Hovold 		return;
446df66e8a2SJohan Hovold 
447df66e8a2SJohan Hovold 	*transfer_buffer = ir_xbof | ir_baud;
448df66e8a2SJohan Hovold 
449e7542bc3SJohan Hovold 	ret = usb_bulk_msg(udev,
450e7542bc3SJohan Hovold 			usb_sndbulkpipe(udev, port->bulk_out_endpointAddress),
451e7542bc3SJohan Hovold 			transfer_buffer, 1, &actual_length, 5000);
452e7542bc3SJohan Hovold 	if (ret || actual_length != 1) {
453c664a4faSDan Carpenter 		if (!ret)
454e7542bc3SJohan Hovold 			ret = -EIO;
455e7542bc3SJohan Hovold 		dev_err(&port->dev, "failed to change line speed: %d\n", ret);
456df66e8a2SJohan Hovold 	}
457df66e8a2SJohan Hovold 
458df66e8a2SJohan Hovold 	kfree(transfer_buffer);
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
ir_init(void)4611da177e4SLinus Torvalds static int __init ir_init(void)
4621da177e4SLinus Torvalds {
4636f6ed696SJohan Hovold 	if (buffer_size) {
4646f6ed696SJohan Hovold 		ir_device.bulk_in_size = buffer_size;
4656f6ed696SJohan Hovold 		ir_device.bulk_out_size = buffer_size;
4666f6ed696SJohan Hovold 	}
4676f6ed696SJohan Hovold 
4687663e308SGreg Kroah-Hartman 	return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ir_id_table);
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
ir_exit(void)4711da177e4SLinus Torvalds static void __exit ir_exit(void)
4721da177e4SLinus Torvalds {
47368e24113SGreg Kroah-Hartman 	usb_serial_deregister_drivers(serial_drivers);
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds module_init(ir_init);
4781da177e4SLinus Torvalds module_exit(ir_exit);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
4811da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
4821da177e4SLinus Torvalds MODULE_LICENSE("GPL");
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds module_param(xbof, int, 0);
4851da177e4SLinus Torvalds MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
4861da177e4SLinus Torvalds module_param(buffer_size, int, 0);
4871da177e4SLinus Torvalds MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
4881da177e4SLinus Torvalds 
489