[PATCH] devfs: Remove the tty_driver devfs_name field as it's no longer needed
[deliverable/linux.git] / drivers / usb / serial / usb-serial.c
CommitLineData
1da177e4
LT
1/*
2 * USB Serial Converter driver
3 *
502b95c1 4 * Copyright (C) 1999 - 2005 Greg Kroah-Hartman (greg@kroah.com)
1da177e4
LT
5 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
6 * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
502b95c1 12 * This driver was originally based on the ACM driver by Armin Fuerst (which was
1da177e4
LT
13 * based on a driver by Brad Keryan)
14 *
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
16 *
1da177e4
LT
17 */
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>
1ce7dd26 30#include <linux/mutex.h>
1da177e4
LT
31#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 */
1da177e4
LT
41#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
42#define DRIVER_DESC "USB Serial Driver core"
43
44/* Driver structure we register with the USB core */
45static struct usb_driver usb_serial_driver = {
1da177e4
LT
46 .name = "usbserial",
47 .probe = usb_serial_probe,
48 .disconnect = usb_serial_disconnect,
ba9dc657 49 .no_dynamic_id = 1,
1da177e4
LT
50};
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
269bda1c 125 dbg("%s - %s", __FUNCTION__, serial->type->description);
1da177e4
LT
126
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
2f8ad9a1
PZ
165 flush_scheduled_work(); /* port->work */
166
1da177e4
LT
167 usb_put_dev(serial->dev);
168
169 /* free up any memory that we allocated */
170 kfree (serial);
171}
172
73e487fd
GL
173void usb_serial_put(struct usb_serial *serial)
174{
175 kref_put(&serial->kref, destroy_serial);
176}
177
1da177e4
LT
178/*****************************************************************************
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];
71a84163
LFC
199 if (!port) {
200 retval = -ENODEV;
201 goto bailout_kref_put;
202 }
8a4613f0 203
71a84163
LFC
204 if (mutex_lock_interruptible(&port->mutex)) {
205 retval = -ERESTARTSYS;
206 goto bailout_kref_put;
207 }
1da177e4
LT
208
209 ++port->open_count;
210
ca85485c
PF
211 /* set up our port structure making the tty driver
212 * remember our port object, and us it */
213 tty->driver_data = port;
214 port->tty = tty;
1da177e4 215
ca85485c 216 if (port->open_count == 1) {
1da177e4
LT
217
218 /* lock this module before we call it
219 * this may fail, which means we must bail out,
220 * safe because we are called with BKL held */
18fcac35 221 if (!try_module_get(serial->type->driver.owner)) {
1da177e4 222 retval = -ENODEV;
71a84163 223 goto bailout_mutex_unlock;
1da177e4
LT
224 }
225
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
1ce7dd26 233 mutex_unlock(&port->mutex);
1da177e4
LT
234 return 0;
235
236bailout_module_put:
18fcac35 237 module_put(serial->type->driver.owner);
71a84163 238bailout_mutex_unlock:
1da177e4 239 port->open_count = 0;
b059c81a
FG
240 tty->driver_data = NULL;
241 port->tty = NULL;
1ce7dd26 242 mutex_unlock(&port->mutex);
71a84163 243bailout_kref_put:
73e487fd 244 usb_serial_put(serial);
1da177e4
LT
245 return retval;
246}
247
248static void serial_close(struct tty_struct *tty, struct file * filp)
249{
81671ddb 250 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
251
252 if (!port)
253 return;
254
255 dbg("%s - port %d", __FUNCTION__, port->number);
256
1ce7dd26 257 mutex_lock(&port->mutex);
8a4613f0 258
91c0bce2 259 if (port->open_count == 0) {
1ce7dd26 260 mutex_unlock(&port->mutex);
91c0bce2
GKH
261 return;
262 }
1da177e4
LT
263
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
18fcac35 276 module_put(port->serial->type->driver.owner);
1da177e4
LT
277 }
278
1ce7dd26 279 mutex_unlock(&port->mutex);
73e487fd 280 usb_serial_put(port->serial);
1da177e4
LT
281}
282
283static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
284{
81671ddb 285 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
286 int retval = -EINVAL;
287
73e487fd 288 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
487f9c67
LFC
289 goto exit;
290
1da177e4
LT
291 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{
81671ddb 307 struct usb_serial_port *port = tty->driver_data;
db54a53d 308 int retval = -ENODEV;
1da177e4 309
487f9c67
LFC
310 if (!port)
311 goto exit;
312
1da177e4
LT
313 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{
81671ddb 329 struct usb_serial_port *port = tty->driver_data;
db54a53d 330 int retval = -ENODEV;
1da177e4 331
487f9c67
LFC
332 if (!port)
333 goto exit;
334
1da177e4
LT
335 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{
81671ddb 351 struct usb_serial_port *port = tty->driver_data;
1da177e4 352
487f9c67
LFC
353 if (!port)
354 return;
355
1da177e4
LT
356 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{
81671ddb 370 struct usb_serial_port *port = tty->driver_data;
1da177e4 371
487f9c67
LFC
372 if (!port)
373 return;
374
1da177e4
LT
375 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{
81671ddb 389 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
390 int retval = -ENODEV;
391
487f9c67
LFC
392 if (!port)
393 goto exit;
394
1da177e4
LT
395 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{
81671ddb 414 struct usb_serial_port *port = tty->driver_data;
1da177e4 415
487f9c67
LFC
416 if (!port)
417 return;
418
1da177e4
LT
419 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{
81671ddb 433 struct usb_serial_port *port = tty->driver_data;
1da177e4 434
487f9c67
LFC
435 if (!port)
436 return;
437
1da177e4
LT
438 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__);
17a882fc 459 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
1da177e4
LT
460 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);
18fcac35
GKH
466 if (serial->type->driver.owner)
467 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
269bda1c 468 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
1da177e4
LT
469 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 }
73e487fd 485 usb_serial_put(serial);
1da177e4
LT
486 }
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{
81671ddb 497 struct usb_serial_port *port = tty->driver_data;
1da177e4 498
487f9c67 499 if (!port)
db54a53d 500 return -ENODEV;
487f9c67 501
1da177e4
LT
502 dbg("%s - port %d", __FUNCTION__, port->number);
503
504 if (!port->open_count) {
505 dbg("%s - port not open", __FUNCTION__);
db54a53d 506 return -ENODEV;
1da177e4
LT
507 }
508
509 if (port->serial->type->tiocmget)
510 return port->serial->type->tiocmget(port, file);
511
1da177e4
LT
512 return -EINVAL;
513}
514
515static int serial_tiocmset (struct tty_struct *tty, struct file *file,
516 unsigned int set, unsigned int clear)
517{
81671ddb 518 struct usb_serial_port *port = tty->driver_data;
1da177e4 519
487f9c67 520 if (!port)
db54a53d 521 return -ENODEV;
487f9c67 522
1da177e4
LT
523 dbg("%s - port %d", __FUNCTION__, port->number);
524
525 if (!port->open_count) {
526 dbg("%s - port not open", __FUNCTION__);
db54a53d 527 return -ENODEV;
1da177e4
LT
528 }
529
530 if (port->serial->type->tiocmset)
531 return port->serial->type->tiocmset(port, file, set, clear);
532
1da177e4
LT
533 return -EINVAL;
534}
535
cf2c7481
PZ
536/*
537 * We would be calling tty_wakeup here, but unfortunately some line
538 * disciplines have an annoying habit of calling tty->write from
539 * the write wakeup callback (e.g. n_hdlc.c).
540 */
541void usb_serial_port_softint(struct usb_serial_port *port)
542{
543 schedule_work(&port->work);
544}
545
546static void usb_serial_port_work(void *private)
1da177e4 547{
81671ddb 548 struct usb_serial_port *port = private;
1da177e4
LT
549 struct tty_struct *tty;
550
551 dbg("%s - port %d", __FUNCTION__, port->number);
552
553 if (!port)
554 return;
555
556 tty = port->tty;
557 if (!tty)
558 return;
559
560 tty_wakeup(tty);
561}
562
563static void port_release(struct device *dev)
564{
565 struct usb_serial_port *port = to_usb_serial_port(dev);
566
567 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
568 usb_kill_urb(port->read_urb);
569 usb_free_urb(port->read_urb);
570 usb_kill_urb(port->write_urb);
571 usb_free_urb(port->write_urb);
572 usb_kill_urb(port->interrupt_in_urb);
573 usb_free_urb(port->interrupt_in_urb);
574 usb_kill_urb(port->interrupt_out_urb);
575 usb_free_urb(port->interrupt_out_urb);
576 kfree(port->bulk_in_buffer);
577 kfree(port->bulk_out_buffer);
578 kfree(port->interrupt_in_buffer);
579 kfree(port->interrupt_out_buffer);
580 kfree(port);
581}
582
583static struct usb_serial * create_serial (struct usb_device *dev,
584 struct usb_interface *interface,
ea65370d 585 struct usb_serial_driver *driver)
1da177e4
LT
586{
587 struct usb_serial *serial;
588
80b6ca48 589 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
1da177e4
LT
590 if (!serial) {
591 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
592 return NULL;
593 }
1da177e4 594 serial->dev = usb_get_dev(dev);
ea65370d 595 serial->type = driver;
1da177e4
LT
596 serial->interface = interface;
597 kref_init(&serial->kref);
598
599 return serial;
600}
601
ea65370d 602static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
1da177e4
LT
603{
604 struct list_head *p;
605 const struct usb_device_id *id;
ea65370d 606 struct usb_serial_driver *t;
1da177e4 607
93b1fae4 608 /* Check if the usb id matches a known device */
1da177e4 609 list_for_each(p, &usb_serial_driver_list) {
ea65370d 610 t = list_entry(p, struct usb_serial_driver, driver_list);
1da177e4
LT
611 id = usb_match_id(iface, t->id_table);
612 if (id != NULL) {
613 dbg("descriptor matches");
614 return t;
615 }
616 }
617
618 return NULL;
619}
620
621int usb_serial_probe(struct usb_interface *interface,
622 const struct usb_device_id *id)
623{
624 struct usb_device *dev = interface_to_usbdev (interface);
625 struct usb_serial *serial = NULL;
626 struct usb_serial_port *port;
627 struct usb_host_interface *iface_desc;
628 struct usb_endpoint_descriptor *endpoint;
629 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
630 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
631 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
632 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
ea65370d 633 struct usb_serial_driver *type = NULL;
1da177e4
LT
634 int retval;
635 int minor;
636 int buffer_size;
637 int i;
638 int num_interrupt_in = 0;
639 int num_interrupt_out = 0;
640 int num_bulk_in = 0;
641 int num_bulk_out = 0;
642 int num_ports = 0;
643 int max_endpoints;
644
645 type = search_serial_device(interface);
646 if (!type) {
647 dbg("none matched");
648 return -ENODEV;
649 }
650
651 serial = create_serial (dev, interface, type);
652 if (!serial) {
653 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
654 return -ENOMEM;
655 }
656
657 /* if this device type has a probe function, call it */
658 if (type->probe) {
659 const struct usb_device_id *id;
660
18fcac35 661 if (!try_module_get(type->driver.owner)) {
1da177e4
LT
662 dev_err(&interface->dev, "module get failed, exiting\n");
663 kfree (serial);
664 return -EIO;
665 }
666
667 id = usb_match_id(interface, type->id_table);
668 retval = type->probe(serial, id);
18fcac35 669 module_put(type->driver.owner);
1da177e4
LT
670
671 if (retval) {
672 dbg ("sub driver rejected device");
673 kfree (serial);
674 return retval;
675 }
676 }
677
678 /* descriptor matches, let's find the endpoints needed */
679 /* check out the endpoints */
680 iface_desc = interface->cur_altsetting;
681 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
682 endpoint = &iface_desc->endpoint[i].desc;
683
684 if ((endpoint->bEndpointAddress & 0x80) &&
685 ((endpoint->bmAttributes & 3) == 0x02)) {
686 /* we found a bulk in endpoint */
687 dbg("found bulk in on endpoint %d", i);
688 bulk_in_endpoint[num_bulk_in] = endpoint;
689 ++num_bulk_in;
690 }
691
692 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
693 ((endpoint->bmAttributes & 3) == 0x02)) {
694 /* we found a bulk out endpoint */
695 dbg("found bulk out on endpoint %d", i);
696 bulk_out_endpoint[num_bulk_out] = endpoint;
697 ++num_bulk_out;
698 }
699
700 if ((endpoint->bEndpointAddress & 0x80) &&
701 ((endpoint->bmAttributes & 3) == 0x03)) {
702 /* we found a interrupt in endpoint */
703 dbg("found interrupt in on endpoint %d", i);
704 interrupt_in_endpoint[num_interrupt_in] = endpoint;
705 ++num_interrupt_in;
706 }
707
708 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
709 ((endpoint->bmAttributes & 3) == 0x03)) {
710 /* we found an interrupt out endpoint */
711 dbg("found interrupt out on endpoint %d", i);
712 interrupt_out_endpoint[num_interrupt_out] = endpoint;
713 ++num_interrupt_out;
714 }
715 }
716
717#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
718 /* BEGIN HORRIBLE HACK FOR PL2303 */
719 /* this is needed due to the looney way its endpoints are set up */
720 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
721 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
722 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
723 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
724 if (interface != dev->actconfig->interface[0]) {
725 /* check out the endpoints of the other interface*/
726 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
727 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
728 endpoint = &iface_desc->endpoint[i].desc;
729 if ((endpoint->bEndpointAddress & 0x80) &&
730 ((endpoint->bmAttributes & 3) == 0x03)) {
731 /* we found a interrupt in endpoint */
732 dbg("found interrupt in for Prolific device on separate interface");
733 interrupt_in_endpoint[num_interrupt_in] = endpoint;
734 ++num_interrupt_in;
735 }
736 }
737 }
738
739 /* Now make sure the PL-2303 is configured correctly.
740 * If not, give up now and hope this hack will work
741 * properly during a later invocation of usb_serial_probe
742 */
743 if (num_bulk_in == 0 || num_bulk_out == 0) {
744 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
745 kfree (serial);
746 return -ENODEV;
747 }
748 }
749 /* END HORRIBLE HACK FOR PL2303 */
750#endif
751
752 /* found all that we need */
269bda1c 753 dev_info(&interface->dev, "%s converter detected\n", type->description);
1da177e4
LT
754
755#ifdef CONFIG_USB_SERIAL_GENERIC
756 if (type == &usb_serial_generic_device) {
757 num_ports = num_bulk_out;
758 if (num_ports == 0) {
759 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
760 kfree (serial);
761 return -EIO;
762 }
763 }
764#endif
765 if (!num_ports) {
766 /* if this device type has a calc_num_ports function, call it */
767 if (type->calc_num_ports) {
18fcac35 768 if (!try_module_get(type->driver.owner)) {
1da177e4
LT
769 dev_err(&interface->dev, "module get failed, exiting\n");
770 kfree (serial);
771 return -EIO;
772 }
773 num_ports = type->calc_num_ports (serial);
18fcac35 774 module_put(type->driver.owner);
1da177e4
LT
775 }
776 if (!num_ports)
777 num_ports = type->num_ports;
778 }
779
780 if (get_free_serial (serial, num_ports, &minor) == NULL) {
781 dev_err(&interface->dev, "No more free serial devices\n");
782 kfree (serial);
783 return -ENOMEM;
784 }
785
786 serial->minor = minor;
787 serial->num_ports = num_ports;
788 serial->num_bulk_in = num_bulk_in;
789 serial->num_bulk_out = num_bulk_out;
790 serial->num_interrupt_in = num_interrupt_in;
791 serial->num_interrupt_out = num_interrupt_out;
792
793 /* create our ports, we need as many as the max endpoints */
794 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
795 max_endpoints = max(num_bulk_in, num_bulk_out);
796 max_endpoints = max(max_endpoints, num_interrupt_in);
797 max_endpoints = max(max_endpoints, num_interrupt_out);
798 max_endpoints = max(max_endpoints, (int)serial->num_ports);
799 serial->num_port_pointers = max_endpoints;
800 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
801 for (i = 0; i < max_endpoints; ++i) {
80b6ca48 802 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
1da177e4
LT
803 if (!port)
804 goto probe_error;
1da177e4
LT
805 port->number = i + serial->minor;
806 port->serial = serial;
507ca9bc 807 spin_lock_init(&port->lock);
1ce7dd26 808 mutex_init(&port->mutex);
cf2c7481 809 INIT_WORK(&port->work, usb_serial_port_work, port);
1da177e4
LT
810 serial->port[i] = port;
811 }
812
813 /* set up the endpoint information */
814 for (i = 0; i < num_bulk_in; ++i) {
815 endpoint = bulk_in_endpoint[i];
816 port = serial->port[i];
817 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
818 if (!port->read_urb) {
819 dev_err(&interface->dev, "No free urbs available\n");
820 goto probe_error;
821 }
822 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
823 port->bulk_in_size = buffer_size;
824 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
825 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
826 if (!port->bulk_in_buffer) {
827 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
828 goto probe_error;
829 }
830 usb_fill_bulk_urb (port->read_urb, dev,
831 usb_rcvbulkpipe (dev,
832 endpoint->bEndpointAddress),
833 port->bulk_in_buffer, buffer_size,
834 serial->type->read_bulk_callback,
835 port);
836 }
837
838 for (i = 0; i < num_bulk_out; ++i) {
839 endpoint = bulk_out_endpoint[i];
840 port = serial->port[i];
841 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
842 if (!port->write_urb) {
843 dev_err(&interface->dev, "No free urbs available\n");
844 goto probe_error;
845 }
846 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
847 port->bulk_out_size = buffer_size;
848 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
849 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
850 if (!port->bulk_out_buffer) {
851 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
852 goto probe_error;
853 }
854 usb_fill_bulk_urb (port->write_urb, dev,
855 usb_sndbulkpipe (dev,
856 endpoint->bEndpointAddress),
857 port->bulk_out_buffer, buffer_size,
858 serial->type->write_bulk_callback,
859 port);
860 }
861
862 if (serial->type->read_int_callback) {
863 for (i = 0; i < num_interrupt_in; ++i) {
864 endpoint = interrupt_in_endpoint[i];
865 port = serial->port[i];
866 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
867 if (!port->interrupt_in_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->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
873 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
874 if (!port->interrupt_in_buffer) {
875 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
876 goto probe_error;
877 }
878 usb_fill_int_urb (port->interrupt_in_urb, dev,
879 usb_rcvintpipe (dev,
880 endpoint->bEndpointAddress),
881 port->interrupt_in_buffer, buffer_size,
882 serial->type->read_int_callback, port,
883 endpoint->bInterval);
884 }
885 } else if (num_interrupt_in) {
886 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
887 }
888
889 if (serial->type->write_int_callback) {
890 for (i = 0; i < num_interrupt_out; ++i) {
891 endpoint = interrupt_out_endpoint[i];
892 port = serial->port[i];
893 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
894 if (!port->interrupt_out_urb) {
895 dev_err(&interface->dev, "No free urbs available\n");
896 goto probe_error;
897 }
898 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
899 port->interrupt_out_size = buffer_size;
900 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
901 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
902 if (!port->interrupt_out_buffer) {
903 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
904 goto probe_error;
905 }
906 usb_fill_int_urb (port->interrupt_out_urb, dev,
907 usb_sndintpipe (dev,
908 endpoint->bEndpointAddress),
909 port->interrupt_out_buffer, buffer_size,
910 serial->type->write_int_callback, port,
911 endpoint->bInterval);
912 }
913 } else if (num_interrupt_out) {
914 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
915 }
916
917 /* if this device type has an attach function, call it */
918 if (type->attach) {
18fcac35 919 if (!try_module_get(type->driver.owner)) {
1da177e4
LT
920 dev_err(&interface->dev, "module get failed, exiting\n");
921 goto probe_error;
922 }
923 retval = type->attach (serial);
18fcac35 924 module_put(type->driver.owner);
1da177e4
LT
925 if (retval < 0)
926 goto probe_error;
927 if (retval > 0) {
928 /* quietly accept this device, but don't bind to a serial port
929 * as it's about to disappear */
930 goto exit;
931 }
932 }
933
934 /* register all of the individual ports with the driver core */
935 for (i = 0; i < num_ports; ++i) {
936 port = serial->port[i];
937 port->dev.parent = &interface->dev;
938 port->dev.driver = NULL;
939 port->dev.bus = &usb_serial_bus_type;
940 port->dev.release = &port_release;
941
942 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
943 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
944 device_register (&port->dev);
945 }
946
947 usb_serial_console_init (debug, minor);
948
949exit:
950 /* success */
951 usb_set_intfdata (interface, serial);
952 return 0;
953
954probe_error:
955 for (i = 0; i < num_bulk_in; ++i) {
956 port = serial->port[i];
957 if (!port)
958 continue;
959 if (port->read_urb)
960 usb_free_urb (port->read_urb);
961 kfree(port->bulk_in_buffer);
962 }
963 for (i = 0; i < num_bulk_out; ++i) {
964 port = serial->port[i];
965 if (!port)
966 continue;
967 if (port->write_urb)
968 usb_free_urb (port->write_urb);
969 kfree(port->bulk_out_buffer);
970 }
971 for (i = 0; i < num_interrupt_in; ++i) {
972 port = serial->port[i];
973 if (!port)
974 continue;
975 if (port->interrupt_in_urb)
976 usb_free_urb (port->interrupt_in_urb);
977 kfree(port->interrupt_in_buffer);
978 }
979 for (i = 0; i < num_interrupt_out; ++i) {
980 port = serial->port[i];
981 if (!port)
982 continue;
983 if (port->interrupt_out_urb)
984 usb_free_urb (port->interrupt_out_urb);
985 kfree(port->interrupt_out_buffer);
986 }
987
988 /* return the minor range that this device had */
989 return_serial (serial);
990
991 /* free up any memory that we allocated */
992 for (i = 0; i < serial->num_port_pointers; ++i)
993 kfree(serial->port[i]);
994 kfree (serial);
995 return -EIO;
996}
997
998void usb_serial_disconnect(struct usb_interface *interface)
999{
1000 int i;
1001 struct usb_serial *serial = usb_get_intfdata (interface);
1002 struct device *dev = &interface->dev;
1003 struct usb_serial_port *port;
1004
73e487fd 1005 usb_serial_console_disconnect(serial);
1da177e4
LT
1006 dbg ("%s", __FUNCTION__);
1007
1008 usb_set_intfdata (interface, NULL);
1009 if (serial) {
1010 for (i = 0; i < serial->num_ports; ++i) {
1011 port = serial->port[i];
1012 if (port && port->tty)
1013 tty_hangup(port->tty);
1014 }
1015 /* let the last holder of this object
1016 * cause it to be cleaned up */
73e487fd 1017 usb_serial_put(serial);
1da177e4
LT
1018 }
1019 dev_info(dev, "device disconnected\n");
1020}
1021
1022static struct tty_operations serial_ops = {
1023 .open = serial_open,
1024 .close = serial_close,
1025 .write = serial_write,
1026 .write_room = serial_write_room,
1027 .ioctl = serial_ioctl,
1028 .set_termios = serial_set_termios,
1029 .throttle = serial_throttle,
1030 .unthrottle = serial_unthrottle,
1031 .break_ctl = serial_break,
1032 .chars_in_buffer = serial_chars_in_buffer,
1033 .read_proc = serial_read_proc,
1034 .tiocmget = serial_tiocmget,
1035 .tiocmset = serial_tiocmset,
1036};
1037
1038struct tty_driver *usb_serial_tty_driver;
1039
1040static int __init usb_serial_init(void)
1041{
1042 int i;
1043 int result;
1044
1045 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1046 if (!usb_serial_tty_driver)
1047 return -ENOMEM;
1048
1049 /* Initialize our global data */
1050 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1051 serial_table[i] = NULL;
1052 }
1053
1054 result = bus_register(&usb_serial_bus_type);
1055 if (result) {
1056 err("%s - registering bus driver failed", __FUNCTION__);
1057 goto exit_bus;
1058 }
1059
1da177e4
LT
1060 usb_serial_tty_driver->owner = THIS_MODULE;
1061 usb_serial_tty_driver->driver_name = "usbserial";
1da177e4
LT
1062 usb_serial_tty_driver->name = "ttyUSB";
1063 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1064 usb_serial_tty_driver->minor_start = 0;
1065 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1066 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1067 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1068 usb_serial_tty_driver->init_termios = tty_std_termios;
1069 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1070 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1071 result = tty_register_driver(usb_serial_tty_driver);
1072 if (result) {
1073 err("%s - tty_register_driver failed", __FUNCTION__);
1074 goto exit_reg_driver;
1075 }
1076
1077 /* register the USB driver */
1078 result = usb_register(&usb_serial_driver);
1079 if (result < 0) {
1080 err("%s - usb_register failed", __FUNCTION__);
1081 goto exit_tty;
1082 }
1083
06299db3
GKH
1084 /* register the generic driver, if we should */
1085 result = usb_serial_generic_register(debug);
1086 if (result < 0) {
1087 err("%s - registering generic driver failed", __FUNCTION__);
1088 goto exit_generic;
1089 }
1090
17a882fc 1091 info(DRIVER_DESC);
1da177e4
LT
1092
1093 return result;
1094
06299db3
GKH
1095exit_generic:
1096 usb_deregister(&usb_serial_driver);
1097
1da177e4
LT
1098exit_tty:
1099 tty_unregister_driver(usb_serial_tty_driver);
1100
1101exit_reg_driver:
1da177e4
LT
1102 bus_unregister(&usb_serial_bus_type);
1103
1104exit_bus:
1105 err ("%s - returning with error %d", __FUNCTION__, result);
1106 put_tty_driver(usb_serial_tty_driver);
1107 return result;
1108}
1109
1110
1111static void __exit usb_serial_exit(void)
1112{
1113 usb_serial_console_exit();
1114
1115 usb_serial_generic_deregister();
1116
1117 usb_deregister(&usb_serial_driver);
1118 tty_unregister_driver(usb_serial_tty_driver);
1119 put_tty_driver(usb_serial_tty_driver);
1120 bus_unregister(&usb_serial_bus_type);
1121}
1122
1123
1124module_init(usb_serial_init);
1125module_exit(usb_serial_exit);
1126
1127#define set_to_generic_if_null(type, function) \
1128 do { \
1129 if (!type->function) { \
1130 type->function = usb_serial_generic_##function; \
1131 dbg("Had to override the " #function \
1132 " usb serial operation with the generic one.");\
1133 } \
1134 } while (0)
1135
ea65370d 1136static void fixup_generic(struct usb_serial_driver *device)
1da177e4
LT
1137{
1138 set_to_generic_if_null(device, open);
1139 set_to_generic_if_null(device, write);
1140 set_to_generic_if_null(device, close);
1141 set_to_generic_if_null(device, write_room);
1142 set_to_generic_if_null(device, chars_in_buffer);
1143 set_to_generic_if_null(device, read_bulk_callback);
1144 set_to_generic_if_null(device, write_bulk_callback);
1145 set_to_generic_if_null(device, shutdown);
1146}
1147
ea65370d 1148int usb_serial_register(struct usb_serial_driver *driver)
1da177e4
LT
1149{
1150 int retval;
1151
ea65370d 1152 fixup_generic(driver);
1da177e4 1153
269bda1c
GKH
1154 if (!driver->description)
1155 driver->description = driver->driver.name;
1156
1da177e4 1157 /* Add this device to our list of devices */
ea65370d 1158 list_add(&driver->driver_list, &usb_serial_driver_list);
1da177e4 1159
ea65370d 1160 retval = usb_serial_bus_register(driver);
1da177e4 1161 if (retval) {
269bda1c 1162 err("problem %d when registering driver %s", retval, driver->description);
ea65370d 1163 list_del(&driver->driver_list);
1da177e4
LT
1164 }
1165 else
269bda1c 1166 info("USB Serial support registered for %s", driver->description);
1da177e4
LT
1167
1168 return retval;
1169}
1170
1171
ea65370d 1172void usb_serial_deregister(struct usb_serial_driver *device)
1da177e4 1173{
269bda1c 1174 info("USB Serial deregistering driver %s", device->description);
1da177e4
LT
1175 list_del(&device->driver_list);
1176 usb_serial_bus_deregister(device);
1177}
1178
1179
1180
1181/* If the usb-serial core is built into the core, the usb-serial drivers
1182 need these symbols to load properly as modules. */
1183EXPORT_SYMBOL_GPL(usb_serial_register);
1184EXPORT_SYMBOL_GPL(usb_serial_deregister);
1185EXPORT_SYMBOL_GPL(usb_serial_probe);
1186EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1187EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1188
1189
1190/* Module information */
1191MODULE_AUTHOR( DRIVER_AUTHOR );
1192MODULE_DESCRIPTION( DRIVER_DESC );
1da177e4
LT
1193MODULE_LICENSE("GPL");
1194
1195module_param(debug, bool, S_IRUGO | S_IWUSR);
1196MODULE_PARM_DESC(debug, "Debug enabled or not");
This page took 0.217412 seconds and 5 git commands to generate.