blob: 4e2cda93da596f7d5b98db2ae9a60b001418b392 [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);
89static int ir_open(struct tty_struct *tty, struct usb_serial_port *port,
90 struct file *filep);
91static void ir_close(struct tty_struct *tty, struct usb_serial_port *port,
92 struct file *filep);
93static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
94 const unsigned char *buf, int count);
95static void ir_write_bulk_callback (struct urb *urb);
96static void ir_read_bulk_callback (struct urb *urb);
97static void ir_set_termios(struct tty_struct *tty,
98 struct usb_serial_port *port, struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Alan Coxa6ea4382007-06-22 14:44:54 +0100100/* Not that this lot means you can only have one per system */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300101static u8 ir_baud;
102static u8 ir_xbof;
103static u8 ir_add_bof;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Felipe Balbie0d795e2008-06-03 14:47:52 +0300105static struct usb_device_id ir_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */
107 { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */
108 { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300109 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 { } /* Terminating entry */
111};
112
Felipe Balbie0d795e2008-06-03 14:47:52 +0300113MODULE_DEVICE_TABLE(usb, ir_id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static struct usb_driver ir_driver = {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300116 .name = "ir-usb",
117 .probe = usb_serial_probe,
118 .disconnect = usb_serial_disconnect,
119 .id_table = ir_id_table,
120 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700123static struct usb_serial_driver ir_device = {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300124 .driver = {
125 .owner = THIS_MODULE,
126 .name = "ir-usb",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700127 },
Felipe Balbie0d795e2008-06-03 14:47:52 +0300128 .description = "IR Dongle",
129 .usb_driver = &ir_driver,
130 .id_table = ir_id_table,
131 .num_ports = 1,
132 .set_termios = ir_set_termios,
133 .attach = ir_startup,
134 .open = ir_open,
135 .close = ir_close,
136 .write = ir_write,
137 .write_bulk_callback = ir_write_bulk_callback,
138 .read_bulk_callback = ir_read_bulk_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Felipe Balbie0d795e2008-06-03 14:47:52 +0300141static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 dbg("bLength=%x", desc->bLength);
144 dbg("bDescriptorType=%x", desc->bDescriptorType);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300145 dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 dbg("bmDataSize=%x", desc->bmDataSize);
147 dbg("bmWindowSize=%x", desc->bmWindowSize);
148 dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300149 dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
151 dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
152 dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
153}
154
155/*------------------------------------------------------------------*/
156/*
157 * Function irda_usb_find_class_desc(dev, ifnum)
158 *
159 * Returns instance of IrDA class descriptor, or NULL if not found
160 *
161 * The class descriptor is some extra info that IrDA USB devices will
162 * offer to us, describing their IrDA characteristics. We will use that in
163 * irda_usb_init_qos()
164 *
165 * Based on the same function in drivers/net/irda/irda-usb.c
166 */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300167static struct usb_irda_cs_descriptor *
168irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Felipe Balbie0d795e2008-06-03 14:47:52 +0300170 struct usb_irda_cs_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int ret;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300172
173 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
174 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return NULL;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300176
177 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
178 USB_REQ_CS_IRDA_GET_CLASS_DESC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
180 0, ifnum, desc, sizeof(*desc), 1000);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300181
Harvey Harrison441b62c2008-03-03 16:08:34 -0800182 dbg("%s - ret=%d", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (ret < sizeof(*desc)) {
184 dbg("%s - class descriptor read %s (%d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800185 __func__,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300186 (ret < 0) ? "failed" : "too short",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ret);
188 goto error;
189 }
Felipe Balbie0d795e2008-06-03 14:47:52 +0300190 if (desc->bDescriptorType != USB_DT_CS_IRDA) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800191 dbg("%s - bad class descriptor type", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto error;
193 }
Felipe Balbie0d795e2008-06-03 14:47:52 +0300194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 irda_usb_dump_class_desc(desc);
196 return desc;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198error:
199 kfree(desc);
200 return NULL;
201}
202
203
204static u8 ir_xbof_change(u8 xbof)
205{
206 u8 result;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* reference irda-usb.c */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300209 switch (xbof) {
210 case 48:
211 result = 0x10;
212 break;
213 case 28:
214 case 24:
215 result = 0x20;
216 break;
217 default:
218 case 12:
219 result = 0x30;
220 break;
221 case 5:
222 case 6:
223 result = 0x40;
224 break;
225 case 3:
226 result = 0x50;
227 break;
228 case 2:
229 result = 0x60;
230 break;
231 case 1:
232 result = 0x70;
233 break;
234 case 0:
235 result = 0x80;
236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Felipe Balbie0d795e2008-06-03 14:47:52 +0300238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return(result);
240}
241
242
Felipe Balbie0d795e2008-06-03 14:47:52 +0300243static int ir_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Felipe Balbie0d795e2008-06-03 14:47:52 +0300245 struct usb_irda_cs_descriptor *irda_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Felipe Balbie0d795e2008-06-03 14:47:52 +0300247 irda_desc = irda_usb_find_class_desc(serial->dev, 0);
248 if (!irda_desc) {
249 dev_err(&serial->dev->dev,
250 "IRDA class descriptor not found, device not bound\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -ENODEV;
252 }
253
Felipe Balbie0d795e2008-06-03 14:47:52 +0300254 dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800255 __func__,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300256 (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
257 (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
258 (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
259 (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
260 (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
261 (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
262 (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
263 (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
264 (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Felipe Balbie0d795e2008-06-03 14:47:52 +0300266 switch (irda_desc->bmAdditionalBOFs) {
267 case USB_IRDA_AB_48:
268 ir_add_bof = 48;
269 break;
270 case USB_IRDA_AB_24:
271 ir_add_bof = 24;
272 break;
273 case USB_IRDA_AB_12:
274 ir_add_bof = 12;
275 break;
276 case USB_IRDA_AB_6:
277 ir_add_bof = 6;
278 break;
279 case USB_IRDA_AB_3:
280 ir_add_bof = 3;
281 break;
282 case USB_IRDA_AB_2:
283 ir_add_bof = 2;
284 break;
285 case USB_IRDA_AB_1:
286 ir_add_bof = 1;
287 break;
288 case USB_IRDA_AB_0:
289 ir_add_bof = 0;
290 break;
291 default:
292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Felipe Balbie0d795e2008-06-03 14:47:52 +0300295 kfree(irda_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Felipe Balbie0d795e2008-06-03 14:47:52 +0300297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Alan Cox95da3102008-07-22 11:09:07 +0100300static int ir_open(struct tty_struct *tty,
301 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 char *buffer;
304 int result = 0;
305
Harvey Harrison441b62c2008-03-03 16:08:34 -0800306 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (buffer_size) {
309 /* override the default buffer sizes */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300310 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!buffer) {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300312 dev_err(&port->dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -ENOMEM;
314 }
Felipe Balbie0d795e2008-06-03 14:47:52 +0300315 kfree(port->read_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 port->read_urb->transfer_buffer = buffer;
317 port->read_urb->transfer_buffer_length = buffer_size;
318
Felipe Balbie0d795e2008-06-03 14:47:52 +0300319 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!buffer) {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300321 dev_err(&port->dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return -ENOMEM;
323 }
Felipe Balbie0d795e2008-06-03 14:47:52 +0300324 kfree(port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 port->write_urb->transfer_buffer = buffer;
326 port->write_urb->transfer_buffer_length = buffer_size;
327 port->bulk_out_size = buffer_size;
328 }
329
330 /* Start reading from the device */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300331 usb_fill_bulk_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 port->read_urb,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300333 port->serial->dev,
334 usb_rcvbulkpipe(port->serial->dev,
335 port->bulk_in_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 port->read_urb->transfer_buffer,
337 port->read_urb->transfer_buffer_length,
338 ir_read_bulk_callback,
339 port);
340 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
341 if (result)
Felipe Balbie0d795e2008-06-03 14:47:52 +0300342 dev_err(&port->dev,
343 "%s - failed submitting read urb, error %d\n",
344 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 return result;
347}
348
Alan Cox95da3102008-07-22 11:09:07 +0100349static void ir_close(struct tty_struct *tty,
350 struct usb_serial_port *port, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800352 dbg("%s - port %d", __func__, port->number);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* shutdown our bulk read */
355 usb_kill_urb(port->read_urb);
356}
357
Alan Cox95da3102008-07-22 11:09:07 +0100358static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
359 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 unsigned char *transfer_buffer;
362 int result;
363 int transfer_size;
364
Harvey Harrison441b62c2008-03-03 16:08:34 -0800365 dbg("%s - port = %d, count = %d", __func__, port->number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (count == 0)
368 return 0;
369
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200370 spin_lock_bh(&port->lock);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700371 if (port->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200372 spin_unlock_bh(&port->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800373 dbg("%s - already writing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700376 port->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200377 spin_unlock_bh(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 transfer_buffer = port->write_urb->transfer_buffer;
380 transfer_size = min(count, port->bulk_out_size - 1);
381
382 /*
383 * The first byte of the packet we send to the device contains an
Felipe Balbie0d795e2008-06-03 14:47:52 +0300384 * inbound header which indicates an additional number of BOFs and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * a baud rate change.
386 *
387 * See section 5.4.2.2 of the USB IrDA spec.
388 */
389 *transfer_buffer = ir_xbof | ir_baud;
390 ++transfer_buffer;
391
Felipe Balbie0d795e2008-06-03 14:47:52 +0300392 memcpy(transfer_buffer, buf, transfer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Felipe Balbie0d795e2008-06-03 14:47:52 +0300394 usb_fill_bulk_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 port->write_urb,
396 port->serial->dev,
397 usb_sndbulkpipe(port->serial->dev,
398 port->bulk_out_endpointAddress),
399 port->write_urb->transfer_buffer,
400 transfer_size + 1,
401 ir_write_bulk_callback,
402 port);
403
404 port->write_urb->transfer_flags = URB_ZERO_PACKET;
405
Felipe Balbie0d795e2008-06-03 14:47:52 +0300406 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700407 if (result) {
408 port->write_urb_busy = 0;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300409 dev_err(&port->dev,
410 "%s - failed submitting write urb, error %d\n",
411 __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700412 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 result = transfer_size;
414
415 return result;
416}
417
Felipe Balbie0d795e2008-06-03 14:47:52 +0300418static void ir_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Ming Leicdc97792008-02-24 18:41:47 +0800420 struct usb_serial_port *port = urb->context;
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);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700424
425 port->write_urb_busy = 0;
Greg Kroah-Hartman3152b742007-06-15 15:44:13 -0700426 if (status) {
427 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800428 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return;
430 }
431
Felipe Balbie0d795e2008-06-03 14:47:52 +0300432 usb_serial_debug_data(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 debug,
434 &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800435 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 urb->actual_length,
437 urb->transfer_buffer);
438
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700439 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Felipe Balbie0d795e2008-06-03 14:47:52 +0300442static void ir_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Ming Leicdc97792008-02-24 18:41:47 +0800444 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct tty_struct *tty;
446 unsigned char *data = urb->transfer_buffer;
447 int result;
Greg Kroah-Hartman3152b742007-06-15 15:44:13 -0700448 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Harvey Harrison441b62c2008-03-03 16:08:34 -0800450 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Alan Cox95da3102008-07-22 11:09:07 +0100452 if (!port->port.count) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800453 dbg("%s - port closed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return;
455 }
456
Greg Kroah-Hartman3152b742007-06-15 15:44:13 -0700457 switch (status) {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300458 case 0: /* Successful */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300459 /*
460 * The first byte of the packet we get from the device
461 * contains a busy indicator and baud rate change.
462 * See section 5.4.1.2 of the USB IrDA spec.
463 */
464 if ((*data & 0x0f) > 0)
465 ir_baud = *data & 0x0f;
Alan Cox95da3102008-07-22 11:09:07 +0100466 usb_serial_debug_data(debug, &port->dev, __func__,
467 urb->actual_length, data);
Alan Cox4a90f092008-10-13 10:39:46 +0100468 tty = tty_port_tty_get(&port->port);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300469 if (tty_buffer_request_room(tty, urb->actual_length - 1)) {
Alan Cox95da3102008-07-22 11:09:07 +0100470 tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300471 tty_flip_buffer_push(tty);
472 }
Alan Cox4a90f092008-10-13 10:39:46 +0100473 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Felipe Balbie0d795e2008-06-03 14:47:52 +0300475 /*
476 * No break here.
477 * We want to resubmit the urb so we can read
478 * again.
479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Felipe Balbie0d795e2008-06-03 14:47:52 +0300481 case -EPROTO: /* taking inspiration from pl2303.c */
Alan Cox95da3102008-07-22 11:09:07 +0100482 /* Continue trying to always read */
Felipe Balbie0d795e2008-06-03 14:47:52 +0300483 usb_fill_bulk_urb(
484 port->read_urb,
Alan Cox95da3102008-07-22 11:09:07 +0100485 port->serial->dev,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300486 usb_rcvbulkpipe(port->serial->dev,
487 port->bulk_in_endpointAddress),
488 port->read_urb->transfer_buffer,
489 port->read_urb->transfer_buffer_length,
490 ir_read_bulk_callback,
491 port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Felipe Balbie0d795e2008-06-03 14:47:52 +0300493 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
494 if (result)
Alan Cox95da3102008-07-22 11:09:07 +0100495 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
Felipe Balbie0d795e2008-06-03 14:47:52 +0300496 __func__, result);
Alan Cox95da3102008-07-22 11:09:07 +0100497 break ;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300498 default:
499 dbg("%s - nonzero read bulk status received: %d",
Alan Cox95da3102008-07-22 11:09:07 +0100500 __func__, status);
501 break ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return;
504}
505
Alan Cox95da3102008-07-22 11:09:07 +0100506static void ir_set_termios(struct tty_struct *tty,
507 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 unsigned char *transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int result;
Alan Coxa6ea4382007-06-22 14:44:54 +0100511 speed_t baud;
512 int ir_baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Harvey Harrison441b62c2008-03-03 16:08:34 -0800514 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Alan Cox95da3102008-07-22 11:09:07 +0100516 baud = tty_get_baud_rate(tty);
Alan Coxa6ea4382007-06-22 14:44:54 +0100517
518 /*
519 * FIXME, we should compare the baud request against the
520 * capability stated in the IR header that we got in the
521 * startup function.
522 */
523
524 switch (baud) {
Felipe Balbie0d795e2008-06-03 14:47:52 +0300525 case 2400:
526 ir_baud = USB_IRDA_BR_2400;
527 break;
528 case 9600:
529 ir_baud = USB_IRDA_BR_9600;
530 break;
531 case 19200:
532 ir_baud = USB_IRDA_BR_19200;
533 break;
534 case 38400:
535 ir_baud = USB_IRDA_BR_38400;
536 break;
537 case 57600:
538 ir_baud = USB_IRDA_BR_57600;
539 break;
540 case 115200:
541 ir_baud = USB_IRDA_BR_115200;
542 break;
543 case 576000:
544 ir_baud = USB_IRDA_BR_576000;
545 break;
546 case 1152000:
547 ir_baud = USB_IRDA_BR_1152000;
548 break;
549 case 4000000:
550 ir_baud = USB_IRDA_BR_4000000;
551 break;
552 default:
553 ir_baud = USB_IRDA_BR_9600;
554 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
Alan Coxa6ea4382007-06-22 14:44:54 +0100557 if (xbof == -1)
558 ir_xbof = ir_xbof_change(ir_add_bof);
559 else
560 ir_xbof = ir_xbof_change(xbof) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Alan Coxa6ea4382007-06-22 14:44:54 +0100562 /* FIXME need to check to see if our write urb is busy right
563 * now, or use a urb pool.
564 *
565 * send the baud change out on an "empty" data packet
566 */
567 transfer_buffer = port->write_urb->transfer_buffer;
568 *transfer_buffer = ir_xbof | ir_baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Felipe Balbie0d795e2008-06-03 14:47:52 +0300570 usb_fill_bulk_urb(
Alan Coxa6ea4382007-06-22 14:44:54 +0100571 port->write_urb,
572 port->serial->dev,
Felipe Balbie0d795e2008-06-03 14:47:52 +0300573 usb_sndbulkpipe(port->serial->dev,
574 port->bulk_out_endpointAddress),
Alan Coxa6ea4382007-06-22 14:44:54 +0100575 port->write_urb->transfer_buffer,
576 1,
577 ir_write_bulk_callback,
578 port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Alan Coxa6ea4382007-06-22 14:44:54 +0100580 port->write_urb->transfer_flags = URB_ZERO_PACKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Felipe Balbie0d795e2008-06-03 14:47:52 +0300582 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
Alan Coxa6ea4382007-06-22 14:44:54 +0100583 if (result)
Felipe Balbie0d795e2008-06-03 14:47:52 +0300584 dev_err(&port->dev,
585 "%s - failed submitting write urb, error %d\n",
586 __func__, result);
Alan Cox560aac22007-10-18 01:24:20 -0700587
588 /* Only speed changes are supported */
Alan Cox95da3102008-07-22 11:09:07 +0100589 tty_termios_copy_hw(tty->termios, old_termios);
590 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Felipe Balbie0d795e2008-06-03 14:47:52 +0300593static int __init ir_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 int retval;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 retval = usb_serial_register(&ir_device);
598 if (retval)
599 goto failed_usb_serial_register;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 retval = usb_register(&ir_driver);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300602 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 goto failed_usb_register;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300604
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700605 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
606 DRIVER_DESC "\n");
Felipe Balbie0d795e2008-06-03 14:47:52 +0300607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 0;
Felipe Balbie0d795e2008-06-03 14:47:52 +0300609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610failed_usb_register:
611 usb_serial_deregister(&ir_device);
Felipe Balbie0d795e2008-06-03 14:47:52 +0300612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613failed_usb_serial_register:
614 return retval;
615}
616
Felipe Balbie0d795e2008-06-03 14:47:52 +0300617static void __exit ir_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Felipe Balbie0d795e2008-06-03 14:47:52 +0300619 usb_deregister(&ir_driver);
620 usb_serial_deregister(&ir_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623
624module_init(ir_init);
625module_exit(ir_exit);
626
627MODULE_AUTHOR(DRIVER_AUTHOR);
628MODULE_DESCRIPTION(DRIVER_DESC);
629MODULE_LICENSE("GPL");
630
631module_param(debug, bool, S_IRUGO | S_IWUSR);
632MODULE_PARM_DESC(debug, "Debug enabled or not");
633module_param(xbof, int, 0);
634MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
635module_param(buffer_size, int, 0);
636MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
637