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