16 #include <linux/module.h>
18 #include <linux/usb/ch9.h>
28 #define DRIVER_VERSION "20 October 2010"
30 static const char udc_name[] =
"fusb300_udc";
31 static const char *
const fusb300_ep_name[] = {
32 "ep0",
"ep1",
"ep2",
"ep3",
"ep4",
"ep5",
"ep6",
"ep7",
"ep8",
"ep9",
33 "ep10",
"ep11",
"ep12",
"ep13",
"ep14",
"ep15"
58 static void fusb300_ep_setting(
struct fusb300_ep *ep,
65 static int fusb300_ep_release(
struct fusb300_ep *ep)
85 static void fusb300_set_start_entry(
struct fusb300 *fusb300,
97 pr_err(
"fifo entry is over the maximum number!\n");
103 static void fusb300_set_epaddrofs(
struct fusb300 *fusb300,
114 static void ep_fifo_setting(
struct fusb300 *fusb300,
117 fusb300_set_fifo_entry(fusb300, info.
epnum);
118 fusb300_set_start_entry(fusb300, info.
epnum);
119 fusb300_set_epaddrofs(fusb300, info);
122 static void fusb300_set_eptype(
struct fusb300 *fusb300,
132 static void fusb300_set_epdir(
struct fusb300 *fusb300,
145 static void fusb300_set_ep_active(
struct fusb300 *fusb300,
154 static void fusb300_set_epmps(
struct fusb300 *fusb300,
164 static void fusb300_set_interval(
struct fusb300 *fusb300,
174 static void fusb300_set_bwnum(
struct fusb300 *fusb300,
184 static void set_ep_reg(
struct fusb300 *fusb300,
187 fusb300_set_eptype(fusb300, info);
188 fusb300_set_epdir(fusb300, info);
189 fusb300_set_epmps(fusb300, info);
192 fusb300_set_interval(fusb300, info);
195 fusb300_set_bwnum(fusb300, info);
197 fusb300_set_ep_active(fusb300, info.
epnum);
203 struct fusb300 *fusb300 = ep->
fusb300;
214 info.
maxpacket = usb_endpoint_maxp(desc);
224 ep_fifo_setting(fusb300, info);
226 set_ep_reg(fusb300, info);
228 fusb300_ep_setting(ep, info);
235 static int fusb300_enable(
struct usb_ep *_ep,
243 ep->
fusb300->fifo_entry_num = 0;
248 return config_ep(ep, desc);
251 static int fusb300_disable(
struct usb_ep *_ep)
261 while (!list_empty(&ep->
queue)) {
265 spin_unlock_irqrestore(&ep->
fusb300->lock, flags);
268 return fusb300_ep_release(ep);
279 INIT_LIST_HEAD(&req->
queue);
292 static int enable_fifo_int(
struct fusb300_ep *ep)
294 struct fusb300 *fusb300 = ep->
fusb300;
300 pr_err(
"can't enable_fifo_int ep0\n");
307 static int disable_fifo_int(
struct fusb300_ep *ep)
309 struct fusb300 *fusb300 = ep->
fusb300;
315 pr_err(
"can't disable_fifo_int ep0\n");
322 static void fusb300_set_cxlen(
struct fusb300 *fusb300,
u32 length)
333 static void fusb300_wrcxf(
struct fusb300_ep *ep,
339 struct fusb300 *fusb300 = ep->
fusb300;
342 tmp = req->
req.buf + req->
req.actual;
347 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
354 fusb300_set_cxlen(fusb300, length);
355 for (i = length >> 2; i > 0; i--) {
356 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
362 switch (length % 4) {
369 data = *tmp | *(tmp + 1) << 8;
374 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16;
385 static void fusb300_set_epnstall(
struct fusb300 *fusb300,
u8 ep)
391 static void fusb300_clear_epnstall(
struct fusb300 *fusb300,
u8 ep)
397 reg &= ~FUSB300_EPSET0_STL;
405 if (req->
req.length) {
406 fusb300_wrcxf(ep, req);
409 __func__, req->
req.length);
410 if ((req->
req.length == req->
req.actual) ||
411 (req->
req.actual < ep->
ep.maxpacket))
414 if (!req->
req.length)
438 if (list_empty(&ep->
queue))
448 else if (request && !ep->
stall)
451 spin_unlock_irqrestore(&ep->
fusb300->lock, flags);
466 if (!list_empty(&ep->
queue))
468 spin_unlock_irqrestore(&ep->
fusb300->lock, flags);
473 static int fusb300_set_halt_and_wedge(
struct usb_ep *_ep,
int value,
int wedge)
476 struct fusb300 *fusb300;
486 if (!list_empty(&ep->
queue)) {
492 fusb300_set_epnstall(fusb300, ep->
epnum);
497 fusb300_clear_epnstall(fusb300, ep->
epnum);
503 spin_unlock_irqrestore(&ep->
fusb300->lock, flags);
507 static int fusb300_set_halt(
struct usb_ep *_ep,
int value)
509 return fusb300_set_halt_and_wedge(_ep, value, 0);
512 static int fusb300_set_wedge(
struct usb_ep *_ep)
514 return fusb300_set_halt_and_wedge(_ep, 1, 1);
517 static void fusb300_fifo_flush(
struct usb_ep *_ep)
522 .enable = fusb300_enable,
523 .disable = fusb300_disable,
525 .alloc_request = fusb300_alloc_request,
526 .free_request = fusb300_free_request,
528 .queue = fusb300_queue,
529 .dequeue = fusb300_dequeue,
531 .set_halt = fusb300_set_halt,
532 .fifo_flush = fusb300_fifo_flush,
533 .set_wedge = fusb300_set_wedge,
537 static void fusb300_clear_int(
struct fusb300 *fusb300,
u32 offset,
543 static void fusb300_reset(
void)
547 static void fusb300_set_cxstall(
struct fusb300 *fusb300)
553 static void fusb300_set_cxdone(
struct fusb300 *fusb300)
569 for (i = (length >> 2); i > 0; i--) {
573 *(tmp + 1) = (data >> 8) & 0xFF;
574 *(tmp + 2) = (data >> 16) & 0xFF;
575 *(tmp + 3) = (data >> 24) & 0xFF;
579 switch (length % 4) {
589 *(tmp + 1) = (data >> 8) & 0xFF;
595 *(tmp + 1) = (data >> 8) & 0xFF;
596 *(tmp + 2) = (data >> 16) & 0xFF;
603 static void fusb300_rdfifo(
struct fusb300_ep *ep,
610 struct fusb300 *fusb300 = ep->
fusb300;
612 tmp = req->
req.buf + req->
req.actual;
615 if (req->
req.actual > req->
req.length)
618 for (i = (length >> 2); i > 0; i--) {
622 *(tmp + 1) = (data >> 8) & 0xFF;
623 *(tmp + 2) = (data >> 16) & 0xFF;
624 *(tmp + 3) = (data >> 24) & 0xFF;
628 switch (length % 4) {
638 *(tmp + 1) = (data >> 8) & 0xFF;
644 *(tmp + 1) = (data >> 8) & 0xFF;
645 *(tmp + 2) = (data >> 16) & 0xFF;
660 static u8 fusb300_get_epnstall(
struct fusb300 *fusb300,
u8 ep)
670 static u8 fusb300_get_cxstall(
struct fusb300 *fusb300)
680 static void request_error(
struct fusb300 *fusb300)
682 fusb300_set_cxstall(fusb300);
704 if (fusb300_get_epnstall(fusb300, ep))
707 if (fusb300_get_cxstall(fusb300))
713 request_error(fusb300);
721 spin_unlock(&fusb300->
lock);
723 spin_lock(&fusb300->
lock);
732 fusb300_set_cxdone(fusb300);
735 fusb300_set_cxdone(fusb300);
742 fusb300_set_epnstall(fusb300, ep);
744 fusb300_set_cxstall(fusb300);
745 fusb300_set_cxdone(fusb300);
749 request_error(fusb300);
754 static void fusb300_clear_seqnum(
struct fusb300 *fusb300,
u8 ep)
760 static void clear_feature(
struct fusb300 *fusb300,
struct usb_ctrlrequest *ctrl)
767 fusb300_set_cxdone(fusb300);
770 fusb300_set_cxdone(fusb300);
773 if (ctrl->
wIndex & USB_ENDPOINT_NUMBER_MASK) {
775 fusb300_set_cxdone(fusb300);
780 fusb300_clear_seqnum(fusb300, ep->
epnum);
781 fusb300_clear_epnstall(fusb300, ep->
epnum);
782 if (!list_empty(&ep->
queue))
786 fusb300_set_cxdone(fusb300);
789 request_error(fusb300);
794 static void fusb300_set_dev_addr(
struct fusb300 *fusb300,
u16 addr)
804 static void set_address(
struct fusb300 *fusb300,
struct usb_ctrlrequest *ctrl)
806 if (ctrl->
wValue >= 0x0100)
807 request_error(fusb300);
809 fusb300_set_dev_addr(fusb300, ctrl->
wValue);
810 fusb300_set_cxdone(fusb300);
814 #define UVC_COPY_DESCRIPTORS(mem, src) \
816 const struct usb_descriptor_header * const *__src; \
817 for (__src = src; *__src; ++__src) { \
818 memcpy(mem, *__src, (*__src)->bLength); \
819 mem += (*__src)->bLength; \
823 static int setup_packet(
struct fusb300 *fusb300,
struct usb_ctrlrequest *ctrl)
840 clear_feature(fusb300, ctrl);
843 set_feature(fusb300, ctrl);
846 set_address(fusb300, ctrl);
853 fusb300_clear_seqnum(fusb300, i);
870 list_del_init(&req->
queue);
878 spin_unlock(&ep->
fusb300->lock);
879 req->
req.complete(&ep->
ep, &req->
req);
883 disable_fifo_int(ep);
884 if (!list_empty(&ep->
queue))
887 fusb300_set_cxdone(ep->
fusb300);
915 static void fusb300_wait_idma_finished(
struct fusb300_ep *ep)
938 static void fusb300_set_idma(
struct fusb300_ep *ep,
955 fusb300_fill_idma_prdtbl(ep, d, req->
req.length);
957 fusb300_wait_idma_finished(ep);
962 static void in_ep_fifo_handler(
struct fusb300_ep *ep)
968 fusb300_set_idma(ep, req);
972 static void out_ep_fifo_handler(
struct fusb300_ep *ep)
974 struct fusb300 *fusb300 = ep->
fusb300;
980 fusb300_rdfifo(ep, req, length);
983 if ((req->
req.length == req->
req.actual) || (length < ep->ep.maxpacket))
987 static void check_device_mode(
struct fusb300 *fusb300)
1009 static void fusb300_ep0out(
struct fusb300 *fusb300)
1014 if (!list_empty(&ep->
queue)) {
1019 if (req->
req.length)
1027 pr_err(
"%s : empty queue\n", __func__);
1030 static void fusb300_ep0in(
struct fusb300 *fusb300)
1038 if (req->
req.length)
1039 fusb300_wrcxf(ep, req);
1040 if ((req->
req.length - req->
req.actual) < ep->
ep.maxpacket)
1043 fusb300_set_cxdone(fusb300);
1046 static void fusb300_grp2_handler(
void)
1050 static void fusb300_grp3_handler(
void)
1054 static void fusb300_grp4_handler(
void)
1058 static void fusb300_grp5_handler(
void)
1062 static irqreturn_t fusb300_irq(
int irq,
void *_fusb300)
1064 struct fusb300 *fusb300 = _fusb300;
1074 spin_lock(&fusb300->
lock);
1076 int_grp1 &= int_grp1_en;
1077 int_grp0 &= int_grp0_en;
1079 if (int_grp1 & FUSB300_IGR1_WARM_RST_INT) {
1081 FUSB300_IGR1_WARM_RST_INT);
1086 if (int_grp1 & FUSB300_IGR1_HOT_RST_INT) {
1088 FUSB300_IGR1_HOT_RST_INT);
1095 FUSB300_IGR1_USBRST_INT);
1102 FUSB300_IGR1_CX_COMABT_INT);
1106 if (int_grp1 & FUSB300_IGR1_VBUS_CHG_INT) {
1108 FUSB300_IGR1_VBUS_CHG_INT);
1114 FUSB300_IGR1_U3_EXIT_FAIL_INT);
1119 FUSB300_IGR1_U2_EXIT_FAIL_INT);
1124 FUSB300_IGR1_U1_EXIT_FAIL_INT);
1129 FUSB300_IGR1_U2_ENTRY_FAIL_INT);
1134 FUSB300_IGR1_U1_ENTRY_FAIL_INT);
1139 FUSB300_IGR1_U3_EXIT_INT);
1145 FUSB300_IGR1_U2_EXIT_INT);
1151 FUSB300_IGR1_U1_EXIT_INT);
1157 FUSB300_IGR1_U3_ENTRY_INT);
1165 FUSB300_IGR1_U2_ENTRY_INT);
1171 FUSB300_IGR1_U1_ENTRY_INT);
1177 FUSB300_IGR1_RESM_INT);
1183 FUSB300_IGR1_SUSP_INT);
1189 FUSB300_IGR1_HS_LPM_INT);
1195 FUSB300_IGR1_DEV_MODE_CHG_INT);
1196 check_device_mode(fusb300);
1200 fusb300_set_cxstall(fusb300);
1206 if (setup_packet(fusb300, &ctrl)) {
1207 spin_unlock(&fusb300->
lock);
1208 if (fusb300->
driver->setup(&fusb300->
gadget, &ctrl) < 0)
1209 fusb300_set_cxstall(fusb300);
1210 spin_lock(&fusb300->
lock);
1220 fusb300_ep0out(fusb300);
1225 fusb300_ep0in(fusb300);
1229 fusb300_grp5_handler();
1232 fusb300_grp4_handler();
1235 fusb300_grp3_handler();
1238 fusb300_grp2_handler();
1247 in_ep_fifo_handler(fusb300->
ep[i]);
1249 out_ep_fifo_handler(fusb300->
ep[i]);
1254 spin_unlock(&fusb300->
lock);
1259 static void fusb300_set_u2_timeout(
struct fusb300 *fusb300,
1271 static void fusb300_set_u1_timeout(
struct fusb300 *fusb300,
1277 reg &= ~(0xff << 8);
1283 static void init_controller(
struct fusb300 *fusb300)
1304 fusb300_set_u2_timeout(fusb300, 0xff);
1305 fusb300_set_u1_timeout(fusb300, 0xff);
1338 pr_err(
"device_add error (%d)\n", retval);
1342 retval = bind(&fusb300->
gadget, driver);
1344 pr_err(
"bind to driver error (%d)\n", retval);
1368 init_controller(fusb300);
1382 .pullup = fusb300_udc_pullup,
1383 .start = fusb300_udc_start,
1384 .stop = fusb300_udc_stop,
1395 fusb300_free_request(&fusb300->
ep[0]->ep, fusb300->
ep0_req);
1405 struct fusb300 *fusb300 =
NULL;
1413 pr_err(
"platform_get_resource error.\n");
1421 "platform_get_resource IORESOURCE_IRQ error.\n");
1429 "platform_get_resource IORESOURCE_IRQ 1 error.\n");
1436 pr_err(
"ioremap error.\n");
1441 fusb300 = kzalloc(
sizeof(
struct fusb300),
GFP_KERNEL);
1442 if (fusb300 ==
NULL) {
1443 pr_err(
"kzalloc error\n");
1449 if (_ep[i] ==
NULL) {
1450 pr_err(
"_ep kzalloc error\n");
1453 fusb300->
ep[
i] = _ep[
i];
1460 fusb300->
gadget.ops = &fusb300_gadget_ops;
1467 fusb300->
gadget.dev.parent = &pdev->
dev;
1468 fusb300->
gadget.dev.dma_mask = pdev->
dev.dma_mask;
1469 fusb300->
gadget.dev.release = pdev->
dev.release;
1470 fusb300->
gadget.name = udc_name;
1476 pr_err(
"request_irq error (%d)\n", ret);
1483 pr_err(
"request_irq1 error (%d)\n", ret);
1487 INIT_LIST_HEAD(&fusb300->
gadget.ep_list);
1493 INIT_LIST_HEAD(&fusb300->
ep[i]->ep.ep_list);
1495 &fusb300->
gadget.ep_list);
1498 INIT_LIST_HEAD(&ep->
queue);
1499 ep->
ep.name = fusb300_ep_name[
i];
1500 ep->
ep.ops = &fusb300_ep_ops;
1504 fusb300->
ep[0]->epnum = 0;
1505 fusb300->
gadget.ep0 = &fusb300->
ep[0]->ep;
1506 INIT_LIST_HEAD(&fusb300->
gadget.ep0->ep_list);
1510 fusb300->
ep0_req = fusb300_alloc_request(&fusb300->
ep[0]->ep,
1515 init_controller(fusb300);
1524 fusb300_free_request(&fusb300->
ep[0]->ep, fusb300->
ep0_req);
1532 fusb300_free_request(&fusb300->
ep[0]->ep,
1543 .remove =
__exit_p(fusb300_remove),
1545 .name = (
char *) udc_name,
1550 static int __init fusb300_udc_init(
void)
1557 static void __exit fusb300_udc_cleanup(
void)