blob: e6fd044adfa32a802af7622701cf01b2ba224d51 [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)
Kuninori Morimotoab142302011-10-31 00:48:00 -0700156#define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700157
158#define usbhsh_pipe_info(p) ((p)->mod_private)
159
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700160#define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
161#define usbhsh_device_hubport(d) ((d)->usbv->portnum)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700162#define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
163#define usbhsh_device_nth(h, d) ((h)->udev + d)
164#define usbhsh_device0(h) usbhsh_device_nth(h, 0)
165
166#define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
167#define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
168#define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
169#define usbhsh_port_stat_get(h) ((h)->port_stat)
170
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700171#define usbhsh_pkt_to_ureq(p) \
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700172 container_of((void *)p, struct usbhsh_request, pkt)
173
174/*
175 * req alloc/free
176 */
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700177static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700178 struct urb *urb,
179 gfp_t mem_flags)
180{
181 struct usbhsh_request *ureq;
182 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
183 struct device *dev = usbhs_priv_to_dev(priv);
184
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700185 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700186 if (!ureq) {
187 dev_err(dev, "ureq alloc fail\n");
188 return NULL;
189 }
190
191 usbhs_pkt_init(&ureq->pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700192 ureq->urb = urb;
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700193 usbhsh_urb_to_ureq(urb) = ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700194
195 return ureq;
196}
197
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700198static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700199 struct usbhsh_request *ureq)
200{
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700201 usbhsh_urb_to_ureq(ureq->urb) = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700202 ureq->urb = NULL;
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700203
204 kfree(ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700205}
206
207/*
208 * device control
209 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700210static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
211 struct usbhsh_device *udev)
212{
213 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
214
215 return hcd->self.root_hub == usbv->parent;
216}
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700217
218static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
219{
220 return !list_empty(&udev->ep_list_head);
221}
222
223static struct usbhsh_device *usbhsh_device_alloc(struct usbhsh_hpriv *hpriv,
224 struct urb *urb)
225{
226 struct usbhsh_device *udev = NULL;
227 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
228 struct device *dev = usbhsh_hcd_to_dev(hcd);
229 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
230 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700231 u16 upphub, hubport;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700232 int i;
233
234 /*
Kuninori Morimotoab142302011-10-31 00:48:00 -0700235 * find device
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700236 */
237 if (0 == usb_pipedevice(urb->pipe)) {
Kuninori Morimotoab142302011-10-31 00:48:00 -0700238 /*
239 * device0 is special case
240 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700241 udev = usbhsh_device0(hpriv);
Kuninori Morimotoab142302011-10-31 00:48:00 -0700242 if (usbhsh_udev_is_used(udev))
243 udev = NULL;
244 } else {
245 struct usbhsh_device *pos;
246
247 /*
248 * find unused device
249 */
250 usbhsh_for_each_udev(pos, hpriv, i) {
251 if (usbhsh_udev_is_used(pos))
252 continue;
253 udev = pos;
254 break;
255 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700256 }
257
Kuninori Morimotoab142302011-10-31 00:48:00 -0700258 if (!udev) {
259 dev_err(dev, "no free usbhsh_device\n");
260 return NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700261 }
262
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700263 if (usbhsh_device_has_endpoint(udev))
264 dev_warn(dev, "udev have old endpoint\n");
265
266 /* uep will be attached */
267 INIT_LIST_HEAD(&udev->ep_list_head);
268
269 /*
270 * usbhsh_usbv_to_udev()
271 * usbhsh_udev_to_usbv()
272 * will be enable
273 */
274 dev_set_drvdata(&usbv->dev, udev);
275 udev->usbv = usbv;
276
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700277 upphub = 0;
278 hubport = 0;
279 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
280 /* if udev is not connected to rhdev, it means parent is Hub */
281 struct usbhsh_device *parent = usbhsh_device_parent(udev);
282
283 upphub = usbhsh_device_number(hpriv, parent);
284 hubport = usbhsh_device_hubport(udev);
285
286 dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
287 upphub, hubport, parent);
288 }
289
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700290 /* set device config */
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700291 usbhs_set_device_config(priv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700292 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700293 upphub, hubport, usbv->speed);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700294
295 dev_dbg(dev, "%s [%d](%p)\n", __func__,
296 usbhsh_device_number(hpriv, udev), udev);
297
298 return udev;
299}
300
301static void usbhsh_device_free(struct usbhsh_hpriv *hpriv,
302 struct usbhsh_device *udev)
303{
304 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
305 struct device *dev = usbhsh_hcd_to_dev(hcd);
306 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
307
308 dev_dbg(dev, "%s [%d](%p)\n", __func__,
309 usbhsh_device_number(hpriv, udev), udev);
310
311 if (usbhsh_device_has_endpoint(udev))
312 dev_warn(dev, "udev still have endpoint\n");
313
314 /*
315 * usbhsh_usbv_to_udev()
316 * usbhsh_udev_to_usbv()
317 * will be disable
318 */
319 dev_set_drvdata(&usbv->dev, NULL);
320 udev->usbv = NULL;
321}
322
323/*
324 * end-point control
325 */
326struct usbhsh_ep *usbhsh_endpoint_alloc(struct usbhsh_hpriv *hpriv,
327 struct usbhsh_device *udev,
328 struct usb_host_endpoint *ep,
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700329 int dir_in_req,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700330 gfp_t mem_flags)
331{
332 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
333 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
334 struct usbhsh_ep *uep;
335 struct usbhsh_pipe_info *info;
336 struct usbhs_pipe *pipe, *best_pipe;
337 struct device *dev = usbhsh_hcd_to_dev(hcd);
338 struct usb_endpoint_descriptor *desc = &ep->desc;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700339 int type, i, dir_in;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700340 unsigned int min_usr;
341
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700342 dir_in_req = !!dir_in_req;
343
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700344 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
345 if (!uep) {
346 dev_err(dev, "usbhsh_ep alloc fail\n");
347 return NULL;
348 }
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700349
350 if (usb_endpoint_xfer_control(desc)) {
351 best_pipe = usbhsh_hpriv_to_dcp(hpriv);
352 goto usbhsh_endpoint_alloc_find_pipe;
353 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700354
355 /*
356 * find best pipe for endpoint
357 * see
358 * HARDWARE LIMITATION
359 */
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700360 type = usb_endpoint_type(desc);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700361 min_usr = ~0;
362 best_pipe = NULL;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700363 usbhs_for_each_pipe(pipe, priv, i) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700364 if (!usbhs_pipe_type_is(pipe, type))
365 continue;
366
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700367 dir_in = !!usbhs_pipe_is_dir_in(pipe);
368 if (0 != (dir_in - dir_in_req))
369 continue;
370
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700371 info = usbhsh_pipe_info(pipe);
372
373 if (min_usr > info->usr_cnt) {
374 min_usr = info->usr_cnt;
375 best_pipe = pipe;
376 }
377 }
378
379 if (unlikely(!best_pipe)) {
380 dev_err(dev, "couldn't find best pipe\n");
381 kfree(uep);
382 return NULL;
383 }
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700384usbhsh_endpoint_alloc_find_pipe:
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700385 /*
386 * init uep
387 */
388 uep->pipe = best_pipe;
389 uep->maxp = usb_endpoint_maxp(desc);
390 usbhsh_uep_to_udev(uep) = udev;
391 usbhsh_ep_to_uep(ep) = uep;
392
393 /*
394 * update pipe user count
395 */
396 info = usbhsh_pipe_info(best_pipe);
397 info->usr_cnt++;
398
399 /* init this endpoint, and attach it to udev */
400 INIT_LIST_HEAD(&uep->ep_list);
401 list_add_tail(&uep->ep_list, &udev->ep_list_head);
402
403 /*
404 * usbhs_pipe_config_update() should be called after
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700405 * usbhs_set_device_config()
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700406 * see
407 * DCPMAXP/PIPEMAXP
408 */
Kuninori Morimotod7a00ec2011-10-24 02:25:28 -0700409 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700410 usbhs_pipe_config_update(uep->pipe,
411 usbhsh_device_number(hpriv, udev),
412 usb_endpoint_num(desc),
413 uep->maxp);
414
415 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
416 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700417 usbhs_pipe_name(uep->pipe), uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700418
419 return uep;
420}
421
422void usbhsh_endpoint_free(struct usbhsh_hpriv *hpriv,
423 struct usb_host_endpoint *ep)
424{
425 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
426 struct device *dev = usbhs_priv_to_dev(priv);
427 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700428 struct usbhsh_pipe_info *info;
429
430 if (!uep)
431 return;
432
433 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
Kuninori Morimoto55b5a622011-10-17 18:04:37 -0700434 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700435 usbhs_pipe_name(uep->pipe), uep);
436
437 info = usbhsh_pipe_info(uep->pipe);
438 info->usr_cnt--;
439
440 /* remove this endpoint from udev */
441 list_del_init(&uep->ep_list);
442
443 usbhsh_uep_to_udev(uep) = NULL;
444 usbhsh_ep_to_uep(ep) = NULL;
445
446 kfree(uep);
447}
448
449/*
450 * queue push/pop
451 */
452static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
453{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700454 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700455 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
456 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
457 struct urb *urb = ureq->urb;
458 struct device *dev = usbhs_priv_to_dev(priv);
459
460 dev_dbg(dev, "%s\n", __func__);
461
462 if (!urb) {
463 dev_warn(dev, "pkt doesn't have urb\n");
464 return;
465 }
466
467 urb->actual_length = pkt->actual;
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700468 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700469
470 usb_hcd_unlink_urb_from_ep(hcd, urb);
471 usb_hcd_giveback_urb(hcd, urb, 0);
472}
473
474static int usbhsh_queue_push(struct usb_hcd *hcd,
475 struct usbhs_pipe *pipe,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700476 struct urb *urb,
477 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700478{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700479 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700480 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700481 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700482 void *buf;
483 int len;
484
485 if (usb_pipeisoc(urb->pipe)) {
486 dev_err(dev, "pipe iso is not supported now\n");
487 return -EIO;
488 }
489
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700490 /* this ureq will be freed on usbhsh_queue_done() */
491 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
492 if (unlikely(!ureq)) {
493 dev_err(dev, "ureq alloc fail\n");
494 return -ENOMEM;
495 }
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700496
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700497 if (usb_pipein(urb->pipe))
498 pipe->handler = &usbhs_fifo_pio_pop_handler;
499 else
500 pipe->handler = &usbhs_fifo_pio_push_handler;
501
502 buf = (void *)(urb->transfer_buffer + urb->actual_length);
503 len = urb->transfer_buffer_length - urb->actual_length;
504
505 dev_dbg(dev, "%s\n", __func__);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700506 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700507 buf, len, (urb->transfer_flags & URB_ZERO_PACKET));
508 usbhs_pkt_start(pipe);
509
510 return 0;
511}
512
513/*
514 * DCP setup stage
515 */
516static int usbhsh_is_request_address(struct urb *urb)
517{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700518 struct usb_ctrlrequest *req;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700519
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700520 req = (struct usb_ctrlrequest *)urb->setup_packet;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700521
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700522 if ((DeviceOutRequest == req->bRequestType << 8) &&
523 (USB_REQ_SET_ADDRESS == req->bRequest))
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700524 return 1;
525 else
526 return 0;
527}
528
529static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
530 struct urb *urb,
531 struct usbhs_pipe *pipe)
532{
533 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
534 struct usb_ctrlrequest req;
535 struct device *dev = usbhs_priv_to_dev(priv);
536
537 /*
538 * wait setup packet ACK
539 * see
540 * usbhsh_irq_setup_ack()
541 * usbhsh_irq_setup_err()
542 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700543 init_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700544
545 /* copy original request */
546 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
547
548 /*
549 * renesas_usbhs can not use original usb address.
550 * see HARDWARE LIMITATION.
551 * modify usb address here.
552 */
553 if (usbhsh_is_request_address(urb)) {
554 /* FIXME */
555 req.wValue = 1;
556 dev_dbg(dev, "create new address - %d\n", req.wValue);
557 }
558
559 /* set request */
560 usbhs_usbreq_set_val(priv, &req);
561
562 /*
563 * wait setup packet ACK
564 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700565 wait_for_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700566
567 dev_dbg(dev, "%s done\n", __func__);
568}
569
570/*
571 * DCP data stage
572 */
573static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
574 struct usbhs_pkt *pkt)
575{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700576 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700577 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700578
579 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
580
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700581 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700582}
583
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700584static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
585 struct urb *urb,
586 struct usbhs_pipe *pipe,
587 gfp_t mem_flags)
588
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700589{
590 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700591
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700592 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
593 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
594 if (unlikely(!ureq))
595 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700596
597 if (usb_pipein(urb->pipe))
598 pipe->handler = &usbhs_dcp_data_stage_in_handler;
599 else
600 pipe->handler = &usbhs_dcp_data_stage_out_handler;
601
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700602 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700603 usbhsh_data_stage_packet_done,
604 urb->transfer_buffer,
605 urb->transfer_buffer_length,
606 (urb->transfer_flags & URB_ZERO_PACKET));
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700607
608 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700609}
610
611/*
612 * DCP status stage
613 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700614static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700615 struct urb *urb,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700616 struct usbhs_pipe *pipe,
617 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700618{
619 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700620
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700621 /* This ureq will be freed on usbhsh_queue_done() */
622 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
623 if (unlikely(!ureq))
624 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700625
626 if (usb_pipein(urb->pipe))
627 pipe->handler = &usbhs_dcp_status_stage_in_handler;
628 else
629 pipe->handler = &usbhs_dcp_status_stage_out_handler;
630
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700631 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700632 usbhsh_queue_done,
633 NULL,
634 urb->transfer_buffer_length,
635 0);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700636
637 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700638}
639
640static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700641 struct usbhs_pipe *pipe,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700642 struct urb *urb,
643 gfp_t mflags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700644{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700645 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700646 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700647 int ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700648
649 dev_dbg(dev, "%s\n", __func__);
650
651 /*
652 * setup stage
653 *
654 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
655 */
656 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
657
658 /*
659 * data stage
660 *
661 * It is pushed only when urb has buffer.
662 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700663 if (urb->transfer_buffer_length) {
664 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
665 if (ret < 0) {
666 dev_err(dev, "data stage failed\n");
667 return ret;
668 }
669 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700670
671 /*
672 * status stage
673 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700674 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
675 if (ret < 0) {
676 dev_err(dev, "status stage failed\n");
677 return ret;
678 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700679
680 /*
681 * start pushed packets
682 */
683 usbhs_pkt_start(pipe);
684
685 return 0;
686}
687
688/*
689 * dma map functions
690 */
691static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
692{
693 return 0;
694}
695
696/*
697 * for hc_driver
698 */
699static int usbhsh_host_start(struct usb_hcd *hcd)
700{
701 return 0;
702}
703
704static void usbhsh_host_stop(struct usb_hcd *hcd)
705{
706}
707
708static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
709 struct urb *urb,
710 gfp_t mem_flags)
711{
712 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
713 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
714 struct device *dev = usbhs_priv_to_dev(priv);
715 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
716 struct usb_host_endpoint *ep = urb->ep;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700717 struct usbhsh_device *udev, *new_udev = NULL;
718 struct usbhs_pipe *pipe;
719 struct usbhsh_ep *uep;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700720 int is_dir_in = usb_pipein(urb->pipe);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700721
722 int ret;
723
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700724 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700725
726 ret = usb_hcd_link_urb_to_ep(hcd, urb);
727 if (ret)
728 goto usbhsh_urb_enqueue_error_not_linked;
729
730 /*
731 * get udev
732 */
733 udev = usbhsh_usbv_to_udev(usbv);
734 if (!udev) {
735 new_udev = usbhsh_device_alloc(hpriv, urb);
736 if (!new_udev)
737 goto usbhsh_urb_enqueue_error_not_linked;
738
739 udev = new_udev;
740 }
741
742 /*
743 * get uep
744 */
745 uep = usbhsh_ep_to_uep(ep);
746 if (!uep) {
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700747 uep = usbhsh_endpoint_alloc(hpriv, udev, ep,
748 is_dir_in, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700749 if (!uep)
750 goto usbhsh_urb_enqueue_error_free_device;
751 }
752 pipe = usbhsh_uep_to_pipe(uep);
753
754 /*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700755 * push packet
756 */
757 if (usb_pipecontrol(urb->pipe))
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700758 ret = usbhsh_dcp_queue_push(hcd, pipe, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700759 else
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700760 ret = usbhsh_queue_push(hcd, pipe, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700761
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700762 return ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700763
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700764usbhsh_urb_enqueue_error_free_device:
765 if (new_udev)
766 usbhsh_device_free(hpriv, new_udev);
767usbhsh_urb_enqueue_error_not_linked:
768
769 dev_dbg(dev, "%s error\n", __func__);
770
771 return ret;
772}
773
774static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
775{
776 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
777 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
778
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700779 if (ureq)
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700780 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700781
782 return 0;
783}
784
785static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
786 struct usb_host_endpoint *ep)
787{
788 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
789 struct usbhsh_device *udev;
790 struct usbhsh_hpriv *hpriv;
791
792 /*
793 * this function might be called manytimes by same hcd/ep
Kuninori Morimotofca8ab72011-10-31 00:47:24 -0700794 * in-endpoint == out-endpoint if ep == dcp.
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700795 */
796 if (!uep)
797 return;
798
799 udev = usbhsh_uep_to_udev(uep);
800 hpriv = usbhsh_hcd_to_hpriv(hcd);
801
802 usbhsh_endpoint_free(hpriv, ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700803
804 /*
805 * if there is no endpoint,
806 * free device
807 */
808 if (!usbhsh_device_has_endpoint(udev))
809 usbhsh_device_free(hpriv, udev);
810}
811
812static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
813{
814 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
815 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
816 struct device *dev = usbhs_priv_to_dev(priv);
817 int roothub_id = 1; /* only 1 root hub */
818
819 /*
820 * does port stat was changed ?
821 * check USB_PORT_STAT_C_xxx << 16
822 */
823 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
824 *buf = (1 << roothub_id);
825 else
826 *buf = 0;
827
828 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
829
830 return !!(*buf);
831}
832
833static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
834 u16 typeReq, u16 wValue,
835 u16 wIndex, char *buf, u16 wLength)
836{
837 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
838 struct device *dev = usbhs_priv_to_dev(priv);
839
840 switch (wValue) {
841 case C_HUB_OVER_CURRENT:
842 case C_HUB_LOCAL_POWER:
843 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
844 return 0;
845 }
846
847 return -EPIPE;
848}
849
850static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
851 u16 typeReq, u16 wValue,
852 u16 wIndex, char *buf, u16 wLength)
853{
854 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
855 struct device *dev = usbhs_priv_to_dev(priv);
856 int enable = (typeReq == SetPortFeature);
857 int speed, i, timeout = 128;
858 int roothub_id = 1; /* only 1 root hub */
859
860 /* common error */
861 if (wIndex > roothub_id || wLength != 0)
862 return -EPIPE;
863
864 /* check wValue */
865 switch (wValue) {
866 case USB_PORT_FEAT_POWER:
867 usbhs_vbus_ctrl(priv, enable);
868 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
869 break;
870
871 case USB_PORT_FEAT_ENABLE:
872 case USB_PORT_FEAT_SUSPEND:
873 case USB_PORT_FEAT_C_ENABLE:
874 case USB_PORT_FEAT_C_SUSPEND:
875 case USB_PORT_FEAT_C_CONNECTION:
876 case USB_PORT_FEAT_C_OVER_CURRENT:
877 case USB_PORT_FEAT_C_RESET:
878 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
879 break;
880
881 case USB_PORT_FEAT_RESET:
882 if (!enable)
883 break;
884
885 usbhsh_port_stat_clear(hpriv,
886 USB_PORT_STAT_HIGH_SPEED |
887 USB_PORT_STAT_LOW_SPEED);
888
889 usbhs_bus_send_reset(priv);
890 msleep(20);
891 usbhs_bus_send_sof_enable(priv);
892
893 for (i = 0; i < timeout ; i++) {
894 switch (usbhs_bus_get_speed(priv)) {
895 case USB_SPEED_LOW:
896 speed = USB_PORT_STAT_LOW_SPEED;
897 goto got_usb_bus_speed;
898 case USB_SPEED_HIGH:
899 speed = USB_PORT_STAT_HIGH_SPEED;
900 goto got_usb_bus_speed;
901 case USB_SPEED_FULL:
902 speed = 0;
903 goto got_usb_bus_speed;
904 }
905
906 msleep(20);
907 }
908 return -EPIPE;
909
910got_usb_bus_speed:
911 usbhsh_port_stat_set(hpriv, speed);
912 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
913
914 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
915 __func__, speed);
916
917 /* status change is not needed */
918 return 0;
919
920 default:
921 return -EPIPE;
922 }
923
924 /* set/clear status */
925 if (enable)
926 usbhsh_port_stat_set(hpriv, (1 << wValue));
927 else
928 usbhsh_port_stat_clear(hpriv, (1 << wValue));
929
930 return 0;
931}
932
933static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
934 u16 typeReq, u16 wValue,
935 u16 wIndex, char *buf, u16 wLength)
936{
937 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
938 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
939 struct device *dev = usbhs_priv_to_dev(priv);
940 int roothub_id = 1; /* only 1 root hub */
941
942 switch (typeReq) {
943 case GetHubStatus:
944 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
945
946 *buf = 0x00;
947 break;
948
949 case GetPortStatus:
950 if (wIndex != roothub_id)
951 return -EPIPE;
952
953 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
954 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
955 break;
956
957 case GetHubDescriptor:
958 desc->bDescriptorType = 0x29;
959 desc->bHubContrCurrent = 0;
960 desc->bNbrPorts = roothub_id;
961 desc->bDescLength = 9;
962 desc->bPwrOn2PwrGood = 0;
963 desc->wHubCharacteristics = cpu_to_le16(0x0011);
964 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
965 desc->u.hs.DeviceRemovable[1] = ~0;
966 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
967 break;
968 }
969
970 return 0;
971}
972
973static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
974 u16 wIndex, char *buf, u16 wLength)
975{
976 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
977 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
978 struct device *dev = usbhs_priv_to_dev(priv);
979 int ret = -EPIPE;
980
981 switch (typeReq) {
982
983 /* Hub Feature */
984 case ClearHubFeature:
985 case SetHubFeature:
986 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
987 wValue, wIndex, buf, wLength);
988 break;
989
990 /* Port Feature */
991 case SetPortFeature:
992 case ClearPortFeature:
993 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
994 wValue, wIndex, buf, wLength);
995 break;
996
997 /* Get status */
998 case GetHubStatus:
999 case GetPortStatus:
1000 case GetHubDescriptor:
1001 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1002 wValue, wIndex, buf, wLength);
1003 break;
1004 }
1005
1006 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1007 typeReq, ret, usbhsh_port_stat_get(hpriv));
1008
1009 return ret;
1010}
1011
1012static struct hc_driver usbhsh_driver = {
1013 .description = usbhsh_hcd_name,
1014 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1015
1016 /*
1017 * generic hardware linkage
1018 */
1019 .flags = HCD_USB2,
1020
1021 .start = usbhsh_host_start,
1022 .stop = usbhsh_host_stop,
1023
1024 /*
1025 * managing i/o requests and associated device resources
1026 */
1027 .urb_enqueue = usbhsh_urb_enqueue,
1028 .urb_dequeue = usbhsh_urb_dequeue,
1029 .endpoint_disable = usbhsh_endpoint_disable,
1030
1031 /*
1032 * root hub
1033 */
1034 .hub_status_data = usbhsh_hub_status_data,
1035 .hub_control = usbhsh_hub_control,
1036};
1037
1038/*
1039 * interrupt functions
1040 */
1041static int usbhsh_irq_attch(struct usbhs_priv *priv,
1042 struct usbhs_irq_state *irq_state)
1043{
1044 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1045 struct device *dev = usbhs_priv_to_dev(priv);
1046
1047 dev_dbg(dev, "device attached\n");
1048
1049 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1050 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1051
1052 return 0;
1053}
1054
1055static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1056 struct usbhs_irq_state *irq_state)
1057{
1058 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1059 struct device *dev = usbhs_priv_to_dev(priv);
1060
1061 dev_dbg(dev, "device detached\n");
1062
1063 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1064 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1065
1066 return 0;
1067}
1068
1069static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1070 struct usbhs_irq_state *irq_state)
1071{
1072 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1073 struct device *dev = usbhs_priv_to_dev(priv);
1074
1075 dev_dbg(dev, "setup packet OK\n");
1076
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001077 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001078
1079 return 0;
1080}
1081
1082static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1083 struct usbhs_irq_state *irq_state)
1084{
1085 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1086 struct device *dev = usbhs_priv_to_dev(priv);
1087
1088 dev_dbg(dev, "setup packet Err\n");
1089
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001090 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001091
1092 return 0;
1093}
1094
1095/*
1096 * module start/stop
1097 */
1098static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1099{
1100 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1101 struct usbhsh_pipe_info *pipe_info = hpriv->pipe_info;
1102 struct usbhs_pipe *pipe;
1103 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1104 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1105 int old_type, dir_in, i;
1106
1107 /* init all pipe */
1108 old_type = USB_ENDPOINT_XFER_CONTROL;
1109 for (i = 0; i < pipe_size; i++) {
1110 pipe_info[i].usr_cnt = 0;
1111
1112 /*
1113 * data "output" will be finished as soon as possible,
1114 * but there is no guaranty at data "input" case.
1115 *
1116 * "input" needs "standby" pipe.
1117 * So, "input" direction pipe > "output" direction pipe
1118 * is good idea.
1119 *
1120 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1121 * and the other will be input direction here.
1122 *
1123 * ex)
1124 * ...
1125 * USB_ENDPOINT_XFER_ISOC -> dir out
1126 * USB_ENDPOINT_XFER_ISOC -> dir in
1127 * USB_ENDPOINT_XFER_BULK -> dir out
1128 * USB_ENDPOINT_XFER_BULK -> dir in
1129 * USB_ENDPOINT_XFER_BULK -> dir in
1130 * ...
1131 */
1132 dir_in = (pipe_type[i] == old_type);
1133 old_type = pipe_type[i];
1134
1135 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1136 pipe = usbhs_dcp_malloc(priv);
1137 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1138 } else {
1139 pipe = usbhs_pipe_malloc(priv,
1140 pipe_type[i],
1141 dir_in);
1142 }
1143
1144 pipe->mod_private = pipe_info + i;
1145 }
1146}
1147
1148static int usbhsh_start(struct usbhs_priv *priv)
1149{
1150 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1151 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1152 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1153 struct device *dev = usbhs_priv_to_dev(priv);
1154 int ret;
1155
1156 /* add hcd */
1157 ret = usb_add_hcd(hcd, 0, 0);
1158 if (ret < 0)
1159 return 0;
1160
1161 /*
1162 * pipe initialize and enable DCP
1163 */
1164 usbhs_pipe_init(priv,
1165 usbhsh_dma_map_ctrl);
1166 usbhs_fifo_init(priv);
1167 usbhsh_pipe_init_for_host(priv);
1168
1169 /*
1170 * system config enble
1171 * - HI speed
1172 * - host
1173 * - usb module
1174 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001175 usbhs_sys_host_ctrl(priv, 1);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001176
1177 /*
1178 * enable irq callback
1179 */
1180 mod->irq_attch = usbhsh_irq_attch;
1181 mod->irq_dtch = usbhsh_irq_dtch;
1182 mod->irq_sack = usbhsh_irq_setup_ack;
1183 mod->irq_sign = usbhsh_irq_setup_err;
1184 usbhs_irq_callback_update(priv, mod);
1185
1186 dev_dbg(dev, "start host\n");
1187
1188 return ret;
1189}
1190
1191static int usbhsh_stop(struct usbhs_priv *priv)
1192{
1193 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1194 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001195 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001196 struct device *dev = usbhs_priv_to_dev(priv);
1197
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001198 /*
1199 * disable irq callback
1200 */
1201 mod->irq_attch = NULL;
1202 mod->irq_dtch = NULL;
1203 mod->irq_sack = NULL;
1204 mod->irq_sign = NULL;
1205 usbhs_irq_callback_update(priv, mod);
1206
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001207 usb_remove_hcd(hcd);
1208
1209 /* disable sys */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001210 usbhs_sys_host_ctrl(priv, 0);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001211
1212 dev_dbg(dev, "quit host\n");
1213
1214 return 0;
1215}
1216
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001217int usbhs_mod_host_probe(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001218{
1219 struct usbhsh_hpriv *hpriv;
1220 struct usb_hcd *hcd;
1221 struct usbhsh_pipe_info *pipe_info;
1222 struct usbhsh_device *udev;
1223 struct device *dev = usbhs_priv_to_dev(priv);
1224 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1225 int i;
1226
1227 /* initialize hcd */
1228 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1229 if (!hcd) {
1230 dev_err(dev, "Failed to create hcd\n");
1231 return -ENOMEM;
1232 }
1233
1234 pipe_info = kzalloc(sizeof(*pipe_info) * pipe_size, GFP_KERNEL);
1235 if (!pipe_info) {
1236 dev_err(dev, "Could not allocate pipe_info\n");
1237 goto usbhs_mod_host_probe_err;
1238 }
1239
1240 /*
1241 * CAUTION
1242 *
1243 * There is no guarantee that it is possible to access usb module here.
1244 * Don't accesses to it.
1245 * The accesse will be enable after "usbhsh_start"
1246 */
1247
1248 hpriv = usbhsh_hcd_to_hpriv(hcd);
1249
1250 /*
1251 * register itself
1252 */
1253 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1254
1255 /* init hpriv */
1256 hpriv->mod.name = "host";
1257 hpriv->mod.start = usbhsh_start;
1258 hpriv->mod.stop = usbhsh_stop;
1259 hpriv->pipe_info = pipe_info;
1260 hpriv->pipe_size = pipe_size;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001261 usbhsh_port_stat_init(hpriv);
1262
1263 /* init all device */
1264 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1265 udev->usbv = NULL;
1266 INIT_LIST_HEAD(&udev->ep_list_head);
1267 }
1268
1269 dev_info(dev, "host probed\n");
1270
1271 return 0;
1272
1273usbhs_mod_host_probe_err:
1274 usb_put_hcd(hcd);
1275
1276 return -ENOMEM;
1277}
1278
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001279int usbhs_mod_host_remove(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001280{
1281 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1282 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1283
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001284 usb_put_hcd(hcd);
1285
1286 return 0;
1287}