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