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