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