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