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