blob: 657f9672ceba732140a1544a02a8fe71906fe1fd [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 */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -080017#include <linux/delay.h>
Kuninori Morimotod128a2592011-08-03 21:41:26 -070018#include <linux/dma-mapping.h>
Kuninori Morimoto2f983822011-04-05 11:40:54 +090019#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
Phil Edworthyb5a28752015-07-13 16:30:18 +010024#include <linux/usb/otg.h>
Kuninori Morimoto2f983822011-04-05 11:40:54 +090025#include "common.h"
26
27/*
28 * struct
29 */
30struct usbhsg_request {
31 struct usb_request req;
Kuninori Morimoto4bd04812011-06-06 14:18:07 +090032 struct usbhs_pkt pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090033};
34
35#define EP_NAME_SIZE 8
36struct usbhsg_gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090037struct usbhsg_uep {
38 struct usb_ep ep;
39 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090040
41 char ep_name[EP_NAME_SIZE];
42
43 struct usbhsg_gpriv *gpriv;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090044};
45
46struct usbhsg_gpriv {
47 struct usb_gadget gadget;
48 struct usbhs_mod mod;
49
50 struct usbhsg_uep *uep;
51 int uep_size;
52
53 struct usb_gadget_driver *driver;
Phil Edworthyb5a28752015-07-13 16:30:18 +010054 struct usb_phy *transceiver;
55 bool vbus_active;
Kuninori Morimoto2f983822011-04-05 11:40:54 +090056
57 u32 status;
58#define USBHSG_STATUS_STARTED (1 << 0)
59#define USBHSG_STATUS_REGISTERD (1 << 1)
60#define USBHSG_STATUS_WEDGE (1 << 2)
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +090061#define USBHSG_STATUS_SELF_POWERED (1 << 3)
Takeshi Kihara04a5def2014-11-04 10:05:43 +090062#define USBHSG_STATUS_SOFT_CONNECT (1 << 4)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090063};
64
Kuninori Morimoto2f983822011-04-05 11:40:54 +090065struct usbhsg_recip_handle {
66 char *name;
67 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
68 struct usb_ctrlrequest *ctrl);
69 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
70 struct usb_ctrlrequest *ctrl);
71 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
72 struct usb_ctrlrequest *ctrl);
73};
74
75/*
76 * macro
77 */
78#define usbhsg_priv_to_gpriv(priv) \
79 container_of( \
80 usbhs_mod_get(priv, USBHS_GADGET), \
81 struct usbhsg_gpriv, mod)
82
83#define __usbhsg_for_each_uep(start, pos, g, i) \
Kuninori Morimoto925403f2013-07-11 22:32:31 -070084 for ((i) = start; \
85 ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \
86 (i)++)
Kuninori Morimoto2f983822011-04-05 11:40:54 +090087
88#define usbhsg_for_each_uep(pos, gpriv, i) \
89 __usbhsg_for_each_uep(1, pos, gpriv, i)
90
91#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \
92 __usbhsg_for_each_uep(0, pos, gpriv, i)
93
94#define usbhsg_gadget_to_gpriv(g)\
95 container_of(g, struct usbhsg_gpriv, gadget)
96
97#define usbhsg_req_to_ureq(r)\
98 container_of(r, struct usbhsg_request, req)
99
100#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900101#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv)
102#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv)
103#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep)
104#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i)
105#define usbhsg_uep_to_gpriv(u) ((u)->gpriv)
106#define usbhsg_uep_to_pipe(u) ((u)->pipe)
107#define usbhsg_pipe_to_uep(p) ((p)->mod_private)
108#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv))
109
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900110#define usbhsg_ureq_to_pkt(u) (&(u)->pkt)
111#define usbhsg_pkt_to_ureq(i) \
112 container_of(i, struct usbhsg_request, pkt)
113
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900114#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN)
115
116/* status */
117#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0)
118#define usbhsg_status_set(gp, b) (gp->status |= b)
119#define usbhsg_status_clr(gp, b) (gp->status &= ~b)
120#define usbhsg_status_has(gp, b) (gp->status & b)
121
122/*
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700123 * queue push/pop
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900124 */
Yoshihiro Shimoda00f30d22015-03-16 16:36:48 +0900125static void __usbhsg_queue_pop(struct usbhsg_uep *uep,
126 struct usbhsg_request *ureq,
127 int status)
128{
129 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
130 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
131 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
132 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
133
Yoshihiro Shimodac9eb2952015-11-18 14:33:35 +0900134 if (pipe)
135 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe));
Yoshihiro Shimoda00f30d22015-03-16 16:36:48 +0900136
137 ureq->req.status = status;
138 spin_unlock(usbhs_priv_to_lock(priv));
139 usb_gadget_giveback_request(&uep->ep, &ureq->req);
140 spin_lock(usbhs_priv_to_lock(priv));
141}
142
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900143static void usbhsg_queue_pop(struct usbhsg_uep *uep,
144 struct usbhsg_request *ureq,
145 int status)
146{
147 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Yoshihiro Shimoda00f30d22015-03-16 16:36:48 +0900148 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
149 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900150
Yoshihiro Shimoda00f30d22015-03-16 16:36:48 +0900151 usbhs_lock(priv, flags);
152 __usbhsg_queue_pop(uep, ureq, status);
153 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900154}
155
Kuninori Morimoto2cc97192011-10-10 22:03:39 -0700156static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900157{
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900158 struct usbhs_pipe *pipe = pkt->pipe;
159 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
160 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900161
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900162 ureq->req.actual = pkt->actual;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900163
Kuninori Morimoto659d4952011-06-06 14:18:23 +0900164 usbhsg_queue_pop(uep, ureq, 0);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900165}
166
Kuninori Morimotob3318722011-10-10 22:04:41 -0700167static void usbhsg_queue_push(struct usbhsg_uep *uep,
168 struct usbhsg_request *ureq)
169{
170 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
171 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
172 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
173 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq);
174 struct usb_request *req = &ureq->req;
175
176 req->actual = 0;
177 req->status = -EINPROGRESS;
178 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done,
Kuninori Morimoto3edeee32011-12-08 18:28:54 -0800179 req->buf, req->length, req->zero, -1);
Kuninori Morimoto654c35a2011-10-10 22:04:53 -0700180 usbhs_pkt_start(pipe);
Kuninori Morimotob3318722011-10-10 22:04:41 -0700181
182 dev_dbg(dev, "pipe %d : queue push (%d)\n",
183 usbhs_pipe_number(pipe),
184 req->length);
185}
186
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700187/*
188 * dma map/unmap
189 */
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900190static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map)
191{
Felipe Balbiade78f92011-12-19 11:57:16 +0200192 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt);
193 struct usb_request *req = &ureq->req;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900194 struct usbhs_pipe *pipe = pkt->pipe;
195 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe);
196 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900197 enum dma_data_direction dir;
Felipe Balbiade78f92011-12-19 11:57:16 +0200198 int ret = 0;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900199
Felipe Balbiade78f92011-12-19 11:57:16 +0200200 dir = usbhs_pipe_is_dir_host(pipe);
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900201
Felipe Balbiade78f92011-12-19 11:57:16 +0200202 if (map) {
203 /* it can not use scatter/gather */
204 WARN_ON(req->num_sgs);
205
206 ret = usb_gadget_map_request(&gpriv->gadget, req, dir);
207 if (ret < 0)
208 return ret;
209
210 pkt->dma = req->dma;
211 } else {
212 usb_gadget_unmap_request(&gpriv->gadget, req, dir);
213 }
214
215 return ret;
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900216}
217
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900218/*
219 * USB_TYPE_STANDARD / clear feature functions
220 */
221static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv,
222 struct usbhsg_uep *uep,
223 struct usb_ctrlrequest *ctrl)
224{
225 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
226 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
227 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
228
229 usbhs_dcp_control_transfer_done(pipe);
230
231 return 0;
232}
233
234static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv,
235 struct usbhsg_uep *uep,
236 struct usb_ctrlrequest *ctrl)
237{
238 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
239 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
240
241 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) {
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900242 usbhs_pipe_disable(pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700243 usbhs_pipe_sequence_data0(pipe);
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900244 usbhs_pipe_enable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900245 }
246
247 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
248
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800249 usbhs_pkt_start(pipe);
250
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900251 return 0;
252}
253
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700254static struct usbhsg_recip_handle req_clear_feature = {
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900255 .name = "clear feature",
256 .device = usbhsg_recip_handler_std_control_done,
257 .interface = usbhsg_recip_handler_std_control_done,
258 .endpoint = usbhsg_recip_handler_std_clear_endpoint,
259};
260
261/*
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800262 * USB_TYPE_STANDARD / set feature functions
263 */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800264static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv,
265 struct usbhsg_uep *uep,
266 struct usb_ctrlrequest *ctrl)
267{
268 switch (le16_to_cpu(ctrl->wValue)) {
269 case USB_DEVICE_TEST_MODE:
270 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
271 udelay(100);
272 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8));
273 break;
274 default:
275 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
276 break;
277 }
278
279 return 0;
280}
281
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800282static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv,
283 struct usbhsg_uep *uep,
284 struct usb_ctrlrequest *ctrl)
285{
286 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
287
288 usbhs_pipe_stall(pipe);
289
290 usbhsg_recip_handler_std_control_done(priv, uep, ctrl);
291
292 return 0;
293}
294
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700295static struct usbhsg_recip_handle req_set_feature = {
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800296 .name = "set feature",
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800297 .device = usbhsg_recip_handler_std_set_device,
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800298 .interface = usbhsg_recip_handler_std_control_done,
299 .endpoint = usbhsg_recip_handler_std_set_endpoint,
300};
301
302/*
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800303 * USB_TYPE_STANDARD / get status functions
304 */
305static void __usbhsg_recip_send_complete(struct usb_ep *ep,
306 struct usb_request *req)
307{
308 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
309
310 /* free allocated recip-buffer/usb_request */
311 kfree(ureq->pkt.buf);
312 usb_ep_free_request(ep, req);
313}
314
315static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv,
316 unsigned short status)
317{
318 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
319 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
320 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
321 struct usb_request *req;
322 unsigned short *buf;
323
324 /* alloc new usb_request for recip */
325 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC);
326 if (!req) {
327 dev_err(dev, "recip request allocation fail\n");
328 return;
329 }
330
331 /* alloc recip data buffer */
332 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
333 if (!buf) {
334 usb_ep_free_request(&dcp->ep, req);
335 dev_err(dev, "recip data allocation fail\n");
336 return;
337 }
338
339 /* recip data is status */
340 *buf = cpu_to_le16(status);
341
342 /* allocated usb_request/buffer will be freed */
343 req->complete = __usbhsg_recip_send_complete;
344 req->buf = buf;
345 req->length = sizeof(*buf);
346 req->zero = 0;
347
348 /* push packet */
349 pipe->handler = &usbhs_fifo_pio_push_handler;
350 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req));
351}
352
353static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv,
354 struct usbhsg_uep *uep,
355 struct usb_ctrlrequest *ctrl)
356{
357 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900358 unsigned short status = 0;
359
360 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED))
361 status = 1 << USB_DEVICE_SELF_POWERED;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800362
363 __usbhsg_recip_send_status(gpriv, status);
364
365 return 0;
366}
367
368static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv,
369 struct usbhsg_uep *uep,
370 struct usb_ctrlrequest *ctrl)
371{
372 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
373 unsigned short status = 0;
374
375 __usbhsg_recip_send_status(gpriv, status);
376
377 return 0;
378}
379
380static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv,
381 struct usbhsg_uep *uep,
382 struct usb_ctrlrequest *ctrl)
383{
384 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
385 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
386 unsigned short status = 0;
387
388 if (usbhs_pipe_is_stall(pipe))
389 status = 1 << USB_ENDPOINT_HALT;
390
391 __usbhsg_recip_send_status(gpriv, status);
392
393 return 0;
394}
395
Kuninori Morimoto225da3e2013-03-31 18:34:43 -0700396static struct usbhsg_recip_handle req_get_status = {
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800397 .name = "get status",
398 .device = usbhsg_recip_handler_std_get_device,
399 .interface = usbhsg_recip_handler_std_get_interface,
400 .endpoint = usbhsg_recip_handler_std_get_endpoint,
401};
402
403/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900404 * USB_TYPE handler
405 */
406static int usbhsg_recip_run_handle(struct usbhs_priv *priv,
407 struct usbhsg_recip_handle *handler,
408 struct usb_ctrlrequest *ctrl)
409{
410 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
411 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
412 struct usbhsg_uep *uep;
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900413 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900414 int recip = ctrl->bRequestType & USB_RECIP_MASK;
415 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
Jesper Juhl7983bc72012-01-16 22:42:10 +0100416 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900417 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep,
418 struct usb_ctrlrequest *ctrl);
419 char *msg;
420
421 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth);
Kuninori Morimoto0432eed2011-06-06 14:18:54 +0900422 pipe = usbhsg_uep_to_pipe(uep);
423 if (!pipe) {
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900424 dev_err(dev, "wrong recip request\n");
Kuninori Morimoto25fa7072011-11-24 17:28:17 -0800425 return -EINVAL;
Kuninori Morimoto9a28b7b2011-04-21 14:10:12 +0900426 }
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900427
428 switch (recip) {
429 case USB_RECIP_DEVICE:
430 msg = "DEVICE";
431 func = handler->device;
432 break;
433 case USB_RECIP_INTERFACE:
434 msg = "INTERFACE";
435 func = handler->interface;
436 break;
437 case USB_RECIP_ENDPOINT:
438 msg = "ENDPOINT";
439 func = handler->endpoint;
440 break;
441 default:
442 dev_warn(dev, "unsupported RECIP(%d)\n", recip);
443 func = NULL;
444 ret = -EINVAL;
445 }
446
447 if (func) {
448 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg);
449 ret = func(priv, uep, ctrl);
450 }
451
452 return ret;
453}
454
455/*
456 * irq functions
457 *
458 * it will be called from usbhs_interrupt
459 */
460static int usbhsg_irq_dev_state(struct usbhs_priv *priv,
461 struct usbhs_irq_state *irq_state)
462{
463 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
464 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
465
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700466 gpriv->gadget.speed = usbhs_bus_get_speed(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900467
468 dev_dbg(dev, "state = %x : speed : %d\n",
469 usbhs_status_get_device_state(irq_state),
470 gpriv->gadget.speed);
471
472 return 0;
473}
474
475static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv,
476 struct usbhs_irq_state *irq_state)
477{
478 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
479 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
480 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp);
481 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
482 struct usb_ctrlrequest ctrl;
483 struct usbhsg_recip_handle *recip_handler = NULL;
484 int stage = usbhs_status_get_ctrl_stage(irq_state);
485 int ret = 0;
486
487 dev_dbg(dev, "stage = %d\n", stage);
488
489 /*
490 * see Manual
491 *
492 * "Operation"
493 * - "Interrupt Function"
494 * - "Control Transfer Stage Transition Interrupt"
495 * - Fig. "Control Transfer Stage Transitions"
496 */
497
498 switch (stage) {
499 case READ_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700500 pipe->handler = &usbhs_fifo_pio_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900501 break;
502 case WRITE_DATA_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700503 pipe->handler = &usbhs_fifo_pio_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900504 break;
505 case NODATA_STATUS_STAGE:
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700506 pipe->handler = &usbhs_ctrl_stage_end_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900507 break;
Yoshihiro Shimoda4ef35b12014-11-04 10:05:44 +0900508 case READ_STATUS_STAGE:
509 case WRITE_STATUS_STAGE:
510 usbhs_dcp_control_transfer_done(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900511 default:
512 return ret;
513 }
514
515 /*
516 * get usb request
517 */
518 usbhs_usbreq_get_val(priv, &ctrl);
519
520 switch (ctrl.bRequestType & USB_TYPE_MASK) {
521 case USB_TYPE_STANDARD:
522 switch (ctrl.bRequest) {
523 case USB_REQ_CLEAR_FEATURE:
524 recip_handler = &req_clear_feature;
525 break;
Kuninori Morimotoced6e092011-11-24 17:27:50 -0800526 case USB_REQ_SET_FEATURE:
527 recip_handler = &req_set_feature;
528 break;
Kuninori Morimoto17f7f762011-11-24 17:28:04 -0800529 case USB_REQ_GET_STATUS:
530 recip_handler = &req_get_status;
531 break;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900532 }
533 }
534
535 /*
536 * setup stage / run recip
537 */
538 if (recip_handler)
539 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl);
540 else
541 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl);
542
543 if (ret < 0)
Kuninori Morimotoe8d548d2011-06-06 14:18:03 +0900544 usbhs_pipe_stall(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900545
546 return ret;
547}
548
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900549/*
550 *
551 * usb_dcp_ops
552 *
553 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900554static int usbhsg_pipe_disable(struct usbhsg_uep *uep)
555{
556 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900557 struct usbhs_pkt *pkt;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900558
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900559 while (1) {
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900560 pkt = usbhs_pkt_pop(pipe, NULL);
Kuninori Morimoto8a2c2252011-06-06 14:18:33 +0900561 if (!pkt)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900562 break;
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800563
564 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900565 }
566
Kuninori Morimoto91b158f2011-11-24 17:28:26 -0800567 usbhs_pipe_disable(pipe);
568
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900569 return 0;
570}
571
572/*
573 *
574 * usb_ep_ops
575 *
576 */
577static int usbhsg_ep_enable(struct usb_ep *ep,
578 const struct usb_endpoint_descriptor *desc)
579{
580 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
581 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
582 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
583 struct usbhs_pipe *pipe;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900584 int ret = -EIO;
585
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900586 /*
587 * if it already have pipe,
588 * nothing to do
589 */
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900590 if (uep->pipe) {
591 usbhs_pipe_clear(uep->pipe);
Kuninori Morimoto6e6db822011-10-10 22:05:30 -0700592 usbhs_pipe_sequence_data0(uep->pipe);
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900593 return 0;
Kuninori Morimoto08e6c612011-06-09 16:48:25 +0900594 }
Kuninori Morimoto409ba9e2011-04-26 09:21:35 +0900595
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700596 pipe = usbhs_pipe_malloc(priv,
597 usb_endpoint_type(desc),
598 usb_endpoint_dir_in(desc));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900599 if (pipe) {
600 uep->pipe = pipe;
601 pipe->mod_private = uep;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900602
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700603 /* set epnum / maxp */
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700604 usbhs_pipe_config_update(pipe, 0,
Kuninori Morimotof5aa8892011-10-10 21:59:46 -0700605 usb_endpoint_num(desc),
606 usb_endpoint_maxp(desc));
607
Kuninori Morimoto233f5192011-07-07 00:23:24 -0700608 /*
609 * usbhs_fifo_dma_push/pop_handler try to
610 * use dmaengine if possible.
611 * It will use pio handler if impossible.
612 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900613 if (usb_endpoint_dir_in(desc))
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700614 pipe->handler = &usbhs_fifo_dma_push_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900615 else
Kuninori Morimoto0c6ef982011-10-10 22:00:59 -0700616 pipe->handler = &usbhs_fifo_dma_pop_handler;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900617
618 ret = 0;
619 }
Kuninori Morimotocb966322011-04-21 14:10:08 +0900620
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900621 return ret;
622}
623
624static int usbhsg_ep_disable(struct usb_ep *ep)
625{
626 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
Yoshihiro Shimodadfb87b82014-07-09 20:30:13 +0900627 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900628
Kazuya Mizuguchi11432052014-11-04 10:05:42 +0900629 if (!pipe)
630 return -EINVAL;
631
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800632 usbhsg_pipe_disable(uep);
Yoshihiro Shimodadfb87b82014-07-09 20:30:13 +0900633 usbhs_pipe_free(pipe);
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800634
635 uep->pipe->mod_private = NULL;
636 uep->pipe = NULL;
637
638 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900639}
640
641static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep,
642 gfp_t gfp_flags)
643{
644 struct usbhsg_request *ureq;
645
646 ureq = kzalloc(sizeof *ureq, gfp_flags);
647 if (!ureq)
648 return NULL;
649
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900650 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq));
651
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900652 return &ureq->req;
653}
654
655static void usbhsg_ep_free_request(struct usb_ep *ep,
656 struct usb_request *req)
657{
658 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
659
Kuninori Morimoto6acb95d2011-06-06 14:18:16 +0900660 WARN_ON(!list_empty(&ureq->pkt.node));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900661 kfree(ureq);
662}
663
664static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req,
665 gfp_t gfp_flags)
666{
667 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
668 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
669 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
670 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900671
672 /* param check */
673 if (usbhsg_is_not_connected(gpriv) ||
674 unlikely(!gpriv->driver) ||
675 unlikely(!pipe))
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900676 return -ESHUTDOWN;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900677
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900678 usbhsg_queue_push(uep, ureq);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900679
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900680 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900681}
682
683static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
684{
685 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
686 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900687 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900688
Yoshihiro Shimodac9eb2952015-11-18 14:33:35 +0900689 if (pipe)
690 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq));
691
692 /*
693 * To dequeue a request, this driver should call the usbhsg_queue_pop()
694 * even if the pipe is NULL.
695 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900696 usbhsg_queue_pop(uep, ureq, -ECONNRESET);
697
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900698 return 0;
699}
700
701static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge)
702{
703 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep);
704 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep);
705 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900706 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900707 struct device *dev = usbhsg_gpriv_to_dev(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900708 unsigned long flags;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900709
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900710 usbhsg_pipe_disable(uep);
711
712 dev_dbg(dev, "set halt %d (pipe %d)\n",
713 halt, usbhs_pipe_number(pipe));
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900714
715 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900716 usbhs_lock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900717
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900718 if (halt)
719 usbhs_pipe_stall(pipe);
720 else
721 usbhs_pipe_disable(pipe);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900722
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900723 if (halt && wedge)
724 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE);
725 else
726 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900727
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900728 usbhs_unlock(priv, flags);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900729 /******************** spin unlock ******************/
730
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900731 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900732}
733
734static int usbhsg_ep_set_halt(struct usb_ep *ep, int value)
735{
736 return __usbhsg_ep_set_halt_wedge(ep, value, 0);
737}
738
739static int usbhsg_ep_set_wedge(struct usb_ep *ep)
740{
741 return __usbhsg_ep_set_halt_wedge(ep, 1, 1);
742}
743
744static struct usb_ep_ops usbhsg_ep_ops = {
745 .enable = usbhsg_ep_enable,
746 .disable = usbhsg_ep_disable,
747
748 .alloc_request = usbhsg_ep_alloc_request,
749 .free_request = usbhsg_ep_free_request,
750
751 .queue = usbhsg_ep_queue,
752 .dequeue = usbhsg_ep_dequeue,
753
754 .set_halt = usbhsg_ep_set_halt,
755 .set_wedge = usbhsg_ep_set_wedge,
756};
757
758/*
Takeshi Kihara04a5def2014-11-04 10:05:43 +0900759 * pullup control
760 */
761static int usbhsg_can_pullup(struct usbhs_priv *priv)
762{
763 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
764
765 return gpriv->driver &&
766 usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT);
767}
768
769static void usbhsg_update_pullup(struct usbhs_priv *priv)
770{
771 if (usbhsg_can_pullup(priv))
772 usbhs_sys_function_pullup(priv, 1);
773 else
774 usbhs_sys_function_pullup(priv, 0);
775}
776
777/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900778 * usb module start/end
779 */
780static int usbhsg_try_start(struct usbhs_priv *priv, u32 status)
781{
782 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
783 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
784 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
785 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900786 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900787 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900788
789 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900790 usbhs_lock(priv, flags);
791
792 usbhsg_status_set(gpriv, status);
793 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
794 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)))
795 ret = -1; /* not ready */
796
797 usbhs_unlock(priv, flags);
798 /******************** spin unlock ********************/
799
800 if (ret < 0)
801 return 0; /* not ready is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900802
803 /*
804 * enable interrupt and systems if ready
805 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900806 dev_dbg(dev, "start gadget\n");
807
808 /*
809 * pipe initialize and enable DCP
810 */
Yoshihiro Shimodacdeb7942014-11-04 10:05:45 +0900811 usbhs_fifo_init(priv);
Kuninori Morimoto4bd04812011-06-06 14:18:07 +0900812 usbhs_pipe_init(priv,
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900813 usbhsg_dma_map_ctrl);
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900814
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800815 /* dcp init instead of usbhsg_ep_enable() */
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900816 dcp->pipe = usbhs_dcp_malloc(priv);
817 dcp->pipe->mod_private = dcp;
Kuninori Morimotobc6fbf52011-10-10 22:04:00 -0700818 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900819
820 /*
821 * system config enble
822 * - HI speed
823 * - function
824 * - usb module
825 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900826 usbhs_sys_function_ctrl(priv, 1);
Takeshi Kihara04a5def2014-11-04 10:05:43 +0900827 usbhsg_update_pullup(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900828
829 /*
830 * enable irq callback
831 */
832 mod->irq_dev_state = usbhsg_irq_dev_state;
833 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900834 usbhs_irq_callback_update(priv, mod);
835
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900836 return 0;
837}
838
839static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status)
840{
841 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
842 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
843 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv);
844 struct device *dev = usbhs_priv_to_dev(priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900845 unsigned long flags;
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900846 int ret = 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900847
848 /******************** spin lock ********************/
Kuninori Morimoto97664a22011-06-06 14:18:38 +0900849 usbhs_lock(priv, flags);
850
851 usbhsg_status_clr(gpriv, status);
852 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) &&
853 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))
854 ret = -1; /* already done */
855
856 usbhs_unlock(priv, flags);
857 /******************** spin unlock ********************/
858
859 if (ret < 0)
860 return 0; /* already done is not error */
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900861
862 /*
863 * disable interrupt and systems if 1st try
864 */
Kuninori Morimotodad67392011-06-06 14:18:28 +0900865 usbhs_fifo_quit(priv);
866
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900867 /* disable all irq */
868 mod->irq_dev_state = NULL;
869 mod->irq_ctrl_stage = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900870 usbhs_irq_callback_update(priv, mod);
871
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900872 gpriv->gadget.speed = USB_SPEED_UNKNOWN;
873
874 /* disable sys */
Kuninori Morimotodfbb7f42011-11-24 17:28:35 -0800875 usbhs_sys_set_test_mode(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900876 usbhs_sys_function_ctrl(priv, 0);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900877
Kuninori Morimotod9fa2982012-12-10 22:44:07 -0800878 usbhsg_ep_disable(&dcp->ep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900879
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900880 dev_dbg(dev, "stop gadget\n");
881
882 return 0;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900883}
884
885/*
Phil Edworthyb5a28752015-07-13 16:30:18 +0100886 * VBUS provided by the PHY
887 */
888static int usbhsm_phy_get_vbus(struct platform_device *pdev)
889{
890 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
891 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
892
893 return gpriv->vbus_active;
894}
895
896static void usbhs_mod_phy_mode(struct usbhs_priv *priv)
897{
898 struct usbhs_mod_info *info = &priv->mod_info;
899
900 info->irq_vbus = NULL;
901 priv->pfunc.get_vbus = usbhsm_phy_get_vbus;
902
903 usbhs_irq_callback_update(priv, NULL);
904}
905
906/*
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900907 *
908 * linux usb function
909 *
910 */
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300911static int usbhsg_gadget_start(struct usb_gadget *gadget,
912 struct usb_gadget_driver *driver)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900913{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300914 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800915 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Phil Edworthyb5a28752015-07-13 16:30:18 +0100916 struct device *dev = usbhs_priv_to_dev(priv);
917 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900918
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300919 if (!driver ||
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900920 !driver->setup ||
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100921 driver->max_speed < USB_SPEED_FULL)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900922 return -EINVAL;
Kuninori Morimoto3b872182011-07-03 17:42:35 -0700923
Phil Edworthyb5a28752015-07-13 16:30:18 +0100924 /* connect to bus through transceiver */
925 if (!IS_ERR_OR_NULL(gpriv->transceiver)) {
926 ret = otg_set_peripheral(gpriv->transceiver->otg,
927 &gpriv->gadget);
928 if (ret) {
929 dev_err(dev, "%s: can't bind to transceiver\n",
930 gpriv->gadget.name);
931 return ret;
932 }
933
934 /* get vbus using phy versions */
935 usbhs_mod_phy_mode(priv);
936 }
937
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900938 /* first hook up the driver ... */
939 gpriv->driver = driver;
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900940
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900941 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900942}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900943
Felipe Balbi22835b82014-10-17 12:05:12 -0500944static int usbhsg_gadget_stop(struct usb_gadget *gadget)
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900945{
Felipe Balbiaf1d7052011-10-10 17:11:20 +0300946 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -0800947 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900948
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900949 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD);
Phil Edworthyb5a28752015-07-13 16:30:18 +0100950
951 if (!IS_ERR_OR_NULL(gpriv->transceiver))
952 otg_set_peripheral(gpriv->transceiver->otg, NULL);
953
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900954 gpriv->driver = NULL;
955
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900956 return 0;
957}
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900958
959/*
960 * usb gadget ops
961 */
962static int usbhsg_get_frame(struct usb_gadget *gadget)
963{
964 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
965 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
966
967 return usbhs_frame_get_num(priv);
968}
969
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700970static int usbhsg_pullup(struct usb_gadget *gadget, int is_on)
971{
972 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
973 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
Takeshi Kihara04a5def2014-11-04 10:05:43 +0900974 unsigned long flags;
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700975
Takeshi Kihara04a5def2014-11-04 10:05:43 +0900976 usbhs_lock(priv, flags);
977 if (is_on)
978 usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT);
979 else
980 usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT);
981 usbhsg_update_pullup(priv);
982 usbhs_unlock(priv, flags);
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -0700983
984 return 0;
985}
986
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900987static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self)
988{
989 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
990
991 if (is_self)
992 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED);
993 else
994 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED);
995
Peter Chena17fd412015-01-28 16:32:28 +0800996 gadget->is_selfpowered = (is_self != 0);
997
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +0900998 return 0;
999}
1000
Phil Edworthyb5a28752015-07-13 16:30:18 +01001001static int usbhsg_vbus_session(struct usb_gadget *gadget, int is_active)
1002{
1003 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget);
1004 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv);
1005 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
1006
1007 gpriv->vbus_active = !!is_active;
1008
1009 renesas_usbhs_call_notify_hotplug(pdev);
1010
1011 return 0;
1012}
1013
Felipe Balbieeef4582013-01-24 17:58:16 +02001014static const struct usb_gadget_ops usbhsg_gadget_ops = {
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001015 .get_frame = usbhsg_get_frame,
Shimoda, Yoshihirocac402d2012-03-16 13:10:15 +09001016 .set_selfpowered = usbhsg_set_selfpowered,
Felipe Balbiaf1d7052011-10-10 17:11:20 +03001017 .udc_start = usbhsg_gadget_start,
1018 .udc_stop = usbhsg_gadget_stop,
kuninori.morimoto.gx@renesas.com4cd2f592012-10-15 23:24:19 -07001019 .pullup = usbhsg_pullup,
Phil Edworthyb5a28752015-07-13 16:30:18 +01001020 .vbus_session = usbhsg_vbus_session,
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001021};
1022
1023static int usbhsg_start(struct usbhs_priv *priv)
1024{
1025 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED);
1026}
1027
1028static int usbhsg_stop(struct usbhs_priv *priv)
1029{
Kuninori Morimoto8885a892011-11-17 18:19:38 -08001030 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1031
1032 /* cable disconnect */
1033 if (gpriv->driver &&
1034 gpriv->driver->disconnect)
1035 gpriv->driver->disconnect(&gpriv->gadget);
1036
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001037 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED);
1038}
1039
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001040int usbhs_mod_gadget_probe(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001041{
1042 struct usbhsg_gpriv *gpriv;
1043 struct usbhsg_uep *uep;
1044 struct device *dev = usbhs_priv_to_dev(priv);
Yoshihiro Shimoda64c5f482015-11-18 14:34:10 +09001045 struct renesas_usbhs_driver_pipe_config *pipe_configs =
1046 usbhs_get_dparam(priv, pipe_configs);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001047 int pipe_size = usbhs_get_dparam(priv, pipe_size);
1048 int i;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001049 int ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001050
1051 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL);
1052 if (!gpriv) {
1053 dev_err(dev, "Could not allocate gadget priv\n");
1054 return -ENOMEM;
1055 }
1056
1057 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL);
1058 if (!uep) {
1059 dev_err(dev, "Could not allocate ep\n");
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001060 ret = -ENOMEM;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001061 goto usbhs_mod_gadget_probe_err_gpriv;
1062 }
1063
Phil Edworthyb5a28752015-07-13 16:30:18 +01001064 gpriv->transceiver = usb_get_phy(USB_PHY_TYPE_UNDEFINED);
1065 dev_info(dev, "%stransceiver found\n",
1066 gpriv->transceiver ? "" : "no ");
1067
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001068 /*
1069 * CAUTION
1070 *
1071 * There is no guarantee that it is possible to access usb module here.
1072 * Don't accesses to it.
1073 * The accesse will be enable after "usbhsg_start"
1074 */
1075
1076 /*
1077 * register itself
1078 */
1079 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET);
1080
1081 /* init gpriv */
1082 gpriv->mod.name = "gadget";
1083 gpriv->mod.start = usbhsg_start;
1084 gpriv->mod.stop = usbhsg_stop;
1085 gpriv->uep = uep;
1086 gpriv->uep_size = pipe_size;
1087 usbhsg_status_init(gpriv);
1088
1089 /*
1090 * init gadget
1091 */
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001092 gpriv->gadget.dev.parent = dev;
1093 gpriv->gadget.name = "renesas_usbhs_udc";
1094 gpriv->gadget.ops = &usbhsg_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001095 gpriv->gadget.max_speed = USB_SPEED_HIGH;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001096
1097 INIT_LIST_HEAD(&gpriv->gadget.ep_list);
1098
1099 /*
1100 * init usb_ep
1101 */
1102 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) {
1103 uep->gpriv = gpriv;
Kuninori Morimoto58482942012-12-10 22:43:45 -08001104 uep->pipe = NULL;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001105 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i);
1106
1107 uep->ep.name = uep->ep_name;
1108 uep->ep.ops = &usbhsg_ep_ops;
1109 INIT_LIST_HEAD(&uep->ep.ep_list);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001110
1111 /* init DCP */
1112 if (usbhsg_is_dcp(uep)) {
1113 gpriv->gadget.ep0 = &uep->ep;
Robert Baldygae117e742013-12-13 12:23:38 +01001114 usb_ep_set_maxpacket_limit(&uep->ep, 64);
Robert Baldyga916f7ac2015-07-31 16:00:47 +02001115 uep->ep.caps.type_control = true;
Yoshihiro Shimoda64c5f482015-11-18 14:34:10 +09001116 } else {
1117 /* init normal pipe */
1118 if (pipe_configs[i].type == USB_ENDPOINT_XFER_ISOC)
1119 uep->ep.caps.type_iso = true;
1120 if (pipe_configs[i].type == USB_ENDPOINT_XFER_BULK)
1121 uep->ep.caps.type_bulk = true;
1122 if (pipe_configs[i].type == USB_ENDPOINT_XFER_INT)
1123 uep->ep.caps.type_int = true;
1124 usb_ep_set_maxpacket_limit(&uep->ep,
1125 pipe_configs[i].bufsize);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001126 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list);
1127 }
Robert Baldyga916f7ac2015-07-31 16:00:47 +02001128 uep->ep.caps.dir_in = true;
1129 uep->ep.caps.dir_out = true;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001130 }
1131
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001132 ret = usb_add_gadget_udc(dev, &gpriv->gadget);
1133 if (ret)
Felipe Balbi0972ef72013-01-24 17:08:01 +02001134 goto err_add_udc;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001135
1136
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001137 dev_info(dev, "gadget probed\n");
1138
1139 return 0;
Kuninori Morimotof8eff0a2011-11-17 18:19:06 -08001140
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001141err_add_udc:
1142 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001143
1144usbhs_mod_gadget_probe_err_gpriv:
1145 kfree(gpriv);
1146
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001147 return ret;
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001148}
1149
Kuninori Morimotob7a8d172011-10-26 19:33:49 -07001150void usbhs_mod_gadget_remove(struct usbhs_priv *priv)
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001151{
1152 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv);
1153
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001154 usb_del_gadget_udc(&gpriv->gadget);
Kuninori Morimoto3b872182011-07-03 17:42:35 -07001155
Sebastian Andrzej Siewior3af51ac2011-06-03 19:50:48 +02001156 kfree(gpriv->uep);
Kuninori Morimoto2f983822011-04-05 11:40:54 +09001157 kfree(gpriv);
1158}