Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1 | /* |
| 2 | * driver/usb/gadget/fsl_qe_udc.c |
| 3 | * |
| 4 | * Copyright (c) 2006-2008 Freescale Semiconductor, Inc. All rights reserved. |
| 5 | * |
| 6 | * Xie Xiaobo <X.Xie@freescale.com> |
| 7 | * Li Yang <leoli@freescale.com> |
| 8 | * Based on bareboard code from Shlomi Gridish. |
| 9 | * |
| 10 | * Description: |
| 11 | * Freescle QE/CPM USB Pheripheral Controller Driver |
| 12 | * The controller can be found on MPC8360, MPC8272, and etc. |
| 13 | * MPC8360 Rev 1.1 may need QE mircocode update |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify it |
| 16 | * under the terms of the GNU General Public License as published by the |
| 17 | * Free Software Foundation; either version 2 of the License, or (at your |
| 18 | * option) any later version. |
| 19 | */ |
| 20 | |
| 21 | #undef USB_TRACE |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/ioport.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/list.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/io.h> |
| 33 | #include <linux/moduleparam.h> |
| 34 | #include <linux/of_platform.h> |
| 35 | #include <linux/dma-mapping.h> |
| 36 | #include <linux/usb/ch9.h> |
| 37 | #include <linux/usb/gadget.h> |
| 38 | #include <linux/usb/otg.h> |
| 39 | #include <asm/qe.h> |
| 40 | #include <asm/cpm.h> |
| 41 | #include <asm/dma.h> |
| 42 | #include <asm/reg.h> |
| 43 | #include "fsl_qe_udc.h" |
| 44 | |
| 45 | #define DRIVER_DESC "Freescale QE/CPM USB Device Controller driver" |
| 46 | #define DRIVER_AUTHOR "Xie XiaoBo" |
| 47 | #define DRIVER_VERSION "1.0" |
| 48 | |
| 49 | #define DMA_ADDR_INVALID (~(dma_addr_t)0) |
| 50 | |
| 51 | static const char driver_name[] = "fsl_qe_udc"; |
| 52 | static const char driver_desc[] = DRIVER_DESC; |
| 53 | |
| 54 | /*ep name is important in gadget, it should obey the convention of ep_match()*/ |
| 55 | static const char *const ep_name[] = { |
| 56 | "ep0-control", /* everyone has ep0 */ |
| 57 | /* 3 configurable endpoints */ |
| 58 | "ep1", |
| 59 | "ep2", |
| 60 | "ep3", |
| 61 | }; |
| 62 | |
| 63 | static struct usb_endpoint_descriptor qe_ep0_desc = { |
| 64 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 65 | .bDescriptorType = USB_DT_ENDPOINT, |
| 66 | |
| 67 | .bEndpointAddress = 0, |
| 68 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 69 | .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD, |
| 70 | }; |
| 71 | |
| 72 | /* it is initialized in probe() */ |
| 73 | static struct qe_udc *udc_controller; |
| 74 | |
| 75 | /******************************************************************** |
| 76 | * Internal Used Function Start |
| 77 | ********************************************************************/ |
| 78 | /*----------------------------------------------------------------- |
| 79 | * done() - retire a request; caller blocked irqs |
| 80 | *--------------------------------------------------------------*/ |
| 81 | static void done(struct qe_ep *ep, struct qe_req *req, int status) |
| 82 | { |
| 83 | struct qe_udc *udc = ep->udc; |
| 84 | unsigned char stopped = ep->stopped; |
| 85 | |
| 86 | /* the req->queue pointer is used by ep_queue() func, in which |
| 87 | * the request will be added into a udc_ep->queue 'd tail |
| 88 | * so here the req will be dropped from the ep->queue |
| 89 | */ |
| 90 | list_del_init(&req->queue); |
| 91 | |
| 92 | /* req.status should be set as -EINPROGRESS in ep_queue() */ |
| 93 | if (req->req.status == -EINPROGRESS) |
| 94 | req->req.status = status; |
| 95 | else |
| 96 | status = req->req.status; |
| 97 | |
| 98 | if (req->mapped) { |
| 99 | dma_unmap_single(udc->gadget.dev.parent, |
| 100 | req->req.dma, req->req.length, |
| 101 | ep_is_in(ep) |
| 102 | ? DMA_TO_DEVICE |
| 103 | : DMA_FROM_DEVICE); |
| 104 | req->req.dma = DMA_ADDR_INVALID; |
| 105 | req->mapped = 0; |
| 106 | } else |
| 107 | dma_sync_single_for_cpu(udc->gadget.dev.parent, |
| 108 | req->req.dma, req->req.length, |
| 109 | ep_is_in(ep) |
| 110 | ? DMA_TO_DEVICE |
| 111 | : DMA_FROM_DEVICE); |
| 112 | |
| 113 | if (status && (status != -ESHUTDOWN)) |
| 114 | dev_vdbg(udc->dev, "complete %s req %p stat %d len %u/%u\n", |
| 115 | ep->ep.name, &req->req, status, |
| 116 | req->req.actual, req->req.length); |
| 117 | |
| 118 | /* don't modify queue heads during completion callback */ |
| 119 | ep->stopped = 1; |
| 120 | spin_unlock(&udc->lock); |
| 121 | |
| 122 | /* this complete() should a func implemented by gadget layer, |
| 123 | * eg fsg->bulk_in_complete() */ |
| 124 | if (req->req.complete) |
| 125 | req->req.complete(&ep->ep, &req->req); |
| 126 | |
| 127 | spin_lock(&udc->lock); |
| 128 | |
| 129 | ep->stopped = stopped; |
| 130 | } |
| 131 | |
| 132 | /*----------------------------------------------------------------- |
| 133 | * nuke(): delete all requests related to this ep |
| 134 | *--------------------------------------------------------------*/ |
| 135 | static void nuke(struct qe_ep *ep, int status) |
| 136 | { |
| 137 | /* Whether this eq has request linked */ |
| 138 | while (!list_empty(&ep->queue)) { |
| 139 | struct qe_req *req = NULL; |
| 140 | req = list_entry(ep->queue.next, struct qe_req, queue); |
| 141 | |
| 142 | done(ep, req, status); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /*---------------------------------------------------------------------------* |
| 147 | * USB and Endpoint manipulate process, include parameter and register * |
| 148 | *---------------------------------------------------------------------------*/ |
| 149 | /* @value: 1--set stall 0--clean stall */ |
| 150 | static int qe_eprx_stall_change(struct qe_ep *ep, int value) |
| 151 | { |
| 152 | u16 tem_usep; |
| 153 | u8 epnum = ep->epnum; |
| 154 | struct qe_udc *udc = ep->udc; |
| 155 | |
| 156 | tem_usep = in_be16(&udc->usb_regs->usb_usep[epnum]); |
| 157 | tem_usep = tem_usep & ~USB_RHS_MASK; |
| 158 | if (value == 1) |
| 159 | tem_usep |= USB_RHS_STALL; |
| 160 | else if (ep->dir == USB_DIR_IN) |
| 161 | tem_usep |= USB_RHS_IGNORE_OUT; |
| 162 | |
| 163 | out_be16(&udc->usb_regs->usb_usep[epnum], tem_usep); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int qe_eptx_stall_change(struct qe_ep *ep, int value) |
| 168 | { |
| 169 | u16 tem_usep; |
| 170 | u8 epnum = ep->epnum; |
| 171 | struct qe_udc *udc = ep->udc; |
| 172 | |
| 173 | tem_usep = in_be16(&udc->usb_regs->usb_usep[epnum]); |
| 174 | tem_usep = tem_usep & ~USB_THS_MASK; |
| 175 | if (value == 1) |
| 176 | tem_usep |= USB_THS_STALL; |
| 177 | else if (ep->dir == USB_DIR_OUT) |
| 178 | tem_usep |= USB_THS_IGNORE_IN; |
| 179 | |
| 180 | out_be16(&udc->usb_regs->usb_usep[epnum], tem_usep); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int qe_ep0_stall(struct qe_udc *udc) |
| 186 | { |
| 187 | qe_eptx_stall_change(&udc->eps[0], 1); |
| 188 | qe_eprx_stall_change(&udc->eps[0], 1); |
| 189 | udc_controller->ep0_state = WAIT_FOR_SETUP; |
| 190 | udc_controller->ep0_dir = 0; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int qe_eprx_nack(struct qe_ep *ep) |
| 195 | { |
| 196 | u8 epnum = ep->epnum; |
| 197 | struct qe_udc *udc = ep->udc; |
| 198 | |
| 199 | if (ep->state == EP_STATE_IDLE) { |
| 200 | /* Set the ep's nack */ |
| 201 | clrsetbits_be16(&udc->usb_regs->usb_usep[epnum], |
| 202 | USB_RHS_MASK, USB_RHS_NACK); |
| 203 | |
| 204 | /* Mask Rx and Busy interrupts */ |
| 205 | clrbits16(&udc->usb_regs->usb_usbmr, |
| 206 | (USB_E_RXB_MASK | USB_E_BSY_MASK)); |
| 207 | |
| 208 | ep->state = EP_STATE_NACK; |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int qe_eprx_normal(struct qe_ep *ep) |
| 214 | { |
| 215 | struct qe_udc *udc = ep->udc; |
| 216 | |
| 217 | if (ep->state == EP_STATE_NACK) { |
| 218 | clrsetbits_be16(&udc->usb_regs->usb_usep[ep->epnum], |
| 219 | USB_RTHS_MASK, USB_THS_IGNORE_IN); |
| 220 | |
| 221 | /* Unmask RX interrupts */ |
| 222 | out_be16(&udc->usb_regs->usb_usber, |
| 223 | USB_E_BSY_MASK | USB_E_RXB_MASK); |
| 224 | setbits16(&udc->usb_regs->usb_usbmr, |
| 225 | (USB_E_RXB_MASK | USB_E_BSY_MASK)); |
| 226 | |
| 227 | ep->state = EP_STATE_IDLE; |
| 228 | ep->has_data = 0; |
| 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int qe_ep_cmd_stoptx(struct qe_ep *ep) |
| 235 | { |
| 236 | if (ep->udc->soc_type == PORT_CPM) |
| 237 | cpm_command(CPM_USB_STOP_TX | (ep->epnum << CPM_USB_EP_SHIFT), |
| 238 | CPM_USB_STOP_TX_OPCODE); |
| 239 | else |
| 240 | qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB, |
| 241 | ep->epnum, 0); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int qe_ep_cmd_restarttx(struct qe_ep *ep) |
| 247 | { |
| 248 | if (ep->udc->soc_type == PORT_CPM) |
| 249 | cpm_command(CPM_USB_RESTART_TX | (ep->epnum << |
| 250 | CPM_USB_EP_SHIFT), CPM_USB_RESTART_TX_OPCODE); |
| 251 | else |
| 252 | qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB, |
| 253 | ep->epnum, 0); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int qe_ep_flushtxfifo(struct qe_ep *ep) |
| 259 | { |
| 260 | struct qe_udc *udc = ep->udc; |
| 261 | int i; |
| 262 | |
| 263 | i = (int)ep->epnum; |
| 264 | |
| 265 | qe_ep_cmd_stoptx(ep); |
| 266 | out_8(&udc->usb_regs->usb_uscom, |
| 267 | USB_CMD_FLUSH_FIFO | (USB_CMD_EP_MASK & (ep->epnum))); |
| 268 | out_be16(&udc->ep_param[i]->tbptr, in_be16(&udc->ep_param[i]->tbase)); |
| 269 | out_be32(&udc->ep_param[i]->tstate, 0); |
| 270 | out_be16(&udc->ep_param[i]->tbcnt, 0); |
| 271 | |
| 272 | ep->c_txbd = ep->txbase; |
| 273 | ep->n_txbd = ep->txbase; |
| 274 | qe_ep_cmd_restarttx(ep); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int qe_ep_filltxfifo(struct qe_ep *ep) |
| 279 | { |
| 280 | struct qe_udc *udc = ep->udc; |
| 281 | |
| 282 | out_8(&udc->usb_regs->usb_uscom, |
| 283 | USB_CMD_STR_FIFO | (USB_CMD_EP_MASK & (ep->epnum))); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int qe_epbds_reset(struct qe_udc *udc, int pipe_num) |
| 288 | { |
| 289 | struct qe_ep *ep; |
| 290 | u32 bdring_len; |
| 291 | struct qe_bd __iomem *bd; |
| 292 | int i; |
| 293 | |
| 294 | ep = &udc->eps[pipe_num]; |
| 295 | |
| 296 | if (ep->dir == USB_DIR_OUT) |
| 297 | bdring_len = USB_BDRING_LEN_RX; |
| 298 | else |
| 299 | bdring_len = USB_BDRING_LEN; |
| 300 | |
| 301 | bd = ep->rxbase; |
| 302 | for (i = 0; i < (bdring_len - 1); i++) { |
| 303 | out_be32((u32 __iomem *)bd, R_E | R_I); |
| 304 | bd++; |
| 305 | } |
| 306 | out_be32((u32 __iomem *)bd, R_E | R_I | R_W); |
| 307 | |
| 308 | bd = ep->txbase; |
| 309 | for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) { |
| 310 | out_be32(&bd->buf, 0); |
| 311 | out_be32((u32 __iomem *)bd, 0); |
| 312 | bd++; |
| 313 | } |
| 314 | out_be32((u32 __iomem *)bd, T_W); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int qe_ep_reset(struct qe_udc *udc, int pipe_num) |
| 320 | { |
| 321 | struct qe_ep *ep; |
| 322 | u16 tmpusep; |
| 323 | |
| 324 | ep = &udc->eps[pipe_num]; |
| 325 | tmpusep = in_be16(&udc->usb_regs->usb_usep[pipe_num]); |
| 326 | tmpusep &= ~USB_RTHS_MASK; |
| 327 | |
| 328 | switch (ep->dir) { |
| 329 | case USB_DIR_BOTH: |
| 330 | qe_ep_flushtxfifo(ep); |
| 331 | break; |
| 332 | case USB_DIR_OUT: |
| 333 | tmpusep |= USB_THS_IGNORE_IN; |
| 334 | break; |
| 335 | case USB_DIR_IN: |
| 336 | qe_ep_flushtxfifo(ep); |
| 337 | tmpusep |= USB_RHS_IGNORE_OUT; |
| 338 | break; |
| 339 | default: |
| 340 | break; |
| 341 | } |
| 342 | out_be16(&udc->usb_regs->usb_usep[pipe_num], tmpusep); |
| 343 | |
| 344 | qe_epbds_reset(udc, pipe_num); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int qe_ep_toggledata01(struct qe_ep *ep) |
| 350 | { |
| 351 | ep->data01 ^= 0x1; |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int qe_ep_bd_init(struct qe_udc *udc, unsigned char pipe_num) |
| 356 | { |
| 357 | struct qe_ep *ep = &udc->eps[pipe_num]; |
| 358 | unsigned long tmp_addr = 0; |
| 359 | struct usb_ep_para __iomem *epparam; |
| 360 | int i; |
| 361 | struct qe_bd __iomem *bd; |
| 362 | int bdring_len; |
| 363 | |
| 364 | if (ep->dir == USB_DIR_OUT) |
| 365 | bdring_len = USB_BDRING_LEN_RX; |
| 366 | else |
| 367 | bdring_len = USB_BDRING_LEN; |
| 368 | |
| 369 | epparam = udc->ep_param[pipe_num]; |
| 370 | /* alloc multi-ram for BD rings and set the ep parameters */ |
| 371 | tmp_addr = cpm_muram_alloc(sizeof(struct qe_bd) * (bdring_len + |
| 372 | USB_BDRING_LEN_TX), QE_ALIGNMENT_OF_BD); |
| 373 | out_be16(&epparam->rbase, (u16)tmp_addr); |
| 374 | out_be16(&epparam->tbase, (u16)(tmp_addr + |
| 375 | (sizeof(struct qe_bd) * bdring_len))); |
| 376 | |
| 377 | out_be16(&epparam->rbptr, in_be16(&epparam->rbase)); |
| 378 | out_be16(&epparam->tbptr, in_be16(&epparam->tbase)); |
| 379 | |
| 380 | ep->rxbase = cpm_muram_addr(tmp_addr); |
| 381 | ep->txbase = cpm_muram_addr(tmp_addr + (sizeof(struct qe_bd) |
| 382 | * bdring_len)); |
| 383 | ep->n_rxbd = ep->rxbase; |
| 384 | ep->e_rxbd = ep->rxbase; |
| 385 | ep->n_txbd = ep->txbase; |
| 386 | ep->c_txbd = ep->txbase; |
| 387 | ep->data01 = 0; /* data0 */ |
| 388 | |
| 389 | /* Init TX and RX bds */ |
| 390 | bd = ep->rxbase; |
| 391 | for (i = 0; i < bdring_len - 1; i++) { |
| 392 | out_be32(&bd->buf, 0); |
| 393 | out_be32((u32 __iomem *)bd, 0); |
| 394 | bd++; |
| 395 | } |
| 396 | out_be32(&bd->buf, 0); |
| 397 | out_be32((u32 __iomem *)bd, R_W); |
| 398 | |
| 399 | bd = ep->txbase; |
| 400 | for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) { |
| 401 | out_be32(&bd->buf, 0); |
| 402 | out_be32((u32 __iomem *)bd, 0); |
| 403 | bd++; |
| 404 | } |
| 405 | out_be32(&bd->buf, 0); |
| 406 | out_be32((u32 __iomem *)bd, T_W); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int qe_ep_rxbd_update(struct qe_ep *ep) |
| 412 | { |
| 413 | unsigned int size; |
| 414 | int i; |
| 415 | unsigned int tmp; |
| 416 | struct qe_bd __iomem *bd; |
| 417 | unsigned int bdring_len; |
| 418 | |
| 419 | if (ep->rxbase == NULL) |
| 420 | return -EINVAL; |
| 421 | |
| 422 | bd = ep->rxbase; |
| 423 | |
| 424 | ep->rxframe = kmalloc(sizeof(*ep->rxframe), GFP_ATOMIC); |
| 425 | if (ep->rxframe == NULL) { |
| 426 | dev_err(ep->udc->dev, "malloc rxframe failed\n"); |
| 427 | return -ENOMEM; |
| 428 | } |
| 429 | |
| 430 | qe_frame_init(ep->rxframe); |
| 431 | |
| 432 | if (ep->dir == USB_DIR_OUT) |
| 433 | bdring_len = USB_BDRING_LEN_RX; |
| 434 | else |
| 435 | bdring_len = USB_BDRING_LEN; |
| 436 | |
| 437 | size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * (bdring_len + 1); |
| 438 | ep->rxbuffer = kzalloc(size, GFP_ATOMIC); |
| 439 | if (ep->rxbuffer == NULL) { |
| 440 | dev_err(ep->udc->dev, "malloc rxbuffer failed,size=%d\n", |
| 441 | size); |
| 442 | kfree(ep->rxframe); |
| 443 | return -ENOMEM; |
| 444 | } |
| 445 | |
| 446 | ep->rxbuf_d = virt_to_phys((void *)ep->rxbuffer); |
| 447 | if (ep->rxbuf_d == DMA_ADDR_INVALID) { |
| 448 | ep->rxbuf_d = dma_map_single(udc_controller->gadget.dev.parent, |
| 449 | ep->rxbuffer, |
| 450 | size, |
| 451 | DMA_FROM_DEVICE); |
| 452 | ep->rxbufmap = 1; |
| 453 | } else { |
| 454 | dma_sync_single_for_device(udc_controller->gadget.dev.parent, |
| 455 | ep->rxbuf_d, size, |
| 456 | DMA_FROM_DEVICE); |
| 457 | ep->rxbufmap = 0; |
| 458 | } |
| 459 | |
| 460 | size = ep->ep.maxpacket + USB_CRC_SIZE + 2; |
| 461 | tmp = ep->rxbuf_d; |
| 462 | tmp = (u32)(((tmp >> 2) << 2) + 4); |
| 463 | |
| 464 | for (i = 0; i < bdring_len - 1; i++) { |
| 465 | out_be32(&bd->buf, tmp); |
| 466 | out_be32((u32 __iomem *)bd, (R_E | R_I)); |
| 467 | tmp = tmp + size; |
| 468 | bd++; |
| 469 | } |
| 470 | out_be32(&bd->buf, tmp); |
| 471 | out_be32((u32 __iomem *)bd, (R_E | R_I | R_W)); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int qe_ep_register_init(struct qe_udc *udc, unsigned char pipe_num) |
| 477 | { |
| 478 | struct qe_ep *ep = &udc->eps[pipe_num]; |
| 479 | struct usb_ep_para __iomem *epparam; |
| 480 | u16 usep, logepnum; |
| 481 | u16 tmp; |
| 482 | u8 rtfcr = 0; |
| 483 | |
| 484 | epparam = udc->ep_param[pipe_num]; |
| 485 | |
| 486 | usep = 0; |
| 487 | logepnum = (ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
| 488 | usep |= (logepnum << USB_EPNUM_SHIFT); |
| 489 | |
| 490 | switch (ep->desc->bmAttributes & 0x03) { |
| 491 | case USB_ENDPOINT_XFER_BULK: |
| 492 | usep |= USB_TRANS_BULK; |
| 493 | break; |
| 494 | case USB_ENDPOINT_XFER_ISOC: |
| 495 | usep |= USB_TRANS_ISO; |
| 496 | break; |
| 497 | case USB_ENDPOINT_XFER_INT: |
| 498 | usep |= USB_TRANS_INT; |
| 499 | break; |
| 500 | default: |
| 501 | usep |= USB_TRANS_CTR; |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | switch (ep->dir) { |
| 506 | case USB_DIR_OUT: |
| 507 | usep |= USB_THS_IGNORE_IN; |
| 508 | break; |
| 509 | case USB_DIR_IN: |
| 510 | usep |= USB_RHS_IGNORE_OUT; |
| 511 | break; |
| 512 | default: |
| 513 | break; |
| 514 | } |
| 515 | out_be16(&udc->usb_regs->usb_usep[pipe_num], usep); |
| 516 | |
| 517 | rtfcr = 0x30; |
| 518 | out_8(&epparam->rbmr, rtfcr); |
| 519 | out_8(&epparam->tbmr, rtfcr); |
| 520 | |
| 521 | tmp = (u16)(ep->ep.maxpacket + USB_CRC_SIZE); |
| 522 | /* MRBLR must be divisble by 4 */ |
| 523 | tmp = (u16)(((tmp >> 2) << 2) + 4); |
| 524 | out_be16(&epparam->mrblr, tmp); |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int qe_ep_init(struct qe_udc *udc, |
| 530 | unsigned char pipe_num, |
| 531 | const struct usb_endpoint_descriptor *desc) |
| 532 | { |
| 533 | struct qe_ep *ep = &udc->eps[pipe_num]; |
| 534 | unsigned long flags; |
| 535 | int reval = 0; |
| 536 | u16 max = 0; |
| 537 | |
| 538 | max = le16_to_cpu(desc->wMaxPacketSize); |
| 539 | |
| 540 | /* check the max package size validate for this endpoint */ |
| 541 | /* Refer to USB2.0 spec table 9-13, |
| 542 | */ |
| 543 | if (pipe_num != 0) { |
| 544 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 545 | case USB_ENDPOINT_XFER_BULK: |
| 546 | if (strstr(ep->ep.name, "-iso") |
| 547 | || strstr(ep->ep.name, "-int")) |
| 548 | goto en_done; |
| 549 | switch (udc->gadget.speed) { |
| 550 | case USB_SPEED_HIGH: |
| 551 | if ((max == 128) || (max == 256) || (max == 512)) |
| 552 | break; |
| 553 | default: |
| 554 | switch (max) { |
| 555 | case 4: |
| 556 | case 8: |
| 557 | case 16: |
| 558 | case 32: |
| 559 | case 64: |
| 560 | break; |
| 561 | default: |
| 562 | case USB_SPEED_LOW: |
| 563 | goto en_done; |
| 564 | } |
| 565 | } |
| 566 | break; |
| 567 | case USB_ENDPOINT_XFER_INT: |
| 568 | if (strstr(ep->ep.name, "-iso")) /* bulk is ok */ |
| 569 | goto en_done; |
| 570 | switch (udc->gadget.speed) { |
| 571 | case USB_SPEED_HIGH: |
| 572 | if (max <= 1024) |
| 573 | break; |
| 574 | case USB_SPEED_FULL: |
| 575 | if (max <= 64) |
| 576 | break; |
| 577 | default: |
| 578 | if (max <= 8) |
| 579 | break; |
| 580 | goto en_done; |
| 581 | } |
| 582 | break; |
| 583 | case USB_ENDPOINT_XFER_ISOC: |
| 584 | if (strstr(ep->ep.name, "-bulk") |
| 585 | || strstr(ep->ep.name, "-int")) |
| 586 | goto en_done; |
| 587 | switch (udc->gadget.speed) { |
| 588 | case USB_SPEED_HIGH: |
| 589 | if (max <= 1024) |
| 590 | break; |
| 591 | case USB_SPEED_FULL: |
| 592 | if (max <= 1023) |
| 593 | break; |
| 594 | default: |
| 595 | goto en_done; |
| 596 | } |
| 597 | break; |
| 598 | case USB_ENDPOINT_XFER_CONTROL: |
| 599 | if (strstr(ep->ep.name, "-iso") |
| 600 | || strstr(ep->ep.name, "-int")) |
| 601 | goto en_done; |
| 602 | switch (udc->gadget.speed) { |
| 603 | case USB_SPEED_HIGH: |
| 604 | case USB_SPEED_FULL: |
| 605 | switch (max) { |
| 606 | case 1: |
| 607 | case 2: |
| 608 | case 4: |
| 609 | case 8: |
| 610 | case 16: |
| 611 | case 32: |
| 612 | case 64: |
| 613 | break; |
| 614 | default: |
| 615 | goto en_done; |
| 616 | } |
| 617 | case USB_SPEED_LOW: |
| 618 | switch (max) { |
| 619 | case 1: |
| 620 | case 2: |
| 621 | case 4: |
| 622 | case 8: |
| 623 | break; |
| 624 | default: |
| 625 | goto en_done; |
| 626 | } |
| 627 | default: |
| 628 | goto en_done; |
| 629 | } |
| 630 | break; |
| 631 | |
| 632 | default: |
| 633 | goto en_done; |
| 634 | } |
| 635 | } /* if ep0*/ |
| 636 | |
| 637 | spin_lock_irqsave(&udc->lock, flags); |
| 638 | |
| 639 | /* initialize ep structure */ |
| 640 | ep->ep.maxpacket = max; |
| 641 | ep->tm = (u8)(desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK); |
| 642 | ep->desc = desc; |
| 643 | ep->stopped = 0; |
| 644 | ep->init = 1; |
| 645 | |
| 646 | if (pipe_num == 0) { |
| 647 | ep->dir = USB_DIR_BOTH; |
| 648 | udc->ep0_dir = USB_DIR_OUT; |
| 649 | udc->ep0_state = WAIT_FOR_SETUP; |
| 650 | } else { |
| 651 | switch (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) { |
| 652 | case USB_DIR_OUT: |
| 653 | ep->dir = USB_DIR_OUT; |
| 654 | break; |
| 655 | case USB_DIR_IN: |
| 656 | ep->dir = USB_DIR_IN; |
| 657 | default: |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* hardware special operation */ |
| 663 | qe_ep_bd_init(udc, pipe_num); |
| 664 | if ((ep->tm == USBP_TM_CTL) || (ep->dir == USB_DIR_OUT)) { |
| 665 | reval = qe_ep_rxbd_update(ep); |
| 666 | if (reval) |
| 667 | goto en_done1; |
| 668 | } |
| 669 | |
| 670 | if ((ep->tm == USBP_TM_CTL) || (ep->dir == USB_DIR_IN)) { |
| 671 | ep->txframe = kmalloc(sizeof(*ep->txframe), GFP_ATOMIC); |
| 672 | if (ep->txframe == NULL) { |
| 673 | dev_err(udc->dev, "malloc txframe failed\n"); |
| 674 | goto en_done2; |
| 675 | } |
| 676 | qe_frame_init(ep->txframe); |
| 677 | } |
| 678 | |
| 679 | qe_ep_register_init(udc, pipe_num); |
| 680 | |
| 681 | /* Now HW will be NAKing transfers to that EP, |
| 682 | * until a buffer is queued to it. */ |
| 683 | spin_unlock_irqrestore(&udc->lock, flags); |
| 684 | |
| 685 | return 0; |
| 686 | en_done2: |
| 687 | kfree(ep->rxbuffer); |
| 688 | kfree(ep->rxframe); |
| 689 | en_done1: |
| 690 | spin_unlock_irqrestore(&udc->lock, flags); |
| 691 | en_done: |
| 692 | dev_dbg(udc->dev, "failed to initialize %s\n", ep->ep.name); |
| 693 | return -ENODEV; |
| 694 | } |
| 695 | |
| 696 | static inline void qe_usb_enable(void) |
| 697 | { |
| 698 | setbits8(&udc_controller->usb_regs->usb_usmod, USB_MODE_EN); |
| 699 | } |
| 700 | |
| 701 | static inline void qe_usb_disable(void) |
| 702 | { |
| 703 | clrbits8(&udc_controller->usb_regs->usb_usmod, USB_MODE_EN); |
| 704 | } |
| 705 | |
| 706 | /*----------------------------------------------------------------------------* |
| 707 | * USB and EP basic manipulate function end * |
| 708 | *----------------------------------------------------------------------------*/ |
| 709 | |
| 710 | |
| 711 | /****************************************************************************** |
| 712 | UDC transmit and receive process |
| 713 | ******************************************************************************/ |
| 714 | static void recycle_one_rxbd(struct qe_ep *ep) |
| 715 | { |
| 716 | u32 bdstatus; |
| 717 | |
| 718 | bdstatus = in_be32((u32 __iomem *)ep->e_rxbd); |
| 719 | bdstatus = R_I | R_E | (bdstatus & R_W); |
| 720 | out_be32((u32 __iomem *)ep->e_rxbd, bdstatus); |
| 721 | |
| 722 | if (bdstatus & R_W) |
| 723 | ep->e_rxbd = ep->rxbase; |
| 724 | else |
| 725 | ep->e_rxbd++; |
| 726 | } |
| 727 | |
| 728 | static void recycle_rxbds(struct qe_ep *ep, unsigned char stopatnext) |
| 729 | { |
| 730 | u32 bdstatus; |
| 731 | struct qe_bd __iomem *bd, *nextbd; |
| 732 | unsigned char stop = 0; |
| 733 | |
| 734 | nextbd = ep->n_rxbd; |
| 735 | bd = ep->e_rxbd; |
| 736 | bdstatus = in_be32((u32 __iomem *)bd); |
| 737 | |
| 738 | while (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK) && !stop) { |
| 739 | bdstatus = R_E | R_I | (bdstatus & R_W); |
| 740 | out_be32((u32 __iomem *)bd, bdstatus); |
| 741 | |
| 742 | if (bdstatus & R_W) |
| 743 | bd = ep->rxbase; |
| 744 | else |
| 745 | bd++; |
| 746 | |
| 747 | bdstatus = in_be32((u32 __iomem *)bd); |
| 748 | if (stopatnext && (bd == nextbd)) |
| 749 | stop = 1; |
| 750 | } |
| 751 | |
| 752 | ep->e_rxbd = bd; |
| 753 | } |
| 754 | |
| 755 | static void ep_recycle_rxbds(struct qe_ep *ep) |
| 756 | { |
| 757 | struct qe_bd __iomem *bd = ep->n_rxbd; |
| 758 | u32 bdstatus; |
| 759 | u8 epnum = ep->epnum; |
| 760 | struct qe_udc *udc = ep->udc; |
| 761 | |
| 762 | bdstatus = in_be32((u32 __iomem *)bd); |
| 763 | if (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK)) { |
| 764 | bd = ep->rxbase + |
| 765 | ((in_be16(&udc->ep_param[epnum]->rbptr) - |
| 766 | in_be16(&udc->ep_param[epnum]->rbase)) |
| 767 | >> 3); |
| 768 | bdstatus = in_be32((u32 __iomem *)bd); |
| 769 | |
| 770 | if (bdstatus & R_W) |
| 771 | bd = ep->rxbase; |
| 772 | else |
| 773 | bd++; |
| 774 | |
| 775 | ep->e_rxbd = bd; |
| 776 | recycle_rxbds(ep, 0); |
| 777 | ep->e_rxbd = ep->n_rxbd; |
| 778 | } else |
| 779 | recycle_rxbds(ep, 1); |
| 780 | |
| 781 | if (in_be16(&udc->usb_regs->usb_usber) & USB_E_BSY_MASK) |
| 782 | out_be16(&udc->usb_regs->usb_usber, USB_E_BSY_MASK); |
| 783 | |
| 784 | if (ep->has_data <= 0 && (!list_empty(&ep->queue))) |
| 785 | qe_eprx_normal(ep); |
| 786 | |
| 787 | ep->localnack = 0; |
| 788 | } |
| 789 | |
| 790 | static void setup_received_handle(struct qe_udc *udc, |
| 791 | struct usb_ctrlrequest *setup); |
| 792 | static int qe_ep_rxframe_handle(struct qe_ep *ep); |
| 793 | static void ep0_req_complete(struct qe_udc *udc, struct qe_req *req); |
| 794 | /* when BD PID is setup, handle the packet */ |
| 795 | static int ep0_setup_handle(struct qe_udc *udc) |
| 796 | { |
| 797 | struct qe_ep *ep = &udc->eps[0]; |
| 798 | struct qe_frame *pframe; |
| 799 | unsigned int fsize; |
| 800 | u8 *cp; |
| 801 | |
| 802 | pframe = ep->rxframe; |
| 803 | if ((frame_get_info(pframe) & PID_SETUP) |
| 804 | && (udc->ep0_state == WAIT_FOR_SETUP)) { |
| 805 | fsize = frame_get_length(pframe); |
| 806 | if (unlikely(fsize != 8)) |
| 807 | return -EINVAL; |
| 808 | cp = (u8 *)&udc->local_setup_buff; |
| 809 | memcpy(cp, pframe->data, fsize); |
| 810 | ep->data01 = 1; |
| 811 | |
| 812 | /* handle the usb command base on the usb_ctrlrequest */ |
| 813 | setup_received_handle(udc, &udc->local_setup_buff); |
| 814 | return 0; |
| 815 | } |
| 816 | return -EINVAL; |
| 817 | } |
| 818 | |
| 819 | static int qe_ep0_rx(struct qe_udc *udc) |
| 820 | { |
| 821 | struct qe_ep *ep = &udc->eps[0]; |
| 822 | struct qe_frame *pframe; |
| 823 | struct qe_bd __iomem *bd; |
| 824 | u32 bdstatus, length; |
| 825 | u32 vaddr; |
| 826 | |
| 827 | pframe = ep->rxframe; |
| 828 | |
| 829 | if (ep->dir == USB_DIR_IN) { |
| 830 | dev_err(udc->dev, "ep0 not a control endpoint\n"); |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | |
| 834 | bd = ep->n_rxbd; |
| 835 | bdstatus = in_be32((u32 __iomem *)bd); |
| 836 | length = bdstatus & BD_LENGTH_MASK; |
| 837 | |
| 838 | while (!(bdstatus & R_E) && length) { |
| 839 | if ((bdstatus & R_F) && (bdstatus & R_L) |
| 840 | && !(bdstatus & R_ERROR)) { |
| 841 | if (length == USB_CRC_SIZE) { |
| 842 | udc->ep0_state = WAIT_FOR_SETUP; |
| 843 | dev_vdbg(udc->dev, |
| 844 | "receive a ZLP in status phase\n"); |
| 845 | } else { |
| 846 | qe_frame_clean(pframe); |
| 847 | vaddr = (u32)phys_to_virt(in_be32(&bd->buf)); |
| 848 | frame_set_data(pframe, (u8 *)vaddr); |
| 849 | frame_set_length(pframe, |
| 850 | (length - USB_CRC_SIZE)); |
| 851 | frame_set_status(pframe, FRAME_OK); |
| 852 | switch (bdstatus & R_PID) { |
| 853 | case R_PID_SETUP: |
| 854 | frame_set_info(pframe, PID_SETUP); |
| 855 | break; |
| 856 | case R_PID_DATA1: |
| 857 | frame_set_info(pframe, PID_DATA1); |
| 858 | break; |
| 859 | default: |
| 860 | frame_set_info(pframe, PID_DATA0); |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | if ((bdstatus & R_PID) == R_PID_SETUP) |
| 865 | ep0_setup_handle(udc); |
| 866 | else |
| 867 | qe_ep_rxframe_handle(ep); |
| 868 | } |
| 869 | } else { |
| 870 | dev_err(udc->dev, "The receive frame with error!\n"); |
| 871 | } |
| 872 | |
| 873 | /* note: don't clear the rxbd's buffer address */ |
| 874 | recycle_one_rxbd(ep); |
| 875 | |
| 876 | /* Get next BD */ |
| 877 | if (bdstatus & R_W) |
| 878 | bd = ep->rxbase; |
| 879 | else |
| 880 | bd++; |
| 881 | |
| 882 | bdstatus = in_be32((u32 __iomem *)bd); |
| 883 | length = bdstatus & BD_LENGTH_MASK; |
| 884 | |
| 885 | } |
| 886 | |
| 887 | ep->n_rxbd = bd; |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | static int qe_ep_rxframe_handle(struct qe_ep *ep) |
| 893 | { |
| 894 | struct qe_frame *pframe; |
| 895 | u8 framepid = 0; |
| 896 | unsigned int fsize; |
| 897 | u8 *cp; |
| 898 | struct qe_req *req; |
| 899 | |
| 900 | pframe = ep->rxframe; |
| 901 | |
| 902 | if (frame_get_info(pframe) & PID_DATA1) |
| 903 | framepid = 0x1; |
| 904 | |
| 905 | if (framepid != ep->data01) { |
| 906 | dev_err(ep->udc->dev, "the data01 error!\n"); |
| 907 | return -EIO; |
| 908 | } |
| 909 | |
| 910 | fsize = frame_get_length(pframe); |
| 911 | if (list_empty(&ep->queue)) { |
| 912 | dev_err(ep->udc->dev, "the %s have no requeue!\n", ep->name); |
| 913 | } else { |
| 914 | req = list_entry(ep->queue.next, struct qe_req, queue); |
| 915 | |
| 916 | cp = (u8 *)(req->req.buf) + req->req.actual; |
| 917 | if (cp) { |
| 918 | memcpy(cp, pframe->data, fsize); |
| 919 | req->req.actual += fsize; |
| 920 | if ((fsize < ep->ep.maxpacket) || |
| 921 | (req->req.actual >= req->req.length)) { |
| 922 | if (ep->epnum == 0) |
| 923 | ep0_req_complete(ep->udc, req); |
| 924 | else |
| 925 | done(ep, req, 0); |
| 926 | if (list_empty(&ep->queue) && ep->epnum != 0) |
| 927 | qe_eprx_nack(ep); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | qe_ep_toggledata01(ep); |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static void ep_rx_tasklet(unsigned long data) |
| 938 | { |
| 939 | struct qe_udc *udc = (struct qe_udc *)data; |
| 940 | struct qe_ep *ep; |
| 941 | struct qe_frame *pframe; |
| 942 | struct qe_bd __iomem *bd; |
| 943 | unsigned long flags; |
| 944 | u32 bdstatus, length; |
| 945 | u32 vaddr, i; |
| 946 | |
| 947 | spin_lock_irqsave(&udc->lock, flags); |
| 948 | |
| 949 | for (i = 1; i < USB_MAX_ENDPOINTS; i++) { |
| 950 | ep = &udc->eps[i]; |
| 951 | |
| 952 | if (ep->dir == USB_DIR_IN || ep->enable_tasklet == 0) { |
| 953 | dev_dbg(udc->dev, |
| 954 | "This is a transmit ep or disable tasklet!\n"); |
| 955 | continue; |
| 956 | } |
| 957 | |
| 958 | pframe = ep->rxframe; |
| 959 | bd = ep->n_rxbd; |
| 960 | bdstatus = in_be32((u32 __iomem *)bd); |
| 961 | length = bdstatus & BD_LENGTH_MASK; |
| 962 | |
| 963 | while (!(bdstatus & R_E) && length) { |
| 964 | if (list_empty(&ep->queue)) { |
| 965 | qe_eprx_nack(ep); |
| 966 | dev_dbg(udc->dev, |
| 967 | "The rxep have noreq %d\n", |
| 968 | ep->has_data); |
| 969 | break; |
| 970 | } |
| 971 | |
| 972 | if ((bdstatus & R_F) && (bdstatus & R_L) |
| 973 | && !(bdstatus & R_ERROR)) { |
| 974 | qe_frame_clean(pframe); |
| 975 | vaddr = (u32)phys_to_virt(in_be32(&bd->buf)); |
| 976 | frame_set_data(pframe, (u8 *)vaddr); |
| 977 | frame_set_length(pframe, |
| 978 | (length - USB_CRC_SIZE)); |
| 979 | frame_set_status(pframe, FRAME_OK); |
| 980 | switch (bdstatus & R_PID) { |
| 981 | case R_PID_DATA1: |
| 982 | frame_set_info(pframe, PID_DATA1); |
| 983 | break; |
| 984 | case R_PID_SETUP: |
| 985 | frame_set_info(pframe, PID_SETUP); |
| 986 | break; |
| 987 | default: |
| 988 | frame_set_info(pframe, PID_DATA0); |
| 989 | break; |
| 990 | } |
| 991 | /* handle the rx frame */ |
| 992 | qe_ep_rxframe_handle(ep); |
| 993 | } else { |
| 994 | dev_err(udc->dev, |
| 995 | "error in received frame\n"); |
| 996 | } |
| 997 | /* note: don't clear the rxbd's buffer address */ |
| 998 | /*clear the length */ |
| 999 | out_be32((u32 __iomem *)bd, bdstatus & BD_STATUS_MASK); |
| 1000 | ep->has_data--; |
| 1001 | if (!(ep->localnack)) |
| 1002 | recycle_one_rxbd(ep); |
| 1003 | |
| 1004 | /* Get next BD */ |
| 1005 | if (bdstatus & R_W) |
| 1006 | bd = ep->rxbase; |
| 1007 | else |
| 1008 | bd++; |
| 1009 | |
| 1010 | bdstatus = in_be32((u32 __iomem *)bd); |
| 1011 | length = bdstatus & BD_LENGTH_MASK; |
| 1012 | } |
| 1013 | |
| 1014 | ep->n_rxbd = bd; |
| 1015 | |
| 1016 | if (ep->localnack) |
| 1017 | ep_recycle_rxbds(ep); |
| 1018 | |
| 1019 | ep->enable_tasklet = 0; |
| 1020 | } /* for i=1 */ |
| 1021 | |
| 1022 | spin_unlock_irqrestore(&udc->lock, flags); |
| 1023 | } |
| 1024 | |
| 1025 | static int qe_ep_rx(struct qe_ep *ep) |
| 1026 | { |
| 1027 | struct qe_udc *udc; |
| 1028 | struct qe_frame *pframe; |
| 1029 | struct qe_bd __iomem *bd; |
| 1030 | u16 swoffs, ucoffs, emptybds; |
| 1031 | |
| 1032 | udc = ep->udc; |
| 1033 | pframe = ep->rxframe; |
| 1034 | |
| 1035 | if (ep->dir == USB_DIR_IN) { |
| 1036 | dev_err(udc->dev, "transmit ep in rx function\n"); |
| 1037 | return -EINVAL; |
| 1038 | } |
| 1039 | |
| 1040 | bd = ep->n_rxbd; |
| 1041 | |
| 1042 | swoffs = (u16)(bd - ep->rxbase); |
| 1043 | ucoffs = (u16)((in_be16(&udc->ep_param[ep->epnum]->rbptr) - |
| 1044 | in_be16(&udc->ep_param[ep->epnum]->rbase)) >> 3); |
| 1045 | if (swoffs < ucoffs) |
| 1046 | emptybds = USB_BDRING_LEN_RX - ucoffs + swoffs; |
| 1047 | else |
| 1048 | emptybds = swoffs - ucoffs; |
| 1049 | |
| 1050 | if (emptybds < MIN_EMPTY_BDS) { |
| 1051 | qe_eprx_nack(ep); |
| 1052 | ep->localnack = 1; |
| 1053 | dev_vdbg(udc->dev, "%d empty bds, send NACK\n", emptybds); |
| 1054 | } |
| 1055 | ep->has_data = USB_BDRING_LEN_RX - emptybds; |
| 1056 | |
| 1057 | if (list_empty(&ep->queue)) { |
| 1058 | qe_eprx_nack(ep); |
| 1059 | dev_vdbg(udc->dev, "The rxep have no req queued with %d BDs\n", |
| 1060 | ep->has_data); |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | tasklet_schedule(&udc->rx_tasklet); |
| 1065 | ep->enable_tasklet = 1; |
| 1066 | |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | /* send data from a frame, no matter what tx_req */ |
| 1071 | static int qe_ep_tx(struct qe_ep *ep, struct qe_frame *frame) |
| 1072 | { |
| 1073 | struct qe_udc *udc = ep->udc; |
| 1074 | struct qe_bd __iomem *bd; |
| 1075 | u16 saveusbmr; |
| 1076 | u32 bdstatus, pidmask; |
| 1077 | u32 paddr; |
| 1078 | |
| 1079 | if (ep->dir == USB_DIR_OUT) { |
| 1080 | dev_err(udc->dev, "receive ep passed to tx function\n"); |
| 1081 | return -EINVAL; |
| 1082 | } |
| 1083 | |
| 1084 | /* Disable the Tx interrupt */ |
| 1085 | saveusbmr = in_be16(&udc->usb_regs->usb_usbmr); |
| 1086 | out_be16(&udc->usb_regs->usb_usbmr, |
| 1087 | saveusbmr & ~(USB_E_TXB_MASK | USB_E_TXE_MASK)); |
| 1088 | |
| 1089 | bd = ep->n_txbd; |
| 1090 | bdstatus = in_be32((u32 __iomem *)bd); |
| 1091 | |
| 1092 | if (!(bdstatus & (T_R | BD_LENGTH_MASK))) { |
| 1093 | if (frame_get_length(frame) == 0) { |
| 1094 | frame_set_data(frame, udc->nullbuf); |
| 1095 | frame_set_length(frame, 2); |
| 1096 | frame->info |= (ZLP | NO_CRC); |
| 1097 | dev_vdbg(udc->dev, "the frame size = 0\n"); |
| 1098 | } |
| 1099 | paddr = virt_to_phys((void *)frame->data); |
| 1100 | out_be32(&bd->buf, paddr); |
| 1101 | bdstatus = (bdstatus&T_W); |
| 1102 | if (!(frame_get_info(frame) & NO_CRC)) |
| 1103 | bdstatus |= T_R | T_I | T_L | T_TC |
| 1104 | | frame_get_length(frame); |
| 1105 | else |
| 1106 | bdstatus |= T_R | T_I | T_L | frame_get_length(frame); |
| 1107 | |
| 1108 | /* if the packet is a ZLP in status phase */ |
| 1109 | if ((ep->epnum == 0) && (udc->ep0_state == DATA_STATE_NEED_ZLP)) |
| 1110 | ep->data01 = 0x1; |
| 1111 | |
| 1112 | if (ep->data01) { |
| 1113 | pidmask = T_PID_DATA1; |
| 1114 | frame->info |= PID_DATA1; |
| 1115 | } else { |
| 1116 | pidmask = T_PID_DATA0; |
| 1117 | frame->info |= PID_DATA0; |
| 1118 | } |
| 1119 | bdstatus |= T_CNF; |
| 1120 | bdstatus |= pidmask; |
| 1121 | out_be32((u32 __iomem *)bd, bdstatus); |
| 1122 | qe_ep_filltxfifo(ep); |
| 1123 | |
| 1124 | /* enable the TX interrupt */ |
| 1125 | out_be16(&udc->usb_regs->usb_usbmr, saveusbmr); |
| 1126 | |
| 1127 | qe_ep_toggledata01(ep); |
| 1128 | if (bdstatus & T_W) |
| 1129 | ep->n_txbd = ep->txbase; |
| 1130 | else |
| 1131 | ep->n_txbd++; |
| 1132 | |
| 1133 | return 0; |
| 1134 | } else { |
| 1135 | out_be16(&udc->usb_regs->usb_usbmr, saveusbmr); |
| 1136 | dev_vdbg(udc->dev, "The tx bd is not ready!\n"); |
| 1137 | return -EBUSY; |
| 1138 | } |
| 1139 | } |
| 1140 | |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 1141 | /* when a bd was transmitted, the function can |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1142 | * handle the tx_req, not include ep0 */ |
| 1143 | static int txcomplete(struct qe_ep *ep, unsigned char restart) |
| 1144 | { |
| 1145 | if (ep->tx_req != NULL) { |
| 1146 | if (!restart) { |
| 1147 | int asent = ep->last; |
| 1148 | ep->sent += asent; |
| 1149 | ep->last -= asent; |
| 1150 | } else { |
| 1151 | ep->last = 0; |
| 1152 | } |
| 1153 | |
| 1154 | /* a request already were transmitted completely */ |
| 1155 | if ((ep->tx_req->req.length - ep->sent) <= 0) { |
| 1156 | ep->tx_req->req.actual = (unsigned int)ep->sent; |
| 1157 | done(ep, ep->tx_req, 0); |
| 1158 | ep->tx_req = NULL; |
| 1159 | ep->last = 0; |
| 1160 | ep->sent = 0; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /* we should gain a new tx_req fot this endpoint */ |
| 1165 | if (ep->tx_req == NULL) { |
| 1166 | if (!list_empty(&ep->queue)) { |
| 1167 | ep->tx_req = list_entry(ep->queue.next, struct qe_req, |
| 1168 | queue); |
| 1169 | ep->last = 0; |
| 1170 | ep->sent = 0; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 1177 | /* give a frame and a tx_req, send some data */ |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1178 | static int qe_usb_senddata(struct qe_ep *ep, struct qe_frame *frame) |
| 1179 | { |
| 1180 | unsigned int size; |
| 1181 | u8 *buf; |
| 1182 | |
| 1183 | qe_frame_clean(frame); |
| 1184 | size = min_t(u32, (ep->tx_req->req.length - ep->sent), |
| 1185 | ep->ep.maxpacket); |
| 1186 | buf = (u8 *)ep->tx_req->req.buf + ep->sent; |
| 1187 | if (buf && size) { |
| 1188 | ep->last = size; |
| 1189 | frame_set_data(frame, buf); |
| 1190 | frame_set_length(frame, size); |
| 1191 | frame_set_status(frame, FRAME_OK); |
| 1192 | frame_set_info(frame, 0); |
| 1193 | return qe_ep_tx(ep, frame); |
| 1194 | } |
| 1195 | return -EIO; |
| 1196 | } |
| 1197 | |
| 1198 | /* give a frame struct,send a ZLP */ |
| 1199 | static int sendnulldata(struct qe_ep *ep, struct qe_frame *frame, uint infor) |
| 1200 | { |
| 1201 | struct qe_udc *udc = ep->udc; |
| 1202 | |
| 1203 | if (frame == NULL) |
| 1204 | return -ENODEV; |
| 1205 | |
| 1206 | qe_frame_clean(frame); |
| 1207 | frame_set_data(frame, (u8 *)udc->nullbuf); |
| 1208 | frame_set_length(frame, 2); |
| 1209 | frame_set_status(frame, FRAME_OK); |
| 1210 | frame_set_info(frame, (ZLP | NO_CRC | infor)); |
| 1211 | |
| 1212 | return qe_ep_tx(ep, frame); |
| 1213 | } |
| 1214 | |
| 1215 | static int frame_create_tx(struct qe_ep *ep, struct qe_frame *frame) |
| 1216 | { |
| 1217 | struct qe_req *req = ep->tx_req; |
| 1218 | int reval; |
| 1219 | |
| 1220 | if (req == NULL) |
| 1221 | return -ENODEV; |
| 1222 | |
| 1223 | if ((req->req.length - ep->sent) > 0) |
| 1224 | reval = qe_usb_senddata(ep, frame); |
| 1225 | else |
| 1226 | reval = sendnulldata(ep, frame, 0); |
| 1227 | |
| 1228 | return reval; |
| 1229 | } |
| 1230 | |
| 1231 | /* if direction is DIR_IN, the status is Device->Host |
| 1232 | * if direction is DIR_OUT, the status transaction is Device<-Host |
| 1233 | * in status phase, udc create a request and gain status */ |
| 1234 | static int ep0_prime_status(struct qe_udc *udc, int direction) |
| 1235 | { |
| 1236 | |
| 1237 | struct qe_ep *ep = &udc->eps[0]; |
| 1238 | |
| 1239 | if (direction == USB_DIR_IN) { |
| 1240 | udc->ep0_state = DATA_STATE_NEED_ZLP; |
| 1241 | udc->ep0_dir = USB_DIR_IN; |
| 1242 | sendnulldata(ep, ep->txframe, SETUP_STATUS | NO_REQ); |
| 1243 | } else { |
| 1244 | udc->ep0_dir = USB_DIR_OUT; |
| 1245 | udc->ep0_state = WAIT_FOR_OUT_STATUS; |
| 1246 | } |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | /* a request complete in ep0, whether gadget request or udc request */ |
| 1252 | static void ep0_req_complete(struct qe_udc *udc, struct qe_req *req) |
| 1253 | { |
| 1254 | struct qe_ep *ep = &udc->eps[0]; |
| 1255 | /* because usb and ep's status already been set in ch9setaddress() */ |
| 1256 | |
| 1257 | switch (udc->ep0_state) { |
| 1258 | case DATA_STATE_XMIT: |
| 1259 | done(ep, req, 0); |
| 1260 | /* receive status phase */ |
| 1261 | if (ep0_prime_status(udc, USB_DIR_OUT)) |
| 1262 | qe_ep0_stall(udc); |
| 1263 | break; |
| 1264 | |
| 1265 | case DATA_STATE_NEED_ZLP: |
| 1266 | done(ep, req, 0); |
| 1267 | udc->ep0_state = WAIT_FOR_SETUP; |
| 1268 | break; |
| 1269 | |
| 1270 | case DATA_STATE_RECV: |
| 1271 | done(ep, req, 0); |
| 1272 | /* send status phase */ |
| 1273 | if (ep0_prime_status(udc, USB_DIR_IN)) |
| 1274 | qe_ep0_stall(udc); |
| 1275 | break; |
| 1276 | |
| 1277 | case WAIT_FOR_OUT_STATUS: |
| 1278 | done(ep, req, 0); |
| 1279 | udc->ep0_state = WAIT_FOR_SETUP; |
| 1280 | break; |
| 1281 | |
| 1282 | case WAIT_FOR_SETUP: |
| 1283 | dev_vdbg(udc->dev, "Unexpected interrupt\n"); |
| 1284 | break; |
| 1285 | |
| 1286 | default: |
| 1287 | qe_ep0_stall(udc); |
| 1288 | break; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | static int ep0_txcomplete(struct qe_ep *ep, unsigned char restart) |
| 1293 | { |
| 1294 | struct qe_req *tx_req = NULL; |
| 1295 | struct qe_frame *frame = ep->txframe; |
| 1296 | |
| 1297 | if ((frame_get_info(frame) & (ZLP | NO_REQ)) == (ZLP | NO_REQ)) { |
| 1298 | if (!restart) |
| 1299 | ep->udc->ep0_state = WAIT_FOR_SETUP; |
| 1300 | else |
| 1301 | sendnulldata(ep, ep->txframe, SETUP_STATUS | NO_REQ); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | tx_req = ep->tx_req; |
| 1306 | if (tx_req != NULL) { |
| 1307 | if (!restart) { |
| 1308 | int asent = ep->last; |
| 1309 | ep->sent += asent; |
| 1310 | ep->last -= asent; |
| 1311 | } else { |
| 1312 | ep->last = 0; |
| 1313 | } |
| 1314 | |
| 1315 | /* a request already were transmitted completely */ |
| 1316 | if ((ep->tx_req->req.length - ep->sent) <= 0) { |
| 1317 | ep->tx_req->req.actual = (unsigned int)ep->sent; |
| 1318 | ep0_req_complete(ep->udc, ep->tx_req); |
| 1319 | ep->tx_req = NULL; |
| 1320 | ep->last = 0; |
| 1321 | ep->sent = 0; |
| 1322 | } |
| 1323 | } else { |
| 1324 | dev_vdbg(ep->udc->dev, "the ep0_controller have no req\n"); |
| 1325 | } |
| 1326 | |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | static int ep0_txframe_handle(struct qe_ep *ep) |
| 1331 | { |
| 1332 | /* if have error, transmit again */ |
| 1333 | if (frame_get_status(ep->txframe) & FRAME_ERROR) { |
| 1334 | qe_ep_flushtxfifo(ep); |
| 1335 | dev_vdbg(ep->udc->dev, "The EP0 transmit data have error!\n"); |
| 1336 | if (frame_get_info(ep->txframe) & PID_DATA0) |
| 1337 | ep->data01 = 0; |
| 1338 | else |
| 1339 | ep->data01 = 1; |
| 1340 | |
| 1341 | ep0_txcomplete(ep, 1); |
| 1342 | } else |
| 1343 | ep0_txcomplete(ep, 0); |
| 1344 | |
| 1345 | frame_create_tx(ep, ep->txframe); |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | static int qe_ep0_txconf(struct qe_ep *ep) |
| 1350 | { |
| 1351 | struct qe_bd __iomem *bd; |
| 1352 | struct qe_frame *pframe; |
| 1353 | u32 bdstatus; |
| 1354 | |
| 1355 | bd = ep->c_txbd; |
| 1356 | bdstatus = in_be32((u32 __iomem *)bd); |
| 1357 | while (!(bdstatus & T_R) && (bdstatus & ~T_W)) { |
| 1358 | pframe = ep->txframe; |
| 1359 | |
| 1360 | /* clear and recycle the BD */ |
| 1361 | out_be32((u32 __iomem *)bd, bdstatus & T_W); |
| 1362 | out_be32(&bd->buf, 0); |
| 1363 | if (bdstatus & T_W) |
| 1364 | ep->c_txbd = ep->txbase; |
| 1365 | else |
| 1366 | ep->c_txbd++; |
| 1367 | |
| 1368 | if (ep->c_txbd == ep->n_txbd) { |
| 1369 | if (bdstatus & DEVICE_T_ERROR) { |
| 1370 | frame_set_status(pframe, FRAME_ERROR); |
| 1371 | if (bdstatus & T_TO) |
| 1372 | pframe->status |= TX_ER_TIMEOUT; |
| 1373 | if (bdstatus & T_UN) |
| 1374 | pframe->status |= TX_ER_UNDERUN; |
| 1375 | } |
| 1376 | ep0_txframe_handle(ep); |
| 1377 | } |
| 1378 | |
| 1379 | bd = ep->c_txbd; |
| 1380 | bdstatus = in_be32((u32 __iomem *)bd); |
| 1381 | } |
| 1382 | |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | static int ep_txframe_handle(struct qe_ep *ep) |
| 1387 | { |
| 1388 | if (frame_get_status(ep->txframe) & FRAME_ERROR) { |
| 1389 | qe_ep_flushtxfifo(ep); |
| 1390 | dev_vdbg(ep->udc->dev, "The EP0 transmit data have error!\n"); |
| 1391 | if (frame_get_info(ep->txframe) & PID_DATA0) |
| 1392 | ep->data01 = 0; |
| 1393 | else |
| 1394 | ep->data01 = 1; |
| 1395 | |
| 1396 | txcomplete(ep, 1); |
| 1397 | } else |
| 1398 | txcomplete(ep, 0); |
| 1399 | |
| 1400 | frame_create_tx(ep, ep->txframe); /* send the data */ |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | /* confirm the already trainsmited bd */ |
| 1405 | static int qe_ep_txconf(struct qe_ep *ep) |
| 1406 | { |
| 1407 | struct qe_bd __iomem *bd; |
| 1408 | struct qe_frame *pframe = NULL; |
| 1409 | u32 bdstatus; |
| 1410 | unsigned char breakonrxinterrupt = 0; |
| 1411 | |
| 1412 | bd = ep->c_txbd; |
| 1413 | bdstatus = in_be32((u32 __iomem *)bd); |
| 1414 | while (!(bdstatus & T_R) && (bdstatus & ~T_W)) { |
| 1415 | pframe = ep->txframe; |
| 1416 | if (bdstatus & DEVICE_T_ERROR) { |
| 1417 | frame_set_status(pframe, FRAME_ERROR); |
| 1418 | if (bdstatus & T_TO) |
| 1419 | pframe->status |= TX_ER_TIMEOUT; |
| 1420 | if (bdstatus & T_UN) |
| 1421 | pframe->status |= TX_ER_UNDERUN; |
| 1422 | } |
| 1423 | |
| 1424 | /* clear and recycle the BD */ |
| 1425 | out_be32((u32 __iomem *)bd, bdstatus & T_W); |
| 1426 | out_be32(&bd->buf, 0); |
| 1427 | if (bdstatus & T_W) |
| 1428 | ep->c_txbd = ep->txbase; |
| 1429 | else |
| 1430 | ep->c_txbd++; |
| 1431 | |
| 1432 | /* handle the tx frame */ |
| 1433 | ep_txframe_handle(ep); |
| 1434 | bd = ep->c_txbd; |
| 1435 | bdstatus = in_be32((u32 __iomem *)bd); |
| 1436 | } |
| 1437 | if (breakonrxinterrupt) |
| 1438 | return -EIO; |
| 1439 | else |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
| 1443 | /* Add a request in queue, and try to transmit a packet */ |
| 1444 | static int ep_req_send(struct qe_ep *ep, struct qe_req *req) |
| 1445 | { |
| 1446 | int reval = 0; |
| 1447 | |
| 1448 | if (ep->tx_req == NULL) { |
| 1449 | ep->sent = 0; |
| 1450 | ep->last = 0; |
| 1451 | txcomplete(ep, 0); /* can gain a new tx_req */ |
| 1452 | reval = frame_create_tx(ep, ep->txframe); |
| 1453 | } |
| 1454 | return reval; |
| 1455 | } |
| 1456 | |
| 1457 | /* Maybe this is a good ideal */ |
| 1458 | static int ep_req_rx(struct qe_ep *ep, struct qe_req *req) |
| 1459 | { |
| 1460 | struct qe_udc *udc = ep->udc; |
| 1461 | struct qe_frame *pframe = NULL; |
| 1462 | struct qe_bd __iomem *bd; |
| 1463 | u32 bdstatus, length; |
| 1464 | u32 vaddr, fsize; |
| 1465 | u8 *cp; |
| 1466 | u8 finish_req = 0; |
| 1467 | u8 framepid; |
| 1468 | |
| 1469 | if (list_empty(&ep->queue)) { |
| 1470 | dev_vdbg(udc->dev, "the req already finish!\n"); |
| 1471 | return 0; |
| 1472 | } |
| 1473 | pframe = ep->rxframe; |
| 1474 | |
| 1475 | bd = ep->n_rxbd; |
| 1476 | bdstatus = in_be32((u32 __iomem *)bd); |
| 1477 | length = bdstatus & BD_LENGTH_MASK; |
| 1478 | |
| 1479 | while (!(bdstatus & R_E) && length) { |
| 1480 | if (finish_req) |
| 1481 | break; |
| 1482 | if ((bdstatus & R_F) && (bdstatus & R_L) |
| 1483 | && !(bdstatus & R_ERROR)) { |
| 1484 | qe_frame_clean(pframe); |
| 1485 | vaddr = (u32)phys_to_virt(in_be32(&bd->buf)); |
| 1486 | frame_set_data(pframe, (u8 *)vaddr); |
| 1487 | frame_set_length(pframe, (length - USB_CRC_SIZE)); |
| 1488 | frame_set_status(pframe, FRAME_OK); |
| 1489 | switch (bdstatus & R_PID) { |
| 1490 | case R_PID_DATA1: |
| 1491 | frame_set_info(pframe, PID_DATA1); break; |
| 1492 | default: |
| 1493 | frame_set_info(pframe, PID_DATA0); break; |
| 1494 | } |
| 1495 | /* handle the rx frame */ |
| 1496 | |
| 1497 | if (frame_get_info(pframe) & PID_DATA1) |
| 1498 | framepid = 0x1; |
| 1499 | else |
| 1500 | framepid = 0; |
| 1501 | |
| 1502 | if (framepid != ep->data01) { |
| 1503 | dev_vdbg(udc->dev, "the data01 error!\n"); |
| 1504 | } else { |
| 1505 | fsize = frame_get_length(pframe); |
| 1506 | |
| 1507 | cp = (u8 *)(req->req.buf) + req->req.actual; |
| 1508 | if (cp) { |
| 1509 | memcpy(cp, pframe->data, fsize); |
| 1510 | req->req.actual += fsize; |
| 1511 | if ((fsize < ep->ep.maxpacket) |
| 1512 | || (req->req.actual >= |
| 1513 | req->req.length)) { |
| 1514 | finish_req = 1; |
| 1515 | done(ep, req, 0); |
| 1516 | if (list_empty(&ep->queue)) |
| 1517 | qe_eprx_nack(ep); |
| 1518 | } |
| 1519 | } |
| 1520 | qe_ep_toggledata01(ep); |
| 1521 | } |
| 1522 | } else { |
| 1523 | dev_err(udc->dev, "The receive frame with error!\n"); |
| 1524 | } |
| 1525 | |
| 1526 | /* note: don't clear the rxbd's buffer address * |
| 1527 | * only Clear the length */ |
| 1528 | out_be32((u32 __iomem *)bd, (bdstatus & BD_STATUS_MASK)); |
| 1529 | ep->has_data--; |
| 1530 | |
| 1531 | /* Get next BD */ |
| 1532 | if (bdstatus & R_W) |
| 1533 | bd = ep->rxbase; |
| 1534 | else |
| 1535 | bd++; |
| 1536 | |
| 1537 | bdstatus = in_be32((u32 __iomem *)bd); |
| 1538 | length = bdstatus & BD_LENGTH_MASK; |
| 1539 | } |
| 1540 | |
| 1541 | ep->n_rxbd = bd; |
| 1542 | ep_recycle_rxbds(ep); |
| 1543 | |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
| 1547 | /* only add the request in queue */ |
| 1548 | static int ep_req_receive(struct qe_ep *ep, struct qe_req *req) |
| 1549 | { |
| 1550 | if (ep->state == EP_STATE_NACK) { |
| 1551 | if (ep->has_data <= 0) { |
| 1552 | /* Enable rx and unmask rx interrupt */ |
| 1553 | qe_eprx_normal(ep); |
| 1554 | } else { |
| 1555 | /* Copy the exist BD data */ |
| 1556 | ep_req_rx(ep, req); |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
| 1563 | /******************************************************************** |
| 1564 | Internal Used Function End |
| 1565 | ********************************************************************/ |
| 1566 | |
| 1567 | /*----------------------------------------------------------------------- |
| 1568 | Endpoint Management Functions For Gadget |
| 1569 | -----------------------------------------------------------------------*/ |
| 1570 | static int qe_ep_enable(struct usb_ep *_ep, |
| 1571 | const struct usb_endpoint_descriptor *desc) |
| 1572 | { |
| 1573 | struct qe_udc *udc; |
| 1574 | struct qe_ep *ep; |
| 1575 | int retval = 0; |
| 1576 | unsigned char epnum; |
| 1577 | |
| 1578 | ep = container_of(_ep, struct qe_ep, ep); |
| 1579 | |
| 1580 | /* catch various bogus parameters */ |
| 1581 | if (!_ep || !desc || ep->desc || _ep->name == ep_name[0] || |
| 1582 | (desc->bDescriptorType != USB_DT_ENDPOINT)) |
| 1583 | return -EINVAL; |
| 1584 | |
| 1585 | udc = ep->udc; |
| 1586 | if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN)) |
| 1587 | return -ESHUTDOWN; |
| 1588 | |
| 1589 | epnum = (u8)desc->bEndpointAddress & 0xF; |
| 1590 | |
| 1591 | retval = qe_ep_init(udc, epnum, desc); |
| 1592 | if (retval != 0) { |
| 1593 | cpm_muram_free(cpm_muram_offset(ep->rxbase)); |
| 1594 | dev_dbg(udc->dev, "enable ep%d failed\n", ep->epnum); |
| 1595 | return -EINVAL; |
| 1596 | } |
| 1597 | dev_dbg(udc->dev, "enable ep%d successful\n", ep->epnum); |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | static int qe_ep_disable(struct usb_ep *_ep) |
| 1602 | { |
| 1603 | struct qe_udc *udc; |
| 1604 | struct qe_ep *ep; |
| 1605 | unsigned long flags; |
| 1606 | unsigned int size; |
| 1607 | |
| 1608 | ep = container_of(_ep, struct qe_ep, ep); |
| 1609 | udc = ep->udc; |
| 1610 | |
| 1611 | if (!_ep || !ep->desc) { |
| 1612 | dev_dbg(udc->dev, "%s not enabled\n", _ep ? ep->ep.name : NULL); |
| 1613 | return -EINVAL; |
| 1614 | } |
| 1615 | |
| 1616 | spin_lock_irqsave(&udc->lock, flags); |
| 1617 | /* Nuke all pending requests (does flush) */ |
| 1618 | nuke(ep, -ESHUTDOWN); |
| 1619 | ep->desc = NULL; |
| 1620 | ep->stopped = 1; |
| 1621 | spin_unlock_irqrestore(&udc->lock, flags); |
| 1622 | |
| 1623 | cpm_muram_free(cpm_muram_offset(ep->rxbase)); |
| 1624 | |
| 1625 | if (ep->dir == USB_DIR_OUT) |
| 1626 | size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * |
| 1627 | (USB_BDRING_LEN_RX + 1); |
| 1628 | else |
| 1629 | size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * |
| 1630 | (USB_BDRING_LEN + 1); |
| 1631 | |
| 1632 | if (ep->dir != USB_DIR_IN) { |
| 1633 | kfree(ep->rxframe); |
| 1634 | if (ep->rxbufmap) { |
| 1635 | dma_unmap_single(udc_controller->gadget.dev.parent, |
| 1636 | ep->rxbuf_d, size, |
| 1637 | DMA_FROM_DEVICE); |
| 1638 | ep->rxbuf_d = DMA_ADDR_INVALID; |
| 1639 | } else { |
| 1640 | dma_sync_single_for_cpu( |
| 1641 | udc_controller->gadget.dev.parent, |
| 1642 | ep->rxbuf_d, size, |
| 1643 | DMA_FROM_DEVICE); |
| 1644 | } |
| 1645 | kfree(ep->rxbuffer); |
| 1646 | } |
| 1647 | |
| 1648 | if (ep->dir != USB_DIR_OUT) |
| 1649 | kfree(ep->txframe); |
| 1650 | |
| 1651 | dev_dbg(udc->dev, "disabled %s OK\n", _ep->name); |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
| 1655 | static struct usb_request *qe_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags) |
| 1656 | { |
| 1657 | struct qe_req *req; |
| 1658 | |
| 1659 | req = kzalloc(sizeof(*req), gfp_flags); |
| 1660 | if (!req) |
| 1661 | return NULL; |
| 1662 | |
| 1663 | req->req.dma = DMA_ADDR_INVALID; |
| 1664 | |
| 1665 | INIT_LIST_HEAD(&req->queue); |
| 1666 | |
| 1667 | return &req->req; |
| 1668 | } |
| 1669 | |
| 1670 | static void qe_free_request(struct usb_ep *_ep, struct usb_request *_req) |
| 1671 | { |
| 1672 | struct qe_req *req; |
| 1673 | |
| 1674 | req = container_of(_req, struct qe_req, req); |
| 1675 | |
| 1676 | if (_req) |
| 1677 | kfree(req); |
| 1678 | } |
| 1679 | |
| 1680 | /* queues (submits) an I/O request to an endpoint */ |
| 1681 | static int qe_ep_queue(struct usb_ep *_ep, struct usb_request *_req, |
| 1682 | gfp_t gfp_flags) |
| 1683 | { |
| 1684 | struct qe_ep *ep = container_of(_ep, struct qe_ep, ep); |
| 1685 | struct qe_req *req = container_of(_req, struct qe_req, req); |
| 1686 | struct qe_udc *udc; |
| 1687 | unsigned long flags; |
| 1688 | int reval; |
| 1689 | |
| 1690 | udc = ep->udc; |
| 1691 | /* catch various bogus parameters */ |
| 1692 | if (!_req || !req->req.complete || !req->req.buf |
| 1693 | || !list_empty(&req->queue)) { |
| 1694 | dev_dbg(udc->dev, "bad params\n"); |
| 1695 | return -EINVAL; |
| 1696 | } |
| 1697 | if (!_ep || (!ep->desc && ep_index(ep))) { |
| 1698 | dev_dbg(udc->dev, "bad ep\n"); |
| 1699 | return -EINVAL; |
| 1700 | } |
| 1701 | |
| 1702 | if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) |
| 1703 | return -ESHUTDOWN; |
| 1704 | |
| 1705 | req->ep = ep; |
| 1706 | |
| 1707 | /* map virtual address to hardware */ |
| 1708 | if (req->req.dma == DMA_ADDR_INVALID) { |
| 1709 | req->req.dma = dma_map_single(ep->udc->gadget.dev.parent, |
| 1710 | req->req.buf, |
| 1711 | req->req.length, |
| 1712 | ep_is_in(ep) |
| 1713 | ? DMA_TO_DEVICE : |
| 1714 | DMA_FROM_DEVICE); |
| 1715 | req->mapped = 1; |
| 1716 | } else { |
| 1717 | dma_sync_single_for_device(ep->udc->gadget.dev.parent, |
| 1718 | req->req.dma, req->req.length, |
| 1719 | ep_is_in(ep) |
| 1720 | ? DMA_TO_DEVICE : |
| 1721 | DMA_FROM_DEVICE); |
| 1722 | req->mapped = 0; |
| 1723 | } |
| 1724 | |
| 1725 | req->req.status = -EINPROGRESS; |
| 1726 | req->req.actual = 0; |
| 1727 | |
| 1728 | list_add_tail(&req->queue, &ep->queue); |
| 1729 | dev_vdbg(udc->dev, "gadget have request in %s! %d\n", |
| 1730 | ep->name, req->req.length); |
| 1731 | spin_lock_irqsave(&udc->lock, flags); |
| 1732 | /* push the request to device */ |
| 1733 | if (ep_is_in(ep)) |
| 1734 | reval = ep_req_send(ep, req); |
| 1735 | |
| 1736 | /* EP0 */ |
| 1737 | if (ep_index(ep) == 0 && req->req.length > 0) { |
| 1738 | if (ep_is_in(ep)) |
| 1739 | udc->ep0_state = DATA_STATE_XMIT; |
| 1740 | else |
| 1741 | udc->ep0_state = DATA_STATE_RECV; |
| 1742 | } |
| 1743 | |
| 1744 | if (ep->dir == USB_DIR_OUT) |
| 1745 | reval = ep_req_receive(ep, req); |
| 1746 | |
| 1747 | spin_unlock_irqrestore(&udc->lock, flags); |
| 1748 | |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | /* dequeues (cancels, unlinks) an I/O request from an endpoint */ |
| 1753 | static int qe_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req) |
| 1754 | { |
| 1755 | struct qe_ep *ep = container_of(_ep, struct qe_ep, ep); |
| 1756 | struct qe_req *req; |
| 1757 | unsigned long flags; |
| 1758 | |
| 1759 | if (!_ep || !_req) |
| 1760 | return -EINVAL; |
| 1761 | |
| 1762 | spin_lock_irqsave(&ep->udc->lock, flags); |
| 1763 | |
| 1764 | /* make sure it's actually queued on this endpoint */ |
| 1765 | list_for_each_entry(req, &ep->queue, queue) { |
| 1766 | if (&req->req == _req) |
| 1767 | break; |
| 1768 | } |
| 1769 | |
| 1770 | if (&req->req != _req) { |
| 1771 | spin_unlock_irqrestore(&ep->udc->lock, flags); |
| 1772 | return -EINVAL; |
| 1773 | } |
| 1774 | |
| 1775 | done(ep, req, -ECONNRESET); |
| 1776 | |
| 1777 | spin_unlock_irqrestore(&ep->udc->lock, flags); |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | /*----------------------------------------------------------------- |
| 1782 | * modify the endpoint halt feature |
| 1783 | * @ep: the non-isochronous endpoint being stalled |
| 1784 | * @value: 1--set halt 0--clear halt |
| 1785 | * Returns zero, or a negative error code. |
| 1786 | *----------------------------------------------------------------*/ |
| 1787 | static int qe_ep_set_halt(struct usb_ep *_ep, int value) |
| 1788 | { |
| 1789 | struct qe_ep *ep; |
| 1790 | unsigned long flags; |
| 1791 | int status = -EOPNOTSUPP; |
| 1792 | struct qe_udc *udc; |
| 1793 | |
| 1794 | ep = container_of(_ep, struct qe_ep, ep); |
| 1795 | if (!_ep || !ep->desc) { |
| 1796 | status = -EINVAL; |
| 1797 | goto out; |
| 1798 | } |
| 1799 | |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1800 | udc = ep->udc; |
| 1801 | /* Attempt to halt IN ep will fail if any transfer requests |
| 1802 | * are still queue */ |
| 1803 | if (value && ep_is_in(ep) && !list_empty(&ep->queue)) { |
| 1804 | status = -EAGAIN; |
| 1805 | goto out; |
| 1806 | } |
| 1807 | |
| 1808 | status = 0; |
| 1809 | spin_lock_irqsave(&ep->udc->lock, flags); |
| 1810 | qe_eptx_stall_change(ep, value); |
| 1811 | qe_eprx_stall_change(ep, value); |
| 1812 | spin_unlock_irqrestore(&ep->udc->lock, flags); |
| 1813 | |
| 1814 | if (ep->epnum == 0) { |
| 1815 | udc->ep0_state = WAIT_FOR_SETUP; |
| 1816 | udc->ep0_dir = 0; |
| 1817 | } |
Li Yang | 15d5a9a | 2008-09-24 15:50:27 +0800 | [diff] [blame] | 1818 | |
| 1819 | /* set data toggle to DATA0 on clear halt */ |
| 1820 | if (value == 0) |
| 1821 | ep->data01 = 0; |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1822 | out: |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 1823 | dev_vdbg(udc->dev, "%s %s halt stat %d\n", ep->ep.name, |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1824 | value ? "set" : "clear", status); |
| 1825 | |
| 1826 | return status; |
| 1827 | } |
| 1828 | |
| 1829 | static struct usb_ep_ops qe_ep_ops = { |
| 1830 | .enable = qe_ep_enable, |
| 1831 | .disable = qe_ep_disable, |
| 1832 | |
| 1833 | .alloc_request = qe_alloc_request, |
| 1834 | .free_request = qe_free_request, |
| 1835 | |
| 1836 | .queue = qe_ep_queue, |
| 1837 | .dequeue = qe_ep_dequeue, |
| 1838 | |
| 1839 | .set_halt = qe_ep_set_halt, |
| 1840 | }; |
| 1841 | |
| 1842 | /*------------------------------------------------------------------------ |
| 1843 | Gadget Driver Layer Operations |
| 1844 | ------------------------------------------------------------------------*/ |
| 1845 | |
| 1846 | /* Get the current frame number */ |
| 1847 | static int qe_get_frame(struct usb_gadget *gadget) |
| 1848 | { |
| 1849 | u16 tmp; |
| 1850 | |
| 1851 | tmp = in_be16(&udc_controller->usb_param->frame_n); |
| 1852 | if (tmp & 0x8000) |
| 1853 | tmp = tmp & 0x07ff; |
| 1854 | else |
| 1855 | tmp = -EINVAL; |
| 1856 | |
| 1857 | return (int)tmp; |
| 1858 | } |
| 1859 | |
| 1860 | /* Tries to wake up the host connected to this gadget |
| 1861 | * |
| 1862 | * Return : 0-success |
| 1863 | * Negative-this feature not enabled by host or not supported by device hw |
| 1864 | */ |
| 1865 | static int qe_wakeup(struct usb_gadget *gadget) |
| 1866 | { |
| 1867 | return -ENOTSUPP; |
| 1868 | } |
| 1869 | |
| 1870 | /* Notify controller that VBUS is powered, Called by whatever |
| 1871 | detects VBUS sessions */ |
| 1872 | static int qe_vbus_session(struct usb_gadget *gadget, int is_active) |
| 1873 | { |
| 1874 | return -ENOTSUPP; |
| 1875 | } |
| 1876 | |
| 1877 | /* constrain controller's VBUS power usage |
| 1878 | * This call is used by gadget drivers during SET_CONFIGURATION calls, |
| 1879 | * reporting how much power the device may consume. For example, this |
| 1880 | * could affect how quickly batteries are recharged. |
| 1881 | * |
| 1882 | * Returns zero on success, else negative errno. |
| 1883 | */ |
| 1884 | static int qe_vbus_draw(struct usb_gadget *gadget, unsigned mA) |
| 1885 | { |
| 1886 | return -ENOTSUPP; |
| 1887 | } |
| 1888 | |
| 1889 | /* Change Data+ pullup status |
| 1890 | * this func is used by usb_gadget_connect/disconnect |
| 1891 | */ |
| 1892 | static int qe_pullup(struct usb_gadget *gadget, int is_on) |
| 1893 | { |
| 1894 | return -ENOTSUPP; |
| 1895 | } |
| 1896 | |
| 1897 | /* defined in usb_gadget.h */ |
| 1898 | static struct usb_gadget_ops qe_gadget_ops = { |
| 1899 | .get_frame = qe_get_frame, |
| 1900 | .wakeup = qe_wakeup, |
| 1901 | /* .set_selfpowered = qe_set_selfpowered,*/ /* always selfpowered */ |
| 1902 | .vbus_session = qe_vbus_session, |
| 1903 | .vbus_draw = qe_vbus_draw, |
| 1904 | .pullup = qe_pullup, |
| 1905 | }; |
| 1906 | |
| 1907 | /*------------------------------------------------------------------------- |
| 1908 | USB ep0 Setup process in BUS Enumeration |
| 1909 | -------------------------------------------------------------------------*/ |
| 1910 | static int udc_reset_ep_queue(struct qe_udc *udc, u8 pipe) |
| 1911 | { |
| 1912 | struct qe_ep *ep = &udc->eps[pipe]; |
| 1913 | |
| 1914 | nuke(ep, -ECONNRESET); |
| 1915 | ep->tx_req = NULL; |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
| 1919 | static int reset_queues(struct qe_udc *udc) |
| 1920 | { |
| 1921 | u8 pipe; |
| 1922 | |
| 1923 | for (pipe = 0; pipe < USB_MAX_ENDPOINTS; pipe++) |
| 1924 | udc_reset_ep_queue(udc, pipe); |
| 1925 | |
| 1926 | /* report disconnect; the driver is already quiesced */ |
| 1927 | spin_unlock(&udc->lock); |
| 1928 | udc->driver->disconnect(&udc->gadget); |
| 1929 | spin_lock(&udc->lock); |
| 1930 | |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | static void ch9setaddress(struct qe_udc *udc, u16 value, u16 index, |
| 1935 | u16 length) |
| 1936 | { |
| 1937 | /* Save the new address to device struct */ |
| 1938 | udc->device_address = (u8) value; |
| 1939 | /* Update usb state */ |
| 1940 | udc->usb_state = USB_STATE_ADDRESS; |
| 1941 | |
| 1942 | /* Status phase , send a ZLP */ |
| 1943 | if (ep0_prime_status(udc, USB_DIR_IN)) |
| 1944 | qe_ep0_stall(udc); |
| 1945 | } |
| 1946 | |
| 1947 | static void ownercomplete(struct usb_ep *_ep, struct usb_request *_req) |
| 1948 | { |
| 1949 | struct qe_req *req = container_of(_req, struct qe_req, req); |
| 1950 | |
| 1951 | req->req.buf = NULL; |
| 1952 | kfree(req); |
| 1953 | } |
| 1954 | |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 1955 | static void ch9getstatus(struct qe_udc *udc, u8 request_type, u16 value, |
| 1956 | u16 index, u16 length) |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1957 | { |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 1958 | u16 usb_status = 0; |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1959 | struct qe_req *req; |
| 1960 | struct qe_ep *ep; |
| 1961 | int status = 0; |
| 1962 | |
| 1963 | ep = &udc->eps[0]; |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 1964 | if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) { |
| 1965 | /* Get device status */ |
| 1966 | usb_status = 1 << USB_DEVICE_SELF_POWERED; |
| 1967 | } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) { |
| 1968 | /* Get interface status */ |
| 1969 | /* We don't have interface information in udc driver */ |
| 1970 | usb_status = 0; |
| 1971 | } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) { |
| 1972 | /* Get endpoint status */ |
| 1973 | int pipe = index & USB_ENDPOINT_NUMBER_MASK; |
| 1974 | struct qe_ep *target_ep = &udc->eps[pipe]; |
| 1975 | u16 usep; |
| 1976 | |
| 1977 | /* stall if endpoint doesn't exist */ |
| 1978 | if (!target_ep->desc) |
| 1979 | goto stall; |
| 1980 | |
| 1981 | usep = in_be16(&udc->usb_regs->usb_usep[pipe]); |
| 1982 | if (index & USB_DIR_IN) { |
| 1983 | if (target_ep->dir != USB_DIR_IN) |
| 1984 | goto stall; |
| 1985 | if ((usep & USB_THS_MASK) == USB_THS_STALL) |
| 1986 | usb_status = 1 << USB_ENDPOINT_HALT; |
| 1987 | } else { |
| 1988 | if (target_ep->dir != USB_DIR_OUT) |
| 1989 | goto stall; |
| 1990 | if ((usep & USB_RHS_MASK) == USB_RHS_STALL) |
| 1991 | usb_status = 1 << USB_ENDPOINT_HALT; |
| 1992 | } |
| 1993 | } |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 1994 | |
| 1995 | req = container_of(qe_alloc_request(&ep->ep, GFP_KERNEL), |
| 1996 | struct qe_req, req); |
| 1997 | req->req.length = 2; |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 1998 | req->req.buf = udc->statusbuf; |
| 1999 | *(u16 *)req->req.buf = cpu_to_le16(usb_status); |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2000 | req->req.status = -EINPROGRESS; |
| 2001 | req->req.actual = 0; |
| 2002 | req->req.complete = ownercomplete; |
| 2003 | |
| 2004 | udc->ep0_dir = USB_DIR_IN; |
| 2005 | |
| 2006 | /* data phase */ |
| 2007 | status = qe_ep_queue(&ep->ep, &req->req, GFP_ATOMIC); |
| 2008 | |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2009 | if (status == 0) |
| 2010 | return; |
| 2011 | stall: |
| 2012 | dev_err(udc->dev, "Can't respond to getstatus request \n"); |
| 2013 | qe_ep0_stall(udc); |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | /* only handle the setup request, suppose the device in normal status */ |
| 2017 | static void setup_received_handle(struct qe_udc *udc, |
| 2018 | struct usb_ctrlrequest *setup) |
| 2019 | { |
| 2020 | /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/ |
| 2021 | u16 wValue = le16_to_cpu(setup->wValue); |
| 2022 | u16 wIndex = le16_to_cpu(setup->wIndex); |
| 2023 | u16 wLength = le16_to_cpu(setup->wLength); |
| 2024 | |
| 2025 | /* clear the previous request in the ep0 */ |
| 2026 | udc_reset_ep_queue(udc, 0); |
| 2027 | |
| 2028 | if (setup->bRequestType & USB_DIR_IN) |
| 2029 | udc->ep0_dir = USB_DIR_IN; |
| 2030 | else |
| 2031 | udc->ep0_dir = USB_DIR_OUT; |
| 2032 | |
| 2033 | switch (setup->bRequest) { |
| 2034 | case USB_REQ_GET_STATUS: |
| 2035 | /* Data+Status phase form udc */ |
| 2036 | if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK)) |
| 2037 | != (USB_DIR_IN | USB_TYPE_STANDARD)) |
| 2038 | break; |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2039 | ch9getstatus(udc, setup->bRequestType, wValue, wIndex, |
| 2040 | wLength); |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2041 | return; |
| 2042 | |
| 2043 | case USB_REQ_SET_ADDRESS: |
| 2044 | /* Status phase from udc */ |
| 2045 | if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD | |
| 2046 | USB_RECIP_DEVICE)) |
| 2047 | break; |
| 2048 | ch9setaddress(udc, wValue, wIndex, wLength); |
| 2049 | return; |
| 2050 | |
| 2051 | case USB_REQ_CLEAR_FEATURE: |
| 2052 | case USB_REQ_SET_FEATURE: |
| 2053 | /* Requests with no data phase, status phase from udc */ |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2054 | if ((setup->bRequestType & USB_TYPE_MASK) |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2055 | != USB_TYPE_STANDARD) |
| 2056 | break; |
| 2057 | |
| 2058 | if ((setup->bRequestType & USB_RECIP_MASK) |
| 2059 | == USB_RECIP_ENDPOINT) { |
| 2060 | int pipe = wIndex & USB_ENDPOINT_NUMBER_MASK; |
| 2061 | struct qe_ep *ep; |
| 2062 | |
| 2063 | if (wValue != 0 || wLength != 0 |
| 2064 | || pipe > USB_MAX_ENDPOINTS) |
| 2065 | break; |
| 2066 | ep = &udc->eps[pipe]; |
| 2067 | |
| 2068 | spin_unlock(&udc->lock); |
| 2069 | qe_ep_set_halt(&ep->ep, |
| 2070 | (setup->bRequest == USB_REQ_SET_FEATURE) |
| 2071 | ? 1 : 0); |
| 2072 | spin_lock(&udc->lock); |
| 2073 | } |
| 2074 | |
| 2075 | ep0_prime_status(udc, USB_DIR_IN); |
| 2076 | |
| 2077 | return; |
| 2078 | |
| 2079 | default: |
| 2080 | break; |
| 2081 | } |
| 2082 | |
| 2083 | if (wLength) { |
| 2084 | /* Data phase from gadget, status phase from udc */ |
| 2085 | if (setup->bRequestType & USB_DIR_IN) { |
| 2086 | udc->ep0_state = DATA_STATE_XMIT; |
| 2087 | udc->ep0_dir = USB_DIR_IN; |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2088 | } else { |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2089 | udc->ep0_state = DATA_STATE_RECV; |
| 2090 | udc->ep0_dir = USB_DIR_OUT; |
| 2091 | } |
| 2092 | spin_unlock(&udc->lock); |
| 2093 | if (udc->driver->setup(&udc->gadget, |
| 2094 | &udc->local_setup_buff) < 0) |
| 2095 | qe_ep0_stall(udc); |
| 2096 | spin_lock(&udc->lock); |
| 2097 | } else { |
| 2098 | /* No data phase, IN status from gadget */ |
| 2099 | udc->ep0_dir = USB_DIR_IN; |
| 2100 | spin_unlock(&udc->lock); |
| 2101 | if (udc->driver->setup(&udc->gadget, |
| 2102 | &udc->local_setup_buff) < 0) |
| 2103 | qe_ep0_stall(udc); |
| 2104 | spin_lock(&udc->lock); |
| 2105 | udc->ep0_state = DATA_STATE_NEED_ZLP; |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | /*------------------------------------------------------------------------- |
| 2110 | USB Interrupt handlers |
| 2111 | -------------------------------------------------------------------------*/ |
| 2112 | static void suspend_irq(struct qe_udc *udc) |
| 2113 | { |
| 2114 | udc->resume_state = udc->usb_state; |
| 2115 | udc->usb_state = USB_STATE_SUSPENDED; |
| 2116 | |
| 2117 | /* report suspend to the driver ,serial.c not support this*/ |
| 2118 | if (udc->driver->suspend) |
| 2119 | udc->driver->suspend(&udc->gadget); |
| 2120 | } |
| 2121 | |
| 2122 | static void resume_irq(struct qe_udc *udc) |
| 2123 | { |
| 2124 | udc->usb_state = udc->resume_state; |
| 2125 | udc->resume_state = 0; |
| 2126 | |
| 2127 | /* report resume to the driver , serial.c not support this*/ |
| 2128 | if (udc->driver->resume) |
| 2129 | udc->driver->resume(&udc->gadget); |
| 2130 | } |
| 2131 | |
| 2132 | static void idle_irq(struct qe_udc *udc) |
| 2133 | { |
| 2134 | u8 usbs; |
| 2135 | |
| 2136 | usbs = in_8(&udc->usb_regs->usb_usbs); |
| 2137 | if (usbs & USB_IDLE_STATUS_MASK) { |
| 2138 | if ((udc->usb_state) != USB_STATE_SUSPENDED) |
| 2139 | suspend_irq(udc); |
| 2140 | } else { |
| 2141 | if (udc->usb_state == USB_STATE_SUSPENDED) |
| 2142 | resume_irq(udc); |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | static int reset_irq(struct qe_udc *udc) |
| 2147 | { |
| 2148 | unsigned char i; |
| 2149 | |
| 2150 | qe_usb_disable(); |
| 2151 | out_8(&udc->usb_regs->usb_usadr, 0); |
| 2152 | |
| 2153 | for (i = 0; i < USB_MAX_ENDPOINTS; i++) { |
| 2154 | if (udc->eps[i].init) |
| 2155 | qe_ep_reset(udc, i); |
| 2156 | } |
| 2157 | |
| 2158 | reset_queues(udc); |
| 2159 | udc->usb_state = USB_STATE_DEFAULT; |
| 2160 | udc->ep0_state = WAIT_FOR_SETUP; |
| 2161 | udc->ep0_dir = USB_DIR_OUT; |
| 2162 | qe_usb_enable(); |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | static int bsy_irq(struct qe_udc *udc) |
| 2167 | { |
| 2168 | return 0; |
| 2169 | } |
| 2170 | |
| 2171 | static int txe_irq(struct qe_udc *udc) |
| 2172 | { |
| 2173 | return 0; |
| 2174 | } |
| 2175 | |
| 2176 | /* ep0 tx interrupt also in here */ |
| 2177 | static int tx_irq(struct qe_udc *udc) |
| 2178 | { |
| 2179 | struct qe_ep *ep; |
| 2180 | struct qe_bd __iomem *bd; |
| 2181 | int i, res = 0; |
| 2182 | |
| 2183 | if ((udc->usb_state == USB_STATE_ADDRESS) |
| 2184 | && (in_8(&udc->usb_regs->usb_usadr) == 0)) |
| 2185 | out_8(&udc->usb_regs->usb_usadr, udc->device_address); |
| 2186 | |
| 2187 | for (i = (USB_MAX_ENDPOINTS-1); ((i >= 0) && (res == 0)); i--) { |
| 2188 | ep = &udc->eps[i]; |
| 2189 | if (ep && ep->init && (ep->dir != USB_DIR_OUT)) { |
| 2190 | bd = ep->c_txbd; |
| 2191 | if (!(in_be32((u32 __iomem *)bd) & T_R) |
| 2192 | && (in_be32(&bd->buf))) { |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2193 | /* confirm the transmitted bd */ |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2194 | if (ep->epnum == 0) |
| 2195 | res = qe_ep0_txconf(ep); |
| 2196 | else |
| 2197 | res = qe_ep_txconf(ep); |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | return res; |
| 2202 | } |
| 2203 | |
| 2204 | |
| 2205 | /* setup packect's rx is handle in the function too */ |
| 2206 | static void rx_irq(struct qe_udc *udc) |
| 2207 | { |
| 2208 | struct qe_ep *ep; |
| 2209 | struct qe_bd __iomem *bd; |
| 2210 | int i; |
| 2211 | |
| 2212 | for (i = 0; i < USB_MAX_ENDPOINTS; i++) { |
| 2213 | ep = &udc->eps[i]; |
| 2214 | if (ep && ep->init && (ep->dir != USB_DIR_IN)) { |
| 2215 | bd = ep->n_rxbd; |
| 2216 | if (!(in_be32((u32 __iomem *)bd) & R_E) |
| 2217 | && (in_be32(&bd->buf))) { |
| 2218 | if (ep->epnum == 0) { |
| 2219 | qe_ep0_rx(udc); |
| 2220 | } else { |
| 2221 | /*non-setup package receive*/ |
| 2222 | qe_ep_rx(ep); |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | static irqreturn_t qe_udc_irq(int irq, void *_udc) |
| 2230 | { |
| 2231 | struct qe_udc *udc = (struct qe_udc *)_udc; |
| 2232 | u16 irq_src; |
| 2233 | irqreturn_t status = IRQ_NONE; |
| 2234 | unsigned long flags; |
| 2235 | |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2236 | spin_lock_irqsave(&udc->lock, flags); |
| 2237 | |
| 2238 | irq_src = in_be16(&udc->usb_regs->usb_usber) & |
| 2239 | in_be16(&udc->usb_regs->usb_usbmr); |
| 2240 | /* Clear notification bits */ |
| 2241 | out_be16(&udc->usb_regs->usb_usber, irq_src); |
| 2242 | /* USB Interrupt */ |
| 2243 | if (irq_src & USB_E_IDLE_MASK) { |
| 2244 | idle_irq(udc); |
| 2245 | irq_src &= ~USB_E_IDLE_MASK; |
| 2246 | status = IRQ_HANDLED; |
| 2247 | } |
| 2248 | |
| 2249 | if (irq_src & USB_E_TXB_MASK) { |
| 2250 | tx_irq(udc); |
| 2251 | irq_src &= ~USB_E_TXB_MASK; |
| 2252 | status = IRQ_HANDLED; |
| 2253 | } |
| 2254 | |
| 2255 | if (irq_src & USB_E_RXB_MASK) { |
| 2256 | rx_irq(udc); |
| 2257 | irq_src &= ~USB_E_RXB_MASK; |
| 2258 | status = IRQ_HANDLED; |
| 2259 | } |
| 2260 | |
| 2261 | if (irq_src & USB_E_RESET_MASK) { |
| 2262 | reset_irq(udc); |
| 2263 | irq_src &= ~USB_E_RESET_MASK; |
| 2264 | status = IRQ_HANDLED; |
| 2265 | } |
| 2266 | |
| 2267 | if (irq_src & USB_E_BSY_MASK) { |
| 2268 | bsy_irq(udc); |
| 2269 | irq_src &= ~USB_E_BSY_MASK; |
| 2270 | status = IRQ_HANDLED; |
| 2271 | } |
| 2272 | |
| 2273 | if (irq_src & USB_E_TXE_MASK) { |
| 2274 | txe_irq(udc); |
| 2275 | irq_src &= ~USB_E_TXE_MASK; |
| 2276 | status = IRQ_HANDLED; |
| 2277 | } |
| 2278 | |
| 2279 | spin_unlock_irqrestore(&udc->lock, flags); |
| 2280 | |
| 2281 | return status; |
| 2282 | } |
| 2283 | |
| 2284 | /*------------------------------------------------------------------------- |
| 2285 | Gadget driver register and unregister. |
| 2286 | --------------------------------------------------------------------------*/ |
| 2287 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 2288 | { |
| 2289 | int retval; |
| 2290 | unsigned long flags = 0; |
| 2291 | |
| 2292 | /* standard operations */ |
| 2293 | if (!udc_controller) |
| 2294 | return -ENODEV; |
| 2295 | |
| 2296 | if (!driver || (driver->speed != USB_SPEED_FULL |
| 2297 | && driver->speed != USB_SPEED_HIGH) |
| 2298 | || !driver->bind || !driver->disconnect |
| 2299 | || !driver->setup) |
| 2300 | return -EINVAL; |
| 2301 | |
| 2302 | if (udc_controller->driver) |
| 2303 | return -EBUSY; |
| 2304 | |
| 2305 | /* lock is needed but whether should use this lock or another */ |
| 2306 | spin_lock_irqsave(&udc_controller->lock, flags); |
| 2307 | |
| 2308 | driver->driver.bus = NULL; |
| 2309 | /* hook up the driver */ |
| 2310 | udc_controller->driver = driver; |
| 2311 | udc_controller->gadget.dev.driver = &driver->driver; |
| 2312 | udc_controller->gadget.speed = (enum usb_device_speed)(driver->speed); |
| 2313 | spin_unlock_irqrestore(&udc_controller->lock, flags); |
| 2314 | |
| 2315 | retval = driver->bind(&udc_controller->gadget); |
| 2316 | if (retval) { |
| 2317 | dev_err(udc_controller->dev, "bind to %s --> %d", |
| 2318 | driver->driver.name, retval); |
| 2319 | udc_controller->gadget.dev.driver = NULL; |
| 2320 | udc_controller->driver = NULL; |
| 2321 | return retval; |
| 2322 | } |
| 2323 | |
| 2324 | /* Enable IRQ reg and Set usbcmd reg EN bit */ |
| 2325 | qe_usb_enable(); |
| 2326 | |
| 2327 | out_be16(&udc_controller->usb_regs->usb_usber, 0xffff); |
| 2328 | out_be16(&udc_controller->usb_regs->usb_usbmr, USB_E_DEFAULT_DEVICE); |
| 2329 | udc_controller->usb_state = USB_STATE_ATTACHED; |
| 2330 | udc_controller->ep0_state = WAIT_FOR_SETUP; |
| 2331 | udc_controller->ep0_dir = USB_DIR_OUT; |
| 2332 | dev_info(udc_controller->dev, "%s bind to driver %s \n", |
| 2333 | udc_controller->gadget.name, driver->driver.name); |
| 2334 | return 0; |
| 2335 | } |
| 2336 | EXPORT_SYMBOL(usb_gadget_register_driver); |
| 2337 | |
| 2338 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 2339 | { |
| 2340 | struct qe_ep *loop_ep; |
| 2341 | unsigned long flags; |
| 2342 | |
| 2343 | if (!udc_controller) |
| 2344 | return -ENODEV; |
| 2345 | |
| 2346 | if (!driver || driver != udc_controller->driver) |
| 2347 | return -EINVAL; |
| 2348 | |
| 2349 | /* stop usb controller, disable intr */ |
| 2350 | qe_usb_disable(); |
| 2351 | |
| 2352 | /* in fact, no needed */ |
| 2353 | udc_controller->usb_state = USB_STATE_ATTACHED; |
| 2354 | udc_controller->ep0_state = WAIT_FOR_SETUP; |
| 2355 | udc_controller->ep0_dir = 0; |
| 2356 | |
| 2357 | /* stand operation */ |
| 2358 | spin_lock_irqsave(&udc_controller->lock, flags); |
| 2359 | udc_controller->gadget.speed = USB_SPEED_UNKNOWN; |
| 2360 | nuke(&udc_controller->eps[0], -ESHUTDOWN); |
| 2361 | list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list, |
| 2362 | ep.ep_list) |
| 2363 | nuke(loop_ep, -ESHUTDOWN); |
| 2364 | spin_unlock_irqrestore(&udc_controller->lock, flags); |
| 2365 | |
Anton Vorontsov | 9ac36da | 2008-11-13 14:57:20 +0300 | [diff] [blame^] | 2366 | /* report disconnect; the controller is already quiesced */ |
| 2367 | driver->disconnect(&udc_controller->gadget); |
| 2368 | |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2369 | /* unbind gadget and unhook driver. */ |
| 2370 | driver->unbind(&udc_controller->gadget); |
| 2371 | udc_controller->gadget.dev.driver = NULL; |
| 2372 | udc_controller->driver = NULL; |
| 2373 | |
| 2374 | dev_info(udc_controller->dev, "unregistered gadget driver '%s'\r\n", |
| 2375 | driver->driver.name); |
| 2376 | return 0; |
| 2377 | } |
| 2378 | EXPORT_SYMBOL(usb_gadget_unregister_driver); |
| 2379 | |
| 2380 | /* udc structure's alloc and setup, include ep-param alloc */ |
| 2381 | static struct qe_udc __devinit *qe_udc_config(struct of_device *ofdev) |
| 2382 | { |
| 2383 | struct qe_udc *udc; |
| 2384 | struct device_node *np = ofdev->node; |
| 2385 | unsigned int tmp_addr = 0; |
| 2386 | struct usb_device_para __iomem *usbpram; |
| 2387 | unsigned int i; |
| 2388 | u64 size; |
| 2389 | u32 offset; |
| 2390 | |
| 2391 | udc = kzalloc(sizeof(*udc), GFP_KERNEL); |
| 2392 | if (udc == NULL) { |
| 2393 | dev_err(&ofdev->dev, "malloc udc failed\n"); |
| 2394 | goto cleanup; |
| 2395 | } |
| 2396 | |
| 2397 | udc->dev = &ofdev->dev; |
| 2398 | |
| 2399 | /* get default address of usb parameter in MURAM from device tree */ |
| 2400 | offset = *of_get_address(np, 1, &size, NULL); |
| 2401 | udc->usb_param = cpm_muram_addr(offset); |
| 2402 | memset_io(udc->usb_param, 0, size); |
| 2403 | |
| 2404 | usbpram = udc->usb_param; |
| 2405 | out_be16(&usbpram->frame_n, 0); |
| 2406 | out_be32(&usbpram->rstate, 0); |
| 2407 | |
| 2408 | tmp_addr = cpm_muram_alloc((USB_MAX_ENDPOINTS * |
| 2409 | sizeof(struct usb_ep_para)), |
| 2410 | USB_EP_PARA_ALIGNMENT); |
| 2411 | |
| 2412 | for (i = 0; i < USB_MAX_ENDPOINTS; i++) { |
| 2413 | out_be16(&usbpram->epptr[i], (u16)tmp_addr); |
| 2414 | udc->ep_param[i] = cpm_muram_addr(tmp_addr); |
| 2415 | tmp_addr += 32; |
| 2416 | } |
| 2417 | |
| 2418 | memset_io(udc->ep_param[0], 0, |
| 2419 | USB_MAX_ENDPOINTS * sizeof(struct usb_ep_para)); |
| 2420 | |
| 2421 | udc->resume_state = USB_STATE_NOTATTACHED; |
| 2422 | udc->usb_state = USB_STATE_POWERED; |
| 2423 | udc->ep0_dir = 0; |
| 2424 | |
| 2425 | spin_lock_init(&udc->lock); |
| 2426 | return udc; |
| 2427 | |
| 2428 | cleanup: |
| 2429 | kfree(udc); |
| 2430 | return NULL; |
| 2431 | } |
| 2432 | |
| 2433 | /* USB Controller register init */ |
| 2434 | static int __devinit qe_udc_reg_init(struct qe_udc *udc) |
| 2435 | { |
| 2436 | struct usb_ctlr __iomem *qe_usbregs; |
| 2437 | qe_usbregs = udc->usb_regs; |
| 2438 | |
| 2439 | /* Init the usb register */ |
| 2440 | out_8(&qe_usbregs->usb_usmod, 0x01); |
| 2441 | out_be16(&qe_usbregs->usb_usbmr, 0); |
| 2442 | out_8(&qe_usbregs->usb_uscom, 0); |
| 2443 | out_be16(&qe_usbregs->usb_usber, USBER_ALL_CLEAR); |
| 2444 | |
| 2445 | return 0; |
| 2446 | } |
| 2447 | |
| 2448 | static int __devinit qe_ep_config(struct qe_udc *udc, unsigned char pipe_num) |
| 2449 | { |
| 2450 | struct qe_ep *ep = &udc->eps[pipe_num]; |
| 2451 | |
| 2452 | ep->udc = udc; |
| 2453 | strcpy(ep->name, ep_name[pipe_num]); |
| 2454 | ep->ep.name = ep_name[pipe_num]; |
| 2455 | |
| 2456 | ep->ep.ops = &qe_ep_ops; |
| 2457 | ep->stopped = 1; |
| 2458 | ep->ep.maxpacket = (unsigned short) ~0; |
| 2459 | ep->desc = NULL; |
| 2460 | ep->dir = 0xff; |
| 2461 | ep->epnum = (u8)pipe_num; |
| 2462 | ep->sent = 0; |
| 2463 | ep->last = 0; |
| 2464 | ep->init = 0; |
| 2465 | ep->rxframe = NULL; |
| 2466 | ep->txframe = NULL; |
| 2467 | ep->tx_req = NULL; |
| 2468 | ep->state = EP_STATE_IDLE; |
| 2469 | ep->has_data = 0; |
| 2470 | |
| 2471 | /* the queue lists any req for this ep */ |
| 2472 | INIT_LIST_HEAD(&ep->queue); |
| 2473 | |
| 2474 | /* gagdet.ep_list used for ep_autoconfig so no ep0*/ |
| 2475 | if (pipe_num != 0) |
| 2476 | list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); |
| 2477 | |
| 2478 | ep->gadget = &udc->gadget; |
| 2479 | |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
| 2483 | /*----------------------------------------------------------------------- |
| 2484 | * UDC device Driver operation functions * |
| 2485 | *----------------------------------------------------------------------*/ |
| 2486 | static void qe_udc_release(struct device *dev) |
| 2487 | { |
| 2488 | int i = 0; |
| 2489 | |
| 2490 | complete(udc_controller->done); |
| 2491 | cpm_muram_free(cpm_muram_offset(udc_controller->ep_param[0])); |
| 2492 | for (i = 0; i < USB_MAX_ENDPOINTS; i++) |
| 2493 | udc_controller->ep_param[i] = NULL; |
| 2494 | |
| 2495 | kfree(udc_controller); |
| 2496 | udc_controller = NULL; |
| 2497 | } |
| 2498 | |
| 2499 | /* Driver probe functions */ |
| 2500 | static int __devinit qe_udc_probe(struct of_device *ofdev, |
| 2501 | const struct of_device_id *match) |
| 2502 | { |
| 2503 | struct device_node *np = ofdev->node; |
| 2504 | struct qe_ep *ep; |
| 2505 | unsigned int ret = 0; |
| 2506 | unsigned int i; |
| 2507 | const void *prop; |
| 2508 | |
| 2509 | prop = of_get_property(np, "mode", NULL); |
| 2510 | if (!prop || strcmp(prop, "peripheral")) |
| 2511 | return -ENODEV; |
| 2512 | |
| 2513 | /* Initialize the udc structure including QH member and other member */ |
| 2514 | udc_controller = qe_udc_config(ofdev); |
| 2515 | if (!udc_controller) { |
| 2516 | dev_dbg(&ofdev->dev, "udc_controll is NULL\n"); |
| 2517 | return -ENOMEM; |
| 2518 | } |
| 2519 | |
| 2520 | udc_controller->soc_type = (unsigned long)match->data; |
| 2521 | udc_controller->usb_regs = of_iomap(np, 0); |
| 2522 | if (!udc_controller->usb_regs) { |
| 2523 | ret = -ENOMEM; |
| 2524 | goto err1; |
| 2525 | } |
| 2526 | |
| 2527 | /* initialize usb hw reg except for regs for EP, |
| 2528 | * leave usbintr reg untouched*/ |
| 2529 | qe_udc_reg_init(udc_controller); |
| 2530 | |
| 2531 | /* here comes the stand operations for probe |
| 2532 | * set the qe_udc->gadget.xxx */ |
| 2533 | udc_controller->gadget.ops = &qe_gadget_ops; |
| 2534 | |
| 2535 | /* gadget.ep0 is a pointer */ |
| 2536 | udc_controller->gadget.ep0 = &udc_controller->eps[0].ep; |
| 2537 | |
| 2538 | INIT_LIST_HEAD(&udc_controller->gadget.ep_list); |
| 2539 | |
| 2540 | /* modify in register gadget process */ |
| 2541 | udc_controller->gadget.speed = USB_SPEED_UNKNOWN; |
| 2542 | |
| 2543 | /* name: Identifies the controller hardware type. */ |
| 2544 | udc_controller->gadget.name = driver_name; |
| 2545 | |
| 2546 | device_initialize(&udc_controller->gadget.dev); |
| 2547 | |
| 2548 | strcpy(udc_controller->gadget.dev.bus_id, "gadget"); |
| 2549 | |
| 2550 | udc_controller->gadget.dev.release = qe_udc_release; |
| 2551 | udc_controller->gadget.dev.parent = &ofdev->dev; |
| 2552 | |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2553 | /* initialize qe_ep struct */ |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2554 | for (i = 0; i < USB_MAX_ENDPOINTS ; i++) { |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2555 | /* because the ep type isn't decide here so |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2556 | * qe_ep_init() should be called in ep_enable() */ |
| 2557 | |
| 2558 | /* setup the qe_ep struct and link ep.ep.list |
| 2559 | * into gadget.ep_list */ |
| 2560 | qe_ep_config(udc_controller, (unsigned char)i); |
| 2561 | } |
| 2562 | |
| 2563 | /* ep0 initialization in here */ |
| 2564 | ret = qe_ep_init(udc_controller, 0, &qe_ep0_desc); |
| 2565 | if (ret) |
| 2566 | goto err2; |
| 2567 | |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2568 | /* create a buf for ZLP send, need to remain zeroed */ |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2569 | udc_controller->nullbuf = kzalloc(256, GFP_KERNEL); |
| 2570 | if (udc_controller->nullbuf == NULL) { |
| 2571 | dev_dbg(udc_controller->dev, "cannot alloc nullbuf\n"); |
| 2572 | ret = -ENOMEM; |
| 2573 | goto err3; |
| 2574 | } |
| 2575 | |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2576 | /* buffer for data of get_status request */ |
| 2577 | udc_controller->statusbuf = kzalloc(2, GFP_KERNEL); |
| 2578 | if (udc_controller->statusbuf == NULL) { |
| 2579 | ret = -ENOMEM; |
| 2580 | goto err4; |
| 2581 | } |
| 2582 | |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2583 | udc_controller->nullp = virt_to_phys((void *)udc_controller->nullbuf); |
| 2584 | if (udc_controller->nullp == DMA_ADDR_INVALID) { |
| 2585 | udc_controller->nullp = dma_map_single( |
| 2586 | udc_controller->gadget.dev.parent, |
| 2587 | udc_controller->nullbuf, |
| 2588 | 256, |
| 2589 | DMA_TO_DEVICE); |
| 2590 | udc_controller->nullmap = 1; |
| 2591 | } else { |
| 2592 | dma_sync_single_for_device(udc_controller->gadget.dev.parent, |
| 2593 | udc_controller->nullp, 256, |
| 2594 | DMA_TO_DEVICE); |
| 2595 | } |
| 2596 | |
| 2597 | tasklet_init(&udc_controller->rx_tasklet, ep_rx_tasklet, |
| 2598 | (unsigned long)udc_controller); |
| 2599 | /* request irq and disable DR */ |
| 2600 | udc_controller->usb_irq = irq_of_parse_and_map(np, 0); |
| 2601 | |
| 2602 | ret = request_irq(udc_controller->usb_irq, qe_udc_irq, 0, |
| 2603 | driver_name, udc_controller); |
| 2604 | if (ret) { |
| 2605 | dev_err(udc_controller->dev, "cannot request irq %d err %d \n", |
| 2606 | udc_controller->usb_irq, ret); |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2607 | goto err5; |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | ret = device_add(&udc_controller->gadget.dev); |
| 2611 | if (ret) |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2612 | goto err6; |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2613 | |
| 2614 | dev_info(udc_controller->dev, |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2615 | "%s USB controller initialized as device\n", |
| 2616 | (udc_controller->soc_type == PORT_QE) ? "QE" : "CPM"); |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2617 | return 0; |
| 2618 | |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2619 | err6: |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2620 | free_irq(udc_controller->usb_irq, udc_controller); |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2621 | err5: |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2622 | if (udc_controller->nullmap) { |
| 2623 | dma_unmap_single(udc_controller->gadget.dev.parent, |
| 2624 | udc_controller->nullp, 256, |
| 2625 | DMA_TO_DEVICE); |
| 2626 | udc_controller->nullp = DMA_ADDR_INVALID; |
| 2627 | } else { |
| 2628 | dma_sync_single_for_cpu(udc_controller->gadget.dev.parent, |
| 2629 | udc_controller->nullp, 256, |
| 2630 | DMA_TO_DEVICE); |
| 2631 | } |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2632 | kfree(udc_controller->statusbuf); |
| 2633 | err4: |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2634 | kfree(udc_controller->nullbuf); |
| 2635 | err3: |
| 2636 | ep = &udc_controller->eps[0]; |
| 2637 | cpm_muram_free(cpm_muram_offset(ep->rxbase)); |
| 2638 | kfree(ep->rxframe); |
| 2639 | kfree(ep->rxbuffer); |
| 2640 | kfree(ep->txframe); |
| 2641 | err2: |
| 2642 | iounmap(udc_controller->usb_regs); |
| 2643 | err1: |
| 2644 | kfree(udc_controller); |
| 2645 | |
| 2646 | return ret; |
| 2647 | } |
| 2648 | |
| 2649 | #ifdef CONFIG_PM |
| 2650 | static int qe_udc_suspend(struct of_device *dev, pm_message_t state) |
| 2651 | { |
| 2652 | return -ENOTSUPP; |
| 2653 | } |
| 2654 | |
| 2655 | static int qe_udc_resume(struct of_device *dev) |
| 2656 | { |
| 2657 | return -ENOTSUPP; |
| 2658 | } |
| 2659 | #endif |
| 2660 | |
| 2661 | static int __devexit qe_udc_remove(struct of_device *ofdev) |
| 2662 | { |
| 2663 | struct qe_ep *ep; |
| 2664 | unsigned int size; |
| 2665 | |
| 2666 | DECLARE_COMPLETION(done); |
| 2667 | |
| 2668 | if (!udc_controller) |
| 2669 | return -ENODEV; |
| 2670 | |
| 2671 | udc_controller->done = &done; |
| 2672 | tasklet_disable(&udc_controller->rx_tasklet); |
| 2673 | |
| 2674 | if (udc_controller->nullmap) { |
| 2675 | dma_unmap_single(udc_controller->gadget.dev.parent, |
| 2676 | udc_controller->nullp, 256, |
| 2677 | DMA_TO_DEVICE); |
| 2678 | udc_controller->nullp = DMA_ADDR_INVALID; |
| 2679 | } else { |
| 2680 | dma_sync_single_for_cpu(udc_controller->gadget.dev.parent, |
| 2681 | udc_controller->nullp, 256, |
| 2682 | DMA_TO_DEVICE); |
| 2683 | } |
Li Yang | 928dfa6 | 2008-09-24 15:50:26 +0800 | [diff] [blame] | 2684 | kfree(udc_controller->statusbuf); |
Li Yang | 3948f0e | 2008-09-02 19:58:10 +0800 | [diff] [blame] | 2685 | kfree(udc_controller->nullbuf); |
| 2686 | |
| 2687 | ep = &udc_controller->eps[0]; |
| 2688 | cpm_muram_free(cpm_muram_offset(ep->rxbase)); |
| 2689 | size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * (USB_BDRING_LEN + 1); |
| 2690 | |
| 2691 | kfree(ep->rxframe); |
| 2692 | if (ep->rxbufmap) { |
| 2693 | dma_unmap_single(udc_controller->gadget.dev.parent, |
| 2694 | ep->rxbuf_d, size, |
| 2695 | DMA_FROM_DEVICE); |
| 2696 | ep->rxbuf_d = DMA_ADDR_INVALID; |
| 2697 | } else { |
| 2698 | dma_sync_single_for_cpu(udc_controller->gadget.dev.parent, |
| 2699 | ep->rxbuf_d, size, |
| 2700 | DMA_FROM_DEVICE); |
| 2701 | } |
| 2702 | |
| 2703 | kfree(ep->rxbuffer); |
| 2704 | kfree(ep->txframe); |
| 2705 | |
| 2706 | free_irq(udc_controller->usb_irq, udc_controller); |
| 2707 | |
| 2708 | tasklet_kill(&udc_controller->rx_tasklet); |
| 2709 | |
| 2710 | iounmap(udc_controller->usb_regs); |
| 2711 | |
| 2712 | device_unregister(&udc_controller->gadget.dev); |
| 2713 | /* wait for release() of gadget.dev to free udc */ |
| 2714 | wait_for_completion(&done); |
| 2715 | |
| 2716 | return 0; |
| 2717 | } |
| 2718 | |
| 2719 | /*-------------------------------------------------------------------------*/ |
| 2720 | static struct of_device_id __devinitdata qe_udc_match[] = { |
| 2721 | { |
| 2722 | .compatible = "fsl,mpc8360-qe-usb", |
| 2723 | .data = (void *)PORT_QE, |
| 2724 | }, |
| 2725 | { |
| 2726 | .compatible = "fsl,mpc8272-cpm-usb", |
| 2727 | .data = (void *)PORT_CPM, |
| 2728 | }, |
| 2729 | {}, |
| 2730 | }; |
| 2731 | |
| 2732 | MODULE_DEVICE_TABLE(of, qe_udc_match); |
| 2733 | |
| 2734 | static struct of_platform_driver udc_driver = { |
| 2735 | .name = (char *)driver_name, |
| 2736 | .match_table = qe_udc_match, |
| 2737 | .probe = qe_udc_probe, |
| 2738 | .remove = __devexit_p(qe_udc_remove), |
| 2739 | #ifdef CONFIG_PM |
| 2740 | .suspend = qe_udc_suspend, |
| 2741 | .resume = qe_udc_resume, |
| 2742 | #endif |
| 2743 | }; |
| 2744 | |
| 2745 | static int __init qe_udc_init(void) |
| 2746 | { |
| 2747 | printk(KERN_INFO "%s: %s, %s\n", driver_name, driver_desc, |
| 2748 | DRIVER_VERSION); |
| 2749 | return of_register_platform_driver(&udc_driver); |
| 2750 | } |
| 2751 | |
| 2752 | static void __exit qe_udc_exit(void) |
| 2753 | { |
| 2754 | of_unregister_platform_driver(&udc_driver); |
| 2755 | } |
| 2756 | |
| 2757 | module_init(qe_udc_init); |
| 2758 | module_exit(qe_udc_exit); |
| 2759 | |
| 2760 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 2761 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 2762 | MODULE_LICENSE("GPL"); |
| 2763 | |