Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Toshiba TC86C001 ("Goku-S") USB Device Controller driver |
| 3 | * |
| 4 | * Copyright (C) 2000-2002 Lineo |
| 5 | * by Stuart Lynne, Tom Rushworth, and Bruce Balden |
| 6 | * Copyright (C) 2002 Toshiba Corporation |
| 7 | * Copyright (C) 2003 MontaVista Software (source@mvista.com) |
| 8 | * |
| 9 | * This file is licensed under the terms of the GNU General Public |
| 10 | * License version 2. This program is licensed "as is" without any |
| 11 | * warranty of any kind, whether express or implied. |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * This device has ep0 and three semi-configurable bulk/interrupt endpoints. |
| 16 | * |
| 17 | * - Endpoint numbering is fixed: ep{1,2,3}-bulk |
| 18 | * - Gadget drivers can choose ep maxpacket (8/16/32/64) |
| 19 | * - Gadget drivers can choose direction (IN, OUT) |
| 20 | * - DMA works with ep1 (OUT transfers) and ep2 (IN transfers). |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | // #define VERBOSE /* extra debug messages (success too) */ |
| 24 | // #define USB_TRACE /* packet-level success messages */ |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/pci.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/timer.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/interrupt.h> |
| 37 | #include <linux/proc_fs.h> |
| 38 | #include <linux/device.h> |
David Brownell | 5f84813 | 2006-12-16 15:34:53 -0800 | [diff] [blame] | 39 | #include <linux/usb/ch9.h> |
David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 40 | #include <linux/usb/gadget.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #include <asm/byteorder.h> |
| 43 | #include <asm/io.h> |
| 44 | #include <asm/irq.h> |
| 45 | #include <asm/system.h> |
| 46 | #include <asm/unaligned.h> |
| 47 | |
| 48 | |
| 49 | #include "goku_udc.h" |
| 50 | |
| 51 | #define DRIVER_DESC "TC86C001 USB Device Controller" |
| 52 | #define DRIVER_VERSION "30-Oct 2003" |
| 53 | |
| 54 | #define DMA_ADDR_INVALID (~(dma_addr_t)0) |
| 55 | |
| 56 | static const char driver_name [] = "goku_udc"; |
| 57 | static const char driver_desc [] = DRIVER_DESC; |
| 58 | |
| 59 | MODULE_AUTHOR("source@mvista.com"); |
| 60 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 61 | MODULE_LICENSE("GPL"); |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * IN dma behaves ok under testing, though the IN-dma abort paths don't |
| 66 | * seem to behave quite as expected. Used by default. |
| 67 | * |
| 68 | * OUT dma documents design problems handling the common "short packet" |
David Brownell | 988199f | 2005-05-07 13:05:52 -0700 | [diff] [blame] | 69 | * transfer termination policy; it couldn't be enabled by default, even |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * if the OUT-dma abort problems had a resolution. |
| 71 | */ |
| 72 | static unsigned use_dma = 1; |
| 73 | |
| 74 | #if 0 |
| 75 | //#include <linux/moduleparam.h> |
| 76 | /* "modprobe goku_udc use_dma=1" etc |
| 77 | * 0 to disable dma |
| 78 | * 1 to use IN dma only (normal operation) |
| 79 | * 2 to use IN and OUT dma |
| 80 | */ |
| 81 | module_param(use_dma, uint, S_IRUGO); |
| 82 | #endif |
| 83 | |
| 84 | /*-------------------------------------------------------------------------*/ |
| 85 | |
| 86 | static void nuke(struct goku_ep *, int status); |
| 87 | |
| 88 | static inline void |
| 89 | command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum) |
| 90 | { |
| 91 | writel(COMMAND_EP(epnum) | command, ®s->Command); |
| 92 | udelay(300); |
| 93 | } |
| 94 | |
| 95 | static int |
| 96 | goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) |
| 97 | { |
| 98 | struct goku_udc *dev; |
| 99 | struct goku_ep *ep; |
| 100 | u32 mode; |
| 101 | u16 max; |
| 102 | unsigned long flags; |
| 103 | |
| 104 | ep = container_of(_ep, struct goku_ep, ep); |
| 105 | if (!_ep || !desc || ep->desc |
| 106 | || desc->bDescriptorType != USB_DT_ENDPOINT) |
| 107 | return -EINVAL; |
| 108 | dev = ep->dev; |
| 109 | if (ep == &dev->ep[0]) |
| 110 | return -EINVAL; |
| 111 | if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) |
| 112 | return -ESHUTDOWN; |
| 113 | if (ep->num != (desc->bEndpointAddress & 0x0f)) |
| 114 | return -EINVAL; |
| 115 | |
| 116 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 117 | case USB_ENDPOINT_XFER_BULK: |
| 118 | case USB_ENDPOINT_XFER_INT: |
| 119 | break; |
| 120 | default: |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK) |
| 125 | != EPxSTATUS_EP_INVALID) |
| 126 | return -EBUSY; |
| 127 | |
| 128 | /* enabling the no-toggle interrupt mode would need an api hook */ |
| 129 | mode = 0; |
Harvey Harrison | a5abdea | 2008-04-29 01:03:40 -0700 | [diff] [blame] | 130 | max = get_unaligned_le16(&desc->wMaxPacketSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | switch (max) { |
| 132 | case 64: mode++; |
| 133 | case 32: mode++; |
| 134 | case 16: mode++; |
| 135 | case 8: mode <<= 3; |
| 136 | break; |
| 137 | default: |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | mode |= 2 << 1; /* bulk, or intr-with-toggle */ |
| 141 | |
| 142 | /* ep1/ep2 dma direction is chosen early; it works in the other |
| 143 | * direction, with pio. be cautious with out-dma. |
| 144 | */ |
| 145 | ep->is_in = (USB_DIR_IN & desc->bEndpointAddress) != 0; |
| 146 | if (ep->is_in) { |
| 147 | mode |= 1; |
| 148 | ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT); |
| 149 | } else { |
| 150 | ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT); |
| 151 | if (ep->dma) |
| 152 | DBG(dev, "%s out-dma hides short packets\n", |
| 153 | ep->ep.name); |
| 154 | } |
| 155 | |
| 156 | spin_lock_irqsave(&ep->dev->lock, flags); |
| 157 | |
| 158 | /* ep1 and ep2 can do double buffering and/or dma */ |
| 159 | if (ep->num < 3) { |
| 160 | struct goku_udc_regs __iomem *regs = ep->dev->regs; |
| 161 | u32 tmp; |
| 162 | |
| 163 | /* double buffer except (for now) with pio in */ |
| 164 | tmp = ((ep->dma || !ep->is_in) |
| 165 | ? 0x10 /* double buffered */ |
| 166 | : 0x11 /* single buffer */ |
| 167 | ) << ep->num; |
| 168 | tmp |= readl(®s->EPxSingle); |
| 169 | writel(tmp, ®s->EPxSingle); |
| 170 | |
| 171 | tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num; |
| 172 | tmp |= readl(®s->EPxBCS); |
| 173 | writel(tmp, ®s->EPxBCS); |
| 174 | } |
| 175 | writel(mode, ep->reg_mode); |
| 176 | command(ep->dev->regs, COMMAND_RESET, ep->num); |
| 177 | ep->ep.maxpacket = max; |
| 178 | ep->stopped = 0; |
| 179 | ep->desc = desc; |
| 180 | spin_unlock_irqrestore(&ep->dev->lock, flags); |
| 181 | |
| 182 | DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name, |
| 183 | ep->is_in ? "IN" : "OUT", |
| 184 | ep->dma ? "dma" : "pio", |
| 185 | max); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep) |
| 191 | { |
| 192 | struct goku_udc *dev = ep->dev; |
| 193 | |
| 194 | if (regs) { |
| 195 | command(regs, COMMAND_INVALID, ep->num); |
| 196 | if (ep->num) { |
| 197 | if (ep->num == UDC_MSTWR_ENDPOINT) |
| 198 | dev->int_enable &= ~(INT_MSTWREND |
| 199 | |INT_MSTWRTMOUT); |
| 200 | else if (ep->num == UDC_MSTRD_ENDPOINT) |
| 201 | dev->int_enable &= ~INT_MSTRDEND; |
| 202 | dev->int_enable &= ~INT_EPxDATASET (ep->num); |
| 203 | } else |
| 204 | dev->int_enable &= ~INT_EP0; |
| 205 | writel(dev->int_enable, ®s->int_enable); |
| 206 | readl(®s->int_enable); |
| 207 | if (ep->num < 3) { |
| 208 | struct goku_udc_regs __iomem *r = ep->dev->regs; |
| 209 | u32 tmp; |
| 210 | |
| 211 | tmp = readl(&r->EPxSingle); |
| 212 | tmp &= ~(0x11 << ep->num); |
| 213 | writel(tmp, &r->EPxSingle); |
| 214 | |
| 215 | tmp = readl(&r->EPxBCS); |
| 216 | tmp &= ~(0x11 << ep->num); |
| 217 | writel(tmp, &r->EPxBCS); |
| 218 | } |
| 219 | /* reset dma in case we're still using it */ |
| 220 | if (ep->dma) { |
| 221 | u32 master; |
| 222 | |
| 223 | master = readl(®s->dma_master) & MST_RW_BITS; |
| 224 | if (ep->num == UDC_MSTWR_ENDPOINT) { |
| 225 | master &= ~MST_W_BITS; |
| 226 | master |= MST_WR_RESET; |
| 227 | } else { |
| 228 | master &= ~MST_R_BITS; |
| 229 | master |= MST_RD_RESET; |
| 230 | } |
| 231 | writel(master, ®s->dma_master); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | ep->ep.maxpacket = MAX_FIFO_SIZE; |
| 236 | ep->desc = NULL; |
| 237 | ep->stopped = 1; |
| 238 | ep->irqs = 0; |
| 239 | ep->dma = 0; |
| 240 | } |
| 241 | |
| 242 | static int goku_ep_disable(struct usb_ep *_ep) |
| 243 | { |
| 244 | struct goku_ep *ep; |
| 245 | struct goku_udc *dev; |
| 246 | unsigned long flags; |
| 247 | |
| 248 | ep = container_of(_ep, struct goku_ep, ep); |
| 249 | if (!_ep || !ep->desc) |
| 250 | return -ENODEV; |
| 251 | dev = ep->dev; |
| 252 | if (dev->ep0state == EP0_SUSPEND) |
| 253 | return -EBUSY; |
| 254 | |
| 255 | VDBG(dev, "disable %s\n", _ep->name); |
| 256 | |
| 257 | spin_lock_irqsave(&dev->lock, flags); |
| 258 | nuke(ep, -ESHUTDOWN); |
| 259 | ep_reset(dev->regs, ep); |
| 260 | spin_unlock_irqrestore(&dev->lock, flags); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /*-------------------------------------------------------------------------*/ |
| 266 | |
| 267 | static struct usb_request * |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 268 | goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
| 270 | struct goku_request *req; |
| 271 | |
| 272 | if (!_ep) |
| 273 | return NULL; |
Eric Sesterhenn | 7039f42 | 2006-02-27 13:34:10 -0800 | [diff] [blame] | 274 | req = kzalloc(sizeof *req, gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (!req) |
| 276 | return NULL; |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | req->req.dma = DMA_ADDR_INVALID; |
| 279 | INIT_LIST_HEAD(&req->queue); |
| 280 | return &req->req; |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | goku_free_request(struct usb_ep *_ep, struct usb_request *_req) |
| 285 | { |
| 286 | struct goku_request *req; |
| 287 | |
| 288 | if (!_ep || !_req) |
| 289 | return; |
| 290 | |
| 291 | req = container_of(_req, struct goku_request, req); |
| 292 | WARN_ON(!list_empty(&req->queue)); |
| 293 | kfree(req); |
| 294 | } |
| 295 | |
| 296 | /*-------------------------------------------------------------------------*/ |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | static void |
| 299 | done(struct goku_ep *ep, struct goku_request *req, int status) |
| 300 | { |
| 301 | struct goku_udc *dev; |
| 302 | unsigned stopped = ep->stopped; |
| 303 | |
| 304 | list_del_init(&req->queue); |
| 305 | |
| 306 | if (likely(req->req.status == -EINPROGRESS)) |
| 307 | req->req.status = status; |
| 308 | else |
| 309 | status = req->req.status; |
| 310 | |
| 311 | dev = ep->dev; |
| 312 | if (req->mapped) { |
| 313 | pci_unmap_single(dev->pdev, req->req.dma, req->req.length, |
| 314 | ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); |
| 315 | req->req.dma = DMA_ADDR_INVALID; |
| 316 | req->mapped = 0; |
| 317 | } |
| 318 | |
| 319 | #ifndef USB_TRACE |
| 320 | if (status && status != -ESHUTDOWN) |
| 321 | #endif |
| 322 | VDBG(dev, "complete %s req %p stat %d len %u/%u\n", |
| 323 | ep->ep.name, &req->req, status, |
| 324 | req->req.actual, req->req.length); |
| 325 | |
| 326 | /* don't modify queue heads during completion callback */ |
| 327 | ep->stopped = 1; |
| 328 | spin_unlock(&dev->lock); |
| 329 | req->req.complete(&ep->ep, &req->req); |
| 330 | spin_lock(&dev->lock); |
| 331 | ep->stopped = stopped; |
| 332 | } |
| 333 | |
| 334 | /*-------------------------------------------------------------------------*/ |
| 335 | |
| 336 | static inline int |
| 337 | write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max) |
| 338 | { |
| 339 | unsigned length, count; |
| 340 | |
| 341 | length = min(req->req.length - req->req.actual, max); |
| 342 | req->req.actual += length; |
| 343 | |
| 344 | count = length; |
| 345 | while (likely(count--)) |
| 346 | writel(*buf++, fifo); |
| 347 | return length; |
| 348 | } |
| 349 | |
| 350 | // return: 0 = still running, 1 = completed, negative = errno |
| 351 | static int write_fifo(struct goku_ep *ep, struct goku_request *req) |
| 352 | { |
| 353 | struct goku_udc *dev = ep->dev; |
| 354 | u32 tmp; |
| 355 | u8 *buf; |
| 356 | unsigned count; |
| 357 | int is_last; |
| 358 | |
| 359 | tmp = readl(&dev->regs->DataSet); |
| 360 | buf = req->req.buf + req->req.actual; |
| 361 | prefetch(buf); |
| 362 | |
| 363 | dev = ep->dev; |
| 364 | if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN)) |
| 365 | return -EL2HLT; |
| 366 | |
| 367 | /* NOTE: just single-buffered PIO-IN for now. */ |
| 368 | if (unlikely((tmp & DATASET_A(ep->num)) != 0)) |
| 369 | return 0; |
| 370 | |
| 371 | /* clear our "packet available" irq */ |
| 372 | if (ep->num != 0) |
| 373 | writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status); |
| 374 | |
| 375 | count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket); |
| 376 | |
| 377 | /* last packet often short (sometimes a zlp, especially on ep0) */ |
| 378 | if (unlikely(count != ep->ep.maxpacket)) { |
| 379 | writel(~(1<<ep->num), &dev->regs->EOP); |
| 380 | if (ep->num == 0) { |
| 381 | dev->ep[0].stopped = 1; |
| 382 | dev->ep0state = EP0_STATUS; |
| 383 | } |
| 384 | is_last = 1; |
| 385 | } else { |
| 386 | if (likely(req->req.length != req->req.actual) |
| 387 | || req->req.zero) |
| 388 | is_last = 0; |
| 389 | else |
| 390 | is_last = 1; |
| 391 | } |
| 392 | #if 0 /* printk seemed to trash is_last...*/ |
| 393 | //#ifdef USB_TRACE |
| 394 | VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n", |
| 395 | ep->ep.name, count, is_last ? "/last" : "", |
| 396 | req->req.length - req->req.actual, req); |
| 397 | #endif |
| 398 | |
| 399 | /* requests complete when all IN data is in the FIFO, |
| 400 | * or sometimes later, if a zlp was needed. |
| 401 | */ |
| 402 | if (is_last) { |
| 403 | done(ep, req, 0); |
| 404 | return 1; |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int read_fifo(struct goku_ep *ep, struct goku_request *req) |
| 411 | { |
| 412 | struct goku_udc_regs __iomem *regs; |
| 413 | u32 size, set; |
| 414 | u8 *buf; |
| 415 | unsigned bufferspace, is_short, dbuff; |
| 416 | |
| 417 | regs = ep->dev->regs; |
| 418 | top: |
| 419 | buf = req->req.buf + req->req.actual; |
| 420 | prefetchw(buf); |
| 421 | |
| 422 | if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT)) |
| 423 | return -EL2HLT; |
| 424 | |
| 425 | dbuff = (ep->num == 1 || ep->num == 2); |
| 426 | do { |
| 427 | /* ack dataset irq matching the status we'll handle */ |
| 428 | if (ep->num != 0) |
| 429 | writel(~INT_EPxDATASET(ep->num), ®s->int_status); |
| 430 | |
| 431 | set = readl(®s->DataSet) & DATASET_AB(ep->num); |
| 432 | size = readl(®s->EPxSizeLA[ep->num]); |
| 433 | bufferspace = req->req.length - req->req.actual; |
| 434 | |
| 435 | /* usually do nothing without an OUT packet */ |
| 436 | if (likely(ep->num != 0 || bufferspace != 0)) { |
| 437 | if (unlikely(set == 0)) |
| 438 | break; |
| 439 | /* use ep1/ep2 double-buffering for OUT */ |
| 440 | if (!(size & PACKET_ACTIVE)) |
| 441 | size = readl(®s->EPxSizeLB[ep->num]); |
David Brownell | 2d70c99 | 2007-07-01 17:55:50 -0700 | [diff] [blame] | 442 | if (!(size & PACKET_ACTIVE)) /* "can't happen" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | break; |
| 444 | size &= DATASIZE; /* EPxSizeH == 0 */ |
| 445 | |
| 446 | /* ep0out no-out-data case for set_config, etc */ |
| 447 | } else |
| 448 | size = 0; |
| 449 | |
| 450 | /* read all bytes from this packet */ |
| 451 | req->req.actual += size; |
| 452 | is_short = (size < ep->ep.maxpacket); |
| 453 | #ifdef USB_TRACE |
| 454 | VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n", |
| 455 | ep->ep.name, size, is_short ? "/S" : "", |
| 456 | req, req->req.actual, req->req.length); |
| 457 | #endif |
| 458 | while (likely(size-- != 0)) { |
| 459 | u8 byte = (u8) readl(ep->reg_fifo); |
| 460 | |
| 461 | if (unlikely(bufferspace == 0)) { |
| 462 | /* this happens when the driver's buffer |
| 463 | * is smaller than what the host sent. |
| 464 | * discard the extra data in this packet. |
| 465 | */ |
| 466 | if (req->req.status != -EOVERFLOW) |
| 467 | DBG(ep->dev, "%s overflow %u\n", |
| 468 | ep->ep.name, size); |
| 469 | req->req.status = -EOVERFLOW; |
| 470 | } else { |
| 471 | *buf++ = byte; |
| 472 | bufferspace--; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /* completion */ |
| 477 | if (unlikely(is_short || req->req.actual == req->req.length)) { |
| 478 | if (unlikely(ep->num == 0)) { |
| 479 | /* non-control endpoints now usable? */ |
| 480 | if (ep->dev->req_config) |
| 481 | writel(ep->dev->configured |
| 482 | ? USBSTATE_CONFIGURED |
| 483 | : 0, |
| 484 | ®s->UsbState); |
| 485 | /* ep0out status stage */ |
| 486 | writel(~(1<<0), ®s->EOP); |
| 487 | ep->stopped = 1; |
| 488 | ep->dev->ep0state = EP0_STATUS; |
| 489 | } |
| 490 | done(ep, req, 0); |
| 491 | |
| 492 | /* empty the second buffer asap */ |
| 493 | if (dbuff && !list_empty(&ep->queue)) { |
| 494 | req = list_entry(ep->queue.next, |
| 495 | struct goku_request, queue); |
| 496 | goto top; |
| 497 | } |
| 498 | return 1; |
| 499 | } |
| 500 | } while (dbuff); |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static inline void |
| 505 | pio_irq_enable(struct goku_udc *dev, |
| 506 | struct goku_udc_regs __iomem *regs, int epnum) |
| 507 | { |
| 508 | dev->int_enable |= INT_EPxDATASET (epnum); |
| 509 | writel(dev->int_enable, ®s->int_enable); |
| 510 | /* write may still be posted */ |
| 511 | } |
| 512 | |
| 513 | static inline void |
| 514 | pio_irq_disable(struct goku_udc *dev, |
| 515 | struct goku_udc_regs __iomem *regs, int epnum) |
| 516 | { |
| 517 | dev->int_enable &= ~INT_EPxDATASET (epnum); |
| 518 | writel(dev->int_enable, ®s->int_enable); |
| 519 | /* write may still be posted */ |
| 520 | } |
| 521 | |
| 522 | static inline void |
| 523 | pio_advance(struct goku_ep *ep) |
| 524 | { |
| 525 | struct goku_request *req; |
| 526 | |
| 527 | if (unlikely(list_empty (&ep->queue))) |
| 528 | return; |
| 529 | req = list_entry(ep->queue.next, struct goku_request, queue); |
| 530 | (ep->is_in ? write_fifo : read_fifo)(ep, req); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | /*-------------------------------------------------------------------------*/ |
| 535 | |
| 536 | // return: 0 = q running, 1 = q stopped, negative = errno |
| 537 | static int start_dma(struct goku_ep *ep, struct goku_request *req) |
| 538 | { |
| 539 | struct goku_udc_regs __iomem *regs = ep->dev->regs; |
| 540 | u32 master; |
| 541 | u32 start = req->req.dma; |
| 542 | u32 end = start + req->req.length - 1; |
| 543 | |
| 544 | master = readl(®s->dma_master) & MST_RW_BITS; |
| 545 | |
| 546 | /* re-init the bits affecting IN dma; careful with zlps */ |
| 547 | if (likely(ep->is_in)) { |
| 548 | if (unlikely(master & MST_RD_ENA)) { |
| 549 | DBG (ep->dev, "start, IN active dma %03x!!\n", |
| 550 | master); |
| 551 | // return -EL2HLT; |
| 552 | } |
| 553 | writel(end, ®s->in_dma_end); |
| 554 | writel(start, ®s->in_dma_start); |
| 555 | |
| 556 | master &= ~MST_R_BITS; |
| 557 | if (unlikely(req->req.length == 0)) |
| 558 | master = MST_RD_ENA | MST_RD_EOPB; |
| 559 | else if ((req->req.length % ep->ep.maxpacket) != 0 |
| 560 | || req->req.zero) |
| 561 | master = MST_RD_ENA | MST_EOPB_ENA; |
| 562 | else |
| 563 | master = MST_RD_ENA | MST_EOPB_DIS; |
| 564 | |
| 565 | ep->dev->int_enable |= INT_MSTRDEND; |
| 566 | |
| 567 | /* Goku DMA-OUT merges short packets, which plays poorly with |
| 568 | * protocols where short packets mark the transfer boundaries. |
| 569 | * The chip supports a nonstandard policy with INT_MSTWRTMOUT, |
| 570 | * ending transfers after 3 SOFs; we don't turn it on. |
| 571 | */ |
| 572 | } else { |
| 573 | if (unlikely(master & MST_WR_ENA)) { |
| 574 | DBG (ep->dev, "start, OUT active dma %03x!!\n", |
| 575 | master); |
| 576 | // return -EL2HLT; |
| 577 | } |
| 578 | writel(end, ®s->out_dma_end); |
| 579 | writel(start, ®s->out_dma_start); |
| 580 | |
| 581 | master &= ~MST_W_BITS; |
| 582 | master |= MST_WR_ENA | MST_TIMEOUT_DIS; |
| 583 | |
| 584 | ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT; |
| 585 | } |
| 586 | |
| 587 | writel(master, ®s->dma_master); |
| 588 | writel(ep->dev->int_enable, ®s->int_enable); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static void dma_advance(struct goku_udc *dev, struct goku_ep *ep) |
| 593 | { |
| 594 | struct goku_request *req; |
| 595 | struct goku_udc_regs __iomem *regs = ep->dev->regs; |
| 596 | u32 master; |
| 597 | |
| 598 | master = readl(®s->dma_master); |
| 599 | |
| 600 | if (unlikely(list_empty(&ep->queue))) { |
| 601 | stop: |
| 602 | if (ep->is_in) |
| 603 | dev->int_enable &= ~INT_MSTRDEND; |
| 604 | else |
| 605 | dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT); |
| 606 | writel(dev->int_enable, ®s->int_enable); |
| 607 | return; |
| 608 | } |
| 609 | req = list_entry(ep->queue.next, struct goku_request, queue); |
| 610 | |
| 611 | /* normal hw dma completion (not abort) */ |
| 612 | if (likely(ep->is_in)) { |
| 613 | if (unlikely(master & MST_RD_ENA)) |
| 614 | return; |
| 615 | req->req.actual = readl(®s->in_dma_current); |
| 616 | } else { |
| 617 | if (unlikely(master & MST_WR_ENA)) |
| 618 | return; |
| 619 | |
| 620 | /* hardware merges short packets, and also hides packet |
| 621 | * overruns. a partial packet MAY be in the fifo here. |
| 622 | */ |
| 623 | req->req.actual = readl(®s->out_dma_current); |
| 624 | } |
| 625 | req->req.actual -= req->req.dma; |
| 626 | req->req.actual++; |
| 627 | |
| 628 | #ifdef USB_TRACE |
| 629 | VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n", |
| 630 | ep->ep.name, ep->is_in ? "IN" : "OUT", |
| 631 | req->req.actual, req->req.length, req); |
| 632 | #endif |
| 633 | done(ep, req, 0); |
| 634 | if (list_empty(&ep->queue)) |
| 635 | goto stop; |
| 636 | req = list_entry(ep->queue.next, struct goku_request, queue); |
| 637 | (void) start_dma(ep, req); |
| 638 | } |
| 639 | |
| 640 | static void abort_dma(struct goku_ep *ep, int status) |
| 641 | { |
| 642 | struct goku_udc_regs __iomem *regs = ep->dev->regs; |
| 643 | struct goku_request *req; |
| 644 | u32 curr, master; |
| 645 | |
| 646 | /* NAK future host requests, hoping the implicit delay lets the |
| 647 | * dma engine finish reading (or writing) its latest packet and |
| 648 | * empty the dma buffer (up to 16 bytes). |
| 649 | * |
| 650 | * This avoids needing to clean up a partial packet in the fifo; |
| 651 | * we can't do that for IN without side effects to HALT and TOGGLE. |
| 652 | */ |
| 653 | command(regs, COMMAND_FIFO_DISABLE, ep->num); |
| 654 | req = list_entry(ep->queue.next, struct goku_request, queue); |
| 655 | master = readl(®s->dma_master) & MST_RW_BITS; |
| 656 | |
| 657 | /* FIXME using these resets isn't usably documented. this may |
| 658 | * not work unless it's followed by disabling the endpoint. |
| 659 | * |
| 660 | * FIXME the OUT reset path doesn't even behave consistently. |
| 661 | */ |
| 662 | if (ep->is_in) { |
| 663 | if (unlikely((readl(®s->dma_master) & MST_RD_ENA) == 0)) |
| 664 | goto finished; |
| 665 | curr = readl(®s->in_dma_current); |
| 666 | |
| 667 | writel(curr, ®s->in_dma_end); |
| 668 | writel(curr, ®s->in_dma_start); |
| 669 | |
| 670 | master &= ~MST_R_BITS; |
| 671 | master |= MST_RD_RESET; |
| 672 | writel(master, ®s->dma_master); |
| 673 | |
| 674 | if (readl(®s->dma_master) & MST_RD_ENA) |
| 675 | DBG(ep->dev, "IN dma active after reset!\n"); |
| 676 | |
| 677 | } else { |
| 678 | if (unlikely((readl(®s->dma_master) & MST_WR_ENA) == 0)) |
| 679 | goto finished; |
| 680 | curr = readl(®s->out_dma_current); |
| 681 | |
| 682 | writel(curr, ®s->out_dma_end); |
| 683 | writel(curr, ®s->out_dma_start); |
| 684 | |
| 685 | master &= ~MST_W_BITS; |
| 686 | master |= MST_WR_RESET; |
| 687 | writel(master, ®s->dma_master); |
| 688 | |
| 689 | if (readl(®s->dma_master) & MST_WR_ENA) |
| 690 | DBG(ep->dev, "OUT dma active after reset!\n"); |
| 691 | } |
| 692 | req->req.actual = (curr - req->req.dma) + 1; |
| 693 | req->req.status = status; |
| 694 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 695 | VDBG(ep->dev, "%s %s %s %d/%d\n", __func__, ep->ep.name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | ep->is_in ? "IN" : "OUT", |
| 697 | req->req.actual, req->req.length); |
| 698 | |
| 699 | command(regs, COMMAND_FIFO_ENABLE, ep->num); |
| 700 | |
| 701 | return; |
| 702 | |
| 703 | finished: |
| 704 | /* dma already completed; no abort needed */ |
| 705 | command(regs, COMMAND_FIFO_ENABLE, ep->num); |
| 706 | req->req.actual = req->req.length; |
| 707 | req->req.status = 0; |
| 708 | } |
| 709 | |
| 710 | /*-------------------------------------------------------------------------*/ |
| 711 | |
| 712 | static int |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 713 | goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
| 715 | struct goku_request *req; |
| 716 | struct goku_ep *ep; |
| 717 | struct goku_udc *dev; |
| 718 | unsigned long flags; |
| 719 | int status; |
| 720 | |
| 721 | /* always require a cpu-view buffer so pio works */ |
| 722 | req = container_of(_req, struct goku_request, req); |
| 723 | if (unlikely(!_req || !_req->complete |
| 724 | || !_req->buf || !list_empty(&req->queue))) |
| 725 | return -EINVAL; |
| 726 | ep = container_of(_ep, struct goku_ep, ep); |
| 727 | if (unlikely(!_ep || (!ep->desc && ep->num != 0))) |
| 728 | return -EINVAL; |
| 729 | dev = ep->dev; |
| 730 | if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) |
| 731 | return -ESHUTDOWN; |
| 732 | |
| 733 | /* can't touch registers when suspended */ |
| 734 | if (dev->ep0state == EP0_SUSPEND) |
| 735 | return -EBUSY; |
| 736 | |
| 737 | /* set up dma mapping in case the caller didn't */ |
| 738 | if (ep->dma && _req->dma == DMA_ADDR_INVALID) { |
| 739 | _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length, |
| 740 | ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); |
| 741 | req->mapped = 1; |
| 742 | } |
| 743 | |
| 744 | #ifdef USB_TRACE |
| 745 | VDBG(dev, "%s queue req %p, len %u buf %p\n", |
| 746 | _ep->name, _req, _req->length, _req->buf); |
| 747 | #endif |
| 748 | |
| 749 | spin_lock_irqsave(&dev->lock, flags); |
| 750 | |
| 751 | _req->status = -EINPROGRESS; |
| 752 | _req->actual = 0; |
| 753 | |
| 754 | /* for ep0 IN without premature status, zlp is required and |
| 755 | * writing EOP starts the status stage (OUT). |
| 756 | */ |
| 757 | if (unlikely(ep->num == 0 && ep->is_in)) |
| 758 | _req->zero = 1; |
| 759 | |
| 760 | /* kickstart this i/o queue? */ |
| 761 | status = 0; |
| 762 | if (list_empty(&ep->queue) && likely(!ep->stopped)) { |
| 763 | /* dma: done after dma completion IRQ (or error) |
| 764 | * pio: done after last fifo operation |
| 765 | */ |
| 766 | if (ep->dma) |
| 767 | status = start_dma(ep, req); |
| 768 | else |
| 769 | status = (ep->is_in ? write_fifo : read_fifo)(ep, req); |
| 770 | |
| 771 | if (unlikely(status != 0)) { |
| 772 | if (status > 0) |
| 773 | status = 0; |
| 774 | req = NULL; |
| 775 | } |
| 776 | |
| 777 | } /* else pio or dma irq handler advances the queue. */ |
| 778 | |
| 779 | if (likely(req != 0)) |
| 780 | list_add_tail(&req->queue, &ep->queue); |
| 781 | |
| 782 | if (likely(!list_empty(&ep->queue)) |
| 783 | && likely(ep->num != 0) |
| 784 | && !ep->dma |
| 785 | && !(dev->int_enable & INT_EPxDATASET (ep->num))) |
| 786 | pio_irq_enable(dev, dev->regs, ep->num); |
| 787 | |
| 788 | spin_unlock_irqrestore(&dev->lock, flags); |
| 789 | |
| 790 | /* pci writes may still be posted */ |
| 791 | return status; |
| 792 | } |
| 793 | |
| 794 | /* dequeue ALL requests */ |
| 795 | static void nuke(struct goku_ep *ep, int status) |
| 796 | { |
| 797 | struct goku_request *req; |
| 798 | |
| 799 | ep->stopped = 1; |
| 800 | if (list_empty(&ep->queue)) |
| 801 | return; |
| 802 | if (ep->dma) |
| 803 | abort_dma(ep, status); |
| 804 | while (!list_empty(&ep->queue)) { |
| 805 | req = list_entry(ep->queue.next, struct goku_request, queue); |
| 806 | done(ep, req, status); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* dequeue JUST ONE request */ |
| 811 | static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req) |
| 812 | { |
| 813 | struct goku_request *req; |
| 814 | struct goku_ep *ep; |
| 815 | struct goku_udc *dev; |
| 816 | unsigned long flags; |
| 817 | |
| 818 | ep = container_of(_ep, struct goku_ep, ep); |
| 819 | if (!_ep || !_req || (!ep->desc && ep->num != 0)) |
| 820 | return -EINVAL; |
| 821 | dev = ep->dev; |
| 822 | if (!dev->driver) |
| 823 | return -ESHUTDOWN; |
| 824 | |
| 825 | /* we can't touch (dma) registers when suspended */ |
| 826 | if (dev->ep0state == EP0_SUSPEND) |
| 827 | return -EBUSY; |
| 828 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 829 | VDBG(dev, "%s %s %s %s %p\n", __func__, _ep->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | ep->is_in ? "IN" : "OUT", |
| 831 | ep->dma ? "dma" : "pio", |
| 832 | _req); |
| 833 | |
| 834 | spin_lock_irqsave(&dev->lock, flags); |
| 835 | |
| 836 | /* make sure it's actually queued on this endpoint */ |
| 837 | list_for_each_entry (req, &ep->queue, queue) { |
| 838 | if (&req->req == _req) |
| 839 | break; |
| 840 | } |
| 841 | if (&req->req != _req) { |
| 842 | spin_unlock_irqrestore (&dev->lock, flags); |
| 843 | return -EINVAL; |
| 844 | } |
| 845 | |
| 846 | if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) { |
| 847 | abort_dma(ep, -ECONNRESET); |
| 848 | done(ep, req, -ECONNRESET); |
| 849 | dma_advance(dev, ep); |
| 850 | } else if (!list_empty(&req->queue)) |
| 851 | done(ep, req, -ECONNRESET); |
| 852 | else |
| 853 | req = NULL; |
| 854 | spin_unlock_irqrestore(&dev->lock, flags); |
| 855 | |
| 856 | return req ? 0 : -EOPNOTSUPP; |
| 857 | } |
| 858 | |
| 859 | /*-------------------------------------------------------------------------*/ |
| 860 | |
| 861 | static void goku_clear_halt(struct goku_ep *ep) |
| 862 | { |
| 863 | // assert (ep->num !=0) |
| 864 | VDBG(ep->dev, "%s clear halt\n", ep->ep.name); |
| 865 | command(ep->dev->regs, COMMAND_SETDATA0, ep->num); |
| 866 | command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num); |
| 867 | if (ep->stopped) { |
| 868 | ep->stopped = 0; |
| 869 | if (ep->dma) { |
| 870 | struct goku_request *req; |
| 871 | |
| 872 | if (list_empty(&ep->queue)) |
| 873 | return; |
| 874 | req = list_entry(ep->queue.next, struct goku_request, |
| 875 | queue); |
| 876 | (void) start_dma(ep, req); |
| 877 | } else |
| 878 | pio_advance(ep); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | static int goku_set_halt(struct usb_ep *_ep, int value) |
| 883 | { |
| 884 | struct goku_ep *ep; |
| 885 | unsigned long flags; |
| 886 | int retval = 0; |
| 887 | |
| 888 | if (!_ep) |
| 889 | return -ENODEV; |
| 890 | ep = container_of (_ep, struct goku_ep, ep); |
| 891 | |
| 892 | if (ep->num == 0) { |
| 893 | if (value) { |
| 894 | ep->dev->ep0state = EP0_STALL; |
| 895 | ep->dev->ep[0].stopped = 1; |
| 896 | } else |
| 897 | return -EINVAL; |
| 898 | |
| 899 | /* don't change EPxSTATUS_EP_INVALID to READY */ |
| 900 | } else if (!ep->desc) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 901 | DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | return -EINVAL; |
| 903 | } |
| 904 | |
| 905 | spin_lock_irqsave(&ep->dev->lock, flags); |
| 906 | if (!list_empty(&ep->queue)) |
| 907 | retval = -EAGAIN; |
| 908 | else if (ep->is_in && value |
| 909 | /* data in (either) packet buffer? */ |
| 910 | && (readl(&ep->dev->regs->DataSet) |
| 911 | & DATASET_AB(ep->num))) |
| 912 | retval = -EAGAIN; |
| 913 | else if (!value) |
| 914 | goku_clear_halt(ep); |
| 915 | else { |
| 916 | ep->stopped = 1; |
| 917 | VDBG(ep->dev, "%s set halt\n", ep->ep.name); |
| 918 | command(ep->dev->regs, COMMAND_STALL, ep->num); |
| 919 | readl(ep->reg_status); |
| 920 | } |
| 921 | spin_unlock_irqrestore(&ep->dev->lock, flags); |
| 922 | return retval; |
| 923 | } |
| 924 | |
| 925 | static int goku_fifo_status(struct usb_ep *_ep) |
| 926 | { |
| 927 | struct goku_ep *ep; |
| 928 | struct goku_udc_regs __iomem *regs; |
| 929 | u32 size; |
| 930 | |
| 931 | if (!_ep) |
| 932 | return -ENODEV; |
| 933 | ep = container_of(_ep, struct goku_ep, ep); |
| 934 | |
| 935 | /* size is only reported sanely for OUT */ |
| 936 | if (ep->is_in) |
| 937 | return -EOPNOTSUPP; |
| 938 | |
| 939 | /* ignores 16-byte dma buffer; SizeH == 0 */ |
| 940 | regs = ep->dev->regs; |
| 941 | size = readl(®s->EPxSizeLA[ep->num]) & DATASIZE; |
| 942 | size += readl(®s->EPxSizeLB[ep->num]) & DATASIZE; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 943 | VDBG(ep->dev, "%s %s %u\n", __func__, ep->ep.name, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | return size; |
| 945 | } |
| 946 | |
| 947 | static void goku_fifo_flush(struct usb_ep *_ep) |
| 948 | { |
| 949 | struct goku_ep *ep; |
| 950 | struct goku_udc_regs __iomem *regs; |
| 951 | u32 size; |
| 952 | |
| 953 | if (!_ep) |
| 954 | return; |
| 955 | ep = container_of(_ep, struct goku_ep, ep); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 956 | VDBG(ep->dev, "%s %s\n", __func__, ep->ep.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | /* don't change EPxSTATUS_EP_INVALID to READY */ |
| 959 | if (!ep->desc && ep->num != 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 960 | DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
| 964 | regs = ep->dev->regs; |
| 965 | size = readl(®s->EPxSizeLA[ep->num]); |
| 966 | size &= DATASIZE; |
| 967 | |
| 968 | /* Non-desirable behavior: FIFO_CLEAR also clears the |
| 969 | * endpoint halt feature. For OUT, we _could_ just read |
| 970 | * the bytes out (PIO, if !ep->dma); for in, no choice. |
| 971 | */ |
| 972 | if (size) |
| 973 | command(regs, COMMAND_FIFO_CLEAR, ep->num); |
| 974 | } |
| 975 | |
| 976 | static struct usb_ep_ops goku_ep_ops = { |
| 977 | .enable = goku_ep_enable, |
| 978 | .disable = goku_ep_disable, |
| 979 | |
| 980 | .alloc_request = goku_alloc_request, |
| 981 | .free_request = goku_free_request, |
| 982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | .queue = goku_queue, |
| 984 | .dequeue = goku_dequeue, |
| 985 | |
| 986 | .set_halt = goku_set_halt, |
| 987 | .fifo_status = goku_fifo_status, |
| 988 | .fifo_flush = goku_fifo_flush, |
| 989 | }; |
| 990 | |
| 991 | /*-------------------------------------------------------------------------*/ |
| 992 | |
| 993 | static int goku_get_frame(struct usb_gadget *_gadget) |
| 994 | { |
| 995 | return -EOPNOTSUPP; |
| 996 | } |
| 997 | |
| 998 | static const struct usb_gadget_ops goku_ops = { |
| 999 | .get_frame = goku_get_frame, |
| 1000 | // no remote wakeup |
| 1001 | // not selfpowered |
| 1002 | }; |
| 1003 | |
| 1004 | /*-------------------------------------------------------------------------*/ |
| 1005 | |
| 1006 | static inline char *dmastr(void) |
| 1007 | { |
| 1008 | if (use_dma == 0) |
| 1009 | return "(dma disabled)"; |
| 1010 | else if (use_dma == 2) |
| 1011 | return "(dma IN and OUT)"; |
| 1012 | else |
| 1013 | return "(dma IN)"; |
| 1014 | } |
| 1015 | |
| 1016 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 1017 | |
| 1018 | static const char proc_node_name [] = "driver/udc"; |
| 1019 | |
| 1020 | #define FOURBITS "%s%s%s%s" |
| 1021 | #define EIGHTBITS FOURBITS FOURBITS |
| 1022 | |
| 1023 | static void |
| 1024 | dump_intmask(const char *label, u32 mask, char **next, unsigned *size) |
| 1025 | { |
| 1026 | int t; |
| 1027 | |
| 1028 | /* int_status is the same format ... */ |
| 1029 | t = scnprintf(*next, *size, |
| 1030 | "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n", |
| 1031 | label, mask, |
| 1032 | (mask & INT_PWRDETECT) ? " power" : "", |
| 1033 | (mask & INT_SYSERROR) ? " sys" : "", |
| 1034 | (mask & INT_MSTRDEND) ? " in-dma" : "", |
| 1035 | (mask & INT_MSTWRTMOUT) ? " wrtmo" : "", |
| 1036 | |
| 1037 | (mask & INT_MSTWREND) ? " out-dma" : "", |
| 1038 | (mask & INT_MSTWRSET) ? " wrset" : "", |
| 1039 | (mask & INT_ERR) ? " err" : "", |
| 1040 | (mask & INT_SOF) ? " sof" : "", |
| 1041 | |
| 1042 | (mask & INT_EP3NAK) ? " ep3nak" : "", |
| 1043 | (mask & INT_EP2NAK) ? " ep2nak" : "", |
| 1044 | (mask & INT_EP1NAK) ? " ep1nak" : "", |
| 1045 | (mask & INT_EP3DATASET) ? " ep3" : "", |
| 1046 | |
| 1047 | (mask & INT_EP2DATASET) ? " ep2" : "", |
| 1048 | (mask & INT_EP1DATASET) ? " ep1" : "", |
| 1049 | (mask & INT_STATUSNAK) ? " ep0snak" : "", |
| 1050 | (mask & INT_STATUS) ? " ep0status" : "", |
| 1051 | |
| 1052 | (mask & INT_SETUP) ? " setup" : "", |
| 1053 | (mask & INT_ENDPOINT0) ? " ep0" : "", |
| 1054 | (mask & INT_USBRESET) ? " reset" : "", |
| 1055 | (mask & INT_SUSPEND) ? " suspend" : ""); |
| 1056 | *size -= t; |
| 1057 | *next += t; |
| 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | static int |
| 1062 | udc_proc_read(char *buffer, char **start, off_t off, int count, |
| 1063 | int *eof, void *_dev) |
| 1064 | { |
| 1065 | char *buf = buffer; |
| 1066 | struct goku_udc *dev = _dev; |
| 1067 | struct goku_udc_regs __iomem *regs = dev->regs; |
| 1068 | char *next = buf; |
| 1069 | unsigned size = count; |
| 1070 | unsigned long flags; |
| 1071 | int i, t, is_usb_connected; |
| 1072 | u32 tmp; |
| 1073 | |
| 1074 | if (off != 0) |
| 1075 | return 0; |
| 1076 | |
| 1077 | local_irq_save(flags); |
| 1078 | |
| 1079 | /* basic device status */ |
| 1080 | tmp = readl(®s->power_detect); |
| 1081 | is_usb_connected = tmp & PW_DETECT; |
| 1082 | t = scnprintf(next, size, |
| 1083 | "%s - %s\n" |
| 1084 | "%s version: %s %s\n" |
| 1085 | "Gadget driver: %s\n" |
| 1086 | "Host %s, %s\n" |
| 1087 | "\n", |
| 1088 | pci_name(dev->pdev), driver_desc, |
| 1089 | driver_name, DRIVER_VERSION, dmastr(), |
| 1090 | dev->driver ? dev->driver->driver.name : "(none)", |
| 1091 | is_usb_connected |
| 1092 | ? ((tmp & PW_PULLUP) ? "full speed" : "powered") |
| 1093 | : "disconnected", |
David Brownell | 2d70c99 | 2007-07-01 17:55:50 -0700 | [diff] [blame] | 1094 | ({char *state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | switch(dev->ep0state){ |
David Brownell | 2d70c99 | 2007-07-01 17:55:50 -0700 | [diff] [blame] | 1096 | case EP0_DISCONNECT: state = "ep0_disconnect"; break; |
| 1097 | case EP0_IDLE: state = "ep0_idle"; break; |
| 1098 | case EP0_IN: state = "ep0_in"; break; |
| 1099 | case EP0_OUT: state = "ep0_out"; break; |
| 1100 | case EP0_STATUS: state = "ep0_status"; break; |
| 1101 | case EP0_STALL: state = "ep0_stall"; break; |
| 1102 | case EP0_SUSPEND: state = "ep0_suspend"; break; |
| 1103 | default: state = "ep0_?"; break; |
| 1104 | } state; }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | ); |
| 1106 | size -= t; |
| 1107 | next += t; |
| 1108 | |
| 1109 | dump_intmask("int_status", readl(®s->int_status), &next, &size); |
| 1110 | dump_intmask("int_enable", readl(®s->int_enable), &next, &size); |
| 1111 | |
| 1112 | if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0) |
| 1113 | goto done; |
| 1114 | |
| 1115 | /* registers for (active) device and ep0 */ |
| 1116 | t = scnprintf(next, size, "\nirqs %lu\ndataset %02x " |
| 1117 | "single.bcs %02x.%02x state %x addr %u\n", |
| 1118 | dev->irqs, readl(®s->DataSet), |
| 1119 | readl(®s->EPxSingle), readl(®s->EPxBCS), |
| 1120 | readl(®s->UsbState), |
| 1121 | readl(®s->address)); |
| 1122 | size -= t; |
| 1123 | next += t; |
| 1124 | |
| 1125 | tmp = readl(®s->dma_master); |
| 1126 | t = scnprintf(next, size, |
| 1127 | "dma %03X =" EIGHTBITS "%s %s\n", tmp, |
| 1128 | (tmp & MST_EOPB_DIS) ? " eopb-" : "", |
| 1129 | (tmp & MST_EOPB_ENA) ? " eopb+" : "", |
| 1130 | (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "", |
| 1131 | (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "", |
| 1132 | |
| 1133 | (tmp & MST_RD_EOPB) ? " eopb" : "", |
| 1134 | (tmp & MST_RD_RESET) ? " in_reset" : "", |
| 1135 | (tmp & MST_WR_RESET) ? " out_reset" : "", |
| 1136 | (tmp & MST_RD_ENA) ? " IN" : "", |
| 1137 | |
| 1138 | (tmp & MST_WR_ENA) ? " OUT" : "", |
| 1139 | (tmp & MST_CONNECTION) |
| 1140 | ? "ep1in/ep2out" |
| 1141 | : "ep1out/ep2in"); |
| 1142 | size -= t; |
| 1143 | next += t; |
| 1144 | |
| 1145 | /* dump endpoint queues */ |
| 1146 | for (i = 0; i < 4; i++) { |
| 1147 | struct goku_ep *ep = &dev->ep [i]; |
| 1148 | struct goku_request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
| 1150 | if (i && !ep->desc) |
| 1151 | continue; |
| 1152 | |
| 1153 | tmp = readl(ep->reg_status); |
| 1154 | t = scnprintf(next, size, |
| 1155 | "%s %s max %u %s, irqs %lu, " |
| 1156 | "status %02x (%s) " FOURBITS "\n", |
| 1157 | ep->ep.name, |
| 1158 | ep->is_in ? "in" : "out", |
| 1159 | ep->ep.maxpacket, |
| 1160 | ep->dma ? "dma" : "pio", |
| 1161 | ep->irqs, |
| 1162 | tmp, ({ char *s; |
| 1163 | switch (tmp & EPxSTATUS_EP_MASK) { |
| 1164 | case EPxSTATUS_EP_READY: |
| 1165 | s = "ready"; break; |
| 1166 | case EPxSTATUS_EP_DATAIN: |
| 1167 | s = "packet"; break; |
| 1168 | case EPxSTATUS_EP_FULL: |
| 1169 | s = "full"; break; |
| 1170 | case EPxSTATUS_EP_TX_ERR: // host will retry |
| 1171 | s = "tx_err"; break; |
| 1172 | case EPxSTATUS_EP_RX_ERR: |
| 1173 | s = "rx_err"; break; |
| 1174 | case EPxSTATUS_EP_BUSY: /* ep0 only */ |
| 1175 | s = "busy"; break; |
| 1176 | case EPxSTATUS_EP_STALL: |
| 1177 | s = "stall"; break; |
| 1178 | case EPxSTATUS_EP_INVALID: // these "can't happen" |
| 1179 | s = "invalid"; break; |
| 1180 | default: |
| 1181 | s = "?"; break; |
| 1182 | }; s; }), |
| 1183 | (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0", |
| 1184 | (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "", |
| 1185 | (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "", |
| 1186 | (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : "" |
| 1187 | ); |
| 1188 | if (t <= 0 || t > size) |
| 1189 | goto done; |
| 1190 | size -= t; |
| 1191 | next += t; |
| 1192 | |
| 1193 | if (list_empty(&ep->queue)) { |
| 1194 | t = scnprintf(next, size, "\t(nothing queued)\n"); |
| 1195 | if (t <= 0 || t > size) |
| 1196 | goto done; |
| 1197 | size -= t; |
| 1198 | next += t; |
| 1199 | continue; |
| 1200 | } |
| 1201 | list_for_each_entry(req, &ep->queue, queue) { |
| 1202 | if (ep->dma && req->queue.prev == &ep->queue) { |
| 1203 | if (i == UDC_MSTRD_ENDPOINT) |
| 1204 | tmp = readl(®s->in_dma_current); |
| 1205 | else |
| 1206 | tmp = readl(®s->out_dma_current); |
| 1207 | tmp -= req->req.dma; |
| 1208 | tmp++; |
| 1209 | } else |
| 1210 | tmp = req->req.actual; |
| 1211 | |
| 1212 | t = scnprintf(next, size, |
| 1213 | "\treq %p len %u/%u buf %p\n", |
| 1214 | &req->req, tmp, req->req.length, |
| 1215 | req->req.buf); |
| 1216 | if (t <= 0 || t > size) |
| 1217 | goto done; |
| 1218 | size -= t; |
| 1219 | next += t; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | done: |
| 1224 | local_irq_restore(flags); |
| 1225 | *eof = 1; |
| 1226 | return count - size; |
| 1227 | } |
| 1228 | |
| 1229 | #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ |
| 1230 | |
| 1231 | /*-------------------------------------------------------------------------*/ |
| 1232 | |
| 1233 | static void udc_reinit (struct goku_udc *dev) |
| 1234 | { |
| 1235 | static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" }; |
David Brownell | 2d70c99 | 2007-07-01 17:55:50 -0700 | [diff] [blame] | 1236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | unsigned i; |
| 1238 | |
| 1239 | INIT_LIST_HEAD (&dev->gadget.ep_list); |
| 1240 | dev->gadget.ep0 = &dev->ep [0].ep; |
| 1241 | dev->gadget.speed = USB_SPEED_UNKNOWN; |
| 1242 | dev->ep0state = EP0_DISCONNECT; |
| 1243 | dev->irqs = 0; |
| 1244 | |
| 1245 | for (i = 0; i < 4; i++) { |
| 1246 | struct goku_ep *ep = &dev->ep[i]; |
| 1247 | |
| 1248 | ep->num = i; |
| 1249 | ep->ep.name = names[i]; |
| 1250 | ep->reg_fifo = &dev->regs->ep_fifo [i]; |
| 1251 | ep->reg_status = &dev->regs->ep_status [i]; |
| 1252 | ep->reg_mode = &dev->regs->ep_mode[i]; |
| 1253 | |
| 1254 | ep->ep.ops = &goku_ep_ops; |
| 1255 | list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list); |
| 1256 | ep->dev = dev; |
| 1257 | INIT_LIST_HEAD (&ep->queue); |
| 1258 | |
| 1259 | ep_reset(NULL, ep); |
| 1260 | } |
| 1261 | |
| 1262 | dev->ep[0].reg_mode = NULL; |
| 1263 | dev->ep[0].ep.maxpacket = MAX_EP0_SIZE; |
| 1264 | list_del_init (&dev->ep[0].ep.ep_list); |
| 1265 | } |
| 1266 | |
| 1267 | static void udc_reset(struct goku_udc *dev) |
| 1268 | { |
| 1269 | struct goku_udc_regs __iomem *regs = dev->regs; |
| 1270 | |
| 1271 | writel(0, ®s->power_detect); |
| 1272 | writel(0, ®s->int_enable); |
| 1273 | readl(®s->int_enable); |
| 1274 | dev->int_enable = 0; |
| 1275 | |
| 1276 | /* deassert reset, leave USB D+ at hi-Z (no pullup) |
| 1277 | * don't let INT_PWRDETECT sequence begin |
| 1278 | */ |
| 1279 | udelay(250); |
| 1280 | writel(PW_RESETB, ®s->power_detect); |
| 1281 | readl(®s->int_enable); |
| 1282 | } |
| 1283 | |
| 1284 | static void ep0_start(struct goku_udc *dev) |
| 1285 | { |
| 1286 | struct goku_udc_regs __iomem *regs = dev->regs; |
| 1287 | unsigned i; |
| 1288 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1289 | VDBG(dev, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | |
| 1291 | udc_reset(dev); |
| 1292 | udc_reinit (dev); |
| 1293 | //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, ®s->dma_master); |
| 1294 | |
| 1295 | /* hw handles set_address, set_feature, get_status; maybe more */ |
| 1296 | writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF |
| 1297 | | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF |
| 1298 | | G_REQMODE_GET_DESC |
| 1299 | | G_REQMODE_CLEAR_FEAT |
| 1300 | , ®s->reqmode); |
| 1301 | |
| 1302 | for (i = 0; i < 4; i++) |
| 1303 | dev->ep[i].irqs = 0; |
| 1304 | |
| 1305 | /* can't modify descriptors after writing UsbReady */ |
| 1306 | for (i = 0; i < DESC_LEN; i++) |
| 1307 | writel(0, ®s->descriptors[i]); |
| 1308 | writel(0, ®s->UsbReady); |
| 1309 | |
| 1310 | /* expect ep0 requests when the host drops reset */ |
| 1311 | writel(PW_RESETB | PW_PULLUP, ®s->power_detect); |
| 1312 | dev->int_enable = INT_DEVWIDE | INT_EP0; |
| 1313 | writel(dev->int_enable, &dev->regs->int_enable); |
| 1314 | readl(®s->int_enable); |
| 1315 | dev->gadget.speed = USB_SPEED_FULL; |
| 1316 | dev->ep0state = EP0_IDLE; |
| 1317 | } |
| 1318 | |
| 1319 | static void udc_enable(struct goku_udc *dev) |
| 1320 | { |
| 1321 | /* start enumeration now, or after power detect irq */ |
| 1322 | if (readl(&dev->regs->power_detect) & PW_DETECT) |
| 1323 | ep0_start(dev); |
| 1324 | else { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1325 | DBG(dev, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | dev->int_enable = INT_PWRDETECT; |
| 1327 | writel(dev->int_enable, &dev->regs->int_enable); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | /*-------------------------------------------------------------------------*/ |
| 1332 | |
| 1333 | /* keeping it simple: |
| 1334 | * - one bus driver, initted first; |
| 1335 | * - one function driver, initted second |
| 1336 | */ |
| 1337 | |
| 1338 | static struct goku_udc *the_controller; |
| 1339 | |
| 1340 | /* when a driver is successfully registered, it will receive |
| 1341 | * control requests including set_configuration(), which enables |
| 1342 | * non-control requests. then usb traffic follows until a |
| 1343 | * disconnect is reported. then a host may connect again, or |
| 1344 | * the driver might get unbound. |
| 1345 | */ |
| 1346 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 1347 | { |
| 1348 | struct goku_udc *dev = the_controller; |
| 1349 | int retval; |
| 1350 | |
| 1351 | if (!driver |
SangSu Park | 32e7fea | 2009-01-02 01:11:14 +0900 | [diff] [blame] | 1352 | || driver->speed < USB_SPEED_FULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | || !driver->bind |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | || !driver->disconnect |
| 1355 | || !driver->setup) |
| 1356 | return -EINVAL; |
| 1357 | if (!dev) |
| 1358 | return -ENODEV; |
| 1359 | if (dev->driver) |
| 1360 | return -EBUSY; |
| 1361 | |
| 1362 | /* hook up the driver */ |
| 1363 | driver->driver.bus = NULL; |
| 1364 | dev->driver = driver; |
| 1365 | dev->gadget.dev.driver = &driver->driver; |
| 1366 | retval = driver->bind(&dev->gadget); |
| 1367 | if (retval) { |
| 1368 | DBG(dev, "bind to driver %s --> error %d\n", |
| 1369 | driver->driver.name, retval); |
| 1370 | dev->driver = NULL; |
| 1371 | dev->gadget.dev.driver = NULL; |
| 1372 | return retval; |
| 1373 | } |
| 1374 | |
| 1375 | /* then enable host detection and ep0; and we're ready |
| 1376 | * for set_configuration as well as eventual disconnect. |
| 1377 | */ |
| 1378 | udc_enable(dev); |
| 1379 | |
| 1380 | DBG(dev, "registered gadget driver '%s'\n", driver->driver.name); |
| 1381 | return 0; |
| 1382 | } |
| 1383 | EXPORT_SYMBOL(usb_gadget_register_driver); |
| 1384 | |
| 1385 | static void |
| 1386 | stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver) |
| 1387 | { |
| 1388 | unsigned i; |
| 1389 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1390 | DBG (dev, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
| 1392 | if (dev->gadget.speed == USB_SPEED_UNKNOWN) |
| 1393 | driver = NULL; |
| 1394 | |
| 1395 | /* disconnect gadget driver after quiesceing hw and the driver */ |
| 1396 | udc_reset (dev); |
| 1397 | for (i = 0; i < 4; i++) |
| 1398 | nuke(&dev->ep [i], -ESHUTDOWN); |
| 1399 | if (driver) { |
| 1400 | spin_unlock(&dev->lock); |
| 1401 | driver->disconnect(&dev->gadget); |
| 1402 | spin_lock(&dev->lock); |
| 1403 | } |
| 1404 | |
| 1405 | if (dev->driver) |
| 1406 | udc_enable(dev); |
| 1407 | } |
| 1408 | |
| 1409 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 1410 | { |
| 1411 | struct goku_udc *dev = the_controller; |
| 1412 | unsigned long flags; |
| 1413 | |
| 1414 | if (!dev) |
| 1415 | return -ENODEV; |
David Brownell | 6bea476 | 2006-12-05 03:15:33 -0800 | [diff] [blame] | 1416 | if (!driver || driver != dev->driver || !driver->unbind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | return -EINVAL; |
| 1418 | |
| 1419 | spin_lock_irqsave(&dev->lock, flags); |
| 1420 | dev->driver = NULL; |
| 1421 | stop_activity(dev, driver); |
| 1422 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1423 | |
| 1424 | driver->unbind(&dev->gadget); |
Patrik Sevallius | eb0be47 | 2007-11-20 09:32:00 -0800 | [diff] [blame] | 1425 | dev->gadget.dev.driver = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | |
| 1427 | DBG(dev, "unregistered driver '%s'\n", driver->driver.name); |
| 1428 | return 0; |
| 1429 | } |
| 1430 | EXPORT_SYMBOL(usb_gadget_unregister_driver); |
| 1431 | |
| 1432 | |
| 1433 | /*-------------------------------------------------------------------------*/ |
| 1434 | |
| 1435 | static void ep0_setup(struct goku_udc *dev) |
| 1436 | { |
| 1437 | struct goku_udc_regs __iomem *regs = dev->regs; |
| 1438 | struct usb_ctrlrequest ctrl; |
| 1439 | int tmp; |
| 1440 | |
| 1441 | /* read SETUP packet and enter DATA stage */ |
| 1442 | ctrl.bRequestType = readl(®s->bRequestType); |
| 1443 | ctrl.bRequest = readl(®s->bRequest); |
David Brownell | 988199f | 2005-05-07 13:05:52 -0700 | [diff] [blame] | 1444 | ctrl.wValue = cpu_to_le16((readl(®s->wValueH) << 8) |
| 1445 | | readl(®s->wValueL)); |
| 1446 | ctrl.wIndex = cpu_to_le16((readl(®s->wIndexH) << 8) |
| 1447 | | readl(®s->wIndexL)); |
| 1448 | ctrl.wLength = cpu_to_le16((readl(®s->wLengthH) << 8) |
| 1449 | | readl(®s->wLengthL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | writel(0, ®s->SetupRecv); |
| 1451 | |
| 1452 | nuke(&dev->ep[0], 0); |
| 1453 | dev->ep[0].stopped = 0; |
| 1454 | if (likely(ctrl.bRequestType & USB_DIR_IN)) { |
| 1455 | dev->ep[0].is_in = 1; |
| 1456 | dev->ep0state = EP0_IN; |
| 1457 | /* detect early status stages */ |
| 1458 | writel(ICONTROL_STATUSNAK, &dev->regs->IntControl); |
| 1459 | } else { |
| 1460 | dev->ep[0].is_in = 0; |
| 1461 | dev->ep0state = EP0_OUT; |
| 1462 | |
| 1463 | /* NOTE: CLEAR_FEATURE is done in software so that we can |
| 1464 | * synchronize transfer restarts after bulk IN stalls. data |
| 1465 | * won't even enter the fifo until the halt is cleared. |
| 1466 | */ |
| 1467 | switch (ctrl.bRequest) { |
| 1468 | case USB_REQ_CLEAR_FEATURE: |
| 1469 | switch (ctrl.bRequestType) { |
| 1470 | case USB_RECIP_ENDPOINT: |
David Brownell | 988199f | 2005-05-07 13:05:52 -0700 | [diff] [blame] | 1471 | tmp = le16_to_cpu(ctrl.wIndex) & 0x0f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | /* active endpoint */ |
| 1473 | if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0)) |
| 1474 | goto stall; |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame^] | 1475 | if (ctrl.wIndex & cpu_to_le16( |
David Brownell | 988199f | 2005-05-07 13:05:52 -0700 | [diff] [blame] | 1476 | USB_DIR_IN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (!dev->ep[tmp].is_in) |
| 1478 | goto stall; |
| 1479 | } else { |
| 1480 | if (dev->ep[tmp].is_in) |
| 1481 | goto stall; |
| 1482 | } |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame^] | 1483 | if (ctrl.wValue != cpu_to_le16( |
David Brownell | 988199f | 2005-05-07 13:05:52 -0700 | [diff] [blame] | 1484 | USB_ENDPOINT_HALT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | goto stall; |
| 1486 | if (tmp) |
| 1487 | goku_clear_halt(&dev->ep[tmp]); |
| 1488 | succeed: |
| 1489 | /* start ep0out status stage */ |
| 1490 | writel(~(1<<0), ®s->EOP); |
| 1491 | dev->ep[0].stopped = 1; |
| 1492 | dev->ep0state = EP0_STATUS; |
| 1493 | return; |
| 1494 | case USB_RECIP_DEVICE: |
| 1495 | /* device remote wakeup: always clear */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame^] | 1496 | if (ctrl.wValue != cpu_to_le16(1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | goto stall; |
| 1498 | VDBG(dev, "clear dev remote wakeup\n"); |
| 1499 | goto succeed; |
| 1500 | case USB_RECIP_INTERFACE: |
| 1501 | goto stall; |
| 1502 | default: /* pass to gadget driver */ |
| 1503 | break; |
| 1504 | } |
| 1505 | break; |
| 1506 | default: |
| 1507 | break; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | #ifdef USB_TRACE |
| 1512 | VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n", |
| 1513 | ctrl.bRequestType, ctrl.bRequest, |
David Brownell | 988199f | 2005-05-07 13:05:52 -0700 | [diff] [blame] | 1514 | le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex), |
| 1515 | le16_to_cpu(ctrl.wLength)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | #endif |
| 1517 | |
| 1518 | /* hw wants to know when we're configured (or not) */ |
| 1519 | dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION |
| 1520 | && ctrl.bRequestType == USB_RECIP_DEVICE); |
| 1521 | if (unlikely(dev->req_config)) |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame^] | 1522 | dev->configured = (ctrl.wValue != cpu_to_le16(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | |
| 1524 | /* delegate everything to the gadget driver. |
| 1525 | * it may respond after this irq handler returns. |
| 1526 | */ |
| 1527 | spin_unlock (&dev->lock); |
| 1528 | tmp = dev->driver->setup(&dev->gadget, &ctrl); |
| 1529 | spin_lock (&dev->lock); |
| 1530 | if (unlikely(tmp < 0)) { |
| 1531 | stall: |
| 1532 | #ifdef USB_TRACE |
| 1533 | VDBG(dev, "req %02x.%02x protocol STALL; err %d\n", |
| 1534 | ctrl.bRequestType, ctrl.bRequest, tmp); |
| 1535 | #endif |
| 1536 | command(regs, COMMAND_STALL, 0); |
| 1537 | dev->ep[0].stopped = 1; |
| 1538 | dev->ep0state = EP0_STALL; |
| 1539 | } |
| 1540 | |
| 1541 | /* expect at least one data or status stage irq */ |
| 1542 | } |
| 1543 | |
| 1544 | #define ACK(irqbit) { \ |
| 1545 | stat &= ~irqbit; \ |
| 1546 | writel(~irqbit, ®s->int_status); \ |
| 1547 | handled = 1; \ |
| 1548 | } |
| 1549 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1550 | static irqreturn_t goku_irq(int irq, void *_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | { |
| 1552 | struct goku_udc *dev = _dev; |
| 1553 | struct goku_udc_regs __iomem *regs = dev->regs; |
| 1554 | struct goku_ep *ep; |
| 1555 | u32 stat, handled = 0; |
| 1556 | unsigned i, rescans = 5; |
| 1557 | |
| 1558 | spin_lock(&dev->lock); |
| 1559 | |
| 1560 | rescan: |
| 1561 | stat = readl(®s->int_status) & dev->int_enable; |
| 1562 | if (!stat) |
| 1563 | goto done; |
| 1564 | dev->irqs++; |
| 1565 | |
| 1566 | /* device-wide irqs */ |
| 1567 | if (unlikely(stat & INT_DEVWIDE)) { |
| 1568 | if (stat & INT_SYSERROR) { |
| 1569 | ERROR(dev, "system error\n"); |
| 1570 | stop_activity(dev, dev->driver); |
| 1571 | stat = 0; |
| 1572 | handled = 1; |
| 1573 | // FIXME have a neater way to prevent re-enumeration |
| 1574 | dev->driver = NULL; |
| 1575 | goto done; |
| 1576 | } |
| 1577 | if (stat & INT_PWRDETECT) { |
| 1578 | writel(~stat, ®s->int_status); |
| 1579 | if (readl(&dev->regs->power_detect) & PW_DETECT) { |
| 1580 | VDBG(dev, "connect\n"); |
| 1581 | ep0_start(dev); |
| 1582 | } else { |
| 1583 | DBG(dev, "disconnect\n"); |
| 1584 | if (dev->gadget.speed == USB_SPEED_FULL) |
| 1585 | stop_activity(dev, dev->driver); |
| 1586 | dev->ep0state = EP0_DISCONNECT; |
| 1587 | dev->int_enable = INT_DEVWIDE; |
| 1588 | writel(dev->int_enable, &dev->regs->int_enable); |
| 1589 | } |
| 1590 | stat = 0; |
| 1591 | handled = 1; |
| 1592 | goto done; |
| 1593 | } |
| 1594 | if (stat & INT_SUSPEND) { |
| 1595 | ACK(INT_SUSPEND); |
| 1596 | if (readl(®s->ep_status[0]) & EPxSTATUS_SUSPEND) { |
| 1597 | switch (dev->ep0state) { |
| 1598 | case EP0_DISCONNECT: |
| 1599 | case EP0_SUSPEND: |
| 1600 | goto pm_next; |
| 1601 | default: |
| 1602 | break; |
| 1603 | } |
| 1604 | DBG(dev, "USB suspend\n"); |
| 1605 | dev->ep0state = EP0_SUSPEND; |
| 1606 | if (dev->gadget.speed != USB_SPEED_UNKNOWN |
| 1607 | && dev->driver |
| 1608 | && dev->driver->suspend) { |
| 1609 | spin_unlock(&dev->lock); |
| 1610 | dev->driver->suspend(&dev->gadget); |
| 1611 | spin_lock(&dev->lock); |
| 1612 | } |
| 1613 | } else { |
| 1614 | if (dev->ep0state != EP0_SUSPEND) { |
| 1615 | DBG(dev, "bogus USB resume %d\n", |
| 1616 | dev->ep0state); |
| 1617 | goto pm_next; |
| 1618 | } |
| 1619 | DBG(dev, "USB resume\n"); |
| 1620 | dev->ep0state = EP0_IDLE; |
| 1621 | if (dev->gadget.speed != USB_SPEED_UNKNOWN |
| 1622 | && dev->driver |
| 1623 | && dev->driver->resume) { |
| 1624 | spin_unlock(&dev->lock); |
| 1625 | dev->driver->resume(&dev->gadget); |
| 1626 | spin_lock(&dev->lock); |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | pm_next: |
| 1631 | if (stat & INT_USBRESET) { /* hub reset done */ |
| 1632 | ACK(INT_USBRESET); |
| 1633 | INFO(dev, "USB reset done, gadget %s\n", |
| 1634 | dev->driver->driver.name); |
| 1635 | } |
| 1636 | // and INT_ERR on some endpoint's crc/bitstuff/... problem |
| 1637 | } |
| 1638 | |
| 1639 | /* progress ep0 setup, data, or status stages. |
| 1640 | * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs |
| 1641 | */ |
| 1642 | if (stat & INT_SETUP) { |
| 1643 | ACK(INT_SETUP); |
| 1644 | dev->ep[0].irqs++; |
| 1645 | ep0_setup(dev); |
| 1646 | } |
| 1647 | if (stat & INT_STATUSNAK) { |
| 1648 | ACK(INT_STATUSNAK|INT_ENDPOINT0); |
| 1649 | if (dev->ep0state == EP0_IN) { |
| 1650 | ep = &dev->ep[0]; |
| 1651 | ep->irqs++; |
| 1652 | nuke(ep, 0); |
| 1653 | writel(~(1<<0), ®s->EOP); |
| 1654 | dev->ep0state = EP0_STATUS; |
| 1655 | } |
| 1656 | } |
| 1657 | if (stat & INT_ENDPOINT0) { |
| 1658 | ACK(INT_ENDPOINT0); |
| 1659 | ep = &dev->ep[0]; |
| 1660 | ep->irqs++; |
| 1661 | pio_advance(ep); |
| 1662 | } |
| 1663 | |
| 1664 | /* dma completion */ |
| 1665 | if (stat & INT_MSTRDEND) { /* IN */ |
| 1666 | ACK(INT_MSTRDEND); |
| 1667 | ep = &dev->ep[UDC_MSTRD_ENDPOINT]; |
| 1668 | ep->irqs++; |
| 1669 | dma_advance(dev, ep); |
| 1670 | } |
| 1671 | if (stat & INT_MSTWREND) { /* OUT */ |
| 1672 | ACK(INT_MSTWREND); |
| 1673 | ep = &dev->ep[UDC_MSTWR_ENDPOINT]; |
| 1674 | ep->irqs++; |
| 1675 | dma_advance(dev, ep); |
| 1676 | } |
| 1677 | if (stat & INT_MSTWRTMOUT) { /* OUT */ |
| 1678 | ACK(INT_MSTWRTMOUT); |
| 1679 | ep = &dev->ep[UDC_MSTWR_ENDPOINT]; |
| 1680 | ep->irqs++; |
| 1681 | ERROR(dev, "%s write timeout ?\n", ep->ep.name); |
| 1682 | // reset dma? then dma_advance() |
| 1683 | } |
| 1684 | |
| 1685 | /* pio */ |
| 1686 | for (i = 1; i < 4; i++) { |
| 1687 | u32 tmp = INT_EPxDATASET(i); |
| 1688 | |
| 1689 | if (!(stat & tmp)) |
| 1690 | continue; |
| 1691 | ep = &dev->ep[i]; |
| 1692 | pio_advance(ep); |
| 1693 | if (list_empty (&ep->queue)) |
| 1694 | pio_irq_disable(dev, regs, i); |
| 1695 | stat &= ~tmp; |
| 1696 | handled = 1; |
| 1697 | ep->irqs++; |
| 1698 | } |
| 1699 | |
| 1700 | if (rescans--) |
| 1701 | goto rescan; |
| 1702 | |
| 1703 | done: |
| 1704 | (void)readl(®s->int_enable); |
| 1705 | spin_unlock(&dev->lock); |
| 1706 | if (stat) |
| 1707 | DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat, |
| 1708 | readl(®s->int_status), dev->int_enable); |
| 1709 | return IRQ_RETVAL(handled); |
| 1710 | } |
| 1711 | |
| 1712 | #undef ACK |
| 1713 | |
| 1714 | /*-------------------------------------------------------------------------*/ |
| 1715 | |
| 1716 | static void gadget_release(struct device *_dev) |
| 1717 | { |
| 1718 | struct goku_udc *dev = dev_get_drvdata(_dev); |
| 1719 | |
| 1720 | kfree(dev); |
| 1721 | } |
| 1722 | |
| 1723 | /* tear down the binding between this driver and the pci device */ |
| 1724 | |
| 1725 | static void goku_remove(struct pci_dev *pdev) |
| 1726 | { |
| 1727 | struct goku_udc *dev = pci_get_drvdata(pdev); |
| 1728 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1729 | DBG(dev, "%s\n", __func__); |
David Brownell | 6bea476 | 2006-12-05 03:15:33 -0800 | [diff] [blame] | 1730 | |
| 1731 | BUG_ON(dev->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | |
| 1733 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 1734 | remove_proc_entry(proc_node_name, NULL); |
| 1735 | #endif |
| 1736 | if (dev->regs) |
| 1737 | udc_reset(dev); |
| 1738 | if (dev->got_irq) |
| 1739 | free_irq(pdev->irq, dev); |
| 1740 | if (dev->regs) |
| 1741 | iounmap(dev->regs); |
| 1742 | if (dev->got_region) |
| 1743 | release_mem_region(pci_resource_start (pdev, 0), |
| 1744 | pci_resource_len (pdev, 0)); |
| 1745 | if (dev->enabled) |
| 1746 | pci_disable_device(pdev); |
| 1747 | device_unregister(&dev->gadget.dev); |
| 1748 | |
| 1749 | pci_set_drvdata(pdev, NULL); |
| 1750 | dev->regs = NULL; |
| 1751 | the_controller = NULL; |
| 1752 | |
| 1753 | INFO(dev, "unbind\n"); |
| 1754 | } |
| 1755 | |
| 1756 | /* wrap this driver around the specified pci device, but |
| 1757 | * don't respond over USB until a gadget driver binds to us. |
| 1758 | */ |
| 1759 | |
| 1760 | static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 1761 | { |
| 1762 | struct goku_udc *dev = NULL; |
| 1763 | unsigned long resource, len; |
| 1764 | void __iomem *base = NULL; |
| 1765 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
| 1767 | /* if you want to support more than one controller in a system, |
| 1768 | * usb_gadget_driver_{register,unregister}() must change. |
| 1769 | */ |
| 1770 | if (the_controller) { |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 1771 | WARNING(dev, "ignoring %s\n", pci_name(pdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | return -EBUSY; |
| 1773 | } |
| 1774 | if (!pdev->irq) { |
| 1775 | printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev)); |
| 1776 | retval = -ENODEV; |
| 1777 | goto done; |
| 1778 | } |
| 1779 | |
| 1780 | /* alloc, and start init */ |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 1781 | dev = kzalloc (sizeof *dev, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | if (dev == NULL){ |
| 1783 | pr_debug("enomem %s\n", pci_name(pdev)); |
| 1784 | retval = -ENOMEM; |
| 1785 | goto done; |
| 1786 | } |
| 1787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | spin_lock_init(&dev->lock); |
| 1789 | dev->pdev = pdev; |
| 1790 | dev->gadget.ops = &goku_ops; |
| 1791 | |
| 1792 | /* the "gadget" abstracts/virtualizes the controller */ |
Kay Sievers | 0031a06 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 1793 | dev_set_name(&dev->gadget.dev, "gadget"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | dev->gadget.dev.parent = &pdev->dev; |
| 1795 | dev->gadget.dev.dma_mask = pdev->dev.dma_mask; |
| 1796 | dev->gadget.dev.release = gadget_release; |
| 1797 | dev->gadget.name = driver_name; |
| 1798 | |
| 1799 | /* now all the pci goodies ... */ |
| 1800 | retval = pci_enable_device(pdev); |
| 1801 | if (retval < 0) { |
| 1802 | DBG(dev, "can't enable, %d\n", retval); |
| 1803 | goto done; |
| 1804 | } |
| 1805 | dev->enabled = 1; |
| 1806 | |
| 1807 | resource = pci_resource_start(pdev, 0); |
| 1808 | len = pci_resource_len(pdev, 0); |
| 1809 | if (!request_mem_region(resource, len, driver_name)) { |
| 1810 | DBG(dev, "controller already in use\n"); |
| 1811 | retval = -EBUSY; |
| 1812 | goto done; |
| 1813 | } |
| 1814 | dev->got_region = 1; |
| 1815 | |
| 1816 | base = ioremap_nocache(resource, len); |
| 1817 | if (base == NULL) { |
| 1818 | DBG(dev, "can't map memory\n"); |
| 1819 | retval = -EFAULT; |
| 1820 | goto done; |
| 1821 | } |
| 1822 | dev->regs = (struct goku_udc_regs __iomem *) base; |
| 1823 | |
| 1824 | pci_set_drvdata(pdev, dev); |
| 1825 | INFO(dev, "%s\n", driver_desc); |
| 1826 | INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr()); |
David S. Miller | c6387a4 | 2006-06-20 01:21:29 -0700 | [diff] [blame] | 1827 | INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | |
| 1829 | /* init to known state, then setup irqs */ |
| 1830 | udc_reset(dev); |
| 1831 | udc_reinit (dev); |
Thomas Gleixner | d54b5ca | 2006-07-01 19:29:44 -0700 | [diff] [blame] | 1832 | if (request_irq(pdev->irq, goku_irq, IRQF_SHARED/*|IRQF_SAMPLE_RANDOM*/, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | driver_name, dev) != 0) { |
David S. Miller | c6387a4 | 2006-06-20 01:21:29 -0700 | [diff] [blame] | 1834 | DBG(dev, "request interrupt %d failed\n", pdev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | retval = -EBUSY; |
| 1836 | goto done; |
| 1837 | } |
| 1838 | dev->got_irq = 1; |
| 1839 | if (use_dma) |
| 1840 | pci_set_master(pdev); |
| 1841 | |
| 1842 | |
| 1843 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 1844 | create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev); |
| 1845 | #endif |
| 1846 | |
| 1847 | /* done */ |
| 1848 | the_controller = dev; |
David Brownell | 2d70c99 | 2007-07-01 17:55:50 -0700 | [diff] [blame] | 1849 | retval = device_register(&dev->gadget.dev); |
| 1850 | if (retval == 0) |
| 1851 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | |
| 1853 | done: |
| 1854 | if (dev) |
| 1855 | goku_remove (pdev); |
| 1856 | return retval; |
| 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | /*-------------------------------------------------------------------------*/ |
| 1861 | |
| 1862 | static struct pci_device_id pci_ids [] = { { |
David Brownell | 2d70c99 | 2007-07-01 17:55:50 -0700 | [diff] [blame] | 1863 | .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe), |
| 1864 | .class_mask = ~0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | .vendor = 0x102f, /* Toshiba */ |
| 1866 | .device = 0x0107, /* this UDC */ |
| 1867 | .subvendor = PCI_ANY_ID, |
| 1868 | .subdevice = PCI_ANY_ID, |
| 1869 | |
| 1870 | }, { /* end: all zeroes */ } |
| 1871 | }; |
| 1872 | MODULE_DEVICE_TABLE (pci, pci_ids); |
| 1873 | |
| 1874 | static struct pci_driver goku_pci_driver = { |
| 1875 | .name = (char *) driver_name, |
| 1876 | .id_table = pci_ids, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | |
| 1878 | .probe = goku_probe, |
| 1879 | .remove = goku_remove, |
| 1880 | |
| 1881 | /* FIXME add power management support */ |
| 1882 | }; |
| 1883 | |
| 1884 | static int __init init (void) |
| 1885 | { |
| 1886 | return pci_register_driver (&goku_pci_driver); |
| 1887 | } |
| 1888 | module_init (init); |
| 1889 | |
| 1890 | static void __exit cleanup (void) |
| 1891 | { |
| 1892 | pci_unregister_driver (&goku_pci_driver); |
| 1893 | } |
| 1894 | module_exit (cleanup); |