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