patch-2_6_7-vs1_9_1_12
[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         dbg("%s - port %d", __FUNCTION__, port->number);
161
162         /* Force default termio settings */
163         empeg_set_termios (port, NULL) ;
164
165         bytes_in = 0;
166         bytes_out = 0;
167
168         /* Start reading from the device */
169         usb_fill_bulk_urb(
170                 port->read_urb,
171                 serial->dev, 
172                 usb_rcvbulkpipe(serial->dev,
173                         port->bulk_in_endpointAddress),
174                 port->read_urb->transfer_buffer,
175                 port->read_urb->transfer_buffer_length,
176                 empeg_read_bulk_callback,
177                 port);
178
179         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
180
181         if (result)
182                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
183
184         return result;
185 }
186
187
188 static void empeg_close (struct usb_serial_port *port, struct file * filp)
189 {
190         dbg("%s - port %d", __FUNCTION__, port->number);
191
192         /* shutdown our bulk read */
193         usb_unlink_urb (port->read_urb);
194         /* Uncomment the following line if you want to see some statistics in your syslog */
195         /* dev_info (&port->dev, "Bytes In = %d  Bytes Out = %d\n", bytes_in, bytes_out); */
196 }
197
198
199 static int empeg_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
200 {
201         struct usb_serial *serial = port->serial;
202         struct urb *urb;
203         const unsigned char *current_position = buf;
204         unsigned long flags;
205         int status;
206         int i;
207         int bytes_sent = 0;
208         int transfer_size;
209
210         dbg("%s - port %d", __FUNCTION__, port->number);
211
212         while (count > 0) {
213
214                 /* try to find a free urb in our list of them */
215                 urb = NULL;
216
217                 spin_lock_irqsave (&write_urb_pool_lock, flags);
218
219                 for (i = 0; i < NUM_URBS; ++i) {
220                         if (write_urb_pool[i]->status != -EINPROGRESS) {
221                                 urb = write_urb_pool[i];
222                                 break;
223                         }
224                 }
225
226                 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
227
228                 if (urb == NULL) {
229                         dbg("%s - no more free urbs", __FUNCTION__);
230                         goto exit;
231                 }
232
233                 if (urb->transfer_buffer == NULL) {
234                         urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
235                         if (urb->transfer_buffer == NULL) {
236                                 dev_err(&port->dev, "%s no more kernel memory...\n", __FUNCTION__);
237                                 goto exit;
238                         }
239                 }
240
241                 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
242
243                 if (from_user) {
244                         if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
245                                 bytes_sent = -EFAULT;
246                                 break;
247                         }
248                 } else {
249                         memcpy (urb->transfer_buffer, current_position, transfer_size);
250                 }
251
252                 usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
253
254                 /* build up our urb */
255                 usb_fill_bulk_urb (
256                         urb,
257                         serial->dev,
258                         usb_sndbulkpipe(serial->dev,
259                                 port->bulk_out_endpointAddress), 
260                         urb->transfer_buffer,
261                         transfer_size,
262                         empeg_write_bulk_callback,
263                         port);
264
265                 /* send it down the pipe */
266                 status = usb_submit_urb(urb, GFP_ATOMIC);
267                 if (status) {
268                         dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __FUNCTION__, status);
269                         bytes_sent = status;
270                         break;
271                 }
272
273                 current_position += transfer_size;
274                 bytes_sent += transfer_size;
275                 count -= transfer_size;
276                 bytes_out += transfer_size;
277
278         }
279
280 exit:
281         return bytes_sent;
282
283
284
285
286 static int empeg_write_room (struct usb_serial_port *port)
287 {
288         unsigned long flags;
289         int i;
290         int room = 0;
291
292         dbg("%s - port %d", __FUNCTION__, port->number);
293
294         spin_lock_irqsave (&write_urb_pool_lock, flags);
295
296         /* tally up the number of bytes available */
297         for (i = 0; i < NUM_URBS; ++i) {
298                 if (write_urb_pool[i]->status != -EINPROGRESS) {
299                         room += URB_TRANSFER_BUFFER_SIZE;
300                 }
301         } 
302
303         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
304
305         dbg("%s - returns %d", __FUNCTION__, room);
306
307         return (room);
308
309 }
310
311
312 static int empeg_chars_in_buffer (struct usb_serial_port *port)
313 {
314         unsigned long flags;
315         int i;
316         int chars = 0;
317
318         dbg("%s - port %d", __FUNCTION__, port->number);
319
320         spin_lock_irqsave (&write_urb_pool_lock, flags);
321
322         /* tally up the number of bytes waiting */
323         for (i = 0; i < NUM_URBS; ++i) {
324                 if (write_urb_pool[i]->status == -EINPROGRESS) {
325                         chars += URB_TRANSFER_BUFFER_SIZE;
326                 }
327         }
328
329         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
330
331         dbg("%s - returns %d", __FUNCTION__, chars);
332
333         return (chars);
334
335 }
336
337
338 static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
339 {
340         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
341
342         dbg("%s - port %d", __FUNCTION__, port->number);
343
344         if (urb->status) {
345                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
346                 return;
347         }
348
349         schedule_work(&port->work);
350 }
351
352
353 static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
354 {
355         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
356         struct tty_struct *tty;
357         unsigned char *data = urb->transfer_buffer;
358         int i;
359         int result;
360
361         dbg("%s - port %d", __FUNCTION__, port->number);
362
363         if (urb->status) {
364                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
365                 return;
366         }
367
368         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
369
370         tty = port->tty;
371
372         if (urb->actual_length) {
373                 for (i = 0; i < urb->actual_length ; ++i) {
374                         /* gb - 2000/11/13
375                          * If we insert too many characters we'll overflow the buffer.
376                          * This means we'll lose bytes - Decidedly bad.
377                          */
378                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
379                                 tty_flip_buffer_push(tty);
380                                 }
381                         tty_insert_flip_char(tty, data[i], 0);
382                 }
383                 /* gb - 2000/11/13
384                  * Goes straight through instead of scheduling - if tty->low_latency is set.
385                  */
386                 tty_flip_buffer_push(tty);
387                 bytes_in += urb->actual_length;
388         }
389
390         /* Continue trying to always read  */
391         usb_fill_bulk_urb(
392                 port->read_urb,
393                 port->serial->dev, 
394                 usb_rcvbulkpipe(port->serial->dev,
395                         port->bulk_in_endpointAddress),
396                 port->read_urb->transfer_buffer,
397                 port->read_urb->transfer_buffer_length,
398                 empeg_read_bulk_callback,
399                 port);
400
401         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
402
403         if (result)
404                 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
405
406         return;
407
408 }
409
410
411 static void empeg_throttle (struct usb_serial_port *port)
412 {
413         dbg("%s - port %d", __FUNCTION__, port->number);
414         usb_unlink_urb (port->read_urb);
415 }
416
417
418 static void empeg_unthrottle (struct usb_serial_port *port)
419 {
420         int result;
421
422         dbg("%s - port %d", __FUNCTION__, port->number);
423
424         port->read_urb->dev = port->serial->dev;
425
426         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
427
428         if (result)
429                 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
430
431         return;
432 }
433
434
435 static int  empeg_startup (struct usb_serial *serial)
436 {
437         int r;
438
439         dbg("%s", __FUNCTION__);
440
441         if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
442                 err("active config #%d != 1 ??",
443                         serial->dev->actconfig->desc.bConfigurationValue);
444                 return -ENODEV;
445         }
446         dbg("%s - reset config", __FUNCTION__);
447         r = usb_reset_configuration (serial->dev);
448
449         /* continue on with initialization */
450         return r;
451
452 }
453
454
455 static void empeg_shutdown (struct usb_serial *serial)
456 {
457         dbg ("%s", __FUNCTION__);
458 }
459
460
461 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
462 {
463         dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
464
465         return -ENOIOCTLCMD;
466 }
467
468
469 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
470 {
471
472         dbg("%s - port %d", __FUNCTION__, port->number);
473
474         if ((!port->tty) || (!port->tty->termios)) {
475                 dbg("%s - no tty structures", __FUNCTION__);
476                 return;
477         }
478
479         /*
480          * The empeg-car player wants these particular tty settings.
481          * You could, for example, change the baud rate, however the
482          * player only supports 115200 (currently), so there is really
483          * no point in support for changes to the tty settings.
484          * (at least for now)
485          *
486          * The default requirements for this device are:
487          */
488         port->tty->termios->c_iflag
489                 &= ~(IGNBRK     /* disable ignore break */
490                 | BRKINT        /* disable break causes interrupt */
491                 | PARMRK        /* disable mark parity errors */
492                 | ISTRIP        /* disable clear high bit of input characters */
493                 | INLCR         /* disable translate NL to CR */
494                 | IGNCR         /* disable ignore CR */
495                 | ICRNL         /* disable translate CR to NL */
496                 | IXON);        /* disable enable XON/XOFF flow control */
497
498         port->tty->termios->c_oflag
499                 &= ~OPOST;      /* disable postprocess output characters */
500
501         port->tty->termios->c_lflag
502                 &= ~(ECHO       /* disable echo input characters */
503                 | ECHONL        /* disable echo new line */
504                 | ICANON        /* disable erase, kill, werase, and rprnt special characters */
505                 | ISIG          /* disable interrupt, quit, and suspend special characters */
506                 | IEXTEN);      /* disable non-POSIX special characters */
507
508         port->tty->termios->c_cflag
509                 &= ~(CSIZE      /* no size */
510                 | PARENB        /* disable parity bit */
511                 | CBAUD);       /* clear current baud rate */
512
513         port->tty->termios->c_cflag
514                 |= (CS8         /* character size 8 bits */
515                 | B115200);     /* baud rate 115200 */
516
517         /*
518          * Force low_latency on; otherwise the pushes are scheduled;
519          * this is bad as it opens up the possibility of dropping bytes
520          * on the floor.  We don't want to drop bytes on the floor. :)
521          */
522         port->tty->low_latency = 1;
523
524         /* Notify the tty driver that the termios have changed. */
525         port->tty->ldisc.set_termios(port->tty, NULL);
526
527         return;
528
529 }
530
531
532 static int __init empeg_init (void)
533 {
534         struct urb *urb;
535         int i, retval;
536
537         /* create our write urb pool and transfer buffers */ 
538         spin_lock_init (&write_urb_pool_lock);
539         for (i = 0; i < NUM_URBS; ++i) {
540                 urb = usb_alloc_urb(0, GFP_KERNEL);
541                 write_urb_pool[i] = urb;
542                 if (urb == NULL) {
543                         err("No more urbs???");
544                         continue;
545                 }
546
547                 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
548                 if (!urb->transfer_buffer) {
549                         err("%s - out of memory for urb buffers.", 
550                             __FUNCTION__);
551                         continue;
552                 }
553         }
554
555         retval = usb_serial_register(&empeg_device);
556         if (retval)
557                 goto failed_usb_serial_register;
558         retval = usb_register(&empeg_driver);
559         if (retval)
560                 goto failed_usb_register;
561
562         info(DRIVER_VERSION ":" DRIVER_DESC);
563
564         return 0;
565 failed_usb_register:
566         usb_serial_deregister(&empeg_device);
567 failed_usb_serial_register:
568         for (i = 0; i < NUM_URBS; ++i) {
569                 if (write_urb_pool[i]) {
570                         if (write_urb_pool[i]->transfer_buffer)
571                                 kfree(write_urb_pool[i]->transfer_buffer);
572                         usb_free_urb(write_urb_pool[i]);
573                 }
574         }
575         return retval;
576 }
577
578
579 static void __exit empeg_exit (void)
580 {
581         int i;
582         unsigned long flags;
583
584         usb_deregister(&empeg_driver);
585         usb_serial_deregister (&empeg_device);
586
587         spin_lock_irqsave (&write_urb_pool_lock, flags);
588
589         for (i = 0; i < NUM_URBS; ++i) {
590                 if (write_urb_pool[i]) {
591                         /* FIXME - uncomment the following usb_unlink_urb call when
592                          * the host controllers get fixed to set urb->dev = NULL after
593                          * the urb is finished.  Otherwise this call oopses. */
594                         /* usb_unlink_urb(write_urb_pool[i]); */
595                         if (write_urb_pool[i]->transfer_buffer)
596                                 kfree(write_urb_pool[i]->transfer_buffer);
597                         usb_free_urb (write_urb_pool[i]);
598                 }
599         }
600
601         spin_unlock_irqrestore (&write_urb_pool_lock, flags);
602 }
603
604
605 module_init(empeg_init);
606 module_exit(empeg_exit);
607
608 MODULE_AUTHOR( DRIVER_AUTHOR );
609 MODULE_DESCRIPTION( DRIVER_DESC );
610 MODULE_LICENSE("GPL");
611
612 MODULE_PARM(debug, "i");
613 MODULE_PARM_DESC(debug, "Debug enabled or not");
614