aboutsummaryrefslogtreecommitdiff
path: root/slof/fs/usb/usb-hub.fs
blob: 106b680e14925d8146e48faf83c42aa99b17a9c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
\ *****************************************************************************
\ * Copyright (c) 2004, 2008 IBM Corporation
\ * All rights reserved.
\ * This program and the accompanying materials
\ * are made available under the terms of the BSD License
\ * which accompanies this distribution, and is available at
\ * http://www.opensource.org/licenses/bsd-license.php
\ *
\ * Contributors:
\ *     IBM Corporation - initial implementation
\ ****************************************************************************/


\ ----------------------------------------------------------------------------
\ On detection of a hub after reading the device descriptor this package has to
\ be called so that the hub enumeration is done to idenitify the down stream 
\ device  
\ --------------------------------------------------------------------------
\ OF properties
\ --------------------------------------------------------------------------


s" hub" device-name
s" usb" device-type
1 encode-int s" #address-cells" property
0 encode-int s" #size-cells" property

\ converts physical address to text unit string 


: encode-unit ( port-addr -- unit-str unit-len )  1 hex-encode-unit ;


\ Converts text unit string to phyical address 


: decode-unit ( addr len -- port-addr ) 1 hex-decode-unit ;

0 VALUE new-device-address
0 VALUE port-number
0 VALUE MPS-DCP
0 VALUE mps
0 VALUE my-usb-address

00 value device-speed


\ Get parameters passed from the parent.

: mps-property-set ( -- )
   s"  HUB Compiling mps-property-set " usb-debug-print
   s" USB-ADDRESS" get-my-property ( TRUE | prop-addr prop-len FALSE )
   IF
      s" notpossible" usb-debug-print
   ELSE
      decode-int nip nip to my-usb-address
   THEN  
   s" MPS-DCP" get-my-property ( TRUE | prop-addr prop-len FALSE )
   IF 
      s" MPS-DCP property not found Assuming 8 as MAX PACKET SIZE" ( str len )  
      usb-debug-print
      s" for the default control pipe"  usb-debug-print
      8 to MPS-DCP
   ELSE
      s" MPS-DCP property found!!" usb-debug-print ( prop-addr prop-len FALSE )
      decode-int nip nip to MPS-DCP
   THEN
;


\ --------------------------------------------------------------------------
\ Constant declarations
\ --------------------------------------------------------------------------


2303080000000000 CONSTANT hppwr-set
2301080000000000 CONSTANT hppwr-clear
2303040000000000 CONSTANT hprst-set
A300000000000400 CONSTANT hpsta-get
2303010000000000 CONSTANT hpena-set
A006002900000000 CONSTANT hubds-get
8  CONSTANT DEFAULT-CONTROL-MPS
12 CONSTANT DEVICE-DESCRIPTOR-LEN
9  CONSTANT CONFIG-DESCRIPTOR-LEN
20 CONSTANT BULK-CONFIG-DESCRIPTOR-LEN


\ TODO:
\ CONFIG-DESCRIPTOR-LEN should be only 9. The interface
\ and endpoint descriptors returned along with config
\ descriptor are variable and 0x19 is a very wrong VALUE
\ to specify for this #define.


1 CONSTANT DEVICE-DESCRIPTOR-TYPE
1 CONSTANT DEVICE-DESCRIPTOR-TYPE-OFFSET
4 CONSTANT DEVICE-DESCRIPTOR-DEVCLASS-OFFSET
7 CONSTANT DEVICE-DESCRIPTOR-MPS-OFFSET
9 CONSTANT HUB-DEVICE-CLASS
0 CONSTANT NO-CLASS


\ --------------------------------------------------------------------------
\ Temporary Variable declarations
\ --------------------------------------------------------------------------

00 VALUE temp1
00 VALUE temp2
00 VALUE temp3
00 VALUE po2pg            \ Power On to Power Good


\ --------------------------------------------------------------------------
\ Buffer allocations
\ --------------------------------------------------------------------------


VARIABLE setup-packet     \ 8 bytes for setup packet
VARIABLE ch-buffer        \ 1 byte character buffer

INSTANCE VARIABLE dd-buffer
INSTANCE VARIABLE cd-buffer

\ TODO:
\ Should arrive a proper value for the size of the "cd-buffer"

8 chars alloc-mem VALUE status-buffer
9 chars alloc-mem VALUE hd-buffer


: (allocate-mem)  ( -- )
   DEVICE-DESCRIPTOR-LEN chars alloc-mem dd-buffer !
   BULK-CONFIG-DESCRIPTOR-LEN chars alloc-mem cd-buffer !
;


: (de-allocate-mem)  ( -- )
   dd-buffer @ ?dup IF
      DEVICE-DESCRIPTOR-LEN free-mem
      0 dd-buffer !
   THEN
   cd-buffer @ ?dup IF
      BULK-CONFIG-DESCRIPTOR-LEN free-mem
      0 cd-buffer !
   THEN
;


\ standard open firmware methods 

: open ( -- TRUE )
   (allocate-mem)
   TRUE
;

: close ( -- )
   (de-allocate-mem)
;


\ --------------------------------------------------------------------------
\ Parent's method
\ --------------------------------------------------------------------------


: controlxfer ( dir addr dlen setup-packet MPS ep-fun -- TRUE|FALSE )
   s" controlxfer" $call-parent 
;

: control-std-set-address ( speedbit -- usb-address TRUE|FALSE )
   s" control-std-set-address" $call-parent 
; 

: control-std-get-device-descriptor 
   ( data-buffer data-len MPS funcAddr -- TRUE|FALSE )
   s" control-std-get-device-descriptor" $call-parent 
;

: control-std-get-configuration-descriptor 
   ( data-buffer data-len MPS funcAddr -- TRUE|FALSE )
   s" control-std-get-configuration-descriptor" $call-parent 
;

: control-std-get-maxlun
   ( MPS fun-addr dir data-buff data-len -- TRUE|FALSE )
   s" control-std-get-maxlun" $call-parent 
;

: control-std-set-configuration 
   ( configvalue FuncAddr -- TRUE|FALSE )
   s" control-std-set-configuration" $call-parent 
;

: control-std-get-string-descriptor
   ( StringIndex data-buffer data-len MPS FuncAddr -- TRUE|FALSE )
   s" control-std-get-string-descriptor" $call-parent 
;

: rw-endpoint 
   ( pt ed-type toggle buffer length mps address -- toggle TRUE|toggle FALSE )
   s" rw-endpoint" $call-parent 
;

: debug-td ( -- )
   s" debug-td" $call-parent
;

\ *** NEW ****
: control-bulk-reset ( MPS fun-addr dir data-buff data-len -- TRUE | FALSE )
   s" control-bulk-reset" $call-parent
;


\ --------------------------------------------------------------------------
\ HUB specific methods
\ --------------------------------------------------------------------------
\ To bring on the power on a valid port of a hub with a valid USB address
\ --------------------------------------------------------------------------


: control-hub-port-power-set  ( port# -- TRUE|FALSE )
   hppwr-set setup-packet !	( port#)
   setup-packet 4 + c!
   0 0 0 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE | FALSE )
;


\ --------------------------------------------------------------------------
\ To put power off on ports where device detection or enumeration has failed
\ --------------------------------------------------------------------------


: control-hub-port-power-clear ( port#-- TRUE|FALSE )
   hppwr-clear setup-packet !	( port#)
   setup-packet 4 + c!
   0 0 0 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
;


\ -------------------------------------------------------------------------
\ To reset a valid port of a hub with a valid USB 
\ address
\ --------------------------------------------------------------------------


: control-hub-port-reset-set ( port# -- TRUE|FALSE )
   hprst-set setup-packet !	( port# )
   setup-packet 4 + c!
   0 0 0 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
;


\ -------------------------------------------------------------------------
\ To enable a particular valid port of a hub with a valid USB address
\ -------------------------------------------------------------------------


: control-hub-port-enable ( port# -- TRUE|FALSE )
   hpena-set setup-packet !	( port# )
   setup-packet 4 +  c!
   0 0 0 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
;


\ -------------------------------------------------------------------------
\ To get the status of a valid port of a hub with 
\ a valid USB address
\ -------------------------------------------------------------------------


: control-hub-port-status-get ( buffer port# -- TRUE|FALSE )
   hpsta-get setup-packet !	( buffer port# )
   setup-packet 4 + c!		( buffer )
   0 swap 4 setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
;


\ --------------------------------------------------------------------------
\ To get the hub descriptor to understand how many ports are vailable and the 
\ specs of those ports
\ ---------------------------------------------------------------------------


: control-get-hub-descriptor ( buffer buffer-length -- TRUE|FALSE )
   hubds-get setup-packet ! 
   dup setup-packet 6 + w!-le ( buffer buffer-length )
   0 -rot setup-packet MPS-DCP my-usb-address controlxfer ( TRUE|FALSE )
;


s" usb-enumerate.fs" INCLUDED


: hub-configure-port ( port# -- )

\ this port has been powered on
\ send reset to enable port and
\ start device detection by hub
\ some devices require a long timeout here (10s)

   \ Step 1: check if reset state ended

   BEGIN				( port# )
      status-buffer 4 erase             ( port# )
      status-buffer over control-hub-port-status-get drop ( port# ) 
      status-buffer w@-le 102 and 0= 	( port# TRUE|FALSE )
   WHILE				( port# )
   REPEAT			( port# )
   po2pg 3 * ms    \ wait for bPwrOn2PwrGood*3 ms
   
   \ STEP 2: Reset the port.
   \         (this also enables the port)
   dup control-hub-port-reset-set drop	( port# )
   BEGIN				( port# )
      status-buffer 4 erase             ( port# )
      status-buffer over control-hub-port-status-get drop ( port# ) 
      status-buffer w@-le 10 and 	( port# TRUE|FALSE )
   WHILE				( port# )
   REPEAT				( port# )

   \ STEP 3: Check if a device is connected to the port.

   status-buffer 4 erase                ( port# )
   status-buffer over control-hub-port-status-get drop ( port# ) 
   status-buffer w@-le    103 and    103 <> 	       ( port# TRUE|FALSE )
   s" Port status bits: " status-buffer w@-le usb-debug-print-val
   IF					( port# ) 
      drop			
      s" Connect status: No device connected "  usb-debug-print
      EXIT 
   THEN 


   \ STEP 4: Assign an address to this device.

   status-buffer w@-le 200 and 4 lshift \ get speed bit
   dup to device-speed                  \ store speed bit
                                ( port# speedbit )
   control-std-set-address	( port# usb-addr TRUE|FALSE )
   50 ms			( port# usb-addr TRUE|FALSE )
   debug-td			( port# usb-addr TRUE|FALSE )
   IF 				( port# usb-addr )
      device-speed or           ( port# usb-addr+speedbit )
      to new-device-address     ( port# )
      to port-number
      dd-buffer @ DEVICE-DESCRIPTOR-LEN erase
      dd-buffer @ DEFAULT-CONTROL-MPS DEFAULT-CONTROL-MPS new-device-address
      ( buffer mps mps usb-addr ) 
      control-std-get-device-descriptor     ( TRUE|FALSE )
      IF
         dd-buffer @ DEVICE-DESCRIPTOR-TYPE-OFFSET + c@ ( descriptor-type )
         DEVICE-DESCRIPTOR-TYPE <>          ( TRUE|FALSE )
         IF 
            s" HUB: ERROR!! Invalid Device Descriptor for the new device"
            usb-debug-print
         ELSE
            dd-buffer @ DEVICE-DESCRIPTOR-MPS-OFFSET + c@ to mps

            \ Re-read the device descriptor again with the known MPS.

            dd-buffer @ DEVICE-DESCRIPTOR-LEN erase
            dd-buffer @ DEVICE-DESCRIPTOR-LEN mps new-device-address
            ( buffer descp-len mps usb-addr )
            \ s" DEVICE DESCRIPTOR: " usb-debug-print
            control-std-get-device-descriptor invert
            IF
               s" ** reading dev-descriptor failed ** " usb-debug-print
            THEN
            create-usb-device-tree
         THEN
      ELSE
         s" ERROR!! Failed to get device descriptor" usb-debug-print 
      THEN
   ELSE						    ( port# )
      s" USB Set Adddress failed!!" usb-debug-print ( port# )
      s" Clearing Port Power..."  usb-debug-print   ( port# )
      control-hub-port-power-clear		    ( TRUE|FALSE )
      IF 
         s" Port power down " usb-debug-print
      ELSE
         s" Unable to clear port power!!!" usb-debug-print
      THEN
   THEN
;


\ ---------------------------------------------------------------------------
\ To enumerate all the valid ports of hub
\ TODO:
\ 1. Remove hardcoded constants.
\ 2. Remove Endian Dependencies.
\ 3. Return values of controlxfer should be checked. 
\ ---------------------------------------------------------------------------

: hub-enumerate ( -- )
   cd-buffer @ CONFIG-DESCRIPTOR-LEN erase

   \ Get HUB configuration and SET the configuration
   \ note: remove hard-coded constants.

   cd-buffer @ CONFIG-DESCRIPTOR-LEN MPS-DCP my-usb-address 
   ( buffer descp-len mps usb-address )
   control-std-get-configuration-descriptor drop 
   cd-buffer @ 1+ c@ 2 <>  IF
      s" Unable to read configuration descriptor" usb-debug-print
      EXIT 
   THEN 
   cd-buffer @ 4 + c@ 1 <> IF
      s" Not a valid HUB config descriptor" usb-debug-print 
      EXIT 
   THEN 

   \ TODO: Do further checkings on the returned Configuration descriptor
   \ before proceeding to accept it.

   cd-buffer @ 5 + c@ to temp1 \ Store the configuration in temp1
   temp1 my-usb-address control-std-set-configuration drop
   my-usb-address to temp1
   hd-buffer 9 erase
   hd-buffer 9 control-get-hub-descriptor drop

   \ PENDING: 1. Check Return value.
   \          2. HUB descriptor size is variable. Currently we r hardcoding
   \             a value of 9.

   hd-buffer 2 + c@ to temp2     \ number of downstream ports

   s" HUB: Found " usb-debug-print
   s" number of downstream hub ports! : " temp2 usb-debug-print-val
   hd-buffer 5 + c@ to po2pg     \ get bPwrOn2PwrGood

   \ power on all present hub ports
   \ to allow slow devices to set up

   temp2 1+ 1 DO
      i control-hub-port-power-set drop
      d# 20 ms
   LOOP

   d# 200 ms      \ some devices need a long time (10s)

   \ now start detection and configuration for these ports
   
   temp2 1+ 1 DO
       s" hub-configure-port: " i usb-debug-print-val
       i hub-configure-port
   LOOP
; 


\ --------------------------------------------------------------------------  
\ To initialize hub
\ --------------------------------------------------------------------------

(allocate-mem)
mps-property-set
hub-enumerate
(de-allocate-mem)