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 | /* | |
233f519d | 117 | * queue push/pop |
2f98382d KM |
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 | ||
233f519d KM |
163 | /* |
164 | * dma map/unmap | |
165 | */ | |
e73a9891 KM |
166 | static int usbhsg_dma_map(struct device *dev, |
167 | struct usbhs_pkt *pkt, | |
168 | enum dma_data_direction dir) | |
169 | { | |
170 | struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt); | |
171 | struct usb_request *req = &ureq->req; | |
172 | ||
173 | if (pkt->dma != DMA_ADDR_INVALID) { | |
174 | dev_err(dev, "dma is already mapped\n"); | |
175 | return -EIO; | |
176 | } | |
177 | ||
178 | if (req->dma == DMA_ADDR_INVALID) { | |
179 | pkt->dma = dma_map_single(dev, pkt->buf, pkt->length, dir); | |
180 | } else { | |
181 | dma_sync_single_for_device(dev, req->dma, req->length, dir); | |
182 | pkt->dma = req->dma; | |
183 | } | |
184 | ||
185 | if (dma_mapping_error(dev, pkt->dma)) { | |
186 | dev_err(dev, "dma mapping error %x\n", pkt->dma); | |
187 | return -EIO; | |
188 | } | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
193 | static int usbhsg_dma_unmap(struct device *dev, | |
194 | struct usbhs_pkt *pkt, | |
195 | enum dma_data_direction dir) | |
196 | { | |
197 | struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt); | |
198 | struct usb_request *req = &ureq->req; | |
199 | ||
200 | if (pkt->dma == DMA_ADDR_INVALID) { | |
201 | dev_err(dev, "dma is not mapped\n"); | |
202 | return -EIO; | |
203 | } | |
204 | ||
205 | if (req->dma == DMA_ADDR_INVALID) | |
206 | dma_unmap_single(dev, pkt->dma, pkt->length, dir); | |
207 | else | |
208 | dma_sync_single_for_cpu(dev, req->dma, req->length, dir); | |
209 | ||
210 | pkt->dma = DMA_ADDR_INVALID; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map) | |
216 | { | |
217 | struct usbhs_pipe *pipe = pkt->pipe; | |
218 | struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe); | |
219 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
220 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
221 | enum dma_data_direction dir; | |
222 | ||
223 | dir = usbhs_pipe_is_dir_in(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; | |
224 | ||
225 | if (map) | |
226 | return usbhsg_dma_map(dev, pkt, dir); | |
227 | else | |
228 | return usbhsg_dma_unmap(dev, pkt, dir); | |
229 | } | |
230 | ||
2f98382d KM |
231 | /* |
232 | * USB_TYPE_STANDARD / clear feature functions | |
233 | */ | |
234 | static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv, | |
235 | struct usbhsg_uep *uep, | |
236 | struct usb_ctrlrequest *ctrl) | |
237 | { | |
238 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
239 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); | |
240 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); | |
241 | ||
242 | usbhs_dcp_control_transfer_done(pipe); | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
247 | static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv, | |
248 | struct usbhsg_uep *uep, | |
249 | struct usb_ctrlrequest *ctrl) | |
250 | { | |
251 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
252 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
253 | ||
254 | if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) { | |
e8d548d5 | 255 | usbhs_pipe_disable(pipe); |
2f98382d | 256 | usbhs_pipe_clear_sequence(pipe); |
e8d548d5 | 257 | usbhs_pipe_enable(pipe); |
2f98382d KM |
258 | } |
259 | ||
260 | usbhsg_recip_handler_std_control_done(priv, uep, ctrl); | |
261 | ||
2f98382d KM |
262 | return 0; |
263 | } | |
264 | ||
265 | struct usbhsg_recip_handle req_clear_feature = { | |
266 | .name = "clear feature", | |
267 | .device = usbhsg_recip_handler_std_control_done, | |
268 | .interface = usbhsg_recip_handler_std_control_done, | |
269 | .endpoint = usbhsg_recip_handler_std_clear_endpoint, | |
270 | }; | |
271 | ||
272 | /* | |
273 | * USB_TYPE handler | |
274 | */ | |
275 | static int usbhsg_recip_run_handle(struct usbhs_priv *priv, | |
276 | struct usbhsg_recip_handle *handler, | |
277 | struct usb_ctrlrequest *ctrl) | |
278 | { | |
279 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
280 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
281 | struct usbhsg_uep *uep; | |
0432eed0 | 282 | struct usbhs_pipe *pipe; |
2f98382d KM |
283 | int recip = ctrl->bRequestType & USB_RECIP_MASK; |
284 | int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; | |
285 | int ret; | |
286 | int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep, | |
287 | struct usb_ctrlrequest *ctrl); | |
288 | char *msg; | |
289 | ||
290 | uep = usbhsg_gpriv_to_nth_uep(gpriv, nth); | |
0432eed0 KM |
291 | pipe = usbhsg_uep_to_pipe(uep); |
292 | if (!pipe) { | |
9a28b7bd | 293 | dev_err(dev, "wrong recip request\n"); |
97664a20 KM |
294 | ret = -EINVAL; |
295 | goto usbhsg_recip_run_handle_end; | |
9a28b7bd | 296 | } |
2f98382d KM |
297 | |
298 | switch (recip) { | |
299 | case USB_RECIP_DEVICE: | |
300 | msg = "DEVICE"; | |
301 | func = handler->device; | |
302 | break; | |
303 | case USB_RECIP_INTERFACE: | |
304 | msg = "INTERFACE"; | |
305 | func = handler->interface; | |
306 | break; | |
307 | case USB_RECIP_ENDPOINT: | |
308 | msg = "ENDPOINT"; | |
309 | func = handler->endpoint; | |
310 | break; | |
311 | default: | |
312 | dev_warn(dev, "unsupported RECIP(%d)\n", recip); | |
313 | func = NULL; | |
314 | ret = -EINVAL; | |
315 | } | |
316 | ||
317 | if (func) { | |
97664a20 KM |
318 | unsigned long flags; |
319 | ||
2f98382d | 320 | dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg); |
97664a20 KM |
321 | |
322 | /******************** spin lock ********************/ | |
323 | usbhs_lock(priv, flags); | |
2f98382d | 324 | ret = func(priv, uep, ctrl); |
97664a20 KM |
325 | usbhs_unlock(priv, flags); |
326 | /******************** spin unlock ******************/ | |
2f98382d KM |
327 | } |
328 | ||
97664a20 | 329 | usbhsg_recip_run_handle_end: |
0432eed0 | 330 | usbhs_pkt_start(pipe); |
97664a20 | 331 | |
2f98382d KM |
332 | return ret; |
333 | } | |
334 | ||
335 | /* | |
336 | * irq functions | |
337 | * | |
338 | * it will be called from usbhs_interrupt | |
339 | */ | |
340 | static int usbhsg_irq_dev_state(struct usbhs_priv *priv, | |
341 | struct usbhs_irq_state *irq_state) | |
342 | { | |
343 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
344 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
345 | ||
346 | gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state); | |
347 | ||
348 | dev_dbg(dev, "state = %x : speed : %d\n", | |
349 | usbhs_status_get_device_state(irq_state), | |
350 | gpriv->gadget.speed); | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv, | |
356 | struct usbhs_irq_state *irq_state) | |
357 | { | |
358 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
359 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); | |
360 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); | |
361 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
362 | struct usb_ctrlrequest ctrl; | |
363 | struct usbhsg_recip_handle *recip_handler = NULL; | |
364 | int stage = usbhs_status_get_ctrl_stage(irq_state); | |
365 | int ret = 0; | |
366 | ||
367 | dev_dbg(dev, "stage = %d\n", stage); | |
368 | ||
369 | /* | |
370 | * see Manual | |
371 | * | |
372 | * "Operation" | |
373 | * - "Interrupt Function" | |
374 | * - "Control Transfer Stage Transition Interrupt" | |
375 | * - Fig. "Control Transfer Stage Transitions" | |
376 | */ | |
377 | ||
378 | switch (stage) { | |
379 | case READ_DATA_STAGE: | |
0cb7e61d | 380 | dcp->handler = &usbhs_fifo_pio_push_handler; |
2f98382d KM |
381 | break; |
382 | case WRITE_DATA_STAGE: | |
0cb7e61d | 383 | dcp->handler = &usbhs_fifo_pio_pop_handler; |
2f98382d KM |
384 | break; |
385 | case NODATA_STATUS_STAGE: | |
dad67397 | 386 | dcp->handler = &usbhs_ctrl_stage_end_handler; |
2f98382d KM |
387 | break; |
388 | default: | |
389 | return ret; | |
390 | } | |
391 | ||
392 | /* | |
393 | * get usb request | |
394 | */ | |
395 | usbhs_usbreq_get_val(priv, &ctrl); | |
396 | ||
397 | switch (ctrl.bRequestType & USB_TYPE_MASK) { | |
398 | case USB_TYPE_STANDARD: | |
399 | switch (ctrl.bRequest) { | |
400 | case USB_REQ_CLEAR_FEATURE: | |
401 | recip_handler = &req_clear_feature; | |
402 | break; | |
403 | } | |
404 | } | |
405 | ||
406 | /* | |
407 | * setup stage / run recip | |
408 | */ | |
409 | if (recip_handler) | |
410 | ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl); | |
411 | else | |
412 | ret = gpriv->driver->setup(&gpriv->gadget, &ctrl); | |
413 | ||
414 | if (ret < 0) | |
e8d548d5 | 415 | usbhs_pipe_stall(pipe); |
2f98382d KM |
416 | |
417 | return ret; | |
418 | } | |
419 | ||
2f98382d KM |
420 | /* |
421 | * | |
422 | * usb_dcp_ops | |
423 | * | |
424 | */ | |
2f98382d KM |
425 | static int usbhsg_pipe_disable(struct usbhsg_uep *uep) |
426 | { | |
427 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
8a2c225d | 428 | struct usbhs_pkt *pkt; |
2f98382d | 429 | |
e8d548d5 | 430 | usbhs_pipe_disable(pipe); |
2f98382d | 431 | |
2f98382d | 432 | while (1) { |
97664a20 | 433 | pkt = usbhs_pkt_pop(pipe, NULL); |
8a2c225d | 434 | if (!pkt) |
2f98382d | 435 | break; |
2f98382d KM |
436 | } |
437 | ||
2f98382d KM |
438 | return 0; |
439 | } | |
440 | ||
409ba9e7 KM |
441 | static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv) |
442 | { | |
443 | int i; | |
444 | struct usbhsg_uep *uep; | |
445 | ||
446 | usbhsg_for_each_uep_with_dcp(uep, gpriv, i) | |
447 | uep->pipe = NULL; | |
448 | } | |
449 | ||
2f98382d KM |
450 | /* |
451 | * | |
452 | * usb_ep_ops | |
453 | * | |
454 | */ | |
455 | static int usbhsg_ep_enable(struct usb_ep *ep, | |
456 | const struct usb_endpoint_descriptor *desc) | |
457 | { | |
458 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
459 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
460 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); | |
461 | struct usbhs_pipe *pipe; | |
2f98382d KM |
462 | int ret = -EIO; |
463 | ||
409ba9e7 KM |
464 | /* |
465 | * if it already have pipe, | |
466 | * nothing to do | |
467 | */ | |
08e6c611 KM |
468 | if (uep->pipe) { |
469 | usbhs_pipe_clear(uep->pipe); | |
470 | usbhs_pipe_clear_sequence(uep->pipe); | |
409ba9e7 | 471 | return 0; |
08e6c611 | 472 | } |
409ba9e7 | 473 | |
2f98382d KM |
474 | pipe = usbhs_pipe_malloc(priv, desc); |
475 | if (pipe) { | |
476 | uep->pipe = pipe; | |
477 | pipe->mod_private = uep; | |
2f98382d | 478 | |
233f519d KM |
479 | /* |
480 | * usbhs_fifo_dma_push/pop_handler try to | |
481 | * use dmaengine if possible. | |
482 | * It will use pio handler if impossible. | |
483 | */ | |
2f98382d | 484 | if (usb_endpoint_dir_in(desc)) |
6d721b29 | 485 | uep->handler = &usbhs_fifo_dma_push_handler; |
2f98382d | 486 | else |
6d721b29 | 487 | uep->handler = &usbhs_fifo_dma_pop_handler; |
2f98382d KM |
488 | |
489 | ret = 0; | |
490 | } | |
cb96632c | 491 | |
2f98382d KM |
492 | return ret; |
493 | } | |
494 | ||
495 | static int usbhsg_ep_disable(struct usb_ep *ep) | |
496 | { | |
497 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
2f98382d | 498 | |
97664a20 | 499 | return usbhsg_pipe_disable(uep); |
2f98382d KM |
500 | } |
501 | ||
502 | static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep, | |
503 | gfp_t gfp_flags) | |
504 | { | |
505 | struct usbhsg_request *ureq; | |
506 | ||
507 | ureq = kzalloc(sizeof *ureq, gfp_flags); | |
508 | if (!ureq) | |
509 | return NULL; | |
510 | ||
6acb95d4 KM |
511 | usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq)); |
512 | ||
e73a9891 KM |
513 | ureq->req.dma = DMA_ADDR_INVALID; |
514 | ||
2f98382d KM |
515 | return &ureq->req; |
516 | } | |
517 | ||
518 | static void usbhsg_ep_free_request(struct usb_ep *ep, | |
519 | struct usb_request *req) | |
520 | { | |
521 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); | |
522 | ||
6acb95d4 | 523 | WARN_ON(!list_empty(&ureq->pkt.node)); |
2f98382d KM |
524 | kfree(ureq); |
525 | } | |
526 | ||
527 | static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req, | |
528 | gfp_t gfp_flags) | |
529 | { | |
530 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
531 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
532 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); | |
533 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
2f98382d KM |
534 | |
535 | /* param check */ | |
536 | if (usbhsg_is_not_connected(gpriv) || | |
537 | unlikely(!gpriv->driver) || | |
538 | unlikely(!pipe)) | |
97664a20 | 539 | return -ESHUTDOWN; |
2f98382d | 540 | |
97664a20 | 541 | usbhsg_queue_push(uep, ureq); |
2f98382d | 542 | |
97664a20 | 543 | return 0; |
2f98382d KM |
544 | } |
545 | ||
546 | static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) | |
547 | { | |
548 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
549 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); | |
97664a20 | 550 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
2f98382d | 551 | |
97664a20 | 552 | usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq)); |
2f98382d KM |
553 | usbhsg_queue_pop(uep, ureq, -ECONNRESET); |
554 | ||
2f98382d KM |
555 | return 0; |
556 | } | |
557 | ||
558 | static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge) | |
559 | { | |
560 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); | |
561 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); | |
562 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); | |
97664a20 | 563 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
2f98382d | 564 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
2f98382d | 565 | unsigned long flags; |
2f98382d | 566 | |
97664a20 | 567 | usbhsg_pipe_disable(uep); |
2f98382d | 568 | |
97664a20 KM |
569 | dev_dbg(dev, "set halt %d (pipe %d)\n", |
570 | halt, usbhs_pipe_number(pipe)); | |
2f98382d | 571 | |
97664a20 KM |
572 | /******************** spin lock ********************/ |
573 | usbhs_lock(priv, flags); | |
2f98382d | 574 | |
97664a20 KM |
575 | if (halt) |
576 | usbhs_pipe_stall(pipe); | |
577 | else | |
578 | usbhs_pipe_disable(pipe); | |
2f98382d | 579 | |
97664a20 KM |
580 | if (halt && wedge) |
581 | usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE); | |
582 | else | |
583 | usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE); | |
2f98382d | 584 | |
97664a20 | 585 | usbhs_unlock(priv, flags); |
2f98382d KM |
586 | /******************** spin unlock ******************/ |
587 | ||
97664a20 | 588 | return 0; |
2f98382d KM |
589 | } |
590 | ||
591 | static int usbhsg_ep_set_halt(struct usb_ep *ep, int value) | |
592 | { | |
593 | return __usbhsg_ep_set_halt_wedge(ep, value, 0); | |
594 | } | |
595 | ||
596 | static int usbhsg_ep_set_wedge(struct usb_ep *ep) | |
597 | { | |
598 | return __usbhsg_ep_set_halt_wedge(ep, 1, 1); | |
599 | } | |
600 | ||
601 | static struct usb_ep_ops usbhsg_ep_ops = { | |
602 | .enable = usbhsg_ep_enable, | |
603 | .disable = usbhsg_ep_disable, | |
604 | ||
605 | .alloc_request = usbhsg_ep_alloc_request, | |
606 | .free_request = usbhsg_ep_free_request, | |
607 | ||
608 | .queue = usbhsg_ep_queue, | |
609 | .dequeue = usbhsg_ep_dequeue, | |
610 | ||
611 | .set_halt = usbhsg_ep_set_halt, | |
612 | .set_wedge = usbhsg_ep_set_wedge, | |
613 | }; | |
614 | ||
615 | /* | |
616 | * usb module start/end | |
617 | */ | |
618 | static int usbhsg_try_start(struct usbhs_priv *priv, u32 status) | |
619 | { | |
620 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
621 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); | |
622 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); | |
623 | struct device *dev = usbhs_priv_to_dev(priv); | |
2f98382d | 624 | unsigned long flags; |
97664a20 | 625 | int ret = 0; |
2f98382d KM |
626 | |
627 | /******************** spin lock ********************/ | |
97664a20 | 628 | usbhs_lock(priv, flags); |
2f98382d | 629 | |
2f98382d KM |
630 | usbhsg_status_set(gpriv, status); |
631 | if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && | |
632 | usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))) | |
97664a20 KM |
633 | ret = -1; /* not ready */ |
634 | ||
635 | usbhs_unlock(priv, flags); | |
636 | /******************** spin unlock ********************/ | |
637 | ||
638 | if (ret < 0) | |
639 | return 0; /* not ready is not error */ | |
2f98382d | 640 | |
97664a20 KM |
641 | /* |
642 | * enable interrupt and systems if ready | |
643 | */ | |
2f98382d KM |
644 | dev_dbg(dev, "start gadget\n"); |
645 | ||
646 | /* | |
647 | * pipe initialize and enable DCP | |
648 | */ | |
4bd04811 | 649 | usbhs_pipe_init(priv, |
e73a9891 KM |
650 | usbhsg_queue_done, |
651 | usbhsg_dma_map_ctrl); | |
dad67397 | 652 | usbhs_fifo_init(priv); |
409ba9e7 | 653 | usbhsg_uep_init(gpriv); |
97664a20 KM |
654 | |
655 | /* dcp init */ | |
656 | dcp->pipe = usbhs_dcp_malloc(priv); | |
657 | dcp->pipe->mod_private = dcp; | |
2f98382d KM |
658 | |
659 | /* | |
660 | * system config enble | |
661 | * - HI speed | |
662 | * - function | |
663 | * - usb module | |
664 | */ | |
665 | usbhs_sys_hispeed_ctrl(priv, 1); | |
666 | usbhs_sys_function_ctrl(priv, 1); | |
667 | usbhs_sys_usb_ctrl(priv, 1); | |
668 | ||
669 | /* | |
670 | * enable irq callback | |
671 | */ | |
672 | mod->irq_dev_state = usbhsg_irq_dev_state; | |
673 | mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage; | |
2f98382d KM |
674 | usbhs_irq_callback_update(priv, mod); |
675 | ||
2f98382d KM |
676 | return 0; |
677 | } | |
678 | ||
679 | static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status) | |
680 | { | |
681 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
682 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); | |
683 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); | |
684 | struct device *dev = usbhs_priv_to_dev(priv); | |
2f98382d | 685 | unsigned long flags; |
97664a20 | 686 | int ret = 0; |
2f98382d KM |
687 | |
688 | /******************** spin lock ********************/ | |
97664a20 | 689 | usbhs_lock(priv, flags); |
2f98382d | 690 | |
2f98382d KM |
691 | usbhsg_status_clr(gpriv, status); |
692 | if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && | |
693 | !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)) | |
97664a20 KM |
694 | ret = -1; /* already done */ |
695 | ||
696 | usbhs_unlock(priv, flags); | |
697 | /******************** spin unlock ********************/ | |
698 | ||
699 | if (ret < 0) | |
700 | return 0; /* already done is not error */ | |
2f98382d | 701 | |
97664a20 KM |
702 | /* |
703 | * disable interrupt and systems if 1st try | |
704 | */ | |
dad67397 KM |
705 | usbhs_fifo_quit(priv); |
706 | ||
2f98382d KM |
707 | /* disable all irq */ |
708 | mod->irq_dev_state = NULL; | |
709 | mod->irq_ctrl_stage = NULL; | |
2f98382d KM |
710 | usbhs_irq_callback_update(priv, mod); |
711 | ||
2f98382d KM |
712 | gpriv->gadget.speed = USB_SPEED_UNKNOWN; |
713 | ||
714 | /* disable sys */ | |
715 | usbhs_sys_hispeed_ctrl(priv, 0); | |
716 | usbhs_sys_function_ctrl(priv, 0); | |
717 | usbhs_sys_usb_ctrl(priv, 0); | |
718 | ||
97664a20 | 719 | usbhsg_pipe_disable(dcp); |
2f98382d KM |
720 | |
721 | if (gpriv->driver && | |
722 | gpriv->driver->disconnect) | |
723 | gpriv->driver->disconnect(&gpriv->gadget); | |
724 | ||
725 | dev_dbg(dev, "stop gadget\n"); | |
726 | ||
2f98382d KM |
727 | return 0; |
728 | } | |
729 | ||
730 | /* | |
731 | * | |
732 | * linux usb function | |
733 | * | |
734 | */ | |
735 | struct usbhsg_gpriv *the_controller; | |
0f91349b | 736 | static int usbhsg_gadget_start(struct usb_gadget_driver *driver, |
2f98382d KM |
737 | int (*bind)(struct usb_gadget *)) |
738 | { | |
739 | struct usbhsg_gpriv *gpriv = the_controller; | |
740 | struct usbhs_priv *priv; | |
741 | struct device *dev; | |
742 | int ret; | |
743 | ||
744 | if (!bind || | |
745 | !driver || | |
746 | !driver->setup || | |
747 | driver->speed != USB_SPEED_HIGH) | |
748 | return -EINVAL; | |
749 | if (!gpriv) | |
750 | return -ENODEV; | |
751 | if (gpriv->driver) | |
752 | return -EBUSY; | |
753 | ||
754 | dev = usbhsg_gpriv_to_dev(gpriv); | |
755 | priv = usbhsg_gpriv_to_priv(gpriv); | |
756 | ||
757 | /* first hook up the driver ... */ | |
758 | gpriv->driver = driver; | |
759 | gpriv->gadget.dev.driver = &driver->driver; | |
760 | ||
761 | ret = device_add(&gpriv->gadget.dev); | |
762 | if (ret) { | |
763 | dev_err(dev, "device_add error %d\n", ret); | |
764 | goto add_fail; | |
765 | } | |
766 | ||
767 | ret = bind(&gpriv->gadget); | |
768 | if (ret) { | |
769 | dev_err(dev, "bind to driver %s error %d\n", | |
770 | driver->driver.name, ret); | |
771 | goto bind_fail; | |
772 | } | |
773 | ||
774 | dev_dbg(dev, "bind %s\n", driver->driver.name); | |
775 | ||
776 | return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD); | |
777 | ||
778 | bind_fail: | |
779 | device_del(&gpriv->gadget.dev); | |
780 | add_fail: | |
781 | gpriv->driver = NULL; | |
782 | gpriv->gadget.dev.driver = NULL; | |
783 | ||
784 | return ret; | |
785 | } | |
2f98382d | 786 | |
0f91349b | 787 | static int usbhsg_gadget_stop(struct usb_gadget_driver *driver) |
2f98382d KM |
788 | { |
789 | struct usbhsg_gpriv *gpriv = the_controller; | |
790 | struct usbhs_priv *priv; | |
791 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); | |
792 | ||
793 | if (!gpriv) | |
794 | return -ENODEV; | |
795 | ||
796 | if (!driver || | |
797 | !driver->unbind || | |
798 | driver != gpriv->driver) | |
799 | return -EINVAL; | |
800 | ||
801 | dev = usbhsg_gpriv_to_dev(gpriv); | |
802 | priv = usbhsg_gpriv_to_priv(gpriv); | |
803 | ||
804 | usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD); | |
805 | device_del(&gpriv->gadget.dev); | |
806 | gpriv->driver = NULL; | |
807 | ||
808 | if (driver->disconnect) | |
809 | driver->disconnect(&gpriv->gadget); | |
810 | ||
811 | driver->unbind(&gpriv->gadget); | |
812 | dev_dbg(dev, "unbind %s\n", driver->driver.name); | |
813 | ||
814 | return 0; | |
815 | } | |
2f98382d KM |
816 | |
817 | /* | |
818 | * usb gadget ops | |
819 | */ | |
820 | static int usbhsg_get_frame(struct usb_gadget *gadget) | |
821 | { | |
822 | struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); | |
823 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); | |
824 | ||
825 | return usbhs_frame_get_num(priv); | |
826 | } | |
827 | ||
828 | static struct usb_gadget_ops usbhsg_gadget_ops = { | |
829 | .get_frame = usbhsg_get_frame, | |
0f91349b SAS |
830 | .start = usbhsg_gadget_start, |
831 | .stop = usbhsg_gadget_stop, | |
2f98382d KM |
832 | }; |
833 | ||
834 | static int usbhsg_start(struct usbhs_priv *priv) | |
835 | { | |
836 | return usbhsg_try_start(priv, USBHSG_STATUS_STARTED); | |
837 | } | |
838 | ||
839 | static int usbhsg_stop(struct usbhs_priv *priv) | |
840 | { | |
841 | return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED); | |
842 | } | |
843 | ||
844 | int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv) | |
845 | { | |
846 | struct usbhsg_gpriv *gpriv; | |
847 | struct usbhsg_uep *uep; | |
848 | struct device *dev = usbhs_priv_to_dev(priv); | |
849 | int pipe_size = usbhs_get_dparam(priv, pipe_size); | |
850 | int i; | |
0f91349b | 851 | int ret; |
2f98382d KM |
852 | |
853 | gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL); | |
854 | if (!gpriv) { | |
855 | dev_err(dev, "Could not allocate gadget priv\n"); | |
856 | return -ENOMEM; | |
857 | } | |
858 | ||
859 | uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL); | |
860 | if (!uep) { | |
861 | dev_err(dev, "Could not allocate ep\n"); | |
0f91349b | 862 | ret = -ENOMEM; |
2f98382d KM |
863 | goto usbhs_mod_gadget_probe_err_gpriv; |
864 | } | |
865 | ||
866 | /* | |
867 | * CAUTION | |
868 | * | |
869 | * There is no guarantee that it is possible to access usb module here. | |
870 | * Don't accesses to it. | |
871 | * The accesse will be enable after "usbhsg_start" | |
872 | */ | |
873 | ||
874 | /* | |
875 | * register itself | |
876 | */ | |
877 | usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET); | |
878 | ||
879 | /* init gpriv */ | |
880 | gpriv->mod.name = "gadget"; | |
881 | gpriv->mod.start = usbhsg_start; | |
882 | gpriv->mod.stop = usbhsg_stop; | |
883 | gpriv->uep = uep; | |
884 | gpriv->uep_size = pipe_size; | |
885 | usbhsg_status_init(gpriv); | |
886 | ||
887 | /* | |
888 | * init gadget | |
889 | */ | |
890 | device_initialize(&gpriv->gadget.dev); | |
891 | dev_set_name(&gpriv->gadget.dev, "gadget"); | |
892 | gpriv->gadget.dev.parent = dev; | |
893 | gpriv->gadget.name = "renesas_usbhs_udc"; | |
894 | gpriv->gadget.ops = &usbhsg_gadget_ops; | |
895 | gpriv->gadget.is_dualspeed = 1; | |
896 | ||
897 | INIT_LIST_HEAD(&gpriv->gadget.ep_list); | |
898 | ||
899 | /* | |
900 | * init usb_ep | |
901 | */ | |
902 | usbhsg_for_each_uep_with_dcp(uep, gpriv, i) { | |
903 | uep->gpriv = gpriv; | |
904 | snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i); | |
905 | ||
906 | uep->ep.name = uep->ep_name; | |
907 | uep->ep.ops = &usbhsg_ep_ops; | |
908 | INIT_LIST_HEAD(&uep->ep.ep_list); | |
2f98382d KM |
909 | |
910 | /* init DCP */ | |
911 | if (usbhsg_is_dcp(uep)) { | |
912 | gpriv->gadget.ep0 = &uep->ep; | |
913 | uep->ep.maxpacket = 64; | |
914 | } | |
915 | /* init normal pipe */ | |
916 | else { | |
917 | uep->ep.maxpacket = 512; | |
918 | list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list); | |
919 | } | |
920 | } | |
921 | ||
922 | the_controller = gpriv; | |
923 | ||
0f91349b SAS |
924 | ret = usb_add_gadget_udc(dev, &gpriv->gadget); |
925 | if (ret) | |
926 | goto err_add_udc; | |
927 | ||
928 | ||
2f98382d KM |
929 | dev_info(dev, "gadget probed\n"); |
930 | ||
931 | return 0; | |
0f91349b SAS |
932 | err_add_udc: |
933 | kfree(gpriv->uep); | |
2f98382d KM |
934 | |
935 | usbhs_mod_gadget_probe_err_gpriv: | |
936 | kfree(gpriv); | |
937 | ||
0f91349b | 938 | return ret; |
2f98382d KM |
939 | } |
940 | ||
941 | void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv) | |
942 | { | |
943 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); | |
944 | ||
0f91349b | 945 | usb_del_gadget_udc(&gpriv->gadget); |
3af51ac9 | 946 | kfree(gpriv->uep); |
2f98382d KM |
947 | kfree(gpriv); |
948 | } |