- pre3:
[davej-history.git] / drivers / usb / serial / visor.c
blobc078704f0d88884ca7125c917c4ef517d9bf084c
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/01/2000) Adam J. Richter
15 * usb_device_id table support
17 * (10/05/2000) gkh
18 * Fixed bug with urb->dev not being set properly, now that the usb
19 * core needs it.
21 * (09/11/2000) gkh
22 * Got rid of always calling kmalloc for every urb we wrote out to the
23 * device.
24 * Added visor_read_callback so we can keep track of bytes in and out for
25 * those people who like to know the speed of their device.
26 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
28 * (09/06/2000) gkh
29 * Fixed oops in visor_exit. Need to uncomment usb_unlink_urb call _after_
30 * the host controller drivers set urb->dev = NULL when the urb is finished.
32 * (08/28/2000) gkh
33 * Added locks for SMP safeness.
35 * (08/08/2000) gkh
36 * Fixed endian problem in visor_startup.
37 * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
38 * than once.
40 * (07/23/2000) gkh
41 * Added pool of write urbs to speed up transfers to the visor.
43 * (07/19/2000) gkh
44 * Added module_init and module_exit functions to handle the fact that this
45 * driver is a loadable module now.
47 * (07/03/2000) gkh
48 * Added visor_set_ioctl and visor_set_termios functions (they don't do much
49 * of anything, but are good for debugging.)
51 * (06/25/2000) gkh
52 * Fixed bug in visor_unthrottle that should help with the disconnect in PPP
53 * bug that people have been reporting.
55 * (06/23/2000) gkh
56 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
58 * (04/27/2000) Ryan VanderBijl
59 * Fixed memory leak in visor_close
61 * (03/26/2000) gkh
62 * Split driver up into device specific pieces.
66 #include <linux/config.h>
67 #include <linux/kernel.h>
68 #include <linux/sched.h>
69 #include <linux/signal.h>
70 #include <linux/errno.h>
71 #include <linux/poll.h>
72 #include <linux/init.h>
73 #include <linux/malloc.h>
74 #include <linux/fcntl.h>
75 #include <linux/tty_driver.h>
76 #include <linux/tty_flip.h>
77 #include <linux/tty.h>
78 #include <linux/module.h>
79 #include <linux/spinlock.h>
81 #ifdef CONFIG_USB_SERIAL_DEBUG
82 #define DEBUG
83 #else
84 #undef DEBUG
85 #endif
86 #include <linux/usb.h>
88 #include "usb-serial.h"
90 #include "visor.h"
92 #define MIN(a,b) (((a)<(b))?(a):(b))
94 /* function prototypes for a handspring visor */
95 static int visor_open (struct usb_serial_port *port, struct file *filp);
96 static void visor_close (struct usb_serial_port *port, struct file *filp);
97 static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
98 static void visor_throttle (struct usb_serial_port *port);
99 static void visor_unthrottle (struct usb_serial_port *port);
100 static int visor_startup (struct usb_serial *serial);
101 static void visor_shutdown (struct usb_serial *serial);
102 static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
103 static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios);
104 static void visor_write_bulk_callback (struct urb *urb);
105 static void visor_read_bulk_callback (struct urb *urb);
108 static __devinitdata struct usb_device_id id_table [] = {
109 { idVendor: HANDSPRING_VENDOR_ID, idProduct: HANDSPRING_VISOR_ID },
110 { } /* Terminating entry */
113 MODULE_DEVICE_TABLE (usb, id_table);
117 /* All of the device info needed for the Handspring Visor */
118 struct usb_serial_device_type handspring_device = {
119 name: "Handspring Visor",
120 id_table: id_table,
121 needs_interrupt_in: MUST_HAVE_NOT, /* this device must not have an interrupt in endpoint */
122 needs_bulk_in: MUST_HAVE, /* this device must have a bulk in endpoint */
123 needs_bulk_out: MUST_HAVE, /* this device must have a bulk out endpoint */
124 num_interrupt_in: 0,
125 num_bulk_in: 2,
126 num_bulk_out: 2,
127 num_ports: 2,
128 open: visor_open,
129 close: visor_close,
130 throttle: visor_throttle,
131 unthrottle: visor_unthrottle,
132 startup: visor_startup,
133 shutdown: visor_shutdown,
134 ioctl: visor_ioctl,
135 set_termios: visor_set_termios,
136 write: visor_write,
137 write_bulk_callback: visor_write_bulk_callback,
138 read_bulk_callback: visor_read_bulk_callback,
142 #define NUM_URBS 24
143 #define URB_TRANSFER_BUFFER_SIZE 64
144 static struct urb *write_urb_pool[NUM_URBS];
145 static spinlock_t write_urb_pool_lock;
146 static int bytes_in;
147 static int bytes_out;
150 /******************************************************************************
151 * Handspring Visor specific driver functions
152 ******************************************************************************/
153 static int visor_open (struct usb_serial_port *port, struct file *filp)
155 struct usb_serial *serial = port->serial;
156 unsigned long flags;
157 int result;
159 if (port_paranoia_check (port, __FUNCTION__))
160 return -ENODEV;
162 dbg(__FUNCTION__ " - port %d", port->number);
164 spin_lock_irqsave (&port->port_lock, flags);
166 ++port->open_count;
167 MOD_INC_USE_COUNT;
169 if (!port->active) {
170 port->active = 1;
171 bytes_in = 0;
172 bytes_out = 0;
174 /* Start reading from the device */
175 FILL_BULK_URB(port->read_urb, serial->dev,
176 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
177 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
178 visor_read_bulk_callback, port);
179 result = usb_submit_urb(port->read_urb);
180 if (result)
181 err(__FUNCTION__ " - failed submitting read urb, error %d", result);
184 spin_unlock_irqrestore (&port->port_lock, flags);
186 return 0;
190 static void visor_close (struct usb_serial_port *port, struct file * filp)
192 struct usb_serial *serial;
193 unsigned char *transfer_buffer;
194 unsigned long flags;
196 if (port_paranoia_check (port, __FUNCTION__))
197 return;
199 dbg(__FUNCTION__ " - port %d", port->number);
201 serial = get_usb_serial (port, __FUNCTION__);
202 if (!serial)
203 return;
205 spin_lock_irqsave (&port->port_lock, flags);
207 --port->open_count;
208 MOD_DEC_USE_COUNT;
210 if (port->open_count <= 0) {
211 transfer_buffer = kmalloc (0x12, GFP_KERNEL);
212 if (!transfer_buffer) {
213 err(__FUNCTION__ " - kmalloc(%d) failed.", 0x12);
214 } else {
215 /* send a shutdown message to the device */
216 usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_CLOSE_NOTIFICATION,
217 0xc2, 0x0000, 0x0000, transfer_buffer, 0x12, 300);
218 kfree (transfer_buffer);
221 /* shutdown our bulk read */
222 usb_unlink_urb (port->read_urb);
223 port->active = 0;
224 port->open_count = 0;
227 spin_unlock_irqrestore (&port->port_lock, flags);
229 /* Uncomment the following line if you want to see some statistics in your syslog */
230 /* info ("Bytes In = %d Bytes Out = %d", bytes_in, bytes_out); */
234 static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
236 struct usb_serial *serial = port->serial;
237 struct urb *urb;
238 const unsigned char *current_position = buf;
239 unsigned long flags;
240 int status;
241 int i;
242 int bytes_sent = 0;
243 int transfer_size;
245 dbg(__FUNCTION__ " - port %d", port->number);
247 usb_serial_debug_data (__FILE__, __FUNCTION__, count, buf);
249 while (count > 0) {
250 /* try to find a free urb in our list of them */
251 urb = NULL;
252 spin_lock_irqsave (&write_urb_pool_lock, flags);
253 for (i = 0; i < NUM_URBS; ++i) {
254 if (write_urb_pool[i]->status != -EINPROGRESS) {
255 urb = write_urb_pool[i];
256 break;
259 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
260 if (urb == NULL) {
261 dbg (__FUNCTION__ " - no more free urbs");
262 goto exit;
264 if (urb->transfer_buffer == NULL) {
265 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
266 if (urb->transfer_buffer == NULL) {
267 err(__FUNCTION__" no more kernel memory...");
268 goto exit;
272 transfer_size = MIN (count, URB_TRANSFER_BUFFER_SIZE);
273 if (from_user)
274 copy_from_user (urb->transfer_buffer, current_position, transfer_size);
275 else
276 memcpy (urb->transfer_buffer, current_position, transfer_size);
278 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
280 /* build up our urb */
281 FILL_BULK_URB (urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
282 urb->transfer_buffer, transfer_size, visor_write_bulk_callback, port);
283 urb->transfer_flags |= USB_QUEUE_BULK;
285 /* send it down the pipe */
286 status = usb_submit_urb(urb);
287 if (status)
288 dbg(__FUNCTION__ " - usb_submit_urb(write bulk) failed with status = %d", status);
290 current_position += transfer_size;
291 bytes_sent += transfer_size;
292 count -= transfer_size;
293 bytes_out += transfer_size;
296 exit:
297 return bytes_sent;
301 static void visor_write_bulk_callback (struct urb *urb)
303 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
305 if (port_paranoia_check (port, __FUNCTION__))
306 return;
308 dbg(__FUNCTION__ " - port %d", port->number);
310 if (urb->status) {
311 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
312 return;
315 queue_task(&port->tqueue, &tq_immediate);
316 mark_bh(IMMEDIATE_BH);
318 return;
322 static void visor_read_bulk_callback (struct urb *urb)
324 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
325 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
326 struct tty_struct *tty;
327 unsigned char *data = urb->transfer_buffer;
328 int i;
329 int result;
331 if (port_paranoia_check (port, __FUNCTION__))
332 return;
334 dbg(__FUNCTION__ " - port %d", port->number);
336 if (!serial) {
337 dbg(__FUNCTION__ " - bad serial pointer, exiting");
338 return;
341 if (urb->status) {
342 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
343 return;
346 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
348 tty = port->tty;
349 if (urb->actual_length) {
350 for (i = 0; i < urb->actual_length ; ++i) {
351 tty_insert_flip_char(tty, data[i], 0);
353 tty_flip_buffer_push(tty);
354 bytes_in += urb->actual_length;
357 /* Continue trying to always read */
358 FILL_BULK_URB(port->read_urb, serial->dev,
359 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
360 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
361 visor_read_bulk_callback, port);
362 result = usb_submit_urb(port->read_urb);
363 if (result)
364 err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
365 return;
369 static void visor_throttle (struct usb_serial_port *port)
371 unsigned long flags;
373 dbg(__FUNCTION__ " - port %d", port->number);
375 spin_lock_irqsave (&port->port_lock, flags);
377 usb_unlink_urb (port->read_urb);
379 spin_unlock_irqrestore (&port->port_lock, flags);
381 return;
385 static void visor_unthrottle (struct usb_serial_port *port)
387 unsigned long flags;
388 int result;
390 dbg(__FUNCTION__ " - port %d", port->number);
392 spin_lock_irqsave (&port->port_lock, flags);
394 port->read_urb->dev = port->serial->dev;
395 result = usb_submit_urb(port->read_urb);
396 if (result)
397 err(__FUNCTION__ " - failed submitting read urb, error %d", result);
399 spin_unlock_irqrestore (&port->port_lock, flags);
401 return;
405 static int visor_startup (struct usb_serial *serial)
407 int response;
408 int i;
409 unsigned char *transfer_buffer = kmalloc (256, GFP_KERNEL);
411 if (!transfer_buffer) {
412 err(__FUNCTION__ " - kmalloc(%d) failed.", 256);
413 return -ENOMEM;
416 dbg(__FUNCTION__);
418 dbg(__FUNCTION__ " - Set config to 1");
419 usb_set_configuration (serial->dev, 1);
421 /* send a get connection info request */
422 response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_GET_CONNECTION_INFORMATION,
423 0xc2, 0x0000, 0x0000, transfer_buffer, 0x12, 300);
424 if (response < 0) {
425 err(__FUNCTION__ " - error getting connection information");
426 } else {
427 struct visor_connection_info *connection_info = (struct visor_connection_info *)transfer_buffer;
428 char *string;
430 le16_to_cpus(&connection_info->num_ports);
431 info("%s: Number of ports: %d", serial->type->name, connection_info->num_ports);
432 for (i = 0; i < connection_info->num_ports; ++i) {
433 switch (connection_info->connections[i].port_function_id) {
434 case VISOR_FUNCTION_GENERIC:
435 string = "Generic";
436 break;
437 case VISOR_FUNCTION_DEBUGGER:
438 string = "Debugger";
439 break;
440 case VISOR_FUNCTION_HOTSYNC:
441 string = "HotSync";
442 break;
443 case VISOR_FUNCTION_CONSOLE:
444 string = "Console";
445 break;
446 case VISOR_FUNCTION_REMOTE_FILE_SYS:
447 string = "Remote File System";
448 break;
449 default:
450 string = "unknown";
451 break;
453 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);
457 /* ask for the number of bytes available, but ignore the response as it is broken */
458 response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_REQUEST_BYTES_AVAILABLE,
459 0xc2, 0x0000, 0x0005, transfer_buffer, 0x02, 300);
460 if (response < 0) {
461 err(__FUNCTION__ " - error getting bytes available request");
464 kfree (transfer_buffer);
466 /* continue on with initialization */
467 return 0;
471 static void visor_shutdown (struct usb_serial *serial)
473 int i;
475 dbg (__FUNCTION__);
477 /* stop reads and writes on all ports */
478 for (i=0; i < serial->num_ports; ++i) {
479 while (serial->port[i].open_count > 0) {
480 visor_close (&serial->port[i], NULL);
486 static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
488 dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);
490 return -ENOIOCTLCMD;
494 /* This function is all nice and good, but we don't change anything based on it :) */
495 static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
497 unsigned int cflag = port->tty->termios->c_cflag;
499 dbg(__FUNCTION__ " - port %d", port->number);
501 /* check that they really want us to change something */
502 if (old_termios) {
503 if ((cflag == old_termios->c_cflag) &&
504 (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
505 dbg(__FUNCTION__ " - nothing to change...");
506 return;
510 if ((!port->tty) || (!port->tty->termios)) {
511 dbg(__FUNCTION__" - no tty structures");
512 return;
515 /* get the byte size */
516 switch (cflag & CSIZE) {
517 case CS5: dbg(__FUNCTION__ " - data bits = 5"); break;
518 case CS6: dbg(__FUNCTION__ " - data bits = 6"); break;
519 case CS7: dbg(__FUNCTION__ " - data bits = 7"); break;
520 default:
521 case CS8: dbg(__FUNCTION__ " - data bits = 8"); break;
524 /* determine the parity */
525 if (cflag & PARENB)
526 if (cflag & PARODD)
527 dbg(__FUNCTION__ " - parity = odd");
528 else
529 dbg(__FUNCTION__ " - parity = even");
530 else
531 dbg(__FUNCTION__ " - parity = none");
533 /* figure out the stop bits requested */
534 if (cflag & CSTOPB)
535 dbg(__FUNCTION__ " - stop bits = 2");
536 else
537 dbg(__FUNCTION__ " - stop bits = 1");
540 /* figure out the flow control settings */
541 if (cflag & CRTSCTS)
542 dbg(__FUNCTION__ " - RTS/CTS is enabled");
543 else
544 dbg(__FUNCTION__ " - RTS/CTS is disabled");
546 /* determine software flow control */
547 if (I_IXOFF(port->tty))
548 dbg(__FUNCTION__ " - XON/XOFF is enabled, XON = %2x, XOFF = %2x", START_CHAR(port->tty), STOP_CHAR(port->tty));
549 else
550 dbg(__FUNCTION__ " - XON/XOFF is disabled");
552 /* get the baud rate wanted */
553 dbg(__FUNCTION__ " - baud rate = %d", tty_get_baud_rate(port->tty));
555 return;
559 static int __init visor_init (void)
561 struct urb *urb;
562 int i;
564 usb_serial_register (&handspring_device);
566 /* create our write urb pool and transfer buffers */
567 spin_lock_init (&write_urb_pool_lock);
568 for (i = 0; i < NUM_URBS; ++i) {
569 urb = usb_alloc_urb(0);
570 write_urb_pool[i] = urb;
571 if (urb == NULL) {
572 err("No more urbs???");
573 continue;
576 urb->transfer_buffer = NULL;
577 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
578 if (!urb->transfer_buffer) {
579 err (__FUNCTION__ " - out of memory for urb buffers.");
580 continue;
584 return 0;
588 static void __exit visor_exit (void)
590 int i;
591 unsigned long flags;
593 usb_serial_deregister (&handspring_device);
595 spin_lock_irqsave (&write_urb_pool_lock, flags);
597 for (i = 0; i < NUM_URBS; ++i) {
598 if (write_urb_pool[i]) {
599 /* FIXME - uncomment the following usb_unlink_urb call when
600 * the host controllers get fixed to set urb->dev = NULL after
601 * the urb is finished. Otherwise this call oopses. */
602 /* usb_unlink_urb(write_urb_pool[i]); */
603 if (write_urb_pool[i]->transfer_buffer)
604 kfree(write_urb_pool[i]->transfer_buffer);
605 usb_free_urb (write_urb_pool[i]);
609 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
613 module_init(visor_init);
614 module_exit(visor_exit);
616 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
617 MODULE_DESCRIPTION("USB HandSpring Visor driver");