]>
Commit | Line | Data |
---|---|---|
2f98382d KM |
1 | /* |
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/module.h> | |
19 | #include <linux/platform_device.h> | |
20 | #include <linux/usb/ch9.h> | |
21 | #include <linux/usb/gadget.h> | |
22 | #include "common.h" | |
23 | ||
24 | /* | |
25 | * struct | |
26 | */ | |
27 | struct usbhsg_request { | |
28 | struct usb_request req; | |
4bd04811 | 29 | struct usbhs_pkt pkt; |
2f98382d KM |
30 | }; |
31 | ||
32 | #define EP_NAME_SIZE 8 | |
33 | struct usbhsg_gpriv; | |
2f98382d KM |
34 | struct usbhsg_uep { |
35 | struct usb_ep ep; | |
36 | struct usbhs_pipe *pipe; | |
2f98382d KM |
37 | |
38 | char ep_name[EP_NAME_SIZE]; | |
39 | ||
40 | struct usbhsg_gpriv *gpriv; | |
dad67397 | 41 | struct usbhs_pkt_handle *handler; |
2f98382d KM |
42 | }; |
43 | ||
44 | struct usbhsg_gpriv { | |
45 | struct usb_gadget gadget; | |
46 | struct usbhs_mod mod; | |
47 | ||
48 | struct usbhsg_uep *uep; | |
49 | int uep_size; | |
50 | ||
51 | struct usb_gadget_driver *driver; | |
52 | ||
53 | u32 status; | |
54 | #define USBHSG_STATUS_STARTED (1 << 0) | |
55 | #define USBHSG_STATUS_REGISTERD (1 << 1) | |
56 | #define USBHSG_STATUS_WEDGE (1 << 2) | |
57 | }; | |
58 | ||
2f98382d KM |
59 | struct usbhsg_recip_handle { |
60 | char *name; | |
61 | int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep, | |
62 | struct usb_ctrlrequest *ctrl); | |
63 | int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep, | |
64 | struct usb_ctrlrequest *ctrl); | |
65 | int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep, | |
66 | struct usb_ctrlrequest *ctrl); | |
67 | }; | |
68 | ||
69 | /* | |
70 | * macro | |
71 | */ | |
72 | #define usbhsg_priv_to_gpriv(priv) \ | |
73 | container_of( \ | |
74 | usbhs_mod_get(priv, USBHS_GADGET), \ | |
75 | struct usbhsg_gpriv, mod) | |
76 | ||
77 | #define __usbhsg_for_each_uep(start, pos, g, i) \ | |
78 | for (i = start, pos = (g)->uep; \ | |
79 | i < (g)->uep_size; \ | |
80 | i++, pos = (g)->uep + i) | |
81 | ||
82 | #define usbhsg_for_each_uep(pos, gpriv, i) \ | |
83 | __usbhsg_for_each_uep(1, pos, gpriv, i) | |
84 | ||
85 | #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \ | |
86 | __usbhsg_for_each_uep(0, pos, gpriv, i) | |
87 | ||
88 | #define usbhsg_gadget_to_gpriv(g)\ | |
89 | container_of(g, struct usbhsg_gpriv, gadget) | |
90 | ||
91 | #define usbhsg_req_to_ureq(r)\ | |
92 | container_of(r, struct usbhsg_request, req) | |
93 | ||
94 | #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep) | |
2f98382d KM |
95 | #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv) |
96 | #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv) | |
97 | #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep) | |
98 | #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i) | |
99 | #define usbhsg_uep_to_gpriv(u) ((u)->gpriv) | |
100 | #define usbhsg_uep_to_pipe(u) ((u)->pipe) | |
101 | #define usbhsg_pipe_to_uep(p) ((p)->mod_private) | |
102 | #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv)) | |
103 | ||
4bd04811 KM |
104 | #define usbhsg_ureq_to_pkt(u) (&(u)->pkt) |
105 | #define usbhsg_pkt_to_ureq(i) \ | |
106 | container_of(i, struct usbhsg_request, pkt) | |
107 | ||
2f98382d KM |
108 | #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN) |
109 | ||
110 | /* status */ | |
111 | #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0) | |
112 | #define usbhsg_status_set(gp, b) (gp->status |= b) | |
113 | #define usbhsg_status_clr(gp, b) (gp->status &= ~b) | |
114 | #define usbhsg_status_has(gp, b) (gp->status & b) | |
115 | ||
116 | /* | |
117 | * list push/pop | |
118 | */ | |
119 | static void usbhsg_queue_push(struct usbhsg_uep *uep, | |
120 | struct usbhsg_request *ureq) | |
121 | { | |
122 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
123 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
124 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
6acb95d4 | 125 | struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq); |
659d4954 | 126 | struct usb_request *req = &ureq->req; |
2f98382d | 127 | |
659d4954 KM |
128 | req->actual = 0; |
129 | req->status = -EINPROGRESS; | |
0432eed0 KM |
130 | usbhs_pkt_push(pipe, pkt, uep->handler, |
131 | req->buf, req->length, req->zero); | |
2f98382d KM |
132 | |
133 | dev_dbg(dev, "pipe %d : queue push (%d)\n", | |
134 | usbhs_pipe_number(pipe), | |
659d4954 | 135 | req->length); |
2f98382d KM |
136 | } |
137 | ||
2f98382d KM |
138 | static void usbhsg_queue_pop(struct usbhsg_uep *uep, |
139 | struct usbhsg_request *ureq, | |
140 | int status) | |
141 | { | |
142 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
143 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
144 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
2f98382d KM |
145 | |
146 | dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe)); | |
147 | ||
2f98382d KM |
148 | ureq->req.status = status; |
149 | ureq->req.complete(&uep->ep, &ureq->req); | |
2f98382d KM |
150 | } |
151 | ||
dad67397 | 152 | static void usbhsg_queue_done(struct usbhs_pkt *pkt) |
2f98382d | 153 | { |
4bd04811 KM |
154 | struct usbhs_pipe *pipe = pkt->pipe; |
155 | struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe); | |
156 | struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt); | |
2f98382d | 157 | |
659d4954 | 158 | ureq->req.actual = pkt->actual; |
2f98382d | 159 | |
659d4954 | 160 | usbhsg_queue_pop(uep, ureq, 0); |
4bd04811 KM |
161 | } |
162 | ||
2f98382d KM |
163 | /* |
164 | * USB_TYPE_STANDARD / clear feature functions | |
165 | */ | |
166 | static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv, | |
167 | struct usbhsg_uep *uep, | |
168 | struct usb_ctrlrequest *ctrl) | |
169 | { | |
170 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
171 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); | |
172 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); | |
173 | ||
174 | usbhs_dcp_control_transfer_done(pipe); | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
179 | static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv, | |
180 | struct usbhsg_uep *uep, | |
181 | struct usb_ctrlrequest *ctrl) | |
182 | { | |
183 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
184 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
185 | ||
186 | if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) { | |
e8d548d5 | 187 | usbhs_pipe_disable(pipe); |
2f98382d | 188 | usbhs_pipe_clear_sequence(pipe); |
e8d548d5 | 189 | usbhs_pipe_enable(pipe); |
2f98382d KM |
190 | } |
191 | ||
192 | usbhsg_recip_handler_std_control_done(priv, uep, ctrl); | |
193 | ||
2f98382d KM |
194 | return 0; |
195 | } | |
196 | ||
197 | struct usbhsg_recip_handle req_clear_feature = { | |
198 | .name = "clear feature", | |
199 | .device = usbhsg_recip_handler_std_control_done, | |
200 | .interface = usbhsg_recip_handler_std_control_done, | |
201 | .endpoint = usbhsg_recip_handler_std_clear_endpoint, | |
202 | }; | |
203 | ||
204 | /* | |
205 | * USB_TYPE handler | |
206 | */ | |
207 | static int usbhsg_recip_run_handle(struct usbhs_priv *priv, | |
208 | struct usbhsg_recip_handle *handler, | |
209 | struct usb_ctrlrequest *ctrl) | |
210 | { | |
211 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
212 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
213 | struct usbhsg_uep *uep; | |
0432eed0 | 214 | struct usbhs_pipe *pipe; |
2f98382d KM |
215 | int recip = ctrl->bRequestType & USB_RECIP_MASK; |
216 | int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; | |
217 | int ret; | |
218 | int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep, | |
219 | struct usb_ctrlrequest *ctrl); | |
220 | char *msg; | |
221 | ||
222 | uep = usbhsg_gpriv_to_nth_uep(gpriv, nth); | |
0432eed0 KM |
223 | pipe = usbhsg_uep_to_pipe(uep); |
224 | if (!pipe) { | |
9a28b7bd | 225 | dev_err(dev, "wrong recip request\n"); |
97664a20 KM |
226 | ret = -EINVAL; |
227 | goto usbhsg_recip_run_handle_end; | |
9a28b7bd | 228 | } |
2f98382d KM |
229 | |
230 | switch (recip) { | |
231 | case USB_RECIP_DEVICE: | |
232 | msg = "DEVICE"; | |
233 | func = handler->device; | |
234 | break; | |
235 | case USB_RECIP_INTERFACE: | |
236 | msg = "INTERFACE"; | |
237 | func = handler->interface; | |
238 | break; | |
239 | case USB_RECIP_ENDPOINT: | |
240 | msg = "ENDPOINT"; | |
241 | func = handler->endpoint; | |
242 | break; | |
243 | default: | |
244 | dev_warn(dev, "unsupported RECIP(%d)\n", recip); | |
245 | func = NULL; | |
246 | ret = -EINVAL; | |
247 | } | |
248 | ||
249 | if (func) { | |
97664a20 KM |
250 | unsigned long flags; |
251 | ||
2f98382d | 252 | dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg); |
97664a20 KM |
253 | |
254 | /******************** spin lock ********************/ | |
255 | usbhs_lock(priv, flags); | |
2f98382d | 256 | ret = func(priv, uep, ctrl); |
97664a20 KM |
257 | usbhs_unlock(priv, flags); |
258 | /******************** spin unlock ******************/ | |
2f98382d KM |
259 | } |
260 | ||
97664a20 | 261 | usbhsg_recip_run_handle_end: |
0432eed0 | 262 | usbhs_pkt_start(pipe); |
97664a20 | 263 | |
2f98382d KM |
264 | return ret; |
265 | } | |
266 | ||
267 | /* | |
268 | * irq functions | |
269 | * | |
270 | * it will be called from usbhs_interrupt | |
271 | */ | |
272 | static int usbhsg_irq_dev_state(struct usbhs_priv *priv, | |
273 | struct usbhs_irq_state *irq_state) | |
274 | { | |
275 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
276 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
277 | ||
278 | gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state); | |
279 | ||
280 | dev_dbg(dev, "state = %x : speed : %d\n", | |
281 | usbhs_status_get_device_state(irq_state), | |
282 | gpriv->gadget.speed); | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv, | |
288 | struct usbhs_irq_state *irq_state) | |
289 | { | |
290 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
291 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); | |
292 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); | |
293 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
294 | struct usb_ctrlrequest ctrl; | |
295 | struct usbhsg_recip_handle *recip_handler = NULL; | |
296 | int stage = usbhs_status_get_ctrl_stage(irq_state); | |
297 | int ret = 0; | |
298 | ||
299 | dev_dbg(dev, "stage = %d\n", stage); | |
300 | ||
301 | /* | |
302 | * see Manual | |
303 | * | |
304 | * "Operation" | |
305 | * - "Interrupt Function" | |
306 | * - "Control Transfer Stage Transition Interrupt" | |
307 | * - Fig. "Control Transfer Stage Transitions" | |
308 | */ | |
309 | ||
310 | switch (stage) { | |
311 | case READ_DATA_STAGE: | |
0cb7e61d | 312 | dcp->handler = &usbhs_fifo_pio_push_handler; |
2f98382d KM |
313 | break; |
314 | case WRITE_DATA_STAGE: | |
0cb7e61d | 315 | dcp->handler = &usbhs_fifo_pio_pop_handler; |
2f98382d KM |
316 | break; |
317 | case NODATA_STATUS_STAGE: | |
dad67397 | 318 | dcp->handler = &usbhs_ctrl_stage_end_handler; |
2f98382d KM |
319 | break; |
320 | default: | |
321 | return ret; | |
322 | } | |
323 | ||
324 | /* | |
325 | * get usb request | |
326 | */ | |
327 | usbhs_usbreq_get_val(priv, &ctrl); | |
328 | ||
329 | switch (ctrl.bRequestType & USB_TYPE_MASK) { | |
330 | case USB_TYPE_STANDARD: | |
331 | switch (ctrl.bRequest) { | |
332 | case USB_REQ_CLEAR_FEATURE: | |
333 | recip_handler = &req_clear_feature; | |
334 | break; | |
335 | } | |
336 | } | |
337 | ||
338 | /* | |
339 | * setup stage / run recip | |
340 | */ | |
341 | if (recip_handler) | |
342 | ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl); | |
343 | else | |
344 | ret = gpriv->driver->setup(&gpriv->gadget, &ctrl); | |
345 | ||
346 | if (ret < 0) | |
e8d548d5 | 347 | usbhs_pipe_stall(pipe); |
2f98382d KM |
348 | |
349 | return ret; | |
350 | } | |
351 | ||
2f98382d KM |
352 | /* |
353 | * | |
354 | * usb_dcp_ops | |
355 | * | |
356 | */ | |
2f98382d KM |
357 | static int usbhsg_pipe_disable(struct usbhsg_uep *uep) |
358 | { | |
359 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
8a2c225d | 360 | struct usbhs_pkt *pkt; |
2f98382d | 361 | |
e8d548d5 | 362 | usbhs_pipe_disable(pipe); |
2f98382d | 363 | |
2f98382d | 364 | while (1) { |
97664a20 | 365 | pkt = usbhs_pkt_pop(pipe, NULL); |
8a2c225d | 366 | if (!pkt) |
2f98382d | 367 | break; |
2f98382d KM |
368 | } |
369 | ||
2f98382d KM |
370 | return 0; |
371 | } | |
372 | ||
409ba9e7 KM |
373 | static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv) |
374 | { | |
375 | int i; | |
376 | struct usbhsg_uep *uep; | |
377 | ||
378 | usbhsg_for_each_uep_with_dcp(uep, gpriv, i) | |
379 | uep->pipe = NULL; | |
380 | } | |
381 | ||
2f98382d KM |
382 | /* |
383 | * | |
384 | * usb_ep_ops | |
385 | * | |
386 | */ | |
387 | static int usbhsg_ep_enable(struct usb_ep *ep, | |
388 | const struct usb_endpoint_descriptor *desc) | |
389 | { | |
390 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
391 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
392 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); | |
393 | struct usbhs_pipe *pipe; | |
2f98382d KM |
394 | int ret = -EIO; |
395 | ||
409ba9e7 KM |
396 | /* |
397 | * if it already have pipe, | |
398 | * nothing to do | |
399 | */ | |
400 | if (uep->pipe) | |
401 | return 0; | |
402 | ||
2f98382d KM |
403 | pipe = usbhs_pipe_malloc(priv, desc); |
404 | if (pipe) { | |
405 | uep->pipe = pipe; | |
406 | pipe->mod_private = uep; | |
2f98382d KM |
407 | |
408 | if (usb_endpoint_dir_in(desc)) | |
0cb7e61d | 409 | uep->handler = &usbhs_fifo_pio_push_handler; |
2f98382d | 410 | else |
0cb7e61d | 411 | uep->handler = &usbhs_fifo_pio_pop_handler; |
2f98382d KM |
412 | |
413 | ret = 0; | |
414 | } | |
cb96632c | 415 | |
2f98382d KM |
416 | return ret; |
417 | } | |
418 | ||
419 | static int usbhsg_ep_disable(struct usb_ep *ep) | |
420 | { | |
421 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
2f98382d | 422 | |
97664a20 | 423 | return usbhsg_pipe_disable(uep); |
2f98382d KM |
424 | } |
425 | ||
426 | static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep, | |
427 | gfp_t gfp_flags) | |
428 | { | |
429 | struct usbhsg_request *ureq; | |
430 | ||
431 | ureq = kzalloc(sizeof *ureq, gfp_flags); | |
432 | if (!ureq) | |
433 | return NULL; | |
434 | ||
6acb95d4 KM |
435 | usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq)); |
436 | ||
2f98382d KM |
437 | return &ureq->req; |
438 | } | |
439 | ||
440 | static void usbhsg_ep_free_request(struct usb_ep *ep, | |
441 | struct usb_request *req) | |
442 | { | |
443 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); | |
444 | ||
6acb95d4 | 445 | WARN_ON(!list_empty(&ureq->pkt.node)); |
2f98382d KM |
446 | kfree(ureq); |
447 | } | |
448 | ||
449 | static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req, | |
450 | gfp_t gfp_flags) | |
451 | { | |
452 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
453 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
454 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); | |
455 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
2f98382d KM |
456 | |
457 | /* param check */ | |
458 | if (usbhsg_is_not_connected(gpriv) || | |
459 | unlikely(!gpriv->driver) || | |
460 | unlikely(!pipe)) | |
97664a20 | 461 | return -ESHUTDOWN; |
2f98382d | 462 | |
97664a20 | 463 | usbhsg_queue_push(uep, ureq); |
2f98382d | 464 | |
97664a20 | 465 | return 0; |
2f98382d KM |
466 | } |
467 | ||
468 | static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) | |
469 | { | |
470 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
471 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); | |
97664a20 | 472 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
2f98382d | 473 | |
97664a20 | 474 | usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq)); |
2f98382d KM |
475 | usbhsg_queue_pop(uep, ureq, -ECONNRESET); |
476 | ||
2f98382d KM |
477 | return 0; |
478 | } | |
479 | ||
480 | static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge) | |
481 | { | |
482 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
483 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
484 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
97664a20 | 485 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
2f98382d | 486 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
2f98382d | 487 | unsigned long flags; |
2f98382d | 488 | |
97664a20 | 489 | usbhsg_pipe_disable(uep); |
2f98382d | 490 | |
97664a20 KM |
491 | dev_dbg(dev, "set halt %d (pipe %d)\n", |
492 | halt, usbhs_pipe_number(pipe)); | |
2f98382d | 493 | |
97664a20 KM |
494 | /******************** spin lock ********************/ |
495 | usbhs_lock(priv, flags); | |
2f98382d | 496 | |
97664a20 KM |
497 | if (halt) |
498 | usbhs_pipe_stall(pipe); | |
499 | else | |
500 | usbhs_pipe_disable(pipe); | |
2f98382d | 501 | |
97664a20 KM |
502 | if (halt && wedge) |
503 | usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE); | |
504 | else | |
505 | usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE); | |
2f98382d | 506 | |
97664a20 | 507 | usbhs_unlock(priv, flags); |
2f98382d KM |
508 | /******************** spin unlock ******************/ |
509 | ||
97664a20 | 510 | return 0; |
2f98382d KM |
511 | } |
512 | ||
513 | static int usbhsg_ep_set_halt(struct usb_ep *ep, int value) | |
514 | { | |
515 | return __usbhsg_ep_set_halt_wedge(ep, value, 0); | |
516 | } | |
517 | ||
518 | static int usbhsg_ep_set_wedge(struct usb_ep *ep) | |
519 | { | |
520 | return __usbhsg_ep_set_halt_wedge(ep, 1, 1); | |
521 | } | |
522 | ||
523 | static struct usb_ep_ops usbhsg_ep_ops = { | |
524 | .enable = usbhsg_ep_enable, | |
525 | .disable = usbhsg_ep_disable, | |
526 | ||
527 | .alloc_request = usbhsg_ep_alloc_request, | |
528 | .free_request = usbhsg_ep_free_request, | |
529 | ||
530 | .queue = usbhsg_ep_queue, | |
531 | .dequeue = usbhsg_ep_dequeue, | |
532 | ||
533 | .set_halt = usbhsg_ep_set_halt, | |
534 | .set_wedge = usbhsg_ep_set_wedge, | |
535 | }; | |
536 | ||
537 | /* | |
538 | * usb module start/end | |
539 | */ | |
540 | static int usbhsg_try_start(struct usbhs_priv *priv, u32 status) | |
541 | { | |
542 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
543 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); | |
544 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); | |
545 | struct device *dev = usbhs_priv_to_dev(priv); | |
2f98382d | 546 | unsigned long flags; |
97664a20 | 547 | int ret = 0; |
2f98382d KM |
548 | |
549 | /******************** spin lock ********************/ | |
97664a20 | 550 | usbhs_lock(priv, flags); |
2f98382d | 551 | |
2f98382d KM |
552 | usbhsg_status_set(gpriv, status); |
553 | if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && | |
554 | usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))) | |
97664a20 KM |
555 | ret = -1; /* not ready */ |
556 | ||
557 | usbhs_unlock(priv, flags); | |
558 | /******************** spin unlock ********************/ | |
559 | ||
560 | if (ret < 0) | |
561 | return 0; /* not ready is not error */ | |
2f98382d | 562 | |
97664a20 KM |
563 | /* |
564 | * enable interrupt and systems if ready | |
565 | */ | |
2f98382d KM |
566 | dev_dbg(dev, "start gadget\n"); |
567 | ||
568 | /* | |
569 | * pipe initialize and enable DCP | |
570 | */ | |
4bd04811 | 571 | usbhs_pipe_init(priv, |
dad67397 KM |
572 | usbhsg_queue_done); |
573 | usbhs_fifo_init(priv); | |
409ba9e7 | 574 | usbhsg_uep_init(gpriv); |
97664a20 KM |
575 | |
576 | /* dcp init */ | |
577 | dcp->pipe = usbhs_dcp_malloc(priv); | |
578 | dcp->pipe->mod_private = dcp; | |
2f98382d KM |
579 | |
580 | /* | |
581 | * system config enble | |
582 | * - HI speed | |
583 | * - function | |
584 | * - usb module | |
585 | */ | |
586 | usbhs_sys_hispeed_ctrl(priv, 1); | |
587 | usbhs_sys_function_ctrl(priv, 1); | |
588 | usbhs_sys_usb_ctrl(priv, 1); | |
589 | ||
590 | /* | |
591 | * enable irq callback | |
592 | */ | |
593 | mod->irq_dev_state = usbhsg_irq_dev_state; | |
594 | mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage; | |
2f98382d KM |
595 | usbhs_irq_callback_update(priv, mod); |
596 | ||
2f98382d KM |
597 | return 0; |
598 | } | |
599 | ||
600 | static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status) | |
601 | { | |
602 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
603 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); | |
604 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); | |
605 | struct device *dev = usbhs_priv_to_dev(priv); | |
2f98382d | 606 | unsigned long flags; |
97664a20 | 607 | int ret = 0; |
2f98382d KM |
608 | |
609 | /******************** spin lock ********************/ | |
97664a20 | 610 | usbhs_lock(priv, flags); |
2f98382d | 611 | |
2f98382d KM |
612 | usbhsg_status_clr(gpriv, status); |
613 | if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && | |
614 | !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)) | |
97664a20 KM |
615 | ret = -1; /* already done */ |
616 | ||
617 | usbhs_unlock(priv, flags); | |
618 | /******************** spin unlock ********************/ | |
619 | ||
620 | if (ret < 0) | |
621 | return 0; /* already done is not error */ | |
2f98382d | 622 | |
97664a20 KM |
623 | /* |
624 | * disable interrupt and systems if 1st try | |
625 | */ | |
dad67397 KM |
626 | usbhs_fifo_quit(priv); |
627 | ||
2f98382d KM |
628 | /* disable all irq */ |
629 | mod->irq_dev_state = NULL; | |
630 | mod->irq_ctrl_stage = NULL; | |
2f98382d KM |
631 | usbhs_irq_callback_update(priv, mod); |
632 | ||
2f98382d KM |
633 | gpriv->gadget.speed = USB_SPEED_UNKNOWN; |
634 | ||
635 | /* disable sys */ | |
636 | usbhs_sys_hispeed_ctrl(priv, 0); | |
637 | usbhs_sys_function_ctrl(priv, 0); | |
638 | usbhs_sys_usb_ctrl(priv, 0); | |
639 | ||
97664a20 | 640 | usbhsg_pipe_disable(dcp); |
2f98382d KM |
641 | |
642 | if (gpriv->driver && | |
643 | gpriv->driver->disconnect) | |
644 | gpriv->driver->disconnect(&gpriv->gadget); | |
645 | ||
646 | dev_dbg(dev, "stop gadget\n"); | |
647 | ||
2f98382d KM |
648 | return 0; |
649 | } | |
650 | ||
651 | /* | |
652 | * | |
653 | * linux usb function | |
654 | * | |
655 | */ | |
656 | struct usbhsg_gpriv *the_controller; | |
657 | int usb_gadget_probe_driver(struct usb_gadget_driver *driver, | |
658 | int (*bind)(struct usb_gadget *)) | |
659 | { | |
660 | struct usbhsg_gpriv *gpriv = the_controller; | |
661 | struct usbhs_priv *priv; | |
662 | struct device *dev; | |
663 | int ret; | |
664 | ||
665 | if (!bind || | |
666 | !driver || | |
667 | !driver->setup || | |
668 | driver->speed != USB_SPEED_HIGH) | |
669 | return -EINVAL; | |
670 | if (!gpriv) | |
671 | return -ENODEV; | |
672 | if (gpriv->driver) | |
673 | return -EBUSY; | |
674 | ||
675 | dev = usbhsg_gpriv_to_dev(gpriv); | |
676 | priv = usbhsg_gpriv_to_priv(gpriv); | |
677 | ||
678 | /* first hook up the driver ... */ | |
679 | gpriv->driver = driver; | |
680 | gpriv->gadget.dev.driver = &driver->driver; | |
681 | ||
682 | ret = device_add(&gpriv->gadget.dev); | |
683 | if (ret) { | |
684 | dev_err(dev, "device_add error %d\n", ret); | |
685 | goto add_fail; | |
686 | } | |
687 | ||
688 | ret = bind(&gpriv->gadget); | |
689 | if (ret) { | |
690 | dev_err(dev, "bind to driver %s error %d\n", | |
691 | driver->driver.name, ret); | |
692 | goto bind_fail; | |
693 | } | |
694 | ||
695 | dev_dbg(dev, "bind %s\n", driver->driver.name); | |
696 | ||
697 | return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD); | |
698 | ||
699 | bind_fail: | |
700 | device_del(&gpriv->gadget.dev); | |
701 | add_fail: | |
702 | gpriv->driver = NULL; | |
703 | gpriv->gadget.dev.driver = NULL; | |
704 | ||
705 | return ret; | |
706 | } | |
707 | EXPORT_SYMBOL(usb_gadget_probe_driver); | |
708 | ||
709 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) | |
710 | { | |
711 | struct usbhsg_gpriv *gpriv = the_controller; | |
712 | struct usbhs_priv *priv; | |
713 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
714 | ||
715 | if (!gpriv) | |
716 | return -ENODEV; | |
717 | ||
718 | if (!driver || | |
719 | !driver->unbind || | |
720 | driver != gpriv->driver) | |
721 | return -EINVAL; | |
722 | ||
723 | dev = usbhsg_gpriv_to_dev(gpriv); | |
724 | priv = usbhsg_gpriv_to_priv(gpriv); | |
725 | ||
726 | usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD); | |
727 | device_del(&gpriv->gadget.dev); | |
728 | gpriv->driver = NULL; | |
729 | ||
730 | if (driver->disconnect) | |
731 | driver->disconnect(&gpriv->gadget); | |
732 | ||
733 | driver->unbind(&gpriv->gadget); | |
734 | dev_dbg(dev, "unbind %s\n", driver->driver.name); | |
735 | ||
736 | return 0; | |
737 | } | |
738 | EXPORT_SYMBOL(usb_gadget_unregister_driver); | |
739 | ||
740 | /* | |
741 | * usb gadget ops | |
742 | */ | |
743 | static int usbhsg_get_frame(struct usb_gadget *gadget) | |
744 | { | |
745 | struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); | |
746 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); | |
747 | ||
748 | return usbhs_frame_get_num(priv); | |
749 | } | |
750 | ||
751 | static struct usb_gadget_ops usbhsg_gadget_ops = { | |
752 | .get_frame = usbhsg_get_frame, | |
753 | }; | |
754 | ||
755 | static int usbhsg_start(struct usbhs_priv *priv) | |
756 | { | |
757 | return usbhsg_try_start(priv, USBHSG_STATUS_STARTED); | |
758 | } | |
759 | ||
760 | static int usbhsg_stop(struct usbhs_priv *priv) | |
761 | { | |
762 | return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED); | |
763 | } | |
764 | ||
765 | int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv) | |
766 | { | |
767 | struct usbhsg_gpriv *gpriv; | |
768 | struct usbhsg_uep *uep; | |
769 | struct device *dev = usbhs_priv_to_dev(priv); | |
770 | int pipe_size = usbhs_get_dparam(priv, pipe_size); | |
771 | int i; | |
772 | ||
773 | gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL); | |
774 | if (!gpriv) { | |
775 | dev_err(dev, "Could not allocate gadget priv\n"); | |
776 | return -ENOMEM; | |
777 | } | |
778 | ||
779 | uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL); | |
780 | if (!uep) { | |
781 | dev_err(dev, "Could not allocate ep\n"); | |
782 | goto usbhs_mod_gadget_probe_err_gpriv; | |
783 | } | |
784 | ||
785 | /* | |
786 | * CAUTION | |
787 | * | |
788 | * There is no guarantee that it is possible to access usb module here. | |
789 | * Don't accesses to it. | |
790 | * The accesse will be enable after "usbhsg_start" | |
791 | */ | |
792 | ||
793 | /* | |
794 | * register itself | |
795 | */ | |
796 | usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET); | |
797 | ||
798 | /* init gpriv */ | |
799 | gpriv->mod.name = "gadget"; | |
800 | gpriv->mod.start = usbhsg_start; | |
801 | gpriv->mod.stop = usbhsg_stop; | |
802 | gpriv->uep = uep; | |
803 | gpriv->uep_size = pipe_size; | |
804 | usbhsg_status_init(gpriv); | |
805 | ||
806 | /* | |
807 | * init gadget | |
808 | */ | |
809 | device_initialize(&gpriv->gadget.dev); | |
810 | dev_set_name(&gpriv->gadget.dev, "gadget"); | |
811 | gpriv->gadget.dev.parent = dev; | |
812 | gpriv->gadget.name = "renesas_usbhs_udc"; | |
813 | gpriv->gadget.ops = &usbhsg_gadget_ops; | |
814 | gpriv->gadget.is_dualspeed = 1; | |
815 | ||
816 | INIT_LIST_HEAD(&gpriv->gadget.ep_list); | |
817 | ||
818 | /* | |
819 | * init usb_ep | |
820 | */ | |
821 | usbhsg_for_each_uep_with_dcp(uep, gpriv, i) { | |
822 | uep->gpriv = gpriv; | |
823 | snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i); | |
824 | ||
825 | uep->ep.name = uep->ep_name; | |
826 | uep->ep.ops = &usbhsg_ep_ops; | |
827 | INIT_LIST_HEAD(&uep->ep.ep_list); | |
2f98382d KM |
828 | |
829 | /* init DCP */ | |
830 | if (usbhsg_is_dcp(uep)) { | |
831 | gpriv->gadget.ep0 = &uep->ep; | |
832 | uep->ep.maxpacket = 64; | |
833 | } | |
834 | /* init normal pipe */ | |
835 | else { | |
836 | uep->ep.maxpacket = 512; | |
837 | list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list); | |
838 | } | |
839 | } | |
840 | ||
841 | the_controller = gpriv; | |
842 | ||
843 | dev_info(dev, "gadget probed\n"); | |
844 | ||
845 | return 0; | |
846 | ||
847 | usbhs_mod_gadget_probe_err_gpriv: | |
848 | kfree(gpriv); | |
849 | ||
850 | return -ENOMEM; | |
851 | } | |
852 | ||
853 | void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv) | |
854 | { | |
855 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
856 | ||
857 | kfree(gpriv); | |
858 | } |