GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / usb / wusbcore / rh.c
blobec2025a4b77828efac4261bc527b4aac4d4c35b8
2 #include <linux/slab.h>
3 #include "wusbhc.h"
5 /*
6 * Reset a fake port
8 * Using a Reset Device IE is too heavyweight as it causes the device
9 * to enter the UnConnected state and leave the cluster, this can mean
10 * that when the device reconnects it is connected to a different fake
11 * port.
13 * Instead, reset authenticated devices with a SetAddress(0), followed
14 * by a SetAddresss(AuthAddr).
16 * For unauthenticated devices just pretend to reset but do nothing.
17 * If the device initialization continues to fail it will eventually
18 * time out after TrustTimeout and enter the UnConnected state.
20 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
22 * Supposedly we are the only thread accesing @wusbhc->port; in any
23 * case, maybe we should move the mutex locking from
24 * wusbhc_devconnect_auth() to here.
26 * @port_idx refers to the wusbhc's port index, not the USB port number
28 static int wusbhc_rh_port_reset(struct wusbhc *wusbhc, u8 port_idx)
30 int result = 0;
31 struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
32 struct wusb_dev *wusb_dev = port->wusb_dev;
34 if (wusb_dev == NULL)
35 return -ENOTCONN;
37 port->status |= USB_PORT_STAT_RESET;
38 port->change |= USB_PORT_STAT_C_RESET;
40 if (wusb_dev->addr & WUSB_DEV_ADDR_UNAUTH)
41 result = 0;
42 else
43 result = wusb_dev_update_address(wusbhc, wusb_dev);
45 port->status &= ~USB_PORT_STAT_RESET;
46 port->status |= USB_PORT_STAT_ENABLE;
47 port->change |= USB_PORT_STAT_C_RESET | USB_PORT_STAT_C_ENABLE;
49 return result;
53 * Return the hub change status bitmap
55 * The bits in the change status bitmap are cleared when a
56 * ClearPortFeature request is issued (USB2.0[11.12.3,11.12.4].
58 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
60 * WARNING!! This gets called from atomic context; we cannot get the
61 * mutex--the only race condition we can find is some bit
62 * changing just after we copy it, which shouldn't be too
63 * big of a problem [and we can't make it an spinlock
64 * because other parts need to take it and sleep] .
66 * @usb_hcd is refcounted, so it won't dissapear under us
67 * and before killing a host, the polling of the root hub
68 * would be stopped anyway.
70 int wusbhc_rh_status_data(struct usb_hcd *usb_hcd, char *_buf)
72 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
73 size_t cnt, size;
74 unsigned long *buf = (unsigned long *) _buf;
76 /* WE DON'T LOCK, see comment */
77 size = wusbhc->ports_max + 1 /* hub bit */;
78 size = (size + 8 - 1) / 8; /* round to bytes */
79 for (cnt = 0; cnt < wusbhc->ports_max; cnt++)
80 if (wusb_port_by_idx(wusbhc, cnt)->change)
81 set_bit(cnt + 1, buf);
82 else
83 clear_bit(cnt + 1, buf);
84 return size;
86 EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
89 * Return the hub's desciptor
91 * NOTE: almost cut and paste from ehci-hub.c
93 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked
95 static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
96 u16 wIndex,
97 struct usb_hub_descriptor *descr,
98 u16 wLength)
100 u16 temp = 1 + (wusbhc->ports_max / 8);
101 u8 length = 7 + 2 * temp;
103 if (wLength < length)
104 return -ENOSPC;
105 descr->bDescLength = 7 + 2 * temp;
106 descr->bDescriptorType = 0x29; /* HUB type */
107 descr->bNbrPorts = wusbhc->ports_max;
108 descr->wHubCharacteristics = cpu_to_le16(
109 0x00 /* All ports power at once */
110 | 0x00 /* not part of compound device */
111 | 0x10 /* No overcurrent protection */
112 | 0x00
113 | 0x00); /* No port indicators */
114 descr->bPwrOn2PwrGood = 0;
115 descr->bHubContrCurrent = 0;
116 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
117 memset(&descr->bitmap[0], 0, temp);
118 memset(&descr->bitmap[temp], 0xff, temp);
119 return 0;
123 * Clear a hub feature
125 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
127 * Nothing to do, so no locking needed ;)
129 static int wusbhc_rh_clear_hub_feat(struct wusbhc *wusbhc, u16 feature)
131 int result;
133 switch (feature) {
134 case C_HUB_LOCAL_POWER:
135 case C_HUB_OVER_CURRENT:
136 result = 0;
137 break;
138 default:
139 result = -EPIPE;
141 return result;
145 * Return hub status (it is always zero...)
147 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
149 * Nothing to do, so no locking needed ;)
151 static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
152 u16 wLength)
154 *buf = 0;
155 return 0;
159 * Set a port feature
161 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
163 static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
164 u8 selector, u8 port_idx)
166 struct device *dev = wusbhc->dev;
168 if (port_idx > wusbhc->ports_max)
169 return -EINVAL;
171 switch (feature) {
172 /* According to USB2.0[11.24.2.13]p2, these features
173 * are not required to be implemented. */
174 case USB_PORT_FEAT_C_OVER_CURRENT:
175 case USB_PORT_FEAT_C_ENABLE:
176 case USB_PORT_FEAT_C_SUSPEND:
177 case USB_PORT_FEAT_C_CONNECTION:
178 case USB_PORT_FEAT_C_RESET:
179 return 0;
180 case USB_PORT_FEAT_POWER:
181 /* No such thing, but we fake it works */
182 mutex_lock(&wusbhc->mutex);
183 wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
184 mutex_unlock(&wusbhc->mutex);
185 return 0;
186 case USB_PORT_FEAT_RESET:
187 return wusbhc_rh_port_reset(wusbhc, port_idx);
188 case USB_PORT_FEAT_ENABLE:
189 case USB_PORT_FEAT_SUSPEND:
190 dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
191 port_idx, feature, selector);
192 return -ENOSYS;
193 default:
194 dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
195 port_idx, feature, selector);
196 return -EPIPE;
199 return 0;
203 * Clear a port feature...
205 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
207 static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
208 u8 selector, u8 port_idx)
210 int result = 0;
211 struct device *dev = wusbhc->dev;
213 if (port_idx > wusbhc->ports_max)
214 return -EINVAL;
216 mutex_lock(&wusbhc->mutex);
217 switch (feature) {
218 case USB_PORT_FEAT_POWER: /* fake port always on */
219 /* According to USB2.0[11.24.2.7.1.4], no need to implement? */
220 case USB_PORT_FEAT_C_OVER_CURRENT:
221 break;
222 case USB_PORT_FEAT_C_RESET:
223 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
224 break;
225 case USB_PORT_FEAT_C_CONNECTION:
226 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
227 break;
228 case USB_PORT_FEAT_ENABLE:
229 __wusbhc_dev_disable(wusbhc, port_idx);
230 break;
231 case USB_PORT_FEAT_C_ENABLE:
232 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
233 break;
234 case USB_PORT_FEAT_SUSPEND:
235 case USB_PORT_FEAT_C_SUSPEND:
236 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
237 port_idx, feature, selector);
238 result = -ENOSYS;
239 break;
240 default:
241 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
242 port_idx, feature, selector);
243 result = -EPIPE;
244 break;
246 mutex_unlock(&wusbhc->mutex);
248 return result;
252 * Return the port's status
254 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
256 static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
257 u32 *_buf, u16 wLength)
259 __le16 *buf = (__le16 *)_buf;
261 if (port_idx > wusbhc->ports_max)
262 return -EINVAL;
264 mutex_lock(&wusbhc->mutex);
265 buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
266 buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
267 mutex_unlock(&wusbhc->mutex);
269 return 0;
273 * Entry point for Root Hub operations
275 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
277 int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
278 u16 wIndex, char *buf, u16 wLength)
280 int result = -ENOSYS;
281 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
283 switch (reqntype) {
284 case GetHubDescriptor:
285 result = wusbhc_rh_get_hub_descr(
286 wusbhc, wValue, wIndex,
287 (struct usb_hub_descriptor *) buf, wLength);
288 break;
289 case ClearHubFeature:
290 result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
291 break;
292 case GetHubStatus:
293 result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
294 break;
296 case SetPortFeature:
297 result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
298 (wIndex & 0xff) - 1);
299 break;
300 case ClearPortFeature:
301 result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
302 (wIndex & 0xff) - 1);
303 break;
304 case GetPortStatus:
305 result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
306 (u32 *)buf, wLength);
307 break;
309 case SetHubFeature:
310 default:
311 dev_err(wusbhc->dev, "%s (%p [%p], %x, %x, %x, %p, %x) "
312 "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
313 wValue, wIndex, buf, wLength);
314 /* dump_stack(); */
315 result = -ENOSYS;
317 return result;
319 EXPORT_SYMBOL_GPL(wusbhc_rh_control);
321 int wusbhc_rh_suspend(struct usb_hcd *usb_hcd)
323 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
324 dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
325 usb_hcd, wusbhc);
326 /* dump_stack(); */
327 return -ENOSYS;
329 EXPORT_SYMBOL_GPL(wusbhc_rh_suspend);
331 int wusbhc_rh_resume(struct usb_hcd *usb_hcd)
333 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
334 dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
335 usb_hcd, wusbhc);
336 /* dump_stack(); */
337 return -ENOSYS;
339 EXPORT_SYMBOL_GPL(wusbhc_rh_resume);
341 int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
343 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
344 dev_err(wusbhc->dev, "%s (%p [%p], port_idx %u) UNIMPLEMENTED\n",
345 __func__, usb_hcd, wusbhc, port_idx);
346 WARN_ON(1);
347 return -ENOSYS;
349 EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
351 static void wusb_port_init(struct wusb_port *port)
353 port->status |= USB_PORT_STAT_HIGH_SPEED;
357 * Alloc fake port specific fields and status.
359 int wusbhc_rh_create(struct wusbhc *wusbhc)
361 int result = -ENOMEM;
362 size_t port_size, itr;
363 port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
364 wusbhc->port = kzalloc(port_size, GFP_KERNEL);
365 if (wusbhc->port == NULL)
366 goto error_port_alloc;
367 for (itr = 0; itr < wusbhc->ports_max; itr++)
368 wusb_port_init(&wusbhc->port[itr]);
369 result = 0;
370 error_port_alloc:
371 return result;
374 void wusbhc_rh_destroy(struct wusbhc *wusbhc)
376 kfree(wusbhc->port);