blob: 0dbbc6613c1ff3b7664265c75c0322a41548be5f [file] [log] [blame]
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001/*
2 * Renesas USB driver
3 *
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 */
17#include <linux/io.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <linux/usb.h>
23#include <linux/usb/hcd.h>
24#include "common.h"
25
26/*
27 *** HARDWARE LIMITATION ***
28 *
29 * 1) renesas_usbhs has a limited number of controllable devices.
30 * it can control only 9 devices in generally.
31 * see DEVADDn / DCPMAXP / PIPEMAXP.
32 *
33 * 2) renesas_usbhs pipe number is limited.
34 * the pipe will be re-used for each devices.
35 * so, software should control DATA0/1 sequence of each devices.
36 */
37
38
39/*
40 * image of mod_host
41 *
42 * +--------+
43 * | udev 0 | --> it is used when set address
44 * +--------+
45 *
46 * +--------+ pipes are reused for each uep.
47 * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when
48 * +--------+ | | target device was changed
49 * +- [uep 1 (bulk)] --|---+ +--------------+
50 * | +--------------> | pipe0 (dcp) |
51 * +- [uep 2 (bulk)] --|---|---+ +--------------+
52 * | | | | pipe1 (isoc) |
53 * +--------+ | | | +--------------+
54 * | udev 2 |-+- [uep 0 (dcp) ] --+ +-- |------> | pipe2 (bulk) |
55 * +--------+ | | | | +--------------+
56 * +- [uep 1 (int) ] --|-+ | +------> | pipe3 (bulk) |
57 * | | | | +--------------+
58 * +--------+ | +-|---|------> | pipe4 (int) |
59 * | udev 3 |-+- [uep 0 (dcp) ] --+ | | +--------------+
60 * +--------+ | | | | .... |
61 * +- [uep 1 (bulk)] ------+ | | .... |
62 * | |
63 * +- [uep 2 (bulk)]-----------+
64 */
65
66
67/*
68 * struct
69 */
70struct usbhsh_pipe_info {
71 unsigned int usr_cnt; /* see usbhsh_endpoint_alloc() */
72};
73
74struct usbhsh_request {
75 struct urb *urb;
76 struct usbhs_pkt pkt;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -070077};
78
79struct usbhsh_device {
80 struct usb_device *usbv;
81 struct list_head ep_list_head; /* list of usbhsh_ep */
82};
83
84struct usbhsh_ep {
85 struct usbhs_pipe *pipe;
86 struct usbhsh_device *udev; /* attached udev */
87 struct list_head ep_list; /* list to usbhsh_device */
88
89 int maxp;
90};
91
92#define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */
93#define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */
94struct usbhsh_hpriv {
95 struct usbhs_mod mod;
96 struct usbhs_pipe *dcp;
97
98 struct usbhsh_device udev[USBHSH_DEVICE_MAX];
99
100 struct usbhsh_pipe_info *pipe_info;
101 int pipe_size;
102
103 u32 port_stat; /* USB_PORT_STAT_xxx */
104
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700105 struct completion setup_ack_done;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700106};
107
108
109static const char usbhsh_hcd_name[] = "renesas_usbhs host";
110
111/*
112 * macro
113 */
114#define usbhsh_priv_to_hpriv(priv) \
115 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod)
116
117#define __usbhsh_for_each_hpipe(start, pos, h, i) \
118 for (i = start, pos = (h)->hpipe + i; \
119 i < (h)->hpipe_size; \
120 i++, pos = (h)->hpipe + i)
121
122#define usbhsh_for_each_hpipe(pos, hpriv, i) \
123 __usbhsh_for_each_hpipe(1, pos, hpriv, i)
124
125#define usbhsh_for_each_hpipe_with_dcp(pos, hpriv, i) \
126 __usbhsh_for_each_hpipe(0, pos, hpriv, i)
127
128#define __usbhsh_for_each_udev(start, pos, h, i) \
129 for (i = start, pos = (h)->udev + i; \
130 i < USBHSH_DEVICE_MAX; \
131 i++, pos = (h)->udev + i)
132
133#define usbhsh_for_each_udev(pos, hpriv, i) \
134 __usbhsh_for_each_udev(1, pos, hpriv, i)
135
136#define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \
137 __usbhsh_for_each_udev(0, pos, hpriv, i)
138
139#define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv)
140#define usbhsh_hcd_to_dev(h) ((h)->self.controller)
141
142#define usbhsh_hpriv_to_priv(h) ((h)->mod.priv)
143#define usbhsh_hpriv_to_dcp(h) ((h)->dcp)
144#define usbhsh_hpriv_to_hcd(h) \
145 container_of((void *)h, struct usb_hcd, hcd_priv)
146
147#define usbhsh_ep_to_uep(u) ((u)->hcpriv)
148#define usbhsh_uep_to_pipe(u) ((u)->pipe)
149#define usbhsh_uep_to_udev(u) ((u)->udev)
150#define usbhsh_urb_to_ureq(u) ((u)->hcpriv)
151#define usbhsh_urb_to_usbv(u) ((u)->dev)
152
153#define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev)
154
155#define usbhsh_udev_to_usbv(h) ((h)->usbv)
Kuninori Morimotoab142302011-10-31 00:48:00 -0700156#define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700157
158#define usbhsh_pipe_info(p) ((p)->mod_private)
159
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700160#define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent))
161#define usbhsh_device_hubport(d) ((d)->usbv->portnum)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700162#define usbhsh_device_number(h, d) ((int)((d) - (h)->udev))
163#define usbhsh_device_nth(h, d) ((h)->udev + d)
164#define usbhsh_device0(h) usbhsh_device_nth(h, 0)
165
166#define usbhsh_port_stat_init(h) ((h)->port_stat = 0)
167#define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s))
168#define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s))
169#define usbhsh_port_stat_get(h) ((h)->port_stat)
170
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700171#define usbhsh_pkt_to_ureq(p) \
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700172 container_of((void *)p, struct usbhsh_request, pkt)
173
174/*
175 * req alloc/free
176 */
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700177static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700178 struct urb *urb,
179 gfp_t mem_flags)
180{
181 struct usbhsh_request *ureq;
182 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
183 struct device *dev = usbhs_priv_to_dev(priv);
184
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700185 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700186 if (!ureq) {
187 dev_err(dev, "ureq alloc fail\n");
188 return NULL;
189 }
190
191 usbhs_pkt_init(&ureq->pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700192 ureq->urb = urb;
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700193 usbhsh_urb_to_ureq(urb) = ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700194
195 return ureq;
196}
197
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700198static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700199 struct usbhsh_request *ureq)
200{
Kuninori Morimotofc9d5c72011-10-31 00:47:01 -0700201 usbhsh_urb_to_ureq(ureq->urb) = NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700202 ureq->urb = NULL;
Kuninori Morimotoc5b963f2011-10-31 00:47:44 -0700203
204 kfree(ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700205}
206
207/*
208 * device control
209 */
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700210static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd,
211 struct usbhsh_device *udev)
212{
213 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
214
215 return hcd->self.root_hub == usbv->parent;
216}
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700217
218static int usbhsh_device_has_endpoint(struct usbhsh_device *udev)
219{
220 return !list_empty(&udev->ep_list_head);
221}
222
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700223static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700224 struct urb *urb)
225{
226 struct usbhsh_device *udev = NULL;
227 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
228 struct device *dev = usbhsh_hcd_to_dev(hcd);
229 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
230 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700231 unsigned long flags;
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700232 u16 upphub, hubport;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700233 int i;
234
Kuninori Morimotod399f902011-10-31 00:48:35 -0700235 /******************** spin lock ********************/
236 usbhs_lock(priv, flags);
237
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700238 /*
Kuninori Morimotoab142302011-10-31 00:48:00 -0700239 * find device
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700240 */
241 if (0 == usb_pipedevice(urb->pipe)) {
Kuninori Morimotoab142302011-10-31 00:48:00 -0700242 /*
243 * device0 is special case
244 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700245 udev = usbhsh_device0(hpriv);
Kuninori Morimotoab142302011-10-31 00:48:00 -0700246 if (usbhsh_udev_is_used(udev))
247 udev = NULL;
248 } else {
249 struct usbhsh_device *pos;
250
251 /*
252 * find unused device
253 */
254 usbhsh_for_each_udev(pos, hpriv, i) {
255 if (usbhsh_udev_is_used(pos))
256 continue;
257 udev = pos;
258 break;
259 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700260 }
261
Kuninori Morimotod399f902011-10-31 00:48:35 -0700262 if (udev) {
263 /*
264 * usbhsh_usbv_to_udev()
265 * usbhsh_udev_to_usbv()
266 * will be enable
267 */
268 dev_set_drvdata(&usbv->dev, udev);
269 udev->usbv = usbv;
270 }
271
272 usbhs_unlock(priv, flags);
273 /******************** spin unlock ******************/
274
Kuninori Morimotoab142302011-10-31 00:48:00 -0700275 if (!udev) {
276 dev_err(dev, "no free usbhsh_device\n");
277 return NULL;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700278 }
279
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700280 if (usbhsh_device_has_endpoint(udev))
281 dev_warn(dev, "udev have old endpoint\n");
282
283 /* uep will be attached */
284 INIT_LIST_HEAD(&udev->ep_list_head);
285
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700286 upphub = 0;
287 hubport = 0;
288 if (!usbhsh_connected_to_rhdev(hcd, udev)) {
289 /* if udev is not connected to rhdev, it means parent is Hub */
290 struct usbhsh_device *parent = usbhsh_device_parent(udev);
291
292 upphub = usbhsh_device_number(hpriv, parent);
293 hubport = usbhsh_device_hubport(udev);
294
295 dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__,
296 upphub, hubport, parent);
297 }
298
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700299 /* set device config */
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700300 usbhs_set_device_config(priv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700301 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto9c673652011-10-31 00:47:34 -0700302 upphub, hubport, usbv->speed);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700303
304 dev_dbg(dev, "%s [%d](%p)\n", __func__,
305 usbhsh_device_number(hpriv, udev), udev);
306
307 return udev;
308}
309
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700310static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700311 struct usbhsh_device *udev)
312{
313 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700314 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700315 struct device *dev = usbhsh_hcd_to_dev(hcd);
316 struct usb_device *usbv = usbhsh_udev_to_usbv(udev);
Kuninori Morimotod399f902011-10-31 00:48:35 -0700317 unsigned long flags;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700318
319 dev_dbg(dev, "%s [%d](%p)\n", __func__,
320 usbhsh_device_number(hpriv, udev), udev);
321
322 if (usbhsh_device_has_endpoint(udev))
323 dev_warn(dev, "udev still have endpoint\n");
324
Kuninori Morimotod399f902011-10-31 00:48:35 -0700325 /******************** spin lock ********************/
326 usbhs_lock(priv, flags);
327
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700328 /*
329 * usbhsh_usbv_to_udev()
330 * usbhsh_udev_to_usbv()
331 * will be disable
332 */
333 dev_set_drvdata(&usbv->dev, NULL);
334 udev->usbv = NULL;
Kuninori Morimotod399f902011-10-31 00:48:35 -0700335
336 usbhs_unlock(priv, flags);
337 /******************** spin unlock ******************/
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700338}
339
340/*
341 * end-point control
342 */
Kuninori Morimoto48250932011-10-31 00:48:59 -0700343static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv,
344 struct urb *urb,
345 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700346{
347 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
Kuninori Morimoto48250932011-10-31 00:48:59 -0700348 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
349 struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv);
350 struct usb_host_endpoint *ep = urb->ep;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700351 struct usbhsh_ep *uep;
352 struct usbhsh_pipe_info *info;
Kuninori Morimotof3527412011-10-31 00:48:22 -0700353 struct usbhs_pipe *best_pipe = NULL;
354 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700355 struct usb_endpoint_descriptor *desc = &ep->desc;
Kuninori Morimotod399f902011-10-31 00:48:35 -0700356 unsigned long flags;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700357
358 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags);
359 if (!uep) {
360 dev_err(dev, "usbhsh_ep alloc fail\n");
Kuninori Morimoto48250932011-10-31 00:48:59 -0700361 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700362 }
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700363
Kuninori Morimotod399f902011-10-31 00:48:35 -0700364 /******************** spin lock ********************/
365 usbhs_lock(priv, flags);
366
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700367 /*
368 * find best pipe for endpoint
369 * see
370 * HARDWARE LIMITATION
371 */
Kuninori Morimotof3527412011-10-31 00:48:22 -0700372 if (usb_endpoint_xfer_control(desc)) {
373 /* best pipe is DCP */
374 best_pipe = usbhsh_hpriv_to_dcp(hpriv);
375 } else {
376 struct usbhs_pipe *pipe;
377 unsigned int min_usr = ~0;
Kuninori Morimoto48250932011-10-31 00:48:59 -0700378 int dir_in_req = !!usb_pipein(urb->pipe);
Kuninori Morimotof3527412011-10-31 00:48:22 -0700379 int i, dir_in;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700380
Kuninori Morimotof3527412011-10-31 00:48:22 -0700381 usbhs_for_each_pipe(pipe, priv, i) {
382 if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc)))
383 continue;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700384
Kuninori Morimotof3527412011-10-31 00:48:22 -0700385 dir_in = !!usbhs_pipe_is_dir_in(pipe);
386 if (0 != (dir_in - dir_in_req))
387 continue;
388
389 info = usbhsh_pipe_info(pipe);
390 if (min_usr > info->usr_cnt) {
391 min_usr = info->usr_cnt;
392 best_pipe = pipe;
393 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700394 }
395 }
396
Kuninori Morimotod399f902011-10-31 00:48:35 -0700397 if (best_pipe) {
398 /* update pipe user count */
399 info = usbhsh_pipe_info(best_pipe);
400 info->usr_cnt++;
401
402 /* init this endpoint, and attach it to udev */
403 INIT_LIST_HEAD(&uep->ep_list);
404 list_add_tail(&uep->ep_list, &udev->ep_list_head);
405 }
406
407 usbhs_unlock(priv, flags);
408 /******************** spin unlock ******************/
409
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700410 if (unlikely(!best_pipe)) {
411 dev_err(dev, "couldn't find best pipe\n");
412 kfree(uep);
Kuninori Morimoto48250932011-10-31 00:48:59 -0700413 return -EIO;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700414 }
Kuninori Morimotof3527412011-10-31 00:48:22 -0700415
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700416 /*
417 * init uep
418 */
419 uep->pipe = best_pipe;
420 uep->maxp = usb_endpoint_maxp(desc);
421 usbhsh_uep_to_udev(uep) = udev;
422 usbhsh_ep_to_uep(ep) = uep;
423
424 /*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700425 * usbhs_pipe_config_update() should be called after
Kuninori Morimoto3dd49262011-10-31 00:47:13 -0700426 * usbhs_set_device_config()
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700427 * see
428 * DCPMAXP/PIPEMAXP
429 */
Kuninori Morimotod7a00ec2011-10-24 02:25:28 -0700430 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700431 usbhs_pipe_config_update(uep->pipe,
432 usbhsh_device_number(hpriv, udev),
433 usb_endpoint_num(desc),
434 uep->maxp);
435
436 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
437 usbhsh_device_number(hpriv, udev),
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700438 usbhs_pipe_name(uep->pipe), uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700439
Kuninori Morimoto48250932011-10-31 00:48:59 -0700440 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700441}
442
Kuninori Morimoto48250932011-10-31 00:48:59 -0700443static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv,
444 struct usb_host_endpoint *ep)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700445{
446 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
447 struct device *dev = usbhs_priv_to_dev(priv);
448 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700449 struct usbhsh_pipe_info *info;
Kuninori Morimotod399f902011-10-31 00:48:35 -0700450 unsigned long flags;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700451
452 if (!uep)
453 return;
454
455 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__,
Kuninori Morimoto55b5a622011-10-17 18:04:37 -0700456 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)),
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700457 usbhs_pipe_name(uep->pipe), uep);
458
Kuninori Morimotod399f902011-10-31 00:48:35 -0700459 /******************** spin lock ********************/
460 usbhs_lock(priv, flags);
461
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700462 info = usbhsh_pipe_info(uep->pipe);
463 info->usr_cnt--;
464
465 /* remove this endpoint from udev */
466 list_del_init(&uep->ep_list);
467
468 usbhsh_uep_to_udev(uep) = NULL;
469 usbhsh_ep_to_uep(ep) = NULL;
470
Kuninori Morimotod399f902011-10-31 00:48:35 -0700471 usbhs_unlock(priv, flags);
472 /******************** spin unlock ******************/
473
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700474 kfree(uep);
475}
476
477/*
478 * queue push/pop
479 */
480static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
481{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700482 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700483 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
484 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
485 struct urb *urb = ureq->urb;
486 struct device *dev = usbhs_priv_to_dev(priv);
487
488 dev_dbg(dev, "%s\n", __func__);
489
490 if (!urb) {
491 dev_warn(dev, "pkt doesn't have urb\n");
492 return;
493 }
494
495 urb->actual_length = pkt->actual;
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700496 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700497
498 usb_hcd_unlink_urb_from_ep(hcd, urb);
499 usb_hcd_giveback_urb(hcd, urb, 0);
500}
501
502static int usbhsh_queue_push(struct usb_hcd *hcd,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700503 struct urb *urb,
504 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700505{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700506 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700507 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
508 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700509 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700510 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700511 void *buf;
512 int len;
513
514 if (usb_pipeisoc(urb->pipe)) {
515 dev_err(dev, "pipe iso is not supported now\n");
516 return -EIO;
517 }
518
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700519 /* this ureq will be freed on usbhsh_queue_done() */
520 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
521 if (unlikely(!ureq)) {
522 dev_err(dev, "ureq alloc fail\n");
523 return -ENOMEM;
524 }
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700525
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700526 if (usb_pipein(urb->pipe))
527 pipe->handler = &usbhs_fifo_pio_pop_handler;
528 else
529 pipe->handler = &usbhs_fifo_pio_push_handler;
530
531 buf = (void *)(urb->transfer_buffer + urb->actual_length);
532 len = urb->transfer_buffer_length - urb->actual_length;
533
534 dev_dbg(dev, "%s\n", __func__);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700535 usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700536 buf, len, (urb->transfer_flags & URB_ZERO_PACKET));
537 usbhs_pkt_start(pipe);
538
539 return 0;
540}
541
542/*
543 * DCP setup stage
544 */
545static int usbhsh_is_request_address(struct urb *urb)
546{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700547 struct usb_ctrlrequest *req;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700548
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700549 req = (struct usb_ctrlrequest *)urb->setup_packet;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700550
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700551 if ((DeviceOutRequest == req->bRequestType << 8) &&
552 (USB_REQ_SET_ADDRESS == req->bRequest))
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700553 return 1;
554 else
555 return 0;
556}
557
558static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv,
559 struct urb *urb,
560 struct usbhs_pipe *pipe)
561{
562 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
563 struct usb_ctrlrequest req;
564 struct device *dev = usbhs_priv_to_dev(priv);
565
566 /*
567 * wait setup packet ACK
568 * see
569 * usbhsh_irq_setup_ack()
570 * usbhsh_irq_setup_err()
571 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700572 init_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700573
574 /* copy original request */
575 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest));
576
577 /*
578 * renesas_usbhs can not use original usb address.
579 * see HARDWARE LIMITATION.
580 * modify usb address here.
581 */
582 if (usbhsh_is_request_address(urb)) {
583 /* FIXME */
584 req.wValue = 1;
585 dev_dbg(dev, "create new address - %d\n", req.wValue);
586 }
587
588 /* set request */
589 usbhs_usbreq_set_val(priv, &req);
590
591 /*
592 * wait setup packet ACK
593 */
Kuninori Morimoto7fccd482011-10-23 22:55:54 -0700594 wait_for_completion(&hpriv->setup_ack_done);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700595
596 dev_dbg(dev, "%s done\n", __func__);
597}
598
599/*
600 * DCP data stage
601 */
602static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv,
603 struct usbhs_pkt *pkt)
604{
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700605 struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700606 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700607
608 /* this ureq was connected to urb when usbhsh_urb_enqueue() */
609
Kuninori Morimoto25234b42011-10-23 19:56:53 -0700610 usbhsh_ureq_free(hpriv, ureq);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700611}
612
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700613static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv,
614 struct urb *urb,
615 struct usbhs_pipe *pipe,
616 gfp_t mem_flags)
617
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700618{
619 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700620
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700621 /* this ureq will be freed on usbhsh_data_stage_packet_done() */
622 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
623 if (unlikely(!ureq))
624 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700625
626 if (usb_pipein(urb->pipe))
627 pipe->handler = &usbhs_dcp_data_stage_in_handler;
628 else
629 pipe->handler = &usbhs_dcp_data_stage_out_handler;
630
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700631 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700632 usbhsh_data_stage_packet_done,
633 urb->transfer_buffer,
634 urb->transfer_buffer_length,
635 (urb->transfer_flags & URB_ZERO_PACKET));
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700636
637 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700638}
639
640/*
641 * DCP status stage
642 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700643static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700644 struct urb *urb,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700645 struct usbhs_pipe *pipe,
646 gfp_t mem_flags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700647{
648 struct usbhsh_request *ureq;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700649
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700650 /* This ureq will be freed on usbhsh_queue_done() */
651 ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags);
652 if (unlikely(!ureq))
653 return -ENOMEM;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700654
655 if (usb_pipein(urb->pipe))
656 pipe->handler = &usbhs_dcp_status_stage_in_handler;
657 else
658 pipe->handler = &usbhs_dcp_status_stage_out_handler;
659
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700660 usbhs_pkt_push(pipe, &ureq->pkt,
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700661 usbhsh_queue_done,
662 NULL,
663 urb->transfer_buffer_length,
664 0);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700665
666 return 0;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700667}
668
669static int usbhsh_dcp_queue_push(struct usb_hcd *hcd,
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700670 struct urb *urb,
671 gfp_t mflags)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700672{
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700673 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700674 struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep);
675 struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700676 struct device *dev = usbhsh_hcd_to_dev(hcd);
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700677 int ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700678
679 dev_dbg(dev, "%s\n", __func__);
680
681 /*
682 * setup stage
683 *
684 * usbhsh_send_setup_stage_packet() wait SACK/SIGN
685 */
686 usbhsh_setup_stage_packet_push(hpriv, urb, pipe);
687
688 /*
689 * data stage
690 *
691 * It is pushed only when urb has buffer.
692 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700693 if (urb->transfer_buffer_length) {
694 ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags);
695 if (ret < 0) {
696 dev_err(dev, "data stage failed\n");
697 return ret;
698 }
699 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700700
701 /*
702 * status stage
703 */
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700704 ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags);
705 if (ret < 0) {
706 dev_err(dev, "status stage failed\n");
707 return ret;
708 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700709
710 /*
711 * start pushed packets
712 */
713 usbhs_pkt_start(pipe);
714
715 return 0;
716}
717
718/*
719 * dma map functions
720 */
721static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
722{
723 return 0;
724}
725
726/*
727 * for hc_driver
728 */
729static int usbhsh_host_start(struct usb_hcd *hcd)
730{
731 return 0;
732}
733
734static void usbhsh_host_stop(struct usb_hcd *hcd)
735{
736}
737
738static int usbhsh_urb_enqueue(struct usb_hcd *hcd,
739 struct urb *urb,
740 gfp_t mem_flags)
741{
742 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
743 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
744 struct device *dev = usbhs_priv_to_dev(priv);
745 struct usb_device *usbv = usbhsh_urb_to_usbv(urb);
746 struct usb_host_endpoint *ep = urb->ep;
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700747 struct usbhsh_device *new_udev = NULL;
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700748 int is_dir_in = usb_pipein(urb->pipe);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700749
750 int ret;
751
Kuninori Morimoto73ef6352011-10-24 02:24:49 -0700752 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out");
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700753
754 ret = usb_hcd_link_urb_to_ep(hcd, urb);
755 if (ret)
756 goto usbhsh_urb_enqueue_error_not_linked;
757
758 /*
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700759 * attach udev if needed
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700760 */
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700761 if (!usbhsh_usbv_to_udev(usbv)) {
762 new_udev = usbhsh_device_attach(hpriv, urb);
Kuninori Morimoto37332ee2011-12-08 18:25:37 -0800763 if (!new_udev) {
764 ret = -EIO;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700765 goto usbhsh_urb_enqueue_error_not_linked;
Kuninori Morimoto37332ee2011-12-08 18:25:37 -0800766 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700767 }
768
769 /*
Kuninori Morimoto48250932011-10-31 00:48:59 -0700770 * attach endpoint if needed
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700771 */
Kuninori Morimoto48250932011-10-31 00:48:59 -0700772 if (!usbhsh_ep_to_uep(ep)) {
773 ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags);
774 if (ret < 0)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700775 goto usbhsh_urb_enqueue_error_free_device;
776 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700777
778 /*
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700779 * push packet
780 */
781 if (usb_pipecontrol(urb->pipe))
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700782 ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700783 else
Kuninori Morimoto3eddc9e2011-10-31 00:48:46 -0700784 ret = usbhsh_queue_push(hcd, urb, mem_flags);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700785
Kuninori Morimotoee8a0bf2011-10-31 00:46:50 -0700786 return ret;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700787
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700788usbhsh_urb_enqueue_error_free_device:
789 if (new_udev)
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700790 usbhsh_device_detach(hpriv, new_udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700791usbhsh_urb_enqueue_error_not_linked:
792
793 dev_dbg(dev, "%s error\n", __func__);
794
795 return ret;
796}
797
798static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
799{
800 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
801 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb);
802
Kuninori Morimoto54796542011-12-08 18:26:07 -0800803 if (ureq) {
804 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
805 struct usbhs_pkt *pkt = &ureq->pkt;
806
807 usbhs_pkt_pop(pkt->pipe, pkt);
808 usbhsh_queue_done(priv, pkt);
809 }
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700810
811 return 0;
812}
813
814static void usbhsh_endpoint_disable(struct usb_hcd *hcd,
815 struct usb_host_endpoint *ep)
816{
817 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep);
818 struct usbhsh_device *udev;
819 struct usbhsh_hpriv *hpriv;
820
821 /*
822 * this function might be called manytimes by same hcd/ep
Kuninori Morimotofca8ab72011-10-31 00:47:24 -0700823 * in-endpoint == out-endpoint if ep == dcp.
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700824 */
825 if (!uep)
826 return;
827
828 udev = usbhsh_uep_to_udev(uep);
829 hpriv = usbhsh_hcd_to_hpriv(hcd);
830
Kuninori Morimoto48250932011-10-31 00:48:59 -0700831 usbhsh_endpoint_detach(hpriv, ep);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700832
833 /*
834 * if there is no endpoint,
835 * free device
836 */
837 if (!usbhsh_device_has_endpoint(udev))
Kuninori Morimoto7aac8d12011-10-31 00:49:09 -0700838 usbhsh_device_detach(hpriv, udev);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -0700839}
840
841static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf)
842{
843 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
844 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
845 struct device *dev = usbhs_priv_to_dev(priv);
846 int roothub_id = 1; /* only 1 root hub */
847
848 /*
849 * does port stat was changed ?
850 * check USB_PORT_STAT_C_xxx << 16
851 */
852 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000)
853 *buf = (1 << roothub_id);
854 else
855 *buf = 0;
856
857 dev_dbg(dev, "%s (%02x)\n", __func__, *buf);
858
859 return !!(*buf);
860}
861
862static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv,
863 u16 typeReq, u16 wValue,
864 u16 wIndex, char *buf, u16 wLength)
865{
866 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
867 struct device *dev = usbhs_priv_to_dev(priv);
868
869 switch (wValue) {
870 case C_HUB_OVER_CURRENT:
871 case C_HUB_LOCAL_POWER:
872 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__);
873 return 0;
874 }
875
876 return -EPIPE;
877}
878
879static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv,
880 u16 typeReq, u16 wValue,
881 u16 wIndex, char *buf, u16 wLength)
882{
883 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
884 struct device *dev = usbhs_priv_to_dev(priv);
885 int enable = (typeReq == SetPortFeature);
886 int speed, i, timeout = 128;
887 int roothub_id = 1; /* only 1 root hub */
888
889 /* common error */
890 if (wIndex > roothub_id || wLength != 0)
891 return -EPIPE;
892
893 /* check wValue */
894 switch (wValue) {
895 case USB_PORT_FEAT_POWER:
896 usbhs_vbus_ctrl(priv, enable);
897 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__);
898 break;
899
900 case USB_PORT_FEAT_ENABLE:
901 case USB_PORT_FEAT_SUSPEND:
902 case USB_PORT_FEAT_C_ENABLE:
903 case USB_PORT_FEAT_C_SUSPEND:
904 case USB_PORT_FEAT_C_CONNECTION:
905 case USB_PORT_FEAT_C_OVER_CURRENT:
906 case USB_PORT_FEAT_C_RESET:
907 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__);
908 break;
909
910 case USB_PORT_FEAT_RESET:
911 if (!enable)
912 break;
913
914 usbhsh_port_stat_clear(hpriv,
915 USB_PORT_STAT_HIGH_SPEED |
916 USB_PORT_STAT_LOW_SPEED);
917
918 usbhs_bus_send_reset(priv);
919 msleep(20);
920 usbhs_bus_send_sof_enable(priv);
921
922 for (i = 0; i < timeout ; i++) {
923 switch (usbhs_bus_get_speed(priv)) {
924 case USB_SPEED_LOW:
925 speed = USB_PORT_STAT_LOW_SPEED;
926 goto got_usb_bus_speed;
927 case USB_SPEED_HIGH:
928 speed = USB_PORT_STAT_HIGH_SPEED;
929 goto got_usb_bus_speed;
930 case USB_SPEED_FULL:
931 speed = 0;
932 goto got_usb_bus_speed;
933 }
934
935 msleep(20);
936 }
937 return -EPIPE;
938
939got_usb_bus_speed:
940 usbhsh_port_stat_set(hpriv, speed);
941 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE);
942
943 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n",
944 __func__, speed);
945
946 /* status change is not needed */
947 return 0;
948
949 default:
950 return -EPIPE;
951 }
952
953 /* set/clear status */
954 if (enable)
955 usbhsh_port_stat_set(hpriv, (1 << wValue));
956 else
957 usbhsh_port_stat_clear(hpriv, (1 << wValue));
958
959 return 0;
960}
961
962static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv,
963 u16 typeReq, u16 wValue,
964 u16 wIndex, char *buf, u16 wLength)
965{
966 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
967 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
968 struct device *dev = usbhs_priv_to_dev(priv);
969 int roothub_id = 1; /* only 1 root hub */
970
971 switch (typeReq) {
972 case GetHubStatus:
973 dev_dbg(dev, "%s :: GetHubStatus\n", __func__);
974
975 *buf = 0x00;
976 break;
977
978 case GetPortStatus:
979 if (wIndex != roothub_id)
980 return -EPIPE;
981
982 dev_dbg(dev, "%s :: GetPortStatus\n", __func__);
983 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv));
984 break;
985
986 case GetHubDescriptor:
987 desc->bDescriptorType = 0x29;
988 desc->bHubContrCurrent = 0;
989 desc->bNbrPorts = roothub_id;
990 desc->bDescLength = 9;
991 desc->bPwrOn2PwrGood = 0;
992 desc->wHubCharacteristics = cpu_to_le16(0x0011);
993 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1);
994 desc->u.hs.DeviceRemovable[1] = ~0;
995 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__);
996 break;
997 }
998
999 return 0;
1000}
1001
1002static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1003 u16 wIndex, char *buf, u16 wLength)
1004{
1005 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd);
1006 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv);
1007 struct device *dev = usbhs_priv_to_dev(priv);
1008 int ret = -EPIPE;
1009
1010 switch (typeReq) {
1011
1012 /* Hub Feature */
1013 case ClearHubFeature:
1014 case SetHubFeature:
1015 ret = __usbhsh_hub_hub_feature(hpriv, typeReq,
1016 wValue, wIndex, buf, wLength);
1017 break;
1018
1019 /* Port Feature */
1020 case SetPortFeature:
1021 case ClearPortFeature:
1022 ret = __usbhsh_hub_port_feature(hpriv, typeReq,
1023 wValue, wIndex, buf, wLength);
1024 break;
1025
1026 /* Get status */
1027 case GetHubStatus:
1028 case GetPortStatus:
1029 case GetHubDescriptor:
1030 ret = __usbhsh_hub_get_status(hpriv, typeReq,
1031 wValue, wIndex, buf, wLength);
1032 break;
1033 }
1034
1035 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n",
1036 typeReq, ret, usbhsh_port_stat_get(hpriv));
1037
1038 return ret;
1039}
1040
1041static struct hc_driver usbhsh_driver = {
1042 .description = usbhsh_hcd_name,
1043 .hcd_priv_size = sizeof(struct usbhsh_hpriv),
1044
1045 /*
1046 * generic hardware linkage
1047 */
1048 .flags = HCD_USB2,
1049
1050 .start = usbhsh_host_start,
1051 .stop = usbhsh_host_stop,
1052
1053 /*
1054 * managing i/o requests and associated device resources
1055 */
1056 .urb_enqueue = usbhsh_urb_enqueue,
1057 .urb_dequeue = usbhsh_urb_dequeue,
1058 .endpoint_disable = usbhsh_endpoint_disable,
1059
1060 /*
1061 * root hub
1062 */
1063 .hub_status_data = usbhsh_hub_status_data,
1064 .hub_control = usbhsh_hub_control,
1065};
1066
1067/*
1068 * interrupt functions
1069 */
1070static int usbhsh_irq_attch(struct usbhs_priv *priv,
1071 struct usbhs_irq_state *irq_state)
1072{
1073 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1074 struct device *dev = usbhs_priv_to_dev(priv);
1075
1076 dev_dbg(dev, "device attached\n");
1077
1078 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION);
1079 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1080
1081 return 0;
1082}
1083
1084static int usbhsh_irq_dtch(struct usbhs_priv *priv,
1085 struct usbhs_irq_state *irq_state)
1086{
1087 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1088 struct device *dev = usbhs_priv_to_dev(priv);
1089
1090 dev_dbg(dev, "device detached\n");
1091
1092 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION);
1093 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16);
1094
1095 return 0;
1096}
1097
1098static int usbhsh_irq_setup_ack(struct usbhs_priv *priv,
1099 struct usbhs_irq_state *irq_state)
1100{
1101 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1102 struct device *dev = usbhs_priv_to_dev(priv);
1103
1104 dev_dbg(dev, "setup packet OK\n");
1105
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001106 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001107
1108 return 0;
1109}
1110
1111static int usbhsh_irq_setup_err(struct usbhs_priv *priv,
1112 struct usbhs_irq_state *irq_state)
1113{
1114 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1115 struct device *dev = usbhs_priv_to_dev(priv);
1116
1117 dev_dbg(dev, "setup packet Err\n");
1118
Kuninori Morimoto7fccd482011-10-23 22:55:54 -07001119 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001120
1121 return 0;
1122}
1123
1124/*
1125 * module start/stop
1126 */
1127static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv)
1128{
1129 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1130 struct usbhsh_pipe_info *pipe_info = hpriv->pipe_info;
1131 struct usbhs_pipe *pipe;
1132 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
1133 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1134 int old_type, dir_in, i;
1135
1136 /* init all pipe */
1137 old_type = USB_ENDPOINT_XFER_CONTROL;
1138 for (i = 0; i < pipe_size; i++) {
1139 pipe_info[i].usr_cnt = 0;
1140
1141 /*
1142 * data "output" will be finished as soon as possible,
1143 * but there is no guaranty at data "input" case.
1144 *
1145 * "input" needs "standby" pipe.
1146 * So, "input" direction pipe > "output" direction pipe
1147 * is good idea.
1148 *
1149 * 1st USB_ENDPOINT_XFER_xxx will be output direction,
1150 * and the other will be input direction here.
1151 *
1152 * ex)
1153 * ...
1154 * USB_ENDPOINT_XFER_ISOC -> dir out
1155 * USB_ENDPOINT_XFER_ISOC -> dir in
1156 * USB_ENDPOINT_XFER_BULK -> dir out
1157 * USB_ENDPOINT_XFER_BULK -> dir in
1158 * USB_ENDPOINT_XFER_BULK -> dir in
1159 * ...
1160 */
1161 dir_in = (pipe_type[i] == old_type);
1162 old_type = pipe_type[i];
1163
1164 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) {
1165 pipe = usbhs_dcp_malloc(priv);
1166 usbhsh_hpriv_to_dcp(hpriv) = pipe;
1167 } else {
1168 pipe = usbhs_pipe_malloc(priv,
1169 pipe_type[i],
1170 dir_in);
1171 }
1172
1173 pipe->mod_private = pipe_info + i;
1174 }
1175}
1176
1177static int usbhsh_start(struct usbhs_priv *priv)
1178{
1179 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1180 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1181 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1182 struct device *dev = usbhs_priv_to_dev(priv);
1183 int ret;
1184
1185 /* add hcd */
1186 ret = usb_add_hcd(hcd, 0, 0);
1187 if (ret < 0)
1188 return 0;
1189
1190 /*
1191 * pipe initialize and enable DCP
1192 */
1193 usbhs_pipe_init(priv,
1194 usbhsh_dma_map_ctrl);
1195 usbhs_fifo_init(priv);
1196 usbhsh_pipe_init_for_host(priv);
1197
1198 /*
1199 * system config enble
1200 * - HI speed
1201 * - host
1202 * - usb module
1203 */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001204 usbhs_sys_host_ctrl(priv, 1);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001205
1206 /*
1207 * enable irq callback
1208 */
1209 mod->irq_attch = usbhsh_irq_attch;
1210 mod->irq_dtch = usbhsh_irq_dtch;
1211 mod->irq_sack = usbhsh_irq_setup_ack;
1212 mod->irq_sign = usbhsh_irq_setup_err;
1213 usbhs_irq_callback_update(priv, mod);
1214
1215 dev_dbg(dev, "start host\n");
1216
1217 return ret;
1218}
1219
1220static int usbhsh_stop(struct usbhs_priv *priv)
1221{
1222 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1223 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001224 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001225 struct device *dev = usbhs_priv_to_dev(priv);
1226
Kuninori Morimoto146ee502011-10-24 02:25:07 -07001227 /*
1228 * disable irq callback
1229 */
1230 mod->irq_attch = NULL;
1231 mod->irq_dtch = NULL;
1232 mod->irq_sack = NULL;
1233 mod->irq_sign = NULL;
1234 usbhs_irq_callback_update(priv, mod);
1235
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001236 usb_remove_hcd(hcd);
1237
1238 /* disable sys */
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001239 usbhs_sys_host_ctrl(priv, 0);
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001240
1241 dev_dbg(dev, "quit host\n");
1242
1243 return 0;
1244}
1245
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001246int usbhs_mod_host_probe(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001247{
1248 struct usbhsh_hpriv *hpriv;
1249 struct usb_hcd *hcd;
1250 struct usbhsh_pipe_info *pipe_info;
1251 struct usbhsh_device *udev;
1252 struct device *dev = usbhs_priv_to_dev(priv);
1253 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1254 int i;
1255
1256 /* initialize hcd */
1257 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name);
1258 if (!hcd) {
1259 dev_err(dev, "Failed to create hcd\n");
1260 return -ENOMEM;
1261 }
1262
1263 pipe_info = kzalloc(sizeof(*pipe_info) * pipe_size, GFP_KERNEL);
1264 if (!pipe_info) {
1265 dev_err(dev, "Could not allocate pipe_info\n");
1266 goto usbhs_mod_host_probe_err;
1267 }
1268
1269 /*
1270 * CAUTION
1271 *
1272 * There is no guarantee that it is possible to access usb module here.
1273 * Don't accesses to it.
1274 * The accesse will be enable after "usbhsh_start"
1275 */
1276
1277 hpriv = usbhsh_hcd_to_hpriv(hcd);
1278
1279 /*
1280 * register itself
1281 */
1282 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST);
1283
1284 /* init hpriv */
1285 hpriv->mod.name = "host";
1286 hpriv->mod.start = usbhsh_start;
1287 hpriv->mod.stop = usbhsh_stop;
1288 hpriv->pipe_info = pipe_info;
1289 hpriv->pipe_size = pipe_size;
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001290 usbhsh_port_stat_init(hpriv);
1291
1292 /* init all device */
1293 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) {
1294 udev->usbv = NULL;
1295 INIT_LIST_HEAD(&udev->ep_list_head);
1296 }
1297
1298 dev_info(dev, "host probed\n");
1299
1300 return 0;
1301
1302usbhs_mod_host_probe_err:
1303 usb_put_hcd(hcd);
1304
1305 return -ENOMEM;
1306}
1307
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001308int usbhs_mod_host_remove(struct usbhs_priv *priv)
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001309{
1310 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv);
1311 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv);
1312
Kuninori Morimoto034d7c12011-10-10 22:07:40 -07001313 usb_put_hcd(hcd);
1314
1315 return 0;
1316}