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