[PATCH] USB serial: encapsulate schedule_work, remove double-calling
[deliverable/linux.git] / drivers / usb / serial / usb-serial.c
1 /*
2 * USB Serial Converter driver
3 *
4 * Copyright (C) 1999 - 2005 Greg Kroah-Hartman (greg@kroah.com)
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 *
12 * This driver was originally based on the ACM driver by Armin Fuerst (which was
13 * based on a driver by Brad Keryan)
14 *
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
16 *
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>
30 #include <linux/mutex.h>
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 */
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 */
45 static struct usb_driver usb_serial_driver = {
46 .name = "usbserial",
47 .probe = usb_serial_probe,
48 .disconnect = usb_serial_disconnect,
49 .no_dynamic_id = 1,
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
59 static int debug;
60 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
61 static LIST_HEAD(usb_serial_driver_list);
62
63 struct 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
72 static 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
103 static 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
117 static 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
125 dbg("%s - %s", __FUNCTION__, serial->type->description);
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
165 usb_put_dev(serial->dev);
166
167 /* free up any memory that we allocated */
168 kfree (serial);
169 }
170
171 void usb_serial_put(struct usb_serial *serial)
172 {
173 kref_put(&serial->kref, destroy_serial);
174 }
175
176 /*****************************************************************************
177 * Driver tty interface functions
178 *****************************************************************************/
179 static 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];
197 if (!port) {
198 retval = -ENODEV;
199 goto bailout_kref_put;
200 }
201
202 if (mutex_lock_interruptible(&port->mutex)) {
203 retval = -ERESTARTSYS;
204 goto bailout_kref_put;
205 }
206
207 ++port->open_count;
208
209 /* 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;
213
214 if (port->open_count == 1) {
215
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 */
219 if (!try_module_get(serial->type->driver.owner)) {
220 retval = -ENODEV;
221 goto bailout_mutex_unlock;
222 }
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
231 mutex_unlock(&port->mutex);
232 return 0;
233
234 bailout_module_put:
235 module_put(serial->type->driver.owner);
236 bailout_mutex_unlock:
237 port->open_count = 0;
238 mutex_unlock(&port->mutex);
239 bailout_kref_put:
240 usb_serial_put(serial);
241 return retval;
242 }
243
244 static void serial_close(struct tty_struct *tty, struct file * filp)
245 {
246 struct usb_serial_port *port = tty->driver_data;
247
248 if (!port)
249 return;
250
251 dbg("%s - port %d", __FUNCTION__, port->number);
252
253 mutex_lock(&port->mutex);
254
255 if (port->open_count == 0) {
256 mutex_unlock(&port->mutex);
257 return;
258 }
259
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
272 module_put(port->serial->type->driver.owner);
273 }
274
275 mutex_unlock(&port->mutex);
276 usb_serial_put(port->serial);
277 }
278
279 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
280 {
281 struct usb_serial_port *port = tty->driver_data;
282 int retval = -EINVAL;
283
284 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
285 goto exit;
286
287 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
297 exit:
298 return retval;
299 }
300
301 static int serial_write_room (struct tty_struct *tty)
302 {
303 struct usb_serial_port *port = tty->driver_data;
304 int retval = -EINVAL;
305
306 if (!port)
307 goto exit;
308
309 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
319 exit:
320 return retval;
321 }
322
323 static int serial_chars_in_buffer (struct tty_struct *tty)
324 {
325 struct usb_serial_port *port = tty->driver_data;
326 int retval = -EINVAL;
327
328 if (!port)
329 goto exit;
330
331 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
341 exit:
342 return retval;
343 }
344
345 static void serial_throttle (struct tty_struct * tty)
346 {
347 struct usb_serial_port *port = tty->driver_data;
348
349 if (!port)
350 return;
351
352 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
364 static void serial_unthrottle (struct tty_struct * tty)
365 {
366 struct usb_serial_port *port = tty->driver_data;
367
368 if (!port)
369 return;
370
371 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
383 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
384 {
385 struct usb_serial_port *port = tty->driver_data;
386 int retval = -ENODEV;
387
388 if (!port)
389 goto exit;
390
391 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
404 exit:
405 return retval;
406 }
407
408 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
409 {
410 struct usb_serial_port *port = tty->driver_data;
411
412 if (!port)
413 return;
414
415 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
427 static void serial_break (struct tty_struct *tty, int break_state)
428 {
429 struct usb_serial_port *port = tty->driver_data;
430
431 if (!port)
432 return;
433
434 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
446 static 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__);
455 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
456 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);
462 if (serial->type->driver.owner)
463 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
464 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
465 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 }
481 usb_serial_put(serial);
482 }
483 *eof = 1;
484 done:
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
491 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
492 {
493 struct usb_serial_port *port = tty->driver_data;
494
495 if (!port)
496 goto exit;
497
498 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
508 exit:
509 return -EINVAL;
510 }
511
512 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
513 unsigned int set, unsigned int clear)
514 {
515 struct usb_serial_port *port = tty->driver_data;
516
517 if (!port)
518 goto exit;
519
520 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
530 exit:
531 return -EINVAL;
532 }
533
534 /*
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 */
539 void usb_serial_port_softint(struct usb_serial_port *port)
540 {
541 schedule_work(&port->work);
542 }
543
544 static void usb_serial_port_work(void *private)
545 {
546 struct usb_serial_port *port = private;
547 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
561 static 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
581 static struct usb_serial * create_serial (struct usb_device *dev,
582 struct usb_interface *interface,
583 struct usb_serial_driver *driver)
584 {
585 struct usb_serial *serial;
586
587 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
588 if (!serial) {
589 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
590 return NULL;
591 }
592 serial->dev = usb_get_dev(dev);
593 serial->type = driver;
594 serial->interface = interface;
595 kref_init(&serial->kref);
596
597 return serial;
598 }
599
600 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
601 {
602 struct list_head *p;
603 const struct usb_device_id *id;
604 struct usb_serial_driver *t;
605
606 /* Check if the usb id matches a known device */
607 list_for_each(p, &usb_serial_driver_list) {
608 t = list_entry(p, struct usb_serial_driver, driver_list);
609 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
619 int 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];
631 struct usb_serial_driver *type = NULL;
632 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
659 if (!try_module_get(type->driver.owner)) {
660 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);
667 module_put(type->driver.owner);
668
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 */
751 dev_info(&interface->dev, "%s converter detected\n", type->description);
752
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) {
766 if (!try_module_get(type->driver.owner)) {
767 dev_err(&interface->dev, "module get failed, exiting\n");
768 kfree (serial);
769 return -EIO;
770 }
771 num_ports = type->calc_num_ports (serial);
772 module_put(type->driver.owner);
773 }
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) {
800 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
801 if (!port)
802 goto probe_error;
803 port->number = i + serial->minor;
804 port->serial = serial;
805 spin_lock_init(&port->lock);
806 mutex_init(&port->mutex);
807 INIT_WORK(&port->work, usb_serial_port_work, port);
808 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) {
917 if (!try_module_get(type->driver.owner)) {
918 dev_err(&interface->dev, "module get failed, exiting\n");
919 goto probe_error;
920 }
921 retval = type->attach (serial);
922 module_put(type->driver.owner);
923 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
947 exit:
948 /* success */
949 usb_set_intfdata (interface, serial);
950 return 0;
951
952 probe_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
996 void 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
1003 usb_serial_console_disconnect(serial);
1004 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 */
1015 usb_serial_put(serial);
1016 }
1017 dev_info(dev, "device disconnected\n");
1018 }
1019
1020 static 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
1036 struct tty_driver *usb_serial_tty_driver;
1037
1038 static 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
1058 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
1083 /* 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
1090 info(DRIVER_DESC);
1091
1092 return result;
1093
1094 exit_generic:
1095 usb_deregister(&usb_serial_driver);
1096
1097 exit_tty:
1098 tty_unregister_driver(usb_serial_tty_driver);
1099
1100 exit_reg_driver:
1101 bus_unregister(&usb_serial_bus_type);
1102
1103 exit_bus:
1104 err ("%s - returning with error %d", __FUNCTION__, result);
1105 put_tty_driver(usb_serial_tty_driver);
1106 return result;
1107 }
1108
1109
1110 static 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
1123 module_init(usb_serial_init);
1124 module_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
1135 static void fixup_generic(struct usb_serial_driver *device)
1136 {
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
1147 int usb_serial_register(struct usb_serial_driver *driver)
1148 {
1149 int retval;
1150
1151 fixup_generic(driver);
1152
1153 if (!driver->description)
1154 driver->description = driver->driver.name;
1155
1156 /* Add this device to our list of devices */
1157 list_add(&driver->driver_list, &usb_serial_driver_list);
1158
1159 retval = usb_serial_bus_register(driver);
1160 if (retval) {
1161 err("problem %d when registering driver %s", retval, driver->description);
1162 list_del(&driver->driver_list);
1163 }
1164 else
1165 info("USB Serial support registered for %s", driver->description);
1166
1167 return retval;
1168 }
1169
1170
1171 void usb_serial_deregister(struct usb_serial_driver *device)
1172 {
1173 info("USB Serial deregistering driver %s", device->description);
1174 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. */
1182 EXPORT_SYMBOL_GPL(usb_serial_register);
1183 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1184 EXPORT_SYMBOL_GPL(usb_serial_probe);
1185 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1186 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1187
1188
1189 /* Module information */
1190 MODULE_AUTHOR( DRIVER_AUTHOR );
1191 MODULE_DESCRIPTION( DRIVER_DESC );
1192 MODULE_LICENSE("GPL");
1193
1194 module_param(debug, bool, S_IRUGO | S_IWUSR);
1195 MODULE_PARM_DESC(debug, "Debug enabled or not");
This page took 0.066432 seconds and 5 git commands to generate.