blob: 90beb5c50e59ca482fac9ec380653dc9c1294fca [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>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070034#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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
Pete Zaitcev34f8e762006-06-21 15:00:45 -070043static void port_free(struct usb_serial_port *port);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* Driver structure we register with the USB core */
46static struct usb_driver usb_serial_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 .name = "usbserial",
48 .probe = usb_serial_probe,
49 .disconnect = usb_serial_disconnect,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080050 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53/* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
54 the MODULE_DEVICE_TABLE declarations in each serial driver
55 cause the "hotplug" program to pull in whatever module is necessary
56 via modprobe, and modprobe will load usbserial because the serial
57 drivers depend on it.
58*/
59
60static int debug;
61static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
62static LIST_HEAD(usb_serial_driver_list);
63
64struct usb_serial *usb_serial_get_by_index(unsigned index)
65{
66 struct usb_serial *serial = serial_table[index];
67
68 if (serial)
69 kref_get(&serial->kref);
70 return serial;
71}
72
73static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
74{
75 unsigned int i, j;
76 int good_spot;
77
78 dbg("%s %d", __FUNCTION__, num_ports);
79
80 *minor = 0;
81 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
82 if (serial_table[i])
83 continue;
84
85 good_spot = 1;
86 for (j = 1; j <= num_ports-1; ++j)
87 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
88 good_spot = 0;
89 i += j;
90 break;
91 }
92 if (good_spot == 0)
93 continue;
94
95 *minor = i;
96 dbg("%s - minor base = %d", __FUNCTION__, *minor);
97 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
98 serial_table[i] = serial;
99 return serial;
100 }
101 return NULL;
102}
103
104static void return_serial(struct usb_serial *serial)
105{
106 int i;
107
108 dbg("%s", __FUNCTION__);
109
110 if (serial == NULL)
111 return;
112
113 for (i = 0; i < serial->num_ports; ++i) {
114 serial_table[serial->minor + i] = NULL;
115 }
116}
117
118static void destroy_serial(struct kref *kref)
119{
120 struct usb_serial *serial;
121 struct usb_serial_port *port;
122 int i;
123
124 serial = to_usb_serial(kref);
125
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700126 dbg("%s - %s", __FUNCTION__, serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 serial->type->shutdown(serial);
129
130 /* return the minor range that this device had */
131 return_serial(serial);
132
133 for (i = 0; i < serial->num_ports; ++i)
134 serial->port[i]->open_count = 0;
135
136 /* the ports are cleaned up and released in port_release() */
137 for (i = 0; i < serial->num_ports; ++i)
138 if (serial->port[i]->dev.parent != NULL) {
139 device_unregister(&serial->port[i]->dev);
140 serial->port[i] = NULL;
141 }
142
143 /* If this is a "fake" port, we have to clean it up here, as it will
144 * not get cleaned up in port_release() as it was never registered with
145 * the driver core */
146 if (serial->num_ports < serial->num_port_pointers) {
147 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
148 port = serial->port[i];
149 if (!port)
150 continue;
Pete Zaitcev34f8e762006-06-21 15:00:45 -0700151 port_free(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 }
154
155 usb_put_dev(serial->dev);
156
157 /* free up any memory that we allocated */
158 kfree (serial);
159}
160
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200161void usb_serial_put(struct usb_serial *serial)
162{
163 kref_put(&serial->kref, destroy_serial);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*****************************************************************************
167 * Driver tty interface functions
168 *****************************************************************************/
169static int serial_open (struct tty_struct *tty, struct file * filp)
170{
171 struct usb_serial *serial;
172 struct usb_serial_port *port;
173 unsigned int portNumber;
174 int retval;
175
176 dbg("%s", __FUNCTION__);
177
178 /* get the serial object associated with this tty pointer */
179 serial = usb_serial_get_by_index(tty->index);
180 if (!serial) {
181 tty->driver_data = NULL;
182 return -ENODEV;
183 }
184
185 portNumber = tty->index - serial->minor;
186 port = serial->port[portNumber];
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300187 if (!port) {
188 retval = -ENODEV;
189 goto bailout_kref_put;
190 }
Luiz Fernando Capitulino8a4613f2005-11-28 19:16:07 -0200191
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300192 if (mutex_lock_interruptible(&port->mutex)) {
193 retval = -ERESTARTSYS;
194 goto bailout_kref_put;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 ++port->open_count;
198
Paul Fulghumca854852006-04-13 22:28:17 +0200199 /* set up our port structure making the tty driver
200 * remember our port object, and us it */
201 tty->driver_data = port;
202 port->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Paul Fulghumca854852006-04-13 22:28:17 +0200204 if (port->open_count == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* lock this module before we call it
207 * this may fail, which means we must bail out,
208 * safe because we are called with BKL held */
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700209 if (!try_module_get(serial->type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 retval = -ENODEV;
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300211 goto bailout_mutex_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 /* only call the device specific open if this
215 * is the first time the port is opened */
216 retval = serial->type->open(port, filp);
217 if (retval)
218 goto bailout_module_put;
219 }
220
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300221 mutex_unlock(&port->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223
224bailout_module_put:
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700225 module_put(serial->type->driver.owner);
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300226bailout_mutex_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 port->open_count = 0;
Frank Gevaertsb059c812006-06-14 15:52:05 +0200228 tty->driver_data = NULL;
229 port->tty = NULL;
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300230 mutex_unlock(&port->mutex);
Luiz Fernando Capitulino71a84162006-05-11 22:34:24 -0300231bailout_kref_put:
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200232 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return retval;
234}
235
236static void serial_close(struct tty_struct *tty, struct file * filp)
237{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200238 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (!port)
241 return;
242
243 dbg("%s - port %d", __FUNCTION__, port->number);
244
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300245 mutex_lock(&port->mutex);
Luiz Fernando Capitulino8a4613f2005-11-28 19:16:07 -0200246
Greg Kroah-Hartman91c0bce2006-03-06 13:25:52 -0800247 if (port->open_count == 0) {
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300248 mutex_unlock(&port->mutex);
Greg Kroah-Hartman91c0bce2006-03-06 13:25:52 -0800249 return;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 --port->open_count;
253 if (port->open_count == 0) {
254 /* only call the device specific close if this
255 * port is being closed by the last owner */
256 port->serial->type->close(port, filp);
257
258 if (port->tty) {
259 if (port->tty->driver_data)
260 port->tty->driver_data = NULL;
261 port->tty = NULL;
262 }
263
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700264 module_put(port->serial->type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300267 mutex_unlock(&port->mutex);
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200268 usb_serial_put(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
272{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200273 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int retval = -EINVAL;
275
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200276 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200277 goto exit;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
280
281 if (!port->open_count) {
282 dbg("%s - port not opened", __FUNCTION__);
283 goto exit;
284 }
285
286 /* pass on to the driver specific version of this function */
287 retval = port->serial->type->write(port, buf, count);
288
289exit:
290 return retval;
291}
292
293static int serial_write_room (struct tty_struct *tty)
294{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200295 struct usb_serial_port *port = tty->driver_data;
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300296 int retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200298 if (!port)
299 goto exit;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 dbg("%s - port %d", __FUNCTION__, port->number);
302
303 if (!port->open_count) {
304 dbg("%s - port not open", __FUNCTION__);
305 goto exit;
306 }
307
308 /* pass on to the driver specific version of this function */
309 retval = port->serial->type->write_room(port);
310
311exit:
312 return retval;
313}
314
315static int serial_chars_in_buffer (struct tty_struct *tty)
316{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200317 struct usb_serial_port *port = tty->driver_data;
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300318 int retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200320 if (!port)
321 goto exit;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 dbg("%s = port %d", __FUNCTION__, port->number);
324
325 if (!port->open_count) {
326 dbg("%s - port not open", __FUNCTION__);
327 goto exit;
328 }
329
330 /* pass on to the driver specific version of this function */
331 retval = port->serial->type->chars_in_buffer(port);
332
333exit:
334 return retval;
335}
336
337static void serial_throttle (struct tty_struct * tty)
338{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200339 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200341 if (!port)
342 return;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 dbg("%s - port %d", __FUNCTION__, port->number);
345
346 if (!port->open_count) {
347 dbg ("%s - port not open", __FUNCTION__);
348 return;
349 }
350
351 /* pass on to the driver specific version of this function */
352 if (port->serial->type->throttle)
353 port->serial->type->throttle(port);
354}
355
356static void serial_unthrottle (struct tty_struct * tty)
357{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200358 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200360 if (!port)
361 return;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dbg("%s - port %d", __FUNCTION__, port->number);
364
365 if (!port->open_count) {
366 dbg("%s - port not open", __FUNCTION__);
367 return;
368 }
369
370 /* pass on to the driver specific version of this function */
371 if (port->serial->type->unthrottle)
372 port->serial->type->unthrottle(port);
373}
374
375static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
376{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200377 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int retval = -ENODEV;
379
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200380 if (!port)
381 goto exit;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
384
385 if (!port->open_count) {
386 dbg ("%s - port not open", __FUNCTION__);
387 goto exit;
388 }
389
390 /* pass on to the driver specific version of this function if it is available */
391 if (port->serial->type->ioctl)
392 retval = port->serial->type->ioctl(port, file, cmd, arg);
393 else
394 retval = -ENOIOCTLCMD;
395
396exit:
397 return retval;
398}
399
Alan Cox606d0992006-12-08 02:38:45 -0800400static void serial_set_termios (struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200402 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200404 if (!port)
405 return;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dbg("%s - port %d", __FUNCTION__, port->number);
408
409 if (!port->open_count) {
410 dbg("%s - port not open", __FUNCTION__);
411 return;
412 }
413
414 /* pass on to the driver specific version of this function if it is available */
415 if (port->serial->type->set_termios)
416 port->serial->type->set_termios(port, old);
417}
418
419static void serial_break (struct tty_struct *tty, int break_state)
420{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200421 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200423 if (!port)
424 return;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 dbg("%s - port %d", __FUNCTION__, port->number);
427
428 if (!port->open_count) {
429 dbg("%s - port not open", __FUNCTION__);
430 return;
431 }
432
433 /* pass on to the driver specific version of this function if it is available */
434 if (port->serial->type->break_ctl)
435 port->serial->type->break_ctl(port, break_state);
436}
437
438static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
439{
440 struct usb_serial *serial;
441 int length = 0;
442 int i;
443 off_t begin = 0;
444 char tmp[40];
445
446 dbg("%s", __FUNCTION__);
Greg Kroah-Hartman17a882f2005-06-20 21:15:16 -0700447 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
449 serial = usb_serial_get_by_index(i);
450 if (serial == NULL)
451 continue;
452
453 length += sprintf (page+length, "%d:", i);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700454 if (serial->type->driver.owner)
455 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700456 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 length += sprintf (page+length, " vendor:%04x product:%04x",
458 le16_to_cpu(serial->dev->descriptor.idVendor),
459 le16_to_cpu(serial->dev->descriptor.idProduct));
460 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
461 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
462
463 usb_make_path(serial->dev, tmp, sizeof(tmp));
464 length += sprintf (page+length, " path:%s", tmp);
465
466 length += sprintf (page+length, "\n");
Matthias Urlichs59925832006-09-11 12:35:20 +0200467 if ((length + begin) > (off + count)) {
468 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto done;
Matthias Urlichs59925832006-09-11 12:35:20 +0200470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if ((length + begin) < off) {
472 begin += length;
473 length = 0;
474 }
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +0200475 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 *eof = 1;
478done:
479 if (off >= (length + begin))
480 return 0;
481 *start = page + (off-begin);
482 return ((count < begin+length-off) ? count : begin+length-off);
483}
484
485static int serial_tiocmget (struct tty_struct *tty, struct file *file)
486{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200487 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200489 if (!port)
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300490 return -ENODEV;
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 dbg("%s - port %d", __FUNCTION__, port->number);
493
494 if (!port->open_count) {
495 dbg("%s - port not open", __FUNCTION__);
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300496 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
499 if (port->serial->type->tiocmget)
500 return port->serial->type->tiocmget(port, file);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -EINVAL;
503}
504
505static int serial_tiocmset (struct tty_struct *tty, struct file *file,
506 unsigned int set, unsigned int clear)
507{
Tobias Klauser81671dd2005-07-04 19:32:51 +0200508 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200510 if (!port)
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300511 return -ENODEV;
Luiz Fernando Capitulino487f9c62005-11-28 19:16:05 -0200512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 dbg("%s - port %d", __FUNCTION__, port->number);
514
515 if (!port->open_count) {
516 dbg("%s - port not open", __FUNCTION__);
Luiz Fernando N. Capitulinodb54a532006-06-12 22:46:20 -0300517 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 if (port->serial->type->tiocmset)
521 return port->serial->type->tiocmset(port, file, set, clear);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EINVAL;
524}
525
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700526/*
527 * We would be calling tty_wakeup here, but unfortunately some line
528 * disciplines have an annoying habit of calling tty->write from
529 * the write wakeup callback (e.g. n_hdlc.c).
530 */
531void usb_serial_port_softint(struct usb_serial_port *port)
532{
533 schedule_work(&port->work);
534}
535
David Howellsc4028952006-11-22 14:57:56 +0000536static void usb_serial_port_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
David Howellsc4028952006-11-22 14:57:56 +0000538 struct usb_serial_port *port =
539 container_of(work, struct usb_serial_port, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct tty_struct *tty;
541
542 dbg("%s - port %d", __FUNCTION__, port->number);
543
544 if (!port)
545 return;
546
547 tty = port->tty;
548 if (!tty)
549 return;
550
551 tty_wakeup(tty);
552}
553
554static void port_release(struct device *dev)
555{
556 struct usb_serial_port *port = to_usb_serial_port(dev);
557
558 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
Pete Zaitcev34f8e762006-06-21 15:00:45 -0700559 port_free(port);
560}
561
562static void port_free(struct usb_serial_port *port)
563{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 usb_kill_urb(port->read_urb);
565 usb_free_urb(port->read_urb);
566 usb_kill_urb(port->write_urb);
567 usb_free_urb(port->write_urb);
568 usb_kill_urb(port->interrupt_in_urb);
569 usb_free_urb(port->interrupt_in_urb);
570 usb_kill_urb(port->interrupt_out_urb);
571 usb_free_urb(port->interrupt_out_urb);
572 kfree(port->bulk_in_buffer);
573 kfree(port->bulk_out_buffer);
574 kfree(port->interrupt_in_buffer);
575 kfree(port->interrupt_out_buffer);
Pete Zaitcev34f8e762006-06-21 15:00:45 -0700576 flush_scheduled_work(); /* port->work */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 kfree(port);
578}
579
580static struct usb_serial * create_serial (struct usb_device *dev,
581 struct usb_interface *interface,
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700582 struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct usb_serial *serial;
585
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100586 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!serial) {
588 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
589 return NULL;
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 serial->dev = usb_get_dev(dev);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700592 serial->type = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 serial->interface = interface;
594 kref_init(&serial->kref);
595
596 return serial;
597}
598
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100599static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
600 struct usb_serial_driver *drv)
601{
602 struct usb_dynid *dynid;
603
604 spin_lock(&drv->dynids.lock);
605 list_for_each_entry(dynid, &drv->dynids.list, node) {
606 if (usb_match_one_id(intf, &dynid->id)) {
607 spin_unlock(&drv->dynids.lock);
608 return &dynid->id;
609 }
610 }
611 spin_unlock(&drv->dynids.lock);
612 return NULL;
613}
614
615static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
616 struct usb_interface *intf)
617{
618 const struct usb_device_id *id;
619
620 id = usb_match_id(intf, drv->id_table);
621 if (id) {
622 dbg("static descriptor matches");
623 goto exit;
624 }
625 id = match_dynamic_id(intf, drv);
626 if (id)
627 dbg("dynamic descriptor matches");
628exit:
629 return id;
630}
631
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700632static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct list_head *p;
635 const struct usb_device_id *id;
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700636 struct usb_serial_driver *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Adrian Bunk93b1fae2006-01-10 00:13:33 +0100638 /* Check if the usb id matches a known device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 list_for_each(p, &usb_serial_driver_list) {
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700640 t = list_entry(p, struct usb_serial_driver, driver_list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100641 id = get_iface_id(t, iface);
642 if (id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
646 return NULL;
647}
648
649int usb_serial_probe(struct usb_interface *interface,
650 const struct usb_device_id *id)
651{
652 struct usb_device *dev = interface_to_usbdev (interface);
653 struct usb_serial *serial = NULL;
654 struct usb_serial_port *port;
655 struct usb_host_interface *iface_desc;
656 struct usb_endpoint_descriptor *endpoint;
657 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
658 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
659 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
660 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700661 struct usb_serial_driver *type = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int retval;
663 int minor;
664 int buffer_size;
665 int i;
666 int num_interrupt_in = 0;
667 int num_interrupt_out = 0;
668 int num_bulk_in = 0;
669 int num_bulk_out = 0;
670 int num_ports = 0;
671 int max_endpoints;
672
673 type = search_serial_device(interface);
674 if (!type) {
675 dbg("none matched");
676 return -ENODEV;
677 }
678
679 serial = create_serial (dev, interface, type);
680 if (!serial) {
681 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
682 return -ENOMEM;
683 }
684
685 /* if this device type has a probe function, call it */
686 if (type->probe) {
687 const struct usb_device_id *id;
688
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700689 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 dev_err(&interface->dev, "module get failed, exiting\n");
691 kfree (serial);
692 return -EIO;
693 }
694
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100695 id = get_iface_id(type, interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 retval = type->probe(serial, id);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700697 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (retval) {
700 dbg ("sub driver rejected device");
701 kfree (serial);
702 return retval;
703 }
704 }
705
706 /* descriptor matches, let's find the endpoints needed */
707 /* check out the endpoints */
708 iface_desc = interface->cur_altsetting;
709 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
710 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino4fa1bbf2006-09-27 11:58:53 -0700711
712 if (usb_endpoint_is_bulk_in(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* we found a bulk in endpoint */
714 dbg("found bulk in on endpoint %d", i);
715 bulk_in_endpoint[num_bulk_in] = endpoint;
716 ++num_bulk_in;
717 }
718
Luiz Fernando N. Capitulino4fa1bbf2006-09-27 11:58:53 -0700719 if (usb_endpoint_is_bulk_out(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* we found a bulk out endpoint */
721 dbg("found bulk out on endpoint %d", i);
722 bulk_out_endpoint[num_bulk_out] = endpoint;
723 ++num_bulk_out;
724 }
Luiz Fernando N. Capitulino4fa1bbf2006-09-27 11:58:53 -0700725
726 if (usb_endpoint_is_int_in(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* we found a interrupt in endpoint */
728 dbg("found interrupt in on endpoint %d", i);
729 interrupt_in_endpoint[num_interrupt_in] = endpoint;
730 ++num_interrupt_in;
731 }
732
Luiz Fernando N. Capitulino4fa1bbf2006-09-27 11:58:53 -0700733 if (usb_endpoint_is_int_out(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* we found an interrupt out endpoint */
735 dbg("found interrupt out on endpoint %d", i);
736 interrupt_out_endpoint[num_interrupt_out] = endpoint;
737 ++num_interrupt_out;
738 }
739 }
740
741#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
742 /* BEGIN HORRIBLE HACK FOR PL2303 */
743 /* this is needed due to the looney way its endpoints are set up */
744 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
745 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
746 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
Johannes Steingraeber8fd80132006-09-16 16:17:34 +0200747 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
748 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
749 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (interface != dev->actconfig->interface[0]) {
751 /* check out the endpoints of the other interface*/
752 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
753 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
754 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino4fa1bbf2006-09-27 11:58:53 -0700755 if (usb_endpoint_is_int_in(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* we found a interrupt in endpoint */
757 dbg("found interrupt in for Prolific device on separate interface");
758 interrupt_in_endpoint[num_interrupt_in] = endpoint;
759 ++num_interrupt_in;
760 }
761 }
762 }
763
764 /* Now make sure the PL-2303 is configured correctly.
765 * If not, give up now and hope this hack will work
766 * properly during a later invocation of usb_serial_probe
767 */
768 if (num_bulk_in == 0 || num_bulk_out == 0) {
769 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
770 kfree (serial);
771 return -ENODEV;
772 }
773 }
774 /* END HORRIBLE HACK FOR PL2303 */
775#endif
776
777 /* found all that we need */
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700778 dev_info(&interface->dev, "%s converter detected\n", type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780#ifdef CONFIG_USB_SERIAL_GENERIC
781 if (type == &usb_serial_generic_device) {
782 num_ports = num_bulk_out;
783 if (num_ports == 0) {
784 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
785 kfree (serial);
786 return -EIO;
787 }
788 }
789#endif
790 if (!num_ports) {
791 /* if this device type has a calc_num_ports function, call it */
792 if (type->calc_num_ports) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700793 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 dev_err(&interface->dev, "module get failed, exiting\n");
795 kfree (serial);
796 return -EIO;
797 }
798 num_ports = type->calc_num_ports (serial);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700799 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 if (!num_ports)
802 num_ports = type->num_ports;
803 }
804
805 if (get_free_serial (serial, num_ports, &minor) == NULL) {
806 dev_err(&interface->dev, "No more free serial devices\n");
807 kfree (serial);
808 return -ENOMEM;
809 }
810
811 serial->minor = minor;
812 serial->num_ports = num_ports;
813 serial->num_bulk_in = num_bulk_in;
814 serial->num_bulk_out = num_bulk_out;
815 serial->num_interrupt_in = num_interrupt_in;
816 serial->num_interrupt_out = num_interrupt_out;
817
818 /* create our ports, we need as many as the max endpoints */
819 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
820 max_endpoints = max(num_bulk_in, num_bulk_out);
821 max_endpoints = max(max_endpoints, num_interrupt_in);
822 max_endpoints = max(max_endpoints, num_interrupt_out);
823 max_endpoints = max(max_endpoints, (int)serial->num_ports);
824 serial->num_port_pointers = max_endpoints;
825 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
826 for (i = 0; i < max_endpoints; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100827 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!port)
829 goto probe_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 port->number = i + serial->minor;
831 port->serial = serial;
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700832 spin_lock_init(&port->lock);
Luiz Fernando Capitulino1ce7dd22006-03-24 17:12:31 -0300833 mutex_init(&port->mutex);
David Howellsc4028952006-11-22 14:57:56 +0000834 INIT_WORK(&port->work, usb_serial_port_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 serial->port[i] = port;
836 }
837
838 /* set up the endpoint information */
839 for (i = 0; i < num_bulk_in; ++i) {
840 endpoint = bulk_in_endpoint[i];
841 port = serial->port[i];
842 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
843 if (!port->read_urb) {
844 dev_err(&interface->dev, "No free urbs available\n");
845 goto probe_error;
846 }
847 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
848 port->bulk_in_size = buffer_size;
849 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
850 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
851 if (!port->bulk_in_buffer) {
852 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
853 goto probe_error;
854 }
855 usb_fill_bulk_urb (port->read_urb, dev,
856 usb_rcvbulkpipe (dev,
857 endpoint->bEndpointAddress),
858 port->bulk_in_buffer, buffer_size,
859 serial->type->read_bulk_callback,
860 port);
861 }
862
863 for (i = 0; i < num_bulk_out; ++i) {
864 endpoint = bulk_out_endpoint[i];
865 port = serial->port[i];
866 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
867 if (!port->write_urb) {
868 dev_err(&interface->dev, "No free urbs available\n");
869 goto probe_error;
870 }
871 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
872 port->bulk_out_size = buffer_size;
873 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
874 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
875 if (!port->bulk_out_buffer) {
876 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
877 goto probe_error;
878 }
879 usb_fill_bulk_urb (port->write_urb, dev,
880 usb_sndbulkpipe (dev,
881 endpoint->bEndpointAddress),
882 port->bulk_out_buffer, buffer_size,
883 serial->type->write_bulk_callback,
884 port);
885 }
886
887 if (serial->type->read_int_callback) {
888 for (i = 0; i < num_interrupt_in; ++i) {
889 endpoint = interrupt_in_endpoint[i];
890 port = serial->port[i];
891 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
892 if (!port->interrupt_in_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_in_endpointAddress = endpoint->bEndpointAddress;
898 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
899 if (!port->interrupt_in_buffer) {
900 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
901 goto probe_error;
902 }
903 usb_fill_int_urb (port->interrupt_in_urb, dev,
904 usb_rcvintpipe (dev,
905 endpoint->bEndpointAddress),
906 port->interrupt_in_buffer, buffer_size,
907 serial->type->read_int_callback, port,
908 endpoint->bInterval);
909 }
910 } else if (num_interrupt_in) {
911 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
912 }
913
914 if (serial->type->write_int_callback) {
915 for (i = 0; i < num_interrupt_out; ++i) {
916 endpoint = interrupt_out_endpoint[i];
917 port = serial->port[i];
918 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
919 if (!port->interrupt_out_urb) {
920 dev_err(&interface->dev, "No free urbs available\n");
921 goto probe_error;
922 }
923 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
924 port->interrupt_out_size = buffer_size;
925 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
926 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
927 if (!port->interrupt_out_buffer) {
928 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
929 goto probe_error;
930 }
931 usb_fill_int_urb (port->interrupt_out_urb, dev,
932 usb_sndintpipe (dev,
933 endpoint->bEndpointAddress),
934 port->interrupt_out_buffer, buffer_size,
935 serial->type->write_int_callback, port,
936 endpoint->bInterval);
937 }
938 } else if (num_interrupt_out) {
939 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
940 }
941
942 /* if this device type has an attach function, call it */
943 if (type->attach) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700944 if (!try_module_get(type->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 dev_err(&interface->dev, "module get failed, exiting\n");
946 goto probe_error;
947 }
948 retval = type->attach (serial);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700949 module_put(type->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (retval < 0)
951 goto probe_error;
952 if (retval > 0) {
953 /* quietly accept this device, but don't bind to a serial port
954 * as it's about to disappear */
955 goto exit;
956 }
957 }
958
959 /* register all of the individual ports with the driver core */
960 for (i = 0; i < num_ports; ++i) {
961 port = serial->port[i];
962 port->dev.parent = &interface->dev;
963 port->dev.driver = NULL;
964 port->dev.bus = &usb_serial_bus_type;
965 port->dev.release = &port_release;
966
967 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
968 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
Greg Kroah-Hartman13f4db92006-08-28 11:43:25 -0700969 retval = device_register(&port->dev);
970 if (retval)
971 dev_err(&port->dev, "Error registering port device, "
972 "continuing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
975 usb_serial_console_init (debug, minor);
976
977exit:
978 /* success */
979 usb_set_intfdata (interface, serial);
980 return 0;
981
982probe_error:
983 for (i = 0; i < num_bulk_in; ++i) {
984 port = serial->port[i];
985 if (!port)
986 continue;
Mariusz Kozlowski95d43162006-11-08 15:36:51 +0100987 usb_free_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 kfree(port->bulk_in_buffer);
989 }
990 for (i = 0; i < num_bulk_out; ++i) {
991 port = serial->port[i];
992 if (!port)
993 continue;
Mariusz Kozlowski95d43162006-11-08 15:36:51 +0100994 usb_free_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 kfree(port->bulk_out_buffer);
996 }
997 for (i = 0; i < num_interrupt_in; ++i) {
998 port = serial->port[i];
999 if (!port)
1000 continue;
Mariusz Kozlowski95d43162006-11-08 15:36:51 +01001001 usb_free_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 kfree(port->interrupt_in_buffer);
1003 }
1004 for (i = 0; i < num_interrupt_out; ++i) {
1005 port = serial->port[i];
1006 if (!port)
1007 continue;
Mariusz Kozlowski95d43162006-11-08 15:36:51 +01001008 usb_free_urb(port->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 kfree(port->interrupt_out_buffer);
1010 }
1011
1012 /* return the minor range that this device had */
1013 return_serial (serial);
1014
1015 /* free up any memory that we allocated */
1016 for (i = 0; i < serial->num_port_pointers; ++i)
1017 kfree(serial->port[i]);
1018 kfree (serial);
1019 return -EIO;
1020}
1021
1022void usb_serial_disconnect(struct usb_interface *interface)
1023{
1024 int i;
1025 struct usb_serial *serial = usb_get_intfdata (interface);
1026 struct device *dev = &interface->dev;
1027 struct usb_serial_port *port;
1028
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +02001029 usb_serial_console_disconnect(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 dbg ("%s", __FUNCTION__);
1031
1032 usb_set_intfdata (interface, NULL);
1033 if (serial) {
1034 for (i = 0; i < serial->num_ports; ++i) {
1035 port = serial->port[i];
1036 if (port && port->tty)
1037 tty_hangup(port->tty);
1038 }
1039 /* let the last holder of this object
1040 * cause it to be cleaned up */
Guennadi Liakhovetski73e487f2006-04-25 07:46:17 +02001041 usb_serial_put(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043 dev_info(dev, "device disconnected\n");
1044}
1045
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001046static const struct tty_operations serial_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 .open = serial_open,
1048 .close = serial_close,
1049 .write = serial_write,
1050 .write_room = serial_write_room,
1051 .ioctl = serial_ioctl,
1052 .set_termios = serial_set_termios,
1053 .throttle = serial_throttle,
1054 .unthrottle = serial_unthrottle,
1055 .break_ctl = serial_break,
1056 .chars_in_buffer = serial_chars_in_buffer,
1057 .read_proc = serial_read_proc,
1058 .tiocmget = serial_tiocmget,
1059 .tiocmset = serial_tiocmset,
1060};
1061
1062struct tty_driver *usb_serial_tty_driver;
1063
1064static int __init usb_serial_init(void)
1065{
1066 int i;
1067 int result;
1068
1069 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1070 if (!usb_serial_tty_driver)
1071 return -ENOMEM;
1072
1073 /* Initialize our global data */
1074 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1075 serial_table[i] = NULL;
1076 }
1077
1078 result = bus_register(&usb_serial_bus_type);
1079 if (result) {
1080 err("%s - registering bus driver failed", __FUNCTION__);
1081 goto exit_bus;
1082 }
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 usb_serial_tty_driver->owner = THIS_MODULE;
1085 usb_serial_tty_driver->driver_name = "usbserial";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 usb_serial_tty_driver->name = "ttyUSB";
1087 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1088 usb_serial_tty_driver->minor_start = 0;
1089 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1090 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001091 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 usb_serial_tty_driver->init_termios = tty_std_termios;
1093 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1094 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1095 result = tty_register_driver(usb_serial_tty_driver);
1096 if (result) {
1097 err("%s - tty_register_driver failed", __FUNCTION__);
1098 goto exit_reg_driver;
1099 }
1100
1101 /* register the USB driver */
1102 result = usb_register(&usb_serial_driver);
1103 if (result < 0) {
1104 err("%s - usb_register failed", __FUNCTION__);
1105 goto exit_tty;
1106 }
1107
Greg Kroah-Hartman06299db2005-05-26 05:55:55 -07001108 /* register the generic driver, if we should */
1109 result = usb_serial_generic_register(debug);
1110 if (result < 0) {
1111 err("%s - registering generic driver failed", __FUNCTION__);
1112 goto exit_generic;
1113 }
1114
Greg Kroah-Hartman17a882f2005-06-20 21:15:16 -07001115 info(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 return result;
1118
Greg Kroah-Hartman06299db2005-05-26 05:55:55 -07001119exit_generic:
1120 usb_deregister(&usb_serial_driver);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122exit_tty:
1123 tty_unregister_driver(usb_serial_tty_driver);
1124
1125exit_reg_driver:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 bus_unregister(&usb_serial_bus_type);
1127
1128exit_bus:
1129 err ("%s - returning with error %d", __FUNCTION__, result);
1130 put_tty_driver(usb_serial_tty_driver);
1131 return result;
1132}
1133
1134
1135static void __exit usb_serial_exit(void)
1136{
1137 usb_serial_console_exit();
1138
1139 usb_serial_generic_deregister();
1140
1141 usb_deregister(&usb_serial_driver);
1142 tty_unregister_driver(usb_serial_tty_driver);
1143 put_tty_driver(usb_serial_tty_driver);
1144 bus_unregister(&usb_serial_bus_type);
1145}
1146
1147
1148module_init(usb_serial_init);
1149module_exit(usb_serial_exit);
1150
1151#define set_to_generic_if_null(type, function) \
1152 do { \
1153 if (!type->function) { \
1154 type->function = usb_serial_generic_##function; \
1155 dbg("Had to override the " #function \
1156 " usb serial operation with the generic one.");\
1157 } \
1158 } while (0)
1159
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001160static void fixup_generic(struct usb_serial_driver *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 set_to_generic_if_null(device, open);
1163 set_to_generic_if_null(device, write);
1164 set_to_generic_if_null(device, close);
1165 set_to_generic_if_null(device, write_room);
1166 set_to_generic_if_null(device, chars_in_buffer);
1167 set_to_generic_if_null(device, read_bulk_callback);
1168 set_to_generic_if_null(device, write_bulk_callback);
1169 set_to_generic_if_null(device, shutdown);
1170}
1171
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001172int usb_serial_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 int retval;
1175
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001176 fixup_generic(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001178 if (!driver->description)
1179 driver->description = driver->driver.name;
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* Add this device to our list of devices */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001182 list_add(&driver->driver_list, &usb_serial_driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001184 retval = usb_serial_bus_register(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (retval) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001186 err("problem %d when registering driver %s", retval, driver->description);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001187 list_del(&driver->driver_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 else
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001190 info("USB Serial support registered for %s", driver->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 return retval;
1193}
1194
1195
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001196void usb_serial_deregister(struct usb_serial_driver *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001198 info("USB Serial deregistering driver %s", device->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 list_del(&device->driver_list);
1200 usb_serial_bus_deregister(device);
1201}
1202
1203
1204
1205/* If the usb-serial core is built into the core, the usb-serial drivers
1206 need these symbols to load properly as modules. */
1207EXPORT_SYMBOL_GPL(usb_serial_register);
1208EXPORT_SYMBOL_GPL(usb_serial_deregister);
1209EXPORT_SYMBOL_GPL(usb_serial_probe);
1210EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1211EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1212
1213
1214/* Module information */
1215MODULE_AUTHOR( DRIVER_AUTHOR );
1216MODULE_DESCRIPTION( DRIVER_DESC );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217MODULE_LICENSE("GPL");
1218
1219module_param(debug, bool, S_IRUGO | S_IWUSR);
1220MODULE_PARM_DESC(debug, "Debug enabled or not");