blob: 7f547dc3a5903fd25e0837441115007320ae122d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/tty.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070017#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Alan Coxc97611c2008-07-22 11:09:48 +010019static int usb_serial_device_match(struct device *dev,
20 struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070022 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 const struct usb_serial_port *port;
24
25 /*
26 * drivers are already assigned to ports in serial_probe so it's
27 * a simple check here.
28 */
29 port = to_usb_serial_port(dev);
30 if (!port)
31 return 0;
32
33 driver = to_usb_serial_driver(drv);
34
35 if (driver == port->serial->type)
36 return 1;
37
38 return 0;
39}
40
Roman Kagance0d9322007-08-24 20:22:52 +040041static ssize_t show_port_number(struct device *dev,
42 struct device_attribute *attr, char *buf)
43{
44 struct usb_serial_port *port = to_usb_serial_port(dev);
45
46 return sprintf(buf, "%d\n", port->number - port->serial->minor);
47}
48
49static DEVICE_ATTR(port_number, S_IRUGO, show_port_number, NULL);
50
Alan Coxc97611c2008-07-22 11:09:48 +010051static int usb_serial_device_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070053 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct usb_serial_port *port;
55 int retval = 0;
56 int minor;
57
58 port = to_usb_serial_port(dev);
59 if (!port) {
60 retval = -ENODEV;
61 goto exit;
62 }
Alan Sternc706ebd2009-06-02 11:54:11 -040063 if (port->dev_state != PORT_REGISTERING)
64 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 driver = port->serial->type;
67 if (driver->port_probe) {
Alan Coxc97611c2008-07-22 11:09:48 +010068 retval = driver->port_probe(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (retval)
70 goto exit;
71 }
72
Roman Kagance0d9322007-08-24 20:22:52 +040073 retval = device_create_file(dev, &dev_attr_port_number);
Alan Sternc706ebd2009-06-02 11:54:11 -040074 if (retval) {
75 if (driver->port_remove)
76 retval = driver->port_remove(port);
Roman Kagance0d9322007-08-24 20:22:52 +040077 goto exit;
Alan Sternc706ebd2009-06-02 11:54:11 -040078 }
Roman Kagance0d9322007-08-24 20:22:52 +040079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 minor = port->number;
Alan Coxc97611c2008-07-22 11:09:48 +010081 tty_register_device(usb_serial_tty_driver, minor, dev);
82 dev_info(&port->serial->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 "%s converter now attached to ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070084 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86exit:
87 return retval;
88}
89
Alan Coxc97611c2008-07-22 11:09:48 +010090static int usb_serial_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070092 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct usb_serial_port *port;
94 int retval = 0;
95 int minor;
96
97 port = to_usb_serial_port(dev);
Alan Coxc97611c2008-07-22 11:09:48 +010098 if (!port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Alan Sternc706ebd2009-06-02 11:54:11 -0400101 if (port->dev_state != PORT_UNREGISTERING)
102 return retval;
103
Roman Kagance0d9322007-08-24 20:22:52 +0400104 device_remove_file(&port->dev, &dev_attr_port_number);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 driver = port->serial->type;
Alan Sternc706ebd2009-06-02 11:54:11 -0400107 if (driver->port_remove)
Alan Coxc97611c2008-07-22 11:09:48 +0100108 retval = driver->port_remove(port);
Alan Sternc706ebd2009-06-02 11:54:11 -0400109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 minor = port->number;
Alan Coxc97611c2008-07-22 11:09:48 +0100111 tty_unregister_device(usb_serial_tty_driver, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700113 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return retval;
116}
117
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100118#ifdef CONFIG_HOTPLUG
119static ssize_t store_new_id(struct device_driver *driver,
120 const char *buf, size_t count)
121{
122 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
123 ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count);
124
125 if (retval >= 0 && usb_drv->usb_driver != NULL)
126 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
127 &usb_drv->usb_driver->drvwrap.driver,
128 buf, count);
129 return retval;
130}
131
132static struct driver_attribute drv_attrs[] = {
133 __ATTR(new_id, S_IWUSR, NULL, store_new_id),
134 __ATTR_NULL,
135};
136
137static void free_dynids(struct usb_serial_driver *drv)
138{
139 struct usb_dynid *dynid, *n;
140
141 spin_lock(&drv->dynids.lock);
142 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
143 list_del(&dynid->node);
144 kfree(dynid);
145 }
146 spin_unlock(&drv->dynids.lock);
147}
148
149#else
150static struct driver_attribute drv_attrs[] = {
151 __ATTR_NULL,
152};
Satyam Sharma03fc3702007-09-03 01:42:43 +0530153static inline void free_dynids(struct usb_serial_driver *drv)
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100154{
155}
156#endif
157
Russell Kingac33bc32006-01-05 14:43:11 +0000158struct bus_type usb_serial_bus_type = {
159 .name = "usb-serial",
160 .match = usb_serial_device_match,
161 .probe = usb_serial_device_probe,
162 .remove = usb_serial_device_remove,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100163 .drv_attrs = drv_attrs,
Russell Kingac33bc32006-01-05 14:43:11 +0000164};
165
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700166int usb_serial_bus_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int retval;
169
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700170 driver->driver.bus = &usb_serial_bus_type;
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100171 spin_lock_init(&driver->dynids.lock);
172 INIT_LIST_HEAD(&driver->dynids.list);
173
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700174 retval = driver_register(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return retval;
177}
178
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700179void usb_serial_bus_deregister(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100181 free_dynids(driver);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700182 driver_unregister(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184