tifm_sd: separate command flags, socket flags and register bit masks
[linux-2.6/kvm.git] / drivers / usb / serial / empeg.c
blob4703c8f853837431616249f846647d07b344546b
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/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>
67 static int debug;
70 * Version Information
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"
76 #define EMPEG_VENDOR_ID 0x084f
77 #define EMPEG_PRODUCT_ID 0x0001
79 /* function prototypes for an empeg-car player */
80 static int empeg_open (struct usb_serial_port *port, struct file *filp);
81 static void empeg_close (struct usb_serial_port *port, struct file *filp);
82 static int empeg_write (struct usb_serial_port *port,
83 const unsigned char *buf,
84 int count);
85 static int empeg_write_room (struct usb_serial_port *port);
86 static int empeg_chars_in_buffer (struct usb_serial_port *port);
87 static void empeg_throttle (struct usb_serial_port *port);
88 static void empeg_unthrottle (struct usb_serial_port *port);
89 static int empeg_startup (struct usb_serial *serial);
90 static void empeg_shutdown (struct usb_serial *serial);
91 static int empeg_ioctl (struct usb_serial_port *port,
92 struct file * file,
93 unsigned int cmd,
94 unsigned long arg);
95 static void empeg_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
96 static void empeg_write_bulk_callback (struct urb *urb);
97 static void empeg_read_bulk_callback (struct urb *urb);
99 static struct usb_device_id id_table [] = {
100 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
101 { } /* Terminating entry */
104 MODULE_DEVICE_TABLE (usb, id_table);
106 static struct usb_driver empeg_driver = {
107 .name = "empeg",
108 .probe = usb_serial_probe,
109 .disconnect = usb_serial_disconnect,
110 .id_table = id_table,
111 .no_dynamic_id = 1,
114 static struct usb_serial_driver empeg_device = {
115 .driver = {
116 .owner = THIS_MODULE,
117 .name = "empeg",
119 .id_table = id_table,
120 .usb_driver = &empeg_driver,
121 .num_interrupt_in = 0,
122 .num_bulk_in = 1,
123 .num_bulk_out = 1,
124 .num_ports = 1,
125 .open = empeg_open,
126 .close = empeg_close,
127 .throttle = empeg_throttle,
128 .unthrottle = empeg_unthrottle,
129 .attach = empeg_startup,
130 .shutdown = empeg_shutdown,
131 .ioctl = empeg_ioctl,
132 .set_termios = empeg_set_termios,
133 .write = empeg_write,
134 .write_room = empeg_write_room,
135 .chars_in_buffer = empeg_chars_in_buffer,
136 .write_bulk_callback = empeg_write_bulk_callback,
137 .read_bulk_callback = empeg_read_bulk_callback,
140 #define NUM_URBS 16
141 #define URB_TRANSFER_BUFFER_SIZE 4096
143 static struct urb *write_urb_pool[NUM_URBS];
144 static spinlock_t write_urb_pool_lock;
145 static int bytes_in;
146 static int bytes_out;
148 /******************************************************************************
149 * Empeg specific driver functions
150 ******************************************************************************/
151 static int empeg_open (struct usb_serial_port *port, struct file *filp)
153 struct usb_serial *serial = port->serial;
154 int result = 0;
156 dbg("%s - port %d", __FUNCTION__, port->number);
158 /* Force default termio settings */
159 empeg_set_termios (port, NULL) ;
161 bytes_in = 0;
162 bytes_out = 0;
164 /* Start reading from the device */
165 usb_fill_bulk_urb(
166 port->read_urb,
167 serial->dev,
168 usb_rcvbulkpipe(serial->dev,
169 port->bulk_in_endpointAddress),
170 port->read_urb->transfer_buffer,
171 port->read_urb->transfer_buffer_length,
172 empeg_read_bulk_callback,
173 port);
175 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
177 if (result)
178 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
180 return result;
184 static void empeg_close (struct usb_serial_port *port, struct file * filp)
186 dbg("%s - port %d", __FUNCTION__, port->number);
188 /* shutdown our bulk read */
189 usb_kill_urb(port->read_urb);
190 /* Uncomment the following line if you want to see some statistics in your syslog */
191 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
195 static int empeg_write (struct usb_serial_port *port, const unsigned char *buf, int count)
197 struct usb_serial *serial = port->serial;
198 struct urb *urb;
199 const unsigned char *current_position = buf;
200 unsigned long flags;
201 int status;
202 int i;
203 int bytes_sent = 0;
204 int transfer_size;
206 dbg("%s - port %d", __FUNCTION__, port->number);
208 while (count > 0) {
210 /* try to find a free urb in our list of them */
211 urb = NULL;
213 spin_lock_irqsave (&write_urb_pool_lock, flags);
215 for (i = 0; i < NUM_URBS; ++i) {
216 if (write_urb_pool[i]->status != -EINPROGRESS) {
217 urb = write_urb_pool[i];
218 break;
222 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
224 if (urb == NULL) {
225 dbg("%s - no more free urbs", __FUNCTION__);
226 goto exit;
229 if (urb->transfer_buffer == NULL) {
230 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
231 if (urb->transfer_buffer == NULL) {
232 dev_err(&port->dev, "%s no more kernel memory...\n", __FUNCTION__);
233 goto exit;
237 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
239 memcpy (urb->transfer_buffer, current_position, transfer_size);
241 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size, urb->transfer_buffer);
243 /* build up our urb */
244 usb_fill_bulk_urb (
245 urb,
246 serial->dev,
247 usb_sndbulkpipe(serial->dev,
248 port->bulk_out_endpointAddress),
249 urb->transfer_buffer,
250 transfer_size,
251 empeg_write_bulk_callback,
252 port);
254 /* send it down the pipe */
255 status = usb_submit_urb(urb, GFP_ATOMIC);
256 if (status) {
257 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __FUNCTION__, status);
258 bytes_sent = status;
259 break;
262 current_position += transfer_size;
263 bytes_sent += transfer_size;
264 count -= transfer_size;
265 bytes_out += transfer_size;
269 exit:
270 return bytes_sent;
275 static int empeg_write_room (struct usb_serial_port *port)
277 unsigned long flags;
278 int i;
279 int room = 0;
281 dbg("%s - port %d", __FUNCTION__, port->number);
283 spin_lock_irqsave (&write_urb_pool_lock, flags);
285 /* tally up the number of bytes available */
286 for (i = 0; i < NUM_URBS; ++i) {
287 if (write_urb_pool[i]->status != -EINPROGRESS) {
288 room += URB_TRANSFER_BUFFER_SIZE;
292 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
294 dbg("%s - returns %d", __FUNCTION__, room);
296 return (room);
301 static int empeg_chars_in_buffer (struct usb_serial_port *port)
303 unsigned long flags;
304 int i;
305 int chars = 0;
307 dbg("%s - port %d", __FUNCTION__, port->number);
309 spin_lock_irqsave (&write_urb_pool_lock, flags);
311 /* tally up the number of bytes waiting */
312 for (i = 0; i < NUM_URBS; ++i) {
313 if (write_urb_pool[i]->status == -EINPROGRESS) {
314 chars += URB_TRANSFER_BUFFER_SIZE;
318 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
320 dbg("%s - returns %d", __FUNCTION__, chars);
322 return (chars);
327 static void empeg_write_bulk_callback (struct urb *urb)
329 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
331 dbg("%s - port %d", __FUNCTION__, port->number);
333 if (urb->status) {
334 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
335 return;
338 usb_serial_port_softint(port);
342 static void empeg_read_bulk_callback (struct urb *urb)
344 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
345 struct tty_struct *tty;
346 unsigned char *data = urb->transfer_buffer;
347 int result;
349 dbg("%s - port %d", __FUNCTION__, port->number);
351 if (urb->status) {
352 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
353 return;
356 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
358 tty = port->tty;
360 if (urb->actual_length) {
361 tty_buffer_request_room(tty, urb->actual_length);
362 tty_insert_flip_string(tty, data, urb->actual_length);
363 tty_flip_buffer_push(tty);
364 bytes_in += urb->actual_length;
367 /* Continue trying to always read */
368 usb_fill_bulk_urb(
369 port->read_urb,
370 port->serial->dev,
371 usb_rcvbulkpipe(port->serial->dev,
372 port->bulk_in_endpointAddress),
373 port->read_urb->transfer_buffer,
374 port->read_urb->transfer_buffer_length,
375 empeg_read_bulk_callback,
376 port);
378 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
380 if (result)
381 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
383 return;
388 static void empeg_throttle (struct usb_serial_port *port)
390 dbg("%s - port %d", __FUNCTION__, port->number);
391 usb_kill_urb(port->read_urb);
395 static void empeg_unthrottle (struct usb_serial_port *port)
397 int result;
399 dbg("%s - port %d", __FUNCTION__, port->number);
401 port->read_urb->dev = port->serial->dev;
403 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
405 if (result)
406 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
408 return;
412 static int empeg_startup (struct usb_serial *serial)
414 int r;
416 dbg("%s", __FUNCTION__);
418 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
419 err("active config #%d != 1 ??",
420 serial->dev->actconfig->desc.bConfigurationValue);
421 return -ENODEV;
423 dbg("%s - reset config", __FUNCTION__);
424 r = usb_reset_configuration (serial->dev);
426 /* continue on with initialization */
427 return r;
432 static void empeg_shutdown (struct usb_serial *serial)
434 dbg ("%s", __FUNCTION__);
438 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
440 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
442 return -ENOIOCTLCMD;
446 static void empeg_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
449 dbg("%s - port %d", __FUNCTION__, port->number);
451 if ((!port->tty) || (!port->tty->termios)) {
452 dbg("%s - no tty structures", __FUNCTION__);
453 return;
457 * The empeg-car player wants these particular tty settings.
458 * You could, for example, change the baud rate, however the
459 * player only supports 115200 (currently), so there is really
460 * no point in support for changes to the tty settings.
461 * (at least for now)
463 * The default requirements for this device are:
465 port->tty->termios->c_iflag
466 &= ~(IGNBRK /* disable ignore break */
467 | BRKINT /* disable break causes interrupt */
468 | PARMRK /* disable mark parity errors */
469 | ISTRIP /* disable clear high bit of input characters */
470 | INLCR /* disable translate NL to CR */
471 | IGNCR /* disable ignore CR */
472 | ICRNL /* disable translate CR to NL */
473 | IXON); /* disable enable XON/XOFF flow control */
475 port->tty->termios->c_oflag
476 &= ~OPOST; /* disable postprocess output characters */
478 port->tty->termios->c_lflag
479 &= ~(ECHO /* disable echo input characters */
480 | ECHONL /* disable echo new line */
481 | ICANON /* disable erase, kill, werase, and rprnt special characters */
482 | ISIG /* disable interrupt, quit, and suspend special characters */
483 | IEXTEN); /* disable non-POSIX special characters */
485 port->tty->termios->c_cflag
486 &= ~(CSIZE /* no size */
487 | PARENB /* disable parity bit */
488 | CBAUD); /* clear current baud rate */
490 port->tty->termios->c_cflag
491 |= (CS8 /* character size 8 bits */
492 | B115200); /* baud rate 115200 */
495 * Force low_latency on; otherwise the pushes are scheduled;
496 * this is bad as it opens up the possibility of dropping bytes
497 * on the floor. We don't want to drop bytes on the floor. :)
499 port->tty->low_latency = 1;
501 return;
505 static int __init empeg_init (void)
507 struct urb *urb;
508 int i, retval;
510 /* create our write urb pool and transfer buffers */
511 spin_lock_init (&write_urb_pool_lock);
512 for (i = 0; i < NUM_URBS; ++i) {
513 urb = usb_alloc_urb(0, GFP_KERNEL);
514 write_urb_pool[i] = urb;
515 if (urb == NULL) {
516 err("No more urbs???");
517 continue;
520 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
521 if (!urb->transfer_buffer) {
522 err("%s - out of memory for urb buffers.",
523 __FUNCTION__);
524 continue;
528 retval = usb_serial_register(&empeg_device);
529 if (retval)
530 goto failed_usb_serial_register;
531 retval = usb_register(&empeg_driver);
532 if (retval)
533 goto failed_usb_register;
535 info(DRIVER_VERSION ":" DRIVER_DESC);
537 return 0;
538 failed_usb_register:
539 usb_serial_deregister(&empeg_device);
540 failed_usb_serial_register:
541 for (i = 0; i < NUM_URBS; ++i) {
542 if (write_urb_pool[i]) {
543 kfree(write_urb_pool[i]->transfer_buffer);
544 usb_free_urb(write_urb_pool[i]);
547 return retval;
551 static void __exit empeg_exit (void)
553 int i;
554 unsigned long flags;
556 usb_deregister(&empeg_driver);
557 usb_serial_deregister (&empeg_device);
559 spin_lock_irqsave (&write_urb_pool_lock, flags);
561 for (i = 0; i < NUM_URBS; ++i) {
562 if (write_urb_pool[i]) {
563 /* FIXME - uncomment the following usb_kill_urb call when
564 * the host controllers get fixed to set urb->dev = NULL after
565 * the urb is finished. Otherwise this call oopses. */
566 /* usb_kill_urb(write_urb_pool[i]); */
567 kfree(write_urb_pool[i]->transfer_buffer);
568 usb_free_urb (write_urb_pool[i]);
572 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
576 module_init(empeg_init);
577 module_exit(empeg_exit);
579 MODULE_AUTHOR( DRIVER_AUTHOR );
580 MODULE_DESCRIPTION( DRIVER_DESC );
581 MODULE_LICENSE("GPL");
583 module_param(debug, bool, S_IRUGO | S_IWUSR);
584 MODULE_PARM_DESC(debug, "Debug enabled or not");