14 #include <linux/device.h>
20 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
27 #include <linux/usb/ch9.h>
39 ctrl_endpt_out_desc = {
49 ctrl_endpt_in_desc = {
65 static inline int hw_ep_bit(
int num,
int dir)
67 return num + (dir ? 16 : 0);
70 static inline int ep_to_bit(
struct ci13xxx *ci,
int n)
106 static int hw_ep_flush(
struct ci13xxx *ci,
int num,
int dir)
108 int n = hw_ep_bit(num, dir);
127 static int hw_ep_disable(
struct ci13xxx *ci,
int num,
int dir)
129 hw_ep_flush(ci, num, dir);
143 static int hw_ep_enable(
struct ci13xxx *ci,
int num,
int dir,
int type)
149 data = type << ffs_nr(mask);
158 data = type << ffs_nr(mask);
177 static int hw_ep_get_halt(
struct ci13xxx *ci,
int num,
int dir)
191 static int hw_test_and_clear_setup_status(
struct ci13xxx *ci,
int n)
193 n = ep_to_bit(ci, n);
205 static int hw_ep_prime(
struct ci13xxx *ci,
int num,
int dir,
int is_ctrl)
207 int n = hw_ep_bit(num, dir);
232 static int hw_ep_set_halt(
struct ci13xxx *ci,
int num,
int dir,
int value)
234 if (value != 0 && value != 1)
243 hw_write(ci, reg, mask_xs|mask_xr,
244 value ? mask_xs : mask_xr);
245 }
while (value != hw_ep_get_halt(ci, num, dir));
255 static int hw_port_is_high_speed(
struct ci13xxx *ci)
266 static u32 hw_read_intr_enable(
struct ci13xxx *ci)
276 static u32 hw_read_intr_status(
struct ci13xxx *ci)
288 static int hw_test_and_clear_complete(
struct ci13xxx *ci,
int n)
290 n = ep_to_bit(ci, n);
300 static u32 hw_test_and_clear_intr_active(
struct ci13xxx *ci)
302 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
308 static void hw_enable_vbus_intr(
struct ci13xxx *ci)
315 static void hw_disable_vbus_intr(
struct ci13xxx *ci)
326 static int hw_test_and_clear_setup_guard(
struct ci13xxx *ci)
337 static int hw_test_and_set_setup_guard(
struct ci13xxx *ci)
349 static void hw_usb_set_address(
struct ci13xxx *ci,
u8 value)
361 static int hw_usb_reset(
struct ci13xxx *ci)
363 hw_usb_set_address(ci, 0);
391 usb_gadget_vbus_connect(&ci->
gadget);
393 usb_gadget_vbus_disconnect(&ci->
gadget);
428 if (mReq->
req.zero && length && (length % mEp->
ep.maxpacket == 0)) {
437 if (!mReq->
req.no_interrupt)
456 if (!mReq->
req.no_interrupt)
459 mReq->
ptr->page[0] = mReq->
req.dma;
460 for (i = 1; i < 5; i++)
464 if (!list_empty(&mEp->
qh.queue)) {
466 int n = hw_ep_bit(mEp->
num, mEp->
dir);
488 mEp->
qh.ptr->td.next = mReq->
dma;
494 ret = hw_ep_prime(ci, mEp->
num, mEp->
dir,
522 mReq->
req.status = 0;
528 mReq->
req.status = -1;
530 mReq->
req.status = -1;
532 mReq->
req.status = -1;
536 mReq->
req.actual = mReq->
req.length - mReq->
req.actual;
537 mReq->
req.actual = mReq->
req.status ? 0 : mReq->
req.actual;
539 return mReq->
req.actual;
556 hw_ep_flush(mEp->
ci, mEp->
num, mEp->
dir);
558 while (!list_empty(&mEp->
qh.queue)) {
562 list_entry(mEp->
qh.queue.next,
564 list_del_init(&mReq->
queue);
567 if (mReq->
req.complete !=
NULL) {
568 spin_unlock(mEp->
lock);
569 mReq->
req.complete(&mEp->
ep, &mReq->
req);
570 spin_lock(mEp->
lock);
582 static int _gadget_stop_activity(
struct usb_gadget *gadget)
592 spin_unlock_irqrestore(&ci->
lock, flags);
596 usb_ep_fifo_flush(ep);
598 usb_ep_fifo_flush(&ci->
ep0out->ep);
599 usb_ep_fifo_flush(&ci->
ep0in->ep);
602 ci->
driver->disconnect(gadget);
626 static void isr_reset_handler(
struct ci13xxx *ci)
634 spin_unlock(&ci->
lock);
635 retval = _gadget_stop_activity(&ci->
gadget);
639 retval = hw_usb_reset(ci);
648 spin_lock(&ci->
lock);
667 usb_ep_free_request(ep, req);
677 static int isr_get_status_response(
struct ci13xxx *ci,
690 spin_unlock(mEp->
lock);
691 req = usb_ep_alloc_request(&mEp->
ep, gfp_flags);
692 spin_lock(mEp->
lock);
696 req->
complete = isr_get_status_complete;
698 req->
buf = kzalloc(req->
length, gfp_flags);
713 *(
u16 *)req->
buf = hw_ep_get_halt(ci, num, dir);
717 spin_unlock(mEp->
lock);
718 retval = usb_ep_queue(&mEp->
ep, req, gfp_flags);
719 spin_lock(mEp->
lock);
728 spin_unlock(mEp->
lock);
729 usb_ep_free_request(&mEp->
ep, req);
730 spin_lock(mEp->
lock);
749 hw_usb_set_address(ci, ci->
address);
756 spin_unlock_irqrestore(&ci->
lock, flags);
765 static int isr_setup_status_phase(
struct ci13xxx *ci)
774 ci->
status->complete = isr_setup_status_complete;
776 spin_unlock(mEp->
lock);
778 spin_lock(mEp->
lock);
790 static int isr_tr_complete_low(
struct ci13xxx_ep *mEp)
800 retval = _hardware_dequeue(mEp, mReq);
803 list_del_init(&mReq->
queue);
804 dbg_done(_usb_addr(mEp), mReq->
ptr->token, retval);
805 if (mReq->
req.complete !=
NULL) {
806 spin_unlock(mEp->
lock);
809 mEpTemp = mEp->
ci->ep0in;
810 mReq->
req.complete(&mEpTemp->
ep, &mReq->
req);
811 spin_lock(mEp->
lock);
815 if (retval == -
EBUSY)
818 dbg_event(_usb_addr(mEp),
"DONE", retval);
829 static void isr_tr_complete_handler(
struct ci13xxx *ci)
844 if (hw_test_and_clear_complete(ci, i)) {
845 err = isr_tr_complete_low(mEp);
848 err = isr_setup_status_phase(ci);
852 spin_unlock(&ci->
lock);
853 if (usb_ep_set_halt(&mEp->
ep))
855 "error: ep_set_halt\n");
856 spin_lock(&ci->
lock);
862 !hw_test_and_clear_setup_status(ci, i))
866 dev_warn(ci->
dev,
"ctrl traffic at endpoint %d\n", i);
879 hw_test_and_set_setup_guard(ci);
880 memcpy(&req, &mEp->
qh.ptr->setup,
sizeof(req));
881 }
while (!hw_test_and_clear_setup_guard(ci));
883 type = req.bRequestType;
889 switch (req.bRequest) {
894 if (req.wLength != 0)
902 spin_unlock(&ci->
lock);
903 err = usb_ep_clear_halt(
905 spin_lock(&ci->
lock);
909 err = isr_setup_status_phase(ci);
913 if (req.wLength != 0)
916 err = isr_setup_status_phase(ci);
929 err = isr_get_status_response(ci, &req);
939 err = isr_setup_status_phase(ci);
945 if (req.wLength != 0)
953 spin_unlock(&ci->
lock);
954 err = usb_ep_set_halt(&ci->
ci13xxx_ep[num].ep);
955 spin_lock(&ci->
lock);
957 isr_setup_status_phase(ci);
959 if (req.wLength != 0)
964 err = isr_setup_status_phase(ci);
975 err = isr_setup_status_phase(
990 if (req.wLength == 0)
993 spin_unlock(&ci->
lock);
995 spin_lock(&ci->
lock);
1000 dbg_event(_usb_addr(mEp),
"ERROR", err);
1002 spin_unlock(&ci->
lock);
1003 if (usb_ep_set_halt(&mEp->
ep))
1005 spin_lock(&ci->
lock);
1018 static int ep_enable(
struct usb_ep *ep,
1023 unsigned long flags;
1032 mEp->
ep.desc = desc;
1034 if (!list_empty(&mEp->
qh.queue))
1035 dev_warn(mEp->
ci->dev,
"enabling a non-empty endpoint!\n");
1037 mEp->
dir = usb_endpoint_dir_in(desc) ?
TX :
RX;
1038 mEp->
num = usb_endpoint_num(desc);
1039 mEp->
type = usb_endpoint_type(desc);
1041 mEp->
ep.maxpacket = usb_endpoint_maxp(desc);
1045 mEp->
qh.ptr->cap = 0;
1063 retval |= hw_ep_enable(mEp->
ci, mEp->
num, mEp->
dir, mEp->
type);
1065 spin_unlock_irqrestore(mEp->
lock, flags);
1074 static int ep_disable(
struct usb_ep *ep)
1078 unsigned long flags;
1082 else if (mEp->
ep.desc ==
NULL)
1089 direction = mEp->
dir;
1091 dbg_event(_usb_addr(mEp),
"DISABLE", 0);
1093 retval |= _ep_nuke(mEp);
1094 retval |= hw_ep_disable(mEp->
ci, mEp->
num, mEp->
dir);
1099 }
while (mEp->
dir != direction);
1103 spin_unlock_irqrestore(mEp->
lock, flags);
1120 mReq = kzalloc(
sizeof(
struct ci13xxx_req), gfp_flags);
1122 INIT_LIST_HEAD(&mReq->
queue);
1146 unsigned long flags;
1150 }
else if (!list_empty(&mReq->
queue)) {
1151 dev_err(mEp->
ci->dev,
"freeing queued request\n");
1163 spin_unlock_irqrestore(mEp->
lock, flags);
1178 unsigned long flags;
1189 if (!list_empty(&mEp->
qh.queue)) {
1192 dev_warn(mEp->
ci->dev,
"endpoint ctrl %X nuked\n",
1198 if (!list_empty(&mReq->
queue)) {
1200 dev_err(mEp->
ci->dev,
"request already in queue\n");
1204 if (req->
length > 4 * CI13XXX_PAGE_SIZE) {
1207 dev_warn(mEp->
ci->dev,
"request length truncated\n");
1214 mReq->
req.actual = 0;
1216 retval = _hardware_enqueue(mEp, mReq);
1219 dbg_event(_usb_addr(mEp),
"QUEUE", retval);
1226 spin_unlock_irqrestore(mEp->
lock, flags);
1239 unsigned long flags;
1242 mEp->
ep.desc ==
NULL || list_empty(&mReq->
queue) ||
1243 list_empty(&mEp->
qh.queue))
1248 dbg_event(_usb_addr(mEp),
"DEQUEUE", 0);
1250 hw_ep_flush(mEp->
ci, mEp->
num, mEp->
dir);
1253 list_del_init(&mReq->
queue);
1259 if (mReq->
req.complete !=
NULL) {
1260 spin_unlock(mEp->
lock);
1261 mReq->
req.complete(&mEp->
ep, &mReq->
req);
1262 spin_lock(mEp->
lock);
1265 spin_unlock_irqrestore(mEp->
lock, flags);
1274 static int ep_set_halt(
struct usb_ep *ep,
int value)
1278 unsigned long flags;
1288 !list_empty(&mEp->
qh.queue)) {
1289 spin_unlock_irqrestore(mEp->
lock, flags);
1294 direction = mEp->
dir;
1296 dbg_event(_usb_addr(mEp),
"HALT", value);
1297 retval |= hw_ep_set_halt(mEp->
ci, mEp->
num, mEp->
dir, value);
1305 }
while (mEp->
dir != direction);
1307 spin_unlock_irqrestore(mEp->
lock, flags);
1316 static int ep_set_wedge(
struct usb_ep *ep)
1319 unsigned long flags;
1329 spin_unlock_irqrestore(mEp->
lock, flags);
1331 return usb_ep_set_halt(ep);
1339 static void ep_fifo_flush(
struct usb_ep *ep)
1342 unsigned long flags;
1345 dev_err(mEp->
ci->dev,
"%02X: -EINVAL\n", _usb_addr(mEp));
1352 hw_ep_flush(mEp->
ci, mEp->
num, mEp->
dir);
1354 spin_unlock_irqrestore(mEp->
lock, flags);
1363 .disable = ep_disable,
1364 .alloc_request = ep_alloc_request,
1365 .free_request = ep_free_request,
1367 .dequeue = ep_dequeue,
1368 .set_halt = ep_set_halt,
1369 .set_wedge = ep_set_wedge,
1370 .fifo_flush = ep_fifo_flush,
1379 unsigned long flags;
1380 int gadget_ready = 0;
1389 spin_unlock_irqrestore(&ci->
lock, flags);
1393 pm_runtime_get_sync(&_gadget->
dev);
1395 hw_enable_vbus_intr(ci);
1396 hw_device_state(ci, ci->
ep0out->qh.dma);
1398 hw_device_state(ci, 0);
1402 _gadget_stop_activity(&ci->
gadget);
1403 pm_runtime_put_sync(&_gadget->
dev);
1410 static int ci13xxx_wakeup(
struct usb_gadget *_gadget)
1413 unsigned long flags;
1427 spin_unlock_irqrestore(&ci->
lock, flags);
1431 static int ci13xxx_vbus_draw(
struct usb_gadget *_gadget,
unsigned mA)
1443 static int ci13xxx_pullup(
struct usb_gadget *_gadget,
int is_on)
1455 static int ci13xxx_start(
struct usb_gadget *gadget,
1457 static int ci13xxx_stop(
struct usb_gadget *gadget,
1466 .wakeup = ci13xxx_wakeup,
1467 .pullup = ci13xxx_pullup,
1468 .vbus_draw = ci13xxx_vbus_draw,
1469 .udc_start = ci13xxx_start,
1470 .udc_stop = ci13xxx_stop,
1473 static int init_eps(
struct ci13xxx *ci)
1475 int retval = 0,
i,
j;
1478 for (j =
RX; j <=
TX; j++) {
1483 (j ==
TX) ?
"in" :
"out");
1489 mEp->
ep.name = mEp->
name;
1490 mEp->
ep.ops = &usb_ep_ops;
1496 mEp->
ep.maxpacket = (
unsigned short)~0;
1498 INIT_LIST_HEAD(&mEp->
qh.queue);
1504 memset(mEp->
qh.ptr, 0,
sizeof(*mEp->
qh.ptr));
1526 static void destroy_eps(
struct ci13xxx *ci)
1544 static int ci13xxx_start(
struct usb_gadget *gadget,
1548 unsigned long flags;
1555 ci->
ep0out->ep.desc = &ctrl_endpt_out_desc;
1556 retval = usb_ep_enable(&ci->
ep0out->ep);
1560 ci->
ep0in->ep.desc = &ctrl_endpt_in_desc;
1561 retval = usb_ep_enable(&ci->
ep0in->ep);
1567 pm_runtime_get_sync(&ci->
gadget.dev);
1572 hw_enable_vbus_intr(ci);
1575 pm_runtime_put_sync(&ci->
gadget.dev);
1580 retval = hw_device_state(ci, ci->
ep0out->qh.dma);
1582 pm_runtime_put_sync(&ci->
gadget.dev);
1585 spin_unlock_irqrestore(&ci->
lock, flags);
1592 static int ci13xxx_stop(
struct usb_gadget *gadget,
1596 unsigned long flags;
1602 hw_device_state(ci, 0);
1607 spin_unlock_irqrestore(&ci->
lock, flags);
1608 _gadget_stop_activity(&ci->
gadget);
1610 pm_runtime_put(&ci->
gadget.dev);
1613 spin_unlock_irqrestore(&ci->
lock, flags);
1635 spin_lock(&ci->
lock);
1640 spin_unlock(&ci->
lock);
1644 intr = hw_test_and_clear_intr_active(ci);
1650 isr_reset_handler(ci);
1653 ci->
gadget.speed = hw_port_is_high_speed(ci) ?
1656 spin_unlock(&ci->
lock);
1658 spin_lock(&ci->
lock);
1664 isr_tr_complete_handler(ci);
1670 spin_unlock(&ci->
lock);
1672 spin_lock(&ci->
lock);
1686 spin_unlock(&ci->
lock);
1697 static void udc_release(
struct device *
dev)
1705 static int udc_start(
struct ci13xxx *ci)
1712 ci->
gadget.ops = &usb_gadget_ops;
1718 INIT_LIST_HEAD(&ci->
gadget.ep_list);
1724 ci->
gadget.dev.release = udc_release;
1729 64, CI13XXX_PAGE_SIZE);
1735 64, CI13XXX_PAGE_SIZE);
1741 retval = init_eps(ci);
1760 goto put_transceiver;
1761 hw_enable_vbus_intr(ci);
1767 goto put_transceiver;
1797 dev_err(dev,
"error = %i\n", retval);
1819 static void udc_stop(
struct ci13xxx *ci)
1824 hw_disable_vbus_intr(ci);
1862 rdrv->
start = udc_start;
1863 rdrv->
stop = udc_stop;
1864 rdrv->
irq = udc_irq;
1865 rdrv->
name =
"gadget";