[PATCH] USB console: fix disconnection issues
[linux-2.6-block.git] / drivers / usb / serial / usb-serial.c
CommitLineData
1da177e4
LT
1/*
2 * USB Serial Converter driver
3 *
502b95c1 4 * Copyright (C) 1999 - 2005 Greg Kroah-Hartman (greg@kroah.com)
1da177e4
LT
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
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
502b95c1 12 * This driver was originally based on the ACM driver by Armin Fuerst (which was
1da177e4
LT
13 * based on a driver by Brad Keryan)
14 *
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
16 *
1da177e4
LT
17 */
18
19#include <linux/config.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/tty.h>
25#include <linux/tty_driver.h>
26#include <linux/tty_flip.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/spinlock.h>
1ce7dd26 30#include <linux/mutex.h>
1da177e4
LT
31#include <linux/list.h>
32#include <linux/smp_lock.h>
33#include <asm/uaccess.h>
34#include <linux/usb.h>
35#include "usb-serial.h"
36#include "pl2303.h"
37
38/*
39 * Version Information
40 */
1da177e4
LT
41#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
42#define DRIVER_DESC "USB Serial Driver core"
43
44/* Driver structure we register with the USB core */
45static struct usb_driver usb_serial_driver = {
1da177e4
LT
46 .name = "usbserial",
47 .probe = usb_serial_probe,
48 .disconnect = usb_serial_disconnect,
ba9dc657 49 .no_dynamic_id = 1,
1da177e4
LT
50};
51
52/* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
53 the MODULE_DEVICE_TABLE declarations in each serial driver
54 cause the "hotplug" program to pull in whatever module is necessary
55 via modprobe, and modprobe will load usbserial because the serial
56 drivers depend on it.
57*/
58
59static int debug;
60static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
61static LIST_HEAD(usb_serial_driver_list);
62
63struct usb_serial *usb_serial_get_by_index(unsigned index)
64{
65 struct usb_serial *serial = serial_table[index];
66
67 if (serial)
68 kref_get(&serial->kref);
69 return serial;
70}
71
72static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
73{
74 unsigned int i, j;
75 int good_spot;
76
77 dbg("%s %d", __FUNCTION__, num_ports);
78
79 *minor = 0;
80 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
81 if (serial_table[i])
82 continue;
83
84 good_spot = 1;
85 for (j = 1; j <= num_ports-1; ++j)
86 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
87 good_spot = 0;
88 i += j;
89 break;
90 }
91 if (good_spot == 0)
92 continue;
93
94 *minor = i;
95 dbg("%s - minor base = %d", __FUNCTION__, *minor);
96 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
97 serial_table[i] = serial;
98 return serial;
99 }
100 return NULL;
101}
102
103static void return_serial(struct usb_serial *serial)
104{
105 int i;
106
107 dbg("%s", __FUNCTION__);
108
109 if (serial == NULL)
110 return;
111
112 for (i = 0; i < serial->num_ports; ++i) {
113 serial_table[serial->minor + i] = NULL;
114 }
115}
116
117static void destroy_serial(struct kref *kref)
118{
119 struct usb_serial *serial;
120 struct usb_serial_port *port;
121 int i;
122
123 serial = to_usb_serial(kref);
124
269bda1c 125 dbg("%s - %s", __FUNCTION__, serial->type->description);
1da177e4
LT
126
127 serial->type->shutdown(serial);
128
129 /* return the minor range that this device had */
130 return_serial(serial);
131
132 for (i = 0; i < serial->num_ports; ++i)
133 serial->port[i]->open_count = 0;
134
135 /* the ports are cleaned up and released in port_release() */
136 for (i = 0; i < serial->num_ports; ++i)
137 if (serial->port[i]->dev.parent != NULL) {
138 device_unregister(&serial->port[i]->dev);
139 serial->port[i] = NULL;
140 }
141
142 /* If this is a "fake" port, we have to clean it up here, as it will
143 * not get cleaned up in port_release() as it was never registered with
144 * the driver core */
145 if (serial->num_ports < serial->num_port_pointers) {
146 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
147 port = serial->port[i];
148 if (!port)
149 continue;
150 usb_kill_urb(port->read_urb);
151 usb_free_urb(port->read_urb);
152 usb_kill_urb(port->write_urb);
153 usb_free_urb(port->write_urb);
154 usb_kill_urb(port->interrupt_in_urb);
155 usb_free_urb(port->interrupt_in_urb);
156 usb_kill_urb(port->interrupt_out_urb);
157 usb_free_urb(port->interrupt_out_urb);
158 kfree(port->bulk_in_buffer);
159 kfree(port->bulk_out_buffer);
160 kfree(port->interrupt_in_buffer);
161 kfree(port->interrupt_out_buffer);
162 }
163 }
164
165 usb_put_dev(serial->dev);
166
167 /* free up any memory that we allocated */
168 kfree (serial);
169}
170
73e487fd
GL
171void usb_serial_put(struct usb_serial *serial)
172{
173 kref_put(&serial->kref, destroy_serial);
174}
175
1da177e4
LT
176/*****************************************************************************
177 * Driver tty interface functions
178 *****************************************************************************/
179static int serial_open (struct tty_struct *tty, struct file * filp)
180{
181 struct usb_serial *serial;
182 struct usb_serial_port *port;
183 unsigned int portNumber;
184 int retval;
185
186 dbg("%s", __FUNCTION__);
187
188 /* get the serial object associated with this tty pointer */
189 serial = usb_serial_get_by_index(tty->index);
190 if (!serial) {
191 tty->driver_data = NULL;
192 return -ENODEV;
193 }
194
195 portNumber = tty->index - serial->minor;
196 port = serial->port[portNumber];
71a84163
LFC
197 if (!port) {
198 retval = -ENODEV;
199 goto bailout_kref_put;
200 }
8a4613f0 201
71a84163
LFC
202 if (mutex_lock_interruptible(&port->mutex)) {
203 retval = -ERESTARTSYS;
204 goto bailout_kref_put;
205 }
1da177e4
LT
206
207 ++port->open_count;
208
ca85485c
PF
209 /* set up our port structure making the tty driver
210 * remember our port object, and us it */
211 tty->driver_data = port;
212 port->tty = tty;
1da177e4 213
ca85485c 214 if (port->open_count == 1) {
1da177e4
LT
215
216 /* lock this module before we call it
217 * this may fail, which means we must bail out,
218 * safe because we are called with BKL held */
18fcac35 219 if (!try_module_get(serial->type->driver.owner)) {
1da177e4 220 retval = -ENODEV;
71a84163 221 goto bailout_mutex_unlock;
1da177e4
LT
222 }
223
224 /* only call the device specific open if this
225 * is the first time the port is opened */
226 retval = serial->type->open(port, filp);
227 if (retval)
228 goto bailout_module_put;
229 }
230
1ce7dd26 231 mutex_unlock(&port->mutex);
1da177e4
LT
232 return 0;
233
234bailout_module_put:
18fcac35 235 module_put(serial->type->driver.owner);
71a84163 236bailout_mutex_unlock:
1da177e4 237 port->open_count = 0;
1ce7dd26 238 mutex_unlock(&port->mutex);
71a84163 239bailout_kref_put:
73e487fd 240 usb_serial_put(serial);
1da177e4
LT
241 return retval;
242}
243
244static void serial_close(struct tty_struct *tty, struct file * filp)
245{
81671ddb 246 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
247
248 if (!port)
249 return;
250
251 dbg("%s - port %d", __FUNCTION__, port->number);
252
1ce7dd26 253 mutex_lock(&port->mutex);
8a4613f0 254
91c0bce2 255 if (port->open_count == 0) {
1ce7dd26 256 mutex_unlock(&port->mutex);
91c0bce2
GKH
257 return;
258 }
1da177e4
LT
259
260 --port->open_count;
261 if (port->open_count == 0) {
262 /* only call the device specific close if this
263 * port is being closed by the last owner */
264 port->serial->type->close(port, filp);
265
266 if (port->tty) {
267 if (port->tty->driver_data)
268 port->tty->driver_data = NULL;
269 port->tty = NULL;
270 }
271
18fcac35 272 module_put(port->serial->type->driver.owner);
1da177e4
LT
273 }
274
1ce7dd26 275 mutex_unlock(&port->mutex);
73e487fd 276 usb_serial_put(port->serial);
1da177e4
LT
277}
278
279static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
280{
81671ddb 281 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
282 int retval = -EINVAL;
283
73e487fd 284 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
487f9c67
LFC
285 goto exit;
286
1da177e4
LT
287 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
288
289 if (!port->open_count) {
290 dbg("%s - port not opened", __FUNCTION__);
291 goto exit;
292 }
293
294 /* pass on to the driver specific version of this function */
295 retval = port->serial->type->write(port, buf, count);
296
297exit:
298 return retval;
299}
300
301static int serial_write_room (struct tty_struct *tty)
302{
81671ddb 303 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
304 int retval = -EINVAL;
305
487f9c67
LFC
306 if (!port)
307 goto exit;
308
1da177e4
LT
309 dbg("%s - port %d", __FUNCTION__, port->number);
310
311 if (!port->open_count) {
312 dbg("%s - port not open", __FUNCTION__);
313 goto exit;
314 }
315
316 /* pass on to the driver specific version of this function */
317 retval = port->serial->type->write_room(port);
318
319exit:
320 return retval;
321}
322
323static int serial_chars_in_buffer (struct tty_struct *tty)
324{
81671ddb 325 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
326 int retval = -EINVAL;
327
487f9c67
LFC
328 if (!port)
329 goto exit;
330
1da177e4
LT
331 dbg("%s = port %d", __FUNCTION__, port->number);
332
333 if (!port->open_count) {
334 dbg("%s - port not open", __FUNCTION__);
335 goto exit;
336 }
337
338 /* pass on to the driver specific version of this function */
339 retval = port->serial->type->chars_in_buffer(port);
340
341exit:
342 return retval;
343}
344
345static void serial_throttle (struct tty_struct * tty)
346{
81671ddb 347 struct usb_serial_port *port = tty->driver_data;
1da177e4 348
487f9c67
LFC
349 if (!port)
350 return;
351
1da177e4
LT
352 dbg("%s - port %d", __FUNCTION__, port->number);
353
354 if (!port->open_count) {
355 dbg ("%s - port not open", __FUNCTION__);
356 return;
357 }
358
359 /* pass on to the driver specific version of this function */
360 if (port->serial->type->throttle)
361 port->serial->type->throttle(port);
362}
363
364static void serial_unthrottle (struct tty_struct * tty)
365{
81671ddb 366 struct usb_serial_port *port = tty->driver_data;
1da177e4 367
487f9c67
LFC
368 if (!port)
369 return;
370
1da177e4
LT
371 dbg("%s - port %d", __FUNCTION__, port->number);
372
373 if (!port->open_count) {
374 dbg("%s - port not open", __FUNCTION__);
375 return;
376 }
377
378 /* pass on to the driver specific version of this function */
379 if (port->serial->type->unthrottle)
380 port->serial->type->unthrottle(port);
381}
382
383static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
384{
81671ddb 385 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
386 int retval = -ENODEV;
387
487f9c67
LFC
388 if (!port)
389 goto exit;
390
1da177e4
LT
391 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
392
393 if (!port->open_count) {
394 dbg ("%s - port not open", __FUNCTION__);
395 goto exit;
396 }
397
398 /* pass on to the driver specific version of this function if it is available */
399 if (port->serial->type->ioctl)
400 retval = port->serial->type->ioctl(port, file, cmd, arg);
401 else
402 retval = -ENOIOCTLCMD;
403
404exit:
405 return retval;
406}
407
408static void serial_set_termios (struct tty_struct *tty, struct termios * old)
409{
81671ddb 410 struct usb_serial_port *port = tty->driver_data;
1da177e4 411
487f9c67
LFC
412 if (!port)
413 return;
414
1da177e4
LT
415 dbg("%s - port %d", __FUNCTION__, port->number);
416
417 if (!port->open_count) {
418 dbg("%s - port not open", __FUNCTION__);
419 return;
420 }
421
422 /* pass on to the driver specific version of this function if it is available */
423 if (port->serial->type->set_termios)
424 port->serial->type->set_termios(port, old);
425}
426
427static void serial_break (struct tty_struct *tty, int break_state)
428{
81671ddb 429 struct usb_serial_port *port = tty->driver_data;
1da177e4 430
487f9c67
LFC
431 if (!port)
432 return;
433
1da177e4
LT
434 dbg("%s - port %d", __FUNCTION__, port->number);
435
436 if (!port->open_count) {
437 dbg("%s - port not open", __FUNCTION__);
438 return;
439 }
440
441 /* pass on to the driver specific version of this function if it is available */
442 if (port->serial->type->break_ctl)
443 port->serial->type->break_ctl(port, break_state);
444}
445
446static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
447{
448 struct usb_serial *serial;
449 int length = 0;
450 int i;
451 off_t begin = 0;
452 char tmp[40];
453
454 dbg("%s", __FUNCTION__);
17a882fc 455 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
1da177e4
LT
456 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
457 serial = usb_serial_get_by_index(i);
458 if (serial == NULL)
459 continue;
460
461 length += sprintf (page+length, "%d:", i);
18fcac35
GKH
462 if (serial->type->driver.owner)
463 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
269bda1c 464 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
1da177e4
LT
465 length += sprintf (page+length, " vendor:%04x product:%04x",
466 le16_to_cpu(serial->dev->descriptor.idVendor),
467 le16_to_cpu(serial->dev->descriptor.idProduct));
468 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
469 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
470
471 usb_make_path(serial->dev, tmp, sizeof(tmp));
472 length += sprintf (page+length, " path:%s", tmp);
473
474 length += sprintf (page+length, "\n");
475 if ((length + begin) > (off + count))
476 goto done;
477 if ((length + begin) < off) {
478 begin += length;
479 length = 0;
480 }
73e487fd 481 usb_serial_put(serial);
1da177e4
LT
482 }
483 *eof = 1;
484done:
485 if (off >= (length + begin))
486 return 0;
487 *start = page + (off-begin);
488 return ((count < begin+length-off) ? count : begin+length-off);
489}
490
491static int serial_tiocmget (struct tty_struct *tty, struct file *file)
492{
81671ddb 493 struct usb_serial_port *port = tty->driver_data;
1da177e4 494
487f9c67
LFC
495 if (!port)
496 goto exit;
497
1da177e4
LT
498 dbg("%s - port %d", __FUNCTION__, port->number);
499
500 if (!port->open_count) {
501 dbg("%s - port not open", __FUNCTION__);
502 goto exit;
503 }
504
505 if (port->serial->type->tiocmget)
506 return port->serial->type->tiocmget(port, file);
507
508exit:
509 return -EINVAL;
510}
511
512static int serial_tiocmset (struct tty_struct *tty, struct file *file,
513 unsigned int set, unsigned int clear)
514{
81671ddb 515 struct usb_serial_port *port = tty->driver_data;
1da177e4 516
487f9c67
LFC
517 if (!port)
518 goto exit;
519
1da177e4
LT
520 dbg("%s - port %d", __FUNCTION__, port->number);
521
522 if (!port->open_count) {
523 dbg("%s - port not open", __FUNCTION__);
524 goto exit;
525 }
526
527 if (port->serial->type->tiocmset)
528 return port->serial->type->tiocmset(port, file, set, clear);
529
530exit:
531 return -EINVAL;
532}
533
534void usb_serial_port_softint(void *private)
535{
81671ddb 536 struct usb_serial_port *port = private;
1da177e4
LT
537 struct tty_struct *tty;
538
539 dbg("%s - port %d", __FUNCTION__, port->number);
540
541 if (!port)
542 return;
543
544 tty = port->tty;
545 if (!tty)
546 return;
547
548 tty_wakeup(tty);
549}
550
551static void port_release(struct device *dev)
552{
553 struct usb_serial_port *port = to_usb_serial_port(dev);
554
555 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
556 usb_kill_urb(port->read_urb);
557 usb_free_urb(port->read_urb);
558 usb_kill_urb(port->write_urb);
559 usb_free_urb(port->write_urb);
560 usb_kill_urb(port->interrupt_in_urb);
561 usb_free_urb(port->interrupt_in_urb);
562 usb_kill_urb(port->interrupt_out_urb);
563 usb_free_urb(port->interrupt_out_urb);
564 kfree(port->bulk_in_buffer);
565 kfree(port->bulk_out_buffer);
566 kfree(port->interrupt_in_buffer);
567 kfree(port->interrupt_out_buffer);
568 kfree(port);
569}
570
571static struct usb_serial * create_serial (struct usb_device *dev,
572 struct usb_interface *interface,
ea65370d 573 struct usb_serial_driver *driver)
1da177e4
LT
574{
575 struct usb_serial *serial;
576
80b6ca48 577 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
1da177e4
LT
578 if (!serial) {
579 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
580 return NULL;
581 }
1da177e4 582 serial->dev = usb_get_dev(dev);
ea65370d 583 serial->type = driver;
1da177e4
LT
584 serial->interface = interface;
585 kref_init(&serial->kref);
586
587 return serial;
588}
589
ea65370d 590static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
1da177e4
LT
591{
592 struct list_head *p;
593 const struct usb_device_id *id;
ea65370d 594 struct usb_serial_driver *t;
1da177e4 595
93b1fae4 596 /* Check if the usb id matches a known device */
1da177e4 597 list_for_each(p, &usb_serial_driver_list) {
ea65370d 598 t = list_entry(p, struct usb_serial_driver, driver_list);
1da177e4
LT
599 id = usb_match_id(iface, t->id_table);
600 if (id != NULL) {
601 dbg("descriptor matches");
602 return t;
603 }
604 }
605
606 return NULL;
607}
608
609int usb_serial_probe(struct usb_interface *interface,
610 const struct usb_device_id *id)
611{
612 struct usb_device *dev = interface_to_usbdev (interface);
613 struct usb_serial *serial = NULL;
614 struct usb_serial_port *port;
615 struct usb_host_interface *iface_desc;
616 struct usb_endpoint_descriptor *endpoint;
617 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
618 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
619 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
620 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
ea65370d 621 struct usb_serial_driver *type = NULL;
1da177e4
LT
622 int retval;
623 int minor;
624 int buffer_size;
625 int i;
626 int num_interrupt_in = 0;
627 int num_interrupt_out = 0;
628 int num_bulk_in = 0;
629 int num_bulk_out = 0;
630 int num_ports = 0;
631 int max_endpoints;
632
633 type = search_serial_device(interface);
634 if (!type) {
635 dbg("none matched");
636 return -ENODEV;
637 }
638
639 serial = create_serial (dev, interface, type);
640 if (!serial) {
641 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
642 return -ENOMEM;
643 }
644
645 /* if this device type has a probe function, call it */
646 if (type->probe) {
647 const struct usb_device_id *id;
648
18fcac35 649 if (!try_module_get(type->driver.owner)) {
1da177e4
LT
650 dev_err(&interface->dev, "module get failed, exiting\n");
651 kfree (serial);
652 return -EIO;
653 }
654
655 id = usb_match_id(interface, type->id_table);
656 retval = type->probe(serial, id);
18fcac35 657 module_put(type->driver.owner);
1da177e4
LT
658
659 if (retval) {
660 dbg ("sub driver rejected device");
661 kfree (serial);
662 return retval;
663 }
664 }
665
666 /* descriptor matches, let's find the endpoints needed */
667 /* check out the endpoints */
668 iface_desc = interface->cur_altsetting;
669 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
670 endpoint = &iface_desc->endpoint[i].desc;
671
672 if ((endpoint->bEndpointAddress & 0x80) &&
673 ((endpoint->bmAttributes & 3) == 0x02)) {
674 /* we found a bulk in endpoint */
675 dbg("found bulk in on endpoint %d", i);
676 bulk_in_endpoint[num_bulk_in] = endpoint;
677 ++num_bulk_in;
678 }
679
680 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
681 ((endpoint->bmAttributes & 3) == 0x02)) {
682 /* we found a bulk out endpoint */
683 dbg("found bulk out on endpoint %d", i);
684 bulk_out_endpoint[num_bulk_out] = endpoint;
685 ++num_bulk_out;
686 }
687
688 if ((endpoint->bEndpointAddress & 0x80) &&
689 ((endpoint->bmAttributes & 3) == 0x03)) {
690 /* we found a interrupt in endpoint */
691 dbg("found interrupt in on endpoint %d", i);
692 interrupt_in_endpoint[num_interrupt_in] = endpoint;
693 ++num_interrupt_in;
694 }
695
696 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
697 ((endpoint->bmAttributes & 3) == 0x03)) {
698 /* we found an interrupt out endpoint */
699 dbg("found interrupt out on endpoint %d", i);
700 interrupt_out_endpoint[num_interrupt_out] = endpoint;
701 ++num_interrupt_out;
702 }
703 }
704
705#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
706 /* BEGIN HORRIBLE HACK FOR PL2303 */
707 /* this is needed due to the looney way its endpoints are set up */
708 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
709 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
710 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
711 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
712 if (interface != dev->actconfig->interface[0]) {
713 /* check out the endpoints of the other interface*/
714 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
715 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
716 endpoint = &iface_desc->endpoint[i].desc;
717 if ((endpoint->bEndpointAddress & 0x80) &&
718 ((endpoint->bmAttributes & 3) == 0x03)) {
719 /* we found a interrupt in endpoint */
720 dbg("found interrupt in for Prolific device on separate interface");
721 interrupt_in_endpoint[num_interrupt_in] = endpoint;
722 ++num_interrupt_in;
723 }
724 }
725 }
726
727 /* Now make sure the PL-2303 is configured correctly.
728 * If not, give up now and hope this hack will work
729 * properly during a later invocation of usb_serial_probe
730 */
731 if (num_bulk_in == 0 || num_bulk_out == 0) {
732 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
733 kfree (serial);
734 return -ENODEV;
735 }
736 }
737 /* END HORRIBLE HACK FOR PL2303 */
738#endif
739
740 /* found all that we need */
269bda1c 741 dev_info(&interface->dev, "%s converter detected\n", type->description);
1da177e4
LT
742
743#ifdef CONFIG_USB_SERIAL_GENERIC
744 if (type == &usb_serial_generic_device) {
745 num_ports = num_bulk_out;
746 if (num_ports == 0) {
747 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
748 kfree (serial);
749 return -EIO;
750 }
751 }
752#endif
753 if (!num_ports) {
754 /* if this device type has a calc_num_ports function, call it */
755 if (type->calc_num_ports) {
18fcac35 756 if (!try_module_get(type->driver.owner)) {
1da177e4
LT
757 dev_err(&interface->dev, "module get failed, exiting\n");
758 kfree (serial);
759 return -EIO;
760 }
761 num_ports = type->calc_num_ports (serial);
18fcac35 762 module_put(type->driver.owner);
1da177e4
LT
763 }
764 if (!num_ports)
765 num_ports = type->num_ports;
766 }
767
768 if (get_free_serial (serial, num_ports, &minor) == NULL) {
769 dev_err(&interface->dev, "No more free serial devices\n");
770 kfree (serial);
771 return -ENOMEM;
772 }
773
774 serial->minor = minor;
775 serial->num_ports = num_ports;
776 serial->num_bulk_in = num_bulk_in;
777 serial->num_bulk_out = num_bulk_out;
778 serial->num_interrupt_in = num_interrupt_in;
779 serial->num_interrupt_out = num_interrupt_out;
780
781 /* create our ports, we need as many as the max endpoints */
782 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
783 max_endpoints = max(num_bulk_in, num_bulk_out);
784 max_endpoints = max(max_endpoints, num_interrupt_in);
785 max_endpoints = max(max_endpoints, num_interrupt_out);
786 max_endpoints = max(max_endpoints, (int)serial->num_ports);
787 serial->num_port_pointers = max_endpoints;
788 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
789 for (i = 0; i < max_endpoints; ++i) {
80b6ca48 790 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
1da177e4
LT
791 if (!port)
792 goto probe_error;
1da177e4
LT
793 port->number = i + serial->minor;
794 port->serial = serial;
507ca9bc 795 spin_lock_init(&port->lock);
1ce7dd26 796 mutex_init(&port->mutex);
1da177e4
LT
797 INIT_WORK(&port->work, usb_serial_port_softint, port);
798 serial->port[i] = port;
799 }
800
801 /* set up the endpoint information */
802 for (i = 0; i < num_bulk_in; ++i) {
803 endpoint = bulk_in_endpoint[i];
804 port = serial->port[i];
805 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
806 if (!port->read_urb) {
807 dev_err(&interface->dev, "No free urbs available\n");
808 goto probe_error;
809 }
810 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
811 port->bulk_in_size = buffer_size;
812 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
813 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
814 if (!port->bulk_in_buffer) {
815 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
816 goto probe_error;
817 }
818 usb_fill_bulk_urb (port->read_urb, dev,
819 usb_rcvbulkpipe (dev,
820 endpoint->bEndpointAddress),
821 port->bulk_in_buffer, buffer_size,
822 serial->type->read_bulk_callback,
823 port);
824 }
825
826 for (i = 0; i < num_bulk_out; ++i) {
827 endpoint = bulk_out_endpoint[i];
828 port = serial->port[i];
829 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
830 if (!port->write_urb) {
831 dev_err(&interface->dev, "No free urbs available\n");
832 goto probe_error;
833 }
834 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
835 port->bulk_out_size = buffer_size;
836 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
837 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
838 if (!port->bulk_out_buffer) {
839 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
840 goto probe_error;
841 }
842 usb_fill_bulk_urb (port->write_urb, dev,
843 usb_sndbulkpipe (dev,
844 endpoint->bEndpointAddress),
845 port->bulk_out_buffer, buffer_size,
846 serial->type->write_bulk_callback,
847 port);
848 }
849
850 if (serial->type->read_int_callback) {
851 for (i = 0; i < num_interrupt_in; ++i) {
852 endpoint = interrupt_in_endpoint[i];
853 port = serial->port[i];
854 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
855 if (!port->interrupt_in_urb) {
856 dev_err(&interface->dev, "No free urbs available\n");
857 goto probe_error;
858 }
859 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
860 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
861 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
862 if (!port->interrupt_in_buffer) {
863 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
864 goto probe_error;
865 }
866 usb_fill_int_urb (port->interrupt_in_urb, dev,
867 usb_rcvintpipe (dev,
868 endpoint->bEndpointAddress),
869 port->interrupt_in_buffer, buffer_size,
870 serial->type->read_int_callback, port,
871 endpoint->bInterval);
872 }
873 } else if (num_interrupt_in) {
874 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
875 }
876
877 if (serial->type->write_int_callback) {
878 for (i = 0; i < num_interrupt_out; ++i) {
879 endpoint = interrupt_out_endpoint[i];
880 port = serial->port[i];
881 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
882 if (!port->interrupt_out_urb) {
883 dev_err(&interface->dev, "No free urbs available\n");
884 goto probe_error;
885 }
886 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
887 port->interrupt_out_size = buffer_size;
888 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
889 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
890 if (!port->interrupt_out_buffer) {
891 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
892 goto probe_error;
893 }
894 usb_fill_int_urb (port->interrupt_out_urb, dev,
895 usb_sndintpipe (dev,
896 endpoint->bEndpointAddress),
897 port->interrupt_out_buffer, buffer_size,
898 serial->type->write_int_callback, port,
899 endpoint->bInterval);
900 }
901 } else if (num_interrupt_out) {
902 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
903 }
904
905 /* if this device type has an attach function, call it */
906 if (type->attach) {
18fcac35 907 if (!try_module_get(type->driver.owner)) {
1da177e4
LT
908 dev_err(&interface->dev, "module get failed, exiting\n");
909 goto probe_error;
910 }
911 retval = type->attach (serial);
18fcac35 912 module_put(type->driver.owner);
1da177e4
LT
913 if (retval < 0)
914 goto probe_error;
915 if (retval > 0) {
916 /* quietly accept this device, but don't bind to a serial port
917 * as it's about to disappear */
918 goto exit;
919 }
920 }
921
922 /* register all of the individual ports with the driver core */
923 for (i = 0; i < num_ports; ++i) {
924 port = serial->port[i];
925 port->dev.parent = &interface->dev;
926 port->dev.driver = NULL;
927 port->dev.bus = &usb_serial_bus_type;
928 port->dev.release = &port_release;
929
930 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
931 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
932 device_register (&port->dev);
933 }
934
935 usb_serial_console_init (debug, minor);
936
937exit:
938 /* success */
939 usb_set_intfdata (interface, serial);
940 return 0;
941
942probe_error:
943 for (i = 0; i < num_bulk_in; ++i) {
944 port = serial->port[i];
945 if (!port)
946 continue;
947 if (port->read_urb)
948 usb_free_urb (port->read_urb);
949 kfree(port->bulk_in_buffer);
950 }
951 for (i = 0; i < num_bulk_out; ++i) {
952 port = serial->port[i];
953 if (!port)
954 continue;
955 if (port->write_urb)
956 usb_free_urb (port->write_urb);
957 kfree(port->bulk_out_buffer);
958 }
959 for (i = 0; i < num_interrupt_in; ++i) {
960 port = serial->port[i];
961 if (!port)
962 continue;
963 if (port->interrupt_in_urb)
964 usb_free_urb (port->interrupt_in_urb);
965 kfree(port->interrupt_in_buffer);
966 }
967 for (i = 0; i < num_interrupt_out; ++i) {
968 port = serial->port[i];
969 if (!port)
970 continue;
971 if (port->interrupt_out_urb)
972 usb_free_urb (port->interrupt_out_urb);
973 kfree(port->interrupt_out_buffer);
974 }
975
976 /* return the minor range that this device had */
977 return_serial (serial);
978
979 /* free up any memory that we allocated */
980 for (i = 0; i < serial->num_port_pointers; ++i)
981 kfree(serial->port[i]);
982 kfree (serial);
983 return -EIO;
984}
985
986void usb_serial_disconnect(struct usb_interface *interface)
987{
988 int i;
989 struct usb_serial *serial = usb_get_intfdata (interface);
990 struct device *dev = &interface->dev;
991 struct usb_serial_port *port;
992
73e487fd 993 usb_serial_console_disconnect(serial);
1da177e4
LT
994 dbg ("%s", __FUNCTION__);
995
996 usb_set_intfdata (interface, NULL);
997 if (serial) {
998 for (i = 0; i < serial->num_ports; ++i) {
999 port = serial->port[i];
1000 if (port && port->tty)
1001 tty_hangup(port->tty);
1002 }
1003 /* let the last holder of this object
1004 * cause it to be cleaned up */
73e487fd 1005 usb_serial_put(serial);
1da177e4
LT
1006 }
1007 dev_info(dev, "device disconnected\n");
1008}
1009
1010static struct tty_operations serial_ops = {
1011 .open = serial_open,
1012 .close = serial_close,
1013 .write = serial_write,
1014 .write_room = serial_write_room,
1015 .ioctl = serial_ioctl,
1016 .set_termios = serial_set_termios,
1017 .throttle = serial_throttle,
1018 .unthrottle = serial_unthrottle,
1019 .break_ctl = serial_break,
1020 .chars_in_buffer = serial_chars_in_buffer,
1021 .read_proc = serial_read_proc,
1022 .tiocmget = serial_tiocmget,
1023 .tiocmset = serial_tiocmset,
1024};
1025
1026struct tty_driver *usb_serial_tty_driver;
1027
1028static int __init usb_serial_init(void)
1029{
1030 int i;
1031 int result;
1032
1033 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1034 if (!usb_serial_tty_driver)
1035 return -ENOMEM;
1036
1037 /* Initialize our global data */
1038 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1039 serial_table[i] = NULL;
1040 }
1041
1042 result = bus_register(&usb_serial_bus_type);
1043 if (result) {
1044 err("%s - registering bus driver failed", __FUNCTION__);
1045 goto exit_bus;
1046 }
1047
1da177e4
LT
1048 usb_serial_tty_driver->owner = THIS_MODULE;
1049 usb_serial_tty_driver->driver_name = "usbserial";
1050 usb_serial_tty_driver->devfs_name = "usb/tts/";
1051 usb_serial_tty_driver->name = "ttyUSB";
1052 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1053 usb_serial_tty_driver->minor_start = 0;
1054 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1055 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1056 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1057 usb_serial_tty_driver->init_termios = tty_std_termios;
1058 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1059 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1060 result = tty_register_driver(usb_serial_tty_driver);
1061 if (result) {
1062 err("%s - tty_register_driver failed", __FUNCTION__);
1063 goto exit_reg_driver;
1064 }
1065
1066 /* register the USB driver */
1067 result = usb_register(&usb_serial_driver);
1068 if (result < 0) {
1069 err("%s - usb_register failed", __FUNCTION__);
1070 goto exit_tty;
1071 }
1072
06299db3
GKH
1073 /* register the generic driver, if we should */
1074 result = usb_serial_generic_register(debug);
1075 if (result < 0) {
1076 err("%s - registering generic driver failed", __FUNCTION__);
1077 goto exit_generic;
1078 }
1079
17a882fc 1080 info(DRIVER_DESC);
1da177e4
LT
1081
1082 return result;
1083
06299db3
GKH
1084exit_generic:
1085 usb_deregister(&usb_serial_driver);
1086
1da177e4
LT
1087exit_tty:
1088 tty_unregister_driver(usb_serial_tty_driver);
1089
1090exit_reg_driver:
1da177e4
LT
1091 bus_unregister(&usb_serial_bus_type);
1092
1093exit_bus:
1094 err ("%s - returning with error %d", __FUNCTION__, result);
1095 put_tty_driver(usb_serial_tty_driver);
1096 return result;
1097}
1098
1099
1100static void __exit usb_serial_exit(void)
1101{
1102 usb_serial_console_exit();
1103
1104 usb_serial_generic_deregister();
1105
1106 usb_deregister(&usb_serial_driver);
1107 tty_unregister_driver(usb_serial_tty_driver);
1108 put_tty_driver(usb_serial_tty_driver);
1109 bus_unregister(&usb_serial_bus_type);
1110}
1111
1112
1113module_init(usb_serial_init);
1114module_exit(usb_serial_exit);
1115
1116#define set_to_generic_if_null(type, function) \
1117 do { \
1118 if (!type->function) { \
1119 type->function = usb_serial_generic_##function; \
1120 dbg("Had to override the " #function \
1121 " usb serial operation with the generic one.");\
1122 } \
1123 } while (0)
1124
ea65370d 1125static void fixup_generic(struct usb_serial_driver *device)
1da177e4
LT
1126{
1127 set_to_generic_if_null(device, open);
1128 set_to_generic_if_null(device, write);
1129 set_to_generic_if_null(device, close);
1130 set_to_generic_if_null(device, write_room);
1131 set_to_generic_if_null(device, chars_in_buffer);
1132 set_to_generic_if_null(device, read_bulk_callback);
1133 set_to_generic_if_null(device, write_bulk_callback);
1134 set_to_generic_if_null(device, shutdown);
1135}
1136
ea65370d 1137int usb_serial_register(struct usb_serial_driver *driver)
1da177e4
LT
1138{
1139 int retval;
1140
ea65370d 1141 fixup_generic(driver);
1da177e4 1142
269bda1c
GKH
1143 if (!driver->description)
1144 driver->description = driver->driver.name;
1145
1da177e4 1146 /* Add this device to our list of devices */
ea65370d 1147 list_add(&driver->driver_list, &usb_serial_driver_list);
1da177e4 1148
ea65370d 1149 retval = usb_serial_bus_register(driver);
1da177e4 1150 if (retval) {
269bda1c 1151 err("problem %d when registering driver %s", retval, driver->description);
ea65370d 1152 list_del(&driver->driver_list);
1da177e4
LT
1153 }
1154 else
269bda1c 1155 info("USB Serial support registered for %s", driver->description);
1da177e4
LT
1156
1157 return retval;
1158}
1159
1160
ea65370d 1161void usb_serial_deregister(struct usb_serial_driver *device)
1da177e4 1162{
269bda1c 1163 info("USB Serial deregistering driver %s", device->description);
1da177e4
LT
1164 list_del(&device->driver_list);
1165 usb_serial_bus_deregister(device);
1166}
1167
1168
1169
1170/* If the usb-serial core is built into the core, the usb-serial drivers
1171 need these symbols to load properly as modules. */
1172EXPORT_SYMBOL_GPL(usb_serial_register);
1173EXPORT_SYMBOL_GPL(usb_serial_deregister);
1174EXPORT_SYMBOL_GPL(usb_serial_probe);
1175EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1176EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1177
1178
1179/* Module information */
1180MODULE_AUTHOR( DRIVER_AUTHOR );
1181MODULE_DESCRIPTION( DRIVER_DESC );
1da177e4
LT
1182MODULE_LICENSE("GPL");
1183
1184module_param(debug, bool, S_IRUGO | S_IWUSR);
1185MODULE_PARM_DESC(debug, "Debug enabled or not");