blob: 92c213fa93173bc24dbbaddd6a5faba733ca51f6 [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
19#include <linux/config.h>
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>
29#include <linux/spinlock.h>
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -030030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/list.h>
32#include <linux/smp_lock.h>
33#include <asm/uaccess.h>
34#include <linux/usb.h>
35#include "usb-serial.h"
36#include "pl2303.h"
37
38/*
39 * Version Information
40 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
42#define DRIVER_DESC "USB Serial Driver core"
43
44/* Driver structure we register with the USB core */
45static struct usb_driver usb_serial_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .name = "usbserial",
47 .probe = usb_serial_probe,
48 .disconnect = usb_serial_disconnect,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080049 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52/* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
53 the MODULE_DEVICE_TABLE declarations in each serial driver
54 cause the "hotplug" program to pull in whatever module is necessary
55 via modprobe, and modprobe will load usbserial because the serial
56 drivers depend on it.
57*/
58
59static int debug;
60static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
61static LIST_HEAD(usb_serial_driver_list);
62
63struct usb_serial *usb_serial_get_by_index(unsigned index)
64{
65 struct usb_serial *serial = serial_table[index];
66
67 if (serial)
68 kref_get(&serial->kref);
69 return serial;
70}
71
72static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
73{
74 unsigned int i, j;
75 int good_spot;
76
77 dbg("%s %d", __FUNCTION__, num_ports);
78
79 *minor = 0;
80 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
81 if (serial_table[i])
82 continue;
83
84 good_spot = 1;
85 for (j = 1; j <= num_ports-1; ++j)
86 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
87 good_spot = 0;
88 i += j;
89 break;
90 }
91 if (good_spot == 0)
92 continue;
93
94 *minor = i;
95 dbg("%s - minor base = %d", __FUNCTION__, *minor);
96 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
97 serial_table[i] = serial;
98 return serial;
99 }
100 return NULL;
101}
102
103static void return_serial(struct usb_serial *serial)
104{
105 int i;
106
107 dbg("%s", __FUNCTION__);
108
109 if (serial == NULL)
110 return;
111
112 for (i = 0; i < serial->num_ports; ++i) {
113 serial_table[serial->minor + i] = NULL;
114 }
115}
116
117static void destroy_serial(struct kref *kref)
118{
119 struct usb_serial *serial;
120 struct usb_serial_port *port;
121 int i;
122
123 serial = to_usb_serial(kref);
124
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700125 dbg("%s - %s", __FUNCTION__, serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 serial->type->shutdown(serial);
128
129 /* return the minor range that this device had */
130 return_serial(serial);
131
132 for (i = 0; i < serial->num_ports; ++i)
133 serial->port[i]->open_count = 0;
134
135 /* the ports are cleaned up and released in port_release() */
136 for (i = 0; i < serial->num_ports; ++i)
137 if (serial->port[i]->dev.parent != NULL) {
138 device_unregister(&serial->port[i]->dev);
139 serial->port[i] = NULL;
140 }
141
142 /* If this is a "fake" port, we have to clean it up here, as it will
143 * not get cleaned up in port_release() as it was never registered with
144 * the driver core */
145 if (serial->num_ports < serial->num_port_pointers) {
146 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
147 port = serial->port[i];
148 if (!port)
149 continue;
150 usb_kill_urb(port->read_urb);
151 usb_free_urb(port->read_urb);
152 usb_kill_urb(port->write_urb);
153 usb_free_urb(port->write_urb);
154 usb_kill_urb(port->interrupt_in_urb);
155 usb_free_urb(port->interrupt_in_urb);
156 usb_kill_urb(port->interrupt_out_urb);
157 usb_free_urb(port->interrupt_out_urb);
158 kfree(port->bulk_in_buffer);
159 kfree(port->bulk_out_buffer);
160 kfree(port->interrupt_in_buffer);
161 kfree(port->interrupt_out_buffer);
162 }
163 }
164
Pete Zaitcev2f8ad9a2006-05-24 11:04:04 -0700165 flush_scheduled_work(); /* port->work */
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 usb_put_dev(serial->dev);
168
169 /* free up any memory that we allocated */
170 kfree (serial);
171}
172
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200173void usb_serial_put(struct usb_serial *serial)
174{
175 kref_put(&serial->kref, destroy_serial);
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*****************************************************************************
179 * Driver tty interface functions
180 *****************************************************************************/
181static int serial_open (struct tty_struct *tty, struct file * filp)
182{
183 struct usb_serial *serial;
184 struct usb_serial_port *port;
185 unsigned int portNumber;
186 int retval;
187
188 dbg("%s", __FUNCTION__);
189
190 /* get the serial object associated with this tty pointer */
191 serial = usb_serial_get_by_index(tty->index);
192 if (!serial) {
193 tty->driver_data = NULL;
194 return -ENODEV;
195 }
196
197 portNumber = tty->index - serial->minor;
198 port = serial->port[portNumber];
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300199 if (!port) {
200 retval = -ENODEV;
201 goto bailout_kref_put;
202 }
Luiz Fernando Capitulino8a4613f2005-11-28 19:16:07 -0200203
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300204 if (mutex_lock_interruptible(&port->mutex)) {
205 retval = -ERESTARTSYS;
206 goto bailout_kref_put;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 ++port->open_count;
210
Paul Fulghumca854852006-04-13 22:28:17 +0200211 /* set up our port structure making the tty driver
212 * remember our port object, and us it */
213 tty->driver_data = port;
214 port->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Paul Fulghumca854852006-04-13 22:28:17 +0200216 if (port->open_count == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
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 */
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700221 if (!try_module_get(serial->type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 retval = -ENODEV;
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300223 goto bailout_mutex_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 /* only call the device specific open if this
227 * is the first time the port is opened */
228 retval = serial->type->open(port, filp);
229 if (retval)
230 goto bailout_module_put;
231 }
232
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300233 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
235
236bailout_module_put:
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700237 module_put(serial->type->driver.owner);
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300238bailout_mutex_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 port->open_count = 0;
Frank Gevaertsb059c812006-06-14 15:52:05 +0200240 tty->driver_data = NULL;
241 port->tty = NULL;
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300242 mutex_unlock(&port->mutex);
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300243bailout_kref_put:
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200244 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return retval;
246}
247
248static void serial_close(struct tty_struct *tty, struct file * filp)
249{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200250 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (!port)
253 return;
254
255 dbg("%s - port %d", __FUNCTION__, port->number);
256
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300257 mutex_lock(&port->mutex);
Luiz Fernando Capitulino8a4613f2005-11-28 19:16:07 -0200258
Greg Kroah-Hartman91c0bce2006-03-06 13:25:52 -0800259 if (port->open_count == 0) {
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300260 mutex_unlock(&port->mutex);
Greg Kroah-Hartman91c0bce2006-03-06 13:25:52 -0800261 return;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 --port->open_count;
265 if (port->open_count == 0) {
266 /* only call the device specific close if this
267 * port is being closed by the last owner */
268 port->serial->type->close(port, filp);
269
270 if (port->tty) {
271 if (port->tty->driver_data)
272 port->tty->driver_data = NULL;
273 port->tty = NULL;
274 }
275
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700276 module_put(port->serial->type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300279 mutex_unlock(&port->mutex);
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200280 usb_serial_put(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
284{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200285 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int retval = -EINVAL;
287
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200288 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200289 goto exit;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
292
293 if (!port->open_count) {
294 dbg("%s - port not opened", __FUNCTION__);
295 goto exit;
296 }
297
298 /* pass on to the driver specific version of this function */
299 retval = port->serial->type->write(port, buf, count);
300
301exit:
302 return retval;
303}
304
305static int serial_write_room (struct tty_struct *tty)
306{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200307 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int retval = -EINVAL;
309
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200310 if (!port)
311 goto exit;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 dbg("%s - port %d", __FUNCTION__, port->number);
314
315 if (!port->open_count) {
316 dbg("%s - port not open", __FUNCTION__);
317 goto exit;
318 }
319
320 /* pass on to the driver specific version of this function */
321 retval = port->serial->type->write_room(port);
322
323exit:
324 return retval;
325}
326
327static int serial_chars_in_buffer (struct tty_struct *tty)
328{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200329 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int retval = -EINVAL;
331
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200332 if (!port)
333 goto exit;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 dbg("%s = port %d", __FUNCTION__, port->number);
336
337 if (!port->open_count) {
338 dbg("%s - port not open", __FUNCTION__);
339 goto exit;
340 }
341
342 /* pass on to the driver specific version of this function */
343 retval = port->serial->type->chars_in_buffer(port);
344
345exit:
346 return retval;
347}
348
349static void serial_throttle (struct tty_struct * tty)
350{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200351 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200353 if (!port)
354 return;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 dbg("%s - port %d", __FUNCTION__, port->number);
357
358 if (!port->open_count) {
359 dbg ("%s - port not open", __FUNCTION__);
360 return;
361 }
362
363 /* pass on to the driver specific version of this function */
364 if (port->serial->type->throttle)
365 port->serial->type->throttle(port);
366}
367
368static void serial_unthrottle (struct tty_struct * tty)
369{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200370 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200372 if (!port)
373 return;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dbg("%s - port %d", __FUNCTION__, port->number);
376
377 if (!port->open_count) {
378 dbg("%s - port not open", __FUNCTION__);
379 return;
380 }
381
382 /* pass on to the driver specific version of this function */
383 if (port->serial->type->unthrottle)
384 port->serial->type->unthrottle(port);
385}
386
387static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
388{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200389 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int retval = -ENODEV;
391
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200392 if (!port)
393 goto exit;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
396
397 if (!port->open_count) {
398 dbg ("%s - port not open", __FUNCTION__);
399 goto exit;
400 }
401
402 /* pass on to the driver specific version of this function if it is available */
403 if (port->serial->type->ioctl)
404 retval = port->serial->type->ioctl(port, file, cmd, arg);
405 else
406 retval = -ENOIOCTLCMD;
407
408exit:
409 return retval;
410}
411
412static void serial_set_termios (struct tty_struct *tty, struct termios * old)
413{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200414 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200416 if (!port)
417 return;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dbg("%s - port %d", __FUNCTION__, port->number);
420
421 if (!port->open_count) {
422 dbg("%s - port not open", __FUNCTION__);
423 return;
424 }
425
426 /* pass on to the driver specific version of this function if it is available */
427 if (port->serial->type->set_termios)
428 port->serial->type->set_termios(port, old);
429}
430
431static void serial_break (struct tty_struct *tty, int break_state)
432{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200433 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200435 if (!port)
436 return;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 dbg("%s - port %d", __FUNCTION__, port->number);
439
440 if (!port->open_count) {
441 dbg("%s - port not open", __FUNCTION__);
442 return;
443 }
444
445 /* pass on to the driver specific version of this function if it is available */
446 if (port->serial->type->break_ctl)
447 port->serial->type->break_ctl(port, break_state);
448}
449
450static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
451{
452 struct usb_serial *serial;
453 int length = 0;
454 int i;
455 off_t begin = 0;
456 char tmp[40];
457
458 dbg("%s", __FUNCTION__);
Greg Kroah-Hartman17a882f2005-06-20 21:15:16 -0700459 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
461 serial = usb_serial_get_by_index(i);
462 if (serial == NULL)
463 continue;
464
465 length += sprintf (page+length, "%d:", i);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700466 if (serial->type->driver.owner)
467 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700468 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 length += sprintf (page+length, " vendor:%04x product:%04x",
470 le16_to_cpu(serial->dev->descriptor.idVendor),
471 le16_to_cpu(serial->dev->descriptor.idProduct));
472 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
473 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
474
475 usb_make_path(serial->dev, tmp, sizeof(tmp));
476 length += sprintf (page+length, " path:%s", tmp);
477
478 length += sprintf (page+length, "\n");
479 if ((length + begin) > (off + count))
480 goto done;
481 if ((length + begin) < off) {
482 begin += length;
483 length = 0;
484 }
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200485 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 *eof = 1;
488done:
489 if (off >= (length + begin))
490 return 0;
491 *start = page + (off-begin);
492 return ((count < begin+length-off) ? count : begin+length-off);
493}
494
495static int serial_tiocmget (struct tty_struct *tty, struct file *file)
496{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200497 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200499 if (!port)
500 goto exit;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 dbg("%s - port %d", __FUNCTION__, port->number);
503
504 if (!port->open_count) {
505 dbg("%s - port not open", __FUNCTION__);
506 goto exit;
507 }
508
509 if (port->serial->type->tiocmget)
510 return port->serial->type->tiocmget(port, file);
511
512exit:
513 return -EINVAL;
514}
515
516static int serial_tiocmset (struct tty_struct *tty, struct file *file,
517 unsigned int set, unsigned int clear)
518{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200519 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200521 if (!port)
522 goto exit;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 dbg("%s - port %d", __FUNCTION__, port->number);
525
526 if (!port->open_count) {
527 dbg("%s - port not open", __FUNCTION__);
528 goto exit;
529 }
530
531 if (port->serial->type->tiocmset)
532 return port->serial->type->tiocmset(port, file, set, clear);
533
534exit:
535 return -EINVAL;
536}
537
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700538/*
539 * We would be calling tty_wakeup here, but unfortunately some line
540 * disciplines have an annoying habit of calling tty->write from
541 * the write wakeup callback (e.g. n_hdlc.c).
542 */
543void usb_serial_port_softint(struct usb_serial_port *port)
544{
545 schedule_work(&port->work);
546}
547
548static void usb_serial_port_work(void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200550 struct usb_serial_port *port = private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct tty_struct *tty;
552
553 dbg("%s - port %d", __FUNCTION__, port->number);
554
555 if (!port)
556 return;
557
558 tty = port->tty;
559 if (!tty)
560 return;
561
562 tty_wakeup(tty);
563}
564
565static void port_release(struct device *dev)
566{
567 struct usb_serial_port *port = to_usb_serial_port(dev);
568
569 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
570 usb_kill_urb(port->read_urb);
571 usb_free_urb(port->read_urb);
572 usb_kill_urb(port->write_urb);
573 usb_free_urb(port->write_urb);
574 usb_kill_urb(port->interrupt_in_urb);
575 usb_free_urb(port->interrupt_in_urb);
576 usb_kill_urb(port->interrupt_out_urb);
577 usb_free_urb(port->interrupt_out_urb);
578 kfree(port->bulk_in_buffer);
579 kfree(port->bulk_out_buffer);
580 kfree(port->interrupt_in_buffer);
581 kfree(port->interrupt_out_buffer);
582 kfree(port);
583}
584
585static struct usb_serial * create_serial (struct usb_device *dev,
586 struct usb_interface *interface,
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700587 struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct usb_serial *serial;
590
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100591 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (!serial) {
593 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
594 return NULL;
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 serial->dev = usb_get_dev(dev);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700597 serial->type = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 serial->interface = interface;
599 kref_init(&serial->kref);
600
601 return serial;
602}
603
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700604static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct list_head *p;
607 const struct usb_device_id *id;
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700608 struct usb_serial_driver *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Adrian Bunk93b1fae2006-01-10 00:13:33 +0100610 /* Check if the usb id matches a known device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 list_for_each(p, &usb_serial_driver_list) {
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700612 t = list_entry(p, struct usb_serial_driver, driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 id = usb_match_id(iface, t->id_table);
614 if (id != NULL) {
615 dbg("descriptor matches");
616 return t;
617 }
618 }
619
620 return NULL;
621}
622
623int usb_serial_probe(struct usb_interface *interface,
624 const struct usb_device_id *id)
625{
626 struct usb_device *dev = interface_to_usbdev (interface);
627 struct usb_serial *serial = NULL;
628 struct usb_serial_port *port;
629 struct usb_host_interface *iface_desc;
630 struct usb_endpoint_descriptor *endpoint;
631 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
632 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
633 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
634 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700635 struct usb_serial_driver *type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int retval;
637 int minor;
638 int buffer_size;
639 int i;
640 int num_interrupt_in = 0;
641 int num_interrupt_out = 0;
642 int num_bulk_in = 0;
643 int num_bulk_out = 0;
644 int num_ports = 0;
645 int max_endpoints;
646
647 type = search_serial_device(interface);
648 if (!type) {
649 dbg("none matched");
650 return -ENODEV;
651 }
652
653 serial = create_serial (dev, interface, type);
654 if (!serial) {
655 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
656 return -ENOMEM;
657 }
658
659 /* if this device type has a probe function, call it */
660 if (type->probe) {
661 const struct usb_device_id *id;
662
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700663 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 dev_err(&interface->dev, "module get failed, exiting\n");
665 kfree (serial);
666 return -EIO;
667 }
668
669 id = usb_match_id(interface, type->id_table);
670 retval = type->probe(serial, id);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700671 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (retval) {
674 dbg ("sub driver rejected device");
675 kfree (serial);
676 return retval;
677 }
678 }
679
680 /* descriptor matches, let's find the endpoints needed */
681 /* check out the endpoints */
682 iface_desc = interface->cur_altsetting;
683 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
684 endpoint = &iface_desc->endpoint[i].desc;
685
686 if ((endpoint->bEndpointAddress & 0x80) &&
687 ((endpoint->bmAttributes & 3) == 0x02)) {
688 /* we found a bulk in endpoint */
689 dbg("found bulk in on endpoint %d", i);
690 bulk_in_endpoint[num_bulk_in] = endpoint;
691 ++num_bulk_in;
692 }
693
694 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
695 ((endpoint->bmAttributes & 3) == 0x02)) {
696 /* we found a bulk out endpoint */
697 dbg("found bulk out on endpoint %d", i);
698 bulk_out_endpoint[num_bulk_out] = endpoint;
699 ++num_bulk_out;
700 }
701
702 if ((endpoint->bEndpointAddress & 0x80) &&
703 ((endpoint->bmAttributes & 3) == 0x03)) {
704 /* we found a interrupt in endpoint */
705 dbg("found interrupt in on endpoint %d", i);
706 interrupt_in_endpoint[num_interrupt_in] = endpoint;
707 ++num_interrupt_in;
708 }
709
710 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
711 ((endpoint->bmAttributes & 3) == 0x03)) {
712 /* we found an interrupt out endpoint */
713 dbg("found interrupt out on endpoint %d", i);
714 interrupt_out_endpoint[num_interrupt_out] = endpoint;
715 ++num_interrupt_out;
716 }
717 }
718
719#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
720 /* BEGIN HORRIBLE HACK FOR PL2303 */
721 /* this is needed due to the looney way its endpoints are set up */
722 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
723 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
724 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
725 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
726 if (interface != dev->actconfig->interface[0]) {
727 /* check out the endpoints of the other interface*/
728 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
729 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
730 endpoint = &iface_desc->endpoint[i].desc;
731 if ((endpoint->bEndpointAddress & 0x80) &&
732 ((endpoint->bmAttributes & 3) == 0x03)) {
733 /* we found a interrupt in endpoint */
734 dbg("found interrupt in for Prolific device on separate interface");
735 interrupt_in_endpoint[num_interrupt_in] = endpoint;
736 ++num_interrupt_in;
737 }
738 }
739 }
740
741 /* Now make sure the PL-2303 is configured correctly.
742 * If not, give up now and hope this hack will work
743 * properly during a later invocation of usb_serial_probe
744 */
745 if (num_bulk_in == 0 || num_bulk_out == 0) {
746 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
747 kfree (serial);
748 return -ENODEV;
749 }
750 }
751 /* END HORRIBLE HACK FOR PL2303 */
752#endif
753
754 /* found all that we need */
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700755 dev_info(&interface->dev, "%s converter detected\n", type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757#ifdef CONFIG_USB_SERIAL_GENERIC
758 if (type == &usb_serial_generic_device) {
759 num_ports = num_bulk_out;
760 if (num_ports == 0) {
761 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
762 kfree (serial);
763 return -EIO;
764 }
765 }
766#endif
767 if (!num_ports) {
768 /* if this device type has a calc_num_ports function, call it */
769 if (type->calc_num_ports) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700770 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dev_err(&interface->dev, "module get failed, exiting\n");
772 kfree (serial);
773 return -EIO;
774 }
775 num_ports = type->calc_num_ports (serial);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700776 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 if (!num_ports)
779 num_ports = type->num_ports;
780 }
781
782 if (get_free_serial (serial, num_ports, &minor) == NULL) {
783 dev_err(&interface->dev, "No more free serial devices\n");
784 kfree (serial);
785 return -ENOMEM;
786 }
787
788 serial->minor = minor;
789 serial->num_ports = num_ports;
790 serial->num_bulk_in = num_bulk_in;
791 serial->num_bulk_out = num_bulk_out;
792 serial->num_interrupt_in = num_interrupt_in;
793 serial->num_interrupt_out = num_interrupt_out;
794
795 /* create our ports, we need as many as the max endpoints */
796 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
797 max_endpoints = max(num_bulk_in, num_bulk_out);
798 max_endpoints = max(max_endpoints, num_interrupt_in);
799 max_endpoints = max(max_endpoints, num_interrupt_out);
800 max_endpoints = max(max_endpoints, (int)serial->num_ports);
801 serial->num_port_pointers = max_endpoints;
802 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
803 for (i = 0; i < max_endpoints; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100804 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!port)
806 goto probe_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 port->number = i + serial->minor;
808 port->serial = serial;
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700809 spin_lock_init(&port->lock);
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300810 mutex_init(&port->mutex);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700811 INIT_WORK(&port->work, usb_serial_port_work, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 serial->port[i] = port;
813 }
814
815 /* set up the endpoint information */
816 for (i = 0; i < num_bulk_in; ++i) {
817 endpoint = bulk_in_endpoint[i];
818 port = serial->port[i];
819 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
820 if (!port->read_urb) {
821 dev_err(&interface->dev, "No free urbs available\n");
822 goto probe_error;
823 }
824 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
825 port->bulk_in_size = buffer_size;
826 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
827 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
828 if (!port->bulk_in_buffer) {
829 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
830 goto probe_error;
831 }
832 usb_fill_bulk_urb (port->read_urb, dev,
833 usb_rcvbulkpipe (dev,
834 endpoint->bEndpointAddress),
835 port->bulk_in_buffer, buffer_size,
836 serial->type->read_bulk_callback,
837 port);
838 }
839
840 for (i = 0; i < num_bulk_out; ++i) {
841 endpoint = bulk_out_endpoint[i];
842 port = serial->port[i];
843 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
844 if (!port->write_urb) {
845 dev_err(&interface->dev, "No free urbs available\n");
846 goto probe_error;
847 }
848 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
849 port->bulk_out_size = buffer_size;
850 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
851 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
852 if (!port->bulk_out_buffer) {
853 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
854 goto probe_error;
855 }
856 usb_fill_bulk_urb (port->write_urb, dev,
857 usb_sndbulkpipe (dev,
858 endpoint->bEndpointAddress),
859 port->bulk_out_buffer, buffer_size,
860 serial->type->write_bulk_callback,
861 port);
862 }
863
864 if (serial->type->read_int_callback) {
865 for (i = 0; i < num_interrupt_in; ++i) {
866 endpoint = interrupt_in_endpoint[i];
867 port = serial->port[i];
868 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
869 if (!port->interrupt_in_urb) {
870 dev_err(&interface->dev, "No free urbs available\n");
871 goto probe_error;
872 }
873 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
874 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
875 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
876 if (!port->interrupt_in_buffer) {
877 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
878 goto probe_error;
879 }
880 usb_fill_int_urb (port->interrupt_in_urb, dev,
881 usb_rcvintpipe (dev,
882 endpoint->bEndpointAddress),
883 port->interrupt_in_buffer, buffer_size,
884 serial->type->read_int_callback, port,
885 endpoint->bInterval);
886 }
887 } else if (num_interrupt_in) {
888 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
889 }
890
891 if (serial->type->write_int_callback) {
892 for (i = 0; i < num_interrupt_out; ++i) {
893 endpoint = interrupt_out_endpoint[i];
894 port = serial->port[i];
895 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
896 if (!port->interrupt_out_urb) {
897 dev_err(&interface->dev, "No free urbs available\n");
898 goto probe_error;
899 }
900 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
901 port->interrupt_out_size = buffer_size;
902 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
903 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
904 if (!port->interrupt_out_buffer) {
905 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
906 goto probe_error;
907 }
908 usb_fill_int_urb (port->interrupt_out_urb, dev,
909 usb_sndintpipe (dev,
910 endpoint->bEndpointAddress),
911 port->interrupt_out_buffer, buffer_size,
912 serial->type->write_int_callback, port,
913 endpoint->bInterval);
914 }
915 } else if (num_interrupt_out) {
916 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
917 }
918
919 /* if this device type has an attach function, call it */
920 if (type->attach) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700921 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 dev_err(&interface->dev, "module get failed, exiting\n");
923 goto probe_error;
924 }
925 retval = type->attach (serial);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700926 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (retval < 0)
928 goto probe_error;
929 if (retval > 0) {
930 /* quietly accept this device, but don't bind to a serial port
931 * as it's about to disappear */
932 goto exit;
933 }
934 }
935
936 /* register all of the individual ports with the driver core */
937 for (i = 0; i < num_ports; ++i) {
938 port = serial->port[i];
939 port->dev.parent = &interface->dev;
940 port->dev.driver = NULL;
941 port->dev.bus = &usb_serial_bus_type;
942 port->dev.release = &port_release;
943
944 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
945 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
946 device_register (&port->dev);
947 }
948
949 usb_serial_console_init (debug, minor);
950
951exit:
952 /* success */
953 usb_set_intfdata (interface, serial);
954 return 0;
955
956probe_error:
957 for (i = 0; i < num_bulk_in; ++i) {
958 port = serial->port[i];
959 if (!port)
960 continue;
961 if (port->read_urb)
962 usb_free_urb (port->read_urb);
963 kfree(port->bulk_in_buffer);
964 }
965 for (i = 0; i < num_bulk_out; ++i) {
966 port = serial->port[i];
967 if (!port)
968 continue;
969 if (port->write_urb)
970 usb_free_urb (port->write_urb);
971 kfree(port->bulk_out_buffer);
972 }
973 for (i = 0; i < num_interrupt_in; ++i) {
974 port = serial->port[i];
975 if (!port)
976 continue;
977 if (port->interrupt_in_urb)
978 usb_free_urb (port->interrupt_in_urb);
979 kfree(port->interrupt_in_buffer);
980 }
981 for (i = 0; i < num_interrupt_out; ++i) {
982 port = serial->port[i];
983 if (!port)
984 continue;
985 if (port->interrupt_out_urb)
986 usb_free_urb (port->interrupt_out_urb);
987 kfree(port->interrupt_out_buffer);
988 }
989
990 /* return the minor range that this device had */
991 return_serial (serial);
992
993 /* free up any memory that we allocated */
994 for (i = 0; i < serial->num_port_pointers; ++i)
995 kfree(serial->port[i]);
996 kfree (serial);
997 return -EIO;
998}
999
1000void usb_serial_disconnect(struct usb_interface *interface)
1001{
1002 int i;
1003 struct usb_serial *serial = usb_get_intfdata (interface);
1004 struct device *dev = &interface->dev;
1005 struct usb_serial_port *port;
1006
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +02001007 usb_serial_console_disconnect(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 dbg ("%s", __FUNCTION__);
1009
1010 usb_set_intfdata (interface, NULL);
1011 if (serial) {
1012 for (i = 0; i < serial->num_ports; ++i) {
1013 port = serial->port[i];
1014 if (port && port->tty)
1015 tty_hangup(port->tty);
1016 }
1017 /* let the last holder of this object
1018 * cause it to be cleaned up */
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +02001019 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 dev_info(dev, "device disconnected\n");
1022}
1023
1024static struct tty_operations serial_ops = {
1025 .open = serial_open,
1026 .close = serial_close,
1027 .write = serial_write,
1028 .write_room = serial_write_room,
1029 .ioctl = serial_ioctl,
1030 .set_termios = serial_set_termios,
1031 .throttle = serial_throttle,
1032 .unthrottle = serial_unthrottle,
1033 .break_ctl = serial_break,
1034 .chars_in_buffer = serial_chars_in_buffer,
1035 .read_proc = serial_read_proc,
1036 .tiocmget = serial_tiocmget,
1037 .tiocmset = serial_tiocmset,
1038};
1039
1040struct tty_driver *usb_serial_tty_driver;
1041
1042static int __init usb_serial_init(void)
1043{
1044 int i;
1045 int result;
1046
1047 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1048 if (!usb_serial_tty_driver)
1049 return -ENOMEM;
1050
1051 /* Initialize our global data */
1052 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1053 serial_table[i] = NULL;
1054 }
1055
1056 result = bus_register(&usb_serial_bus_type);
1057 if (result) {
1058 err("%s - registering bus driver failed", __FUNCTION__);
1059 goto exit_bus;
1060 }
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 usb_serial_tty_driver->owner = THIS_MODULE;
1063 usb_serial_tty_driver->driver_name = "usbserial";
1064 usb_serial_tty_driver->devfs_name = "usb/tts/";
1065 usb_serial_tty_driver->name = "ttyUSB";
1066 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1067 usb_serial_tty_driver->minor_start = 0;
1068 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1069 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1070 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1071 usb_serial_tty_driver->init_termios = tty_std_termios;
1072 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1073 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1074 result = tty_register_driver(usb_serial_tty_driver);
1075 if (result) {
1076 err("%s - tty_register_driver failed", __FUNCTION__);
1077 goto exit_reg_driver;
1078 }
1079
1080 /* register the USB driver */
1081 result = usb_register(&usb_serial_driver);
1082 if (result < 0) {
1083 err("%s - usb_register failed", __FUNCTION__);
1084 goto exit_tty;
1085 }
1086
Greg Kroah-Hartman06299db2005-05-26 05:55:55 -07001087 /* register the generic driver, if we should */
1088 result = usb_serial_generic_register(debug);
1089 if (result < 0) {
1090 err("%s - registering generic driver failed", __FUNCTION__);
1091 goto exit_generic;
1092 }
1093
Greg Kroah-Hartman17a882f2005-06-20 21:15:16 -07001094 info(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 return result;
1097
Greg Kroah-Hartman06299db2005-05-26 05:55:55 -07001098exit_generic:
1099 usb_deregister(&usb_serial_driver);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101exit_tty:
1102 tty_unregister_driver(usb_serial_tty_driver);
1103
1104exit_reg_driver:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 bus_unregister(&usb_serial_bus_type);
1106
1107exit_bus:
1108 err ("%s - returning with error %d", __FUNCTION__, result);
1109 put_tty_driver(usb_serial_tty_driver);
1110 return result;
1111}
1112
1113
1114static void __exit usb_serial_exit(void)
1115{
1116 usb_serial_console_exit();
1117
1118 usb_serial_generic_deregister();
1119
1120 usb_deregister(&usb_serial_driver);
1121 tty_unregister_driver(usb_serial_tty_driver);
1122 put_tty_driver(usb_serial_tty_driver);
1123 bus_unregister(&usb_serial_bus_type);
1124}
1125
1126
1127module_init(usb_serial_init);
1128module_exit(usb_serial_exit);
1129
1130#define set_to_generic_if_null(type, function) \
1131 do { \
1132 if (!type->function) { \
1133 type->function = usb_serial_generic_##function; \
1134 dbg("Had to override the " #function \
1135 " usb serial operation with the generic one.");\
1136 } \
1137 } while (0)
1138
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001139static void fixup_generic(struct usb_serial_driver *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 set_to_generic_if_null(device, open);
1142 set_to_generic_if_null(device, write);
1143 set_to_generic_if_null(device, close);
1144 set_to_generic_if_null(device, write_room);
1145 set_to_generic_if_null(device, chars_in_buffer);
1146 set_to_generic_if_null(device, read_bulk_callback);
1147 set_to_generic_if_null(device, write_bulk_callback);
1148 set_to_generic_if_null(device, shutdown);
1149}
1150
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001151int usb_serial_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 int retval;
1154
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001155 fixup_generic(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001157 if (!driver->description)
1158 driver->description = driver->driver.name;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /* Add this device to our list of devices */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001161 list_add(&driver->driver_list, &usb_serial_driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001163 retval = usb_serial_bus_register(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (retval) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001165 err("problem %d when registering driver %s", retval, driver->description);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001166 list_del(&driver->driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 else
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001169 info("USB Serial support registered for %s", driver->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 return retval;
1172}
1173
1174
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001175void usb_serial_deregister(struct usb_serial_driver *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001177 info("USB Serial deregistering driver %s", device->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 list_del(&device->driver_list);
1179 usb_serial_bus_deregister(device);
1180}
1181
1182
1183
1184/* If the usb-serial core is built into the core, the usb-serial drivers
1185 need these symbols to load properly as modules. */
1186EXPORT_SYMBOL_GPL(usb_serial_register);
1187EXPORT_SYMBOL_GPL(usb_serial_deregister);
1188EXPORT_SYMBOL_GPL(usb_serial_probe);
1189EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1190EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1191
1192
1193/* Module information */
1194MODULE_AUTHOR( DRIVER_AUTHOR );
1195MODULE_DESCRIPTION( DRIVER_DESC );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196MODULE_LICENSE("GPL");
1197
1198module_param(debug, bool, S_IRUGO | S_IWUSR);
1199MODULE_PARM_DESC(debug, "Debug enabled or not");