19 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/list.h>
27 #include <linux/device.h>
32 #include <linux/slab.h>
33 #include <linux/prefetch.h>
35 #include <linux/usb/ch9.h>
39 #include <mach/hardware.h>
44 static const char ep0name[] =
"ep0";
99 for (j = 0; j < 5; j++) {
100 i = (j == 2 ? imx_usb->
imx_ep[0].fifosize : 0x00);
108 for (cfg = 1; cfg < 3; cfg++) {
112 ep_conf[0] = (i << 4) | (cfg << 2);
124 "<%s> ep%d_conf[%d]:"
125 "[%02x-%02x-%02x-%02x-%02x]\n",
127 ep_conf[0], ep_conf[1], ep_conf[2],
128 ep_conf[3], ep_conf[4]);
130 for (j = 0; j < 5; j++) {
188 temp = (
EP_DIR(imx_ep) << 7) | (max << 5)
193 D_INI(imx_usb->
dev,
"<%s> ep%d_stat %08x\n", __func__, i,
206 temp =
EP_DIR(imx_ep) ? 0x0B000000 : 0x0F000000;
208 D_INI(imx_usb->
dev,
"<%s> ep%d_fctrl %08x\n", __func__, i,
212 temp = (i ? imx_ep->
fifosize / 2 : 0);
214 D_INI(imx_usb->
dev,
"<%s> ep%d_falrm %08x\n", __func__, i,
288 "<%s> Forced stall on %s\n", __func__, imx_ep->
ep.name);
293 if (!
EP_NO(imx_ep)) {
307 for (i = 0; i < 100; i ++) {
315 D_ERR(imx_usb->
dev,
"<%s> Non finished stall on %s\n",
316 __func__, imx_ep->
ep.name);
320 static int imx_udc_get_frame(
struct usb_gadget *_gadget)
328 static int imx_udc_wakeup(
struct usb_gadget *_gadget)
354 list_del_init(&req->
queue);
361 ep_del_request(imx_ep, req);
366 status = req->
req.status;
370 "<%s> complete %s req %p stat %d len %u/%u\n", __func__,
371 imx_ep->
ep.name, &req->
req, status,
372 req->
req.actual, req->
req.length);
374 req->
req.complete(&imx_ep->
ep, &req->
req);
381 while (!list_empty(&imx_ep->
queue)) {
383 done(imx_ep, req, status);
394 int bytes_ep, bufferspace,
count,
i;
397 bufferspace = req->
req.length - req->
req.actual;
399 buf = req->
req.buf + req->
req.actual;
405 count =
min(bytes_ep, bufferspace);
407 for (i = count; i > 0; i--)
422 D_TRX(imx_ep->
imx_usb->dev,
"<%s> zlp still queued in EP %s\n",
423 __func__, imx_ep->
ep.name);
427 buf = req->
req.buf + req->
req.actual;
433 D_TRX(imx_ep->
imx_usb->dev,
"<%s> packet overfill %s fifo\n",
434 __func__, imx_ep->
ep.name);
441 if (!count && req->
req.zero) {
446 D_TRX(imx_ep->
imx_usb->dev,
"<%s> zero packet\n", __func__);
472 count = read_packet(imx_ep, req);
475 completed = (count != imx_ep->
fifosize);
476 if (completed || req->
req.actual == req->
req.length) {
482 if (completed || !req->
req.length) {
483 done(imx_ep, req, 0);
485 __func__, imx_ep->
ep.name, req,
486 completed ?
"completed" :
"not completed");
491 D_TRX(imx_ep->
imx_usb->dev,
"<%s> bytes read: %d\n", __func__, bytes);
503 count = write_packet(imx_ep, req);
509 completed = (count != imx_ep->
fifosize);
512 done(imx_ep, req, 0);
514 __func__, imx_ep->
ep.name, req,
515 completed ?
"completed" :
"not completed");
522 D_TRX(imx_ep->
imx_usb->dev,
"<%s> bytes sent: %d\n", __func__, bytes);
537 if (!list_empty(&imx_ep->
queue))
542 __func__, imx_ep->
ep.name);
547 completed = write_fifo(imx_ep, req);
549 completed = read_fifo(imx_ep, req);
563 if (!list_empty(&imx_ep->
queue)) {
566 switch (imx_ep->
imx_usb->ep0state) {
569 write_fifo(imx_ep, req);
572 read_fifo(imx_ep, req);
576 "<%s> ep0 i/o, odd state %d\n",
577 __func__, imx_ep->
imx_usb->ep0state);
578 ep_del_request(imx_ep, req);
586 __func__, imx_ep->
ep.name);
604 for (i = 0; i < 2; i++) {
607 "<%s> no setup packet received\n", __func__);
618 "<%s> wrong to have extra bytes for setup : 0x%08x\n",
628 D_REQ(imx_usb->
dev,
"<%s> SETUP %02x.%02x v%04x i%04x l%04x\n",
629 __func__,
u.r.bRequestType,
u.r.bRequest,
630 u.r.wValue,
u.r.wIndex,
u.r.wLength);
638 "<%s> set config req is pending, NACK the host\n",
650 D_ERR(imx_usb->
dev,
"<%s> device setup error %d\n",
657 D_ERR(imx_usb->
dev,
"<%s> protocol STALL\n", __func__);
682 "<%s> bad ep or descriptor\n", __func__);
688 "<%s> %s type mismatch\n", __func__, usb_ep->
name);
692 if (imx_ep->
fifosize < usb_endpoint_maxp(desc)) {
694 "<%s> bad %s maxpacket\n", __func__, usb_ep->
name);
699 D_ERR(imx_usb->
dev,
"<%s> bogus device state\n", __func__);
711 D_EPX(imx_usb->
dev,
"<%s> ENABLED %s\n", __func__, usb_ep->
name);
715 static int imx_ep_disable(
struct usb_ep *usb_ep)
721 if (!usb_ep || !
EP_NO(imx_ep) || !list_empty(&imx_ep->
queue)) {
722 D_ERR(imx_ep->
imx_usb->dev,
"<%s> %s can not be disabled\n",
723 __func__, usb_ep ? imx_ep->
ep.name :
NULL);
737 "<%s> DISABLED %s\n", __func__, usb_ep->
name);
742 (
struct usb_ep *usb_ep,
gfp_t gfp_flags)
749 req = kzalloc(
sizeof *req, gfp_flags);
753 INIT_LIST_HEAD(&req->
queue);
759 static void imx_ep_free_request
769 static int imx_ep_queue
791 "<%s> gadget reply set config\n", __func__);
796 D_ERR(imx_usb->
dev,
"<%s> bad params\n", __func__);
801 D_ERR(imx_usb->
dev,
"<%s> bad ep\n", __func__);
806 D_ERR(imx_usb->
dev,
"<%s> bogus device state\n", __func__);
811 D_REQ(imx_usb->
dev,
"<%s> ep%d %s request for [%d] bytes\n",
812 __func__,
EP_NO(imx_ep),
816 ?
"IN" :
"OUT", usb_req->
length);
817 dump_req(__func__, imx_ep, usb_req);
826 "<%s> refusing to queue req %p (already queued)\n",
836 ep_add_request(imx_ep, req);
839 ret = handle_ep0(imx_ep);
841 ret = handle_ep(imx_ep);
847 static int imx_ep_dequeue(
struct usb_ep *usb_ep,
struct usb_request *usb_req)
864 if (&req->
req == usb_req)
867 if (&req->
req != usb_req) {
878 static int imx_ep_set_halt(
struct usb_ep *usb_ep,
int value)
892 && !list_empty(&imx_ep->
queue)) {
905 static int imx_ep_fifo_status(
struct usb_ep *usb_ep)
921 static void imx_ep_fifo_flush(
struct usb_ep *usb_ep)
929 if (!usb_ep || !
EP_NO(imx_ep) || !list_empty(&imx_ep->
queue)) {
942 .enable = imx_ep_enable,
943 .disable = imx_ep_disable,
945 .alloc_request = imx_ep_alloc_request,
946 .free_request = imx_ep_free_request,
948 .queue = imx_ep_queue,
949 .dequeue = imx_ep_dequeue,
951 .set_halt = imx_ep_set_halt,
952 .fifo_status = imx_ep_fifo_status,
953 .fifo_flush = imx_ep_fifo_flush,
964 D_EP0(imx_usb->
dev,
"<%s> from %15s to %15s\n",
965 label, state_name[imx_usb->
ep0state], state_name[stat]);
979 INIT_LIST_HEAD(&imx_usb->
gadget.ep_list);
980 INIT_LIST_HEAD(&imx_usb->
gadget.ep0->ep_list);
989 &imx_usb->
gadget.ep_list);
994 INIT_LIST_HEAD(&imx_ep->
queue);
1033 static void handle_config(
unsigned long data)
1047 "<%s> orig config C=%d, I=%d, A=%d / "
1048 "req config C=%d, I=%d, A=%d\n",
1049 __func__, imx_usb->
cfg, imx_usb->
intf, imx_usb->
alt,
1052 if (cfg == 1 || cfg == 2) {
1054 if (imx_usb->
cfg != cfg) {
1066 if (imx_usb->
intf != intf || imx_usb->
alt != alt) {
1153 udc_stop_activity(imx_usb, imx_usb->
driver);
1167 static irqreturn_t imx_udc_ctrl_irq(
int irq,
void *dev)
1182 handle_ep0_devreq(imx_usb);
1187 else if (!list_empty(&imx_ep->
queue))
1195 #ifndef MX1_INT_USBD0
1196 #define MX1_INT_USBD0 MX1_USBD_INT0
1199 static irqreturn_t imx_udc_bulk_irq(
int irq,
void *dev)
1223 return imx_udc_ctrl_irq;
1229 return imx_udc_bulk_irq;
1240 static int imx_udc_start(
struct usb_gadget *gadget,
1242 static int imx_udc_stop(
struct usb_gadget *gadget,
1245 .get_frame = imx_udc_get_frame,
1246 .wakeup = imx_udc_wakeup,
1247 .udc_start = imx_udc_start,
1248 .udc_stop = imx_udc_stop,
1253 .ops = &imx_udc_ops,
1254 .ep0 = &controller.
imx_ep[0].ep,
1257 .init_name =
"gadget",
1269 .bEndpointAddress = 0,
1274 .name =
"ep1in-bulk",
1285 .name =
"ep2out-bulk",
1296 .name =
"ep3out-bulk",
1307 .name =
"ep4in-int",
1318 .name =
"ep5out-int",
1348 D_INI(imx_usb->
dev,
"<%s> registered gadget driver '%s'\n",
1349 __func__, driver->
driver.name);
1360 static int imx_udc_stop(
struct usb_gadget *gadget,
1366 udc_stop_activity(imx_usb, driver);
1375 D_INI(imx_usb->
dev,
"<%s> unregistered gadget driver '%s'\n",
1376 __func__, driver->
driver.name);
1399 dev_err(&pdev->
dev,
"can't get device resources\n");
1403 pdata = pdev->
dev.platform_data;
1405 dev_err(&pdev->
dev,
"driver needs platform data\n");
1409 res_size = resource_size(res);
1411 dev_err(&pdev->
dev,
"can't allocate %d bytes at %d address\n",
1412 res_size, res->
start);
1417 ret = pdata->
init(&pdev->
dev);
1432 dev_err(&pdev->
dev,
"can't get USB clock\n");
1435 clk_prepare_enable(clk);
1439 "Bad USB clock (%d Hz), changing to 48000000 Hz\n",
1443 "Unable to set correct USB clock (48MHz)\n");
1449 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1452 dev_err(&pdev->
dev,
"can't get irq number\n");
1458 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1462 dev_err(&pdev->
dev,
"can't get irq %i, err %d\n",
1464 for (--i; i >= 0; i--)
1471 imx_usb->
base = base;
1473 imx_usb->
dev = &pdev->
dev;
1477 imx_usb->
gadget.dev.parent = &pdev->
dev;
1478 imx_usb->
gadget.dev.dma_mask = pdev->
dev.dma_mask;
1480 platform_set_drvdata(pdev, imx_usb);
1482 usb_init_data(imx_usb);
1483 imx_udc_init(imx_usb);
1486 imx_usb->
timer.function = handle_config;
1487 imx_usb->
timer.data = (
unsigned long)imx_usb;
1495 for (i = 0; i < IMX_USB_NB_EP + 1; i++)
1499 clk_disable_unprepare(clk);
1520 for (i = 0; i < IMX_USB_NB_EP + 1; i++)
1524 clk_disable_unprepare(imx_usb->
clk);
1532 platform_set_drvdata(pdev,
NULL);
1540 #define imx_udc_suspend NULL
1541 #define imx_udc_resume NULL
1543 #define imx_udc_suspend NULL
1544 #define imx_udc_resume NULL
1554 .remove =
__exit_p(imx_udc_remove),
1559 static int __init udc_init(
void)
1565 static void __exit udc_exit(
void)