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