patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / usb / serial / ir-usb.c
1 /*
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  *
22  * See Documentation/usb/usb-serial.txt for more information on using this driver
23  *
24  * 2002_Mar_07  greg kh
25  *      moved some needed structures and #define values from the
26  *      net/irda/irda-usb.h file into our file, as we don't want to depend on
27  *      that codebase compiling correctly :)
28  *
29  * 2002_Jan_14  gb
30  *      Added module parameter to force specific number of XBOFs.
31  *      Added ir_xbof_change().
32  *      Reorganized read_bulk_callback error handling.
33  *      Switched from FILL_BULK_URB() to usb_fill_bulk_urb().
34  *
35  * 2001_Nov_08  greg kh
36  *      Changed the irda_usb_find_class_desc() function based on comments and
37  *      code from Martin Diehl.
38  *
39  * 2001_Nov_01  greg kh
40  *      Added support for more IrDA USB devices.
41  *      Added support for zero packet.  Added buffer override paramater, so
42  *      users can transfer larger packets at once if they wish.  Both patches
43  *      came from Dag Brattli <dag@obexcode.com>.
44  *
45  * 2001_Oct_07  greg kh
46  *      initial version released.
47  */
48
49 #include <linux/config.h>
50 #include <linux/kernel.h>
51 #include <linux/errno.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/tty.h>
55 #include <linux/tty_driver.h>
56 #include <linux/tty_flip.h>
57 #include <linux/module.h>
58 #include <linux/spinlock.h>
59 #include <asm/uaccess.h>
60 #include <linux/usb.h>
61
62 #ifdef CONFIG_USB_SERIAL_DEBUG
63         static int debug = 1;
64 #else
65         static int debug;
66 #endif
67
68 #include "usb-serial.h"
69
70 /*
71  * Version Information
72  */
73 #define DRIVER_VERSION "v0.4"
74 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
75 #define DRIVER_DESC "USB IR Dongle driver"
76
77 /* USB IrDA class spec information */
78 #define USB_CLASS_IRDA          0x02
79 #define USB_DT_IRDA             0x21
80 #define IU_REQ_GET_CLASS_DESC   0x06
81 #define SPEED_2400              0x01
82 #define SPEED_9600              0x02
83 #define SPEED_19200             0x03
84 #define SPEED_38400             0x04
85 #define SPEED_57600             0x05
86 #define SPEED_115200            0x06
87 #define SPEED_576000            0x07
88 #define SPEED_1152000           0x08
89 #define SPEED_4000000           0x09
90
91 struct irda_class_desc {
92         u8      bLength;
93         u8      bDescriptorType;
94         u16     bcdSpecRevision;
95         u8      bmDataSize;
96         u8      bmWindowSize;
97         u8      bmMinTurnaroundTime;
98         u16     wBaudRate;
99         u8      bmAdditionalBOFs;
100         u8      bIrdaRateSniff;
101         u8      bMaxUnicastList;
102 } __attribute__ ((packed));
103
104 /* if overridden by the user, then use their value for the size of the read and
105  * write urbs */
106 static int buffer_size = 0;
107 /* if overridden by the user, then use the specified number of XBOFs */
108 static int xbof = -1;
109
110 static int  ir_startup (struct usb_serial *serial);
111 static int  ir_open (struct usb_serial_port *port, struct file *filep);
112 static void ir_close (struct usb_serial_port *port, struct file *filep);
113 static int  ir_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
114 static void ir_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
115 static void ir_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
116 static void ir_set_termios (struct usb_serial_port *port, struct termios *old_termios);
117
118 static u8 ir_baud = 0;
119 static u8 ir_xbof = 0;
120 static u8 ir_add_bof = 0;
121
122 static struct usb_device_id id_table [] = {
123         { USB_DEVICE(0x050f, 0x0180) },         /* KC Technology, KC-180 */
124         { USB_DEVICE(0x08e9, 0x0100) },         /* XTNDAccess */
125         { USB_DEVICE(0x09c4, 0x0011) },         /* ACTiSys ACT-IR2000U */
126         { USB_INTERFACE_INFO (USB_CLASS_APP_SPEC, USB_CLASS_IRDA, 0) },
127         { }                                     /* Terminating entry */
128 };
129
130 MODULE_DEVICE_TABLE (usb, id_table);
131
132 static struct usb_driver ir_driver = {
133         .owner =        THIS_MODULE,
134         .name =         "ir-usb",
135         .probe =        usb_serial_probe,
136         .disconnect =   usb_serial_disconnect,
137         .id_table =     id_table,
138 };
139
140
141 static struct usb_serial_device_type ir_device = {
142         .owner =                THIS_MODULE,
143         .name =                 "IR Dongle",
144         .id_table =             id_table,
145         .num_interrupt_in =     1,
146         .num_bulk_in =          1,
147         .num_bulk_out =         1,
148         .num_ports =            1,
149         .set_termios =          ir_set_termios,
150         .attach =               ir_startup,
151         .open =                 ir_open,
152         .close =                ir_close,
153         .write =                ir_write,
154         .write_bulk_callback =  ir_write_bulk_callback,
155         .read_bulk_callback =   ir_read_bulk_callback,
156 };
157
158 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
159 {
160         dbg("bLength=%x", desc->bLength);
161         dbg("bDescriptorType=%x", desc->bDescriptorType);
162         dbg("bcdSpecRevision=%x", desc->bcdSpecRevision); 
163         dbg("bmDataSize=%x", desc->bmDataSize);
164         dbg("bmWindowSize=%x", desc->bmWindowSize);
165         dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
166         dbg("wBaudRate=%x", desc->wBaudRate);
167         dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
168         dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
169         dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
170 }
171
172 /*------------------------------------------------------------------*/
173 /*
174  * Function irda_usb_find_class_desc(dev, ifnum)
175  *
176  *    Returns instance of IrDA class descriptor, or NULL if not found
177  *
178  * The class descriptor is some extra info that IrDA USB devices will
179  * offer to us, describing their IrDA characteristics. We will use that in
180  * irda_usb_init_qos()
181  *
182  * Based on the same function in drivers/net/irda/irda-usb.c
183  */
184 static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
185 {
186         struct irda_class_desc *desc;
187         int ret;
188                 
189         desc = kmalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
190         if (desc == NULL) 
191                 return NULL;
192         memset(desc, 0, sizeof(struct irda_class_desc));
193         
194         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
195                         IU_REQ_GET_CLASS_DESC,
196                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
197                         0, ifnum, desc, sizeof(*desc), HZ);
198         
199         dbg("%s -  ret=%d", __FUNCTION__, ret);
200         if (ret < sizeof(*desc)) {
201                 dbg("%s - class descriptor read %s (%d)",
202                                 __FUNCTION__, 
203                                 (ret<0) ? "failed" : "too short",
204                                 ret);
205                 goto error;
206         }
207         if (desc->bDescriptorType != USB_DT_IRDA) {
208                 dbg("%s - bad class descriptor type", __FUNCTION__);
209                 goto error;
210         }
211         
212         irda_usb_dump_class_desc(desc);
213         return desc;
214 error:
215         kfree(desc);
216         return NULL;
217 }
218
219
220 static u8 ir_xbof_change(u8 xbof)
221 {
222         u8 result;
223         /* reference irda-usb.c */
224         switch(xbof) {
225                 case 48: result = 0x10; break;
226                 case 28:
227                 case 24: result = 0x20; break;
228                 default:
229                 case 12: result = 0x30; break;
230                 case  5:
231                 case  6: result = 0x40; break;
232                 case  3: result = 0x50; break;
233                 case  2: result = 0x60; break;
234                 case  1: result = 0x70; break;
235                 case  0: result = 0x80; break;
236         }
237         return(result);
238 }
239
240
241 static int ir_startup (struct usb_serial *serial)
242 {
243         struct irda_class_desc *irda_desc;
244
245         irda_desc = irda_usb_find_class_desc (serial->dev, 0);
246         if (irda_desc == NULL) {
247                 dev_err (&serial->dev->dev, "IRDA class descriptor not found, device not bound\n");
248                 return -ENODEV;
249         }
250
251         dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
252                 __FUNCTION__,
253                 (irda_desc->wBaudRate & 0x0001) ? " 2400"    : "",
254                 (irda_desc->wBaudRate & 0x0002) ? " 9600"    : "",
255                 (irda_desc->wBaudRate & 0x0004) ? " 19200"   : "",
256                 (irda_desc->wBaudRate & 0x0008) ? " 38400"   : "",
257                 (irda_desc->wBaudRate & 0x0010) ? " 57600"   : "",
258                 (irda_desc->wBaudRate & 0x0020) ? " 115200"  : "",
259                 (irda_desc->wBaudRate & 0x0040) ? " 576000"  : "",
260                 (irda_desc->wBaudRate & 0x0080) ? " 1152000" : "",
261                 (irda_desc->wBaudRate & 0x0100) ? " 4000000" : "");
262
263         switch( irda_desc->bmAdditionalBOFs ) {
264                 case 0x01: ir_add_bof = 48; break;
265                 case 0x02: ir_add_bof = 24; break;
266                 case 0x04: ir_add_bof = 12; break;
267                 case 0x08: ir_add_bof =  6; break;
268                 case 0x10: ir_add_bof =  3; break;
269                 case 0x20: ir_add_bof =  2; break;
270                 case 0x40: ir_add_bof =  1; break;
271                 case 0x80: ir_add_bof =  0; break;
272                 default:;
273         }
274
275         kfree (irda_desc);
276
277         return 0;               
278 }
279
280 static int ir_open (struct usb_serial_port *port, struct file *filp)
281 {
282         char *buffer;
283         int result = 0;
284
285         dbg("%s - port %d", __FUNCTION__, port->number);
286
287         if (buffer_size) {
288                 /* override the default buffer sizes */
289                 buffer = kmalloc (buffer_size, GFP_KERNEL);
290                 if (!buffer) {
291                         dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
292                         return -ENOMEM;
293                 }
294                 kfree (port->read_urb->transfer_buffer);
295                 port->read_urb->transfer_buffer = buffer;
296                 port->read_urb->transfer_buffer_length = buffer_size;
297
298                 buffer = kmalloc (buffer_size, GFP_KERNEL);
299                 if (!buffer) {
300                         dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
301                         return -ENOMEM;
302                 }
303                 kfree (port->write_urb->transfer_buffer);
304                 port->write_urb->transfer_buffer = buffer;
305                 port->write_urb->transfer_buffer_length = buffer_size;
306                 port->bulk_out_size = buffer_size;
307         }
308
309         /* Start reading from the device */
310         usb_fill_bulk_urb (
311                 port->read_urb,
312                 port->serial->dev, 
313                 usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
314                 port->read_urb->transfer_buffer,
315                 port->read_urb->transfer_buffer_length,
316                 ir_read_bulk_callback,
317                 port);
318         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
319         if (result)
320                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
321
322         return result;
323 }
324
325 static void ir_close (struct usb_serial_port *port, struct file * filp)
326 {
327         dbg("%s - port %d", __FUNCTION__, port->number);
328                          
329         /* shutdown our bulk read */
330         usb_unlink_urb (port->read_urb);
331 }
332
333 static int ir_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
334 {
335         unsigned char *transfer_buffer;
336         int result;
337         int transfer_size;
338
339         dbg("%s - port = %d, count = %d", __FUNCTION__, port->number, count);
340
341         if (!port->tty) {
342                 dev_err (&port->dev, "%s - no tty???\n", __FUNCTION__);
343                 return 0;
344         }
345
346         if (count == 0)
347                 return 0;
348
349         if (port->write_urb->status == -EINPROGRESS) {
350                 dbg ("%s - already writing", __FUNCTION__);
351                 return 0;
352         }
353
354         transfer_buffer = port->write_urb->transfer_buffer;
355         transfer_size = min(count, port->bulk_out_size - 1);
356
357         /*
358          * The first byte of the packet we send to the device contains an
359          * inband header which indicates an additional number of BOFs and
360          * a baud rate change.
361          *
362          * See section 5.4.2.2 of the USB IrDA spec.
363          */
364         *transfer_buffer = ir_xbof | ir_baud;
365         ++transfer_buffer;
366
367         if (from_user) {
368                 if (copy_from_user (transfer_buffer, buf, transfer_size))
369                         return -EFAULT;
370         } else {
371                 memcpy (transfer_buffer, buf, transfer_size);
372         }
373
374         usb_fill_bulk_urb (
375                 port->write_urb,
376                 port->serial->dev,
377                 usb_sndbulkpipe(port->serial->dev,
378                         port->bulk_out_endpointAddress),
379                 port->write_urb->transfer_buffer,
380                 transfer_size + 1,
381                 ir_write_bulk_callback,
382                 port);
383
384         port->write_urb->transfer_flags = URB_ZERO_PACKET;
385
386         result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
387         if (result)
388                 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
389         else
390                 result = transfer_size;
391
392         return result;
393 }
394
395 static void ir_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
396 {
397         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
398
399         dbg("%s - port %d", __FUNCTION__, port->number);
400         
401         if (urb->status) {
402                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
403                 return;
404         }
405
406         usb_serial_debug_data (
407                 __FILE__,
408                 __FUNCTION__,
409                 urb->actual_length,
410                 urb->transfer_buffer);
411
412         schedule_work(&port->work);
413 }
414
415 static void ir_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
416 {
417         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
418         struct tty_struct *tty;
419         unsigned char *data = urb->transfer_buffer;
420         int result;
421
422         dbg("%s - port %d", __FUNCTION__, port->number);
423
424         if (!port->open_count) {
425                 dbg("%s - port closed.", __FUNCTION__);
426                 return;
427         }
428
429         switch (urb->status) {
430
431                 case 0: /* Successful */
432
433                         /*
434                          * The first byte of the packet we get from the device
435                          * contains a busy indicator and baud rate change.
436                          * See section 5.4.1.2 of the USB IrDA spec.
437                          */
438                         if ((*data & 0x0f) > 0)
439                                 ir_baud = *data & 0x0f;
440
441                         usb_serial_debug_data (
442                                 __FILE__,
443                                 __FUNCTION__,
444                                 urb->actual_length,
445                                 data);
446
447                         /*
448                          * Bypass flip-buffers, and feed the ldisc directly
449                          * due to our potentially large buffer size.  Since we
450                          * used to set low_latency, this is exactly what the
451                          * tty layer did anyway :)
452                          */
453                         tty = port->tty;
454
455                         tty->ldisc.receive_buf(
456                                 tty,
457                                 data+1,
458                                 NULL,
459                                 urb->actual_length-1);
460
461                         /*
462                          * No break here.
463                          * We want to resubmit the urb so we can read
464                          * again.
465                          */
466
467                 case -EPROTO: /* taking inspiration from pl2303.c */
468
469                         /* Continue trying to always read */
470                         usb_fill_bulk_urb (
471                                 port->read_urb,
472                                 port->serial->dev, 
473                                 usb_rcvbulkpipe(port->serial->dev,
474                                         port->bulk_in_endpointAddress),
475                                 port->read_urb->transfer_buffer,
476                                 port->read_urb->transfer_buffer_length,
477                                 ir_read_bulk_callback,
478                                 port);
479
480                         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
481                         if (result)
482                                 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
483                                         __FUNCTION__, result);
484
485                         break ;
486
487                 default:
488                         dbg("%s - nonzero read bulk status received: %d",
489                                 __FUNCTION__, 
490                                 urb->status);
491                         break ;
492
493         }
494
495         return;
496 }
497
498 static void ir_set_termios (struct usb_serial_port *port, struct termios *old_termios)
499 {
500         unsigned char *transfer_buffer;
501         unsigned int cflag;
502         int result;
503
504         dbg("%s - port %d", __FUNCTION__, port->number);
505
506         if ((!port->tty) || (!port->tty->termios)) {
507                 dbg("%s - no tty structures", __FUNCTION__);
508                 return;
509         }
510
511         cflag = port->tty->termios->c_cflag;
512         /* check that they really want us to change something */
513         if (old_termios) {
514                 if ((cflag == old_termios->c_cflag) &&
515                     (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
516                         dbg("%s - nothing to change...", __FUNCTION__);
517                         return;
518                 }
519         }
520
521         /* All we can change is the baud rate */
522         if (cflag & CBAUD) {
523
524                 dbg ("%s - asking for baud %d",
525                         __FUNCTION__,
526                         tty_get_baud_rate(port->tty));
527
528                 /* 
529                  * FIXME, we should compare the baud request against the
530                  * capability stated in the IR header that we got in the
531                  * startup function.
532                  */
533                 switch (cflag & CBAUD) {
534                         case B2400:    ir_baud = SPEED_2400;    break;
535                         default:
536                         case B9600:    ir_baud = SPEED_9600;    break;
537                         case B19200:   ir_baud = SPEED_19200;   break;
538                         case B38400:   ir_baud = SPEED_38400;   break;
539                         case B57600:   ir_baud = SPEED_57600;   break;
540                         case B115200:  ir_baud = SPEED_115200;  break;
541                         case B576000:  ir_baud = SPEED_576000;  break;
542                         case B1152000: ir_baud = SPEED_1152000; break;
543 #ifdef B4000000
544                         case B4000000: ir_baud = SPEED_4000000; break;
545 #endif
546                 }
547
548                 if (xbof == -1) {
549                         ir_xbof = ir_xbof_change(ir_add_bof);
550                 } else {
551                         ir_xbof = ir_xbof_change(xbof) ;
552                 }
553
554                 /* Notify the tty driver that the termios have changed. */
555                 port->tty->ldisc.set_termios(port->tty, NULL);
556
557                 /* FIXME need to check to see if our write urb is busy right
558                  * now, or use a urb pool.
559                  *
560                  * send the baud change out on an "empty" data packet
561                  */
562                 transfer_buffer = port->write_urb->transfer_buffer;
563                 *transfer_buffer = ir_xbof | ir_baud;
564
565                 usb_fill_bulk_urb (
566                         port->write_urb,
567                         port->serial->dev,
568                         usb_sndbulkpipe(port->serial->dev,
569                                 port->bulk_out_endpointAddress),
570                         port->write_urb->transfer_buffer,
571                         1,
572                         ir_write_bulk_callback,
573                         port);
574
575                 port->write_urb->transfer_flags = URB_ZERO_PACKET;
576
577                 result = usb_submit_urb (port->write_urb, GFP_KERNEL);
578                 if (result)
579                         dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
580         }
581         return;
582 }
583
584
585 static int __init ir_init (void)
586 {
587         int retval;
588         retval = usb_serial_register(&ir_device);
589         if (retval)
590                 goto failed_usb_serial_register;
591         retval = usb_register(&ir_driver);
592         if (retval) 
593                 goto failed_usb_register;
594         info(DRIVER_DESC " " DRIVER_VERSION);
595         return 0;
596 failed_usb_register:
597         usb_serial_deregister(&ir_device);
598 failed_usb_serial_register:
599         return retval;
600 }
601
602
603 static void __exit ir_exit (void)
604 {
605         usb_deregister (&ir_driver);
606         usb_serial_deregister (&ir_device);
607 }
608
609
610 module_init(ir_init);
611 module_exit(ir_exit);
612
613 MODULE_AUTHOR(DRIVER_AUTHOR);
614 MODULE_DESCRIPTION(DRIVER_DESC);
615 MODULE_LICENSE("GPL");
616
617 MODULE_PARM(debug, "i");
618 MODULE_PARM_DESC(debug, "Debug enabled or not");
619 MODULE_PARM(xbof, "i");
620 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
621 MODULE_PARM(buffer_size, "i");
622 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
623