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