[PATCH] USB: PL2303 and TIOCMIWAIT
[linux-2.6] / drivers / usb / serial / bus.c
1 /*
2  * USB Serial Converter Bus specific functions
3  *
4  * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com)
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License version
8  *      2 as published by the Free Software Foundation.
9  */
10
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/tty.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include "usb-serial.h"
18
19 static int usb_serial_device_match (struct device *dev, struct device_driver *drv)
20 {
21         struct usb_serial_driver *driver;
22         const struct usb_serial_port *port;
23
24         /*
25          * drivers are already assigned to ports in serial_probe so it's
26          * a simple check here.
27          */
28         port = to_usb_serial_port(dev);
29         if (!port)
30                 return 0;
31
32         driver = to_usb_serial_driver(drv);
33
34         if (driver == port->serial->type)
35                 return 1;
36
37         return 0;
38 }
39
40 static int usb_serial_device_probe (struct device *dev)
41 {
42         struct usb_serial_driver *driver;
43         struct usb_serial_port *port;
44         int retval = 0;
45         int minor;
46
47         port = to_usb_serial_port(dev);
48         if (!port) {
49                 retval = -ENODEV;
50                 goto exit;
51         }
52
53         driver = port->serial->type;
54         if (driver->port_probe) {
55                 if (!try_module_get(driver->driver.owner)) {
56                         dev_err(dev, "module get failed, exiting\n");
57                         retval = -EIO;
58                         goto exit;
59                 }
60                 retval = driver->port_probe (port);
61                 module_put(driver->driver.owner);
62                 if (retval)
63                         goto exit;
64         }
65
66         minor = port->number;
67         tty_register_device (usb_serial_tty_driver, minor, dev);
68         dev_info(&port->serial->dev->dev, 
69                  "%s converter now attached to ttyUSB%d\n",
70                  driver->description, minor);
71
72 exit:
73         return retval;
74 }
75
76 static int usb_serial_device_remove (struct device *dev)
77 {
78         struct usb_serial_driver *driver;
79         struct usb_serial_port *port;
80         int retval = 0;
81         int minor;
82
83         port = to_usb_serial_port(dev);
84         if (!port) {
85                 return -ENODEV;
86         }
87
88         driver = port->serial->type;
89         if (driver->port_remove) {
90                 if (!try_module_get(driver->driver.owner)) {
91                         dev_err(dev, "module get failed, exiting\n");
92                         retval = -EIO;
93                         goto exit;
94                 }
95                 retval = driver->port_remove (port);
96                 module_put(driver->driver.owner);
97         }
98 exit:
99         minor = port->number;
100         tty_unregister_device (usb_serial_tty_driver, minor);
101         dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
102                  driver->description, minor);
103
104         return retval;
105 }
106
107 struct bus_type usb_serial_bus_type = {
108         .name =         "usb-serial",
109         .match =        usb_serial_device_match,
110         .probe =        usb_serial_device_probe,
111         .remove =       usb_serial_device_remove,
112 };
113
114 int usb_serial_bus_register(struct usb_serial_driver *driver)
115 {
116         int retval;
117
118         driver->driver.bus = &usb_serial_bus_type;
119         retval = driver_register(&driver->driver);
120
121         return retval;
122 }
123
124 void usb_serial_bus_deregister(struct usb_serial_driver *driver)
125 {
126         driver_unregister(&driver->driver);
127 }
128