blob: 3e6c4ba7667b1eba0d86d24e9900ce87f9e5659a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Empeg empeg-car player driver
3 *
4 * Copyright (C) 2000, 2001
5 * Gary Brubaker (xavyer@ix.netcom.com)
6 *
7 * Copyright (C) 1999 - 2001
8 * Greg Kroah-Hartman (greg@kroah.com)
9 *
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.
13 *
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
15 *
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().
19 *
20 * (05/30/2001) gkh
21 * switched from using spinlock to a semaphore, which fixes lots of problems.
22 *
23 * (04/08/2001) gb
24 * Identify version on module load.
25 *
26 * (01/22/2001) gb
27 * Added write_room() and chars_in_buffer() support.
28 *
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().
32 *
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.
36 *
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!)
40 *
41 * (11/11/2000) gb
42 * Updated to work with id_table structure.
43 *
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.
51 *
52 */
53
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>
66#include "usb-serial.h"
67
68static int debug;
69
70/*
71 * Version Information
72 */
73#define DRIVER_VERSION "v1.2"
74#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
75#define DRIVER_DESC "USB Empeg Mark I/II Driver"
76
77#define EMPEG_VENDOR_ID 0x084f
78#define EMPEG_PRODUCT_ID 0x0001
79
80/* function prototypes for an empeg-car player */
81static int empeg_open (struct usb_serial_port *port, struct file *filp);
82static void empeg_close (struct usb_serial_port *port, struct file *filp);
83static int empeg_write (struct usb_serial_port *port,
84 const unsigned char *buf,
85 int count);
86static int empeg_write_room (struct usb_serial_port *port);
87static int empeg_chars_in_buffer (struct usb_serial_port *port);
88static void empeg_throttle (struct usb_serial_port *port);
89static void empeg_unthrottle (struct usb_serial_port *port);
90static int empeg_startup (struct usb_serial *serial);
91static void empeg_shutdown (struct usb_serial *serial);
92static int empeg_ioctl (struct usb_serial_port *port,
93 struct file * file,
94 unsigned int cmd,
95 unsigned long arg);
96static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios);
97static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
98static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
99
100static struct usb_device_id id_table [] = {
101 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
102 { } /* Terminating entry */
103};
104
105MODULE_DEVICE_TABLE (usb, id_table);
106
107static struct usb_driver empeg_driver = {
108 .owner = THIS_MODULE,
109 .name = "empeg",
110 .probe = usb_serial_probe,
111 .disconnect = usb_serial_disconnect,
112 .id_table = id_table,
113};
114
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700115static struct usb_serial_driver empeg_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700116 .driver = {
117 .owner = THIS_MODULE,
118 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .name = "Empeg",
120 .id_table = id_table,
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,
138};
139
140#define NUM_URBS 16
141#define URB_TRANSFER_BUFFER_SIZE 4096
142
143static struct urb *write_urb_pool[NUM_URBS];
144static spinlock_t write_urb_pool_lock;
145static int bytes_in;
146static int bytes_out;
147
148/******************************************************************************
149 * Empeg specific driver functions
150 ******************************************************************************/
151static int empeg_open (struct usb_serial_port *port, struct file *filp)
152{
153 struct usb_serial *serial = port->serial;
154 int result = 0;
155
156 dbg("%s - port %d", __FUNCTION__, port->number);
157
158 /* Force default termio settings */
159 empeg_set_termios (port, NULL) ;
160
161 bytes_in = 0;
162 bytes_out = 0;
163
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);
174
175 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
176
177 if (result)
178 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
179
180 return result;
181}
182
183
184static void empeg_close (struct usb_serial_port *port, struct file * filp)
185{
186 dbg("%s - port %d", __FUNCTION__, port->number);
187
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); */
192}
193
194
195static int empeg_write (struct usb_serial_port *port, const unsigned char *buf, int count)
196{
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;
205
206 dbg("%s - port %d", __FUNCTION__, port->number);
207
208 while (count > 0) {
209
210 /* try to find a free urb in our list of them */
211 urb = NULL;
212
213 spin_lock_irqsave (&write_urb_pool_lock, flags);
214
215 for (i = 0; i < NUM_URBS; ++i) {
216 if (write_urb_pool[i]->status != -EINPROGRESS) {
217 urb = write_urb_pool[i];
218 break;
219 }
220 }
221
222 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
223
224 if (urb == NULL) {
225 dbg("%s - no more free urbs", __FUNCTION__);
226 goto exit;
227 }
228
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;
234 }
235 }
236
237 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
238
239 memcpy (urb->transfer_buffer, current_position, transfer_size);
240
241 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size, urb->transfer_buffer);
242
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);
253
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;
260 }
261
262 current_position += transfer_size;
263 bytes_sent += transfer_size;
264 count -= transfer_size;
265 bytes_out += transfer_size;
266
267 }
268
269exit:
270 return bytes_sent;
271
272}
273
274
275static int empeg_write_room (struct usb_serial_port *port)
276{
277 unsigned long flags;
278 int i;
279 int room = 0;
280
281 dbg("%s - port %d", __FUNCTION__, port->number);
282
283 spin_lock_irqsave (&write_urb_pool_lock, flags);
284
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;
289 }
290 }
291
292 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
293
294 dbg("%s - returns %d", __FUNCTION__, room);
295
296 return (room);
297
298}
299
300
301static int empeg_chars_in_buffer (struct usb_serial_port *port)
302{
303 unsigned long flags;
304 int i;
305 int chars = 0;
306
307 dbg("%s - port %d", __FUNCTION__, port->number);
308
309 spin_lock_irqsave (&write_urb_pool_lock, flags);
310
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;
315 }
316 }
317
318 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
319
320 dbg("%s - returns %d", __FUNCTION__, chars);
321
322 return (chars);
323
324}
325
326
327static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
328{
329 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
330
331 dbg("%s - port %d", __FUNCTION__, port->number);
332
333 if (urb->status) {
334 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
335 return;
336 }
337
338 schedule_work(&port->work);
339}
340
341
342static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
343{
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 i;
348 int result;
349
350 dbg("%s - port %d", __FUNCTION__, port->number);
351
352 if (urb->status) {
353 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
354 return;
355 }
356
357 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
358
359 tty = port->tty;
360
361 if (urb->actual_length) {
362 for (i = 0; i < urb->actual_length ; ++i) {
363 /* gb - 2000/11/13
364 * If we insert too many characters we'll overflow the buffer.
365 * This means we'll lose bytes - Decidedly bad.
366 */
367 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
368 tty_flip_buffer_push(tty);
369 }
370 tty_insert_flip_char(tty, data[i], 0);
371 }
372 /* gb - 2000/11/13
373 * Goes straight through instead of scheduling - if tty->low_latency is set.
374 */
375 tty_flip_buffer_push(tty);
376 bytes_in += urb->actual_length;
377 }
378
379 /* Continue trying to always read */
380 usb_fill_bulk_urb(
381 port->read_urb,
382 port->serial->dev,
383 usb_rcvbulkpipe(port->serial->dev,
384 port->bulk_in_endpointAddress),
385 port->read_urb->transfer_buffer,
386 port->read_urb->transfer_buffer_length,
387 empeg_read_bulk_callback,
388 port);
389
390 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
391
392 if (result)
393 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
394
395 return;
396
397}
398
399
400static void empeg_throttle (struct usb_serial_port *port)
401{
402 dbg("%s - port %d", __FUNCTION__, port->number);
403 usb_kill_urb(port->read_urb);
404}
405
406
407static void empeg_unthrottle (struct usb_serial_port *port)
408{
409 int result;
410
411 dbg("%s - port %d", __FUNCTION__, port->number);
412
413 port->read_urb->dev = port->serial->dev;
414
415 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
416
417 if (result)
418 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
419
420 return;
421}
422
423
424static int empeg_startup (struct usb_serial *serial)
425{
426 int r;
427
428 dbg("%s", __FUNCTION__);
429
430 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
431 err("active config #%d != 1 ??",
432 serial->dev->actconfig->desc.bConfigurationValue);
433 return -ENODEV;
434 }
435 dbg("%s - reset config", __FUNCTION__);
436 r = usb_reset_configuration (serial->dev);
437
438 /* continue on with initialization */
439 return r;
440
441}
442
443
444static void empeg_shutdown (struct usb_serial *serial)
445{
446 dbg ("%s", __FUNCTION__);
447}
448
449
450static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
451{
452 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
453
454 return -ENOIOCTLCMD;
455}
456
457
458static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
459{
460
461 dbg("%s - port %d", __FUNCTION__, port->number);
462
463 if ((!port->tty) || (!port->tty->termios)) {
464 dbg("%s - no tty structures", __FUNCTION__);
465 return;
466 }
467
468 /*
469 * The empeg-car player wants these particular tty settings.
470 * You could, for example, change the baud rate, however the
471 * player only supports 115200 (currently), so there is really
472 * no point in support for changes to the tty settings.
473 * (at least for now)
474 *
475 * The default requirements for this device are:
476 */
477 port->tty->termios->c_iflag
478 &= ~(IGNBRK /* disable ignore break */
479 | BRKINT /* disable break causes interrupt */
480 | PARMRK /* disable mark parity errors */
481 | ISTRIP /* disable clear high bit of input characters */
482 | INLCR /* disable translate NL to CR */
483 | IGNCR /* disable ignore CR */
484 | ICRNL /* disable translate CR to NL */
485 | IXON); /* disable enable XON/XOFF flow control */
486
487 port->tty->termios->c_oflag
488 &= ~OPOST; /* disable postprocess output characters */
489
490 port->tty->termios->c_lflag
491 &= ~(ECHO /* disable echo input characters */
492 | ECHONL /* disable echo new line */
493 | ICANON /* disable erase, kill, werase, and rprnt special characters */
494 | ISIG /* disable interrupt, quit, and suspend special characters */
495 | IEXTEN); /* disable non-POSIX special characters */
496
497 port->tty->termios->c_cflag
498 &= ~(CSIZE /* no size */
499 | PARENB /* disable parity bit */
500 | CBAUD); /* clear current baud rate */
501
502 port->tty->termios->c_cflag
503 |= (CS8 /* character size 8 bits */
504 | B115200); /* baud rate 115200 */
505
506 /*
507 * Force low_latency on; otherwise the pushes are scheduled;
508 * this is bad as it opens up the possibility of dropping bytes
509 * on the floor. We don't want to drop bytes on the floor. :)
510 */
511 port->tty->low_latency = 1;
512
513 return;
514}
515
516
517static int __init empeg_init (void)
518{
519 struct urb *urb;
520 int i, retval;
521
522 /* create our write urb pool and transfer buffers */
523 spin_lock_init (&write_urb_pool_lock);
524 for (i = 0; i < NUM_URBS; ++i) {
525 urb = usb_alloc_urb(0, GFP_KERNEL);
526 write_urb_pool[i] = urb;
527 if (urb == NULL) {
528 err("No more urbs???");
529 continue;
530 }
531
532 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
533 if (!urb->transfer_buffer) {
534 err("%s - out of memory for urb buffers.",
535 __FUNCTION__);
536 continue;
537 }
538 }
539
540 retval = usb_serial_register(&empeg_device);
541 if (retval)
542 goto failed_usb_serial_register;
543 retval = usb_register(&empeg_driver);
544 if (retval)
545 goto failed_usb_register;
546
547 info(DRIVER_VERSION ":" DRIVER_DESC);
548
549 return 0;
550failed_usb_register:
551 usb_serial_deregister(&empeg_device);
552failed_usb_serial_register:
553 for (i = 0; i < NUM_URBS; ++i) {
554 if (write_urb_pool[i]) {
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700555 kfree(write_urb_pool[i]->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 usb_free_urb(write_urb_pool[i]);
557 }
558 }
559 return retval;
560}
561
562
563static void __exit empeg_exit (void)
564{
565 int i;
566 unsigned long flags;
567
568 usb_deregister(&empeg_driver);
569 usb_serial_deregister (&empeg_device);
570
571 spin_lock_irqsave (&write_urb_pool_lock, flags);
572
573 for (i = 0; i < NUM_URBS; ++i) {
574 if (write_urb_pool[i]) {
575 /* FIXME - uncomment the following usb_kill_urb call when
576 * the host controllers get fixed to set urb->dev = NULL after
577 * the urb is finished. Otherwise this call oopses. */
578 /* usb_kill_urb(write_urb_pool[i]); */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700579 kfree(write_urb_pool[i]->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 usb_free_urb (write_urb_pool[i]);
581 }
582 }
583
584 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
585}
586
587
588module_init(empeg_init);
589module_exit(empeg_exit);
590
591MODULE_AUTHOR( DRIVER_AUTHOR );
592MODULE_DESCRIPTION( DRIVER_DESC );
593MODULE_LICENSE("GPL");
594
595module_param(debug, bool, S_IRUGO | S_IWUSR);
596MODULE_PARM_DESC(debug, "Debug enabled or not");