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
18 * remove unused code in empeg_close() (thanks to Oliver Neukum for
19 * pointing this out) and rewrote empeg_set_termios().
22 * switched from using spinlock to a semaphore, which fixes lots of
26 * Identify version on module load.
29 * Added write_room() and chars_in_buffer() support.
32 * Moved termio stuff inside the port->active check.
33 * Moved MOD_DEC_USE_COUNT to end of empeg_close().
36 * Added tty->ldisc.set_termios(port, tty, NULL) to empeg_open().
37 * This notifies the tty driver that the termios have changed.
40 * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to
41 * empeg_open() (It only needs to be set once - Doh!)
44 * Updated to work with id_table structure.
47 * Forked this from visor.c, and hacked it up to work with an
48 * Empeg ltd. empeg-car player. Constructive criticism welcomed.
49 * I would like to say, 'Thank You' to Greg Kroah-Hartman for the
50 * use of his code, and for his guidance, advice and patience. :)
51 * A 'Thank You' is in order for John Ripley of Empeg ltd for his
52 * advice, and patience too.
56 #include <linux/kernel.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
60 #include <linux/tty.h>
61 #include <linux/tty_driver.h>
62 #include <linux/tty_flip.h>
63 #include <linux/module.h>
64 #include <linux/spinlock.h>
65 #include <linux/uaccess.h>
66 #include <linux/usb.h>
67 #include <linux/usb/serial.h>
74 #define DRIVER_VERSION "v1.2"
75 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
76 #define DRIVER_DESC "USB Empeg Mark I/II Driver"
78 #define EMPEG_VENDOR_ID 0x084f
79 #define EMPEG_PRODUCT_ID 0x0001
81 /* function prototypes for an empeg-car player */
82 static int empeg_open(struct tty_struct
*tty
, struct usb_serial_port
*port
);
83 static void empeg_close(struct usb_serial_port
*port
);
84 static int empeg_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
85 const unsigned char *buf
,
87 static int empeg_write_room(struct tty_struct
*tty
);
88 static int empeg_chars_in_buffer(struct tty_struct
*tty
);
89 static void empeg_throttle(struct tty_struct
*tty
);
90 static void empeg_unthrottle(struct tty_struct
*tty
);
91 static int empeg_startup(struct usb_serial
*serial
);
92 static void empeg_init_termios(struct tty_struct
*tty
);
93 static void empeg_write_bulk_callback(struct urb
*urb
);
94 static void empeg_read_bulk_callback(struct urb
*urb
);
96 static struct usb_device_id id_table
[] = {
97 { USB_DEVICE(EMPEG_VENDOR_ID
, EMPEG_PRODUCT_ID
) },
98 { } /* Terminating entry */
101 MODULE_DEVICE_TABLE(usb
, id_table
);
103 static struct usb_driver empeg_driver
= {
105 .probe
= usb_serial_probe
,
106 .disconnect
= usb_serial_disconnect
,
107 .id_table
= id_table
,
111 static struct usb_serial_driver empeg_device
= {
113 .owner
= THIS_MODULE
,
116 .id_table
= id_table
,
117 .usb_driver
= &empeg_driver
,
120 .close
= empeg_close
,
121 .throttle
= empeg_throttle
,
122 .unthrottle
= empeg_unthrottle
,
123 .attach
= empeg_startup
,
124 .init_termios
= empeg_init_termios
,
125 .write
= empeg_write
,
126 .write_room
= empeg_write_room
,
127 .chars_in_buffer
= empeg_chars_in_buffer
,
128 .write_bulk_callback
= empeg_write_bulk_callback
,
129 .read_bulk_callback
= empeg_read_bulk_callback
,
133 #define URB_TRANSFER_BUFFER_SIZE 4096
135 static struct urb
*write_urb_pool
[NUM_URBS
];
136 static spinlock_t write_urb_pool_lock
;
138 static int bytes_out
;
140 /******************************************************************************
141 * Empeg specific driver functions
142 ******************************************************************************/
143 static int empeg_open(struct tty_struct
*tty
,struct usb_serial_port
*port
)
145 struct usb_serial
*serial
= port
->serial
;
148 dbg("%s - port %d", __func__
, port
->number
);
153 /* Start reading from the device */
157 usb_rcvbulkpipe(serial
->dev
,
158 port
->bulk_in_endpointAddress
),
159 port
->read_urb
->transfer_buffer
,
160 port
->read_urb
->transfer_buffer_length
,
161 empeg_read_bulk_callback
,
164 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
168 "%s - failed submitting read urb, error %d\n",
175 static void empeg_close(struct usb_serial_port
*port
)
177 dbg("%s - port %d", __func__
, port
->number
);
179 /* shutdown our bulk read */
180 usb_kill_urb(port
->read_urb
);
181 /* Uncomment the following line if you want to see some statistics in your syslog */
182 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
186 static int empeg_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
187 const unsigned char *buf
, int count
)
189 struct usb_serial
*serial
= port
->serial
;
191 const unsigned char *current_position
= buf
;
198 dbg("%s - port %d", __func__
, port
->number
);
201 /* try to find a free urb in our list of them */
204 spin_lock_irqsave(&write_urb_pool_lock
, flags
);
206 for (i
= 0; i
< NUM_URBS
; ++i
) {
207 if (write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
208 urb
= write_urb_pool
[i
];
213 spin_unlock_irqrestore(&write_urb_pool_lock
, flags
);
216 dbg("%s - no more free urbs", __func__
);
220 if (urb
->transfer_buffer
== NULL
) {
221 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_ATOMIC
);
222 if (urb
->transfer_buffer
== NULL
) {
224 "%s no more kernel memory...\n",
230 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
232 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
234 usb_serial_debug_data(debug
, &port
->dev
, __func__
, transfer_size
, urb
->transfer_buffer
);
236 /* build up our urb */
240 usb_sndbulkpipe(serial
->dev
,
241 port
->bulk_out_endpointAddress
),
242 urb
->transfer_buffer
,
244 empeg_write_bulk_callback
,
247 /* send it down the pipe */
248 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
250 dev_err(&port
->dev
, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__
, status
);
255 current_position
+= transfer_size
;
256 bytes_sent
+= transfer_size
;
257 count
-= transfer_size
;
258 bytes_out
+= transfer_size
;
266 static int empeg_write_room(struct tty_struct
*tty
)
268 struct usb_serial_port
*port
= tty
->driver_data
;
273 dbg("%s - port %d", __func__
, port
->number
);
275 spin_lock_irqsave(&write_urb_pool_lock
, flags
);
276 /* tally up the number of bytes available */
277 for (i
= 0; i
< NUM_URBS
; ++i
) {
278 if (write_urb_pool
[i
]->status
!= -EINPROGRESS
)
279 room
+= URB_TRANSFER_BUFFER_SIZE
;
281 spin_unlock_irqrestore(&write_urb_pool_lock
, flags
);
282 dbg("%s - returns %d", __func__
, room
);
288 static int empeg_chars_in_buffer(struct tty_struct
*tty
)
290 struct usb_serial_port
*port
= tty
->driver_data
;
295 dbg("%s - port %d", __func__
, port
->number
);
297 spin_lock_irqsave(&write_urb_pool_lock
, flags
);
299 /* tally up the number of bytes waiting */
300 for (i
= 0; i
< NUM_URBS
; ++i
) {
301 if (write_urb_pool
[i
]->status
== -EINPROGRESS
)
302 chars
+= URB_TRANSFER_BUFFER_SIZE
;
305 spin_unlock_irqrestore(&write_urb_pool_lock
, flags
);
306 dbg("%s - returns %d", __func__
, chars
);
311 static void empeg_write_bulk_callback(struct urb
*urb
)
313 struct usb_serial_port
*port
= urb
->context
;
314 int status
= urb
->status
;
316 dbg("%s - port %d", __func__
, port
->number
);
319 dbg("%s - nonzero write bulk status received: %d",
324 usb_serial_port_softint(port
);
328 static void empeg_read_bulk_callback(struct urb
*urb
)
330 struct usb_serial_port
*port
= urb
->context
;
331 struct tty_struct
*tty
;
332 unsigned char *data
= urb
->transfer_buffer
;
334 int status
= urb
->status
;
336 dbg("%s - port %d", __func__
, port
->number
);
339 dbg("%s - nonzero read bulk status received: %d",
344 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
345 urb
->actual_length
, data
);
346 tty
= tty_port_tty_get(&port
->port
);
348 if (urb
->actual_length
) {
349 tty_buffer_request_room(tty
, urb
->actual_length
);
350 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
351 tty_flip_buffer_push(tty
);
352 bytes_in
+= urb
->actual_length
;
356 /* Continue trying to always read */
360 usb_rcvbulkpipe(port
->serial
->dev
,
361 port
->bulk_in_endpointAddress
),
362 port
->read_urb
->transfer_buffer
,
363 port
->read_urb
->transfer_buffer_length
,
364 empeg_read_bulk_callback
,
367 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
370 dev_err(&urb
->dev
->dev
,
371 "%s - failed resubmitting read urb, error %d\n",
379 static void empeg_throttle(struct tty_struct
*tty
)
381 struct usb_serial_port
*port
= tty
->driver_data
;
382 dbg("%s - port %d", __func__
, port
->number
);
383 usb_kill_urb(port
->read_urb
);
387 static void empeg_unthrottle(struct tty_struct
*tty
)
389 struct usb_serial_port
*port
= tty
->driver_data
;
391 dbg("%s - port %d", __func__
, port
->number
);
393 port
->read_urb
->dev
= port
->serial
->dev
;
394 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
397 "%s - failed submitting read urb, error %d\n",
402 static int empeg_startup(struct usb_serial
*serial
)
408 if (serial
->dev
->actconfig
->desc
.bConfigurationValue
!= 1) {
409 dev_err(&serial
->dev
->dev
, "active config #%d != 1 ??\n",
410 serial
->dev
->actconfig
->desc
.bConfigurationValue
);
413 dbg("%s - reset config", __func__
);
414 r
= usb_reset_configuration(serial
->dev
);
416 /* continue on with initialization */
422 static void empeg_init_termios(struct tty_struct
*tty
)
424 struct ktermios
*termios
= tty
->termios
;
427 * The empeg-car player wants these particular tty settings.
428 * You could, for example, change the baud rate, however the
429 * player only supports 115200 (currently), so there is really
430 * no point in support for changes to the tty settings.
433 * The default requirements for this device are:
436 &= ~(IGNBRK
/* disable ignore break */
437 | BRKINT
/* disable break causes interrupt */
438 | PARMRK
/* disable mark parity errors */
439 | ISTRIP
/* disable clear high bit of input characters */
440 | INLCR
/* disable translate NL to CR */
441 | IGNCR
/* disable ignore CR */
442 | ICRNL
/* disable translate CR to NL */
443 | IXON
); /* disable enable XON/XOFF flow control */
446 &= ~OPOST
; /* disable postprocess output characters */
449 &= ~(ECHO
/* disable echo input characters */
450 | ECHONL
/* disable echo new line */
451 | ICANON
/* disable erase, kill, werase, and rprnt special characters */
452 | ISIG
/* disable interrupt, quit, and suspend special characters */
453 | IEXTEN
); /* disable non-POSIX special characters */
456 &= ~(CSIZE
/* no size */
457 | PARENB
/* disable parity bit */
458 | CBAUD
); /* clear current baud rate */
461 |= CS8
; /* character size 8 bits */
463 tty_encode_baud_rate(tty
, 115200, 115200);
467 static int __init
empeg_init(void)
472 /* create our write urb pool and transfer buffers */
473 spin_lock_init(&write_urb_pool_lock
);
474 for (i
= 0; i
< NUM_URBS
; ++i
) {
475 urb
= usb_alloc_urb(0, GFP_KERNEL
);
476 write_urb_pool
[i
] = urb
;
478 printk(KERN_ERR
"empeg: No more urbs???\n");
482 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
484 if (!urb
->transfer_buffer
) {
485 printk(KERN_ERR
"empeg: %s - out of memory for urb "
486 "buffers.", __func__
);
491 retval
= usb_serial_register(&empeg_device
);
493 goto failed_usb_serial_register
;
494 retval
= usb_register(&empeg_driver
);
496 goto failed_usb_register
;
498 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
503 usb_serial_deregister(&empeg_device
);
504 failed_usb_serial_register
:
505 for (i
= 0; i
< NUM_URBS
; ++i
) {
506 if (write_urb_pool
[i
]) {
507 kfree(write_urb_pool
[i
]->transfer_buffer
);
508 usb_free_urb(write_urb_pool
[i
]);
515 static void __exit
empeg_exit(void)
520 usb_deregister(&empeg_driver
);
521 usb_serial_deregister(&empeg_device
);
523 spin_lock_irqsave(&write_urb_pool_lock
, flags
);
525 for (i
= 0; i
< NUM_URBS
; ++i
) {
526 if (write_urb_pool
[i
]) {
527 /* FIXME - uncomment the following usb_kill_urb call
528 * when the host controllers get fixed to set urb->dev
529 * = NULL after the urb is finished. Otherwise this
531 /* usb_kill_urb(write_urb_pool[i]); */
532 kfree(write_urb_pool
[i
]->transfer_buffer
);
533 usb_free_urb(write_urb_pool
[i
]);
536 spin_unlock_irqrestore(&write_urb_pool_lock
, flags
);
540 module_init(empeg_init
);
541 module_exit(empeg_exit
);
543 MODULE_AUTHOR(DRIVER_AUTHOR
);
544 MODULE_DESCRIPTION(DRIVER_DESC
);
545 MODULE_LICENSE("GPL");
547 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
548 MODULE_PARM_DESC(debug
, "Debug enabled or not");