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
17 * remove unused code in empeg_close() (thanks to Oliver Neukum for pointing this
18 * out) and rewrote empeg_set_termios().
21 * switched from using spinlock to a semaphore, which fixes lots of problems.
24 * Identify version on module load.
27 * Added write_room() and chars_in_buffer() support.
30 * Moved termio stuff inside the port->active check.
31 * Moved MOD_DEC_USE_COUNT to end of empeg_close().
34 * Added port->tty->ldisc.set_termios(port->tty, NULL) to empeg_open()
35 * This notifies the tty driver that the termios have changed.
38 * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to empeg_open()
39 * (It only needs to be set once - Doh!)
42 * Updated to work with id_table structure.
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>
67 #ifdef CONFIG_USB_SERIAL_DEBUG
73 #include "usb-serial.h"
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"
82 #define EMPEG_VENDOR_ID 0x084f
83 #define EMPEG_PRODUCT_ID 0x0001
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,
90 const unsigned char *buf,
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,
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);
106 static struct usb_device_id id_table [] = {
107 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
108 { } /* Terminating entry */
111 MODULE_DEVICE_TABLE (usb, id_table);
113 static struct usb_driver empeg_driver = {
114 .owner = THIS_MODULE,
116 .probe = usb_serial_probe,
117 .disconnect = usb_serial_disconnect,
118 .id_table = id_table,
121 static struct usb_serial_device_type empeg_device = {
122 .owner = THIS_MODULE,
124 .id_table = id_table,
125 .num_interrupt_in = 0,
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,
145 #define URB_TRANSFER_BUFFER_SIZE 4096
147 static struct urb *write_urb_pool[NUM_URBS];
148 static spinlock_t write_urb_pool_lock;
150 static int bytes_out;
152 /******************************************************************************
153 * Empeg specific driver functions
154 ******************************************************************************/
155 static int empeg_open (struct usb_serial_port *port, struct file *filp)
157 struct usb_serial *serial = port->serial;
160 if (port_paranoia_check (port, __FUNCTION__))
163 dbg("%s - port %d", __FUNCTION__, port->number);
165 /* Force default termio settings */
166 empeg_set_termios (port, NULL) ;
171 /* Start reading from the device */
175 usb_rcvbulkpipe(serial->dev,
176 port->bulk_in_endpointAddress),
177 port->read_urb->transfer_buffer,
178 port->read_urb->transfer_buffer_length,
179 empeg_read_bulk_callback,
182 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
185 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
191 static void empeg_close (struct usb_serial_port *port, struct file * filp)
193 struct usb_serial *serial;
195 if (port_paranoia_check (port, __FUNCTION__))
198 dbg("%s - port %d", __FUNCTION__, port->number);
200 serial = get_usb_serial (port, __FUNCTION__);
205 /* shutdown our bulk read */
206 usb_unlink_urb (port->read_urb);
208 /* Uncomment the following line if you want to see some statistics in your syslog */
209 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
213 static int empeg_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
215 struct usb_serial *serial = port->serial;
217 const unsigned char *current_position = buf;
224 dbg("%s - port %d", __FUNCTION__, port->number);
228 /* try to find a free urb in our list of them */
231 spin_lock_irqsave (&write_urb_pool_lock, flags);
233 for (i = 0; i < NUM_URBS; ++i) {
234 if (write_urb_pool[i]->status != -EINPROGRESS) {
235 urb = write_urb_pool[i];
240 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
243 dbg("%s - no more free urbs", __FUNCTION__);
247 if (urb->transfer_buffer == NULL) {
248 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
249 if (urb->transfer_buffer == NULL) {
250 dev_err(&port->dev, "%s no more kernel memory...\n", __FUNCTION__);
255 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
258 if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
259 bytes_sent = -EFAULT;
263 memcpy (urb->transfer_buffer, current_position, transfer_size);
266 usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
268 /* build up our urb */
272 usb_sndbulkpipe(serial->dev,
273 port->bulk_out_endpointAddress),
274 urb->transfer_buffer,
276 empeg_write_bulk_callback,
279 /* send it down the pipe */
280 status = usb_submit_urb(urb, GFP_ATOMIC);
282 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __FUNCTION__, status);
287 current_position += transfer_size;
288 bytes_sent += transfer_size;
289 count -= transfer_size;
290 bytes_out += transfer_size;
300 static int empeg_write_room (struct usb_serial_port *port)
306 dbg("%s - port %d", __FUNCTION__, port->number);
308 spin_lock_irqsave (&write_urb_pool_lock, flags);
310 /* tally up the number of bytes available */
311 for (i = 0; i < NUM_URBS; ++i) {
312 if (write_urb_pool[i]->status != -EINPROGRESS) {
313 room += URB_TRANSFER_BUFFER_SIZE;
317 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
319 dbg("%s - returns %d", __FUNCTION__, room);
326 static int empeg_chars_in_buffer (struct usb_serial_port *port)
332 dbg("%s - port %d", __FUNCTION__, port->number);
334 spin_lock_irqsave (&write_urb_pool_lock, flags);
336 /* tally up the number of bytes waiting */
337 for (i = 0; i < NUM_URBS; ++i) {
338 if (write_urb_pool[i]->status == -EINPROGRESS) {
339 chars += URB_TRANSFER_BUFFER_SIZE;
343 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
345 dbg("%s - returns %d", __FUNCTION__, chars);
352 static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
354 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
356 if (port_paranoia_check (port, __FUNCTION__))
359 dbg("%s - port %d", __FUNCTION__, port->number);
362 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
366 schedule_work(&port->work);
370 static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
372 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
373 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
374 struct tty_struct *tty;
375 unsigned char *data = urb->transfer_buffer;
379 if (port_paranoia_check (port, __FUNCTION__))
382 dbg("%s - port %d", __FUNCTION__, port->number);
385 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
390 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
394 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
398 if (urb->actual_length) {
399 for (i = 0; i < urb->actual_length ; ++i) {
401 * If we insert too many characters we'll overflow the buffer.
402 * This means we'll lose bytes - Decidedly bad.
404 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
405 tty_flip_buffer_push(tty);
407 tty_insert_flip_char(tty, data[i], 0);
410 * Goes straight through instead of scheduling - if tty->low_latency is set.
412 tty_flip_buffer_push(tty);
413 bytes_in += urb->actual_length;
416 /* Continue trying to always read */
420 usb_rcvbulkpipe(serial->dev,
421 port->bulk_in_endpointAddress),
422 port->read_urb->transfer_buffer,
423 port->read_urb->transfer_buffer_length,
424 empeg_read_bulk_callback,
427 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
430 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
437 static void empeg_throttle (struct usb_serial_port *port)
439 dbg("%s - port %d", __FUNCTION__, port->number);
440 usb_unlink_urb (port->read_urb);
444 static void empeg_unthrottle (struct usb_serial_port *port)
448 dbg("%s - port %d", __FUNCTION__, port->number);
450 port->read_urb->dev = port->serial->dev;
452 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
455 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
461 static int empeg_startup (struct usb_serial *serial)
465 dbg("%s", __FUNCTION__);
467 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
468 err("active config #%d != 1 ??",
469 serial->dev->actconfig->desc.bConfigurationValue);
472 dbg("%s - reset config", __FUNCTION__);
473 r = usb_reset_configuration (serial->dev);
475 /* continue on with initialization */
481 static void empeg_shutdown (struct usb_serial *serial)
483 dbg ("%s", __FUNCTION__);
487 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
489 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
495 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
498 dbg("%s - port %d", __FUNCTION__, port->number);
500 if ((!port->tty) || (!port->tty->termios)) {
501 dbg("%s - no tty structures", __FUNCTION__);
506 * The empeg-car player wants these particular tty settings.
507 * You could, for example, change the baud rate, however the
508 * player only supports 115200 (currently), so there is really
509 * no point in support for changes to the tty settings.
512 * The default requirements for this device are:
514 port->tty->termios->c_iflag
515 &= ~(IGNBRK /* disable ignore break */
516 | BRKINT /* disable break causes interrupt */
517 | PARMRK /* disable mark parity errors */
518 | ISTRIP /* disable clear high bit of input characters */
519 | INLCR /* disable translate NL to CR */
520 | IGNCR /* disable ignore CR */
521 | ICRNL /* disable translate CR to NL */
522 | IXON); /* disable enable XON/XOFF flow control */
524 port->tty->termios->c_oflag
525 &= ~OPOST; /* disable postprocess output characters */
527 port->tty->termios->c_lflag
528 &= ~(ECHO /* disable echo input characters */
529 | ECHONL /* disable echo new line */
530 | ICANON /* disable erase, kill, werase, and rprnt special characters */
531 | ISIG /* disable interrupt, quit, and suspend special characters */
532 | IEXTEN); /* disable non-POSIX special characters */
534 port->tty->termios->c_cflag
535 &= ~(CSIZE /* no size */
536 | PARENB /* disable parity bit */
537 | CBAUD); /* clear current baud rate */
539 port->tty->termios->c_cflag
540 |= (CS8 /* character size 8 bits */
541 | B115200); /* baud rate 115200 */
544 * Force low_latency on; otherwise the pushes are scheduled;
545 * this is bad as it opens up the possibility of dropping bytes
546 * on the floor. We don't want to drop bytes on the floor. :)
548 port->tty->low_latency = 1;
550 /* Notify the tty driver that the termios have changed. */
551 port->tty->ldisc.set_termios(port->tty, NULL);
558 static int __init empeg_init (void)
563 /* create our write urb pool and transfer buffers */
564 spin_lock_init (&write_urb_pool_lock);
565 for (i = 0; i < NUM_URBS; ++i) {
566 urb = usb_alloc_urb(0, GFP_KERNEL);
567 write_urb_pool[i] = urb;
569 err("No more urbs???");
573 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
574 if (!urb->transfer_buffer) {
575 err("%s - out of memory for urb buffers.",
581 retval = usb_serial_register(&empeg_device);
583 goto failed_usb_serial_register;
584 retval = usb_register(&empeg_driver);
586 goto failed_usb_register;
588 info(DRIVER_VERSION ":" DRIVER_DESC);
592 usb_serial_deregister(&empeg_device);
593 failed_usb_serial_register:
594 for (i = 0; i < NUM_URBS; ++i) {
595 if (write_urb_pool[i]) {
596 if (write_urb_pool[i]->transfer_buffer)
597 kfree(write_urb_pool[i]->transfer_buffer);
598 usb_free_urb(write_urb_pool[i]);
605 static void __exit empeg_exit (void)
610 usb_deregister(&empeg_driver);
611 usb_serial_deregister (&empeg_device);
613 spin_lock_irqsave (&write_urb_pool_lock, flags);
615 for (i = 0; i < NUM_URBS; ++i) {
616 if (write_urb_pool[i]) {
617 /* FIXME - uncomment the following usb_unlink_urb call when
618 * the host controllers get fixed to set urb->dev = NULL after
619 * the urb is finished. Otherwise this call oopses. */
620 /* usb_unlink_urb(write_urb_pool[i]); */
621 if (write_urb_pool[i]->transfer_buffer)
622 kfree(write_urb_pool[i]->transfer_buffer);
623 usb_free_urb (write_urb_pool[i]);
627 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
631 module_init(empeg_init);
632 module_exit(empeg_exit);
634 MODULE_AUTHOR( DRIVER_AUTHOR );
635 MODULE_DESCRIPTION( DRIVER_DESC );
636 MODULE_LICENSE("GPL");
638 MODULE_PARM(debug, "i");
639 MODULE_PARM_DESC(debug, "Debug enabled or not");