tty: USB serial termios bits
[linux-2.6-block.git] / drivers / usb / serial / empeg.c
CommitLineData
1da177e4
LT
1/*
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 *
93c46795
AC
14 * See Documentation/usb/usb-serial.txt for more information on using this
15 * driver
16 *
1da177e4 17 * (07/16/2001) gb
93c46795
AC
18 * remove unused code in empeg_close() (thanks to Oliver Neukum for
19 * pointing this out) and rewrote empeg_set_termios().
20 *
1da177e4 21 * (05/30/2001) gkh
93c46795
AC
22 * switched from using spinlock to a semaphore, which fixes lots of
23 * problems.
1da177e4
LT
24 *
25 * (04/08/2001) gb
26 * Identify version on module load.
93c46795 27 *
1da177e4 28 * (01/22/2001) gb
93c46795
AC
29 * Added write_room() and chars_in_buffer() support.
30 *
1da177e4
LT
31 * (12/21/2000) gb
32 * Moved termio stuff inside the port->active check.
33 * Moved MOD_DEC_USE_COUNT to end of empeg_close().
93c46795 34 *
1da177e4 35 * (12/03/2000) gb
4a90f09b
AC
36 * Added tty->ldisc.set_termios(port, tty, NULL) to empeg_open().
37 * This notifies the tty driver that the termios have changed.
93c46795 38 *
1da177e4 39 * (11/13/2000) gb
93c46795
AC
40 * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to
41 * empeg_open() (It only needs to be set once - Doh!)
42 *
1da177e4
LT
43 * (11/11/2000) gb
44 * Updated to work with id_table structure.
93c46795 45 *
1da177e4
LT
46 * (11/04/2000) gb
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.
93c46795 53 *
1da177e4
LT
54 */
55
1da177e4
LT
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>
93c46795 65#include <linux/uaccess.h>
1da177e4 66#include <linux/usb.h>
a969888c 67#include <linux/usb/serial.h>
1da177e4
LT
68
69static int debug;
70
71/*
72 * Version Information
73 */
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"
77
78#define EMPEG_VENDOR_ID 0x084f
79#define EMPEG_PRODUCT_ID 0x0001
80
81/* function prototypes for an empeg-car player */
a509a7e4 82static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port);
335f8514 83static void empeg_close(struct usb_serial_port *port);
93c46795
AC
84static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
85 const unsigned char *buf,
86 int count);
87static int empeg_write_room(struct tty_struct *tty);
88static int empeg_chars_in_buffer(struct tty_struct *tty);
89static void empeg_throttle(struct tty_struct *tty);
90static void empeg_unthrottle(struct tty_struct *tty);
91static int empeg_startup(struct usb_serial *serial);
fe1ae7fd 92static void empeg_init_termios(struct tty_struct *tty);
93c46795
AC
93static void empeg_write_bulk_callback(struct urb *urb);
94static void empeg_read_bulk_callback(struct urb *urb);
1da177e4
LT
95
96static struct usb_device_id id_table [] = {
97 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
98 { } /* Terminating entry */
99};
100
93c46795 101MODULE_DEVICE_TABLE(usb, id_table);
1da177e4
LT
102
103static struct usb_driver empeg_driver = {
1da177e4
LT
104 .name = "empeg",
105 .probe = usb_serial_probe,
106 .disconnect = usb_serial_disconnect,
107 .id_table = id_table,
ba9dc657 108 .no_dynamic_id = 1,
1da177e4
LT
109};
110
ea65370d 111static struct usb_serial_driver empeg_device = {
18fcac35
GKH
112 .driver = {
113 .owner = THIS_MODULE,
269bda1c 114 .name = "empeg",
18fcac35 115 },
1da177e4 116 .id_table = id_table,
d9b1b787 117 .usb_driver = &empeg_driver,
1da177e4
LT
118 .num_ports = 1,
119 .open = empeg_open,
120 .close = empeg_close,
121 .throttle = empeg_throttle,
122 .unthrottle = empeg_unthrottle,
123 .attach = empeg_startup,
fe1ae7fd 124 .init_termios = empeg_init_termios,
1da177e4
LT
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,
130};
131
132#define NUM_URBS 16
133#define URB_TRANSFER_BUFFER_SIZE 4096
134
135static struct urb *write_urb_pool[NUM_URBS];
136static spinlock_t write_urb_pool_lock;
137static int bytes_in;
138static int bytes_out;
139
140/******************************************************************************
141 * Empeg specific driver functions
142 ******************************************************************************/
a509a7e4 143static int empeg_open(struct tty_struct *tty,struct usb_serial_port *port)
1da177e4
LT
144{
145 struct usb_serial *serial = port->serial;
146 int result = 0;
147
441b62c1 148 dbg("%s - port %d", __func__, port->number);
1da177e4 149
1da177e4
LT
150 bytes_in = 0;
151 bytes_out = 0;
152
153 /* Start reading from the device */
154 usb_fill_bulk_urb(
155 port->read_urb,
93c46795 156 serial->dev,
1da177e4
LT
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,
162 port);
163
164 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
165
166 if (result)
93c46795
AC
167 dev_err(&port->dev,
168 "%s - failed submitting read urb, error %d\n",
169 __func__, result);
1da177e4
LT
170
171 return result;
172}
173
174
335f8514 175static void empeg_close(struct usb_serial_port *port)
1da177e4 176{
441b62c1 177 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
178
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); */
183}
184
185
93c46795
AC
186static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
187 const unsigned char *buf, int count)
1da177e4
LT
188{
189 struct usb_serial *serial = port->serial;
190 struct urb *urb;
191 const unsigned char *current_position = buf;
192 unsigned long flags;
193 int status;
194 int i;
195 int bytes_sent = 0;
196 int transfer_size;
197
441b62c1 198 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
199
200 while (count > 0) {
1da177e4
LT
201 /* try to find a free urb in our list of them */
202 urb = NULL;
203
93c46795 204 spin_lock_irqsave(&write_urb_pool_lock, flags);
1da177e4
LT
205
206 for (i = 0; i < NUM_URBS; ++i) {
207 if (write_urb_pool[i]->status != -EINPROGRESS) {
208 urb = write_urb_pool[i];
209 break;
210 }
211 }
212
93c46795 213 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
1da177e4
LT
214
215 if (urb == NULL) {
441b62c1 216 dbg("%s - no more free urbs", __func__);
1da177e4
LT
217 goto exit;
218 }
219
220 if (urb->transfer_buffer == NULL) {
93c46795 221 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
1da177e4 222 if (urb->transfer_buffer == NULL) {
93c46795
AC
223 dev_err(&port->dev,
224 "%s no more kernel memory...\n",
225 __func__);
1da177e4
LT
226 goto exit;
227 }
228 }
229
93c46795 230 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1da177e4 231
93c46795 232 memcpy(urb->transfer_buffer, current_position, transfer_size);
1da177e4 233
441b62c1 234 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer);
1da177e4
LT
235
236 /* build up our urb */
93c46795 237 usb_fill_bulk_urb(
1da177e4
LT
238 urb,
239 serial->dev,
240 usb_sndbulkpipe(serial->dev,
93c46795 241 port->bulk_out_endpointAddress),
1da177e4
LT
242 urb->transfer_buffer,
243 transfer_size,
244 empeg_write_bulk_callback,
245 port);
246
247 /* send it down the pipe */
248 status = usb_submit_urb(urb, GFP_ATOMIC);
249 if (status) {
441b62c1 250 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
1da177e4
LT
251 bytes_sent = status;
252 break;
253 }
254
255 current_position += transfer_size;
256 bytes_sent += transfer_size;
257 count -= transfer_size;
258 bytes_out += transfer_size;
259
260 }
1da177e4
LT
261exit:
262 return bytes_sent;
93c46795 263}
1da177e4
LT
264
265
95da310e 266static int empeg_write_room(struct tty_struct *tty)
1da177e4 267{
95da310e 268 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
269 unsigned long flags;
270 int i;
271 int room = 0;
272
441b62c1 273 dbg("%s - port %d", __func__, port->number);
1da177e4 274
93c46795 275 spin_lock_irqsave(&write_urb_pool_lock, flags);
1da177e4
LT
276 /* tally up the number of bytes available */
277 for (i = 0; i < NUM_URBS; ++i) {
93c46795 278 if (write_urb_pool[i]->status != -EINPROGRESS)
1da177e4 279 room += URB_TRANSFER_BUFFER_SIZE;
93c46795
AC
280 }
281 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
441b62c1 282 dbg("%s - returns %d", __func__, room);
95da310e 283 return room;
1da177e4
LT
284
285}
286
287
95da310e 288static int empeg_chars_in_buffer(struct tty_struct *tty)
1da177e4 289{
95da310e 290 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
291 unsigned long flags;
292 int i;
293 int chars = 0;
294
441b62c1 295 dbg("%s - port %d", __func__, port->number);
1da177e4 296
93c46795 297 spin_lock_irqsave(&write_urb_pool_lock, flags);
1da177e4
LT
298
299 /* tally up the number of bytes waiting */
300 for (i = 0; i < NUM_URBS; ++i) {
93c46795 301 if (write_urb_pool[i]->status == -EINPROGRESS)
1da177e4 302 chars += URB_TRANSFER_BUFFER_SIZE;
1da177e4
LT
303 }
304
93c46795 305 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
441b62c1 306 dbg("%s - returns %d", __func__, chars);
93c46795 307 return chars;
1da177e4
LT
308}
309
310
93c46795 311static void empeg_write_bulk_callback(struct urb *urb)
1da177e4 312{
335202f4
GKH
313 struct usb_serial_port *port = urb->context;
314 int status = urb->status;
1da177e4 315
441b62c1 316 dbg("%s - port %d", __func__, port->number);
1da177e4 317
335202f4
GKH
318 if (status) {
319 dbg("%s - nonzero write bulk status received: %d",
441b62c1 320 __func__, status);
1da177e4
LT
321 return;
322 }
323
cf2c7481 324 usb_serial_port_softint(port);
1da177e4
LT
325}
326
327
93c46795 328static void empeg_read_bulk_callback(struct urb *urb)
1da177e4 329{
cdc97792 330 struct usb_serial_port *port = urb->context;
1da177e4
LT
331 struct tty_struct *tty;
332 unsigned char *data = urb->transfer_buffer;
1da177e4 333 int result;
335202f4 334 int status = urb->status;
1da177e4 335
441b62c1 336 dbg("%s - port %d", __func__, port->number);
1da177e4 337
335202f4
GKH
338 if (status) {
339 dbg("%s - nonzero read bulk status received: %d",
441b62c1 340 __func__, status);
1da177e4
LT
341 return;
342 }
343
93c46795
AC
344 usb_serial_debug_data(debug, &port->dev, __func__,
345 urb->actual_length, data);
4a90f09b 346 tty = tty_port_tty_get(&port->port);
1da177e4
LT
347
348 if (urb->actual_length) {
33f0f88f
AC
349 tty_buffer_request_room(tty, urb->actual_length);
350 tty_insert_flip_string(tty, data, urb->actual_length);
1da177e4
LT
351 tty_flip_buffer_push(tty);
352 bytes_in += urb->actual_length;
353 }
4a90f09b 354 tty_kref_put(tty);
1da177e4
LT
355
356 /* Continue trying to always read */
357 usb_fill_bulk_urb(
358 port->read_urb,
93c46795 359 port->serial->dev,
1da177e4
LT
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,
365 port);
366
367 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
368
369 if (result)
93c46795
AC
370 dev_err(&urb->dev->dev,
371 "%s - failed resubmitting read urb, error %d\n",
372 __func__, result);
1da177e4
LT
373
374 return;
375
376}
377
378
95da310e 379static void empeg_throttle(struct tty_struct *tty)
1da177e4 380{
95da310e 381 struct usb_serial_port *port = tty->driver_data;
441b62c1 382 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
383 usb_kill_urb(port->read_urb);
384}
385
386
95da310e 387static void empeg_unthrottle(struct tty_struct *tty)
1da177e4 388{
95da310e 389 struct usb_serial_port *port = tty->driver_data;
1da177e4 390 int result;
441b62c1 391 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
392
393 port->read_urb->dev = port->serial->dev;
1da177e4 394 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1da177e4 395 if (result)
93c46795
AC
396 dev_err(&port->dev,
397 "%s - failed submitting read urb, error %d\n",
398 __func__, result);
1da177e4
LT
399}
400
401
93c46795 402static int empeg_startup(struct usb_serial *serial)
1da177e4
LT
403{
404 int r;
405
441b62c1 406 dbg("%s", __func__);
1da177e4
LT
407
408 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
194343d9 409 dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
1da177e4
LT
410 serial->dev->actconfig->desc.bConfigurationValue);
411 return -ENODEV;
412 }
441b62c1 413 dbg("%s - reset config", __func__);
93c46795 414 r = usb_reset_configuration(serial->dev);
1da177e4
LT
415
416 /* continue on with initialization */
417 return r;
418
419}
420
421
fe1ae7fd 422static void empeg_init_termios(struct tty_struct *tty)
1da177e4 423{
95da310e 424 struct ktermios *termios = tty->termios;
1da177e4 425
1da177e4 426 /*
93c46795
AC
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.
431 * (at least for now)
432 *
433 * The default requirements for this device are:
434 */
998e8638 435 termios->c_iflag
1da177e4
LT
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 */
444
998e8638 445 termios->c_oflag
1da177e4
LT
446 &= ~OPOST; /* disable postprocess output characters */
447
998e8638 448 termios->c_lflag
1da177e4
LT
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 */
454
998e8638 455 termios->c_cflag
1da177e4
LT
456 &= ~(CSIZE /* no size */
457 | PARENB /* disable parity bit */
458 | CBAUD); /* clear current baud rate */
459
998e8638
AC
460 termios->c_cflag
461 |= CS8; /* character size 8 bits */
1da177e4 462
95da310e 463 tty_encode_baud_rate(tty, 115200, 115200);
1da177e4
LT
464}
465
466
93c46795 467static int __init empeg_init(void)
1da177e4
LT
468{
469 struct urb *urb;
470 int i, retval;
471
93c46795
AC
472 /* create our write urb pool and transfer buffers */
473 spin_lock_init(&write_urb_pool_lock);
1da177e4
LT
474 for (i = 0; i < NUM_URBS; ++i) {
475 urb = usb_alloc_urb(0, GFP_KERNEL);
476 write_urb_pool[i] = urb;
477 if (urb == NULL) {
194343d9 478 printk(KERN_ERR "empeg: No more urbs???\n");
1da177e4
LT
479 continue;
480 }
481
93c46795
AC
482 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
483 GFP_KERNEL);
1da177e4 484 if (!urb->transfer_buffer) {
194343d9
GKH
485 printk(KERN_ERR "empeg: %s - out of memory for urb "
486 "buffers.", __func__);
1da177e4
LT
487 continue;
488 }
489 }
490
491 retval = usb_serial_register(&empeg_device);
492 if (retval)
493 goto failed_usb_serial_register;
494 retval = usb_register(&empeg_driver);
495 if (retval)
496 goto failed_usb_register;
497
c197a8db
GKH
498 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
499 DRIVER_DESC "\n");
1da177e4
LT
500
501 return 0;
502failed_usb_register:
503 usb_serial_deregister(&empeg_device);
504failed_usb_serial_register:
505 for (i = 0; i < NUM_URBS; ++i) {
506 if (write_urb_pool[i]) {
1bc3c9e1 507 kfree(write_urb_pool[i]->transfer_buffer);
1da177e4
LT
508 usb_free_urb(write_urb_pool[i]);
509 }
510 }
511 return retval;
512}
513
514
93c46795 515static void __exit empeg_exit(void)
1da177e4
LT
516{
517 int i;
518 unsigned long flags;
519
520 usb_deregister(&empeg_driver);
93c46795 521 usb_serial_deregister(&empeg_device);
1da177e4 522
93c46795 523 spin_lock_irqsave(&write_urb_pool_lock, flags);
1da177e4
LT
524
525 for (i = 0; i < NUM_URBS; ++i) {
526 if (write_urb_pool[i]) {
93c46795
AC
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
530 * call oopses. */
1da177e4 531 /* usb_kill_urb(write_urb_pool[i]); */
1bc3c9e1 532 kfree(write_urb_pool[i]->transfer_buffer);
93c46795 533 usb_free_urb(write_urb_pool[i]);
1da177e4
LT
534 }
535 }
93c46795 536 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
1da177e4
LT
537}
538
539
540module_init(empeg_init);
541module_exit(empeg_exit);
542
93c46795
AC
543MODULE_AUTHOR(DRIVER_AUTHOR);
544MODULE_DESCRIPTION(DRIVER_DESC);
1da177e4
LT
545MODULE_LICENSE("GPL");
546
547module_param(debug, bool, S_IRUGO | S_IWUSR);
548MODULE_PARM_DESC(debug, "Debug enabled or not");