ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / serial / empeg.c
1 /*
2  * USB Empeg empeg-car player driver
3  *
4  *      Copyright (C) 2000, 2001
5  *          Gary Brubaker (xavyer@ix.netcom.com)
6  *
7  *      Copyright (C) 1999 - 2001
8  *          Greg Kroah-Hartman (greg@kroah.com)
9  *
10  *      This program is free software; you can redistribute it and/or modify
11  *      it under the terms of the GNU General Public License, as published by
12  *      the Free Software Foundation, version 2.
13  *
14  * See Documentation/usb/usb-serial.txt for more information on using this driver
15  * 
16  * (07/16/2001) gb
17  *      remove unused code in empeg_close() (thanks to Oliver Neukum for pointing this
18  *      out) and rewrote empeg_set_termios().
19  * 
20  * (05/30/2001) gkh
21  *      switched from using spinlock to a semaphore, which fixes lots of problems.
22  *
23  * (04/08/2001) gb
24  *      Identify version on module load.
25  * 
26  * (01/22/2001) gb
27  *      Added write_room() and chars_in_buffer() support. 
28  * 
29  * (12/21/2000) gb
30  *      Moved termio stuff inside the port->active check.
31  *      Moved MOD_DEC_USE_COUNT to end of empeg_close().
32  * 
33  * (12/03/2000) gb
34  *      Added port->tty->ldisc.set_termios(port->tty, NULL) to empeg_open()
35  *      This notifies the tty driver that the termios have changed.
36  * 
37  * (11/13/2000) gb
38  *      Moved tty->low_latency = 1 from empeg_read_bulk_callback() to empeg_open()
39  *      (It only needs to be set once - Doh!)
40  * 
41  * (11/11/2000) gb
42  *      Updated to work with id_table structure.
43  * 
44  * (11/04/2000) gb
45  *      Forked this from visor.c, and hacked it up to work with an
46  *      Empeg ltd. empeg-car player.  Constructive criticism welcomed.
47  *      I would like to say, 'Thank You' to Greg Kroah-Hartman for the
48  *      use of his code, and for his guidance, advice and patience. :)
49  *      A 'Thank You' is in order for John Ripley of Empeg ltd for his
50  *      advice, and patience too.
51  * 
52  */
53
54 #include <linux/config.h>
55 #include <linux/kernel.h>
56 #include <linux/errno.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #include <linux/tty.h>
60 #include <linux/tty_driver.h>
61 #include <linux/tty_flip.h>
62 #include <linux/module.h>
63 #include <linux/spinlock.h>
64 #include <asm/uaccess.h>
65 #include <linux/usb.h>
66
67 #ifdef CONFIG_USB_SERIAL_DEBUG
68         static int debug = 1;
69 #else
70         static int debug;
71 #endif
72
73 #include "usb-serial.h"
74
75 /*
76  * Version Information
77  */
78 #define DRIVER_VERSION "v1.2"
79 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
80 #define DRIVER_DESC "USB Empeg Mark I/II Driver"
81
82 #define EMPEG_VENDOR_ID                 0x084f
83 #define EMPEG_PRODUCT_ID                0x0001
84
85 /* function prototypes for an empeg-car player */
86 static int  empeg_open                  (struct usb_serial_port *port, struct file *filp);
87 static void empeg_close                 (struct usb_serial_port *port, struct file *filp);
88 static int  empeg_write                 (struct usb_serial_port *port,
89                                         int from_user,
90                                         const unsigned char *buf,
91                                         int count);
92 static int  empeg_write_room            (struct usb_serial_port *port);
93 static int  empeg_chars_in_buffer       (struct usb_serial_port *port);
94 static void empeg_throttle              (struct usb_serial_port *port);
95 static void empeg_unthrottle            (struct usb_serial_port *port);
96 static int  empeg_startup               (struct usb_serial *serial);
97 static void empeg_shutdown              (struct usb_serial *serial);
98 static int  empeg_ioctl                 (struct usb_serial_port *port,
99                                         struct file * file,
100                                         unsigned int cmd,
101                                         unsigned long arg);
102 static void empeg_set_termios           (struct usb_serial_port *port, struct termios *old_termios);
103 static void empeg_write_bulk_callback   (struct urb *urb, struct pt_regs *regs);
104 static void empeg_read_bulk_callback    (struct urb *urb, struct pt_regs *regs);
105
106 static struct usb_device_id id_table [] = {
107         { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
108         { }                                     /* Terminating entry */
109 };
110
111 MODULE_DEVICE_TABLE (usb, id_table);
112
113 static struct usb_driver empeg_driver = {
114         .owner =        THIS_MODULE,
115         .name =         "empeg",
116         .probe =        usb_serial_probe,
117         .disconnect =   usb_serial_disconnect,
118         .id_table =     id_table,
119 };
120
121 static struct usb_serial_device_type empeg_device = {
122         .owner =                THIS_MODULE,
123         .name =                 "Empeg",
124         .id_table =             id_table,
125         .num_interrupt_in =     0,
126         .num_bulk_in =          1,
127         .num_bulk_out =         1,
128         .num_ports =            1,
129         .open =                 empeg_open,
130         .close =                empeg_close,
131         .throttle =             empeg_throttle,
132         .unthrottle =           empeg_unthrottle,
133         .attach =               empeg_startup,
134         .shutdown =             empeg_shutdown,
135         .ioctl =                empeg_ioctl,
136         .set_termios =          empeg_set_termios,
137         .write =                empeg_write,
138         .write_room =           empeg_write_room,
139         .chars_in_buffer =      empeg_chars_in_buffer,
140         .write_bulk_callback =  empeg_write_bulk_callback,
141         .read_bulk_callback =   empeg_read_bulk_callback,
142 };
143
144 #define NUM_URBS                        16
145 #define URB_TRANSFER_BUFFER_SIZE        4096
146
147 static struct urb       *write_urb_pool[NUM_URBS];
148 static spinlock_t       write_urb_pool_lock;
149 static int              bytes_in;
150 static int              bytes_out;
151
152 /******************************************************************************
153  * Empeg specific driver functions
154  ******************************************************************************/
155 static int empeg_open (struct usb_serial_port *port, struct file *filp)
156 {
157         struct usb_serial *serial = port->serial;
158         int result = 0;
159
160         if (port_paranoia_check (port, __FUNCTION__))
161                 return -ENODEV;
162
163         dbg("%s - port %d", __FUNCTION__, port->number);
164
165         /* Force default termio settings */
166         empeg_set_termios (port, NULL) ;
167
168         bytes_in = 0;
169         bytes_out = 0;
170
171         /* Start reading from the device */
172         usb_fill_bulk_urb(
173                 port->read_urb,
174                 serial->dev, 
175                 usb_rcvbulkpipe(serial->dev,
176                         port->bulk_in_endpointAddress),
177                 port->read_urb->transfer_buffer,
178                 port->read_urb->transfer_buffer_length,
179                 empeg_read_bulk_callback,
180                 port);
181
182         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
183
184         if (result)
185                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
186
187         return result;
188 }
189
190
191 static void empeg_close (struct usb_serial_port *port, struct file * filp)
192 {
193         struct usb_serial *serial;
194
195         if (port_paranoia_check (port, __FUNCTION__))
196                 return;
197
198         dbg("%s - port %d", __FUNCTION__, port->number);
199
200         serial = get_usb_serial (port, __FUNCTION__);
201         if (!serial)
202                 return;
203
204         if (serial->dev) {
205                 /* shutdown our bulk read */
206                 usb_unlink_urb (port->read_urb);
207         }
208         /* Uncomment the following line if you want to see some statistics in your syslog */
209         /* dev_info (&port->dev, "Bytes In = %d  Bytes Out = %d\n", bytes_in, bytes_out); */
210 }
211
212
213 static int empeg_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
214 {
215         struct usb_serial *serial = port->serial;
216         struct urb *urb;
217         const unsigned char *current_position = buf;
218         unsigned long flags;
219         int status;
220         int i;
221         int bytes_sent = 0;
222         int transfer_size;
223
224         dbg("%s - port %d", __FUNCTION__, port->number);
225
226         while (count > 0) {
227
228                 /* try to find a free urb in our list of them */
229                 urb = NULL;
230
231                 spin_lock_irqsave (&write_urb_pool_lock, flags);
232
233                 for (i = 0; i < NUM_URBS; ++i) {
234                         if (write_urb_pool[i]->status != -EINPROGRESS) {
235                                 urb = write_urb_pool[i];
236                                 break;
237                         }
238                 }
239
240                 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
241
242                 if (urb == NULL) {
243                         dbg("%s - no more free urbs", __FUNCTION__);
244                         goto exit;
245                 }
246
247                 if (urb->transfer_buffer == NULL) {
248                         urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
249                         if (urb->transfer_buffer == NULL) {
250                                 dev_err(&port->dev, "%s no more kernel memory...\n", __FUNCTION__);
251                                 goto exit;
252                         }
253                 }
254
255                 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
256
257                 if (from_user) {
258                         if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
259                                 bytes_sent = -EFAULT;
260                                 break;
261                         }
262                 } else {
263                         memcpy (urb->transfer_buffer, current_position, transfer_size);
264                 }
265
266                 usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
267
268                 /* build up our urb */
269                 usb_fill_bulk_urb (
270                         urb,
271                         serial->dev,
272                         usb_sndbulkpipe(serial->dev,
273                                 port->bulk_out_endpointAddress), 
274                         urb->transfer_buffer,
275                         transfer_size,
276                         empeg_write_bulk_callback,
277                         port);
278
279                 /* send it down the pipe */
280                 status = usb_submit_urb(urb, GFP_ATOMIC);
281                 if (status) {
282                         dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __FUNCTION__, status);
283                         bytes_sent = status;
284                         break;
285                 }
286
287                 current_position += transfer_size;
288                 bytes_sent += transfer_size;
289                 count -= transfer_size;
290                 bytes_out += transfer_size;
291
292         }
293
294 exit:
295         return bytes_sent;
296
297
298
299
300 static int empeg_write_room (struct usb_serial_port *port)
301 {
302         unsigned long flags;
303         int i;
304         int room = 0;
305
306         dbg("%s - port %d", __FUNCTION__, port->number);
307
308         spin_lock_irqsave (&write_urb_pool_lock, flags);
309
310         /* tally up the number of bytes available */
311         for (i = 0; i < NUM_URBS; ++i) {
312                 if (write_urb_pool[i]->status != -EINPROGRESS) {
313                         room += URB_TRANSFER_BUFFER_SIZE;
314                 }
315         } 
316
317         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
318
319         dbg("%s - returns %d", __FUNCTION__, room);
320
321         return (room);
322
323 }
324
325
326 static int empeg_chars_in_buffer (struct usb_serial_port *port)
327 {
328         unsigned long flags;
329         int i;
330         int chars = 0;
331
332         dbg("%s - port %d", __FUNCTION__, port->number);
333
334         spin_lock_irqsave (&write_urb_pool_lock, flags);
335
336         /* tally up the number of bytes waiting */
337         for (i = 0; i < NUM_URBS; ++i) {
338                 if (write_urb_pool[i]->status == -EINPROGRESS) {
339                         chars += URB_TRANSFER_BUFFER_SIZE;
340                 }
341         }
342
343         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
344
345         dbg("%s - returns %d", __FUNCTION__, chars);
346
347         return (chars);
348
349 }
350
351
352 static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
353 {
354         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
355
356         if (port_paranoia_check (port, __FUNCTION__))
357                 return;
358
359         dbg("%s - port %d", __FUNCTION__, port->number);
360
361         if (urb->status) {
362                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
363                 return;
364         }
365
366         schedule_work(&port->work);
367 }
368
369
370 static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
371 {
372         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
373         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
374         struct tty_struct *tty;
375         unsigned char *data = urb->transfer_buffer;
376         int i;
377         int result;
378
379         if (port_paranoia_check (port, __FUNCTION__))
380                 return;
381
382         dbg("%s - port %d", __FUNCTION__, port->number);
383
384         if (!serial) {
385                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
386                 return;
387         }
388
389         if (urb->status) {
390                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
391                 return;
392         }
393
394         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
395
396         tty = port->tty;
397
398         if (urb->actual_length) {
399                 for (i = 0; i < urb->actual_length ; ++i) {
400                         /* gb - 2000/11/13
401                          * If we insert too many characters we'll overflow the buffer.
402                          * This means we'll lose bytes - Decidedly bad.
403                          */
404                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
405                                 tty_flip_buffer_push(tty);
406                                 }
407                         tty_insert_flip_char(tty, data[i], 0);
408                 }
409                 /* gb - 2000/11/13
410                  * Goes straight through instead of scheduling - if tty->low_latency is set.
411                  */
412                 tty_flip_buffer_push(tty);
413                 bytes_in += urb->actual_length;
414         }
415
416         /* Continue trying to always read  */
417         usb_fill_bulk_urb(
418                 port->read_urb,
419                 serial->dev, 
420                 usb_rcvbulkpipe(serial->dev,
421                         port->bulk_in_endpointAddress),
422                 port->read_urb->transfer_buffer,
423                 port->read_urb->transfer_buffer_length,
424                 empeg_read_bulk_callback,
425                 port);
426
427         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
428
429         if (result)
430                 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
431
432         return;
433
434 }
435
436
437 static void empeg_throttle (struct usb_serial_port *port)
438 {
439         dbg("%s - port %d", __FUNCTION__, port->number);
440         usb_unlink_urb (port->read_urb);
441 }
442
443
444 static void empeg_unthrottle (struct usb_serial_port *port)
445 {
446         int result;
447
448         dbg("%s - port %d", __FUNCTION__, port->number);
449
450         port->read_urb->dev = port->serial->dev;
451
452         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
453
454         if (result)
455                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
456
457         return;
458 }
459
460
461 static int  empeg_startup (struct usb_serial *serial)
462 {
463         int r;
464
465         dbg("%s", __FUNCTION__);
466
467         if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
468                 err("active config #%d != 1 ??",
469                         serial->dev->actconfig->desc.bConfigurationValue);
470                 return -ENODEV;
471         }
472         dbg("%s - reset config", __FUNCTION__);
473         r = usb_reset_configuration (serial->dev);
474
475         /* continue on with initialization */
476         return r;
477
478 }
479
480
481 static void empeg_shutdown (struct usb_serial *serial)
482 {
483         dbg ("%s", __FUNCTION__);
484 }
485
486
487 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
488 {
489         dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
490
491         return -ENOIOCTLCMD;
492 }
493
494
495 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
496 {
497
498         dbg("%s - port %d", __FUNCTION__, port->number);
499
500         if ((!port->tty) || (!port->tty->termios)) {
501                 dbg("%s - no tty structures", __FUNCTION__);
502                 return;
503         }
504
505         /*
506          * The empeg-car player wants these particular tty settings.
507          * You could, for example, change the baud rate, however the
508          * player only supports 115200 (currently), so there is really
509          * no point in support for changes to the tty settings.
510          * (at least for now)
511          *
512          * The default requirements for this device are:
513          */
514         port->tty->termios->c_iflag
515                 &= ~(IGNBRK     /* disable ignore break */
516                 | BRKINT        /* disable break causes interrupt */
517                 | PARMRK        /* disable mark parity errors */
518                 | ISTRIP        /* disable clear high bit of input characters */
519                 | INLCR         /* disable translate NL to CR */
520                 | IGNCR         /* disable ignore CR */
521                 | ICRNL         /* disable translate CR to NL */
522                 | IXON);        /* disable enable XON/XOFF flow control */
523
524         port->tty->termios->c_oflag
525                 &= ~OPOST;      /* disable postprocess output characters */
526
527         port->tty->termios->c_lflag
528                 &= ~(ECHO       /* disable echo input characters */
529                 | ECHONL        /* disable echo new line */
530                 | ICANON        /* disable erase, kill, werase, and rprnt special characters */
531                 | ISIG          /* disable interrupt, quit, and suspend special characters */
532                 | IEXTEN);      /* disable non-POSIX special characters */
533
534         port->tty->termios->c_cflag
535                 &= ~(CSIZE      /* no size */
536                 | PARENB        /* disable parity bit */
537                 | CBAUD);       /* clear current baud rate */
538
539         port->tty->termios->c_cflag
540                 |= (CS8         /* character size 8 bits */
541                 | B115200);     /* baud rate 115200 */
542
543         /*
544          * Force low_latency on; otherwise the pushes are scheduled;
545          * this is bad as it opens up the possibility of dropping bytes
546          * on the floor.  We don't want to drop bytes on the floor. :)
547          */
548         port->tty->low_latency = 1;
549
550         /* Notify the tty driver that the termios have changed. */
551         port->tty->ldisc.set_termios(port->tty, NULL);
552
553         return;
554
555 }
556
557
558 static int __init empeg_init (void)
559 {
560         struct urb *urb;
561         int i, retval;
562
563         /* create our write urb pool and transfer buffers */ 
564         spin_lock_init (&write_urb_pool_lock);
565         for (i = 0; i < NUM_URBS; ++i) {
566                 urb = usb_alloc_urb(0, GFP_KERNEL);
567                 write_urb_pool[i] = urb;
568                 if (urb == NULL) {
569                         err("No more urbs???");
570                         continue;
571                 }
572
573                 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
574                 if (!urb->transfer_buffer) {
575                         err("%s - out of memory for urb buffers.", 
576                             __FUNCTION__);
577                         continue;
578                 }
579         }
580
581         retval = usb_serial_register(&empeg_device);
582         if (retval)
583                 goto failed_usb_serial_register;
584         retval = usb_register(&empeg_driver);
585         if (retval)
586                 goto failed_usb_register;
587
588         info(DRIVER_VERSION ":" DRIVER_DESC);
589
590         return 0;
591 failed_usb_register:
592         usb_serial_deregister(&empeg_device);
593 failed_usb_serial_register:
594         for (i = 0; i < NUM_URBS; ++i) {
595                 if (write_urb_pool[i]) {
596                         if (write_urb_pool[i]->transfer_buffer)
597                                 kfree(write_urb_pool[i]->transfer_buffer);
598                         usb_free_urb(write_urb_pool[i]);
599                 }
600         }
601         return retval;
602 }
603
604
605 static void __exit empeg_exit (void)
606 {
607         int i;
608         unsigned long flags;
609
610         usb_deregister(&empeg_driver);
611         usb_serial_deregister (&empeg_device);
612
613         spin_lock_irqsave (&write_urb_pool_lock, flags);
614
615         for (i = 0; i < NUM_URBS; ++i) {
616                 if (write_urb_pool[i]) {
617                         /* FIXME - uncomment the following usb_unlink_urb call when
618                          * the host controllers get fixed to set urb->dev = NULL after
619                          * the urb is finished.  Otherwise this call oopses. */
620                         /* usb_unlink_urb(write_urb_pool[i]); */
621                         if (write_urb_pool[i]->transfer_buffer)
622                                 kfree(write_urb_pool[i]->transfer_buffer);
623                         usb_free_urb (write_urb_pool[i]);
624                 }
625         }
626
627         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
628 }
629
630
631 module_init(empeg_init);
632 module_exit(empeg_exit);
633
634 MODULE_AUTHOR( DRIVER_AUTHOR );
635 MODULE_DESCRIPTION( DRIVER_DESC );
636 MODULE_LICENSE("GPL");
637
638 MODULE_PARM(debug, "i");
639 MODULE_PARM_DESC(debug, "Debug enabled or not");
640