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