blob: 4a1d1fcc90fd3cdd6136ecc002acdba3ee80203f [file] [log] [blame]
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001/*
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 */
27struct usbhsg_request {
28 struct usb_request req;
29 struct list_head node;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090030 struct usbhs_pkt pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090031};
32
33#define EP_NAME_SIZE 8
34struct usbhsg_gpriv;
35struct usbhsg_pipe_handle;
36struct usbhsg_uep {
37 struct usb_ep ep;
38 struct usbhs_pipe *pipe;
39 struct list_head list;
40
41 char ep_name[EP_NAME_SIZE];
42
43 struct usbhsg_gpriv *gpriv;
44 struct usbhsg_pipe_handle *handler;
45};
46
47struct usbhsg_gpriv {
48 struct usb_gadget gadget;
49 struct usbhs_mod mod;
50
51 struct usbhsg_uep *uep;
52 int uep_size;
53
54 struct usb_gadget_driver *driver;
55
56 u32 status;
57#define USBHSG_STATUS_STARTED (1 << 0)
58#define USBHSG_STATUS_REGISTERD (1 << 1)
59#define USBHSG_STATUS_WEDGE (1 << 2)
60};
61
62struct usbhsg_pipe_handle {
63 int (*prepare)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
64 int (*try_run)(struct usbhsg_uep *uep, struct usbhsg_request *ureq);
65 void (*irq_mask)(struct usbhsg_uep *uep, int enable);
66};
67
68struct usbhsg_recip_handle {
69 char *name;
70 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
71 struct usb_ctrlrequest *ctrl);
72 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
73 struct usb_ctrlrequest *ctrl);
74 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
75 struct usb_ctrlrequest *ctrl);
76};
77
78/*
79 * macro
80 */
81#define usbhsg_priv_to_gpriv(priv) \
82 container_of( \
83 usbhs_mod_get(priv, USBHS_GADGET), \
84 struct usbhsg_gpriv, mod)
85
86#define __usbhsg_for_each_uep(start, pos, g, i) \
87 for (i = start, pos = (g)->uep; \
88 i < (g)->uep_size; \
89 i++, pos = (g)->uep + i)
90
91#define usbhsg_for_each_uep(pos, gpriv, i) \
92 __usbhsg_for_each_uep(1, pos, gpriv, i)
93
94#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
95 __usbhsg_for_each_uep(0, pos, gpriv, i)
96
97#define usbhsg_gadget_to_gpriv(g)\
98 container_of(g, struct usbhsg_gpriv, gadget)
99
100#define usbhsg_req_to_ureq(r)\
101 container_of(r, struct usbhsg_request, req)
102
103#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
104#define usbhsg_gpriv_to_lock(gp) usbhs_priv_to_lock((gp)->mod.priv)
105#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
106#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
107#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
108#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
109#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
110#define usbhsg_uep_to_pipe(u) ((u)->pipe)
111#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
112#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
113
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900114#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
115#define usbhsg_pkt_to_ureq(i) \
116 container_of(i, struct usbhsg_request, pkt)
117
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900118#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
119
120/* status */
121#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
122#define usbhsg_status_set(gp, b) (gp->status |= b)
123#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
124#define usbhsg_status_has(gp, b) (gp->status & b)
125
126/*
Kuninori Morimotocb966322011-04-21 14:10:08 +0900127 * usbhsg_trylock
128 *
129 * This driver don't use spin_try_lock
130 * to avoid warning of CONFIG_DEBUG_SPINLOCK
131 */
132static spinlock_t *usbhsg_trylock(struct usbhsg_gpriv *gpriv,
133 unsigned long *flags)
134{
135 spinlock_t *lock = usbhsg_gpriv_to_lock(gpriv);
136
137 /* check spin lock status
138 * to avoid deadlock/nest */
139 if (spin_is_locked(lock))
140 return NULL;
141
142 spin_lock_irqsave(lock, *flags);
143
144 return lock;
145}
146
147static void usbhsg_unlock(spinlock_t *lock, unsigned long *flags)
148{
149 if (!lock)
150 return;
151
152 spin_unlock_irqrestore(lock, *flags);
153}
154
155/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900156 * list push/pop
157 */
158static void usbhsg_queue_push(struct usbhsg_uep *uep,
159 struct usbhsg_request *ureq)
160{
161 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
162 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
163 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
164
165 /*
166 ********* assume under spin lock *********
167 */
168 list_del_init(&ureq->node);
169 list_add_tail(&ureq->node, &uep->list);
170 ureq->req.actual = 0;
171 ureq->req.status = -EINPROGRESS;
172
173 dev_dbg(dev, "pipe %d : queue push (%d)\n",
174 usbhs_pipe_number(pipe),
175 ureq->req.length);
176}
177
178static struct usbhsg_request *usbhsg_queue_get(struct usbhsg_uep *uep)
179{
180 /*
181 ********* assume under spin lock *********
182 */
183 if (list_empty(&uep->list))
184 return NULL;
185
186 return list_entry(uep->list.next, struct usbhsg_request, node);
187}
188
189#define usbhsg_queue_prepare(uep) __usbhsg_queue_handler(uep, 1);
190#define usbhsg_queue_handle(uep) __usbhsg_queue_handler(uep, 0);
191static int __usbhsg_queue_handler(struct usbhsg_uep *uep, int prepare)
192{
193 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
194 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
195 struct usbhsg_request *ureq;
Kuninori Morimotocb966322011-04-21 14:10:08 +0900196 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900197 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900198 int ret = 0;
199
200 if (!uep->handler) {
201 dev_err(dev, "no handler function\n");
202 return -EIO;
203 }
204
205 /*
206 * CAUTION [*queue handler*]
207 *
208 * This function will be called for start/restart queue operation.
209 * OTOH the most much worry for USB driver is spinlock nest.
210 * Specially it are
211 * - usb_ep_ops :: queue
212 * - usb_request :: complete
213 *
214 * But the caller of this function need not care about spinlock.
Kuninori Morimotocb966322011-04-21 14:10:08 +0900215 * This function is using usbhsg_trylock for it.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900216 * if "is_locked" is 1, this mean this function lock it.
217 * but if it is 0, this mean it is already under spin lock.
218 * see also
219 * CAUTION [*endpoint queue*]
220 * CAUTION [*request complete*]
221 */
222
223 /****************** spin try lock *******************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900224 lock = usbhsg_trylock(gpriv, &flags);
225
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900226 ureq = usbhsg_queue_get(uep);
227 if (ureq) {
228 if (prepare)
229 ret = uep->handler->prepare(uep, ureq);
230 else
231 ret = uep->handler->try_run(uep, ureq);
232 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900233 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900234 /******************** spin unlock ******************/
235
236 return ret;
237}
238
239static void usbhsg_queue_pop(struct usbhsg_uep *uep,
240 struct usbhsg_request *ureq,
241 int status)
242{
243 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
244 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
245 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
246
247 /*
248 ********* assume under spin lock *********
249 */
250
251 /*
252 * CAUTION [*request complete*]
253 *
254 * There is a possibility not to be called in correct order
255 * if "complete" is called without spinlock.
256 *
257 * So, this function assume it is under spinlock,
258 * and call usb_request :: complete.
259 *
260 * But this "complete" will push next usb_request.
261 * It mean "usb_ep_ops :: queue" which is using spinlock is called
262 * under spinlock.
263 *
Kuninori Morimotocb966322011-04-21 14:10:08 +0900264 * To avoid dead-lock, this driver is using usbhsg_trylock.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900265 * CAUTION [*endpoint queue*]
266 * CAUTION [*queue handler*]
267 */
268
269 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
270
271 list_del_init(&ureq->node);
272
273 ureq->req.status = status;
274 ureq->req.complete(&uep->ep, &ureq->req);
275
276 /* more request ? */
277 if (0 == status)
278 usbhsg_queue_prepare(uep);
279}
280
281/*
282 * irq enable/disable function
283 */
284#define usbhsg_irq_callback_ctrl(uep, status, enable) \
285 ({ \
286 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); \
287 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); \
288 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); \
289 struct usbhs_mod *mod = usbhs_mod_get_current(priv); \
290 if (!mod) \
291 return; \
292 if (enable) \
293 mod->irq_##status |= (1 << usbhs_pipe_number(pipe)); \
294 else \
295 mod->irq_##status &= ~(1 << usbhs_pipe_number(pipe)); \
296 usbhs_irq_callback_update(priv, mod); \
297 })
298
299static void usbhsg_irq_empty_ctrl(struct usbhsg_uep *uep, int enable)
300{
301 usbhsg_irq_callback_ctrl(uep, bempsts, enable);
302}
303
304static void usbhsg_irq_ready_ctrl(struct usbhsg_uep *uep, int enable)
305{
306 usbhsg_irq_callback_ctrl(uep, brdysts, enable);
307}
308
309/*
310 * handler function
311 */
312static int usbhsg_try_run_ctrl_stage_end(struct usbhsg_uep *uep,
313 struct usbhsg_request *ureq)
314{
315 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
316
317 /*
318 ********* assume under spin lock *********
319 */
320
321 usbhs_dcp_control_transfer_done(pipe);
322 usbhsg_queue_pop(uep, ureq, 0);
323
324 return 0;
325}
326
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900327/*
328 * packet send hander
329 */
330static void usbhsg_try_run_send_packet_bh(struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900331{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900332 struct usbhs_pipe *pipe = pkt->pipe;
333 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
334 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900335 struct usb_request *req = &ureq->req;
336 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
337 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900338 int remainder, send, maxp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900339 int is_done = 0;
340 int enable;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900341
342 maxp = pkt->maxp;
343 send = pkt->actual;
344 remainder = pkt->length;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900345
346 /*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900347 * send = 0 : send zero packet
348 * send > 0 : send data
349 *
350 * send <= max_packet
351 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900352 req->actual += send;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900353
354 /* send all packet ? */
355 if (send < remainder)
356 is_done = 0; /* there are remainder data */
357 else if (send < maxp)
358 is_done = 1; /* short packet */
359 else
360 is_done = !req->zero; /* send zero packet ? */
361
362 dev_dbg(dev, " send %d (%d/ %d/ %d/ %d)\n",
363 usbhs_pipe_number(pipe),
364 remainder, send, is_done, req->zero);
365
366 /*
367 * enable interrupt and send again in irq handler
368 * if it still have remainder data which should be sent.
369 */
370 enable = !is_done;
371 uep->handler->irq_mask(uep, enable);
372
373 /*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900374 * all data were sent ?
375 */
376 if (is_done) {
377 /* it care below call in
378 "function mode" */
379 if (usbhsg_is_dcp(uep))
380 usbhs_dcp_control_transfer_done(pipe);
381
382 usbhsg_queue_pop(uep, ureq, 0);
383 }
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900384}
385
386static int usbhsg_try_run_send_packet(struct usbhsg_uep *uep,
387 struct usbhsg_request *ureq)
388{
389 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
390 struct usb_request *req = &ureq->req;
391 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
392 int ret;
393
394 /*
395 ********* assume under spin lock *********
396 */
397
398 usbhs_pkt_update(pkt, pipe,
399 req->buf + req->actual,
400 req->length - req->actual);
401
402 ret = usbhs_fifo_write(pkt);
403 if (ret < 0) {
404 /* pipe is busy.
405 * retry in interrupt */
406 uep->handler->irq_mask(uep, 1);
407 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900408
409 return 0;
410}
411
412static int usbhsg_prepare_send_packet(struct usbhsg_uep *uep,
413 struct usbhsg_request *ureq)
414{
415 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
416
417 /*
418 ********* assume under spin lock *********
419 */
420
421 usbhs_fifo_prepare_write(pipe);
422 usbhsg_try_run_send_packet(uep, ureq);
423
424 return 0;
425}
426
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900427/*
428 * packet recv hander
429 */
430static void usbhsg_try_run_receive_packet_bh(struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900431{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900432 struct usbhs_pipe *pipe = pkt->pipe;
433 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
434 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900435 struct usb_request *req = &ureq->req;
436 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
437 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900438 int remainder, recv, maxp;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900439 int is_done = 0;
440
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900441 maxp = pkt->maxp;
442 remainder = pkt->length;
443 recv = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900444
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900445 /*
446 * recv < 0 : pipe busy
447 * recv >= 0 : receive data
448 *
449 * recv <= max_packet
450 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900451
452 /* update parameters */
453 req->actual += recv;
454
455 if ((recv == remainder) || /* receive all data */
456 (recv < maxp)) /* short packet */
457 is_done = 1;
458
459 dev_dbg(dev, " recv %d (%d/ %d/ %d/ %d)\n",
460 usbhs_pipe_number(pipe),
461 remainder, recv, is_done, req->zero);
462
463 /* read all data ? */
464 if (is_done) {
465 int disable = 0;
466
467 uep->handler->irq_mask(uep, disable);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900468 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900469 usbhsg_queue_pop(uep, ureq, 0);
470 }
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900471}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900472
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900473static int usbhsg_try_run_receive_packet(struct usbhsg_uep *uep,
474 struct usbhsg_request *ureq)
475{
476 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
477 struct usb_request *req = &ureq->req;
478 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
479
480 /*
481 ********* assume under spin lock *********
482 */
483
484 usbhs_pkt_update(pkt, pipe,
485 req->buf + req->actual,
486 req->length - req->actual);
487
488 return usbhs_fifo_read(pkt);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900489}
490
491static int usbhsg_prepare_receive_packet(struct usbhsg_uep *uep,
492 struct usbhsg_request *ureq)
493{
494 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
495 int enable = 1;
496 int ret;
497
498 /*
499 ********* assume under spin lock *********
500 */
501
502 ret = usbhs_fifo_prepare_read(pipe);
503 if (ret < 0)
504 return ret;
505
506 /*
507 * data will be read in interrupt handler
508 */
509 uep->handler->irq_mask(uep, enable);
510
511 return ret;
512}
513
514static struct usbhsg_pipe_handle usbhsg_handler_send_by_empty = {
515 .prepare = usbhsg_prepare_send_packet,
516 .try_run = usbhsg_try_run_send_packet,
517 .irq_mask = usbhsg_irq_empty_ctrl,
518};
519
520static struct usbhsg_pipe_handle usbhsg_handler_send_by_ready = {
521 .prepare = usbhsg_prepare_send_packet,
522 .try_run = usbhsg_try_run_send_packet,
523 .irq_mask = usbhsg_irq_ready_ctrl,
524};
525
526static struct usbhsg_pipe_handle usbhsg_handler_recv_by_ready = {
527 .prepare = usbhsg_prepare_receive_packet,
528 .try_run = usbhsg_try_run_receive_packet,
529 .irq_mask = usbhsg_irq_ready_ctrl,
530};
531
532static struct usbhsg_pipe_handle usbhsg_handler_ctrl_stage_end = {
533 .prepare = usbhsg_try_run_ctrl_stage_end,
534 .try_run = usbhsg_try_run_ctrl_stage_end,
535};
536
537/*
538 * DCP pipe can NOT use "ready interrupt" for "send"
539 * it should use "empty" interrupt.
540 * see
541 * "Operation" - "Interrupt Function" - "BRDY Interrupt"
542 *
543 * on the other hand, normal pipe can use "ready interrupt" for "send"
544 * even though it is single/double buffer
545 */
546#define usbhsg_handler_send_ctrl usbhsg_handler_send_by_empty
547#define usbhsg_handler_recv_ctrl usbhsg_handler_recv_by_ready
548
549#define usbhsg_handler_send_packet usbhsg_handler_send_by_ready
550#define usbhsg_handler_recv_packet usbhsg_handler_recv_by_ready
551
552/*
553 * USB_TYPE_STANDARD / clear feature functions
554 */
555static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
556 struct usbhsg_uep *uep,
557 struct usb_ctrlrequest *ctrl)
558{
559 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
560 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
561 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
562
563 usbhs_dcp_control_transfer_done(pipe);
564
565 return 0;
566}
567
568static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
569 struct usbhsg_uep *uep,
570 struct usb_ctrlrequest *ctrl)
571{
572 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
573 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
574
575 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900576 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900577 usbhs_pipe_clear_sequence(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900578 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900579 }
580
581 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
582
583 usbhsg_queue_prepare(uep);
584
585 return 0;
586}
587
588struct usbhsg_recip_handle req_clear_feature = {
589 .name = "clear feature",
590 .device = usbhsg_recip_handler_std_control_done,
591 .interface = usbhsg_recip_handler_std_control_done,
592 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
593};
594
595/*
596 * USB_TYPE handler
597 */
598static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
599 struct usbhsg_recip_handle *handler,
600 struct usb_ctrlrequest *ctrl)
601{
602 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
603 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
604 struct usbhsg_uep *uep;
605 int recip = ctrl->bRequestType & USB_RECIP_MASK;
606 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
607 int ret;
608 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
609 struct usb_ctrlrequest *ctrl);
610 char *msg;
611
612 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900613 if (!usbhsg_uep_to_pipe(uep)) {
614 dev_err(dev, "wrong recip request\n");
615 return -EINVAL;
616 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900617
618 switch (recip) {
619 case USB_RECIP_DEVICE:
620 msg = "DEVICE";
621 func = handler->device;
622 break;
623 case USB_RECIP_INTERFACE:
624 msg = "INTERFACE";
625 func = handler->interface;
626 break;
627 case USB_RECIP_ENDPOINT:
628 msg = "ENDPOINT";
629 func = handler->endpoint;
630 break;
631 default:
632 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
633 func = NULL;
634 ret = -EINVAL;
635 }
636
637 if (func) {
638 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
639 ret = func(priv, uep, ctrl);
640 }
641
642 return ret;
643}
644
645/*
646 * irq functions
647 *
648 * it will be called from usbhs_interrupt
649 */
650static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
651 struct usbhs_irq_state *irq_state)
652{
653 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
654 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
655
656 gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state);
657
658 dev_dbg(dev, "state = %x : speed : %d\n",
659 usbhs_status_get_device_state(irq_state),
660 gpriv->gadget.speed);
661
662 return 0;
663}
664
665static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
666 struct usbhs_irq_state *irq_state)
667{
668 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
669 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
670 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
671 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
672 struct usb_ctrlrequest ctrl;
673 struct usbhsg_recip_handle *recip_handler = NULL;
674 int stage = usbhs_status_get_ctrl_stage(irq_state);
675 int ret = 0;
676
677 dev_dbg(dev, "stage = %d\n", stage);
678
679 /*
680 * see Manual
681 *
682 * "Operation"
683 * - "Interrupt Function"
684 * - "Control Transfer Stage Transition Interrupt"
685 * - Fig. "Control Transfer Stage Transitions"
686 */
687
688 switch (stage) {
689 case READ_DATA_STAGE:
690 dcp->handler = &usbhsg_handler_send_ctrl;
691 break;
692 case WRITE_DATA_STAGE:
693 dcp->handler = &usbhsg_handler_recv_ctrl;
694 break;
695 case NODATA_STATUS_STAGE:
696 dcp->handler = &usbhsg_handler_ctrl_stage_end;
697 break;
698 default:
699 return ret;
700 }
701
702 /*
703 * get usb request
704 */
705 usbhs_usbreq_get_val(priv, &ctrl);
706
707 switch (ctrl.bRequestType & USB_TYPE_MASK) {
708 case USB_TYPE_STANDARD:
709 switch (ctrl.bRequest) {
710 case USB_REQ_CLEAR_FEATURE:
711 recip_handler = &req_clear_feature;
712 break;
713 }
714 }
715
716 /*
717 * setup stage / run recip
718 */
719 if (recip_handler)
720 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
721 else
722 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
723
724 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900725 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900726
727 return ret;
728}
729
730static int usbhsg_irq_empty(struct usbhs_priv *priv,
731 struct usbhs_irq_state *irq_state)
732{
733 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
734 struct usbhsg_uep *uep;
735 struct usbhs_pipe *pipe;
736 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
737 int i, ret;
738
739 if (!irq_state->bempsts) {
740 dev_err(dev, "debug %s !!\n", __func__);
741 return -EIO;
742 }
743
744 dev_dbg(dev, "irq empty [0x%04x]\n", irq_state->bempsts);
745
746 /*
747 * search interrupted "pipe"
748 * not "uep".
749 */
750 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
751 if (!(irq_state->bempsts & (1 << i)))
752 continue;
753
754 uep = usbhsg_pipe_to_uep(pipe);
755 ret = usbhsg_queue_handle(uep);
756 if (ret < 0)
757 dev_err(dev, "send error %d : %d\n", i, ret);
758 }
759
760 return 0;
761}
762
763static int usbhsg_irq_ready(struct usbhs_priv *priv,
764 struct usbhs_irq_state *irq_state)
765{
766 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
767 struct usbhsg_uep *uep;
768 struct usbhs_pipe *pipe;
769 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
770 int i, ret;
771
772 if (!irq_state->brdysts) {
773 dev_err(dev, "debug %s !!\n", __func__);
774 return -EIO;
775 }
776
777 dev_dbg(dev, "irq ready [0x%04x]\n", irq_state->brdysts);
778
779 /*
780 * search interrupted "pipe"
781 * not "uep".
782 */
783 usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
784 if (!(irq_state->brdysts & (1 << i)))
785 continue;
786
787 uep = usbhsg_pipe_to_uep(pipe);
788 ret = usbhsg_queue_handle(uep);
789 if (ret < 0)
790 dev_err(dev, "receive error %d : %d\n", i, ret);
791 }
792
793 return 0;
794}
795
796/*
797 *
798 * usb_dcp_ops
799 *
800 */
801static int usbhsg_dcp_enable(struct usbhsg_uep *uep)
802{
803 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
804 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
805 struct usbhs_pipe *pipe;
806
807 /*
808 ********* assume under spin lock *********
809 */
810
811 pipe = usbhs_dcp_malloc(priv);
812 if (!pipe)
813 return -EIO;
814
815 uep->pipe = pipe;
816 uep->pipe->mod_private = uep;
817 INIT_LIST_HEAD(&uep->list);
818
819 return 0;
820}
821
822#define usbhsg_dcp_disable usbhsg_pipe_disable
823static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
824{
825 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
826 struct usbhsg_request *ureq;
827 int disable = 0;
828
829 /*
830 ********* assume under spin lock *********
831 */
832
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900833 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900834
835 /*
836 * disable pipe irq
837 */
838 usbhsg_irq_empty_ctrl(uep, disable);
839 usbhsg_irq_ready_ctrl(uep, disable);
840
841 while (1) {
842 ureq = usbhsg_queue_get(uep);
843 if (!ureq)
844 break;
845
846 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
847 }
848
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900849 return 0;
850}
851
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900852static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv)
853{
854 int i;
855 struct usbhsg_uep *uep;
856
857 usbhsg_for_each_uep_with_dcp(uep, gpriv, i)
858 uep->pipe = NULL;
859}
860
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900861/*
862 *
863 * usb_ep_ops
864 *
865 */
866static int usbhsg_ep_enable(struct usb_ep *ep,
867 const struct usb_endpoint_descriptor *desc)
868{
869 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
870 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
871 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
872 struct usbhs_pipe *pipe;
Kuninori Morimotocb966322011-04-21 14:10:08 +0900873 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900874 unsigned long flags;
875 int ret = -EIO;
876
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900877 /*
878 * if it already have pipe,
879 * nothing to do
880 */
881 if (uep->pipe)
882 return 0;
883
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900884 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900885 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900886
887 pipe = usbhs_pipe_malloc(priv, desc);
888 if (pipe) {
889 uep->pipe = pipe;
890 pipe->mod_private = uep;
891 INIT_LIST_HEAD(&uep->list);
892
893 if (usb_endpoint_dir_in(desc))
894 uep->handler = &usbhsg_handler_send_packet;
895 else
896 uep->handler = &usbhsg_handler_recv_packet;
897
898 ret = 0;
899 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900900
901 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900902 /******************** spin unlock ******************/
903
904 return ret;
905}
906
907static int usbhsg_ep_disable(struct usb_ep *ep)
908{
909 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
910 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900911 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900912 unsigned long flags;
913 int ret;
914
915 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900916 lock = usbhsg_trylock(gpriv, &flags);
917
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900918 ret = usbhsg_pipe_disable(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900919
920 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900921 /******************** spin unlock ******************/
922
923 return ret;
924}
925
926static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
927 gfp_t gfp_flags)
928{
929 struct usbhsg_request *ureq;
930
931 ureq = kzalloc(sizeof *ureq, gfp_flags);
932 if (!ureq)
933 return NULL;
934
935 INIT_LIST_HEAD(&ureq->node);
936 return &ureq->req;
937}
938
939static void usbhsg_ep_free_request(struct usb_ep *ep,
940 struct usb_request *req)
941{
942 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
943
944 WARN_ON(!list_empty(&ureq->node));
945 kfree(ureq);
946}
947
948static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
949 gfp_t gfp_flags)
950{
951 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
952 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
953 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
954 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +0900955 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900956 unsigned long flags;
957 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900958
959 /*
960 * CAUTION [*endpoint queue*]
961 *
962 * This function will be called from usb_request :: complete
963 * or usb driver timing.
964 * If this function is called from usb_request :: complete,
965 * it is already under spinlock on this driver.
966 * but it is called frm usb driver, this function should call spinlock.
967 *
Kuninori Morimotocb966322011-04-21 14:10:08 +0900968 * This function is using usbshg_trylock to solve this issue.
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900969 * if "is_locked" is 1, this mean this function lock it.
970 * but if it is 0, this mean it is already under spin lock.
971 * see also
972 * CAUTION [*queue handler*]
973 * CAUTION [*request complete*]
974 */
975
976 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +0900977 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900978
979 /* param check */
980 if (usbhsg_is_not_connected(gpriv) ||
981 unlikely(!gpriv->driver) ||
982 unlikely(!pipe))
983 ret = -ESHUTDOWN;
984 else
985 usbhsg_queue_push(uep, ureq);
986
Kuninori Morimotocb966322011-04-21 14:10:08 +0900987 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900988 /******************** spin unlock ******************/
989
990 usbhsg_queue_prepare(uep);
991
992 return ret;
993}
994
995static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
996{
997 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
998 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
999 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001000 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001001 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001002
1003 /*
1004 * see
1005 * CAUTION [*queue handler*]
1006 * CAUTION [*endpoint queue*]
1007 * CAUTION [*request complete*]
1008 */
1009
1010 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001011 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001012
1013 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
1014
Kuninori Morimotocb966322011-04-21 14:10:08 +09001015 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001016 /******************** spin unlock ******************/
1017
1018 return 0;
1019}
1020
1021static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
1022{
1023 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
1024 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
1025 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
1026 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001027 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001028 unsigned long flags;
1029 int ret = -EAGAIN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001030
1031 /*
1032 * see
1033 * CAUTION [*queue handler*]
1034 * CAUTION [*endpoint queue*]
1035 * CAUTION [*request complete*]
1036 */
1037
1038 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001039 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001040 if (!usbhsg_queue_get(uep)) {
1041
1042 dev_dbg(dev, "set halt %d (pipe %d)\n",
1043 halt, usbhs_pipe_number(pipe));
1044
1045 if (halt)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +09001046 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001047 else
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +09001048 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001049
1050 if (halt && wedge)
1051 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
1052 else
1053 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
1054
1055 ret = 0;
1056 }
1057
Kuninori Morimotocb966322011-04-21 14:10:08 +09001058 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001059 /******************** spin unlock ******************/
1060
1061 return ret;
1062}
1063
1064static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
1065{
1066 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
1067}
1068
1069static int usbhsg_ep_set_wedge(struct usb_ep *ep)
1070{
1071 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
1072}
1073
1074static struct usb_ep_ops usbhsg_ep_ops = {
1075 .enable = usbhsg_ep_enable,
1076 .disable = usbhsg_ep_disable,
1077
1078 .alloc_request = usbhsg_ep_alloc_request,
1079 .free_request = usbhsg_ep_free_request,
1080
1081 .queue = usbhsg_ep_queue,
1082 .dequeue = usbhsg_ep_dequeue,
1083
1084 .set_halt = usbhsg_ep_set_halt,
1085 .set_wedge = usbhsg_ep_set_wedge,
1086};
1087
1088/*
1089 * usb module start/end
1090 */
1091static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
1092{
1093 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1094 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1095 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1096 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001097 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001098 unsigned long flags;
1099
1100 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001101 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001102
1103 /*
1104 * enable interrupt and systems if ready
1105 */
1106 usbhsg_status_set(gpriv, status);
1107 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1108 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
1109 goto usbhsg_try_start_unlock;
1110
1111 dev_dbg(dev, "start gadget\n");
1112
1113 /*
1114 * pipe initialize and enable DCP
1115 */
Kuninori Morimoto4bd04812011-06-06 14:18:07 +09001116 usbhs_pipe_init(priv,
1117 usbhsg_try_run_send_packet_bh,
1118 usbhsg_try_run_receive_packet_bh);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +09001119 usbhsg_uep_init(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001120 usbhsg_dcp_enable(dcp);
1121
1122 /*
1123 * system config enble
1124 * - HI speed
1125 * - function
1126 * - usb module
1127 */
1128 usbhs_sys_hispeed_ctrl(priv, 1);
1129 usbhs_sys_function_ctrl(priv, 1);
1130 usbhs_sys_usb_ctrl(priv, 1);
1131
1132 /*
1133 * enable irq callback
1134 */
1135 mod->irq_dev_state = usbhsg_irq_dev_state;
1136 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
1137 mod->irq_empty = usbhsg_irq_empty;
1138 mod->irq_ready = usbhsg_irq_ready;
1139 mod->irq_bempsts = 0;
1140 mod->irq_brdysts = 0;
1141 usbhs_irq_callback_update(priv, mod);
1142
1143usbhsg_try_start_unlock:
Kuninori Morimotocb966322011-04-21 14:10:08 +09001144 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001145 /******************** spin unlock ********************/
1146
1147 return 0;
1148}
1149
1150static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
1151{
1152 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1153 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
1154 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
1155 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimotocb966322011-04-21 14:10:08 +09001156 spinlock_t *lock;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001157 unsigned long flags;
1158
1159 /******************** spin lock ********************/
Kuninori Morimotocb966322011-04-21 14:10:08 +09001160 lock = usbhsg_trylock(gpriv, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001161
1162 /*
1163 * disable interrupt and systems if 1st try
1164 */
1165 usbhsg_status_clr(gpriv, status);
1166 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
1167 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
1168 goto usbhsg_try_stop_unlock;
1169
1170 /* disable all irq */
1171 mod->irq_dev_state = NULL;
1172 mod->irq_ctrl_stage = NULL;
1173 mod->irq_empty = NULL;
1174 mod->irq_ready = NULL;
1175 mod->irq_bempsts = 0;
1176 mod->irq_brdysts = 0;
1177 usbhs_irq_callback_update(priv, mod);
1178
1179 usbhsg_dcp_disable(dcp);
1180
1181 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
1182
1183 /* disable sys */
1184 usbhs_sys_hispeed_ctrl(priv, 0);
1185 usbhs_sys_function_ctrl(priv, 0);
1186 usbhs_sys_usb_ctrl(priv, 0);
1187
Kuninori Morimotocb966322011-04-21 14:10:08 +09001188 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001189 /******************** spin unlock ********************/
1190
1191 if (gpriv->driver &&
1192 gpriv->driver->disconnect)
1193 gpriv->driver->disconnect(&gpriv->gadget);
1194
1195 dev_dbg(dev, "stop gadget\n");
1196
1197 return 0;
1198
1199usbhsg_try_stop_unlock:
Kuninori Morimotocb966322011-04-21 14:10:08 +09001200 usbhsg_unlock(lock, &flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001201
1202 return 0;
1203}
1204
1205/*
1206 *
1207 * linux usb function
1208 *
1209 */
1210struct usbhsg_gpriv *the_controller;
1211int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1212 int (*bind)(struct usb_gadget *))
1213{
1214 struct usbhsg_gpriv *gpriv = the_controller;
1215 struct usbhs_priv *priv;
1216 struct device *dev;
1217 int ret;
1218
1219 if (!bind ||
1220 !driver ||
1221 !driver->setup ||
1222 driver->speed != USB_SPEED_HIGH)
1223 return -EINVAL;
1224 if (!gpriv)
1225 return -ENODEV;
1226 if (gpriv->driver)
1227 return -EBUSY;
1228
1229 dev = usbhsg_gpriv_to_dev(gpriv);
1230 priv = usbhsg_gpriv_to_priv(gpriv);
1231
1232 /* first hook up the driver ... */
1233 gpriv->driver = driver;
1234 gpriv->gadget.dev.driver = &driver->driver;
1235
1236 ret = device_add(&gpriv->gadget.dev);
1237 if (ret) {
1238 dev_err(dev, "device_add error %d\n", ret);
1239 goto add_fail;
1240 }
1241
1242 ret = bind(&gpriv->gadget);
1243 if (ret) {
1244 dev_err(dev, "bind to driver %s error %d\n",
1245 driver->driver.name, ret);
1246 goto bind_fail;
1247 }
1248
1249 dev_dbg(dev, "bind %s\n", driver->driver.name);
1250
1251 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
1252
1253bind_fail:
1254 device_del(&gpriv->gadget.dev);
1255add_fail:
1256 gpriv->driver = NULL;
1257 gpriv->gadget.dev.driver = NULL;
1258
1259 return ret;
1260}
1261EXPORT_SYMBOL(usb_gadget_probe_driver);
1262
1263int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1264{
1265 struct usbhsg_gpriv *gpriv = the_controller;
1266 struct usbhs_priv *priv;
1267 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
1268
1269 if (!gpriv)
1270 return -ENODEV;
1271
1272 if (!driver ||
1273 !driver->unbind ||
1274 driver != gpriv->driver)
1275 return -EINVAL;
1276
1277 dev = usbhsg_gpriv_to_dev(gpriv);
1278 priv = usbhsg_gpriv_to_priv(gpriv);
1279
1280 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
1281 device_del(&gpriv->gadget.dev);
1282 gpriv->driver = NULL;
1283
1284 if (driver->disconnect)
1285 driver->disconnect(&gpriv->gadget);
1286
1287 driver->unbind(&gpriv->gadget);
1288 dev_dbg(dev, "unbind %s\n", driver->driver.name);
1289
1290 return 0;
1291}
1292EXPORT_SYMBOL(usb_gadget_unregister_driver);
1293
1294/*
1295 * usb gadget ops
1296 */
1297static int usbhsg_get_frame(struct usb_gadget *gadget)
1298{
1299 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1300 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1301
1302 return usbhs_frame_get_num(priv);
1303}
1304
1305static struct usb_gadget_ops usbhsg_gadget_ops = {
1306 .get_frame = usbhsg_get_frame,
1307};
1308
1309static int usbhsg_start(struct usbhs_priv *priv)
1310{
1311 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1312}
1313
1314static int usbhsg_stop(struct usbhs_priv *priv)
1315{
1316 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1317}
1318
1319int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv)
1320{
1321 struct usbhsg_gpriv *gpriv;
1322 struct usbhsg_uep *uep;
1323 struct device *dev = usbhs_priv_to_dev(priv);
1324 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1325 int i;
1326
1327 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1328 if (!gpriv) {
1329 dev_err(dev, "Could not allocate gadget priv\n");
1330 return -ENOMEM;
1331 }
1332
1333 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1334 if (!uep) {
1335 dev_err(dev, "Could not allocate ep\n");
1336 goto usbhs_mod_gadget_probe_err_gpriv;
1337 }
1338
1339 /*
1340 * CAUTION
1341 *
1342 * There is no guarantee that it is possible to access usb module here.
1343 * Don't accesses to it.
1344 * The accesse will be enable after "usbhsg_start"
1345 */
1346
1347 /*
1348 * register itself
1349 */
1350 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1351
1352 /* init gpriv */
1353 gpriv->mod.name = "gadget";
1354 gpriv->mod.start = usbhsg_start;
1355 gpriv->mod.stop = usbhsg_stop;
1356 gpriv->uep = uep;
1357 gpriv->uep_size = pipe_size;
1358 usbhsg_status_init(gpriv);
1359
1360 /*
1361 * init gadget
1362 */
1363 device_initialize(&gpriv->gadget.dev);
1364 dev_set_name(&gpriv->gadget.dev, "gadget");
1365 gpriv->gadget.dev.parent = dev;
1366 gpriv->gadget.name = "renesas_usbhs_udc";
1367 gpriv->gadget.ops = &usbhsg_gadget_ops;
1368 gpriv->gadget.is_dualspeed = 1;
1369
1370 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1371
1372 /*
1373 * init usb_ep
1374 */
1375 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1376 uep->gpriv = gpriv;
1377 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1378
1379 uep->ep.name = uep->ep_name;
1380 uep->ep.ops = &usbhsg_ep_ops;
1381 INIT_LIST_HEAD(&uep->ep.ep_list);
1382 INIT_LIST_HEAD(&uep->list);
1383
1384 /* init DCP */
1385 if (usbhsg_is_dcp(uep)) {
1386 gpriv->gadget.ep0 = &uep->ep;
1387 uep->ep.maxpacket = 64;
1388 }
1389 /* init normal pipe */
1390 else {
1391 uep->ep.maxpacket = 512;
1392 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1393 }
1394 }
1395
1396 the_controller = gpriv;
1397
1398 dev_info(dev, "gadget probed\n");
1399
1400 return 0;
1401
1402usbhs_mod_gadget_probe_err_gpriv:
1403 kfree(gpriv);
1404
1405 return -ENOMEM;
1406}
1407
1408void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv)
1409{
1410 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1411
1412 kfree(gpriv);
1413}