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