blob: 92dcc7e64630e713eeb812ec0a7d910335254d42 [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 Morimotoc5b963f2011-10-31 00:47:44 -0700189
190 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;
222
223 /*
224 * 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]
229 */
230
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
337 /******************** spin lock ********************/
338 usbhs_lock(priv, flags);
339
340 pipe = usbhsh_uep_to_pipe(uep);
341
342 if (unlikely(!pipe)) {
343 dev_err(dev, "uep doens't have pipe\n");
344 } else {
345 struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep);
346 struct usbhsh_device *udev = usbhsh_uep_to_udev(uep);
347
348 /* detach pipe from uep */
349 usbhsh_uep_to_pipe(uep) = NULL;
350 usbhsh_pipe_to_uep(pipe) = NULL;
351
352 dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__,
353 usbhsh_device_number(hpriv, udev),
354 usb_endpoint_num(&ep->desc),
355 usbhs_pipe_name(pipe));
356 }
357
358 usbhs_unlock(priv, flags);
359 /******************** spin unlock ******************/
360}
361
362/*
363 * endpoint control
364 */
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800365static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
366 struct urb *urb,
367 gfp_t mem_flags)
368{
369 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
370 struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
371 struct usb_host_endpoint *ep = urb->ep;
372 struct usbhsh_ep *uep;
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800373 struct device *dev = usbhs_priv_to_dev(priv);
374 struct usb_endpoint_descriptor *desc = &ep->desc;
375 unsigned long flags;
376
377 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
378 if (!uep) {
379 dev_err(dev, "usbhsh_ep alloc fail\n");
380 return -ENOMEM;
381 }
382
383 /******************** spin lock ********************/
384 usbhs_lock(priv, flags);
385
386 /*
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800387 * init endpoint
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800388 */
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800389 INIT_LIST_HEAD(&uep->ep_list);
390 list_add_tail(&uep->ep_list, &udev->ep_list_head);
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800391
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800392 usbhsh_uep_to_udev(uep) = udev;
393 usbhsh_uep_to_ep(uep) = ep;
394 usbhsh_ep_to_uep(ep) = uep;
395
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800396 usbhs_unlock(priv, flags);
397 /******************** spin unlock ******************/
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800398
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800399 dev_dbg(dev, "%s [%d-%d]\n", __func__,
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800400 usbhsh_device_number(hpriv, udev),
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800401 usb_endpoint_num(desc));
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800402
403 return 0;
404}
405
406static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
407 struct usb_host_endpoint *ep)
408{
409 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
410 struct device *dev = usbhs_priv_to_dev(priv);
411 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800412 unsigned long flags;
413
414 if (!uep)
415 return;
416
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800417 dev_dbg(dev, "%s [%d-%d]\n", __func__,
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800418 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800419 usb_endpoint_num(&ep->desc));
420
421 if (usbhsh_uep_to_pipe(uep))
422 usbhsh_pipe_detach(hpriv, uep);
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800423
424 /******************** spin lock ********************/
425 usbhs_lock(priv, flags);
426
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800427 /* remove this endpoint from udev */
428 list_del_init(&uep->ep_list);
429
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800430 usbhsh_uep_to_udev(uep) = NULL;
431 usbhsh_uep_to_ep(uep) = NULL;
432 usbhsh_ep_to_uep(ep) = NULL;
433
434 usbhs_unlock(priv, flags);
435 /******************** spin unlock ******************/
436
437 kfree(uep);
438}
439
440static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
441 struct usbhsh_device *udev)
442{
443 struct usbhsh_ep *uep, *next;
444
445 list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
446 usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
447}
448
449/*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700450 * device control
451 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700452static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
453 struct usbhsh_device *udev)
454{
455 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
456
457 return hcd->self.root_hub == usbv->parent;
458}
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700459
460static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
461{
462 return !list_empty(&udev->ep_list_head);
463}
464
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800465static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
466 struct urb *urb)
467{
468 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
469 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
470
471 /* usbhsh_device_attach() is still not called */
472 if (!udev)
473 return NULL;
474
475 /* if it is device0, return it */
476 if (0 == usb_pipedevice(urb->pipe))
477 return usbhsh_device0(hpriv);
478
479 /* return attached device */
480 return udev;
481}
482
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700483static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700484 struct urb *urb)
485{
486 struct usbhsh_device *udev = NULL;
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800487 struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
488 struct usbhsh_device *pos;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700489 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
490 struct device *dev = usbhsh_hcd_to_dev(hcd);
491 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
492 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700493 unsigned long flags;
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700494 u16 upphub, hubport;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700495 int i;
496
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800497 /*
498 * This function should be called only while urb is pointing to device0.
499 * It will attach unused usbhsh_device to urb (usbv),
500 * and initialize device0.
501 * You can use usbhsh_device_get() to get "current" udev,
502 * and usbhsh_usbv_to_udev() is for "attached" udev.
503 */
504 if (0 != usb_pipedevice(urb->pipe)) {
505 dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
506 return NULL;
507 }
508
Kuninori Morimotod399f902011-10-31 00:48:35 -0700509 /******************** spin lock ********************/
510 usbhs_lock(priv, flags);
511
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700512 /*
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800513 * find unused device
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700514 */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800515 usbhsh_for_each_udev(pos, hpriv, i) {
516 if (usbhsh_udev_is_used(pos))
517 continue;
518 udev = pos;
519 break;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700520 }
521
Kuninori Morimotod399f902011-10-31 00:48:35 -0700522 if (udev) {
523 /*
524 * usbhsh_usbv_to_udev()
525 * usbhsh_udev_to_usbv()
526 * will be enable
527 */
528 dev_set_drvdata(&usbv->dev, udev);
529 udev->usbv = usbv;
530 }
531
532 usbhs_unlock(priv, flags);
533 /******************** spin unlock ******************/
534
Kuninori Morimotoab142302011-10-31 00:48:00 -0700535 if (!udev) {
536 dev_err(dev, "no free usbhsh_device\n");
537 return NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700538 }
539
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800540 if (usbhsh_device_has_endpoint(udev)) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700541 dev_warn(dev, "udev have old endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800542 usbhsh_endpoint_detach_all(hpriv, udev);
543 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700544
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800545 if (usbhsh_device_has_endpoint(udev0)) {
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800546 dev_warn(dev, "udev0 have old endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800547 usbhsh_endpoint_detach_all(hpriv, udev0);
548 }
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800549
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700550 /* uep will be attached */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800551 INIT_LIST_HEAD(&udev0->ep_list_head);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700552 INIT_LIST_HEAD(&udev->ep_list_head);
553
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800554 /*
555 * set device0 config
556 */
557 usbhs_set_device_config(priv,
558 0, 0, 0, usbv->speed);
559
560 /*
561 * set new device config
562 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700563 upphub = 0;
564 hubport = 0;
565 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
566 /* if udev is not connected to rhdev, it means parent is Hub */
567 struct usbhsh_device *parent = usbhsh_device_parent(udev);
568
569 upphub = usbhsh_device_number(hpriv, parent);
570 hubport = usbhsh_device_hubport(udev);
571
572 dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
573 upphub, hubport, parent);
574 }
575
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700576 usbhs_set_device_config(priv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700577 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700578 upphub, hubport, usbv->speed);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700579
580 dev_dbg(dev, "%s [%d](%p)\n", __func__,
581 usbhsh_device_number(hpriv, udev), udev);
582
583 return udev;
584}
585
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700586static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700587 struct usbhsh_device *udev)
588{
589 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700590 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700591 struct device *dev = usbhsh_hcd_to_dev(hcd);
592 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700593 unsigned long flags;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700594
595 dev_dbg(dev, "%s [%d](%p)\n", __func__,
596 usbhsh_device_number(hpriv, udev), udev);
597
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800598 if (usbhsh_device_has_endpoint(udev)) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700599 dev_warn(dev, "udev still have endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800600 usbhsh_endpoint_detach_all(hpriv, udev);
601 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700602
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800603 /*
604 * There is nothing to do if it is device0.
605 * see
606 * usbhsh_device_attach()
607 * usbhsh_device_get()
608 */
609 if (0 == usbhsh_device_number(hpriv, udev))
610 return;
611
Kuninori Morimotod399f902011-10-31 00:48:35 -0700612 /******************** spin lock ********************/
613 usbhs_lock(priv, flags);
614
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700615 /*
616 * usbhsh_usbv_to_udev()
617 * usbhsh_udev_to_usbv()
618 * will be disable
619 */
620 dev_set_drvdata(&usbv->dev, NULL);
621 udev->usbv = NULL;
Kuninori Morimotod399f902011-10-31 00:48:35 -0700622
623 usbhs_unlock(priv, flags);
624 /******************** spin unlock ******************/
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700625}
626
627/*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700628 * queue push/pop
629 */
630static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
631{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700632 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700633 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
634 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
635 struct urb *urb = ureq->urb;
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800636 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700637 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto6d0376f2011-12-08 18:31:11 -0800638 int status = 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700639
640 dev_dbg(dev, "%s\n", __func__);
641
642 if (!urb) {
643 dev_warn(dev, "pkt doesn't have urb\n");
644 return;
645 }
646
Kuninori Morimoto6d0376f2011-12-08 18:31:11 -0800647 if (!usbhsh_is_running(hpriv))
648 status = -ESHUTDOWN;
649
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700650 urb->actual_length = pkt->actual;
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700651 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700652
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800653 usbhsh_endpoint_sequence_save(hpriv, urb, pkt);
654 usbhsh_pipe_detach(hpriv, uep);
655
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700656 usb_hcd_unlink_urb_from_ep(hcd, urb);
Kuninori Morimoto6d0376f2011-12-08 18:31:11 -0800657 usb_hcd_giveback_urb(hcd, urb, status);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700658}
659
660static int usbhsh_queue_push(struct usb_hcd *hcd,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700661 struct urb *urb,
662 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700663{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700664 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700665 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
666 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700667 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700668 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700669 void *buf;
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800670 int len, sequence;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700671
672 if (usb_pipeisoc(urb->pipe)) {
673 dev_err(dev, "pipe iso is not supported now\n");
674 return -EIO;
675 }
676
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700677 /* this ureq will be freed on usbhsh_queue_done() */
678 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
679 if (unlikely(!ureq)) {
680 dev_err(dev, "ureq alloc fail\n");
681 return -ENOMEM;
682 }
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700683
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700684 if (usb_pipein(urb->pipe))
685 pipe->handler = &usbhs_fifo_pio_pop_handler;
686 else
687 pipe->handler = &usbhs_fifo_pio_push_handler;
688
689 buf = (void *)(urb->transfer_buffer + urb->actual_length);
690 len = urb->transfer_buffer_length - urb->actual_length;
691
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800692 sequence = usb_gettoggle(urb->dev,
693 usb_pipeendpoint(urb->pipe),
694 usb_pipeout(urb->pipe));
695
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700696 dev_dbg(dev, "%s\n", __func__);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700697 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800698 buf, len, (urb->transfer_flags & URB_ZERO_PACKET),
699 sequence);
700
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700701 usbhs_pkt_start(pipe);
702
703 return 0;
704}
705
Kuninori Morimoto2d833fa2011-12-08 18:31:37 -0800706static void usbhsh_queue_force_pop(struct usbhs_priv *priv,
707 struct usbhs_pipe *pipe)
708{
709 struct usbhs_pkt *pkt;
710
711 while (1) {
712 pkt = usbhs_pkt_pop(pipe, NULL);
713 if (!pkt)
714 break;
715
716 /*
717 * if all packet are gone, usbhsh_endpoint_disable()
718 * will be called.
719 * then, attached device/endpoint/pipe will be detached
720 */
721 usbhsh_queue_done(priv, pkt);
722 }
723}
724
725static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv)
726{
727 struct usbhs_pipe *pos;
728 int i;
729
730 usbhs_for_each_pipe_with_dcp(pos, priv, i)
731 usbhsh_queue_force_pop(priv, pos);
732}
733
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700734/*
735 * DCP setup stage
736 */
737static int usbhsh_is_request_address(struct urb *urb)
738{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700739 struct usb_ctrlrequest *req;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700740
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700741 req = (struct usb_ctrlrequest *)urb->setup_packet;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700742
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700743 if ((DeviceOutRequest == req->bRequestType << 8) &&
744 (USB_REQ_SET_ADDRESS == req->bRequest))
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700745 return 1;
746 else
747 return 0;
748}
749
750static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
751 struct urb *urb,
752 struct usbhs_pipe *pipe)
753{
754 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
755 struct usb_ctrlrequest req;
756 struct device *dev = usbhs_priv_to_dev(priv);
757
758 /*
759 * wait setup packet ACK
760 * see
761 * usbhsh_irq_setup_ack()
762 * usbhsh_irq_setup_err()
763 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700764 init_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700765
766 /* copy original request */
767 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
768
769 /*
770 * renesas_usbhs can not use original usb address.
771 * see HARDWARE LIMITATION.
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800772 * modify usb address here to use attached device.
773 * see usbhsh_device_attach()
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700774 */
775 if (usbhsh_is_request_address(urb)) {
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800776 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
777 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
778
779 /* udev is a attached device */
780 req.wValue = usbhsh_device_number(hpriv, udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700781 dev_dbg(dev, "create new address - %d\n", req.wValue);
782 }
783
784 /* set request */
785 usbhs_usbreq_set_val(priv, &req);
786
787 /*
788 * wait setup packet ACK
789 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700790 wait_for_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700791
792 dev_dbg(dev, "%s done\n", __func__);
793}
794
795/*
796 * DCP data stage
797 */
798static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
799 struct usbhs_pkt *pkt)
800{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700801 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700802 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700803
804 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
805
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700806 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700807}
808
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700809static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
810 struct urb *urb,
811 struct usbhs_pipe *pipe,
812 gfp_t mem_flags)
813
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700814{
815 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700816
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700817 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
818 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
819 if (unlikely(!ureq))
820 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700821
822 if (usb_pipein(urb->pipe))
823 pipe->handler = &usbhs_dcp_data_stage_in_handler;
824 else
825 pipe->handler = &usbhs_dcp_data_stage_out_handler;
826
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700827 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700828 usbhsh_data_stage_packet_done,
829 urb->transfer_buffer,
830 urb->transfer_buffer_length,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800831 (urb->transfer_flags & URB_ZERO_PACKET),
832 -1);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700833
834 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700835}
836
837/*
838 * DCP status stage
839 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700840static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700841 struct urb *urb,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700842 struct usbhs_pipe *pipe,
843 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700844{
845 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700846
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700847 /* This ureq will be freed on usbhsh_queue_done() */
848 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
849 if (unlikely(!ureq))
850 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700851
852 if (usb_pipein(urb->pipe))
853 pipe->handler = &usbhs_dcp_status_stage_in_handler;
854 else
855 pipe->handler = &usbhs_dcp_status_stage_out_handler;
856
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700857 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700858 usbhsh_queue_done,
859 NULL,
860 urb->transfer_buffer_length,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800861 0, -1);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700862
863 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700864}
865
866static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700867 struct urb *urb,
868 gfp_t mflags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700869{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700870 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700871 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
872 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700873 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700874 int ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700875
876 dev_dbg(dev, "%s\n", __func__);
877
878 /*
879 * setup stage
880 *
881 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
882 */
883 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
884
885 /*
886 * data stage
887 *
888 * It is pushed only when urb has buffer.
889 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700890 if (urb->transfer_buffer_length) {
891 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
892 if (ret < 0) {
893 dev_err(dev, "data stage failed\n");
894 return ret;
895 }
896 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700897
898 /*
899 * status stage
900 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700901 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
902 if (ret < 0) {
903 dev_err(dev, "status stage failed\n");
904 return ret;
905 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700906
907 /*
908 * start pushed packets
909 */
910 usbhs_pkt_start(pipe);
911
912 return 0;
913}
914
915/*
916 * dma map functions
917 */
918static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
919{
920 return 0;
921}
922
923/*
924 * for hc_driver
925 */
926static int usbhsh_host_start(struct usb_hcd *hcd)
927{
928 return 0;
929}
930
931static void usbhsh_host_stop(struct usb_hcd *hcd)
932{
933}
934
935static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
936 struct urb *urb,
937 gfp_t mem_flags)
938{
939 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
940 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
941 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700942 struct usb_host_endpoint *ep = urb->ep;
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700943 struct usbhsh_device *new_udev = NULL;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700944 int is_dir_in = usb_pipein(urb->pipe);
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800945 int i;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700946 int ret;
947
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700948 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700949
Kuninori Morimotob1930da2011-12-08 18:30:23 -0800950 if (!usbhsh_is_running(hpriv)) {
951 ret = -EIO;
952 goto usbhsh_urb_enqueue_error_not_linked;
953 }
954
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700955 ret = usb_hcd_link_urb_to_ep(hcd, urb);
956 if (ret)
957 goto usbhsh_urb_enqueue_error_not_linked;
958
959 /*
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700960 * attach udev if needed
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800961 * see [image of mod_host]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700962 */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800963 if (!usbhsh_device_get(hpriv, urb)) {
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700964 new_udev = usbhsh_device_attach(hpriv, urb);
Kuninori Morimoto37332ee2011-12-08 18:25:37 -0800965 if (!new_udev) {
966 ret = -EIO;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700967 goto usbhsh_urb_enqueue_error_not_linked;
Kuninori Morimoto37332ee2011-12-08 18:25:37 -0800968 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700969 }
970
971 /*
Kuninori Morimoto48250932011-10-31 00:48:59 -0700972 * attach endpoint if needed
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800973 * see [image of mod_host]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700974 */
Kuninori Morimoto48250932011-10-31 00:48:59 -0700975 if (!usbhsh_ep_to_uep(ep)) {
976 ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
977 if (ret < 0)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700978 goto usbhsh_urb_enqueue_error_free_device;
979 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700980
981 /*
Kuninori Morimotoe5679d02011-12-08 18:28:24 -0800982 * attach pipe to endpoint
983 * see [image of mod_host]
984 */
985 for (i = 0; i < 1024; i++) {
986 ret = usbhsh_pipe_attach(hpriv, urb);
987 if (ret < 0)
988 msleep(100);
989 else
990 break;
991 }
992 if (ret < 0)
993 goto usbhsh_urb_enqueue_error_free_endpoint;
994
995 /*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700996 * push packet
997 */
998 if (usb_pipecontrol(urb->pipe))
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700999 ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001000 else
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -07001001 ret = usbhsh_queue_push(hcd, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001002
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -07001003 return ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001004
Kuninori Morimotoe5679d02011-12-08 18:28:24 -08001005usbhsh_urb_enqueue_error_free_endpoint:
1006 usbhsh_endpoint_detach(hpriv, ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001007usbhsh_urb_enqueue_error_free_device:
1008 if (new_udev)
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -07001009 usbhsh_device_detach(hpriv, new_udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001010usbhsh_urb_enqueue_error_not_linked:
1011
1012 dev_dbg(dev, "%s error\n", __func__);
1013
1014 return ret;
1015}
1016
1017static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1018{
1019 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1020 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
1021
Kuninori Morimoto54796542011-12-08 18:26:07 -08001022 if (ureq) {
1023 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1024 struct usbhs_pkt *pkt = &ureq->pkt;
1025
1026 usbhs_pkt_pop(pkt->pipe, pkt);
1027 usbhsh_queue_done(priv, pkt);
1028 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001029
1030 return 0;
1031}
1032
1033static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
1034 struct usb_host_endpoint *ep)
1035{
1036 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
1037 struct usbhsh_device *udev;
1038 struct usbhsh_hpriv *hpriv;
1039
1040 /*
1041 * this function might be called manytimes by same hcd/ep
Kuninori Morimotofca8ab72011-10-31 00:47:24 -07001042 * in-endpoint == out-endpoint if ep == dcp.
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001043 */
1044 if (!uep)
1045 return;
1046
1047 udev = usbhsh_uep_to_udev(uep);
1048 hpriv = usbhsh_hcd_to_hpriv(hcd);
1049
Kuninori Morimoto48250932011-10-31 00:48:59 -07001050 usbhsh_endpoint_detach(hpriv, ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001051
1052 /*
1053 * if there is no endpoint,
1054 * free device
1055 */
1056 if (!usbhsh_device_has_endpoint(udev))
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -07001057 usbhsh_device_detach(hpriv, udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001058}
1059
1060static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
1061{
1062 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1063 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1064 struct device *dev = usbhs_priv_to_dev(priv);
1065 int roothub_id = 1; /* only 1 root hub */
1066
1067 /*
1068 * does port stat was changed ?
1069 * check USB_PORT_STAT_C_xxx << 16
1070 */
1071 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
1072 *buf = (1 << roothub_id);
1073 else
1074 *buf = 0;
1075
1076 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
1077
1078 return !!(*buf);
1079}
1080
1081static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
1082 u16 typeReq, u16 wValue,
1083 u16 wIndex, char *buf, u16 wLength)
1084{
1085 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1086 struct device *dev = usbhs_priv_to_dev(priv);
1087
1088 switch (wValue) {
1089 case C_HUB_OVER_CURRENT:
1090 case C_HUB_LOCAL_POWER:
1091 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
1092 return 0;
1093 }
1094
1095 return -EPIPE;
1096}
1097
1098static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
1099 u16 typeReq, u16 wValue,
1100 u16 wIndex, char *buf, u16 wLength)
1101{
1102 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1103 struct device *dev = usbhs_priv_to_dev(priv);
1104 int enable = (typeReq == SetPortFeature);
1105 int speed, i, timeout = 128;
1106 int roothub_id = 1; /* only 1 root hub */
1107
1108 /* common error */
1109 if (wIndex > roothub_id || wLength != 0)
1110 return -EPIPE;
1111
1112 /* check wValue */
1113 switch (wValue) {
1114 case USB_PORT_FEAT_POWER:
1115 usbhs_vbus_ctrl(priv, enable);
1116 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
1117 break;
1118
1119 case USB_PORT_FEAT_ENABLE:
1120 case USB_PORT_FEAT_SUSPEND:
1121 case USB_PORT_FEAT_C_ENABLE:
1122 case USB_PORT_FEAT_C_SUSPEND:
1123 case USB_PORT_FEAT_C_CONNECTION:
1124 case USB_PORT_FEAT_C_OVER_CURRENT:
1125 case USB_PORT_FEAT_C_RESET:
1126 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
1127 break;
1128
1129 case USB_PORT_FEAT_RESET:
1130 if (!enable)
1131 break;
1132
1133 usbhsh_port_stat_clear(hpriv,
1134 USB_PORT_STAT_HIGH_SPEED |
1135 USB_PORT_STAT_LOW_SPEED);
1136
Kuninori Morimoto2d833fa2011-12-08 18:31:37 -08001137 usbhsh_queue_force_pop_all(priv);
1138
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001139 usbhs_bus_send_reset(priv);
1140 msleep(20);
1141 usbhs_bus_send_sof_enable(priv);
1142
1143 for (i = 0; i < timeout ; i++) {
1144 switch (usbhs_bus_get_speed(priv)) {
1145 case USB_SPEED_LOW:
1146 speed = USB_PORT_STAT_LOW_SPEED;
1147 goto got_usb_bus_speed;
1148 case USB_SPEED_HIGH:
1149 speed = USB_PORT_STAT_HIGH_SPEED;
1150 goto got_usb_bus_speed;
1151 case USB_SPEED_FULL:
1152 speed = 0;
1153 goto got_usb_bus_speed;
1154 }
1155
1156 msleep(20);
1157 }
1158 return -EPIPE;
1159
1160got_usb_bus_speed:
1161 usbhsh_port_stat_set(hpriv, speed);
1162 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
1163
1164 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1165 __func__, speed);
1166
1167 /* status change is not needed */
1168 return 0;
1169
1170 default:
1171 return -EPIPE;
1172 }
1173
1174 /* set/clear status */
1175 if (enable)
1176 usbhsh_port_stat_set(hpriv, (1 << wValue));
1177 else
1178 usbhsh_port_stat_clear(hpriv, (1 << wValue));
1179
1180 return 0;
1181}
1182
1183static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1184 u16 typeReq, u16 wValue,
1185 u16 wIndex, char *buf, u16 wLength)
1186{
1187 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1188 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
1189 struct device *dev = usbhs_priv_to_dev(priv);
1190 int roothub_id = 1; /* only 1 root hub */
1191
1192 switch (typeReq) {
1193 case GetHubStatus:
1194 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
1195
1196 *buf = 0x00;
1197 break;
1198
1199 case GetPortStatus:
1200 if (wIndex != roothub_id)
1201 return -EPIPE;
1202
1203 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
1204 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
1205 break;
1206
1207 case GetHubDescriptor:
1208 desc->bDescriptorType = 0x29;
1209 desc->bHubContrCurrent = 0;
1210 desc->bNbrPorts = roothub_id;
1211 desc->bDescLength = 9;
1212 desc->bPwrOn2PwrGood = 0;
1213 desc->wHubCharacteristics = cpu_to_le16(0x0011);
1214 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
1215 desc->u.hs.DeviceRemovable[1] = ~0;
1216 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
1217 break;
1218 }
1219
1220 return 0;
1221}
1222
1223static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1224 u16 wIndex, char *buf, u16 wLength)
1225{
1226 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1227 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1228 struct device *dev = usbhs_priv_to_dev(priv);
1229 int ret = -EPIPE;
1230
1231 switch (typeReq) {
1232
1233 /* Hub Feature */
1234 case ClearHubFeature:
1235 case SetHubFeature:
1236 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1237 wValue, wIndex, buf, wLength);
1238 break;
1239
1240 /* Port Feature */
1241 case SetPortFeature:
1242 case ClearPortFeature:
1243 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1244 wValue, wIndex, buf, wLength);
1245 break;
1246
1247 /* Get status */
1248 case GetHubStatus:
1249 case GetPortStatus:
1250 case GetHubDescriptor:
1251 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1252 wValue, wIndex, buf, wLength);
1253 break;
1254 }
1255
1256 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1257 typeReq, ret, usbhsh_port_stat_get(hpriv));
1258
1259 return ret;
1260}
1261
1262static struct hc_driver usbhsh_driver = {
1263 .description = usbhsh_hcd_name,
1264 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1265
1266 /*
1267 * generic hardware linkage
1268 */
1269 .flags = HCD_USB2,
1270
1271 .start = usbhsh_host_start,
1272 .stop = usbhsh_host_stop,
1273
1274 /*
1275 * managing i/o requests and associated device resources
1276 */
1277 .urb_enqueue = usbhsh_urb_enqueue,
1278 .urb_dequeue = usbhsh_urb_dequeue,
1279 .endpoint_disable = usbhsh_endpoint_disable,
1280
1281 /*
1282 * root hub
1283 */
1284 .hub_status_data = usbhsh_hub_status_data,
1285 .hub_control = usbhsh_hub_control,
1286};
1287
1288/*
1289 * interrupt functions
1290 */
1291static int usbhsh_irq_attch(struct usbhs_priv *priv,
1292 struct usbhs_irq_state *irq_state)
1293{
1294 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1295 struct device *dev = usbhs_priv_to_dev(priv);
1296
1297 dev_dbg(dev, "device attached\n");
1298
1299 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1300 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1301
Kuninori Morimoto31e00fd2011-12-08 18:29:22 -08001302 /*
1303 * attch interrupt might happen infinitely on some device
1304 * (on self power USB hub ?)
1305 * disable it here.
Kuninori Morimotob1930da2011-12-08 18:30:23 -08001306 *
1307 * usbhsh_is_running() becomes effective
1308 * according to this process.
1309 * see
1310 * usbhsh_is_running()
1311 * usbhsh_urb_enqueue()
Kuninori Morimoto31e00fd2011-12-08 18:29:22 -08001312 */
1313 hpriv->mod.irq_attch = NULL;
1314 usbhs_irq_callback_update(priv, &hpriv->mod);
1315
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001316 return 0;
1317}
1318
1319static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1320 struct usbhs_irq_state *irq_state)
1321{
1322 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1323 struct device *dev = usbhs_priv_to_dev(priv);
1324
1325 dev_dbg(dev, "device detached\n");
1326
1327 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1328 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1329
Kuninori Morimoto31e00fd2011-12-08 18:29:22 -08001330 /*
1331 * enable attch interrupt again
Kuninori Morimotob1930da2011-12-08 18:30:23 -08001332 *
1333 * usbhsh_is_running() becomes invalid
1334 * according to this process.
1335 * see
1336 * usbhsh_is_running()
1337 * usbhsh_urb_enqueue()
Kuninori Morimoto31e00fd2011-12-08 18:29:22 -08001338 */
1339 hpriv->mod.irq_attch = usbhsh_irq_attch;
1340 usbhs_irq_callback_update(priv, &hpriv->mod);
1341
Kuninori Morimoto2d833fa2011-12-08 18:31:37 -08001342 /*
1343 * usbhsh_queue_force_pop_all() should be called
1344 * after usbhsh_is_running() becomes invalid.
1345 */
1346 usbhsh_queue_force_pop_all(priv);
1347
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001348 return 0;
1349}
1350
1351static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1352 struct usbhs_irq_state *irq_state)
1353{
1354 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1355 struct device *dev = usbhs_priv_to_dev(priv);
1356
1357 dev_dbg(dev, "setup packet OK\n");
1358
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001359 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001360
1361 return 0;
1362}
1363
1364static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1365 struct usbhs_irq_state *irq_state)
1366{
1367 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1368 struct device *dev = usbhs_priv_to_dev(priv);
1369
1370 dev_dbg(dev, "setup packet Err\n");
1371
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001372 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001373
1374 return 0;
1375}
1376
1377/*
1378 * module start/stop
1379 */
1380static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1381{
1382 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001383 struct usbhs_pipe *pipe;
1384 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1385 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1386 int old_type, dir_in, i;
1387
1388 /* init all pipe */
1389 old_type = USB_ENDPOINT_XFER_CONTROL;
1390 for (i = 0; i < pipe_size; i++) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001391
1392 /*
1393 * data "output" will be finished as soon as possible,
1394 * but there is no guaranty at data "input" case.
1395 *
1396 * "input" needs "standby" pipe.
1397 * So, "input" direction pipe > "output" direction pipe
1398 * is good idea.
1399 *
1400 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1401 * and the other will be input direction here.
1402 *
1403 * ex)
1404 * ...
1405 * USB_ENDPOINT_XFER_ISOC -> dir out
1406 * USB_ENDPOINT_XFER_ISOC -> dir in
1407 * USB_ENDPOINT_XFER_BULK -> dir out
1408 * USB_ENDPOINT_XFER_BULK -> dir in
1409 * USB_ENDPOINT_XFER_BULK -> dir in
1410 * ...
1411 */
1412 dir_in = (pipe_type[i] == old_type);
1413 old_type = pipe_type[i];
1414
1415 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1416 pipe = usbhs_dcp_malloc(priv);
1417 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1418 } else {
1419 pipe = usbhs_pipe_malloc(priv,
1420 pipe_type[i],
1421 dir_in);
1422 }
1423
Kuninori Morimotoe5679d02011-12-08 18:28:24 -08001424 pipe->mod_private = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001425 }
1426}
1427
1428static int usbhsh_start(struct usbhs_priv *priv)
1429{
1430 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1431 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1432 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1433 struct device *dev = usbhs_priv_to_dev(priv);
1434 int ret;
1435
1436 /* add hcd */
1437 ret = usb_add_hcd(hcd, 0, 0);
1438 if (ret < 0)
1439 return 0;
1440
1441 /*
1442 * pipe initialize and enable DCP
1443 */
1444 usbhs_pipe_init(priv,
1445 usbhsh_dma_map_ctrl);
1446 usbhs_fifo_init(priv);
1447 usbhsh_pipe_init_for_host(priv);
1448
1449 /*
1450 * system config enble
1451 * - HI speed
1452 * - host
1453 * - usb module
1454 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001455 usbhs_sys_host_ctrl(priv, 1);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001456
1457 /*
1458 * enable irq callback
1459 */
1460 mod->irq_attch = usbhsh_irq_attch;
1461 mod->irq_dtch = usbhsh_irq_dtch;
1462 mod->irq_sack = usbhsh_irq_setup_ack;
1463 mod->irq_sign = usbhsh_irq_setup_err;
1464 usbhs_irq_callback_update(priv, mod);
1465
1466 dev_dbg(dev, "start host\n");
1467
1468 return ret;
1469}
1470
1471static int usbhsh_stop(struct usbhs_priv *priv)
1472{
1473 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1474 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001475 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001476 struct device *dev = usbhs_priv_to_dev(priv);
1477
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001478 /*
1479 * disable irq callback
1480 */
1481 mod->irq_attch = NULL;
1482 mod->irq_dtch = NULL;
1483 mod->irq_sack = NULL;
1484 mod->irq_sign = NULL;
1485 usbhs_irq_callback_update(priv, mod);
1486
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001487 usb_remove_hcd(hcd);
1488
1489 /* disable sys */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001490 usbhs_sys_host_ctrl(priv, 0);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001491
1492 dev_dbg(dev, "quit host\n");
1493
1494 return 0;
1495}
1496
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001497int usbhs_mod_host_probe(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001498{
1499 struct usbhsh_hpriv *hpriv;
1500 struct usb_hcd *hcd;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001501 struct usbhsh_device *udev;
1502 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001503 int i;
1504
1505 /* initialize hcd */
1506 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1507 if (!hcd) {
1508 dev_err(dev, "Failed to create hcd\n");
1509 return -ENOMEM;
1510 }
1511
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001512 /*
1513 * CAUTION
1514 *
1515 * There is no guarantee that it is possible to access usb module here.
1516 * Don't accesses to it.
1517 * The accesse will be enable after "usbhsh_start"
1518 */
1519
1520 hpriv = usbhsh_hcd_to_hpriv(hcd);
1521
1522 /*
1523 * register itself
1524 */
1525 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1526
1527 /* init hpriv */
1528 hpriv->mod.name = "host";
1529 hpriv->mod.start = usbhsh_start;
1530 hpriv->mod.stop = usbhsh_stop;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001531 usbhsh_port_stat_init(hpriv);
1532
1533 /* init all device */
1534 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1535 udev->usbv = NULL;
1536 INIT_LIST_HEAD(&udev->ep_list_head);
1537 }
1538
1539 dev_info(dev, "host probed\n");
1540
1541 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001542}
1543
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001544int usbhs_mod_host_remove(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001545{
1546 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1547 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1548
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001549 usb_put_hcd(hcd);
1550
1551 return 0;
1552}