]>
Commit | Line | Data |
---|---|---|
034d7c13 KM |
1 | /* |
2 | * Renesas USB driver | |
3 | * | |
4 | * Copyright (C) 2011 Renesas Solutions Corp. | |
5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this program; if not, write to the Free Software | |
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
15 | * | |
16 | */ | |
17 | #include <linux/io.h> | |
18 | #include <linux/list.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/platform_device.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/usb.h> | |
23 | #include <linux/usb/hcd.h> | |
24 | #include "common.h" | |
25 | ||
26 | /* | |
27 | *** HARDWARE LIMITATION *** | |
28 | * | |
29 | * 1) renesas_usbhs has a limited number of controllable devices. | |
30 | * it can control only 9 devices in generally. | |
31 | * see DEVADDn / DCPMAXP / PIPEMAXP. | |
32 | * | |
33 | * 2) renesas_usbhs pipe number is limited. | |
34 | * the pipe will be re-used for each devices. | |
35 | * so, software should control DATA0/1 sequence of each devices. | |
36 | */ | |
37 | ||
38 | ||
39 | /* | |
40 | * image of mod_host | |
41 | * | |
42 | * +--------+ | |
43 | * | udev 0 | --> it is used when set address | |
44 | * +--------+ | |
45 | * | |
46 | * +--------+ pipes are reused for each uep. | |
47 | * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when | |
48 | * +--------+ | | target device was changed | |
49 | * +- [uep 1 (bulk)] --|---+ +--------------+ | |
50 | * | +--------------> | pipe0 (dcp) | | |
51 | * +- [uep 2 (bulk)] --|---|---+ +--------------+ | |
52 | * | | | | pipe1 (isoc) | | |
53 | * +--------+ | | | +--------------+ | |
54 | * | udev 2 |-+- [uep 0 (dcp) ] --+ +-- |------> | pipe2 (bulk) | | |
55 | * +--------+ | | | | +--------------+ | |
56 | * +- [uep 1 (int) ] --|-+ | +------> | pipe3 (bulk) | | |
57 | * | | | | +--------------+ | |
58 | * +--------+ | +-|---|------> | pipe4 (int) | | |
59 | * | udev 3 |-+- [uep 0 (dcp) ] --+ | | +--------------+ | |
60 | * +--------+ | | | | .... | | |
61 | * +- [uep 1 (bulk)] ------+ | | .... | | |
62 | * | | | |
63 | * +- [uep 2 (bulk)]-----------+ | |
64 | */ | |
65 | ||
66 | ||
67 | /* | |
68 | * struct | |
69 | */ | |
70 | struct usbhsh_pipe_info { | |
71 | unsigned int usr_cnt; /* see usbhsh_endpoint_alloc() */ | |
72 | }; | |
73 | ||
74 | struct usbhsh_request { | |
75 | struct urb *urb; | |
76 | struct usbhs_pkt pkt; | |
034d7c13 KM |
77 | }; |
78 | ||
79 | struct usbhsh_device { | |
80 | struct usb_device *usbv; | |
81 | struct list_head ep_list_head; /* list of usbhsh_ep */ | |
82 | }; | |
83 | ||
84 | struct usbhsh_ep { | |
85 | struct usbhs_pipe *pipe; | |
86 | struct usbhsh_device *udev; /* attached udev */ | |
87 | struct list_head ep_list; /* list to usbhsh_device */ | |
88 | ||
89 | int maxp; | |
90 | }; | |
91 | ||
92 | #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */ | |
93 | #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */ | |
94 | struct usbhsh_hpriv { | |
95 | struct usbhs_mod mod; | |
96 | struct usbhs_pipe *dcp; | |
97 | ||
98 | struct usbhsh_device udev[USBHSH_DEVICE_MAX]; | |
99 | ||
100 | struct usbhsh_pipe_info *pipe_info; | |
101 | int pipe_size; | |
102 | ||
103 | u32 port_stat; /* USB_PORT_STAT_xxx */ | |
104 | ||
7fccd480 | 105 | struct completion setup_ack_done; |
034d7c13 KM |
106 | }; |
107 | ||
108 | ||
109 | static const char usbhsh_hcd_name[] = "renesas_usbhs host"; | |
110 | ||
111 | /* | |
112 | * macro | |
113 | */ | |
114 | #define usbhsh_priv_to_hpriv(priv) \ | |
115 | container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod) | |
116 | ||
117 | #define __usbhsh_for_each_hpipe(start, pos, h, i) \ | |
118 | for (i = start, pos = (h)->hpipe + i; \ | |
119 | i < (h)->hpipe_size; \ | |
120 | i++, pos = (h)->hpipe + i) | |
121 | ||
122 | #define usbhsh_for_each_hpipe(pos, hpriv, i) \ | |
123 | __usbhsh_for_each_hpipe(1, pos, hpriv, i) | |
124 | ||
125 | #define usbhsh_for_each_hpipe_with_dcp(pos, hpriv, i) \ | |
126 | __usbhsh_for_each_hpipe(0, pos, hpriv, i) | |
127 | ||
128 | #define __usbhsh_for_each_udev(start, pos, h, i) \ | |
129 | for (i = start, pos = (h)->udev + i; \ | |
130 | i < USBHSH_DEVICE_MAX; \ | |
131 | i++, pos = (h)->udev + i) | |
132 | ||
133 | #define usbhsh_for_each_udev(pos, hpriv, i) \ | |
134 | __usbhsh_for_each_udev(1, pos, hpriv, i) | |
135 | ||
136 | #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \ | |
137 | __usbhsh_for_each_udev(0, pos, hpriv, i) | |
138 | ||
139 | #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv) | |
140 | #define usbhsh_hcd_to_dev(h) ((h)->self.controller) | |
141 | ||
142 | #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv) | |
143 | #define usbhsh_hpriv_to_dcp(h) ((h)->dcp) | |
144 | #define usbhsh_hpriv_to_hcd(h) \ | |
145 | container_of((void *)h, struct usb_hcd, hcd_priv) | |
146 | ||
147 | #define usbhsh_ep_to_uep(u) ((u)->hcpriv) | |
148 | #define usbhsh_uep_to_pipe(u) ((u)->pipe) | |
149 | #define usbhsh_uep_to_udev(u) ((u)->udev) | |
150 | #define usbhsh_urb_to_ureq(u) ((u)->hcpriv) | |
151 | #define usbhsh_urb_to_usbv(u) ((u)->dev) | |
152 | ||
153 | #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev) | |
154 | ||
155 | #define usbhsh_udev_to_usbv(h) ((h)->usbv) | |
ab142308 | 156 | #define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h) |
034d7c13 KM |
157 | |
158 | #define usbhsh_pipe_info(p) ((p)->mod_private) | |
159 | ||
9c673652 KM |
160 | #define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent)) |
161 | #define usbhsh_device_hubport(d) ((d)->usbv->portnum) | |
034d7c13 KM |
162 | #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev)) |
163 | #define usbhsh_device_nth(h, d) ((h)->udev + d) | |
164 | #define usbhsh_device0(h) usbhsh_device_nth(h, 0) | |
165 | ||
166 | #define usbhsh_port_stat_init(h) ((h)->port_stat = 0) | |
167 | #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s)) | |
168 | #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s)) | |
169 | #define usbhsh_port_stat_get(h) ((h)->port_stat) | |
170 | ||
25234b46 | 171 | #define usbhsh_pkt_to_ureq(p) \ |
034d7c13 KM |
172 | container_of((void *)p, struct usbhsh_request, pkt) |
173 | ||
174 | /* | |
175 | * req alloc/free | |
176 | */ | |
25234b46 | 177 | static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv, |
034d7c13 KM |
178 | struct urb *urb, |
179 | gfp_t mem_flags) | |
180 | { | |
181 | struct usbhsh_request *ureq; | |
182 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
183 | struct device *dev = usbhs_priv_to_dev(priv); | |
184 | ||
c5b963f8 | 185 | ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags); |
034d7c13 KM |
186 | if (!ureq) { |
187 | dev_err(dev, "ureq alloc fail\n"); | |
188 | return NULL; | |
189 | } | |
190 | ||
191 | usbhs_pkt_init(&ureq->pkt); | |
034d7c13 | 192 | ureq->urb = urb; |
fc9d5c79 | 193 | usbhsh_urb_to_ureq(urb) = ureq; |
034d7c13 KM |
194 | |
195 | return ureq; | |
196 | } | |
197 | ||
25234b46 | 198 | static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv, |
034d7c13 KM |
199 | struct usbhsh_request *ureq) |
200 | { | |
fc9d5c79 | 201 | usbhsh_urb_to_ureq(ureq->urb) = NULL; |
034d7c13 | 202 | ureq->urb = NULL; |
c5b963f8 KM |
203 | |
204 | kfree(ureq); | |
034d7c13 KM |
205 | } |
206 | ||
207 | /* | |
208 | * device control | |
209 | */ | |
9c673652 KM |
210 | static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd, |
211 | struct usbhsh_device *udev) | |
212 | { | |
213 | struct usb_device *usbv = usbhsh_udev_to_usbv(udev); | |
214 | ||
215 | return hcd->self.root_hub == usbv->parent; | |
216 | } | |
034d7c13 KM |
217 | |
218 | static int usbhsh_device_has_endpoint(struct usbhsh_device *udev) | |
219 | { | |
220 | return !list_empty(&udev->ep_list_head); | |
221 | } | |
222 | ||
c1e4877a KM |
223 | static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv, |
224 | struct urb *urb) | |
225 | { | |
226 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); | |
227 | struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); | |
228 | ||
229 | /* usbhsh_device_attach() is still not called */ | |
230 | if (!udev) | |
231 | return NULL; | |
232 | ||
233 | /* if it is device0, return it */ | |
234 | if (0 == usb_pipedevice(urb->pipe)) | |
235 | return usbhsh_device0(hpriv); | |
236 | ||
237 | /* return attached device */ | |
238 | return udev; | |
239 | } | |
240 | ||
7aac8d15 | 241 | static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv, |
034d7c13 KM |
242 | struct urb *urb) |
243 | { | |
244 | struct usbhsh_device *udev = NULL; | |
c1e4877a KM |
245 | struct usbhsh_device *udev0 = usbhsh_device0(hpriv); |
246 | struct usbhsh_device *pos; | |
034d7c13 KM |
247 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
248 | struct device *dev = usbhsh_hcd_to_dev(hcd); | |
249 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); | |
250 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
d399f90d | 251 | unsigned long flags; |
9c673652 | 252 | u16 upphub, hubport; |
034d7c13 KM |
253 | int i; |
254 | ||
c1e4877a KM |
255 | /* |
256 | * This function should be called only while urb is pointing to device0. | |
257 | * It will attach unused usbhsh_device to urb (usbv), | |
258 | * and initialize device0. | |
259 | * You can use usbhsh_device_get() to get "current" udev, | |
260 | * and usbhsh_usbv_to_udev() is for "attached" udev. | |
261 | */ | |
262 | if (0 != usb_pipedevice(urb->pipe)) { | |
263 | dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__); | |
264 | return NULL; | |
265 | } | |
266 | ||
d399f90d KM |
267 | /******************** spin lock ********************/ |
268 | usbhs_lock(priv, flags); | |
269 | ||
034d7c13 | 270 | /* |
c1e4877a | 271 | * find unused device |
034d7c13 | 272 | */ |
c1e4877a KM |
273 | usbhsh_for_each_udev(pos, hpriv, i) { |
274 | if (usbhsh_udev_is_used(pos)) | |
275 | continue; | |
276 | udev = pos; | |
277 | break; | |
034d7c13 KM |
278 | } |
279 | ||
d399f90d KM |
280 | if (udev) { |
281 | /* | |
282 | * usbhsh_usbv_to_udev() | |
283 | * usbhsh_udev_to_usbv() | |
284 | * will be enable | |
285 | */ | |
286 | dev_set_drvdata(&usbv->dev, udev); | |
287 | udev->usbv = usbv; | |
288 | } | |
289 | ||
290 | usbhs_unlock(priv, flags); | |
291 | /******************** spin unlock ******************/ | |
292 | ||
ab142308 KM |
293 | if (!udev) { |
294 | dev_err(dev, "no free usbhsh_device\n"); | |
295 | return NULL; | |
034d7c13 KM |
296 | } |
297 | ||
034d7c13 KM |
298 | if (usbhsh_device_has_endpoint(udev)) |
299 | dev_warn(dev, "udev have old endpoint\n"); | |
300 | ||
c1e4877a KM |
301 | if (usbhsh_device_has_endpoint(udev0)) |
302 | dev_warn(dev, "udev0 have old endpoint\n"); | |
303 | ||
034d7c13 | 304 | /* uep will be attached */ |
c1e4877a | 305 | INIT_LIST_HEAD(&udev0->ep_list_head); |
034d7c13 KM |
306 | INIT_LIST_HEAD(&udev->ep_list_head); |
307 | ||
c1e4877a KM |
308 | /* |
309 | * set device0 config | |
310 | */ | |
311 | usbhs_set_device_config(priv, | |
312 | 0, 0, 0, usbv->speed); | |
313 | ||
314 | /* | |
315 | * set new device config | |
316 | */ | |
9c673652 KM |
317 | upphub = 0; |
318 | hubport = 0; | |
319 | if (!usbhsh_connected_to_rhdev(hcd, udev)) { | |
320 | /* if udev is not connected to rhdev, it means parent is Hub */ | |
321 | struct usbhsh_device *parent = usbhsh_device_parent(udev); | |
322 | ||
323 | upphub = usbhsh_device_number(hpriv, parent); | |
324 | hubport = usbhsh_device_hubport(udev); | |
325 | ||
326 | dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__, | |
327 | upphub, hubport, parent); | |
328 | } | |
329 | ||
3dd49268 | 330 | usbhs_set_device_config(priv, |
034d7c13 | 331 | usbhsh_device_number(hpriv, udev), |
9c673652 | 332 | upphub, hubport, usbv->speed); |
034d7c13 KM |
333 | |
334 | dev_dbg(dev, "%s [%d](%p)\n", __func__, | |
335 | usbhsh_device_number(hpriv, udev), udev); | |
336 | ||
337 | return udev; | |
338 | } | |
339 | ||
7aac8d15 | 340 | static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv, |
034d7c13 KM |
341 | struct usbhsh_device *udev) |
342 | { | |
343 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); | |
d399f90d | 344 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
034d7c13 KM |
345 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
346 | struct usb_device *usbv = usbhsh_udev_to_usbv(udev); | |
d399f90d | 347 | unsigned long flags; |
034d7c13 KM |
348 | |
349 | dev_dbg(dev, "%s [%d](%p)\n", __func__, | |
350 | usbhsh_device_number(hpriv, udev), udev); | |
351 | ||
352 | if (usbhsh_device_has_endpoint(udev)) | |
353 | dev_warn(dev, "udev still have endpoint\n"); | |
354 | ||
c1e4877a KM |
355 | /* |
356 | * There is nothing to do if it is device0. | |
357 | * see | |
358 | * usbhsh_device_attach() | |
359 | * usbhsh_device_get() | |
360 | */ | |
361 | if (0 == usbhsh_device_number(hpriv, udev)) | |
362 | return; | |
363 | ||
d399f90d KM |
364 | /******************** spin lock ********************/ |
365 | usbhs_lock(priv, flags); | |
366 | ||
034d7c13 KM |
367 | /* |
368 | * usbhsh_usbv_to_udev() | |
369 | * usbhsh_udev_to_usbv() | |
370 | * will be disable | |
371 | */ | |
372 | dev_set_drvdata(&usbv->dev, NULL); | |
373 | udev->usbv = NULL; | |
d399f90d KM |
374 | |
375 | usbhs_unlock(priv, flags); | |
376 | /******************** spin unlock ******************/ | |
034d7c13 KM |
377 | } |
378 | ||
379 | /* | |
380 | * end-point control | |
381 | */ | |
4825093e KM |
382 | static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv, |
383 | struct urb *urb, | |
384 | gfp_t mem_flags) | |
034d7c13 KM |
385 | { |
386 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
c1e4877a | 387 | struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); |
4825093e | 388 | struct usb_host_endpoint *ep = urb->ep; |
034d7c13 KM |
389 | struct usbhsh_ep *uep; |
390 | struct usbhsh_pipe_info *info; | |
f352741d KM |
391 | struct usbhs_pipe *best_pipe = NULL; |
392 | struct device *dev = usbhs_priv_to_dev(priv); | |
034d7c13 | 393 | struct usb_endpoint_descriptor *desc = &ep->desc; |
d399f90d | 394 | unsigned long flags; |
034d7c13 KM |
395 | |
396 | uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags); | |
397 | if (!uep) { | |
398 | dev_err(dev, "usbhsh_ep alloc fail\n"); | |
4825093e | 399 | return -ENOMEM; |
034d7c13 | 400 | } |
73ef635a | 401 | |
d399f90d KM |
402 | /******************** spin lock ********************/ |
403 | usbhs_lock(priv, flags); | |
404 | ||
034d7c13 KM |
405 | /* |
406 | * find best pipe for endpoint | |
407 | * see | |
408 | * HARDWARE LIMITATION | |
409 | */ | |
f352741d KM |
410 | if (usb_endpoint_xfer_control(desc)) { |
411 | /* best pipe is DCP */ | |
412 | best_pipe = usbhsh_hpriv_to_dcp(hpriv); | |
413 | } else { | |
414 | struct usbhs_pipe *pipe; | |
415 | unsigned int min_usr = ~0; | |
4825093e | 416 | int dir_in_req = !!usb_pipein(urb->pipe); |
f352741d KM |
417 | int i, dir_in; |
418 | ||
f352741d KM |
419 | usbhs_for_each_pipe(pipe, priv, i) { |
420 | if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc))) | |
421 | continue; | |
422 | ||
423 | dir_in = !!usbhs_pipe_is_dir_in(pipe); | |
424 | if (0 != (dir_in - dir_in_req)) | |
425 | continue; | |
426 | ||
427 | info = usbhsh_pipe_info(pipe); | |
428 | if (min_usr > info->usr_cnt) { | |
429 | min_usr = info->usr_cnt; | |
430 | best_pipe = pipe; | |
431 | } | |
034d7c13 KM |
432 | } |
433 | } | |
434 | ||
d399f90d KM |
435 | if (best_pipe) { |
436 | /* update pipe user count */ | |
437 | info = usbhsh_pipe_info(best_pipe); | |
438 | info->usr_cnt++; | |
439 | ||
440 | /* init this endpoint, and attach it to udev */ | |
441 | INIT_LIST_HEAD(&uep->ep_list); | |
442 | list_add_tail(&uep->ep_list, &udev->ep_list_head); | |
443 | } | |
444 | ||
445 | usbhs_unlock(priv, flags); | |
446 | /******************** spin unlock ******************/ | |
447 | ||
034d7c13 KM |
448 | if (unlikely(!best_pipe)) { |
449 | dev_err(dev, "couldn't find best pipe\n"); | |
450 | kfree(uep); | |
4825093e | 451 | return -EIO; |
034d7c13 | 452 | } |
f352741d | 453 | |
034d7c13 KM |
454 | /* |
455 | * init uep | |
456 | */ | |
457 | uep->pipe = best_pipe; | |
458 | uep->maxp = usb_endpoint_maxp(desc); | |
459 | usbhsh_uep_to_udev(uep) = udev; | |
460 | usbhsh_ep_to_uep(ep) = uep; | |
461 | ||
034d7c13 KM |
462 | /* |
463 | * usbhs_pipe_config_update() should be called after | |
3dd49268 | 464 | * usbhs_set_device_config() |
034d7c13 KM |
465 | * see |
466 | * DCPMAXP/PIPEMAXP | |
467 | */ | |
d7a00ec1 | 468 | usbhs_pipe_sequence_data0(uep->pipe); |
034d7c13 KM |
469 | usbhs_pipe_config_update(uep->pipe, |
470 | usbhsh_device_number(hpriv, udev), | |
471 | usb_endpoint_num(desc), | |
472 | uep->maxp); | |
473 | ||
474 | dev_dbg(dev, "%s [%d-%s](%p)\n", __func__, | |
475 | usbhsh_device_number(hpriv, udev), | |
73ef635a | 476 | usbhs_pipe_name(uep->pipe), uep); |
034d7c13 | 477 | |
4825093e | 478 | return 0; |
034d7c13 KM |
479 | } |
480 | ||
4825093e KM |
481 | static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv, |
482 | struct usb_host_endpoint *ep) | |
034d7c13 KM |
483 | { |
484 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
485 | struct device *dev = usbhs_priv_to_dev(priv); | |
486 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); | |
034d7c13 | 487 | struct usbhsh_pipe_info *info; |
d399f90d | 488 | unsigned long flags; |
034d7c13 KM |
489 | |
490 | if (!uep) | |
491 | return; | |
492 | ||
493 | dev_dbg(dev, "%s [%d-%s](%p)\n", __func__, | |
55b5a624 | 494 | usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)), |
034d7c13 KM |
495 | usbhs_pipe_name(uep->pipe), uep); |
496 | ||
d399f90d KM |
497 | /******************** spin lock ********************/ |
498 | usbhs_lock(priv, flags); | |
499 | ||
034d7c13 KM |
500 | info = usbhsh_pipe_info(uep->pipe); |
501 | info->usr_cnt--; | |
502 | ||
503 | /* remove this endpoint from udev */ | |
504 | list_del_init(&uep->ep_list); | |
505 | ||
506 | usbhsh_uep_to_udev(uep) = NULL; | |
507 | usbhsh_ep_to_uep(ep) = NULL; | |
508 | ||
d399f90d KM |
509 | usbhs_unlock(priv, flags); |
510 | /******************** spin unlock ******************/ | |
511 | ||
034d7c13 KM |
512 | kfree(uep); |
513 | } | |
514 | ||
515 | /* | |
516 | * queue push/pop | |
517 | */ | |
518 | static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) | |
519 | { | |
25234b46 | 520 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
034d7c13 KM |
521 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
522 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); | |
523 | struct urb *urb = ureq->urb; | |
524 | struct device *dev = usbhs_priv_to_dev(priv); | |
525 | ||
526 | dev_dbg(dev, "%s\n", __func__); | |
527 | ||
528 | if (!urb) { | |
529 | dev_warn(dev, "pkt doesn't have urb\n"); | |
530 | return; | |
531 | } | |
532 | ||
533 | urb->actual_length = pkt->actual; | |
25234b46 | 534 | usbhsh_ureq_free(hpriv, ureq); |
034d7c13 KM |
535 | |
536 | usb_hcd_unlink_urb_from_ep(hcd, urb); | |
537 | usb_hcd_giveback_urb(hcd, urb, 0); | |
538 | } | |
539 | ||
540 | static int usbhsh_queue_push(struct usb_hcd *hcd, | |
ee8a0bf5 KM |
541 | struct urb *urb, |
542 | gfp_t mem_flags) | |
034d7c13 | 543 | { |
ee8a0bf5 | 544 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
3eddc9e4 KM |
545 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
546 | struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep); | |
034d7c13 | 547 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
ee8a0bf5 | 548 | struct usbhsh_request *ureq; |
034d7c13 KM |
549 | void *buf; |
550 | int len; | |
551 | ||
552 | if (usb_pipeisoc(urb->pipe)) { | |
553 | dev_err(dev, "pipe iso is not supported now\n"); | |
554 | return -EIO; | |
555 | } | |
556 | ||
ee8a0bf5 KM |
557 | /* this ureq will be freed on usbhsh_queue_done() */ |
558 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); | |
559 | if (unlikely(!ureq)) { | |
560 | dev_err(dev, "ureq alloc fail\n"); | |
561 | return -ENOMEM; | |
562 | } | |
ee8a0bf5 | 563 | |
034d7c13 KM |
564 | if (usb_pipein(urb->pipe)) |
565 | pipe->handler = &usbhs_fifo_pio_pop_handler; | |
566 | else | |
567 | pipe->handler = &usbhs_fifo_pio_push_handler; | |
568 | ||
569 | buf = (void *)(urb->transfer_buffer + urb->actual_length); | |
570 | len = urb->transfer_buffer_length - urb->actual_length; | |
571 | ||
572 | dev_dbg(dev, "%s\n", __func__); | |
ee8a0bf5 | 573 | usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done, |
034d7c13 KM |
574 | buf, len, (urb->transfer_flags & URB_ZERO_PACKET)); |
575 | usbhs_pkt_start(pipe); | |
576 | ||
577 | return 0; | |
578 | } | |
579 | ||
580 | /* | |
581 | * DCP setup stage | |
582 | */ | |
583 | static int usbhsh_is_request_address(struct urb *urb) | |
584 | { | |
25234b46 | 585 | struct usb_ctrlrequest *req; |
034d7c13 | 586 | |
25234b46 | 587 | req = (struct usb_ctrlrequest *)urb->setup_packet; |
034d7c13 | 588 | |
25234b46 KM |
589 | if ((DeviceOutRequest == req->bRequestType << 8) && |
590 | (USB_REQ_SET_ADDRESS == req->bRequest)) | |
034d7c13 KM |
591 | return 1; |
592 | else | |
593 | return 0; | |
594 | } | |
595 | ||
596 | static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv, | |
597 | struct urb *urb, | |
598 | struct usbhs_pipe *pipe) | |
599 | { | |
600 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
601 | struct usb_ctrlrequest req; | |
602 | struct device *dev = usbhs_priv_to_dev(priv); | |
603 | ||
604 | /* | |
605 | * wait setup packet ACK | |
606 | * see | |
607 | * usbhsh_irq_setup_ack() | |
608 | * usbhsh_irq_setup_err() | |
609 | */ | |
7fccd480 | 610 | init_completion(&hpriv->setup_ack_done); |
034d7c13 KM |
611 | |
612 | /* copy original request */ | |
613 | memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest)); | |
614 | ||
615 | /* | |
616 | * renesas_usbhs can not use original usb address. | |
617 | * see HARDWARE LIMITATION. | |
c1e4877a KM |
618 | * modify usb address here to use attached device. |
619 | * see usbhsh_device_attach() | |
034d7c13 KM |
620 | */ |
621 | if (usbhsh_is_request_address(urb)) { | |
c1e4877a KM |
622 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
623 | struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); | |
624 | ||
625 | /* udev is a attached device */ | |
626 | req.wValue = usbhsh_device_number(hpriv, udev); | |
034d7c13 KM |
627 | dev_dbg(dev, "create new address - %d\n", req.wValue); |
628 | } | |
629 | ||
630 | /* set request */ | |
631 | usbhs_usbreq_set_val(priv, &req); | |
632 | ||
633 | /* | |
634 | * wait setup packet ACK | |
635 | */ | |
7fccd480 | 636 | wait_for_completion(&hpriv->setup_ack_done); |
034d7c13 KM |
637 | |
638 | dev_dbg(dev, "%s done\n", __func__); | |
639 | } | |
640 | ||
641 | /* | |
642 | * DCP data stage | |
643 | */ | |
644 | static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv, | |
645 | struct usbhs_pkt *pkt) | |
646 | { | |
25234b46 | 647 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
034d7c13 | 648 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
034d7c13 KM |
649 | |
650 | /* this ureq was connected to urb when usbhsh_urb_enqueue() */ | |
651 | ||
25234b46 | 652 | usbhsh_ureq_free(hpriv, ureq); |
034d7c13 KM |
653 | } |
654 | ||
ee8a0bf5 KM |
655 | static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv, |
656 | struct urb *urb, | |
657 | struct usbhs_pipe *pipe, | |
658 | gfp_t mem_flags) | |
659 | ||
034d7c13 KM |
660 | { |
661 | struct usbhsh_request *ureq; | |
034d7c13 | 662 | |
ee8a0bf5 KM |
663 | /* this ureq will be freed on usbhsh_data_stage_packet_done() */ |
664 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); | |
665 | if (unlikely(!ureq)) | |
666 | return -ENOMEM; | |
034d7c13 KM |
667 | |
668 | if (usb_pipein(urb->pipe)) | |
669 | pipe->handler = &usbhs_dcp_data_stage_in_handler; | |
670 | else | |
671 | pipe->handler = &usbhs_dcp_data_stage_out_handler; | |
672 | ||
ee8a0bf5 | 673 | usbhs_pkt_push(pipe, &ureq->pkt, |
034d7c13 KM |
674 | usbhsh_data_stage_packet_done, |
675 | urb->transfer_buffer, | |
676 | urb->transfer_buffer_length, | |
677 | (urb->transfer_flags & URB_ZERO_PACKET)); | |
ee8a0bf5 KM |
678 | |
679 | return 0; | |
034d7c13 KM |
680 | } |
681 | ||
682 | /* | |
683 | * DCP status stage | |
684 | */ | |
ee8a0bf5 | 685 | static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv, |
034d7c13 | 686 | struct urb *urb, |
ee8a0bf5 KM |
687 | struct usbhs_pipe *pipe, |
688 | gfp_t mem_flags) | |
034d7c13 KM |
689 | { |
690 | struct usbhsh_request *ureq; | |
034d7c13 | 691 | |
ee8a0bf5 KM |
692 | /* This ureq will be freed on usbhsh_queue_done() */ |
693 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); | |
694 | if (unlikely(!ureq)) | |
695 | return -ENOMEM; | |
034d7c13 KM |
696 | |
697 | if (usb_pipein(urb->pipe)) | |
698 | pipe->handler = &usbhs_dcp_status_stage_in_handler; | |
699 | else | |
700 | pipe->handler = &usbhs_dcp_status_stage_out_handler; | |
701 | ||
ee8a0bf5 | 702 | usbhs_pkt_push(pipe, &ureq->pkt, |
034d7c13 KM |
703 | usbhsh_queue_done, |
704 | NULL, | |
705 | urb->transfer_buffer_length, | |
706 | 0); | |
ee8a0bf5 KM |
707 | |
708 | return 0; | |
034d7c13 KM |
709 | } |
710 | ||
711 | static int usbhsh_dcp_queue_push(struct usb_hcd *hcd, | |
ee8a0bf5 KM |
712 | struct urb *urb, |
713 | gfp_t mflags) | |
034d7c13 | 714 | { |
ee8a0bf5 | 715 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
3eddc9e4 KM |
716 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
717 | struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep); | |
034d7c13 | 718 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
ee8a0bf5 | 719 | int ret; |
034d7c13 KM |
720 | |
721 | dev_dbg(dev, "%s\n", __func__); | |
722 | ||
723 | /* | |
724 | * setup stage | |
725 | * | |
726 | * usbhsh_send_setup_stage_packet() wait SACK/SIGN | |
727 | */ | |
728 | usbhsh_setup_stage_packet_push(hpriv, urb, pipe); | |
729 | ||
730 | /* | |
731 | * data stage | |
732 | * | |
733 | * It is pushed only when urb has buffer. | |
734 | */ | |
ee8a0bf5 KM |
735 | if (urb->transfer_buffer_length) { |
736 | ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags); | |
737 | if (ret < 0) { | |
738 | dev_err(dev, "data stage failed\n"); | |
739 | return ret; | |
740 | } | |
741 | } | |
034d7c13 KM |
742 | |
743 | /* | |
744 | * status stage | |
745 | */ | |
ee8a0bf5 KM |
746 | ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags); |
747 | if (ret < 0) { | |
748 | dev_err(dev, "status stage failed\n"); | |
749 | return ret; | |
750 | } | |
034d7c13 KM |
751 | |
752 | /* | |
753 | * start pushed packets | |
754 | */ | |
755 | usbhs_pkt_start(pipe); | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
760 | /* | |
761 | * dma map functions | |
762 | */ | |
763 | static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map) | |
764 | { | |
765 | return 0; | |
766 | } | |
767 | ||
768 | /* | |
769 | * for hc_driver | |
770 | */ | |
771 | static int usbhsh_host_start(struct usb_hcd *hcd) | |
772 | { | |
773 | return 0; | |
774 | } | |
775 | ||
776 | static void usbhsh_host_stop(struct usb_hcd *hcd) | |
777 | { | |
778 | } | |
779 | ||
780 | static int usbhsh_urb_enqueue(struct usb_hcd *hcd, | |
781 | struct urb *urb, | |
782 | gfp_t mem_flags) | |
783 | { | |
784 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); | |
785 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
786 | struct device *dev = usbhs_priv_to_dev(priv); | |
034d7c13 | 787 | struct usb_host_endpoint *ep = urb->ep; |
7aac8d15 | 788 | struct usbhsh_device *new_udev = NULL; |
73ef635a | 789 | int is_dir_in = usb_pipein(urb->pipe); |
034d7c13 KM |
790 | |
791 | int ret; | |
792 | ||
73ef635a | 793 | dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out"); |
034d7c13 KM |
794 | |
795 | ret = usb_hcd_link_urb_to_ep(hcd, urb); | |
796 | if (ret) | |
797 | goto usbhsh_urb_enqueue_error_not_linked; | |
798 | ||
799 | /* | |
7aac8d15 | 800 | * attach udev if needed |
034d7c13 | 801 | */ |
c1e4877a | 802 | if (!usbhsh_device_get(hpriv, urb)) { |
7aac8d15 | 803 | new_udev = usbhsh_device_attach(hpriv, urb); |
37332ee0 KM |
804 | if (!new_udev) { |
805 | ret = -EIO; | |
034d7c13 | 806 | goto usbhsh_urb_enqueue_error_not_linked; |
37332ee0 | 807 | } |
034d7c13 KM |
808 | } |
809 | ||
810 | /* | |
4825093e | 811 | * attach endpoint if needed |
034d7c13 | 812 | */ |
4825093e KM |
813 | if (!usbhsh_ep_to_uep(ep)) { |
814 | ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags); | |
815 | if (ret < 0) | |
034d7c13 KM |
816 | goto usbhsh_urb_enqueue_error_free_device; |
817 | } | |
034d7c13 | 818 | |
034d7c13 KM |
819 | /* |
820 | * push packet | |
821 | */ | |
822 | if (usb_pipecontrol(urb->pipe)) | |
3eddc9e4 | 823 | ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags); |
034d7c13 | 824 | else |
3eddc9e4 | 825 | ret = usbhsh_queue_push(hcd, urb, mem_flags); |
034d7c13 | 826 | |
ee8a0bf5 | 827 | return ret; |
034d7c13 | 828 | |
034d7c13 KM |
829 | usbhsh_urb_enqueue_error_free_device: |
830 | if (new_udev) | |
7aac8d15 | 831 | usbhsh_device_detach(hpriv, new_udev); |
034d7c13 KM |
832 | usbhsh_urb_enqueue_error_not_linked: |
833 | ||
834 | dev_dbg(dev, "%s error\n", __func__); | |
835 | ||
836 | return ret; | |
837 | } | |
838 | ||
839 | static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) | |
840 | { | |
841 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); | |
842 | struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb); | |
843 | ||
54796543 KM |
844 | if (ureq) { |
845 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
846 | struct usbhs_pkt *pkt = &ureq->pkt; | |
847 | ||
848 | usbhs_pkt_pop(pkt->pipe, pkt); | |
849 | usbhsh_queue_done(priv, pkt); | |
850 | } | |
034d7c13 KM |
851 | |
852 | return 0; | |
853 | } | |
854 | ||
855 | static void usbhsh_endpoint_disable(struct usb_hcd *hcd, | |
856 | struct usb_host_endpoint *ep) | |
857 | { | |
858 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); | |
859 | struct usbhsh_device *udev; | |
860 | struct usbhsh_hpriv *hpriv; | |
861 | ||
862 | /* | |
863 | * this function might be called manytimes by same hcd/ep | |
fca8ab7e | 864 | * in-endpoint == out-endpoint if ep == dcp. |
034d7c13 KM |
865 | */ |
866 | if (!uep) | |
867 | return; | |
868 | ||
869 | udev = usbhsh_uep_to_udev(uep); | |
870 | hpriv = usbhsh_hcd_to_hpriv(hcd); | |
871 | ||
4825093e | 872 | usbhsh_endpoint_detach(hpriv, ep); |
034d7c13 KM |
873 | |
874 | /* | |
875 | * if there is no endpoint, | |
876 | * free device | |
877 | */ | |
878 | if (!usbhsh_device_has_endpoint(udev)) | |
7aac8d15 | 879 | usbhsh_device_detach(hpriv, udev); |
034d7c13 KM |
880 | } |
881 | ||
882 | static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf) | |
883 | { | |
884 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); | |
885 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
886 | struct device *dev = usbhs_priv_to_dev(priv); | |
887 | int roothub_id = 1; /* only 1 root hub */ | |
888 | ||
889 | /* | |
890 | * does port stat was changed ? | |
891 | * check USB_PORT_STAT_C_xxx << 16 | |
892 | */ | |
893 | if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000) | |
894 | *buf = (1 << roothub_id); | |
895 | else | |
896 | *buf = 0; | |
897 | ||
898 | dev_dbg(dev, "%s (%02x)\n", __func__, *buf); | |
899 | ||
900 | return !!(*buf); | |
901 | } | |
902 | ||
903 | static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv, | |
904 | u16 typeReq, u16 wValue, | |
905 | u16 wIndex, char *buf, u16 wLength) | |
906 | { | |
907 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
908 | struct device *dev = usbhs_priv_to_dev(priv); | |
909 | ||
910 | switch (wValue) { | |
911 | case C_HUB_OVER_CURRENT: | |
912 | case C_HUB_LOCAL_POWER: | |
913 | dev_dbg(dev, "%s :: C_HUB_xx\n", __func__); | |
914 | return 0; | |
915 | } | |
916 | ||
917 | return -EPIPE; | |
918 | } | |
919 | ||
920 | static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv, | |
921 | u16 typeReq, u16 wValue, | |
922 | u16 wIndex, char *buf, u16 wLength) | |
923 | { | |
924 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
925 | struct device *dev = usbhs_priv_to_dev(priv); | |
926 | int enable = (typeReq == SetPortFeature); | |
927 | int speed, i, timeout = 128; | |
928 | int roothub_id = 1; /* only 1 root hub */ | |
929 | ||
930 | /* common error */ | |
931 | if (wIndex > roothub_id || wLength != 0) | |
932 | return -EPIPE; | |
933 | ||
934 | /* check wValue */ | |
935 | switch (wValue) { | |
936 | case USB_PORT_FEAT_POWER: | |
937 | usbhs_vbus_ctrl(priv, enable); | |
938 | dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__); | |
939 | break; | |
940 | ||
941 | case USB_PORT_FEAT_ENABLE: | |
942 | case USB_PORT_FEAT_SUSPEND: | |
943 | case USB_PORT_FEAT_C_ENABLE: | |
944 | case USB_PORT_FEAT_C_SUSPEND: | |
945 | case USB_PORT_FEAT_C_CONNECTION: | |
946 | case USB_PORT_FEAT_C_OVER_CURRENT: | |
947 | case USB_PORT_FEAT_C_RESET: | |
948 | dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__); | |
949 | break; | |
950 | ||
951 | case USB_PORT_FEAT_RESET: | |
952 | if (!enable) | |
953 | break; | |
954 | ||
955 | usbhsh_port_stat_clear(hpriv, | |
956 | USB_PORT_STAT_HIGH_SPEED | | |
957 | USB_PORT_STAT_LOW_SPEED); | |
958 | ||
959 | usbhs_bus_send_reset(priv); | |
960 | msleep(20); | |
961 | usbhs_bus_send_sof_enable(priv); | |
962 | ||
963 | for (i = 0; i < timeout ; i++) { | |
964 | switch (usbhs_bus_get_speed(priv)) { | |
965 | case USB_SPEED_LOW: | |
966 | speed = USB_PORT_STAT_LOW_SPEED; | |
967 | goto got_usb_bus_speed; | |
968 | case USB_SPEED_HIGH: | |
969 | speed = USB_PORT_STAT_HIGH_SPEED; | |
970 | goto got_usb_bus_speed; | |
971 | case USB_SPEED_FULL: | |
972 | speed = 0; | |
973 | goto got_usb_bus_speed; | |
974 | } | |
975 | ||
976 | msleep(20); | |
977 | } | |
978 | return -EPIPE; | |
979 | ||
980 | got_usb_bus_speed: | |
981 | usbhsh_port_stat_set(hpriv, speed); | |
982 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE); | |
983 | ||
984 | dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n", | |
985 | __func__, speed); | |
986 | ||
987 | /* status change is not needed */ | |
988 | return 0; | |
989 | ||
990 | default: | |
991 | return -EPIPE; | |
992 | } | |
993 | ||
994 | /* set/clear status */ | |
995 | if (enable) | |
996 | usbhsh_port_stat_set(hpriv, (1 << wValue)); | |
997 | else | |
998 | usbhsh_port_stat_clear(hpriv, (1 << wValue)); | |
999 | ||
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv, | |
1004 | u16 typeReq, u16 wValue, | |
1005 | u16 wIndex, char *buf, u16 wLength) | |
1006 | { | |
1007 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
1008 | struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf; | |
1009 | struct device *dev = usbhs_priv_to_dev(priv); | |
1010 | int roothub_id = 1; /* only 1 root hub */ | |
1011 | ||
1012 | switch (typeReq) { | |
1013 | case GetHubStatus: | |
1014 | dev_dbg(dev, "%s :: GetHubStatus\n", __func__); | |
1015 | ||
1016 | *buf = 0x00; | |
1017 | break; | |
1018 | ||
1019 | case GetPortStatus: | |
1020 | if (wIndex != roothub_id) | |
1021 | return -EPIPE; | |
1022 | ||
1023 | dev_dbg(dev, "%s :: GetPortStatus\n", __func__); | |
1024 | *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv)); | |
1025 | break; | |
1026 | ||
1027 | case GetHubDescriptor: | |
1028 | desc->bDescriptorType = 0x29; | |
1029 | desc->bHubContrCurrent = 0; | |
1030 | desc->bNbrPorts = roothub_id; | |
1031 | desc->bDescLength = 9; | |
1032 | desc->bPwrOn2PwrGood = 0; | |
1033 | desc->wHubCharacteristics = cpu_to_le16(0x0011); | |
1034 | desc->u.hs.DeviceRemovable[0] = (roothub_id << 1); | |
1035 | desc->u.hs.DeviceRemovable[1] = ~0; | |
1036 | dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__); | |
1037 | break; | |
1038 | } | |
1039 | ||
1040 | return 0; | |
1041 | } | |
1042 | ||
1043 | static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, | |
1044 | u16 wIndex, char *buf, u16 wLength) | |
1045 | { | |
1046 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); | |
1047 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); | |
1048 | struct device *dev = usbhs_priv_to_dev(priv); | |
1049 | int ret = -EPIPE; | |
1050 | ||
1051 | switch (typeReq) { | |
1052 | ||
1053 | /* Hub Feature */ | |
1054 | case ClearHubFeature: | |
1055 | case SetHubFeature: | |
1056 | ret = __usbhsh_hub_hub_feature(hpriv, typeReq, | |
1057 | wValue, wIndex, buf, wLength); | |
1058 | break; | |
1059 | ||
1060 | /* Port Feature */ | |
1061 | case SetPortFeature: | |
1062 | case ClearPortFeature: | |
1063 | ret = __usbhsh_hub_port_feature(hpriv, typeReq, | |
1064 | wValue, wIndex, buf, wLength); | |
1065 | break; | |
1066 | ||
1067 | /* Get status */ | |
1068 | case GetHubStatus: | |
1069 | case GetPortStatus: | |
1070 | case GetHubDescriptor: | |
1071 | ret = __usbhsh_hub_get_status(hpriv, typeReq, | |
1072 | wValue, wIndex, buf, wLength); | |
1073 | break; | |
1074 | } | |
1075 | ||
1076 | dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n", | |
1077 | typeReq, ret, usbhsh_port_stat_get(hpriv)); | |
1078 | ||
1079 | return ret; | |
1080 | } | |
1081 | ||
1082 | static struct hc_driver usbhsh_driver = { | |
1083 | .description = usbhsh_hcd_name, | |
1084 | .hcd_priv_size = sizeof(struct usbhsh_hpriv), | |
1085 | ||
1086 | /* | |
1087 | * generic hardware linkage | |
1088 | */ | |
1089 | .flags = HCD_USB2, | |
1090 | ||
1091 | .start = usbhsh_host_start, | |
1092 | .stop = usbhsh_host_stop, | |
1093 | ||
1094 | /* | |
1095 | * managing i/o requests and associated device resources | |
1096 | */ | |
1097 | .urb_enqueue = usbhsh_urb_enqueue, | |
1098 | .urb_dequeue = usbhsh_urb_dequeue, | |
1099 | .endpoint_disable = usbhsh_endpoint_disable, | |
1100 | ||
1101 | /* | |
1102 | * root hub | |
1103 | */ | |
1104 | .hub_status_data = usbhsh_hub_status_data, | |
1105 | .hub_control = usbhsh_hub_control, | |
1106 | }; | |
1107 | ||
1108 | /* | |
1109 | * interrupt functions | |
1110 | */ | |
1111 | static int usbhsh_irq_attch(struct usbhs_priv *priv, | |
1112 | struct usbhs_irq_state *irq_state) | |
1113 | { | |
1114 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); | |
1115 | struct device *dev = usbhs_priv_to_dev(priv); | |
1116 | ||
1117 | dev_dbg(dev, "device attached\n"); | |
1118 | ||
1119 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION); | |
1120 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); | |
1121 | ||
1122 | return 0; | |
1123 | } | |
1124 | ||
1125 | static int usbhsh_irq_dtch(struct usbhs_priv *priv, | |
1126 | struct usbhs_irq_state *irq_state) | |
1127 | { | |
1128 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); | |
1129 | struct device *dev = usbhs_priv_to_dev(priv); | |
1130 | ||
1131 | dev_dbg(dev, "device detached\n"); | |
1132 | ||
1133 | usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION); | |
1134 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); | |
1135 | ||
1136 | return 0; | |
1137 | } | |
1138 | ||
1139 | static int usbhsh_irq_setup_ack(struct usbhs_priv *priv, | |
1140 | struct usbhs_irq_state *irq_state) | |
1141 | { | |
1142 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); | |
1143 | struct device *dev = usbhs_priv_to_dev(priv); | |
1144 | ||
1145 | dev_dbg(dev, "setup packet OK\n"); | |
1146 | ||
7fccd480 | 1147 | complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ |
034d7c13 KM |
1148 | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | static int usbhsh_irq_setup_err(struct usbhs_priv *priv, | |
1153 | struct usbhs_irq_state *irq_state) | |
1154 | { | |
1155 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); | |
1156 | struct device *dev = usbhs_priv_to_dev(priv); | |
1157 | ||
1158 | dev_dbg(dev, "setup packet Err\n"); | |
1159 | ||
7fccd480 | 1160 | complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ |
034d7c13 KM |
1161 | |
1162 | return 0; | |
1163 | } | |
1164 | ||
1165 | /* | |
1166 | * module start/stop | |
1167 | */ | |
1168 | static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv) | |
1169 | { | |
1170 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); | |
1171 | struct usbhsh_pipe_info *pipe_info = hpriv->pipe_info; | |
1172 | struct usbhs_pipe *pipe; | |
1173 | u32 *pipe_type = usbhs_get_dparam(priv, pipe_type); | |
1174 | int pipe_size = usbhs_get_dparam(priv, pipe_size); | |
1175 | int old_type, dir_in, i; | |
1176 | ||
1177 | /* init all pipe */ | |
1178 | old_type = USB_ENDPOINT_XFER_CONTROL; | |
1179 | for (i = 0; i < pipe_size; i++) { | |
1180 | pipe_info[i].usr_cnt = 0; | |
1181 | ||
1182 | /* | |
1183 | * data "output" will be finished as soon as possible, | |
1184 | * but there is no guaranty at data "input" case. | |
1185 | * | |
1186 | * "input" needs "standby" pipe. | |
1187 | * So, "input" direction pipe > "output" direction pipe | |
1188 | * is good idea. | |
1189 | * | |
1190 | * 1st USB_ENDPOINT_XFER_xxx will be output direction, | |
1191 | * and the other will be input direction here. | |
1192 | * | |
1193 | * ex) | |
1194 | * ... | |
1195 | * USB_ENDPOINT_XFER_ISOC -> dir out | |
1196 | * USB_ENDPOINT_XFER_ISOC -> dir in | |
1197 | * USB_ENDPOINT_XFER_BULK -> dir out | |
1198 | * USB_ENDPOINT_XFER_BULK -> dir in | |
1199 | * USB_ENDPOINT_XFER_BULK -> dir in | |
1200 | * ... | |
1201 | */ | |
1202 | dir_in = (pipe_type[i] == old_type); | |
1203 | old_type = pipe_type[i]; | |
1204 | ||
1205 | if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) { | |
1206 | pipe = usbhs_dcp_malloc(priv); | |
1207 | usbhsh_hpriv_to_dcp(hpriv) = pipe; | |
1208 | } else { | |
1209 | pipe = usbhs_pipe_malloc(priv, | |
1210 | pipe_type[i], | |
1211 | dir_in); | |
1212 | } | |
1213 | ||
1214 | pipe->mod_private = pipe_info + i; | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | static int usbhsh_start(struct usbhs_priv *priv) | |
1219 | { | |
1220 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); | |
1221 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); | |
1222 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); | |
1223 | struct device *dev = usbhs_priv_to_dev(priv); | |
1224 | int ret; | |
1225 | ||
1226 | /* add hcd */ | |
1227 | ret = usb_add_hcd(hcd, 0, 0); | |
1228 | if (ret < 0) | |
1229 | return 0; | |
1230 | ||
1231 | /* | |
1232 | * pipe initialize and enable DCP | |
1233 | */ | |
1234 | usbhs_pipe_init(priv, | |
1235 | usbhsh_dma_map_ctrl); | |
1236 | usbhs_fifo_init(priv); | |
1237 | usbhsh_pipe_init_for_host(priv); | |
1238 | ||
1239 | /* | |
1240 | * system config enble | |
1241 | * - HI speed | |
1242 | * - host | |
1243 | * - usb module | |
1244 | */ | |
034d7c13 | 1245 | usbhs_sys_host_ctrl(priv, 1); |
034d7c13 KM |
1246 | |
1247 | /* | |
1248 | * enable irq callback | |
1249 | */ | |
1250 | mod->irq_attch = usbhsh_irq_attch; | |
1251 | mod->irq_dtch = usbhsh_irq_dtch; | |
1252 | mod->irq_sack = usbhsh_irq_setup_ack; | |
1253 | mod->irq_sign = usbhsh_irq_setup_err; | |
1254 | usbhs_irq_callback_update(priv, mod); | |
1255 | ||
1256 | dev_dbg(dev, "start host\n"); | |
1257 | ||
1258 | return ret; | |
1259 | } | |
1260 | ||
1261 | static int usbhsh_stop(struct usbhs_priv *priv) | |
1262 | { | |
1263 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); | |
1264 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); | |
146ee50a | 1265 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
034d7c13 KM |
1266 | struct device *dev = usbhs_priv_to_dev(priv); |
1267 | ||
146ee50a KM |
1268 | /* |
1269 | * disable irq callback | |
1270 | */ | |
1271 | mod->irq_attch = NULL; | |
1272 | mod->irq_dtch = NULL; | |
1273 | mod->irq_sack = NULL; | |
1274 | mod->irq_sign = NULL; | |
1275 | usbhs_irq_callback_update(priv, mod); | |
1276 | ||
034d7c13 KM |
1277 | usb_remove_hcd(hcd); |
1278 | ||
1279 | /* disable sys */ | |
034d7c13 | 1280 | usbhs_sys_host_ctrl(priv, 0); |
034d7c13 KM |
1281 | |
1282 | dev_dbg(dev, "quit host\n"); | |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
b7a8d17d | 1287 | int usbhs_mod_host_probe(struct usbhs_priv *priv) |
034d7c13 KM |
1288 | { |
1289 | struct usbhsh_hpriv *hpriv; | |
1290 | struct usb_hcd *hcd; | |
1291 | struct usbhsh_pipe_info *pipe_info; | |
1292 | struct usbhsh_device *udev; | |
1293 | struct device *dev = usbhs_priv_to_dev(priv); | |
1294 | int pipe_size = usbhs_get_dparam(priv, pipe_size); | |
1295 | int i; | |
1296 | ||
1297 | /* initialize hcd */ | |
1298 | hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name); | |
1299 | if (!hcd) { | |
1300 | dev_err(dev, "Failed to create hcd\n"); | |
1301 | return -ENOMEM; | |
1302 | } | |
1303 | ||
1304 | pipe_info = kzalloc(sizeof(*pipe_info) * pipe_size, GFP_KERNEL); | |
1305 | if (!pipe_info) { | |
1306 | dev_err(dev, "Could not allocate pipe_info\n"); | |
1307 | goto usbhs_mod_host_probe_err; | |
1308 | } | |
1309 | ||
1310 | /* | |
1311 | * CAUTION | |
1312 | * | |
1313 | * There is no guarantee that it is possible to access usb module here. | |
1314 | * Don't accesses to it. | |
1315 | * The accesse will be enable after "usbhsh_start" | |
1316 | */ | |
1317 | ||
1318 | hpriv = usbhsh_hcd_to_hpriv(hcd); | |
1319 | ||
1320 | /* | |
1321 | * register itself | |
1322 | */ | |
1323 | usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST); | |
1324 | ||
1325 | /* init hpriv */ | |
1326 | hpriv->mod.name = "host"; | |
1327 | hpriv->mod.start = usbhsh_start; | |
1328 | hpriv->mod.stop = usbhsh_stop; | |
1329 | hpriv->pipe_info = pipe_info; | |
1330 | hpriv->pipe_size = pipe_size; | |
034d7c13 KM |
1331 | usbhsh_port_stat_init(hpriv); |
1332 | ||
1333 | /* init all device */ | |
1334 | usbhsh_for_each_udev_with_dev0(udev, hpriv, i) { | |
1335 | udev->usbv = NULL; | |
1336 | INIT_LIST_HEAD(&udev->ep_list_head); | |
1337 | } | |
1338 | ||
1339 | dev_info(dev, "host probed\n"); | |
1340 | ||
1341 | return 0; | |
1342 | ||
1343 | usbhs_mod_host_probe_err: | |
1344 | usb_put_hcd(hcd); | |
1345 | ||
1346 | return -ENOMEM; | |
1347 | } | |
1348 | ||
b7a8d17d | 1349 | int usbhs_mod_host_remove(struct usbhs_priv *priv) |
034d7c13 KM |
1350 | { |
1351 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); | |
1352 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); | |
1353 | ||
034d7c13 KM |
1354 | usb_put_hcd(hcd); |
1355 | ||
1356 | return 0; | |
1357 | } |