Annotation of qemu/roms/SLOF/slof/fs/usb/usb-hub.fs, revision 1.1.1.1

1.1       root        1: \ *****************************************************************************
                      2: \ * Copyright (c) 2004, 2008 IBM Corporation
                      3: \ * All rights reserved.
                      4: \ * This program and the accompanying materials
                      5: \ * are made available under the terms of the BSD License
                      6: \ * which accompanies this distribution, and is available at
                      7: \ * http://www.opensource.org/licenses/bsd-license.php
                      8: \ *
                      9: \ * Contributors:
                     10: \ *     IBM Corporation - initial implementation
                     11: \ ****************************************************************************/
                     12: 
                     13: 
                     14: \ ----------------------------------------------------------------------------
                     15: \ On detection of a hub after reading the device descriptor this package has to
                     16: \ be called so that the hub enumeration is done to idenitify the down stream 
                     17: \ device  
                     18: \ --------------------------------------------------------------------------
                     19: \ OF properties
                     20: \ --------------------------------------------------------------------------
                     21: 
                     22: 
                     23: s" hub" device-name
                     24: s" usb" device-type
                     25: 1 encode-int s" #address-cells" property
                     26: 0 encode-int s" #size-cells" property
                     27: 
                     28: \ converts physical address to text unit string 
                     29: 
                     30: 
                     31: : encode-unit ( port-addr -- unit-str unit-len )  1 hex-encode-unit ;
                     32: 
                     33: 
                     34: \ Converts text unit string to phyical address 
                     35: 
                     36: 
                     37: : decode-unit ( addr len -- port-addr ) 1 hex-decode-unit ;
                     38: 
                     39: 0 VALUE new-device-address
                     40: 0 VALUE port-number
                     41: 0 VALUE MPS-DCP
                     42: 0 VALUE mps
                     43: 0 VALUE my-usb-address
                     44: 
                     45: 00 value device-speed
                     46: 
                     47: 
                     48: \ Get parameters passed from the parent.
                     49: 
                     50: : mps-property-set ( -- )
                     51:    s"  HUB Compiling mps-property-set " usb-debug-print
                     52:    s" USB-ADDRESS" get-my-property ( TRUE | prop-addr prop-len FALSE )
                     53:    IF
                     54:       s" notpossible" usb-debug-print
                     55:    ELSE
                     56:       decode-int nip nip to my-usb-address
                     57:    THEN  
                     58:    s" MPS-DCP" get-my-property ( TRUE | prop-addr prop-len FALSE )
                     59:    IF 
                     60:       s" MPS-DCP property not found Assuming 8 as MAX PACKET SIZE" ( str len )  
                     61:       usb-debug-print
                     62:       s" for the default control pipe"  usb-debug-print
                     63:       8 to MPS-DCP
                     64:    ELSE
                     65:       s" MPS-DCP property found!!" usb-debug-print ( prop-addr prop-len FALSE )
                     66:       decode-int nip nip to MPS-DCP
                     67:    THEN
                     68: ;
                     69: 
                     70: 
                     71: \ --------------------------------------------------------------------------
                     72: \ Constant declarations
                     73: \ --------------------------------------------------------------------------
                     74: 
                     75: 
                     76: 2303080000000000 CONSTANT hppwr-set
                     77: 2301080000000000 CONSTANT hppwr-clear
                     78: 2303040000000000 CONSTANT hprst-set
                     79: A300000000000400 CONSTANT hpsta-get
                     80: 2303010000000000 CONSTANT hpena-set
                     81: A006002900000000 CONSTANT hubds-get
                     82: 8  CONSTANT DEFAULT-CONTROL-MPS
                     83: 12 CONSTANT DEVICE-DESCRIPTOR-LEN
                     84: 9  CONSTANT CONFIG-DESCRIPTOR-LEN
                     85: 20 CONSTANT BULK-CONFIG-DESCRIPTOR-LEN
                     86: 
                     87: 
                     88: \ TODO:
                     89: \ CONFIG-DESCRIPTOR-LEN should be only 9. The interface
                     90: \ and endpoint descriptors returned along with config
                     91: \ descriptor are variable and 0x19 is a very wrong VALUE
                     92: \ to specify for this #define.
                     93: 
                     94: 
                     95: 1 CONSTANT DEVICE-DESCRIPTOR-TYPE
                     96: 1 CONSTANT DEVICE-DESCRIPTOR-TYPE-OFFSET
                     97: 4 CONSTANT DEVICE-DESCRIPTOR-DEVCLASS-OFFSET
                     98: 7 CONSTANT DEVICE-DESCRIPTOR-MPS-OFFSET
                     99: 9 CONSTANT HUB-DEVICE-CLASS
                    100: 0 CONSTANT NO-CLASS
                    101: 
                    102: 
                    103: \ --------------------------------------------------------------------------
                    104: \ Temporary Variable declarations
                    105: \ --------------------------------------------------------------------------
                    106: 
                    107: 00 VALUE temp1
                    108: 00 VALUE temp2
                    109: 00 VALUE temp3
                    110: 00 VALUE po2pg            \ Power On to Power Good
                    111: 
                    112: 
                    113: \ --------------------------------------------------------------------------
                    114: \ Buffer allocations
                    115: \ --------------------------------------------------------------------------
                    116: 
                    117: 
                    118: VARIABLE setup-packet     \ 8 bytes for setup packet
                    119: VARIABLE ch-buffer        \ 1 byte character buffer
                    120: 
                    121: INSTANCE VARIABLE dd-buffer
                    122: INSTANCE VARIABLE cd-buffer
                    123: 
                    124: \ TODO:
                    125: \ Should arrive a proper value for the size of the "cd-buffer"
                    126: 
                    127: 8 chars alloc-mem VALUE status-buffer
                    128: 9 chars alloc-mem VALUE hd-buffer
                    129: 
                    130: 
                    131: : (allocate-mem)  ( -- )
                    132:    DEVICE-DESCRIPTOR-LEN chars alloc-mem dd-buffer !
                    133:    BULK-CONFIG-DESCRIPTOR-LEN chars alloc-mem cd-buffer !
                    134: ;
                    135: 
                    136: 
                    137: : (de-allocate-mem)  ( -- )
                    138:    dd-buffer @ ?dup IF
                    139:       DEVICE-DESCRIPTOR-LEN free-mem
                    140:       0 dd-buffer !
                    141:    THEN
                    142:    cd-buffer @ ?dup IF
                    143:       BULK-CONFIG-DESCRIPTOR-LEN free-mem
                    144:       0 cd-buffer !
                    145:    THEN
                    146: ;
                    147: 
                    148: 
                    149: \ standard open firmware methods 
                    150: 
                    151: : open ( -- TRUE )
                    152:    (allocate-mem)
                    153:    TRUE
                    154: ;
                    155: 
                    156: : close ( -- )
                    157:    (de-allocate-mem)
                    158: ;
                    159: 
                    160: 
                    161: \ --------------------------------------------------------------------------
                    162: \ Parent's method
                    163: \ --------------------------------------------------------------------------
                    164: 
                    165: 
                    166: : controlxfer ( dir addr dlen setup-packet MPS ep-fun -- TRUE|FALSE )
                    167:    s" controlxfer" $call-parent 
                    168: ;
                    169: 
                    170: : control-std-set-address ( speedbit -- usb-address TRUE|FALSE )
                    171:    s" control-std-set-address" $call-parent 
                    172: ; 
                    173: 
                    174: : control-std-get-device-descriptor 
                    175:    ( data-buffer data-len MPS funcAddr -- TRUE|FALSE )
                    176:    s" control-std-get-device-descriptor" $call-parent 
                    177: ;
                    178: 
                    179: : control-std-get-configuration-descriptor 
                    180:    ( data-buffer data-len MPS funcAddr -- TRUE|FALSE )
                    181:    s" control-std-get-configuration-descriptor" $call-parent 
                    182: ;
                    183: 
                    184: : control-std-get-maxlun
                    185:    ( MPS fun-addr dir data-buff data-len -- TRUE|FALSE )
                    186:    s" control-std-get-maxlun" $call-parent 
                    187: ;
                    188: 
                    189: : control-std-set-configuration 
                    190:    ( configvalue FuncAddr -- TRUE|FALSE )
                    191:    s" control-std-set-configuration" $call-parent 
                    192: ;
                    193: 
                    194: : control-std-get-string-descriptor
                    195:    ( StringIndex data-buffer data-len MPS FuncAddr -- TRUE|FALSE )
                    196:    s" control-std-get-string-descriptor" $call-parent 
                    197: ;
                    198: 
                    199: : rw-endpoint 
                    200:    ( pt ed-type toggle buffer length mps address -- toggle TRUE|toggle FALSE )
                    201:    s" rw-endpoint" $call-parent 
                    202: ;
                    203: 
                    204: : debug-td ( -- )
                    205:    s" debug-td" $call-parent
                    206: ;
                    207: 
                    208: \ *** NEW ****
                    209: : control-bulk-reset ( MPS fun-addr dir data-buff data-len -- TRUE | FALSE )
                    210:    s" control-bulk-reset" $call-parent
                    211: ;
                    212: 
                    213: 
                    214: \ --------------------------------------------------------------------------
                    215: \ HUB specific methods
                    216: \ --------------------------------------------------------------------------
                    217: \ To bring on the power on a valid port of a hub with a valid USB address
                    218: \ --------------------------------------------------------------------------
                    219: 
                    220: 
                    221: : control-hub-port-power-set  ( port# -- TRUE|FALSE )
                    222:    hppwr-set setup-packet !    ( port#)
                    223:    setup-packet 4 + c!
                    224:    0 0 0 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE | FALSE )
                    225: ;
                    226: 
                    227: 
                    228: \ --------------------------------------------------------------------------
                    229: \ To put power off on ports where device detection or enumeration has failed
                    230: \ --------------------------------------------------------------------------
                    231: 
                    232: 
                    233: : control-hub-port-power-clear ( port#-- TRUE|FALSE )
                    234:    hppwr-clear setup-packet !  ( port#)
                    235:    setup-packet 4 + c!
                    236:    0 0 0 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
                    237: ;
                    238: 
                    239: 
                    240: \ -------------------------------------------------------------------------
                    241: \ To reset a valid port of a hub with a valid USB 
                    242: \ address
                    243: \ --------------------------------------------------------------------------
                    244: 
                    245: 
                    246: : control-hub-port-reset-set ( port# -- TRUE|FALSE )
                    247:    hprst-set setup-packet !    ( port# )
                    248:    setup-packet 4 + c!
                    249:    0 0 0 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
                    250: ;
                    251: 
                    252: 
                    253: \ -------------------------------------------------------------------------
                    254: \ To enable a particular valid port of a hub with a valid USB address
                    255: \ -------------------------------------------------------------------------
                    256: 
                    257: 
                    258: : control-hub-port-enable ( port# -- TRUE|FALSE )
                    259:    hpena-set setup-packet !    ( port# )
                    260:    setup-packet 4 +  c!
                    261:    0 0 0 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
                    262: ;
                    263: 
                    264: 
                    265: \ -------------------------------------------------------------------------
                    266: \ To get the status of a valid port of a hub with 
                    267: \ a valid USB address
                    268: \ -------------------------------------------------------------------------
                    269: 
                    270: 
                    271: : control-hub-port-status-get ( buffer port# -- TRUE|FALSE )
                    272:    hpsta-get setup-packet !    ( buffer port# )
                    273:    setup-packet 4 + c!         ( buffer )
                    274:    0 swap 4 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
                    275: ;
                    276: 
                    277: 
                    278: \ --------------------------------------------------------------------------
                    279: \ To get the hub descriptor to understand how many ports are vailable and the 
                    280: \ specs of those ports
                    281: \ ---------------------------------------------------------------------------
                    282: 
                    283: 
                    284: : control-get-hub-descriptor ( buffer buffer-length -- TRUE|FALSE )
                    285:    hubds-get setup-packet ! 
                    286:    dup setup-packet 6 + w!-le ( buffer buffer-length )
                    287:    0 -rot setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
                    288: ;
                    289: 
                    290: 
                    291: s" usb-enumerate.fs" INCLUDED
                    292: 
                    293: 
                    294: : hub-configure-port ( port# -- )
                    295: 
                    296: \ this port has been powered on
                    297: \ send reset to enable port and
                    298: \ start device detection by hub
                    299: \ some devices require a long timeout here (10s)
                    300: 
                    301:    \ Step 1: check if reset state ended
                    302: 
                    303:    BEGIN                               ( port# )
                    304:       status-buffer 4 erase             ( port# )
                    305:       status-buffer over control-hub-port-status-get drop ( port# ) 
                    306:       status-buffer w@-le 102 and 0=   ( port# TRUE|FALSE )
                    307:    WHILE                               ( port# )
                    308:    REPEAT                      ( port# )
                    309:    po2pg 3 * ms    \ wait for bPwrOn2PwrGood*3 ms
                    310:    
                    311:    \ STEP 2: Reset the port.
                    312:    \         (this also enables the port)
                    313:    dup control-hub-port-reset-set drop ( port# )
                    314:    BEGIN                               ( port# )
                    315:       status-buffer 4 erase             ( port# )
                    316:       status-buffer over control-hub-port-status-get drop ( port# ) 
                    317:       status-buffer w@-le 10 and       ( port# TRUE|FALSE )
                    318:    WHILE                               ( port# )
                    319:    REPEAT                              ( port# )
                    320: 
                    321:    \ STEP 3: Check if a device is connected to the port.
                    322: 
                    323:    status-buffer 4 erase                ( port# )
                    324:    status-buffer over control-hub-port-status-get drop ( port# ) 
                    325:    status-buffer w@-le    103 and    103 <>           ( port# TRUE|FALSE )
                    326:    s" Port status bits: " status-buffer w@-le usb-debug-print-val
                    327:    IF                                  ( port# ) 
                    328:       drop                     
                    329:       s" Connect status: No device connected "  usb-debug-print
                    330:       EXIT 
                    331:    THEN 
                    332: 
                    333: 
                    334:    \ STEP 4: Assign an address to this device.
                    335: 
                    336:    status-buffer w@-le 200 and 4 lshift \ get speed bit
                    337:    dup to device-speed                  \ store speed bit
                    338:                                 ( port# speedbit )
                    339:    control-std-set-address     ( port# usb-addr TRUE|FALSE )
                    340:    50 ms                       ( port# usb-addr TRUE|FALSE )
                    341:    debug-td                    ( port# usb-addr TRUE|FALSE )
                    342:    IF                          ( port# usb-addr )
                    343:       device-speed or           ( port# usb-addr+speedbit )
                    344:       to new-device-address     ( port# )
                    345:       to port-number
                    346:       dd-buffer @ DEVICE-DESCRIPTOR-LEN erase
                    347:       dd-buffer @ DEFAULT-CONTROL-MPS DEFAULT-CONTROL-MPS new-device-address
                    348:       ( buffer mps mps usb-addr ) 
                    349:       control-std-get-device-descriptor     ( TRUE|FALSE )
                    350:       IF
                    351:          dd-buffer @ DEVICE-DESCRIPTOR-TYPE-OFFSET + c@ ( descriptor-type )
                    352:          DEVICE-DESCRIPTOR-TYPE <>          ( TRUE|FALSE )
                    353:          IF 
                    354:             s" HUB: ERROR!! Invalid Device Descriptor for the new device"
                    355:             usb-debug-print
                    356:          ELSE
                    357:             dd-buffer @ DEVICE-DESCRIPTOR-MPS-OFFSET + c@ to mps
                    358: 
                    359:             \ Re-read the device descriptor again with the known MPS.
                    360: 
                    361:             dd-buffer @ DEVICE-DESCRIPTOR-LEN erase
                    362:             dd-buffer @ DEVICE-DESCRIPTOR-LEN mps new-device-address
                    363:             ( buffer descp-len mps usb-addr )
                    364:             \ s" DEVICE DESCRIPTOR: " usb-debug-print
                    365:             control-std-get-device-descriptor invert
                    366:             IF
                    367:                s" ** reading dev-descriptor failed ** " usb-debug-print
                    368:             THEN
                    369:             create-usb-device-tree
                    370:          THEN
                    371:       ELSE
                    372:          s" ERROR!! Failed to get device descriptor" usb-debug-print 
                    373:       THEN
                    374:    ELSE                                                    ( port# )
                    375:       s" USB Set Adddress failed!!" usb-debug-print ( port# )
                    376:       s" Clearing Port Power..."  usb-debug-print   ( port# )
                    377:       control-hub-port-power-clear                 ( TRUE|FALSE )
                    378:       IF 
                    379:          s" Port power down " usb-debug-print
                    380:       ELSE
                    381:          s" Unable to clear port power!!!" usb-debug-print
                    382:       THEN
                    383:    THEN
                    384: ;
                    385: 
                    386: 
                    387: \ ---------------------------------------------------------------------------
                    388: \ To enumerate all the valid ports of hub
                    389: \ TODO:
                    390: \ 1. Remove hardcoded constants.
                    391: \ 2. Remove Endian Dependencies.
                    392: \ 3. Return values of controlxfer should be checked. 
                    393: \ ---------------------------------------------------------------------------
                    394: 
                    395: : hub-enumerate ( -- )
                    396:    cd-buffer @ CONFIG-DESCRIPTOR-LEN erase
                    397: 
                    398:    \ Get HUB configuration and SET the configuration
                    399:    \ note: remove hard-coded constants.
                    400: 
                    401:    cd-buffer @ CONFIG-DESCRIPTOR-LEN MPS-DCP my-usb-address 
                    402:    ( buffer descp-len mps usb-address )
                    403:    control-std-get-configuration-descriptor drop 
                    404:    cd-buffer @ 1+ c@ 2 <>  IF
                    405:       s" Unable to read configuration descriptor" usb-debug-print
                    406:       EXIT 
                    407:    THEN 
                    408:    cd-buffer @ 4 + c@ 1 <> IF
                    409:       s" Not a valid HUB config descriptor" usb-debug-print 
                    410:       EXIT 
                    411:    THEN 
                    412: 
                    413:    \ TODO: Do further checkings on the returned Configuration descriptor
                    414:    \ before proceeding to accept it.
                    415: 
                    416:    cd-buffer @ 5 + c@ to temp1 \ Store the configuration in temp1
                    417:    temp1 my-usb-address control-std-set-configuration drop
                    418:    my-usb-address to temp1
                    419:    hd-buffer 9 erase
                    420:    hd-buffer 9 control-get-hub-descriptor drop
                    421: 
                    422:    \ PENDING: 1. Check Return value.
                    423:    \          2. HUB descriptor size is variable. Currently we r hardcoding
                    424:    \             a value of 9.
                    425: 
                    426:    hd-buffer 2 + c@ to temp2     \ number of downstream ports
                    427: 
                    428:    s" HUB: Found " usb-debug-print
                    429:    s" number of downstream hub ports! : " temp2 usb-debug-print-val
                    430:    hd-buffer 5 + c@ to po2pg     \ get bPwrOn2PwrGood
                    431: 
                    432:    \ power on all present hub ports
                    433:    \ to allow slow devices to set up
                    434: 
                    435:    temp2 1+ 1 DO
                    436:       i control-hub-port-power-set drop
                    437:       d# 20 ms
                    438:    LOOP
                    439: 
                    440:    d# 200 ms      \ some devices need a long time (10s)
                    441: 
                    442:    \ now start detection and configuration for these ports
                    443:    
                    444:    temp2 1+ 1 DO
                    445:        s" hub-configure-port: " i usb-debug-print-val
                    446:        i hub-configure-port
                    447:    LOOP
                    448: ; 
                    449: 
                    450: 
                    451: \ --------------------------------------------------------------------------  
                    452: \ To initialize hub
                    453: \ --------------------------------------------------------------------------
                    454: 
                    455: (allocate-mem)
                    456: mps-property-set
                    457: hub-enumerate
                    458: (de-allocate-mem)
                    459: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.