Subversion Repositories shark

Rev

Rev 846 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
846 giacomo 1
/*
2
 * Copyright (c) 2000-2002 by David Brownell
3
 *
4
 * This program is free software; you can redistribute it and/or modify it
5
 * under the terms of the GNU General Public License as published by the
6
 * Free Software Foundation; either version 2 of the License, or (at your
7
 * option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful, but
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
 */
18
 
19
#include <linuxcomp.h>
20
 
21
#include <linux/config.h>
22
 
23
#ifdef CONFIG_USB_DEBUG
24
        #define DEBUG
25
#else
26
        #undef DEBUG
27
#endif
28
 
29
#include <linux/module.h>
30
#include <linux/pci.h>
31
#include <linux/kernel.h>
32
#include <linux/delay.h>
33
#include <linux/ioport.h>
34
#include <linux/sched.h>
35
#include <linux/slab.h>
36
#include <linux/smp_lock.h>
37
#include <linux/errno.h>
38
#include <linux/init.h>
39
#include <linux/timer.h>
40
#include <linux/list.h>
41
#include <linux/interrupt.h>
42
#include <linux/reboot.h>
43
#include <linux/usb.h>
44
#include <linux/moduleparam.h>
45
 
46
#include "../core/hcd.h"
47
 
48
#include <asm/byteorder.h>
49
#include <asm/io.h>
50
#include <asm/irq.h>
51
#include <asm/system.h>
52
#include <asm/unaligned.h>
53
 
54
 
55
/*-------------------------------------------------------------------------*/
56
 
57
/*
58
 * EHCI hc_driver implementation ... experimental, incomplete.
59
 * Based on the final 1.0 register interface specification.
60
 *
61
 * USB 2.0 shows up in upcoming www.pcmcia.org technology.
62
 * First was PCMCIA, like ISA; then CardBus, which is PCI.
63
 * Next comes "CardBay", using USB 2.0 signals.
64
 *
65
 * Contains additional contributions by Brad Hards, Rory Bolt, and others.
66
 * Special thanks to Intel and VIA for providing host controllers to
67
 * test this driver on, and Cypress (including In-System Design) for
68
 * providing early devices for those host controllers to talk to!
69
 *
70
 * HISTORY:
71
 *
72
 * 2002-11-29   Correct handling for hw async_next register.
73
 * 2002-08-06   Handling for bulk and interrupt transfers is mostly shared;
74
 *      only scheduling is different, no arbitrary limitations.
75
 * 2002-07-25   Sanity check PCI reads, mostly for better cardbus support,
76
 *      clean up HC run state handshaking.
77
 * 2002-05-24   Preliminary FS/LS interrupts, using scheduling shortcuts
78
 * 2002-05-11   Clear TT errors for FS/LS ctrl/bulk.  Fill in some other
79
 *      missing pieces:  enabling 64bit dma, handoff from BIOS/SMM.
80
 * 2002-05-07   Some error path cleanups to report better errors; wmb();
81
 *      use non-CVS version id; better iso bandwidth claim.
82
 * 2002-04-19   Control/bulk/interrupt submit no longer uses giveback() on
83
 *      errors in submit path.  Bugfixes to interrupt scheduling/processing.
84
 * 2002-03-05   Initial high-speed ISO support; reduce ITD memory; shift
85
 *      more checking to generic hcd framework (db).  Make it work with
86
 *      Philips EHCI; reduce PCI traffic; shorten IRQ path (Rory Bolt).
87
 * 2002-01-14   Minor cleanup; version synch.
88
 * 2002-01-08   Fix roothub handoff of FS/LS to companion controllers.
89
 * 2002-01-04   Control/Bulk queuing behaves.
90
 *
91
 * 2001-12-12   Initial patch version for Linux 2.5.1 kernel.
92
 * 2001-June    Works with usb-storage and NEC EHCI on 2.4
93
 */
94
 
95
#define DRIVER_VERSION "2003-Jun-13"
96
#define DRIVER_AUTHOR "David Brownell"
97
#define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
98
 
99
static const char       hcd_name [] = "ehci_hcd";
100
 
101
 
102
// #define EHCI_VERBOSE_DEBUG
103
// #define have_split_iso
104
 
105
#ifdef DEBUG
106
#define EHCI_STATS
107
#endif
108
 
109
/* magic numbers that can affect system performance */
110
#define EHCI_TUNE_CERR          3       /* 0-3 qtd retries; 0 == don't stop */
111
#define EHCI_TUNE_RL_HS         4       /* nak throttle; see 4.9 */
112
#define EHCI_TUNE_RL_TT         0
113
#define EHCI_TUNE_MULT_HS       1       /* 1-3 transactions/uframe; 4.10.3 */
114
#define EHCI_TUNE_MULT_TT       1
115
#define EHCI_TUNE_FLS           2       /* (small) 256 frame schedule */
116
 
117
#define EHCI_IAA_JIFFIES        (HZ/100)        /* arbitrary; ~10 msec */
118
#define EHCI_IO_JIFFIES         (HZ/10)         /* io watchdog > irq_thresh */
119
#define EHCI_ASYNC_JIFFIES      (HZ/20)         /* async idle timeout */
120
#define EHCI_SHRINK_JIFFIES     (HZ/200)        /* async qh unlink delay */
121
 
122
/* Initial IRQ latency:  lower than default */
123
static int log2_irq_thresh = 0;         // 0 to 6
1049 mauro 124
 
125
/*module_param (log2_irq_thresh, int, S_IRUGO);
846 giacomo 126
MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
1049 mauro 127
*/
846 giacomo 128
 
129
#define INTR_MASK (STS_IAA | STS_FATAL | STS_ERR | STS_INT)
130
 
131
/*-------------------------------------------------------------------------*/
132
 
133
#include "ehci.h"
134
#include "ehci-dbg.c"
135
 
136
/*-------------------------------------------------------------------------*/
137
 
138
/*
139
 * handshake - spin reading hc until handshake completes or fails
140
 * @ptr: address of hc register to be read
141
 * @mask: bits to look at in result of read
142
 * @done: value of those bits when handshake succeeds
143
 * @usec: timeout in microseconds
144
 *
145
 * Returns negative errno, or zero on success
146
 *
147
 * Success happens when the "mask" bits have the specified value (hardware
148
 * handshake done).  There are two failure modes:  "usec" have passed (major
149
 * hardware flakeout), or the register reads as all-ones (hardware removed).
150
 *
151
 * That last failure should_only happen in cases like physical cardbus eject
152
 * before driver shutdown. But it also seems to be caused by bugs in cardbus
153
 * bridge shutdown:  shutting down the bridge before the devices using it.
154
 */
155
static int handshake (u32 *ptr, u32 mask, u32 done, int usec)
156
{
157
        u32     result;
158
 
159
        do {
160
                result = readl (ptr);
161
                if (result == ~(u32)0)          /* card removed */
162
                        return -ENODEV;
163
                result &= mask;
164
                if (result == done)
165
                        return 0;
166
                udelay (1);
167
                usec--;
168
        } while (usec > 0);
169
        return -ETIMEDOUT;
170
}
171
 
172
/*
173
 * hc states include: unknown, halted, ready, running
174
 * transitional states are messy just now
175
 * trying to avoid "running" unless urbs are active
176
 * a "ready" hc can be finishing prefetched work
177
 */
178
 
179
/* force HC to halt state from unknown (EHCI spec section 2.3) */
180
static int ehci_halt (struct ehci_hcd *ehci)
181
{
182
        u32     temp = readl (&ehci->regs->status);
183
 
184
        if ((temp & STS_HALT) != 0)
185
                return 0;
186
 
187
        temp = readl (&ehci->regs->command);
188
        temp &= ~CMD_RUN;
189
        writel (temp, &ehci->regs->command);
190
        return handshake (&ehci->regs->status, STS_HALT, STS_HALT, 16 * 125);
191
}
192
 
193
/* reset a non-running (STS_HALT == 1) controller */
194
static int ehci_reset (struct ehci_hcd *ehci)
195
{
196
        u32     command = readl (&ehci->regs->command);
197
 
198
        command |= CMD_RESET;
199
        dbg_cmd (ehci, "reset", command);
200
        writel (command, &ehci->regs->command);
201
        ehci->hcd.state = USB_STATE_HALT;
202
        return handshake (&ehci->regs->command, CMD_RESET, 0, 250 * 1000);
203
}
204
 
205
/* idle the controller (from running) */
206
static void ehci_ready (struct ehci_hcd *ehci)
207
{
208
        u32     temp;
209
 
210
#ifdef DEBUG
211
        if (!HCD_IS_RUNNING (ehci->hcd.state))
212
                BUG ();
213
#endif
214
 
215
        /* wait for any schedule enables/disables to take effect */
216
        temp = 0;
217
        if (ehci->async->qh_next.qh)
218
                temp = STS_ASS;
219
        if (ehci->next_uframe != -1)
220
                temp |= STS_PSS;
221
        if (handshake (&ehci->regs->status, STS_ASS | STS_PSS,
222
                                temp, 16 * 125) != 0) {
223
                ehci->hcd.state = USB_STATE_HALT;
224
                return;
225
        }
226
 
227
        /* then disable anything that's still active */
228
        temp = readl (&ehci->regs->command);
229
        temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
230
        writel (temp, &ehci->regs->command);
231
 
232
        /* hardware can take 16 microframes to turn off ... */
233
        if (handshake (&ehci->regs->status, STS_ASS | STS_PSS,
234
                                0, 16 * 125) != 0) {
235
                ehci->hcd.state = USB_STATE_HALT;
236
                return;
237
        }
238
}
239
 
240
/*-------------------------------------------------------------------------*/
241
 
242
#include "ehci-hub.c"
243
#include "ehci-mem.c"
244
#include "ehci-q.c"
245
#include "ehci-sched.c"
246
 
247
/*-------------------------------------------------------------------------*/
248
 
249
static void ehci_work(struct ehci_hcd *ehci, struct pt_regs *regs);
250
 
251
static void ehci_watchdog (unsigned long param)
252
{
253
        struct ehci_hcd         *ehci = (struct ehci_hcd *) param;
254
        unsigned long           flags;
255
 
256
        spin_lock_irqsave (&ehci->lock, flags);
257
 
258
        /* lost IAA irqs wedge things badly; seen with a vt8235 */
259
        if (ehci->reclaim) {
260
                u32             status = readl (&ehci->regs->status);
261
 
262
                if (status & STS_IAA) {
263
                        ehci_vdbg (ehci, "lost IAA\n");
264
                        COUNT (ehci->stats.lost_iaa);
265
                        writel (STS_IAA, &ehci->regs->status);
266
                        ehci->reclaim_ready = 1;
267
                }
268
        }
269
 
270
        /* stop async processing after it's idled a bit */
271
        if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
272
                start_unlink_async (ehci, ehci->async);
273
 
274
        /* ehci could run by timer, without IRQs ... */
275
        ehci_work (ehci, NULL);
276
 
277
        spin_unlock_irqrestore (&ehci->lock, flags);
278
}
279
 
280
/* EHCI 0.96 (and later) section 5.1 says how to kick BIOS/SMM/...
281
 * off the controller (maybe it can boot from highspeed USB disks).
282
 */
283
static int bios_handoff (struct ehci_hcd *ehci, int where, u32 cap)
284
{
285
        if (cap & (1 << 16)) {
286
                int msec = 500;
287
 
288
                /* request handoff to OS */
289
                cap &= 1 << 24;
290
                pci_write_config_dword (ehci->hcd.pdev, where, cap);
291
 
292
                /* and wait a while for it to happen */
293
                do {
294
                        wait_ms (10);
295
                        msec -= 10;
296
                        pci_read_config_dword (ehci->hcd.pdev, where, &cap);
297
                } while ((cap & (1 << 16)) && msec);
298
                if (cap & (1 << 16)) {
299
                        ehci_err (ehci, "BIOS handoff failed (%d, %04x)\n",
300
                                where, cap);
301
                        return 1;
302
                }
303
                ehci_dbg (ehci, "BIOS handoff succeeded\n");
304
        }
305
        return 0;
306
}
307
 
308
static int
309
ehci_reboot (struct notifier_block *self, unsigned long code, void *null)
310
{
311
        struct ehci_hcd         *ehci;
312
 
313
        ehci = container_of (self, struct ehci_hcd, reboot_notifier);
314
 
315
        /* make BIOS/etc use companion controller during reboot */
316
        writel (0, &ehci->regs->configured_flag);
317
        return 0;
318
}
319
 
320
 
321
/* called by khubd or root hub init threads */
322
 
323
static int ehci_hc_reset (struct usb_hcd *hcd)
324
{
325
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
326
        u32                     temp;
327
 
328
        spin_lock_init (&ehci->lock);
329
 
330
        ehci->caps = (struct ehci_caps *) hcd->regs;
331
        ehci->regs = (struct ehci_regs *) (hcd->regs +
332
                                readb (&ehci->caps->length));
333
        dbg_hcs_params (ehci, "reset");
334
        dbg_hcc_params (ehci, "reset");
335
 
336
        /* EHCI 0.96 and later may have "extended capabilities" */
337
        temp = HCC_EXT_CAPS (readl (&ehci->caps->hcc_params));
338
        while (temp) {
339
                u32             cap;
340
 
341
                pci_read_config_dword (ehci->hcd.pdev, temp, &cap);
342
                ehci_dbg (ehci, "capability %04x at %02x\n", cap, temp);
343
                switch (cap & 0xff) {
344
                case 1:                 /* BIOS/SMM/... handoff */
345
                        if (bios_handoff (ehci, temp, cap) != 0)
346
                                return -EOPNOTSUPP;
347
                        break;
348
                case 0:                 /* illegal reserved capability */
349
                        ehci_warn (ehci, "illegal capability!\n");
350
                        cap = 0;
351
                        /* FALLTHROUGH */
352
                default:                /* unknown */
353
                        break;
354
                }
355
                temp = (cap >> 8) & 0xff;
356
        }
357
 
358
        /* cache this readonly data; minimize PCI reads */
359
        ehci->hcs_params = readl (&ehci->caps->hcs_params);
360
 
361
        /* force HC to halt state */
362
        return ehci_halt (ehci);
363
}
364
 
365
static int ehci_start (struct usb_hcd *hcd)
366
{
367
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
368
        u32                     temp;
369
        struct usb_device       *udev;
370
        struct usb_bus          *bus;
371
        int                     retval;
372
        u32                     hcc_params;
373
        u8                      tempbyte;
374
 
375
        /*
376
         * hw default: 1K periodic list heads, one per frame.
377
         * periodic_size can shrink by USBCMD update if hcc_params allows.
378
         */
379
        ehci->periodic_size = DEFAULT_I_TDPS;
380
        if ((retval = ehci_mem_init (ehci, SLAB_KERNEL)) < 0)
381
                return retval;
382
 
383
        /* controllers may cache some of the periodic schedule ... */
384
        hcc_params = readl (&ehci->caps->hcc_params);
385
        if (HCC_ISOC_CACHE (hcc_params))        // full frame cache
386
                ehci->i_thresh = 8;
387
        else                                    // N microframes cached
388
                ehci->i_thresh = 2 + HCC_ISOC_THRES (hcc_params);
389
 
390
        ehci->reclaim = 0;
391
        ehci->next_uframe = -1;
392
 
393
        /* controller state:  unknown --> reset */
394
 
395
        /* EHCI spec section 4.1 */
396
        if ((retval = ehci_reset (ehci)) != 0) {
397
                ehci_mem_cleanup (ehci);
398
                return retval;
399
        }
400
        writel (INTR_MASK, &ehci->regs->intr_enable);
401
        writel (ehci->periodic_dma, &ehci->regs->frame_list);
402
 
403
        /*
404
         * dedicate a qh for the async ring head, since we couldn't unlink
405
         * a 'real' qh without stopping the async schedule [4.8].  use it
406
         * as the 'reclamation list head' too.
407
         * its dummy is used in hw_alt_next of many tds, to prevent the qh
408
         * from automatically advancing to the next td after short reads.
409
         */
410
        ehci->async->qh_next.qh = 0;
411
        ehci->async->hw_next = QH_NEXT (ehci->async->qh_dma);
412
        ehci->async->hw_info1 = cpu_to_le32 (QH_HEAD);
413
        ehci->async->hw_token = cpu_to_le32 (QTD_STS_HALT);
414
        ehci->async->hw_qtd_next = EHCI_LIST_END;
415
        ehci->async->qh_state = QH_STATE_LINKED;
416
        ehci->async->hw_alt_next = QTD_NEXT (ehci->async->dummy->qtd_dma);
417
        writel ((u32)ehci->async->qh_dma, &ehci->regs->async_next);
418
 
419
        /*
420
         * hcc_params controls whether ehci->regs->segment must (!!!)
421
         * be used; it constrains QH/ITD/SITD and QTD locations.
422
         * pci_pool consistent memory always uses segment zero.
423
         * streaming mappings for I/O buffers, like pci_map_single(),
424
         * can return segments above 4GB, if the device allows.
425
         *
426
         * NOTE:  the dma mask is visible through dma_supported(), so
427
         * drivers can pass this info along ... like NETIF_F_HIGHDMA,
428
         * Scsi_Host.highmem_io, and so forth.  It's readonly to all
429
         * host side drivers though.
430
         */
431
        if (HCC_64BIT_ADDR (hcc_params)) {
432
                writel (0, &ehci->regs->segment);
433
#if 0
434
// this is deeply broken on almost all architectures
435
                if (!pci_set_dma_mask (ehci->hcd.pdev, 0xffffffffffffffffULL))
436
                        ehci_info (ehci, "enabled 64bit PCI DMA\n");
437
#endif
438
        }
439
 
440
        /* help hc dma work well with cachelines */
441
        pci_set_mwi (ehci->hcd.pdev);
442
 
443
        /* clear interrupt enables, set irq latency */
444
        temp = readl (&ehci->regs->command) & 0x0fff;
445
        if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
446
                log2_irq_thresh = 0;
447
        temp |= 1 << (16 + log2_irq_thresh);
448
        // if hc can park (ehci >= 0.96), default is 3 packets per async QH 
449
        if (HCC_PGM_FRAMELISTLEN (hcc_params)) {
450
                /* periodic schedule size can be smaller than default */
451
                temp &= ~(3 << 2);
452
                temp |= (EHCI_TUNE_FLS << 2);
453
                switch (EHCI_TUNE_FLS) {
454
                case 0: ehci->periodic_size = 1024; break;
455
                case 1: ehci->periodic_size = 512; break;
456
                case 2: ehci->periodic_size = 256; break;
457
                default:        BUG ();
458
                }
459
        }
460
        temp &= ~(CMD_IAAD | CMD_ASE | CMD_PSE),
461
        // Philips, Intel, and maybe others need CMD_RUN before the
462
        // root hub will detect new devices (why?); NEC doesn't
463
        temp |= CMD_RUN;
464
        writel (temp, &ehci->regs->command);
465
        dbg_cmd (ehci, "init", temp);
466
 
467
        /* set async sleep time = 10 us ... ? */
468
 
469
        init_timer (&ehci->watchdog);
470
        ehci->watchdog.function = ehci_watchdog;
471
        ehci->watchdog.data = (unsigned long) ehci;
472
 
473
        /* wire up the root hub */
474
        bus = hcd_to_bus (hcd);
475
        bus->root_hub = udev = usb_alloc_dev (NULL, bus);
476
        if (!udev) {
477
done2:
478
                ehci_mem_cleanup (ehci);
479
                return -ENOMEM;
480
        }
481
 
482
        /*
483
         * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
484
         * are explicitly handed to companion controller(s), so no TT is
485
         * involved with the root hub.
486
         */
487
        ehci->reboot_notifier.notifier_call = ehci_reboot;
1049 mauro 488
//      register_reboot_notifier (&ehci->reboot_notifier);
846 giacomo 489
 
490
        ehci->hcd.state = USB_STATE_RUNNING;
491
        writel (FLAG_CF, &ehci->regs->configured_flag);
492
        readl (&ehci->regs->command);   /* unblock posted write */
493
 
494
        /* PCI Serial Bus Release Number is at 0x60 offset */
495
        pci_read_config_byte (hcd->pdev, 0x60, &tempbyte);
496
        temp = readw (&ehci->caps->hci_version);
497
        ehci_info (ehci,
498
                "USB %x.%x enabled, EHCI %x.%02x, driver %s\n",
499
                ((tempbyte & 0xf0)>>4), (tempbyte & 0x0f),
500
                temp >> 8, temp & 0xff, DRIVER_VERSION);
501
 
502
        /*
503
         * From here on, khubd concurrently accesses the root
504
         * hub; drivers will be talking to enumerated devices.
505
         *
506
         * Before this point the HC was idle/ready.  After, khubd
507
         * and device drivers may start it running.
508
         */
509
        udev->speed = USB_SPEED_HIGH;
510
        if (hcd_register_root (hcd) != 0) {
511
                if (hcd->state == USB_STATE_RUNNING)
512
                        ehci_ready (ehci);
513
                ehci_reset (ehci);
514
                bus->root_hub = 0;
515
                usb_put_dev (udev);
516
                retval = -ENODEV;
517
                goto done2;
518
        }
519
 
520
        create_debug_files (ehci);
521
 
522
        return 0;
523
}
524
 
525
/* always called by thread; normally rmmod */
526
 
527
static void ehci_stop (struct usb_hcd *hcd)
528
{
529
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
530
 
531
        ehci_dbg (ehci, "stop\n");
532
 
533
        /* no more interrupts ... */
534
        if (hcd->state == USB_STATE_RUNNING)
535
                ehci_ready (ehci);
536
        if (in_interrupt ()) {          /* must not happen!! */
537
                ehci_err (ehci, "stopped in_interrupt!\n");
538
                return;
539
        }
540
        del_timer_sync (&ehci->watchdog);
541
        ehci_reset (ehci);
542
 
543
        /* let companion controllers work when we aren't */
544
        writel (0, &ehci->regs->configured_flag);
1049 mauro 545
//      unregister_reboot_notifier (&ehci->reboot_notifier);
846 giacomo 546
 
547
        remove_debug_files (ehci);
548
 
549
        /* root hub is shut down separately (first, when possible) */
550
        spin_lock_irq (&ehci->lock);
551
        ehci_work (ehci, NULL);
552
        spin_unlock_irq (&ehci->lock);
553
        ehci_mem_cleanup (ehci);
554
 
555
#ifdef  EHCI_STATS
556
        ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
557
                ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
558
                ehci->stats.lost_iaa);
559
        ehci_dbg (ehci, "complete %ld unlink %ld\n",
560
                ehci->stats.complete, ehci->stats.unlink);
561
#endif
562
 
563
        dbg_status (ehci, "ehci_stop completed", readl (&ehci->regs->status));
564
}
565
 
566
static int ehci_get_frame (struct usb_hcd *hcd)
567
{
568
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
569
        return (readl (&ehci->regs->frame_index) >> 3) % ehci->periodic_size;
570
}
571
 
572
/*-------------------------------------------------------------------------*/
573
 
574
#ifdef  CONFIG_PM
575
 
576
/* suspend/resume, section 4.3 */
577
 
578
static int ehci_suspend (struct usb_hcd *hcd, u32 state)
579
{
580
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
581
        int                     ports;
582
        int                     i;
583
 
584
        ehci_dbg (ehci, "suspend to %d\n", state);
585
 
586
        ports = HCS_N_PORTS (ehci->hcs_params);
587
 
588
        // FIXME:  This assumes what's probably a D3 level suspend...
589
 
590
        // FIXME:  usb wakeup events on this bus should resume the machine.
591
        // pci config register PORTWAKECAP controls which ports can do it;
592
        // bios may have initted the register...
593
 
594
        /* suspend each port, then stop the hc */
595
        for (i = 0; i < ports; i++) {
596
                int     temp = readl (&ehci->regs->port_status [i]);
597
 
598
                if ((temp & PORT_PE) == 0
599
                                || (temp & PORT_OWNER) != 0)
600
                        continue;
601
                ehci_dbg (ehci, "suspend port %d", i);
602
                temp |= PORT_SUSPEND;
603
                writel (temp, &ehci->regs->port_status [i]);
604
        }
605
 
606
        if (hcd->state == USB_STATE_RUNNING)
607
                ehci_ready (ehci);
608
        writel (readl (&ehci->regs->command) & ~CMD_RUN, &ehci->regs->command);
609
 
610
// save pci FLADJ value
611
 
612
        /* who tells PCI to reduce power consumption? */
613
 
614
        return 0;
615
}
616
 
617
static int ehci_resume (struct usb_hcd *hcd)
618
{
619
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
620
        int                     ports;
621
        int                     i;
622
 
623
        ehci_dbg (ehci, "resume\n");
624
 
625
        ports = HCS_N_PORTS (ehci->hcs_params);
626
 
627
        // FIXME:  if controller didn't retain state,
628
        // return and let generic code clean it up
629
        // test configured_flag ?
630
 
631
        /* resume HC and each port */
632
// restore pci FLADJ value
633
        // khubd and drivers will set HC running, if needed;
634
        hcd->state = USB_STATE_RUNNING;
635
        // FIXME Philips/Intel/... etc don't really have a "READY"
636
        // state ... turn on CMD_RUN too
637
        for (i = 0; i < ports; i++) {
638
                int     temp = readl (&ehci->regs->port_status [i]);
639
 
640
                if ((temp & PORT_PE) == 0
641
                                || (temp & PORT_SUSPEND) != 0)
642
                        continue;
643
                ehci_dbg (ehci, "resume port %d", i);
644
                temp |= PORT_RESUME;
645
                writel (temp, &ehci->regs->port_status [i]);
646
                readl (&ehci->regs->command);   /* unblock posted writes */
647
 
648
                wait_ms (20);
649
                temp &= ~PORT_RESUME;
650
                writel (temp, &ehci->regs->port_status [i]);
651
        }
652
        readl (&ehci->regs->command);   /* unblock posted writes */
653
        return 0;
654
}
655
 
656
#endif
657
 
658
/*-------------------------------------------------------------------------*/
659
 
660
/*
661
 * ehci_work is called from some interrupts, timers, and so on.
662
 * it calls driver completion functions, after dropping ehci->lock.
663
 */
664
static void ehci_work (struct ehci_hcd *ehci, struct pt_regs *regs)
665
{
666
        timer_action_done (ehci, TIMER_IO_WATCHDOG);
667
        if (ehci->reclaim_ready)
668
                end_unlink_async (ehci, regs);
669
        scan_async (ehci, regs);
670
        if (ehci->next_uframe != -1)
671
                scan_periodic (ehci, regs);
672
 
673
        /* the IO watchdog guards against hardware or driver bugs that
674
         * misplace IRQs, and should let us run completely without IRQs.
675
         */
676
        if ((ehci->async->qh_next.ptr != 0) || (ehci->periodic_sched != 0))
677
                timer_action (ehci, TIMER_IO_WATCHDOG);
678
}
679
 
680
/*-------------------------------------------------------------------------*/
681
 
682
static void ehci_irq (struct usb_hcd *hcd, struct pt_regs *regs)
683
{
684
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
685
        u32                     status;
686
        int                     bh;
687
 
688
        spin_lock (&ehci->lock);
689
 
690
        status = readl (&ehci->regs->status);
691
 
692
        /* e.g. cardbus physical eject */
693
        if (status == ~(u32) 0) {
694
                ehci_dbg (ehci, "device removed\n");
695
                goto dead;
696
        }
697
 
698
        status &= INTR_MASK;
699
        if (!status)                    /* irq sharing? */
700
                goto done;
701
 
702
        /* clear (just) interrupts */
703
        writel (status, &ehci->regs->status);
704
        readl (&ehci->regs->command);   /* unblock posted write */
705
        bh = 0;
706
 
707
#ifdef  EHCI_VERBOSE_DEBUG
708
        /* unrequested/ignored: Port Change Detect, Frame List Rollover */
709
        dbg_status (ehci, "irq", status);
710
#endif
711
 
712
        /* INT, ERR, and IAA interrupt rates can be throttled */
713
 
714
        /* normal [4.15.1.2] or error [4.15.1.1] completion */
715
        if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
716
                if (likely ((status & STS_ERR) == 0))
717
                        COUNT (ehci->stats.normal);
718
                else
719
                        COUNT (ehci->stats.error);
720
                bh = 1;
721
        }
722
 
723
        /* complete the unlinking of some qh [4.15.2.3] */
724
        if (status & STS_IAA) {
725
                COUNT (ehci->stats.reclaim);
726
                ehci->reclaim_ready = 1;
727
                bh = 1;
728
        }
729
 
730
        /* PCI errors [4.15.2.4] */
731
        if (unlikely ((status & STS_FATAL) != 0)) {
732
                ehci_err (ehci, "fatal error\n");
733
dead:
734
                ehci_reset (ehci);
735
                /* generic layer kills/unlinks all urbs, then
736
                 * uses ehci_stop to clean up the rest
737
                 */
738
                bh = 1;
739
        }
740
 
741
        if (bh)
742
                ehci_work (ehci, regs);
743
done:
744
        spin_unlock (&ehci->lock);
745
}
746
 
747
/*-------------------------------------------------------------------------*/
748
 
749
/*
750
 * non-error returns are a promise to giveback() the urb later
751
 * we drop ownership so next owner (or urb unlink) can get it
752
 *
753
 * urb + dev is in hcd_dev.urb_list
754
 * we're queueing TDs onto software and hardware lists
755
 *
756
 * hcd-specific init for hcpriv hasn't been done yet
757
 *
758
 * NOTE:  control, bulk, and interrupt share the same code to append TDs
759
 * to a (possibly active) QH, and the same QH scanning code.
760
 */
761
static int ehci_urb_enqueue (
762
        struct usb_hcd  *hcd,
763
        struct urb      *urb,
764
        int             mem_flags
765
) {
766
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
767
        struct list_head        qtd_list;
768
 
769
        urb->transfer_flags &= ~EHCI_STATE_UNLINK;
770
        INIT_LIST_HEAD (&qtd_list);
771
 
772
        switch (usb_pipetype (urb->pipe)) {
773
        // case PIPE_CONTROL:
774
        // case PIPE_BULK:
775
        default:
776
                if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
777
                        return -ENOMEM;
778
                return submit_async (ehci, urb, &qtd_list, mem_flags);
779
 
780
        case PIPE_INTERRUPT:
781
                if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
782
                        return -ENOMEM;
783
                return intr_submit (ehci, urb, &qtd_list, mem_flags);
784
 
785
        case PIPE_ISOCHRONOUS:
786
                if (urb->dev->speed == USB_SPEED_HIGH)
787
                        return itd_submit (ehci, urb, mem_flags);
788
#ifdef have_split_iso
789
                else
790
                        return sitd_submit (ehci, urb, mem_flags);
791
#else
792
                dbg ("no split iso support yet");
793
                return -ENOSYS;
794
#endif /* have_split_iso */
795
        }
796
}
797
 
798
/* remove from hardware lists
799
 * completions normally happen asynchronously
800
 */
801
 
802
static int ehci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
803
{
804
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
805
        struct ehci_qh          *qh;
806
        unsigned long           flags;
807
 
808
        spin_lock_irqsave (&ehci->lock, flags);
809
        switch (usb_pipetype (urb->pipe)) {
810
        // case PIPE_CONTROL:
811
        // case PIPE_BULK:
812
        default:
813
                qh = (struct ehci_qh *) urb->hcpriv;
814
                if (!qh)
815
                        break;
816
 
817
                /* if we need to use IAA and it's busy, defer */
818
                if (qh->qh_state == QH_STATE_LINKED
819
                                && ehci->reclaim
820
                                && HCD_IS_RUNNING (ehci->hcd.state)
821
                                ) {
822
                        struct ehci_qh          *last;
823
 
824
                        for (last = ehci->reclaim;
825
                                        last->reclaim;
826
                                        last = last->reclaim)
827
                                continue;
828
                        qh->qh_state = QH_STATE_UNLINK_WAIT;
829
                        last->reclaim = qh;
830
 
831
                /* bypass IAA if the hc can't care */
832
                } else if (!HCD_IS_RUNNING (ehci->hcd.state) && ehci->reclaim)
833
                        end_unlink_async (ehci, NULL);
834
 
835
                /* something else might have unlinked the qh by now */
836
                if (qh->qh_state == QH_STATE_LINKED)
837
                        start_unlink_async (ehci, qh);
838
                break;
839
 
840
        case PIPE_INTERRUPT:
841
                qh = (struct ehci_qh *) urb->hcpriv;
842
                if (!qh)
843
                        break;
844
                if (qh->qh_state == QH_STATE_LINKED) {
845
                        /* messy, can spin or block a microframe ... */
846
                        intr_deschedule (ehci, qh, 1);
847
                        /* qh_state == IDLE */
848
                }
849
                qh_completions (ehci, qh, NULL);
850
 
851
                /* reschedule QH iff another request is queued */
852
                if (!list_empty (&qh->qtd_list)
853
                                && HCD_IS_RUNNING (ehci->hcd.state)) {
854
                        int status;
855
 
856
                        status = qh_schedule (ehci, qh);
857
                        spin_unlock_irqrestore (&ehci->lock, flags);
858
 
859
                        if (status != 0) {
860
                                // shouldn't happen often, but ...
861
                                // FIXME kill those tds' urbs
862
                                err ("can't reschedule qh %p, err %d",
863
                                        qh, status);
864
                        }
865
                        return status;
866
                }
867
                break;
868
 
869
        case PIPE_ISOCHRONOUS:
870
                // itd or sitd ...
871
 
872
                // wait till next completion, do it then.
873
                // completion irqs can wait up to 1024 msec,
874
                urb->transfer_flags |= EHCI_STATE_UNLINK;
875
                break;
876
        }
877
        spin_unlock_irqrestore (&ehci->lock, flags);
878
        return 0;
879
}
880
 
881
/*-------------------------------------------------------------------------*/
882
 
883
// bulk qh holds the data toggle
884
 
885
static void
886
ehci_endpoint_disable (struct usb_hcd *hcd, struct hcd_dev *dev, int ep)
887
{
888
        struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
889
        int                     epnum;
890
        unsigned long           flags;
891
        struct ehci_qh          *qh;
892
 
893
        /* ASSERT:  any requests/urbs are being unlinked */
894
        /* ASSERT:  nobody can be submitting urbs for this any more */
895
 
896
        epnum = ep & USB_ENDPOINT_NUMBER_MASK;
897
        if (epnum != 0 && (ep & USB_DIR_IN))
898
                epnum |= 0x10;
899
 
900
rescan:
901
        spin_lock_irqsave (&ehci->lock, flags);
902
        qh = (struct ehci_qh *) dev->ep [epnum];
903
        if (!qh)
904
                goto done;
905
 
906
        if (!HCD_IS_RUNNING (ehci->hcd.state))
907
                qh->qh_state = QH_STATE_IDLE;
908
        switch (qh->qh_state) {
909
        case QH_STATE_UNLINK:           /* wait for hw to finish? */
910
                spin_unlock_irqrestore (&ehci->lock, flags);
911
                set_current_state (TASK_UNINTERRUPTIBLE);
912
                schedule_timeout (1);
913
                goto rescan;
914
        case QH_STATE_IDLE:             /* fully unlinked */
915
                if (list_empty (&qh->qtd_list)) {
916
                        qh_put (ehci, qh);
917
                        break;
918
                }
919
                /* else FALL THROUGH */
920
        default:
921
                /* caller was supposed to have unlinked any requests;
922
                 * that's not our job.  just leak this memory.
923
                 */
924
                ehci_err (ehci, "qh %p (#%d) state %d%s\n",
925
                        qh, epnum, qh->qh_state,
926
                        list_empty (&qh->qtd_list) ? "" : "(has tds)");
927
                break;
928
        }
929
        dev->ep [epnum] = 0;
930
done:
931
        spin_unlock_irqrestore (&ehci->lock, flags);
932
        return;
933
}
934
 
935
/*-------------------------------------------------------------------------*/
936
 
937
static const struct hc_driver ehci_driver = {
938
        .description =          hcd_name,
939
 
940
        /*
941
         * generic hardware linkage
942
         */
943
        .irq =                  ehci_irq,
944
        .flags =                HCD_MEMORY | HCD_USB2,
945
 
946
        /*
947
         * basic lifecycle operations
948
         */
949
        .reset =                ehci_hc_reset,
950
        .start =                ehci_start,
951
#ifdef  CONFIG_PM
952
        .suspend =              ehci_suspend,
953
        .resume =               ehci_resume,
954
#endif
955
        .stop =                 ehci_stop,
956
 
957
        /*
958
         * memory lifecycle (except per-request)
959
         */
960
        .hcd_alloc =            ehci_hcd_alloc,
961
        .hcd_free =             ehci_hcd_free,
962
 
963
        /*
964
         * managing i/o requests and associated device resources
965
         */
966
        .urb_enqueue =          ehci_urb_enqueue,
967
        .urb_dequeue =          ehci_urb_dequeue,
968
        .endpoint_disable =     ehci_endpoint_disable,
969
 
970
        /*
971
         * scheduling support
972
         */
973
        .get_frame_number =     ehci_get_frame,
974
 
975
        /*
976
         * root hub support
977
         */
978
        .hub_status_data =      ehci_hub_status_data,
979
        .hub_control =          ehci_hub_control,
980
};
981
 
982
/*-------------------------------------------------------------------------*/
983
 
984
/* EHCI spec says PCI is required. */
985
 
986
/* PCI driver selection metadata; PCI hotplugging uses this */
987
static const struct pci_device_id pci_ids [] = { {
988
        /* handle any USB 2.0 EHCI controller */
989
        PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x20), ~0),
990
        .driver_data =  (unsigned long) &ehci_driver,
991
        },
992
        { /* end: all zeroes */ }
993
};
994
MODULE_DEVICE_TABLE (pci, pci_ids);
995
 
996
/* pci driver glue; this is a "new style" PCI driver module */
997
static struct pci_driver ehci_pci_driver = {
998
        .name =         (char *) hcd_name,
999
        .id_table =     pci_ids,
1000
 
1001
        .probe =        usb_hcd_pci_probe,
1002
        .remove =       usb_hcd_pci_remove,
1003
 
1004
#ifdef  CONFIG_PM
1005
        .suspend =      usb_hcd_pci_suspend,
1006
        .resume =       usb_hcd_pci_resume,
1007
#endif
1008
};
1009
 
1010
#define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
1011
 
1012
MODULE_DESCRIPTION (DRIVER_INFO);
1013
MODULE_AUTHOR (DRIVER_AUTHOR);
1014
MODULE_LICENSE ("GPL");
1015
 
1049 mauro 1016
/*static*/ int __init ehci_hcd_init (void)
846 giacomo 1017
{
1018
        if (usb_disabled())
1019
                return -ENODEV;
1020
 
1021
        pr_debug ("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
1022
                hcd_name,
1023
                sizeof (struct ehci_qh), sizeof (struct ehci_qtd),
1024
                sizeof (struct ehci_itd), sizeof (struct ehci_sitd));
1025
 
1026
        return pci_module_init (&ehci_pci_driver);
1027
}
1049 mauro 1028
//module_init (init);
846 giacomo 1029
 
1049 mauro 1030
/*static*/ void __exit ehci_hcd_cleanup (void)
846 giacomo 1031
{      
1032
        pci_unregister_driver (&ehci_pci_driver);
1033
}
1049 mauro 1034
//module_exit (cleanup);