/drivers/usb/host/isp1362-hcd.c
https://gitlab.com/stalker-android/linux-omap3 · C · 2884 lines · 2336 code · 381 blank · 167 comment · 354 complexity · 412aa43275b7a120a83438b23cf5cb38 MD5 · raw file
Large files are truncated click here to view the full file
- /*
- * ISP1362 HCD (Host Controller Driver) for USB.
- *
- * Copyright (C) 2005 Lothar Wassmann <LW@KARO-electronics.de>
- *
- * Derived from the SL811 HCD, rewritten for ISP116x.
- * Copyright (C) 2005 Olav Kongas <ok@artecdesign.ee>
- *
- * Portions:
- * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
- * Copyright (C) 2004 David Brownell
- */
- /*
- * The ISP1362 chip requires a large delay (300ns and 462ns) between
- * accesses to the address and data register.
- * The following timing options exist:
- *
- * 1. Configure your memory controller to add such delays if it can (the best)
- * 2. Implement platform-specific delay function possibly
- * combined with configuring the memory controller; see
- * include/linux/usb_isp1362.h for more info.
- * 3. Use ndelay (easiest, poorest).
- *
- * Use the corresponding macros USE_PLATFORM_DELAY and USE_NDELAY in the
- * platform specific section of isp1362.h to select the appropriate variant.
- *
- * Also note that according to the Philips "ISP1362 Errata" document
- * Rev 1.00 from 27 May data corruption may occur when the #WR signal
- * is reasserted (even with #CS deasserted) within 132ns after a
- * write cycle to any controller register. If the hardware doesn't
- * implement the recommended fix (gating the #WR with #CS) software
- * must ensure that no further write cycle (not necessarily to the chip!)
- * is issued by the CPU within this interval.
- * For PXA25x this can be ensured by using VLIO with the maximum
- * recovery time (MSCx = 0x7f8c) with a memory clock of 99.53 MHz.
- */
- #ifdef CONFIG_USB_DEBUG
- # define ISP1362_DEBUG
- #else
- # undef ISP1362_DEBUG
- #endif
- /*
- * The PXA255 UDC apparently doesn't handle GET_STATUS, GET_CONFIG and
- * GET_INTERFACE requests correctly when the SETUP and DATA stages of the
- * requests are carried out in separate frames. This will delay any SETUP
- * packets until the start of the next frame so that this situation is
- * unlikely to occur (and makes usbtest happy running with a PXA255 target
- * device).
- */
- #undef BUGGY_PXA2XX_UDC_USBTEST
- #undef PTD_TRACE
- #undef URB_TRACE
- #undef VERBOSE
- #undef REGISTERS
- /* This enables a memory test on the ISP1362 chip memory to make sure the
- * chip access timing is correct.
- */
- #undef CHIP_BUFFER_TEST
- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/kernel.h>
- #include <linux/delay.h>
- #include <linux/ioport.h>
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/errno.h>
- #include <linux/init.h>
- #include <linux/list.h>
- #include <linux/interrupt.h>
- #include <linux/usb.h>
- #include <linux/usb/isp1362.h>
- #include <linux/usb/hcd.h>
- #include <linux/platform_device.h>
- #include <linux/pm.h>
- #include <linux/io.h>
- #include <linux/bitmap.h>
- #include <asm/irq.h>
- #include <asm/system.h>
- #include <asm/byteorder.h>
- #include <asm/unaligned.h>
- static int dbg_level;
- #ifdef ISP1362_DEBUG
- module_param(dbg_level, int, 0644);
- #else
- module_param(dbg_level, int, 0);
- #define STUB_DEBUG_FILE
- #endif
- #include "../core/usb.h"
- #include "isp1362.h"
- #define DRIVER_VERSION "2005-04-04"
- #define DRIVER_DESC "ISP1362 USB Host Controller Driver"
- MODULE_DESCRIPTION(DRIVER_DESC);
- MODULE_LICENSE("GPL");
- static const char hcd_name[] = "isp1362-hcd";
- static void isp1362_hc_stop(struct usb_hcd *hcd);
- static int isp1362_hc_start(struct usb_hcd *hcd);
- /*-------------------------------------------------------------------------*/
- /*
- * When called from the interrupthandler only isp1362_hcd->irqenb is modified,
- * since the interrupt handler will write isp1362_hcd->irqenb to HCuPINT upon
- * completion.
- * We don't need a 'disable' counterpart, since interrupts will be disabled
- * only by the interrupt handler.
- */
- static inline void isp1362_enable_int(struct isp1362_hcd *isp1362_hcd, u16 mask)
- {
- if ((isp1362_hcd->irqenb | mask) == isp1362_hcd->irqenb)
- return;
- if (mask & ~isp1362_hcd->irqenb)
- isp1362_write_reg16(isp1362_hcd, HCuPINT, mask & ~isp1362_hcd->irqenb);
- isp1362_hcd->irqenb |= mask;
- if (isp1362_hcd->irq_active)
- return;
- isp1362_write_reg16(isp1362_hcd, HCuPINTENB, isp1362_hcd->irqenb);
- }
- /*-------------------------------------------------------------------------*/
- static inline struct isp1362_ep_queue *get_ptd_queue(struct isp1362_hcd *isp1362_hcd,
- u16 offset)
- {
- struct isp1362_ep_queue *epq = NULL;
- if (offset < isp1362_hcd->istl_queue[1].buf_start)
- epq = &isp1362_hcd->istl_queue[0];
- else if (offset < isp1362_hcd->intl_queue.buf_start)
- epq = &isp1362_hcd->istl_queue[1];
- else if (offset < isp1362_hcd->atl_queue.buf_start)
- epq = &isp1362_hcd->intl_queue;
- else if (offset < isp1362_hcd->atl_queue.buf_start +
- isp1362_hcd->atl_queue.buf_size)
- epq = &isp1362_hcd->atl_queue;
- if (epq)
- DBG(1, "%s: PTD $%04x is on %s queue\n", __func__, offset, epq->name);
- else
- pr_warning("%s: invalid PTD $%04x\n", __func__, offset);
- return epq;
- }
- static inline int get_ptd_offset(struct isp1362_ep_queue *epq, u8 index)
- {
- int offset;
- if (index * epq->blk_size > epq->buf_size) {
- pr_warning("%s: Bad %s index %d(%d)\n", __func__, epq->name, index,
- epq->buf_size / epq->blk_size);
- return -EINVAL;
- }
- offset = epq->buf_start + index * epq->blk_size;
- DBG(3, "%s: %s PTD[%02x] # %04x\n", __func__, epq->name, index, offset);
- return offset;
- }
- /*-------------------------------------------------------------------------*/
- static inline u16 max_transfer_size(struct isp1362_ep_queue *epq, size_t size,
- int mps)
- {
- u16 xfer_size = min_t(size_t, MAX_XFER_SIZE, size);
- xfer_size = min_t(size_t, xfer_size, epq->buf_avail * epq->blk_size - PTD_HEADER_SIZE);
- if (xfer_size < size && xfer_size % mps)
- xfer_size -= xfer_size % mps;
- return xfer_size;
- }
- static int claim_ptd_buffers(struct isp1362_ep_queue *epq,
- struct isp1362_ep *ep, u16 len)
- {
- int ptd_offset = -EINVAL;
- int num_ptds = ((len + PTD_HEADER_SIZE - 1) / epq->blk_size) + 1;
- int found;
- BUG_ON(len > epq->buf_size);
- if (!epq->buf_avail)
- return -ENOMEM;
- if (ep->num_ptds)
- pr_err("%s: %s len %d/%d num_ptds %d buf_map %08lx skip_map %08lx\n", __func__,
- epq->name, len, epq->blk_size, num_ptds, epq->buf_map, epq->skip_map);
- BUG_ON(ep->num_ptds != 0);
- found = bitmap_find_next_zero_area(&epq->buf_map, epq->buf_count, 0,
- num_ptds, 0);
- if (found >= epq->buf_count)
- return -EOVERFLOW;
- DBG(1, "%s: Found %d PTDs[%d] for %d/%d byte\n", __func__,
- num_ptds, found, len, (int)(epq->blk_size - PTD_HEADER_SIZE));
- ptd_offset = get_ptd_offset(epq, found);
- WARN_ON(ptd_offset < 0);
- ep->ptd_offset = ptd_offset;
- ep->num_ptds += num_ptds;
- epq->buf_avail -= num_ptds;
- BUG_ON(epq->buf_avail > epq->buf_count);
- ep->ptd_index = found;
- bitmap_set(&epq->buf_map, found, num_ptds);
- DBG(1, "%s: Done %s PTD[%d] $%04x, avail %d count %d claimed %d %08lx:%08lx\n",
- __func__, epq->name, ep->ptd_index, ep->ptd_offset,
- epq->buf_avail, epq->buf_count, num_ptds, epq->buf_map, epq->skip_map);
- return found;
- }
- static inline void release_ptd_buffers(struct isp1362_ep_queue *epq, struct isp1362_ep *ep)
- {
- int index = ep->ptd_index;
- int last = ep->ptd_index + ep->num_ptds;
- if (last > epq->buf_count)
- pr_err("%s: ep %p req %d len %d %s PTD[%d] $%04x num_ptds %d buf_count %d buf_avail %d buf_map %08lx skip_map %08lx\n",
- __func__, ep, ep->num_req, ep->length, epq->name, ep->ptd_index,
- ep->ptd_offset, ep->num_ptds, epq->buf_count, epq->buf_avail,
- epq->buf_map, epq->skip_map);
- BUG_ON(last > epq->buf_count);
- for (; index < last; index++) {
- __clear_bit(index, &epq->buf_map);
- __set_bit(index, &epq->skip_map);
- }
- epq->buf_avail += ep->num_ptds;
- epq->ptd_count--;
- BUG_ON(epq->buf_avail > epq->buf_count);
- BUG_ON(epq->ptd_count > epq->buf_count);
- DBG(1, "%s: Done %s PTDs $%04x released %d avail %d count %d\n",
- __func__, epq->name,
- ep->ptd_offset, ep->num_ptds, epq->buf_avail, epq->buf_count);
- DBG(1, "%s: buf_map %08lx skip_map %08lx\n", __func__,
- epq->buf_map, epq->skip_map);
- ep->num_ptds = 0;
- ep->ptd_offset = -EINVAL;
- ep->ptd_index = -EINVAL;
- }
- /*-------------------------------------------------------------------------*/
- /*
- Set up PTD's.
- */
- static void prepare_ptd(struct isp1362_hcd *isp1362_hcd, struct urb *urb,
- struct isp1362_ep *ep, struct isp1362_ep_queue *epq,
- u16 fno)
- {
- struct ptd *ptd;
- int toggle;
- int dir;
- u16 len;
- size_t buf_len = urb->transfer_buffer_length - urb->actual_length;
- DBG(3, "%s: %s ep %p\n", __func__, epq->name, ep);
- ptd = &ep->ptd;
- ep->data = (unsigned char *)urb->transfer_buffer + urb->actual_length;
- switch (ep->nextpid) {
- case USB_PID_IN:
- toggle = usb_gettoggle(urb->dev, ep->epnum, 0);
- dir = PTD_DIR_IN;
- if (usb_pipecontrol(urb->pipe)) {
- len = min_t(size_t, ep->maxpacket, buf_len);
- } else if (usb_pipeisoc(urb->pipe)) {
- len = min_t(size_t, urb->iso_frame_desc[fno].length, MAX_XFER_SIZE);
- ep->data = urb->transfer_buffer + urb->iso_frame_desc[fno].offset;
- } else
- len = max_transfer_size(epq, buf_len, ep->maxpacket);
- DBG(1, "%s: IN len %d/%d/%d from URB\n", __func__, len, ep->maxpacket,
- (int)buf_len);
- break;
- case USB_PID_OUT:
- toggle = usb_gettoggle(urb->dev, ep->epnum, 1);
- dir = PTD_DIR_OUT;
- if (usb_pipecontrol(urb->pipe))
- len = min_t(size_t, ep->maxpacket, buf_len);
- else if (usb_pipeisoc(urb->pipe))
- len = min_t(size_t, urb->iso_frame_desc[0].length, MAX_XFER_SIZE);
- else
- len = max_transfer_size(epq, buf_len, ep->maxpacket);
- if (len == 0)
- pr_info("%s: Sending ZERO packet: %d\n", __func__,
- urb->transfer_flags & URB_ZERO_PACKET);
- DBG(1, "%s: OUT len %d/%d/%d from URB\n", __func__, len, ep->maxpacket,
- (int)buf_len);
- break;
- case USB_PID_SETUP:
- toggle = 0;
- dir = PTD_DIR_SETUP;
- len = sizeof(struct usb_ctrlrequest);
- DBG(1, "%s: SETUP len %d\n", __func__, len);
- ep->data = urb->setup_packet;
- break;
- case USB_PID_ACK:
- toggle = 1;
- len = 0;
- dir = (urb->transfer_buffer_length && usb_pipein(urb->pipe)) ?
- PTD_DIR_OUT : PTD_DIR_IN;
- DBG(1, "%s: ACK len %d\n", __func__, len);
- break;
- default:
- toggle = dir = len = 0;
- pr_err("%s@%d: ep->nextpid %02x\n", __func__, __LINE__, ep->nextpid);
- BUG_ON(1);
- }
- ep->length = len;
- if (!len)
- ep->data = NULL;
- ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK | PTD_TOGGLE(toggle);
- ptd->mps = PTD_MPS(ep->maxpacket) | PTD_SPD(urb->dev->speed == USB_SPEED_LOW) |
- PTD_EP(ep->epnum);
- ptd->len = PTD_LEN(len) | PTD_DIR(dir);
- ptd->faddr = PTD_FA(usb_pipedevice(urb->pipe));
- if (usb_pipeint(urb->pipe)) {
- ptd->faddr |= PTD_SF_INT(ep->branch);
- ptd->faddr |= PTD_PR(ep->interval ? __ffs(ep->interval) : 0);
- }
- if (usb_pipeisoc(urb->pipe))
- ptd->faddr |= PTD_SF_ISO(fno);
- DBG(1, "%s: Finished\n", __func__);
- }
- static void isp1362_write_ptd(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep,
- struct isp1362_ep_queue *epq)
- {
- struct ptd *ptd = &ep->ptd;
- int len = PTD_GET_DIR(ptd) == PTD_DIR_IN ? 0 : ep->length;
- _BUG_ON(ep->ptd_offset < 0);
- prefetch(ptd);
- isp1362_write_buffer(isp1362_hcd, ptd, ep->ptd_offset, PTD_HEADER_SIZE);
- if (len)
- isp1362_write_buffer(isp1362_hcd, ep->data,
- ep->ptd_offset + PTD_HEADER_SIZE, len);
- dump_ptd(ptd);
- dump_ptd_out_data(ptd, ep->data);
- }
- static void isp1362_read_ptd(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep,
- struct isp1362_ep_queue *epq)
- {
- struct ptd *ptd = &ep->ptd;
- int act_len;
- WARN_ON(list_empty(&ep->active));
- BUG_ON(ep->ptd_offset < 0);
- list_del_init(&ep->active);
- DBG(1, "%s: ep %p removed from active list %p\n", __func__, ep, &epq->active);
- prefetchw(ptd);
- isp1362_read_buffer(isp1362_hcd, ptd, ep->ptd_offset, PTD_HEADER_SIZE);
- dump_ptd(ptd);
- act_len = PTD_GET_COUNT(ptd);
- if (PTD_GET_DIR(ptd) != PTD_DIR_IN || act_len == 0)
- return;
- if (act_len > ep->length)
- pr_err("%s: ep %p PTD $%04x act_len %d ep->length %d\n", __func__, ep,
- ep->ptd_offset, act_len, ep->length);
- BUG_ON(act_len > ep->length);
- /* Only transfer the amount of data that has actually been overwritten
- * in the chip buffer. We don't want any data that doesn't belong to the
- * transfer to leak out of the chip to the callers transfer buffer!
- */
- prefetchw(ep->data);
- isp1362_read_buffer(isp1362_hcd, ep->data,
- ep->ptd_offset + PTD_HEADER_SIZE, act_len);
- dump_ptd_in_data(ptd, ep->data);
- }
- /*
- * INT PTDs will stay in the chip until data is available.
- * This function will remove a PTD from the chip when the URB is dequeued.
- * Must be called with the spinlock held and IRQs disabled
- */
- static void remove_ptd(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep)
- {
- int index;
- struct isp1362_ep_queue *epq;
- DBG(1, "%s: ep %p PTD[%d] $%04x\n", __func__, ep, ep->ptd_index, ep->ptd_offset);
- BUG_ON(ep->ptd_offset < 0);
- epq = get_ptd_queue(isp1362_hcd, ep->ptd_offset);
- BUG_ON(!epq);
- /* put ep in remove_list for cleanup */
- WARN_ON(!list_empty(&ep->remove_list));
- list_add_tail(&ep->remove_list, &isp1362_hcd->remove_list);
- /* let SOF interrupt handle the cleanup */
- isp1362_enable_int(isp1362_hcd, HCuPINT_SOF);
- index = ep->ptd_index;
- if (index < 0)
- /* ISO queues don't have SKIP registers */
- return;
- DBG(1, "%s: Disabling PTD[%02x] $%04x %08lx|%08x\n", __func__,
- index, ep->ptd_offset, epq->skip_map, 1 << index);
- /* prevent further processing of PTD (will be effective after next SOF) */
- epq->skip_map |= 1 << index;
- if (epq == &isp1362_hcd->atl_queue) {
- DBG(2, "%s: ATLSKIP = %08x -> %08lx\n", __func__,
- isp1362_read_reg32(isp1362_hcd, HCATLSKIP), epq->skip_map);
- isp1362_write_reg32(isp1362_hcd, HCATLSKIP, epq->skip_map);
- if (~epq->skip_map == 0)
- isp1362_clr_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_ATL_ACTIVE);
- } else if (epq == &isp1362_hcd->intl_queue) {
- DBG(2, "%s: INTLSKIP = %08x -> %08lx\n", __func__,
- isp1362_read_reg32(isp1362_hcd, HCINTLSKIP), epq->skip_map);
- isp1362_write_reg32(isp1362_hcd, HCINTLSKIP, epq->skip_map);
- if (~epq->skip_map == 0)
- isp1362_clr_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_INTL_ACTIVE);
- }
- }
- /*
- Take done or failed requests out of schedule. Give back
- processed urbs.
- */
- static void finish_request(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep,
- struct urb *urb, int status)
- __releases(isp1362_hcd->lock)
- __acquires(isp1362_hcd->lock)
- {
- urb->hcpriv = NULL;
- ep->error_count = 0;
- if (usb_pipecontrol(urb->pipe))
- ep->nextpid = USB_PID_SETUP;
- URB_DBG("%s: req %d FA %d ep%d%s %s: len %d/%d %s stat %d\n", __func__,
- ep->num_req, usb_pipedevice(urb->pipe),
- usb_pipeendpoint(urb->pipe),
- !usb_pipein(urb->pipe) ? "out" : "in",
- usb_pipecontrol(urb->pipe) ? "ctrl" :
- usb_pipeint(urb->pipe) ? "int" :
- usb_pipebulk(urb->pipe) ? "bulk" :
- "iso",
- urb->actual_length, urb->transfer_buffer_length,
- !(urb->transfer_flags & URB_SHORT_NOT_OK) ?
- "short_ok" : "", urb->status);
- usb_hcd_unlink_urb_from_ep(isp1362_hcd_to_hcd(isp1362_hcd), urb);
- spin_unlock(&isp1362_hcd->lock);
- usb_hcd_giveback_urb(isp1362_hcd_to_hcd(isp1362_hcd), urb, status);
- spin_lock(&isp1362_hcd->lock);
- /* take idle endpoints out of the schedule right away */
- if (!list_empty(&ep->hep->urb_list))
- return;
- /* async deschedule */
- if (!list_empty(&ep->schedule)) {
- list_del_init(&ep->schedule);
- return;
- }
- if (ep->interval) {
- /* periodic deschedule */
- DBG(1, "deschedule qh%d/%p branch %d load %d bandwidth %d -> %d\n", ep->interval,
- ep, ep->branch, ep->load,
- isp1362_hcd->load[ep->branch],
- isp1362_hcd->load[ep->branch] - ep->load);
- isp1362_hcd->load[ep->branch] -= ep->load;
- ep->branch = PERIODIC_SIZE;
- }
- }
- /*
- * Analyze transfer results, handle partial transfers and errors
- */
- static void postproc_ep(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep)
- {
- struct urb *urb = get_urb(ep);
- struct usb_device *udev;
- struct ptd *ptd;
- int short_ok;
- u16 len;
- int urbstat = -EINPROGRESS;
- u8 cc;
- DBG(2, "%s: ep %p req %d\n", __func__, ep, ep->num_req);
- udev = urb->dev;
- ptd = &ep->ptd;
- cc = PTD_GET_CC(ptd);
- if (cc == PTD_NOTACCESSED) {
- pr_err("%s: req %d PTD %p Untouched by ISP1362\n", __func__,
- ep->num_req, ptd);
- cc = PTD_DEVNOTRESP;
- }
- short_ok = !(urb->transfer_flags & URB_SHORT_NOT_OK);
- len = urb->transfer_buffer_length - urb->actual_length;
- /* Data underrun is special. For allowed underrun
- we clear the error and continue as normal. For
- forbidden underrun we finish the DATA stage
- immediately while for control transfer,
- we do a STATUS stage.
- */
- if (cc == PTD_DATAUNDERRUN) {
- if (short_ok) {
- DBG(1, "%s: req %d Allowed data underrun short_%sok %d/%d/%d byte\n",
- __func__, ep->num_req, short_ok ? "" : "not_",
- PTD_GET_COUNT(ptd), ep->maxpacket, len);
- cc = PTD_CC_NOERROR;
- urbstat = 0;
- } else {
- DBG(1, "%s: req %d Data Underrun %s nextpid %02x short_%sok %d/%d/%d byte\n",
- __func__, ep->num_req,
- usb_pipein(urb->pipe) ? "IN" : "OUT", ep->nextpid,
- short_ok ? "" : "not_",
- PTD_GET_COUNT(ptd), ep->maxpacket, len);
- if (usb_pipecontrol(urb->pipe)) {
- ep->nextpid = USB_PID_ACK;
- /* save the data underrun error code for later and
- * procede with the status stage
- */
- urb->actual_length += PTD_GET_COUNT(ptd);
- BUG_ON(urb->actual_length > urb->transfer_buffer_length);
- if (urb->status == -EINPROGRESS)
- urb->status = cc_to_error[PTD_DATAUNDERRUN];
- } else {
- usb_settoggle(udev, ep->epnum, ep->nextpid == USB_PID_OUT,
- PTD_GET_TOGGLE(ptd));
- urbstat = cc_to_error[PTD_DATAUNDERRUN];
- }
- goto out;
- }
- }
- if (cc != PTD_CC_NOERROR) {
- if (++ep->error_count >= 3 || cc == PTD_CC_STALL || cc == PTD_DATAOVERRUN) {
- urbstat = cc_to_error[cc];
- DBG(1, "%s: req %d nextpid %02x, status %d, error %d, error_count %d\n",
- __func__, ep->num_req, ep->nextpid, urbstat, cc,
- ep->error_count);
- }
- goto out;
- }
- switch (ep->nextpid) {
- case USB_PID_OUT:
- if (PTD_GET_COUNT(ptd) != ep->length)
- pr_err("%s: count=%d len=%d\n", __func__,
- PTD_GET_COUNT(ptd), ep->length);
- BUG_ON(PTD_GET_COUNT(ptd) != ep->length);
- urb->actual_length += ep->length;
- BUG_ON(urb->actual_length > urb->transfer_buffer_length);
- usb_settoggle(udev, ep->epnum, 1, PTD_GET_TOGGLE(ptd));
- if (urb->actual_length == urb->transfer_buffer_length) {
- DBG(3, "%s: req %d xfer complete %d/%d status %d -> 0\n", __func__,
- ep->num_req, len, ep->maxpacket, urbstat);
- if (usb_pipecontrol(urb->pipe)) {
- DBG(3, "%s: req %d %s Wait for ACK\n", __func__,
- ep->num_req,
- usb_pipein(urb->pipe) ? "IN" : "OUT");
- ep->nextpid = USB_PID_ACK;
- } else {
- if (len % ep->maxpacket ||
- !(urb->transfer_flags & URB_ZERO_PACKET)) {
- urbstat = 0;
- DBG(3, "%s: req %d URB %s status %d count %d/%d/%d\n",
- __func__, ep->num_req, usb_pipein(urb->pipe) ? "IN" : "OUT",
- urbstat, len, ep->maxpacket, urb->actual_length);
- }
- }
- }
- break;
- case USB_PID_IN:
- len = PTD_GET_COUNT(ptd);
- BUG_ON(len > ep->length);
- urb->actual_length += len;
- BUG_ON(urb->actual_length > urb->transfer_buffer_length);
- usb_settoggle(udev, ep->epnum, 0, PTD_GET_TOGGLE(ptd));
- /* if transfer completed or (allowed) data underrun */
- if ((urb->transfer_buffer_length == urb->actual_length) ||
- len % ep->maxpacket) {
- DBG(3, "%s: req %d xfer complete %d/%d status %d -> 0\n", __func__,
- ep->num_req, len, ep->maxpacket, urbstat);
- if (usb_pipecontrol(urb->pipe)) {
- DBG(3, "%s: req %d %s Wait for ACK\n", __func__,
- ep->num_req,
- usb_pipein(urb->pipe) ? "IN" : "OUT");
- ep->nextpid = USB_PID_ACK;
- } else {
- urbstat = 0;
- DBG(3, "%s: req %d URB %s status %d count %d/%d/%d\n",
- __func__, ep->num_req, usb_pipein(urb->pipe) ? "IN" : "OUT",
- urbstat, len, ep->maxpacket, urb->actual_length);
- }
- }
- break;
- case USB_PID_SETUP:
- if (urb->transfer_buffer_length == urb->actual_length) {
- ep->nextpid = USB_PID_ACK;
- } else if (usb_pipeout(urb->pipe)) {
- usb_settoggle(udev, 0, 1, 1);
- ep->nextpid = USB_PID_OUT;
- } else {
- usb_settoggle(udev, 0, 0, 1);
- ep->nextpid = USB_PID_IN;
- }
- break;
- case USB_PID_ACK:
- DBG(3, "%s: req %d got ACK %d -> 0\n", __func__, ep->num_req,
- urbstat);
- WARN_ON(urbstat != -EINPROGRESS);
- urbstat = 0;
- ep->nextpid = 0;
- break;
- default:
- BUG_ON(1);
- }
- out:
- if (urbstat != -EINPROGRESS) {
- DBG(2, "%s: Finishing ep %p req %d urb %p status %d\n", __func__,
- ep, ep->num_req, urb, urbstat);
- finish_request(isp1362_hcd, ep, urb, urbstat);
- }
- }
- static void finish_unlinks(struct isp1362_hcd *isp1362_hcd)
- {
- struct isp1362_ep *ep;
- struct isp1362_ep *tmp;
- list_for_each_entry_safe(ep, tmp, &isp1362_hcd->remove_list, remove_list) {
- struct isp1362_ep_queue *epq =
- get_ptd_queue(isp1362_hcd, ep->ptd_offset);
- int index = ep->ptd_index;
- BUG_ON(epq == NULL);
- if (index >= 0) {
- DBG(1, "%s: remove PTD[%d] $%04x\n", __func__, index, ep->ptd_offset);
- BUG_ON(ep->num_ptds == 0);
- release_ptd_buffers(epq, ep);
- }
- if (!list_empty(&ep->hep->urb_list)) {
- struct urb *urb = get_urb(ep);
- DBG(1, "%s: Finishing req %d ep %p from remove_list\n", __func__,
- ep->num_req, ep);
- finish_request(isp1362_hcd, ep, urb, -ESHUTDOWN);
- }
- WARN_ON(list_empty(&ep->active));
- if (!list_empty(&ep->active)) {
- list_del_init(&ep->active);
- DBG(1, "%s: ep %p removed from active list\n", __func__, ep);
- }
- list_del_init(&ep->remove_list);
- DBG(1, "%s: ep %p removed from remove_list\n", __func__, ep);
- }
- DBG(1, "%s: Done\n", __func__);
- }
- static inline void enable_atl_transfers(struct isp1362_hcd *isp1362_hcd, int count)
- {
- if (count > 0) {
- if (count < isp1362_hcd->atl_queue.ptd_count)
- isp1362_write_reg16(isp1362_hcd, HCATLDTC, count);
- isp1362_enable_int(isp1362_hcd, HCuPINT_ATL);
- isp1362_write_reg32(isp1362_hcd, HCATLSKIP, isp1362_hcd->atl_queue.skip_map);
- isp1362_set_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_ATL_ACTIVE);
- } else
- isp1362_enable_int(isp1362_hcd, HCuPINT_SOF);
- }
- static inline void enable_intl_transfers(struct isp1362_hcd *isp1362_hcd)
- {
- isp1362_enable_int(isp1362_hcd, HCuPINT_INTL);
- isp1362_set_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_INTL_ACTIVE);
- isp1362_write_reg32(isp1362_hcd, HCINTLSKIP, isp1362_hcd->intl_queue.skip_map);
- }
- static inline void enable_istl_transfers(struct isp1362_hcd *isp1362_hcd, int flip)
- {
- isp1362_enable_int(isp1362_hcd, flip ? HCuPINT_ISTL1 : HCuPINT_ISTL0);
- isp1362_set_mask16(isp1362_hcd, HCBUFSTAT, flip ?
- HCBUFSTAT_ISTL1_FULL : HCBUFSTAT_ISTL0_FULL);
- }
- static int submit_req(struct isp1362_hcd *isp1362_hcd, struct urb *urb,
- struct isp1362_ep *ep, struct isp1362_ep_queue *epq)
- {
- int index = epq->free_ptd;
- prepare_ptd(isp1362_hcd, urb, ep, epq, 0);
- index = claim_ptd_buffers(epq, ep, ep->length);
- if (index == -ENOMEM) {
- DBG(1, "%s: req %d No free %s PTD available: %d, %08lx:%08lx\n", __func__,
- ep->num_req, epq->name, ep->num_ptds, epq->buf_map, epq->skip_map);
- return index;
- } else if (index == -EOVERFLOW) {
- DBG(1, "%s: req %d Not enough space for %d byte %s PTD %d %08lx:%08lx\n",
- __func__, ep->num_req, ep->length, epq->name, ep->num_ptds,
- epq->buf_map, epq->skip_map);
- return index;
- } else
- BUG_ON(index < 0);
- list_add_tail(&ep->active, &epq->active);
- DBG(1, "%s: ep %p req %d len %d added to active list %p\n", __func__,
- ep, ep->num_req, ep->length, &epq->active);
- DBG(1, "%s: Submitting %s PTD $%04x for ep %p req %d\n", __func__, epq->name,
- ep->ptd_offset, ep, ep->num_req);
- isp1362_write_ptd(isp1362_hcd, ep, epq);
- __clear_bit(ep->ptd_index, &epq->skip_map);
- return 0;
- }
- static void start_atl_transfers(struct isp1362_hcd *isp1362_hcd)
- {
- int ptd_count = 0;
- struct isp1362_ep_queue *epq = &isp1362_hcd->atl_queue;
- struct isp1362_ep *ep;
- int defer = 0;
- if (atomic_read(&epq->finishing)) {
- DBG(1, "%s: finish_transfers is active for %s\n", __func__, epq->name);
- return;
- }
- list_for_each_entry(ep, &isp1362_hcd->async, schedule) {
- struct urb *urb = get_urb(ep);
- int ret;
- if (!list_empty(&ep->active)) {
- DBG(2, "%s: Skipping active %s ep %p\n", __func__, epq->name, ep);
- continue;
- }
- DBG(1, "%s: Processing %s ep %p req %d\n", __func__, epq->name,
- ep, ep->num_req);
- ret = submit_req(isp1362_hcd, urb, ep, epq);
- if (ret == -ENOMEM) {
- defer = 1;
- break;
- } else if (ret == -EOVERFLOW) {
- defer = 1;
- continue;
- }
- #ifdef BUGGY_PXA2XX_UDC_USBTEST
- defer = ep->nextpid == USB_PID_SETUP;
- #endif
- ptd_count++;
- }
- /* Avoid starving of endpoints */
- if (isp1362_hcd->async.next != isp1362_hcd->async.prev) {
- DBG(2, "%s: Cycling ASYNC schedule %d\n", __func__, ptd_count);
- list_move(&isp1362_hcd->async, isp1362_hcd->async.next);
- }
- if (ptd_count || defer)
- enable_atl_transfers(isp1362_hcd, defer ? 0 : ptd_count);
- epq->ptd_count += ptd_count;
- if (epq->ptd_count > epq->stat_maxptds) {
- epq->stat_maxptds = epq->ptd_count;
- DBG(0, "%s: max_ptds: %d\n", __func__, epq->stat_maxptds);
- }
- }
- static void start_intl_transfers(struct isp1362_hcd *isp1362_hcd)
- {
- int ptd_count = 0;
- struct isp1362_ep_queue *epq = &isp1362_hcd->intl_queue;
- struct isp1362_ep *ep;
- if (atomic_read(&epq->finishing)) {
- DBG(1, "%s: finish_transfers is active for %s\n", __func__, epq->name);
- return;
- }
- list_for_each_entry(ep, &isp1362_hcd->periodic, schedule) {
- struct urb *urb = get_urb(ep);
- int ret;
- if (!list_empty(&ep->active)) {
- DBG(1, "%s: Skipping active %s ep %p\n", __func__,
- epq->name, ep);
- continue;
- }
- DBG(1, "%s: Processing %s ep %p req %d\n", __func__,
- epq->name, ep, ep->num_req);
- ret = submit_req(isp1362_hcd, urb, ep, epq);
- if (ret == -ENOMEM)
- break;
- else if (ret == -EOVERFLOW)
- continue;
- ptd_count++;
- }
- if (ptd_count) {
- static int last_count;
- if (ptd_count != last_count) {
- DBG(0, "%s: ptd_count: %d\n", __func__, ptd_count);
- last_count = ptd_count;
- }
- enable_intl_transfers(isp1362_hcd);
- }
- epq->ptd_count += ptd_count;
- if (epq->ptd_count > epq->stat_maxptds)
- epq->stat_maxptds = epq->ptd_count;
- }
- static inline int next_ptd(struct isp1362_ep_queue *epq, struct isp1362_ep *ep)
- {
- u16 ptd_offset = ep->ptd_offset;
- int num_ptds = (ep->length + PTD_HEADER_SIZE + (epq->blk_size - 1)) / epq->blk_size;
- DBG(2, "%s: PTD offset $%04x + %04x => %d * %04x -> $%04x\n", __func__, ptd_offset,
- ep->length, num_ptds, epq->blk_size, ptd_offset + num_ptds * epq->blk_size);
- ptd_offset += num_ptds * epq->blk_size;
- if (ptd_offset < epq->buf_start + epq->buf_size)
- return ptd_offset;
- else
- return -ENOMEM;
- }
- static void start_iso_transfers(struct isp1362_hcd *isp1362_hcd)
- {
- int ptd_count = 0;
- int flip = isp1362_hcd->istl_flip;
- struct isp1362_ep_queue *epq;
- int ptd_offset;
- struct isp1362_ep *ep;
- struct isp1362_ep *tmp;
- u16 fno = isp1362_read_reg32(isp1362_hcd, HCFMNUM);
- fill2:
- epq = &isp1362_hcd->istl_queue[flip];
- if (atomic_read(&epq->finishing)) {
- DBG(1, "%s: finish_transfers is active for %s\n", __func__, epq->name);
- return;
- }
- if (!list_empty(&epq->active))
- return;
- ptd_offset = epq->buf_start;
- list_for_each_entry_safe(ep, tmp, &isp1362_hcd->isoc, schedule) {
- struct urb *urb = get_urb(ep);
- s16 diff = fno - (u16)urb->start_frame;
- DBG(1, "%s: Processing %s ep %p\n", __func__, epq->name, ep);
- if (diff > urb->number_of_packets) {
- /* time frame for this URB has elapsed */
- finish_request(isp1362_hcd, ep, urb, -EOVERFLOW);
- continue;
- } else if (diff < -1) {
- /* URB is not due in this frame or the next one.
- * Comparing with '-1' instead of '0' accounts for double
- * buffering in the ISP1362 which enables us to queue the PTD
- * one frame ahead of time
- */
- } else if (diff == -1) {
- /* submit PTD's that are due in the next frame */
- prepare_ptd(isp1362_hcd, urb, ep, epq, fno);
- if (ptd_offset + PTD_HEADER_SIZE + ep->length >
- epq->buf_start + epq->buf_size) {
- pr_err("%s: Not enough ISO buffer space for %d byte PTD\n",
- __func__, ep->length);
- continue;
- }
- ep->ptd_offset = ptd_offset;
- list_add_tail(&ep->active, &epq->active);
- ptd_offset = next_ptd(epq, ep);
- if (ptd_offset < 0) {
- pr_warning("%s: req %d No more %s PTD buffers available\n", __func__,
- ep->num_req, epq->name);
- break;
- }
- }
- }
- list_for_each_entry(ep, &epq->active, active) {
- if (epq->active.next == &ep->active)
- ep->ptd.mps |= PTD_LAST_MSK;
- isp1362_write_ptd(isp1362_hcd, ep, epq);
- ptd_count++;
- }
- if (ptd_count)
- enable_istl_transfers(isp1362_hcd, flip);
- epq->ptd_count += ptd_count;
- if (epq->ptd_count > epq->stat_maxptds)
- epq->stat_maxptds = epq->ptd_count;
- /* check, whether the second ISTL buffer may also be filled */
- if (!(isp1362_read_reg16(isp1362_hcd, HCBUFSTAT) &
- (flip ? HCBUFSTAT_ISTL0_FULL : HCBUFSTAT_ISTL1_FULL))) {
- fno++;
- ptd_count = 0;
- flip = 1 - flip;
- goto fill2;
- }
- }
- static void finish_transfers(struct isp1362_hcd *isp1362_hcd, unsigned long done_map,
- struct isp1362_ep_queue *epq)
- {
- struct isp1362_ep *ep;
- struct isp1362_ep *tmp;
- if (list_empty(&epq->active)) {
- DBG(1, "%s: Nothing to do for %s queue\n", __func__, epq->name);
- return;
- }
- DBG(1, "%s: Finishing %s transfers %08lx\n", __func__, epq->name, done_map);
- atomic_inc(&epq->finishing);
- list_for_each_entry_safe(ep, tmp, &epq->active, active) {
- int index = ep->ptd_index;
- DBG(1, "%s: Checking %s PTD[%02x] $%04x\n", __func__, epq->name,
- index, ep->ptd_offset);
- BUG_ON(index < 0);
- if (__test_and_clear_bit(index, &done_map)) {
- isp1362_read_ptd(isp1362_hcd, ep, epq);
- epq->free_ptd = index;
- BUG_ON(ep->num_ptds == 0);
- release_ptd_buffers(epq, ep);
- DBG(1, "%s: ep %p req %d removed from active list\n", __func__,
- ep, ep->num_req);
- if (!list_empty(&ep->remove_list)) {
- list_del_init(&ep->remove_list);
- DBG(1, "%s: ep %p removed from remove list\n", __func__, ep);
- }
- DBG(1, "%s: Postprocessing %s ep %p req %d\n", __func__, epq->name,
- ep, ep->num_req);
- postproc_ep(isp1362_hcd, ep);
- }
- if (!done_map)
- break;
- }
- if (done_map)
- pr_warning("%s: done_map not clear: %08lx:%08lx\n", __func__, done_map,
- epq->skip_map);
- atomic_dec(&epq->finishing);
- }
- static void finish_iso_transfers(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep_queue *epq)
- {
- struct isp1362_ep *ep;
- struct isp1362_ep *tmp;
- if (list_empty(&epq->active)) {
- DBG(1, "%s: Nothing to do for %s queue\n", __func__, epq->name);
- return;
- }
- DBG(1, "%s: Finishing %s transfers\n", __func__, epq->name);
- atomic_inc(&epq->finishing);
- list_for_each_entry_safe(ep, tmp, &epq->active, active) {
- DBG(1, "%s: Checking PTD $%04x\n", __func__, ep->ptd_offset);
- isp1362_read_ptd(isp1362_hcd, ep, epq);
- DBG(1, "%s: Postprocessing %s ep %p\n", __func__, epq->name, ep);
- postproc_ep(isp1362_hcd, ep);
- }
- WARN_ON(epq->blk_size != 0);
- atomic_dec(&epq->finishing);
- }
- static irqreturn_t isp1362_irq(struct usb_hcd *hcd)
- {
- int handled = 0;
- struct isp1362_hcd *isp1362_hcd = hcd_to_isp1362_hcd(hcd);
- u16 irqstat;
- u16 svc_mask;
- spin_lock(&isp1362_hcd->lock);
- BUG_ON(isp1362_hcd->irq_active++);
- isp1362_write_reg16(isp1362_hcd, HCuPINTENB, 0);
- irqstat = isp1362_read_reg16(isp1362_hcd, HCuPINT);
- DBG(3, "%s: got IRQ %04x:%04x\n", __func__, irqstat, isp1362_hcd->irqenb);
- /* only handle interrupts that are currently enabled */
- irqstat &= isp1362_hcd->irqenb;
- isp1362_write_reg16(isp1362_hcd, HCuPINT, irqstat);
- svc_mask = irqstat;
- if (irqstat & HCuPINT_SOF) {
- isp1362_hcd->irqenb &= ~HCuPINT_SOF;
- isp1362_hcd->irq_stat[ISP1362_INT_SOF]++;
- handled = 1;
- svc_mask &= ~HCuPINT_SOF;
- DBG(3, "%s: SOF\n", __func__);
- isp1362_hcd->fmindex = isp1362_read_reg32(isp1362_hcd, HCFMNUM);
- if (!list_empty(&isp1362_hcd->remove_list))
- finish_unlinks(isp1362_hcd);
- if (!list_empty(&isp1362_hcd->async) && !(irqstat & HCuPINT_ATL)) {
- if (list_empty(&isp1362_hcd->atl_queue.active)) {
- start_atl_transfers(isp1362_hcd);
- } else {
- isp1362_enable_int(isp1362_hcd, HCuPINT_ATL);
- isp1362_write_reg32(isp1362_hcd, HCATLSKIP,
- isp1362_hcd->atl_queue.skip_map);
- isp1362_set_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_ATL_ACTIVE);
- }
- }
- }
- if (irqstat & HCuPINT_ISTL0) {
- isp1362_hcd->irq_stat[ISP1362_INT_ISTL0]++;
- handled = 1;
- svc_mask &= ~HCuPINT_ISTL0;
- isp1362_clr_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_ISTL0_FULL);
- DBG(1, "%s: ISTL0\n", __func__);
- WARN_ON((int)!!isp1362_hcd->istl_flip);
- WARN_ON(isp1362_read_reg16(isp1362_hcd, HCBUFSTAT) &
- HCBUFSTAT_ISTL0_ACTIVE);
- WARN_ON(!(isp1362_read_reg16(isp1362_hcd, HCBUFSTAT) &
- HCBUFSTAT_ISTL0_DONE));
- isp1362_hcd->irqenb &= ~HCuPINT_ISTL0;
- }
- if (irqstat & HCuPINT_ISTL1) {
- isp1362_hcd->irq_stat[ISP1362_INT_ISTL1]++;
- handled = 1;
- svc_mask &= ~HCuPINT_ISTL1;
- isp1362_clr_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_ISTL1_FULL);
- DBG(1, "%s: ISTL1\n", __func__);
- WARN_ON(!(int)isp1362_hcd->istl_flip);
- WARN_ON(isp1362_read_reg16(isp1362_hcd, HCBUFSTAT) &
- HCBUFSTAT_ISTL1_ACTIVE);
- WARN_ON(!(isp1362_read_reg16(isp1362_hcd, HCBUFSTAT) &
- HCBUFSTAT_ISTL1_DONE));
- isp1362_hcd->irqenb &= ~HCuPINT_ISTL1;
- }
- if (irqstat & (HCuPINT_ISTL0 | HCuPINT_ISTL1)) {
- WARN_ON((irqstat & (HCuPINT_ISTL0 | HCuPINT_ISTL1)) ==
- (HCuPINT_ISTL0 | HCuPINT_ISTL1));
- finish_iso_transfers(isp1362_hcd,
- &isp1362_hcd->istl_queue[isp1362_hcd->istl_flip]);
- start_iso_transfers(isp1362_hcd);
- isp1362_hcd->istl_flip = 1 - isp1362_hcd->istl_flip;
- }
- if (irqstat & HCuPINT_INTL) {
- u32 done_map = isp1362_read_reg32(isp1362_hcd, HCINTLDONE);
- u32 skip_map = isp1362_read_reg32(isp1362_hcd, HCINTLSKIP);
- isp1362_hcd->irq_stat[ISP1362_INT_INTL]++;
- DBG(2, "%s: INTL\n", __func__);
- svc_mask &= ~HCuPINT_INTL;
- isp1362_write_reg32(isp1362_hcd, HCINTLSKIP, skip_map | done_map);
- if (~(done_map | skip_map) == 0)
- /* All PTDs are finished, disable INTL processing entirely */
- isp1362_clr_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_INTL_ACTIVE);
- handled = 1;
- WARN_ON(!done_map);
- if (done_map) {
- DBG(3, "%s: INTL done_map %08x\n", __func__, done_map);
- finish_transfers(isp1362_hcd, done_map, &isp1362_hcd->intl_queue);
- start_intl_transfers(isp1362_hcd);
- }
- }
- if (irqstat & HCuPINT_ATL) {
- u32 done_map = isp1362_read_reg32(isp1362_hcd, HCATLDONE);
- u32 skip_map = isp1362_read_reg32(isp1362_hcd, HCATLSKIP);
- isp1362_hcd->irq_stat[ISP1362_INT_ATL]++;
- DBG(2, "%s: ATL\n", __func__);
- svc_mask &= ~HCuPINT_ATL;
- isp1362_write_reg32(isp1362_hcd, HCATLSKIP, skip_map | done_map);
- if (~(done_map | skip_map) == 0)
- isp1362_clr_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_ATL_ACTIVE);
- if (done_map) {
- DBG(3, "%s: ATL done_map %08x\n", __func__, done_map);
- finish_transfers(isp1362_hcd, done_map, &isp1362_hcd->atl_queue);
- start_atl_transfers(isp1362_hcd);
- }
- handled = 1;
- }
- if (irqstat & HCuPINT_OPR) {
- u32 intstat = isp1362_read_reg32(isp1362_hcd, HCINTSTAT);
- isp1362_hcd->irq_stat[ISP1362_INT_OPR]++;
- svc_mask &= ~HCuPINT_OPR;
- DBG(2, "%s: OPR %08x:%08x\n", __func__, intstat, isp1362_hcd->intenb);
- intstat &= isp1362_hcd->intenb;
- if (intstat & OHCI_INTR_UE) {
- pr_err("Unrecoverable error\n");
- /* FIXME: do here reset or cleanup or whatever */
- }
- if (intstat & OHCI_INTR_RHSC) {
- isp1362_hcd->rhstatus = isp1362_read_reg32(isp1362_hcd, HCRHSTATUS);
- isp1362_hcd->rhport[0] = isp1362_read_reg32(isp1362_hcd, HCRHPORT1);
- isp1362_hcd->rhport[1] = isp1362_read_reg32(isp1362_hcd, HCRHPORT2);
- }
- if (intstat & OHCI_INTR_RD) {
- pr_info("%s: RESUME DETECTED\n", __func__);
- isp1362_show_reg(isp1362_hcd, HCCONTROL);
- usb_hcd_resume_root_hub(hcd);
- }
- isp1362_write_reg32(isp1362_hcd, HCINTSTAT, intstat);
- irqstat &= ~HCuPINT_OPR;
- handled = 1;
- }
- if (irqstat & HCuPINT_SUSP) {
- isp1362_hcd->irq_stat[ISP1362_INT_SUSP]++;
- handled = 1;
- svc_mask &= ~HCuPINT_SUSP;
- pr_info("%s: SUSPEND IRQ\n", __func__);
- }
- if (irqstat & HCuPINT_CLKRDY) {
- isp1362_hcd->irq_stat[ISP1362_INT_CLKRDY]++;
- handled = 1;
- isp1362_hcd->irqenb &= ~HCuPINT_CLKRDY;
- svc_mask &= ~HCuPINT_CLKRDY;
- pr_info("%s: CLKRDY IRQ\n", __func__);
- }
- if (svc_mask)
- pr_err("%s: Unserviced interrupt(s) %04x\n", __func__, svc_mask);
- isp1362_write_reg16(isp1362_hcd, HCuPINTENB, isp1362_hcd->irqenb);
- isp1362_hcd->irq_active--;
- spin_unlock(&isp1362_hcd->lock);
- return IRQ_RETVAL(handled);
- }
- /*-------------------------------------------------------------------------*/
- #define MAX_PERIODIC_LOAD 900 /* out of 1000 usec */
- static int balance(struct isp1362_hcd *isp1362_hcd, u16 interval, u16 load)
- {
- int i, branch = -ENOSPC;
- /* search for the least loaded schedule branch of that interval
- * which has enough bandwidth left unreserved.
- */
- for (i = 0; i < interval; i++) {
- if (branch < 0 || isp1362_hcd->load[branch] > isp1362_hcd->load[i]) {
- int j;
- for (j = i; j < PERIODIC_SIZE; j += interval) {
- if ((isp1362_hcd->load[j] + load) > MAX_PERIODIC_LOAD) {
- pr_err("%s: new load %d load[%02x] %d max %d\n", __func__,
- load, j, isp1362_hcd->load[j], MAX_PERIODIC_LOAD);
- break;
- }
- }
- if (j < PERIODIC_SIZE)
- continue;
- branch = i;
- }
- }
- return branch;
- }
- /* NB! ALL the code above this point runs with isp1362_hcd->lock
- held, irqs off
- */
- /*-------------------------------------------------------------------------*/
- static int isp1362_urb_enqueue(struct usb_hcd *hcd,
- struct urb *urb,
- gfp_t mem_flags)
- {
- struct isp1362_hcd *isp1362_hcd = hcd_to_isp1362_hcd(hcd);
- struct usb_device *udev = urb->dev;
- unsigned int pipe = urb->pipe;
- int is_out = !usb_pipein(pipe);
- int type = usb_pipetype(pipe);
- int epnum = usb_pipeendpoint(pipe);
- struct usb_host_endpoint *hep = urb->ep;
- struct isp1362_ep *ep = NULL;
- unsigned long flags;
- int retval = 0;
- DBG(3, "%s: urb %p\n", __func__, urb);
- if (type == PIPE_ISOCHRONOUS) {
- pr_err("Isochronous transfers not supported\n");
- return -ENOSPC;
- }
- URB_DBG("%s: FA %d ep%d%s %s: len %d %s%s\n", __func__,
- usb_pipedevice(pipe), epnum,
- is_out ? "out" : "in",
- usb_pipecontrol(pipe) ? "ctrl" :
- usb_pipeint(pipe) ? "int" :
- usb_pipebulk(pipe) ? "bulk" :
- "iso",
- urb->transfer_buffer_length,
- (urb->transfer_flags & URB_ZERO_PACKET) ? "ZERO_PACKET " : "",
- !(urb->transfer_flags & URB_SHORT_NOT_OK) ?
- "short_ok" : "");
- /* avoid all allocations within spinlocks: request or endpoint */
- if (!hep->hcpriv) {
- ep = kzalloc(sizeof *ep, mem_flags);
- if (!ep)
- return -ENOMEM;
- }
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- /* don't submit to a dead or disabled port */
- if (!((isp1362_hcd->rhport[0] | isp1362_hcd->rhport[1]) &
- USB_PORT_STAT_ENABLE) ||
- !HC_IS_RUNNING(hcd->state)) {
- kfree(ep);
- retval = -ENODEV;
- goto fail_not_linked;
- }
- retval = usb_hcd_link_urb_to_ep(hcd, urb);
- if (retval) {
- kfree(ep);
- goto fail_not_linked;
- }
- if (hep->hcpriv) {
- ep = hep->hcpriv;
- } else {
- INIT_LIST_HEAD(&ep->schedule);
- INIT_LIST_HEAD(&ep->active);
- INIT_LIST_HEAD(&ep->remove_list);
- ep->udev = usb_get_dev(udev);
- ep->hep = hep;
- ep->epnum = epnum;
- ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
- ep->ptd_offset = -EINVAL;
- ep->ptd_index = -EINVAL;
- usb_settoggle(udev, epnum, is_out, 0);
- if (type == PIPE_CONTROL)
- ep->nextpid = USB_PID_SETUP;
- else if (is_out)
- ep->nextpid = USB_PID_OUT;
- else
- ep->nextpid = USB_PID_IN;
- switch (type) {
- case PIPE_ISOCHRONOUS:
- case PIPE_INTERRUPT:
- if (urb->interval > PERIODIC_SIZE)
- urb->interval = PERIODIC_SIZE;
- ep->interval = urb->interval;
- ep->branch = PERIODIC_SIZE;
- ep->load = usb_calc_bus_time(udev->speed, !is_out,
- (type == PIPE_ISOCHRONOUS),
- usb_maxpacket(udev, pipe, is_out)) / 1000;
- break;
- }
- hep->hcpriv = ep;
- }
- ep->num_req = isp1362_hcd->req_serial++;
- /* maybe put endpoint into schedule */
- switch (type) {
- case PIPE_CONTROL:
- case PIPE_BULK:
- if (list_empty(&ep->schedule)) {
- DBG(1, "%s: Adding ep %p req %d to async schedule\n",
- __func__, ep, ep->num_req);
- list_add_tail(&ep->schedule, &isp1362_hcd->async);
- }
- break;
- case PIPE_ISOCHRONOUS:
- case PIPE_INTERRUPT:
- urb->interval = ep->interval;
- /* urb submitted for already existing EP */
- if (ep->branch < PERIODIC_SIZE)
- break;
- retval = balance(isp1362_hcd, ep->interval, ep->load);
- if (retval < 0) {
- pr_err("%s: balance returned %d\n", __func__, retval);
- goto fail;
- }
- ep->branch = retval;
- retval = 0;
- isp1362_hcd->fmindex = isp1362_read_reg32(isp1362_hcd, HCFMNUM);
- DBG(1, "%s: Current frame %04x branch %02x start_frame %04x(%04x)\n",
- __func__, isp1362_hcd->fmindex, ep->branch,
- ((isp1362_hcd->fmindex + PERIODIC_SIZE - 1) &
- ~(PERIODIC_SIZE - 1)) + ep->branch,
- (isp1362_hcd->fmindex & (PERIODIC_SIZE - 1)) + ep->branch);
- if (list_empty(&ep->schedule)) {
- if (type == PIPE_ISOCHRONOUS) {
- u16 frame = isp1362_hcd->fmindex;
- frame += max_t(u16, 8, ep->interval);
- frame &= ~(ep->interval - 1);
- frame |= ep->branch;
- if (frame_before(frame, isp1362_hcd->fmindex))
- frame += ep->interval;
- urb->start_frame = frame;
- DBG(1, "%s: Adding ep %p to isoc schedule\n", __func__, ep);
- list_add_tail(&ep->schedule, &isp1362_hcd->isoc);
- } else {
- DBG(1, "%s: Adding ep %p to periodic schedule\n", __func__, ep);
- list_add_tail(&ep->schedule, &isp1362_hcd->periodic);
- }
- } else
- DBG(1, "%s: ep %p already scheduled\n", __func__, ep);
- DBG(2, "%s: load %d bandwidth %d -> %d\n", __func__,
- ep->load / ep->interval, isp1362_hcd->load[ep->branch],
- isp1362_hcd->load[ep->branch] + ep->load);
- isp1362_hcd->load[ep->branch] += ep->load;
- }
- urb->hcpriv = hep;
- ALIGNSTAT(isp1362_hcd, urb->transfer_buffer);
- switch (type) {
- case PIPE_CONTROL:
- case PIPE_BULK:
- start_atl_transfers(isp1362_hcd);
- break;
- case PIPE_INTERRUPT:
- start_intl_transfers(isp1362_hcd);
- break;
- case PIPE_ISOCHRONOUS:
- start_iso_transfers(isp1362_hcd);
- break;
- default:
- BUG();
- }
- fail:
- if (retval)
- usb_hcd_unlink_urb_from_ep(hcd, urb);
- fail_not_linked:
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- if (retval)
- DBG(0, "%s: urb %p failed with %d\n", __func__, urb, retval);
- return retval;
- }
- static int isp1362_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
- {
- struct isp1362_hcd *isp1362_hcd = hcd_to_isp1362_hcd(hcd);
- struct usb_host_endpoint *hep;
- unsigned long flags;
- struct isp1362_ep *ep;
- int retval = 0;
- DBG(3, "%s: urb %p\n", __func__, urb);
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- retval = usb_hcd_check_unlink_urb(hcd, urb, status);
- if (retval)
- goto done;
- hep = urb->hcpriv;
- if (!hep) {
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- return -EIDRM;
- }
- ep = hep->hcpriv;
- if (ep) {
- /* In front of queue? */
- if (ep->hep->urb_list.next == &urb->urb_list) {
- if (!list_empty(&ep->active)) {
- DBG(1, "%s: urb %p ep %p req %d active PTD[%d] $%04x\n", __func__,
- urb, ep, ep->num_req, ep->ptd_index, ep->ptd_offset);
- /* disable processing and queue PTD for removal */
- remove_ptd(isp1362_hcd, ep);
- urb = NULL;
- }
- }
- if (urb) {
- DBG(1, "%s: Finishing ep %p req %d\n", __func__, ep,
- ep->num_req);
- finish_request(isp1362_hcd, ep, urb, status);
- } else
- DBG(1, "%s: urb %p active; wait4irq\n", __func__, urb);
- } else {
- pr_warning("%s: No EP in URB %p\n", __func__, urb);
- retval = -EINVAL;
- }
- done:
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- DBG(3, "%s: exit\n", __func__);
- return retval;
- }
- static void isp1362_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
- {
- struct isp1362_ep *ep = hep->hcpriv;
- struct isp1362_hcd *isp1362_hcd = hcd_to_isp1362_hcd(hcd);
- unsigned long flags;
- DBG(1, "%s: ep %p\n", __func__, ep);
- if (!ep)
- return;
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- if (!list_empty(&hep->urb_list)) {
- if (!list_empty(&ep->active) && list_empty(&ep->remove_list)) {
- DBG(1, "%s: Removing ep %p req %d PTD[%d] $%04x\n", __func__,
- ep, ep->num_req, ep->ptd_index, ep->ptd_offset);
- remove_ptd(isp1362_hcd, ep);
- pr_info("%s: Waiting for Interrupt to clean up\n", __func__);
- }
- }
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- /* Wait for interrupt to clear out active list */
- while (!list_empty(&ep->active))
- msleep(1);
- DBG(1, "%s: Freeing EP %p\n", __func__, ep);
- usb_put_dev(ep->udev);
- kfree(ep);
- hep->hcpriv = NULL;
- }
- static int isp1362_get_frame(struct usb_hcd *hcd)
- {
- struct isp1362_hcd *isp1362_hcd = hcd_to_isp1362_hcd(hcd);
- u32 fmnum;
- unsigned long flags;
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- fmnum = isp1362_read_reg32(isp1362_hcd, HCFMNUM);
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- return (int)fmnum;
- }
- /*-------------------------------------------------------------------------*/
- /* Adapted from ohci-hub.c */
- static int isp1362_hub_status_data(struct usb_hcd *hcd, char *buf)
- {
- struct isp1362_hcd *isp1362_hcd = hcd_to_isp1362_hcd(hcd);
- int ports, i, changed = 0;
- unsigned long flags;
- if (!HC_IS_RUNNING(hcd->state))
- return -ESHUTDOWN;
- /* Report no status change now, if we are scheduled to be
- called later */
- if (timer_pending(&hcd->rh_timer))
- return 0;
- ports = isp1362_hcd->rhdesca & RH_A_NDP;
- BUG_ON(ports > 2);
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- /* init status */
- if (isp1362_hcd->rhstatus & (RH_HS_LPSC | RH_HS_OCIC))
- buf[0] = changed = 1;
- else
- buf[0] = 0;
- for (i = 0; i < ports; i++) {
- u32 status = isp1362_hcd->rhport[i];
- if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC |
- RH_PS_OCIC | RH_PS_PRSC)) {
- changed = 1;
- buf[0] |= 1 << (i + 1);
- continue;
- }
- if (!(status & RH_PS_CCS))
- continue;
- }
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- return changed;
- }
- static void isp1362_hub_descriptor(struct isp1362_hcd *isp1362_hcd,
- struct usb_hub_descriptor *desc)
- {
- u32 reg = isp1362_hcd->rhdesca;
- DBG(3, "%s: enter\n", __func__);
- desc->bDescriptorType = 0x29;
- desc->bDescLength = 9;
- desc->bHubContrCurrent = 0;
- desc->bNbrPorts = reg & 0x3;
- /* Power switching, device type, overcurrent. */
- desc->wHubCharacteristics = cpu_to_le16((reg >> 8) & 0x1f);
- DBG(0, "%s: hubcharacteristics = %02x\n", __func__, cpu_to_le16((reg >> 8) & 0x1f));
- desc->bPwrOn2PwrGood = (reg >> 24) & 0xff;
- /* two bitmaps: ports removable, and legacy PortPwrCtrlMask */
- desc->bitmap[0] = desc->bNbrPorts == 1 ? 1 << 1 : 3 << 1;
- desc->bitmap[1] = ~0;
- DBG(3, "%s: exit\n", __func__);
- }
- /* Adapted from ohci-hub.c */
- static int isp1362_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
- u16 wIndex, char *buf, u16 wLength)
- {
- struct isp1362_hcd *isp1362_hcd = hcd_to_isp1362_hcd(hcd);
- int retval = 0;
- unsigned long flags;
- unsigned long t1;
- int ports = isp1362_hcd->rhdesca & RH_A_NDP;
- u32 tmp = 0;
- switch (typeReq) {
- case ClearHubFeature:
- DBG(0, "ClearHubFeature: ");
- switch (wValue) {
- case C_HUB_OVER_CURRENT:
- _DBG(0, "C_HUB_OVER_CURRENT\n");
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- isp1362_write_reg32(isp1362_hcd, HCRHSTATUS, RH_HS_OCIC);
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- case C_HUB_LOCAL_POWER:
- _DBG(0, "C_HUB_LOCAL_POWER\n");
- break;
- default:
- goto error;
- }
- break;
- case SetHubFeature:
- DBG(0, "SetHubFeature: ");
- switch (wValue) {
- case C_HUB_OVER_CURRENT:
- case C_HUB_LOCAL_POWER:
- _DBG(0, "C_HUB_OVER_CURRENT or C_HUB_LOCAL_POWER\n");
- break;
- default:
- goto error;
- }
- break;
- case GetHubDescriptor:
- DBG(0, "GetHubDescriptor\n");
- isp1362_hub_descriptor(isp1362_hcd, (struct usb_hub_descriptor *)buf);
- break;
- case GetHubStatus:
- DBG(0, "GetHubStatus\n");
- put_unaligned(cpu_to_le32(0), (__le32 *) buf);
- break;
- case GetPortStatus:
- #ifndef VERBOSE
- DBG(0, "GetPortStatus\n");
- #endif
- if (!wIndex || wIndex > ports)
- goto error;
- tmp = isp1362_hcd->rhport[--wIndex];
- put_unaligned(cpu_to_le32(tmp), (__le32 *) buf);
- break;
- case ClearPortFeature:
- DBG(0, "ClearPortFeature: ");
- if (!wIndex || wIndex > ports)
- goto error;
- wIndex--;
- switch (wValue) {
- case USB_PORT_FEAT_ENABLE:
- _DBG(0, "USB_PORT_FEAT_ENABLE\n");
- tmp = RH_PS_CCS;
- break;
- case USB_PORT_FEAT_C_ENABLE:
- _DBG(0, "USB_PORT_FEAT_C_ENABLE\n");
- tmp = RH_PS_PESC;
- break;
- case USB_PORT_FEAT_SUSPEND:
- _DBG(0, "USB_PORT_FEAT_SUSPEND\n");
- tmp = RH_PS_POCI;
- break;
- case USB_PORT_FEAT_C_SUSPEND:
- _DBG(0, "USB_PORT_FEAT_C_SUSPEND\n");
- tmp = RH_PS_PSSC;
- break;
- case USB_PORT_FEAT_POWER:
- _DBG(0, "USB_PORT_FEAT_POWER\n");
- tmp = RH_PS_LSDA;
- break;
- case USB_PORT_FEAT_C_CONNECTION:
- _DBG(0, "USB_PORT_FEAT_C_CONNECTION\n");
- tmp = RH_PS_CSC;
- break;
- case USB_PORT_FEAT_C_OVER_CURRENT:
- _DBG(0, "USB_PORT_FEAT_C_OVER_CURRENT\n");
- tmp = RH_PS_OCIC;
- break;
- case USB_PORT_FEAT_C_RESET:
- _DBG(0, "USB_PORT_FEAT_C_RESET\n");
- tmp = RH_PS_PRSC;
- break;
- default:
- goto error;
- }
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- isp1362_write_reg32(isp1362_hcd, HCRHPORT1 + wIndex, tmp);
- isp1362_hcd->rhport[wIndex] =
- isp1362_read_reg32(isp1362_hcd, HCRHPORT1 + wIndex);
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- break;
- case SetPortFeature:
- DBG(0, "SetPortFeature: ");
- if (!wIndex || wIndex > ports)
- goto error;
- wIndex--;
- switch (wValue) {
- case USB_PORT_FEAT_SUSPEND:
- _DBG(0, "USB_PORT_FEAT_SUSPEND\n");
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- isp1362_write_reg32(isp1362_hcd, HCRHPORT1 + wIndex, RH_PS_PSS);
- isp1362_hcd->rhport[wIndex] =
- isp1362_read_reg32(isp1362_hcd, HCRHPORT1 + wIndex);
- spin_unlock_irqrestore(&isp1362_hcd->lock, flags);
- break;
- case USB_PORT_FEAT_POWER:
- _DBG(0, "USB_PORT_FEAT_POWER\n");
- spin_lock_irqsave(&isp1362_hcd->lock, flags);
- isp1362_write_reg32(isp1362_hcd, HCRHPORT1 + wIndex, RH_PS_PPS);
- isp1…