File: /usr/src/linux/drivers/usb/serial/usbserial.c
1 /*
2 * USB Serial Converter driver
3 *
4 * Copyright (C) 1999 - 2001 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (c) 2000 Peter Berger (pberger@brimson.com)
6 * Copyright (c) 2000 Al Borchers (borchers@steinerpoint.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver was originally based on the ACM driver by Armin Fuerst (which was
14 * based on a driver by Brad Keryan)
15 *
16 * See Documentation/usb/usb-serial.txt for more information on using this driver
17 *
18 * (09/13/2001) gkh
19 * Moved generic driver initialize after we have registered with the USB
20 * core. Thanks to Randy Dunlap for pointing this problem out.
21 *
22 * (07/03/2001) gkh
23 * Fixed module paramater size. Thanks to John Brockmeyer for the pointer.
24 * Fixed vendor and product getting defined through the MODULE_PARM macro
25 * if the Generic driver wasn't compiled in.
26 * Fixed problem with generic_shutdown() not being called for drivers that
27 * don't have a shutdown() function.
28 *
29 * (06/06/2001) gkh
30 * added evil hack that is needed for the prolific pl2303 device due to the
31 * crazy way its endpoints are set up.
32 *
33 * (05/30/2001) gkh
34 * switched from using spinlock to a semaphore, which fixes lots of problems.
35 *
36 * (04/08/2001) gb
37 * Identify version on module load.
38 *
39 * 2001_02_05 gkh
40 * Fixed buffer overflows bug with the generic serial driver. Thanks to
41 * Todd Squires <squirest@ct0.com> for fixing this.
42 *
43 * (01/10/2001) gkh
44 * Fixed bug where the generic serial adaptor grabbed _any_ device that was
45 * offered to it.
46 *
47 * (12/12/2000) gkh
48 * Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
49 * moved them to the serial_open and serial_close functions.
50 * Also fixed bug with there not being a MOD_DEC for the generic driver
51 * (thanks to Gary Brubaker for finding this.)
52 *
53 * (11/29/2000) gkh
54 * Small NULL pointer initialization cleanup which saves a bit of disk image
55 *
56 * (11/01/2000) Adam J. Richter
57 * instead of using idVendor/idProduct pairs, usb serial drivers
58 * now identify their hardware interest with usb_device_id tables,
59 * which they usually have anyhow for use with MODULE_DEVICE_TABLE.
60 *
61 * (10/05/2000) gkh
62 * Fixed bug with urb->dev not being set properly, now that the usb
63 * core needs it.
64 *
65 * (09/11/2000) gkh
66 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
67 *
68 * (08/28/2000) gkh
69 * Added port_lock to port structure.
70 * Added locks for SMP safeness to generic driver
71 * Fixed the ability to open a generic device's port more than once.
72 *
73 * (07/23/2000) gkh
74 * Added bulk_out_endpointAddress to port structure.
75 *
76 * (07/19/2000) gkh, pberger, and borchers
77 * Modifications to allow usb-serial drivers to be modules.
78 *
79 * (07/03/2000) gkh
80 * Added more debugging to serial_ioctl call
81 *
82 * (06/25/2000) gkh
83 * Changed generic_write_bulk_callback to not call wake_up_interruptible
84 * directly, but to have port_softint do it at a safer time.
85 *
86 * (06/23/2000) gkh
87 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
88 *
89 * (05/22/2000) gkh
90 * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
91 * removed from the individual device source files.
92 *
93 * (05/03/2000) gkh
94 * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
95 *
96 * (05/02/2000) gkh
97 * Changed devfs and tty register code to work properly now. This was based on
98 * the ACM driver changes by Vojtech Pavlik.
99 *
100 * (04/27/2000) Ryan VanderBijl
101 * Put calls to *_paranoia_checks into one function.
102 *
103 * (04/23/2000) gkh
104 * Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
105 * Moved when the startup code printed out the devices that are supported.
106 *
107 * (04/19/2000) gkh
108 * Added driver for ZyXEL omni.net lcd plus ISDN TA
109 * Made startup info message specify which drivers were compiled in.
110 *
111 * (04/03/2000) gkh
112 * Changed the probe process to remove the module unload races.
113 * Changed where the tty layer gets initialized to have devfs work nicer.
114 * Added initial devfs support.
115 *
116 * (03/26/2000) gkh
117 * Split driver up into device specific pieces.
118 *
119 * (03/19/2000) gkh
120 * Fixed oops that could happen when device was removed while a program
121 * was talking to the device.
122 * Removed the static urbs and now all urbs are created and destroyed
123 * dynamically.
124 * Reworked the internal interface. Now everything is based on the
125 * usb_serial_port structure instead of the larger usb_serial structure.
126 * This fixes the bug that a multiport device could not have more than
127 * one port open at one time.
128 *
129 * (03/17/2000) gkh
130 * Added config option for debugging messages.
131 * Added patch for keyspan pda from Brian Warner.
132 *
133 * (03/06/2000) gkh
134 * Added the keyspan pda code from Brian Warner <warner@lothar.com>
135 * Moved a bunch of the port specific stuff into its own structure. This
136 * is in anticipation of the true multiport devices (there's a bug if you
137 * try to access more than one port of any multiport device right now)
138 *
139 * (02/21/2000) gkh
140 * Made it so that any serial devices only have to specify which functions
141 * they want to overload from the generic function calls (great,
142 * inheritance in C, in a driver, just what I wanted...)
143 * Added support for set_termios and ioctl function calls. No drivers take
144 * advantage of this yet.
145 * Removed the #ifdef MODULE, now there is no module specific code.
146 * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
147 * to Miles Lott).
148 * Small fix to get_free_serial.
149 *
150 * (02/14/2000) gkh
151 * Removed the Belkin and Peracom functionality from the driver due to
152 * the lack of support from the vendor, and me not wanting people to
153 * accidenatly buy the device, expecting it to work with Linux.
154 * Added read_bulk_callback and write_bulk_callback to the type structure
155 * for the needs of the FTDI and WhiteHEAT driver.
156 * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
157 * Ryder.
158 * Changed the output urb size back to the max endpoint size to make
159 * the ftdi_sio driver have it easier, and due to the fact that it didn't
160 * really increase the speed any.
161 *
162 * (02/11/2000) gkh
163 * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
164 * patch from Miles Lott (milos@insync.net).
165 * Fixed bug with not restoring the minor range that a device grabs, if
166 * the startup function fails (thanks Miles for finding this).
167 *
168 * (02/05/2000) gkh
169 * Added initial framework for the Keyspan PDA serial converter so that
170 * Brian Warner has a place to put his code.
171 * Made the ezusb specific functions generic enough that different
172 * devices can use them (whiteheat and keyspan_pda both need them).
173 * Split out a whole bunch of structure and other stuff to a seperate
174 * usb-serial.h file.
175 * Made the Visor connection messages a little more understandable, now
176 * that Miles Lott (milos@insync.net) has gotten the Generic channel to
177 * work. Also made them always show up in the log file.
178 *
179 * (01/25/2000) gkh
180 * Added initial framework for FTDI serial converter so that Bill Ryder
181 * has a place to put his code.
182 * Added the vendor specific info from Handspring. Now we can print out
183 * informational debug messages as well as understand what is happening.
184 *
185 * (01/23/2000) gkh
186 * Fixed problem of crash when trying to open a port that didn't have a
187 * device assigned to it. Made the minor node finding a little smarter,
188 * now it looks to find a continous space for the new device.
189 *
190 * (01/21/2000) gkh
191 * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
192 * Fixed get_serial_by_minor which was all messed up for multi port
193 * devices. Fixed multi port problem for generic devices. Now the number
194 * of ports is determined by the number of bulk out endpoints for the
195 * generic device.
196 *
197 * (01/19/2000) gkh
198 * Removed lots of cruft that was around from the old (pre urb) driver
199 * interface.
200 * Made the serial_table dynamic. This should save lots of memory when
201 * the number of minor nodes goes up to 256.
202 * Added initial support for devices that have more than one port.
203 * Added more debugging comments for the Visor, and added a needed
204 * set_configuration call.
205 *
206 * (01/17/2000) gkh
207 * Fixed the WhiteHEAT firmware (my processing tool had a bug)
208 * and added new debug loader firmware for it.
209 * Removed the put_char function as it isn't really needed.
210 * Added visor startup commands as found by the Win98 dump.
211 *
212 * (01/13/2000) gkh
213 * Fixed the vendor id for the generic driver to the one I meant it to be.
214 *
215 * (01/12/2000) gkh
216 * Forget the version numbering...that's pretty useless...
217 * Made the driver able to be compiled so that the user can select which
218 * converter they want to use. This allows people who only want the Visor
219 * support to not pay the memory size price of the WhiteHEAT.
220 * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
221 * grabbed the root hub. Not good.
222 *
223 * version 0.4.0 (01/10/2000) gkh
224 * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
225 * device. Added startup function to allow firmware to be downloaded to
226 * a device if it needs to be.
227 * Added firmware download logic to the WhiteHEAT device.
228 * Started to add #defines to split up the different drivers for potential
229 * configuration option.
230 *
231 * version 0.3.1 (12/30/99) gkh
232 * Fixed problems with urb for bulk out.
233 * Added initial support for multiple sets of endpoints. This enables
234 * the Handspring Visor to be attached successfully. Only the first
235 * bulk in / bulk out endpoint pair is being used right now.
236 *
237 * version 0.3.0 (12/27/99) gkh
238 * Added initial support for the Handspring Visor based on a patch from
239 * Miles Lott (milos@sneety.insync.net)
240 * Cleaned up the code a bunch and converted over to using urbs only.
241 *
242 * version 0.2.3 (12/21/99) gkh
243 * Added initial support for the Connect Tech WhiteHEAT converter.
244 * Incremented the number of ports in expectation of getting the
245 * WhiteHEAT to work properly (4 ports per connection).
246 * Added notification on insertion and removal of what port the
247 * device is/was connected to (and what kind of device it was).
248 *
249 * version 0.2.2 (12/16/99) gkh
250 * Changed major number to the new allocated number. We're legal now!
251 *
252 * version 0.2.1 (12/14/99) gkh
253 * Fixed bug that happens when device node is opened when there isn't a
254 * device attached to it. Thanks to marek@webdesign.no for noticing this.
255 *
256 * version 0.2.0 (11/10/99) gkh
257 * Split up internals to make it easier to add different types of serial
258 * converters to the code.
259 * Added a "generic" driver that gets it's vendor and product id
260 * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
261 * for the idea and sample code (from the usb scanner driver.)
262 * Cleared up any licensing questions by releasing it under the GNU GPL.
263 *
264 * version 0.1.2 (10/25/99) gkh
265 * Fixed bug in detecting device.
266 *
267 * version 0.1.1 (10/05/99) gkh
268 * Changed the major number to not conflict with anything else.
269 *
270 * version 0.1 (09/28/99) gkh
271 * Can recognize the two different devices and start up a read from
272 * device when asked to. Writes also work. No control signals yet, this
273 * all is vendor specific data (i.e. no spec), also no control for
274 * different baud rates or other bit settings.
275 * Currently we are using the same devid as the acm driver. This needs
276 * to change.
277 *
278 */
279
280 #include <linux/config.h>
281 #include <linux/kernel.h>
282 #include <linux/sched.h>
283 #include <linux/signal.h>
284 #include <linux/errno.h>
285 #include <linux/poll.h>
286 #include <linux/init.h>
287 #include <linux/slab.h>
288 #include <linux/fcntl.h>
289 #include <linux/tty.h>
290 #include <linux/tty_driver.h>
291 #include <linux/tty_flip.h>
292 #include <linux/module.h>
293 #include <linux/spinlock.h>
294 #include <linux/list.h>
295 #include <linux/smp_lock.h>
296 #include <linux/usb.h>
297
298 #ifdef CONFIG_USB_SERIAL_DEBUG
299 static int debug = 1;
300 #else
301 static int debug;
302 #endif
303
304 #include "usb-serial.h"
305 #include "pl2303.h"
306
307 /*
308 * Version Information
309 */
310 #define DRIVER_VERSION "v1.3"
311 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/"
312 #define DRIVER_DESC "USB Serial Driver core"
313
314 /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
315 /* need to always compile these in, as some of the other devices use these functions as their own. */
316 /* if a driver does not provide a function pointer, the generic function will be called. */
317 static int generic_open (struct usb_serial_port *port, struct file *filp);
318 static void generic_close (struct usb_serial_port *port, struct file *filp);
319 static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
320 static int generic_write_room (struct usb_serial_port *port);
321 static int generic_chars_in_buffer (struct usb_serial_port *port);
322 static void generic_read_bulk_callback (struct urb *urb);
323 static void generic_write_bulk_callback (struct urb *urb);
324 static void generic_shutdown (struct usb_serial *serial);
325
326
327 #ifdef CONFIG_USB_SERIAL_GENERIC
328 static __u16 vendor = 0x05f9;
329 static __u16 product = 0xffff;
330
331 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
332
333 /* All of the device info needed for the Generic Serial Converter */
334 static struct usb_serial_device_type generic_device = {
335 name: "Generic",
336 id_table: generic_device_ids,
337 needs_interrupt_in: DONT_CARE, /* don't have to have an interrupt in endpoint */
338 needs_bulk_in: DONT_CARE, /* don't have to have a bulk in endpoint */
339 needs_bulk_out: DONT_CARE, /* don't have to have a bulk out endpoint */
340 num_interrupt_in: NUM_DONT_CARE,
341 num_bulk_in: NUM_DONT_CARE,
342 num_bulk_out: NUM_DONT_CARE,
343 num_ports: 1,
344 shutdown: generic_shutdown,
345 };
346 #endif
347
348
349 /* local function prototypes */
350 static int serial_open (struct tty_struct *tty, struct file * filp);
351 static void serial_close (struct tty_struct *tty, struct file * filp);
352 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
353 static int serial_write_room (struct tty_struct *tty);
354 static int serial_chars_in_buffer (struct tty_struct *tty);
355 static void serial_throttle (struct tty_struct * tty);
356 static void serial_unthrottle (struct tty_struct * tty);
357 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg);
358 static void serial_set_termios (struct tty_struct *tty, struct termios * old);
359 static void serial_shutdown (struct usb_serial *serial);
360
361 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
362 const struct usb_device_id *id);
363 static void usb_serial_disconnect(struct usb_device *dev, void *ptr);
364
365 static struct usb_driver usb_serial_driver = {
366 name: "serial",
367 probe: usb_serial_probe,
368 disconnect: usb_serial_disconnect,
369 id_table: NULL, /* check all devices */
370 };
371
372 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
373 the MODULE_DEVICE_TABLE declarations in each serial driver
374 cause the "hotplug" program to pull in whatever module is necessary
375 via modprobe, and modprobe will load usbserial because the serial
376 drivers depend on it.
377 */
378
379
380 static int serial_refcount;
381 static struct tty_driver serial_tty_driver;
382 static struct tty_struct * serial_tty[SERIAL_TTY_MINORS];
383 static struct termios * serial_termios[SERIAL_TTY_MINORS];
384 static struct termios * serial_termios_locked[SERIAL_TTY_MINORS];
385 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
386
387
388 LIST_HEAD(usb_serial_driver_list);
389
390
391 static struct usb_serial *get_serial_by_minor (int minor)
392 {
393 return serial_table[minor];
394 }
395
396
397 static struct usb_serial *get_free_serial (int num_ports, int *minor)
398 {
399 struct usb_serial *serial = NULL;
400 int i, j;
401 int good_spot;
402
403 dbg(__FUNCTION__ " %d", num_ports);
404
405 *minor = 0;
406 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
407 if (serial_table[i])
408 continue;
409
410 good_spot = 1;
411 for (j = 1; j <= num_ports-1; ++j)
412 if (serial_table[i+j])
413 good_spot = 0;
414 if (good_spot == 0)
415 continue;
416
417 if (!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) {
418 err(__FUNCTION__ " - Out of memory");
419 return NULL;
420 }
421 memset(serial, 0, sizeof(struct usb_serial));
422 serial->magic = USB_SERIAL_MAGIC;
423 serial_table[i] = serial;
424 *minor = i;
425 dbg(__FUNCTION__ " - minor base = %d", *minor);
426 for (i = *minor+1; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
427 serial_table[i] = serial;
428 return serial;
429 }
430 return NULL;
431 }
432
433
434 static void return_serial (struct usb_serial *serial)
435 {
436 int i;
437
438 dbg(__FUNCTION__);
439
440 if (serial == NULL)
441 return;
442
443 for (i = 0; i < serial->num_ports; ++i) {
444 serial_table[serial->minor + i] = NULL;
445 }
446
447 return;
448 }
449
450
451 #ifdef USES_EZUSB_FUNCTIONS
452 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
453 #define CPUCS_REG 0x7F92
454
455 int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest)
456 {
457 int result;
458 unsigned char *transfer_buffer = kmalloc (length, GFP_KERNEL);
459
460 // dbg("ezusb_writememory %x, %d", address, length);
461
462 if (!transfer_buffer) {
463 err(__FUNCTION__ " - kmalloc(%d) failed.", length);
464 return -ENOMEM;
465 }
466 memcpy (transfer_buffer, data, length);
467 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), bRequest, 0x40, address, 0, transfer_buffer, length, 300);
468 kfree (transfer_buffer);
469 return result;
470 }
471
472
473 int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
474 {
475 int response;
476 dbg(__FUNCTION__ " - %d", reset_bit);
477 response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0);
478 if (response < 0) {
479 err(__FUNCTION__ "- %d failed", reset_bit);
480 }
481 return response;
482 }
483
484 #endif /* USES_EZUSB_FUNCTIONS */
485
486
487 /*****************************************************************************
488 * Driver tty interface functions
489 *****************************************************************************/
490 static int serial_open (struct tty_struct *tty, struct file * filp)
491 {
492 struct usb_serial *serial;
493 struct usb_serial_port *port;
494 int portNumber;
495
496 dbg(__FUNCTION__);
497
498 /* initialize the pointer incase something fails */
499 tty->driver_data = NULL;
500
501 /* get the serial object associated with this tty pointer */
502 serial = get_serial_by_minor (MINOR(tty->device));
503
504 if (serial_paranoia_check (serial, __FUNCTION__)) {
505 return -ENODEV;
506 }
507
508 MOD_INC_USE_COUNT;
509
510 /* set up our port structure making the tty driver remember our port object, and us it */
511 portNumber = MINOR(tty->device) - serial->minor;
512 port = &serial->port[portNumber];
513 tty->driver_data = port;
514 port->tty = tty;
515
516 /* pass on to the driver specific version of this function if it is available */
517 if (serial->type->open) {
518 return (serial->type->open(port, filp));
519 } else {
520 return (generic_open(port, filp));
521 }
522 }
523
524
525 static void serial_close(struct tty_struct *tty, struct file * filp)
526 {
527 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
528 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
529
530 if (!serial) {
531 return;
532 }
533
534 dbg(__FUNCTION__ " - port %d", port->number);
535
536 if (!port->active) {
537 dbg (__FUNCTION__ " - port not opened");
538 return;
539 }
540
541 /* pass on to the driver specific version of this function if it is available */
542 if (serial->type->close) {
543 serial->type->close(port, filp);
544 } else {
545 generic_close(port, filp);
546 }
547
548 MOD_DEC_USE_COUNT;
549 }
550
551
552 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
553 {
554 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
555 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
556
557 if (!serial) {
558 return -ENODEV;
559 }
560
561 dbg(__FUNCTION__ " - port %d, %d byte(s)", port->number, count);
562
563 if (!port->active) {
564 dbg (__FUNCTION__ " - port not opened");
565 return -EINVAL;
566 }
567
568 /* pass on to the driver specific version of this function if it is available */
569 if (serial->type->write) {
570 return (serial->type->write(port, from_user, buf, count));
571 } else {
572 return (generic_write(port, from_user, buf, count));
573 }
574 }
575
576
577 static int serial_write_room (struct tty_struct *tty)
578 {
579 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
580 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
581
582 if (!serial) {
583 return -ENODEV;
584 }
585
586 dbg(__FUNCTION__ " - port %d", port->number);
587
588 if (!port->active) {
589 dbg (__FUNCTION__ " - port not open");
590 return -EINVAL;
591 }
592
593 /* pass on to the driver specific version of this function if it is available */
594 if (serial->type->write_room) {
595 return (serial->type->write_room(port));
596 } else {
597 return (generic_write_room(port));
598 }
599 }
600
601
602 static int serial_chars_in_buffer (struct tty_struct *tty)
603 {
604 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
605 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
606
607 if (!serial) {
608 return -ENODEV;
609 }
610
611 if (!port->active) {
612 dbg (__FUNCTION__ " - port not open");
613 return -EINVAL;
614 }
615
616 /* pass on to the driver specific version of this function if it is available */
617 if (serial->type->chars_in_buffer) {
618 return (serial->type->chars_in_buffer(port));
619 } else {
620 return (generic_chars_in_buffer(port));
621 }
622 }
623
624
625 static void serial_throttle (struct tty_struct * tty)
626 {
627 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
628 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
629
630 if (!serial) {
631 return;
632 }
633
634 dbg(__FUNCTION__ " - port %d", port->number);
635
636 if (!port->active) {
637 dbg (__FUNCTION__ " - port not open");
638 return;
639 }
640
641 /* pass on to the driver specific version of this function */
642 if (serial->type->throttle) {
643 serial->type->throttle(port);
644 }
645
646 return;
647 }
648
649
650 static void serial_unthrottle (struct tty_struct * tty)
651 {
652 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
653 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
654
655 if (!serial) {
656 return;
657 }
658
659 dbg(__FUNCTION__ " - port %d", port->number);
660
661 if (!port->active) {
662 dbg (__FUNCTION__ " - port not open");
663 return;
664 }
665
666 /* pass on to the driver specific version of this function */
667 if (serial->type->unthrottle) {
668 serial->type->unthrottle(port);
669 }
670
671 return;
672 }
673
674
675 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
676 {
677 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
678 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
679
680 if (!serial) {
681 return -ENODEV;
682 }
683
684 dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);
685
686 if (!port->active) {
687 dbg (__FUNCTION__ " - port not open");
688 return -ENODEV;
689 }
690
691 /* pass on to the driver specific version of this function if it is available */
692 if (serial->type->ioctl) {
693 return (serial->type->ioctl(port, file, cmd, arg));
694 } else {
695 return -ENOIOCTLCMD;
696 }
697 }
698
699
700 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
701 {
702 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
703 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
704
705 if (!serial) {
706 return;
707 }
708
709 dbg(__FUNCTION__ " - port %d", port->number);
710
711 if (!port->active) {
712 dbg (__FUNCTION__ " - port not open");
713 return;
714 }
715
716 /* pass on to the driver specific version of this function if it is available */
717 if (serial->type->set_termios) {
718 serial->type->set_termios(port, old);
719 }
720
721 return;
722 }
723
724
725 static void serial_break (struct tty_struct *tty, int break_state)
726 {
727 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
728 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
729
730 if (!serial) {
731 return;
732 }
733
734 dbg(__FUNCTION__ " - port %d", port->number);
735
736 if (!port->active) {
737 dbg (__FUNCTION__ " - port not open");
738 return;
739 }
740
741 /* pass on to the driver specific version of this function if it is
742 available */
743 if (serial->type->break_ctl) {
744 serial->type->break_ctl(port, break_state);
745 }
746 }
747
748
749 static void serial_shutdown (struct usb_serial *serial)
750 {
751 if (serial->type->shutdown) {
752 serial->type->shutdown(serial);
753 } else {
754 generic_shutdown(serial);
755 }
756 }
757
758
759
760 /*****************************************************************************
761 * generic devices specific driver functions
762 *****************************************************************************/
763 static int generic_open (struct usb_serial_port *port, struct file *filp)
764 {
765 struct usb_serial *serial = port->serial;
766 int result = 0;
767
768 if (port_paranoia_check (port, __FUNCTION__))
769 return -ENODEV;
770
771 MOD_INC_USE_COUNT;
772
773 dbg(__FUNCTION__ " - port %d", port->number);
774
775 down (&port->sem);
776
777 ++port->open_count;
778
779 if (!port->active) {
780 port->active = 1;
781
782 /* force low_latency on so that our tty_push actually forces the data through,
783 otherwise it is scheduled, and with high data rates (like with OHCI) data
784 can get lost. */
785 port->tty->low_latency = 1;
786
787 /* if we have a bulk interrupt, start reading from it */
788 if (serial->num_bulk_in) {
789 /* Start reading from the device */
790 FILL_BULK_URB(port->read_urb, serial->dev,
791 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
792 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
793 ((serial->type->read_bulk_callback) ?
794 serial->type->read_bulk_callback :
795 generic_read_bulk_callback),
796 port);
797 result = usb_submit_urb(port->read_urb);
798 if (result)
799 err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
800 }
801 }
802
803 up (&port->sem);
804
805 return result;
806 }
807
808
809 static void generic_close (struct usb_serial_port *port, struct file * filp)
810 {
811 struct usb_serial *serial = port->serial;
812
813 dbg(__FUNCTION__ " - port %d", port->number);
814
815 down (&port->sem);
816
817 --port->open_count;
818
819 if (port->open_count <= 0) {
820 /* shutdown any bulk reads that might be going on */
821 if (serial->num_bulk_out)
822 usb_unlink_urb (port->write_urb);
823 if (serial->num_bulk_in)
824 usb_unlink_urb (port->read_urb);
825
826 port->active = 0;
827 port->open_count = 0;
828 }
829
830 up (&port->sem);
831 MOD_DEC_USE_COUNT;
832 }
833
834
835 static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
836 {
837 struct usb_serial *serial = port->serial;
838 int result;
839
840 dbg(__FUNCTION__ " - port %d", port->number);
841
842 if (count == 0) {
843 dbg(__FUNCTION__ " - write request of 0 bytes");
844 return (0);
845 }
846
847 /* only do something if we have a bulk out endpoint */
848 if (serial->num_bulk_out) {
849 if (port->write_urb->status == -EINPROGRESS) {
850 dbg (__FUNCTION__ " - already writing");
851 return (0);
852 }
853
854 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
855
856 if (from_user) {
857 if (copy_from_user(port->write_urb->transfer_buffer, buf, count))
858 return -EFAULT;
859 }
860 else {
861 memcpy (port->write_urb->transfer_buffer, buf, count);
862 }
863
864 usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
865
866 /* set up our urb */
867 FILL_BULK_URB(port->write_urb, serial->dev,
868 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
869 port->write_urb->transfer_buffer, count,
870 ((serial->type->write_bulk_callback) ?
871 serial->type->write_bulk_callback :
872 generic_write_bulk_callback),
873 port);
874
875 /* send the data out the bulk port */
876 result = usb_submit_urb(port->write_urb);
877 if (result)
878 err(__FUNCTION__ " - failed submitting write urb, error %d", result);
879 else
880 result = count;
881
882 return result;
883 }
884
885 /* no bulk out, so return 0 bytes written */
886 return (0);
887 }
888
889
890 static int generic_write_room (struct usb_serial_port *port)
891 {
892 struct usb_serial *serial = port->serial;
893 int room = 0;
894
895 dbg(__FUNCTION__ " - port %d", port->number);
896
897 if (serial->num_bulk_out) {
898 if (port->write_urb->status != -EINPROGRESS)
899 room = port->bulk_out_size;
900 }
901
902 dbg(__FUNCTION__ " - returns %d", room);
903 return (room);
904 }
905
906
907 static int generic_chars_in_buffer (struct usb_serial_port *port)
908 {
909 struct usb_serial *serial = port->serial;
910 int chars = 0;
911
912 dbg(__FUNCTION__ " - port %d", port->number);
913
914 if (serial->num_bulk_out) {
915 if (port->write_urb->status == -EINPROGRESS)
916 chars = port->write_urb->transfer_buffer_length;
917 }
918
919 dbg (__FUNCTION__ " - returns %d", chars);
920 return (chars);
921 }
922
923
924 static void generic_read_bulk_callback (struct urb *urb)
925 {
926 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
927 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
928 struct tty_struct *tty;
929 unsigned char *data = urb->transfer_buffer;
930 int i;
931 int result;
932
933 dbg(__FUNCTION__ " - port %d", port->number);
934
935 if (!serial) {
936 dbg(__FUNCTION__ " - bad serial pointer, exiting");
937 return;
938 }
939
940 if (urb->status) {
941 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
942 return;
943 }
944
945 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
946
947 tty = port->tty;
948 if (urb->actual_length) {
949 for (i = 0; i < urb->actual_length ; ++i) {
950 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
951 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
952 tty_flip_buffer_push(tty);
953 }
954 /* this doesn't actually push the data through unless tty->low_latency is set */
955 tty_insert_flip_char(tty, data[i], 0);
956 }
957 tty_flip_buffer_push(tty);
958 }
959
960 /* Continue trying to always read */
961 FILL_BULK_URB(port->read_urb, serial->dev,
962 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
963 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
964 ((serial->type->read_bulk_callback) ?
965 serial->type->read_bulk_callback :
966 generic_read_bulk_callback),
967 port);
968 result = usb_submit_urb(port->read_urb);
969 if (result)
970 err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
971 }
972
973
974 static void generic_write_bulk_callback (struct urb *urb)
975 {
976 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
977 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
978
979 dbg(__FUNCTION__ " - port %d", port->number);
980
981 if (!serial) {
982 dbg(__FUNCTION__ " - bad serial pointer, exiting");
983 return;
984 }
985
986 if (urb->status) {
987 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
988 return;
989 }
990
991 queue_task(&port->tqueue, &tq_immediate);
992 mark_bh(IMMEDIATE_BH);
993
994 return;
995 }
996
997
998 static void generic_shutdown (struct usb_serial *serial)
999 {
1000 int i;
1001
1002 dbg (__FUNCTION__);
1003
1004 /* stop reads and writes on all ports */
1005 for (i=0; i < serial->num_ports; ++i) {
1006 while (serial->port[i].open_count > 0) {
1007 generic_close (&serial->port[i], NULL);
1008 }
1009 }
1010 }
1011
1012
1013 static void port_softint(void *private)
1014 {
1015 struct usb_serial_port *port = (struct usb_serial_port *)private;
1016 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1017 struct tty_struct *tty;
1018
1019 dbg(__FUNCTION__ " - port %d", port->number);
1020
1021 if (!serial) {
1022 return;
1023 }
1024
1025 tty = port->tty;
1026 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
1027 dbg(__FUNCTION__ " - write wakeup call.");
1028 (tty->ldisc.write_wakeup)(tty);
1029 }
1030
1031 wake_up_interruptible(&tty->write_wait);
1032 }
1033
1034
1035
1036 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
1037 const struct usb_device_id *id)
1038 {
1039 struct usb_serial *serial = NULL;
1040 struct usb_serial_port *port;
1041 struct usb_interface *interface;
1042 struct usb_interface_descriptor *iface_desc;
1043 struct usb_endpoint_descriptor *endpoint;
1044 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
1045 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
1046 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
1047 struct usb_serial_device_type *type = NULL;
1048 struct list_head *tmp;
1049 int found;
1050 int minor;
1051 int buffer_size;
1052 int i;
1053 char interrupt_pipe;
1054 char bulk_in_pipe;
1055 char bulk_out_pipe;
1056 int num_interrupt_in = 0;
1057 int num_bulk_in = 0;
1058 int num_bulk_out = 0;
1059 int num_ports;
1060 int max_endpoints;
1061 const struct usb_device_id *id_pattern = NULL;
1062
1063
1064 /* loop through our list of known serial converters, and see if this
1065 device matches. */
1066 found = 0;
1067 interface = &dev->actconfig->interface[ifnum];
1068 list_for_each (tmp, &usb_serial_driver_list) {
1069 type = list_entry(tmp, struct usb_serial_device_type, driver_list);
1070 id_pattern = usb_match_id(dev, interface, type->id_table);
1071 if (id_pattern != NULL) {
1072 dbg("descriptor matches");
1073 found = 1;
1074 break;
1075 }
1076 }
1077 if (!found) {
1078 /* no match */
1079 dbg("none matched");
1080 return(NULL);
1081 }
1082
1083 /* descriptor matches, let's find the endpoints needed */
1084 interrupt_pipe = bulk_in_pipe = bulk_out_pipe = HAS_NOT;
1085
1086 /* check out the endpoints */
1087 iface_desc = &interface->altsetting[0];
1088 for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1089 endpoint = &iface_desc->endpoint[i];
1090
1091 if ((endpoint->bEndpointAddress & 0x80) &&
1092 ((endpoint->bmAttributes & 3) == 0x02)) {
1093 /* we found a bulk in endpoint */
1094 dbg("found bulk in");
1095 bulk_in_pipe = HAS;
1096 bulk_in_endpoint[num_bulk_in] = endpoint;
1097 ++num_bulk_in;
1098 }
1099
1100 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1101 ((endpoint->bmAttributes & 3) == 0x02)) {
1102 /* we found a bulk out endpoint */
1103 dbg("found bulk out");
1104 bulk_out_pipe = HAS;
1105 bulk_out_endpoint[num_bulk_out] = endpoint;
1106 ++num_bulk_out;
1107 }
1108
1109 if ((endpoint->bEndpointAddress & 0x80) &&
1110 ((endpoint->bmAttributes & 3) == 0x03)) {
1111 /* we found a interrupt in endpoint */
1112 dbg("found interrupt in");
1113 interrupt_pipe = HAS;
1114 interrupt_in_endpoint[num_interrupt_in] = endpoint;
1115 ++num_interrupt_in;
1116 }
1117 }
1118
1119 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
1120 /* BEGIN HORRIBLE HACK FOR PL2303 */
1121 /* this is needed due to the looney way its endpoints are set up */
1122 if (ifnum == 1) {
1123 if (((dev->descriptor.idVendor == PL2303_VENDOR_ID) &&
1124 (dev->descriptor.idProduct == PL2303_PRODUCT_ID)) ||
1125 ((dev->descriptor.idVendor == ATEN_VENDOR_ID) &&
1126 (dev->descriptor.idProduct == ATEN_PRODUCT_ID))) {
1127 /* check out the endpoints of the other interface*/
1128 interface = &dev->actconfig->interface[ifnum ^ 1];
1129 iface_desc = &interface->altsetting[0];
1130 for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1131 endpoint = &iface_desc->endpoint[i];
1132 if ((endpoint->bEndpointAddress & 0x80) &&
1133 ((endpoint->bmAttributes & 3) == 0x03)) {
1134 /* we found a interrupt in endpoint */
1135 dbg("found interrupt in for Prolific device on separate interface");
1136 interrupt_pipe = HAS;
1137 interrupt_in_endpoint[num_interrupt_in] = endpoint;
1138 ++num_interrupt_in;
1139 }
1140 }
1141 }
1142 }
1143 /* END HORRIBLE HACK FOR PL2303 */
1144 #endif
1145
1146 /* verify that we found all of the endpoints that we need */
1147 if (!((interrupt_pipe & type->needs_interrupt_in) &&
1148 (bulk_in_pipe & type->needs_bulk_in) &&
1149 (bulk_out_pipe & type->needs_bulk_out))) {
1150 /* nope, they don't match what we expected */
1151 info("descriptors matched, but endpoints did not");
1152 return NULL;
1153 }
1154
1155 /* found all that we need */
1156 info("%s converter detected", type->name);
1157
1158 #ifdef CONFIG_USB_SERIAL_GENERIC
1159 if (type == &generic_device) {
1160 num_ports = num_bulk_out;
1161 if (num_ports == 0) {
1162 err("Generic device with no bulk out, not allowed.");
1163 return NULL;
1164 }
1165 } else
1166 #endif
1167 num_ports = type->num_ports;
1168
1169 serial = get_free_serial (num_ports, &minor);
1170 if (serial == NULL) {
1171 err("No more free serial devices");
1172 return NULL;
1173 }
1174
1175 serial->dev = dev;
1176 serial->type = type;
1177 serial->interface = interface;
1178 serial->minor = minor;
1179 serial->num_ports = num_ports;
1180 serial->num_bulk_in = num_bulk_in;
1181 serial->num_bulk_out = num_bulk_out;
1182 serial->num_interrupt_in = num_interrupt_in;
1183
1184 /* if this device type has a startup function, call it */
1185 if (type->startup) {
1186 if (type->startup (serial)) {
1187 goto probe_error;
1188 }
1189 }
1190
1191 /* set up the endpoint information */
1192 for (i = 0; i < num_bulk_in; ++i) {
1193 endpoint = bulk_in_endpoint[i];
1194 port = &serial->port[i];
1195 port->read_urb = usb_alloc_urb (0);
1196 if (!port->read_urb) {
1197 err("No free urbs available");
1198 goto probe_error;
1199 }
1200 buffer_size = endpoint->wMaxPacketSize;
1201 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1202 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1203 if (!port->bulk_in_buffer) {
1204 err("Couldn't allocate bulk_in_buffer");
1205 goto probe_error;
1206 }
1207 FILL_BULK_URB(port->read_urb, dev,
1208 usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
1209 port->bulk_in_buffer, buffer_size,
1210 ((serial->type->read_bulk_callback) ?
1211 serial->type->read_bulk_callback :
1212 generic_read_bulk_callback),
1213 port);
1214 }
1215
1216 for (i = 0; i < num_bulk_out; ++i) {
1217 endpoint = bulk_out_endpoint[i];
1218 port = &serial->port[i];
1219 port->write_urb = usb_alloc_urb(0);
1220 if (!port->write_urb) {
1221 err("No free urbs available");
1222 goto probe_error;
1223 }
1224 buffer_size = endpoint->wMaxPacketSize;
1225 port->bulk_out_size = buffer_size;
1226 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1227 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
1228 if (!port->bulk_out_buffer) {
1229 err("Couldn't allocate bulk_out_buffer");
1230 goto probe_error;
1231 }
1232 FILL_BULK_URB(port->write_urb, dev,
1233 usb_sndbulkpipe(dev, endpoint->bEndpointAddress),
1234 port->bulk_out_buffer, buffer_size,
1235 ((serial->type->write_bulk_callback) ?
1236 serial->type->write_bulk_callback :
1237 generic_write_bulk_callback),
1238 port);
1239 }
1240
1241 for (i = 0; i < num_interrupt_in; ++i) {
1242 endpoint = interrupt_in_endpoint[i];
1243 port = &serial->port[i];
1244 port->interrupt_in_urb = usb_alloc_urb(0);
1245 if (!port->interrupt_in_urb) {
1246 err("No free urbs available");
1247 goto probe_error;
1248 }
1249 buffer_size = endpoint->wMaxPacketSize;
1250 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1251 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1252 if (!port->interrupt_in_buffer) {
1253 err("Couldn't allocate interrupt_in_buffer");
1254 goto probe_error;
1255 }
1256 FILL_INT_URB(port->interrupt_in_urb, dev,
1257 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1258 port->interrupt_in_buffer, buffer_size,
1259 serial->type->read_int_callback,
1260 port,
1261 endpoint->bInterval);
1262 }
1263
1264 /* initialize some parts of the port structures */
1265 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1266 max_endpoints = max(num_bulk_in, num_bulk_out);
1267 max_endpoints = max(max_endpoints, num_interrupt_in);
1268 max_endpoints = max(max_endpoints, (int)serial->num_ports);
1269 dbg (__FUNCTION__ " - setting up %d port structures for this device", max_endpoints);
1270 for (i = 0; i < max_endpoints; ++i) {
1271 port = &serial->port[i];
1272 port->number = i + serial->minor;
1273 port->serial = serial;
1274 port->magic = USB_SERIAL_PORT_MAGIC;
1275 port->tqueue.routine = port_softint;
1276 port->tqueue.data = port;
1277 init_MUTEX (&port->sem);
1278 }
1279
1280 /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
1281 for (i = 0; i < serial->num_ports; ++i) {
1282 tty_register_devfs (&serial_tty_driver, 0, serial->port[i].number);
1283 info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)",
1284 type->name, serial->port[i].number, serial->port[i].number);
1285 }
1286
1287 return serial; /* success */
1288
1289
1290 probe_error:
1291 for (i = 0; i < num_bulk_in; ++i) {
1292 port = &serial->port[i];
1293 if (port->read_urb)
1294 usb_free_urb (port->read_urb);
1295 if (port->bulk_in_buffer)
1296 kfree (port->bulk_in_buffer);
1297 }
1298 for (i = 0; i < num_bulk_out; ++i) {
1299 port = &serial->port[i];
1300 if (port->write_urb)
1301 usb_free_urb (port->write_urb);
1302 if (port->bulk_out_buffer)
1303 kfree (port->bulk_out_buffer);
1304 }
1305 for (i = 0; i < num_interrupt_in; ++i) {
1306 port = &serial->port[i];
1307 if (port->interrupt_in_urb)
1308 usb_free_urb (port->interrupt_in_urb);
1309 if (port->interrupt_in_buffer)
1310 kfree (port->interrupt_in_buffer);
1311 }
1312
1313 /* return the minor range that this device had */
1314 return_serial (serial);
1315
1316 /* free up any memory that we allocated */
1317 kfree (serial);
1318 return NULL;
1319 }
1320
1321
1322 static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
1323 {
1324 struct usb_serial *serial = (struct usb_serial *) ptr;
1325 struct usb_serial_port *port;
1326 int i;
1327
1328 if (serial) {
1329 /* fail all future close/read/write/ioctl/etc calls */
1330 for (i = 0; i < serial->num_ports; ++i) {
1331 if (serial->port[i].tty != NULL)
1332 serial->port[i].tty->driver_data = NULL;
1333 }
1334
1335 serial_shutdown (serial);
1336
1337 for (i = 0; i < serial->num_ports; ++i)
1338 serial->port[i].active = 0;
1339
1340 for (i = 0; i < serial->num_bulk_in; ++i) {
1341 port = &serial->port[i];
1342 if (port->read_urb) {
1343 usb_unlink_urb (port->read_urb);
1344 usb_free_urb (port->read_urb);
1345 }
1346 if (port->bulk_in_buffer)
1347 kfree (port->bulk_in_buffer);
1348 }
1349 for (i = 0; i < serial->num_bulk_out; ++i) {
1350 port = &serial->port[i];
1351 if (port->write_urb) {
1352 usb_unlink_urb (port->write_urb);
1353 usb_free_urb (port->write_urb);
1354 }
1355 if (port->bulk_out_buffer)
1356 kfree (port->bulk_out_buffer);
1357 }
1358 for (i = 0; i < serial->num_interrupt_in; ++i) {
1359 port = &serial->port[i];
1360 if (port->interrupt_in_urb) {
1361 usb_unlink_urb (port->interrupt_in_urb);
1362 usb_free_urb (port->interrupt_in_urb);
1363 }
1364 if (port->interrupt_in_buffer)
1365 kfree (port->interrupt_in_buffer);
1366 }
1367
1368 for (i = 0; i < serial->num_ports; ++i) {
1369 tty_unregister_devfs (&serial_tty_driver, serial->port[i].number);
1370 info("%s converter now disconnected from ttyUSB%d", serial->type->name, serial->port[i].number);
1371 }
1372
1373 /* return the minor range that this device had */
1374 return_serial (serial);
1375
1376 /* free up any memory that we allocated */
1377 kfree (serial);
1378
1379 } else {
1380 info("device disconnected");
1381 }
1382
1383 }
1384
1385
1386 static struct tty_driver serial_tty_driver = {
1387 magic: TTY_DRIVER_MAGIC,
1388 driver_name: "usb-serial",
1389 name: "usb/tts/%d",
1390 major: SERIAL_TTY_MAJOR,
1391 minor_start: 0,
1392 num: SERIAL_TTY_MINORS,
1393 type: TTY_DRIVER_TYPE_SERIAL,
1394 subtype: SERIAL_TYPE_NORMAL,
1395 flags: TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1396
1397 refcount: &serial_refcount,
1398 table: serial_tty,
1399 termios: serial_termios,
1400 termios_locked: serial_termios_locked,
1401
1402 open: serial_open,
1403 close: serial_close,
1404 write: serial_write,
1405 write_room: serial_write_room,
1406 ioctl: serial_ioctl,
1407 set_termios: serial_set_termios,
1408 throttle: serial_throttle,
1409 unthrottle: serial_unthrottle,
1410 break_ctl: serial_break,
1411 chars_in_buffer: serial_chars_in_buffer,
1412 };
1413
1414
1415 int usb_serial_init(void)
1416 {
1417 int i;
1418 int result;
1419
1420 /* Initalize our global data */
1421 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1422 serial_table[i] = NULL;
1423 }
1424
1425 /* register the tty driver */
1426 serial_tty_driver.init_termios = tty_std_termios;
1427 serial_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1428 if (tty_register_driver (&serial_tty_driver)) {
1429 err(__FUNCTION__ " - failed to register tty driver");
1430 return -1;
1431 }
1432
1433 /* register the USB driver */
1434 result = usb_register(&usb_serial_driver);
1435 if (result < 0) {
1436 tty_unregister_driver(&serial_tty_driver);
1437 err("usb_register failed for the usb-serial driver. Error number %d", result);
1438 return -1;
1439 }
1440
1441 #ifdef CONFIG_USB_SERIAL_GENERIC
1442 generic_device_ids[0].idVendor = vendor;
1443 generic_device_ids[0].idProduct = product;
1444 generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
1445 /* register our generic driver with ourselves */
1446 usb_serial_register (&generic_device);
1447 #endif
1448
1449 info(DRIVER_DESC " " DRIVER_VERSION);
1450
1451 return 0;
1452 }
1453
1454
1455 void usb_serial_exit(void)
1456 {
1457
1458 #ifdef CONFIG_USB_SERIAL_GENERIC
1459 /* remove our generic driver */
1460 usb_serial_deregister (&generic_device);
1461 #endif
1462
1463 usb_deregister(&usb_serial_driver);
1464 tty_unregister_driver(&serial_tty_driver);
1465 }
1466
1467
1468 module_init(usb_serial_init);
1469 module_exit(usb_serial_exit);
1470
1471
1472 int usb_serial_register(struct usb_serial_device_type *new_device)
1473 {
1474 /* Add this device to our list of devices */
1475 list_add(&new_device->driver_list, &usb_serial_driver_list);
1476
1477 info ("USB Serial support registered for %s", new_device->name);
1478
1479 usb_scan_devices();
1480
1481 return 0;
1482 }
1483
1484
1485 void usb_serial_deregister(struct usb_serial_device_type *device)
1486 {
1487 struct usb_serial *serial;
1488 int i;
1489
1490 info("USB Serial deregistering driver %s", device->name);
1491
1492 /* clear out the serial_table if the device is attached to a port */
1493 for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
1494 serial = serial_table[i];
1495 if ((serial != NULL) && (serial->type == device)) {
1496 usb_driver_release_interface (&usb_serial_driver, serial->interface);
1497 usb_serial_disconnect (NULL, serial);
1498 }
1499 }
1500
1501 list_del(&device->driver_list);
1502 }
1503
1504
1505
1506 /* If the usb-serial core is build into the core, the usb-serial drivers
1507 need these symbols to load properly as modules. */
1508 EXPORT_SYMBOL(usb_serial_register);
1509 EXPORT_SYMBOL(usb_serial_deregister);
1510 #ifdef USES_EZUSB_FUNCTIONS
1511 EXPORT_SYMBOL(ezusb_writememory);
1512 EXPORT_SYMBOL(ezusb_set_reset);
1513 #endif
1514
1515
1516 /* Module information */
1517 MODULE_AUTHOR( DRIVER_AUTHOR );
1518 MODULE_DESCRIPTION( DRIVER_DESC );
1519 MODULE_LICENSE("GPL");
1520
1521 MODULE_PARM(debug, "i");
1522 MODULE_PARM_DESC(debug, "Debug enabled or not");
1523
1524 #ifdef CONFIG_USB_SERIAL_GENERIC
1525 MODULE_PARM(vendor, "h");
1526 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
1527
1528 MODULE_PARM(product, "h");
1529 MODULE_PARM_DESC(product, "User specified USB idProduct");
1530 #endif
1531