Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 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 | */ |
Kuninori Morimoto | dfbb7f4 | 2011-11-24 17:28:35 -0800 | [diff] [blame] | 17 | #include <linux/delay.h> |
Kuninori Morimoto | d128a259 | 2011-08-03 21:41:26 -0700 | [diff] [blame] | 18 | #include <linux/dma-mapping.h> |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 19 | #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> |
| 24 | #include "common.h" |
| 25 | |
| 26 | /* |
| 27 | * struct |
| 28 | */ |
| 29 | struct usbhsg_request { |
| 30 | struct usb_request req; |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 31 | struct usbhs_pkt pkt; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | #define EP_NAME_SIZE 8 |
| 35 | struct usbhsg_gpriv; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 36 | struct usbhsg_uep { |
| 37 | struct usb_ep ep; |
| 38 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 39 | |
| 40 | char ep_name[EP_NAME_SIZE]; |
| 41 | |
| 42 | struct usbhsg_gpriv *gpriv; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct usbhsg_gpriv { |
| 46 | struct usb_gadget gadget; |
| 47 | struct usbhs_mod mod; |
| 48 | |
| 49 | struct usbhsg_uep *uep; |
| 50 | int uep_size; |
| 51 | |
| 52 | struct usb_gadget_driver *driver; |
| 53 | |
| 54 | u32 status; |
| 55 | #define USBHSG_STATUS_STARTED (1 << 0) |
| 56 | #define USBHSG_STATUS_REGISTERD (1 << 1) |
| 57 | #define USBHSG_STATUS_WEDGE (1 << 2) |
Shimoda, Yoshihiro | cac402d | 2012-03-16 13:10:15 +0900 | [diff] [blame] | 58 | #define USBHSG_STATUS_SELF_POWERED (1 << 3) |
Takeshi Kihara | 04a5def | 2014-11-04 10:05:43 +0900 | [diff] [blame] | 59 | #define USBHSG_STATUS_SOFT_CONNECT (1 << 4) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 60 | }; |
| 61 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 62 | struct usbhsg_recip_handle { |
| 63 | char *name; |
| 64 | int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep, |
| 65 | struct usb_ctrlrequest *ctrl); |
| 66 | int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep, |
| 67 | struct usb_ctrlrequest *ctrl); |
| 68 | int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep, |
| 69 | struct usb_ctrlrequest *ctrl); |
| 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * macro |
| 74 | */ |
| 75 | #define usbhsg_priv_to_gpriv(priv) \ |
| 76 | container_of( \ |
| 77 | usbhs_mod_get(priv, USBHS_GADGET), \ |
| 78 | struct usbhsg_gpriv, mod) |
| 79 | |
| 80 | #define __usbhsg_for_each_uep(start, pos, g, i) \ |
Kuninori Morimoto | 925403f | 2013-07-11 22:32:31 -0700 | [diff] [blame] | 81 | for ((i) = start; \ |
| 82 | ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \ |
| 83 | (i)++) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 84 | |
| 85 | #define usbhsg_for_each_uep(pos, gpriv, i) \ |
| 86 | __usbhsg_for_each_uep(1, pos, gpriv, i) |
| 87 | |
| 88 | #define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \ |
| 89 | __usbhsg_for_each_uep(0, pos, gpriv, i) |
| 90 | |
| 91 | #define usbhsg_gadget_to_gpriv(g)\ |
| 92 | container_of(g, struct usbhsg_gpriv, gadget) |
| 93 | |
| 94 | #define usbhsg_req_to_ureq(r)\ |
| 95 | container_of(r, struct usbhsg_request, req) |
| 96 | |
| 97 | #define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 98 | #define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv) |
| 99 | #define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv) |
| 100 | #define usbhsg_gpriv_to_dcp(gp) ((gp)->uep) |
| 101 | #define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i) |
| 102 | #define usbhsg_uep_to_gpriv(u) ((u)->gpriv) |
| 103 | #define usbhsg_uep_to_pipe(u) ((u)->pipe) |
| 104 | #define usbhsg_pipe_to_uep(p) ((p)->mod_private) |
| 105 | #define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv)) |
| 106 | |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 107 | #define usbhsg_ureq_to_pkt(u) (&(u)->pkt) |
| 108 | #define usbhsg_pkt_to_ureq(i) \ |
| 109 | container_of(i, struct usbhsg_request, pkt) |
| 110 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 111 | #define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN) |
| 112 | |
| 113 | /* status */ |
| 114 | #define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0) |
| 115 | #define usbhsg_status_set(gp, b) (gp->status |= b) |
| 116 | #define usbhsg_status_clr(gp, b) (gp->status &= ~b) |
| 117 | #define usbhsg_status_has(gp, b) (gp->status & b) |
| 118 | |
| 119 | /* |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 120 | * queue push/pop |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 121 | */ |
Yoshihiro Shimoda | 00f30d2 | 2015-03-16 16:36:48 +0900 | [diff] [blame] | 122 | static void __usbhsg_queue_pop(struct usbhsg_uep *uep, |
| 123 | struct usbhsg_request *ureq, |
| 124 | int status) |
| 125 | { |
| 126 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 127 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
| 128 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 129 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
| 130 | |
| 131 | dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe)); |
| 132 | |
| 133 | ureq->req.status = status; |
| 134 | spin_unlock(usbhs_priv_to_lock(priv)); |
| 135 | usb_gadget_giveback_request(&uep->ep, &ureq->req); |
| 136 | spin_lock(usbhs_priv_to_lock(priv)); |
| 137 | } |
| 138 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 139 | static void usbhsg_queue_pop(struct usbhsg_uep *uep, |
| 140 | struct usbhsg_request *ureq, |
| 141 | int status) |
| 142 | { |
| 143 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
Yoshihiro Shimoda | 00f30d2 | 2015-03-16 16:36:48 +0900 | [diff] [blame] | 144 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
| 145 | unsigned long flags; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 146 | |
Yoshihiro Shimoda | 00f30d2 | 2015-03-16 16:36:48 +0900 | [diff] [blame] | 147 | usbhs_lock(priv, flags); |
| 148 | __usbhsg_queue_pop(uep, ureq, status); |
| 149 | usbhs_unlock(priv, flags); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 150 | } |
| 151 | |
Kuninori Morimoto | 2cc9719 | 2011-10-10 22:03:39 -0700 | [diff] [blame] | 152 | static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 153 | { |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 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); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 157 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 158 | ureq->req.actual = pkt->actual; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 159 | |
Kuninori Morimoto | 659d495 | 2011-06-06 14:18:23 +0900 | [diff] [blame] | 160 | usbhsg_queue_pop(uep, ureq, 0); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 161 | } |
| 162 | |
Kuninori Morimoto | b331872 | 2011-10-10 22:04:41 -0700 | [diff] [blame] | 163 | static void usbhsg_queue_push(struct usbhsg_uep *uep, |
| 164 | struct usbhsg_request *ureq) |
| 165 | { |
| 166 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 167 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 168 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
| 169 | struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq); |
| 170 | struct usb_request *req = &ureq->req; |
| 171 | |
| 172 | req->actual = 0; |
| 173 | req->status = -EINPROGRESS; |
| 174 | usbhs_pkt_push(pipe, pkt, usbhsg_queue_done, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 175 | req->buf, req->length, req->zero, -1); |
Kuninori Morimoto | 654c35a | 2011-10-10 22:04:53 -0700 | [diff] [blame] | 176 | usbhs_pkt_start(pipe); |
Kuninori Morimoto | b331872 | 2011-10-10 22:04:41 -0700 | [diff] [blame] | 177 | |
| 178 | dev_dbg(dev, "pipe %d : queue push (%d)\n", |
| 179 | usbhs_pipe_number(pipe), |
| 180 | req->length); |
| 181 | } |
| 182 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 183 | /* |
| 184 | * dma map/unmap |
| 185 | */ |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 186 | static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map) |
| 187 | { |
Felipe Balbi | ade78f9 | 2011-12-19 11:57:16 +0200 | [diff] [blame] | 188 | struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt); |
| 189 | struct usb_request *req = &ureq->req; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 190 | struct usbhs_pipe *pipe = pkt->pipe; |
| 191 | struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe); |
| 192 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 193 | enum dma_data_direction dir; |
Felipe Balbi | ade78f9 | 2011-12-19 11:57:16 +0200 | [diff] [blame] | 194 | int ret = 0; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 195 | |
Felipe Balbi | ade78f9 | 2011-12-19 11:57:16 +0200 | [diff] [blame] | 196 | dir = usbhs_pipe_is_dir_host(pipe); |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 197 | |
Felipe Balbi | ade78f9 | 2011-12-19 11:57:16 +0200 | [diff] [blame] | 198 | if (map) { |
| 199 | /* it can not use scatter/gather */ |
| 200 | WARN_ON(req->num_sgs); |
| 201 | |
| 202 | ret = usb_gadget_map_request(&gpriv->gadget, req, dir); |
| 203 | if (ret < 0) |
| 204 | return ret; |
| 205 | |
| 206 | pkt->dma = req->dma; |
| 207 | } else { |
| 208 | usb_gadget_unmap_request(&gpriv->gadget, req, dir); |
| 209 | } |
| 210 | |
| 211 | return ret; |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 212 | } |
| 213 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 214 | /* |
| 215 | * USB_TYPE_STANDARD / clear feature functions |
| 216 | */ |
| 217 | static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv, |
| 218 | struct usbhsg_uep *uep, |
| 219 | struct usb_ctrlrequest *ctrl) |
| 220 | { |
| 221 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 222 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 223 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); |
| 224 | |
| 225 | usbhs_dcp_control_transfer_done(pipe); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv, |
| 231 | struct usbhsg_uep *uep, |
| 232 | struct usb_ctrlrequest *ctrl) |
| 233 | { |
| 234 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 235 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
| 236 | |
| 237 | if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 238 | usbhs_pipe_disable(pipe); |
Kuninori Morimoto | 6e6db82 | 2011-10-10 22:05:30 -0700 | [diff] [blame] | 239 | usbhs_pipe_sequence_data0(pipe); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 240 | usbhs_pipe_enable(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | usbhsg_recip_handler_std_control_done(priv, uep, ctrl); |
| 244 | |
Kuninori Morimoto | 25fa707 | 2011-11-24 17:28:17 -0800 | [diff] [blame] | 245 | usbhs_pkt_start(pipe); |
| 246 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
Kuninori Morimoto | 225da3e | 2013-03-31 18:34:43 -0700 | [diff] [blame] | 250 | static struct usbhsg_recip_handle req_clear_feature = { |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 251 | .name = "clear feature", |
| 252 | .device = usbhsg_recip_handler_std_control_done, |
| 253 | .interface = usbhsg_recip_handler_std_control_done, |
| 254 | .endpoint = usbhsg_recip_handler_std_clear_endpoint, |
| 255 | }; |
| 256 | |
| 257 | /* |
Kuninori Morimoto | ced6e09 | 2011-11-24 17:27:50 -0800 | [diff] [blame] | 258 | * USB_TYPE_STANDARD / set feature functions |
| 259 | */ |
Kuninori Morimoto | dfbb7f4 | 2011-11-24 17:28:35 -0800 | [diff] [blame] | 260 | static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv, |
| 261 | struct usbhsg_uep *uep, |
| 262 | struct usb_ctrlrequest *ctrl) |
| 263 | { |
| 264 | switch (le16_to_cpu(ctrl->wValue)) { |
| 265 | case USB_DEVICE_TEST_MODE: |
| 266 | usbhsg_recip_handler_std_control_done(priv, uep, ctrl); |
| 267 | udelay(100); |
| 268 | usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8)); |
| 269 | break; |
| 270 | default: |
| 271 | usbhsg_recip_handler_std_control_done(priv, uep, ctrl); |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Kuninori Morimoto | ced6e09 | 2011-11-24 17:27:50 -0800 | [diff] [blame] | 278 | static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv, |
| 279 | struct usbhsg_uep *uep, |
| 280 | struct usb_ctrlrequest *ctrl) |
| 281 | { |
| 282 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
| 283 | |
| 284 | usbhs_pipe_stall(pipe); |
| 285 | |
| 286 | usbhsg_recip_handler_std_control_done(priv, uep, ctrl); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Kuninori Morimoto | 225da3e | 2013-03-31 18:34:43 -0700 | [diff] [blame] | 291 | static struct usbhsg_recip_handle req_set_feature = { |
Kuninori Morimoto | ced6e09 | 2011-11-24 17:27:50 -0800 | [diff] [blame] | 292 | .name = "set feature", |
Kuninori Morimoto | dfbb7f4 | 2011-11-24 17:28:35 -0800 | [diff] [blame] | 293 | .device = usbhsg_recip_handler_std_set_device, |
Kuninori Morimoto | ced6e09 | 2011-11-24 17:27:50 -0800 | [diff] [blame] | 294 | .interface = usbhsg_recip_handler_std_control_done, |
| 295 | .endpoint = usbhsg_recip_handler_std_set_endpoint, |
| 296 | }; |
| 297 | |
| 298 | /* |
Kuninori Morimoto | 17f7f76 | 2011-11-24 17:28:04 -0800 | [diff] [blame] | 299 | * USB_TYPE_STANDARD / get status functions |
| 300 | */ |
| 301 | static void __usbhsg_recip_send_complete(struct usb_ep *ep, |
| 302 | struct usb_request *req) |
| 303 | { |
| 304 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); |
| 305 | |
| 306 | /* free allocated recip-buffer/usb_request */ |
| 307 | kfree(ureq->pkt.buf); |
| 308 | usb_ep_free_request(ep, req); |
| 309 | } |
| 310 | |
| 311 | static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv, |
| 312 | unsigned short status) |
| 313 | { |
| 314 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 315 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); |
| 316 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 317 | struct usb_request *req; |
| 318 | unsigned short *buf; |
| 319 | |
| 320 | /* alloc new usb_request for recip */ |
| 321 | req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC); |
| 322 | if (!req) { |
| 323 | dev_err(dev, "recip request allocation fail\n"); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | /* alloc recip data buffer */ |
| 328 | buf = kmalloc(sizeof(*buf), GFP_ATOMIC); |
| 329 | if (!buf) { |
| 330 | usb_ep_free_request(&dcp->ep, req); |
| 331 | dev_err(dev, "recip data allocation fail\n"); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | /* recip data is status */ |
| 336 | *buf = cpu_to_le16(status); |
| 337 | |
| 338 | /* allocated usb_request/buffer will be freed */ |
| 339 | req->complete = __usbhsg_recip_send_complete; |
| 340 | req->buf = buf; |
| 341 | req->length = sizeof(*buf); |
| 342 | req->zero = 0; |
| 343 | |
| 344 | /* push packet */ |
| 345 | pipe->handler = &usbhs_fifo_pio_push_handler; |
| 346 | usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req)); |
| 347 | } |
| 348 | |
| 349 | static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv, |
| 350 | struct usbhsg_uep *uep, |
| 351 | struct usb_ctrlrequest *ctrl) |
| 352 | { |
| 353 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
Shimoda, Yoshihiro | cac402d | 2012-03-16 13:10:15 +0900 | [diff] [blame] | 354 | unsigned short status = 0; |
| 355 | |
| 356 | if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED)) |
| 357 | status = 1 << USB_DEVICE_SELF_POWERED; |
Kuninori Morimoto | 17f7f76 | 2011-11-24 17:28:04 -0800 | [diff] [blame] | 358 | |
| 359 | __usbhsg_recip_send_status(gpriv, status); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv, |
| 365 | struct usbhsg_uep *uep, |
| 366 | struct usb_ctrlrequest *ctrl) |
| 367 | { |
| 368 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 369 | unsigned short status = 0; |
| 370 | |
| 371 | __usbhsg_recip_send_status(gpriv, status); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv, |
| 377 | struct usbhsg_uep *uep, |
| 378 | struct usb_ctrlrequest *ctrl) |
| 379 | { |
| 380 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 381 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
| 382 | unsigned short status = 0; |
| 383 | |
| 384 | if (usbhs_pipe_is_stall(pipe)) |
| 385 | status = 1 << USB_ENDPOINT_HALT; |
| 386 | |
| 387 | __usbhsg_recip_send_status(gpriv, status); |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Kuninori Morimoto | 225da3e | 2013-03-31 18:34:43 -0700 | [diff] [blame] | 392 | static struct usbhsg_recip_handle req_get_status = { |
Kuninori Morimoto | 17f7f76 | 2011-11-24 17:28:04 -0800 | [diff] [blame] | 393 | .name = "get status", |
| 394 | .device = usbhsg_recip_handler_std_get_device, |
| 395 | .interface = usbhsg_recip_handler_std_get_interface, |
| 396 | .endpoint = usbhsg_recip_handler_std_get_endpoint, |
| 397 | }; |
| 398 | |
| 399 | /* |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 400 | * USB_TYPE handler |
| 401 | */ |
| 402 | static int usbhsg_recip_run_handle(struct usbhs_priv *priv, |
| 403 | struct usbhsg_recip_handle *handler, |
| 404 | struct usb_ctrlrequest *ctrl) |
| 405 | { |
| 406 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 407 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 408 | struct usbhsg_uep *uep; |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 409 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 410 | int recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 411 | int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; |
Jesper Juhl | 7983bc7 | 2012-01-16 22:42:10 +0100 | [diff] [blame] | 412 | int ret = 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 413 | int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep, |
| 414 | struct usb_ctrlrequest *ctrl); |
| 415 | char *msg; |
| 416 | |
| 417 | uep = usbhsg_gpriv_to_nth_uep(gpriv, nth); |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 418 | pipe = usbhsg_uep_to_pipe(uep); |
| 419 | if (!pipe) { |
Kuninori Morimoto | 9a28b7b | 2011-04-21 14:10:12 +0900 | [diff] [blame] | 420 | dev_err(dev, "wrong recip request\n"); |
Kuninori Morimoto | 25fa707 | 2011-11-24 17:28:17 -0800 | [diff] [blame] | 421 | return -EINVAL; |
Kuninori Morimoto | 9a28b7b | 2011-04-21 14:10:12 +0900 | [diff] [blame] | 422 | } |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 423 | |
| 424 | switch (recip) { |
| 425 | case USB_RECIP_DEVICE: |
| 426 | msg = "DEVICE"; |
| 427 | func = handler->device; |
| 428 | break; |
| 429 | case USB_RECIP_INTERFACE: |
| 430 | msg = "INTERFACE"; |
| 431 | func = handler->interface; |
| 432 | break; |
| 433 | case USB_RECIP_ENDPOINT: |
| 434 | msg = "ENDPOINT"; |
| 435 | func = handler->endpoint; |
| 436 | break; |
| 437 | default: |
| 438 | dev_warn(dev, "unsupported RECIP(%d)\n", recip); |
| 439 | func = NULL; |
| 440 | ret = -EINVAL; |
| 441 | } |
| 442 | |
| 443 | if (func) { |
| 444 | dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg); |
| 445 | ret = func(priv, uep, ctrl); |
| 446 | } |
| 447 | |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * irq functions |
| 453 | * |
| 454 | * it will be called from usbhs_interrupt |
| 455 | */ |
| 456 | static int usbhsg_irq_dev_state(struct usbhs_priv *priv, |
| 457 | struct usbhs_irq_state *irq_state) |
| 458 | { |
| 459 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 460 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 461 | |
Kuninori Morimoto | 75587f5 | 2011-10-10 22:01:51 -0700 | [diff] [blame] | 462 | gpriv->gadget.speed = usbhs_bus_get_speed(priv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 463 | |
| 464 | dev_dbg(dev, "state = %x : speed : %d\n", |
| 465 | usbhs_status_get_device_state(irq_state), |
| 466 | gpriv->gadget.speed); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv, |
| 472 | struct usbhs_irq_state *irq_state) |
| 473 | { |
| 474 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 475 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 476 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); |
| 477 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 478 | struct usb_ctrlrequest ctrl; |
| 479 | struct usbhsg_recip_handle *recip_handler = NULL; |
| 480 | int stage = usbhs_status_get_ctrl_stage(irq_state); |
| 481 | int ret = 0; |
| 482 | |
| 483 | dev_dbg(dev, "stage = %d\n", stage); |
| 484 | |
| 485 | /* |
| 486 | * see Manual |
| 487 | * |
| 488 | * "Operation" |
| 489 | * - "Interrupt Function" |
| 490 | * - "Control Transfer Stage Transition Interrupt" |
| 491 | * - Fig. "Control Transfer Stage Transitions" |
| 492 | */ |
| 493 | |
| 494 | switch (stage) { |
| 495 | case READ_DATA_STAGE: |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 496 | pipe->handler = &usbhs_fifo_pio_push_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 497 | break; |
| 498 | case WRITE_DATA_STAGE: |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 499 | pipe->handler = &usbhs_fifo_pio_pop_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 500 | break; |
| 501 | case NODATA_STATUS_STAGE: |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 502 | pipe->handler = &usbhs_ctrl_stage_end_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 503 | break; |
Yoshihiro Shimoda | 4ef35b1 | 2014-11-04 10:05:44 +0900 | [diff] [blame] | 504 | case READ_STATUS_STAGE: |
| 505 | case WRITE_STATUS_STAGE: |
| 506 | usbhs_dcp_control_transfer_done(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 507 | default: |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * get usb request |
| 513 | */ |
| 514 | usbhs_usbreq_get_val(priv, &ctrl); |
| 515 | |
| 516 | switch (ctrl.bRequestType & USB_TYPE_MASK) { |
| 517 | case USB_TYPE_STANDARD: |
| 518 | switch (ctrl.bRequest) { |
| 519 | case USB_REQ_CLEAR_FEATURE: |
| 520 | recip_handler = &req_clear_feature; |
| 521 | break; |
Kuninori Morimoto | ced6e09 | 2011-11-24 17:27:50 -0800 | [diff] [blame] | 522 | case USB_REQ_SET_FEATURE: |
| 523 | recip_handler = &req_set_feature; |
| 524 | break; |
Kuninori Morimoto | 17f7f76 | 2011-11-24 17:28:04 -0800 | [diff] [blame] | 525 | case USB_REQ_GET_STATUS: |
| 526 | recip_handler = &req_get_status; |
| 527 | break; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * setup stage / run recip |
| 533 | */ |
| 534 | if (recip_handler) |
| 535 | ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl); |
| 536 | else |
| 537 | ret = gpriv->driver->setup(&gpriv->gadget, &ctrl); |
| 538 | |
| 539 | if (ret < 0) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 540 | usbhs_pipe_stall(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 545 | /* |
| 546 | * |
| 547 | * usb_dcp_ops |
| 548 | * |
| 549 | */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 550 | static int usbhsg_pipe_disable(struct usbhsg_uep *uep) |
| 551 | { |
| 552 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
Kuninori Morimoto | 8a2c225 | 2011-06-06 14:18:33 +0900 | [diff] [blame] | 553 | struct usbhs_pkt *pkt; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 554 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 555 | while (1) { |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 556 | pkt = usbhs_pkt_pop(pipe, NULL); |
Kuninori Morimoto | 8a2c225 | 2011-06-06 14:18:33 +0900 | [diff] [blame] | 557 | if (!pkt) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 558 | break; |
Kuninori Morimoto | 91b158f | 2011-11-24 17:28:26 -0800 | [diff] [blame] | 559 | |
| 560 | usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 561 | } |
| 562 | |
Kuninori Morimoto | 91b158f | 2011-11-24 17:28:26 -0800 | [diff] [blame] | 563 | usbhs_pipe_disable(pipe); |
| 564 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * |
| 570 | * usb_ep_ops |
| 571 | * |
| 572 | */ |
| 573 | static int usbhsg_ep_enable(struct usb_ep *ep, |
| 574 | const struct usb_endpoint_descriptor *desc) |
| 575 | { |
| 576 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
| 577 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 578 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
| 579 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 580 | int ret = -EIO; |
| 581 | |
Kuninori Morimoto | 409ba9e | 2011-04-26 09:21:35 +0900 | [diff] [blame] | 582 | /* |
| 583 | * if it already have pipe, |
| 584 | * nothing to do |
| 585 | */ |
Kuninori Morimoto | 08e6c61 | 2011-06-09 16:48:25 +0900 | [diff] [blame] | 586 | if (uep->pipe) { |
| 587 | usbhs_pipe_clear(uep->pipe); |
Kuninori Morimoto | 6e6db82 | 2011-10-10 22:05:30 -0700 | [diff] [blame] | 588 | usbhs_pipe_sequence_data0(uep->pipe); |
Kuninori Morimoto | 409ba9e | 2011-04-26 09:21:35 +0900 | [diff] [blame] | 589 | return 0; |
Kuninori Morimoto | 08e6c61 | 2011-06-09 16:48:25 +0900 | [diff] [blame] | 590 | } |
Kuninori Morimoto | 409ba9e | 2011-04-26 09:21:35 +0900 | [diff] [blame] | 591 | |
Kuninori Morimoto | f5aa889 | 2011-10-10 21:59:46 -0700 | [diff] [blame] | 592 | pipe = usbhs_pipe_malloc(priv, |
| 593 | usb_endpoint_type(desc), |
| 594 | usb_endpoint_dir_in(desc)); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 595 | if (pipe) { |
| 596 | uep->pipe = pipe; |
| 597 | pipe->mod_private = uep; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 598 | |
Kuninori Morimoto | f5aa889 | 2011-10-10 21:59:46 -0700 | [diff] [blame] | 599 | /* set epnum / maxp */ |
Kuninori Morimoto | bc6fbf5 | 2011-10-10 22:04:00 -0700 | [diff] [blame] | 600 | usbhs_pipe_config_update(pipe, 0, |
Kuninori Morimoto | f5aa889 | 2011-10-10 21:59:46 -0700 | [diff] [blame] | 601 | usb_endpoint_num(desc), |
| 602 | usb_endpoint_maxp(desc)); |
| 603 | |
Kuninori Morimoto | 233f519 | 2011-07-07 00:23:24 -0700 | [diff] [blame] | 604 | /* |
| 605 | * usbhs_fifo_dma_push/pop_handler try to |
| 606 | * use dmaengine if possible. |
| 607 | * It will use pio handler if impossible. |
| 608 | */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 609 | if (usb_endpoint_dir_in(desc)) |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 610 | pipe->handler = &usbhs_fifo_dma_push_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 611 | else |
Kuninori Morimoto | 0c6ef98 | 2011-10-10 22:00:59 -0700 | [diff] [blame] | 612 | pipe->handler = &usbhs_fifo_dma_pop_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 613 | |
| 614 | ret = 0; |
| 615 | } |
Kuninori Morimoto | cb96632 | 2011-04-21 14:10:08 +0900 | [diff] [blame] | 616 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | static int usbhsg_ep_disable(struct usb_ep *ep) |
| 621 | { |
| 622 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
Yoshihiro Shimoda | dfb87b8 | 2014-07-09 20:30:13 +0900 | [diff] [blame] | 623 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 624 | |
Kazuya Mizuguchi | 1143205 | 2014-11-04 10:05:42 +0900 | [diff] [blame] | 625 | if (!pipe) |
| 626 | return -EINVAL; |
| 627 | |
Kuninori Morimoto | d9fa298 | 2012-12-10 22:44:07 -0800 | [diff] [blame] | 628 | usbhsg_pipe_disable(uep); |
Yoshihiro Shimoda | dfb87b8 | 2014-07-09 20:30:13 +0900 | [diff] [blame] | 629 | usbhs_pipe_free(pipe); |
Kuninori Morimoto | d9fa298 | 2012-12-10 22:44:07 -0800 | [diff] [blame] | 630 | |
| 631 | uep->pipe->mod_private = NULL; |
| 632 | uep->pipe = NULL; |
| 633 | |
| 634 | return 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep, |
| 638 | gfp_t gfp_flags) |
| 639 | { |
| 640 | struct usbhsg_request *ureq; |
| 641 | |
| 642 | ureq = kzalloc(sizeof *ureq, gfp_flags); |
| 643 | if (!ureq) |
| 644 | return NULL; |
| 645 | |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 646 | usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq)); |
| 647 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 648 | return &ureq->req; |
| 649 | } |
| 650 | |
| 651 | static void usbhsg_ep_free_request(struct usb_ep *ep, |
| 652 | struct usb_request *req) |
| 653 | { |
| 654 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); |
| 655 | |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 656 | WARN_ON(!list_empty(&ureq->pkt.node)); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 657 | kfree(ureq); |
| 658 | } |
| 659 | |
| 660 | static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req, |
| 661 | gfp_t gfp_flags) |
| 662 | { |
| 663 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
| 664 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 665 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); |
| 666 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 667 | |
| 668 | /* param check */ |
| 669 | if (usbhsg_is_not_connected(gpriv) || |
| 670 | unlikely(!gpriv->driver) || |
| 671 | unlikely(!pipe)) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 672 | return -ESHUTDOWN; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 673 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 674 | usbhsg_queue_push(uep, ureq); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 675 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 676 | return 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) |
| 680 | { |
| 681 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
| 682 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 683 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 684 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 685 | usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq)); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 686 | usbhsg_queue_pop(uep, ureq, -ECONNRESET); |
| 687 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge) |
| 692 | { |
| 693 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
| 694 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
| 695 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 696 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 697 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 698 | unsigned long flags; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 699 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 700 | usbhsg_pipe_disable(uep); |
| 701 | |
| 702 | dev_dbg(dev, "set halt %d (pipe %d)\n", |
| 703 | halt, usbhs_pipe_number(pipe)); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 704 | |
| 705 | /******************** spin lock ********************/ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 706 | usbhs_lock(priv, flags); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 707 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 708 | if (halt) |
| 709 | usbhs_pipe_stall(pipe); |
| 710 | else |
| 711 | usbhs_pipe_disable(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 712 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 713 | if (halt && wedge) |
| 714 | usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE); |
| 715 | else |
| 716 | usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 717 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 718 | usbhs_unlock(priv, flags); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 719 | /******************** spin unlock ******************/ |
| 720 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 721 | return 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | static int usbhsg_ep_set_halt(struct usb_ep *ep, int value) |
| 725 | { |
| 726 | return __usbhsg_ep_set_halt_wedge(ep, value, 0); |
| 727 | } |
| 728 | |
| 729 | static int usbhsg_ep_set_wedge(struct usb_ep *ep) |
| 730 | { |
| 731 | return __usbhsg_ep_set_halt_wedge(ep, 1, 1); |
| 732 | } |
| 733 | |
| 734 | static struct usb_ep_ops usbhsg_ep_ops = { |
| 735 | .enable = usbhsg_ep_enable, |
| 736 | .disable = usbhsg_ep_disable, |
| 737 | |
| 738 | .alloc_request = usbhsg_ep_alloc_request, |
| 739 | .free_request = usbhsg_ep_free_request, |
| 740 | |
| 741 | .queue = usbhsg_ep_queue, |
| 742 | .dequeue = usbhsg_ep_dequeue, |
| 743 | |
| 744 | .set_halt = usbhsg_ep_set_halt, |
| 745 | .set_wedge = usbhsg_ep_set_wedge, |
| 746 | }; |
| 747 | |
| 748 | /* |
Takeshi Kihara | 04a5def | 2014-11-04 10:05:43 +0900 | [diff] [blame] | 749 | * pullup control |
| 750 | */ |
| 751 | static int usbhsg_can_pullup(struct usbhs_priv *priv) |
| 752 | { |
| 753 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 754 | |
| 755 | return gpriv->driver && |
| 756 | usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT); |
| 757 | } |
| 758 | |
| 759 | static void usbhsg_update_pullup(struct usbhs_priv *priv) |
| 760 | { |
| 761 | if (usbhsg_can_pullup(priv)) |
| 762 | usbhs_sys_function_pullup(priv, 1); |
| 763 | else |
| 764 | usbhs_sys_function_pullup(priv, 0); |
| 765 | } |
| 766 | |
| 767 | /* |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 768 | * usb module start/end |
| 769 | */ |
| 770 | static int usbhsg_try_start(struct usbhs_priv *priv, u32 status) |
| 771 | { |
| 772 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 773 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 774 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 775 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 776 | unsigned long flags; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 777 | int ret = 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 778 | |
| 779 | /******************** spin lock ********************/ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 780 | usbhs_lock(priv, flags); |
| 781 | |
| 782 | usbhsg_status_set(gpriv, status); |
| 783 | if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && |
| 784 | usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))) |
| 785 | ret = -1; /* not ready */ |
| 786 | |
| 787 | usbhs_unlock(priv, flags); |
| 788 | /******************** spin unlock ********************/ |
| 789 | |
| 790 | if (ret < 0) |
| 791 | return 0; /* not ready is not error */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 792 | |
| 793 | /* |
| 794 | * enable interrupt and systems if ready |
| 795 | */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 796 | dev_dbg(dev, "start gadget\n"); |
| 797 | |
| 798 | /* |
| 799 | * pipe initialize and enable DCP |
| 800 | */ |
Yoshihiro Shimoda | cdeb794 | 2014-11-04 10:05:45 +0900 | [diff] [blame] | 801 | usbhs_fifo_init(priv); |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 802 | usbhs_pipe_init(priv, |
Kuninori Morimoto | e73a989 | 2011-06-06 14:19:03 +0900 | [diff] [blame] | 803 | usbhsg_dma_map_ctrl); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 804 | |
Kuninori Morimoto | d9fa298 | 2012-12-10 22:44:07 -0800 | [diff] [blame] | 805 | /* dcp init instead of usbhsg_ep_enable() */ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 806 | dcp->pipe = usbhs_dcp_malloc(priv); |
| 807 | dcp->pipe->mod_private = dcp; |
Kuninori Morimoto | bc6fbf5 | 2011-10-10 22:04:00 -0700 | [diff] [blame] | 808 | usbhs_pipe_config_update(dcp->pipe, 0, 0, 64); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 809 | |
| 810 | /* |
| 811 | * system config enble |
| 812 | * - HI speed |
| 813 | * - function |
| 814 | * - usb module |
| 815 | */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 816 | usbhs_sys_function_ctrl(priv, 1); |
Takeshi Kihara | 04a5def | 2014-11-04 10:05:43 +0900 | [diff] [blame] | 817 | usbhsg_update_pullup(priv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 818 | |
| 819 | /* |
| 820 | * enable irq callback |
| 821 | */ |
| 822 | mod->irq_dev_state = usbhsg_irq_dev_state; |
| 823 | mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 824 | usbhs_irq_callback_update(priv, mod); |
| 825 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status) |
| 830 | { |
| 831 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 832 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 833 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 834 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 835 | unsigned long flags; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 836 | int ret = 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 837 | |
| 838 | /******************** spin lock ********************/ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 839 | usbhs_lock(priv, flags); |
| 840 | |
| 841 | usbhsg_status_clr(gpriv, status); |
| 842 | if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && |
| 843 | !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)) |
| 844 | ret = -1; /* already done */ |
| 845 | |
| 846 | usbhs_unlock(priv, flags); |
| 847 | /******************** spin unlock ********************/ |
| 848 | |
| 849 | if (ret < 0) |
| 850 | return 0; /* already done is not error */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 851 | |
| 852 | /* |
| 853 | * disable interrupt and systems if 1st try |
| 854 | */ |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 855 | usbhs_fifo_quit(priv); |
| 856 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 857 | /* disable all irq */ |
| 858 | mod->irq_dev_state = NULL; |
| 859 | mod->irq_ctrl_stage = NULL; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 860 | usbhs_irq_callback_update(priv, mod); |
| 861 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 862 | gpriv->gadget.speed = USB_SPEED_UNKNOWN; |
| 863 | |
| 864 | /* disable sys */ |
Kuninori Morimoto | dfbb7f4 | 2011-11-24 17:28:35 -0800 | [diff] [blame] | 865 | usbhs_sys_set_test_mode(priv, 0); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 866 | usbhs_sys_function_ctrl(priv, 0); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 867 | |
Kuninori Morimoto | d9fa298 | 2012-12-10 22:44:07 -0800 | [diff] [blame] | 868 | usbhsg_ep_disable(&dcp->ep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 869 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 870 | dev_dbg(dev, "stop gadget\n"); |
| 871 | |
| 872 | return 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /* |
| 876 | * |
| 877 | * linux usb function |
| 878 | * |
| 879 | */ |
Felipe Balbi | af1d705 | 2011-10-10 17:11:20 +0300 | [diff] [blame] | 880 | static int usbhsg_gadget_start(struct usb_gadget *gadget, |
| 881 | struct usb_gadget_driver *driver) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 882 | { |
Felipe Balbi | af1d705 | 2011-10-10 17:11:20 +0300 | [diff] [blame] | 883 | struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); |
Kuninori Morimoto | f8eff0a | 2011-11-17 18:19:06 -0800 | [diff] [blame] | 884 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 885 | |
Felipe Balbi | af1d705 | 2011-10-10 17:11:20 +0300 | [diff] [blame] | 886 | if (!driver || |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 887 | !driver->setup || |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 888 | driver->max_speed < USB_SPEED_FULL) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 889 | return -EINVAL; |
Kuninori Morimoto | 3b87218 | 2011-07-03 17:42:35 -0700 | [diff] [blame] | 890 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 891 | /* first hook up the driver ... */ |
| 892 | gpriv->driver = driver; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 893 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 894 | return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 895 | } |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 896 | |
Felipe Balbi | 22835b8 | 2014-10-17 12:05:12 -0500 | [diff] [blame] | 897 | static int usbhsg_gadget_stop(struct usb_gadget *gadget) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 898 | { |
Felipe Balbi | af1d705 | 2011-10-10 17:11:20 +0300 | [diff] [blame] | 899 | struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); |
Kuninori Morimoto | f8eff0a | 2011-11-17 18:19:06 -0800 | [diff] [blame] | 900 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 901 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 902 | usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 903 | gpriv->driver = NULL; |
| 904 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 905 | return 0; |
| 906 | } |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | * usb gadget ops |
| 910 | */ |
| 911 | static int usbhsg_get_frame(struct usb_gadget *gadget) |
| 912 | { |
| 913 | struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); |
| 914 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
| 915 | |
| 916 | return usbhs_frame_get_num(priv); |
| 917 | } |
| 918 | |
kuninori.morimoto.gx@renesas.com | 4cd2f59 | 2012-10-15 23:24:19 -0700 | [diff] [blame] | 919 | static int usbhsg_pullup(struct usb_gadget *gadget, int is_on) |
| 920 | { |
| 921 | struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); |
| 922 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
Takeshi Kihara | 04a5def | 2014-11-04 10:05:43 +0900 | [diff] [blame] | 923 | unsigned long flags; |
kuninori.morimoto.gx@renesas.com | 4cd2f59 | 2012-10-15 23:24:19 -0700 | [diff] [blame] | 924 | |
Takeshi Kihara | 04a5def | 2014-11-04 10:05:43 +0900 | [diff] [blame] | 925 | usbhs_lock(priv, flags); |
| 926 | if (is_on) |
| 927 | usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT); |
| 928 | else |
| 929 | usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT); |
| 930 | usbhsg_update_pullup(priv); |
| 931 | usbhs_unlock(priv, flags); |
kuninori.morimoto.gx@renesas.com | 4cd2f59 | 2012-10-15 23:24:19 -0700 | [diff] [blame] | 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
Shimoda, Yoshihiro | cac402d | 2012-03-16 13:10:15 +0900 | [diff] [blame] | 936 | static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self) |
| 937 | { |
| 938 | struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); |
| 939 | |
| 940 | if (is_self) |
| 941 | usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED); |
| 942 | else |
| 943 | usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED); |
| 944 | |
Peter Chen | a17fd41 | 2015-01-28 16:32:28 +0800 | [diff] [blame] | 945 | gadget->is_selfpowered = (is_self != 0); |
| 946 | |
Shimoda, Yoshihiro | cac402d | 2012-03-16 13:10:15 +0900 | [diff] [blame] | 947 | return 0; |
| 948 | } |
| 949 | |
Felipe Balbi | eeef458 | 2013-01-24 17:58:16 +0200 | [diff] [blame] | 950 | static const struct usb_gadget_ops usbhsg_gadget_ops = { |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 951 | .get_frame = usbhsg_get_frame, |
Shimoda, Yoshihiro | cac402d | 2012-03-16 13:10:15 +0900 | [diff] [blame] | 952 | .set_selfpowered = usbhsg_set_selfpowered, |
Felipe Balbi | af1d705 | 2011-10-10 17:11:20 +0300 | [diff] [blame] | 953 | .udc_start = usbhsg_gadget_start, |
| 954 | .udc_stop = usbhsg_gadget_stop, |
kuninori.morimoto.gx@renesas.com | 4cd2f59 | 2012-10-15 23:24:19 -0700 | [diff] [blame] | 955 | .pullup = usbhsg_pullup, |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 956 | }; |
| 957 | |
| 958 | static int usbhsg_start(struct usbhs_priv *priv) |
| 959 | { |
| 960 | return usbhsg_try_start(priv, USBHSG_STATUS_STARTED); |
| 961 | } |
| 962 | |
| 963 | static int usbhsg_stop(struct usbhs_priv *priv) |
| 964 | { |
Kuninori Morimoto | 8885a89 | 2011-11-17 18:19:38 -0800 | [diff] [blame] | 965 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 966 | |
| 967 | /* cable disconnect */ |
| 968 | if (gpriv->driver && |
| 969 | gpriv->driver->disconnect) |
| 970 | gpriv->driver->disconnect(&gpriv->gadget); |
| 971 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 972 | return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED); |
| 973 | } |
| 974 | |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 975 | int usbhs_mod_gadget_probe(struct usbhs_priv *priv) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 976 | { |
| 977 | struct usbhsg_gpriv *gpriv; |
| 978 | struct usbhsg_uep *uep; |
| 979 | struct device *dev = usbhs_priv_to_dev(priv); |
| 980 | int pipe_size = usbhs_get_dparam(priv, pipe_size); |
| 981 | int i; |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 982 | int ret; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 983 | |
| 984 | gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL); |
| 985 | if (!gpriv) { |
| 986 | dev_err(dev, "Could not allocate gadget priv\n"); |
| 987 | return -ENOMEM; |
| 988 | } |
| 989 | |
| 990 | uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL); |
| 991 | if (!uep) { |
| 992 | dev_err(dev, "Could not allocate ep\n"); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 993 | ret = -ENOMEM; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 994 | goto usbhs_mod_gadget_probe_err_gpriv; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * CAUTION |
| 999 | * |
| 1000 | * There is no guarantee that it is possible to access usb module here. |
| 1001 | * Don't accesses to it. |
| 1002 | * The accesse will be enable after "usbhsg_start" |
| 1003 | */ |
| 1004 | |
| 1005 | /* |
| 1006 | * register itself |
| 1007 | */ |
| 1008 | usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET); |
| 1009 | |
| 1010 | /* init gpriv */ |
| 1011 | gpriv->mod.name = "gadget"; |
| 1012 | gpriv->mod.start = usbhsg_start; |
| 1013 | gpriv->mod.stop = usbhsg_stop; |
| 1014 | gpriv->uep = uep; |
| 1015 | gpriv->uep_size = pipe_size; |
| 1016 | usbhsg_status_init(gpriv); |
| 1017 | |
| 1018 | /* |
| 1019 | * init gadget |
| 1020 | */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1021 | gpriv->gadget.dev.parent = dev; |
| 1022 | gpriv->gadget.name = "renesas_usbhs_udc"; |
| 1023 | gpriv->gadget.ops = &usbhsg_gadget_ops; |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 1024 | gpriv->gadget.max_speed = USB_SPEED_HIGH; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1025 | |
| 1026 | INIT_LIST_HEAD(&gpriv->gadget.ep_list); |
| 1027 | |
| 1028 | /* |
| 1029 | * init usb_ep |
| 1030 | */ |
| 1031 | usbhsg_for_each_uep_with_dcp(uep, gpriv, i) { |
| 1032 | uep->gpriv = gpriv; |
Kuninori Morimoto | 5848294 | 2012-12-10 22:43:45 -0800 | [diff] [blame] | 1033 | uep->pipe = NULL; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1034 | snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i); |
| 1035 | |
| 1036 | uep->ep.name = uep->ep_name; |
| 1037 | uep->ep.ops = &usbhsg_ep_ops; |
| 1038 | INIT_LIST_HEAD(&uep->ep.ep_list); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1039 | |
| 1040 | /* init DCP */ |
| 1041 | if (usbhsg_is_dcp(uep)) { |
| 1042 | gpriv->gadget.ep0 = &uep->ep; |
Robert Baldyga | e117e74 | 2013-12-13 12:23:38 +0100 | [diff] [blame] | 1043 | usb_ep_set_maxpacket_limit(&uep->ep, 64); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1044 | } |
| 1045 | /* init normal pipe */ |
| 1046 | else { |
Robert Baldyga | e117e74 | 2013-12-13 12:23:38 +0100 | [diff] [blame] | 1047 | usb_ep_set_maxpacket_limit(&uep->ep, 512); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1048 | list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1052 | ret = usb_add_gadget_udc(dev, &gpriv->gadget); |
| 1053 | if (ret) |
Felipe Balbi | 0972ef7 | 2013-01-24 17:08:01 +0200 | [diff] [blame] | 1054 | goto err_add_udc; |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1055 | |
| 1056 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1057 | dev_info(dev, "gadget probed\n"); |
| 1058 | |
| 1059 | return 0; |
Kuninori Morimoto | f8eff0a | 2011-11-17 18:19:06 -0800 | [diff] [blame] | 1060 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1061 | err_add_udc: |
| 1062 | kfree(gpriv->uep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1063 | |
| 1064 | usbhs_mod_gadget_probe_err_gpriv: |
| 1065 | kfree(gpriv); |
| 1066 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1067 | return ret; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1068 | } |
| 1069 | |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 1070 | void usbhs_mod_gadget_remove(struct usbhs_priv *priv) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1071 | { |
| 1072 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 1073 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1074 | usb_del_gadget_udc(&gpriv->gadget); |
Kuninori Morimoto | 3b87218 | 2011-07-03 17:42:35 -0700 | [diff] [blame] | 1075 | |
Sebastian Andrzej Siewior | 3af51ac | 2011-06-03 19:50:48 +0200 | [diff] [blame] | 1076 | kfree(gpriv->uep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 1077 | kfree(gpriv); |
| 1078 | } |