2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
5 Desc: BeginIO - Start up a timer.device request.
9 #include "timer_intern.h"
10 #include <devices/newstyle.h>
11 #include <exec/errors.h>
12 #include <exec/initializers.h>
13 #include <proto/exec.h>
14 #include <aros/debug.h>
16 /****************************************************************************************/
18 #define NEWSTYLE_DEVICE 1
20 #define ioStd(x) ((struct IOStdReq *)x)
22 /****************************************************************************************/
26 static const UWORD SupportedCommands
[] =
37 /****************************************************************************************/
39 static void addToWaitList(struct TimerBase
*, struct MinList
*, struct timerequest
*);
41 /*****i***********************************************************************
44 #include <devices/timer.h>
45 #include <proto/timer.h>
46 AROS_LH1(void, BeginIO
,
49 AROS_LHA(struct timerequest
*, timereq
, A1
),
52 struct TimerBase
*, TimerBase
, 5, Timer
)
55 BeginIO() will perform a timer.device command. It is normally
56 called from within DoIO() and SendIO().
59 timereq - The request to process.
62 The requested message will be processed.
65 This function is safe to call from interrupts.
72 exec.library/Abort(), exec.library/SendIO(), exec.library/DoIO()
77 23-01-1998 iaint Implemented again.
79 ******************************************************************************/
86 timereq
->tr_node
.io_Message
.mn_Node
.ln_Type
= NT_MESSAGE
;
87 timereq
->tr_node
.io_Error
= 0;
89 unitNum
= (ULONG
)timereq
->tr_node
.io_Unit
;
91 switch(timereq
->tr_node
.io_Command
)
94 case NSCMD_DEVICEQUERY
:
95 #warning In timer.device this is maybe a bit problematic, as the timerequest structure does not have io_Data and io_Length members
97 if (timereq
->tr_node
.io_Message
.mn_Length
< sizeof(struct IOStdReq
))
99 timereq
->tr_node
.io_Error
= IOERR_BADLENGTH
;
101 else if(ioStd(timereq
)->io_Length
< ((LONG
)OFFSET(NSDeviceQueryResult
, SupportedCommands
)) + sizeof(UWORD
*))
103 timereq
->tr_node
.io_Error
= IOERR_BADLENGTH
;
107 struct NSDeviceQueryResult
*d
;
109 d
= (struct NSDeviceQueryResult
*)ioStd(timereq
)->io_Data
;
111 d
->DevQueryFormat
= 0;
112 d
->SizeAvailable
= sizeof(struct NSDeviceQueryResult
);
113 d
->DeviceType
= NSDEVTYPE_TIMER
;
114 d
->DeviceSubType
= 0;
115 d
->SupportedCommands
= (UWORD
*)SupportedCommands
;
117 ioStd(timereq
)->io_Actual
= sizeof(struct NSDeviceQueryResult
);
123 GetSysTime(&timereq
->tr_time
);
125 if(!(timereq
->tr_node
.io_Flags
& IOF_QUICK
))
127 ReplyMsg((struct Message
*)timereq
);
129 replyit
= FALSE
; /* Because replyit will clear the timeval */
134 TimerBase
->tb_CurrentTime
.tv_secs
= timereq
->tr_time
.tv_secs
;
135 TimerBase
->tb_CurrentTime
.tv_micro
= timereq
->tr_time
.tv_micro
;
144 /* Firstly, check to see if request is for past */
146 if(CmpTime(&TimerBase
->tb_CurrentTime
, &timereq
->tr_time
) <= 0)
149 timereq
->tr_time
.tv_secs
= timereq
->tr_time
.tv_micro
= 0;
150 timereq
->tr_node
.io_Error
= 0;
155 /* Ok, we add this to the list */
156 addToWaitList(TimerBase
, &TimerBase
->tb_Lists
[TL_WAITVBL
], timereq
);
159 timereq
->tr_node
.io_Flags
&= ~IOF_QUICK
;
166 Adjust the time request to be relative to the
167 the elapsed time counter that we keep.
170 AddTime(&timereq
->tr_time
, &TimerBase
->tb_Elapsed
);
172 /* Slot it into the list */
173 addToWaitList(TimerBase
, &TimerBase
->tb_Lists
[TL_VBLANK
], timereq
);
175 timereq
->tr_node
.io_Flags
&= ~IOF_QUICK
;
180 case UNIT_WAITECLOCK
:
183 timereq
->tr_node
.io_Error
= IOERR_NOCMD
;
185 } /* switch(unitNum) */
199 timereq
->tr_node
.io_Error
= IOERR_NOCMD
;
202 } /* switch(command) */
206 timereq
->tr_time
.tv_secs
= 0;
207 timereq
->tr_time
.tv_micro
= 0;
208 if(!(timereq
->tr_node
.io_Flags
& IOF_QUICK
))
210 ReplyMsg((struct Message
*)timereq
);
218 addToWaitList( struct TimerBase
*TimerBase
,
219 struct MinList
*list
,
220 struct timerequest
*iotr
)
222 /* We are disabled, so we should take as little time as possible. */
223 struct timerequest
*tr
;
226 tr
= (struct timerequest
*)list
->mlh_Head
;
228 while(tr
->tr_node
.io_Message
.mn_Node
.ln_Succ
!= NULL
)
230 /* If the time in the new request is less than the next request */
231 if(CmpTime(&tr
->tr_time
, &iotr
->tr_time
) < 0)
233 /* Add the node before the next request */
237 tr
->tr_node
.io_Message
.mn_Node
.ln_Pred
242 tr
= (struct timerequest
*)tr
->tr_node
.io_Message
.mn_Node
.ln_Succ
;
246 This will catch the case of either an empty list, or request is
247 for after all other requests
251 AddTail((struct List
*)list
, (struct Node
*)iotr
);
256 bug("Current list contents:\n");
257 ForeachNode(list
, tr
)
259 bug("%ld: %ld.%ld\n", i
,
260 tr
->tr_time
.tv_secs
, tr
->tr_time
.tv_micro
);