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