2 #include <linux/slab.h>
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
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
)
31 struct wusb_port
*port
= wusb_port_by_idx(wusbhc
, port_idx
);
32 struct wusb_dev
*wusb_dev
= port
->wusb_dev
;
37 port
->status
|= USB_PORT_STAT_RESET
;
38 port
->change
|= USB_PORT_STAT_C_RESET
;
40 if (wusb_dev
->addr
& WUSB_DEV_ADDR_UNAUTH
)
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
;
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
);
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
);
83 clear_bit(cnt
+ 1, buf
);
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
,
97 struct usb_hub_descriptor
*descr
,
100 u16 temp
= 1 + (wusbhc
->ports_max
/ 8);
101 u8 length
= 7 + 2 * temp
;
103 if (wLength
< length
)
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 */
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
);
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
)
134 case C_HUB_LOCAL_POWER
:
135 case C_HUB_OVER_CURRENT
:
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
,
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
)
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
:
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
);
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
);
194 dev_err(dev
, "(port_idx %d) set feat %d/%d UNKNOWN\n",
195 port_idx
, feature
, selector
);
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
)
211 struct device
*dev
= wusbhc
->dev
;
213 if (port_idx
> wusbhc
->ports_max
)
216 mutex_lock(&wusbhc
->mutex
);
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
:
222 case USB_PORT_FEAT_C_RESET
:
223 wusb_port_by_idx(wusbhc
, port_idx
)->change
&= ~USB_PORT_STAT_C_RESET
;
225 case USB_PORT_FEAT_C_CONNECTION
:
226 wusb_port_by_idx(wusbhc
, port_idx
)->change
&= ~USB_PORT_STAT_C_CONNECTION
;
228 case USB_PORT_FEAT_ENABLE
:
229 __wusbhc_dev_disable(wusbhc
, port_idx
);
231 case USB_PORT_FEAT_C_ENABLE
:
232 wusb_port_by_idx(wusbhc
, port_idx
)->change
&= ~USB_PORT_STAT_C_ENABLE
;
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
);
241 dev_err(dev
, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
242 port_idx
, feature
, selector
);
246 mutex_unlock(&wusbhc
->mutex
);
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
)
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
);
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
);
284 case GetHubDescriptor
:
285 result
= wusbhc_rh_get_hub_descr(
286 wusbhc
, wValue
, wIndex
,
287 (struct usb_hub_descriptor
*) buf
, wLength
);
289 case ClearHubFeature
:
290 result
= wusbhc_rh_clear_hub_feat(wusbhc
, wValue
);
293 result
= wusbhc_rh_get_hub_status(wusbhc
, (u32
*)buf
, wLength
);
297 result
= wusbhc_rh_set_port_feat(wusbhc
, wValue
, wIndex
>> 8,
298 (wIndex
& 0xff) - 1);
300 case ClearPortFeature
:
301 result
= wusbhc_rh_clear_port_feat(wusbhc
, wValue
, wIndex
>> 8,
302 (wIndex
& 0xff) - 1);
305 result
= wusbhc_rh_get_port_status(wusbhc
, wIndex
- 1,
306 (u32
*)buf
, wLength
);
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
);
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__
,
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__
,
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
);
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
]);
374 void wusbhc_rh_destroy(struct wusbhc
*wusbhc
)