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