blob: b59a0536ea5c98a2952ef9c4075986dc43445ba7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Serial Converter driver
3 *
Greg Kroah-Hartman502b95c2005-06-20 21:15:16 -07004 * Copyright (C) 1999 - 2005 Greg Kroah-Hartman (greg@kroah.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 *
Greg Kroah-Hartman502b95c2005-06-20 21:15:16 -070012 * This driver was originally based on the ACM driver by Armin Fuerst (which was
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * based on a driver by Brad Keryan)
14 *
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/tty.h>
24#include <linux/tty_driver.h>
25#include <linux/tty_flip.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/spinlock.h>
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -030029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/list.h>
31#include <linux/smp_lock.h>
32#include <asm/uaccess.h>
33#include <linux/usb.h>
34#include "usb-serial.h"
35#include "pl2303.h"
36
37/*
38 * Version Information
39 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
41#define DRIVER_DESC "USB Serial Driver core"
42
43/* Driver structure we register with the USB core */
44static struct usb_driver usb_serial_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .name = "usbserial",
46 .probe = usb_serial_probe,
47 .disconnect = usb_serial_disconnect,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080048 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
51/* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
52 the MODULE_DEVICE_TABLE declarations in each serial driver
53 cause the "hotplug" program to pull in whatever module is necessary
54 via modprobe, and modprobe will load usbserial because the serial
55 drivers depend on it.
56*/
57
58static int debug;
59static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
60static LIST_HEAD(usb_serial_driver_list);
61
62struct usb_serial *usb_serial_get_by_index(unsigned index)
63{
64 struct usb_serial *serial = serial_table[index];
65
66 if (serial)
67 kref_get(&serial->kref);
68 return serial;
69}
70
71static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
72{
73 unsigned int i, j;
74 int good_spot;
75
76 dbg("%s %d", __FUNCTION__, num_ports);
77
78 *minor = 0;
79 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
80 if (serial_table[i])
81 continue;
82
83 good_spot = 1;
84 for (j = 1; j <= num_ports-1; ++j)
85 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
86 good_spot = 0;
87 i += j;
88 break;
89 }
90 if (good_spot == 0)
91 continue;
92
93 *minor = i;
94 dbg("%s - minor base = %d", __FUNCTION__, *minor);
95 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
96 serial_table[i] = serial;
97 return serial;
98 }
99 return NULL;
100}
101
102static void return_serial(struct usb_serial *serial)
103{
104 int i;
105
106 dbg("%s", __FUNCTION__);
107
108 if (serial == NULL)
109 return;
110
111 for (i = 0; i < serial->num_ports; ++i) {
112 serial_table[serial->minor + i] = NULL;
113 }
114}
115
116static void destroy_serial(struct kref *kref)
117{
118 struct usb_serial *serial;
119 struct usb_serial_port *port;
120 int i;
121
122 serial = to_usb_serial(kref);
123
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700124 dbg("%s - %s", __FUNCTION__, serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 serial->type->shutdown(serial);
127
128 /* return the minor range that this device had */
129 return_serial(serial);
130
131 for (i = 0; i < serial->num_ports; ++i)
132 serial->port[i]->open_count = 0;
133
134 /* the ports are cleaned up and released in port_release() */
135 for (i = 0; i < serial->num_ports; ++i)
136 if (serial->port[i]->dev.parent != NULL) {
137 device_unregister(&serial->port[i]->dev);
138 serial->port[i] = NULL;
139 }
140
141 /* If this is a "fake" port, we have to clean it up here, as it will
142 * not get cleaned up in port_release() as it was never registered with
143 * the driver core */
144 if (serial->num_ports < serial->num_port_pointers) {
145 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
146 port = serial->port[i];
147 if (!port)
148 continue;
149 usb_kill_urb(port->read_urb);
150 usb_free_urb(port->read_urb);
151 usb_kill_urb(port->write_urb);
152 usb_free_urb(port->write_urb);
153 usb_kill_urb(port->interrupt_in_urb);
154 usb_free_urb(port->interrupt_in_urb);
155 usb_kill_urb(port->interrupt_out_urb);
156 usb_free_urb(port->interrupt_out_urb);
157 kfree(port->bulk_in_buffer);
158 kfree(port->bulk_out_buffer);
159 kfree(port->interrupt_in_buffer);
160 kfree(port->interrupt_out_buffer);
161 }
162 }
163
Pete Zaitcev2f8ad9a2006-05-24 11:04:04 -0700164 flush_scheduled_work(); /* port->work */
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 usb_put_dev(serial->dev);
167
168 /* free up any memory that we allocated */
169 kfree (serial);
170}
171
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200172void usb_serial_put(struct usb_serial *serial)
173{
174 kref_put(&serial->kref, destroy_serial);
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*****************************************************************************
178 * Driver tty interface functions
179 *****************************************************************************/
180static int serial_open (struct tty_struct *tty, struct file * filp)
181{
182 struct usb_serial *serial;
183 struct usb_serial_port *port;
184 unsigned int portNumber;
185 int retval;
186
187 dbg("%s", __FUNCTION__);
188
189 /* get the serial object associated with this tty pointer */
190 serial = usb_serial_get_by_index(tty->index);
191 if (!serial) {
192 tty->driver_data = NULL;
193 return -ENODEV;
194 }
195
196 portNumber = tty->index - serial->minor;
197 port = serial->port[portNumber];
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300198 if (!port) {
199 retval = -ENODEV;
200 goto bailout_kref_put;
201 }
Luiz Fernando Capitulino8a4613f2005-11-28 19:16:07 -0200202
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300203 if (mutex_lock_interruptible(&port->mutex)) {
204 retval = -ERESTARTSYS;
205 goto bailout_kref_put;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 ++port->open_count;
209
Paul Fulghumca854852006-04-13 22:28:17 +0200210 /* set up our port structure making the tty driver
211 * remember our port object, and us it */
212 tty->driver_data = port;
213 port->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Paul Fulghumca854852006-04-13 22:28:17 +0200215 if (port->open_count == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* lock this module before we call it
218 * this may fail, which means we must bail out,
219 * safe because we are called with BKL held */
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700220 if (!try_module_get(serial->type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 retval = -ENODEV;
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300222 goto bailout_mutex_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
225 /* only call the device specific open if this
226 * is the first time the port is opened */
227 retval = serial->type->open(port, filp);
228 if (retval)
229 goto bailout_module_put;
230 }
231
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300232 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234
235bailout_module_put:
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700236 module_put(serial->type->driver.owner);
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300237bailout_mutex_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 port->open_count = 0;
Frank Gevaertsb059c812006-06-14 15:52:05 +0200239 tty->driver_data = NULL;
240 port->tty = NULL;
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300241 mutex_unlock(&port->mutex);
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300242bailout_kref_put:
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200243 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return retval;
245}
246
247static void serial_close(struct tty_struct *tty, struct file * filp)
248{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200249 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (!port)
252 return;
253
254 dbg("%s - port %d", __FUNCTION__, port->number);
255
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300256 mutex_lock(&port->mutex);
Luiz Fernando Capitulino8a4613f2005-11-28 19:16:07 -0200257
Greg Kroah-Hartman91c0bce2006-03-06 13:25:52 -0800258 if (port->open_count == 0) {
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300259 mutex_unlock(&port->mutex);
Greg Kroah-Hartman91c0bce2006-03-06 13:25:52 -0800260 return;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 --port->open_count;
264 if (port->open_count == 0) {
265 /* only call the device specific close if this
266 * port is being closed by the last owner */
267 port->serial->type->close(port, filp);
268
269 if (port->tty) {
270 if (port->tty->driver_data)
271 port->tty->driver_data = NULL;
272 port->tty = NULL;
273 }
274
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700275 module_put(port->serial->type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300278 mutex_unlock(&port->mutex);
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200279 usb_serial_put(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
283{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200284 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int retval = -EINVAL;
286
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200287 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200288 goto exit;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
291
292 if (!port->open_count) {
293 dbg("%s - port not opened", __FUNCTION__);
294 goto exit;
295 }
296
297 /* pass on to the driver specific version of this function */
298 retval = port->serial->type->write(port, buf, count);
299
300exit:
301 return retval;
302}
303
304static int serial_write_room (struct tty_struct *tty)
305{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200306 struct usb_serial_port *port = tty->driver_data;
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300307 int retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200309 if (!port)
310 goto exit;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 dbg("%s - port %d", __FUNCTION__, port->number);
313
314 if (!port->open_count) {
315 dbg("%s - port not open", __FUNCTION__);
316 goto exit;
317 }
318
319 /* pass on to the driver specific version of this function */
320 retval = port->serial->type->write_room(port);
321
322exit:
323 return retval;
324}
325
326static int serial_chars_in_buffer (struct tty_struct *tty)
327{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200328 struct usb_serial_port *port = tty->driver_data;
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300329 int retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200331 if (!port)
332 goto exit;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dbg("%s = port %d", __FUNCTION__, port->number);
335
336 if (!port->open_count) {
337 dbg("%s - port not open", __FUNCTION__);
338 goto exit;
339 }
340
341 /* pass on to the driver specific version of this function */
342 retval = port->serial->type->chars_in_buffer(port);
343
344exit:
345 return retval;
346}
347
348static void serial_throttle (struct tty_struct * tty)
349{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200350 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200352 if (!port)
353 return;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dbg("%s - port %d", __FUNCTION__, port->number);
356
357 if (!port->open_count) {
358 dbg ("%s - port not open", __FUNCTION__);
359 return;
360 }
361
362 /* pass on to the driver specific version of this function */
363 if (port->serial->type->throttle)
364 port->serial->type->throttle(port);
365}
366
367static void serial_unthrottle (struct tty_struct * tty)
368{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200369 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200371 if (!port)
372 return;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 dbg("%s - port %d", __FUNCTION__, port->number);
375
376 if (!port->open_count) {
377 dbg("%s - port not open", __FUNCTION__);
378 return;
379 }
380
381 /* pass on to the driver specific version of this function */
382 if (port->serial->type->unthrottle)
383 port->serial->type->unthrottle(port);
384}
385
386static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
387{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200388 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int retval = -ENODEV;
390
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200391 if (!port)
392 goto exit;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
395
396 if (!port->open_count) {
397 dbg ("%s - port not open", __FUNCTION__);
398 goto exit;
399 }
400
401 /* pass on to the driver specific version of this function if it is available */
402 if (port->serial->type->ioctl)
403 retval = port->serial->type->ioctl(port, file, cmd, arg);
404 else
405 retval = -ENOIOCTLCMD;
406
407exit:
408 return retval;
409}
410
411static void serial_set_termios (struct tty_struct *tty, struct termios * old)
412{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200413 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200415 if (!port)
416 return;
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 dbg("%s - port %d", __FUNCTION__, port->number);
419
420 if (!port->open_count) {
421 dbg("%s - port not open", __FUNCTION__);
422 return;
423 }
424
425 /* pass on to the driver specific version of this function if it is available */
426 if (port->serial->type->set_termios)
427 port->serial->type->set_termios(port, old);
428}
429
430static void serial_break (struct tty_struct *tty, int break_state)
431{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200432 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200434 if (!port)
435 return;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 dbg("%s - port %d", __FUNCTION__, port->number);
438
439 if (!port->open_count) {
440 dbg("%s - port not open", __FUNCTION__);
441 return;
442 }
443
444 /* pass on to the driver specific version of this function if it is available */
445 if (port->serial->type->break_ctl)
446 port->serial->type->break_ctl(port, break_state);
447}
448
449static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
450{
451 struct usb_serial *serial;
452 int length = 0;
453 int i;
454 off_t begin = 0;
455 char tmp[40];
456
457 dbg("%s", __FUNCTION__);
Greg Kroah-Hartman17a882f2005-06-20 21:15:16 -0700458 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
460 serial = usb_serial_get_by_index(i);
461 if (serial == NULL)
462 continue;
463
464 length += sprintf (page+length, "%d:", i);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700465 if (serial->type->driver.owner)
466 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700467 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 length += sprintf (page+length, " vendor:%04x product:%04x",
469 le16_to_cpu(serial->dev->descriptor.idVendor),
470 le16_to_cpu(serial->dev->descriptor.idProduct));
471 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
472 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
473
474 usb_make_path(serial->dev, tmp, sizeof(tmp));
475 length += sprintf (page+length, " path:%s", tmp);
476
477 length += sprintf (page+length, "\n");
478 if ((length + begin) > (off + count))
479 goto done;
480 if ((length + begin) < off) {
481 begin += length;
482 length = 0;
483 }
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200484 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 *eof = 1;
487done:
488 if (off >= (length + begin))
489 return 0;
490 *start = page + (off-begin);
491 return ((count < begin+length-off) ? count : begin+length-off);
492}
493
494static int serial_tiocmget (struct tty_struct *tty, struct file *file)
495{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200496 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200498 if (!port)
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300499 return -ENODEV;
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dbg("%s - port %d", __FUNCTION__, port->number);
502
503 if (!port->open_count) {
504 dbg("%s - port not open", __FUNCTION__);
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300505 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
508 if (port->serial->type->tiocmget)
509 return port->serial->type->tiocmget(port, file);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return -EINVAL;
512}
513
514static int serial_tiocmset (struct tty_struct *tty, struct file *file,
515 unsigned int set, unsigned int clear)
516{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200517 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200519 if (!port)
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300520 return -ENODEV;
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 dbg("%s - port %d", __FUNCTION__, port->number);
523
524 if (!port->open_count) {
525 dbg("%s - port not open", __FUNCTION__);
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300526 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
529 if (port->serial->type->tiocmset)
530 return port->serial->type->tiocmset(port, file, set, clear);
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -EINVAL;
533}
534
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700535/*
536 * We would be calling tty_wakeup here, but unfortunately some line
537 * disciplines have an annoying habit of calling tty->write from
538 * the write wakeup callback (e.g. n_hdlc.c).
539 */
540void usb_serial_port_softint(struct usb_serial_port *port)
541{
542 schedule_work(&port->work);
543}
544
545static void usb_serial_port_work(void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200547 struct usb_serial_port *port = private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct tty_struct *tty;
549
550 dbg("%s - port %d", __FUNCTION__, port->number);
551
552 if (!port)
553 return;
554
555 tty = port->tty;
556 if (!tty)
557 return;
558
559 tty_wakeup(tty);
560}
561
562static void port_release(struct device *dev)
563{
564 struct usb_serial_port *port = to_usb_serial_port(dev);
565
566 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
567 usb_kill_urb(port->read_urb);
568 usb_free_urb(port->read_urb);
569 usb_kill_urb(port->write_urb);
570 usb_free_urb(port->write_urb);
571 usb_kill_urb(port->interrupt_in_urb);
572 usb_free_urb(port->interrupt_in_urb);
573 usb_kill_urb(port->interrupt_out_urb);
574 usb_free_urb(port->interrupt_out_urb);
575 kfree(port->bulk_in_buffer);
576 kfree(port->bulk_out_buffer);
577 kfree(port->interrupt_in_buffer);
578 kfree(port->interrupt_out_buffer);
579 kfree(port);
580}
581
582static struct usb_serial * create_serial (struct usb_device *dev,
583 struct usb_interface *interface,
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700584 struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct usb_serial *serial;
587
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100588 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!serial) {
590 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
591 return NULL;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 serial->dev = usb_get_dev(dev);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700594 serial->type = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 serial->interface = interface;
596 kref_init(&serial->kref);
597
598 return serial;
599}
600
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700601static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct list_head *p;
604 const struct usb_device_id *id;
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700605 struct usb_serial_driver *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Adrian Bunk93b1fae2006-01-10 00:13:33 +0100607 /* Check if the usb id matches a known device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 list_for_each(p, &usb_serial_driver_list) {
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700609 t = list_entry(p, struct usb_serial_driver, driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 id = usb_match_id(iface, t->id_table);
611 if (id != NULL) {
612 dbg("descriptor matches");
613 return t;
614 }
615 }
616
617 return NULL;
618}
619
620int usb_serial_probe(struct usb_interface *interface,
621 const struct usb_device_id *id)
622{
623 struct usb_device *dev = interface_to_usbdev (interface);
624 struct usb_serial *serial = NULL;
625 struct usb_serial_port *port;
626 struct usb_host_interface *iface_desc;
627 struct usb_endpoint_descriptor *endpoint;
628 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
629 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
630 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
631 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700632 struct usb_serial_driver *type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int retval;
634 int minor;
635 int buffer_size;
636 int i;
637 int num_interrupt_in = 0;
638 int num_interrupt_out = 0;
639 int num_bulk_in = 0;
640 int num_bulk_out = 0;
641 int num_ports = 0;
642 int max_endpoints;
643
644 type = search_serial_device(interface);
645 if (!type) {
646 dbg("none matched");
647 return -ENODEV;
648 }
649
650 serial = create_serial (dev, interface, type);
651 if (!serial) {
652 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
653 return -ENOMEM;
654 }
655
656 /* if this device type has a probe function, call it */
657 if (type->probe) {
658 const struct usb_device_id *id;
659
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700660 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 dev_err(&interface->dev, "module get failed, exiting\n");
662 kfree (serial);
663 return -EIO;
664 }
665
666 id = usb_match_id(interface, type->id_table);
667 retval = type->probe(serial, id);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700668 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (retval) {
671 dbg ("sub driver rejected device");
672 kfree (serial);
673 return retval;
674 }
675 }
676
677 /* descriptor matches, let's find the endpoints needed */
678 /* check out the endpoints */
679 iface_desc = interface->cur_altsetting;
680 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
681 endpoint = &iface_desc->endpoint[i].desc;
682
683 if ((endpoint->bEndpointAddress & 0x80) &&
684 ((endpoint->bmAttributes & 3) == 0x02)) {
685 /* we found a bulk in endpoint */
686 dbg("found bulk in on endpoint %d", i);
687 bulk_in_endpoint[num_bulk_in] = endpoint;
688 ++num_bulk_in;
689 }
690
691 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
692 ((endpoint->bmAttributes & 3) == 0x02)) {
693 /* we found a bulk out endpoint */
694 dbg("found bulk out on endpoint %d", i);
695 bulk_out_endpoint[num_bulk_out] = endpoint;
696 ++num_bulk_out;
697 }
698
699 if ((endpoint->bEndpointAddress & 0x80) &&
700 ((endpoint->bmAttributes & 3) == 0x03)) {
701 /* we found a interrupt in endpoint */
702 dbg("found interrupt in on endpoint %d", i);
703 interrupt_in_endpoint[num_interrupt_in] = endpoint;
704 ++num_interrupt_in;
705 }
706
707 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
708 ((endpoint->bmAttributes & 3) == 0x03)) {
709 /* we found an interrupt out endpoint */
710 dbg("found interrupt out on endpoint %d", i);
711 interrupt_out_endpoint[num_interrupt_out] = endpoint;
712 ++num_interrupt_out;
713 }
714 }
715
716#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
717 /* BEGIN HORRIBLE HACK FOR PL2303 */
718 /* this is needed due to the looney way its endpoints are set up */
719 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
720 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
721 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
722 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
723 if (interface != dev->actconfig->interface[0]) {
724 /* check out the endpoints of the other interface*/
725 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
726 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
727 endpoint = &iface_desc->endpoint[i].desc;
728 if ((endpoint->bEndpointAddress & 0x80) &&
729 ((endpoint->bmAttributes & 3) == 0x03)) {
730 /* we found a interrupt in endpoint */
731 dbg("found interrupt in for Prolific device on separate interface");
732 interrupt_in_endpoint[num_interrupt_in] = endpoint;
733 ++num_interrupt_in;
734 }
735 }
736 }
737
738 /* Now make sure the PL-2303 is configured correctly.
739 * If not, give up now and hope this hack will work
740 * properly during a later invocation of usb_serial_probe
741 */
742 if (num_bulk_in == 0 || num_bulk_out == 0) {
743 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
744 kfree (serial);
745 return -ENODEV;
746 }
747 }
748 /* END HORRIBLE HACK FOR PL2303 */
749#endif
750
751 /* found all that we need */
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700752 dev_info(&interface->dev, "%s converter detected\n", type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754#ifdef CONFIG_USB_SERIAL_GENERIC
755 if (type == &usb_serial_generic_device) {
756 num_ports = num_bulk_out;
757 if (num_ports == 0) {
758 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
759 kfree (serial);
760 return -EIO;
761 }
762 }
763#endif
764 if (!num_ports) {
765 /* if this device type has a calc_num_ports function, call it */
766 if (type->calc_num_ports) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700767 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 dev_err(&interface->dev, "module get failed, exiting\n");
769 kfree (serial);
770 return -EIO;
771 }
772 num_ports = type->calc_num_ports (serial);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700773 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 if (!num_ports)
776 num_ports = type->num_ports;
777 }
778
779 if (get_free_serial (serial, num_ports, &minor) == NULL) {
780 dev_err(&interface->dev, "No more free serial devices\n");
781 kfree (serial);
782 return -ENOMEM;
783 }
784
785 serial->minor = minor;
786 serial->num_ports = num_ports;
787 serial->num_bulk_in = num_bulk_in;
788 serial->num_bulk_out = num_bulk_out;
789 serial->num_interrupt_in = num_interrupt_in;
790 serial->num_interrupt_out = num_interrupt_out;
791
792 /* create our ports, we need as many as the max endpoints */
793 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
794 max_endpoints = max(num_bulk_in, num_bulk_out);
795 max_endpoints = max(max_endpoints, num_interrupt_in);
796 max_endpoints = max(max_endpoints, num_interrupt_out);
797 max_endpoints = max(max_endpoints, (int)serial->num_ports);
798 serial->num_port_pointers = max_endpoints;
799 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
800 for (i = 0; i < max_endpoints; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100801 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!port)
803 goto probe_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 port->number = i + serial->minor;
805 port->serial = serial;
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700806 spin_lock_init(&port->lock);
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300807 mutex_init(&port->mutex);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700808 INIT_WORK(&port->work, usb_serial_port_work, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 serial->port[i] = port;
810 }
811
812 /* set up the endpoint information */
813 for (i = 0; i < num_bulk_in; ++i) {
814 endpoint = bulk_in_endpoint[i];
815 port = serial->port[i];
816 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
817 if (!port->read_urb) {
818 dev_err(&interface->dev, "No free urbs available\n");
819 goto probe_error;
820 }
821 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
822 port->bulk_in_size = buffer_size;
823 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
824 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
825 if (!port->bulk_in_buffer) {
826 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
827 goto probe_error;
828 }
829 usb_fill_bulk_urb (port->read_urb, dev,
830 usb_rcvbulkpipe (dev,
831 endpoint->bEndpointAddress),
832 port->bulk_in_buffer, buffer_size,
833 serial->type->read_bulk_callback,
834 port);
835 }
836
837 for (i = 0; i < num_bulk_out; ++i) {
838 endpoint = bulk_out_endpoint[i];
839 port = serial->port[i];
840 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
841 if (!port->write_urb) {
842 dev_err(&interface->dev, "No free urbs available\n");
843 goto probe_error;
844 }
845 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
846 port->bulk_out_size = buffer_size;
847 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
848 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
849 if (!port->bulk_out_buffer) {
850 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
851 goto probe_error;
852 }
853 usb_fill_bulk_urb (port->write_urb, dev,
854 usb_sndbulkpipe (dev,
855 endpoint->bEndpointAddress),
856 port->bulk_out_buffer, buffer_size,
857 serial->type->write_bulk_callback,
858 port);
859 }
860
861 if (serial->type->read_int_callback) {
862 for (i = 0; i < num_interrupt_in; ++i) {
863 endpoint = interrupt_in_endpoint[i];
864 port = serial->port[i];
865 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
866 if (!port->interrupt_in_urb) {
867 dev_err(&interface->dev, "No free urbs available\n");
868 goto probe_error;
869 }
870 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
871 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
872 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
873 if (!port->interrupt_in_buffer) {
874 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
875 goto probe_error;
876 }
877 usb_fill_int_urb (port->interrupt_in_urb, dev,
878 usb_rcvintpipe (dev,
879 endpoint->bEndpointAddress),
880 port->interrupt_in_buffer, buffer_size,
881 serial->type->read_int_callback, port,
882 endpoint->bInterval);
883 }
884 } else if (num_interrupt_in) {
885 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
886 }
887
888 if (serial->type->write_int_callback) {
889 for (i = 0; i < num_interrupt_out; ++i) {
890 endpoint = interrupt_out_endpoint[i];
891 port = serial->port[i];
892 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
893 if (!port->interrupt_out_urb) {
894 dev_err(&interface->dev, "No free urbs available\n");
895 goto probe_error;
896 }
897 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
898 port->interrupt_out_size = buffer_size;
899 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
900 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
901 if (!port->interrupt_out_buffer) {
902 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
903 goto probe_error;
904 }
905 usb_fill_int_urb (port->interrupt_out_urb, dev,
906 usb_sndintpipe (dev,
907 endpoint->bEndpointAddress),
908 port->interrupt_out_buffer, buffer_size,
909 serial->type->write_int_callback, port,
910 endpoint->bInterval);
911 }
912 } else if (num_interrupt_out) {
913 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
914 }
915
916 /* if this device type has an attach function, call it */
917 if (type->attach) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700918 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 dev_err(&interface->dev, "module get failed, exiting\n");
920 goto probe_error;
921 }
922 retval = type->attach (serial);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700923 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (retval < 0)
925 goto probe_error;
926 if (retval > 0) {
927 /* quietly accept this device, but don't bind to a serial port
928 * as it's about to disappear */
929 goto exit;
930 }
931 }
932
933 /* register all of the individual ports with the driver core */
934 for (i = 0; i < num_ports; ++i) {
935 port = serial->port[i];
936 port->dev.parent = &interface->dev;
937 port->dev.driver = NULL;
938 port->dev.bus = &usb_serial_bus_type;
939 port->dev.release = &port_release;
940
941 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
942 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
943 device_register (&port->dev);
944 }
945
946 usb_serial_console_init (debug, minor);
947
948exit:
949 /* success */
950 usb_set_intfdata (interface, serial);
951 return 0;
952
953probe_error:
954 for (i = 0; i < num_bulk_in; ++i) {
955 port = serial->port[i];
956 if (!port)
957 continue;
958 if (port->read_urb)
959 usb_free_urb (port->read_urb);
960 kfree(port->bulk_in_buffer);
961 }
962 for (i = 0; i < num_bulk_out; ++i) {
963 port = serial->port[i];
964 if (!port)
965 continue;
966 if (port->write_urb)
967 usb_free_urb (port->write_urb);
968 kfree(port->bulk_out_buffer);
969 }
970 for (i = 0; i < num_interrupt_in; ++i) {
971 port = serial->port[i];
972 if (!port)
973 continue;
974 if (port->interrupt_in_urb)
975 usb_free_urb (port->interrupt_in_urb);
976 kfree(port->interrupt_in_buffer);
977 }
978 for (i = 0; i < num_interrupt_out; ++i) {
979 port = serial->port[i];
980 if (!port)
981 continue;
982 if (port->interrupt_out_urb)
983 usb_free_urb (port->interrupt_out_urb);
984 kfree(port->interrupt_out_buffer);
985 }
986
987 /* return the minor range that this device had */
988 return_serial (serial);
989
990 /* free up any memory that we allocated */
991 for (i = 0; i < serial->num_port_pointers; ++i)
992 kfree(serial->port[i]);
993 kfree (serial);
994 return -EIO;
995}
996
997void usb_serial_disconnect(struct usb_interface *interface)
998{
999 int i;
1000 struct usb_serial *serial = usb_get_intfdata (interface);
1001 struct device *dev = &interface->dev;
1002 struct usb_serial_port *port;
1003
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +02001004 usb_serial_console_disconnect(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 dbg ("%s", __FUNCTION__);
1006
1007 usb_set_intfdata (interface, NULL);
1008 if (serial) {
1009 for (i = 0; i < serial->num_ports; ++i) {
1010 port = serial->port[i];
1011 if (port && port->tty)
1012 tty_hangup(port->tty);
1013 }
1014 /* let the last holder of this object
1015 * cause it to be cleaned up */
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +02001016 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 dev_info(dev, "device disconnected\n");
1019}
1020
1021static struct tty_operations serial_ops = {
1022 .open = serial_open,
1023 .close = serial_close,
1024 .write = serial_write,
1025 .write_room = serial_write_room,
1026 .ioctl = serial_ioctl,
1027 .set_termios = serial_set_termios,
1028 .throttle = serial_throttle,
1029 .unthrottle = serial_unthrottle,
1030 .break_ctl = serial_break,
1031 .chars_in_buffer = serial_chars_in_buffer,
1032 .read_proc = serial_read_proc,
1033 .tiocmget = serial_tiocmget,
1034 .tiocmset = serial_tiocmset,
1035};
1036
1037struct tty_driver *usb_serial_tty_driver;
1038
1039static int __init usb_serial_init(void)
1040{
1041 int i;
1042 int result;
1043
1044 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1045 if (!usb_serial_tty_driver)
1046 return -ENOMEM;
1047
1048 /* Initialize our global data */
1049 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1050 serial_table[i] = NULL;
1051 }
1052
1053 result = bus_register(&usb_serial_bus_type);
1054 if (result) {
1055 err("%s - registering bus driver failed", __FUNCTION__);
1056 goto exit_bus;
1057 }
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 usb_serial_tty_driver->owner = THIS_MODULE;
1060 usb_serial_tty_driver->driver_name = "usbserial";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 usb_serial_tty_driver->name = "ttyUSB";
1062 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1063 usb_serial_tty_driver->minor_start = 0;
1064 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1065 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001066 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 usb_serial_tty_driver->init_termios = tty_std_termios;
1068 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1069 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1070 result = tty_register_driver(usb_serial_tty_driver);
1071 if (result) {
1072 err("%s - tty_register_driver failed", __FUNCTION__);
1073 goto exit_reg_driver;
1074 }
1075
1076 /* register the USB driver */
1077 result = usb_register(&usb_serial_driver);
1078 if (result < 0) {
1079 err("%s - usb_register failed", __FUNCTION__);
1080 goto exit_tty;
1081 }
1082
Greg Kroah-Hartman06299db2005-05-26 05:55:55 -07001083 /* register the generic driver, if we should */
1084 result = usb_serial_generic_register(debug);
1085 if (result < 0) {
1086 err("%s - registering generic driver failed", __FUNCTION__);
1087 goto exit_generic;
1088 }
1089
Greg Kroah-Hartman17a882f2005-06-20 21:15:16 -07001090 info(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 return result;
1093
Greg Kroah-Hartman06299db2005-05-26 05:55:55 -07001094exit_generic:
1095 usb_deregister(&usb_serial_driver);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097exit_tty:
1098 tty_unregister_driver(usb_serial_tty_driver);
1099
1100exit_reg_driver:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 bus_unregister(&usb_serial_bus_type);
1102
1103exit_bus:
1104 err ("%s - returning with error %d", __FUNCTION__, result);
1105 put_tty_driver(usb_serial_tty_driver);
1106 return result;
1107}
1108
1109
1110static void __exit usb_serial_exit(void)
1111{
1112 usb_serial_console_exit();
1113
1114 usb_serial_generic_deregister();
1115
1116 usb_deregister(&usb_serial_driver);
1117 tty_unregister_driver(usb_serial_tty_driver);
1118 put_tty_driver(usb_serial_tty_driver);
1119 bus_unregister(&usb_serial_bus_type);
1120}
1121
1122
1123module_init(usb_serial_init);
1124module_exit(usb_serial_exit);
1125
1126#define set_to_generic_if_null(type, function) \
1127 do { \
1128 if (!type->function) { \
1129 type->function = usb_serial_generic_##function; \
1130 dbg("Had to override the " #function \
1131 " usb serial operation with the generic one.");\
1132 } \
1133 } while (0)
1134
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001135static void fixup_generic(struct usb_serial_driver *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 set_to_generic_if_null(device, open);
1138 set_to_generic_if_null(device, write);
1139 set_to_generic_if_null(device, close);
1140 set_to_generic_if_null(device, write_room);
1141 set_to_generic_if_null(device, chars_in_buffer);
1142 set_to_generic_if_null(device, read_bulk_callback);
1143 set_to_generic_if_null(device, write_bulk_callback);
1144 set_to_generic_if_null(device, shutdown);
1145}
1146
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001147int usb_serial_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 int retval;
1150
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001151 fixup_generic(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001153 if (!driver->description)
1154 driver->description = driver->driver.name;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* Add this device to our list of devices */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001157 list_add(&driver->driver_list, &usb_serial_driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001159 retval = usb_serial_bus_register(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (retval) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001161 err("problem %d when registering driver %s", retval, driver->description);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001162 list_del(&driver->driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 else
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001165 info("USB Serial support registered for %s", driver->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 return retval;
1168}
1169
1170
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001171void usb_serial_deregister(struct usb_serial_driver *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001173 info("USB Serial deregistering driver %s", device->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 list_del(&device->driver_list);
1175 usb_serial_bus_deregister(device);
1176}
1177
1178
1179
1180/* If the usb-serial core is built into the core, the usb-serial drivers
1181 need these symbols to load properly as modules. */
1182EXPORT_SYMBOL_GPL(usb_serial_register);
1183EXPORT_SYMBOL_GPL(usb_serial_deregister);
1184EXPORT_SYMBOL_GPL(usb_serial_probe);
1185EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1186EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1187
1188
1189/* Module information */
1190MODULE_AUTHOR( DRIVER_AUTHOR );
1191MODULE_DESCRIPTION( DRIVER_DESC );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192MODULE_LICENSE("GPL");
1193
1194module_param(debug, bool, S_IRUGO | S_IWUSR);
1195MODULE_PARM_DESC(debug, "Debug enabled or not");