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
18 * Fixed bug with urb->dev not being set properly, now that the usb
22 * Got rid of always calling kmalloc for every urb we wrote out to the
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
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.
33 * Added locks for SMP safeness.
36 * Fixed endian problem in visor_startup.
37 * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
41 * Added pool of write urbs to speed up transfers to the visor.
44 * Added module_init and module_exit functions to handle the fact that this
45 * driver is a loadable module now.
48 * Added visor_set_ioctl and visor_set_termios functions (they don't do much
49 * of anything, but are good for debugging.)
52 * Fixed bug in visor_unthrottle that should help with the disconnect in PPP
53 * bug that people have been reporting.
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
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
86 #include <linux/usb.h>
88 #include "usb-serial.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",
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 */
130 throttle
: visor_throttle
,
131 unthrottle
: visor_unthrottle
,
132 startup
: visor_startup
,
133 shutdown
: visor_shutdown
,
135 set_termios
: visor_set_termios
,
137 write_bulk_callback
: visor_write_bulk_callback
,
138 read_bulk_callback
: visor_read_bulk_callback
,
143 #define URB_TRANSFER_BUFFER_SIZE 64
144 static struct urb
*write_urb_pool
[NUM_URBS
];
145 static spinlock_t write_urb_pool_lock
;
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
;
159 if (port_paranoia_check (port
, __FUNCTION__
))
162 dbg(__FUNCTION__
" - port %d", port
->number
);
164 spin_lock_irqsave (&port
->port_lock
, flags
);
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
);
181 err(__FUNCTION__
" - failed submitting read urb, error %d", result
);
184 spin_unlock_irqrestore (&port
->port_lock
, flags
);
190 static void visor_close (struct usb_serial_port
*port
, struct file
* filp
)
192 struct usb_serial
*serial
;
193 unsigned char *transfer_buffer
;
196 if (port_paranoia_check (port
, __FUNCTION__
))
199 dbg(__FUNCTION__
" - port %d", port
->number
);
201 serial
= get_usb_serial (port
, __FUNCTION__
);
205 spin_lock_irqsave (&port
->port_lock
, flags
);
210 if (port
->open_count
<= 0) {
211 transfer_buffer
= kmalloc (0x12, GFP_KERNEL
);
212 if (!transfer_buffer
) {
213 err(__FUNCTION__
" - kmalloc(%d) failed.", 0x12);
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
);
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
;
238 const unsigned char *current_position
= buf
;
245 dbg(__FUNCTION__
" - port %d", port
->number
);
247 usb_serial_debug_data (__FILE__
, __FUNCTION__
, count
, buf
);
250 /* try to find a free urb in our list of them */
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
];
259 spin_unlock_irqrestore (&write_urb_pool_lock
, flags
);
261 dbg (__FUNCTION__
" - no more free urbs");
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...");
272 transfer_size
= MIN (count
, URB_TRANSFER_BUFFER_SIZE
);
274 copy_from_user (urb
->transfer_buffer
, current_position
, transfer_size
);
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
);
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
;
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__
))
308 dbg(__FUNCTION__
" - port %d", port
->number
);
311 dbg(__FUNCTION__
" - nonzero write bulk status received: %d", urb
->status
);
315 queue_task(&port
->tqueue
, &tq_immediate
);
316 mark_bh(IMMEDIATE_BH
);
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
;
331 if (port_paranoia_check (port
, __FUNCTION__
))
334 dbg(__FUNCTION__
" - port %d", port
->number
);
337 dbg(__FUNCTION__
" - bad serial pointer, exiting");
342 dbg(__FUNCTION__
" - nonzero read bulk status received: %d", urb
->status
);
346 usb_serial_debug_data (__FILE__
, __FUNCTION__
, urb
->actual_length
, data
);
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
);
364 err(__FUNCTION__
" - failed resubmitting read urb, error %d", result
);
369 static void visor_throttle (struct usb_serial_port
*port
)
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
);
385 static void visor_unthrottle (struct usb_serial_port
*port
)
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
);
397 err(__FUNCTION__
" - failed submitting read urb, error %d", result
);
399 spin_unlock_irqrestore (&port
->port_lock
, flags
);
405 static int visor_startup (struct usb_serial
*serial
)
409 unsigned char *transfer_buffer
= kmalloc (256, GFP_KERNEL
);
411 if (!transfer_buffer
) {
412 err(__FUNCTION__
" - kmalloc(%d) failed.", 256);
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);
425 err(__FUNCTION__
" - error getting connection information");
427 struct visor_connection_info
*connection_info
= (struct visor_connection_info
*)transfer_buffer
;
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
:
437 case VISOR_FUNCTION_DEBUGGER
:
440 case VISOR_FUNCTION_HOTSYNC
:
443 case VISOR_FUNCTION_CONSOLE
:
446 case VISOR_FUNCTION_REMOTE_FILE_SYS
:
447 string
= "Remote File System";
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);
461 err(__FUNCTION__
" - error getting bytes available request");
464 kfree (transfer_buffer
);
466 /* continue on with initialization */
471 static void visor_shutdown (struct usb_serial
*serial
)
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
);
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 */
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...");
510 if ((!port
->tty
) || (!port
->tty
->termios
)) {
511 dbg(__FUNCTION__
" - no tty structures");
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;
521 case CS8
: dbg(__FUNCTION__
" - data bits = 8"); break;
524 /* determine the parity */
527 dbg(__FUNCTION__
" - parity = odd");
529 dbg(__FUNCTION__
" - parity = even");
531 dbg(__FUNCTION__
" - parity = none");
533 /* figure out the stop bits requested */
535 dbg(__FUNCTION__
" - stop bits = 2");
537 dbg(__FUNCTION__
" - stop bits = 1");
540 /* figure out the flow control settings */
542 dbg(__FUNCTION__
" - RTS/CTS is enabled");
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
));
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
));
559 static int __init
visor_init (void)
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
;
572 err("No more urbs???");
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.");
588 static void __exit
visor_exit (void)
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");