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 | /* |
| 117 | * list push/pop |
| 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 | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 163 | /* |
| 164 | * USB_TYPE_STANDARD / clear feature functions |
| 165 | */ |
| 166 | static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv, |
| 167 | struct usbhsg_uep *uep, |
| 168 | struct usb_ctrlrequest *ctrl) |
| 169 | { |
| 170 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 171 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 172 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); |
| 173 | |
| 174 | usbhs_dcp_control_transfer_done(pipe); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv, |
| 180 | struct usbhsg_uep *uep, |
| 181 | struct usb_ctrlrequest *ctrl) |
| 182 | { |
| 183 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 184 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
| 185 | |
| 186 | if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) { |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 187 | usbhs_pipe_disable(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 188 | usbhs_pipe_clear_sequence(pipe); |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 189 | usbhs_pipe_enable(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | usbhsg_recip_handler_std_control_done(priv, uep, ctrl); |
| 193 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | struct usbhsg_recip_handle req_clear_feature = { |
| 198 | .name = "clear feature", |
| 199 | .device = usbhsg_recip_handler_std_control_done, |
| 200 | .interface = usbhsg_recip_handler_std_control_done, |
| 201 | .endpoint = usbhsg_recip_handler_std_clear_endpoint, |
| 202 | }; |
| 203 | |
| 204 | /* |
| 205 | * USB_TYPE handler |
| 206 | */ |
| 207 | static int usbhsg_recip_run_handle(struct usbhs_priv *priv, |
| 208 | struct usbhsg_recip_handle *handler, |
| 209 | struct usb_ctrlrequest *ctrl) |
| 210 | { |
| 211 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 212 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 213 | struct usbhsg_uep *uep; |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 214 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 215 | int recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 216 | int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; |
| 217 | int ret; |
| 218 | int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep, |
| 219 | struct usb_ctrlrequest *ctrl); |
| 220 | char *msg; |
| 221 | |
| 222 | uep = usbhsg_gpriv_to_nth_uep(gpriv, nth); |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 223 | pipe = usbhsg_uep_to_pipe(uep); |
| 224 | if (!pipe) { |
Kuninori Morimoto | 9a28b7b | 2011-04-21 14:10:12 +0900 | [diff] [blame] | 225 | dev_err(dev, "wrong recip request\n"); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 226 | ret = -EINVAL; |
| 227 | goto usbhsg_recip_run_handle_end; |
Kuninori Morimoto | 9a28b7b | 2011-04-21 14:10:12 +0900 | [diff] [blame] | 228 | } |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 229 | |
| 230 | switch (recip) { |
| 231 | case USB_RECIP_DEVICE: |
| 232 | msg = "DEVICE"; |
| 233 | func = handler->device; |
| 234 | break; |
| 235 | case USB_RECIP_INTERFACE: |
| 236 | msg = "INTERFACE"; |
| 237 | func = handler->interface; |
| 238 | break; |
| 239 | case USB_RECIP_ENDPOINT: |
| 240 | msg = "ENDPOINT"; |
| 241 | func = handler->endpoint; |
| 242 | break; |
| 243 | default: |
| 244 | dev_warn(dev, "unsupported RECIP(%d)\n", recip); |
| 245 | func = NULL; |
| 246 | ret = -EINVAL; |
| 247 | } |
| 248 | |
| 249 | if (func) { |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 250 | unsigned long flags; |
| 251 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 252 | dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 253 | |
| 254 | /******************** spin lock ********************/ |
| 255 | usbhs_lock(priv, flags); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 256 | ret = func(priv, uep, ctrl); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 257 | usbhs_unlock(priv, flags); |
| 258 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 261 | usbhsg_recip_run_handle_end: |
Kuninori Morimoto | 0432eed | 2011-06-06 14:18:54 +0900 | [diff] [blame] | 262 | usbhs_pkt_start(pipe); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 263 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * irq functions |
| 269 | * |
| 270 | * it will be called from usbhs_interrupt |
| 271 | */ |
| 272 | static int usbhsg_irq_dev_state(struct usbhs_priv *priv, |
| 273 | struct usbhs_irq_state *irq_state) |
| 274 | { |
| 275 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 276 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 277 | |
| 278 | gpriv->gadget.speed = usbhs_status_get_usb_speed(irq_state); |
| 279 | |
| 280 | dev_dbg(dev, "state = %x : speed : %d\n", |
| 281 | usbhs_status_get_device_state(irq_state), |
| 282 | gpriv->gadget.speed); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv, |
| 288 | struct usbhs_irq_state *irq_state) |
| 289 | { |
| 290 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 291 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 292 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); |
| 293 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 294 | struct usb_ctrlrequest ctrl; |
| 295 | struct usbhsg_recip_handle *recip_handler = NULL; |
| 296 | int stage = usbhs_status_get_ctrl_stage(irq_state); |
| 297 | int ret = 0; |
| 298 | |
| 299 | dev_dbg(dev, "stage = %d\n", stage); |
| 300 | |
| 301 | /* |
| 302 | * see Manual |
| 303 | * |
| 304 | * "Operation" |
| 305 | * - "Interrupt Function" |
| 306 | * - "Control Transfer Stage Transition Interrupt" |
| 307 | * - Fig. "Control Transfer Stage Transitions" |
| 308 | */ |
| 309 | |
| 310 | switch (stage) { |
| 311 | case READ_DATA_STAGE: |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame^] | 312 | dcp->handler = &usbhs_fifo_pio_push_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 313 | break; |
| 314 | case WRITE_DATA_STAGE: |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame^] | 315 | dcp->handler = &usbhs_fifo_pio_pop_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 316 | break; |
| 317 | case NODATA_STATUS_STAGE: |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 318 | dcp->handler = &usbhs_ctrl_stage_end_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 319 | break; |
| 320 | default: |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * get usb request |
| 326 | */ |
| 327 | usbhs_usbreq_get_val(priv, &ctrl); |
| 328 | |
| 329 | switch (ctrl.bRequestType & USB_TYPE_MASK) { |
| 330 | case USB_TYPE_STANDARD: |
| 331 | switch (ctrl.bRequest) { |
| 332 | case USB_REQ_CLEAR_FEATURE: |
| 333 | recip_handler = &req_clear_feature; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * setup stage / run recip |
| 340 | */ |
| 341 | if (recip_handler) |
| 342 | ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl); |
| 343 | else |
| 344 | ret = gpriv->driver->setup(&gpriv->gadget, &ctrl); |
| 345 | |
| 346 | if (ret < 0) |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 347 | usbhs_pipe_stall(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 348 | |
| 349 | return ret; |
| 350 | } |
| 351 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 352 | /* |
| 353 | * |
| 354 | * usb_dcp_ops |
| 355 | * |
| 356 | */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 357 | static int usbhsg_pipe_disable(struct usbhsg_uep *uep) |
| 358 | { |
| 359 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
Kuninori Morimoto | 8a2c225 | 2011-06-06 14:18:33 +0900 | [diff] [blame] | 360 | struct usbhs_pkt *pkt; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 361 | |
Kuninori Morimoto | e8d548d | 2011-06-06 14:18:03 +0900 | [diff] [blame] | 362 | usbhs_pipe_disable(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 363 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 364 | while (1) { |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 365 | pkt = usbhs_pkt_pop(pipe, NULL); |
Kuninori Morimoto | 8a2c225 | 2011-06-06 14:18:33 +0900 | [diff] [blame] | 366 | if (!pkt) |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 367 | break; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 368 | } |
| 369 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
Kuninori Morimoto | 409ba9e | 2011-04-26 09:21:35 +0900 | [diff] [blame] | 373 | static void usbhsg_uep_init(struct usbhsg_gpriv *gpriv) |
| 374 | { |
| 375 | int i; |
| 376 | struct usbhsg_uep *uep; |
| 377 | |
| 378 | usbhsg_for_each_uep_with_dcp(uep, gpriv, i) |
| 379 | uep->pipe = NULL; |
| 380 | } |
| 381 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 382 | /* |
| 383 | * |
| 384 | * usb_ep_ops |
| 385 | * |
| 386 | */ |
| 387 | static int usbhsg_ep_enable(struct usb_ep *ep, |
| 388 | const struct usb_endpoint_descriptor *desc) |
| 389 | { |
| 390 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
| 391 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 392 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
| 393 | struct usbhs_pipe *pipe; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 394 | int ret = -EIO; |
| 395 | |
Kuninori Morimoto | 409ba9e | 2011-04-26 09:21:35 +0900 | [diff] [blame] | 396 | /* |
| 397 | * if it already have pipe, |
| 398 | * nothing to do |
| 399 | */ |
| 400 | if (uep->pipe) |
| 401 | return 0; |
| 402 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 403 | pipe = usbhs_pipe_malloc(priv, desc); |
| 404 | if (pipe) { |
| 405 | uep->pipe = pipe; |
| 406 | pipe->mod_private = uep; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 407 | |
| 408 | if (usb_endpoint_dir_in(desc)) |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame^] | 409 | uep->handler = &usbhs_fifo_pio_push_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 410 | else |
Kuninori Morimoto | 0cb7e61 | 2011-06-06 14:18:58 +0900 | [diff] [blame^] | 411 | uep->handler = &usbhs_fifo_pio_pop_handler; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 412 | |
| 413 | ret = 0; |
| 414 | } |
Kuninori Morimoto | cb96632 | 2011-04-21 14:10:08 +0900 | [diff] [blame] | 415 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | static int usbhsg_ep_disable(struct usb_ep *ep) |
| 420 | { |
| 421 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 422 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 423 | return usbhsg_pipe_disable(uep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep, |
| 427 | gfp_t gfp_flags) |
| 428 | { |
| 429 | struct usbhsg_request *ureq; |
| 430 | |
| 431 | ureq = kzalloc(sizeof *ureq, gfp_flags); |
| 432 | if (!ureq) |
| 433 | return NULL; |
| 434 | |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 435 | usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq)); |
| 436 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 437 | return &ureq->req; |
| 438 | } |
| 439 | |
| 440 | static void usbhsg_ep_free_request(struct usb_ep *ep, |
| 441 | struct usb_request *req) |
| 442 | { |
| 443 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); |
| 444 | |
Kuninori Morimoto | 6acb95d | 2011-06-06 14:18:16 +0900 | [diff] [blame] | 445 | WARN_ON(!list_empty(&ureq->pkt.node)); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 446 | kfree(ureq); |
| 447 | } |
| 448 | |
| 449 | static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req, |
| 450 | gfp_t gfp_flags) |
| 451 | { |
| 452 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
| 453 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
| 454 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); |
| 455 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 456 | |
| 457 | /* param check */ |
| 458 | if (usbhsg_is_not_connected(gpriv) || |
| 459 | unlikely(!gpriv->driver) || |
| 460 | unlikely(!pipe)) |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 461 | return -ESHUTDOWN; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 462 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 463 | usbhsg_queue_push(uep, ureq); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 464 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 465 | return 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) |
| 469 | { |
| 470 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
| 471 | struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 472 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 473 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 474 | usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq)); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 475 | usbhsg_queue_pop(uep, ureq, -ECONNRESET); |
| 476 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge) |
| 481 | { |
| 482 | struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); |
| 483 | struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); |
| 484 | struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 485 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 486 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 487 | unsigned long flags; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 488 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 489 | usbhsg_pipe_disable(uep); |
| 490 | |
| 491 | dev_dbg(dev, "set halt %d (pipe %d)\n", |
| 492 | halt, usbhs_pipe_number(pipe)); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 493 | |
| 494 | /******************** spin lock ********************/ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 495 | usbhs_lock(priv, flags); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 496 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 497 | if (halt) |
| 498 | usbhs_pipe_stall(pipe); |
| 499 | else |
| 500 | usbhs_pipe_disable(pipe); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 501 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 502 | if (halt && wedge) |
| 503 | usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE); |
| 504 | else |
| 505 | usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 506 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 507 | usbhs_unlock(priv, flags); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 508 | /******************** spin unlock ******************/ |
| 509 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 510 | return 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int usbhsg_ep_set_halt(struct usb_ep *ep, int value) |
| 514 | { |
| 515 | return __usbhsg_ep_set_halt_wedge(ep, value, 0); |
| 516 | } |
| 517 | |
| 518 | static int usbhsg_ep_set_wedge(struct usb_ep *ep) |
| 519 | { |
| 520 | return __usbhsg_ep_set_halt_wedge(ep, 1, 1); |
| 521 | } |
| 522 | |
| 523 | static struct usb_ep_ops usbhsg_ep_ops = { |
| 524 | .enable = usbhsg_ep_enable, |
| 525 | .disable = usbhsg_ep_disable, |
| 526 | |
| 527 | .alloc_request = usbhsg_ep_alloc_request, |
| 528 | .free_request = usbhsg_ep_free_request, |
| 529 | |
| 530 | .queue = usbhsg_ep_queue, |
| 531 | .dequeue = usbhsg_ep_dequeue, |
| 532 | |
| 533 | .set_halt = usbhsg_ep_set_halt, |
| 534 | .set_wedge = usbhsg_ep_set_wedge, |
| 535 | }; |
| 536 | |
| 537 | /* |
| 538 | * usb module start/end |
| 539 | */ |
| 540 | static int usbhsg_try_start(struct usbhs_priv *priv, u32 status) |
| 541 | { |
| 542 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 543 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 544 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 545 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 546 | unsigned long flags; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 547 | int ret = 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 548 | |
| 549 | /******************** spin lock ********************/ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 550 | usbhs_lock(priv, flags); |
| 551 | |
| 552 | usbhsg_status_set(gpriv, status); |
| 553 | if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && |
| 554 | usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))) |
| 555 | ret = -1; /* not ready */ |
| 556 | |
| 557 | usbhs_unlock(priv, flags); |
| 558 | /******************** spin unlock ********************/ |
| 559 | |
| 560 | if (ret < 0) |
| 561 | return 0; /* not ready is not error */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 562 | |
| 563 | /* |
| 564 | * enable interrupt and systems if ready |
| 565 | */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 566 | dev_dbg(dev, "start gadget\n"); |
| 567 | |
| 568 | /* |
| 569 | * pipe initialize and enable DCP |
| 570 | */ |
Kuninori Morimoto | 4bd0481 | 2011-06-06 14:18:07 +0900 | [diff] [blame] | 571 | usbhs_pipe_init(priv, |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 572 | usbhsg_queue_done); |
| 573 | usbhs_fifo_init(priv); |
Kuninori Morimoto | 409ba9e | 2011-04-26 09:21:35 +0900 | [diff] [blame] | 574 | usbhsg_uep_init(gpriv); |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 575 | |
| 576 | /* dcp init */ |
| 577 | dcp->pipe = usbhs_dcp_malloc(priv); |
| 578 | dcp->pipe->mod_private = dcp; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * system config enble |
| 582 | * - HI speed |
| 583 | * - function |
| 584 | * - usb module |
| 585 | */ |
| 586 | usbhs_sys_hispeed_ctrl(priv, 1); |
| 587 | usbhs_sys_function_ctrl(priv, 1); |
| 588 | usbhs_sys_usb_ctrl(priv, 1); |
| 589 | |
| 590 | /* |
| 591 | * enable irq callback |
| 592 | */ |
| 593 | mod->irq_dev_state = usbhsg_irq_dev_state; |
| 594 | mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 595 | usbhs_irq_callback_update(priv, mod); |
| 596 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status) |
| 601 | { |
| 602 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 603 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 604 | struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); |
| 605 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 606 | unsigned long flags; |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 607 | int ret = 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 608 | |
| 609 | /******************** spin lock ********************/ |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 610 | usbhs_lock(priv, flags); |
| 611 | |
| 612 | usbhsg_status_clr(gpriv, status); |
| 613 | if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && |
| 614 | !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)) |
| 615 | ret = -1; /* already done */ |
| 616 | |
| 617 | usbhs_unlock(priv, flags); |
| 618 | /******************** spin unlock ********************/ |
| 619 | |
| 620 | if (ret < 0) |
| 621 | return 0; /* already done is not error */ |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | * disable interrupt and systems if 1st try |
| 625 | */ |
Kuninori Morimoto | dad6739 | 2011-06-06 14:18:28 +0900 | [diff] [blame] | 626 | usbhs_fifo_quit(priv); |
| 627 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 628 | /* disable all irq */ |
| 629 | mod->irq_dev_state = NULL; |
| 630 | mod->irq_ctrl_stage = NULL; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 631 | usbhs_irq_callback_update(priv, mod); |
| 632 | |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 633 | gpriv->gadget.speed = USB_SPEED_UNKNOWN; |
| 634 | |
| 635 | /* disable sys */ |
| 636 | usbhs_sys_hispeed_ctrl(priv, 0); |
| 637 | usbhs_sys_function_ctrl(priv, 0); |
| 638 | usbhs_sys_usb_ctrl(priv, 0); |
| 639 | |
Kuninori Morimoto | 97664a2 | 2011-06-06 14:18:38 +0900 | [diff] [blame] | 640 | usbhsg_pipe_disable(dcp); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 641 | |
| 642 | if (gpriv->driver && |
| 643 | gpriv->driver->disconnect) |
| 644 | gpriv->driver->disconnect(&gpriv->gadget); |
| 645 | |
| 646 | dev_dbg(dev, "stop gadget\n"); |
| 647 | |
| 648 | return 0; |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* |
| 652 | * |
| 653 | * linux usb function |
| 654 | * |
| 655 | */ |
| 656 | struct usbhsg_gpriv *the_controller; |
| 657 | int usb_gadget_probe_driver(struct usb_gadget_driver *driver, |
| 658 | int (*bind)(struct usb_gadget *)) |
| 659 | { |
| 660 | struct usbhsg_gpriv *gpriv = the_controller; |
| 661 | struct usbhs_priv *priv; |
| 662 | struct device *dev; |
| 663 | int ret; |
| 664 | |
| 665 | if (!bind || |
| 666 | !driver || |
| 667 | !driver->setup || |
| 668 | driver->speed != USB_SPEED_HIGH) |
| 669 | return -EINVAL; |
| 670 | if (!gpriv) |
| 671 | return -ENODEV; |
| 672 | if (gpriv->driver) |
| 673 | return -EBUSY; |
| 674 | |
| 675 | dev = usbhsg_gpriv_to_dev(gpriv); |
| 676 | priv = usbhsg_gpriv_to_priv(gpriv); |
| 677 | |
| 678 | /* first hook up the driver ... */ |
| 679 | gpriv->driver = driver; |
| 680 | gpriv->gadget.dev.driver = &driver->driver; |
| 681 | |
| 682 | ret = device_add(&gpriv->gadget.dev); |
| 683 | if (ret) { |
| 684 | dev_err(dev, "device_add error %d\n", ret); |
| 685 | goto add_fail; |
| 686 | } |
| 687 | |
| 688 | ret = bind(&gpriv->gadget); |
| 689 | if (ret) { |
| 690 | dev_err(dev, "bind to driver %s error %d\n", |
| 691 | driver->driver.name, ret); |
| 692 | goto bind_fail; |
| 693 | } |
| 694 | |
| 695 | dev_dbg(dev, "bind %s\n", driver->driver.name); |
| 696 | |
| 697 | return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD); |
| 698 | |
| 699 | bind_fail: |
| 700 | device_del(&gpriv->gadget.dev); |
| 701 | add_fail: |
| 702 | gpriv->driver = NULL; |
| 703 | gpriv->gadget.dev.driver = NULL; |
| 704 | |
| 705 | return ret; |
| 706 | } |
| 707 | EXPORT_SYMBOL(usb_gadget_probe_driver); |
| 708 | |
| 709 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 710 | { |
| 711 | struct usbhsg_gpriv *gpriv = the_controller; |
| 712 | struct usbhs_priv *priv; |
| 713 | struct device *dev = usbhsg_gpriv_to_dev(gpriv); |
| 714 | |
| 715 | if (!gpriv) |
| 716 | return -ENODEV; |
| 717 | |
| 718 | if (!driver || |
| 719 | !driver->unbind || |
| 720 | driver != gpriv->driver) |
| 721 | return -EINVAL; |
| 722 | |
| 723 | dev = usbhsg_gpriv_to_dev(gpriv); |
| 724 | priv = usbhsg_gpriv_to_priv(gpriv); |
| 725 | |
| 726 | usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD); |
| 727 | device_del(&gpriv->gadget.dev); |
| 728 | gpriv->driver = NULL; |
| 729 | |
| 730 | if (driver->disconnect) |
| 731 | driver->disconnect(&gpriv->gadget); |
| 732 | |
| 733 | driver->unbind(&gpriv->gadget); |
| 734 | dev_dbg(dev, "unbind %s\n", driver->driver.name); |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | EXPORT_SYMBOL(usb_gadget_unregister_driver); |
| 739 | |
| 740 | /* |
| 741 | * usb gadget ops |
| 742 | */ |
| 743 | static int usbhsg_get_frame(struct usb_gadget *gadget) |
| 744 | { |
| 745 | struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); |
| 746 | struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); |
| 747 | |
| 748 | return usbhs_frame_get_num(priv); |
| 749 | } |
| 750 | |
| 751 | static struct usb_gadget_ops usbhsg_gadget_ops = { |
| 752 | .get_frame = usbhsg_get_frame, |
| 753 | }; |
| 754 | |
| 755 | static int usbhsg_start(struct usbhs_priv *priv) |
| 756 | { |
| 757 | return usbhsg_try_start(priv, USBHSG_STATUS_STARTED); |
| 758 | } |
| 759 | |
| 760 | static int usbhsg_stop(struct usbhs_priv *priv) |
| 761 | { |
| 762 | return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED); |
| 763 | } |
| 764 | |
| 765 | int __devinit usbhs_mod_gadget_probe(struct usbhs_priv *priv) |
| 766 | { |
| 767 | struct usbhsg_gpriv *gpriv; |
| 768 | struct usbhsg_uep *uep; |
| 769 | struct device *dev = usbhs_priv_to_dev(priv); |
| 770 | int pipe_size = usbhs_get_dparam(priv, pipe_size); |
| 771 | int i; |
| 772 | |
| 773 | gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL); |
| 774 | if (!gpriv) { |
| 775 | dev_err(dev, "Could not allocate gadget priv\n"); |
| 776 | return -ENOMEM; |
| 777 | } |
| 778 | |
| 779 | uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL); |
| 780 | if (!uep) { |
| 781 | dev_err(dev, "Could not allocate ep\n"); |
| 782 | goto usbhs_mod_gadget_probe_err_gpriv; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * CAUTION |
| 787 | * |
| 788 | * There is no guarantee that it is possible to access usb module here. |
| 789 | * Don't accesses to it. |
| 790 | * The accesse will be enable after "usbhsg_start" |
| 791 | */ |
| 792 | |
| 793 | /* |
| 794 | * register itself |
| 795 | */ |
| 796 | usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET); |
| 797 | |
| 798 | /* init gpriv */ |
| 799 | gpriv->mod.name = "gadget"; |
| 800 | gpriv->mod.start = usbhsg_start; |
| 801 | gpriv->mod.stop = usbhsg_stop; |
| 802 | gpriv->uep = uep; |
| 803 | gpriv->uep_size = pipe_size; |
| 804 | usbhsg_status_init(gpriv); |
| 805 | |
| 806 | /* |
| 807 | * init gadget |
| 808 | */ |
| 809 | device_initialize(&gpriv->gadget.dev); |
| 810 | dev_set_name(&gpriv->gadget.dev, "gadget"); |
| 811 | gpriv->gadget.dev.parent = dev; |
| 812 | gpriv->gadget.name = "renesas_usbhs_udc"; |
| 813 | gpriv->gadget.ops = &usbhsg_gadget_ops; |
| 814 | gpriv->gadget.is_dualspeed = 1; |
| 815 | |
| 816 | INIT_LIST_HEAD(&gpriv->gadget.ep_list); |
| 817 | |
| 818 | /* |
| 819 | * init usb_ep |
| 820 | */ |
| 821 | usbhsg_for_each_uep_with_dcp(uep, gpriv, i) { |
| 822 | uep->gpriv = gpriv; |
| 823 | snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i); |
| 824 | |
| 825 | uep->ep.name = uep->ep_name; |
| 826 | uep->ep.ops = &usbhsg_ep_ops; |
| 827 | INIT_LIST_HEAD(&uep->ep.ep_list); |
Kuninori Morimoto | 2f98382 | 2011-04-05 11:40:54 +0900 | [diff] [blame] | 828 | |
| 829 | /* init DCP */ |
| 830 | if (usbhsg_is_dcp(uep)) { |
| 831 | gpriv->gadget.ep0 = &uep->ep; |
| 832 | uep->ep.maxpacket = 64; |
| 833 | } |
| 834 | /* init normal pipe */ |
| 835 | else { |
| 836 | uep->ep.maxpacket = 512; |
| 837 | list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | the_controller = gpriv; |
| 842 | |
| 843 | dev_info(dev, "gadget probed\n"); |
| 844 | |
| 845 | return 0; |
| 846 | |
| 847 | usbhs_mod_gadget_probe_err_gpriv: |
| 848 | kfree(gpriv); |
| 849 | |
| 850 | return -ENOMEM; |
| 851 | } |
| 852 | |
| 853 | void __devexit usbhs_mod_gadget_remove(struct usbhs_priv *priv) |
| 854 | { |
| 855 | struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); |
| 856 | |
| 857 | kfree(gpriv); |
| 858 | } |