blob: 7a1cc1b04979c3ebbb38bdeab1d6fd4c3e696cbf [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
165 usb_put_dev(serial->dev);
166
167 /* free up any memory that we allocated */
168 kfree (serial);
169}
170
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200171void usb_serial_put(struct usb_serial *serial)
172{
173 kref_put(&serial->kref, destroy_serial);
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*****************************************************************************
177 * Driver tty interface functions
178 *****************************************************************************/
179static int serial_open (struct tty_struct *tty, struct file * filp)
180{
181 struct usb_serial *serial;
182 struct usb_serial_port *port;
183 unsigned int portNumber;
184 int retval;
185
186 dbg("%s", __FUNCTION__);
187
188 /* get the serial object associated with this tty pointer */
189 serial = usb_serial_get_by_index(tty->index);
190 if (!serial) {
191 tty->driver_data = NULL;
192 return -ENODEV;
193 }
194
195 portNumber = tty->index - serial->minor;
196 port = serial->port[portNumber];
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300197 if (!port) {
198 retval = -ENODEV;
199 goto bailout_kref_put;
200 }
Luiz Fernando Capitulino8a4613f2005-11-28 19:16:07 -0200201
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300202 if (mutex_lock_interruptible(&port->mutex)) {
203 retval = -ERESTARTSYS;
204 goto bailout_kref_put;
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 ++port->open_count;
208
Paul Fulghumca854852006-04-13 22:28:17 +0200209 /* set up our port structure making the tty driver
210 * remember our port object, and us it */
211 tty->driver_data = port;
212 port->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Paul Fulghumca854852006-04-13 22:28:17 +0200214 if (port->open_count == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* lock this module before we call it
217 * this may fail, which means we must bail out,
218 * safe because we are called with BKL held */
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700219 if (!try_module_get(serial->type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 retval = -ENODEV;
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300221 goto bailout_mutex_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
224 /* only call the device specific open if this
225 * is the first time the port is opened */
226 retval = serial->type->open(port, filp);
227 if (retval)
228 goto bailout_module_put;
229 }
230
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300231 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233
234bailout_module_put:
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700235 module_put(serial->type->driver.owner);
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300236bailout_mutex_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 port->open_count = 0;
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300238 mutex_unlock(&port->mutex);
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300239bailout_kref_put:
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200240 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return retval;
242}
243
244static void serial_close(struct tty_struct *tty, struct file * filp)
245{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200246 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (!port)
249 return;
250
251 dbg("%s - port %d", __FUNCTION__, port->number);
252
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300253 mutex_lock(&port->mutex);
Luiz Fernando Capitulino8a4613f2005-11-28 19:16:07 -0200254
Greg Kroah-Hartman91c0bce2006-03-06 13:25:52 -0800255 if (port->open_count == 0) {
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300256 mutex_unlock(&port->mutex);
Greg Kroah-Hartman91c0bce2006-03-06 13:25:52 -0800257 return;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 --port->open_count;
261 if (port->open_count == 0) {
262 /* only call the device specific close if this
263 * port is being closed by the last owner */
264 port->serial->type->close(port, filp);
265
266 if (port->tty) {
267 if (port->tty->driver_data)
268 port->tty->driver_data = NULL;
269 port->tty = NULL;
270 }
271
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700272 module_put(port->serial->type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300275 mutex_unlock(&port->mutex);
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200276 usb_serial_put(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
280{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200281 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int retval = -EINVAL;
283
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200284 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200285 goto exit;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
288
289 if (!port->open_count) {
290 dbg("%s - port not opened", __FUNCTION__);
291 goto exit;
292 }
293
294 /* pass on to the driver specific version of this function */
295 retval = port->serial->type->write(port, buf, count);
296
297exit:
298 return retval;
299}
300
301static int serial_write_room (struct tty_struct *tty)
302{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200303 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int retval = -EINVAL;
305
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200306 if (!port)
307 goto exit;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 dbg("%s - port %d", __FUNCTION__, port->number);
310
311 if (!port->open_count) {
312 dbg("%s - port not open", __FUNCTION__);
313 goto exit;
314 }
315
316 /* pass on to the driver specific version of this function */
317 retval = port->serial->type->write_room(port);
318
319exit:
320 return retval;
321}
322
323static int serial_chars_in_buffer (struct tty_struct *tty)
324{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200325 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int retval = -EINVAL;
327
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200328 if (!port)
329 goto exit;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 dbg("%s = port %d", __FUNCTION__, port->number);
332
333 if (!port->open_count) {
334 dbg("%s - port not open", __FUNCTION__);
335 goto exit;
336 }
337
338 /* pass on to the driver specific version of this function */
339 retval = port->serial->type->chars_in_buffer(port);
340
341exit:
342 return retval;
343}
344
345static void serial_throttle (struct tty_struct * tty)
346{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200347 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200349 if (!port)
350 return;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 dbg("%s - port %d", __FUNCTION__, port->number);
353
354 if (!port->open_count) {
355 dbg ("%s - port not open", __FUNCTION__);
356 return;
357 }
358
359 /* pass on to the driver specific version of this function */
360 if (port->serial->type->throttle)
361 port->serial->type->throttle(port);
362}
363
364static void serial_unthrottle (struct tty_struct * tty)
365{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200366 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200368 if (!port)
369 return;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 dbg("%s - port %d", __FUNCTION__, port->number);
372
373 if (!port->open_count) {
374 dbg("%s - port not open", __FUNCTION__);
375 return;
376 }
377
378 /* pass on to the driver specific version of this function */
379 if (port->serial->type->unthrottle)
380 port->serial->type->unthrottle(port);
381}
382
383static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
384{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200385 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int retval = -ENODEV;
387
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200388 if (!port)
389 goto exit;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
392
393 if (!port->open_count) {
394 dbg ("%s - port not open", __FUNCTION__);
395 goto exit;
396 }
397
398 /* pass on to the driver specific version of this function if it is available */
399 if (port->serial->type->ioctl)
400 retval = port->serial->type->ioctl(port, file, cmd, arg);
401 else
402 retval = -ENOIOCTLCMD;
403
404exit:
405 return retval;
406}
407
408static void serial_set_termios (struct tty_struct *tty, struct termios * old)
409{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200410 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200412 if (!port)
413 return;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 dbg("%s - port %d", __FUNCTION__, port->number);
416
417 if (!port->open_count) {
418 dbg("%s - port not open", __FUNCTION__);
419 return;
420 }
421
422 /* pass on to the driver specific version of this function if it is available */
423 if (port->serial->type->set_termios)
424 port->serial->type->set_termios(port, old);
425}
426
427static void serial_break (struct tty_struct *tty, int break_state)
428{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200429 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200431 if (!port)
432 return;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dbg("%s - port %d", __FUNCTION__, port->number);
435
436 if (!port->open_count) {
437 dbg("%s - port not open", __FUNCTION__);
438 return;
439 }
440
441 /* pass on to the driver specific version of this function if it is available */
442 if (port->serial->type->break_ctl)
443 port->serial->type->break_ctl(port, break_state);
444}
445
446static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
447{
448 struct usb_serial *serial;
449 int length = 0;
450 int i;
451 off_t begin = 0;
452 char tmp[40];
453
454 dbg("%s", __FUNCTION__);
Greg Kroah-Hartman17a882f2005-06-20 21:15:16 -0700455 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
457 serial = usb_serial_get_by_index(i);
458 if (serial == NULL)
459 continue;
460
461 length += sprintf (page+length, "%d:", i);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700462 if (serial->type->driver.owner)
463 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700464 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 length += sprintf (page+length, " vendor:%04x product:%04x",
466 le16_to_cpu(serial->dev->descriptor.idVendor),
467 le16_to_cpu(serial->dev->descriptor.idProduct));
468 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
469 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
470
471 usb_make_path(serial->dev, tmp, sizeof(tmp));
472 length += sprintf (page+length, " path:%s", tmp);
473
474 length += sprintf (page+length, "\n");
475 if ((length + begin) > (off + count))
476 goto done;
477 if ((length + begin) < off) {
478 begin += length;
479 length = 0;
480 }
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200481 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 *eof = 1;
484done:
485 if (off >= (length + begin))
486 return 0;
487 *start = page + (off-begin);
488 return ((count < begin+length-off) ? count : begin+length-off);
489}
490
491static int serial_tiocmget (struct tty_struct *tty, struct file *file)
492{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200493 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200495 if (!port)
496 goto exit;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dbg("%s - port %d", __FUNCTION__, port->number);
499
500 if (!port->open_count) {
501 dbg("%s - port not open", __FUNCTION__);
502 goto exit;
503 }
504
505 if (port->serial->type->tiocmget)
506 return port->serial->type->tiocmget(port, file);
507
508exit:
509 return -EINVAL;
510}
511
512static int serial_tiocmset (struct tty_struct *tty, struct file *file,
513 unsigned int set, unsigned int clear)
514{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200515 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200517 if (!port)
518 goto exit;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dbg("%s - port %d", __FUNCTION__, port->number);
521
522 if (!port->open_count) {
523 dbg("%s - port not open", __FUNCTION__);
524 goto exit;
525 }
526
527 if (port->serial->type->tiocmset)
528 return port->serial->type->tiocmset(port, file, set, clear);
529
530exit:
531 return -EINVAL;
532}
533
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700534/*
535 * We would be calling tty_wakeup here, but unfortunately some line
536 * disciplines have an annoying habit of calling tty->write from
537 * the write wakeup callback (e.g. n_hdlc.c).
538 */
539void usb_serial_port_softint(struct usb_serial_port *port)
540{
541 schedule_work(&port->work);
542}
543
544static void usb_serial_port_work(void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200546 struct usb_serial_port *port = private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct tty_struct *tty;
548
549 dbg("%s - port %d", __FUNCTION__, port->number);
550
551 if (!port)
552 return;
553
554 tty = port->tty;
555 if (!tty)
556 return;
557
558 tty_wakeup(tty);
559}
560
561static void port_release(struct device *dev)
562{
563 struct usb_serial_port *port = to_usb_serial_port(dev);
564
565 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
566 usb_kill_urb(port->read_urb);
567 usb_free_urb(port->read_urb);
568 usb_kill_urb(port->write_urb);
569 usb_free_urb(port->write_urb);
570 usb_kill_urb(port->interrupt_in_urb);
571 usb_free_urb(port->interrupt_in_urb);
572 usb_kill_urb(port->interrupt_out_urb);
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
581static struct usb_serial * create_serial (struct usb_device *dev,
582 struct usb_interface *interface,
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700583 struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct usb_serial *serial;
586
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100587 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (!serial) {
589 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
590 return NULL;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 serial->dev = usb_get_dev(dev);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700593 serial->type = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 serial->interface = interface;
595 kref_init(&serial->kref);
596
597 return serial;
598}
599
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700600static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct list_head *p;
603 const struct usb_device_id *id;
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700604 struct usb_serial_driver *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Adrian Bunk93b1fae2006-01-10 00:13:33 +0100606 /* Check if the usb id matches a known device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 list_for_each(p, &usb_serial_driver_list) {
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700608 t = list_entry(p, struct usb_serial_driver, driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 id = usb_match_id(iface, t->id_table);
610 if (id != NULL) {
611 dbg("descriptor matches");
612 return t;
613 }
614 }
615
616 return NULL;
617}
618
619int usb_serial_probe(struct usb_interface *interface,
620 const struct usb_device_id *id)
621{
622 struct usb_device *dev = interface_to_usbdev (interface);
623 struct usb_serial *serial = NULL;
624 struct usb_serial_port *port;
625 struct usb_host_interface *iface_desc;
626 struct usb_endpoint_descriptor *endpoint;
627 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
628 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
629 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
630 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700631 struct usb_serial_driver *type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int retval;
633 int minor;
634 int buffer_size;
635 int i;
636 int num_interrupt_in = 0;
637 int num_interrupt_out = 0;
638 int num_bulk_in = 0;
639 int num_bulk_out = 0;
640 int num_ports = 0;
641 int max_endpoints;
642
643 type = search_serial_device(interface);
644 if (!type) {
645 dbg("none matched");
646 return -ENODEV;
647 }
648
649 serial = create_serial (dev, interface, type);
650 if (!serial) {
651 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
652 return -ENOMEM;
653 }
654
655 /* if this device type has a probe function, call it */
656 if (type->probe) {
657 const struct usb_device_id *id;
658
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700659 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 dev_err(&interface->dev, "module get failed, exiting\n");
661 kfree (serial);
662 return -EIO;
663 }
664
665 id = usb_match_id(interface, type->id_table);
666 retval = type->probe(serial, id);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700667 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (retval) {
670 dbg ("sub driver rejected device");
671 kfree (serial);
672 return retval;
673 }
674 }
675
676 /* descriptor matches, let's find the endpoints needed */
677 /* check out the endpoints */
678 iface_desc = interface->cur_altsetting;
679 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
680 endpoint = &iface_desc->endpoint[i].desc;
681
682 if ((endpoint->bEndpointAddress & 0x80) &&
683 ((endpoint->bmAttributes & 3) == 0x02)) {
684 /* we found a bulk in endpoint */
685 dbg("found bulk in on endpoint %d", i);
686 bulk_in_endpoint[num_bulk_in] = endpoint;
687 ++num_bulk_in;
688 }
689
690 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
691 ((endpoint->bmAttributes & 3) == 0x02)) {
692 /* we found a bulk out endpoint */
693 dbg("found bulk out on endpoint %d", i);
694 bulk_out_endpoint[num_bulk_out] = endpoint;
695 ++num_bulk_out;
696 }
697
698 if ((endpoint->bEndpointAddress & 0x80) &&
699 ((endpoint->bmAttributes & 3) == 0x03)) {
700 /* we found a interrupt in endpoint */
701 dbg("found interrupt in on endpoint %d", i);
702 interrupt_in_endpoint[num_interrupt_in] = endpoint;
703 ++num_interrupt_in;
704 }
705
706 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
707 ((endpoint->bmAttributes & 3) == 0x03)) {
708 /* we found an interrupt out endpoint */
709 dbg("found interrupt out on endpoint %d", i);
710 interrupt_out_endpoint[num_interrupt_out] = endpoint;
711 ++num_interrupt_out;
712 }
713 }
714
715#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
716 /* BEGIN HORRIBLE HACK FOR PL2303 */
717 /* this is needed due to the looney way its endpoints are set up */
718 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
719 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
720 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
721 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
722 if (interface != dev->actconfig->interface[0]) {
723 /* check out the endpoints of the other interface*/
724 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
725 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
726 endpoint = &iface_desc->endpoint[i].desc;
727 if ((endpoint->bEndpointAddress & 0x80) &&
728 ((endpoint->bmAttributes & 3) == 0x03)) {
729 /* we found a interrupt in endpoint */
730 dbg("found interrupt in for Prolific device on separate interface");
731 interrupt_in_endpoint[num_interrupt_in] = endpoint;
732 ++num_interrupt_in;
733 }
734 }
735 }
736
737 /* Now make sure the PL-2303 is configured correctly.
738 * If not, give up now and hope this hack will work
739 * properly during a later invocation of usb_serial_probe
740 */
741 if (num_bulk_in == 0 || num_bulk_out == 0) {
742 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
743 kfree (serial);
744 return -ENODEV;
745 }
746 }
747 /* END HORRIBLE HACK FOR PL2303 */
748#endif
749
750 /* found all that we need */
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700751 dev_info(&interface->dev, "%s converter detected\n", type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753#ifdef CONFIG_USB_SERIAL_GENERIC
754 if (type == &usb_serial_generic_device) {
755 num_ports = num_bulk_out;
756 if (num_ports == 0) {
757 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
758 kfree (serial);
759 return -EIO;
760 }
761 }
762#endif
763 if (!num_ports) {
764 /* if this device type has a calc_num_ports function, call it */
765 if (type->calc_num_ports) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700766 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 dev_err(&interface->dev, "module get failed, exiting\n");
768 kfree (serial);
769 return -EIO;
770 }
771 num_ports = type->calc_num_ports (serial);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700772 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 if (!num_ports)
775 num_ports = type->num_ports;
776 }
777
778 if (get_free_serial (serial, num_ports, &minor) == NULL) {
779 dev_err(&interface->dev, "No more free serial devices\n");
780 kfree (serial);
781 return -ENOMEM;
782 }
783
784 serial->minor = minor;
785 serial->num_ports = num_ports;
786 serial->num_bulk_in = num_bulk_in;
787 serial->num_bulk_out = num_bulk_out;
788 serial->num_interrupt_in = num_interrupt_in;
789 serial->num_interrupt_out = num_interrupt_out;
790
791 /* create our ports, we need as many as the max endpoints */
792 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
793 max_endpoints = max(num_bulk_in, num_bulk_out);
794 max_endpoints = max(max_endpoints, num_interrupt_in);
795 max_endpoints = max(max_endpoints, num_interrupt_out);
796 max_endpoints = max(max_endpoints, (int)serial->num_ports);
797 serial->num_port_pointers = max_endpoints;
798 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
799 for (i = 0; i < max_endpoints; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100800 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!port)
802 goto probe_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 port->number = i + serial->minor;
804 port->serial = serial;
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700805 spin_lock_init(&port->lock);
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300806 mutex_init(&port->mutex);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700807 INIT_WORK(&port->work, usb_serial_port_work, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 serial->port[i] = port;
809 }
810
811 /* set up the endpoint information */
812 for (i = 0; i < num_bulk_in; ++i) {
813 endpoint = bulk_in_endpoint[i];
814 port = serial->port[i];
815 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
816 if (!port->read_urb) {
817 dev_err(&interface->dev, "No free urbs available\n");
818 goto probe_error;
819 }
820 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
821 port->bulk_in_size = buffer_size;
822 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
823 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
824 if (!port->bulk_in_buffer) {
825 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
826 goto probe_error;
827 }
828 usb_fill_bulk_urb (port->read_urb, dev,
829 usb_rcvbulkpipe (dev,
830 endpoint->bEndpointAddress),
831 port->bulk_in_buffer, buffer_size,
832 serial->type->read_bulk_callback,
833 port);
834 }
835
836 for (i = 0; i < num_bulk_out; ++i) {
837 endpoint = bulk_out_endpoint[i];
838 port = serial->port[i];
839 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
840 if (!port->write_urb) {
841 dev_err(&interface->dev, "No free urbs available\n");
842 goto probe_error;
843 }
844 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
845 port->bulk_out_size = buffer_size;
846 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
847 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
848 if (!port->bulk_out_buffer) {
849 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
850 goto probe_error;
851 }
852 usb_fill_bulk_urb (port->write_urb, dev,
853 usb_sndbulkpipe (dev,
854 endpoint->bEndpointAddress),
855 port->bulk_out_buffer, buffer_size,
856 serial->type->write_bulk_callback,
857 port);
858 }
859
860 if (serial->type->read_int_callback) {
861 for (i = 0; i < num_interrupt_in; ++i) {
862 endpoint = interrupt_in_endpoint[i];
863 port = serial->port[i];
864 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
865 if (!port->interrupt_in_urb) {
866 dev_err(&interface->dev, "No free urbs available\n");
867 goto probe_error;
868 }
869 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
870 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
871 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
872 if (!port->interrupt_in_buffer) {
873 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
874 goto probe_error;
875 }
876 usb_fill_int_urb (port->interrupt_in_urb, dev,
877 usb_rcvintpipe (dev,
878 endpoint->bEndpointAddress),
879 port->interrupt_in_buffer, buffer_size,
880 serial->type->read_int_callback, port,
881 endpoint->bInterval);
882 }
883 } else if (num_interrupt_in) {
884 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
885 }
886
887 if (serial->type->write_int_callback) {
888 for (i = 0; i < num_interrupt_out; ++i) {
889 endpoint = interrupt_out_endpoint[i];
890 port = serial->port[i];
891 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
892 if (!port->interrupt_out_urb) {
893 dev_err(&interface->dev, "No free urbs available\n");
894 goto probe_error;
895 }
896 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
897 port->interrupt_out_size = buffer_size;
898 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
899 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
900 if (!port->interrupt_out_buffer) {
901 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
902 goto probe_error;
903 }
904 usb_fill_int_urb (port->interrupt_out_urb, dev,
905 usb_sndintpipe (dev,
906 endpoint->bEndpointAddress),
907 port->interrupt_out_buffer, buffer_size,
908 serial->type->write_int_callback, port,
909 endpoint->bInterval);
910 }
911 } else if (num_interrupt_out) {
912 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
913 }
914
915 /* if this device type has an attach function, call it */
916 if (type->attach) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700917 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dev_err(&interface->dev, "module get failed, exiting\n");
919 goto probe_error;
920 }
921 retval = type->attach (serial);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700922 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (retval < 0)
924 goto probe_error;
925 if (retval > 0) {
926 /* quietly accept this device, but don't bind to a serial port
927 * as it's about to disappear */
928 goto exit;
929 }
930 }
931
932 /* register all of the individual ports with the driver core */
933 for (i = 0; i < num_ports; ++i) {
934 port = serial->port[i];
935 port->dev.parent = &interface->dev;
936 port->dev.driver = NULL;
937 port->dev.bus = &usb_serial_bus_type;
938 port->dev.release = &port_release;
939
940 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
941 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
942 device_register (&port->dev);
943 }
944
945 usb_serial_console_init (debug, minor);
946
947exit:
948 /* success */
949 usb_set_intfdata (interface, serial);
950 return 0;
951
952probe_error:
953 for (i = 0; i < num_bulk_in; ++i) {
954 port = serial->port[i];
955 if (!port)
956 continue;
957 if (port->read_urb)
958 usb_free_urb (port->read_urb);
959 kfree(port->bulk_in_buffer);
960 }
961 for (i = 0; i < num_bulk_out; ++i) {
962 port = serial->port[i];
963 if (!port)
964 continue;
965 if (port->write_urb)
966 usb_free_urb (port->write_urb);
967 kfree(port->bulk_out_buffer);
968 }
969 for (i = 0; i < num_interrupt_in; ++i) {
970 port = serial->port[i];
971 if (!port)
972 continue;
973 if (port->interrupt_in_urb)
974 usb_free_urb (port->interrupt_in_urb);
975 kfree(port->interrupt_in_buffer);
976 }
977 for (i = 0; i < num_interrupt_out; ++i) {
978 port = serial->port[i];
979 if (!port)
980 continue;
981 if (port->interrupt_out_urb)
982 usb_free_urb (port->interrupt_out_urb);
983 kfree(port->interrupt_out_buffer);
984 }
985
986 /* return the minor range that this device had */
987 return_serial (serial);
988
989 /* free up any memory that we allocated */
990 for (i = 0; i < serial->num_port_pointers; ++i)
991 kfree(serial->port[i]);
992 kfree (serial);
993 return -EIO;
994}
995
996void usb_serial_disconnect(struct usb_interface *interface)
997{
998 int i;
999 struct usb_serial *serial = usb_get_intfdata (interface);
1000 struct device *dev = &interface->dev;
1001 struct usb_serial_port *port;
1002
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +02001003 usb_serial_console_disconnect(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 dbg ("%s", __FUNCTION__);
1005
1006 usb_set_intfdata (interface, NULL);
1007 if (serial) {
1008 for (i = 0; i < serial->num_ports; ++i) {
1009 port = serial->port[i];
1010 if (port && port->tty)
1011 tty_hangup(port->tty);
1012 }
1013 /* let the last holder of this object
1014 * cause it to be cleaned up */
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +02001015 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 dev_info(dev, "device disconnected\n");
1018}
1019
1020static struct tty_operations serial_ops = {
1021 .open = serial_open,
1022 .close = serial_close,
1023 .write = serial_write,
1024 .write_room = serial_write_room,
1025 .ioctl = serial_ioctl,
1026 .set_termios = serial_set_termios,
1027 .throttle = serial_throttle,
1028 .unthrottle = serial_unthrottle,
1029 .break_ctl = serial_break,
1030 .chars_in_buffer = serial_chars_in_buffer,
1031 .read_proc = serial_read_proc,
1032 .tiocmget = serial_tiocmget,
1033 .tiocmset = serial_tiocmset,
1034};
1035
1036struct tty_driver *usb_serial_tty_driver;
1037
1038static int __init usb_serial_init(void)
1039{
1040 int i;
1041 int result;
1042
1043 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1044 if (!usb_serial_tty_driver)
1045 return -ENOMEM;
1046
1047 /* Initialize our global data */
1048 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1049 serial_table[i] = NULL;
1050 }
1051
1052 result = bus_register(&usb_serial_bus_type);
1053 if (result) {
1054 err("%s - registering bus driver failed", __FUNCTION__);
1055 goto exit_bus;
1056 }
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 usb_serial_tty_driver->owner = THIS_MODULE;
1059 usb_serial_tty_driver->driver_name = "usbserial";
1060 usb_serial_tty_driver->devfs_name = "usb/tts/";
1061 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;
1066 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1067 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");