diff options
author | Greg Kroah-Hartman <gregkh@suse.de> | 2006-06-14 15:14:34 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2006-06-21 18:04:19 -0400 |
commit | 9bde7497e0b54178c317fac47a18be7f948dd471 (patch) | |
tree | 395a1c86801a1ba1bd04a007ffac3c5a73d9f27d /drivers | |
parent | 36679ea59846d8f34a48f71ca1a37671ca0ad3c5 (diff) |
[PATCH] USB: make endpoints real struct devices
This will allow for us to give endpoints a major/minor to create a
"usbfs2-like" way to access endpoints directly from userspace in an
easier manner than the current usbfs provides us.
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/usb/core/endpoint.c | 238 |
1 files changed, 150 insertions, 88 deletions
diff --git a/drivers/usb/core/endpoint.c b/drivers/usb/core/endpoint.c index 4c2fe8f723e5..247b5a4913a8 100644 --- a/drivers/usb/core/endpoint.c +++ b/drivers/usb/core/endpoint.c | |||
@@ -14,13 +14,14 @@ | |||
14 | #include "usb.h" | 14 | #include "usb.h" |
15 | 15 | ||
16 | /* endpoint stuff */ | 16 | /* endpoint stuff */ |
17 | struct ep_object { | 17 | |
18 | struct ep_device { | ||
18 | struct usb_endpoint_descriptor *desc; | 19 | struct usb_endpoint_descriptor *desc; |
19 | struct usb_device *udev; | 20 | struct usb_device *udev; |
20 | struct kobject kobj; | 21 | struct device dev; |
21 | }; | 22 | }; |
22 | #define to_ep_object(_kobj) \ | 23 | #define to_ep_device(_dev) \ |
23 | container_of(_kobj, struct ep_object, kobj) | 24 | container_of(_dev, struct ep_device, dev) |
24 | 25 | ||
25 | struct ep_attribute { | 26 | struct ep_attribute { |
26 | struct attribute attr; | 27 | struct attribute attr; |
@@ -30,40 +31,37 @@ struct ep_attribute { | |||
30 | #define to_ep_attribute(_attr) \ | 31 | #define to_ep_attribute(_attr) \ |
31 | container_of(_attr, struct ep_attribute, attr) | 32 | container_of(_attr, struct ep_attribute, attr) |
32 | 33 | ||
33 | #define EP_ATTR(_name) \ | ||
34 | struct ep_attribute ep_##_name = { \ | ||
35 | .attr = {.name = #_name, .owner = THIS_MODULE, \ | ||
36 | .mode = S_IRUGO}, \ | ||
37 | .show = show_ep_##_name} | ||
38 | |||
39 | #define usb_ep_attr(field, format_string) \ | 34 | #define usb_ep_attr(field, format_string) \ |
40 | static ssize_t show_ep_##field(struct usb_device *udev, \ | 35 | static ssize_t show_ep_##field(struct device *dev, \ |
41 | struct usb_endpoint_descriptor *desc, \ | 36 | struct device_attribute *attr, \ |
42 | char *buf) \ | 37 | char *buf) \ |
43 | { \ | 38 | { \ |
44 | return sprintf(buf, format_string, desc->field); \ | 39 | struct ep_device *ep = to_ep_device(dev); \ |
40 | return sprintf(buf, format_string, ep->desc->field); \ | ||
45 | } \ | 41 | } \ |
46 | static EP_ATTR(field); | 42 | static DEVICE_ATTR(field, S_IRUGO, show_ep_##field, NULL); |
47 | 43 | ||
48 | usb_ep_attr(bLength, "%02x\n") | 44 | usb_ep_attr(bLength, "%02x\n") |
49 | usb_ep_attr(bEndpointAddress, "%02x\n") | 45 | usb_ep_attr(bEndpointAddress, "%02x\n") |
50 | usb_ep_attr(bmAttributes, "%02x\n") | 46 | usb_ep_attr(bmAttributes, "%02x\n") |
51 | usb_ep_attr(bInterval, "%02x\n") | 47 | usb_ep_attr(bInterval, "%02x\n") |
52 | 48 | ||
53 | static ssize_t show_ep_wMaxPacketSize(struct usb_device *udev, | 49 | static ssize_t show_ep_wMaxPacketSize(struct device *dev, |
54 | struct usb_endpoint_descriptor *desc, char *buf) | 50 | struct device_attribute *attr, char *buf) |
55 | { | 51 | { |
52 | struct ep_device *ep = to_ep_device(dev); | ||
56 | return sprintf(buf, "%04x\n", | 53 | return sprintf(buf, "%04x\n", |
57 | le16_to_cpu(desc->wMaxPacketSize) & 0x07ff); | 54 | le16_to_cpu(ep->desc->wMaxPacketSize) & 0x07ff); |
58 | } | 55 | } |
59 | static EP_ATTR(wMaxPacketSize); | 56 | static DEVICE_ATTR(wMaxPacketSize, S_IRUGO, show_ep_wMaxPacketSize, NULL); |
60 | 57 | ||
61 | static ssize_t show_ep_type(struct usb_device *udev, | 58 | static ssize_t show_ep_type(struct device *dev, struct device_attribute *attr, |
62 | struct usb_endpoint_descriptor *desc, char *buf) | 59 | char *buf) |
63 | { | 60 | { |
61 | struct ep_device *ep = to_ep_device(dev); | ||
64 | char *type = "unknown"; | 62 | char *type = "unknown"; |
65 | 63 | ||
66 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { | 64 | switch (ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
67 | case USB_ENDPOINT_XFER_CONTROL: | 65 | case USB_ENDPOINT_XFER_CONTROL: |
68 | type = "Control"; | 66 | type = "Control"; |
69 | break; | 67 | break; |
@@ -79,37 +77,38 @@ static ssize_t show_ep_type(struct usb_device *udev, | |||
79 | } | 77 | } |
80 | return sprintf(buf, "%s\n", type); | 78 | return sprintf(buf, "%s\n", type); |
81 | } | 79 | } |
82 | static EP_ATTR(type); | 80 | static DEVICE_ATTR(type, S_IRUGO, show_ep_type, NULL); |
83 | 81 | ||
84 | static ssize_t show_ep_interval(struct usb_device *udev, | 82 | static ssize_t show_ep_interval(struct device *dev, |
85 | struct usb_endpoint_descriptor *desc, char *buf) | 83 | struct device_attribute *attr, char *buf) |
86 | { | 84 | { |
85 | struct ep_device *ep = to_ep_device(dev); | ||
87 | char unit; | 86 | char unit; |
88 | unsigned interval = 0; | 87 | unsigned interval = 0; |
89 | unsigned in; | 88 | unsigned in; |
90 | 89 | ||
91 | in = (desc->bEndpointAddress & USB_DIR_IN); | 90 | in = (ep->desc->bEndpointAddress & USB_DIR_IN); |
92 | 91 | ||
93 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { | 92 | switch (ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
94 | case USB_ENDPOINT_XFER_CONTROL: | 93 | case USB_ENDPOINT_XFER_CONTROL: |
95 | if (udev->speed == USB_SPEED_HIGH) /* uframes per NAK */ | 94 | if (ep->udev->speed == USB_SPEED_HIGH) /* uframes per NAK */ |
96 | interval = desc->bInterval; | 95 | interval = ep->desc->bInterval; |
97 | break; | 96 | break; |
98 | case USB_ENDPOINT_XFER_ISOC: | 97 | case USB_ENDPOINT_XFER_ISOC: |
99 | interval = 1 << (desc->bInterval - 1); | 98 | interval = 1 << (ep->desc->bInterval - 1); |
100 | break; | 99 | break; |
101 | case USB_ENDPOINT_XFER_BULK: | 100 | case USB_ENDPOINT_XFER_BULK: |
102 | if (udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */ | 101 | if (ep->udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */ |
103 | interval = desc->bInterval; | 102 | interval = ep->desc->bInterval; |
104 | break; | 103 | break; |
105 | case USB_ENDPOINT_XFER_INT: | 104 | case USB_ENDPOINT_XFER_INT: |
106 | if (udev->speed == USB_SPEED_HIGH) | 105 | if (ep->udev->speed == USB_SPEED_HIGH) |
107 | interval = 1 << (desc->bInterval - 1); | 106 | interval = 1 << (ep->desc->bInterval - 1); |
108 | else | 107 | else |
109 | interval = desc->bInterval; | 108 | interval = ep->desc->bInterval; |
110 | break; | 109 | break; |
111 | } | 110 | } |
112 | interval *= (udev->speed == USB_SPEED_HIGH) ? 125 : 1000; | 111 | interval *= (ep->udev->speed == USB_SPEED_HIGH) ? 125 : 1000; |
113 | if (interval % 1000) | 112 | if (interval % 1000) |
114 | unit = 'u'; | 113 | unit = 'u'; |
115 | else { | 114 | else { |
@@ -119,95 +118,158 @@ static ssize_t show_ep_interval(struct usb_device *udev, | |||
119 | 118 | ||
120 | return sprintf(buf, "%d%cs\n", interval, unit); | 119 | return sprintf(buf, "%d%cs\n", interval, unit); |
121 | } | 120 | } |
122 | static EP_ATTR(interval); | 121 | static DEVICE_ATTR(interval, S_IRUGO, show_ep_interval, NULL); |
123 | 122 | ||
124 | static ssize_t show_ep_direction(struct usb_device *udev, | 123 | static ssize_t show_ep_direction(struct device *dev, |
125 | struct usb_endpoint_descriptor *desc, char *buf) | 124 | struct device_attribute *attr, char *buf) |
126 | { | 125 | { |
126 | struct ep_device *ep = to_ep_device(dev); | ||
127 | char *direction; | 127 | char *direction; |
128 | 128 | ||
129 | if ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == | 129 | if ((ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
130 | USB_ENDPOINT_XFER_CONTROL) | 130 | USB_ENDPOINT_XFER_CONTROL) |
131 | direction = "both"; | 131 | direction = "both"; |
132 | else if (desc->bEndpointAddress & USB_DIR_IN) | 132 | else if (ep->desc->bEndpointAddress & USB_DIR_IN) |
133 | direction = "in"; | 133 | direction = "in"; |
134 | else | 134 | else |
135 | direction = "out"; | 135 | direction = "out"; |
136 | return sprintf(buf, "%s\n", direction); | 136 | return sprintf(buf, "%s\n", direction); |
137 | } | 137 | } |
138 | static EP_ATTR(direction); | 138 | static DEVICE_ATTR(direction, S_IRUGO, show_ep_direction, NULL); |
139 | 139 | ||
140 | static struct attribute *ep_attrs[] = { | 140 | static struct attribute *ep_dev_attrs[] = { |
141 | &ep_bLength.attr, | 141 | &dev_attr_bLength.attr, |
142 | &ep_bEndpointAddress.attr, | 142 | &dev_attr_bEndpointAddress.attr, |
143 | &ep_bmAttributes.attr, | 143 | &dev_attr_bmAttributes.attr, |
144 | &ep_bInterval.attr, | 144 | &dev_attr_bInterval.attr, |
145 | &ep_wMaxPacketSize.attr, | 145 | &dev_attr_wMaxPacketSize.attr, |
146 | &ep_type.attr, | 146 | &dev_attr_interval.attr, |
147 | &ep_interval.attr, | 147 | &dev_attr_type.attr, |
148 | &ep_direction.attr, | 148 | &dev_attr_direction.attr, |
149 | NULL, | 149 | NULL, |
150 | }; | 150 | }; |
151 | static struct attribute_group ep_dev_attr_grp = { | ||
152 | .attrs = ep_dev_attrs, | ||
153 | }; | ||
151 | 154 | ||
152 | static void ep_object_release(struct kobject *kobj) | 155 | static struct endpoint_class { |
156 | struct kref kref; | ||
157 | struct class *class; | ||
158 | } *ep_class; | ||
159 | |||
160 | static int init_endpoint_class(void) | ||
153 | { | 161 | { |
154 | kfree(to_ep_object(kobj)); | 162 | int result = 0; |
163 | |||
164 | if (ep_class != NULL) { | ||
165 | kref_get(&ep_class->kref); | ||
166 | goto exit; | ||
167 | } | ||
168 | |||
169 | ep_class = kmalloc(sizeof(*ep_class), GFP_KERNEL); | ||
170 | if (!ep_class) { | ||
171 | result = -ENOMEM; | ||
172 | goto exit; | ||
173 | } | ||
174 | |||
175 | kref_init(&ep_class->kref); | ||
176 | ep_class->class = class_create(THIS_MODULE, "usb_endpoint"); | ||
177 | if (IS_ERR(ep_class->class)) { | ||
178 | result = IS_ERR(ep_class->class); | ||
179 | kfree(ep_class); | ||
180 | ep_class = NULL; | ||
181 | goto exit; | ||
182 | } | ||
183 | |||
184 | exit: | ||
185 | return result; | ||
155 | } | 186 | } |
156 | 187 | ||
157 | static ssize_t ep_object_show(struct kobject *kobj, struct attribute *attr, | 188 | static void release_endpoint_class(struct kref *kref) |
158 | char *buf) | ||
159 | { | 189 | { |
160 | struct ep_object *ep_obj = to_ep_object(kobj); | 190 | /* Ok, we cheat as we know we only have one ep_class */ |
161 | struct ep_attribute *ep_attr = to_ep_attribute(attr); | 191 | class_destroy(ep_class->class); |
192 | kfree(ep_class); | ||
193 | ep_class = NULL; | ||
194 | } | ||
162 | 195 | ||
163 | return (ep_attr->show)(ep_obj->udev, ep_obj->desc, buf); | 196 | static void destroy_endpoint_class(void) |
197 | { | ||
198 | if (ep_class) | ||
199 | kref_put(&ep_class->kref, release_endpoint_class); | ||
164 | } | 200 | } |
165 | 201 | ||
166 | static struct sysfs_ops ep_object_sysfs_ops = { | 202 | static void ep_device_release(struct device *dev) |
167 | .show = ep_object_show, | 203 | { |
168 | }; | 204 | struct ep_device *ep_dev = to_ep_device(dev); |
169 | 205 | ||
170 | static struct kobj_type ep_object_ktype = { | 206 | dev_dbg(dev, "%s called for %s\n", __FUNCTION__, dev->bus_id); |
171 | .release = ep_object_release, | 207 | kfree(ep_dev); |
172 | .sysfs_ops = &ep_object_sysfs_ops, | 208 | } |
173 | .default_attrs = ep_attrs, | ||
174 | }; | ||
175 | 209 | ||
176 | void usb_create_ep_files(struct device *parent, | 210 | void usb_create_ep_files(struct device *parent, |
177 | struct usb_host_endpoint *endpoint, | 211 | struct usb_host_endpoint *endpoint, |
178 | struct usb_device *udev) | 212 | struct usb_device *udev) |
179 | { | 213 | { |
180 | struct ep_object *ep_obj; | 214 | char name[8]; |
181 | struct kobject *kobj; | 215 | struct ep_device *ep_dev; |
216 | int minor; | ||
217 | int retval; | ||
218 | |||
219 | retval = init_endpoint_class(); | ||
220 | if (retval) | ||
221 | goto exit; | ||
182 | 222 | ||
183 | ep_obj = kzalloc(sizeof(struct ep_object), GFP_KERNEL); | 223 | ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL); |
184 | if (!ep_obj) | 224 | if (!ep_dev) { |
185 | return; | 225 | retval = -ENOMEM; |
226 | goto exit; | ||
227 | } | ||
186 | 228 | ||
187 | ep_obj->desc = &endpoint->desc; | 229 | /* fun calculation to determine the minor of this endpoint */ |
188 | ep_obj->udev = udev; | 230 | minor = (((udev->bus->busnum - 1) * 128) * 16) + (udev->devnum - 1); |
189 | 231 | ||
190 | kobj = &ep_obj->kobj; | 232 | ep_dev->desc = &endpoint->desc; |
191 | kobject_set_name(kobj, "ep_%02x", endpoint->desc.bEndpointAddress); | 233 | ep_dev->udev = udev; |
192 | kobj->parent = &parent->kobj; | 234 | ep_dev->dev.devt = MKDEV(442, minor); // FIXME fake number... |
193 | kobj->ktype = &ep_object_ktype; | 235 | ep_dev->dev.class = ep_class->class; |
236 | ep_dev->dev.parent = parent; | ||
237 | ep_dev->dev.release = ep_device_release; | ||
238 | snprintf(ep_dev->dev.bus_id, BUS_ID_SIZE, "usbdev%d.%d_ep%02x", | ||
239 | udev->bus->busnum, udev->devnum, | ||
240 | endpoint->desc.bEndpointAddress); | ||
194 | 241 | ||
195 | /* Don't use kobject_register, because it generates a hotplug event */ | 242 | retval = device_register(&ep_dev->dev); |
196 | kobject_init(kobj); | 243 | if (retval) |
197 | if (kobject_add(kobj) == 0) | 244 | goto error; |
198 | endpoint->kobj = kobj; | 245 | sysfs_create_group(&ep_dev->dev.kobj, &ep_dev_attr_grp); |
199 | else | 246 | |
200 | kobject_put(kobj); | 247 | endpoint->ep_dev = ep_dev; |
248 | |||
249 | /* create the symlink to the old-style "ep_XX" directory */ | ||
250 | sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress); | ||
251 | sysfs_create_link(&parent->kobj, &endpoint->ep_dev->dev.kobj, name); | ||
252 | |||
253 | exit: | ||
254 | return; | ||
255 | error: | ||
256 | kfree(ep_dev); | ||
257 | return; | ||
201 | } | 258 | } |
202 | 259 | ||
203 | void usb_remove_ep_files(struct usb_host_endpoint *endpoint) | 260 | void usb_remove_ep_files(struct usb_host_endpoint *endpoint) |
204 | { | 261 | { |
205 | 262 | ||
206 | if (endpoint->kobj) { | 263 | if (endpoint->ep_dev) { |
207 | kobject_del(endpoint->kobj); | 264 | char name[8]; |
208 | kobject_put(endpoint->kobj); | 265 | |
209 | endpoint->kobj = NULL; | 266 | sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress); |
267 | sysfs_remove_link(&endpoint->ep_dev->dev.parent->kobj, name); | ||
268 | sysfs_remove_group(&endpoint->ep_dev->dev.kobj, &ep_dev_attr_grp); | ||
269 | device_unregister(&endpoint->ep_dev->dev); | ||
270 | endpoint->ep_dev = NULL; | ||
210 | } | 271 | } |
272 | destroy_endpoint_class(); | ||
211 | } | 273 | } |
212 | 274 | ||
213 | 275 | ||