1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
30 /* wrap-safe macros for tick comparison */
31 #define TIME_AFTER(a,b) ((long)(b) - (long)(a) < 0)
32 #define TIME_BEFORE(a,b) TIME_AFTER(b,a)
34 #define HZ 100 /* number of ticks per second */
36 #define MAX_NUM_TICK_TASKS 8
38 #define MAX_NUM_QUEUES 32
39 #define QUEUE_LENGTH 16 /* MUST be a power of 2 */
40 #define QUEUE_LENGTH_MASK (QUEUE_LENGTH - 1)
42 /* System defined message ID's - |sign bit = 1|class|id| */
43 /* Event class list */
44 #define SYS_EVENT_CLS_QUEUE 0
45 #define SYS_EVENT_CLS_USB 1
46 #define SYS_EVENT_CLS_POWER 2
47 #define SYS_EVENT_CLS_FILESYS 3
48 #define SYS_EVENT_CLS_PLUG 4
49 #define SYS_EVENT_CLS_MISC 5
50 #define SYS_EVENT_CLS_PRIVATE 7 /* For use inside plugins */
51 /* make sure SYS_EVENT_CLS_BITS has enough range */
53 /* Bit 31->|S|c...c|i...i| */
54 #define SYS_EVENT ((long)(int)(1 << 31))
55 #define SYS_EVENT_CLS_BITS (3)
56 #define SYS_EVENT_CLS_SHIFT (31-SYS_EVENT_CLS_BITS)
57 #define SYS_EVENT_CLS_MASK (((1l << SYS_EVENT_CLS_BITS)-1) << SYS_EVENT_SHIFT)
58 #define MAKE_SYS_EVENT(cls, id) (SYS_EVENT | ((long)(cls) << SYS_EVENT_CLS_SHIFT) | (long)(id))
59 /* Macros for extracting codes */
60 #define SYS_EVENT_CLS(e) (((e) & SYS_EVENT_CLS_MASK) >> SYS_EVENT_SHIFT)
61 #define SYS_EVENT_ID(e) ((e) & ~(SYS_EVENT|SYS_EVENT_CLS_MASK))
63 #define SYS_TIMEOUT MAKE_SYS_EVENT(SYS_EVENT_CLS_QUEUE, 0)
64 #define SYS_USB_CONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 0)
65 #define SYS_USB_CONNECTED_ACK MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 1)
66 #define SYS_USB_DISCONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 2)
67 #define SYS_USB_DISCONNECTED_ACK MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 3)
68 #define SYS_POWEROFF MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 0)
69 #define SYS_CHARGER_CONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 1)
70 #define SYS_CHARGER_DISCONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 2)
71 #define SYS_BATTERY_UPDATE MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 3)
72 #define SYS_FS_CHANGED MAKE_SYS_EVENT(SYS_EVENT_CLS_FILESYS, 0)
73 #define SYS_HOTSWAP_INSERTED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 0)
74 #define SYS_HOTSWAP_EXTRACTED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 1)
75 #define SYS_PHONE_PLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 2)
76 #define SYS_PHONE_UNPLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 3)
77 #define SYS_REMOTE_PLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 4)
78 #define SYS_REMOTE_UNPLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 5)
79 #define SYS_SCREENDUMP MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 0)
80 #define SYS_CAR_ADAPTER_RESUME MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 1)
82 #define IS_SYSEVENT(ev) ((ev & SYS_EVENT) == SYS_EVENT)
85 #define TIMEOUT_BLOCK -1
86 #define TIMEOUT_NOBLOCK 0
95 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
96 struct queue_sender_list
98 /* If non-NULL, there is a thread waiting for the corresponding event */
99 /* Must be statically allocated to put in non-cached ram. */
100 struct thread_entry
*senders
[QUEUE_LENGTH
]; /* message->thread map */
101 struct thread_entry
*list
; /* list of senders in map */
102 /* Send info for last message dequeued or NULL if replied or not sent */
103 struct thread_entry
*curr_sender
;
104 #ifdef HAVE_PRIORITY_SCHEDULING
105 struct blocker blocker
;
108 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
110 #ifdef HAVE_PRIORITY_SCHEDULING
111 #define QUEUE_GET_THREAD(q) \
112 (((q)->send == NULL) ? NULL : (q)->send->blocker.thread)
114 /* Queue without priority enabled have no owner provision _at this time_ */
115 #define QUEUE_GET_THREAD(q) \
121 struct thread_entry
*queue
; /* waiter list */
122 struct queue_event events
[QUEUE_LENGTH
]; /* list of events */
123 unsigned int read
; /* head of queue */
124 unsigned int write
; /* tail of queue */
125 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
126 struct queue_sender_list
*send
; /* list of threads waiting for
128 #ifdef HAVE_PRIORITY_SCHEDULING
129 struct blocker
*blocker_p
; /* priority inheritance info
130 for sync message senders */
133 IF_COP( struct corelock cl
; ) /* multiprocessor sync */
136 #ifdef HAVE_PRIORITY_SCHEDULING
137 #define MUTEX_SET_THREAD(m, t) ((m)->blocker.thread = (t))
138 #define MUTEX_GET_THREAD(m) ((m)->blocker.thread)
140 #define MUTEX_SET_THREAD(m, t) ((m)->thread = (t))
141 #define MUTEX_GET_THREAD(m) ((m)->thread)
146 struct thread_entry
*queue
; /* waiter list */
147 int count
; /* lock owner recursion count */
148 #ifdef HAVE_PRIORITY_SCHEDULING
149 struct blocker blocker
; /* priority inheritance info
151 bool no_preempt
; /* don't allow higher-priority thread
152 to be scheduled even if woken */
154 struct thread_entry
*thread
;
156 IF_COP( struct corelock cl
; ) /* multiprocessor sync */
157 unsigned char locked
; /* locked semaphore */
163 struct thread_entry
*thread
; /* lock owner */
164 int count
; /* lock owner recursion count */
165 struct corelock cl
; /* multiprocessor sync */
169 #ifdef HAVE_SEMAPHORE_OBJECTS
172 struct thread_entry
*queue
; /* Waiter list */
173 int count
; /* # of waits remaining before unsignaled */
174 int max
; /* maximum # of waits to remain signaled */
175 IF_COP( struct corelock cl
; ) /* multiprocessor sync */
179 #ifdef HAVE_EVENT_OBJECTS
182 struct thread_entry
*queues
[2]; /* waiters for each state */
183 unsigned char automatic
; /* event performs auto-reset */
184 unsigned char state
; /* state: 1 = signaled */
185 IF_COP( struct corelock cl
; ) /* multiprocessor sync */
190 #ifdef HAVE_WAKEUP_OBJECTS
193 struct thread_entry
*queue
; /* waiter list */
194 unsigned char signalled
; /* signalled status */
195 IF_COP( struct corelock cl
; ) /* multiprocessor sync */
200 /* global tick variable */
201 #if defined(CPU_PP) && defined(BOOTLOADER)
202 /* We don't enable interrupts in the iPod bootloader, so we need to fake
203 the current_tick variable */
204 #define current_tick (signed)(USEC_TIMER/10000)
206 extern volatile long current_tick
;
210 #define sleep(x) sim_sleep(x)
213 /* kernel functions */
214 extern void kernel_init(void);
215 extern void yield(void);
216 extern void sleep(int ticks
);
217 int tick_add_task(void (*f
)(void));
218 int tick_remove_task(void (*f
)(void));
219 extern void tick_start(unsigned int interval_in_ms
);
223 /* timeout callback type
224 * tmo - pointer to struct timeout associated with event
226 typedef bool (* timeout_cb_type
)(struct timeout
*tmo
);
230 /* for use by callback/internal - read/write */
231 timeout_cb_type callback
;/* callback - returning false cancels */
232 int ticks
; /* timeout period in ticks */
233 intptr_t data
; /* data passed to callback */
234 /* internal use - read-only */
235 const struct timeout
* const next
; /* next timeout in list */
236 const long expires
; /* expiration tick */
239 void timeout_register(struct timeout
*tmo
, timeout_cb_type callback
,
240 int ticks
, intptr_t data
);
241 void timeout_cancel(struct timeout
*tmo
);
243 #define STATE_NONSIGNALED 0
244 #define STATE_SIGNALED 1
246 #define OBJ_WAIT_TIMEDOUT (-1)
247 #define OBJ_WAIT_FAILED 0
248 #define OBJ_WAIT_SUCCEEDED 1
250 extern void queue_init(struct event_queue
*q
, bool register_queue
);
251 extern void queue_delete(struct event_queue
*q
);
252 extern void queue_wait(struct event_queue
*q
, struct queue_event
*ev
);
253 extern void queue_wait_w_tmo(struct event_queue
*q
, struct queue_event
*ev
,
255 extern void queue_post(struct event_queue
*q
, long id
, intptr_t data
);
256 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
257 extern void queue_enable_queue_send(struct event_queue
*q
,
258 struct queue_sender_list
*send
,
259 struct thread_entry
*owner
);
260 extern intptr_t queue_send(struct event_queue
*q
, long id
, intptr_t data
);
261 extern void queue_reply(struct event_queue
*q
, intptr_t retval
);
262 extern bool queue_in_queue_send(struct event_queue
*q
);
263 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
264 extern bool queue_empty(const struct event_queue
* q
);
265 extern bool queue_peek(struct event_queue
*q
, struct queue_event
*ev
);
266 extern void queue_clear(struct event_queue
* q
);
267 extern void queue_remove_from_head(struct event_queue
*q
, long id
);
268 extern int queue_count(const struct event_queue
*q
);
269 extern int queue_broadcast(long id
, intptr_t data
);
271 extern void mutex_init(struct mutex
*m
);
272 extern void mutex_lock(struct mutex
*m
);
273 extern void mutex_unlock(struct mutex
*m
);
274 #ifdef HAVE_PRIORITY_SCHEDULING
275 /* Temporary function to disable mutex preempting a thread on unlock */
276 static inline void mutex_set_preempt(struct mutex
*m
, bool preempt
)
277 { m
->no_preempt
= !preempt
; }
280 extern void spinlock_init(struct spinlock
*l
);
281 extern void spinlock_lock(struct spinlock
*l
);
282 extern void spinlock_unlock(struct spinlock
*l
);
284 #ifdef HAVE_SEMAPHORE_OBJECTS
285 extern void semaphore_init(struct semaphore
*s
, int max
, int start
);
286 extern void semaphore_wait(struct semaphore
*s
);
287 extern void semaphore_release(struct semaphore
*s
);
288 #endif /* HAVE_SEMAPHORE_OBJECTS */
289 #ifdef HAVE_EVENT_OBJECTS
290 #define EVENT_AUTOMATIC 0x10
291 #define EVENT_MANUAL 0x00
292 extern void event_init(struct event
*e
, unsigned int flags
);
293 extern void event_wait(struct event
*e
, unsigned int for_state
);
294 extern void event_set_state(struct event
*e
, unsigned int state
);
295 #endif /* HAVE_EVENT_OBJECTS */
297 #ifdef HAVE_WAKEUP_OBJECTS
298 extern void wakeup_init(struct wakeup
*w
);
299 extern int wakeup_wait(struct wakeup
*w
, int timeout
);
300 extern int wakeup_signal(struct wakeup
*w
);
301 #endif /* HAVE_WAKEUP_OBJECTS */
303 #endif /* _KERNEL_H_ */