- Peter Anvin: more P4 configuration parsing
[davej-history.git] / drivers / usb / serial / visor.c
blobee1b73893ded53c092b037cc5e1e91a9dabc5a6d
1 /*
2 * USB HandSpring Visor driver
4 * Copyright (C) 1999, 2000
5 * Greg Kroah-Hartman (greg@kroah.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * See Documentation/usb/usb-serial.txt for more information on using this driver
14 * (11/12/2000) gkh
15 * Fixed bug with data being dropped on the floor by forcing tty->low_latency
16 * to be on. Hopefully this fixes the OHCI issue!
18 * (11/01/2000) Adam J. Richter
19 * usb_device_id table support
21 * (10/05/2000) gkh
22 * Fixed bug with urb->dev not being set properly, now that the usb
23 * core needs it.
25 * (09/11/2000) gkh
26 * Got rid of always calling kmalloc for every urb we wrote out to the
27 * device.
28 * Added visor_read_callback so we can keep track of bytes in and out for
29 * those people who like to know the speed of their device.
30 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
32 * (09/06/2000) gkh
33 * Fixed oops in visor_exit. Need to uncomment usb_unlink_urb call _after_
34 * the host controller drivers set urb->dev = NULL when the urb is finished.
36 * (08/28/2000) gkh
37 * Added locks for SMP safeness.
39 * (08/08/2000) gkh
40 * Fixed endian problem in visor_startup.
41 * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
42 * than once.
44 * (07/23/2000) gkh
45 * Added pool of write urbs to speed up transfers to the visor.
47 * (07/19/2000) gkh
48 * Added module_init and module_exit functions to handle the fact that this
49 * driver is a loadable module now.
51 * (07/03/2000) gkh
52 * Added visor_set_ioctl and visor_set_termios functions (they don't do much
53 * of anything, but are good for debugging.)
55 * (06/25/2000) gkh
56 * Fixed bug in visor_unthrottle that should help with the disconnect in PPP
57 * bug that people have been reporting.
59 * (06/23/2000) gkh
60 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
62 * (04/27/2000) Ryan VanderBijl
63 * Fixed memory leak in visor_close
65 * (03/26/2000) gkh
66 * Split driver up into device specific pieces.
70 #include <linux/config.h>
71 #include <linux/kernel.h>
72 #include <linux/sched.h>
73 #include <linux/signal.h>
74 #include <linux/errno.h>
75 #include <linux/poll.h>
76 #include <linux/init.h>
77 #include <linux/malloc.h>
78 #include <linux/fcntl.h>
79 #include <linux/tty_driver.h>
80 #include <linux/tty_flip.h>
81 #include <linux/tty.h>
82 #include <linux/module.h>
83 #include <linux/spinlock.h>
85 #ifdef CONFIG_USB_SERIAL_DEBUG
86 #define DEBUG
87 #else
88 #undef DEBUG
89 #endif
90 #include <linux/usb.h>
92 #include "usb-serial.h"
94 #include "visor.h"
96 #define MIN(a,b) (((a)<(b))?(a):(b))
98 /* function prototypes for a handspring visor */
99 static int visor_open (struct usb_serial_port *port, struct file *filp);
100 static void visor_close (struct usb_serial_port *port, struct file *filp);
101 static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
102 static void visor_throttle (struct usb_serial_port *port);
103 static void visor_unthrottle (struct usb_serial_port *port);
104 static int visor_startup (struct usb_serial *serial);
105 static void visor_shutdown (struct usb_serial *serial);
106 static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
107 static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios);
108 static void visor_write_bulk_callback (struct urb *urb);
109 static void visor_read_bulk_callback (struct urb *urb);
112 static __devinitdata struct usb_device_id id_table [] = {
113 { idVendor: HANDSPRING_VENDOR_ID, idProduct: HANDSPRING_VISOR_ID },
114 { } /* Terminating entry */
117 MODULE_DEVICE_TABLE (usb, id_table);
121 /* All of the device info needed for the Handspring Visor */
122 struct usb_serial_device_type handspring_device = {
123 name: "Handspring Visor",
124 id_table: id_table,
125 needs_interrupt_in: MUST_HAVE_NOT, /* this device must not have an interrupt in endpoint */
126 needs_bulk_in: MUST_HAVE, /* this device must have a bulk in endpoint */
127 needs_bulk_out: MUST_HAVE, /* this device must have a bulk out endpoint */
128 num_interrupt_in: 0,
129 num_bulk_in: 2,
130 num_bulk_out: 2,
131 num_ports: 2,
132 open: visor_open,
133 close: visor_close,
134 throttle: visor_throttle,
135 unthrottle: visor_unthrottle,
136 startup: visor_startup,
137 shutdown: visor_shutdown,
138 ioctl: visor_ioctl,
139 set_termios: visor_set_termios,
140 write: visor_write,
141 write_bulk_callback: visor_write_bulk_callback,
142 read_bulk_callback: visor_read_bulk_callback,
146 #define NUM_URBS 24
147 #define URB_TRANSFER_BUFFER_SIZE 64
148 static struct urb *write_urb_pool[NUM_URBS];
149 static spinlock_t write_urb_pool_lock;
150 static int bytes_in;
151 static int bytes_out;
154 /******************************************************************************
155 * Handspring Visor specific driver functions
156 ******************************************************************************/
157 static int visor_open (struct usb_serial_port *port, struct file *filp)
159 struct usb_serial *serial = port->serial;
160 unsigned long flags;
161 int result;
163 if (port_paranoia_check (port, __FUNCTION__))
164 return -ENODEV;
166 dbg(__FUNCTION__ " - port %d", port->number);
168 spin_lock_irqsave (&port->port_lock, flags);
170 ++port->open_count;
171 MOD_INC_USE_COUNT;
173 if (!port->active) {
174 port->active = 1;
175 bytes_in = 0;
176 bytes_out = 0;
178 /* force low_latency on so that our tty_push actually forces the data through,
179 otherwise it is scheduled, and with high data rates (like with OHCI) data
180 can get lost. */
181 port->tty->low_latency = 1;
183 /* Start reading from the device */
184 FILL_BULK_URB(port->read_urb, serial->dev,
185 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
186 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
187 visor_read_bulk_callback, port);
188 result = usb_submit_urb(port->read_urb);
189 if (result)
190 err(__FUNCTION__ " - failed submitting read urb, error %d", result);
193 spin_unlock_irqrestore (&port->port_lock, flags);
195 return 0;
199 static void visor_close (struct usb_serial_port *port, struct file * filp)
201 struct usb_serial *serial;
202 unsigned char *transfer_buffer;
203 unsigned long flags;
205 if (port_paranoia_check (port, __FUNCTION__))
206 return;
208 dbg(__FUNCTION__ " - port %d", port->number);
210 serial = get_usb_serial (port, __FUNCTION__);
211 if (!serial)
212 return;
214 spin_lock_irqsave (&port->port_lock, flags);
216 --port->open_count;
217 MOD_DEC_USE_COUNT;
219 if (port->open_count <= 0) {
220 transfer_buffer = kmalloc (0x12, GFP_KERNEL);
221 if (!transfer_buffer) {
222 err(__FUNCTION__ " - kmalloc(%d) failed.", 0x12);
223 } else {
224 /* send a shutdown message to the device */
225 usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_CLOSE_NOTIFICATION,
226 0xc2, 0x0000, 0x0000, transfer_buffer, 0x12, 300);
227 kfree (transfer_buffer);
230 /* shutdown our bulk read */
231 usb_unlink_urb (port->read_urb);
232 port->active = 0;
233 port->open_count = 0;
236 spin_unlock_irqrestore (&port->port_lock, flags);
238 /* Uncomment the following line if you want to see some statistics in your syslog */
239 /* info ("Bytes In = %d Bytes Out = %d", bytes_in, bytes_out); */
243 static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
245 struct usb_serial *serial = port->serial;
246 struct urb *urb;
247 const unsigned char *current_position = buf;
248 unsigned long flags;
249 int status;
250 int i;
251 int bytes_sent = 0;
252 int transfer_size;
254 dbg(__FUNCTION__ " - port %d", port->number);
256 usb_serial_debug_data (__FILE__, __FUNCTION__, count, buf);
258 while (count > 0) {
259 /* try to find a free urb in our list of them */
260 urb = NULL;
261 spin_lock_irqsave (&write_urb_pool_lock, flags);
262 for (i = 0; i < NUM_URBS; ++i) {
263 if (write_urb_pool[i]->status != -EINPROGRESS) {
264 urb = write_urb_pool[i];
265 break;
268 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
269 if (urb == NULL) {
270 dbg (__FUNCTION__ " - no more free urbs");
271 goto exit;
273 if (urb->transfer_buffer == NULL) {
274 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
275 if (urb->transfer_buffer == NULL) {
276 err(__FUNCTION__" no more kernel memory...");
277 goto exit;
281 transfer_size = MIN (count, URB_TRANSFER_BUFFER_SIZE);
282 if (from_user)
283 copy_from_user (urb->transfer_buffer, current_position, transfer_size);
284 else
285 memcpy (urb->transfer_buffer, current_position, transfer_size);
287 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
289 /* build up our urb */
290 FILL_BULK_URB (urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
291 urb->transfer_buffer, transfer_size, visor_write_bulk_callback, port);
292 urb->transfer_flags |= USB_QUEUE_BULK;
294 /* send it down the pipe */
295 status = usb_submit_urb(urb);
296 if (status)
297 dbg(__FUNCTION__ " - usb_submit_urb(write bulk) failed with status = %d", status);
299 current_position += transfer_size;
300 bytes_sent += transfer_size;
301 count -= transfer_size;
302 bytes_out += transfer_size;
305 exit:
306 return bytes_sent;
310 static void visor_write_bulk_callback (struct urb *urb)
312 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
314 if (port_paranoia_check (port, __FUNCTION__))
315 return;
317 dbg(__FUNCTION__ " - port %d", port->number);
319 if (urb->status) {
320 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
321 return;
324 queue_task(&port->tqueue, &tq_immediate);
325 mark_bh(IMMEDIATE_BH);
327 return;
331 static void visor_read_bulk_callback (struct urb *urb)
333 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
334 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
335 struct tty_struct *tty;
336 unsigned char *data = urb->transfer_buffer;
337 int i;
338 int result;
340 if (port_paranoia_check (port, __FUNCTION__))
341 return;
343 dbg(__FUNCTION__ " - port %d", port->number);
345 if (!serial) {
346 dbg(__FUNCTION__ " - bad serial pointer, exiting");
347 return;
350 if (urb->status) {
351 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
352 return;
355 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
357 tty = port->tty;
358 if (urb->actual_length) {
359 for (i = 0; i < urb->actual_length ; ++i) {
360 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
361 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
362 tty_flip_buffer_push(tty);
364 /* this doesn't actually push the data through unless tty->low_latency is set */
365 tty_insert_flip_char(tty, data[i], 0);
367 tty_flip_buffer_push(tty);
368 bytes_in += urb->actual_length;
371 /* Continue trying to always read */
372 FILL_BULK_URB(port->read_urb, serial->dev,
373 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
374 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
375 visor_read_bulk_callback, port);
376 result = usb_submit_urb(port->read_urb);
377 if (result)
378 err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
379 return;
383 static void visor_throttle (struct usb_serial_port *port)
385 unsigned long flags;
387 dbg(__FUNCTION__ " - port %d", port->number);
389 spin_lock_irqsave (&port->port_lock, flags);
391 usb_unlink_urb (port->read_urb);
393 spin_unlock_irqrestore (&port->port_lock, flags);
395 return;
399 static void visor_unthrottle (struct usb_serial_port *port)
401 unsigned long flags;
402 int result;
404 dbg(__FUNCTION__ " - port %d", port->number);
406 spin_lock_irqsave (&port->port_lock, flags);
408 port->read_urb->dev = port->serial->dev;
409 result = usb_submit_urb(port->read_urb);
410 if (result)
411 err(__FUNCTION__ " - failed submitting read urb, error %d", result);
413 spin_unlock_irqrestore (&port->port_lock, flags);
415 return;
419 static int visor_startup (struct usb_serial *serial)
421 int response;
422 int i;
423 unsigned char *transfer_buffer = kmalloc (256, GFP_KERNEL);
425 if (!transfer_buffer) {
426 err(__FUNCTION__ " - kmalloc(%d) failed.", 256);
427 return -ENOMEM;
430 dbg(__FUNCTION__);
432 dbg(__FUNCTION__ " - Set config to 1");
433 usb_set_configuration (serial->dev, 1);
435 /* send a get connection info request */
436 response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_GET_CONNECTION_INFORMATION,
437 0xc2, 0x0000, 0x0000, transfer_buffer, 0x12, 300);
438 if (response < 0) {
439 err(__FUNCTION__ " - error getting connection information");
440 } else {
441 struct visor_connection_info *connection_info = (struct visor_connection_info *)transfer_buffer;
442 char *string;
444 le16_to_cpus(&connection_info->num_ports);
445 info("%s: Number of ports: %d", serial->type->name, connection_info->num_ports);
446 for (i = 0; i < connection_info->num_ports; ++i) {
447 switch (connection_info->connections[i].port_function_id) {
448 case VISOR_FUNCTION_GENERIC:
449 string = "Generic";
450 break;
451 case VISOR_FUNCTION_DEBUGGER:
452 string = "Debugger";
453 break;
454 case VISOR_FUNCTION_HOTSYNC:
455 string = "HotSync";
456 break;
457 case VISOR_FUNCTION_CONSOLE:
458 string = "Console";
459 break;
460 case VISOR_FUNCTION_REMOTE_FILE_SYS:
461 string = "Remote File System";
462 break;
463 default:
464 string = "unknown";
465 break;
467 info("%s: port %d, is for %s use and is bound to ttyUSB%d", serial->type->name, connection_info->connections[i].port, string, serial->minor + i);
471 /* ask for the number of bytes available, but ignore the response as it is broken */
472 response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_REQUEST_BYTES_AVAILABLE,
473 0xc2, 0x0000, 0x0005, transfer_buffer, 0x02, 300);
474 if (response < 0) {
475 err(__FUNCTION__ " - error getting bytes available request");
478 kfree (transfer_buffer);
480 /* continue on with initialization */
481 return 0;
485 static void visor_shutdown (struct usb_serial *serial)
487 int i;
489 dbg (__FUNCTION__);
491 /* stop reads and writes on all ports */
492 for (i=0; i < serial->num_ports; ++i) {
493 while (serial->port[i].open_count > 0) {
494 visor_close (&serial->port[i], NULL);
500 static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
502 dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);
504 return -ENOIOCTLCMD;
508 /* This function is all nice and good, but we don't change anything based on it :) */
509 static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
511 unsigned int cflag = port->tty->termios->c_cflag;
513 dbg(__FUNCTION__ " - port %d", port->number);
515 /* check that they really want us to change something */
516 if (old_termios) {
517 if ((cflag == old_termios->c_cflag) &&
518 (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
519 dbg(__FUNCTION__ " - nothing to change...");
520 return;
524 if ((!port->tty) || (!port->tty->termios)) {
525 dbg(__FUNCTION__" - no tty structures");
526 return;
529 /* get the byte size */
530 switch (cflag & CSIZE) {
531 case CS5: dbg(__FUNCTION__ " - data bits = 5"); break;
532 case CS6: dbg(__FUNCTION__ " - data bits = 6"); break;
533 case CS7: dbg(__FUNCTION__ " - data bits = 7"); break;
534 default:
535 case CS8: dbg(__FUNCTION__ " - data bits = 8"); break;
538 /* determine the parity */
539 if (cflag & PARENB)
540 if (cflag & PARODD)
541 dbg(__FUNCTION__ " - parity = odd");
542 else
543 dbg(__FUNCTION__ " - parity = even");
544 else
545 dbg(__FUNCTION__ " - parity = none");
547 /* figure out the stop bits requested */
548 if (cflag & CSTOPB)
549 dbg(__FUNCTION__ " - stop bits = 2");
550 else
551 dbg(__FUNCTION__ " - stop bits = 1");
554 /* figure out the flow control settings */
555 if (cflag & CRTSCTS)
556 dbg(__FUNCTION__ " - RTS/CTS is enabled");
557 else
558 dbg(__FUNCTION__ " - RTS/CTS is disabled");
560 /* determine software flow control */
561 if (I_IXOFF(port->tty))
562 dbg(__FUNCTION__ " - XON/XOFF is enabled, XON = %2x, XOFF = %2x", START_CHAR(port->tty), STOP_CHAR(port->tty));
563 else
564 dbg(__FUNCTION__ " - XON/XOFF is disabled");
566 /* get the baud rate wanted */
567 dbg(__FUNCTION__ " - baud rate = %d", tty_get_baud_rate(port->tty));
569 return;
573 static int __init visor_init (void)
575 struct urb *urb;
576 int i;
578 usb_serial_register (&handspring_device);
580 /* create our write urb pool and transfer buffers */
581 spin_lock_init (&write_urb_pool_lock);
582 for (i = 0; i < NUM_URBS; ++i) {
583 urb = usb_alloc_urb(0);
584 write_urb_pool[i] = urb;
585 if (urb == NULL) {
586 err("No more urbs???");
587 continue;
590 urb->transfer_buffer = NULL;
591 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
592 if (!urb->transfer_buffer) {
593 err (__FUNCTION__ " - out of memory for urb buffers.");
594 continue;
598 return 0;
602 static void __exit visor_exit (void)
604 int i;
605 unsigned long flags;
607 usb_serial_deregister (&handspring_device);
609 spin_lock_irqsave (&write_urb_pool_lock, flags);
611 for (i = 0; i < NUM_URBS; ++i) {
612 if (write_urb_pool[i]) {
613 /* FIXME - uncomment the following usb_unlink_urb call when
614 * the host controllers get fixed to set urb->dev = NULL after
615 * the urb is finished. Otherwise this call oopses. */
616 /* usb_unlink_urb(write_urb_pool[i]); */
617 if (write_urb_pool[i]->transfer_buffer)
618 kfree(write_urb_pool[i]->transfer_buffer);
619 usb_free_urb (write_urb_pool[i]);
623 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
627 module_init(visor_init);
628 module_exit(visor_exit);
630 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
631 MODULE_DESCRIPTION("USB HandSpring Visor driver");