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