initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / usb / serial / empeg.c
blob251a50fdab8670ffe12182a48d266fbe2f5b72e2
1 /*
2 * USB Empeg empeg-car player driver
4 * Copyright (C) 2000, 2001
5 * Gary Brubaker (xavyer@ix.netcom.com)
7 * Copyright (C) 1999 - 2001
8 * Greg Kroah-Hartman (greg@kroah.com)
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.
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
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().
20 * (05/30/2001) gkh
21 * switched from using spinlock to a semaphore, which fixes lots of problems.
23 * (04/08/2001) gb
24 * Identify version on module load.
26 * (01/22/2001) gb
27 * Added write_room() and chars_in_buffer() support.
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().
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.
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!)
41 * (11/11/2000) gb
42 * Updated to work with id_table structure.
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.
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"
68 static int debug;
71 * Version Information
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"
77 #define EMPEG_VENDOR_ID 0x084f
78 #define EMPEG_PRODUCT_ID 0x0001
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 int from_user,
85 const unsigned char *buf,
86 int count);
87 static int empeg_write_room (struct usb_serial_port *port);
88 static int empeg_chars_in_buffer (struct usb_serial_port *port);
89 static void empeg_throttle (struct usb_serial_port *port);
90 static void empeg_unthrottle (struct usb_serial_port *port);
91 static int empeg_startup (struct usb_serial *serial);
92 static void empeg_shutdown (struct usb_serial *serial);
93 static int empeg_ioctl (struct usb_serial_port *port,
94 struct file * file,
95 unsigned int cmd,
96 unsigned long arg);
97 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios);
98 static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
99 static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
101 static struct usb_device_id id_table [] = {
102 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
103 { } /* Terminating entry */
106 MODULE_DEVICE_TABLE (usb, id_table);
108 static struct usb_driver empeg_driver = {
109 .owner = THIS_MODULE,
110 .name = "empeg",
111 .probe = usb_serial_probe,
112 .disconnect = usb_serial_disconnect,
113 .id_table = id_table,
116 static struct usb_serial_device_type empeg_device = {
117 .owner = THIS_MODULE,
118 .name = "Empeg",
119 .id_table = id_table,
120 .num_interrupt_in = 0,
121 .num_bulk_in = 1,
122 .num_bulk_out = 1,
123 .num_ports = 1,
124 .open = empeg_open,
125 .close = empeg_close,
126 .throttle = empeg_throttle,
127 .unthrottle = empeg_unthrottle,
128 .attach = empeg_startup,
129 .shutdown = empeg_shutdown,
130 .ioctl = empeg_ioctl,
131 .set_termios = empeg_set_termios,
132 .write = empeg_write,
133 .write_room = empeg_write_room,
134 .chars_in_buffer = empeg_chars_in_buffer,
135 .write_bulk_callback = empeg_write_bulk_callback,
136 .read_bulk_callback = empeg_read_bulk_callback,
139 #define NUM_URBS 16
140 #define URB_TRANSFER_BUFFER_SIZE 4096
142 static struct urb *write_urb_pool[NUM_URBS];
143 static spinlock_t write_urb_pool_lock;
144 static int bytes_in;
145 static int bytes_out;
147 /******************************************************************************
148 * Empeg specific driver functions
149 ******************************************************************************/
150 static int empeg_open (struct usb_serial_port *port, struct file *filp)
152 struct usb_serial *serial = port->serial;
153 int result = 0;
155 dbg("%s - port %d", __FUNCTION__, port->number);
157 /* Force default termio settings */
158 empeg_set_termios (port, NULL) ;
160 bytes_in = 0;
161 bytes_out = 0;
163 /* Start reading from the device */
164 usb_fill_bulk_urb(
165 port->read_urb,
166 serial->dev,
167 usb_rcvbulkpipe(serial->dev,
168 port->bulk_in_endpointAddress),
169 port->read_urb->transfer_buffer,
170 port->read_urb->transfer_buffer_length,
171 empeg_read_bulk_callback,
172 port);
174 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
176 if (result)
177 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
179 return result;
183 static void empeg_close (struct usb_serial_port *port, struct file * filp)
185 dbg("%s - port %d", __FUNCTION__, port->number);
187 /* shutdown our bulk read */
188 usb_unlink_urb (port->read_urb);
189 /* Uncomment the following line if you want to see some statistics in your syslog */
190 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
194 static int empeg_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
196 struct usb_serial *serial = port->serial;
197 struct urb *urb;
198 const unsigned char *current_position = buf;
199 unsigned long flags;
200 int status;
201 int i;
202 int bytes_sent = 0;
203 int transfer_size;
205 dbg("%s - port %d", __FUNCTION__, port->number);
207 while (count > 0) {
209 /* try to find a free urb in our list of them */
210 urb = NULL;
212 spin_lock_irqsave (&write_urb_pool_lock, flags);
214 for (i = 0; i < NUM_URBS; ++i) {
215 if (write_urb_pool[i]->status != -EINPROGRESS) {
216 urb = write_urb_pool[i];
217 break;
221 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
223 if (urb == NULL) {
224 dbg("%s - no more free urbs", __FUNCTION__);
225 goto exit;
228 if (urb->transfer_buffer == NULL) {
229 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
230 if (urb->transfer_buffer == NULL) {
231 dev_err(&port->dev, "%s no more kernel memory...\n", __FUNCTION__);
232 goto exit;
236 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
238 if (from_user) {
239 if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
240 bytes_sent = -EFAULT;
241 break;
243 } else {
244 memcpy (urb->transfer_buffer, current_position, transfer_size);
247 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size, urb->transfer_buffer);
249 /* build up our urb */
250 usb_fill_bulk_urb (
251 urb,
252 serial->dev,
253 usb_sndbulkpipe(serial->dev,
254 port->bulk_out_endpointAddress),
255 urb->transfer_buffer,
256 transfer_size,
257 empeg_write_bulk_callback,
258 port);
260 /* send it down the pipe */
261 status = usb_submit_urb(urb, GFP_ATOMIC);
262 if (status) {
263 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __FUNCTION__, status);
264 bytes_sent = status;
265 break;
268 current_position += transfer_size;
269 bytes_sent += transfer_size;
270 count -= transfer_size;
271 bytes_out += transfer_size;
275 exit:
276 return bytes_sent;
281 static int empeg_write_room (struct usb_serial_port *port)
283 unsigned long flags;
284 int i;
285 int room = 0;
287 dbg("%s - port %d", __FUNCTION__, port->number);
289 spin_lock_irqsave (&write_urb_pool_lock, flags);
291 /* tally up the number of bytes available */
292 for (i = 0; i < NUM_URBS; ++i) {
293 if (write_urb_pool[i]->status != -EINPROGRESS) {
294 room += URB_TRANSFER_BUFFER_SIZE;
298 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
300 dbg("%s - returns %d", __FUNCTION__, room);
302 return (room);
307 static int empeg_chars_in_buffer (struct usb_serial_port *port)
309 unsigned long flags;
310 int i;
311 int chars = 0;
313 dbg("%s - port %d", __FUNCTION__, port->number);
315 spin_lock_irqsave (&write_urb_pool_lock, flags);
317 /* tally up the number of bytes waiting */
318 for (i = 0; i < NUM_URBS; ++i) {
319 if (write_urb_pool[i]->status == -EINPROGRESS) {
320 chars += URB_TRANSFER_BUFFER_SIZE;
324 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
326 dbg("%s - returns %d", __FUNCTION__, chars);
328 return (chars);
333 static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
335 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
337 dbg("%s - port %d", __FUNCTION__, port->number);
339 if (urb->status) {
340 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
341 return;
344 schedule_work(&port->work);
348 static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
350 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
351 struct tty_struct *tty;
352 unsigned char *data = urb->transfer_buffer;
353 int i;
354 int result;
356 dbg("%s - port %d", __FUNCTION__, port->number);
358 if (urb->status) {
359 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
360 return;
363 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
365 tty = port->tty;
367 if (urb->actual_length) {
368 for (i = 0; i < urb->actual_length ; ++i) {
369 /* gb - 2000/11/13
370 * If we insert too many characters we'll overflow the buffer.
371 * This means we'll lose bytes - Decidedly bad.
373 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
374 tty_flip_buffer_push(tty);
376 tty_insert_flip_char(tty, data[i], 0);
378 /* gb - 2000/11/13
379 * Goes straight through instead of scheduling - if tty->low_latency is set.
381 tty_flip_buffer_push(tty);
382 bytes_in += urb->actual_length;
385 /* Continue trying to always read */
386 usb_fill_bulk_urb(
387 port->read_urb,
388 port->serial->dev,
389 usb_rcvbulkpipe(port->serial->dev,
390 port->bulk_in_endpointAddress),
391 port->read_urb->transfer_buffer,
392 port->read_urb->transfer_buffer_length,
393 empeg_read_bulk_callback,
394 port);
396 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
398 if (result)
399 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
401 return;
406 static void empeg_throttle (struct usb_serial_port *port)
408 dbg("%s - port %d", __FUNCTION__, port->number);
409 usb_unlink_urb (port->read_urb);
413 static void empeg_unthrottle (struct usb_serial_port *port)
415 int result;
417 dbg("%s - port %d", __FUNCTION__, port->number);
419 port->read_urb->dev = port->serial->dev;
421 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
423 if (result)
424 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
426 return;
430 static int empeg_startup (struct usb_serial *serial)
432 int r;
434 dbg("%s", __FUNCTION__);
436 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
437 err("active config #%d != 1 ??",
438 serial->dev->actconfig->desc.bConfigurationValue);
439 return -ENODEV;
441 dbg("%s - reset config", __FUNCTION__);
442 r = usb_reset_configuration (serial->dev);
444 /* continue on with initialization */
445 return r;
450 static void empeg_shutdown (struct usb_serial *serial)
452 dbg ("%s", __FUNCTION__);
456 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
458 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
460 return -ENOIOCTLCMD;
464 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
467 dbg("%s - port %d", __FUNCTION__, port->number);
469 if ((!port->tty) || (!port->tty->termios)) {
470 dbg("%s - no tty structures", __FUNCTION__);
471 return;
475 * The empeg-car player wants these particular tty settings.
476 * You could, for example, change the baud rate, however the
477 * player only supports 115200 (currently), so there is really
478 * no point in support for changes to the tty settings.
479 * (at least for now)
481 * The default requirements for this device are:
483 port->tty->termios->c_iflag
484 &= ~(IGNBRK /* disable ignore break */
485 | BRKINT /* disable break causes interrupt */
486 | PARMRK /* disable mark parity errors */
487 | ISTRIP /* disable clear high bit of input characters */
488 | INLCR /* disable translate NL to CR */
489 | IGNCR /* disable ignore CR */
490 | ICRNL /* disable translate CR to NL */
491 | IXON); /* disable enable XON/XOFF flow control */
493 port->tty->termios->c_oflag
494 &= ~OPOST; /* disable postprocess output characters */
496 port->tty->termios->c_lflag
497 &= ~(ECHO /* disable echo input characters */
498 | ECHONL /* disable echo new line */
499 | ICANON /* disable erase, kill, werase, and rprnt special characters */
500 | ISIG /* disable interrupt, quit, and suspend special characters */
501 | IEXTEN); /* disable non-POSIX special characters */
503 port->tty->termios->c_cflag
504 &= ~(CSIZE /* no size */
505 | PARENB /* disable parity bit */
506 | CBAUD); /* clear current baud rate */
508 port->tty->termios->c_cflag
509 |= (CS8 /* character size 8 bits */
510 | B115200); /* baud rate 115200 */
513 * Force low_latency on; otherwise the pushes are scheduled;
514 * this is bad as it opens up the possibility of dropping bytes
515 * on the floor. We don't want to drop bytes on the floor. :)
517 port->tty->low_latency = 1;
519 return;
523 static int __init empeg_init (void)
525 struct urb *urb;
526 int i, retval;
528 /* create our write urb pool and transfer buffers */
529 spin_lock_init (&write_urb_pool_lock);
530 for (i = 0; i < NUM_URBS; ++i) {
531 urb = usb_alloc_urb(0, GFP_KERNEL);
532 write_urb_pool[i] = urb;
533 if (urb == NULL) {
534 err("No more urbs???");
535 continue;
538 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
539 if (!urb->transfer_buffer) {
540 err("%s - out of memory for urb buffers.",
541 __FUNCTION__);
542 continue;
546 retval = usb_serial_register(&empeg_device);
547 if (retval)
548 goto failed_usb_serial_register;
549 retval = usb_register(&empeg_driver);
550 if (retval)
551 goto failed_usb_register;
553 info(DRIVER_VERSION ":" DRIVER_DESC);
555 return 0;
556 failed_usb_register:
557 usb_serial_deregister(&empeg_device);
558 failed_usb_serial_register:
559 for (i = 0; i < NUM_URBS; ++i) {
560 if (write_urb_pool[i]) {
561 if (write_urb_pool[i]->transfer_buffer)
562 kfree(write_urb_pool[i]->transfer_buffer);
563 usb_free_urb(write_urb_pool[i]);
566 return retval;
570 static void __exit empeg_exit (void)
572 int i;
573 unsigned long flags;
575 usb_deregister(&empeg_driver);
576 usb_serial_deregister (&empeg_device);
578 spin_lock_irqsave (&write_urb_pool_lock, flags);
580 for (i = 0; i < NUM_URBS; ++i) {
581 if (write_urb_pool[i]) {
582 /* FIXME - uncomment the following usb_unlink_urb call when
583 * the host controllers get fixed to set urb->dev = NULL after
584 * the urb is finished. Otherwise this call oopses. */
585 /* usb_unlink_urb(write_urb_pool[i]); */
586 if (write_urb_pool[i]->transfer_buffer)
587 kfree(write_urb_pool[i]->transfer_buffer);
588 usb_free_urb (write_urb_pool[i]);
592 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
596 module_init(empeg_init);
597 module_exit(empeg_exit);
599 MODULE_AUTHOR( DRIVER_AUTHOR );
600 MODULE_DESCRIPTION( DRIVER_DESC );
601 MODULE_LICENSE("GPL");
603 module_param(debug, bool, S_IRUGO | S_IWUSR);
604 MODULE_PARM_DESC(debug, "Debug enabled or not");