blob: 5b6ae2a8d303905d339fbcf918afc1debec95357 [file] [log] [blame]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001/*
2 * Renesas USB driver
3 *
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 */
17#include <linux/io.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <linux/usb.h>
23#include <linux/usb/hcd.h>
24#include "common.h"
25
26/*
27 *** HARDWARE LIMITATION ***
28 *
29 * 1) renesas_usbhs has a limited number of controllable devices.
30 * it can control only 9 devices in generally.
31 * see DEVADDn / DCPMAXP / PIPEMAXP.
32 *
33 * 2) renesas_usbhs pipe number is limited.
34 * the pipe will be re-used for each devices.
35 * so, software should control DATA0/1 sequence of each devices.
36 */
37
38
39/*
40 * image of mod_host
41 *
42 * +--------+
43 * | udev 0 | --> it is used when set address
44 * +--------+
45 *
46 * +--------+ pipes are reused for each uep.
47 * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when
48 * +--------+ | | target device was changed
49 * +- [uep 1 (bulk)] --|---+ +--------------+
50 * | +--------------> | pipe0 (dcp) |
51 * +- [uep 2 (bulk)] --|---|---+ +--------------+
52 * | | | | pipe1 (isoc) |
53 * +--------+ | | | +--------------+
54 * | udev 2 |-+- [uep 0 (dcp) ] --+ +-- |------> | pipe2 (bulk) |
55 * +--------+ | | | | +--------------+
56 * +- [uep 1 (int) ] --|-+ | +------> | pipe3 (bulk) |
57 * | | | | +--------------+
58 * +--------+ | +-|---|------> | pipe4 (int) |
59 * | udev 3 |-+- [uep 0 (dcp) ] --+ | | +--------------+
60 * +--------+ | | | | .... |
61 * +- [uep 1 (bulk)] ------+ | | .... |
62 * | |
63 * +- [uep 2 (bulk)]-----------+
64 */
65
66
67/*
68 * struct
69 */
70struct usbhsh_pipe_info {
71 unsigned int usr_cnt; /* see usbhsh_endpoint_alloc() */
72};
73
74struct usbhsh_request {
75 struct urb *urb;
76 struct usbhs_pkt pkt;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070077};
78
79struct usbhsh_device {
80 struct usb_device *usbv;
81 struct list_head ep_list_head; /* list of usbhsh_ep */
82};
83
84struct usbhsh_ep {
85 struct usbhs_pipe *pipe;
86 struct usbhsh_device *udev; /* attached udev */
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -080087 struct usb_host_endpoint *ep;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070088 struct list_head ep_list; /* list to usbhsh_device */
89
90 int maxp;
91};
92
93#define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
94#define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
95struct usbhsh_hpriv {
96 struct usbhs_mod mod;
97 struct usbhs_pipe *dcp;
98
99 struct usbhsh_device udev[USBHSH_DEVICE_MAX];
100
101 struct usbhsh_pipe_info *pipe_info;
102 int pipe_size;
103
104 u32 port_stat; /* USB_PORT_STAT_xxx */
105
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700106 struct completion setup_ack_done;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700107};
108
109
110static const char usbhsh_hcd_name[] = "renesas_usbhs host";
111
112/*
113 * macro
114 */
115#define usbhsh_priv_to_hpriv(priv) \
116 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
117
118#define __usbhsh_for_each_hpipe(start, pos, h, i) \
119 for (i = start, pos = (h)->hpipe + i; \
120 i < (h)->hpipe_size; \
121 i++, pos = (h)->hpipe + i)
122
123#define usbhsh_for_each_hpipe(pos, hpriv, i) \
124 __usbhsh_for_each_hpipe(1, pos, hpriv, i)
125
126#define usbhsh_for_each_hpipe_with_dcp(pos, hpriv, i) \
127 __usbhsh_for_each_hpipe(0, pos, hpriv, i)
128
129#define __usbhsh_for_each_udev(start, pos, h, i) \
130 for (i = start, pos = (h)->udev + i; \
131 i < USBHSH_DEVICE_MAX; \
132 i++, pos = (h)->udev + i)
133
134#define usbhsh_for_each_udev(pos, hpriv, i) \
135 __usbhsh_for_each_udev(1, pos, hpriv, i)
136
137#define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
138 __usbhsh_for_each_udev(0, pos, hpriv, i)
139
140#define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
141#define usbhsh_hcd_to_dev(h) ((h)->self.controller)
142
143#define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
144#define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
145#define usbhsh_hpriv_to_hcd(h) \
146 container_of((void *)h, struct usb_hcd, hcd_priv)
147
148#define usbhsh_ep_to_uep(u) ((u)->hcpriv)
149#define usbhsh_uep_to_pipe(u) ((u)->pipe)
150#define usbhsh_uep_to_udev(u) ((u)->udev)
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800151#define usbhsh_uep_to_ep(u) ((u)->ep)
152
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700153#define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
154#define usbhsh_urb_to_usbv(u) ((u)->dev)
155
156#define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
157
158#define usbhsh_udev_to_usbv(h) ((h)->usbv)
Kuninori Morimotoab142302011-10-31 00:48:00 -0700159#define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700160
161#define usbhsh_pipe_info(p) ((p)->mod_private)
162
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700163#define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
164#define usbhsh_device_hubport(d) ((d)->usbv->portnum)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700165#define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
166#define usbhsh_device_nth(h, d) ((h)->udev + d)
167#define usbhsh_device0(h) usbhsh_device_nth(h, 0)
168
169#define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
170#define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
171#define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
172#define usbhsh_port_stat_get(h) ((h)->port_stat)
173
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700174#define usbhsh_pkt_to_ureq(p) \
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700175 container_of((void *)p, struct usbhsh_request, pkt)
176
177/*
178 * req alloc/free
179 */
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700180static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700181 struct urb *urb,
182 gfp_t mem_flags)
183{
184 struct usbhsh_request *ureq;
185 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
186 struct device *dev = usbhs_priv_to_dev(priv);
187
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700188 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700189 if (!ureq) {
190 dev_err(dev, "ureq alloc fail\n");
191 return NULL;
192 }
193
194 usbhs_pkt_init(&ureq->pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700195 ureq->urb = urb;
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700196 usbhsh_urb_to_ureq(urb) = ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700197
198 return ureq;
199}
200
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700201static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700202 struct usbhsh_request *ureq)
203{
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700204 usbhsh_urb_to_ureq(ureq->urb) = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700205 ureq->urb = NULL;
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700206
207 kfree(ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700208}
209
210/*
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800211 * end-point control
212 */
213static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
214 struct urb *urb);
215static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
216 struct urb *urb,
217 gfp_t mem_flags)
218{
219 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
220 struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb);
221 struct usb_host_endpoint *ep = urb->ep;
222 struct usbhsh_ep *uep;
223 struct usbhsh_pipe_info *info;
224 struct usbhs_pipe *best_pipe = NULL;
225 struct device *dev = usbhs_priv_to_dev(priv);
226 struct usb_endpoint_descriptor *desc = &ep->desc;
227 unsigned long flags;
228
229 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
230 if (!uep) {
231 dev_err(dev, "usbhsh_ep alloc fail\n");
232 return -ENOMEM;
233 }
234
235 /******************** spin lock ********************/
236 usbhs_lock(priv, flags);
237
238 /*
239 * find best pipe for endpoint
240 * see
241 * HARDWARE LIMITATION
242 */
243 if (usb_endpoint_xfer_control(desc)) {
244 /* best pipe is DCP */
245 best_pipe = usbhsh_hpriv_to_dcp(hpriv);
246 } else {
247 struct usbhs_pipe *pipe;
248 unsigned int min_usr = ~0;
249 int dir_in_req = !!usb_pipein(urb->pipe);
250 int i, dir_in;
251
252 usbhs_for_each_pipe(pipe, priv, i) {
253 if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
254 continue;
255
256 dir_in = !!usbhs_pipe_is_dir_in(pipe);
257 if (0 != (dir_in - dir_in_req))
258 continue;
259
260 info = usbhsh_pipe_info(pipe);
261 if (min_usr > info->usr_cnt) {
262 min_usr = info->usr_cnt;
263 best_pipe = pipe;
264 }
265 }
266 }
267
268 if (best_pipe) {
269 /* update pipe user count */
270 info = usbhsh_pipe_info(best_pipe);
271 info->usr_cnt++;
272
273 /* init this endpoint, and attach it to udev */
274 INIT_LIST_HEAD(&uep->ep_list);
275 list_add_tail(&uep->ep_list, &udev->ep_list_head);
276 }
277
278 usbhs_unlock(priv, flags);
279 /******************** spin unlock ******************/
280
281 if (unlikely(!best_pipe)) {
282 dev_err(dev, "couldn't find best pipe\n");
283 kfree(uep);
284 return -EIO;
285 }
286
287 /*
288 * init uep
289 */
290 uep->pipe = best_pipe;
291 uep->maxp = usb_endpoint_maxp(desc);
292 usbhsh_uep_to_udev(uep) = udev;
293 usbhsh_uep_to_ep(uep) = ep;
294 usbhsh_ep_to_uep(ep) = uep;
295
296 /*
297 * usbhs_pipe_config_update() should be called after
298 * usbhs_set_device_config()
299 * see
300 * DCPMAXP/PIPEMAXP
301 */
302 usbhs_pipe_sequence_data0(uep->pipe);
303 usbhs_pipe_config_update(uep->pipe,
304 usbhsh_device_number(hpriv, udev),
305 usb_endpoint_num(desc),
306 uep->maxp);
307
308 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
309 usbhsh_device_number(hpriv, udev),
310 usbhs_pipe_name(uep->pipe), uep);
311
312 return 0;
313}
314
315static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
316 struct usb_host_endpoint *ep)
317{
318 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
319 struct device *dev = usbhs_priv_to_dev(priv);
320 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
321 struct usbhsh_pipe_info *info;
322 unsigned long flags;
323
324 if (!uep)
325 return;
326
327 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
328 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
329 usbhs_pipe_name(uep->pipe), uep);
330
331 /******************** spin lock ********************/
332 usbhs_lock(priv, flags);
333
334 info = usbhsh_pipe_info(uep->pipe);
335 info->usr_cnt--;
336
337 /* remove this endpoint from udev */
338 list_del_init(&uep->ep_list);
339
340 uep->pipe = NULL;
341 uep->maxp = 0;
342 usbhsh_uep_to_udev(uep) = NULL;
343 usbhsh_uep_to_ep(uep) = NULL;
344 usbhsh_ep_to_uep(ep) = NULL;
345
346 usbhs_unlock(priv, flags);
347 /******************** spin unlock ******************/
348
349 kfree(uep);
350}
351
352static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv,
353 struct usbhsh_device *udev)
354{
355 struct usbhsh_ep *uep, *next;
356
357 list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list)
358 usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep));
359}
360
361/*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700362 * device control
363 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700364static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
365 struct usbhsh_device *udev)
366{
367 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
368
369 return hcd->self.root_hub == usbv->parent;
370}
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700371
372static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
373{
374 return !list_empty(&udev->ep_list_head);
375}
376
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800377static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv,
378 struct urb *urb)
379{
380 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
381 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
382
383 /* usbhsh_device_attach() is still not called */
384 if (!udev)
385 return NULL;
386
387 /* if it is device0, return it */
388 if (0 == usb_pipedevice(urb->pipe))
389 return usbhsh_device0(hpriv);
390
391 /* return attached device */
392 return udev;
393}
394
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700395static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700396 struct urb *urb)
397{
398 struct usbhsh_device *udev = NULL;
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800399 struct usbhsh_device *udev0 = usbhsh_device0(hpriv);
400 struct usbhsh_device *pos;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700401 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
402 struct device *dev = usbhsh_hcd_to_dev(hcd);
403 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
404 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700405 unsigned long flags;
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700406 u16 upphub, hubport;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700407 int i;
408
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800409 /*
410 * This function should be called only while urb is pointing to device0.
411 * It will attach unused usbhsh_device to urb (usbv),
412 * and initialize device0.
413 * You can use usbhsh_device_get() to get "current" udev,
414 * and usbhsh_usbv_to_udev() is for "attached" udev.
415 */
416 if (0 != usb_pipedevice(urb->pipe)) {
417 dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__);
418 return NULL;
419 }
420
Kuninori Morimotod399f902011-10-31 00:48:35 -0700421 /******************** spin lock ********************/
422 usbhs_lock(priv, flags);
423
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700424 /*
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800425 * find unused device
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700426 */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800427 usbhsh_for_each_udev(pos, hpriv, i) {
428 if (usbhsh_udev_is_used(pos))
429 continue;
430 udev = pos;
431 break;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700432 }
433
Kuninori Morimotod399f902011-10-31 00:48:35 -0700434 if (udev) {
435 /*
436 * usbhsh_usbv_to_udev()
437 * usbhsh_udev_to_usbv()
438 * will be enable
439 */
440 dev_set_drvdata(&usbv->dev, udev);
441 udev->usbv = usbv;
442 }
443
444 usbhs_unlock(priv, flags);
445 /******************** spin unlock ******************/
446
Kuninori Morimotoab142302011-10-31 00:48:00 -0700447 if (!udev) {
448 dev_err(dev, "no free usbhsh_device\n");
449 return NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700450 }
451
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800452 if (usbhsh_device_has_endpoint(udev)) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700453 dev_warn(dev, "udev have old endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800454 usbhsh_endpoint_detach_all(hpriv, udev);
455 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700456
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800457 if (usbhsh_device_has_endpoint(udev0)) {
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800458 dev_warn(dev, "udev0 have old endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800459 usbhsh_endpoint_detach_all(hpriv, udev0);
460 }
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800461
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700462 /* uep will be attached */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800463 INIT_LIST_HEAD(&udev0->ep_list_head);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700464 INIT_LIST_HEAD(&udev->ep_list_head);
465
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800466 /*
467 * set device0 config
468 */
469 usbhs_set_device_config(priv,
470 0, 0, 0, usbv->speed);
471
472 /*
473 * set new device config
474 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700475 upphub = 0;
476 hubport = 0;
477 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
478 /* if udev is not connected to rhdev, it means parent is Hub */
479 struct usbhsh_device *parent = usbhsh_device_parent(udev);
480
481 upphub = usbhsh_device_number(hpriv, parent);
482 hubport = usbhsh_device_hubport(udev);
483
484 dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
485 upphub, hubport, parent);
486 }
487
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700488 usbhs_set_device_config(priv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700489 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700490 upphub, hubport, usbv->speed);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700491
492 dev_dbg(dev, "%s [%d](%p)\n", __func__,
493 usbhsh_device_number(hpriv, udev), udev);
494
495 return udev;
496}
497
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700498static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700499 struct usbhsh_device *udev)
500{
501 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700502 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700503 struct device *dev = usbhsh_hcd_to_dev(hcd);
504 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700505 unsigned long flags;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700506
507 dev_dbg(dev, "%s [%d](%p)\n", __func__,
508 usbhsh_device_number(hpriv, udev), udev);
509
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800510 if (usbhsh_device_has_endpoint(udev)) {
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700511 dev_warn(dev, "udev still have endpoint\n");
Kuninori Morimotoe4c57de2011-12-08 18:27:49 -0800512 usbhsh_endpoint_detach_all(hpriv, udev);
513 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700514
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800515 /*
516 * There is nothing to do if it is device0.
517 * see
518 * usbhsh_device_attach()
519 * usbhsh_device_get()
520 */
521 if (0 == usbhsh_device_number(hpriv, udev))
522 return;
523
Kuninori Morimotod399f902011-10-31 00:48:35 -0700524 /******************** spin lock ********************/
525 usbhs_lock(priv, flags);
526
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700527 /*
528 * usbhsh_usbv_to_udev()
529 * usbhsh_udev_to_usbv()
530 * will be disable
531 */
532 dev_set_drvdata(&usbv->dev, NULL);
533 udev->usbv = NULL;
Kuninori Morimotod399f902011-10-31 00:48:35 -0700534
535 usbhs_unlock(priv, flags);
536 /******************** spin unlock ******************/
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700537}
538
539/*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700540 * queue push/pop
541 */
542static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
543{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700544 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700545 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
546 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
547 struct urb *urb = ureq->urb;
548 struct device *dev = usbhs_priv_to_dev(priv);
549
550 dev_dbg(dev, "%s\n", __func__);
551
552 if (!urb) {
553 dev_warn(dev, "pkt doesn't have urb\n");
554 return;
555 }
556
557 urb->actual_length = pkt->actual;
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700558 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700559
560 usb_hcd_unlink_urb_from_ep(hcd, urb);
561 usb_hcd_giveback_urb(hcd, urb, 0);
562}
563
564static int usbhsh_queue_push(struct usb_hcd *hcd,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700565 struct urb *urb,
566 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700567{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700568 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700569 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
570 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700571 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700572 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700573 void *buf;
574 int len;
575
576 if (usb_pipeisoc(urb->pipe)) {
577 dev_err(dev, "pipe iso is not supported now\n");
578 return -EIO;
579 }
580
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700581 /* this ureq will be freed on usbhsh_queue_done() */
582 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
583 if (unlikely(!ureq)) {
584 dev_err(dev, "ureq alloc fail\n");
585 return -ENOMEM;
586 }
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700587
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700588 if (usb_pipein(urb->pipe))
589 pipe->handler = &usbhs_fifo_pio_pop_handler;
590 else
591 pipe->handler = &usbhs_fifo_pio_push_handler;
592
593 buf = (void *)(urb->transfer_buffer + urb->actual_length);
594 len = urb->transfer_buffer_length - urb->actual_length;
595
596 dev_dbg(dev, "%s\n", __func__);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700597 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700598 buf, len, (urb->transfer_flags & URB_ZERO_PACKET));
599 usbhs_pkt_start(pipe);
600
601 return 0;
602}
603
604/*
605 * DCP setup stage
606 */
607static int usbhsh_is_request_address(struct urb *urb)
608{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700609 struct usb_ctrlrequest *req;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700610
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700611 req = (struct usb_ctrlrequest *)urb->setup_packet;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700612
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700613 if ((DeviceOutRequest == req->bRequestType << 8) &&
614 (USB_REQ_SET_ADDRESS == req->bRequest))
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700615 return 1;
616 else
617 return 0;
618}
619
620static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
621 struct urb *urb,
622 struct usbhs_pipe *pipe)
623{
624 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
625 struct usb_ctrlrequest req;
626 struct device *dev = usbhs_priv_to_dev(priv);
627
628 /*
629 * wait setup packet ACK
630 * see
631 * usbhsh_irq_setup_ack()
632 * usbhsh_irq_setup_err()
633 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700634 init_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700635
636 /* copy original request */
637 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
638
639 /*
640 * renesas_usbhs can not use original usb address.
641 * see HARDWARE LIMITATION.
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800642 * modify usb address here to use attached device.
643 * see usbhsh_device_attach()
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700644 */
645 if (usbhsh_is_request_address(urb)) {
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800646 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
647 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
648
649 /* udev is a attached device */
650 req.wValue = usbhsh_device_number(hpriv, udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700651 dev_dbg(dev, "create new address - %d\n", req.wValue);
652 }
653
654 /* set request */
655 usbhs_usbreq_set_val(priv, &req);
656
657 /*
658 * wait setup packet ACK
659 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700660 wait_for_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700661
662 dev_dbg(dev, "%s done\n", __func__);
663}
664
665/*
666 * DCP data stage
667 */
668static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
669 struct usbhs_pkt *pkt)
670{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700671 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700672 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700673
674 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
675
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700676 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700677}
678
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700679static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
680 struct urb *urb,
681 struct usbhs_pipe *pipe,
682 gfp_t mem_flags)
683
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700684{
685 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700686
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700687 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
688 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
689 if (unlikely(!ureq))
690 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700691
692 if (usb_pipein(urb->pipe))
693 pipe->handler = &usbhs_dcp_data_stage_in_handler;
694 else
695 pipe->handler = &usbhs_dcp_data_stage_out_handler;
696
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700697 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700698 usbhsh_data_stage_packet_done,
699 urb->transfer_buffer,
700 urb->transfer_buffer_length,
701 (urb->transfer_flags & URB_ZERO_PACKET));
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700702
703 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700704}
705
706/*
707 * DCP status stage
708 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700709static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700710 struct urb *urb,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700711 struct usbhs_pipe *pipe,
712 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700713{
714 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700715
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700716 /* This ureq will be freed on usbhsh_queue_done() */
717 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
718 if (unlikely(!ureq))
719 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700720
721 if (usb_pipein(urb->pipe))
722 pipe->handler = &usbhs_dcp_status_stage_in_handler;
723 else
724 pipe->handler = &usbhs_dcp_status_stage_out_handler;
725
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700726 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700727 usbhsh_queue_done,
728 NULL,
729 urb->transfer_buffer_length,
730 0);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700731
732 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700733}
734
735static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700736 struct urb *urb,
737 gfp_t mflags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700738{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700739 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700740 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
741 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700742 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700743 int ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700744
745 dev_dbg(dev, "%s\n", __func__);
746
747 /*
748 * setup stage
749 *
750 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
751 */
752 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
753
754 /*
755 * data stage
756 *
757 * It is pushed only when urb has buffer.
758 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700759 if (urb->transfer_buffer_length) {
760 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
761 if (ret < 0) {
762 dev_err(dev, "data stage failed\n");
763 return ret;
764 }
765 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700766
767 /*
768 * status stage
769 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700770 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
771 if (ret < 0) {
772 dev_err(dev, "status stage failed\n");
773 return ret;
774 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700775
776 /*
777 * start pushed packets
778 */
779 usbhs_pkt_start(pipe);
780
781 return 0;
782}
783
784/*
785 * dma map functions
786 */
787static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
788{
789 return 0;
790}
791
792/*
793 * for hc_driver
794 */
795static int usbhsh_host_start(struct usb_hcd *hcd)
796{
797 return 0;
798}
799
800static void usbhsh_host_stop(struct usb_hcd *hcd)
801{
802}
803
804static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
805 struct urb *urb,
806 gfp_t mem_flags)
807{
808 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
809 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
810 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700811 struct usb_host_endpoint *ep = urb->ep;
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700812 struct usbhsh_device *new_udev = NULL;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700813 int is_dir_in = usb_pipein(urb->pipe);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700814
815 int ret;
816
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700817 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700818
819 ret = usb_hcd_link_urb_to_ep(hcd, urb);
820 if (ret)
821 goto usbhsh_urb_enqueue_error_not_linked;
822
823 /*
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700824 * attach udev if needed
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700825 */
Kuninori Morimotoc1e48772011-12-08 18:27:21 -0800826 if (!usbhsh_device_get(hpriv, urb)) {
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700827 new_udev = usbhsh_device_attach(hpriv, urb);
Kuninori Morimoto37332ee2011-12-08 18:25:37 -0800828 if (!new_udev) {
829 ret = -EIO;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700830 goto usbhsh_urb_enqueue_error_not_linked;
Kuninori Morimoto37332ee2011-12-08 18:25:37 -0800831 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700832 }
833
834 /*
Kuninori Morimoto48250932011-10-31 00:48:59 -0700835 * attach endpoint if needed
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700836 */
Kuninori Morimoto48250932011-10-31 00:48:59 -0700837 if (!usbhsh_ep_to_uep(ep)) {
838 ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
839 if (ret < 0)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700840 goto usbhsh_urb_enqueue_error_free_device;
841 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700842
843 /*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700844 * push packet
845 */
846 if (usb_pipecontrol(urb->pipe))
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700847 ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700848 else
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700849 ret = usbhsh_queue_push(hcd, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700850
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700851 return ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700852
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700853usbhsh_urb_enqueue_error_free_device:
854 if (new_udev)
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700855 usbhsh_device_detach(hpriv, new_udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700856usbhsh_urb_enqueue_error_not_linked:
857
858 dev_dbg(dev, "%s error\n", __func__);
859
860 return ret;
861}
862
863static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
864{
865 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
866 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
867
Kuninori Morimoto54796542011-12-08 18:26:07 -0800868 if (ureq) {
869 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
870 struct usbhs_pkt *pkt = &ureq->pkt;
871
872 usbhs_pkt_pop(pkt->pipe, pkt);
873 usbhsh_queue_done(priv, pkt);
874 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700875
876 return 0;
877}
878
879static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
880 struct usb_host_endpoint *ep)
881{
882 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
883 struct usbhsh_device *udev;
884 struct usbhsh_hpriv *hpriv;
885
886 /*
887 * this function might be called manytimes by same hcd/ep
Kuninori Morimotofca8ab72011-10-31 00:47:24 -0700888 * in-endpoint == out-endpoint if ep == dcp.
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700889 */
890 if (!uep)
891 return;
892
893 udev = usbhsh_uep_to_udev(uep);
894 hpriv = usbhsh_hcd_to_hpriv(hcd);
895
Kuninori Morimoto48250932011-10-31 00:48:59 -0700896 usbhsh_endpoint_detach(hpriv, ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700897
898 /*
899 * if there is no endpoint,
900 * free device
901 */
902 if (!usbhsh_device_has_endpoint(udev))
Kuninori Morimoto7aac8d152011-10-31 00:49:09 -0700903 usbhsh_device_detach(hpriv, udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700904}
905
906static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
907{
908 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
909 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
910 struct device *dev = usbhs_priv_to_dev(priv);
911 int roothub_id = 1; /* only 1 root hub */
912
913 /*
914 * does port stat was changed ?
915 * check USB_PORT_STAT_C_xxx << 16
916 */
917 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
918 *buf = (1 << roothub_id);
919 else
920 *buf = 0;
921
922 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
923
924 return !!(*buf);
925}
926
927static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
928 u16 typeReq, u16 wValue,
929 u16 wIndex, char *buf, u16 wLength)
930{
931 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
932 struct device *dev = usbhs_priv_to_dev(priv);
933
934 switch (wValue) {
935 case C_HUB_OVER_CURRENT:
936 case C_HUB_LOCAL_POWER:
937 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
938 return 0;
939 }
940
941 return -EPIPE;
942}
943
944static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
945 u16 typeReq, u16 wValue,
946 u16 wIndex, char *buf, u16 wLength)
947{
948 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
949 struct device *dev = usbhs_priv_to_dev(priv);
950 int enable = (typeReq == SetPortFeature);
951 int speed, i, timeout = 128;
952 int roothub_id = 1; /* only 1 root hub */
953
954 /* common error */
955 if (wIndex > roothub_id || wLength != 0)
956 return -EPIPE;
957
958 /* check wValue */
959 switch (wValue) {
960 case USB_PORT_FEAT_POWER:
961 usbhs_vbus_ctrl(priv, enable);
962 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
963 break;
964
965 case USB_PORT_FEAT_ENABLE:
966 case USB_PORT_FEAT_SUSPEND:
967 case USB_PORT_FEAT_C_ENABLE:
968 case USB_PORT_FEAT_C_SUSPEND:
969 case USB_PORT_FEAT_C_CONNECTION:
970 case USB_PORT_FEAT_C_OVER_CURRENT:
971 case USB_PORT_FEAT_C_RESET:
972 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
973 break;
974
975 case USB_PORT_FEAT_RESET:
976 if (!enable)
977 break;
978
979 usbhsh_port_stat_clear(hpriv,
980 USB_PORT_STAT_HIGH_SPEED |
981 USB_PORT_STAT_LOW_SPEED);
982
983 usbhs_bus_send_reset(priv);
984 msleep(20);
985 usbhs_bus_send_sof_enable(priv);
986
987 for (i = 0; i < timeout ; i++) {
988 switch (usbhs_bus_get_speed(priv)) {
989 case USB_SPEED_LOW:
990 speed = USB_PORT_STAT_LOW_SPEED;
991 goto got_usb_bus_speed;
992 case USB_SPEED_HIGH:
993 speed = USB_PORT_STAT_HIGH_SPEED;
994 goto got_usb_bus_speed;
995 case USB_SPEED_FULL:
996 speed = 0;
997 goto got_usb_bus_speed;
998 }
999
1000 msleep(20);
1001 }
1002 return -EPIPE;
1003
1004got_usb_bus_speed:
1005 usbhsh_port_stat_set(hpriv, speed);
1006 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
1007
1008 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
1009 __func__, speed);
1010
1011 /* status change is not needed */
1012 return 0;
1013
1014 default:
1015 return -EPIPE;
1016 }
1017
1018 /* set/clear status */
1019 if (enable)
1020 usbhsh_port_stat_set(hpriv, (1 << wValue));
1021 else
1022 usbhsh_port_stat_clear(hpriv, (1 << wValue));
1023
1024 return 0;
1025}
1026
1027static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
1028 u16 typeReq, u16 wValue,
1029 u16 wIndex, char *buf, u16 wLength)
1030{
1031 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1032 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
1033 struct device *dev = usbhs_priv_to_dev(priv);
1034 int roothub_id = 1; /* only 1 root hub */
1035
1036 switch (typeReq) {
1037 case GetHubStatus:
1038 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
1039
1040 *buf = 0x00;
1041 break;
1042
1043 case GetPortStatus:
1044 if (wIndex != roothub_id)
1045 return -EPIPE;
1046
1047 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
1048 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
1049 break;
1050
1051 case GetHubDescriptor:
1052 desc->bDescriptorType = 0x29;
1053 desc->bHubContrCurrent = 0;
1054 desc->bNbrPorts = roothub_id;
1055 desc->bDescLength = 9;
1056 desc->bPwrOn2PwrGood = 0;
1057 desc->wHubCharacteristics = cpu_to_le16(0x0011);
1058 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
1059 desc->u.hs.DeviceRemovable[1] = ~0;
1060 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
1061 break;
1062 }
1063
1064 return 0;
1065}
1066
1067static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1068 u16 wIndex, char *buf, u16 wLength)
1069{
1070 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1071 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1072 struct device *dev = usbhs_priv_to_dev(priv);
1073 int ret = -EPIPE;
1074
1075 switch (typeReq) {
1076
1077 /* Hub Feature */
1078 case ClearHubFeature:
1079 case SetHubFeature:
1080 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1081 wValue, wIndex, buf, wLength);
1082 break;
1083
1084 /* Port Feature */
1085 case SetPortFeature:
1086 case ClearPortFeature:
1087 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1088 wValue, wIndex, buf, wLength);
1089 break;
1090
1091 /* Get status */
1092 case GetHubStatus:
1093 case GetPortStatus:
1094 case GetHubDescriptor:
1095 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1096 wValue, wIndex, buf, wLength);
1097 break;
1098 }
1099
1100 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1101 typeReq, ret, usbhsh_port_stat_get(hpriv));
1102
1103 return ret;
1104}
1105
1106static struct hc_driver usbhsh_driver = {
1107 .description = usbhsh_hcd_name,
1108 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1109
1110 /*
1111 * generic hardware linkage
1112 */
1113 .flags = HCD_USB2,
1114
1115 .start = usbhsh_host_start,
1116 .stop = usbhsh_host_stop,
1117
1118 /*
1119 * managing i/o requests and associated device resources
1120 */
1121 .urb_enqueue = usbhsh_urb_enqueue,
1122 .urb_dequeue = usbhsh_urb_dequeue,
1123 .endpoint_disable = usbhsh_endpoint_disable,
1124
1125 /*
1126 * root hub
1127 */
1128 .hub_status_data = usbhsh_hub_status_data,
1129 .hub_control = usbhsh_hub_control,
1130};
1131
1132/*
1133 * interrupt functions
1134 */
1135static int usbhsh_irq_attch(struct usbhs_priv *priv,
1136 struct usbhs_irq_state *irq_state)
1137{
1138 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1139 struct device *dev = usbhs_priv_to_dev(priv);
1140
1141 dev_dbg(dev, "device attached\n");
1142
1143 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1144 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1145
1146 return 0;
1147}
1148
1149static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1150 struct usbhs_irq_state *irq_state)
1151{
1152 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1153 struct device *dev = usbhs_priv_to_dev(priv);
1154
1155 dev_dbg(dev, "device detached\n");
1156
1157 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1158 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1159
1160 return 0;
1161}
1162
1163static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1164 struct usbhs_irq_state *irq_state)
1165{
1166 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1167 struct device *dev = usbhs_priv_to_dev(priv);
1168
1169 dev_dbg(dev, "setup packet OK\n");
1170
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001171 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001172
1173 return 0;
1174}
1175
1176static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1177 struct usbhs_irq_state *irq_state)
1178{
1179 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1180 struct device *dev = usbhs_priv_to_dev(priv);
1181
1182 dev_dbg(dev, "setup packet Err\n");
1183
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001184 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001185
1186 return 0;
1187}
1188
1189/*
1190 * module start/stop
1191 */
1192static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1193{
1194 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1195 struct usbhsh_pipe_info *pipe_info = hpriv->pipe_info;
1196 struct usbhs_pipe *pipe;
1197 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1198 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1199 int old_type, dir_in, i;
1200
1201 /* init all pipe */
1202 old_type = USB_ENDPOINT_XFER_CONTROL;
1203 for (i = 0; i < pipe_size; i++) {
1204 pipe_info[i].usr_cnt = 0;
1205
1206 /*
1207 * data "output" will be finished as soon as possible,
1208 * but there is no guaranty at data "input" case.
1209 *
1210 * "input" needs "standby" pipe.
1211 * So, "input" direction pipe > "output" direction pipe
1212 * is good idea.
1213 *
1214 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1215 * and the other will be input direction here.
1216 *
1217 * ex)
1218 * ...
1219 * USB_ENDPOINT_XFER_ISOC -> dir out
1220 * USB_ENDPOINT_XFER_ISOC -> dir in
1221 * USB_ENDPOINT_XFER_BULK -> dir out
1222 * USB_ENDPOINT_XFER_BULK -> dir in
1223 * USB_ENDPOINT_XFER_BULK -> dir in
1224 * ...
1225 */
1226 dir_in = (pipe_type[i] == old_type);
1227 old_type = pipe_type[i];
1228
1229 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1230 pipe = usbhs_dcp_malloc(priv);
1231 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1232 } else {
1233 pipe = usbhs_pipe_malloc(priv,
1234 pipe_type[i],
1235 dir_in);
1236 }
1237
1238 pipe->mod_private = pipe_info + i;
1239 }
1240}
1241
1242static int usbhsh_start(struct usbhs_priv *priv)
1243{
1244 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1245 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1246 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1247 struct device *dev = usbhs_priv_to_dev(priv);
1248 int ret;
1249
1250 /* add hcd */
1251 ret = usb_add_hcd(hcd, 0, 0);
1252 if (ret < 0)
1253 return 0;
1254
1255 /*
1256 * pipe initialize and enable DCP
1257 */
1258 usbhs_pipe_init(priv,
1259 usbhsh_dma_map_ctrl);
1260 usbhs_fifo_init(priv);
1261 usbhsh_pipe_init_for_host(priv);
1262
1263 /*
1264 * system config enble
1265 * - HI speed
1266 * - host
1267 * - usb module
1268 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001269 usbhs_sys_host_ctrl(priv, 1);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001270
1271 /*
1272 * enable irq callback
1273 */
1274 mod->irq_attch = usbhsh_irq_attch;
1275 mod->irq_dtch = usbhsh_irq_dtch;
1276 mod->irq_sack = usbhsh_irq_setup_ack;
1277 mod->irq_sign = usbhsh_irq_setup_err;
1278 usbhs_irq_callback_update(priv, mod);
1279
1280 dev_dbg(dev, "start host\n");
1281
1282 return ret;
1283}
1284
1285static int usbhsh_stop(struct usbhs_priv *priv)
1286{
1287 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1288 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001289 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001290 struct device *dev = usbhs_priv_to_dev(priv);
1291
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001292 /*
1293 * disable irq callback
1294 */
1295 mod->irq_attch = NULL;
1296 mod->irq_dtch = NULL;
1297 mod->irq_sack = NULL;
1298 mod->irq_sign = NULL;
1299 usbhs_irq_callback_update(priv, mod);
1300
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001301 usb_remove_hcd(hcd);
1302
1303 /* disable sys */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001304 usbhs_sys_host_ctrl(priv, 0);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001305
1306 dev_dbg(dev, "quit host\n");
1307
1308 return 0;
1309}
1310
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001311int usbhs_mod_host_probe(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001312{
1313 struct usbhsh_hpriv *hpriv;
1314 struct usb_hcd *hcd;
1315 struct usbhsh_pipe_info *pipe_info;
1316 struct usbhsh_device *udev;
1317 struct device *dev = usbhs_priv_to_dev(priv);
1318 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1319 int i;
1320
1321 /* initialize hcd */
1322 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1323 if (!hcd) {
1324 dev_err(dev, "Failed to create hcd\n");
1325 return -ENOMEM;
1326 }
1327
1328 pipe_info = kzalloc(sizeof(*pipe_info) * pipe_size, GFP_KERNEL);
1329 if (!pipe_info) {
1330 dev_err(dev, "Could not allocate pipe_info\n");
1331 goto usbhs_mod_host_probe_err;
1332 }
1333
1334 /*
1335 * CAUTION
1336 *
1337 * There is no guarantee that it is possible to access usb module here.
1338 * Don't accesses to it.
1339 * The accesse will be enable after "usbhsh_start"
1340 */
1341
1342 hpriv = usbhsh_hcd_to_hpriv(hcd);
1343
1344 /*
1345 * register itself
1346 */
1347 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1348
1349 /* init hpriv */
1350 hpriv->mod.name = "host";
1351 hpriv->mod.start = usbhsh_start;
1352 hpriv->mod.stop = usbhsh_stop;
1353 hpriv->pipe_info = pipe_info;
1354 hpriv->pipe_size = pipe_size;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001355 usbhsh_port_stat_init(hpriv);
1356
1357 /* init all device */
1358 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1359 udev->usbv = NULL;
1360 INIT_LIST_HEAD(&udev->ep_list_head);
1361 }
1362
1363 dev_info(dev, "host probed\n");
1364
1365 return 0;
1366
1367usbhs_mod_host_probe_err:
1368 usb_put_hcd(hcd);
1369
1370 return -ENOMEM;
1371}
1372
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001373int usbhs_mod_host_remove(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001374{
1375 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1376 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1377
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001378 usb_put_hcd(hcd);
1379
1380 return 0;
1381}