more changes on original files
[linux-2.4.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);
115 static void ir_read_bulk_callback (struct urb *urb);
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
133 struct usb_serial_device_type ir_device = {
134         .owner =                THIS_MODULE,
135         .name =                 "IR Dongle",
136         .id_table =             id_table,
137         .num_interrupt_in =     1,
138         .num_bulk_in =          1,
139         .num_bulk_out =         1,
140         .num_ports =            1,
141         .set_termios =          ir_set_termios,
142         .startup =              ir_startup,
143         .open =                 ir_open,
144         .close =                ir_close,
145         .write =                ir_write,
146         .write_bulk_callback =  ir_write_bulk_callback,
147         .read_bulk_callback =   ir_read_bulk_callback,
148 };
149
150 static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
151 {
152         dbg("bLength=%x", desc->bLength);
153         dbg("bDescriptorType=%x", desc->bDescriptorType);
154         dbg("bcdSpecRevision=%x", desc->bcdSpecRevision); 
155         dbg("bmDataSize=%x", desc->bmDataSize);
156         dbg("bmWindowSize=%x", desc->bmWindowSize);
157         dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
158         dbg("wBaudRate=%x", desc->wBaudRate);
159         dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
160         dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
161         dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
162 }
163
164 /*------------------------------------------------------------------*/
165 /*
166  * Function irda_usb_find_class_desc(dev, ifnum)
167  *
168  *    Returns instance of IrDA class descriptor, or NULL if not found
169  *
170  * The class descriptor is some extra info that IrDA USB devices will
171  * offer to us, describing their IrDA characteristics. We will use that in
172  * irda_usb_init_qos()
173  *
174  * Based on the same function in drivers/net/irda/irda-usb.c
175  */
176 static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
177 {
178         struct irda_class_desc *desc;
179         int ret;
180                 
181         desc = kmalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
182         if (desc == NULL) 
183                 return NULL;
184         memset(desc, 0, sizeof(struct irda_class_desc));
185         
186         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
187                         IU_REQ_GET_CLASS_DESC,
188                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
189                         0, ifnum, desc, sizeof(*desc), HZ);
190         
191         dbg("%s -  ret=%d", __FUNCTION__, ret);
192         if (ret < sizeof(*desc)) {
193                 dbg("%s - class descriptor read %s (%d)",
194                                 __FUNCTION__, 
195                                 (ret<0) ? "failed" : "too short",
196                                 ret);
197                 goto error;
198         }
199         if (desc->bDescriptorType != USB_DT_IRDA) {
200                 dbg("%s - bad class descriptor type", __FUNCTION__);
201                 goto error;
202         }
203         
204         irda_usb_dump_class_desc(desc);
205         return desc;
206 error:
207         kfree(desc);
208         return NULL;
209 }
210
211
212 static u8 ir_xbof_change(u8 xbof)
213 {
214         u8 result;
215         /* reference irda-usb.c */
216         switch(xbof) {
217                 case 48: result = 0x10; break;
218                 case 28:
219                 case 24: result = 0x20; break;
220                 default:
221                 case 12: result = 0x30; break;
222                 case  5:
223                 case  6: result = 0x40; break;
224                 case  3: result = 0x50; break;
225                 case  2: result = 0x60; break;
226                 case  1: result = 0x70; break;
227                 case  0: result = 0x80; break;
228         }
229         return(result);
230 }
231
232
233 static int ir_startup (struct usb_serial *serial)
234 {
235         struct irda_class_desc *irda_desc;
236
237         irda_desc = irda_usb_find_class_desc (serial->dev, 0);
238         if (irda_desc == NULL) {
239                 err ("IRDA class descriptor not found, device not bound");
240                 return -ENODEV;
241         }
242
243         dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
244                 __FUNCTION__,
245                 (irda_desc->wBaudRate & 0x0001) ? " 2400"    : "",
246                 (irda_desc->wBaudRate & 0x0002) ? " 9600"    : "",
247                 (irda_desc->wBaudRate & 0x0004) ? " 19200"   : "",
248                 (irda_desc->wBaudRate & 0x0008) ? " 38400"   : "",
249                 (irda_desc->wBaudRate & 0x0010) ? " 57600"   : "",
250                 (irda_desc->wBaudRate & 0x0020) ? " 115200"  : "",
251                 (irda_desc->wBaudRate & 0x0040) ? " 576000"  : "",
252                 (irda_desc->wBaudRate & 0x0080) ? " 1152000" : "",
253                 (irda_desc->wBaudRate & 0x0100) ? " 4000000" : "");
254
255         switch( irda_desc->bmAdditionalBOFs ) {
256                 case 0x01: ir_add_bof = 48; break;
257                 case 0x02: ir_add_bof = 24; break;
258                 case 0x04: ir_add_bof = 12; break;
259                 case 0x08: ir_add_bof =  6; break;
260                 case 0x10: ir_add_bof =  3; break;
261                 case 0x20: ir_add_bof =  2; break;
262                 case 0x40: ir_add_bof =  1; break;
263                 case 0x80: ir_add_bof =  0; break;
264                 default:;
265         }
266
267         kfree (irda_desc);
268
269         return 0;               
270 }
271
272 static int ir_open (struct usb_serial_port *port, struct file *filp)
273 {
274         struct usb_serial *serial = port->serial;
275         char *buffer;
276         int result = 0;
277
278         if (port_paranoia_check (port, __FUNCTION__))
279                 return -ENODEV;
280         
281         dbg("%s - port %d", __FUNCTION__, port->number);
282
283         if (buffer_size) {
284                 /* override the default buffer sizes */
285                 buffer = kmalloc (buffer_size, GFP_KERNEL);
286                 if (!buffer) {
287                         err ("%s - out of memory.", __FUNCTION__);
288                         return -ENOMEM;
289                 }
290                 kfree (port->read_urb->transfer_buffer);
291                 port->read_urb->transfer_buffer = buffer;
292                 port->read_urb->transfer_buffer_length = buffer_size;
293
294                 buffer = kmalloc (buffer_size, GFP_KERNEL);
295                 if (!buffer) {
296                         err ("%s - out of memory.", __FUNCTION__);
297                         return -ENOMEM;
298                 }
299                 kfree (port->write_urb->transfer_buffer);
300                 port->write_urb->transfer_buffer = buffer;
301                 port->write_urb->transfer_buffer_length = buffer_size;
302                 port->bulk_out_size = buffer_size;
303         }
304
305         /* Start reading from the device */
306         usb_fill_bulk_urb (
307                 port->read_urb,
308                 serial->dev, 
309                 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
310                 port->read_urb->transfer_buffer,
311                 port->read_urb->transfer_buffer_length,
312                 ir_read_bulk_callback,
313                 port);
314         port->read_urb->transfer_flags = USB_QUEUE_BULK;
315         result = usb_submit_urb(port->read_urb);
316         if (result)
317                 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
318
319         return result;
320 }
321
322 static void ir_close (struct usb_serial_port *port, struct file * filp)
323 {
324         struct usb_serial *serial;
325
326         if (port_paranoia_check (port, __FUNCTION__))
327                 return;
328         
329         dbg("%s - port %d", __FUNCTION__, port->number);
330                          
331         serial = get_usb_serial (port, __FUNCTION__);
332         if (!serial)
333                 return;
334         
335         if (serial->dev) {
336                 /* shutdown our bulk read */
337                 usb_unlink_urb (port->read_urb);
338         }
339 }
340
341 static int ir_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
342 {
343         unsigned char *transfer_buffer;
344         int result;
345         int transfer_size;
346
347         dbg("%s - port = %d, count = %d", __FUNCTION__, port->number, count);
348
349         if (!port->tty) {
350                 err ("%s - no tty???", __FUNCTION__);
351                 return 0;
352         }
353
354         if (count == 0)
355                 return 0;
356
357         if (port->write_urb->status == -EINPROGRESS) {
358                 dbg ("%s - already writing", __FUNCTION__);
359                 return 0;
360         }
361
362         transfer_buffer = port->write_urb->transfer_buffer;
363         transfer_size = min(count, port->bulk_out_size - 1);
364
365         /*
366          * The first byte of the packet we send to the device contains an
367          * inband header which indicates an additional number of BOFs and
368          * a baud rate change.
369          *
370          * See section 5.4.2.2 of the USB IrDA spec.
371          */
372         *transfer_buffer = ir_xbof | ir_baud;
373         ++transfer_buffer;
374
375         if (from_user) {
376                 if (copy_from_user (transfer_buffer, buf, transfer_size))
377                         return -EFAULT;
378         } else {
379                 memcpy (transfer_buffer, buf, transfer_size);
380         }
381
382         usb_fill_bulk_urb (
383                 port->write_urb,
384                 port->serial->dev,
385                 usb_sndbulkpipe(port->serial->dev,
386                         port->bulk_out_endpointAddress),
387                 port->write_urb->transfer_buffer,
388                 transfer_size + 1,
389                 ir_write_bulk_callback,
390                 port);
391
392         port->write_urb->transfer_flags
393                 = USB_QUEUE_BULK
394                 | USB_ZERO_PACKET;
395
396         result = usb_submit_urb (port->write_urb);
397         if (result)
398                 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
399         else
400                 result = transfer_size;
401
402         return result;
403 }
404
405 static void ir_write_bulk_callback (struct urb *urb)
406 {
407         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
408
409         if (port_paranoia_check (port, __FUNCTION__))
410                 return;
411         
412         dbg("%s - port %d", __FUNCTION__, port->number);
413         
414         if (urb->status) {
415                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
416                 return;
417         }
418
419         usb_serial_debug_data (
420                 __FILE__,
421                 __FUNCTION__,
422                 urb->actual_length,
423                 urb->transfer_buffer);
424
425         queue_task(&port->tqueue, &tq_immediate);
426         mark_bh(IMMEDIATE_BH);
427         
428         return;
429 }
430
431 static void ir_read_bulk_callback (struct urb *urb)
432 {
433         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
434         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
435         struct tty_struct *tty;
436         unsigned char *data = urb->transfer_buffer;
437         int result;
438
439         if (port_paranoia_check (port, __FUNCTION__))
440                 return;
441
442         dbg("%s - port %d", __FUNCTION__, port->number);
443
444         if (!serial) {
445                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
446                 return;
447         }
448
449         if (!port->open_count) {
450                 dbg("%s - port closed.", __FUNCTION__);
451                 return;
452         }
453
454         switch (urb->status) {
455
456                 case 0: /* Successful */
457
458                         /*
459                          * The first byte of the packet we get from the device
460                          * contains a busy indicator and baud rate change.
461                          * See section 5.4.1.2 of the USB IrDA spec.
462                          */
463                         if((*data & 0x0f) > 0) ir_baud = *data & 0x0f;
464
465                         usb_serial_debug_data (
466                                 __FILE__,
467                                 __FUNCTION__,
468                                 urb->actual_length,
469                                 data);
470
471                         /*
472                          * Bypass flip-buffers, and feed the ldisc directly
473                          * due to our potentally large buffer size.  Since we
474                          * used to set low_latency, this is exactly what the
475                          * tty layer did anyway :)
476                          */
477                         tty = port->tty;
478
479                         tty->ldisc.receive_buf(
480                                 tty,
481                                 data+1,
482                                 NULL,
483                                 urb->actual_length-1);
484
485                         /*
486                          * No break here.
487                          * We want to resubmit the urb so we can read
488                          * again.
489                          */
490
491                 case -EPROTO: /* taking inspiration from pl2303.c */
492
493                         /* Continue trying to always read */
494                         usb_fill_bulk_urb (
495                                 port->read_urb,
496                                 serial->dev, 
497                                 usb_rcvbulkpipe(serial->dev,
498                                         port->bulk_in_endpointAddress),
499                                 port->read_urb->transfer_buffer,
500                                 port->read_urb->transfer_buffer_length,
501                                 ir_read_bulk_callback,
502                                 port);
503
504                         port->read_urb->transfer_flags = USB_QUEUE_BULK;
505
506                         result = usb_submit_urb(port->read_urb);
507
508                         if (result)
509                                 err("%s - failed resubmitting read urb, error %d",
510                                         __FUNCTION__, 
511                                         result);
512
513                         break ;
514
515                 default:
516                         dbg("%s - nonzero read bulk status received: %d",
517                                 __FUNCTION__, 
518                                 urb->status);
519                         break ;
520
521         }
522
523         return;
524 }
525
526 static void ir_set_termios (struct usb_serial_port *port, struct termios *old_termios)
527 {
528         unsigned char *transfer_buffer;
529         unsigned int cflag;
530         int result;
531
532         dbg("%s - port %d", __FUNCTION__, port->number);
533
534         if ((!port->tty) || (!port->tty->termios)) {
535                 dbg("%s - no tty structures", __FUNCTION__);
536                 return;
537         }
538
539         cflag = port->tty->termios->c_cflag;
540         /* check that they really want us to change something */
541         if (old_termios) {
542                 if ((cflag == old_termios->c_cflag) &&
543                     (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
544                         dbg("%s - nothing to change...", __FUNCTION__);
545                         return;
546                 }
547         }
548
549         /* All we can change is the baud rate */
550         if (cflag & CBAUD) {
551
552                 dbg ("%s - asking for baud %d",
553                         __FUNCTION__,
554                         tty_get_baud_rate(port->tty));
555
556                 /* 
557                  * FIXME, we should compare the baud request against the
558                  * capability stated in the IR header that we got in the
559                  * startup funtion.
560                  */
561                 switch (cflag & CBAUD) {
562                         case B2400:    ir_baud = SPEED_2400;    break;
563                         default:
564                         case B9600:    ir_baud = SPEED_9600;    break;
565                         case B19200:   ir_baud = SPEED_19200;   break;
566                         case B38400:   ir_baud = SPEED_38400;   break;
567                         case B57600:   ir_baud = SPEED_57600;   break;
568                         case B115200:  ir_baud = SPEED_115200;  break;
569                         case B576000:  ir_baud = SPEED_576000;  break;
570                         case B1152000: ir_baud = SPEED_1152000; break;
571 #ifdef B4000000
572                         case B4000000: ir_baud = SPEED_4000000; break;
573 #endif
574                 }
575
576                 if (xbof == -1) {
577                         ir_xbof = ir_xbof_change(ir_add_bof);
578                 } else {
579                         ir_xbof = ir_xbof_change(xbof) ;
580                 }
581
582                 /* Notify the tty driver that the termios have changed. */
583                 port->tty->ldisc.set_termios(port->tty, NULL);
584
585                 /* FIXME need to check to see if our write urb is busy right
586                  * now, or use a urb pool.
587                  *
588                  * send the baud change out on an "empty" data packet
589                  */
590                 transfer_buffer = port->write_urb->transfer_buffer;
591                 *transfer_buffer = ir_xbof | ir_baud;
592
593                 usb_fill_bulk_urb (
594                         port->write_urb,
595                         port->serial->dev,
596                         usb_sndbulkpipe(port->serial->dev,
597                                 port->bulk_out_endpointAddress),
598                         port->write_urb->transfer_buffer,
599                         1,
600                         ir_write_bulk_callback,
601                         port);
602
603                 port->write_urb->transfer_flags
604                         = USB_QUEUE_BULK
605                         | USB_ZERO_PACKET;
606
607                 result = usb_submit_urb (port->write_urb);
608                 if (result)
609                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
610         }
611         return;
612 }
613
614
615 static int __init ir_init (void)
616 {
617         usb_serial_register (&ir_device);
618         info(DRIVER_DESC " " DRIVER_VERSION);
619         return 0;
620 }
621
622
623 static void __exit ir_exit (void)
624 {
625         usb_serial_deregister (&ir_device);
626 }
627
628
629 module_init(ir_init);
630 module_exit(ir_exit);
631
632 MODULE_AUTHOR(DRIVER_AUTHOR);
633 MODULE_DESCRIPTION(DRIVER_DESC);
634 MODULE_LICENSE("GPL");
635
636 MODULE_PARM(debug, "i");
637 MODULE_PARM_DESC(debug, "Debug enabled or not");
638 MODULE_PARM(xbof, "i");
639 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
640 MODULE_PARM(buffer_size, "i");
641 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
642