blob: d8490eaeb234f61cd85af78f7fca4cc3c5c42b06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Felipe Balbie0d795e2008-06-03 14:47:52 +030022 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Alan Cox8eb04cf2008-11-11 14:48:44 +000029 * 2007_Jun_21 Alan Cox <alan@lxorguk.ukuu.org.uk>
Alan Coxa6ea4382007-06-22 14:44:54 +010030 * Minimal cleanups for some of the driver problens and tty layer abuse.
31 * Still needs fixing to allow multiple dongles.
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#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>
Felipe Balbie0d795e2008-06-03 14:47:52 +030067#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070069#include <linux/usb/serial.h>
Felipe Balbie0d795e2008-06-03 14:47:52 +030070#include <linux/usb/irda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int debug;
80
81/* if overridden by the user, then use their value for the size of the read and
82 * write urbs */
83static int buffer_size;
Felipe Balbie0d795e2008-06-03 14:47:52 +030084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* if overridden by the user, then use the specified number of XBOFs */
86static int xbof = -1;
87
Alan Cox95da3102008-07-22 11:09:07 +010088static int ir_startup (struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -070089static int ir_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010090static void ir_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010091static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
92 const unsigned char *buf, int count);
93static void ir_write_bulk_callback (struct urb *urb);
94static void ir_read_bulk_callback (struct urb *urb);
95static void ir_set_termios(struct tty_struct *tty,
96 struct usb_serial_port *port, struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Alan Coxa6ea4382007-06-22 14:44:54 +010098/* Not that this lot means you can only have one per system */
Felipe Balbie0d795e2008-06-03 14:47:52 +030099static u8 ir_baud;
100static u8 ir_xbof;
101static u8 ir_add_bof;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Németh Márton7d40d7e2010-01-10 15:34:24 +0100103static const struct usb_device_id ir_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
105 { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
106 { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300107 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 { } /* Terminating entry */
109};
110
Felipe Balbie0d795e2008-06-03 14:47:52 +0300111MODULE_DEVICE_TABLE(usb, ir_id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct usb_driver ir_driver = {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300114 .name = "ir-usb",
115 .probe = usb_serial_probe,
116 .disconnect = usb_serial_disconnect,
117 .id_table = ir_id_table,
118 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700121static struct usb_serial_driver ir_device = {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300122 .driver = {
123 .owner = THIS_MODULE,
124 .name = "ir-usb",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700125 },
Felipe Balbie0d795e2008-06-03 14:47:52 +0300126 .description = "IR Dongle",
127 .usb_driver = &ir_driver,
128 .id_table = ir_id_table,
129 .num_ports = 1,
130 .set_termios = ir_set_termios,
131 .attach = ir_startup,
132 .open = ir_open,
133 .close = ir_close,
134 .write = ir_write,
135 .write_bulk_callback = ir_write_bulk_callback,
136 .read_bulk_callback = ir_read_bulk_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Felipe Balbie0d795e2008-06-03 14:47:52 +0300139static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 dbg("bLength=%x", desc->bLength);
142 dbg("bDescriptorType=%x", desc->bDescriptorType);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300143 dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dbg("bmDataSize=%x", desc->bmDataSize);
145 dbg("bmWindowSize=%x", desc->bmWindowSize);
146 dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300147 dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
149 dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
150 dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
151}
152
153/*------------------------------------------------------------------*/
154/*
155 * Function irda_usb_find_class_desc(dev, ifnum)
156 *
157 * Returns instance of IrDA class descriptor, or NULL if not found
158 *
159 * The class descriptor is some extra info that IrDA USB devices will
160 * offer to us, describing their IrDA characteristics. We will use that in
161 * irda_usb_init_qos()
162 *
163 * Based on the same function in drivers/net/irda/irda-usb.c
164 */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300165static struct usb_irda_cs_descriptor *
166irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Felipe Balbie0d795e2008-06-03 14:47:52 +0300168 struct usb_irda_cs_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 int ret;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300170
171 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
172 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return NULL;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300174
175 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
176 USB_REQ_CS_IRDA_GET_CLASS_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
178 0, ifnum, desc, sizeof(*desc), 1000);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300179
Harvey Harrison441b62c2008-03-03 16:08:34 -0800180 dbg("%s - ret=%d", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (ret < sizeof(*desc)) {
182 dbg("%s - class descriptor read %s (%d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800183 __func__,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300184 (ret < 0) ? "failed" : "too short",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ret);
186 goto error;
187 }
Felipe Balbie0d795e2008-06-03 14:47:52 +0300188 if (desc->bDescriptorType != USB_DT_CS_IRDA) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800189 dbg("%s - bad class descriptor type", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto error;
191 }
Felipe Balbie0d795e2008-06-03 14:47:52 +0300192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 irda_usb_dump_class_desc(desc);
194 return desc;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196error:
197 kfree(desc);
198 return NULL;
199}
200
201
202static u8 ir_xbof_change(u8 xbof)
203{
204 u8 result;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* reference irda-usb.c */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300207 switch (xbof) {
208 case 48:
209 result = 0x10;
210 break;
211 case 28:
212 case 24:
213 result = 0x20;
214 break;
215 default:
216 case 12:
217 result = 0x30;
218 break;
219 case 5:
220 case 6:
221 result = 0x40;
222 break;
223 case 3:
224 result = 0x50;
225 break;
226 case 2:
227 result = 0x60;
228 break;
229 case 1:
230 result = 0x70;
231 break;
232 case 0:
233 result = 0x80;
234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Felipe Balbie0d795e2008-06-03 14:47:52 +0300236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return(result);
238}
239
240
Felipe Balbie0d795e2008-06-03 14:47:52 +0300241static int ir_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Felipe Balbie0d795e2008-06-03 14:47:52 +0300243 struct usb_irda_cs_descriptor *irda_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Felipe Balbie0d795e2008-06-03 14:47:52 +0300245 irda_desc = irda_usb_find_class_desc(serial->dev, 0);
246 if (!irda_desc) {
247 dev_err(&serial->dev->dev,
248 "IRDA class descriptor not found, device not bound\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -ENODEV;
250 }
251
Felipe Balbie0d795e2008-06-03 14:47:52 +0300252 dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800253 __func__,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300254 (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
255 (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
256 (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
257 (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
258 (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
259 (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
260 (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
261 (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
262 (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Felipe Balbie0d795e2008-06-03 14:47:52 +0300264 switch (irda_desc->bmAdditionalBOFs) {
265 case USB_IRDA_AB_48:
266 ir_add_bof = 48;
267 break;
268 case USB_IRDA_AB_24:
269 ir_add_bof = 24;
270 break;
271 case USB_IRDA_AB_12:
272 ir_add_bof = 12;
273 break;
274 case USB_IRDA_AB_6:
275 ir_add_bof = 6;
276 break;
277 case USB_IRDA_AB_3:
278 ir_add_bof = 3;
279 break;
280 case USB_IRDA_AB_2:
281 ir_add_bof = 2;
282 break;
283 case USB_IRDA_AB_1:
284 ir_add_bof = 1;
285 break;
286 case USB_IRDA_AB_0:
287 ir_add_bof = 0;
288 break;
289 default:
290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Felipe Balbie0d795e2008-06-03 14:47:52 +0300293 kfree(irda_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Felipe Balbie0d795e2008-06-03 14:47:52 +0300295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Alan Coxa509a7e2009-09-19 13:13:26 -0700298static int ir_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int result = 0;
301
Harvey Harrison441b62c2008-03-03 16:08:34 -0800302 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Start reading from the device */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300305 usb_fill_bulk_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 port->read_urb,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300307 port->serial->dev,
308 usb_rcvbulkpipe(port->serial->dev,
309 port->bulk_in_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 port->read_urb->transfer_buffer,
311 port->read_urb->transfer_buffer_length,
312 ir_read_bulk_callback,
313 port);
314 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
315 if (result)
Felipe Balbie0d795e2008-06-03 14:47:52 +0300316 dev_err(&port->dev,
317 "%s - failed submitting read urb, error %d\n",
318 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 return result;
321}
322
Alan Cox335f8512009-06-11 12:26:29 +0100323static void ir_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800325 dbg("%s - port %d", __func__, port->number);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* shutdown our bulk read */
328 usb_kill_urb(port->read_urb);
329}
330
Alan Cox95da3102008-07-22 11:09:07 +0100331static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
332 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 unsigned char *transfer_buffer;
335 int result;
336 int transfer_size;
337
Harvey Harrison441b62c2008-03-03 16:08:34 -0800338 dbg("%s - port = %d, count = %d", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (count == 0)
341 return 0;
342
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200343 spin_lock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700344 if (port->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200345 spin_unlock_bh(&port->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800346 dbg("%s - already writing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700349 port->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200350 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 transfer_buffer = port->write_urb->transfer_buffer;
353 transfer_size = min(count, port->bulk_out_size - 1);
354
355 /*
356 * The first byte of the packet we send to the device contains an
Felipe Balbie0d795e2008-06-03 14:47:52 +0300357 * inbound header which indicates an additional number of BOFs and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * a baud rate change.
359 *
360 * See section 5.4.2.2 of the USB IrDA spec.
361 */
362 *transfer_buffer = ir_xbof | ir_baud;
363 ++transfer_buffer;
364
Felipe Balbie0d795e2008-06-03 14:47:52 +0300365 memcpy(transfer_buffer, buf, transfer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Felipe Balbie0d795e2008-06-03 14:47:52 +0300367 usb_fill_bulk_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 port->write_urb,
369 port->serial->dev,
370 usb_sndbulkpipe(port->serial->dev,
371 port->bulk_out_endpointAddress),
372 port->write_urb->transfer_buffer,
373 transfer_size + 1,
374 ir_write_bulk_callback,
375 port);
376
377 port->write_urb->transfer_flags = URB_ZERO_PACKET;
378
Felipe Balbie0d795e2008-06-03 14:47:52 +0300379 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700380 if (result) {
381 port->write_urb_busy = 0;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300382 dev_err(&port->dev,
383 "%s - failed submitting write urb, error %d\n",
384 __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700385 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 result = transfer_size;
387
388 return result;
389}
390
Felipe Balbie0d795e2008-06-03 14:47:52 +0300391static void ir_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Ming Leicdc97792008-02-24 18:41:47 +0800393 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartman3152b742007-06-15 15:44:13 -0700394 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Harvey Harrison441b62c2008-03-03 16:08:34 -0800396 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700397
398 port->write_urb_busy = 0;
Greg Kroah-Hartman3152b742007-06-15 15:44:13 -0700399 if (status) {
400 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800401 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return;
403 }
404
Felipe Balbie0d795e2008-06-03 14:47:52 +0300405 usb_serial_debug_data(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 debug,
407 &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800408 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 urb->actual_length,
410 urb->transfer_buffer);
411
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700412 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Felipe Balbie0d795e2008-06-03 14:47:52 +0300415static void ir_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Ming Leicdc97792008-02-24 18:41:47 +0800417 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct tty_struct *tty;
419 unsigned char *data = urb->transfer_buffer;
420 int result;
Greg Kroah-Hartman3152b742007-06-15 15:44:13 -0700421 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Harvey Harrison441b62c2008-03-03 16:08:34 -0800423 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Greg Kroah-Hartman3152b742007-06-15 15:44:13 -0700425 switch (status) {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300426 case 0: /* Successful */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300427 /*
428 * The first byte of the packet we get from the device
429 * contains a busy indicator and baud rate change.
430 * See section 5.4.1.2 of the USB IrDA spec.
431 */
432 if ((*data & 0x0f) > 0)
433 ir_baud = *data & 0x0f;
Alan Cox95da3102008-07-22 11:09:07 +0100434 usb_serial_debug_data(debug, &port->dev, __func__,
435 urb->actual_length, data);
Alan Cox4a90f092008-10-13 10:39:46 +0100436 tty = tty_port_tty_get(&port->port);
Alan Coxa108bfc2010-02-18 16:44:01 +0000437 tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
438 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100439 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Felipe Balbie0d795e2008-06-03 14:47:52 +0300441 /*
442 * No break here.
443 * We want to resubmit the urb so we can read
444 * again.
445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Felipe Balbie0d795e2008-06-03 14:47:52 +0300447 case -EPROTO: /* taking inspiration from pl2303.c */
Alan Cox95da3102008-07-22 11:09:07 +0100448 /* Continue trying to always read */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300449 usb_fill_bulk_urb(
450 port->read_urb,
Alan Cox95da3102008-07-22 11:09:07 +0100451 port->serial->dev,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300452 usb_rcvbulkpipe(port->serial->dev,
453 port->bulk_in_endpointAddress),
454 port->read_urb->transfer_buffer,
455 port->read_urb->transfer_buffer_length,
456 ir_read_bulk_callback,
457 port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Felipe Balbie0d795e2008-06-03 14:47:52 +0300459 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
460 if (result)
Alan Cox95da3102008-07-22 11:09:07 +0100461 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
Felipe Balbie0d795e2008-06-03 14:47:52 +0300462 __func__, result);
Alan Cox95da3102008-07-22 11:09:07 +0100463 break ;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300464 default:
465 dbg("%s - nonzero read bulk status received: %d",
Alan Cox95da3102008-07-22 11:09:07 +0100466 __func__, status);
467 break ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return;
470}
471
Johan Hovolddf66e8a2010-05-13 21:02:02 +0200472static void ir_set_termios_callback(struct urb *urb)
473{
474 struct usb_serial_port *port = urb->context;
475 int status = urb->status;
476
477 dbg("%s - port %d", __func__, port->number);
478
479 kfree(urb->transfer_buffer);
480
481 if (status)
482 dbg("%s - non-zero urb status: %d", __func__, status);
483}
484
Alan Cox95da3102008-07-22 11:09:07 +0100485static void ir_set_termios(struct tty_struct *tty,
486 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Johan Hovolddf66e8a2010-05-13 21:02:02 +0200488 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned char *transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int result;
Alan Coxa6ea4382007-06-22 14:44:54 +0100491 speed_t baud;
492 int ir_baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Harvey Harrison441b62c2008-03-03 16:08:34 -0800494 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Alan Cox95da3102008-07-22 11:09:07 +0100496 baud = tty_get_baud_rate(tty);
Alan Coxa6ea4382007-06-22 14:44:54 +0100497
498 /*
499 * FIXME, we should compare the baud request against the
500 * capability stated in the IR header that we got in the
501 * startup function.
502 */
503
504 switch (baud) {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300505 case 2400:
506 ir_baud = USB_IRDA_BR_2400;
507 break;
508 case 9600:
509 ir_baud = USB_IRDA_BR_9600;
510 break;
511 case 19200:
512 ir_baud = USB_IRDA_BR_19200;
513 break;
514 case 38400:
515 ir_baud = USB_IRDA_BR_38400;
516 break;
517 case 57600:
518 ir_baud = USB_IRDA_BR_57600;
519 break;
520 case 115200:
521 ir_baud = USB_IRDA_BR_115200;
522 break;
523 case 576000:
524 ir_baud = USB_IRDA_BR_576000;
525 break;
526 case 1152000:
527 ir_baud = USB_IRDA_BR_1152000;
528 break;
529 case 4000000:
530 ir_baud = USB_IRDA_BR_4000000;
531 break;
532 default:
533 ir_baud = USB_IRDA_BR_9600;
534 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
Alan Coxa6ea4382007-06-22 14:44:54 +0100537 if (xbof == -1)
538 ir_xbof = ir_xbof_change(ir_add_bof);
539 else
540 ir_xbof = ir_xbof_change(xbof) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Alan Cox560aac22007-10-18 01:24:20 -0700542 /* Only speed changes are supported */
Alan Cox95da3102008-07-22 11:09:07 +0100543 tty_termios_copy_hw(tty->termios, old_termios);
544 tty_encode_baud_rate(tty, baud, baud);
Johan Hovolddf66e8a2010-05-13 21:02:02 +0200545
546 /*
547 * send the baud change out on an "empty" data packet
548 */
549 urb = usb_alloc_urb(0, GFP_KERNEL);
550 if (!urb) {
551 dev_err(&port->dev, "%s - no more urbs\n", __func__);
552 return;
553 }
554 transfer_buffer = kmalloc(1, GFP_KERNEL);
555 if (!transfer_buffer) {
556 dev_err(&port->dev, "%s - out of memory\n", __func__);
557 goto err_buf;
558 }
559
560 *transfer_buffer = ir_xbof | ir_baud;
561
562 usb_fill_bulk_urb(
563 urb,
564 port->serial->dev,
565 usb_sndbulkpipe(port->serial->dev,
566 port->bulk_out_endpointAddress),
567 transfer_buffer,
568 1,
569 ir_set_termios_callback,
570 port);
571
572 urb->transfer_flags = URB_ZERO_PACKET;
573
574 result = usb_submit_urb(urb, GFP_KERNEL);
575 if (result) {
576 dev_err(&port->dev, "%s - failed to submit urb: %d\n",
577 __func__, result);
578 goto err_subm;
579 }
580
581 usb_free_urb(urb);
582
583 return;
584err_subm:
585 kfree(transfer_buffer);
586err_buf:
587 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Felipe Balbie0d795e2008-06-03 14:47:52 +0300590static int __init ir_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 int retval;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300593
Johan Hovold6f6ed692010-05-13 21:02:01 +0200594 if (buffer_size) {
595 ir_device.bulk_in_size = buffer_size;
596 ir_device.bulk_out_size = buffer_size;
597 }
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 retval = usb_serial_register(&ir_device);
600 if (retval)
601 goto failed_usb_serial_register;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 retval = usb_register(&ir_driver);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300604 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto failed_usb_register;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300606
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700607 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
608 DRIVER_DESC "\n");
Felipe Balbie0d795e2008-06-03 14:47:52 +0300609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return 0;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612failed_usb_register:
613 usb_serial_deregister(&ir_device);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615failed_usb_serial_register:
616 return retval;
617}
618
Felipe Balbie0d795e2008-06-03 14:47:52 +0300619static void __exit ir_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Felipe Balbie0d795e2008-06-03 14:47:52 +0300621 usb_deregister(&ir_driver);
622 usb_serial_deregister(&ir_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625
626module_init(ir_init);
627module_exit(ir_exit);
628
629MODULE_AUTHOR(DRIVER_AUTHOR);
630MODULE_DESCRIPTION(DRIVER_DESC);
631MODULE_LICENSE("GPL");
632
633module_param(debug, bool, S_IRUGO | S_IWUSR);
634MODULE_PARM_DESC(debug, "Debug enabled or not");
635module_param(xbof, int, 0);
636MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
637module_param(buffer_size, int, 0);
638MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
639