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 dbg("%s - port %d", __FUNCTION__, port->number);
162 /* Force default termio settings */
163 empeg_set_termios (port, NULL) ;
168 /* Start reading from the device */
172 usb_rcvbulkpipe(serial->dev,
173 port->bulk_in_endpointAddress),
174 port->read_urb->transfer_buffer,
175 port->read_urb->transfer_buffer_length,
176 empeg_read_bulk_callback,
179 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
182 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
188 static void empeg_close (struct usb_serial_port *port, struct file * filp)
190 dbg("%s - port %d", __FUNCTION__, port->number);
192 /* shutdown our bulk read */
193 usb_unlink_urb (port->read_urb);
194 /* Uncomment the following line if you want to see some statistics in your syslog */
195 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
199 static int empeg_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
201 struct usb_serial *serial = port->serial;
203 const unsigned char *current_position = buf;
210 dbg("%s - port %d", __FUNCTION__, port->number);
214 /* try to find a free urb in our list of them */
217 spin_lock_irqsave (&write_urb_pool_lock, flags);
219 for (i = 0; i < NUM_URBS; ++i) {
220 if (write_urb_pool[i]->status != -EINPROGRESS) {
221 urb = write_urb_pool[i];
226 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
229 dbg("%s - no more free urbs", __FUNCTION__);
233 if (urb->transfer_buffer == NULL) {
234 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
235 if (urb->transfer_buffer == NULL) {
236 dev_err(&port->dev, "%s no more kernel memory...\n", __FUNCTION__);
241 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
244 if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
245 bytes_sent = -EFAULT;
249 memcpy (urb->transfer_buffer, current_position, transfer_size);
252 usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
254 /* build up our urb */
258 usb_sndbulkpipe(serial->dev,
259 port->bulk_out_endpointAddress),
260 urb->transfer_buffer,
262 empeg_write_bulk_callback,
265 /* send it down the pipe */
266 status = usb_submit_urb(urb, GFP_ATOMIC);
268 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __FUNCTION__, status);
273 current_position += transfer_size;
274 bytes_sent += transfer_size;
275 count -= transfer_size;
276 bytes_out += transfer_size;
286 static int empeg_write_room (struct usb_serial_port *port)
292 dbg("%s - port %d", __FUNCTION__, port->number);
294 spin_lock_irqsave (&write_urb_pool_lock, flags);
296 /* tally up the number of bytes available */
297 for (i = 0; i < NUM_URBS; ++i) {
298 if (write_urb_pool[i]->status != -EINPROGRESS) {
299 room += URB_TRANSFER_BUFFER_SIZE;
303 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
305 dbg("%s - returns %d", __FUNCTION__, room);
312 static int empeg_chars_in_buffer (struct usb_serial_port *port)
318 dbg("%s - port %d", __FUNCTION__, port->number);
320 spin_lock_irqsave (&write_urb_pool_lock, flags);
322 /* tally up the number of bytes waiting */
323 for (i = 0; i < NUM_URBS; ++i) {
324 if (write_urb_pool[i]->status == -EINPROGRESS) {
325 chars += URB_TRANSFER_BUFFER_SIZE;
329 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
331 dbg("%s - returns %d", __FUNCTION__, chars);
338 static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
340 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
342 dbg("%s - port %d", __FUNCTION__, port->number);
345 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
349 schedule_work(&port->work);
353 static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
355 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
356 struct tty_struct *tty;
357 unsigned char *data = urb->transfer_buffer;
361 dbg("%s - port %d", __FUNCTION__, port->number);
364 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
368 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
372 if (urb->actual_length) {
373 for (i = 0; i < urb->actual_length ; ++i) {
375 * If we insert too many characters we'll overflow the buffer.
376 * This means we'll lose bytes - Decidedly bad.
378 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
379 tty_flip_buffer_push(tty);
381 tty_insert_flip_char(tty, data[i], 0);
384 * Goes straight through instead of scheduling - if tty->low_latency is set.
386 tty_flip_buffer_push(tty);
387 bytes_in += urb->actual_length;
390 /* Continue trying to always read */
394 usb_rcvbulkpipe(port->serial->dev,
395 port->bulk_in_endpointAddress),
396 port->read_urb->transfer_buffer,
397 port->read_urb->transfer_buffer_length,
398 empeg_read_bulk_callback,
401 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
404 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
411 static void empeg_throttle (struct usb_serial_port *port)
413 dbg("%s - port %d", __FUNCTION__, port->number);
414 usb_unlink_urb (port->read_urb);
418 static void empeg_unthrottle (struct usb_serial_port *port)
422 dbg("%s - port %d", __FUNCTION__, port->number);
424 port->read_urb->dev = port->serial->dev;
426 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
429 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
435 static int empeg_startup (struct usb_serial *serial)
439 dbg("%s", __FUNCTION__);
441 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
442 err("active config #%d != 1 ??",
443 serial->dev->actconfig->desc.bConfigurationValue);
446 dbg("%s - reset config", __FUNCTION__);
447 r = usb_reset_configuration (serial->dev);
449 /* continue on with initialization */
455 static void empeg_shutdown (struct usb_serial *serial)
457 dbg ("%s", __FUNCTION__);
461 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
463 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
469 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
472 dbg("%s - port %d", __FUNCTION__, port->number);
474 if ((!port->tty) || (!port->tty->termios)) {
475 dbg("%s - no tty structures", __FUNCTION__);
480 * The empeg-car player wants these particular tty settings.
481 * You could, for example, change the baud rate, however the
482 * player only supports 115200 (currently), so there is really
483 * no point in support for changes to the tty settings.
486 * The default requirements for this device are:
488 port->tty->termios->c_iflag
489 &= ~(IGNBRK /* disable ignore break */
490 | BRKINT /* disable break causes interrupt */
491 | PARMRK /* disable mark parity errors */
492 | ISTRIP /* disable clear high bit of input characters */
493 | INLCR /* disable translate NL to CR */
494 | IGNCR /* disable ignore CR */
495 | ICRNL /* disable translate CR to NL */
496 | IXON); /* disable enable XON/XOFF flow control */
498 port->tty->termios->c_oflag
499 &= ~OPOST; /* disable postprocess output characters */
501 port->tty->termios->c_lflag
502 &= ~(ECHO /* disable echo input characters */
503 | ECHONL /* disable echo new line */
504 | ICANON /* disable erase, kill, werase, and rprnt special characters */
505 | ISIG /* disable interrupt, quit, and suspend special characters */
506 | IEXTEN); /* disable non-POSIX special characters */
508 port->tty->termios->c_cflag
509 &= ~(CSIZE /* no size */
510 | PARENB /* disable parity bit */
511 | CBAUD); /* clear current baud rate */
513 port->tty->termios->c_cflag
514 |= (CS8 /* character size 8 bits */
515 | B115200); /* baud rate 115200 */
518 * Force low_latency on; otherwise the pushes are scheduled;
519 * this is bad as it opens up the possibility of dropping bytes
520 * on the floor. We don't want to drop bytes on the floor. :)
522 port->tty->low_latency = 1;
524 /* Notify the tty driver that the termios have changed. */
525 port->tty->ldisc.set_termios(port->tty, NULL);
532 static int __init empeg_init (void)
537 /* create our write urb pool and transfer buffers */
538 spin_lock_init (&write_urb_pool_lock);
539 for (i = 0; i < NUM_URBS; ++i) {
540 urb = usb_alloc_urb(0, GFP_KERNEL);
541 write_urb_pool[i] = urb;
543 err("No more urbs???");
547 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
548 if (!urb->transfer_buffer) {
549 err("%s - out of memory for urb buffers.",
555 retval = usb_serial_register(&empeg_device);
557 goto failed_usb_serial_register;
558 retval = usb_register(&empeg_driver);
560 goto failed_usb_register;
562 info(DRIVER_VERSION ":" DRIVER_DESC);
566 usb_serial_deregister(&empeg_device);
567 failed_usb_serial_register:
568 for (i = 0; i < NUM_URBS; ++i) {
569 if (write_urb_pool[i]) {
570 if (write_urb_pool[i]->transfer_buffer)
571 kfree(write_urb_pool[i]->transfer_buffer);
572 usb_free_urb(write_urb_pool[i]);
579 static void __exit empeg_exit (void)
584 usb_deregister(&empeg_driver);
585 usb_serial_deregister (&empeg_device);
587 spin_lock_irqsave (&write_urb_pool_lock, flags);
589 for (i = 0; i < NUM_URBS; ++i) {
590 if (write_urb_pool[i]) {
591 /* FIXME - uncomment the following usb_unlink_urb call when
592 * the host controllers get fixed to set urb->dev = NULL after
593 * the urb is finished. Otherwise this call oopses. */
594 /* usb_unlink_urb(write_urb_pool[i]); */
595 if (write_urb_pool[i]->transfer_buffer)
596 kfree(write_urb_pool[i]->transfer_buffer);
597 usb_free_urb (write_urb_pool[i]);
601 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
605 module_init(empeg_init);
606 module_exit(empeg_exit);
608 MODULE_AUTHOR( DRIVER_AUTHOR );
609 MODULE_DESCRIPTION( DRIVER_DESC );
610 MODULE_LICENSE("GPL");
612 MODULE_PARM(debug, "i");
613 MODULE_PARM_DESC(debug, "Debug enabled or not");