blob: 069cd765400cbf51f649564d5ed8490d90cd8259 [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
Kuninori Morimotoe5679d02011-12-08 18:28:24 -080048 * +--------+ | | other device requested
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070049 * +- [uep 1 (bulk)] --|---+ +--------------+
50 * | +--------------> | pipe0 (dcp) |
Kuninori Morimotoe5679d02011-12-08 18:28:24 -080051 * +- [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)] -@ | | .... |
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070062 * | |
63 * +- [uep 2 (bulk)]-----------+
Kuninori Morimotoe5679d02011-12-08 18:28:24 -080064 *
65 * @ : uep requested free pipe, but all have been used.
66 * now it is waiting for free pipe
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070067 */
68
69
70/*
71 * struct
72 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070073struct usbhsh_request {
74 struct urb *urb;
75 struct usbhs_pkt pkt;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070076};
77
78struct usbhsh_device {
79 struct usb_device *usbv;
80 struct list_head ep_list_head; /* list of usbhsh_ep */
81};
82
83struct usbhsh_ep {
Kuninori Morimotoe5679d02011-12-08 18:28:24 -080084 struct usbhs_pipe *pipe; /* attached pipe */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070085 struct usbhsh_device *udev; /* attached udev */
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -080086 struct usb_host_endpoint *ep;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070087 struct list_head ep_list; /* list to usbhsh_device */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070088};
89
90#define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
91#define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
92struct usbhsh_hpriv {
93 struct usbhs_mod mod;
94 struct usbhs_pipe *dcp;
95
96 struct usbhsh_device udev[USBHSH_DEVICE_MAX];
97
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070098 u32 port_stat; /* USB_PORT_STAT_xxx */
99
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700100 struct completion setup_ack_done;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700101};
102
103
104static const char usbhsh_hcd_name[] = "renesas_usbhs host";
105
106/*
107 * macro
108 */
109#define usbhsh_priv_to_hpriv(priv) \
110 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
111
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700112#define __usbhsh_for_each_udev(start, pos, h, i) \
113 for (i = start, pos = (h)->udev + i; \
114 i < USBHSH_DEVICE_MAX; \
115 i++, pos = (h)->udev + i)
116
117#define usbhsh_for_each_udev(pos, hpriv, i) \
118 __usbhsh_for_each_udev(1, pos, hpriv, i)
119
120#define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
121 __usbhsh_for_each_udev(0, pos, hpriv, i)
122
123#define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
124#define usbhsh_hcd_to_dev(h) ((h)->self.controller)
125
126#define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
127#define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
128#define usbhsh_hpriv_to_hcd(h) \
129 container_of((void *)h, struct usb_hcd, hcd_priv)
130
131#define usbhsh_ep_to_uep(u) ((u)->hcpriv)
132#define usbhsh_uep_to_pipe(u) ((u)->pipe)
133#define usbhsh_uep_to_udev(u) ((u)->udev)
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800134#define usbhsh_uep_to_ep(u) ((u)->ep)
135
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700136#define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
137#define usbhsh_urb_to_usbv(u) ((u)->dev)
138
139#define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
140
141#define usbhsh_udev_to_usbv(h) ((h)->usbv)
Kuninori Morimotoab142302011-10-31 00:48:00 -0700142#define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700143
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800144#define usbhsh_pipe_to_uep(p) ((p)->mod_private)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700145
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700146#define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
147#define usbhsh_device_hubport(d) ((d)->usbv->portnum)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700148#define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
149#define usbhsh_device_nth(h, d) ((h)->udev + d)
150#define usbhsh_device0(h) usbhsh_device_nth(h, 0)
151
152#define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
153#define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
154#define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
155#define usbhsh_port_stat_get(h) ((h)->port_stat)
156
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700157#define usbhsh_pkt_to_ureq(p) \
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700158 container_of((void *)p, struct usbhsh_request, pkt)
159
160/*
161 * req alloc/free
162 */
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700163static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700164 struct urb *urb,
165 gfp_t mem_flags)
166{
167 struct usbhsh_request *ureq;
168 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
169 struct device *dev = usbhs_priv_to_dev(priv);
170
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700171 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700172 if (!ureq) {
173 dev_err(dev, "ureq alloc fail\n");
174 return NULL;
175 }
176
177 usbhs_pkt_init(&ureq->pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700178 ureq->urb = urb;
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700179 usbhsh_urb_to_ureq(urb) = ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700180
181 return ureq;
182}
183
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700184static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700185 struct usbhsh_request *ureq)
186{
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700187 usbhsh_urb_to_ureq(ureq->urb) = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700188 ureq->urb = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700189
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700190 kfree(ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700191}
192
193/*
Kuninori Morimotob1930da2011-12-08 18:30:23 -0800194 * status
195 */
196static int usbhsh_is_running(struct usbhsh_hpriv *hpriv)
197{
198 /*
199 * we can decide some device is attached or not
200 * by checking mod.irq_attch
201 * see
202 * usbhsh_irq_attch()
203 * usbhsh_irq_dtch()
204 */
205 return (hpriv->mod.irq_attch == NULL);
206}
207
208/*
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800209 * pipe control
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800210 */
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800211static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv,
212 struct urb *urb,
213 struct usbhs_pkt *pkt)
214{
215 int len = urb->actual_length;
216 int maxp = usb_endpoint_maxp(&urb->ep->desc);
217 int t = 0;
218
219 /* DCP is out of sequence control */
220 if (usb_pipecontrol(urb->pipe))
221 return;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700222
223 /*
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800224 * renesas_usbhs pipe has a limitation in a number.
225 * So, driver should re-use the limited pipe for each device/endpoint.
226 * DATA0/1 sequence should be saved for it.
227 * see [image of mod_host]
228 * [HARDWARE LIMITATION]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700229 */
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800230
231 /*
232 * next sequence depends on actual_length
233 *
234 * ex) actual_length = 1147, maxp = 512
235 * data0 : 512
236 * data1 : 512
237 * data0 : 123
238 * data1 is the next sequence
239 */
240 t = len / maxp;
241 if (len % maxp)
242 t++;
243 if (pkt->zero)
244 t++;
245 t %= 2;
246
247 if (t)
248 usb_dotoggle(urb->dev,
249 usb_pipeendpoint(urb->pipe),
250 usb_pipeout(urb->pipe));
251}
252
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800253static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
254 struct urb *urb);
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800255
256static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv,
257 struct urb *urb)
258{
259 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
260 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
261 struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
262 struct usbhs_pipe *pipe;
263 struct usb_endpoint_descriptor *desc = &urb->ep->desc;
264 struct device *dev = usbhs_priv_to_dev(priv);
265 unsigned long flags;
266 int dir_in_req = !!usb_pipein(urb->pipe);
267 int is_dcp = usb_endpoint_xfer_control(desc);
268 int i, dir_in;
269 int ret = -EBUSY;
270
271 /******************** spin lock ********************/
272 usbhs_lock(priv, flags);
273
274 if (unlikely(usbhsh_uep_to_pipe(uep))) {
275 dev_err(dev, "uep already has pipe\n");
276 goto usbhsh_pipe_attach_done;
277 }
278
279 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
280
281 /* check pipe type */
282 if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
283 continue;
284
285 /* check pipe direction if normal pipe */
286 if (!is_dcp) {
287 dir_in = !!usbhs_pipe_is_dir_in(pipe);
288 if (0 != (dir_in - dir_in_req))
289 continue;
290 }
291
292 /* check pipe is free */
293 if (usbhsh_pipe_to_uep(pipe))
294 continue;
295
296 /*
297 * attach pipe to uep
298 *
299 * usbhs_pipe_config_update() should be called after
300 * usbhs_set_device_config()
301 * see
302 * DCPMAXP/PIPEMAXP
303 */
304 usbhsh_uep_to_pipe(uep) = pipe;
305 usbhsh_pipe_to_uep(pipe) = uep;
306
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800307 usbhs_pipe_config_update(pipe,
308 usbhsh_device_number(hpriv, udev),
309 usb_endpoint_num(desc),
310 usb_endpoint_maxp(desc));
311
312 dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__,
313 usbhsh_device_number(hpriv, udev),
314 usb_endpoint_num(desc),
315 usbhs_pipe_name(pipe),
316 dir_in_req ? "in" : "out");
317
318 ret = 0;
319 break;
320 }
321
322usbhsh_pipe_attach_done:
323 usbhs_unlock(priv, flags);
324 /******************** spin unlock ******************/
325
326 return ret;
327}
328
329static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv,
330 struct usbhsh_ep *uep)
331{
332 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
333 struct usbhs_pipe *pipe;
334 struct device *dev = usbhs_priv_to_dev(priv);
335 unsigned long flags;
336
Kuninori Morimoto4f053a22012-10-16 23:31:33 -0700337 if (unlikely(!uep)) {
338 dev_err(dev, "no uep\n");
339 return;
340 }
341
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800342 /******************** spin lock ********************/
343 usbhs_lock(priv, flags);
344
345 pipe = usbhsh_uep_to_pipe(uep);
346
347 if (unlikely(!pipe)) {
348 dev_err(dev, "uep doens't have pipe\n");
349 } else {
350 struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep);
351 struct usbhsh_device *udev = usbhsh_uep_to_udev(uep);
352
353 /* detach pipe from uep */
354 usbhsh_uep_to_pipe(uep) = NULL;
355 usbhsh_pipe_to_uep(pipe) = NULL;
356
357 dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__,
358 usbhsh_device_number(hpriv, udev),
359 usb_endpoint_num(&ep->desc),
360 usbhs_pipe_name(pipe));
361 }
362
363 usbhs_unlock(priv, flags);
364 /******************** spin unlock ******************/
365}
366
367/*
368 * endpoint control
369 */
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800370static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
371 struct urb *urb,
372 gfp_t mem_flags)
373{
374 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
375 struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
376 struct usb_host_endpoint *ep = urb->ep;
377 struct usbhsh_ep *uep;
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800378 struct device *dev = usbhs_priv_to_dev(priv);
379 struct usb_endpoint_descriptor *desc = &ep->desc;
380 unsigned long flags;
381
382 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
383 if (!uep) {
384 dev_err(dev, "usbhsh_ep alloc fail\n");
385 return -ENOMEM;
386 }
387
388 /******************** spin lock ********************/
389 usbhs_lock(priv, flags);
390
391 /*
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800392 * init endpoint
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800393 */
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800394 INIT_LIST_HEAD(&uep->ep_list);
395 list_add_tail(&uep->ep_list, &udev->ep_list_head);
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800396
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800397 usbhsh_uep_to_udev(uep) = udev;
398 usbhsh_uep_to_ep(uep) = ep;
399 usbhsh_ep_to_uep(ep) = uep;
400
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800401 usbhs_unlock(priv, flags);
402 /******************** spin unlock ******************/
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800403
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800404 dev_dbg(dev, "%s [%d-%d]\n", __func__,
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800405 usbhsh_device_number(hpriv, udev),
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800406 usb_endpoint_num(desc));
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800407
408 return 0;
409}
410
411static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
412 struct usb_host_endpoint *ep)
413{
414 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
415 struct device *dev = usbhs_priv_to_dev(priv);
416 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800417 unsigned long flags;
418
419 if (!uep)
420 return;
421
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800422 dev_dbg(dev, "%s [%d-%d]\n", __func__,
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800423 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800424 usb_endpoint_num(&ep->desc));
425
426 if (usbhsh_uep_to_pipe(uep))
427 usbhsh_pipe_detach(hpriv, uep);
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800428
429 /******************** spin lock ********************/
430 usbhs_lock(priv, flags);
431
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800432 /* remove this endpoint from udev */
433 list_del_init(&uep->ep_list);
434
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800435 usbhsh_uep_to_udev(uep) = NULL;
436 usbhsh_uep_to_ep(uep) = NULL;
437 usbhsh_ep_to_uep(ep) = NULL;
438
439 usbhs_unlock(priv, flags);
440 /******************** spin unlock ******************/
441
442 kfree(uep);
443}
444
445static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
446 struct usbhsh_device *udev)
447{
448 struct usbhsh_ep *uep, *next;
449
450 list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
451 usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700452}
453
454/*
455 * device control
456 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700457static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
458 struct usbhsh_device *udev)
459{
460 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
461
462 return hcd->self.root_hub == usbv->parent;
463}
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700464
465static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
466{
467 return !list_empty(&udev->ep_list_head);
468}
469
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800470static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
471 struct urb *urb)
472{
473 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
474 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
475
476 /* usbhsh_device_attach() is still not called */
477 if (!udev)
478 return NULL;
479
480 /* if it is device0, return it */
481 if (0 == usb_pipedevice(urb->pipe))
482 return usbhsh_device0(hpriv);
483
484 /* return attached device */
485 return udev;
486}
487
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700488static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700489 struct urb *urb)
490{
491 struct usbhsh_device *udev = NULL;
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800492 struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
493 struct usbhsh_device *pos;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700494 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
495 struct device *dev = usbhsh_hcd_to_dev(hcd);
496 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
497 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700498 unsigned long flags;
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700499 u16 upphub, hubport;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700500 int i;
501
502 /*
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800503 * This function should be called only while urb is pointing to device0.
504 * It will attach unused usbhsh_device to urb (usbv),
505 * and initialize device0.
506 * You can use usbhsh_device_get() to get "current" udev,
507 * and usbhsh_usbv_to_udev() is for "attached" udev.
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700508 */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800509 if (0 != usb_pipedevice(urb->pipe)) {
510 dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
511 return NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700512 }
513
Kuninori Morimotod399f902011-10-31 00:48:35 -0700514 /******************** spin lock ********************/
515 usbhs_lock(priv, flags);
516
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700517 /*
518 * find unused device
519 */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800520 usbhsh_for_each_udev(pos, hpriv, i) {
521 if (usbhsh_udev_is_used(pos))
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700522 continue;
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800523 udev = pos;
524 break;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700525 }
526
Kuninori Morimotod399f902011-10-31 00:48:35 -0700527 if (udev) {
528 /*
529 * usbhsh_usbv_to_udev()
530 * usbhsh_udev_to_usbv()
531 * will be enable
532 */
533 dev_set_drvdata(&usbv->dev, udev);
534 udev->usbv = usbv;
535 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700536
Kuninori Morimotod399f902011-10-31 00:48:35 -0700537 usbhs_unlock(priv, flags);
538 /******************** spin unlock ******************/
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700539
Kuninori Morimotoab142302011-10-31 00:48:00 -0700540 if (!udev) {
541 dev_err(dev, "no free usbhsh_device\n");
542 return NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700543 }
544
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800545 if (usbhsh_device_has_endpoint(udev)) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700546 dev_warn(dev, "udev have old endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800547 usbhsh_endpoint_detach_all(hpriv, udev);
548 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700549
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800550 if (usbhsh_device_has_endpoint(udev0)) {
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800551 dev_warn(dev, "udev0 have old endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800552 usbhsh_endpoint_detach_all(hpriv, udev0);
553 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700554
555 /* uep will be attached */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800556 INIT_LIST_HEAD(&udev0->ep_list_head);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700557 INIT_LIST_HEAD(&udev->ep_list_head);
558
559 /*
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800560 * set device0 config
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700561 */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800562 usbhs_set_device_config(priv,
563 0, 0, 0, usbv->speed);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700564
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800565 /*
566 * set new device config
567 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700568 upphub = 0;
569 hubport = 0;
570 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
571 /* if udev is not connected to rhdev, it means parent is Hub */
572 struct usbhsh_device *parent = usbhsh_device_parent(udev);
573
574 upphub = usbhsh_device_number(hpriv, parent);
575 hubport = usbhsh_device_hubport(udev);
576
577 dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
578 upphub, hubport, parent);
579 }
580
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700581 usbhs_set_device_config(priv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700582 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700583 upphub, hubport, usbv->speed);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700584
585 dev_dbg(dev, "%s [%d](%p)\n", __func__,
586 usbhsh_device_number(hpriv, udev), udev);
587
588 return udev;
589}
590
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700591static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700592 struct usbhsh_device *udev)
593{
594 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700595 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700596 struct device *dev = usbhsh_hcd_to_dev(hcd);
597 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700598 unsigned long flags;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700599
600 dev_dbg(dev, "%s [%d](%p)\n", __func__,
601 usbhsh_device_number(hpriv, udev), udev);
602
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800603 if (usbhsh_device_has_endpoint(udev)) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700604 dev_warn(dev, "udev still have endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800605 usbhsh_endpoint_detach_all(hpriv, udev);
606 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700607
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800608 /*
609 * There is nothing to do if it is device0.
610 * see
611 * usbhsh_device_attach()
612 * usbhsh_device_get()
613 */
614 if (0 == usbhsh_device_number(hpriv, udev))
615 return;
616
Kuninori Morimotod399f902011-10-31 00:48:35 -0700617 /******************** spin lock ********************/
618 usbhs_lock(priv, flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700619
620 /*
621 * usbhsh_usbv_to_udev()
622 * usbhsh_udev_to_usbv()
623 * will be disable
624 */
625 dev_set_drvdata(&usbv->dev, NULL);
626 udev->usbv = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700627
Kuninori Morimotod399f902011-10-31 00:48:35 -0700628 usbhs_unlock(priv, flags);
629 /******************** spin unlock ******************/
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700630}
631
632/*
633 * queue push/pop
634 */
635static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
636{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700637 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700638 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
639 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
640 struct urb *urb = ureq->urb;
641 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto6d0376f2011-12-08 18:31:11 -0800642 int status = 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700643
644 dev_dbg(dev, "%s\n", __func__);
645
646 if (!urb) {
647 dev_warn(dev, "pkt doesn't have urb\n");
648 return;
649 }
650
Kuninori Morimoto6d0376f2011-12-08 18:31:11 -0800651 if (!usbhsh_is_running(hpriv))
652 status = -ESHUTDOWN;
653
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700654 urb->actual_length = pkt->actual;
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700655 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700656
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800657 usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
Kuninori Morimotod9b78f32011-12-15 01:53:18 -0800658 usbhsh_pipe_detach(hpriv, usbhsh_ep_to_uep(urb->ep));
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700659
660 usb_hcd_unlink_urb_from_ep(hcd, urb);
Kuninori Morimoto6d0376f2011-12-08 18:31:11 -0800661 usb_hcd_giveback_urb(hcd, urb, status);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700662}
663
664static int usbhsh_queue_push(struct usb_hcd *hcd,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700665 struct urb *urb,
666 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700667{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700668 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700669 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
670 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700671 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700672 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700673 void *buf;
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800674 int len, sequence;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700675
676 if (usb_pipeisoc(urb->pipe)) {
677 dev_err(dev, "pipe iso is not supported now\n");
678 return -EIO;
679 }
680
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700681 /* this ureq will be freed on usbhsh_queue_done() */
682 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
683 if (unlikely(!ureq)) {
684 dev_err(dev, "ureq alloc fail\n");
685 return -ENOMEM;
686 }
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700687
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700688 if (usb_pipein(urb->pipe))
689 pipe->handler = &usbhs_fifo_pio_pop_handler;
690 else
691 pipe->handler = &usbhs_fifo_pio_push_handler;
692
693 buf = (void *)(urb->transfer_buffer + urb->actual_length);
694 len = urb->transfer_buffer_length - urb->actual_length;
695
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800696 sequence = usb_gettoggle(urb->dev,
697 usb_pipeendpoint(urb->pipe),
698 usb_pipeout(urb->pipe));
699
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700700 dev_dbg(dev, "%s\n", __func__);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700701 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800702 buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
703 sequence);
704
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700705 usbhs_pkt_start(pipe);
706
707 return 0;
708}
709
Kuninori Morimoto2d833fa2011-12-08 18:31:37 -0800710static void usbhsh_queue_force_pop(struct usbhs_priv *priv,
711 struct usbhs_pipe *pipe)
712{
713 struct usbhs_pkt *pkt;
714
715 while (1) {
716 pkt = usbhs_pkt_pop(pipe, NULL);
717 if (!pkt)
718 break;
719
720 /*
721 * if all packet are gone, usbhsh_endpoint_disable()
722 * will be called.
723 * then, attached device/endpoint/pipe will be detached
724 */
725 usbhsh_queue_done(priv, pkt);
726 }
727}
728
729static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv)
730{
731 struct usbhs_pipe *pos;
732 int i;
733
734 usbhs_for_each_pipe_with_dcp(pos, priv, i)
735 usbhsh_queue_force_pop(priv, pos);
736}
737
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700738/*
739 * DCP setup stage
740 */
741static int usbhsh_is_request_address(struct urb *urb)
742{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700743 struct usb_ctrlrequest *req;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700744
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700745 req = (struct usb_ctrlrequest *)urb->setup_packet;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700746
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700747 if ((DeviceOutRequest == req->bRequestType << 8) &&
748 (USB_REQ_SET_ADDRESS == req->bRequest))
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700749 return 1;
750 else
751 return 0;
752}
753
754static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
755 struct urb *urb,
756 struct usbhs_pipe *pipe)
757{
758 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
759 struct usb_ctrlrequest req;
760 struct device *dev = usbhs_priv_to_dev(priv);
761
762 /*
763 * wait setup packet ACK
764 * see
765 * usbhsh_irq_setup_ack()
766 * usbhsh_irq_setup_err()
767 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700768 init_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700769
770 /* copy original request */
771 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
772
773 /*
774 * renesas_usbhs can not use original usb address.
775 * see HARDWARE LIMITATION.
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800776 * modify usb address here to use attached device.
777 * see usbhsh_device_attach()
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700778 */
779 if (usbhsh_is_request_address(urb)) {
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800780 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
781 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
782
783 /* udev is a attached device */
784 req.wValue = usbhsh_device_number(hpriv, udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700785 dev_dbg(dev, "create new address - %d\n", req.wValue);
786 }
787
788 /* set request */
789 usbhs_usbreq_set_val(priv, &req);
790
791 /*
792 * wait setup packet ACK
793 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700794 wait_for_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700795
796 dev_dbg(dev, "%s done\n", __func__);
797}
798
799/*
800 * DCP data stage
801 */
802static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
803 struct usbhs_pkt *pkt)
804{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700805 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700806 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700807
808 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
809
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700810 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700811}
812
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700813static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
814 struct urb *urb,
815 struct usbhs_pipe *pipe,
816 gfp_t mem_flags)
817
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700818{
819 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700820
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700821 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
822 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
823 if (unlikely(!ureq))
824 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700825
826 if (usb_pipein(urb->pipe))
827 pipe->handler = &usbhs_dcp_data_stage_in_handler;
828 else
829 pipe->handler = &usbhs_dcp_data_stage_out_handler;
830
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700831 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700832 usbhsh_data_stage_packet_done,
833 urb->transfer_buffer,
834 urb->transfer_buffer_length,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800835 (urb->transfer_flags & URB_ZERO_PACKET),
836 -1);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700837
838 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700839}
840
841/*
842 * DCP status stage
843 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700844static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700845 struct urb *urb,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700846 struct usbhs_pipe *pipe,
847 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700848{
849 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700850
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700851 /* This ureq will be freed on usbhsh_queue_done() */
852 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
853 if (unlikely(!ureq))
854 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700855
856 if (usb_pipein(urb->pipe))
857 pipe->handler = &usbhs_dcp_status_stage_in_handler;
858 else
859 pipe->handler = &usbhs_dcp_status_stage_out_handler;
860
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700861 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700862 usbhsh_queue_done,
863 NULL,
864 urb->transfer_buffer_length,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800865 0, -1);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700866
867 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700868}
869
870static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700871 struct urb *urb,
872 gfp_t mflags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700873{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700874 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700875 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
876 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700877 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700878 int ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700879
880 dev_dbg(dev, "%s\n", __func__);
881
882 /*
883 * setup stage
884 *
885 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
886 */
887 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
888
889 /*
890 * data stage
891 *
892 * It is pushed only when urb has buffer.
893 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700894 if (urb->transfer_buffer_length) {
895 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
896 if (ret < 0) {
897 dev_err(dev, "data stage failed\n");
898 return ret;
899 }
900 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700901
902 /*
903 * status stage
904 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700905 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
906 if (ret < 0) {
907 dev_err(dev, "status stage failed\n");
908 return ret;
909 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700910
911 /*
912 * start pushed packets
913 */
914 usbhs_pkt_start(pipe);
915
916 return 0;
917}
918
919/*
920 * dma map functions
921 */
922static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
923{
924 return 0;
925}
926
927/*
928 * for hc_driver
929 */
930static int usbhsh_host_start(struct usb_hcd *hcd)
931{
932 return 0;
933}
934
935static void usbhsh_host_stop(struct usb_hcd *hcd)
936{
937}
938
939static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
940 struct urb *urb,
941 gfp_t mem_flags)
942{
943 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
944 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
945 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700946 struct usb_host_endpoint *ep = urb->ep;
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700947 struct usbhsh_device *new_udev = NULL;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700948 int is_dir_in = usb_pipein(urb->pipe);
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800949 int i;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700950 int ret;
951
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700952 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700953
Kuninori Morimotob1930da2011-12-08 18:30:23 -0800954 if (!usbhsh_is_running(hpriv)) {
955 ret = -EIO;
Kuninori Morimoto15a38382011-12-08 18:31:53 -0800956 dev_err(dev, "host is not running\n");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700957 goto usbhsh_urb_enqueue_error_not_linked;
Kuninori Morimotob1930da2011-12-08 18:30:23 -0800958 }
959
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700960 ret = usb_hcd_link_urb_to_ep(hcd, urb);
Kuninori Morimoto15a38382011-12-08 18:31:53 -0800961 if (ret) {
962 dev_err(dev, "urb link failed\n");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700963 goto usbhsh_urb_enqueue_error_not_linked;
Kuninori Morimoto15a38382011-12-08 18:31:53 -0800964 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700965
966 /*
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700967 * attach udev if needed
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800968 * see [image of mod_host]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700969 */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800970 if (!usbhsh_device_get(hpriv, urb)) {
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700971 new_udev = usbhsh_device_attach(hpriv, urb);
Kuninori Morimoto37332ee2011-12-08 18:25:37 -0800972 if (!new_udev) {
973 ret = -EIO;
Kuninori Morimoto15a38382011-12-08 18:31:53 -0800974 dev_err(dev, "device attach failed\n");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700975 goto usbhsh_urb_enqueue_error_not_linked;
Kuninori Morimoto37332ee2011-12-08 18:25:37 -0800976 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700977 }
978
979 /*
Kuninori Morimoto48250932011-10-31 00:48:59 -0700980 * attach endpoint if needed
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800981 * see [image of mod_host]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700982 */
Kuninori Morimoto48250932011-10-31 00:48:59 -0700983 if (!usbhsh_ep_to_uep(ep)) {
984 ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
Kuninori Morimoto15a38382011-12-08 18:31:53 -0800985 if (ret < 0) {
986 dev_err(dev, "endpoint attach failed\n");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700987 goto usbhsh_urb_enqueue_error_free_device;
Kuninori Morimoto15a38382011-12-08 18:31:53 -0800988 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700989 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700990
991 /*
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800992 * attach pipe to endpoint
993 * see [image of mod_host]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700994 */
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800995 for (i = 0; i < 1024; i++) {
996 ret = usbhsh_pipe_attach(hpriv, urb);
997 if (ret < 0)
998 msleep(100);
999 else
1000 break;
1001 }
Kuninori Morimoto15a38382011-12-08 18:31:53 -08001002 if (ret < 0) {
1003 dev_err(dev, "pipe attach failed\n");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001004 goto usbhsh_urb_enqueue_error_free_endpoint;
1005 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001006
1007 /*
1008 * push packet
1009 */
1010 if (usb_pipecontrol(urb->pipe))
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -07001011 ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001012 else
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -07001013 ret = usbhsh_queue_push(hcd, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001014
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -07001015 return ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001016
1017usbhsh_urb_enqueue_error_free_endpoint:
Kuninori Morimotoe5679d02011-12-08 18:28:24 -08001018 usbhsh_endpoint_detach(hpriv, ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001019usbhsh_urb_enqueue_error_free_device:
1020 if (new_udev)
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -07001021 usbhsh_device_detach(hpriv, new_udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001022usbhsh_urb_enqueue_error_not_linked:
1023
1024 dev_dbg(dev, "%s error\n", __func__);
1025
1026 return ret;
1027}
1028
1029static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1030{
1031 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1032 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
1033
1034 if (ureq) {
Kuninori Morimoto54796542011-12-08 18:26:07 -08001035 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1036 struct usbhs_pkt *pkt = &ureq->pkt;
1037
1038 usbhs_pkt_pop(pkt->pipe, pkt);
1039 usbhsh_queue_done(priv, pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001040 }
1041
1042 return 0;
1043}
1044
1045static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
1046 struct usb_host_endpoint *ep)
1047{
1048 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
1049 struct usbhsh_device *udev;
1050 struct usbhsh_hpriv *hpriv;
1051
1052 /*
1053 * this function might be called manytimes by same hcd/ep
Kuninori Morimotofca8ab72011-10-31 00:47:24 -07001054 * in-endpoint == out-endpoint if ep == dcp.
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001055 */
1056 if (!uep)
1057 return;
1058
1059 udev = usbhsh_uep_to_udev(uep);
1060 hpriv = usbhsh_hcd_to_hpriv(hcd);
1061
Kuninori Morimoto48250932011-10-31 00:48:59 -07001062 usbhsh_endpoint_detach(hpriv, ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001063
1064 /*
1065 * if there is no endpoint,
1066 * free device
1067 */
1068 if (!usbhsh_device_has_endpoint(udev))
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -07001069 usbhsh_device_detach(hpriv, udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001070}
1071
1072static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
1073{
1074 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1075 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1076 struct device *dev = usbhs_priv_to_dev(priv);
1077 int roothub_id = 1; /* only 1 root hub */
1078
1079 /*
1080 * does port stat was changed ?
1081 * check USB_PORT_STAT_C_xxx << 16
1082 */
1083 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
1084 *buf = (1 << roothub_id);
1085 else
1086 *buf = 0;
1087
1088 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
1089
1090 return !!(*buf);
1091}
1092
1093static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
1094 u16 typeReq, u16 wValue,
1095 u16 wIndex, char *buf, u16 wLength)
1096{
1097 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1098 struct device *dev = usbhs_priv_to_dev(priv);
1099
1100 switch (wValue) {
1101 case C_HUB_OVER_CURRENT:
1102 case C_HUB_LOCAL_POWER:
1103 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
1104 return 0;
1105 }
1106
1107 return -EPIPE;
1108}
1109
1110static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
1111 u16 typeReq, u16 wValue,
1112 u16 wIndex, char *buf, u16 wLength)
1113{
1114 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1115 struct device *dev = usbhs_priv_to_dev(priv);
1116 int enable = (typeReq == SetPortFeature);
1117 int speed, i, timeout = 128;
1118 int roothub_id = 1; /* only 1 root hub */
1119
1120 /* common error */
1121 if (wIndex > roothub_id || wLength != 0)
1122 return -EPIPE;
1123
1124 /* check wValue */
1125 switch (wValue) {
1126 case USB_PORT_FEAT_POWER:
1127 usbhs_vbus_ctrl(priv, enable);
1128 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
1129 break;
1130
1131 case USB_PORT_FEAT_ENABLE:
1132 case USB_PORT_FEAT_SUSPEND:
1133 case USB_PORT_FEAT_C_ENABLE:
1134 case USB_PORT_FEAT_C_SUSPEND:
1135 case USB_PORT_FEAT_C_CONNECTION:
1136 case USB_PORT_FEAT_C_OVER_CURRENT:
1137 case USB_PORT_FEAT_C_RESET:
1138 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
1139 break;
1140
1141 case USB_PORT_FEAT_RESET:
1142 if (!enable)
1143 break;
1144
1145 usbhsh_port_stat_clear(hpriv,
1146 USB_PORT_STAT_HIGH_SPEED |
1147 USB_PORT_STAT_LOW_SPEED);
1148
Kuninori Morimoto2d833fa2011-12-08 18:31:37 -08001149 usbhsh_queue_force_pop_all(priv);
1150
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001151 usbhs_bus_send_reset(priv);
1152 msleep(20);
1153 usbhs_bus_send_sof_enable(priv);
1154
1155 for (i = 0; i < timeout ; i++) {
1156 switch (usbhs_bus_get_speed(priv)) {
1157 case USB_SPEED_LOW:
1158 speed = USB_PORT_STAT_LOW_SPEED;
1159 goto got_usb_bus_speed;
1160 case USB_SPEED_HIGH:
1161 speed = USB_PORT_STAT_HIGH_SPEED;
1162 goto got_usb_bus_speed;
1163 case USB_SPEED_FULL:
1164 speed = 0;
1165 goto got_usb_bus_speed;
1166 }
1167
1168 msleep(20);
1169 }
1170 return -EPIPE;
1171
1172got_usb_bus_speed:
1173 usbhsh_port_stat_set(hpriv, speed);
1174 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
1175
1176 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1177 __func__, speed);
1178
1179 /* status change is not needed */
1180 return 0;
1181
1182 default:
1183 return -EPIPE;
1184 }
1185
1186 /* set/clear status */
1187 if (enable)
1188 usbhsh_port_stat_set(hpriv, (1 << wValue));
1189 else
1190 usbhsh_port_stat_clear(hpriv, (1 << wValue));
1191
1192 return 0;
1193}
1194
1195static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1196 u16 typeReq, u16 wValue,
1197 u16 wIndex, char *buf, u16 wLength)
1198{
1199 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1200 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
1201 struct device *dev = usbhs_priv_to_dev(priv);
1202 int roothub_id = 1; /* only 1 root hub */
1203
1204 switch (typeReq) {
1205 case GetHubStatus:
1206 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
1207
1208 *buf = 0x00;
1209 break;
1210
1211 case GetPortStatus:
1212 if (wIndex != roothub_id)
1213 return -EPIPE;
1214
1215 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
1216 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
1217 break;
1218
1219 case GetHubDescriptor:
1220 desc->bDescriptorType = 0x29;
1221 desc->bHubContrCurrent = 0;
1222 desc->bNbrPorts = roothub_id;
1223 desc->bDescLength = 9;
1224 desc->bPwrOn2PwrGood = 0;
1225 desc->wHubCharacteristics = cpu_to_le16(0x0011);
1226 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
1227 desc->u.hs.DeviceRemovable[1] = ~0;
1228 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
1229 break;
1230 }
1231
1232 return 0;
1233}
1234
1235static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1236 u16 wIndex, char *buf, u16 wLength)
1237{
1238 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1239 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1240 struct device *dev = usbhs_priv_to_dev(priv);
1241 int ret = -EPIPE;
1242
1243 switch (typeReq) {
1244
1245 /* Hub Feature */
1246 case ClearHubFeature:
1247 case SetHubFeature:
1248 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1249 wValue, wIndex, buf, wLength);
1250 break;
1251
1252 /* Port Feature */
1253 case SetPortFeature:
1254 case ClearPortFeature:
1255 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1256 wValue, wIndex, buf, wLength);
1257 break;
1258
1259 /* Get status */
1260 case GetHubStatus:
1261 case GetPortStatus:
1262 case GetHubDescriptor:
1263 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1264 wValue, wIndex, buf, wLength);
1265 break;
1266 }
1267
1268 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1269 typeReq, ret, usbhsh_port_stat_get(hpriv));
1270
1271 return ret;
1272}
1273
Kuninori Morimotoe7ae64c2012-08-05 22:44:07 -07001274static int usbhsh_bus_nop(struct usb_hcd *hcd)
1275{
1276 /* nothing to do */
1277 return 0;
1278}
1279
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001280static struct hc_driver usbhsh_driver = {
1281 .description = usbhsh_hcd_name,
1282 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1283
1284 /*
1285 * generic hardware linkage
1286 */
1287 .flags = HCD_USB2,
1288
1289 .start = usbhsh_host_start,
1290 .stop = usbhsh_host_stop,
1291
1292 /*
1293 * managing i/o requests and associated device resources
1294 */
1295 .urb_enqueue = usbhsh_urb_enqueue,
1296 .urb_dequeue = usbhsh_urb_dequeue,
1297 .endpoint_disable = usbhsh_endpoint_disable,
1298
1299 /*
1300 * root hub
1301 */
1302 .hub_status_data = usbhsh_hub_status_data,
1303 .hub_control = usbhsh_hub_control,
Kuninori Morimotoe7ae64c2012-08-05 22:44:07 -07001304 .bus_suspend = usbhsh_bus_nop,
1305 .bus_resume = usbhsh_bus_nop,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001306};
1307
1308/*
1309 * interrupt functions
1310 */
1311static int usbhsh_irq_attch(struct usbhs_priv *priv,
1312 struct usbhs_irq_state *irq_state)
1313{
1314 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1315 struct device *dev = usbhs_priv_to_dev(priv);
1316
1317 dev_dbg(dev, "device attached\n");
1318
1319 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1320 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1321
Kuninori Morimoto31e00fd2011-12-08 18:29:22 -08001322 /*
1323 * attch interrupt might happen infinitely on some device
1324 * (on self power USB hub ?)
1325 * disable it here.
Kuninori Morimotob1930da2011-12-08 18:30:23 -08001326 *
1327 * usbhsh_is_running() becomes effective
1328 * according to this process.
1329 * see
1330 * usbhsh_is_running()
1331 * usbhsh_urb_enqueue()
Kuninori Morimoto31e00fd2011-12-08 18:29:22 -08001332 */
1333 hpriv->mod.irq_attch = NULL;
1334 usbhs_irq_callback_update(priv, &hpriv->mod);
1335
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001336 return 0;
1337}
1338
1339static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1340 struct usbhs_irq_state *irq_state)
1341{
1342 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1343 struct device *dev = usbhs_priv_to_dev(priv);
1344
1345 dev_dbg(dev, "device detached\n");
1346
1347 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1348 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1349
Kuninori Morimoto31e00fd2011-12-08 18:29:22 -08001350 /*
1351 * enable attch interrupt again
Kuninori Morimotob1930da2011-12-08 18:30:23 -08001352 *
1353 * usbhsh_is_running() becomes invalid
1354 * according to this process.
1355 * see
1356 * usbhsh_is_running()
1357 * usbhsh_urb_enqueue()
Kuninori Morimoto31e00fd2011-12-08 18:29:22 -08001358 */
1359 hpriv->mod.irq_attch = usbhsh_irq_attch;
1360 usbhs_irq_callback_update(priv, &hpriv->mod);
1361
Kuninori Morimoto2d833fa2011-12-08 18:31:37 -08001362 /*
1363 * usbhsh_queue_force_pop_all() should be called
1364 * after usbhsh_is_running() becomes invalid.
1365 */
1366 usbhsh_queue_force_pop_all(priv);
1367
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001368 return 0;
1369}
1370
1371static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1372 struct usbhs_irq_state *irq_state)
1373{
1374 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1375 struct device *dev = usbhs_priv_to_dev(priv);
1376
1377 dev_dbg(dev, "setup packet OK\n");
1378
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001379 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001380
1381 return 0;
1382}
1383
1384static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1385 struct usbhs_irq_state *irq_state)
1386{
1387 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1388 struct device *dev = usbhs_priv_to_dev(priv);
1389
1390 dev_dbg(dev, "setup packet Err\n");
1391
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001392 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001393
1394 return 0;
1395}
1396
1397/*
1398 * module start/stop
1399 */
1400static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1401{
1402 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001403 struct usbhs_pipe *pipe;
1404 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1405 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1406 int old_type, dir_in, i;
1407
1408 /* init all pipe */
1409 old_type = USB_ENDPOINT_XFER_CONTROL;
1410 for (i = 0; i < pipe_size; i++) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001411
1412 /*
1413 * data "output" will be finished as soon as possible,
1414 * but there is no guaranty at data "input" case.
1415 *
1416 * "input" needs "standby" pipe.
1417 * So, "input" direction pipe > "output" direction pipe
1418 * is good idea.
1419 *
1420 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1421 * and the other will be input direction here.
1422 *
1423 * ex)
1424 * ...
1425 * USB_ENDPOINT_XFER_ISOC -> dir out
1426 * USB_ENDPOINT_XFER_ISOC -> dir in
1427 * USB_ENDPOINT_XFER_BULK -> dir out
1428 * USB_ENDPOINT_XFER_BULK -> dir in
1429 * USB_ENDPOINT_XFER_BULK -> dir in
1430 * ...
1431 */
1432 dir_in = (pipe_type[i] == old_type);
1433 old_type = pipe_type[i];
1434
1435 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1436 pipe = usbhs_dcp_malloc(priv);
1437 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1438 } else {
1439 pipe = usbhs_pipe_malloc(priv,
1440 pipe_type[i],
1441 dir_in);
1442 }
1443
Kuninori Morimotoe5679d02011-12-08 18:28:24 -08001444 pipe->mod_private = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001445 }
1446}
1447
1448static int usbhsh_start(struct usbhs_priv *priv)
1449{
1450 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1451 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1452 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1453 struct device *dev = usbhs_priv_to_dev(priv);
1454 int ret;
1455
1456 /* add hcd */
1457 ret = usb_add_hcd(hcd, 0, 0);
1458 if (ret < 0)
1459 return 0;
1460
1461 /*
1462 * pipe initialize and enable DCP
1463 */
1464 usbhs_pipe_init(priv,
1465 usbhsh_dma_map_ctrl);
1466 usbhs_fifo_init(priv);
1467 usbhsh_pipe_init_for_host(priv);
1468
1469 /*
1470 * system config enble
1471 * - HI speed
1472 * - host
1473 * - usb module
1474 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001475 usbhs_sys_host_ctrl(priv, 1);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001476
1477 /*
1478 * enable irq callback
1479 */
1480 mod->irq_attch = usbhsh_irq_attch;
1481 mod->irq_dtch = usbhsh_irq_dtch;
1482 mod->irq_sack = usbhsh_irq_setup_ack;
1483 mod->irq_sign = usbhsh_irq_setup_err;
1484 usbhs_irq_callback_update(priv, mod);
1485
1486 dev_dbg(dev, "start host\n");
1487
1488 return ret;
1489}
1490
1491static int usbhsh_stop(struct usbhs_priv *priv)
1492{
1493 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1494 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001495 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001496 struct device *dev = usbhs_priv_to_dev(priv);
1497
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001498 /*
1499 * disable irq callback
1500 */
1501 mod->irq_attch = NULL;
1502 mod->irq_dtch = NULL;
1503 mod->irq_sack = NULL;
1504 mod->irq_sign = NULL;
1505 usbhs_irq_callback_update(priv, mod);
1506
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001507 usb_remove_hcd(hcd);
1508
1509 /* disable sys */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001510 usbhs_sys_host_ctrl(priv, 0);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001511
1512 dev_dbg(dev, "quit host\n");
1513
1514 return 0;
1515}
1516
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001517int usbhs_mod_host_probe(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001518{
1519 struct usbhsh_hpriv *hpriv;
1520 struct usb_hcd *hcd;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001521 struct usbhsh_device *udev;
1522 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001523 int i;
1524
1525 /* initialize hcd */
1526 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1527 if (!hcd) {
1528 dev_err(dev, "Failed to create hcd\n");
1529 return -ENOMEM;
1530 }
Kuninori Morimoto1115b9e2011-12-08 18:24:47 -08001531 hcd->has_tt = 1; /* for low/full speed */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001532
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001533 /*
1534 * CAUTION
1535 *
1536 * There is no guarantee that it is possible to access usb module here.
1537 * Don't accesses to it.
1538 * The accesse will be enable after "usbhsh_start"
1539 */
1540
1541 hpriv = usbhsh_hcd_to_hpriv(hcd);
1542
1543 /*
1544 * register itself
1545 */
1546 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1547
1548 /* init hpriv */
1549 hpriv->mod.name = "host";
1550 hpriv->mod.start = usbhsh_start;
1551 hpriv->mod.stop = usbhsh_stop;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001552 usbhsh_port_stat_init(hpriv);
1553
1554 /* init all device */
1555 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1556 udev->usbv = NULL;
1557 INIT_LIST_HEAD(&udev->ep_list_head);
1558 }
1559
1560 dev_info(dev, "host probed\n");
1561
1562 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001563}
1564
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001565int usbhs_mod_host_remove(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001566{
1567 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1568 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1569
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001570 usb_put_hcd(hcd);
1571
1572 return 0;
1573}