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
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
22 * Fixed bug with urb->dev not being set properly, now that the usb
26 * Got rid of always calling kmalloc for every urb we wrote out to the
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
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.
37 * Added locks for SMP safeness.
40 * Fixed endian problem in visor_startup.
41 * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
45 * Added pool of write urbs to speed up transfers to the visor.
48 * Added module_init and module_exit functions to handle the fact that this
49 * driver is a loadable module now.
52 * Added visor_set_ioctl and visor_set_termios functions (they don't do much
53 * of anything, but are good for debugging.)
56 * Fixed bug in visor_unthrottle that should help with the disconnect in PPP
57 * bug that people have been reporting.
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
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
90 #include <linux/usb.h>
92 #include "usb-serial.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",
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 */
134 throttle
: visor_throttle
,
135 unthrottle
: visor_unthrottle
,
136 startup
: visor_startup
,
137 shutdown
: visor_shutdown
,
139 set_termios
: visor_set_termios
,
141 write_bulk_callback
: visor_write_bulk_callback
,
142 read_bulk_callback
: visor_read_bulk_callback
,
147 #define URB_TRANSFER_BUFFER_SIZE 64
148 static struct urb
*write_urb_pool
[NUM_URBS
];
149 static spinlock_t write_urb_pool_lock
;
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
;
163 if (port_paranoia_check (port
, __FUNCTION__
))
166 dbg(__FUNCTION__
" - port %d", port
->number
);
168 spin_lock_irqsave (&port
->port_lock
, flags
);
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
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
);
190 err(__FUNCTION__
" - failed submitting read urb, error %d", result
);
193 spin_unlock_irqrestore (&port
->port_lock
, flags
);
199 static void visor_close (struct usb_serial_port
*port
, struct file
* filp
)
201 struct usb_serial
*serial
;
202 unsigned char *transfer_buffer
;
205 if (port_paranoia_check (port
, __FUNCTION__
))
208 dbg(__FUNCTION__
" - port %d", port
->number
);
210 serial
= get_usb_serial (port
, __FUNCTION__
);
214 spin_lock_irqsave (&port
->port_lock
, flags
);
219 if (port
->open_count
<= 0) {
220 transfer_buffer
= kmalloc (0x12, GFP_KERNEL
);
221 if (!transfer_buffer
) {
222 err(__FUNCTION__
" - kmalloc(%d) failed.", 0x12);
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
);
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
;
247 const unsigned char *current_position
= buf
;
254 dbg(__FUNCTION__
" - port %d", port
->number
);
256 usb_serial_debug_data (__FILE__
, __FUNCTION__
, count
, buf
);
259 /* try to find a free urb in our list of them */
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
];
268 spin_unlock_irqrestore (&write_urb_pool_lock
, flags
);
270 dbg (__FUNCTION__
" - no more free urbs");
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...");
281 transfer_size
= MIN (count
, URB_TRANSFER_BUFFER_SIZE
);
283 copy_from_user (urb
->transfer_buffer
, current_position
, transfer_size
);
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
);
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
;
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__
))
317 dbg(__FUNCTION__
" - port %d", port
->number
);
320 dbg(__FUNCTION__
" - nonzero write bulk status received: %d", urb
->status
);
324 queue_task(&port
->tqueue
, &tq_immediate
);
325 mark_bh(IMMEDIATE_BH
);
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
;
340 if (port_paranoia_check (port
, __FUNCTION__
))
343 dbg(__FUNCTION__
" - port %d", port
->number
);
346 dbg(__FUNCTION__
" - bad serial pointer, exiting");
351 dbg(__FUNCTION__
" - nonzero read bulk status received: %d", urb
->status
);
355 usb_serial_debug_data (__FILE__
, __FUNCTION__
, urb
->actual_length
, data
);
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
);
378 err(__FUNCTION__
" - failed resubmitting read urb, error %d", result
);
383 static void visor_throttle (struct usb_serial_port
*port
)
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
);
399 static void visor_unthrottle (struct usb_serial_port
*port
)
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
);
411 err(__FUNCTION__
" - failed submitting read urb, error %d", result
);
413 spin_unlock_irqrestore (&port
->port_lock
, flags
);
419 static int visor_startup (struct usb_serial
*serial
)
423 unsigned char *transfer_buffer
= kmalloc (256, GFP_KERNEL
);
425 if (!transfer_buffer
) {
426 err(__FUNCTION__
" - kmalloc(%d) failed.", 256);
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);
439 err(__FUNCTION__
" - error getting connection information");
441 struct visor_connection_info
*connection_info
= (struct visor_connection_info
*)transfer_buffer
;
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
:
451 case VISOR_FUNCTION_DEBUGGER
:
454 case VISOR_FUNCTION_HOTSYNC
:
457 case VISOR_FUNCTION_CONSOLE
:
460 case VISOR_FUNCTION_REMOTE_FILE_SYS
:
461 string
= "Remote File System";
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);
475 err(__FUNCTION__
" - error getting bytes available request");
478 kfree (transfer_buffer
);
480 /* continue on with initialization */
485 static void visor_shutdown (struct usb_serial
*serial
)
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
);
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 */
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...");
524 if ((!port
->tty
) || (!port
->tty
->termios
)) {
525 dbg(__FUNCTION__
" - no tty structures");
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;
535 case CS8
: dbg(__FUNCTION__
" - data bits = 8"); break;
538 /* determine the parity */
541 dbg(__FUNCTION__
" - parity = odd");
543 dbg(__FUNCTION__
" - parity = even");
545 dbg(__FUNCTION__
" - parity = none");
547 /* figure out the stop bits requested */
549 dbg(__FUNCTION__
" - stop bits = 2");
551 dbg(__FUNCTION__
" - stop bits = 1");
554 /* figure out the flow control settings */
556 dbg(__FUNCTION__
" - RTS/CTS is enabled");
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
));
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
));
573 static int __init
visor_init (void)
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
;
586 err("No more urbs???");
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.");
602 static void __exit
visor_exit (void)
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");