]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/serial/usb-serial.c
USB: serial: fix runtime PM after driver unbind
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / serial / usb-serial.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * USB Serial Converter driver
4 *
659597b7 5 * Copyright (C) 2009 - 2013 Johan Hovold (jhovold@gmail.com)
7186364e 6 * Copyright (C) 1999 - 2012 Greg Kroah-Hartman (greg@kroah.com)
1da177e4
LT
7 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
8 * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com)
9 *
502b95c1 10 * This driver was originally based on the ACM driver by Armin Fuerst (which was
1da177e4
LT
11 * based on a driver by Brad Keryan)
12 *
a8d6f0a9
AC
13 * See Documentation/usb/usb-serial.txt for more information on using this
14 * driver
1da177e4
LT
15 */
16
92931d24
GKH
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
6fd69d3c 28#include <linux/seq_file.h>
1da177e4 29#include <linux/spinlock.h>
1ce7dd26 30#include <linux/mutex.h>
1da177e4 31#include <linux/list.h>
a8d6f0a9 32#include <linux/uaccess.h>
c56d3000 33#include <linux/serial.h>
1da177e4 34#include <linux/usb.h>
a969888c 35#include <linux/usb/serial.h>
8e8dce06 36#include <linux/kfifo.h>
e5b1e206 37#include <linux/idr.h>
1da177e4 38
ee42f6c9 39#define DRIVER_AUTHOR "Greg Kroah-Hartman <gregkh@linuxfoundation.org>"
1da177e4
LT
40#define DRIVER_DESC "USB Serial Driver core"
41
455b4f7e
GKH
42#define USB_SERIAL_TTY_MAJOR 188
43#define USB_SERIAL_TTY_MINORS 512 /* should be enough for a while */
44
1da177e4
LT
45/* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
46 the MODULE_DEVICE_TABLE declarations in each serial driver
47 cause the "hotplug" program to pull in whatever module is necessary
48 via modprobe, and modprobe will load usbserial because the serial
49 drivers depend on it.
50*/
51
e5b1e206 52static DEFINE_IDR(serial_minors);
3ddad823 53static DEFINE_MUTEX(table_lock);
1da177e4
LT
54static LIST_HEAD(usb_serial_driver_list);
55
8bc2c1b2 56/*
e5b1e206
GKH
57 * Look up the serial port structure. If it is found and it hasn't been
58 * disconnected, return with the parent usb_serial structure's disc_mutex held
59 * and its refcount incremented. Otherwise return NULL.
8bc2c1b2 60 */
e5b1e206 61struct usb_serial_port *usb_serial_port_get_by_minor(unsigned minor)
1da177e4 62{
34ef50e5 63 struct usb_serial *serial;
e5b1e206 64 struct usb_serial_port *port;
34ef50e5 65
3ddad823 66 mutex_lock(&table_lock);
e5b1e206
GKH
67 port = idr_find(&serial_minors, minor);
68 if (!port)
69 goto exit;
70
71 serial = port->serial;
72 mutex_lock(&serial->disc_mutex);
73 if (serial->disconnected) {
74 mutex_unlock(&serial->disc_mutex);
75 port = NULL;
76 } else {
77 kref_get(&serial->kref);
8bc2c1b2 78 }
e5b1e206 79exit:
3ddad823 80 mutex_unlock(&table_lock);
e5b1e206 81 return port;
1da177e4
LT
82}
83
e5b1e206 84static int allocate_minors(struct usb_serial *serial, int num_ports)
1da177e4 85{
e5b1e206 86 struct usb_serial_port *port;
1da177e4 87 unsigned int i, j;
e5b1e206 88 int minor;
1da177e4 89
92931d24 90 dev_dbg(&serial->interface->dev, "%s %d\n", __func__, num_ports);
1da177e4 91
3ddad823 92 mutex_lock(&table_lock);
e5b1e206
GKH
93 for (i = 0; i < num_ports; ++i) {
94 port = serial->port[i];
194e958c
JH
95 minor = idr_alloc(&serial_minors, port, 0,
96 USB_SERIAL_TTY_MINORS, GFP_KERNEL);
e5b1e206
GKH
97 if (minor < 0)
98 goto error;
99 port->minor = minor;
100 port->port_number = i;
1da177e4 101 }
e5b1e206 102 serial->minors_reserved = 1;
3ddad823 103 mutex_unlock(&table_lock);
e5b1e206
GKH
104 return 0;
105error:
106 /* unwind the already allocated minors */
107 for (j = 0; j < i; ++j)
108 idr_remove(&serial_minors, serial->port[j]->minor);
109 mutex_unlock(&table_lock);
110 return minor;
1da177e4
LT
111}
112
e5b1e206 113static void release_minors(struct usb_serial *serial)
1da177e4
LT
114{
115 int i;
116
8bc2c1b2 117 mutex_lock(&table_lock);
a8d6f0a9 118 for (i = 0; i < serial->num_ports; ++i)
e5b1e206 119 idr_remove(&serial_minors, serial->port[i]->minor);
8bc2c1b2 120 mutex_unlock(&table_lock);
e5b1e206 121 serial->minors_reserved = 0;
1da177e4
LT
122}
123
124static void destroy_serial(struct kref *kref)
125{
126 struct usb_serial *serial;
127 struct usb_serial_port *port;
128 int i;
129
130 serial = to_usb_serial(kref);
131
521b85ae 132 /* return the minor range that this device had */
e5b1e206
GKH
133 if (serial->minors_reserved)
134 release_minors(serial);
521b85ae 135
79b80b8a 136 if (serial->attached && serial->type->release)
a4720c65 137 serial->type->release(serial);
f9c99bb8 138
41bd34dd
AS
139 /* Now that nothing is using the ports, they can be freed */
140 for (i = 0; i < serial->num_port_pointers; ++i) {
f9c99bb8 141 port = serial->port[i];
41bd34dd
AS
142 if (port) {
143 port->serial = NULL;
f9c99bb8 144 put_device(&port->dev);
1da177e4
LT
145 }
146 }
147
d7971051 148 usb_put_intf(serial->interface);
1da177e4 149 usb_put_dev(serial->dev);
a8d6f0a9 150 kfree(serial);
1da177e4
LT
151}
152
73e487fd
GL
153void usb_serial_put(struct usb_serial *serial)
154{
155 kref_put(&serial->kref, destroy_serial);
156}
157
1da177e4
LT
158/*****************************************************************************
159 * Driver tty interface functions
160 *****************************************************************************/
f5b0953a
AS
161
162/**
163 * serial_install - install tty
164 * @driver: the driver (USB in our case)
165 * @tty: the tty being created
166 *
167 * Create the termios objects for this tty. We use the default
168 * USB serial settings but permit them to be overridden by
169 * serial->type->init_termios.
cc56cd01
AS
170 *
171 * This is the first place a new tty gets used. Hence this is where we
172 * acquire references to the usb_serial structure and the driver module,
173 * where we store a pointer to the port, and where we do an autoresume.
f278a2f7 174 * All these actions are reversed in serial_cleanup().
f5b0953a
AS
175 */
176static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
177{
178 int idx = tty->index;
179 struct usb_serial *serial;
cc56cd01
AS
180 struct usb_serial_port *port;
181 int retval = -ENODEV;
182
e5b1e206
GKH
183 port = usb_serial_port_get_by_minor(idx);
184 if (!port)
cc56cd01
AS
185 return retval;
186
e5b1e206 187 serial = port->serial;
cc56cd01
AS
188 if (!try_module_get(serial->type->driver.owner))
189 goto error_module_get;
190
191 retval = usb_autopm_get_interface(serial->interface);
192 if (retval)
193 goto error_get_interface;
f5b0953a 194
ca4ff100 195 retval = tty_port_install(&port->port, driver, tty);
76f82a7a
JS
196 if (retval)
197 goto error_init_termios;
198
cc56cd01
AS
199 mutex_unlock(&serial->disc_mutex);
200
7e29bb4b
AS
201 /* allow the driver to update the settings */
202 if (serial->type->init_termios)
203 serial->type->init_termios(tty);
204
cc56cd01
AS
205 tty->driver_data = port;
206
cc56cd01
AS
207 return retval;
208
7e29bb4b 209 error_init_termios:
76f82a7a
JS
210 usb_autopm_put_interface(serial->interface);
211 error_get_interface:
cc56cd01
AS
212 module_put(serial->type->driver.owner);
213 error_module_get:
cc56cd01
AS
214 usb_serial_put(serial);
215 mutex_unlock(&serial->disc_mutex);
216 return retval;
f5b0953a
AS
217}
218
395e08da 219static int serial_port_activate(struct tty_port *tport, struct tty_struct *tty)
1da177e4 220{
64bc3979
AC
221 struct usb_serial_port *port =
222 container_of(tport, struct usb_serial_port, port);
320348c8
AS
223 struct usb_serial *serial = port->serial;
224 int retval;
331879fd 225
64bc3979
AC
226 mutex_lock(&serial->disc_mutex);
227 if (serial->disconnected)
228 retval = -ENODEV;
229 else
230 retval = port->serial->type->open(tty, port);
231 mutex_unlock(&serial->disc_mutex);
3827d876
JH
232
233 if (retval < 0)
234 retval = usb_translate_errors(retval);
235
64bc3979
AC
236 return retval;
237}
1da177e4 238
64bc3979
AC
239static int serial_open(struct tty_struct *tty, struct file *filp)
240{
241 struct usb_serial_port *port = tty->driver_data;
320348c8 242
d12e211d
JH
243 dev_dbg(tty->dev, "%s\n", __func__);
244
64bc3979 245 return tty_port_open(&port->port, tty, filp);
1da177e4
LT
246}
247
335f8514 248/**
395e08da 249 * serial_port_shutdown - shut down hardware
e1108a63 250 * @tport: tty port to shut down
335f8514 251 *
8a7298d3
PH
252 * Shut down a USB serial port. Serialized against activate by the
253 * tport mutex and kept to matching open/close pairs
d41861ca 254 * of calls by the initialized flag.
8a7298d3
PH
255 *
256 * Not called if tty is console.
335f8514 257 */
395e08da 258static void serial_port_shutdown(struct tty_port *tport)
1da177e4 259{
e1108a63
AC
260 struct usb_serial_port *port =
261 container_of(tport, struct usb_serial_port, port);
335f8514 262 struct usb_serial_driver *drv = port->serial->type;
8a7298d3 263
335f8514
AC
264 if (drv->close)
265 drv->close(port);
335f8514
AC
266}
267
f5b0953a
AS
268static void serial_hangup(struct tty_struct *tty)
269{
270 struct usb_serial_port *port = tty->driver_data;
92931d24 271
d12e211d
JH
272 dev_dbg(tty->dev, "%s\n", __func__);
273
f5b0953a 274 tty_port_hangup(&port->port);
f5b0953a
AS
275}
276
277static void serial_close(struct tty_struct *tty, struct file *filp)
278{
279 struct usb_serial_port *port = tty->driver_data;
92931d24 280
d12e211d
JH
281 dev_dbg(tty->dev, "%s\n", __func__);
282
e1108a63 283 tty_port_close(&port->port, tty, filp);
f5b0953a
AS
284}
285
335f8514 286/**
f278a2f7 287 * serial_cleanup - free resources post close/hangup
f5b0953a 288 * @port: port to free up
335f8514 289 *
f5b0953a
AS
290 * Do the resource freeing and refcount dropping for the port.
291 * Avoid freeing the console.
4455e344 292 *
36b3c070 293 * Called asynchronously after the last tty kref is dropped.
335f8514 294 */
f278a2f7 295static void serial_cleanup(struct tty_struct *tty)
335f8514 296{
4455e344 297 struct usb_serial_port *port = tty->driver_data;
335f8514
AC
298 struct usb_serial *serial;
299 struct module *owner;
300
d12e211d
JH
301 dev_dbg(tty->dev, "%s\n", __func__);
302
f5b0953a
AS
303 /* The console is magical. Do not hang up the console hardware
304 * or there will be tears.
305 */
bd5afa9e 306 if (port->port.console)
335f8514
AC
307 return;
308
cc56cd01
AS
309 tty->driver_data = NULL;
310
335f8514
AC
311 serial = port->serial;
312 owner = serial->type->driver.owner;
41bd34dd 313
69ca674e 314 usb_autopm_put_interface(serial->interface);
41bd34dd 315
2d93148a 316 usb_serial_put(serial);
335f8514
AC
317 module_put(owner);
318}
319
a8d6f0a9
AC
320static int serial_write(struct tty_struct *tty, const unsigned char *buf,
321 int count)
1da177e4 322{
81671ddb 323 struct usb_serial_port *port = tty->driver_data;
3ff4fd94 324 int retval = -ENODEV;
1da177e4 325
f34d7a5b 326 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
487f9c67
LFC
327 goto exit;
328
d12e211d 329 dev_dbg(tty->dev, "%s - %d byte(s)\n", __func__, count);
1da177e4 330
95da310e 331 retval = port->serial->type->write(tty, port, buf, count);
c6249ff7
JH
332 if (retval < 0)
333 retval = usb_translate_errors(retval);
1da177e4
LT
334exit:
335 return retval;
336}
337
a8d6f0a9 338static int serial_write_room(struct tty_struct *tty)
1da177e4 339{
81671ddb 340 struct usb_serial_port *port = tty->driver_data;
92931d24 341
d12e211d 342 dev_dbg(tty->dev, "%s\n", __func__);
9993b42b 343
95da310e 344 return port->serial->type->write_room(tty);
1da177e4
LT
345}
346
a8d6f0a9 347static int serial_chars_in_buffer(struct tty_struct *tty)
1da177e4 348{
81671ddb 349 struct usb_serial_port *port = tty->driver_data;
810360a0 350 struct usb_serial *serial = port->serial;
92931d24 351
d12e211d 352 dev_dbg(tty->dev, "%s\n", __func__);
1da177e4 353
810360a0 354 if (serial->disconnected)
4746b6c6 355 return 0;
810360a0 356
4746b6c6 357 return serial->type->chars_in_buffer(tty);
1da177e4
LT
358}
359
0693196f
JH
360static void serial_wait_until_sent(struct tty_struct *tty, int timeout)
361{
362 struct usb_serial_port *port = tty->driver_data;
363 struct usb_serial *serial = port->serial;
364
365 dev_dbg(tty->dev, "%s\n", __func__);
366
367 if (!port->serial->type->wait_until_sent)
368 return;
369
370 mutex_lock(&serial->disc_mutex);
371 if (!serial->disconnected)
372 port->serial->type->wait_until_sent(tty, timeout);
373 mutex_unlock(&serial->disc_mutex);
374}
375
a8d6f0a9 376static void serial_throttle(struct tty_struct *tty)
1da177e4 377{
81671ddb 378 struct usb_serial_port *port = tty->driver_data;
92931d24 379
d12e211d 380 dev_dbg(tty->dev, "%s\n", __func__);
1da177e4 381
1da177e4 382 if (port->serial->type->throttle)
95da310e 383 port->serial->type->throttle(tty);
1da177e4
LT
384}
385
a8d6f0a9 386static void serial_unthrottle(struct tty_struct *tty)
1da177e4 387{
81671ddb 388 struct usb_serial_port *port = tty->driver_data;
92931d24 389
d12e211d 390 dev_dbg(tty->dev, "%s\n", __func__);
1da177e4 391
1da177e4 392 if (port->serial->type->unthrottle)
95da310e 393 port->serial->type->unthrottle(tty);
1da177e4
LT
394}
395
6caa76b7 396static int serial_ioctl(struct tty_struct *tty,
a8d6f0a9 397 unsigned int cmd, unsigned long arg)
1da177e4 398{
81671ddb 399 struct usb_serial_port *port = tty->driver_data;
f4488035 400 int retval = -ENOIOCTLCMD;
1da177e4 401
4d5147ec 402 dev_dbg(tty->dev, "%s - cmd 0x%04x\n", __func__, cmd);
1da177e4 403
143d9d96
JH
404 switch (cmd) {
405 case TIOCMIWAIT:
406 if (port->serial->type->tiocmiwait)
407 retval = port->serial->type->tiocmiwait(tty, arg);
408 break;
409 default:
410 if (port->serial->type->ioctl)
411 retval = port->serial->type->ioctl(tty, cmd, arg);
143d9d96 412 }
1da177e4 413
1da177e4
LT
414 return retval;
415}
416
a8d6f0a9 417static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1da177e4 418{
81671ddb 419 struct usb_serial_port *port = tty->driver_data;
92931d24 420
d12e211d 421 dev_dbg(tty->dev, "%s\n", __func__);
1da177e4 422
1da177e4 423 if (port->serial->type->set_termios)
95da310e 424 port->serial->type->set_termios(tty, port, old);
33785091 425 else
adc8d746 426 tty_termios_copy_hw(&tty->termios, old);
1da177e4
LT
427}
428
9e98966c 429static int serial_break(struct tty_struct *tty, int break_state)
1da177e4 430{
81671ddb 431 struct usb_serial_port *port = tty->driver_data;
1da177e4 432
d12e211d 433 dev_dbg(tty->dev, "%s\n", __func__);
1da177e4 434
6b447f04 435 if (port->serial->type->break_ctl)
95da310e 436 port->serial->type->break_ctl(tty, break_state);
9993b42b 437
9e98966c 438 return 0;
1da177e4
LT
439}
440
6fd69d3c 441static int serial_proc_show(struct seq_file *m, void *v)
1da177e4
LT
442{
443 struct usb_serial *serial;
e5b1e206 444 struct usb_serial_port *port;
1da177e4 445 int i;
1da177e4
LT
446 char tmp[40];
447
6fd69d3c 448 seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
455b4f7e 449 for (i = 0; i < USB_SERIAL_TTY_MINORS; ++i) {
e5b1e206
GKH
450 port = usb_serial_port_get_by_minor(i);
451 if (port == NULL)
1da177e4 452 continue;
e5b1e206 453 serial = port->serial;
1da177e4 454
6fd69d3c 455 seq_printf(m, "%d:", i);
18fcac35 456 if (serial->type->driver.owner)
6fd69d3c 457 seq_printf(m, " module:%s",
a8d6f0a9 458 module_name(serial->type->driver.owner));
6fd69d3c 459 seq_printf(m, " name:\"%s\"",
a8d6f0a9 460 serial->type->description);
6fd69d3c 461 seq_printf(m, " vendor:%04x product:%04x",
a8d6f0a9
AC
462 le16_to_cpu(serial->dev->descriptor.idVendor),
463 le16_to_cpu(serial->dev->descriptor.idProduct));
6fd69d3c 464 seq_printf(m, " num_ports:%d", serial->num_ports);
e5b1e206 465 seq_printf(m, " port:%d", port->port_number);
1da177e4 466 usb_make_path(serial->dev, tmp, sizeof(tmp));
6fd69d3c 467 seq_printf(m, " path:%s", tmp);
a8d6f0a9 468
6fd69d3c 469 seq_putc(m, '\n');
73e487fd 470 usb_serial_put(serial);
8bc2c1b2 471 mutex_unlock(&serial->disc_mutex);
1da177e4 472 }
6fd69d3c 473 return 0;
1da177e4
LT
474}
475
6fd69d3c
AD
476static int serial_proc_open(struct inode *inode, struct file *file)
477{
478 return single_open(file, serial_proc_show, NULL);
479}
480
481static const struct file_operations serial_proc_fops = {
482 .owner = THIS_MODULE,
483 .open = serial_proc_open,
484 .read = seq_read,
485 .llseek = seq_lseek,
486 .release = single_release,
487};
488
60b33c13 489static int serial_tiocmget(struct tty_struct *tty)
1da177e4 490{
81671ddb 491 struct usb_serial_port *port = tty->driver_data;
1da177e4 492
d12e211d 493 dev_dbg(tty->dev, "%s\n", __func__);
1da177e4 494
1da177e4 495 if (port->serial->type->tiocmget)
60b33c13 496 return port->serial->type->tiocmget(tty);
1da177e4
LT
497 return -EINVAL;
498}
499
20b9d177 500static int serial_tiocmset(struct tty_struct *tty,
1da177e4
LT
501 unsigned int set, unsigned int clear)
502{
81671ddb 503 struct usb_serial_port *port = tty->driver_data;
1da177e4 504
d12e211d 505 dev_dbg(tty->dev, "%s\n", __func__);
1da177e4 506
1da177e4 507 if (port->serial->type->tiocmset)
20b9d177 508 return port->serial->type->tiocmset(tty, set, clear);
1da177e4
LT
509 return -EINVAL;
510}
511
d281da7f
AC
512static int serial_get_icount(struct tty_struct *tty,
513 struct serial_icounter_struct *icount)
514{
515 struct usb_serial_port *port = tty->driver_data;
516
d12e211d 517 dev_dbg(tty->dev, "%s\n", __func__);
d281da7f
AC
518
519 if (port->serial->type->get_icount)
520 return port->serial->type->get_icount(tty, icount);
521 return -EINVAL;
522}
523
cf2c7481
PZ
524/*
525 * We would be calling tty_wakeup here, but unfortunately some line
526 * disciplines have an annoying habit of calling tty->write from
527 * the write wakeup callback (e.g. n_hdlc.c).
528 */
529void usb_serial_port_softint(struct usb_serial_port *port)
530{
531 schedule_work(&port->work);
532}
a8d6f0a9 533EXPORT_SYMBOL_GPL(usb_serial_port_softint);
cf2c7481 534
c4028958 535static void usb_serial_port_work(struct work_struct *work)
1da177e4 536{
c4028958
DH
537 struct usb_serial_port *port =
538 container_of(work, struct usb_serial_port, work);
1da177e4 539
6aad04f2 540 tty_port_tty_wakeup(&port->port);
1da177e4
LT
541}
542
6a5c821c 543static void usb_serial_port_poison_urbs(struct usb_serial_port *port)
34f8e761 544{
27c7acf2
JH
545 int i;
546
d83b4053 547 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
6a5c821c 548 usb_poison_urb(port->read_urbs[i]);
27c7acf2 549 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
6a5c821c
JH
550 usb_poison_urb(port->write_urbs[i]);
551
552 usb_poison_urb(port->interrupt_in_urb);
553 usb_poison_urb(port->interrupt_out_urb);
554}
555
556static void usb_serial_port_unpoison_urbs(struct usb_serial_port *port)
557{
558 int i;
559
560 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
561 usb_unpoison_urb(port->read_urbs[i]);
562 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
563 usb_unpoison_urb(port->write_urbs[i]);
564
565 usb_unpoison_urb(port->interrupt_in_urb);
566 usb_unpoison_urb(port->interrupt_out_urb);
34ef50e5
ON
567}
568
69a3d212 569static void usb_serial_port_release(struct device *dev)
34ef50e5 570{
41bd34dd 571 struct usb_serial_port *port = to_usb_serial_port(dev);
27c7acf2 572 int i;
41bd34dd 573
92931d24 574 dev_dbg(dev, "%s\n", __func__);
41bd34dd 575
34ef50e5 576 usb_free_urb(port->interrupt_in_urb);
1da177e4 577 usb_free_urb(port->interrupt_out_urb);
d83b4053
JH
578 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
579 usb_free_urb(port->read_urbs[i]);
580 kfree(port->bulk_in_buffers[i]);
581 }
27c7acf2
JH
582 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
583 usb_free_urb(port->write_urbs[i]);
584 kfree(port->bulk_out_buffers[i]);
585 }
119eecc8 586 kfifo_free(&port->write_fifo);
1da177e4
LT
587 kfree(port->interrupt_in_buffer);
588 kfree(port->interrupt_out_buffer);
191c5f10 589 tty_port_destroy(&port->port);
1da177e4
LT
590 kfree(port);
591}
592
a8d6f0a9
AC
593static struct usb_serial *create_serial(struct usb_device *dev,
594 struct usb_interface *interface,
595 struct usb_serial_driver *driver)
1da177e4
LT
596{
597 struct usb_serial *serial;
598
80b6ca48 599 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
6b03f7f7 600 if (!serial)
1da177e4 601 return NULL;
1da177e4 602 serial->dev = usb_get_dev(dev);
ea65370d 603 serial->type = driver;
d7971051 604 serial->interface = usb_get_intf(interface);
1da177e4 605 kref_init(&serial->kref);
a1cd7e99 606 mutex_init(&serial->disc_mutex);
e5b1e206 607 serial->minors_reserved = 0;
1da177e4
LT
608
609 return serial;
610}
611
93bacefc 612static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
a8d6f0a9 613 struct usb_serial_driver *drv)
93bacefc
GKH
614{
615 struct usb_dynid *dynid;
616
617 spin_lock(&drv->dynids.lock);
618 list_for_each_entry(dynid, &drv->dynids.list, node) {
619 if (usb_match_one_id(intf, &dynid->id)) {
620 spin_unlock(&drv->dynids.lock);
621 return &dynid->id;
622 }
623 }
624 spin_unlock(&drv->dynids.lock);
625 return NULL;
626}
627
628static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
629 struct usb_interface *intf)
630{
631 const struct usb_device_id *id;
632
633 id = usb_match_id(intf, drv->id_table);
634 if (id) {
92931d24 635 dev_dbg(&intf->dev, "static descriptor matches\n");
93bacefc
GKH
636 goto exit;
637 }
638 id = match_dynamic_id(intf, drv);
639 if (id)
92931d24 640 dev_dbg(&intf->dev, "dynamic descriptor matches\n");
93bacefc
GKH
641exit:
642 return id;
643}
644
0daeed38 645/* Caller must hold table_lock */
a8d6f0a9
AC
646static struct usb_serial_driver *search_serial_device(
647 struct usb_interface *iface)
1da177e4 648{
954c3f8a 649 const struct usb_device_id *id = NULL;
063a2da8 650 struct usb_serial_driver *drv;
954c3f8a 651 struct usb_driver *driver = to_usb_driver(iface->dev.driver);
1da177e4 652
93b1fae4 653 /* Check if the usb id matches a known device */
063a2da8 654 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
954c3f8a
BM
655 if (drv->usb_driver == driver)
656 id = get_iface_id(drv, iface);
93bacefc 657 if (id)
063a2da8 658 return drv;
1da177e4
LT
659 }
660
661 return NULL;
662}
663
395e08da 664static int serial_port_carrier_raised(struct tty_port *port)
335f8514
AC
665{
666 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
667 struct usb_serial_driver *drv = p->serial->type;
3e1f4901 668
335f8514
AC
669 if (drv->carrier_raised)
670 return drv->carrier_raised(p);
671 /* No carrier control - don't block */
3e1f4901 672 return 1;
335f8514
AC
673}
674
395e08da 675static void serial_port_dtr_rts(struct tty_port *port, int on)
335f8514
AC
676{
677 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
f5f45304 678 struct usb_serial_driver *drv = p->serial->type;
3e1f4901 679
f5f45304 680 if (drv->dtr_rts)
335f8514
AC
681 drv->dtr_rts(p, on);
682}
683
2deb96b5
JH
684static ssize_t port_number_show(struct device *dev,
685 struct device_attribute *attr, char *buf)
686{
687 struct usb_serial_port *port = to_usb_serial_port(dev);
688
689 return sprintf(buf, "%u\n", port->port_number);
690}
691static DEVICE_ATTR_RO(port_number);
692
693static struct attribute *usb_serial_port_attrs[] = {
694 &dev_attr_port_number.attr,
695 NULL
696};
697ATTRIBUTE_GROUPS(usb_serial_port);
698
335f8514 699static const struct tty_port_operations serial_port_ops = {
395e08da
JH
700 .carrier_raised = serial_port_carrier_raised,
701 .dtr_rts = serial_port_dtr_rts,
702 .activate = serial_port_activate,
703 .shutdown = serial_port_shutdown,
335f8514
AC
704};
705
1546e6ae
JH
706static void find_endpoints(struct usb_serial *serial,
707 struct usb_serial_endpoints *epds)
708{
709 struct device *dev = &serial->interface->dev;
710 struct usb_host_interface *iface_desc;
711 struct usb_endpoint_descriptor *epd;
712 unsigned int i;
713
8520ac0d
JH
714 BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_in) < USB_MAXENDPOINTS / 2);
715 BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < USB_MAXENDPOINTS / 2);
716 BUILD_BUG_ON(ARRAY_SIZE(epds->interrupt_in) < USB_MAXENDPOINTS / 2);
717 BUILD_BUG_ON(ARRAY_SIZE(epds->interrupt_out) < USB_MAXENDPOINTS / 2);
718
1546e6ae
JH
719 iface_desc = serial->interface->cur_altsetting;
720 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
721 epd = &iface_desc->endpoint[i].desc;
722
723 if (usb_endpoint_is_bulk_in(epd)) {
724 dev_dbg(dev, "found bulk in on endpoint %u\n", i);
1546e6ae
JH
725 epds->bulk_in[epds->num_bulk_in++] = epd;
726 } else if (usb_endpoint_is_bulk_out(epd)) {
727 dev_dbg(dev, "found bulk out on endpoint %u\n", i);
1546e6ae
JH
728 epds->bulk_out[epds->num_bulk_out++] = epd;
729 } else if (usb_endpoint_is_int_in(epd)) {
730 dev_dbg(dev, "found interrupt in on endpoint %u\n", i);
1546e6ae
JH
731 epds->interrupt_in[epds->num_interrupt_in++] = epd;
732 } else if (usb_endpoint_is_int_out(epd)) {
733 dev_dbg(dev, "found interrupt out on endpoint %u\n", i);
1546e6ae
JH
734 epds->interrupt_out[epds->num_interrupt_out++] = epd;
735 }
736 }
737}
738
45e5d4d4
JH
739static int setup_port_bulk_in(struct usb_serial_port *port,
740 struct usb_endpoint_descriptor *epd)
741{
742 struct usb_serial_driver *type = port->serial->type;
743 struct usb_device *udev = port->serial->dev;
744 int buffer_size;
745 int i;
746
747 buffer_size = max_t(int, type->bulk_in_size, usb_endpoint_maxp(epd));
748 port->bulk_in_size = buffer_size;
749 port->bulk_in_endpointAddress = epd->bEndpointAddress;
750
751 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
752 set_bit(i, &port->read_urbs_free);
753 port->read_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
754 if (!port->read_urbs[i])
755 return -ENOMEM;
756 port->bulk_in_buffers[i] = kmalloc(buffer_size, GFP_KERNEL);
757 if (!port->bulk_in_buffers[i])
758 return -ENOMEM;
759 usb_fill_bulk_urb(port->read_urbs[i], udev,
760 usb_rcvbulkpipe(udev, epd->bEndpointAddress),
761 port->bulk_in_buffers[i], buffer_size,
762 type->read_bulk_callback, port);
763 }
764
765 port->read_urb = port->read_urbs[0];
766 port->bulk_in_buffer = port->bulk_in_buffers[0];
767
768 return 0;
769}
770
771static int setup_port_bulk_out(struct usb_serial_port *port,
772 struct usb_endpoint_descriptor *epd)
773{
774 struct usb_serial_driver *type = port->serial->type;
775 struct usb_device *udev = port->serial->dev;
776 int buffer_size;
777 int i;
778
779 if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
780 return -ENOMEM;
781 if (type->bulk_out_size)
782 buffer_size = type->bulk_out_size;
783 else
784 buffer_size = usb_endpoint_maxp(epd);
785 port->bulk_out_size = buffer_size;
786 port->bulk_out_endpointAddress = epd->bEndpointAddress;
787
788 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
789 set_bit(i, &port->write_urbs_free);
790 port->write_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
791 if (!port->write_urbs[i])
792 return -ENOMEM;
793 port->bulk_out_buffers[i] = kmalloc(buffer_size, GFP_KERNEL);
794 if (!port->bulk_out_buffers[i])
795 return -ENOMEM;
796 usb_fill_bulk_urb(port->write_urbs[i], udev,
797 usb_sndbulkpipe(udev, epd->bEndpointAddress),
798 port->bulk_out_buffers[i], buffer_size,
799 type->write_bulk_callback, port);
800 }
801
802 port->write_urb = port->write_urbs[0];
803 port->bulk_out_buffer = port->bulk_out_buffers[0];
804
805 return 0;
806}
807
808static int setup_port_interrupt_in(struct usb_serial_port *port,
809 struct usb_endpoint_descriptor *epd)
810{
811 struct usb_serial_driver *type = port->serial->type;
812 struct usb_device *udev = port->serial->dev;
813 int buffer_size;
814
815 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
816 if (!port->interrupt_in_urb)
817 return -ENOMEM;
818 buffer_size = usb_endpoint_maxp(epd);
819 port->interrupt_in_endpointAddress = epd->bEndpointAddress;
820 port->interrupt_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
821 if (!port->interrupt_in_buffer)
822 return -ENOMEM;
823 usb_fill_int_urb(port->interrupt_in_urb, udev,
824 usb_rcvintpipe(udev, epd->bEndpointAddress),
825 port->interrupt_in_buffer, buffer_size,
826 type->read_int_callback, port,
827 epd->bInterval);
828
829 return 0;
830}
831
832static int setup_port_interrupt_out(struct usb_serial_port *port,
833 struct usb_endpoint_descriptor *epd)
834{
835 struct usb_serial_driver *type = port->serial->type;
836 struct usb_device *udev = port->serial->dev;
837 int buffer_size;
838
839 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
840 if (!port->interrupt_out_urb)
841 return -ENOMEM;
842 buffer_size = usb_endpoint_maxp(epd);
843 port->interrupt_out_size = buffer_size;
844 port->interrupt_out_endpointAddress = epd->bEndpointAddress;
845 port->interrupt_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
846 if (!port->interrupt_out_buffer)
847 return -ENOMEM;
848 usb_fill_int_urb(port->interrupt_out_urb, udev,
849 usb_sndintpipe(udev, epd->bEndpointAddress),
850 port->interrupt_out_buffer, buffer_size,
851 type->write_int_callback, port,
852 epd->bInterval);
853
854 return 0;
855}
856
2edd284b 857static int usb_serial_probe(struct usb_interface *interface,
1da177e4
LT
858 const struct usb_device_id *id)
859{
92931d24 860 struct device *ddev = &interface->dev;
a8d6f0a9 861 struct usb_device *dev = interface_to_usbdev(interface);
1da177e4
LT
862 struct usb_serial *serial = NULL;
863 struct usb_serial_port *port;
1546e6ae 864 struct usb_serial_endpoints *epds;
ea65370d 865 struct usb_serial_driver *type = NULL;
1da177e4 866 int retval;
1da177e4 867 int i;
1da177e4 868 int num_ports = 0;
ef88f33f 869 unsigned char max_endpoints;
1da177e4 870
0daeed38 871 mutex_lock(&table_lock);
1da177e4
LT
872 type = search_serial_device(interface);
873 if (!type) {
0daeed38 874 mutex_unlock(&table_lock);
92931d24 875 dev_dbg(ddev, "none matched\n");
1da177e4
LT
876 return -ENODEV;
877 }
878
0daeed38
AK
879 if (!try_module_get(type->driver.owner)) {
880 mutex_unlock(&table_lock);
92931d24 881 dev_err(ddev, "module get failed, exiting\n");
0daeed38
AK
882 return -EIO;
883 }
884 mutex_unlock(&table_lock);
885
a8d6f0a9 886 serial = create_serial(dev, interface, type);
1da177e4 887 if (!serial) {
c2fef456
JH
888 retval = -ENOMEM;
889 goto err_put_module;
1da177e4
LT
890 }
891
892 /* if this device type has a probe function, call it */
893 if (type->probe) {
894 const struct usb_device_id *id;
895
93bacefc 896 id = get_iface_id(type, interface);
1da177e4 897 retval = type->probe(serial, id);
1da177e4
LT
898
899 if (retval) {
92931d24 900 dev_dbg(ddev, "sub driver rejected device\n");
c2fef456 901 goto err_put_serial;
1da177e4
LT
902 }
903 }
904
905 /* descriptor matches, let's find the endpoints needed */
1546e6ae
JH
906 epds = kzalloc(sizeof(*epds), GFP_KERNEL);
907 if (!epds) {
908 retval = -ENOMEM;
909 goto err_put_serial;
1da177e4
LT
910 }
911
1546e6ae
JH
912 find_endpoints(serial, epds);
913
92e6b2c6
JH
914 if (epds->num_bulk_in < type->num_bulk_in ||
915 epds->num_bulk_out < type->num_bulk_out ||
916 epds->num_interrupt_in < type->num_interrupt_in ||
917 epds->num_interrupt_out < type->num_interrupt_out) {
918 dev_err(ddev, "required endpoints missing\n");
919 retval = -ENODEV;
920 goto err_free_epds;
921 }
a794499b
JH
922
923 if (type->calc_num_ports) {
924 retval = type->calc_num_ports(serial, epds);
925 if (retval < 0)
92e6b2c6 926 goto err_free_epds;
a794499b 927 num_ports = retval;
1da177e4
LT
928 }
929
a794499b
JH
930 if (!num_ports)
931 num_ports = type->num_ports;
932
5654699f
JH
933 if (num_ports > MAX_NUM_PORTS) {
934 dev_warn(ddev, "too many ports requested: %d\n", num_ports);
935 num_ports = MAX_NUM_PORTS;
936 }
937
ef88f33f 938 serial->num_ports = (unsigned char)num_ports;
1546e6ae
JH
939 serial->num_bulk_in = epds->num_bulk_in;
940 serial->num_bulk_out = epds->num_bulk_out;
941 serial->num_interrupt_in = epds->num_interrupt_in;
942 serial->num_interrupt_out = epds->num_interrupt_out;
1da177e4 943
063a2da8 944 /* found all that we need */
92931d24 945 dev_info(ddev, "%s converter detected\n", type->description);
063a2da8 946
1da177e4 947 /* create our ports, we need as many as the max endpoints */
a8d6f0a9
AC
948 /* we don't use num_ports here because some devices have more
949 endpoint pairs than ports */
1546e6ae
JH
950 max_endpoints = max(epds->num_bulk_in, epds->num_bulk_out);
951 max_endpoints = max(max_endpoints, epds->num_interrupt_in);
952 max_endpoints = max(max_endpoints, epds->num_interrupt_out);
ef88f33f 953 max_endpoints = max(max_endpoints, serial->num_ports);
1da177e4 954 serial->num_port_pointers = max_endpoints;
4b10f0f3 955
d9a38a87 956 dev_dbg(ddev, "setting up %d port structure(s)\n", max_endpoints);
1da177e4 957 for (i = 0; i < max_endpoints; ++i) {
80b6ca48 958 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
c22ac6d2
JH
959 if (!port) {
960 retval = -ENOMEM;
961 goto err_free_epds;
962 }
4a90f09b 963 tty_port_init(&port->port);
335f8514 964 port->port.ops = &serial_port_ops;
1da177e4 965 port->serial = serial;
507ca9bc 966 spin_lock_init(&port->lock);
e1108a63
AC
967 /* Keep this for private driver use for the moment but
968 should probably go away */
c4028958 969 INIT_WORK(&port->work, usb_serial_port_work);
1da177e4 970 serial->port[i] = port;
41bd34dd
AS
971 port->dev.parent = &interface->dev;
972 port->dev.driver = NULL;
973 port->dev.bus = &usb_serial_bus_type;
69a3d212 974 port->dev.release = &usb_serial_port_release;
2deb96b5 975 port->dev.groups = usb_serial_port_groups;
41bd34dd 976 device_initialize(&port->dev);
1da177e4
LT
977 }
978
979 /* set up the endpoint information */
1546e6ae 980 for (i = 0; i < epds->num_bulk_in; ++i) {
45e5d4d4
JH
981 retval = setup_port_bulk_in(serial->port[i], epds->bulk_in[i]);
982 if (retval)
c22ac6d2 983 goto err_free_epds;
1da177e4
LT
984 }
985
1546e6ae 986 for (i = 0; i < epds->num_bulk_out; ++i) {
45e5d4d4
JH
987 retval = setup_port_bulk_out(serial->port[i],
988 epds->bulk_out[i]);
989 if (retval)
c22ac6d2 990 goto err_free_epds;
1da177e4
LT
991 }
992
993 if (serial->type->read_int_callback) {
1546e6ae 994 for (i = 0; i < epds->num_interrupt_in; ++i) {
45e5d4d4
JH
995 retval = setup_port_interrupt_in(serial->port[i],
996 epds->interrupt_in[i]);
997 if (retval)
c22ac6d2 998 goto err_free_epds;
1da177e4 999 }
1546e6ae 1000 } else if (epds->num_interrupt_in) {
92931d24 1001 dev_dbg(ddev, "The device claims to support interrupt in transfers, but read_int_callback is not defined\n");
1da177e4 1002 }
a8d6f0a9 1003
1da177e4 1004 if (serial->type->write_int_callback) {
1546e6ae 1005 for (i = 0; i < epds->num_interrupt_out; ++i) {
45e5d4d4
JH
1006 retval = setup_port_interrupt_out(serial->port[i],
1007 epds->interrupt_out[i]);
1008 if (retval)
c22ac6d2 1009 goto err_free_epds;
1da177e4 1010 }
1546e6ae 1011 } else if (epds->num_interrupt_out) {
92931d24 1012 dev_dbg(ddev, "The device claims to support interrupt out transfers, but write_int_callback is not defined\n");
1da177e4 1013 }
a8d6f0a9 1014
bdce6612
JH
1015 usb_set_intfdata(interface, serial);
1016
1da177e4
LT
1017 /* if this device type has an attach function, call it */
1018 if (type->attach) {
a8d6f0a9 1019 retval = type->attach(serial);
1da177e4 1020 if (retval < 0)
c22ac6d2 1021 goto err_free_epds;
a4720c65 1022 serial->attached = 1;
1da177e4 1023 if (retval > 0) {
a8d6f0a9
AC
1024 /* quietly accept this device, but don't bind to a
1025 serial port as it's about to disappear */
0a3c8549 1026 serial->num_ports = 0;
1da177e4
LT
1027 goto exit;
1028 }
a4720c65
AS
1029 } else {
1030 serial->attached = 1;
1da177e4
LT
1031 }
1032
c22ac6d2
JH
1033 retval = allocate_minors(serial, num_ports);
1034 if (retval) {
e5b1e206 1035 dev_err(ddev, "No more free serial minor numbers\n");
c22ac6d2 1036 goto err_free_epds;
34ef50e5
ON
1037 }
1038
1da177e4
LT
1039 /* register all of the individual ports with the driver core */
1040 for (i = 0; i < num_ports; ++i) {
1041 port = serial->port[i];
1143832e 1042 dev_set_name(&port->dev, "ttyUSB%d", port->minor);
d9a38a87 1043 dev_dbg(ddev, "registering %s\n", dev_name(&port->dev));
a7a6b79b
ML
1044 device_enable_async_suspend(&port->dev);
1045
41bd34dd 1046 retval = device_add(&port->dev);
891a3b1f 1047 if (retval)
92931d24 1048 dev_err(ddev, "Error registering port device, continuing\n");
1da177e4
LT
1049 }
1050
126d26f6
JH
1051 if (num_ports > 0)
1052 usb_serial_console_init(serial->port[0]->minor);
1da177e4 1053exit:
1546e6ae 1054 kfree(epds);
d92a3ca6 1055 module_put(type->driver.owner);
1da177e4
LT
1056 return 0;
1057
92e6b2c6
JH
1058err_free_epds:
1059 kfree(epds);
c2fef456 1060err_put_serial:
41bd34dd 1061 usb_serial_put(serial);
c2fef456 1062err_put_module:
d92a3ca6 1063 module_put(type->driver.owner);
c2fef456
JH
1064
1065 return retval;
1da177e4
LT
1066}
1067
32078f91 1068static void usb_serial_disconnect(struct usb_interface *interface)
1da177e4
LT
1069{
1070 int i;
a8d6f0a9 1071 struct usb_serial *serial = usb_get_intfdata(interface);
1da177e4
LT
1072 struct device *dev = &interface->dev;
1073 struct usb_serial_port *port;
3fff3b43 1074 struct tty_struct *tty;
1da177e4 1075
73e487fd 1076 usb_serial_console_disconnect(serial);
1da177e4 1077
a1cd7e99 1078 mutex_lock(&serial->disc_mutex);
a1cd7e99
ON
1079 /* must set a flag, to signal subdrivers */
1080 serial->disconnected = 1;
2d93148a
AS
1081 mutex_unlock(&serial->disc_mutex);
1082
a1cd7e99
ON
1083 for (i = 0; i < serial->num_ports; ++i) {
1084 port = serial->port[i];
3fff3b43
JH
1085 tty = tty_port_tty_get(&port->port);
1086 if (tty) {
1087 tty_vhangup(tty);
1088 tty_kref_put(tty);
2d93148a 1089 }
3fff3b43
JH
1090 usb_serial_port_poison_urbs(port);
1091 wake_up_interruptible(&port->port.delta_msr_wait);
1092 cancel_work_sync(&port->work);
1093 if (device_is_registered(&port->dev))
1094 device_del(&port->dev);
2d93148a 1095 }
0f16cfe3
JH
1096 if (serial->type->disconnect)
1097 serial->type->disconnect(serial);
2d93148a 1098
41bd34dd 1099 /* let the last holder of this object cause it to be cleaned up */
a1cd7e99 1100 usb_serial_put(serial);
1da177e4
LT
1101 dev_info(dev, "device disconnected\n");
1102}
1103
ec22559e
ON
1104int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1105{
1106 struct usb_serial *serial = usb_get_intfdata(intf);
ec22559e
ON
1107 int i, r = 0;
1108
f8bece8d
ON
1109 serial->suspending = 1;
1110
93e4f47f
ML
1111 /*
1112 * serial->type->suspend() MUST return 0 in system sleep context,
1113 * otherwise, the resume callback has to recover device from
1114 * previous suspend failure.
1115 */
81e5b23c
ON
1116 if (serial->type->suspend) {
1117 r = serial->type->suspend(serial, message);
a5f6005d
ON
1118 if (r < 0) {
1119 serial->suspending = 0;
81e5b23c 1120 goto err_out;
a5f6005d 1121 }
81e5b23c
ON
1122 }
1123
3fff3b43
JH
1124 for (i = 0; i < serial->num_ports; ++i)
1125 usb_serial_port_poison_urbs(serial->port[i]);
81e5b23c 1126err_out:
ec22559e
ON
1127 return r;
1128}
1129EXPORT_SYMBOL(usb_serial_suspend);
1130
6a5c821c
JH
1131static void usb_serial_unpoison_port_urbs(struct usb_serial *serial)
1132{
6a5c821c
JH
1133 int i;
1134
3fff3b43
JH
1135 for (i = 0; i < serial->num_ports; ++i)
1136 usb_serial_port_unpoison_urbs(serial->port[i]);
6a5c821c
JH
1137}
1138
ec22559e
ON
1139int usb_serial_resume(struct usb_interface *intf)
1140{
1141 struct usb_serial *serial = usb_get_intfdata(intf);
c49cfa91 1142 int rv;
ec22559e 1143
6a5c821c
JH
1144 usb_serial_unpoison_port_urbs(serial);
1145
f8bece8d 1146 serial->suspending = 0;
8abaee23 1147 if (serial->type->resume)
c49cfa91
ON
1148 rv = serial->type->resume(serial);
1149 else
1150 rv = usb_serial_generic_resume(serial);
f8bece8d 1151
c49cfa91 1152 return rv;
ec22559e
ON
1153}
1154EXPORT_SYMBOL(usb_serial_resume);
1155
7186364e
GKH
1156static int usb_serial_reset_resume(struct usb_interface *intf)
1157{
1158 struct usb_serial *serial = usb_get_intfdata(intf);
1159 int rv;
1160
6a5c821c
JH
1161 usb_serial_unpoison_port_urbs(serial);
1162
7186364e 1163 serial->suspending = 0;
ca0400d2 1164 if (serial->type->reset_resume) {
7186364e 1165 rv = serial->type->reset_resume(serial);
ca0400d2 1166 } else {
dcd82cd1
GKH
1167 rv = -EOPNOTSUPP;
1168 intf->needs_binding = 1;
1169 }
7186364e
GKH
1170
1171 return rv;
1172}
1173
b68e31d0 1174static const struct tty_operations serial_ops = {
1da177e4
LT
1175 .open = serial_open,
1176 .close = serial_close,
1177 .write = serial_write,
3e1f4901 1178 .hangup = serial_hangup,
1da177e4
LT
1179 .write_room = serial_write_room,
1180 .ioctl = serial_ioctl,
1181 .set_termios = serial_set_termios,
1182 .throttle = serial_throttle,
1183 .unthrottle = serial_unthrottle,
1184 .break_ctl = serial_break,
1185 .chars_in_buffer = serial_chars_in_buffer,
0693196f 1186 .wait_until_sent = serial_wait_until_sent,
1da177e4
LT
1187 .tiocmget = serial_tiocmget,
1188 .tiocmset = serial_tiocmset,
3e1f4901
JH
1189 .get_icount = serial_get_icount,
1190 .cleanup = serial_cleanup,
1191 .install = serial_install,
6fd69d3c 1192 .proc_fops = &serial_proc_fops,
1da177e4
LT
1193};
1194
335f8514 1195
1da177e4
LT
1196struct tty_driver *usb_serial_tty_driver;
1197
1198static int __init usb_serial_init(void)
1199{
1da177e4
LT
1200 int result;
1201
455b4f7e 1202 usb_serial_tty_driver = alloc_tty_driver(USB_SERIAL_TTY_MINORS);
1da177e4
LT
1203 if (!usb_serial_tty_driver)
1204 return -ENOMEM;
1205
1206 /* Initialize our global data */
1da177e4
LT
1207 result = bus_register(&usb_serial_bus_type);
1208 if (result) {
92931d24 1209 pr_err("%s - registering bus driver failed\n", __func__);
1da177e4
LT
1210 goto exit_bus;
1211 }
1212
1da177e4 1213 usb_serial_tty_driver->driver_name = "usbserial";
3e1f4901 1214 usb_serial_tty_driver->name = "ttyUSB";
455b4f7e 1215 usb_serial_tty_driver->major = USB_SERIAL_TTY_MAJOR;
1da177e4
LT
1216 usb_serial_tty_driver->minor_start = 0;
1217 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1218 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
a8d6f0a9
AC
1219 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1220 TTY_DRIVER_DYNAMIC_DEV;
1da177e4 1221 usb_serial_tty_driver->init_termios = tty_std_termios;
a8d6f0a9
AC
1222 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1223 | HUPCL | CLOCAL;
a5b6f60c
AC
1224 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1225 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1da177e4
LT
1226 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1227 result = tty_register_driver(usb_serial_tty_driver);
1228 if (result) {
92931d24 1229 pr_err("%s - tty_register_driver failed\n", __func__);
1da177e4
LT
1230 goto exit_reg_driver;
1231 }
1232
06299db3 1233 /* register the generic driver, if we should */
3033bc8d 1234 result = usb_serial_generic_register();
06299db3 1235 if (result < 0) {
92931d24 1236 pr_err("%s - registering generic driver failed\n", __func__);
06299db3
GKH
1237 goto exit_generic;
1238 }
1239
1da177e4
LT
1240 return result;
1241
06299db3 1242exit_generic:
1da177e4
LT
1243 tty_unregister_driver(usb_serial_tty_driver);
1244
1245exit_reg_driver:
1da177e4
LT
1246 bus_unregister(&usb_serial_bus_type);
1247
1248exit_bus:
92931d24 1249 pr_err("%s - returning with error %d\n", __func__, result);
1da177e4
LT
1250 put_tty_driver(usb_serial_tty_driver);
1251 return result;
1252}
1253
1254
1255static void __exit usb_serial_exit(void)
1256{
1257 usb_serial_console_exit();
1258
1259 usb_serial_generic_deregister();
1260
1da177e4
LT
1261 tty_unregister_driver(usb_serial_tty_driver);
1262 put_tty_driver(usb_serial_tty_driver);
1263 bus_unregister(&usb_serial_bus_type);
d23f47d4 1264 idr_destroy(&serial_minors);
1da177e4
LT
1265}
1266
1267
1268module_init(usb_serial_init);
1269module_exit(usb_serial_exit);
1270
1271#define set_to_generic_if_null(type, function) \
1272 do { \
1273 if (!type->function) { \
1274 type->function = usb_serial_generic_##function; \
c3452f5e
JH
1275 pr_debug("%s: using generic " #function "\n", \
1276 type->driver.name); \
1277 } \
1da177e4
LT
1278 } while (0)
1279
c3452f5e 1280static void usb_serial_operations_init(struct usb_serial_driver *device)
1da177e4
LT
1281{
1282 set_to_generic_if_null(device, open);
1283 set_to_generic_if_null(device, write);
1284 set_to_generic_if_null(device, close);
1285 set_to_generic_if_null(device, write_room);
1286 set_to_generic_if_null(device, chars_in_buffer);
dcf01050
JH
1287 if (device->tx_empty)
1288 set_to_generic_if_null(device, wait_until_sent);
1da177e4
LT
1289 set_to_generic_if_null(device, read_bulk_callback);
1290 set_to_generic_if_null(device, write_bulk_callback);
23154320 1291 set_to_generic_if_null(device, process_read_urb);
eaa3bcb0 1292 set_to_generic_if_null(device, prepare_write_buffer);
1da177e4
LT
1293}
1294
f799e767 1295static int usb_serial_register(struct usb_serial_driver *driver)
1da177e4
LT
1296{
1297 int retval;
1298
e4abe665
DY
1299 if (usb_disabled())
1300 return -ENODEV;
1301
269bda1c
GKH
1302 if (!driver->description)
1303 driver->description = driver->driver.name;
5620b5f7
AS
1304 if (!driver->usb_driver) {
1305 WARN(1, "Serial driver %s has no usb_driver\n",
1306 driver->description);
1307 return -EINVAL;
1308 }
269bda1c 1309
c3452f5e
JH
1310 usb_serial_operations_init(driver);
1311
1da177e4 1312 /* Add this device to our list of devices */
0daeed38 1313 mutex_lock(&table_lock);
ea65370d 1314 list_add(&driver->driver_list, &usb_serial_driver_list);
1da177e4 1315
ea65370d 1316 retval = usb_serial_bus_register(driver);
1da177e4 1317 if (retval) {
92931d24 1318 pr_err("problem %d when registering driver %s\n", retval, driver->description);
ea65370d 1319 list_del(&driver->driver_list);
ca0400d2 1320 } else {
ee42f6c9 1321 pr_info("USB Serial support registered for %s\n", driver->description);
ca0400d2 1322 }
0daeed38 1323 mutex_unlock(&table_lock);
1da177e4
LT
1324 return retval;
1325}
1326
f799e767 1327static void usb_serial_deregister(struct usb_serial_driver *device)
1da177e4 1328{
ee42f6c9 1329 pr_info("USB Serial deregistering driver %s\n", device->description);
10164c2a 1330
0daeed38 1331 mutex_lock(&table_lock);
1da177e4 1332 list_del(&device->driver_list);
0daeed38 1333 mutex_unlock(&table_lock);
10164c2a
JH
1334
1335 usb_serial_bus_deregister(device);
1da177e4 1336}
1da177e4 1337
765e0ba6
AS
1338/**
1339 * usb_serial_register_drivers - register drivers for a usb-serial module
765e0ba6 1340 * @serial_drivers: NULL-terminated array of pointers to drivers to be registered
68e24113
GKH
1341 * @name: name of the usb_driver for this set of @serial_drivers
1342 * @id_table: list of all devices this @serial_drivers set binds to
765e0ba6 1343 *
68e24113
GKH
1344 * Registers all the drivers in the @serial_drivers array, and dynamically
1345 * creates a struct usb_driver with the name @name and id_table of @id_table.
765e0ba6 1346 */
68e24113
GKH
1347int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
1348 const char *name,
1349 const struct usb_device_id *id_table)
765e0ba6
AS
1350{
1351 int rc;
68e24113 1352 struct usb_driver *udriver;
765e0ba6
AS
1353 struct usb_serial_driver * const *sd;
1354
1355 /*
1356 * udriver must be registered before any of the serial drivers,
1357 * because the store_new_id() routine for the serial drivers (in
1358 * bus.c) probes udriver.
1359 *
1360 * Performance hack: We don't want udriver to be probed until
1361 * the serial drivers are registered, because the probe would
1362 * simply fail for lack of a matching serial driver.
68e24113 1363 * So we leave udriver's id_table set to NULL until we are all set.
5cbe61c5
AS
1364 *
1365 * Suspend/resume support is implemented in the usb-serial core,
1366 * so fill in the PM-related fields in udriver.
765e0ba6 1367 */
68e24113
GKH
1368 udriver = kzalloc(sizeof(*udriver), GFP_KERNEL);
1369 if (!udriver)
1370 return -ENOMEM;
765e0ba6 1371
68e24113 1372 udriver->name = name;
765e0ba6 1373 udriver->no_dynamic_id = 1;
5cbe61c5
AS
1374 udriver->supports_autosuspend = 1;
1375 udriver->suspend = usb_serial_suspend;
1376 udriver->resume = usb_serial_resume;
5026bb07 1377 udriver->probe = usb_serial_probe;
32078f91 1378 udriver->disconnect = usb_serial_disconnect;
7186364e
GKH
1379
1380 /* we only set the reset_resume field if the serial_driver has one */
1381 for (sd = serial_drivers; *sd; ++sd) {
44b0f083 1382 if ((*sd)->reset_resume) {
7186364e
GKH
1383 udriver->reset_resume = usb_serial_reset_resume;
1384 break;
44b0f083 1385 }
7186364e
GKH
1386 }
1387
765e0ba6
AS
1388 rc = usb_register(udriver);
1389 if (rc)
647024a7 1390 goto failed_usb_register;
765e0ba6
AS
1391
1392 for (sd = serial_drivers; *sd; ++sd) {
1393 (*sd)->usb_driver = udriver;
1394 rc = usb_serial_register(*sd);
1395 if (rc)
1396 goto failed;
1397 }
1398
68e24113
GKH
1399 /* Now set udriver's id_table and look for matches */
1400 udriver->id_table = id_table;
765e0ba6
AS
1401 rc = driver_attach(&udriver->drvwrap.driver);
1402 return 0;
1403
1404 failed:
1405 while (sd-- > serial_drivers)
1406 usb_serial_deregister(*sd);
1407 usb_deregister(udriver);
647024a7
AK
1408failed_usb_register:
1409 kfree(udriver);
765e0ba6
AS
1410 return rc;
1411}
1412EXPORT_SYMBOL_GPL(usb_serial_register_drivers);
1413
1414/**
1415 * usb_serial_deregister_drivers - deregister drivers for a usb-serial module
765e0ba6
AS
1416 * @serial_drivers: NULL-terminated array of pointers to drivers to be deregistered
1417 *
68e24113
GKH
1418 * Deregisters all the drivers in the @serial_drivers array and deregisters and
1419 * frees the struct usb_driver that was created by the call to
1420 * usb_serial_register_drivers().
765e0ba6 1421 */
68e24113 1422void usb_serial_deregister_drivers(struct usb_serial_driver *const serial_drivers[])
765e0ba6 1423{
68e24113
GKH
1424 struct usb_driver *udriver = (*serial_drivers)->usb_driver;
1425
765e0ba6
AS
1426 for (; *serial_drivers; ++serial_drivers)
1427 usb_serial_deregister(*serial_drivers);
1428 usb_deregister(udriver);
68e24113 1429 kfree(udriver);
765e0ba6
AS
1430}
1431EXPORT_SYMBOL_GPL(usb_serial_deregister_drivers);
1da177e4 1432
a8d6f0a9
AC
1433MODULE_AUTHOR(DRIVER_AUTHOR);
1434MODULE_DESCRIPTION(DRIVER_DESC);
627cfa89 1435MODULE_LICENSE("GPL v2");