blob: 3f1eaf15e0ba1523a39b993be47b867ad5ffaa78 [file] [log] [blame]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001/*
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;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070077};
78
79struct usbhsh_device {
80 struct usb_device *usbv;
81 struct list_head ep_list_head; /* list of usbhsh_ep */
82};
83
84struct usbhsh_ep {
85 struct usbhs_pipe *pipe;
86 struct usbhsh_device *udev; /* attached udev */
87 struct list_head ep_list; /* list to usbhsh_device */
88
89 int maxp;
90};
91
92#define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
93#define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
94struct usbhsh_hpriv {
95 struct usbhs_mod mod;
96 struct usbhs_pipe *dcp;
97
98 struct usbhsh_device udev[USBHSH_DEVICE_MAX];
99
100 struct usbhsh_pipe_info *pipe_info;
101 int pipe_size;
102
103 u32 port_stat; /* USB_PORT_STAT_xxx */
104
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700105 struct completion setup_ack_done;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700106};
107
108
109static const char usbhsh_hcd_name[] = "renesas_usbhs host";
110
111/*
112 * macro
113 */
114#define usbhsh_priv_to_hpriv(priv) \
115 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
116
117#define __usbhsh_for_each_hpipe(start, pos, h, i) \
118 for (i = start, pos = (h)->hpipe + i; \
119 i < (h)->hpipe_size; \
120 i++, pos = (h)->hpipe + i)
121
122#define usbhsh_for_each_hpipe(pos, hpriv, i) \
123 __usbhsh_for_each_hpipe(1, pos, hpriv, i)
124
125#define usbhsh_for_each_hpipe_with_dcp(pos, hpriv, i) \
126 __usbhsh_for_each_hpipe(0, pos, hpriv, i)
127
128#define __usbhsh_for_each_udev(start, pos, h, i) \
129 for (i = start, pos = (h)->udev + i; \
130 i < USBHSH_DEVICE_MAX; \
131 i++, pos = (h)->udev + i)
132
133#define usbhsh_for_each_udev(pos, hpriv, i) \
134 __usbhsh_for_each_udev(1, pos, hpriv, i)
135
136#define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
137 __usbhsh_for_each_udev(0, pos, hpriv, i)
138
139#define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
140#define usbhsh_hcd_to_dev(h) ((h)->self.controller)
141
142#define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
143#define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
144#define usbhsh_hpriv_to_hcd(h) \
145 container_of((void *)h, struct usb_hcd, hcd_priv)
146
147#define usbhsh_ep_to_uep(u) ((u)->hcpriv)
148#define usbhsh_uep_to_pipe(u) ((u)->pipe)
149#define usbhsh_uep_to_udev(u) ((u)->udev)
150#define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
151#define usbhsh_urb_to_usbv(u) ((u)->dev)
152
153#define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
154
155#define usbhsh_udev_to_usbv(h) ((h)->usbv)
156
157#define usbhsh_pipe_info(p) ((p)->mod_private)
158
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700159#define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
160#define usbhsh_device_hubport(d) ((d)->usbv->portnum)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700161#define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
162#define usbhsh_device_nth(h, d) ((h)->udev + d)
163#define usbhsh_device0(h) usbhsh_device_nth(h, 0)
164
165#define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
166#define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
167#define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
168#define usbhsh_port_stat_get(h) ((h)->port_stat)
169
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700170#define usbhsh_pkt_to_ureq(p) \
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700171 container_of((void *)p, struct usbhsh_request, pkt)
172
173/*
174 * req alloc/free
175 */
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700176static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700177 struct urb *urb,
178 gfp_t mem_flags)
179{
180 struct usbhsh_request *ureq;
181 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
182 struct device *dev = usbhs_priv_to_dev(priv);
183
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700184 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700185 if (!ureq) {
186 dev_err(dev, "ureq alloc fail\n");
187 return NULL;
188 }
189
190 usbhs_pkt_init(&ureq->pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700191 ureq->urb = urb;
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700192 usbhsh_urb_to_ureq(urb) = ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700193
194 return ureq;
195}
196
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700197static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700198 struct usbhsh_request *ureq)
199{
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700200 usbhsh_urb_to_ureq(ureq->urb) = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700201 ureq->urb = NULL;
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700202
203 kfree(ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700204}
205
206/*
207 * device control
208 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700209static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
210 struct usbhsh_device *udev)
211{
212 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
213
214 return hcd->self.root_hub == usbv->parent;
215}
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700216
217static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
218{
219 return !list_empty(&udev->ep_list_head);
220}
221
222static struct usbhsh_device *usbhsh_device_alloc(struct usbhsh_hpriv *hpriv,
223 struct urb *urb)
224{
225 struct usbhsh_device *udev = NULL;
226 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
227 struct device *dev = usbhsh_hcd_to_dev(hcd);
228 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
229 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700230 u16 upphub, hubport;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700231 int i;
232
233 /*
234 * device 0
235 */
236 if (0 == usb_pipedevice(urb->pipe)) {
237 udev = usbhsh_device0(hpriv);
238 goto usbhsh_device_find;
239 }
240
241 /*
242 * find unused device
243 */
244 usbhsh_for_each_udev(udev, hpriv, i) {
245 if (usbhsh_udev_to_usbv(udev))
246 continue;
247 goto usbhsh_device_find;
248 }
249
250 dev_err(dev, "no free usbhsh_device\n");
251
252 return NULL;
253
254usbhsh_device_find:
255 if (usbhsh_device_has_endpoint(udev))
256 dev_warn(dev, "udev have old endpoint\n");
257
258 /* uep will be attached */
259 INIT_LIST_HEAD(&udev->ep_list_head);
260
261 /*
262 * usbhsh_usbv_to_udev()
263 * usbhsh_udev_to_usbv()
264 * will be enable
265 */
266 dev_set_drvdata(&usbv->dev, udev);
267 udev->usbv = usbv;
268
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700269 upphub = 0;
270 hubport = 0;
271 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
272 /* if udev is not connected to rhdev, it means parent is Hub */
273 struct usbhsh_device *parent = usbhsh_device_parent(udev);
274
275 upphub = usbhsh_device_number(hpriv, parent);
276 hubport = usbhsh_device_hubport(udev);
277
278 dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
279 upphub, hubport, parent);
280 }
281
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700282 /* set device config */
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700283 usbhs_set_device_config(priv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700284 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700285 upphub, hubport, usbv->speed);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700286
287 dev_dbg(dev, "%s [%d](%p)\n", __func__,
288 usbhsh_device_number(hpriv, udev), udev);
289
290 return udev;
291}
292
293static void usbhsh_device_free(struct usbhsh_hpriv *hpriv,
294 struct usbhsh_device *udev)
295{
296 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
297 struct device *dev = usbhsh_hcd_to_dev(hcd);
298 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
299
300 dev_dbg(dev, "%s [%d](%p)\n", __func__,
301 usbhsh_device_number(hpriv, udev), udev);
302
303 if (usbhsh_device_has_endpoint(udev))
304 dev_warn(dev, "udev still have endpoint\n");
305
306 /*
307 * usbhsh_usbv_to_udev()
308 * usbhsh_udev_to_usbv()
309 * will be disable
310 */
311 dev_set_drvdata(&usbv->dev, NULL);
312 udev->usbv = NULL;
313}
314
315/*
316 * end-point control
317 */
318struct usbhsh_ep *usbhsh_endpoint_alloc(struct usbhsh_hpriv *hpriv,
319 struct usbhsh_device *udev,
320 struct usb_host_endpoint *ep,
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700321 int dir_in_req,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700322 gfp_t mem_flags)
323{
324 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
325 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
326 struct usbhsh_ep *uep;
327 struct usbhsh_pipe_info *info;
328 struct usbhs_pipe *pipe, *best_pipe;
329 struct device *dev = usbhsh_hcd_to_dev(hcd);
330 struct usb_endpoint_descriptor *desc = &ep->desc;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700331 int type, i, dir_in;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700332 unsigned int min_usr;
333
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700334 dir_in_req = !!dir_in_req;
335
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700336 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
337 if (!uep) {
338 dev_err(dev, "usbhsh_ep alloc fail\n");
339 return NULL;
340 }
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700341
342 if (usb_endpoint_xfer_control(desc)) {
343 best_pipe = usbhsh_hpriv_to_dcp(hpriv);
344 goto usbhsh_endpoint_alloc_find_pipe;
345 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700346
347 /*
348 * find best pipe for endpoint
349 * see
350 * HARDWARE LIMITATION
351 */
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700352 type = usb_endpoint_type(desc);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700353 min_usr = ~0;
354 best_pipe = NULL;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700355 usbhs_for_each_pipe(pipe, priv, i) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700356 if (!usbhs_pipe_type_is(pipe, type))
357 continue;
358
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700359 dir_in = !!usbhs_pipe_is_dir_in(pipe);
360 if (0 != (dir_in - dir_in_req))
361 continue;
362
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700363 info = usbhsh_pipe_info(pipe);
364
365 if (min_usr > info->usr_cnt) {
366 min_usr = info->usr_cnt;
367 best_pipe = pipe;
368 }
369 }
370
371 if (unlikely(!best_pipe)) {
372 dev_err(dev, "couldn't find best pipe\n");
373 kfree(uep);
374 return NULL;
375 }
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700376usbhsh_endpoint_alloc_find_pipe:
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700377 /*
378 * init uep
379 */
380 uep->pipe = best_pipe;
381 uep->maxp = usb_endpoint_maxp(desc);
382 usbhsh_uep_to_udev(uep) = udev;
383 usbhsh_ep_to_uep(ep) = uep;
384
385 /*
386 * update pipe user count
387 */
388 info = usbhsh_pipe_info(best_pipe);
389 info->usr_cnt++;
390
391 /* init this endpoint, and attach it to udev */
392 INIT_LIST_HEAD(&uep->ep_list);
393 list_add_tail(&uep->ep_list, &udev->ep_list_head);
394
395 /*
396 * usbhs_pipe_config_update() should be called after
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700397 * usbhs_set_device_config()
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700398 * see
399 * DCPMAXP/PIPEMAXP
400 */
Kuninori Morimotod7a00ec2011-10-24 02:25:28 -0700401 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700402 usbhs_pipe_config_update(uep->pipe,
403 usbhsh_device_number(hpriv, udev),
404 usb_endpoint_num(desc),
405 uep->maxp);
406
407 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
408 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700409 usbhs_pipe_name(uep->pipe), uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700410
411 return uep;
412}
413
414void usbhsh_endpoint_free(struct usbhsh_hpriv *hpriv,
415 struct usb_host_endpoint *ep)
416{
417 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
418 struct device *dev = usbhs_priv_to_dev(priv);
419 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700420 struct usbhsh_pipe_info *info;
421
422 if (!uep)
423 return;
424
425 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
Kuninori Morimoto55b5a622011-10-17 18:04:37 -0700426 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700427 usbhs_pipe_name(uep->pipe), uep);
428
429 info = usbhsh_pipe_info(uep->pipe);
430 info->usr_cnt--;
431
432 /* remove this endpoint from udev */
433 list_del_init(&uep->ep_list);
434
435 usbhsh_uep_to_udev(uep) = NULL;
436 usbhsh_ep_to_uep(ep) = NULL;
437
438 kfree(uep);
439}
440
441/*
442 * queue push/pop
443 */
444static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
445{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700446 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700447 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
448 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
449 struct urb *urb = ureq->urb;
450 struct device *dev = usbhs_priv_to_dev(priv);
451
452 dev_dbg(dev, "%s\n", __func__);
453
454 if (!urb) {
455 dev_warn(dev, "pkt doesn't have urb\n");
456 return;
457 }
458
459 urb->actual_length = pkt->actual;
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700460 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700461
462 usb_hcd_unlink_urb_from_ep(hcd, urb);
463 usb_hcd_giveback_urb(hcd, urb, 0);
464}
465
466static int usbhsh_queue_push(struct usb_hcd *hcd,
467 struct usbhs_pipe *pipe,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700468 struct urb *urb,
469 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700470{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700471 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700472 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700473 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700474 void *buf;
475 int len;
476
477 if (usb_pipeisoc(urb->pipe)) {
478 dev_err(dev, "pipe iso is not supported now\n");
479 return -EIO;
480 }
481
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700482 /* this ureq will be freed on usbhsh_queue_done() */
483 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
484 if (unlikely(!ureq)) {
485 dev_err(dev, "ureq alloc fail\n");
486 return -ENOMEM;
487 }
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700488
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700489 if (usb_pipein(urb->pipe))
490 pipe->handler = &usbhs_fifo_pio_pop_handler;
491 else
492 pipe->handler = &usbhs_fifo_pio_push_handler;
493
494 buf = (void *)(urb->transfer_buffer + urb->actual_length);
495 len = urb->transfer_buffer_length - urb->actual_length;
496
497 dev_dbg(dev, "%s\n", __func__);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700498 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700499 buf, len, (urb->transfer_flags & URB_ZERO_PACKET));
500 usbhs_pkt_start(pipe);
501
502 return 0;
503}
504
505/*
506 * DCP setup stage
507 */
508static int usbhsh_is_request_address(struct urb *urb)
509{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700510 struct usb_ctrlrequest *req;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700511
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700512 req = (struct usb_ctrlrequest *)urb->setup_packet;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700513
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700514 if ((DeviceOutRequest == req->bRequestType << 8) &&
515 (USB_REQ_SET_ADDRESS == req->bRequest))
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700516 return 1;
517 else
518 return 0;
519}
520
521static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
522 struct urb *urb,
523 struct usbhs_pipe *pipe)
524{
525 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
526 struct usb_ctrlrequest req;
527 struct device *dev = usbhs_priv_to_dev(priv);
528
529 /*
530 * wait setup packet ACK
531 * see
532 * usbhsh_irq_setup_ack()
533 * usbhsh_irq_setup_err()
534 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700535 init_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700536
537 /* copy original request */
538 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
539
540 /*
541 * renesas_usbhs can not use original usb address.
542 * see HARDWARE LIMITATION.
543 * modify usb address here.
544 */
545 if (usbhsh_is_request_address(urb)) {
546 /* FIXME */
547 req.wValue = 1;
548 dev_dbg(dev, "create new address - %d\n", req.wValue);
549 }
550
551 /* set request */
552 usbhs_usbreq_set_val(priv, &req);
553
554 /*
555 * wait setup packet ACK
556 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700557 wait_for_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700558
559 dev_dbg(dev, "%s done\n", __func__);
560}
561
562/*
563 * DCP data stage
564 */
565static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
566 struct usbhs_pkt *pkt)
567{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700568 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700569 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700570
571 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
572
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700573 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700574}
575
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700576static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
577 struct urb *urb,
578 struct usbhs_pipe *pipe,
579 gfp_t mem_flags)
580
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700581{
582 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700583
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700584 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
585 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
586 if (unlikely(!ureq))
587 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700588
589 if (usb_pipein(urb->pipe))
590 pipe->handler = &usbhs_dcp_data_stage_in_handler;
591 else
592 pipe->handler = &usbhs_dcp_data_stage_out_handler;
593
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700594 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700595 usbhsh_data_stage_packet_done,
596 urb->transfer_buffer,
597 urb->transfer_buffer_length,
598 (urb->transfer_flags & URB_ZERO_PACKET));
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700599
600 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700601}
602
603/*
604 * DCP status stage
605 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700606static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700607 struct urb *urb,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700608 struct usbhs_pipe *pipe,
609 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700610{
611 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700612
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700613 /* This ureq will be freed on usbhsh_queue_done() */
614 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
615 if (unlikely(!ureq))
616 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700617
618 if (usb_pipein(urb->pipe))
619 pipe->handler = &usbhs_dcp_status_stage_in_handler;
620 else
621 pipe->handler = &usbhs_dcp_status_stage_out_handler;
622
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700623 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700624 usbhsh_queue_done,
625 NULL,
626 urb->transfer_buffer_length,
627 0);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700628
629 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700630}
631
632static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700633 struct usbhs_pipe *pipe,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700634 struct urb *urb,
635 gfp_t mflags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700636{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700637 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700638 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700639 int ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700640
641 dev_dbg(dev, "%s\n", __func__);
642
643 /*
644 * setup stage
645 *
646 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
647 */
648 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
649
650 /*
651 * data stage
652 *
653 * It is pushed only when urb has buffer.
654 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700655 if (urb->transfer_buffer_length) {
656 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
657 if (ret < 0) {
658 dev_err(dev, "data stage failed\n");
659 return ret;
660 }
661 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700662
663 /*
664 * status stage
665 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700666 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
667 if (ret < 0) {
668 dev_err(dev, "status stage failed\n");
669 return ret;
670 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700671
672 /*
673 * start pushed packets
674 */
675 usbhs_pkt_start(pipe);
676
677 return 0;
678}
679
680/*
681 * dma map functions
682 */
683static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
684{
685 return 0;
686}
687
688/*
689 * for hc_driver
690 */
691static int usbhsh_host_start(struct usb_hcd *hcd)
692{
693 return 0;
694}
695
696static void usbhsh_host_stop(struct usb_hcd *hcd)
697{
698}
699
700static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
701 struct urb *urb,
702 gfp_t mem_flags)
703{
704 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
705 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
706 struct device *dev = usbhs_priv_to_dev(priv);
707 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
708 struct usb_host_endpoint *ep = urb->ep;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700709 struct usbhsh_device *udev, *new_udev = NULL;
710 struct usbhs_pipe *pipe;
711 struct usbhsh_ep *uep;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700712 int is_dir_in = usb_pipein(urb->pipe);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700713
714 int ret;
715
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700716 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700717
718 ret = usb_hcd_link_urb_to_ep(hcd, urb);
719 if (ret)
720 goto usbhsh_urb_enqueue_error_not_linked;
721
722 /*
723 * get udev
724 */
725 udev = usbhsh_usbv_to_udev(usbv);
726 if (!udev) {
727 new_udev = usbhsh_device_alloc(hpriv, urb);
728 if (!new_udev)
729 goto usbhsh_urb_enqueue_error_not_linked;
730
731 udev = new_udev;
732 }
733
734 /*
735 * get uep
736 */
737 uep = usbhsh_ep_to_uep(ep);
738 if (!uep) {
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700739 uep = usbhsh_endpoint_alloc(hpriv, udev, ep,
740 is_dir_in, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700741 if (!uep)
742 goto usbhsh_urb_enqueue_error_free_device;
743 }
744 pipe = usbhsh_uep_to_pipe(uep);
745
746 /*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700747 * push packet
748 */
749 if (usb_pipecontrol(urb->pipe))
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700750 ret = usbhsh_dcp_queue_push(hcd, pipe, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700751 else
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700752 ret = usbhsh_queue_push(hcd, pipe, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700753
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700754 return ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700755
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700756usbhsh_urb_enqueue_error_free_device:
757 if (new_udev)
758 usbhsh_device_free(hpriv, new_udev);
759usbhsh_urb_enqueue_error_not_linked:
760
761 dev_dbg(dev, "%s error\n", __func__);
762
763 return ret;
764}
765
766static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
767{
768 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
769 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
770
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700771 if (ureq)
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700772 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700773
774 return 0;
775}
776
777static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
778 struct usb_host_endpoint *ep)
779{
780 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
781 struct usbhsh_device *udev;
782 struct usbhsh_hpriv *hpriv;
783
784 /*
785 * this function might be called manytimes by same hcd/ep
Kuninori Morimotofca8ab72011-10-31 00:47:24 -0700786 * in-endpoint == out-endpoint if ep == dcp.
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700787 */
788 if (!uep)
789 return;
790
791 udev = usbhsh_uep_to_udev(uep);
792 hpriv = usbhsh_hcd_to_hpriv(hcd);
793
794 usbhsh_endpoint_free(hpriv, ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700795
796 /*
797 * if there is no endpoint,
798 * free device
799 */
800 if (!usbhsh_device_has_endpoint(udev))
801 usbhsh_device_free(hpriv, udev);
802}
803
804static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
805{
806 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
807 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
808 struct device *dev = usbhs_priv_to_dev(priv);
809 int roothub_id = 1; /* only 1 root hub */
810
811 /*
812 * does port stat was changed ?
813 * check USB_PORT_STAT_C_xxx << 16
814 */
815 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
816 *buf = (1 << roothub_id);
817 else
818 *buf = 0;
819
820 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
821
822 return !!(*buf);
823}
824
825static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
826 u16 typeReq, u16 wValue,
827 u16 wIndex, char *buf, u16 wLength)
828{
829 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
830 struct device *dev = usbhs_priv_to_dev(priv);
831
832 switch (wValue) {
833 case C_HUB_OVER_CURRENT:
834 case C_HUB_LOCAL_POWER:
835 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
836 return 0;
837 }
838
839 return -EPIPE;
840}
841
842static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
843 u16 typeReq, u16 wValue,
844 u16 wIndex, char *buf, u16 wLength)
845{
846 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
847 struct device *dev = usbhs_priv_to_dev(priv);
848 int enable = (typeReq == SetPortFeature);
849 int speed, i, timeout = 128;
850 int roothub_id = 1; /* only 1 root hub */
851
852 /* common error */
853 if (wIndex > roothub_id || wLength != 0)
854 return -EPIPE;
855
856 /* check wValue */
857 switch (wValue) {
858 case USB_PORT_FEAT_POWER:
859 usbhs_vbus_ctrl(priv, enable);
860 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
861 break;
862
863 case USB_PORT_FEAT_ENABLE:
864 case USB_PORT_FEAT_SUSPEND:
865 case USB_PORT_FEAT_C_ENABLE:
866 case USB_PORT_FEAT_C_SUSPEND:
867 case USB_PORT_FEAT_C_CONNECTION:
868 case USB_PORT_FEAT_C_OVER_CURRENT:
869 case USB_PORT_FEAT_C_RESET:
870 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
871 break;
872
873 case USB_PORT_FEAT_RESET:
874 if (!enable)
875 break;
876
877 usbhsh_port_stat_clear(hpriv,
878 USB_PORT_STAT_HIGH_SPEED |
879 USB_PORT_STAT_LOW_SPEED);
880
881 usbhs_bus_send_reset(priv);
882 msleep(20);
883 usbhs_bus_send_sof_enable(priv);
884
885 for (i = 0; i < timeout ; i++) {
886 switch (usbhs_bus_get_speed(priv)) {
887 case USB_SPEED_LOW:
888 speed = USB_PORT_STAT_LOW_SPEED;
889 goto got_usb_bus_speed;
890 case USB_SPEED_HIGH:
891 speed = USB_PORT_STAT_HIGH_SPEED;
892 goto got_usb_bus_speed;
893 case USB_SPEED_FULL:
894 speed = 0;
895 goto got_usb_bus_speed;
896 }
897
898 msleep(20);
899 }
900 return -EPIPE;
901
902got_usb_bus_speed:
903 usbhsh_port_stat_set(hpriv, speed);
904 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
905
906 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
907 __func__, speed);
908
909 /* status change is not needed */
910 return 0;
911
912 default:
913 return -EPIPE;
914 }
915
916 /* set/clear status */
917 if (enable)
918 usbhsh_port_stat_set(hpriv, (1 << wValue));
919 else
920 usbhsh_port_stat_clear(hpriv, (1 << wValue));
921
922 return 0;
923}
924
925static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
926 u16 typeReq, u16 wValue,
927 u16 wIndex, char *buf, u16 wLength)
928{
929 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
930 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
931 struct device *dev = usbhs_priv_to_dev(priv);
932 int roothub_id = 1; /* only 1 root hub */
933
934 switch (typeReq) {
935 case GetHubStatus:
936 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
937
938 *buf = 0x00;
939 break;
940
941 case GetPortStatus:
942 if (wIndex != roothub_id)
943 return -EPIPE;
944
945 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
946 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
947 break;
948
949 case GetHubDescriptor:
950 desc->bDescriptorType = 0x29;
951 desc->bHubContrCurrent = 0;
952 desc->bNbrPorts = roothub_id;
953 desc->bDescLength = 9;
954 desc->bPwrOn2PwrGood = 0;
955 desc->wHubCharacteristics = cpu_to_le16(0x0011);
956 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
957 desc->u.hs.DeviceRemovable[1] = ~0;
958 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
959 break;
960 }
961
962 return 0;
963}
964
965static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
966 u16 wIndex, char *buf, u16 wLength)
967{
968 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
969 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
970 struct device *dev = usbhs_priv_to_dev(priv);
971 int ret = -EPIPE;
972
973 switch (typeReq) {
974
975 /* Hub Feature */
976 case ClearHubFeature:
977 case SetHubFeature:
978 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
979 wValue, wIndex, buf, wLength);
980 break;
981
982 /* Port Feature */
983 case SetPortFeature:
984 case ClearPortFeature:
985 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
986 wValue, wIndex, buf, wLength);
987 break;
988
989 /* Get status */
990 case GetHubStatus:
991 case GetPortStatus:
992 case GetHubDescriptor:
993 ret = __usbhsh_hub_get_status(hpriv, typeReq,
994 wValue, wIndex, buf, wLength);
995 break;
996 }
997
998 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
999 typeReq, ret, usbhsh_port_stat_get(hpriv));
1000
1001 return ret;
1002}
1003
1004static struct hc_driver usbhsh_driver = {
1005 .description = usbhsh_hcd_name,
1006 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1007
1008 /*
1009 * generic hardware linkage
1010 */
1011 .flags = HCD_USB2,
1012
1013 .start = usbhsh_host_start,
1014 .stop = usbhsh_host_stop,
1015
1016 /*
1017 * managing i/o requests and associated device resources
1018 */
1019 .urb_enqueue = usbhsh_urb_enqueue,
1020 .urb_dequeue = usbhsh_urb_dequeue,
1021 .endpoint_disable = usbhsh_endpoint_disable,
1022
1023 /*
1024 * root hub
1025 */
1026 .hub_status_data = usbhsh_hub_status_data,
1027 .hub_control = usbhsh_hub_control,
1028};
1029
1030/*
1031 * interrupt functions
1032 */
1033static int usbhsh_irq_attch(struct usbhs_priv *priv,
1034 struct usbhs_irq_state *irq_state)
1035{
1036 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1037 struct device *dev = usbhs_priv_to_dev(priv);
1038
1039 dev_dbg(dev, "device attached\n");
1040
1041 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1042 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1043
1044 return 0;
1045}
1046
1047static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1048 struct usbhs_irq_state *irq_state)
1049{
1050 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1051 struct device *dev = usbhs_priv_to_dev(priv);
1052
1053 dev_dbg(dev, "device detached\n");
1054
1055 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1056 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1057
1058 return 0;
1059}
1060
1061static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1062 struct usbhs_irq_state *irq_state)
1063{
1064 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1065 struct device *dev = usbhs_priv_to_dev(priv);
1066
1067 dev_dbg(dev, "setup packet OK\n");
1068
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001069 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001070
1071 return 0;
1072}
1073
1074static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1075 struct usbhs_irq_state *irq_state)
1076{
1077 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1078 struct device *dev = usbhs_priv_to_dev(priv);
1079
1080 dev_dbg(dev, "setup packet Err\n");
1081
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001082 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001083
1084 return 0;
1085}
1086
1087/*
1088 * module start/stop
1089 */
1090static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1091{
1092 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1093 struct usbhsh_pipe_info *pipe_info = hpriv->pipe_info;
1094 struct usbhs_pipe *pipe;
1095 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1096 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1097 int old_type, dir_in, i;
1098
1099 /* init all pipe */
1100 old_type = USB_ENDPOINT_XFER_CONTROL;
1101 for (i = 0; i < pipe_size; i++) {
1102 pipe_info[i].usr_cnt = 0;
1103
1104 /*
1105 * data "output" will be finished as soon as possible,
1106 * but there is no guaranty at data "input" case.
1107 *
1108 * "input" needs "standby" pipe.
1109 * So, "input" direction pipe > "output" direction pipe
1110 * is good idea.
1111 *
1112 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1113 * and the other will be input direction here.
1114 *
1115 * ex)
1116 * ...
1117 * USB_ENDPOINT_XFER_ISOC -> dir out
1118 * USB_ENDPOINT_XFER_ISOC -> dir in
1119 * USB_ENDPOINT_XFER_BULK -> dir out
1120 * USB_ENDPOINT_XFER_BULK -> dir in
1121 * USB_ENDPOINT_XFER_BULK -> dir in
1122 * ...
1123 */
1124 dir_in = (pipe_type[i] == old_type);
1125 old_type = pipe_type[i];
1126
1127 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1128 pipe = usbhs_dcp_malloc(priv);
1129 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1130 } else {
1131 pipe = usbhs_pipe_malloc(priv,
1132 pipe_type[i],
1133 dir_in);
1134 }
1135
1136 pipe->mod_private = pipe_info + i;
1137 }
1138}
1139
1140static int usbhsh_start(struct usbhs_priv *priv)
1141{
1142 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1143 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1144 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1145 struct device *dev = usbhs_priv_to_dev(priv);
1146 int ret;
1147
1148 /* add hcd */
1149 ret = usb_add_hcd(hcd, 0, 0);
1150 if (ret < 0)
1151 return 0;
1152
1153 /*
1154 * pipe initialize and enable DCP
1155 */
1156 usbhs_pipe_init(priv,
1157 usbhsh_dma_map_ctrl);
1158 usbhs_fifo_init(priv);
1159 usbhsh_pipe_init_for_host(priv);
1160
1161 /*
1162 * system config enble
1163 * - HI speed
1164 * - host
1165 * - usb module
1166 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001167 usbhs_sys_host_ctrl(priv, 1);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001168
1169 /*
1170 * enable irq callback
1171 */
1172 mod->irq_attch = usbhsh_irq_attch;
1173 mod->irq_dtch = usbhsh_irq_dtch;
1174 mod->irq_sack = usbhsh_irq_setup_ack;
1175 mod->irq_sign = usbhsh_irq_setup_err;
1176 usbhs_irq_callback_update(priv, mod);
1177
1178 dev_dbg(dev, "start host\n");
1179
1180 return ret;
1181}
1182
1183static int usbhsh_stop(struct usbhs_priv *priv)
1184{
1185 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1186 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001187 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001188 struct device *dev = usbhs_priv_to_dev(priv);
1189
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001190 /*
1191 * disable irq callback
1192 */
1193 mod->irq_attch = NULL;
1194 mod->irq_dtch = NULL;
1195 mod->irq_sack = NULL;
1196 mod->irq_sign = NULL;
1197 usbhs_irq_callback_update(priv, mod);
1198
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001199 usb_remove_hcd(hcd);
1200
1201 /* disable sys */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001202 usbhs_sys_host_ctrl(priv, 0);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001203
1204 dev_dbg(dev, "quit host\n");
1205
1206 return 0;
1207}
1208
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001209int usbhs_mod_host_probe(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001210{
1211 struct usbhsh_hpriv *hpriv;
1212 struct usb_hcd *hcd;
1213 struct usbhsh_pipe_info *pipe_info;
1214 struct usbhsh_device *udev;
1215 struct device *dev = usbhs_priv_to_dev(priv);
1216 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1217 int i;
1218
1219 /* initialize hcd */
1220 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1221 if (!hcd) {
1222 dev_err(dev, "Failed to create hcd\n");
1223 return -ENOMEM;
1224 }
1225
1226 pipe_info = kzalloc(sizeof(*pipe_info) * pipe_size, GFP_KERNEL);
1227 if (!pipe_info) {
1228 dev_err(dev, "Could not allocate pipe_info\n");
1229 goto usbhs_mod_host_probe_err;
1230 }
1231
1232 /*
1233 * CAUTION
1234 *
1235 * There is no guarantee that it is possible to access usb module here.
1236 * Don't accesses to it.
1237 * The accesse will be enable after "usbhsh_start"
1238 */
1239
1240 hpriv = usbhsh_hcd_to_hpriv(hcd);
1241
1242 /*
1243 * register itself
1244 */
1245 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1246
1247 /* init hpriv */
1248 hpriv->mod.name = "host";
1249 hpriv->mod.start = usbhsh_start;
1250 hpriv->mod.stop = usbhsh_stop;
1251 hpriv->pipe_info = pipe_info;
1252 hpriv->pipe_size = pipe_size;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001253 usbhsh_port_stat_init(hpriv);
1254
1255 /* init all device */
1256 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1257 udev->usbv = NULL;
1258 INIT_LIST_HEAD(&udev->ep_list_head);
1259 }
1260
1261 dev_info(dev, "host probed\n");
1262
1263 return 0;
1264
1265usbhs_mod_host_probe_err:
1266 usb_put_hcd(hcd);
1267
1268 return -ENOMEM;
1269}
1270
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001271int usbhs_mod_host_remove(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001272{
1273 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1274 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1275
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001276 usb_put_hcd(hcd);
1277
1278 return 0;
1279}