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/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>
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
,
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
,
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
= {
108 .probe
= usb_serial_probe
,
109 .disconnect
= usb_serial_disconnect
,
110 .id_table
= id_table
,
114 static struct usb_serial_driver empeg_device
= {
116 .owner
= THIS_MODULE
,
119 .id_table
= id_table
,
120 .usb_driver
= &empeg_driver
,
123 .close
= empeg_close
,
124 .throttle
= empeg_throttle
,
125 .unthrottle
= empeg_unthrottle
,
126 .attach
= empeg_startup
,
127 .shutdown
= empeg_shutdown
,
128 .ioctl
= empeg_ioctl
,
129 .set_termios
= empeg_set_termios
,
130 .write
= empeg_write
,
131 .write_room
= empeg_write_room
,
132 .chars_in_buffer
= empeg_chars_in_buffer
,
133 .write_bulk_callback
= empeg_write_bulk_callback
,
134 .read_bulk_callback
= empeg_read_bulk_callback
,
138 #define URB_TRANSFER_BUFFER_SIZE 4096
140 static struct urb
*write_urb_pool
[NUM_URBS
];
141 static spinlock_t write_urb_pool_lock
;
143 static int bytes_out
;
145 /******************************************************************************
146 * Empeg specific driver functions
147 ******************************************************************************/
148 static int empeg_open (struct usb_serial_port
*port
, struct file
*filp
)
150 struct usb_serial
*serial
= port
->serial
;
153 dbg("%s - port %d", __func__
, port
->number
);
155 /* Force default termio settings */
156 empeg_set_termios (port
, NULL
) ;
161 /* Start reading from the device */
165 usb_rcvbulkpipe(serial
->dev
,
166 port
->bulk_in_endpointAddress
),
167 port
->read_urb
->transfer_buffer
,
168 port
->read_urb
->transfer_buffer_length
,
169 empeg_read_bulk_callback
,
172 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
175 dev_err(&port
->dev
, "%s - failed submitting read urb, error %d\n", __func__
, result
);
181 static void empeg_close (struct usb_serial_port
*port
, struct file
* filp
)
183 dbg("%s - port %d", __func__
, port
->number
);
185 /* shutdown our bulk read */
186 usb_kill_urb(port
->read_urb
);
187 /* Uncomment the following line if you want to see some statistics in your syslog */
188 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
192 static int empeg_write (struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
194 struct usb_serial
*serial
= port
->serial
;
196 const unsigned char *current_position
= buf
;
203 dbg("%s - port %d", __func__
, port
->number
);
207 /* try to find a free urb in our list of them */
210 spin_lock_irqsave (&write_urb_pool_lock
, flags
);
212 for (i
= 0; i
< NUM_URBS
; ++i
) {
213 if (write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
214 urb
= write_urb_pool
[i
];
219 spin_unlock_irqrestore (&write_urb_pool_lock
, flags
);
222 dbg("%s - no more free urbs", __func__
);
226 if (urb
->transfer_buffer
== NULL
) {
227 urb
->transfer_buffer
= kmalloc (URB_TRANSFER_BUFFER_SIZE
, GFP_ATOMIC
);
228 if (urb
->transfer_buffer
== NULL
) {
229 dev_err(&port
->dev
, "%s no more kernel memory...\n", __func__
);
234 transfer_size
= min (count
, URB_TRANSFER_BUFFER_SIZE
);
236 memcpy (urb
->transfer_buffer
, current_position
, transfer_size
);
238 usb_serial_debug_data(debug
, &port
->dev
, __func__
, transfer_size
, urb
->transfer_buffer
);
240 /* build up our urb */
244 usb_sndbulkpipe(serial
->dev
,
245 port
->bulk_out_endpointAddress
),
246 urb
->transfer_buffer
,
248 empeg_write_bulk_callback
,
251 /* send it down the pipe */
252 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
254 dev_err(&port
->dev
, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__
, status
);
259 current_position
+= transfer_size
;
260 bytes_sent
+= transfer_size
;
261 count
-= transfer_size
;
262 bytes_out
+= transfer_size
;
272 static int empeg_write_room (struct usb_serial_port
*port
)
278 dbg("%s - port %d", __func__
, port
->number
);
280 spin_lock_irqsave (&write_urb_pool_lock
, flags
);
282 /* tally up the number of bytes available */
283 for (i
= 0; i
< NUM_URBS
; ++i
) {
284 if (write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
285 room
+= URB_TRANSFER_BUFFER_SIZE
;
289 spin_unlock_irqrestore (&write_urb_pool_lock
, flags
);
291 dbg("%s - returns %d", __func__
, room
);
298 static int empeg_chars_in_buffer (struct usb_serial_port
*port
)
304 dbg("%s - port %d", __func__
, port
->number
);
306 spin_lock_irqsave (&write_urb_pool_lock
, flags
);
308 /* tally up the number of bytes waiting */
309 for (i
= 0; i
< NUM_URBS
; ++i
) {
310 if (write_urb_pool
[i
]->status
== -EINPROGRESS
) {
311 chars
+= URB_TRANSFER_BUFFER_SIZE
;
315 spin_unlock_irqrestore (&write_urb_pool_lock
, flags
);
317 dbg("%s - returns %d", __func__
, chars
);
324 static void empeg_write_bulk_callback (struct urb
*urb
)
326 struct usb_serial_port
*port
= urb
->context
;
327 int status
= urb
->status
;
329 dbg("%s - port %d", __func__
, port
->number
);
332 dbg("%s - nonzero write bulk status received: %d",
337 usb_serial_port_softint(port
);
341 static void empeg_read_bulk_callback (struct urb
*urb
)
343 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
344 struct tty_struct
*tty
;
345 unsigned char *data
= urb
->transfer_buffer
;
347 int status
= urb
->status
;
349 dbg("%s - port %d", __func__
, port
->number
);
352 dbg("%s - nonzero read bulk status received: %d",
357 usb_serial_debug_data(debug
, &port
->dev
, __func__
, urb
->actual_length
, data
);
361 if (urb
->actual_length
) {
362 tty_buffer_request_room(tty
, urb
->actual_length
);
363 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
364 tty_flip_buffer_push(tty
);
365 bytes_in
+= urb
->actual_length
;
368 /* Continue trying to always read */
372 usb_rcvbulkpipe(port
->serial
->dev
,
373 port
->bulk_in_endpointAddress
),
374 port
->read_urb
->transfer_buffer
,
375 port
->read_urb
->transfer_buffer_length
,
376 empeg_read_bulk_callback
,
379 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
382 dev_err(&urb
->dev
->dev
, "%s - failed resubmitting read urb, error %d\n", __func__
, result
);
389 static void empeg_throttle (struct usb_serial_port
*port
)
391 dbg("%s - port %d", __func__
, port
->number
);
392 usb_kill_urb(port
->read_urb
);
396 static void empeg_unthrottle (struct usb_serial_port
*port
)
400 dbg("%s - port %d", __func__
, port
->number
);
402 port
->read_urb
->dev
= port
->serial
->dev
;
404 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
407 dev_err(&port
->dev
, "%s - failed submitting read urb, error %d\n", __func__
, result
);
413 static int empeg_startup (struct usb_serial
*serial
)
419 if (serial
->dev
->actconfig
->desc
.bConfigurationValue
!= 1) {
420 err("active config #%d != 1 ??",
421 serial
->dev
->actconfig
->desc
.bConfigurationValue
);
424 dbg("%s - reset config", __func__
);
425 r
= usb_reset_configuration (serial
->dev
);
427 /* continue on with initialization */
433 static void empeg_shutdown (struct usb_serial
*serial
)
435 dbg ("%s", __func__
);
439 static int empeg_ioctl (struct usb_serial_port
*port
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
441 dbg("%s - port %d, cmd 0x%.4x", __func__
, port
->number
, cmd
);
447 static void empeg_set_termios (struct usb_serial_port
*port
, struct ktermios
*old_termios
)
449 struct ktermios
*termios
= port
->tty
->termios
;
450 dbg("%s - port %d", __func__
, port
->number
);
453 * The empeg-car player wants these particular tty settings.
454 * You could, for example, change the baud rate, however the
455 * player only supports 115200 (currently), so there is really
456 * no point in support for changes to the tty settings.
459 * The default requirements for this device are:
462 &= ~(IGNBRK
/* disable ignore break */
463 | BRKINT
/* disable break causes interrupt */
464 | PARMRK
/* disable mark parity errors */
465 | ISTRIP
/* disable clear high bit of input characters */
466 | INLCR
/* disable translate NL to CR */
467 | IGNCR
/* disable ignore CR */
468 | ICRNL
/* disable translate CR to NL */
469 | IXON
); /* disable enable XON/XOFF flow control */
472 &= ~OPOST
; /* disable postprocess output characters */
475 &= ~(ECHO
/* disable echo input characters */
476 | ECHONL
/* disable echo new line */
477 | ICANON
/* disable erase, kill, werase, and rprnt special characters */
478 | ISIG
/* disable interrupt, quit, and suspend special characters */
479 | IEXTEN
); /* disable non-POSIX special characters */
482 &= ~(CSIZE
/* no size */
483 | PARENB
/* disable parity bit */
484 | CBAUD
); /* clear current baud rate */
487 |= CS8
; /* character size 8 bits */
490 * Force low_latency on; otherwise the pushes are scheduled;
491 * this is bad as it opens up the possibility of dropping bytes
492 * on the floor. We don't want to drop bytes on the floor. :)
494 port
->tty
->low_latency
= 1;
495 tty_encode_baud_rate(port
->tty
, 115200, 115200);
499 static int __init
empeg_init (void)
504 /* create our write urb pool and transfer buffers */
505 spin_lock_init (&write_urb_pool_lock
);
506 for (i
= 0; i
< NUM_URBS
; ++i
) {
507 urb
= usb_alloc_urb(0, GFP_KERNEL
);
508 write_urb_pool
[i
] = urb
;
510 err("No more urbs???");
514 urb
->transfer_buffer
= kmalloc (URB_TRANSFER_BUFFER_SIZE
, GFP_KERNEL
);
515 if (!urb
->transfer_buffer
) {
516 err("%s - out of memory for urb buffers.",
522 retval
= usb_serial_register(&empeg_device
);
524 goto failed_usb_serial_register
;
525 retval
= usb_register(&empeg_driver
);
527 goto failed_usb_register
;
529 info(DRIVER_VERSION
":" DRIVER_DESC
);
533 usb_serial_deregister(&empeg_device
);
534 failed_usb_serial_register
:
535 for (i
= 0; i
< NUM_URBS
; ++i
) {
536 if (write_urb_pool
[i
]) {
537 kfree(write_urb_pool
[i
]->transfer_buffer
);
538 usb_free_urb(write_urb_pool
[i
]);
545 static void __exit
empeg_exit (void)
550 usb_deregister(&empeg_driver
);
551 usb_serial_deregister (&empeg_device
);
553 spin_lock_irqsave (&write_urb_pool_lock
, flags
);
555 for (i
= 0; i
< NUM_URBS
; ++i
) {
556 if (write_urb_pool
[i
]) {
557 /* FIXME - uncomment the following usb_kill_urb call when
558 * the host controllers get fixed to set urb->dev = NULL after
559 * the urb is finished. Otherwise this call oopses. */
560 /* usb_kill_urb(write_urb_pool[i]); */
561 kfree(write_urb_pool
[i
]->transfer_buffer
);
562 usb_free_urb (write_urb_pool
[i
]);
566 spin_unlock_irqrestore (&write_urb_pool_lock
, flags
);
570 module_init(empeg_init
);
571 module_exit(empeg_exit
);
573 MODULE_AUTHOR( DRIVER_AUTHOR
);
574 MODULE_DESCRIPTION( DRIVER_DESC
);
575 MODULE_LICENSE("GPL");
577 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
578 MODULE_PARM_DESC(debug
, "Debug enabled or not");