Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.12 5735 lines 161 kB view raw
1// SPDX-License-Identifier: GPL-2.0+ 2/* Faraday FOTG210 EHCI-like driver 3 * 4 * Copyright (c) 2013 Faraday Technology Corporation 5 * 6 * Author: Yuan-Hsin Chen <yhchen@faraday-tech.com> 7 * Feng-Hsin Chiang <john453@faraday-tech.com> 8 * Po-Yu Chuang <ratbert.chuang@gmail.com> 9 * 10 * Most of code borrowed from the Linux-3.7 EHCI driver 11 */ 12#include <linux/module.h> 13#include <linux/of.h> 14#include <linux/device.h> 15#include <linux/dmapool.h> 16#include <linux/kernel.h> 17#include <linux/delay.h> 18#include <linux/ioport.h> 19#include <linux/sched.h> 20#include <linux/vmalloc.h> 21#include <linux/errno.h> 22#include <linux/init.h> 23#include <linux/hrtimer.h> 24#include <linux/list.h> 25#include <linux/interrupt.h> 26#include <linux/usb.h> 27#include <linux/usb/hcd.h> 28#include <linux/moduleparam.h> 29#include <linux/dma-mapping.h> 30#include <linux/debugfs.h> 31#include <linux/slab.h> 32#include <linux/uaccess.h> 33#include <linux/platform_device.h> 34#include <linux/io.h> 35#include <linux/iopoll.h> 36#include <linux/clk.h> 37 38#include <asm/byteorder.h> 39#include <asm/irq.h> 40#include <asm/unaligned.h> 41 42#define DRIVER_AUTHOR "Yuan-Hsin Chen" 43#define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver" 44static const char hcd_name[] = "fotg210_hcd"; 45 46#undef FOTG210_URB_TRACE 47#define FOTG210_STATS 48 49/* magic numbers that can affect system performance */ 50#define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */ 51#define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */ 52#define FOTG210_TUNE_RL_TT 0 53#define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */ 54#define FOTG210_TUNE_MULT_TT 1 55 56/* Some drivers think it's safe to schedule isochronous transfers more than 256 57 * ms into the future (partly as a result of an old bug in the scheduling 58 * code). In an attempt to avoid trouble, we will use a minimum scheduling 59 * length of 512 frames instead of 256. 60 */ 61#define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */ 62 63/* Initial IRQ latency: faster than hw default */ 64static int log2_irq_thresh; /* 0 to 6 */ 65module_param(log2_irq_thresh, int, S_IRUGO); 66MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes"); 67 68/* initial park setting: slower than hw default */ 69static unsigned park; 70module_param(park, uint, S_IRUGO); 71MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets"); 72 73/* for link power management(LPM) feature */ 74static unsigned int hird; 75module_param(hird, int, S_IRUGO); 76MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us"); 77 78#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT) 79 80#include "fotg210.h" 81 82#define fotg210_dbg(fotg210, fmt, args...) \ 83 dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args) 84#define fotg210_err(fotg210, fmt, args...) \ 85 dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args) 86#define fotg210_info(fotg210, fmt, args...) \ 87 dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args) 88#define fotg210_warn(fotg210, fmt, args...) \ 89 dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args) 90 91/* check the values in the HCSPARAMS register (host controller _Structural_ 92 * parameters) see EHCI spec, Table 2-4 for each value 93 */ 94static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label) 95{ 96 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params); 97 98 fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params, 99 HCS_N_PORTS(params)); 100} 101 102/* check the values in the HCCPARAMS register (host controller _Capability_ 103 * parameters) see EHCI Spec, Table 2-5 for each value 104 */ 105static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label) 106{ 107 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params); 108 109 fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label, 110 params, 111 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024", 112 HCC_CANPARK(params) ? " park" : ""); 113} 114 115static void __maybe_unused 116dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd) 117{ 118 fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd, 119 hc32_to_cpup(fotg210, &qtd->hw_next), 120 hc32_to_cpup(fotg210, &qtd->hw_alt_next), 121 hc32_to_cpup(fotg210, &qtd->hw_token), 122 hc32_to_cpup(fotg210, &qtd->hw_buf[0])); 123 if (qtd->hw_buf[1]) 124 fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n", 125 hc32_to_cpup(fotg210, &qtd->hw_buf[1]), 126 hc32_to_cpup(fotg210, &qtd->hw_buf[2]), 127 hc32_to_cpup(fotg210, &qtd->hw_buf[3]), 128 hc32_to_cpup(fotg210, &qtd->hw_buf[4])); 129} 130 131static void __maybe_unused 132dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh) 133{ 134 struct fotg210_qh_hw *hw = qh->hw; 135 136 fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label, qh, 137 hw->hw_next, hw->hw_info1, hw->hw_info2, 138 hw->hw_current); 139 140 dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next); 141} 142 143static void __maybe_unused 144dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd) 145{ 146 fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n", label, 147 itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next), 148 itd->urb); 149 150 fotg210_dbg(fotg210, 151 " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n", 152 hc32_to_cpu(fotg210, itd->hw_transaction[0]), 153 hc32_to_cpu(fotg210, itd->hw_transaction[1]), 154 hc32_to_cpu(fotg210, itd->hw_transaction[2]), 155 hc32_to_cpu(fotg210, itd->hw_transaction[3]), 156 hc32_to_cpu(fotg210, itd->hw_transaction[4]), 157 hc32_to_cpu(fotg210, itd->hw_transaction[5]), 158 hc32_to_cpu(fotg210, itd->hw_transaction[6]), 159 hc32_to_cpu(fotg210, itd->hw_transaction[7])); 160 161 fotg210_dbg(fotg210, 162 " buf: %08x %08x %08x %08x %08x %08x %08x\n", 163 hc32_to_cpu(fotg210, itd->hw_bufp[0]), 164 hc32_to_cpu(fotg210, itd->hw_bufp[1]), 165 hc32_to_cpu(fotg210, itd->hw_bufp[2]), 166 hc32_to_cpu(fotg210, itd->hw_bufp[3]), 167 hc32_to_cpu(fotg210, itd->hw_bufp[4]), 168 hc32_to_cpu(fotg210, itd->hw_bufp[5]), 169 hc32_to_cpu(fotg210, itd->hw_bufp[6])); 170 171 fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n", 172 itd->index[0], itd->index[1], itd->index[2], 173 itd->index[3], itd->index[4], itd->index[5], 174 itd->index[6], itd->index[7]); 175} 176 177static int __maybe_unused 178dbg_status_buf(char *buf, unsigned len, const char *label, u32 status) 179{ 180 return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s", 181 label, label[0] ? " " : "", status, 182 (status & STS_ASS) ? " Async" : "", 183 (status & STS_PSS) ? " Periodic" : "", 184 (status & STS_RECL) ? " Recl" : "", 185 (status & STS_HALT) ? " Halt" : "", 186 (status & STS_IAA) ? " IAA" : "", 187 (status & STS_FATAL) ? " FATAL" : "", 188 (status & STS_FLR) ? " FLR" : "", 189 (status & STS_PCD) ? " PCD" : "", 190 (status & STS_ERR) ? " ERR" : "", 191 (status & STS_INT) ? " INT" : ""); 192} 193 194static int __maybe_unused 195dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable) 196{ 197 return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s", 198 label, label[0] ? " " : "", enable, 199 (enable & STS_IAA) ? " IAA" : "", 200 (enable & STS_FATAL) ? " FATAL" : "", 201 (enable & STS_FLR) ? " FLR" : "", 202 (enable & STS_PCD) ? " PCD" : "", 203 (enable & STS_ERR) ? " ERR" : "", 204 (enable & STS_INT) ? " INT" : ""); 205} 206 207static const char *const fls_strings[] = { "1024", "512", "256", "??" }; 208 209static int dbg_command_buf(char *buf, unsigned len, const char *label, 210 u32 command) 211{ 212 return scnprintf(buf, len, 213 "%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s", 214 label, label[0] ? " " : "", command, 215 (command & CMD_PARK) ? " park" : "(park)", 216 CMD_PARK_CNT(command), 217 (command >> 16) & 0x3f, 218 (command & CMD_IAAD) ? " IAAD" : "", 219 (command & CMD_ASE) ? " Async" : "", 220 (command & CMD_PSE) ? " Periodic" : "", 221 fls_strings[(command >> 2) & 0x3], 222 (command & CMD_RESET) ? " Reset" : "", 223 (command & CMD_RUN) ? "RUN" : "HALT"); 224} 225 226static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port, 227 u32 status) 228{ 229 char *sig; 230 231 /* signaling state */ 232 switch (status & (3 << 10)) { 233 case 0 << 10: 234 sig = "se0"; 235 break; 236 case 1 << 10: 237 sig = "k"; 238 break; /* low speed */ 239 case 2 << 10: 240 sig = "j"; 241 break; 242 default: 243 sig = "?"; 244 break; 245 } 246 247 scnprintf(buf, len, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s", 248 label, label[0] ? " " : "", port, status, 249 status >> 25, /*device address */ 250 sig, 251 (status & PORT_RESET) ? " RESET" : "", 252 (status & PORT_SUSPEND) ? " SUSPEND" : "", 253 (status & PORT_RESUME) ? " RESUME" : "", 254 (status & PORT_PEC) ? " PEC" : "", 255 (status & PORT_PE) ? " PE" : "", 256 (status & PORT_CSC) ? " CSC" : "", 257 (status & PORT_CONNECT) ? " CONNECT" : ""); 258 259 return buf; 260} 261 262/* functions have the "wrong" filename when they're output... */ 263#define dbg_status(fotg210, label, status) { \ 264 char _buf[80]; \ 265 dbg_status_buf(_buf, sizeof(_buf), label, status); \ 266 fotg210_dbg(fotg210, "%s\n", _buf); \ 267} 268 269#define dbg_cmd(fotg210, label, command) { \ 270 char _buf[80]; \ 271 dbg_command_buf(_buf, sizeof(_buf), label, command); \ 272 fotg210_dbg(fotg210, "%s\n", _buf); \ 273} 274 275#define dbg_port(fotg210, label, port, status) { \ 276 char _buf[80]; \ 277 fotg210_dbg(fotg210, "%s\n", \ 278 dbg_port_buf(_buf, sizeof(_buf), label, port, status));\ 279} 280 281/* troubleshooting help: expose state in debugfs */ 282static int debug_async_open(struct inode *, struct file *); 283static int debug_periodic_open(struct inode *, struct file *); 284static int debug_registers_open(struct inode *, struct file *); 285static int debug_async_open(struct inode *, struct file *); 286 287static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*); 288static int debug_close(struct inode *, struct file *); 289 290static const struct file_operations debug_async_fops = { 291 .owner = THIS_MODULE, 292 .open = debug_async_open, 293 .read = debug_output, 294 .release = debug_close, 295 .llseek = default_llseek, 296}; 297static const struct file_operations debug_periodic_fops = { 298 .owner = THIS_MODULE, 299 .open = debug_periodic_open, 300 .read = debug_output, 301 .release = debug_close, 302 .llseek = default_llseek, 303}; 304static const struct file_operations debug_registers_fops = { 305 .owner = THIS_MODULE, 306 .open = debug_registers_open, 307 .read = debug_output, 308 .release = debug_close, 309 .llseek = default_llseek, 310}; 311 312static struct dentry *fotg210_debug_root; 313 314struct debug_buffer { 315 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */ 316 struct usb_bus *bus; 317 struct mutex mutex; /* protect filling of buffer */ 318 size_t count; /* number of characters filled into buffer */ 319 char *output_buf; 320 size_t alloc_size; 321}; 322 323static inline char speed_char(u32 scratch) 324{ 325 switch (scratch & (3 << 12)) { 326 case QH_FULL_SPEED: 327 return 'f'; 328 329 case QH_LOW_SPEED: 330 return 'l'; 331 332 case QH_HIGH_SPEED: 333 return 'h'; 334 335 default: 336 return '?'; 337 } 338} 339 340static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token) 341{ 342 __u32 v = hc32_to_cpu(fotg210, token); 343 344 if (v & QTD_STS_ACTIVE) 345 return '*'; 346 if (v & QTD_STS_HALT) 347 return '-'; 348 if (!IS_SHORT_READ(v)) 349 return ' '; 350 /* tries to advance through hw_alt_next */ 351 return '/'; 352} 353 354static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh, 355 char **nextp, unsigned *sizep) 356{ 357 u32 scratch; 358 u32 hw_curr; 359 struct fotg210_qtd *td; 360 unsigned temp; 361 unsigned size = *sizep; 362 char *next = *nextp; 363 char mark; 364 __le32 list_end = FOTG210_LIST_END(fotg210); 365 struct fotg210_qh_hw *hw = qh->hw; 366 367 if (hw->hw_qtd_next == list_end) /* NEC does this */ 368 mark = '@'; 369 else 370 mark = token_mark(fotg210, hw->hw_token); 371 if (mark == '/') { /* qh_alt_next controls qh advance? */ 372 if ((hw->hw_alt_next & QTD_MASK(fotg210)) == 373 fotg210->async->hw->hw_alt_next) 374 mark = '#'; /* blocked */ 375 else if (hw->hw_alt_next == list_end) 376 mark = '.'; /* use hw_qtd_next */ 377 /* else alt_next points to some other qtd */ 378 } 379 scratch = hc32_to_cpup(fotg210, &hw->hw_info1); 380 hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0; 381 temp = scnprintf(next, size, 382 "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)", 383 qh, scratch & 0x007f, 384 speed_char(scratch), 385 (scratch >> 8) & 0x000f, 386 scratch, hc32_to_cpup(fotg210, &hw->hw_info2), 387 hc32_to_cpup(fotg210, &hw->hw_token), mark, 388 (cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token) 389 ? "data1" : "data0", 390 (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f); 391 size -= temp; 392 next += temp; 393 394 /* hc may be modifying the list as we read it ... */ 395 list_for_each_entry(td, &qh->qtd_list, qtd_list) { 396 scratch = hc32_to_cpup(fotg210, &td->hw_token); 397 mark = ' '; 398 if (hw_curr == td->qtd_dma) 399 mark = '*'; 400 else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma)) 401 mark = '+'; 402 else if (QTD_LENGTH(scratch)) { 403 if (td->hw_alt_next == fotg210->async->hw->hw_alt_next) 404 mark = '#'; 405 else if (td->hw_alt_next != list_end) 406 mark = '/'; 407 } 408 temp = snprintf(next, size, 409 "\n\t%p%c%s len=%d %08x urb %p", 410 td, mark, ({ char *tmp; 411 switch ((scratch>>8)&0x03) { 412 case 0: 413 tmp = "out"; 414 break; 415 case 1: 416 tmp = "in"; 417 break; 418 case 2: 419 tmp = "setup"; 420 break; 421 default: 422 tmp = "?"; 423 break; 424 } tmp; }), 425 (scratch >> 16) & 0x7fff, 426 scratch, 427 td->urb); 428 if (size < temp) 429 temp = size; 430 size -= temp; 431 next += temp; 432 if (temp == size) 433 goto done; 434 } 435 436 temp = snprintf(next, size, "\n"); 437 if (size < temp) 438 temp = size; 439 440 size -= temp; 441 next += temp; 442 443done: 444 *sizep = size; 445 *nextp = next; 446} 447 448static ssize_t fill_async_buffer(struct debug_buffer *buf) 449{ 450 struct usb_hcd *hcd; 451 struct fotg210_hcd *fotg210; 452 unsigned long flags; 453 unsigned temp, size; 454 char *next; 455 struct fotg210_qh *qh; 456 457 hcd = bus_to_hcd(buf->bus); 458 fotg210 = hcd_to_fotg210(hcd); 459 next = buf->output_buf; 460 size = buf->alloc_size; 461 462 *next = 0; 463 464 /* dumps a snapshot of the async schedule. 465 * usually empty except for long-term bulk reads, or head. 466 * one QH per line, and TDs we know about 467 */ 468 spin_lock_irqsave(&fotg210->lock, flags); 469 for (qh = fotg210->async->qh_next.qh; size > 0 && qh; 470 qh = qh->qh_next.qh) 471 qh_lines(fotg210, qh, &next, &size); 472 if (fotg210->async_unlink && size > 0) { 473 temp = scnprintf(next, size, "\nunlink =\n"); 474 size -= temp; 475 next += temp; 476 477 for (qh = fotg210->async_unlink; size > 0 && qh; 478 qh = qh->unlink_next) 479 qh_lines(fotg210, qh, &next, &size); 480 } 481 spin_unlock_irqrestore(&fotg210->lock, flags); 482 483 return strlen(buf->output_buf); 484} 485 486/* count tds, get ep direction */ 487static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210, 488 struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size) 489{ 490 u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1); 491 struct fotg210_qtd *qtd; 492 char *type = ""; 493 unsigned temp = 0; 494 495 /* count tds, get ep direction */ 496 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) { 497 temp++; 498 switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) { 499 case 0: 500 type = "out"; 501 continue; 502 case 1: 503 type = "in"; 504 continue; 505 } 506 } 507 508 return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)", 509 speed_char(scratch), scratch & 0x007f, 510 (scratch >> 8) & 0x000f, type, qh->usecs, 511 qh->c_usecs, temp, (scratch >> 16) & 0x7ff); 512} 513 514#define DBG_SCHED_LIMIT 64 515static ssize_t fill_periodic_buffer(struct debug_buffer *buf) 516{ 517 struct usb_hcd *hcd; 518 struct fotg210_hcd *fotg210; 519 unsigned long flags; 520 union fotg210_shadow p, *seen; 521 unsigned temp, size, seen_count; 522 char *next; 523 unsigned i; 524 __hc32 tag; 525 526 seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC); 527 if (!seen) 528 return 0; 529 530 seen_count = 0; 531 532 hcd = bus_to_hcd(buf->bus); 533 fotg210 = hcd_to_fotg210(hcd); 534 next = buf->output_buf; 535 size = buf->alloc_size; 536 537 temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size); 538 size -= temp; 539 next += temp; 540 541 /* dump a snapshot of the periodic schedule. 542 * iso changes, interrupt usually doesn't. 543 */ 544 spin_lock_irqsave(&fotg210->lock, flags); 545 for (i = 0; i < fotg210->periodic_size; i++) { 546 p = fotg210->pshadow[i]; 547 if (likely(!p.ptr)) 548 continue; 549 550 tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]); 551 552 temp = scnprintf(next, size, "%4d: ", i); 553 size -= temp; 554 next += temp; 555 556 do { 557 struct fotg210_qh_hw *hw; 558 559 switch (hc32_to_cpu(fotg210, tag)) { 560 case Q_TYPE_QH: 561 hw = p.qh->hw; 562 temp = scnprintf(next, size, " qh%d-%04x/%p", 563 p.qh->period, 564 hc32_to_cpup(fotg210, 565 &hw->hw_info2) 566 /* uframe masks */ 567 & (QH_CMASK | QH_SMASK), 568 p.qh); 569 size -= temp; 570 next += temp; 571 /* don't repeat what follows this qh */ 572 for (temp = 0; temp < seen_count; temp++) { 573 if (seen[temp].ptr != p.ptr) 574 continue; 575 if (p.qh->qh_next.ptr) { 576 temp = scnprintf(next, size, 577 " ..."); 578 size -= temp; 579 next += temp; 580 } 581 break; 582 } 583 /* show more info the first time around */ 584 if (temp == seen_count) { 585 temp = output_buf_tds_dir(next, 586 fotg210, hw, 587 p.qh, size); 588 589 if (seen_count < DBG_SCHED_LIMIT) 590 seen[seen_count++].qh = p.qh; 591 } else 592 temp = 0; 593 tag = Q_NEXT_TYPE(fotg210, hw->hw_next); 594 p = p.qh->qh_next; 595 break; 596 case Q_TYPE_FSTN: 597 temp = scnprintf(next, size, 598 " fstn-%8x/%p", 599 p.fstn->hw_prev, p.fstn); 600 tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next); 601 p = p.fstn->fstn_next; 602 break; 603 case Q_TYPE_ITD: 604 temp = scnprintf(next, size, 605 " itd/%p", p.itd); 606 tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next); 607 p = p.itd->itd_next; 608 break; 609 } 610 size -= temp; 611 next += temp; 612 } while (p.ptr); 613 614 temp = scnprintf(next, size, "\n"); 615 size -= temp; 616 next += temp; 617 } 618 spin_unlock_irqrestore(&fotg210->lock, flags); 619 kfree(seen); 620 621 return buf->alloc_size - size; 622} 623#undef DBG_SCHED_LIMIT 624 625static const char *rh_state_string(struct fotg210_hcd *fotg210) 626{ 627 switch (fotg210->rh_state) { 628 case FOTG210_RH_HALTED: 629 return "halted"; 630 case FOTG210_RH_SUSPENDED: 631 return "suspended"; 632 case FOTG210_RH_RUNNING: 633 return "running"; 634 case FOTG210_RH_STOPPING: 635 return "stopping"; 636 } 637 return "?"; 638} 639 640static ssize_t fill_registers_buffer(struct debug_buffer *buf) 641{ 642 struct usb_hcd *hcd; 643 struct fotg210_hcd *fotg210; 644 unsigned long flags; 645 unsigned temp, size, i; 646 char *next, scratch[80]; 647 static const char fmt[] = "%*s\n"; 648 static const char label[] = ""; 649 650 hcd = bus_to_hcd(buf->bus); 651 fotg210 = hcd_to_fotg210(hcd); 652 next = buf->output_buf; 653 size = buf->alloc_size; 654 655 spin_lock_irqsave(&fotg210->lock, flags); 656 657 if (!HCD_HW_ACCESSIBLE(hcd)) { 658 size = scnprintf(next, size, 659 "bus %s, device %s\n" 660 "%s\n" 661 "SUSPENDED(no register access)\n", 662 hcd->self.controller->bus->name, 663 dev_name(hcd->self.controller), 664 hcd->product_desc); 665 goto done; 666 } 667 668 /* Capability Registers */ 669 i = HC_VERSION(fotg210, fotg210_readl(fotg210, 670 &fotg210->caps->hc_capbase)); 671 temp = scnprintf(next, size, 672 "bus %s, device %s\n" 673 "%s\n" 674 "EHCI %x.%02x, rh state %s\n", 675 hcd->self.controller->bus->name, 676 dev_name(hcd->self.controller), 677 hcd->product_desc, 678 i >> 8, i & 0x0ff, rh_state_string(fotg210)); 679 size -= temp; 680 next += temp; 681 682 /* FIXME interpret both types of params */ 683 i = fotg210_readl(fotg210, &fotg210->caps->hcs_params); 684 temp = scnprintf(next, size, "structural params 0x%08x\n", i); 685 size -= temp; 686 next += temp; 687 688 i = fotg210_readl(fotg210, &fotg210->caps->hcc_params); 689 temp = scnprintf(next, size, "capability params 0x%08x\n", i); 690 size -= temp; 691 next += temp; 692 693 /* Operational Registers */ 694 temp = dbg_status_buf(scratch, sizeof(scratch), label, 695 fotg210_readl(fotg210, &fotg210->regs->status)); 696 temp = scnprintf(next, size, fmt, temp, scratch); 697 size -= temp; 698 next += temp; 699 700 temp = dbg_command_buf(scratch, sizeof(scratch), label, 701 fotg210_readl(fotg210, &fotg210->regs->command)); 702 temp = scnprintf(next, size, fmt, temp, scratch); 703 size -= temp; 704 next += temp; 705 706 temp = dbg_intr_buf(scratch, sizeof(scratch), label, 707 fotg210_readl(fotg210, &fotg210->regs->intr_enable)); 708 temp = scnprintf(next, size, fmt, temp, scratch); 709 size -= temp; 710 next += temp; 711 712 temp = scnprintf(next, size, "uframe %04x\n", 713 fotg210_read_frame_index(fotg210)); 714 size -= temp; 715 next += temp; 716 717 if (fotg210->async_unlink) { 718 temp = scnprintf(next, size, "async unlink qh %p\n", 719 fotg210->async_unlink); 720 size -= temp; 721 next += temp; 722 } 723 724#ifdef FOTG210_STATS 725 temp = scnprintf(next, size, 726 "irq normal %ld err %ld iaa %ld(lost %ld)\n", 727 fotg210->stats.normal, fotg210->stats.error, 728 fotg210->stats.iaa, fotg210->stats.lost_iaa); 729 size -= temp; 730 next += temp; 731 732 temp = scnprintf(next, size, "complete %ld unlink %ld\n", 733 fotg210->stats.complete, fotg210->stats.unlink); 734 size -= temp; 735 next += temp; 736#endif 737 738done: 739 spin_unlock_irqrestore(&fotg210->lock, flags); 740 741 return buf->alloc_size - size; 742} 743 744static struct debug_buffer 745*alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *)) 746{ 747 struct debug_buffer *buf; 748 749 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL); 750 751 if (buf) { 752 buf->bus = bus; 753 buf->fill_func = fill_func; 754 mutex_init(&buf->mutex); 755 buf->alloc_size = PAGE_SIZE; 756 } 757 758 return buf; 759} 760 761static int fill_buffer(struct debug_buffer *buf) 762{ 763 int ret = 0; 764 765 if (!buf->output_buf) 766 buf->output_buf = vmalloc(buf->alloc_size); 767 768 if (!buf->output_buf) { 769 ret = -ENOMEM; 770 goto out; 771 } 772 773 ret = buf->fill_func(buf); 774 775 if (ret >= 0) { 776 buf->count = ret; 777 ret = 0; 778 } 779 780out: 781 return ret; 782} 783 784static ssize_t debug_output(struct file *file, char __user *user_buf, 785 size_t len, loff_t *offset) 786{ 787 struct debug_buffer *buf = file->private_data; 788 int ret = 0; 789 790 mutex_lock(&buf->mutex); 791 if (buf->count == 0) { 792 ret = fill_buffer(buf); 793 if (ret != 0) { 794 mutex_unlock(&buf->mutex); 795 goto out; 796 } 797 } 798 mutex_unlock(&buf->mutex); 799 800 ret = simple_read_from_buffer(user_buf, len, offset, 801 buf->output_buf, buf->count); 802 803out: 804 return ret; 805 806} 807 808static int debug_close(struct inode *inode, struct file *file) 809{ 810 struct debug_buffer *buf = file->private_data; 811 812 if (buf) { 813 vfree(buf->output_buf); 814 kfree(buf); 815 } 816 817 return 0; 818} 819static int debug_async_open(struct inode *inode, struct file *file) 820{ 821 file->private_data = alloc_buffer(inode->i_private, fill_async_buffer); 822 823 return file->private_data ? 0 : -ENOMEM; 824} 825 826static int debug_periodic_open(struct inode *inode, struct file *file) 827{ 828 struct debug_buffer *buf; 829 830 buf = alloc_buffer(inode->i_private, fill_periodic_buffer); 831 if (!buf) 832 return -ENOMEM; 833 834 buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE; 835 file->private_data = buf; 836 return 0; 837} 838 839static int debug_registers_open(struct inode *inode, struct file *file) 840{ 841 file->private_data = alloc_buffer(inode->i_private, 842 fill_registers_buffer); 843 844 return file->private_data ? 0 : -ENOMEM; 845} 846 847static inline void create_debug_files(struct fotg210_hcd *fotg210) 848{ 849 struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self; 850 struct dentry *root; 851 852 root = debugfs_create_dir(bus->bus_name, fotg210_debug_root); 853 fotg210->debug_dir = root; 854 855 debugfs_create_file("async", S_IRUGO, root, bus, &debug_async_fops); 856 debugfs_create_file("periodic", S_IRUGO, root, bus, 857 &debug_periodic_fops); 858 debugfs_create_file("registers", S_IRUGO, root, bus, 859 &debug_registers_fops); 860} 861 862static inline void remove_debug_files(struct fotg210_hcd *fotg210) 863{ 864 debugfs_remove_recursive(fotg210->debug_dir); 865} 866 867/* handshake - spin reading hc until handshake completes or fails 868 * @ptr: address of hc register to be read 869 * @mask: bits to look at in result of read 870 * @done: value of those bits when handshake succeeds 871 * @usec: timeout in microseconds 872 * 873 * Returns negative errno, or zero on success 874 * 875 * Success happens when the "mask" bits have the specified value (hardware 876 * handshake done). There are two failure modes: "usec" have passed (major 877 * hardware flakeout), or the register reads as all-ones (hardware removed). 878 * 879 * That last failure should_only happen in cases like physical cardbus eject 880 * before driver shutdown. But it also seems to be caused by bugs in cardbus 881 * bridge shutdown: shutting down the bridge before the devices using it. 882 */ 883static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr, 884 u32 mask, u32 done, int usec) 885{ 886 u32 result; 887 int ret; 888 889 ret = readl_poll_timeout_atomic(ptr, result, 890 ((result & mask) == done || 891 result == U32_MAX), 1, usec); 892 if (result == U32_MAX) /* card removed */ 893 return -ENODEV; 894 895 return ret; 896} 897 898/* Force HC to halt state from unknown (EHCI spec section 2.3). 899 * Must be called with interrupts enabled and the lock not held. 900 */ 901static int fotg210_halt(struct fotg210_hcd *fotg210) 902{ 903 u32 temp; 904 905 spin_lock_irq(&fotg210->lock); 906 907 /* disable any irqs left enabled by previous code */ 908 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable); 909 910 /* 911 * This routine gets called during probe before fotg210->command 912 * has been initialized, so we can't rely on its value. 913 */ 914 fotg210->command &= ~CMD_RUN; 915 temp = fotg210_readl(fotg210, &fotg210->regs->command); 916 temp &= ~(CMD_RUN | CMD_IAAD); 917 fotg210_writel(fotg210, temp, &fotg210->regs->command); 918 919 spin_unlock_irq(&fotg210->lock); 920 synchronize_irq(fotg210_to_hcd(fotg210)->irq); 921 922 return handshake(fotg210, &fotg210->regs->status, 923 STS_HALT, STS_HALT, 16 * 125); 924} 925 926/* Reset a non-running (STS_HALT == 1) controller. 927 * Must be called with interrupts enabled and the lock not held. 928 */ 929static int fotg210_reset(struct fotg210_hcd *fotg210) 930{ 931 int retval; 932 u32 command = fotg210_readl(fotg210, &fotg210->regs->command); 933 934 /* If the EHCI debug controller is active, special care must be 935 * taken before and after a host controller reset 936 */ 937 if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210))) 938 fotg210->debug = NULL; 939 940 command |= CMD_RESET; 941 dbg_cmd(fotg210, "reset", command); 942 fotg210_writel(fotg210, command, &fotg210->regs->command); 943 fotg210->rh_state = FOTG210_RH_HALTED; 944 fotg210->next_statechange = jiffies; 945 retval = handshake(fotg210, &fotg210->regs->command, 946 CMD_RESET, 0, 250 * 1000); 947 948 if (retval) 949 return retval; 950 951 if (fotg210->debug) 952 dbgp_external_startup(fotg210_to_hcd(fotg210)); 953 954 fotg210->port_c_suspend = fotg210->suspended_ports = 955 fotg210->resuming_ports = 0; 956 return retval; 957} 958 959/* Idle the controller (turn off the schedules). 960 * Must be called with interrupts enabled and the lock not held. 961 */ 962static void fotg210_quiesce(struct fotg210_hcd *fotg210) 963{ 964 u32 temp; 965 966 if (fotg210->rh_state != FOTG210_RH_RUNNING) 967 return; 968 969 /* wait for any schedule enables/disables to take effect */ 970 temp = (fotg210->command << 10) & (STS_ASS | STS_PSS); 971 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp, 972 16 * 125); 973 974 /* then disable anything that's still active */ 975 spin_lock_irq(&fotg210->lock); 976 fotg210->command &= ~(CMD_ASE | CMD_PSE); 977 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command); 978 spin_unlock_irq(&fotg210->lock); 979 980 /* hardware can take 16 microframes to turn off ... */ 981 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0, 982 16 * 125); 983} 984 985static void end_unlink_async(struct fotg210_hcd *fotg210); 986static void unlink_empty_async(struct fotg210_hcd *fotg210); 987static void fotg210_work(struct fotg210_hcd *fotg210); 988static void start_unlink_intr(struct fotg210_hcd *fotg210, 989 struct fotg210_qh *qh); 990static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh); 991 992/* Set a bit in the USBCMD register */ 993static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit) 994{ 995 fotg210->command |= bit; 996 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command); 997 998 /* unblock posted write */ 999 fotg210_readl(fotg210, &fotg210->regs->command); 1000} 1001 1002/* Clear a bit in the USBCMD register */ 1003static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit) 1004{ 1005 fotg210->command &= ~bit; 1006 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command); 1007 1008 /* unblock posted write */ 1009 fotg210_readl(fotg210, &fotg210->regs->command); 1010} 1011 1012/* EHCI timer support... Now using hrtimers. 1013 * 1014 * Lots of different events are triggered from fotg210->hrtimer. Whenever 1015 * the timer routine runs, it checks each possible event; events that are 1016 * currently enabled and whose expiration time has passed get handled. 1017 * The set of enabled events is stored as a collection of bitflags in 1018 * fotg210->enabled_hrtimer_events, and they are numbered in order of 1019 * increasing delay values (ranging between 1 ms and 100 ms). 1020 * 1021 * Rather than implementing a sorted list or tree of all pending events, 1022 * we keep track only of the lowest-numbered pending event, in 1023 * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its 1024 * expiration time is set to the timeout value for this event. 1025 * 1026 * As a result, events might not get handled right away; the actual delay 1027 * could be anywhere up to twice the requested delay. This doesn't 1028 * matter, because none of the events are especially time-critical. The 1029 * ones that matter most all have a delay of 1 ms, so they will be 1030 * handled after 2 ms at most, which is okay. In addition to this, we 1031 * allow for an expiration range of 1 ms. 1032 */ 1033 1034/* Delay lengths for the hrtimer event types. 1035 * Keep this list sorted by delay length, in the same order as 1036 * the event types indexed by enum fotg210_hrtimer_event in fotg210.h. 1037 */ 1038static unsigned event_delays_ns[] = { 1039 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */ 1040 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */ 1041 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */ 1042 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */ 1043 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */ 1044 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */ 1045 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */ 1046 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */ 1047 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */ 1048 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */ 1049}; 1050 1051/* Enable a pending hrtimer event */ 1052static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event, 1053 bool resched) 1054{ 1055 ktime_t *timeout = &fotg210->hr_timeouts[event]; 1056 1057 if (resched) 1058 *timeout = ktime_add(ktime_get(), event_delays_ns[event]); 1059 fotg210->enabled_hrtimer_events |= (1 << event); 1060 1061 /* Track only the lowest-numbered pending event */ 1062 if (event < fotg210->next_hrtimer_event) { 1063 fotg210->next_hrtimer_event = event; 1064 hrtimer_start_range_ns(&fotg210->hrtimer, *timeout, 1065 NSEC_PER_MSEC, HRTIMER_MODE_ABS); 1066 } 1067} 1068 1069 1070/* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */ 1071static void fotg210_poll_ASS(struct fotg210_hcd *fotg210) 1072{ 1073 unsigned actual, want; 1074 1075 /* Don't enable anything if the controller isn't running (e.g., died) */ 1076 if (fotg210->rh_state != FOTG210_RH_RUNNING) 1077 return; 1078 1079 want = (fotg210->command & CMD_ASE) ? STS_ASS : 0; 1080 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS; 1081 1082 if (want != actual) { 1083 1084 /* Poll again later, but give up after about 20 ms */ 1085 if (fotg210->ASS_poll_count++ < 20) { 1086 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS, 1087 true); 1088 return; 1089 } 1090 fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n", 1091 want, actual); 1092 } 1093 fotg210->ASS_poll_count = 0; 1094 1095 /* The status is up-to-date; restart or stop the schedule as needed */ 1096 if (want == 0) { /* Stopped */ 1097 if (fotg210->async_count > 0) 1098 fotg210_set_command_bit(fotg210, CMD_ASE); 1099 1100 } else { /* Running */ 1101 if (fotg210->async_count == 0) { 1102 1103 /* Turn off the schedule after a while */ 1104 fotg210_enable_event(fotg210, 1105 FOTG210_HRTIMER_DISABLE_ASYNC, 1106 true); 1107 } 1108 } 1109} 1110 1111/* Turn off the async schedule after a brief delay */ 1112static void fotg210_disable_ASE(struct fotg210_hcd *fotg210) 1113{ 1114 fotg210_clear_command_bit(fotg210, CMD_ASE); 1115} 1116 1117 1118/* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */ 1119static void fotg210_poll_PSS(struct fotg210_hcd *fotg210) 1120{ 1121 unsigned actual, want; 1122 1123 /* Don't do anything if the controller isn't running (e.g., died) */ 1124 if (fotg210->rh_state != FOTG210_RH_RUNNING) 1125 return; 1126 1127 want = (fotg210->command & CMD_PSE) ? STS_PSS : 0; 1128 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS; 1129 1130 if (want != actual) { 1131 1132 /* Poll again later, but give up after about 20 ms */ 1133 if (fotg210->PSS_poll_count++ < 20) { 1134 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS, 1135 true); 1136 return; 1137 } 1138 fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n", 1139 want, actual); 1140 } 1141 fotg210->PSS_poll_count = 0; 1142 1143 /* The status is up-to-date; restart or stop the schedule as needed */ 1144 if (want == 0) { /* Stopped */ 1145 if (fotg210->periodic_count > 0) 1146 fotg210_set_command_bit(fotg210, CMD_PSE); 1147 1148 } else { /* Running */ 1149 if (fotg210->periodic_count == 0) { 1150 1151 /* Turn off the schedule after a while */ 1152 fotg210_enable_event(fotg210, 1153 FOTG210_HRTIMER_DISABLE_PERIODIC, 1154 true); 1155 } 1156 } 1157} 1158 1159/* Turn off the periodic schedule after a brief delay */ 1160static void fotg210_disable_PSE(struct fotg210_hcd *fotg210) 1161{ 1162 fotg210_clear_command_bit(fotg210, CMD_PSE); 1163} 1164 1165 1166/* Poll the STS_HALT status bit; see when a dead controller stops */ 1167static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210) 1168{ 1169 if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) { 1170 1171 /* Give up after a few milliseconds */ 1172 if (fotg210->died_poll_count++ < 5) { 1173 /* Try again later */ 1174 fotg210_enable_event(fotg210, 1175 FOTG210_HRTIMER_POLL_DEAD, true); 1176 return; 1177 } 1178 fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n"); 1179 } 1180 1181 /* Clean up the mess */ 1182 fotg210->rh_state = FOTG210_RH_HALTED; 1183 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable); 1184 fotg210_work(fotg210); 1185 end_unlink_async(fotg210); 1186 1187 /* Not in process context, so don't try to reset the controller */ 1188} 1189 1190 1191/* Handle unlinked interrupt QHs once they are gone from the hardware */ 1192static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210) 1193{ 1194 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING); 1195 1196 /* 1197 * Process all the QHs on the intr_unlink list that were added 1198 * before the current unlink cycle began. The list is in 1199 * temporal order, so stop when we reach the first entry in the 1200 * current cycle. But if the root hub isn't running then 1201 * process all the QHs on the list. 1202 */ 1203 fotg210->intr_unlinking = true; 1204 while (fotg210->intr_unlink) { 1205 struct fotg210_qh *qh = fotg210->intr_unlink; 1206 1207 if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle) 1208 break; 1209 fotg210->intr_unlink = qh->unlink_next; 1210 qh->unlink_next = NULL; 1211 end_unlink_intr(fotg210, qh); 1212 } 1213 1214 /* Handle remaining entries later */ 1215 if (fotg210->intr_unlink) { 1216 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR, 1217 true); 1218 ++fotg210->intr_unlink_cycle; 1219 } 1220 fotg210->intr_unlinking = false; 1221} 1222 1223 1224/* Start another free-iTDs/siTDs cycle */ 1225static void start_free_itds(struct fotg210_hcd *fotg210) 1226{ 1227 if (!(fotg210->enabled_hrtimer_events & 1228 BIT(FOTG210_HRTIMER_FREE_ITDS))) { 1229 fotg210->last_itd_to_free = list_entry( 1230 fotg210->cached_itd_list.prev, 1231 struct fotg210_itd, itd_list); 1232 fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true); 1233 } 1234} 1235 1236/* Wait for controller to stop using old iTDs and siTDs */ 1237static void end_free_itds(struct fotg210_hcd *fotg210) 1238{ 1239 struct fotg210_itd *itd, *n; 1240 1241 if (fotg210->rh_state < FOTG210_RH_RUNNING) 1242 fotg210->last_itd_to_free = NULL; 1243 1244 list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) { 1245 list_del(&itd->itd_list); 1246 dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma); 1247 if (itd == fotg210->last_itd_to_free) 1248 break; 1249 } 1250 1251 if (!list_empty(&fotg210->cached_itd_list)) 1252 start_free_itds(fotg210); 1253} 1254 1255 1256/* Handle lost (or very late) IAA interrupts */ 1257static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210) 1258{ 1259 if (fotg210->rh_state != FOTG210_RH_RUNNING) 1260 return; 1261 1262 /* 1263 * Lost IAA irqs wedge things badly; seen first with a vt8235. 1264 * So we need this watchdog, but must protect it against both 1265 * (a) SMP races against real IAA firing and retriggering, and 1266 * (b) clean HC shutdown, when IAA watchdog was pending. 1267 */ 1268 if (fotg210->async_iaa) { 1269 u32 cmd, status; 1270 1271 /* If we get here, IAA is *REALLY* late. It's barely 1272 * conceivable that the system is so busy that CMD_IAAD 1273 * is still legitimately set, so let's be sure it's 1274 * clear before we read STS_IAA. (The HC should clear 1275 * CMD_IAAD when it sets STS_IAA.) 1276 */ 1277 cmd = fotg210_readl(fotg210, &fotg210->regs->command); 1278 1279 /* 1280 * If IAA is set here it either legitimately triggered 1281 * after the watchdog timer expired (_way_ late, so we'll 1282 * still count it as lost) ... or a silicon erratum: 1283 * - VIA seems to set IAA without triggering the IRQ; 1284 * - IAAD potentially cleared without setting IAA. 1285 */ 1286 status = fotg210_readl(fotg210, &fotg210->regs->status); 1287 if ((status & STS_IAA) || !(cmd & CMD_IAAD)) { 1288 INCR(fotg210->stats.lost_iaa); 1289 fotg210_writel(fotg210, STS_IAA, 1290 &fotg210->regs->status); 1291 } 1292 1293 fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n", 1294 status, cmd); 1295 end_unlink_async(fotg210); 1296 } 1297} 1298 1299 1300/* Enable the I/O watchdog, if appropriate */ 1301static void turn_on_io_watchdog(struct fotg210_hcd *fotg210) 1302{ 1303 /* Not needed if the controller isn't running or it's already enabled */ 1304 if (fotg210->rh_state != FOTG210_RH_RUNNING || 1305 (fotg210->enabled_hrtimer_events & 1306 BIT(FOTG210_HRTIMER_IO_WATCHDOG))) 1307 return; 1308 1309 /* 1310 * Isochronous transfers always need the watchdog. 1311 * For other sorts we use it only if the flag is set. 1312 */ 1313 if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog && 1314 fotg210->async_count + fotg210->intr_count > 0)) 1315 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG, 1316 true); 1317} 1318 1319 1320/* Handler functions for the hrtimer event types. 1321 * Keep this array in the same order as the event types indexed by 1322 * enum fotg210_hrtimer_event in fotg210.h. 1323 */ 1324static void (*event_handlers[])(struct fotg210_hcd *) = { 1325 fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */ 1326 fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */ 1327 fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */ 1328 fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */ 1329 end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */ 1330 unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */ 1331 fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */ 1332 fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */ 1333 fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */ 1334 fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */ 1335}; 1336 1337static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t) 1338{ 1339 struct fotg210_hcd *fotg210 = 1340 container_of(t, struct fotg210_hcd, hrtimer); 1341 ktime_t now; 1342 unsigned long events; 1343 unsigned long flags; 1344 unsigned e; 1345 1346 spin_lock_irqsave(&fotg210->lock, flags); 1347 1348 events = fotg210->enabled_hrtimer_events; 1349 fotg210->enabled_hrtimer_events = 0; 1350 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT; 1351 1352 /* 1353 * Check each pending event. If its time has expired, handle 1354 * the event; otherwise re-enable it. 1355 */ 1356 now = ktime_get(); 1357 for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) { 1358 if (ktime_compare(now, fotg210->hr_timeouts[e]) >= 0) 1359 event_handlers[e](fotg210); 1360 else 1361 fotg210_enable_event(fotg210, e, false); 1362 } 1363 1364 spin_unlock_irqrestore(&fotg210->lock, flags); 1365 return HRTIMER_NORESTART; 1366} 1367 1368#define fotg210_bus_suspend NULL 1369#define fotg210_bus_resume NULL 1370 1371static int check_reset_complete(struct fotg210_hcd *fotg210, int index, 1372 u32 __iomem *status_reg, int port_status) 1373{ 1374 if (!(port_status & PORT_CONNECT)) 1375 return port_status; 1376 1377 /* if reset finished and it's still not enabled -- handoff */ 1378 if (!(port_status & PORT_PE)) 1379 /* with integrated TT, there's nobody to hand it to! */ 1380 fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n", 1381 index + 1); 1382 else 1383 fotg210_dbg(fotg210, "port %d reset complete, port enabled\n", 1384 index + 1); 1385 1386 return port_status; 1387} 1388 1389 1390/* build "status change" packet (one or two bytes) from HC registers */ 1391 1392static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf) 1393{ 1394 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 1395 u32 temp, status; 1396 u32 mask; 1397 int retval = 1; 1398 unsigned long flags; 1399 1400 /* init status to no-changes */ 1401 buf[0] = 0; 1402 1403 /* Inform the core about resumes-in-progress by returning 1404 * a non-zero value even if there are no status changes. 1405 */ 1406 status = fotg210->resuming_ports; 1407 1408 mask = PORT_CSC | PORT_PEC; 1409 /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */ 1410 1411 /* no hub change reports (bit 0) for now (power, ...) */ 1412 1413 /* port N changes (bit N)? */ 1414 spin_lock_irqsave(&fotg210->lock, flags); 1415 1416 temp = fotg210_readl(fotg210, &fotg210->regs->port_status); 1417 1418 /* 1419 * Return status information even for ports with OWNER set. 1420 * Otherwise hub_wq wouldn't see the disconnect event when a 1421 * high-speed device is switched over to the companion 1422 * controller by the user. 1423 */ 1424 1425 if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend) || 1426 (fotg210->reset_done[0] && 1427 time_after_eq(jiffies, fotg210->reset_done[0]))) { 1428 buf[0] |= 1 << 1; 1429 status = STS_PCD; 1430 } 1431 /* FIXME autosuspend idle root hubs */ 1432 spin_unlock_irqrestore(&fotg210->lock, flags); 1433 return status ? retval : 0; 1434} 1435 1436static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210, 1437 struct usb_hub_descriptor *desc) 1438{ 1439 int ports = HCS_N_PORTS(fotg210->hcs_params); 1440 u16 temp; 1441 1442 desc->bDescriptorType = USB_DT_HUB; 1443 desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */ 1444 desc->bHubContrCurrent = 0; 1445 1446 desc->bNbrPorts = ports; 1447 temp = 1 + (ports / 8); 1448 desc->bDescLength = 7 + 2 * temp; 1449 1450 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */ 1451 memset(&desc->u.hs.DeviceRemovable[0], 0, temp); 1452 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp); 1453 1454 temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */ 1455 temp |= HUB_CHAR_NO_LPSM; /* no power switching */ 1456 desc->wHubCharacteristics = cpu_to_le16(temp); 1457} 1458 1459static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, 1460 u16 wIndex, char *buf, u16 wLength) 1461{ 1462 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 1463 int ports = HCS_N_PORTS(fotg210->hcs_params); 1464 u32 __iomem *status_reg = &fotg210->regs->port_status; 1465 u32 temp, temp1, status; 1466 unsigned long flags; 1467 int retval = 0; 1468 unsigned selector; 1469 1470 /* 1471 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR. 1472 * HCS_INDICATOR may say we can change LEDs to off/amber/green. 1473 * (track current state ourselves) ... blink for diagnostics, 1474 * power, "this is the one", etc. EHCI spec supports this. 1475 */ 1476 1477 spin_lock_irqsave(&fotg210->lock, flags); 1478 switch (typeReq) { 1479 case ClearHubFeature: 1480 switch (wValue) { 1481 case C_HUB_LOCAL_POWER: 1482 case C_HUB_OVER_CURRENT: 1483 /* no hub-wide feature/status flags */ 1484 break; 1485 default: 1486 goto error; 1487 } 1488 break; 1489 case ClearPortFeature: 1490 if (!wIndex || wIndex > ports) 1491 goto error; 1492 wIndex--; 1493 temp = fotg210_readl(fotg210, status_reg); 1494 temp &= ~PORT_RWC_BITS; 1495 1496 /* 1497 * Even if OWNER is set, so the port is owned by the 1498 * companion controller, hub_wq needs to be able to clear 1499 * the port-change status bits (especially 1500 * USB_PORT_STAT_C_CONNECTION). 1501 */ 1502 1503 switch (wValue) { 1504 case USB_PORT_FEAT_ENABLE: 1505 fotg210_writel(fotg210, temp & ~PORT_PE, status_reg); 1506 break; 1507 case USB_PORT_FEAT_C_ENABLE: 1508 fotg210_writel(fotg210, temp | PORT_PEC, status_reg); 1509 break; 1510 case USB_PORT_FEAT_SUSPEND: 1511 if (temp & PORT_RESET) 1512 goto error; 1513 if (!(temp & PORT_SUSPEND)) 1514 break; 1515 if ((temp & PORT_PE) == 0) 1516 goto error; 1517 1518 /* resume signaling for 20 msec */ 1519 fotg210_writel(fotg210, temp | PORT_RESUME, status_reg); 1520 fotg210->reset_done[wIndex] = jiffies 1521 + msecs_to_jiffies(USB_RESUME_TIMEOUT); 1522 break; 1523 case USB_PORT_FEAT_C_SUSPEND: 1524 clear_bit(wIndex, &fotg210->port_c_suspend); 1525 break; 1526 case USB_PORT_FEAT_C_CONNECTION: 1527 fotg210_writel(fotg210, temp | PORT_CSC, status_reg); 1528 break; 1529 case USB_PORT_FEAT_C_OVER_CURRENT: 1530 fotg210_writel(fotg210, temp | OTGISR_OVC, 1531 &fotg210->regs->otgisr); 1532 break; 1533 case USB_PORT_FEAT_C_RESET: 1534 /* GetPortStatus clears reset */ 1535 break; 1536 default: 1537 goto error; 1538 } 1539 fotg210_readl(fotg210, &fotg210->regs->command); 1540 break; 1541 case GetHubDescriptor: 1542 fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *) 1543 buf); 1544 break; 1545 case GetHubStatus: 1546 /* no hub-wide feature/status flags */ 1547 memset(buf, 0, 4); 1548 /*cpu_to_le32s ((u32 *) buf); */ 1549 break; 1550 case GetPortStatus: 1551 if (!wIndex || wIndex > ports) 1552 goto error; 1553 wIndex--; 1554 status = 0; 1555 temp = fotg210_readl(fotg210, status_reg); 1556 1557 /* wPortChange bits */ 1558 if (temp & PORT_CSC) 1559 status |= USB_PORT_STAT_C_CONNECTION << 16; 1560 if (temp & PORT_PEC) 1561 status |= USB_PORT_STAT_C_ENABLE << 16; 1562 1563 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr); 1564 if (temp1 & OTGISR_OVC) 1565 status |= USB_PORT_STAT_C_OVERCURRENT << 16; 1566 1567 /* whoever resumes must GetPortStatus to complete it!! */ 1568 if (temp & PORT_RESUME) { 1569 1570 /* Remote Wakeup received? */ 1571 if (!fotg210->reset_done[wIndex]) { 1572 /* resume signaling for 20 msec */ 1573 fotg210->reset_done[wIndex] = jiffies 1574 + msecs_to_jiffies(20); 1575 /* check the port again */ 1576 mod_timer(&fotg210_to_hcd(fotg210)->rh_timer, 1577 fotg210->reset_done[wIndex]); 1578 } 1579 1580 /* resume completed? */ 1581 else if (time_after_eq(jiffies, 1582 fotg210->reset_done[wIndex])) { 1583 clear_bit(wIndex, &fotg210->suspended_ports); 1584 set_bit(wIndex, &fotg210->port_c_suspend); 1585 fotg210->reset_done[wIndex] = 0; 1586 1587 /* stop resume signaling */ 1588 temp = fotg210_readl(fotg210, status_reg); 1589 fotg210_writel(fotg210, temp & 1590 ~(PORT_RWC_BITS | PORT_RESUME), 1591 status_reg); 1592 clear_bit(wIndex, &fotg210->resuming_ports); 1593 retval = handshake(fotg210, status_reg, 1594 PORT_RESUME, 0, 2000);/* 2ms */ 1595 if (retval != 0) { 1596 fotg210_err(fotg210, 1597 "port %d resume error %d\n", 1598 wIndex + 1, retval); 1599 goto error; 1600 } 1601 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10)); 1602 } 1603 } 1604 1605 /* whoever resets must GetPortStatus to complete it!! */ 1606 if ((temp & PORT_RESET) && time_after_eq(jiffies, 1607 fotg210->reset_done[wIndex])) { 1608 status |= USB_PORT_STAT_C_RESET << 16; 1609 fotg210->reset_done[wIndex] = 0; 1610 clear_bit(wIndex, &fotg210->resuming_ports); 1611 1612 /* force reset to complete */ 1613 fotg210_writel(fotg210, 1614 temp & ~(PORT_RWC_BITS | PORT_RESET), 1615 status_reg); 1616 /* REVISIT: some hardware needs 550+ usec to clear 1617 * this bit; seems too long to spin routinely... 1618 */ 1619 retval = handshake(fotg210, status_reg, 1620 PORT_RESET, 0, 1000); 1621 if (retval != 0) { 1622 fotg210_err(fotg210, "port %d reset error %d\n", 1623 wIndex + 1, retval); 1624 goto error; 1625 } 1626 1627 /* see what we found out */ 1628 temp = check_reset_complete(fotg210, wIndex, status_reg, 1629 fotg210_readl(fotg210, status_reg)); 1630 1631 /* restart schedule */ 1632 fotg210->command |= CMD_RUN; 1633 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command); 1634 } 1635 1636 if (!(temp & (PORT_RESUME|PORT_RESET))) { 1637 fotg210->reset_done[wIndex] = 0; 1638 clear_bit(wIndex, &fotg210->resuming_ports); 1639 } 1640 1641 /* transfer dedicated ports to the companion hc */ 1642 if ((temp & PORT_CONNECT) && 1643 test_bit(wIndex, &fotg210->companion_ports)) { 1644 temp &= ~PORT_RWC_BITS; 1645 fotg210_writel(fotg210, temp, status_reg); 1646 fotg210_dbg(fotg210, "port %d --> companion\n", 1647 wIndex + 1); 1648 temp = fotg210_readl(fotg210, status_reg); 1649 } 1650 1651 /* 1652 * Even if OWNER is set, there's no harm letting hub_wq 1653 * see the wPortStatus values (they should all be 0 except 1654 * for PORT_POWER anyway). 1655 */ 1656 1657 if (temp & PORT_CONNECT) { 1658 status |= USB_PORT_STAT_CONNECTION; 1659 status |= fotg210_port_speed(fotg210, temp); 1660 } 1661 if (temp & PORT_PE) 1662 status |= USB_PORT_STAT_ENABLE; 1663 1664 /* maybe the port was unsuspended without our knowledge */ 1665 if (temp & (PORT_SUSPEND|PORT_RESUME)) { 1666 status |= USB_PORT_STAT_SUSPEND; 1667 } else if (test_bit(wIndex, &fotg210->suspended_ports)) { 1668 clear_bit(wIndex, &fotg210->suspended_ports); 1669 clear_bit(wIndex, &fotg210->resuming_ports); 1670 fotg210->reset_done[wIndex] = 0; 1671 if (temp & PORT_PE) 1672 set_bit(wIndex, &fotg210->port_c_suspend); 1673 } 1674 1675 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr); 1676 if (temp1 & OTGISR_OVC) 1677 status |= USB_PORT_STAT_OVERCURRENT; 1678 if (temp & PORT_RESET) 1679 status |= USB_PORT_STAT_RESET; 1680 if (test_bit(wIndex, &fotg210->port_c_suspend)) 1681 status |= USB_PORT_STAT_C_SUSPEND << 16; 1682 1683 if (status & ~0xffff) /* only if wPortChange is interesting */ 1684 dbg_port(fotg210, "GetStatus", wIndex + 1, temp); 1685 put_unaligned_le32(status, buf); 1686 break; 1687 case SetHubFeature: 1688 switch (wValue) { 1689 case C_HUB_LOCAL_POWER: 1690 case C_HUB_OVER_CURRENT: 1691 /* no hub-wide feature/status flags */ 1692 break; 1693 default: 1694 goto error; 1695 } 1696 break; 1697 case SetPortFeature: 1698 selector = wIndex >> 8; 1699 wIndex &= 0xff; 1700 1701 if (!wIndex || wIndex > ports) 1702 goto error; 1703 wIndex--; 1704 temp = fotg210_readl(fotg210, status_reg); 1705 temp &= ~PORT_RWC_BITS; 1706 switch (wValue) { 1707 case USB_PORT_FEAT_SUSPEND: 1708 if ((temp & PORT_PE) == 0 1709 || (temp & PORT_RESET) != 0) 1710 goto error; 1711 1712 /* After above check the port must be connected. 1713 * Set appropriate bit thus could put phy into low power 1714 * mode if we have hostpc feature 1715 */ 1716 fotg210_writel(fotg210, temp | PORT_SUSPEND, 1717 status_reg); 1718 set_bit(wIndex, &fotg210->suspended_ports); 1719 break; 1720 case USB_PORT_FEAT_RESET: 1721 if (temp & PORT_RESUME) 1722 goto error; 1723 /* line status bits may report this as low speed, 1724 * which can be fine if this root hub has a 1725 * transaction translator built in. 1726 */ 1727 fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1); 1728 temp |= PORT_RESET; 1729 temp &= ~PORT_PE; 1730 1731 /* 1732 * caller must wait, then call GetPortStatus 1733 * usb 2.0 spec says 50 ms resets on root 1734 */ 1735 fotg210->reset_done[wIndex] = jiffies 1736 + msecs_to_jiffies(50); 1737 fotg210_writel(fotg210, temp, status_reg); 1738 break; 1739 1740 /* For downstream facing ports (these): one hub port is put 1741 * into test mode according to USB2 11.24.2.13, then the hub 1742 * must be reset (which for root hub now means rmmod+modprobe, 1743 * or else system reboot). See EHCI 2.3.9 and 4.14 for info 1744 * about the EHCI-specific stuff. 1745 */ 1746 case USB_PORT_FEAT_TEST: 1747 if (!selector || selector > 5) 1748 goto error; 1749 spin_unlock_irqrestore(&fotg210->lock, flags); 1750 fotg210_quiesce(fotg210); 1751 spin_lock_irqsave(&fotg210->lock, flags); 1752 1753 /* Put all enabled ports into suspend */ 1754 temp = fotg210_readl(fotg210, status_reg) & 1755 ~PORT_RWC_BITS; 1756 if (temp & PORT_PE) 1757 fotg210_writel(fotg210, temp | PORT_SUSPEND, 1758 status_reg); 1759 1760 spin_unlock_irqrestore(&fotg210->lock, flags); 1761 fotg210_halt(fotg210); 1762 spin_lock_irqsave(&fotg210->lock, flags); 1763 1764 temp = fotg210_readl(fotg210, status_reg); 1765 temp |= selector << 16; 1766 fotg210_writel(fotg210, temp, status_reg); 1767 break; 1768 1769 default: 1770 goto error; 1771 } 1772 fotg210_readl(fotg210, &fotg210->regs->command); 1773 break; 1774 1775 default: 1776error: 1777 /* "stall" on error */ 1778 retval = -EPIPE; 1779 } 1780 spin_unlock_irqrestore(&fotg210->lock, flags); 1781 return retval; 1782} 1783 1784static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd, 1785 int portnum) 1786{ 1787 return; 1788} 1789 1790static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd, 1791 int portnum) 1792{ 1793 return 0; 1794} 1795 1796/* There's basically three types of memory: 1797 * - data used only by the HCD ... kmalloc is fine 1798 * - async and periodic schedules, shared by HC and HCD ... these 1799 * need to use dma_pool or dma_alloc_coherent 1800 * - driver buffers, read/written by HC ... single shot DMA mapped 1801 * 1802 * There's also "register" data (e.g. PCI or SOC), which is memory mapped. 1803 * No memory seen by this driver is pageable. 1804 */ 1805 1806/* Allocate the key transfer structures from the previously allocated pool */ 1807static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210, 1808 struct fotg210_qtd *qtd, dma_addr_t dma) 1809{ 1810 memset(qtd, 0, sizeof(*qtd)); 1811 qtd->qtd_dma = dma; 1812 qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT); 1813 qtd->hw_next = FOTG210_LIST_END(fotg210); 1814 qtd->hw_alt_next = FOTG210_LIST_END(fotg210); 1815 INIT_LIST_HEAD(&qtd->qtd_list); 1816} 1817 1818static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210, 1819 gfp_t flags) 1820{ 1821 struct fotg210_qtd *qtd; 1822 dma_addr_t dma; 1823 1824 qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma); 1825 if (qtd != NULL) 1826 fotg210_qtd_init(fotg210, qtd, dma); 1827 1828 return qtd; 1829} 1830 1831static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210, 1832 struct fotg210_qtd *qtd) 1833{ 1834 dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma); 1835} 1836 1837 1838static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh) 1839{ 1840 /* clean qtds first, and know this is not linked */ 1841 if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) { 1842 fotg210_dbg(fotg210, "unused qh not empty!\n"); 1843 BUG(); 1844 } 1845 if (qh->dummy) 1846 fotg210_qtd_free(fotg210, qh->dummy); 1847 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma); 1848 kfree(qh); 1849} 1850 1851static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210, 1852 gfp_t flags) 1853{ 1854 struct fotg210_qh *qh; 1855 dma_addr_t dma; 1856 1857 qh = kzalloc(sizeof(*qh), GFP_ATOMIC); 1858 if (!qh) 1859 goto done; 1860 qh->hw = dma_pool_zalloc(fotg210->qh_pool, flags, &dma); 1861 if (!qh->hw) 1862 goto fail; 1863 qh->qh_dma = dma; 1864 INIT_LIST_HEAD(&qh->qtd_list); 1865 1866 /* dummy td enables safe urb queuing */ 1867 qh->dummy = fotg210_qtd_alloc(fotg210, flags); 1868 if (qh->dummy == NULL) { 1869 fotg210_dbg(fotg210, "no dummy td\n"); 1870 goto fail1; 1871 } 1872done: 1873 return qh; 1874fail1: 1875 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma); 1876fail: 1877 kfree(qh); 1878 return NULL; 1879} 1880 1881/* The queue heads and transfer descriptors are managed from pools tied 1882 * to each of the "per device" structures. 1883 * This is the initialisation and cleanup code. 1884 */ 1885 1886static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210) 1887{ 1888 if (fotg210->async) 1889 qh_destroy(fotg210, fotg210->async); 1890 fotg210->async = NULL; 1891 1892 if (fotg210->dummy) 1893 qh_destroy(fotg210, fotg210->dummy); 1894 fotg210->dummy = NULL; 1895 1896 /* DMA consistent memory and pools */ 1897 dma_pool_destroy(fotg210->qtd_pool); 1898 fotg210->qtd_pool = NULL; 1899 1900 dma_pool_destroy(fotg210->qh_pool); 1901 fotg210->qh_pool = NULL; 1902 1903 dma_pool_destroy(fotg210->itd_pool); 1904 fotg210->itd_pool = NULL; 1905 1906 if (fotg210->periodic) 1907 dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller, 1908 fotg210->periodic_size * sizeof(u32), 1909 fotg210->periodic, fotg210->periodic_dma); 1910 fotg210->periodic = NULL; 1911 1912 /* shadow periodic table */ 1913 kfree(fotg210->pshadow); 1914 fotg210->pshadow = NULL; 1915} 1916 1917/* remember to add cleanup code (above) if you add anything here */ 1918static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags) 1919{ 1920 int i; 1921 1922 /* QTDs for control/bulk/intr transfers */ 1923 fotg210->qtd_pool = dma_pool_create("fotg210_qtd", 1924 fotg210_to_hcd(fotg210)->self.controller, 1925 sizeof(struct fotg210_qtd), 1926 32 /* byte alignment (for hw parts) */, 1927 4096 /* can't cross 4K */); 1928 if (!fotg210->qtd_pool) 1929 goto fail; 1930 1931 /* QHs for control/bulk/intr transfers */ 1932 fotg210->qh_pool = dma_pool_create("fotg210_qh", 1933 fotg210_to_hcd(fotg210)->self.controller, 1934 sizeof(struct fotg210_qh_hw), 1935 32 /* byte alignment (for hw parts) */, 1936 4096 /* can't cross 4K */); 1937 if (!fotg210->qh_pool) 1938 goto fail; 1939 1940 fotg210->async = fotg210_qh_alloc(fotg210, flags); 1941 if (!fotg210->async) 1942 goto fail; 1943 1944 /* ITD for high speed ISO transfers */ 1945 fotg210->itd_pool = dma_pool_create("fotg210_itd", 1946 fotg210_to_hcd(fotg210)->self.controller, 1947 sizeof(struct fotg210_itd), 1948 64 /* byte alignment (for hw parts) */, 1949 4096 /* can't cross 4K */); 1950 if (!fotg210->itd_pool) 1951 goto fail; 1952 1953 /* Hardware periodic table */ 1954 fotg210->periodic = 1955 dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller, 1956 fotg210->periodic_size * sizeof(__le32), 1957 &fotg210->periodic_dma, 0); 1958 if (fotg210->periodic == NULL) 1959 goto fail; 1960 1961 for (i = 0; i < fotg210->periodic_size; i++) 1962 fotg210->periodic[i] = FOTG210_LIST_END(fotg210); 1963 1964 /* software shadow of hardware table */ 1965 fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *), 1966 flags); 1967 if (fotg210->pshadow != NULL) 1968 return 0; 1969 1970fail: 1971 fotg210_dbg(fotg210, "couldn't init memory\n"); 1972 fotg210_mem_cleanup(fotg210); 1973 return -ENOMEM; 1974} 1975/* EHCI hardware queue manipulation ... the core. QH/QTD manipulation. 1976 * 1977 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd" 1978 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned 1979 * buffers needed for the larger number). We use one QH per endpoint, queue 1980 * multiple urbs (all three types) per endpoint. URBs may need several qtds. 1981 * 1982 * ISO traffic uses "ISO TD" (itd) records, and (along with 1983 * interrupts) needs careful scheduling. Performance improvements can be 1984 * an ongoing challenge. That's in "ehci-sched.c". 1985 * 1986 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs, 1987 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using 1988 * (b) special fields in qh entries or (c) split iso entries. TTs will 1989 * buffer low/full speed data so the host collects it at high speed. 1990 */ 1991 1992/* fill a qtd, returning how much of the buffer we were able to queue up */ 1993static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd, 1994 dma_addr_t buf, size_t len, int token, int maxpacket) 1995{ 1996 int i, count; 1997 u64 addr = buf; 1998 1999 /* one buffer entry per 4K ... first might be short or unaligned */ 2000 qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr); 2001 qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32)); 2002 count = 0x1000 - (buf & 0x0fff); /* rest of that page */ 2003 if (likely(len < count)) /* ... iff needed */ 2004 count = len; 2005 else { 2006 buf += 0x1000; 2007 buf &= ~0x0fff; 2008 2009 /* per-qtd limit: from 16K to 20K (best alignment) */ 2010 for (i = 1; count < len && i < 5; i++) { 2011 addr = buf; 2012 qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr); 2013 qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210, 2014 (u32)(addr >> 32)); 2015 buf += 0x1000; 2016 if ((count + 0x1000) < len) 2017 count += 0x1000; 2018 else 2019 count = len; 2020 } 2021 2022 /* short packets may only terminate transfers */ 2023 if (count != len) 2024 count -= (count % maxpacket); 2025 } 2026 qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token); 2027 qtd->length = count; 2028 2029 return count; 2030} 2031 2032static inline void qh_update(struct fotg210_hcd *fotg210, 2033 struct fotg210_qh *qh, struct fotg210_qtd *qtd) 2034{ 2035 struct fotg210_qh_hw *hw = qh->hw; 2036 2037 /* writes to an active overlay are unsafe */ 2038 BUG_ON(qh->qh_state != QH_STATE_IDLE); 2039 2040 hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma); 2041 hw->hw_alt_next = FOTG210_LIST_END(fotg210); 2042 2043 /* Except for control endpoints, we make hardware maintain data 2044 * toggle (like OHCI) ... here (re)initialize the toggle in the QH, 2045 * and set the pseudo-toggle in udev. Only usb_clear_halt() will 2046 * ever clear it. 2047 */ 2048 if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) { 2049 unsigned is_out, epnum; 2050 2051 is_out = qh->is_out; 2052 epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f; 2053 if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) { 2054 hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE); 2055 usb_settoggle(qh->dev, epnum, is_out, 1); 2056 } 2057 } 2058 2059 hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING); 2060} 2061 2062/* if it weren't for a common silicon quirk (writing the dummy into the qh 2063 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault 2064 * recovery (including urb dequeue) would need software changes to a QH... 2065 */ 2066static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh) 2067{ 2068 struct fotg210_qtd *qtd; 2069 2070 if (list_empty(&qh->qtd_list)) 2071 qtd = qh->dummy; 2072 else { 2073 qtd = list_entry(qh->qtd_list.next, 2074 struct fotg210_qtd, qtd_list); 2075 /* 2076 * first qtd may already be partially processed. 2077 * If we come here during unlink, the QH overlay region 2078 * might have reference to the just unlinked qtd. The 2079 * qtd is updated in qh_completions(). Update the QH 2080 * overlay here. 2081 */ 2082 if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) { 2083 qh->hw->hw_qtd_next = qtd->hw_next; 2084 qtd = NULL; 2085 } 2086 } 2087 2088 if (qtd) 2089 qh_update(fotg210, qh, qtd); 2090} 2091 2092static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh); 2093 2094static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd, 2095 struct usb_host_endpoint *ep) 2096{ 2097 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 2098 struct fotg210_qh *qh = ep->hcpriv; 2099 unsigned long flags; 2100 2101 spin_lock_irqsave(&fotg210->lock, flags); 2102 qh->clearing_tt = 0; 2103 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list) 2104 && fotg210->rh_state == FOTG210_RH_RUNNING) 2105 qh_link_async(fotg210, qh); 2106 spin_unlock_irqrestore(&fotg210->lock, flags); 2107} 2108 2109static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210, 2110 struct fotg210_qh *qh, struct urb *urb, u32 token) 2111{ 2112 2113 /* If an async split transaction gets an error or is unlinked, 2114 * the TT buffer may be left in an indeterminate state. We 2115 * have to clear the TT buffer. 2116 * 2117 * Note: this routine is never called for Isochronous transfers. 2118 */ 2119 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) { 2120 struct usb_device *tt = urb->dev->tt->hub; 2121 2122 dev_dbg(&tt->dev, 2123 "clear tt buffer port %d, a%d ep%d t%08x\n", 2124 urb->dev->ttport, urb->dev->devnum, 2125 usb_pipeendpoint(urb->pipe), token); 2126 2127 if (urb->dev->tt->hub != 2128 fotg210_to_hcd(fotg210)->self.root_hub) { 2129 if (usb_hub_clear_tt_buffer(urb) == 0) 2130 qh->clearing_tt = 1; 2131 } 2132 } 2133} 2134 2135static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb, 2136 size_t length, u32 token) 2137{ 2138 int status = -EINPROGRESS; 2139 2140 /* count IN/OUT bytes, not SETUP (even short packets) */ 2141 if (likely(QTD_PID(token) != 2)) 2142 urb->actual_length += length - QTD_LENGTH(token); 2143 2144 /* don't modify error codes */ 2145 if (unlikely(urb->unlinked)) 2146 return status; 2147 2148 /* force cleanup after short read; not always an error */ 2149 if (unlikely(IS_SHORT_READ(token))) 2150 status = -EREMOTEIO; 2151 2152 /* serious "can't proceed" faults reported by the hardware */ 2153 if (token & QTD_STS_HALT) { 2154 if (token & QTD_STS_BABBLE) { 2155 /* FIXME "must" disable babbling device's port too */ 2156 status = -EOVERFLOW; 2157 /* CERR nonzero + halt --> stall */ 2158 } else if (QTD_CERR(token)) { 2159 status = -EPIPE; 2160 2161 /* In theory, more than one of the following bits can be set 2162 * since they are sticky and the transaction is retried. 2163 * Which to test first is rather arbitrary. 2164 */ 2165 } else if (token & QTD_STS_MMF) { 2166 /* fs/ls interrupt xfer missed the complete-split */ 2167 status = -EPROTO; 2168 } else if (token & QTD_STS_DBE) { 2169 status = (QTD_PID(token) == 1) /* IN ? */ 2170 ? -ENOSR /* hc couldn't read data */ 2171 : -ECOMM; /* hc couldn't write data */ 2172 } else if (token & QTD_STS_XACT) { 2173 /* timeout, bad CRC, wrong PID, etc */ 2174 fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n", 2175 urb->dev->devpath, 2176 usb_pipeendpoint(urb->pipe), 2177 usb_pipein(urb->pipe) ? "in" : "out"); 2178 status = -EPROTO; 2179 } else { /* unknown */ 2180 status = -EPROTO; 2181 } 2182 2183 fotg210_dbg(fotg210, 2184 "dev%d ep%d%s qtd token %08x --> status %d\n", 2185 usb_pipedevice(urb->pipe), 2186 usb_pipeendpoint(urb->pipe), 2187 usb_pipein(urb->pipe) ? "in" : "out", 2188 token, status); 2189 } 2190 2191 return status; 2192} 2193 2194static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb, 2195 int status) 2196__releases(fotg210->lock) 2197__acquires(fotg210->lock) 2198{ 2199 if (likely(urb->hcpriv != NULL)) { 2200 struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv; 2201 2202 /* S-mask in a QH means it's an interrupt urb */ 2203 if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) { 2204 2205 /* ... update hc-wide periodic stats (for usbfs) */ 2206 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--; 2207 } 2208 } 2209 2210 if (unlikely(urb->unlinked)) { 2211 INCR(fotg210->stats.unlink); 2212 } else { 2213 /* report non-error and short read status as zero */ 2214 if (status == -EINPROGRESS || status == -EREMOTEIO) 2215 status = 0; 2216 INCR(fotg210->stats.complete); 2217 } 2218 2219#ifdef FOTG210_URB_TRACE 2220 fotg210_dbg(fotg210, 2221 "%s %s urb %p ep%d%s status %d len %d/%d\n", 2222 __func__, urb->dev->devpath, urb, 2223 usb_pipeendpoint(urb->pipe), 2224 usb_pipein(urb->pipe) ? "in" : "out", 2225 status, 2226 urb->actual_length, urb->transfer_buffer_length); 2227#endif 2228 2229 /* complete() can reenter this HCD */ 2230 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb); 2231 spin_unlock(&fotg210->lock); 2232 usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status); 2233 spin_lock(&fotg210->lock); 2234} 2235 2236static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh); 2237 2238/* Process and free completed qtds for a qh, returning URBs to drivers. 2239 * Chases up to qh->hw_current. Returns number of completions called, 2240 * indicating how much "real" work we did. 2241 */ 2242static unsigned qh_completions(struct fotg210_hcd *fotg210, 2243 struct fotg210_qh *qh) 2244{ 2245 struct fotg210_qtd *last, *end = qh->dummy; 2246 struct fotg210_qtd *qtd, *tmp; 2247 int last_status; 2248 int stopped; 2249 unsigned count = 0; 2250 u8 state; 2251 struct fotg210_qh_hw *hw = qh->hw; 2252 2253 if (unlikely(list_empty(&qh->qtd_list))) 2254 return count; 2255 2256 /* completions (or tasks on other cpus) must never clobber HALT 2257 * till we've gone through and cleaned everything up, even when 2258 * they add urbs to this qh's queue or mark them for unlinking. 2259 * 2260 * NOTE: unlinking expects to be done in queue order. 2261 * 2262 * It's a bug for qh->qh_state to be anything other than 2263 * QH_STATE_IDLE, unless our caller is scan_async() or 2264 * scan_intr(). 2265 */ 2266 state = qh->qh_state; 2267 qh->qh_state = QH_STATE_COMPLETING; 2268 stopped = (state == QH_STATE_IDLE); 2269 2270rescan: 2271 last = NULL; 2272 last_status = -EINPROGRESS; 2273 qh->needs_rescan = 0; 2274 2275 /* remove de-activated QTDs from front of queue. 2276 * after faults (including short reads), cleanup this urb 2277 * then let the queue advance. 2278 * if queue is stopped, handles unlinks. 2279 */ 2280 list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) { 2281 struct urb *urb; 2282 u32 token = 0; 2283 2284 urb = qtd->urb; 2285 2286 /* clean up any state from previous QTD ...*/ 2287 if (last) { 2288 if (likely(last->urb != urb)) { 2289 fotg210_urb_done(fotg210, last->urb, 2290 last_status); 2291 count++; 2292 last_status = -EINPROGRESS; 2293 } 2294 fotg210_qtd_free(fotg210, last); 2295 last = NULL; 2296 } 2297 2298 /* ignore urbs submitted during completions we reported */ 2299 if (qtd == end) 2300 break; 2301 2302 /* hardware copies qtd out of qh overlay */ 2303 rmb(); 2304 token = hc32_to_cpu(fotg210, qtd->hw_token); 2305 2306 /* always clean up qtds the hc de-activated */ 2307retry_xacterr: 2308 if ((token & QTD_STS_ACTIVE) == 0) { 2309 2310 /* Report Data Buffer Error: non-fatal but useful */ 2311 if (token & QTD_STS_DBE) 2312 fotg210_dbg(fotg210, 2313 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n", 2314 urb, usb_endpoint_num(&urb->ep->desc), 2315 usb_endpoint_dir_in(&urb->ep->desc) 2316 ? "in" : "out", 2317 urb->transfer_buffer_length, qtd, qh); 2318 2319 /* on STALL, error, and short reads this urb must 2320 * complete and all its qtds must be recycled. 2321 */ 2322 if ((token & QTD_STS_HALT) != 0) { 2323 2324 /* retry transaction errors until we 2325 * reach the software xacterr limit 2326 */ 2327 if ((token & QTD_STS_XACT) && 2328 QTD_CERR(token) == 0 && 2329 ++qh->xacterrs < QH_XACTERR_MAX && 2330 !urb->unlinked) { 2331 fotg210_dbg(fotg210, 2332 "detected XactErr len %zu/%zu retry %d\n", 2333 qtd->length - QTD_LENGTH(token), 2334 qtd->length, 2335 qh->xacterrs); 2336 2337 /* reset the token in the qtd and the 2338 * qh overlay (which still contains 2339 * the qtd) so that we pick up from 2340 * where we left off 2341 */ 2342 token &= ~QTD_STS_HALT; 2343 token |= QTD_STS_ACTIVE | 2344 (FOTG210_TUNE_CERR << 10); 2345 qtd->hw_token = cpu_to_hc32(fotg210, 2346 token); 2347 wmb(); 2348 hw->hw_token = cpu_to_hc32(fotg210, 2349 token); 2350 goto retry_xacterr; 2351 } 2352 stopped = 1; 2353 2354 /* magic dummy for some short reads; qh won't advance. 2355 * that silicon quirk can kick in with this dummy too. 2356 * 2357 * other short reads won't stop the queue, including 2358 * control transfers (status stage handles that) or 2359 * most other single-qtd reads ... the queue stops if 2360 * URB_SHORT_NOT_OK was set so the driver submitting 2361 * the urbs could clean it up. 2362 */ 2363 } else if (IS_SHORT_READ(token) && 2364 !(qtd->hw_alt_next & 2365 FOTG210_LIST_END(fotg210))) { 2366 stopped = 1; 2367 } 2368 2369 /* stop scanning when we reach qtds the hc is using */ 2370 } else if (likely(!stopped 2371 && fotg210->rh_state >= FOTG210_RH_RUNNING)) { 2372 break; 2373 2374 /* scan the whole queue for unlinks whenever it stops */ 2375 } else { 2376 stopped = 1; 2377 2378 /* cancel everything if we halt, suspend, etc */ 2379 if (fotg210->rh_state < FOTG210_RH_RUNNING) 2380 last_status = -ESHUTDOWN; 2381 2382 /* this qtd is active; skip it unless a previous qtd 2383 * for its urb faulted, or its urb was canceled. 2384 */ 2385 else if (last_status == -EINPROGRESS && !urb->unlinked) 2386 continue; 2387 2388 /* qh unlinked; token in overlay may be most current */ 2389 if (state == QH_STATE_IDLE && 2390 cpu_to_hc32(fotg210, qtd->qtd_dma) 2391 == hw->hw_current) { 2392 token = hc32_to_cpu(fotg210, hw->hw_token); 2393 2394 /* An unlink may leave an incomplete 2395 * async transaction in the TT buffer. 2396 * We have to clear it. 2397 */ 2398 fotg210_clear_tt_buffer(fotg210, qh, urb, 2399 token); 2400 } 2401 } 2402 2403 /* unless we already know the urb's status, collect qtd status 2404 * and update count of bytes transferred. in common short read 2405 * cases with only one data qtd (including control transfers), 2406 * queue processing won't halt. but with two or more qtds (for 2407 * example, with a 32 KB transfer), when the first qtd gets a 2408 * short read the second must be removed by hand. 2409 */ 2410 if (last_status == -EINPROGRESS) { 2411 last_status = qtd_copy_status(fotg210, urb, 2412 qtd->length, token); 2413 if (last_status == -EREMOTEIO && 2414 (qtd->hw_alt_next & 2415 FOTG210_LIST_END(fotg210))) 2416 last_status = -EINPROGRESS; 2417 2418 /* As part of low/full-speed endpoint-halt processing 2419 * we must clear the TT buffer (11.17.5). 2420 */ 2421 if (unlikely(last_status != -EINPROGRESS && 2422 last_status != -EREMOTEIO)) { 2423 /* The TT's in some hubs malfunction when they 2424 * receive this request following a STALL (they 2425 * stop sending isochronous packets). Since a 2426 * STALL can't leave the TT buffer in a busy 2427 * state (if you believe Figures 11-48 - 11-51 2428 * in the USB 2.0 spec), we won't clear the TT 2429 * buffer in this case. Strictly speaking this 2430 * is a violation of the spec. 2431 */ 2432 if (last_status != -EPIPE) 2433 fotg210_clear_tt_buffer(fotg210, qh, 2434 urb, token); 2435 } 2436 } 2437 2438 /* if we're removing something not at the queue head, 2439 * patch the hardware queue pointer. 2440 */ 2441 if (stopped && qtd->qtd_list.prev != &qh->qtd_list) { 2442 last = list_entry(qtd->qtd_list.prev, 2443 struct fotg210_qtd, qtd_list); 2444 last->hw_next = qtd->hw_next; 2445 } 2446 2447 /* remove qtd; it's recycled after possible urb completion */ 2448 list_del(&qtd->qtd_list); 2449 last = qtd; 2450 2451 /* reinit the xacterr counter for the next qtd */ 2452 qh->xacterrs = 0; 2453 } 2454 2455 /* last urb's completion might still need calling */ 2456 if (likely(last != NULL)) { 2457 fotg210_urb_done(fotg210, last->urb, last_status); 2458 count++; 2459 fotg210_qtd_free(fotg210, last); 2460 } 2461 2462 /* Do we need to rescan for URBs dequeued during a giveback? */ 2463 if (unlikely(qh->needs_rescan)) { 2464 /* If the QH is already unlinked, do the rescan now. */ 2465 if (state == QH_STATE_IDLE) 2466 goto rescan; 2467 2468 /* Otherwise we have to wait until the QH is fully unlinked. 2469 * Our caller will start an unlink if qh->needs_rescan is 2470 * set. But if an unlink has already started, nothing needs 2471 * to be done. 2472 */ 2473 if (state != QH_STATE_LINKED) 2474 qh->needs_rescan = 0; 2475 } 2476 2477 /* restore original state; caller must unlink or relink */ 2478 qh->qh_state = state; 2479 2480 /* be sure the hardware's done with the qh before refreshing 2481 * it after fault cleanup, or recovering from silicon wrongly 2482 * overlaying the dummy qtd (which reduces DMA chatter). 2483 */ 2484 if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) { 2485 switch (state) { 2486 case QH_STATE_IDLE: 2487 qh_refresh(fotg210, qh); 2488 break; 2489 case QH_STATE_LINKED: 2490 /* We won't refresh a QH that's linked (after the HC 2491 * stopped the queue). That avoids a race: 2492 * - HC reads first part of QH; 2493 * - CPU updates that first part and the token; 2494 * - HC reads rest of that QH, including token 2495 * Result: HC gets an inconsistent image, and then 2496 * DMAs to/from the wrong memory (corrupting it). 2497 * 2498 * That should be rare for interrupt transfers, 2499 * except maybe high bandwidth ... 2500 */ 2501 2502 /* Tell the caller to start an unlink */ 2503 qh->needs_rescan = 1; 2504 break; 2505 /* otherwise, unlink already started */ 2506 } 2507 } 2508 2509 return count; 2510} 2511 2512/* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */ 2513#define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03)) 2514/* ... and packet size, for any kind of endpoint descriptor */ 2515#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff) 2516 2517/* reverse of qh_urb_transaction: free a list of TDs. 2518 * used for cleanup after errors, before HC sees an URB's TDs. 2519 */ 2520static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb, 2521 struct list_head *head) 2522{ 2523 struct fotg210_qtd *qtd, *temp; 2524 2525 list_for_each_entry_safe(qtd, temp, head, qtd_list) { 2526 list_del(&qtd->qtd_list); 2527 fotg210_qtd_free(fotg210, qtd); 2528 } 2529} 2530 2531/* create a list of filled qtds for this URB; won't link into qh. 2532 */ 2533static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210, 2534 struct urb *urb, struct list_head *head, gfp_t flags) 2535{ 2536 struct fotg210_qtd *qtd, *qtd_prev; 2537 dma_addr_t buf; 2538 int len, this_sg_len, maxpacket; 2539 int is_input; 2540 u32 token; 2541 int i; 2542 struct scatterlist *sg; 2543 2544 /* 2545 * URBs map to sequences of QTDs: one logical transaction 2546 */ 2547 qtd = fotg210_qtd_alloc(fotg210, flags); 2548 if (unlikely(!qtd)) 2549 return NULL; 2550 list_add_tail(&qtd->qtd_list, head); 2551 qtd->urb = urb; 2552 2553 token = QTD_STS_ACTIVE; 2554 token |= (FOTG210_TUNE_CERR << 10); 2555 /* for split transactions, SplitXState initialized to zero */ 2556 2557 len = urb->transfer_buffer_length; 2558 is_input = usb_pipein(urb->pipe); 2559 if (usb_pipecontrol(urb->pipe)) { 2560 /* SETUP pid */ 2561 qtd_fill(fotg210, qtd, urb->setup_dma, 2562 sizeof(struct usb_ctrlrequest), 2563 token | (2 /* "setup" */ << 8), 8); 2564 2565 /* ... and always at least one more pid */ 2566 token ^= QTD_TOGGLE; 2567 qtd_prev = qtd; 2568 qtd = fotg210_qtd_alloc(fotg210, flags); 2569 if (unlikely(!qtd)) 2570 goto cleanup; 2571 qtd->urb = urb; 2572 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma); 2573 list_add_tail(&qtd->qtd_list, head); 2574 2575 /* for zero length DATA stages, STATUS is always IN */ 2576 if (len == 0) 2577 token |= (1 /* "in" */ << 8); 2578 } 2579 2580 /* 2581 * data transfer stage: buffer setup 2582 */ 2583 i = urb->num_mapped_sgs; 2584 if (len > 0 && i > 0) { 2585 sg = urb->sg; 2586 buf = sg_dma_address(sg); 2587 2588 /* urb->transfer_buffer_length may be smaller than the 2589 * size of the scatterlist (or vice versa) 2590 */ 2591 this_sg_len = min_t(int, sg_dma_len(sg), len); 2592 } else { 2593 sg = NULL; 2594 buf = urb->transfer_dma; 2595 this_sg_len = len; 2596 } 2597 2598 if (is_input) 2599 token |= (1 /* "in" */ << 8); 2600 /* else it's already initted to "out" pid (0 << 8) */ 2601 2602 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input)); 2603 2604 /* 2605 * buffer gets wrapped in one or more qtds; 2606 * last one may be "short" (including zero len) 2607 * and may serve as a control status ack 2608 */ 2609 for (;;) { 2610 int this_qtd_len; 2611 2612 this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token, 2613 maxpacket); 2614 this_sg_len -= this_qtd_len; 2615 len -= this_qtd_len; 2616 buf += this_qtd_len; 2617 2618 /* 2619 * short reads advance to a "magic" dummy instead of the next 2620 * qtd ... that forces the queue to stop, for manual cleanup. 2621 * (this will usually be overridden later.) 2622 */ 2623 if (is_input) 2624 qtd->hw_alt_next = fotg210->async->hw->hw_alt_next; 2625 2626 /* qh makes control packets use qtd toggle; maybe switch it */ 2627 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0) 2628 token ^= QTD_TOGGLE; 2629 2630 if (likely(this_sg_len <= 0)) { 2631 if (--i <= 0 || len <= 0) 2632 break; 2633 sg = sg_next(sg); 2634 buf = sg_dma_address(sg); 2635 this_sg_len = min_t(int, sg_dma_len(sg), len); 2636 } 2637 2638 qtd_prev = qtd; 2639 qtd = fotg210_qtd_alloc(fotg210, flags); 2640 if (unlikely(!qtd)) 2641 goto cleanup; 2642 qtd->urb = urb; 2643 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma); 2644 list_add_tail(&qtd->qtd_list, head); 2645 } 2646 2647 /* 2648 * unless the caller requires manual cleanup after short reads, 2649 * have the alt_next mechanism keep the queue running after the 2650 * last data qtd (the only one, for control and most other cases). 2651 */ 2652 if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 || 2653 usb_pipecontrol(urb->pipe))) 2654 qtd->hw_alt_next = FOTG210_LIST_END(fotg210); 2655 2656 /* 2657 * control requests may need a terminating data "status" ack; 2658 * other OUT ones may need a terminating short packet 2659 * (zero length). 2660 */ 2661 if (likely(urb->transfer_buffer_length != 0)) { 2662 int one_more = 0; 2663 2664 if (usb_pipecontrol(urb->pipe)) { 2665 one_more = 1; 2666 token ^= 0x0100; /* "in" <--> "out" */ 2667 token |= QTD_TOGGLE; /* force DATA1 */ 2668 } else if (usb_pipeout(urb->pipe) 2669 && (urb->transfer_flags & URB_ZERO_PACKET) 2670 && !(urb->transfer_buffer_length % maxpacket)) { 2671 one_more = 1; 2672 } 2673 if (one_more) { 2674 qtd_prev = qtd; 2675 qtd = fotg210_qtd_alloc(fotg210, flags); 2676 if (unlikely(!qtd)) 2677 goto cleanup; 2678 qtd->urb = urb; 2679 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma); 2680 list_add_tail(&qtd->qtd_list, head); 2681 2682 /* never any data in such packets */ 2683 qtd_fill(fotg210, qtd, 0, 0, token, 0); 2684 } 2685 } 2686 2687 /* by default, enable interrupt on urb completion */ 2688 if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT))) 2689 qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC); 2690 return head; 2691 2692cleanup: 2693 qtd_list_free(fotg210, urb, head); 2694 return NULL; 2695} 2696 2697/* Would be best to create all qh's from config descriptors, 2698 * when each interface/altsetting is established. Unlink 2699 * any previous qh and cancel its urbs first; endpoints are 2700 * implicitly reset then (data toggle too). 2701 * That'd mean updating how usbcore talks to HCDs. (2.7?) 2702*/ 2703 2704 2705/* Each QH holds a qtd list; a QH is used for everything except iso. 2706 * 2707 * For interrupt urbs, the scheduler must set the microframe scheduling 2708 * mask(s) each time the QH gets scheduled. For highspeed, that's 2709 * just one microframe in the s-mask. For split interrupt transactions 2710 * there are additional complications: c-mask, maybe FSTNs. 2711 */ 2712static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb, 2713 gfp_t flags) 2714{ 2715 struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags); 2716 u32 info1 = 0, info2 = 0; 2717 int is_input, type; 2718 int maxp = 0; 2719 struct usb_tt *tt = urb->dev->tt; 2720 struct fotg210_qh_hw *hw; 2721 2722 if (!qh) 2723 return qh; 2724 2725 /* 2726 * init endpoint/device data for this QH 2727 */ 2728 info1 |= usb_pipeendpoint(urb->pipe) << 8; 2729 info1 |= usb_pipedevice(urb->pipe) << 0; 2730 2731 is_input = usb_pipein(urb->pipe); 2732 type = usb_pipetype(urb->pipe); 2733 maxp = usb_maxpacket(urb->dev, urb->pipe, !is_input); 2734 2735 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth 2736 * acts like up to 3KB, but is built from smaller packets. 2737 */ 2738 if (max_packet(maxp) > 1024) { 2739 fotg210_dbg(fotg210, "bogus qh maxpacket %d\n", 2740 max_packet(maxp)); 2741 goto done; 2742 } 2743 2744 /* Compute interrupt scheduling parameters just once, and save. 2745 * - allowing for high bandwidth, how many nsec/uframe are used? 2746 * - split transactions need a second CSPLIT uframe; same question 2747 * - splits also need a schedule gap (for full/low speed I/O) 2748 * - qh has a polling interval 2749 * 2750 * For control/bulk requests, the HC or TT handles these. 2751 */ 2752 if (type == PIPE_INTERRUPT) { 2753 qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH, 2754 is_input, 0, 2755 hb_mult(maxp) * max_packet(maxp))); 2756 qh->start = NO_FRAME; 2757 2758 if (urb->dev->speed == USB_SPEED_HIGH) { 2759 qh->c_usecs = 0; 2760 qh->gap_uf = 0; 2761 2762 qh->period = urb->interval >> 3; 2763 if (qh->period == 0 && urb->interval != 1) { 2764 /* NOTE interval 2 or 4 uframes could work. 2765 * But interval 1 scheduling is simpler, and 2766 * includes high bandwidth. 2767 */ 2768 urb->interval = 1; 2769 } else if (qh->period > fotg210->periodic_size) { 2770 qh->period = fotg210->periodic_size; 2771 urb->interval = qh->period << 3; 2772 } 2773 } else { 2774 int think_time; 2775 2776 /* gap is f(FS/LS transfer times) */ 2777 qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed, 2778 is_input, 0, maxp) / (125 * 1000); 2779 2780 /* FIXME this just approximates SPLIT/CSPLIT times */ 2781 if (is_input) { /* SPLIT, gap, CSPLIT+DATA */ 2782 qh->c_usecs = qh->usecs + HS_USECS(0); 2783 qh->usecs = HS_USECS(1); 2784 } else { /* SPLIT+DATA, gap, CSPLIT */ 2785 qh->usecs += HS_USECS(1); 2786 qh->c_usecs = HS_USECS(0); 2787 } 2788 2789 think_time = tt ? tt->think_time : 0; 2790 qh->tt_usecs = NS_TO_US(think_time + 2791 usb_calc_bus_time(urb->dev->speed, 2792 is_input, 0, max_packet(maxp))); 2793 qh->period = urb->interval; 2794 if (qh->period > fotg210->periodic_size) { 2795 qh->period = fotg210->periodic_size; 2796 urb->interval = qh->period; 2797 } 2798 } 2799 } 2800 2801 /* support for tt scheduling, and access to toggles */ 2802 qh->dev = urb->dev; 2803 2804 /* using TT? */ 2805 switch (urb->dev->speed) { 2806 case USB_SPEED_LOW: 2807 info1 |= QH_LOW_SPEED; 2808 fallthrough; 2809 2810 case USB_SPEED_FULL: 2811 /* EPS 0 means "full" */ 2812 if (type != PIPE_INTERRUPT) 2813 info1 |= (FOTG210_TUNE_RL_TT << 28); 2814 if (type == PIPE_CONTROL) { 2815 info1 |= QH_CONTROL_EP; /* for TT */ 2816 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */ 2817 } 2818 info1 |= maxp << 16; 2819 2820 info2 |= (FOTG210_TUNE_MULT_TT << 30); 2821 2822 /* Some Freescale processors have an erratum in which the 2823 * port number in the queue head was 0..N-1 instead of 1..N. 2824 */ 2825 if (fotg210_has_fsl_portno_bug(fotg210)) 2826 info2 |= (urb->dev->ttport-1) << 23; 2827 else 2828 info2 |= urb->dev->ttport << 23; 2829 2830 /* set the address of the TT; for TDI's integrated 2831 * root hub tt, leave it zeroed. 2832 */ 2833 if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub) 2834 info2 |= tt->hub->devnum << 16; 2835 2836 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */ 2837 2838 break; 2839 2840 case USB_SPEED_HIGH: /* no TT involved */ 2841 info1 |= QH_HIGH_SPEED; 2842 if (type == PIPE_CONTROL) { 2843 info1 |= (FOTG210_TUNE_RL_HS << 28); 2844 info1 |= 64 << 16; /* usb2 fixed maxpacket */ 2845 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */ 2846 info2 |= (FOTG210_TUNE_MULT_HS << 30); 2847 } else if (type == PIPE_BULK) { 2848 info1 |= (FOTG210_TUNE_RL_HS << 28); 2849 /* The USB spec says that high speed bulk endpoints 2850 * always use 512 byte maxpacket. But some device 2851 * vendors decided to ignore that, and MSFT is happy 2852 * to help them do so. So now people expect to use 2853 * such nonconformant devices with Linux too; sigh. 2854 */ 2855 info1 |= max_packet(maxp) << 16; 2856 info2 |= (FOTG210_TUNE_MULT_HS << 30); 2857 } else { /* PIPE_INTERRUPT */ 2858 info1 |= max_packet(maxp) << 16; 2859 info2 |= hb_mult(maxp) << 30; 2860 } 2861 break; 2862 default: 2863 fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev, 2864 urb->dev->speed); 2865done: 2866 qh_destroy(fotg210, qh); 2867 return NULL; 2868 } 2869 2870 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */ 2871 2872 /* init as live, toggle clear, advance to dummy */ 2873 qh->qh_state = QH_STATE_IDLE; 2874 hw = qh->hw; 2875 hw->hw_info1 = cpu_to_hc32(fotg210, info1); 2876 hw->hw_info2 = cpu_to_hc32(fotg210, info2); 2877 qh->is_out = !is_input; 2878 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1); 2879 qh_refresh(fotg210, qh); 2880 return qh; 2881} 2882 2883static void enable_async(struct fotg210_hcd *fotg210) 2884{ 2885 if (fotg210->async_count++) 2886 return; 2887 2888 /* Stop waiting to turn off the async schedule */ 2889 fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC); 2890 2891 /* Don't start the schedule until ASS is 0 */ 2892 fotg210_poll_ASS(fotg210); 2893 turn_on_io_watchdog(fotg210); 2894} 2895 2896static void disable_async(struct fotg210_hcd *fotg210) 2897{ 2898 if (--fotg210->async_count) 2899 return; 2900 2901 /* The async schedule and async_unlink list are supposed to be empty */ 2902 WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink); 2903 2904 /* Don't turn off the schedule until ASS is 1 */ 2905 fotg210_poll_ASS(fotg210); 2906} 2907 2908/* move qh (and its qtds) onto async queue; maybe enable queue. */ 2909 2910static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh) 2911{ 2912 __hc32 dma = QH_NEXT(fotg210, qh->qh_dma); 2913 struct fotg210_qh *head; 2914 2915 /* Don't link a QH if there's a Clear-TT-Buffer pending */ 2916 if (unlikely(qh->clearing_tt)) 2917 return; 2918 2919 WARN_ON(qh->qh_state != QH_STATE_IDLE); 2920 2921 /* clear halt and/or toggle; and maybe recover from silicon quirk */ 2922 qh_refresh(fotg210, qh); 2923 2924 /* splice right after start */ 2925 head = fotg210->async; 2926 qh->qh_next = head->qh_next; 2927 qh->hw->hw_next = head->hw->hw_next; 2928 wmb(); 2929 2930 head->qh_next.qh = qh; 2931 head->hw->hw_next = dma; 2932 2933 qh->xacterrs = 0; 2934 qh->qh_state = QH_STATE_LINKED; 2935 /* qtd completions reported later by interrupt */ 2936 2937 enable_async(fotg210); 2938} 2939 2940/* For control/bulk/interrupt, return QH with these TDs appended. 2941 * Allocates and initializes the QH if necessary. 2942 * Returns null if it can't allocate a QH it needs to. 2943 * If the QH has TDs (urbs) already, that's great. 2944 */ 2945static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210, 2946 struct urb *urb, struct list_head *qtd_list, 2947 int epnum, void **ptr) 2948{ 2949 struct fotg210_qh *qh = NULL; 2950 __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f); 2951 2952 qh = (struct fotg210_qh *) *ptr; 2953 if (unlikely(qh == NULL)) { 2954 /* can't sleep here, we have fotg210->lock... */ 2955 qh = qh_make(fotg210, urb, GFP_ATOMIC); 2956 *ptr = qh; 2957 } 2958 if (likely(qh != NULL)) { 2959 struct fotg210_qtd *qtd; 2960 2961 if (unlikely(list_empty(qtd_list))) 2962 qtd = NULL; 2963 else 2964 qtd = list_entry(qtd_list->next, struct fotg210_qtd, 2965 qtd_list); 2966 2967 /* control qh may need patching ... */ 2968 if (unlikely(epnum == 0)) { 2969 /* usb_reset_device() briefly reverts to address 0 */ 2970 if (usb_pipedevice(urb->pipe) == 0) 2971 qh->hw->hw_info1 &= ~qh_addr_mask; 2972 } 2973 2974 /* just one way to queue requests: swap with the dummy qtd. 2975 * only hc or qh_refresh() ever modify the overlay. 2976 */ 2977 if (likely(qtd != NULL)) { 2978 struct fotg210_qtd *dummy; 2979 dma_addr_t dma; 2980 __hc32 token; 2981 2982 /* to avoid racing the HC, use the dummy td instead of 2983 * the first td of our list (becomes new dummy). both 2984 * tds stay deactivated until we're done, when the 2985 * HC is allowed to fetch the old dummy (4.10.2). 2986 */ 2987 token = qtd->hw_token; 2988 qtd->hw_token = HALT_BIT(fotg210); 2989 2990 dummy = qh->dummy; 2991 2992 dma = dummy->qtd_dma; 2993 *dummy = *qtd; 2994 dummy->qtd_dma = dma; 2995 2996 list_del(&qtd->qtd_list); 2997 list_add(&dummy->qtd_list, qtd_list); 2998 list_splice_tail(qtd_list, &qh->qtd_list); 2999 3000 fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma); 3001 qh->dummy = qtd; 3002 3003 /* hc must see the new dummy at list end */ 3004 dma = qtd->qtd_dma; 3005 qtd = list_entry(qh->qtd_list.prev, 3006 struct fotg210_qtd, qtd_list); 3007 qtd->hw_next = QTD_NEXT(fotg210, dma); 3008 3009 /* let the hc process these next qtds */ 3010 wmb(); 3011 dummy->hw_token = token; 3012 3013 urb->hcpriv = qh; 3014 } 3015 } 3016 return qh; 3017} 3018 3019static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb, 3020 struct list_head *qtd_list, gfp_t mem_flags) 3021{ 3022 int epnum; 3023 unsigned long flags; 3024 struct fotg210_qh *qh = NULL; 3025 int rc; 3026 3027 epnum = urb->ep->desc.bEndpointAddress; 3028 3029#ifdef FOTG210_URB_TRACE 3030 { 3031 struct fotg210_qtd *qtd; 3032 3033 qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list); 3034 fotg210_dbg(fotg210, 3035 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n", 3036 __func__, urb->dev->devpath, urb, 3037 epnum & 0x0f, (epnum & USB_DIR_IN) 3038 ? "in" : "out", 3039 urb->transfer_buffer_length, 3040 qtd, urb->ep->hcpriv); 3041 } 3042#endif 3043 3044 spin_lock_irqsave(&fotg210->lock, flags); 3045 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) { 3046 rc = -ESHUTDOWN; 3047 goto done; 3048 } 3049 rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb); 3050 if (unlikely(rc)) 3051 goto done; 3052 3053 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv); 3054 if (unlikely(qh == NULL)) { 3055 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb); 3056 rc = -ENOMEM; 3057 goto done; 3058 } 3059 3060 /* Control/bulk operations through TTs don't need scheduling, 3061 * the HC and TT handle it when the TT has a buffer ready. 3062 */ 3063 if (likely(qh->qh_state == QH_STATE_IDLE)) 3064 qh_link_async(fotg210, qh); 3065done: 3066 spin_unlock_irqrestore(&fotg210->lock, flags); 3067 if (unlikely(qh == NULL)) 3068 qtd_list_free(fotg210, urb, qtd_list); 3069 return rc; 3070} 3071 3072static void single_unlink_async(struct fotg210_hcd *fotg210, 3073 struct fotg210_qh *qh) 3074{ 3075 struct fotg210_qh *prev; 3076 3077 /* Add to the end of the list of QHs waiting for the next IAAD */ 3078 qh->qh_state = QH_STATE_UNLINK; 3079 if (fotg210->async_unlink) 3080 fotg210->async_unlink_last->unlink_next = qh; 3081 else 3082 fotg210->async_unlink = qh; 3083 fotg210->async_unlink_last = qh; 3084 3085 /* Unlink it from the schedule */ 3086 prev = fotg210->async; 3087 while (prev->qh_next.qh != qh) 3088 prev = prev->qh_next.qh; 3089 3090 prev->hw->hw_next = qh->hw->hw_next; 3091 prev->qh_next = qh->qh_next; 3092 if (fotg210->qh_scan_next == qh) 3093 fotg210->qh_scan_next = qh->qh_next.qh; 3094} 3095 3096static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested) 3097{ 3098 /* 3099 * Do nothing if an IAA cycle is already running or 3100 * if one will be started shortly. 3101 */ 3102 if (fotg210->async_iaa || fotg210->async_unlinking) 3103 return; 3104 3105 /* Do all the waiting QHs at once */ 3106 fotg210->async_iaa = fotg210->async_unlink; 3107 fotg210->async_unlink = NULL; 3108 3109 /* If the controller isn't running, we don't have to wait for it */ 3110 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) { 3111 if (!nested) /* Avoid recursion */ 3112 end_unlink_async(fotg210); 3113 3114 /* Otherwise start a new IAA cycle */ 3115 } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) { 3116 /* Make sure the unlinks are all visible to the hardware */ 3117 wmb(); 3118 3119 fotg210_writel(fotg210, fotg210->command | CMD_IAAD, 3120 &fotg210->regs->command); 3121 fotg210_readl(fotg210, &fotg210->regs->command); 3122 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG, 3123 true); 3124 } 3125} 3126 3127/* the async qh for the qtds being unlinked are now gone from the HC */ 3128 3129static void end_unlink_async(struct fotg210_hcd *fotg210) 3130{ 3131 struct fotg210_qh *qh; 3132 3133 /* Process the idle QHs */ 3134restart: 3135 fotg210->async_unlinking = true; 3136 while (fotg210->async_iaa) { 3137 qh = fotg210->async_iaa; 3138 fotg210->async_iaa = qh->unlink_next; 3139 qh->unlink_next = NULL; 3140 3141 qh->qh_state = QH_STATE_IDLE; 3142 qh->qh_next.qh = NULL; 3143 3144 qh_completions(fotg210, qh); 3145 if (!list_empty(&qh->qtd_list) && 3146 fotg210->rh_state == FOTG210_RH_RUNNING) 3147 qh_link_async(fotg210, qh); 3148 disable_async(fotg210); 3149 } 3150 fotg210->async_unlinking = false; 3151 3152 /* Start a new IAA cycle if any QHs are waiting for it */ 3153 if (fotg210->async_unlink) { 3154 start_iaa_cycle(fotg210, true); 3155 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) 3156 goto restart; 3157 } 3158} 3159 3160static void unlink_empty_async(struct fotg210_hcd *fotg210) 3161{ 3162 struct fotg210_qh *qh, *next; 3163 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING); 3164 bool check_unlinks_later = false; 3165 3166 /* Unlink all the async QHs that have been empty for a timer cycle */ 3167 next = fotg210->async->qh_next.qh; 3168 while (next) { 3169 qh = next; 3170 next = qh->qh_next.qh; 3171 3172 if (list_empty(&qh->qtd_list) && 3173 qh->qh_state == QH_STATE_LINKED) { 3174 if (!stopped && qh->unlink_cycle == 3175 fotg210->async_unlink_cycle) 3176 check_unlinks_later = true; 3177 else 3178 single_unlink_async(fotg210, qh); 3179 } 3180 } 3181 3182 /* Start a new IAA cycle if any QHs are waiting for it */ 3183 if (fotg210->async_unlink) 3184 start_iaa_cycle(fotg210, false); 3185 3186 /* QHs that haven't been empty for long enough will be handled later */ 3187 if (check_unlinks_later) { 3188 fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS, 3189 true); 3190 ++fotg210->async_unlink_cycle; 3191 } 3192} 3193 3194/* makes sure the async qh will become idle */ 3195/* caller must own fotg210->lock */ 3196 3197static void start_unlink_async(struct fotg210_hcd *fotg210, 3198 struct fotg210_qh *qh) 3199{ 3200 /* 3201 * If the QH isn't linked then there's nothing we can do 3202 * unless we were called during a giveback, in which case 3203 * qh_completions() has to deal with it. 3204 */ 3205 if (qh->qh_state != QH_STATE_LINKED) { 3206 if (qh->qh_state == QH_STATE_COMPLETING) 3207 qh->needs_rescan = 1; 3208 return; 3209 } 3210 3211 single_unlink_async(fotg210, qh); 3212 start_iaa_cycle(fotg210, false); 3213} 3214 3215static void scan_async(struct fotg210_hcd *fotg210) 3216{ 3217 struct fotg210_qh *qh; 3218 bool check_unlinks_later = false; 3219 3220 fotg210->qh_scan_next = fotg210->async->qh_next.qh; 3221 while (fotg210->qh_scan_next) { 3222 qh = fotg210->qh_scan_next; 3223 fotg210->qh_scan_next = qh->qh_next.qh; 3224rescan: 3225 /* clean any finished work for this qh */ 3226 if (!list_empty(&qh->qtd_list)) { 3227 int temp; 3228 3229 /* 3230 * Unlinks could happen here; completion reporting 3231 * drops the lock. That's why fotg210->qh_scan_next 3232 * always holds the next qh to scan; if the next qh 3233 * gets unlinked then fotg210->qh_scan_next is adjusted 3234 * in single_unlink_async(). 3235 */ 3236 temp = qh_completions(fotg210, qh); 3237 if (qh->needs_rescan) { 3238 start_unlink_async(fotg210, qh); 3239 } else if (list_empty(&qh->qtd_list) 3240 && qh->qh_state == QH_STATE_LINKED) { 3241 qh->unlink_cycle = fotg210->async_unlink_cycle; 3242 check_unlinks_later = true; 3243 } else if (temp != 0) 3244 goto rescan; 3245 } 3246 } 3247 3248 /* 3249 * Unlink empty entries, reducing DMA usage as well 3250 * as HCD schedule-scanning costs. Delay for any qh 3251 * we just scanned, there's a not-unusual case that it 3252 * doesn't stay idle for long. 3253 */ 3254 if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING && 3255 !(fotg210->enabled_hrtimer_events & 3256 BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) { 3257 fotg210_enable_event(fotg210, 3258 FOTG210_HRTIMER_ASYNC_UNLINKS, true); 3259 ++fotg210->async_unlink_cycle; 3260 } 3261} 3262/* EHCI scheduled transaction support: interrupt, iso, split iso 3263 * These are called "periodic" transactions in the EHCI spec. 3264 * 3265 * Note that for interrupt transfers, the QH/QTD manipulation is shared 3266 * with the "asynchronous" transaction support (control/bulk transfers). 3267 * The only real difference is in how interrupt transfers are scheduled. 3268 * 3269 * For ISO, we make an "iso_stream" head to serve the same role as a QH. 3270 * It keeps track of every ITD (or SITD) that's linked, and holds enough 3271 * pre-calculated schedule data to make appending to the queue be quick. 3272 */ 3273static int fotg210_get_frame(struct usb_hcd *hcd); 3274 3275/* periodic_next_shadow - return "next" pointer on shadow list 3276 * @periodic: host pointer to qh/itd 3277 * @tag: hardware tag for type of this record 3278 */ 3279static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210, 3280 union fotg210_shadow *periodic, __hc32 tag) 3281{ 3282 switch (hc32_to_cpu(fotg210, tag)) { 3283 case Q_TYPE_QH: 3284 return &periodic->qh->qh_next; 3285 case Q_TYPE_FSTN: 3286 return &periodic->fstn->fstn_next; 3287 default: 3288 return &periodic->itd->itd_next; 3289 } 3290} 3291 3292static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210, 3293 union fotg210_shadow *periodic, __hc32 tag) 3294{ 3295 switch (hc32_to_cpu(fotg210, tag)) { 3296 /* our fotg210_shadow.qh is actually software part */ 3297 case Q_TYPE_QH: 3298 return &periodic->qh->hw->hw_next; 3299 /* others are hw parts */ 3300 default: 3301 return periodic->hw_next; 3302 } 3303} 3304 3305/* caller must hold fotg210->lock */ 3306static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame, 3307 void *ptr) 3308{ 3309 union fotg210_shadow *prev_p = &fotg210->pshadow[frame]; 3310 __hc32 *hw_p = &fotg210->periodic[frame]; 3311 union fotg210_shadow here = *prev_p; 3312 3313 /* find predecessor of "ptr"; hw and shadow lists are in sync */ 3314 while (here.ptr && here.ptr != ptr) { 3315 prev_p = periodic_next_shadow(fotg210, prev_p, 3316 Q_NEXT_TYPE(fotg210, *hw_p)); 3317 hw_p = shadow_next_periodic(fotg210, &here, 3318 Q_NEXT_TYPE(fotg210, *hw_p)); 3319 here = *prev_p; 3320 } 3321 /* an interrupt entry (at list end) could have been shared */ 3322 if (!here.ptr) 3323 return; 3324 3325 /* update shadow and hardware lists ... the old "next" pointers 3326 * from ptr may still be in use, the caller updates them. 3327 */ 3328 *prev_p = *periodic_next_shadow(fotg210, &here, 3329 Q_NEXT_TYPE(fotg210, *hw_p)); 3330 3331 *hw_p = *shadow_next_periodic(fotg210, &here, 3332 Q_NEXT_TYPE(fotg210, *hw_p)); 3333} 3334 3335/* how many of the uframe's 125 usecs are allocated? */ 3336static unsigned short periodic_usecs(struct fotg210_hcd *fotg210, 3337 unsigned frame, unsigned uframe) 3338{ 3339 __hc32 *hw_p = &fotg210->periodic[frame]; 3340 union fotg210_shadow *q = &fotg210->pshadow[frame]; 3341 unsigned usecs = 0; 3342 struct fotg210_qh_hw *hw; 3343 3344 while (q->ptr) { 3345 switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) { 3346 case Q_TYPE_QH: 3347 hw = q->qh->hw; 3348 /* is it in the S-mask? */ 3349 if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe)) 3350 usecs += q->qh->usecs; 3351 /* ... or C-mask? */ 3352 if (hw->hw_info2 & cpu_to_hc32(fotg210, 3353 1 << (8 + uframe))) 3354 usecs += q->qh->c_usecs; 3355 hw_p = &hw->hw_next; 3356 q = &q->qh->qh_next; 3357 break; 3358 /* case Q_TYPE_FSTN: */ 3359 default: 3360 /* for "save place" FSTNs, count the relevant INTR 3361 * bandwidth from the previous frame 3362 */ 3363 if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210)) 3364 fotg210_dbg(fotg210, "ignoring FSTN cost ...\n"); 3365 3366 hw_p = &q->fstn->hw_next; 3367 q = &q->fstn->fstn_next; 3368 break; 3369 case Q_TYPE_ITD: 3370 if (q->itd->hw_transaction[uframe]) 3371 usecs += q->itd->stream->usecs; 3372 hw_p = &q->itd->hw_next; 3373 q = &q->itd->itd_next; 3374 break; 3375 } 3376 } 3377 if (usecs > fotg210->uframe_periodic_max) 3378 fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n", 3379 frame * 8 + uframe, usecs); 3380 return usecs; 3381} 3382 3383static int same_tt(struct usb_device *dev1, struct usb_device *dev2) 3384{ 3385 if (!dev1->tt || !dev2->tt) 3386 return 0; 3387 if (dev1->tt != dev2->tt) 3388 return 0; 3389 if (dev1->tt->multi) 3390 return dev1->ttport == dev2->ttport; 3391 else 3392 return 1; 3393} 3394 3395/* return true iff the device's transaction translator is available 3396 * for a periodic transfer starting at the specified frame, using 3397 * all the uframes in the mask. 3398 */ 3399static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period, 3400 struct usb_device *dev, unsigned frame, u32 uf_mask) 3401{ 3402 if (period == 0) /* error */ 3403 return 0; 3404 3405 /* note bandwidth wastage: split never follows csplit 3406 * (different dev or endpoint) until the next uframe. 3407 * calling convention doesn't make that distinction. 3408 */ 3409 for (; frame < fotg210->periodic_size; frame += period) { 3410 union fotg210_shadow here; 3411 __hc32 type; 3412 struct fotg210_qh_hw *hw; 3413 3414 here = fotg210->pshadow[frame]; 3415 type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]); 3416 while (here.ptr) { 3417 switch (hc32_to_cpu(fotg210, type)) { 3418 case Q_TYPE_ITD: 3419 type = Q_NEXT_TYPE(fotg210, here.itd->hw_next); 3420 here = here.itd->itd_next; 3421 continue; 3422 case Q_TYPE_QH: 3423 hw = here.qh->hw; 3424 if (same_tt(dev, here.qh->dev)) { 3425 u32 mask; 3426 3427 mask = hc32_to_cpu(fotg210, 3428 hw->hw_info2); 3429 /* "knows" no gap is needed */ 3430 mask |= mask >> 8; 3431 if (mask & uf_mask) 3432 break; 3433 } 3434 type = Q_NEXT_TYPE(fotg210, hw->hw_next); 3435 here = here.qh->qh_next; 3436 continue; 3437 /* case Q_TYPE_FSTN: */ 3438 default: 3439 fotg210_dbg(fotg210, 3440 "periodic frame %d bogus type %d\n", 3441 frame, type); 3442 } 3443 3444 /* collision or error */ 3445 return 0; 3446 } 3447 } 3448 3449 /* no collision */ 3450 return 1; 3451} 3452 3453static void enable_periodic(struct fotg210_hcd *fotg210) 3454{ 3455 if (fotg210->periodic_count++) 3456 return; 3457 3458 /* Stop waiting to turn off the periodic schedule */ 3459 fotg210->enabled_hrtimer_events &= 3460 ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC); 3461 3462 /* Don't start the schedule until PSS is 0 */ 3463 fotg210_poll_PSS(fotg210); 3464 turn_on_io_watchdog(fotg210); 3465} 3466 3467static void disable_periodic(struct fotg210_hcd *fotg210) 3468{ 3469 if (--fotg210->periodic_count) 3470 return; 3471 3472 /* Don't turn off the schedule until PSS is 1 */ 3473 fotg210_poll_PSS(fotg210); 3474} 3475 3476/* periodic schedule slots have iso tds (normal or split) first, then a 3477 * sparse tree for active interrupt transfers. 3478 * 3479 * this just links in a qh; caller guarantees uframe masks are set right. 3480 * no FSTN support (yet; fotg210 0.96+) 3481 */ 3482static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh) 3483{ 3484 unsigned i; 3485 unsigned period = qh->period; 3486 3487 dev_dbg(&qh->dev->dev, 3488 "link qh%d-%04x/%p start %d [%d/%d us]\n", period, 3489 hc32_to_cpup(fotg210, &qh->hw->hw_info2) & 3490 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs, 3491 qh->c_usecs); 3492 3493 /* high bandwidth, or otherwise every microframe */ 3494 if (period == 0) 3495 period = 1; 3496 3497 for (i = qh->start; i < fotg210->periodic_size; i += period) { 3498 union fotg210_shadow *prev = &fotg210->pshadow[i]; 3499 __hc32 *hw_p = &fotg210->periodic[i]; 3500 union fotg210_shadow here = *prev; 3501 __hc32 type = 0; 3502 3503 /* skip the iso nodes at list head */ 3504 while (here.ptr) { 3505 type = Q_NEXT_TYPE(fotg210, *hw_p); 3506 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH)) 3507 break; 3508 prev = periodic_next_shadow(fotg210, prev, type); 3509 hw_p = shadow_next_periodic(fotg210, &here, type); 3510 here = *prev; 3511 } 3512 3513 /* sorting each branch by period (slow-->fast) 3514 * enables sharing interior tree nodes 3515 */ 3516 while (here.ptr && qh != here.qh) { 3517 if (qh->period > here.qh->period) 3518 break; 3519 prev = &here.qh->qh_next; 3520 hw_p = &here.qh->hw->hw_next; 3521 here = *prev; 3522 } 3523 /* link in this qh, unless some earlier pass did that */ 3524 if (qh != here.qh) { 3525 qh->qh_next = here; 3526 if (here.qh) 3527 qh->hw->hw_next = *hw_p; 3528 wmb(); 3529 prev->qh = qh; 3530 *hw_p = QH_NEXT(fotg210, qh->qh_dma); 3531 } 3532 } 3533 qh->qh_state = QH_STATE_LINKED; 3534 qh->xacterrs = 0; 3535 3536 /* update per-qh bandwidth for usbfs */ 3537 fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period 3538 ? ((qh->usecs + qh->c_usecs) / qh->period) 3539 : (qh->usecs * 8); 3540 3541 list_add(&qh->intr_node, &fotg210->intr_qh_list); 3542 3543 /* maybe enable periodic schedule processing */ 3544 ++fotg210->intr_count; 3545 enable_periodic(fotg210); 3546} 3547 3548static void qh_unlink_periodic(struct fotg210_hcd *fotg210, 3549 struct fotg210_qh *qh) 3550{ 3551 unsigned i; 3552 unsigned period; 3553 3554 /* 3555 * If qh is for a low/full-speed device, simply unlinking it 3556 * could interfere with an ongoing split transaction. To unlink 3557 * it safely would require setting the QH_INACTIVATE bit and 3558 * waiting at least one frame, as described in EHCI 4.12.2.5. 3559 * 3560 * We won't bother with any of this. Instead, we assume that the 3561 * only reason for unlinking an interrupt QH while the current URB 3562 * is still active is to dequeue all the URBs (flush the whole 3563 * endpoint queue). 3564 * 3565 * If rebalancing the periodic schedule is ever implemented, this 3566 * approach will no longer be valid. 3567 */ 3568 3569 /* high bandwidth, or otherwise part of every microframe */ 3570 period = qh->period; 3571 if (!period) 3572 period = 1; 3573 3574 for (i = qh->start; i < fotg210->periodic_size; i += period) 3575 periodic_unlink(fotg210, i, qh); 3576 3577 /* update per-qh bandwidth for usbfs */ 3578 fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period 3579 ? ((qh->usecs + qh->c_usecs) / qh->period) 3580 : (qh->usecs * 8); 3581 3582 dev_dbg(&qh->dev->dev, 3583 "unlink qh%d-%04x/%p start %d [%d/%d us]\n", 3584 qh->period, hc32_to_cpup(fotg210, &qh->hw->hw_info2) & 3585 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs, 3586 qh->c_usecs); 3587 3588 /* qh->qh_next still "live" to HC */ 3589 qh->qh_state = QH_STATE_UNLINK; 3590 qh->qh_next.ptr = NULL; 3591 3592 if (fotg210->qh_scan_next == qh) 3593 fotg210->qh_scan_next = list_entry(qh->intr_node.next, 3594 struct fotg210_qh, intr_node); 3595 list_del(&qh->intr_node); 3596} 3597 3598static void start_unlink_intr(struct fotg210_hcd *fotg210, 3599 struct fotg210_qh *qh) 3600{ 3601 /* If the QH isn't linked then there's nothing we can do 3602 * unless we were called during a giveback, in which case 3603 * qh_completions() has to deal with it. 3604 */ 3605 if (qh->qh_state != QH_STATE_LINKED) { 3606 if (qh->qh_state == QH_STATE_COMPLETING) 3607 qh->needs_rescan = 1; 3608 return; 3609 } 3610 3611 qh_unlink_periodic(fotg210, qh); 3612 3613 /* Make sure the unlinks are visible before starting the timer */ 3614 wmb(); 3615 3616 /* 3617 * The EHCI spec doesn't say how long it takes the controller to 3618 * stop accessing an unlinked interrupt QH. The timer delay is 3619 * 9 uframes; presumably that will be long enough. 3620 */ 3621 qh->unlink_cycle = fotg210->intr_unlink_cycle; 3622 3623 /* New entries go at the end of the intr_unlink list */ 3624 if (fotg210->intr_unlink) 3625 fotg210->intr_unlink_last->unlink_next = qh; 3626 else 3627 fotg210->intr_unlink = qh; 3628 fotg210->intr_unlink_last = qh; 3629 3630 if (fotg210->intr_unlinking) 3631 ; /* Avoid recursive calls */ 3632 else if (fotg210->rh_state < FOTG210_RH_RUNNING) 3633 fotg210_handle_intr_unlinks(fotg210); 3634 else if (fotg210->intr_unlink == qh) { 3635 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR, 3636 true); 3637 ++fotg210->intr_unlink_cycle; 3638 } 3639} 3640 3641static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh) 3642{ 3643 struct fotg210_qh_hw *hw = qh->hw; 3644 int rc; 3645 3646 qh->qh_state = QH_STATE_IDLE; 3647 hw->hw_next = FOTG210_LIST_END(fotg210); 3648 3649 qh_completions(fotg210, qh); 3650 3651 /* reschedule QH iff another request is queued */ 3652 if (!list_empty(&qh->qtd_list) && 3653 fotg210->rh_state == FOTG210_RH_RUNNING) { 3654 rc = qh_schedule(fotg210, qh); 3655 3656 /* An error here likely indicates handshake failure 3657 * or no space left in the schedule. Neither fault 3658 * should happen often ... 3659 * 3660 * FIXME kill the now-dysfunctional queued urbs 3661 */ 3662 if (rc != 0) 3663 fotg210_err(fotg210, "can't reschedule qh %p, err %d\n", 3664 qh, rc); 3665 } 3666 3667 /* maybe turn off periodic schedule */ 3668 --fotg210->intr_count; 3669 disable_periodic(fotg210); 3670} 3671 3672static int check_period(struct fotg210_hcd *fotg210, unsigned frame, 3673 unsigned uframe, unsigned period, unsigned usecs) 3674{ 3675 int claimed; 3676 3677 /* complete split running into next frame? 3678 * given FSTN support, we could sometimes check... 3679 */ 3680 if (uframe >= 8) 3681 return 0; 3682 3683 /* convert "usecs we need" to "max already claimed" */ 3684 usecs = fotg210->uframe_periodic_max - usecs; 3685 3686 /* we "know" 2 and 4 uframe intervals were rejected; so 3687 * for period 0, check _every_ microframe in the schedule. 3688 */ 3689 if (unlikely(period == 0)) { 3690 do { 3691 for (uframe = 0; uframe < 7; uframe++) { 3692 claimed = periodic_usecs(fotg210, frame, 3693 uframe); 3694 if (claimed > usecs) 3695 return 0; 3696 } 3697 } while ((frame += 1) < fotg210->periodic_size); 3698 3699 /* just check the specified uframe, at that period */ 3700 } else { 3701 do { 3702 claimed = periodic_usecs(fotg210, frame, uframe); 3703 if (claimed > usecs) 3704 return 0; 3705 } while ((frame += period) < fotg210->periodic_size); 3706 } 3707 3708 /* success! */ 3709 return 1; 3710} 3711 3712static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame, 3713 unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp) 3714{ 3715 int retval = -ENOSPC; 3716 u8 mask = 0; 3717 3718 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */ 3719 goto done; 3720 3721 if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs)) 3722 goto done; 3723 if (!qh->c_usecs) { 3724 retval = 0; 3725 *c_maskp = 0; 3726 goto done; 3727 } 3728 3729 /* Make sure this tt's buffer is also available for CSPLITs. 3730 * We pessimize a bit; probably the typical full speed case 3731 * doesn't need the second CSPLIT. 3732 * 3733 * NOTE: both SPLIT and CSPLIT could be checked in just 3734 * one smart pass... 3735 */ 3736 mask = 0x03 << (uframe + qh->gap_uf); 3737 *c_maskp = cpu_to_hc32(fotg210, mask << 8); 3738 3739 mask |= 1 << uframe; 3740 if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) { 3741 if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1, 3742 qh->period, qh->c_usecs)) 3743 goto done; 3744 if (!check_period(fotg210, frame, uframe + qh->gap_uf, 3745 qh->period, qh->c_usecs)) 3746 goto done; 3747 retval = 0; 3748 } 3749done: 3750 return retval; 3751} 3752 3753/* "first fit" scheduling policy used the first time through, 3754 * or when the previous schedule slot can't be re-used. 3755 */ 3756static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh) 3757{ 3758 int status; 3759 unsigned uframe; 3760 __hc32 c_mask; 3761 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */ 3762 struct fotg210_qh_hw *hw = qh->hw; 3763 3764 qh_refresh(fotg210, qh); 3765 hw->hw_next = FOTG210_LIST_END(fotg210); 3766 frame = qh->start; 3767 3768 /* reuse the previous schedule slots, if we can */ 3769 if (frame < qh->period) { 3770 uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK); 3771 status = check_intr_schedule(fotg210, frame, --uframe, 3772 qh, &c_mask); 3773 } else { 3774 uframe = 0; 3775 c_mask = 0; 3776 status = -ENOSPC; 3777 } 3778 3779 /* else scan the schedule to find a group of slots such that all 3780 * uframes have enough periodic bandwidth available. 3781 */ 3782 if (status) { 3783 /* "normal" case, uframing flexible except with splits */ 3784 if (qh->period) { 3785 int i; 3786 3787 for (i = qh->period; status && i > 0; --i) { 3788 frame = ++fotg210->random_frame % qh->period; 3789 for (uframe = 0; uframe < 8; uframe++) { 3790 status = check_intr_schedule(fotg210, 3791 frame, uframe, qh, 3792 &c_mask); 3793 if (status == 0) 3794 break; 3795 } 3796 } 3797 3798 /* qh->period == 0 means every uframe */ 3799 } else { 3800 frame = 0; 3801 status = check_intr_schedule(fotg210, 0, 0, qh, 3802 &c_mask); 3803 } 3804 if (status) 3805 goto done; 3806 qh->start = frame; 3807 3808 /* reset S-frame and (maybe) C-frame masks */ 3809 hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK)); 3810 hw->hw_info2 |= qh->period 3811 ? cpu_to_hc32(fotg210, 1 << uframe) 3812 : cpu_to_hc32(fotg210, QH_SMASK); 3813 hw->hw_info2 |= c_mask; 3814 } else 3815 fotg210_dbg(fotg210, "reused qh %p schedule\n", qh); 3816 3817 /* stuff into the periodic schedule */ 3818 qh_link_periodic(fotg210, qh); 3819done: 3820 return status; 3821} 3822 3823static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb, 3824 struct list_head *qtd_list, gfp_t mem_flags) 3825{ 3826 unsigned epnum; 3827 unsigned long flags; 3828 struct fotg210_qh *qh; 3829 int status; 3830 struct list_head empty; 3831 3832 /* get endpoint and transfer/schedule data */ 3833 epnum = urb->ep->desc.bEndpointAddress; 3834 3835 spin_lock_irqsave(&fotg210->lock, flags); 3836 3837 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) { 3838 status = -ESHUTDOWN; 3839 goto done_not_linked; 3840 } 3841 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb); 3842 if (unlikely(status)) 3843 goto done_not_linked; 3844 3845 /* get qh and force any scheduling errors */ 3846 INIT_LIST_HEAD(&empty); 3847 qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv); 3848 if (qh == NULL) { 3849 status = -ENOMEM; 3850 goto done; 3851 } 3852 if (qh->qh_state == QH_STATE_IDLE) { 3853 status = qh_schedule(fotg210, qh); 3854 if (status) 3855 goto done; 3856 } 3857 3858 /* then queue the urb's tds to the qh */ 3859 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv); 3860 BUG_ON(qh == NULL); 3861 3862 /* ... update usbfs periodic stats */ 3863 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++; 3864 3865done: 3866 if (unlikely(status)) 3867 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb); 3868done_not_linked: 3869 spin_unlock_irqrestore(&fotg210->lock, flags); 3870 if (status) 3871 qtd_list_free(fotg210, urb, qtd_list); 3872 3873 return status; 3874} 3875 3876static void scan_intr(struct fotg210_hcd *fotg210) 3877{ 3878 struct fotg210_qh *qh; 3879 3880 list_for_each_entry_safe(qh, fotg210->qh_scan_next, 3881 &fotg210->intr_qh_list, intr_node) { 3882rescan: 3883 /* clean any finished work for this qh */ 3884 if (!list_empty(&qh->qtd_list)) { 3885 int temp; 3886 3887 /* 3888 * Unlinks could happen here; completion reporting 3889 * drops the lock. That's why fotg210->qh_scan_next 3890 * always holds the next qh to scan; if the next qh 3891 * gets unlinked then fotg210->qh_scan_next is adjusted 3892 * in qh_unlink_periodic(). 3893 */ 3894 temp = qh_completions(fotg210, qh); 3895 if (unlikely(qh->needs_rescan || 3896 (list_empty(&qh->qtd_list) && 3897 qh->qh_state == QH_STATE_LINKED))) 3898 start_unlink_intr(fotg210, qh); 3899 else if (temp != 0) 3900 goto rescan; 3901 } 3902 } 3903} 3904 3905/* fotg210_iso_stream ops work with both ITD and SITD */ 3906 3907static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags) 3908{ 3909 struct fotg210_iso_stream *stream; 3910 3911 stream = kzalloc(sizeof(*stream), mem_flags); 3912 if (likely(stream != NULL)) { 3913 INIT_LIST_HEAD(&stream->td_list); 3914 INIT_LIST_HEAD(&stream->free_list); 3915 stream->next_uframe = -1; 3916 } 3917 return stream; 3918} 3919 3920static void iso_stream_init(struct fotg210_hcd *fotg210, 3921 struct fotg210_iso_stream *stream, struct usb_device *dev, 3922 int pipe, unsigned interval) 3923{ 3924 u32 buf1; 3925 unsigned epnum, maxp; 3926 int is_input; 3927 long bandwidth; 3928 unsigned multi; 3929 3930 /* 3931 * this might be a "high bandwidth" highspeed endpoint, 3932 * as encoded in the ep descriptor's wMaxPacket field 3933 */ 3934 epnum = usb_pipeendpoint(pipe); 3935 is_input = usb_pipein(pipe) ? USB_DIR_IN : 0; 3936 maxp = usb_maxpacket(dev, pipe, !is_input); 3937 if (is_input) 3938 buf1 = (1 << 11); 3939 else 3940 buf1 = 0; 3941 3942 maxp = max_packet(maxp); 3943 multi = hb_mult(maxp); 3944 buf1 |= maxp; 3945 maxp *= multi; 3946 3947 stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum); 3948 stream->buf1 = cpu_to_hc32(fotg210, buf1); 3949 stream->buf2 = cpu_to_hc32(fotg210, multi); 3950 3951 /* usbfs wants to report the average usecs per frame tied up 3952 * when transfers on this endpoint are scheduled ... 3953 */ 3954 if (dev->speed == USB_SPEED_FULL) { 3955 interval <<= 3; 3956 stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed, 3957 is_input, 1, maxp)); 3958 stream->usecs /= 8; 3959 } else { 3960 stream->highspeed = 1; 3961 stream->usecs = HS_USECS_ISO(maxp); 3962 } 3963 bandwidth = stream->usecs * 8; 3964 bandwidth /= interval; 3965 3966 stream->bandwidth = bandwidth; 3967 stream->udev = dev; 3968 stream->bEndpointAddress = is_input | epnum; 3969 stream->interval = interval; 3970 stream->maxp = maxp; 3971} 3972 3973static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210, 3974 struct urb *urb) 3975{ 3976 unsigned epnum; 3977 struct fotg210_iso_stream *stream; 3978 struct usb_host_endpoint *ep; 3979 unsigned long flags; 3980 3981 epnum = usb_pipeendpoint(urb->pipe); 3982 if (usb_pipein(urb->pipe)) 3983 ep = urb->dev->ep_in[epnum]; 3984 else 3985 ep = urb->dev->ep_out[epnum]; 3986 3987 spin_lock_irqsave(&fotg210->lock, flags); 3988 stream = ep->hcpriv; 3989 3990 if (unlikely(stream == NULL)) { 3991 stream = iso_stream_alloc(GFP_ATOMIC); 3992 if (likely(stream != NULL)) { 3993 ep->hcpriv = stream; 3994 stream->ep = ep; 3995 iso_stream_init(fotg210, stream, urb->dev, urb->pipe, 3996 urb->interval); 3997 } 3998 3999 /* if dev->ep[epnum] is a QH, hw is set */ 4000 } else if (unlikely(stream->hw != NULL)) { 4001 fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n", 4002 urb->dev->devpath, epnum, 4003 usb_pipein(urb->pipe) ? "in" : "out"); 4004 stream = NULL; 4005 } 4006 4007 spin_unlock_irqrestore(&fotg210->lock, flags); 4008 return stream; 4009} 4010 4011/* fotg210_iso_sched ops can be ITD-only or SITD-only */ 4012 4013static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets, 4014 gfp_t mem_flags) 4015{ 4016 struct fotg210_iso_sched *iso_sched; 4017 int size = sizeof(*iso_sched); 4018 4019 size += packets * sizeof(struct fotg210_iso_packet); 4020 iso_sched = kzalloc(size, mem_flags); 4021 if (likely(iso_sched != NULL)) 4022 INIT_LIST_HEAD(&iso_sched->td_list); 4023 4024 return iso_sched; 4025} 4026 4027static inline void itd_sched_init(struct fotg210_hcd *fotg210, 4028 struct fotg210_iso_sched *iso_sched, 4029 struct fotg210_iso_stream *stream, struct urb *urb) 4030{ 4031 unsigned i; 4032 dma_addr_t dma = urb->transfer_dma; 4033 4034 /* how many uframes are needed for these transfers */ 4035 iso_sched->span = urb->number_of_packets * stream->interval; 4036 4037 /* figure out per-uframe itd fields that we'll need later 4038 * when we fit new itds into the schedule. 4039 */ 4040 for (i = 0; i < urb->number_of_packets; i++) { 4041 struct fotg210_iso_packet *uframe = &iso_sched->packet[i]; 4042 unsigned length; 4043 dma_addr_t buf; 4044 u32 trans; 4045 4046 length = urb->iso_frame_desc[i].length; 4047 buf = dma + urb->iso_frame_desc[i].offset; 4048 4049 trans = FOTG210_ISOC_ACTIVE; 4050 trans |= buf & 0x0fff; 4051 if (unlikely(((i + 1) == urb->number_of_packets)) 4052 && !(urb->transfer_flags & URB_NO_INTERRUPT)) 4053 trans |= FOTG210_ITD_IOC; 4054 trans |= length << 16; 4055 uframe->transaction = cpu_to_hc32(fotg210, trans); 4056 4057 /* might need to cross a buffer page within a uframe */ 4058 uframe->bufp = (buf & ~(u64)0x0fff); 4059 buf += length; 4060 if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff)))) 4061 uframe->cross = 1; 4062 } 4063} 4064 4065static void iso_sched_free(struct fotg210_iso_stream *stream, 4066 struct fotg210_iso_sched *iso_sched) 4067{ 4068 if (!iso_sched) 4069 return; 4070 /* caller must hold fotg210->lock!*/ 4071 list_splice(&iso_sched->td_list, &stream->free_list); 4072 kfree(iso_sched); 4073} 4074 4075static int itd_urb_transaction(struct fotg210_iso_stream *stream, 4076 struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags) 4077{ 4078 struct fotg210_itd *itd; 4079 dma_addr_t itd_dma; 4080 int i; 4081 unsigned num_itds; 4082 struct fotg210_iso_sched *sched; 4083 unsigned long flags; 4084 4085 sched = iso_sched_alloc(urb->number_of_packets, mem_flags); 4086 if (unlikely(sched == NULL)) 4087 return -ENOMEM; 4088 4089 itd_sched_init(fotg210, sched, stream, urb); 4090 4091 if (urb->interval < 8) 4092 num_itds = 1 + (sched->span + 7) / 8; 4093 else 4094 num_itds = urb->number_of_packets; 4095 4096 /* allocate/init ITDs */ 4097 spin_lock_irqsave(&fotg210->lock, flags); 4098 for (i = 0; i < num_itds; i++) { 4099 4100 /* 4101 * Use iTDs from the free list, but not iTDs that may 4102 * still be in use by the hardware. 4103 */ 4104 if (likely(!list_empty(&stream->free_list))) { 4105 itd = list_first_entry(&stream->free_list, 4106 struct fotg210_itd, itd_list); 4107 if (itd->frame == fotg210->now_frame) 4108 goto alloc_itd; 4109 list_del(&itd->itd_list); 4110 itd_dma = itd->itd_dma; 4111 } else { 4112alloc_itd: 4113 spin_unlock_irqrestore(&fotg210->lock, flags); 4114 itd = dma_pool_zalloc(fotg210->itd_pool, mem_flags, 4115 &itd_dma); 4116 spin_lock_irqsave(&fotg210->lock, flags); 4117 if (!itd) { 4118 iso_sched_free(stream, sched); 4119 spin_unlock_irqrestore(&fotg210->lock, flags); 4120 return -ENOMEM; 4121 } 4122 } 4123 4124 itd->itd_dma = itd_dma; 4125 list_add(&itd->itd_list, &sched->td_list); 4126 } 4127 spin_unlock_irqrestore(&fotg210->lock, flags); 4128 4129 /* temporarily store schedule info in hcpriv */ 4130 urb->hcpriv = sched; 4131 urb->error_count = 0; 4132 return 0; 4133} 4134 4135static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe, 4136 u8 usecs, u32 period) 4137{ 4138 uframe %= period; 4139 do { 4140 /* can't commit more than uframe_periodic_max usec */ 4141 if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7) 4142 > (fotg210->uframe_periodic_max - usecs)) 4143 return 0; 4144 4145 /* we know urb->interval is 2^N uframes */ 4146 uframe += period; 4147 } while (uframe < mod); 4148 return 1; 4149} 4150 4151/* This scheduler plans almost as far into the future as it has actual 4152 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to 4153 * "as small as possible" to be cache-friendlier.) That limits the size 4154 * transfers you can stream reliably; avoid more than 64 msec per urb. 4155 * Also avoid queue depths of less than fotg210's worst irq latency (affected 4156 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter, 4157 * and other factors); or more than about 230 msec total (for portability, 4158 * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler! 4159 */ 4160 4161#define SCHEDULE_SLOP 80 /* microframes */ 4162 4163static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb, 4164 struct fotg210_iso_stream *stream) 4165{ 4166 u32 now, next, start, period, span; 4167 int status; 4168 unsigned mod = fotg210->periodic_size << 3; 4169 struct fotg210_iso_sched *sched = urb->hcpriv; 4170 4171 period = urb->interval; 4172 span = sched->span; 4173 4174 if (span > mod - SCHEDULE_SLOP) { 4175 fotg210_dbg(fotg210, "iso request %p too long\n", urb); 4176 status = -EFBIG; 4177 goto fail; 4178 } 4179 4180 now = fotg210_read_frame_index(fotg210) & (mod - 1); 4181 4182 /* Typical case: reuse current schedule, stream is still active. 4183 * Hopefully there are no gaps from the host falling behind 4184 * (irq delays etc), but if there are we'll take the next 4185 * slot in the schedule, implicitly assuming URB_ISO_ASAP. 4186 */ 4187 if (likely(!list_empty(&stream->td_list))) { 4188 u32 excess; 4189 4190 /* For high speed devices, allow scheduling within the 4191 * isochronous scheduling threshold. For full speed devices 4192 * and Intel PCI-based controllers, don't (work around for 4193 * Intel ICH9 bug). 4194 */ 4195 if (!stream->highspeed && fotg210->fs_i_thresh) 4196 next = now + fotg210->i_thresh; 4197 else 4198 next = now; 4199 4200 /* Fell behind (by up to twice the slop amount)? 4201 * We decide based on the time of the last currently-scheduled 4202 * slot, not the time of the next available slot. 4203 */ 4204 excess = (stream->next_uframe - period - next) & (mod - 1); 4205 if (excess >= mod - 2 * SCHEDULE_SLOP) 4206 start = next + excess - mod + period * 4207 DIV_ROUND_UP(mod - excess, period); 4208 else 4209 start = next + excess + period; 4210 if (start - now >= mod) { 4211 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n", 4212 urb, start - now - period, period, 4213 mod); 4214 status = -EFBIG; 4215 goto fail; 4216 } 4217 } 4218 4219 /* need to schedule; when's the next (u)frame we could start? 4220 * this is bigger than fotg210->i_thresh allows; scheduling itself 4221 * isn't free, the slop should handle reasonably slow cpus. it 4222 * can also help high bandwidth if the dma and irq loads don't 4223 * jump until after the queue is primed. 4224 */ 4225 else { 4226 int done = 0; 4227 4228 start = SCHEDULE_SLOP + (now & ~0x07); 4229 4230 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */ 4231 4232 /* find a uframe slot with enough bandwidth. 4233 * Early uframes are more precious because full-speed 4234 * iso IN transfers can't use late uframes, 4235 * and therefore they should be allocated last. 4236 */ 4237 next = start; 4238 start += period; 4239 do { 4240 start--; 4241 /* check schedule: enough space? */ 4242 if (itd_slot_ok(fotg210, mod, start, 4243 stream->usecs, period)) 4244 done = 1; 4245 } while (start > next && !done); 4246 4247 /* no room in the schedule */ 4248 if (!done) { 4249 fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n", 4250 urb, now, now + mod); 4251 status = -ENOSPC; 4252 goto fail; 4253 } 4254 } 4255 4256 /* Tried to schedule too far into the future? */ 4257 if (unlikely(start - now + span - period >= 4258 mod - 2 * SCHEDULE_SLOP)) { 4259 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n", 4260 urb, start - now, span - period, 4261 mod - 2 * SCHEDULE_SLOP); 4262 status = -EFBIG; 4263 goto fail; 4264 } 4265 4266 stream->next_uframe = start & (mod - 1); 4267 4268 /* report high speed start in uframes; full speed, in frames */ 4269 urb->start_frame = stream->next_uframe; 4270 if (!stream->highspeed) 4271 urb->start_frame >>= 3; 4272 4273 /* Make sure scan_isoc() sees these */ 4274 if (fotg210->isoc_count == 0) 4275 fotg210->next_frame = now >> 3; 4276 return 0; 4277 4278fail: 4279 iso_sched_free(stream, sched); 4280 urb->hcpriv = NULL; 4281 return status; 4282} 4283 4284static inline void itd_init(struct fotg210_hcd *fotg210, 4285 struct fotg210_iso_stream *stream, struct fotg210_itd *itd) 4286{ 4287 int i; 4288 4289 /* it's been recently zeroed */ 4290 itd->hw_next = FOTG210_LIST_END(fotg210); 4291 itd->hw_bufp[0] = stream->buf0; 4292 itd->hw_bufp[1] = stream->buf1; 4293 itd->hw_bufp[2] = stream->buf2; 4294 4295 for (i = 0; i < 8; i++) 4296 itd->index[i] = -1; 4297 4298 /* All other fields are filled when scheduling */ 4299} 4300 4301static inline void itd_patch(struct fotg210_hcd *fotg210, 4302 struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched, 4303 unsigned index, u16 uframe) 4304{ 4305 struct fotg210_iso_packet *uf = &iso_sched->packet[index]; 4306 unsigned pg = itd->pg; 4307 4308 uframe &= 0x07; 4309 itd->index[uframe] = index; 4310 4311 itd->hw_transaction[uframe] = uf->transaction; 4312 itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12); 4313 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0); 4314 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32)); 4315 4316 /* iso_frame_desc[].offset must be strictly increasing */ 4317 if (unlikely(uf->cross)) { 4318 u64 bufp = uf->bufp + 4096; 4319 4320 itd->pg = ++pg; 4321 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0); 4322 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32)); 4323 } 4324} 4325 4326static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame, 4327 struct fotg210_itd *itd) 4328{ 4329 union fotg210_shadow *prev = &fotg210->pshadow[frame]; 4330 __hc32 *hw_p = &fotg210->periodic[frame]; 4331 union fotg210_shadow here = *prev; 4332 __hc32 type = 0; 4333 4334 /* skip any iso nodes which might belong to previous microframes */ 4335 while (here.ptr) { 4336 type = Q_NEXT_TYPE(fotg210, *hw_p); 4337 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH)) 4338 break; 4339 prev = periodic_next_shadow(fotg210, prev, type); 4340 hw_p = shadow_next_periodic(fotg210, &here, type); 4341 here = *prev; 4342 } 4343 4344 itd->itd_next = here; 4345 itd->hw_next = *hw_p; 4346 prev->itd = itd; 4347 itd->frame = frame; 4348 wmb(); 4349 *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD); 4350} 4351 4352/* fit urb's itds into the selected schedule slot; activate as needed */ 4353static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb, 4354 unsigned mod, struct fotg210_iso_stream *stream) 4355{ 4356 int packet; 4357 unsigned next_uframe, uframe, frame; 4358 struct fotg210_iso_sched *iso_sched = urb->hcpriv; 4359 struct fotg210_itd *itd; 4360 4361 next_uframe = stream->next_uframe & (mod - 1); 4362 4363 if (unlikely(list_empty(&stream->td_list))) { 4364 fotg210_to_hcd(fotg210)->self.bandwidth_allocated 4365 += stream->bandwidth; 4366 fotg210_dbg(fotg210, 4367 "schedule devp %s ep%d%s-iso period %d start %d.%d\n", 4368 urb->dev->devpath, stream->bEndpointAddress & 0x0f, 4369 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out", 4370 urb->interval, 4371 next_uframe >> 3, next_uframe & 0x7); 4372 } 4373 4374 /* fill iTDs uframe by uframe */ 4375 for (packet = 0, itd = NULL; packet < urb->number_of_packets;) { 4376 if (itd == NULL) { 4377 /* ASSERT: we have all necessary itds */ 4378 4379 /* ASSERT: no itds for this endpoint in this uframe */ 4380 4381 itd = list_entry(iso_sched->td_list.next, 4382 struct fotg210_itd, itd_list); 4383 list_move_tail(&itd->itd_list, &stream->td_list); 4384 itd->stream = stream; 4385 itd->urb = urb; 4386 itd_init(fotg210, stream, itd); 4387 } 4388 4389 uframe = next_uframe & 0x07; 4390 frame = next_uframe >> 3; 4391 4392 itd_patch(fotg210, itd, iso_sched, packet, uframe); 4393 4394 next_uframe += stream->interval; 4395 next_uframe &= mod - 1; 4396 packet++; 4397 4398 /* link completed itds into the schedule */ 4399 if (((next_uframe >> 3) != frame) 4400 || packet == urb->number_of_packets) { 4401 itd_link(fotg210, frame & (fotg210->periodic_size - 1), 4402 itd); 4403 itd = NULL; 4404 } 4405 } 4406 stream->next_uframe = next_uframe; 4407 4408 /* don't need that schedule data any more */ 4409 iso_sched_free(stream, iso_sched); 4410 urb->hcpriv = NULL; 4411 4412 ++fotg210->isoc_count; 4413 enable_periodic(fotg210); 4414} 4415 4416#define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\ 4417 FOTG210_ISOC_XACTERR) 4418 4419/* Process and recycle a completed ITD. Return true iff its urb completed, 4420 * and hence its completion callback probably added things to the hardware 4421 * schedule. 4422 * 4423 * Note that we carefully avoid recycling this descriptor until after any 4424 * completion callback runs, so that it won't be reused quickly. That is, 4425 * assuming (a) no more than two urbs per frame on this endpoint, and also 4426 * (b) only this endpoint's completions submit URBs. It seems some silicon 4427 * corrupts things if you reuse completed descriptors very quickly... 4428 */ 4429static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd) 4430{ 4431 struct urb *urb = itd->urb; 4432 struct usb_iso_packet_descriptor *desc; 4433 u32 t; 4434 unsigned uframe; 4435 int urb_index = -1; 4436 struct fotg210_iso_stream *stream = itd->stream; 4437 struct usb_device *dev; 4438 bool retval = false; 4439 4440 /* for each uframe with a packet */ 4441 for (uframe = 0; uframe < 8; uframe++) { 4442 if (likely(itd->index[uframe] == -1)) 4443 continue; 4444 urb_index = itd->index[uframe]; 4445 desc = &urb->iso_frame_desc[urb_index]; 4446 4447 t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]); 4448 itd->hw_transaction[uframe] = 0; 4449 4450 /* report transfer status */ 4451 if (unlikely(t & ISO_ERRS)) { 4452 urb->error_count++; 4453 if (t & FOTG210_ISOC_BUF_ERR) 4454 desc->status = usb_pipein(urb->pipe) 4455 ? -ENOSR /* hc couldn't read */ 4456 : -ECOMM; /* hc couldn't write */ 4457 else if (t & FOTG210_ISOC_BABBLE) 4458 desc->status = -EOVERFLOW; 4459 else /* (t & FOTG210_ISOC_XACTERR) */ 4460 desc->status = -EPROTO; 4461 4462 /* HC need not update length with this error */ 4463 if (!(t & FOTG210_ISOC_BABBLE)) { 4464 desc->actual_length = 4465 fotg210_itdlen(urb, desc, t); 4466 urb->actual_length += desc->actual_length; 4467 } 4468 } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) { 4469 desc->status = 0; 4470 desc->actual_length = fotg210_itdlen(urb, desc, t); 4471 urb->actual_length += desc->actual_length; 4472 } else { 4473 /* URB was too late */ 4474 desc->status = -EXDEV; 4475 } 4476 } 4477 4478 /* handle completion now? */ 4479 if (likely((urb_index + 1) != urb->number_of_packets)) 4480 goto done; 4481 4482 /* ASSERT: it's really the last itd for this urb 4483 * list_for_each_entry (itd, &stream->td_list, itd_list) 4484 * BUG_ON (itd->urb == urb); 4485 */ 4486 4487 /* give urb back to the driver; completion often (re)submits */ 4488 dev = urb->dev; 4489 fotg210_urb_done(fotg210, urb, 0); 4490 retval = true; 4491 urb = NULL; 4492 4493 --fotg210->isoc_count; 4494 disable_periodic(fotg210); 4495 4496 if (unlikely(list_is_singular(&stream->td_list))) { 4497 fotg210_to_hcd(fotg210)->self.bandwidth_allocated 4498 -= stream->bandwidth; 4499 fotg210_dbg(fotg210, 4500 "deschedule devp %s ep%d%s-iso\n", 4501 dev->devpath, stream->bEndpointAddress & 0x0f, 4502 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out"); 4503 } 4504 4505done: 4506 itd->urb = NULL; 4507 4508 /* Add to the end of the free list for later reuse */ 4509 list_move_tail(&itd->itd_list, &stream->free_list); 4510 4511 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */ 4512 if (list_empty(&stream->td_list)) { 4513 list_splice_tail_init(&stream->free_list, 4514 &fotg210->cached_itd_list); 4515 start_free_itds(fotg210); 4516 } 4517 4518 return retval; 4519} 4520 4521static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb, 4522 gfp_t mem_flags) 4523{ 4524 int status = -EINVAL; 4525 unsigned long flags; 4526 struct fotg210_iso_stream *stream; 4527 4528 /* Get iso_stream head */ 4529 stream = iso_stream_find(fotg210, urb); 4530 if (unlikely(stream == NULL)) { 4531 fotg210_dbg(fotg210, "can't get iso stream\n"); 4532 return -ENOMEM; 4533 } 4534 if (unlikely(urb->interval != stream->interval && 4535 fotg210_port_speed(fotg210, 0) == 4536 USB_PORT_STAT_HIGH_SPEED)) { 4537 fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n", 4538 stream->interval, urb->interval); 4539 goto done; 4540 } 4541 4542#ifdef FOTG210_URB_TRACE 4543 fotg210_dbg(fotg210, 4544 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n", 4545 __func__, urb->dev->devpath, urb, 4546 usb_pipeendpoint(urb->pipe), 4547 usb_pipein(urb->pipe) ? "in" : "out", 4548 urb->transfer_buffer_length, 4549 urb->number_of_packets, urb->interval, 4550 stream); 4551#endif 4552 4553 /* allocate ITDs w/o locking anything */ 4554 status = itd_urb_transaction(stream, fotg210, urb, mem_flags); 4555 if (unlikely(status < 0)) { 4556 fotg210_dbg(fotg210, "can't init itds\n"); 4557 goto done; 4558 } 4559 4560 /* schedule ... need to lock */ 4561 spin_lock_irqsave(&fotg210->lock, flags); 4562 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) { 4563 status = -ESHUTDOWN; 4564 goto done_not_linked; 4565 } 4566 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb); 4567 if (unlikely(status)) 4568 goto done_not_linked; 4569 status = iso_stream_schedule(fotg210, urb, stream); 4570 if (likely(status == 0)) 4571 itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream); 4572 else 4573 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb); 4574done_not_linked: 4575 spin_unlock_irqrestore(&fotg210->lock, flags); 4576done: 4577 return status; 4578} 4579 4580static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame, 4581 unsigned now_frame, bool live) 4582{ 4583 unsigned uf; 4584 bool modified; 4585 union fotg210_shadow q, *q_p; 4586 __hc32 type, *hw_p; 4587 4588 /* scan each element in frame's queue for completions */ 4589 q_p = &fotg210->pshadow[frame]; 4590 hw_p = &fotg210->periodic[frame]; 4591 q.ptr = q_p->ptr; 4592 type = Q_NEXT_TYPE(fotg210, *hw_p); 4593 modified = false; 4594 4595 while (q.ptr) { 4596 switch (hc32_to_cpu(fotg210, type)) { 4597 case Q_TYPE_ITD: 4598 /* If this ITD is still active, leave it for 4599 * later processing ... check the next entry. 4600 * No need to check for activity unless the 4601 * frame is current. 4602 */ 4603 if (frame == now_frame && live) { 4604 rmb(); 4605 for (uf = 0; uf < 8; uf++) { 4606 if (q.itd->hw_transaction[uf] & 4607 ITD_ACTIVE(fotg210)) 4608 break; 4609 } 4610 if (uf < 8) { 4611 q_p = &q.itd->itd_next; 4612 hw_p = &q.itd->hw_next; 4613 type = Q_NEXT_TYPE(fotg210, 4614 q.itd->hw_next); 4615 q = *q_p; 4616 break; 4617 } 4618 } 4619 4620 /* Take finished ITDs out of the schedule 4621 * and process them: recycle, maybe report 4622 * URB completion. HC won't cache the 4623 * pointer for much longer, if at all. 4624 */ 4625 *q_p = q.itd->itd_next; 4626 *hw_p = q.itd->hw_next; 4627 type = Q_NEXT_TYPE(fotg210, q.itd->hw_next); 4628 wmb(); 4629 modified = itd_complete(fotg210, q.itd); 4630 q = *q_p; 4631 break; 4632 default: 4633 fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n", 4634 type, frame, q.ptr); 4635 fallthrough; 4636 case Q_TYPE_QH: 4637 case Q_TYPE_FSTN: 4638 /* End of the iTDs and siTDs */ 4639 q.ptr = NULL; 4640 break; 4641 } 4642 4643 /* assume completion callbacks modify the queue */ 4644 if (unlikely(modified && fotg210->isoc_count > 0)) 4645 return -EINVAL; 4646 } 4647 return 0; 4648} 4649 4650static void scan_isoc(struct fotg210_hcd *fotg210) 4651{ 4652 unsigned uf, now_frame, frame, ret; 4653 unsigned fmask = fotg210->periodic_size - 1; 4654 bool live; 4655 4656 /* 4657 * When running, scan from last scan point up to "now" 4658 * else clean up by scanning everything that's left. 4659 * Touches as few pages as possible: cache-friendly. 4660 */ 4661 if (fotg210->rh_state >= FOTG210_RH_RUNNING) { 4662 uf = fotg210_read_frame_index(fotg210); 4663 now_frame = (uf >> 3) & fmask; 4664 live = true; 4665 } else { 4666 now_frame = (fotg210->next_frame - 1) & fmask; 4667 live = false; 4668 } 4669 fotg210->now_frame = now_frame; 4670 4671 frame = fotg210->next_frame; 4672 for (;;) { 4673 ret = 1; 4674 while (ret != 0) 4675 ret = scan_frame_queue(fotg210, frame, 4676 now_frame, live); 4677 4678 /* Stop when we have reached the current frame */ 4679 if (frame == now_frame) 4680 break; 4681 frame = (frame + 1) & fmask; 4682 } 4683 fotg210->next_frame = now_frame; 4684} 4685 4686/* Display / Set uframe_periodic_max 4687 */ 4688static ssize_t uframe_periodic_max_show(struct device *dev, 4689 struct device_attribute *attr, char *buf) 4690{ 4691 struct fotg210_hcd *fotg210; 4692 int n; 4693 4694 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev))); 4695 n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max); 4696 return n; 4697} 4698 4699 4700static ssize_t uframe_periodic_max_store(struct device *dev, 4701 struct device_attribute *attr, const char *buf, size_t count) 4702{ 4703 struct fotg210_hcd *fotg210; 4704 unsigned uframe_periodic_max; 4705 unsigned frame, uframe; 4706 unsigned short allocated_max; 4707 unsigned long flags; 4708 ssize_t ret; 4709 4710 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev))); 4711 if (kstrtouint(buf, 0, &uframe_periodic_max) < 0) 4712 return -EINVAL; 4713 4714 if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) { 4715 fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n", 4716 uframe_periodic_max); 4717 return -EINVAL; 4718 } 4719 4720 ret = -EINVAL; 4721 4722 /* 4723 * lock, so that our checking does not race with possible periodic 4724 * bandwidth allocation through submitting new urbs. 4725 */ 4726 spin_lock_irqsave(&fotg210->lock, flags); 4727 4728 /* 4729 * for request to decrease max periodic bandwidth, we have to check 4730 * every microframe in the schedule to see whether the decrease is 4731 * possible. 4732 */ 4733 if (uframe_periodic_max < fotg210->uframe_periodic_max) { 4734 allocated_max = 0; 4735 4736 for (frame = 0; frame < fotg210->periodic_size; ++frame) 4737 for (uframe = 0; uframe < 7; ++uframe) 4738 allocated_max = max(allocated_max, 4739 periodic_usecs(fotg210, frame, 4740 uframe)); 4741 4742 if (allocated_max > uframe_periodic_max) { 4743 fotg210_info(fotg210, 4744 "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n", 4745 allocated_max, uframe_periodic_max); 4746 goto out_unlock; 4747 } 4748 } 4749 4750 /* increasing is always ok */ 4751 4752 fotg210_info(fotg210, 4753 "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n", 4754 100 * uframe_periodic_max/125, uframe_periodic_max); 4755 4756 if (uframe_periodic_max != 100) 4757 fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n"); 4758 4759 fotg210->uframe_periodic_max = uframe_periodic_max; 4760 ret = count; 4761 4762out_unlock: 4763 spin_unlock_irqrestore(&fotg210->lock, flags); 4764 return ret; 4765} 4766 4767static DEVICE_ATTR_RW(uframe_periodic_max); 4768 4769static inline int create_sysfs_files(struct fotg210_hcd *fotg210) 4770{ 4771 struct device *controller = fotg210_to_hcd(fotg210)->self.controller; 4772 4773 return device_create_file(controller, &dev_attr_uframe_periodic_max); 4774} 4775 4776static inline void remove_sysfs_files(struct fotg210_hcd *fotg210) 4777{ 4778 struct device *controller = fotg210_to_hcd(fotg210)->self.controller; 4779 4780 device_remove_file(controller, &dev_attr_uframe_periodic_max); 4781} 4782/* On some systems, leaving remote wakeup enabled prevents system shutdown. 4783 * The firmware seems to think that powering off is a wakeup event! 4784 * This routine turns off remote wakeup and everything else, on all ports. 4785 */ 4786static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210) 4787{ 4788 u32 __iomem *status_reg = &fotg210->regs->port_status; 4789 4790 fotg210_writel(fotg210, PORT_RWC_BITS, status_reg); 4791} 4792 4793/* Halt HC, turn off all ports, and let the BIOS use the companion controllers. 4794 * Must be called with interrupts enabled and the lock not held. 4795 */ 4796static void fotg210_silence_controller(struct fotg210_hcd *fotg210) 4797{ 4798 fotg210_halt(fotg210); 4799 4800 spin_lock_irq(&fotg210->lock); 4801 fotg210->rh_state = FOTG210_RH_HALTED; 4802 fotg210_turn_off_all_ports(fotg210); 4803 spin_unlock_irq(&fotg210->lock); 4804} 4805 4806/* fotg210_shutdown kick in for silicon on any bus (not just pci, etc). 4807 * This forcibly disables dma and IRQs, helping kexec and other cases 4808 * where the next system software may expect clean state. 4809 */ 4810static void fotg210_shutdown(struct usb_hcd *hcd) 4811{ 4812 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 4813 4814 spin_lock_irq(&fotg210->lock); 4815 fotg210->shutdown = true; 4816 fotg210->rh_state = FOTG210_RH_STOPPING; 4817 fotg210->enabled_hrtimer_events = 0; 4818 spin_unlock_irq(&fotg210->lock); 4819 4820 fotg210_silence_controller(fotg210); 4821 4822 hrtimer_cancel(&fotg210->hrtimer); 4823} 4824 4825/* fotg210_work is called from some interrupts, timers, and so on. 4826 * it calls driver completion functions, after dropping fotg210->lock. 4827 */ 4828static void fotg210_work(struct fotg210_hcd *fotg210) 4829{ 4830 /* another CPU may drop fotg210->lock during a schedule scan while 4831 * it reports urb completions. this flag guards against bogus 4832 * attempts at re-entrant schedule scanning. 4833 */ 4834 if (fotg210->scanning) { 4835 fotg210->need_rescan = true; 4836 return; 4837 } 4838 fotg210->scanning = true; 4839 4840rescan: 4841 fotg210->need_rescan = false; 4842 if (fotg210->async_count) 4843 scan_async(fotg210); 4844 if (fotg210->intr_count > 0) 4845 scan_intr(fotg210); 4846 if (fotg210->isoc_count > 0) 4847 scan_isoc(fotg210); 4848 if (fotg210->need_rescan) 4849 goto rescan; 4850 fotg210->scanning = false; 4851 4852 /* the IO watchdog guards against hardware or driver bugs that 4853 * misplace IRQs, and should let us run completely without IRQs. 4854 * such lossage has been observed on both VT6202 and VT8235. 4855 */ 4856 turn_on_io_watchdog(fotg210); 4857} 4858 4859/* Called when the fotg210_hcd module is removed. 4860 */ 4861static void fotg210_stop(struct usb_hcd *hcd) 4862{ 4863 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 4864 4865 fotg210_dbg(fotg210, "stop\n"); 4866 4867 /* no more interrupts ... */ 4868 4869 spin_lock_irq(&fotg210->lock); 4870 fotg210->enabled_hrtimer_events = 0; 4871 spin_unlock_irq(&fotg210->lock); 4872 4873 fotg210_quiesce(fotg210); 4874 fotg210_silence_controller(fotg210); 4875 fotg210_reset(fotg210); 4876 4877 hrtimer_cancel(&fotg210->hrtimer); 4878 remove_sysfs_files(fotg210); 4879 remove_debug_files(fotg210); 4880 4881 /* root hub is shut down separately (first, when possible) */ 4882 spin_lock_irq(&fotg210->lock); 4883 end_free_itds(fotg210); 4884 spin_unlock_irq(&fotg210->lock); 4885 fotg210_mem_cleanup(fotg210); 4886 4887#ifdef FOTG210_STATS 4888 fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n", 4889 fotg210->stats.normal, fotg210->stats.error, 4890 fotg210->stats.iaa, fotg210->stats.lost_iaa); 4891 fotg210_dbg(fotg210, "complete %ld unlink %ld\n", 4892 fotg210->stats.complete, fotg210->stats.unlink); 4893#endif 4894 4895 dbg_status(fotg210, "fotg210_stop completed", 4896 fotg210_readl(fotg210, &fotg210->regs->status)); 4897} 4898 4899/* one-time init, only for memory state */ 4900static int hcd_fotg210_init(struct usb_hcd *hcd) 4901{ 4902 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 4903 u32 temp; 4904 int retval; 4905 u32 hcc_params; 4906 struct fotg210_qh_hw *hw; 4907 4908 spin_lock_init(&fotg210->lock); 4909 4910 /* 4911 * keep io watchdog by default, those good HCDs could turn off it later 4912 */ 4913 fotg210->need_io_watchdog = 1; 4914 4915 hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 4916 fotg210->hrtimer.function = fotg210_hrtimer_func; 4917 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT; 4918 4919 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params); 4920 4921 /* 4922 * by default set standard 80% (== 100 usec/uframe) max periodic 4923 * bandwidth as required by USB 2.0 4924 */ 4925 fotg210->uframe_periodic_max = 100; 4926 4927 /* 4928 * hw default: 1K periodic list heads, one per frame. 4929 * periodic_size can shrink by USBCMD update if hcc_params allows. 4930 */ 4931 fotg210->periodic_size = DEFAULT_I_TDPS; 4932 INIT_LIST_HEAD(&fotg210->intr_qh_list); 4933 INIT_LIST_HEAD(&fotg210->cached_itd_list); 4934 4935 if (HCC_PGM_FRAMELISTLEN(hcc_params)) { 4936 /* periodic schedule size can be smaller than default */ 4937 switch (FOTG210_TUNE_FLS) { 4938 case 0: 4939 fotg210->periodic_size = 1024; 4940 break; 4941 case 1: 4942 fotg210->periodic_size = 512; 4943 break; 4944 case 2: 4945 fotg210->periodic_size = 256; 4946 break; 4947 default: 4948 BUG(); 4949 } 4950 } 4951 retval = fotg210_mem_init(fotg210, GFP_KERNEL); 4952 if (retval < 0) 4953 return retval; 4954 4955 /* controllers may cache some of the periodic schedule ... */ 4956 fotg210->i_thresh = 2; 4957 4958 /* 4959 * dedicate a qh for the async ring head, since we couldn't unlink 4960 * a 'real' qh without stopping the async schedule [4.8]. use it 4961 * as the 'reclamation list head' too. 4962 * its dummy is used in hw_alt_next of many tds, to prevent the qh 4963 * from automatically advancing to the next td after short reads. 4964 */ 4965 fotg210->async->qh_next.qh = NULL; 4966 hw = fotg210->async->hw; 4967 hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma); 4968 hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD); 4969 hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT); 4970 hw->hw_qtd_next = FOTG210_LIST_END(fotg210); 4971 fotg210->async->qh_state = QH_STATE_LINKED; 4972 hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma); 4973 4974 /* clear interrupt enables, set irq latency */ 4975 if (log2_irq_thresh < 0 || log2_irq_thresh > 6) 4976 log2_irq_thresh = 0; 4977 temp = 1 << (16 + log2_irq_thresh); 4978 if (HCC_CANPARK(hcc_params)) { 4979 /* HW default park == 3, on hardware that supports it (like 4980 * NVidia and ALI silicon), maximizes throughput on the async 4981 * schedule by avoiding QH fetches between transfers. 4982 * 4983 * With fast usb storage devices and NForce2, "park" seems to 4984 * make problems: throughput reduction (!), data errors... 4985 */ 4986 if (park) { 4987 park = min_t(unsigned, park, 3); 4988 temp |= CMD_PARK; 4989 temp |= park << 8; 4990 } 4991 fotg210_dbg(fotg210, "park %d\n", park); 4992 } 4993 if (HCC_PGM_FRAMELISTLEN(hcc_params)) { 4994 /* periodic schedule size can be smaller than default */ 4995 temp &= ~(3 << 2); 4996 temp |= (FOTG210_TUNE_FLS << 2); 4997 } 4998 fotg210->command = temp; 4999 5000 /* Accept arbitrarily long scatter-gather lists */ 5001 if (!hcd->localmem_pool) 5002 hcd->self.sg_tablesize = ~0; 5003 return 0; 5004} 5005 5006/* start HC running; it's halted, hcd_fotg210_init() has been run (once) */ 5007static int fotg210_run(struct usb_hcd *hcd) 5008{ 5009 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5010 u32 temp; 5011 5012 hcd->uses_new_polling = 1; 5013 5014 /* EHCI spec section 4.1 */ 5015 5016 fotg210_writel(fotg210, fotg210->periodic_dma, 5017 &fotg210->regs->frame_list); 5018 fotg210_writel(fotg210, (u32)fotg210->async->qh_dma, 5019 &fotg210->regs->async_next); 5020 5021 /* 5022 * hcc_params controls whether fotg210->regs->segment must (!!!) 5023 * be used; it constrains QH/ITD/SITD and QTD locations. 5024 * dma_pool consistent memory always uses segment zero. 5025 * streaming mappings for I/O buffers, like pci_map_single(), 5026 * can return segments above 4GB, if the device allows. 5027 * 5028 * NOTE: the dma mask is visible through dev->dma_mask, so 5029 * drivers can pass this info along ... like NETIF_F_HIGHDMA, 5030 * Scsi_Host.highmem_io, and so forth. It's readonly to all 5031 * host side drivers though. 5032 */ 5033 fotg210_readl(fotg210, &fotg210->caps->hcc_params); 5034 5035 /* 5036 * Philips, Intel, and maybe others need CMD_RUN before the 5037 * root hub will detect new devices (why?); NEC doesn't 5038 */ 5039 fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET); 5040 fotg210->command |= CMD_RUN; 5041 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command); 5042 dbg_cmd(fotg210, "init", fotg210->command); 5043 5044 /* 5045 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices 5046 * are explicitly handed to companion controller(s), so no TT is 5047 * involved with the root hub. (Except where one is integrated, 5048 * and there's no companion controller unless maybe for USB OTG.) 5049 * 5050 * Turning on the CF flag will transfer ownership of all ports 5051 * from the companions to the EHCI controller. If any of the 5052 * companions are in the middle of a port reset at the time, it 5053 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem 5054 * guarantees that no resets are in progress. After we set CF, 5055 * a short delay lets the hardware catch up; new resets shouldn't 5056 * be started before the port switching actions could complete. 5057 */ 5058 down_write(&ehci_cf_port_reset_rwsem); 5059 fotg210->rh_state = FOTG210_RH_RUNNING; 5060 /* unblock posted writes */ 5061 fotg210_readl(fotg210, &fotg210->regs->command); 5062 usleep_range(5000, 10000); 5063 up_write(&ehci_cf_port_reset_rwsem); 5064 fotg210->last_periodic_enable = ktime_get_real(); 5065 5066 temp = HC_VERSION(fotg210, 5067 fotg210_readl(fotg210, &fotg210->caps->hc_capbase)); 5068 fotg210_info(fotg210, 5069 "USB %x.%x started, EHCI %x.%02x\n", 5070 ((fotg210->sbrn & 0xf0) >> 4), (fotg210->sbrn & 0x0f), 5071 temp >> 8, temp & 0xff); 5072 5073 fotg210_writel(fotg210, INTR_MASK, 5074 &fotg210->regs->intr_enable); /* Turn On Interrupts */ 5075 5076 /* GRR this is run-once init(), being done every time the HC starts. 5077 * So long as they're part of class devices, we can't do it init() 5078 * since the class device isn't created that early. 5079 */ 5080 create_debug_files(fotg210); 5081 create_sysfs_files(fotg210); 5082 5083 return 0; 5084} 5085 5086static int fotg210_setup(struct usb_hcd *hcd) 5087{ 5088 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5089 int retval; 5090 5091 fotg210->regs = (void __iomem *)fotg210->caps + 5092 HC_LENGTH(fotg210, 5093 fotg210_readl(fotg210, &fotg210->caps->hc_capbase)); 5094 dbg_hcs_params(fotg210, "reset"); 5095 dbg_hcc_params(fotg210, "reset"); 5096 5097 /* cache this readonly data; minimize chip reads */ 5098 fotg210->hcs_params = fotg210_readl(fotg210, 5099 &fotg210->caps->hcs_params); 5100 5101 fotg210->sbrn = HCD_USB2; 5102 5103 /* data structure init */ 5104 retval = hcd_fotg210_init(hcd); 5105 if (retval) 5106 return retval; 5107 5108 retval = fotg210_halt(fotg210); 5109 if (retval) 5110 return retval; 5111 5112 fotg210_reset(fotg210); 5113 5114 return 0; 5115} 5116 5117static irqreturn_t fotg210_irq(struct usb_hcd *hcd) 5118{ 5119 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5120 u32 status, masked_status, pcd_status = 0, cmd; 5121 int bh; 5122 5123 spin_lock(&fotg210->lock); 5124 5125 status = fotg210_readl(fotg210, &fotg210->regs->status); 5126 5127 /* e.g. cardbus physical eject */ 5128 if (status == ~(u32) 0) { 5129 fotg210_dbg(fotg210, "device removed\n"); 5130 goto dead; 5131 } 5132 5133 /* 5134 * We don't use STS_FLR, but some controllers don't like it to 5135 * remain on, so mask it out along with the other status bits. 5136 */ 5137 masked_status = status & (INTR_MASK | STS_FLR); 5138 5139 /* Shared IRQ? */ 5140 if (!masked_status || 5141 unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) { 5142 spin_unlock(&fotg210->lock); 5143 return IRQ_NONE; 5144 } 5145 5146 /* clear (just) interrupts */ 5147 fotg210_writel(fotg210, masked_status, &fotg210->regs->status); 5148 cmd = fotg210_readl(fotg210, &fotg210->regs->command); 5149 bh = 0; 5150 5151 /* unrequested/ignored: Frame List Rollover */ 5152 dbg_status(fotg210, "irq", status); 5153 5154 /* INT, ERR, and IAA interrupt rates can be throttled */ 5155 5156 /* normal [4.15.1.2] or error [4.15.1.1] completion */ 5157 if (likely((status & (STS_INT|STS_ERR)) != 0)) { 5158 if (likely((status & STS_ERR) == 0)) 5159 INCR(fotg210->stats.normal); 5160 else 5161 INCR(fotg210->stats.error); 5162 bh = 1; 5163 } 5164 5165 /* complete the unlinking of some qh [4.15.2.3] */ 5166 if (status & STS_IAA) { 5167 5168 /* Turn off the IAA watchdog */ 5169 fotg210->enabled_hrtimer_events &= 5170 ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG); 5171 5172 /* 5173 * Mild optimization: Allow another IAAD to reset the 5174 * hrtimer, if one occurs before the next expiration. 5175 * In theory we could always cancel the hrtimer, but 5176 * tests show that about half the time it will be reset 5177 * for some other event anyway. 5178 */ 5179 if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG) 5180 ++fotg210->next_hrtimer_event; 5181 5182 /* guard against (alleged) silicon errata */ 5183 if (cmd & CMD_IAAD) 5184 fotg210_dbg(fotg210, "IAA with IAAD still set?\n"); 5185 if (fotg210->async_iaa) { 5186 INCR(fotg210->stats.iaa); 5187 end_unlink_async(fotg210); 5188 } else 5189 fotg210_dbg(fotg210, "IAA with nothing unlinked?\n"); 5190 } 5191 5192 /* remote wakeup [4.3.1] */ 5193 if (status & STS_PCD) { 5194 int pstatus; 5195 u32 __iomem *status_reg = &fotg210->regs->port_status; 5196 5197 /* kick root hub later */ 5198 pcd_status = status; 5199 5200 /* resume root hub? */ 5201 if (fotg210->rh_state == FOTG210_RH_SUSPENDED) 5202 usb_hcd_resume_root_hub(hcd); 5203 5204 pstatus = fotg210_readl(fotg210, status_reg); 5205 5206 if (test_bit(0, &fotg210->suspended_ports) && 5207 ((pstatus & PORT_RESUME) || 5208 !(pstatus & PORT_SUSPEND)) && 5209 (pstatus & PORT_PE) && 5210 fotg210->reset_done[0] == 0) { 5211 5212 /* start 20 msec resume signaling from this port, 5213 * and make hub_wq collect PORT_STAT_C_SUSPEND to 5214 * stop that signaling. Use 5 ms extra for safety, 5215 * like usb_port_resume() does. 5216 */ 5217 fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25); 5218 set_bit(0, &fotg210->resuming_ports); 5219 fotg210_dbg(fotg210, "port 1 remote wakeup\n"); 5220 mod_timer(&hcd->rh_timer, fotg210->reset_done[0]); 5221 } 5222 } 5223 5224 /* PCI errors [4.15.2.4] */ 5225 if (unlikely((status & STS_FATAL) != 0)) { 5226 fotg210_err(fotg210, "fatal error\n"); 5227 dbg_cmd(fotg210, "fatal", cmd); 5228 dbg_status(fotg210, "fatal", status); 5229dead: 5230 usb_hc_died(hcd); 5231 5232 /* Don't let the controller do anything more */ 5233 fotg210->shutdown = true; 5234 fotg210->rh_state = FOTG210_RH_STOPPING; 5235 fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE); 5236 fotg210_writel(fotg210, fotg210->command, 5237 &fotg210->regs->command); 5238 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable); 5239 fotg210_handle_controller_death(fotg210); 5240 5241 /* Handle completions when the controller stops */ 5242 bh = 0; 5243 } 5244 5245 if (bh) 5246 fotg210_work(fotg210); 5247 spin_unlock(&fotg210->lock); 5248 if (pcd_status) 5249 usb_hcd_poll_rh_status(hcd); 5250 return IRQ_HANDLED; 5251} 5252 5253/* non-error returns are a promise to giveback() the urb later 5254 * we drop ownership so next owner (or urb unlink) can get it 5255 * 5256 * urb + dev is in hcd.self.controller.urb_list 5257 * we're queueing TDs onto software and hardware lists 5258 * 5259 * hcd-specific init for hcpriv hasn't been done yet 5260 * 5261 * NOTE: control, bulk, and interrupt share the same code to append TDs 5262 * to a (possibly active) QH, and the same QH scanning code. 5263 */ 5264static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, 5265 gfp_t mem_flags) 5266{ 5267 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5268 struct list_head qtd_list; 5269 5270 INIT_LIST_HEAD(&qtd_list); 5271 5272 switch (usb_pipetype(urb->pipe)) { 5273 case PIPE_CONTROL: 5274 /* qh_completions() code doesn't handle all the fault cases 5275 * in multi-TD control transfers. Even 1KB is rare anyway. 5276 */ 5277 if (urb->transfer_buffer_length > (16 * 1024)) 5278 return -EMSGSIZE; 5279 fallthrough; 5280 /* case PIPE_BULK: */ 5281 default: 5282 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags)) 5283 return -ENOMEM; 5284 return submit_async(fotg210, urb, &qtd_list, mem_flags); 5285 5286 case PIPE_INTERRUPT: 5287 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags)) 5288 return -ENOMEM; 5289 return intr_submit(fotg210, urb, &qtd_list, mem_flags); 5290 5291 case PIPE_ISOCHRONOUS: 5292 return itd_submit(fotg210, urb, mem_flags); 5293 } 5294} 5295 5296/* remove from hardware lists 5297 * completions normally happen asynchronously 5298 */ 5299 5300static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 5301{ 5302 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5303 struct fotg210_qh *qh; 5304 unsigned long flags; 5305 int rc; 5306 5307 spin_lock_irqsave(&fotg210->lock, flags); 5308 rc = usb_hcd_check_unlink_urb(hcd, urb, status); 5309 if (rc) 5310 goto done; 5311 5312 switch (usb_pipetype(urb->pipe)) { 5313 /* case PIPE_CONTROL: */ 5314 /* case PIPE_BULK:*/ 5315 default: 5316 qh = (struct fotg210_qh *) urb->hcpriv; 5317 if (!qh) 5318 break; 5319 switch (qh->qh_state) { 5320 case QH_STATE_LINKED: 5321 case QH_STATE_COMPLETING: 5322 start_unlink_async(fotg210, qh); 5323 break; 5324 case QH_STATE_UNLINK: 5325 case QH_STATE_UNLINK_WAIT: 5326 /* already started */ 5327 break; 5328 case QH_STATE_IDLE: 5329 /* QH might be waiting for a Clear-TT-Buffer */ 5330 qh_completions(fotg210, qh); 5331 break; 5332 } 5333 break; 5334 5335 case PIPE_INTERRUPT: 5336 qh = (struct fotg210_qh *) urb->hcpriv; 5337 if (!qh) 5338 break; 5339 switch (qh->qh_state) { 5340 case QH_STATE_LINKED: 5341 case QH_STATE_COMPLETING: 5342 start_unlink_intr(fotg210, qh); 5343 break; 5344 case QH_STATE_IDLE: 5345 qh_completions(fotg210, qh); 5346 break; 5347 default: 5348 fotg210_dbg(fotg210, "bogus qh %p state %d\n", 5349 qh, qh->qh_state); 5350 goto done; 5351 } 5352 break; 5353 5354 case PIPE_ISOCHRONOUS: 5355 /* itd... */ 5356 5357 /* wait till next completion, do it then. */ 5358 /* completion irqs can wait up to 1024 msec, */ 5359 break; 5360 } 5361done: 5362 spin_unlock_irqrestore(&fotg210->lock, flags); 5363 return rc; 5364} 5365 5366/* bulk qh holds the data toggle */ 5367 5368static void fotg210_endpoint_disable(struct usb_hcd *hcd, 5369 struct usb_host_endpoint *ep) 5370{ 5371 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5372 unsigned long flags; 5373 struct fotg210_qh *qh, *tmp; 5374 5375 /* ASSERT: any requests/urbs are being unlinked */ 5376 /* ASSERT: nobody can be submitting urbs for this any more */ 5377 5378rescan: 5379 spin_lock_irqsave(&fotg210->lock, flags); 5380 qh = ep->hcpriv; 5381 if (!qh) 5382 goto done; 5383 5384 /* endpoints can be iso streams. for now, we don't 5385 * accelerate iso completions ... so spin a while. 5386 */ 5387 if (qh->hw == NULL) { 5388 struct fotg210_iso_stream *stream = ep->hcpriv; 5389 5390 if (!list_empty(&stream->td_list)) 5391 goto idle_timeout; 5392 5393 /* BUG_ON(!list_empty(&stream->free_list)); */ 5394 kfree(stream); 5395 goto done; 5396 } 5397 5398 if (fotg210->rh_state < FOTG210_RH_RUNNING) 5399 qh->qh_state = QH_STATE_IDLE; 5400 switch (qh->qh_state) { 5401 case QH_STATE_LINKED: 5402 case QH_STATE_COMPLETING: 5403 for (tmp = fotg210->async->qh_next.qh; 5404 tmp && tmp != qh; 5405 tmp = tmp->qh_next.qh) 5406 continue; 5407 /* periodic qh self-unlinks on empty, and a COMPLETING qh 5408 * may already be unlinked. 5409 */ 5410 if (tmp) 5411 start_unlink_async(fotg210, qh); 5412 fallthrough; 5413 case QH_STATE_UNLINK: /* wait for hw to finish? */ 5414 case QH_STATE_UNLINK_WAIT: 5415idle_timeout: 5416 spin_unlock_irqrestore(&fotg210->lock, flags); 5417 schedule_timeout_uninterruptible(1); 5418 goto rescan; 5419 case QH_STATE_IDLE: /* fully unlinked */ 5420 if (qh->clearing_tt) 5421 goto idle_timeout; 5422 if (list_empty(&qh->qtd_list)) { 5423 qh_destroy(fotg210, qh); 5424 break; 5425 } 5426 fallthrough; 5427 default: 5428 /* caller was supposed to have unlinked any requests; 5429 * that's not our job. just leak this memory. 5430 */ 5431 fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n", 5432 qh, ep->desc.bEndpointAddress, qh->qh_state, 5433 list_empty(&qh->qtd_list) ? "" : "(has tds)"); 5434 break; 5435 } 5436done: 5437 ep->hcpriv = NULL; 5438 spin_unlock_irqrestore(&fotg210->lock, flags); 5439} 5440 5441static void fotg210_endpoint_reset(struct usb_hcd *hcd, 5442 struct usb_host_endpoint *ep) 5443{ 5444 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5445 struct fotg210_qh *qh; 5446 int eptype = usb_endpoint_type(&ep->desc); 5447 int epnum = usb_endpoint_num(&ep->desc); 5448 int is_out = usb_endpoint_dir_out(&ep->desc); 5449 unsigned long flags; 5450 5451 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT) 5452 return; 5453 5454 spin_lock_irqsave(&fotg210->lock, flags); 5455 qh = ep->hcpriv; 5456 5457 /* For Bulk and Interrupt endpoints we maintain the toggle state 5458 * in the hardware; the toggle bits in udev aren't used at all. 5459 * When an endpoint is reset by usb_clear_halt() we must reset 5460 * the toggle bit in the QH. 5461 */ 5462 if (qh) { 5463 usb_settoggle(qh->dev, epnum, is_out, 0); 5464 if (!list_empty(&qh->qtd_list)) { 5465 WARN_ONCE(1, "clear_halt for a busy endpoint\n"); 5466 } else if (qh->qh_state == QH_STATE_LINKED || 5467 qh->qh_state == QH_STATE_COMPLETING) { 5468 5469 /* The toggle value in the QH can't be updated 5470 * while the QH is active. Unlink it now; 5471 * re-linking will call qh_refresh(). 5472 */ 5473 if (eptype == USB_ENDPOINT_XFER_BULK) 5474 start_unlink_async(fotg210, qh); 5475 else 5476 start_unlink_intr(fotg210, qh); 5477 } 5478 } 5479 spin_unlock_irqrestore(&fotg210->lock, flags); 5480} 5481 5482static int fotg210_get_frame(struct usb_hcd *hcd) 5483{ 5484 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5485 5486 return (fotg210_read_frame_index(fotg210) >> 3) % 5487 fotg210->periodic_size; 5488} 5489 5490/* The EHCI in ChipIdea HDRC cannot be a separate module or device, 5491 * because its registers (and irq) are shared between host/gadget/otg 5492 * functions and in order to facilitate role switching we cannot 5493 * give the fotg210 driver exclusive access to those. 5494 */ 5495MODULE_DESCRIPTION(DRIVER_DESC); 5496MODULE_AUTHOR(DRIVER_AUTHOR); 5497MODULE_LICENSE("GPL"); 5498 5499static const struct hc_driver fotg210_fotg210_hc_driver = { 5500 .description = hcd_name, 5501 .product_desc = "Faraday USB2.0 Host Controller", 5502 .hcd_priv_size = sizeof(struct fotg210_hcd), 5503 5504 /* 5505 * generic hardware linkage 5506 */ 5507 .irq = fotg210_irq, 5508 .flags = HCD_MEMORY | HCD_DMA | HCD_USB2, 5509 5510 /* 5511 * basic lifecycle operations 5512 */ 5513 .reset = hcd_fotg210_init, 5514 .start = fotg210_run, 5515 .stop = fotg210_stop, 5516 .shutdown = fotg210_shutdown, 5517 5518 /* 5519 * managing i/o requests and associated device resources 5520 */ 5521 .urb_enqueue = fotg210_urb_enqueue, 5522 .urb_dequeue = fotg210_urb_dequeue, 5523 .endpoint_disable = fotg210_endpoint_disable, 5524 .endpoint_reset = fotg210_endpoint_reset, 5525 5526 /* 5527 * scheduling support 5528 */ 5529 .get_frame_number = fotg210_get_frame, 5530 5531 /* 5532 * root hub support 5533 */ 5534 .hub_status_data = fotg210_hub_status_data, 5535 .hub_control = fotg210_hub_control, 5536 .bus_suspend = fotg210_bus_suspend, 5537 .bus_resume = fotg210_bus_resume, 5538 5539 .relinquish_port = fotg210_relinquish_port, 5540 .port_handed_over = fotg210_port_handed_over, 5541 5542 .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete, 5543}; 5544 5545static void fotg210_init(struct fotg210_hcd *fotg210) 5546{ 5547 u32 value; 5548 5549 iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY, 5550 &fotg210->regs->gmir); 5551 5552 value = ioread32(&fotg210->regs->otgcsr); 5553 value &= ~OTGCSR_A_BUS_DROP; 5554 value |= OTGCSR_A_BUS_REQ; 5555 iowrite32(value, &fotg210->regs->otgcsr); 5556} 5557 5558/* 5559 * fotg210_hcd_probe - initialize faraday FOTG210 HCDs 5560 * 5561 * Allocates basic resources for this USB host controller, and 5562 * then invokes the start() method for the HCD associated with it 5563 * through the hotplug entry's driver_data. 5564 */ 5565static int fotg210_hcd_probe(struct platform_device *pdev) 5566{ 5567 struct device *dev = &pdev->dev; 5568 struct usb_hcd *hcd; 5569 struct resource *res; 5570 int irq; 5571 int retval = -ENODEV; 5572 struct fotg210_hcd *fotg210; 5573 5574 if (usb_disabled()) 5575 return -ENODEV; 5576 5577 pdev->dev.power.power_state = PMSG_ON; 5578 5579 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 5580 if (!res) { 5581 dev_err(dev, "Found HC with no IRQ. Check %s setup!\n", 5582 dev_name(dev)); 5583 return -ENODEV; 5584 } 5585 5586 irq = res->start; 5587 5588 hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev, 5589 dev_name(dev)); 5590 if (!hcd) { 5591 dev_err(dev, "failed to create hcd with err %d\n", retval); 5592 retval = -ENOMEM; 5593 goto fail_create_hcd; 5594 } 5595 5596 hcd->has_tt = 1; 5597 5598 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 5599 hcd->regs = devm_ioremap_resource(&pdev->dev, res); 5600 if (IS_ERR(hcd->regs)) { 5601 retval = PTR_ERR(hcd->regs); 5602 goto failed_put_hcd; 5603 } 5604 5605 hcd->rsrc_start = res->start; 5606 hcd->rsrc_len = resource_size(res); 5607 5608 fotg210 = hcd_to_fotg210(hcd); 5609 5610 fotg210->caps = hcd->regs; 5611 5612 /* It's OK not to supply this clock */ 5613 fotg210->pclk = clk_get(dev, "PCLK"); 5614 if (!IS_ERR(fotg210->pclk)) { 5615 retval = clk_prepare_enable(fotg210->pclk); 5616 if (retval) { 5617 dev_err(dev, "failed to enable PCLK\n"); 5618 goto failed_put_hcd; 5619 } 5620 } else if (PTR_ERR(fotg210->pclk) == -EPROBE_DEFER) { 5621 /* 5622 * Percolate deferrals, for anything else, 5623 * just live without the clocking. 5624 */ 5625 retval = PTR_ERR(fotg210->pclk); 5626 goto failed_dis_clk; 5627 } 5628 5629 retval = fotg210_setup(hcd); 5630 if (retval) 5631 goto failed_dis_clk; 5632 5633 fotg210_init(fotg210); 5634 5635 retval = usb_add_hcd(hcd, irq, IRQF_SHARED); 5636 if (retval) { 5637 dev_err(dev, "failed to add hcd with err %d\n", retval); 5638 goto failed_dis_clk; 5639 } 5640 device_wakeup_enable(hcd->self.controller); 5641 platform_set_drvdata(pdev, hcd); 5642 5643 return retval; 5644 5645failed_dis_clk: 5646 if (!IS_ERR(fotg210->pclk)) { 5647 clk_disable_unprepare(fotg210->pclk); 5648 clk_put(fotg210->pclk); 5649 } 5650failed_put_hcd: 5651 usb_put_hcd(hcd); 5652fail_create_hcd: 5653 dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval); 5654 return retval; 5655} 5656 5657/* 5658 * fotg210_hcd_remove - shutdown processing for EHCI HCDs 5659 * @dev: USB Host Controller being removed 5660 * 5661 */ 5662static int fotg210_hcd_remove(struct platform_device *pdev) 5663{ 5664 struct usb_hcd *hcd = platform_get_drvdata(pdev); 5665 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd); 5666 5667 if (!IS_ERR(fotg210->pclk)) { 5668 clk_disable_unprepare(fotg210->pclk); 5669 clk_put(fotg210->pclk); 5670 } 5671 5672 usb_remove_hcd(hcd); 5673 usb_put_hcd(hcd); 5674 5675 return 0; 5676} 5677 5678#ifdef CONFIG_OF 5679static const struct of_device_id fotg210_of_match[] = { 5680 { .compatible = "faraday,fotg210" }, 5681 {}, 5682}; 5683MODULE_DEVICE_TABLE(of, fotg210_of_match); 5684#endif 5685 5686static struct platform_driver fotg210_hcd_driver = { 5687 .driver = { 5688 .name = "fotg210-hcd", 5689 .of_match_table = of_match_ptr(fotg210_of_match), 5690 }, 5691 .probe = fotg210_hcd_probe, 5692 .remove = fotg210_hcd_remove, 5693}; 5694 5695static int __init fotg210_hcd_init(void) 5696{ 5697 int retval = 0; 5698 5699 if (usb_disabled()) 5700 return -ENODEV; 5701 5702 pr_info("%s: " DRIVER_DESC "\n", hcd_name); 5703 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded); 5704 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) || 5705 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded)) 5706 pr_warn("Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n"); 5707 5708 pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd\n", 5709 hcd_name, sizeof(struct fotg210_qh), 5710 sizeof(struct fotg210_qtd), 5711 sizeof(struct fotg210_itd)); 5712 5713 fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root); 5714 5715 retval = platform_driver_register(&fotg210_hcd_driver); 5716 if (retval < 0) 5717 goto clean; 5718 return retval; 5719 5720clean: 5721 debugfs_remove(fotg210_debug_root); 5722 fotg210_debug_root = NULL; 5723 5724 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded); 5725 return retval; 5726} 5727module_init(fotg210_hcd_init); 5728 5729static void __exit fotg210_hcd_cleanup(void) 5730{ 5731 platform_driver_unregister(&fotg210_hcd_driver); 5732 debugfs_remove(fotg210_debug_root); 5733 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded); 5734} 5735module_exit(fotg210_hcd_cleanup);