Android: no need to keep RockboxPCM_class around
[maemo-rb.git] / firmware / export / kernel.h
blob6aaf11ddb9ececbfb92b5def9bf6ac5ff7320a8e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #ifndef _KERNEL_H_
22 #define _KERNEL_H_
24 #include <stdbool.h>
25 #include <inttypes.h>
26 #include "config.h"
28 #include "thread.h"
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_LUN_LOCKED MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 4)
68 #define SYS_USB_READ_DATA MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 5)
69 #define SYS_USB_WRITE_DATA MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 6)
70 #define SYS_POWEROFF MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 0)
71 #define SYS_CHARGER_CONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 1)
72 #define SYS_CHARGER_DISCONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 2)
73 #define SYS_BATTERY_UPDATE MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 3)
74 #define SYS_FS_CHANGED MAKE_SYS_EVENT(SYS_EVENT_CLS_FILESYS, 0)
75 #define SYS_HOTSWAP_INSERTED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 0)
76 #define SYS_HOTSWAP_EXTRACTED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 1)
77 #define SYS_PHONE_PLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 2)
78 #define SYS_PHONE_UNPLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 3)
79 #define SYS_REMOTE_PLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 4)
80 #define SYS_REMOTE_UNPLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 5)
81 #define SYS_CAR_ADAPTER_RESUME MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 0)
82 #define SYS_IAP_PERIODIC MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 1)
83 #define SYS_IAP_HANDLEPKT MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 2)
84 #define SYS_CALL_INCOMING MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 3)
85 #define SYS_CALL_HUNG_UP MAKE_SYS_EVENT(SYS_EVENT_CLS_MISC, 4)
87 #define IS_SYSEVENT(ev) ((ev & SYS_EVENT) == SYS_EVENT)
89 #ifndef TIMEOUT_BLOCK
90 #define TIMEOUT_BLOCK -1
91 #define TIMEOUT_NOBLOCK 0
92 #endif
94 struct queue_event
96 long id;
97 intptr_t data;
100 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
101 struct queue_sender_list
103 /* If non-NULL, there is a thread waiting for the corresponding event */
104 /* Must be statically allocated to put in non-cached ram. */
105 struct thread_entry *senders[QUEUE_LENGTH]; /* message->thread map */
106 struct thread_entry *list; /* list of senders in map */
107 /* Send info for last message dequeued or NULL if replied or not sent */
108 struct thread_entry * volatile curr_sender;
109 #ifdef HAVE_PRIORITY_SCHEDULING
110 struct blocker blocker;
111 #endif
113 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
115 #ifdef HAVE_PRIORITY_SCHEDULING
116 #define QUEUE_GET_THREAD(q) \
117 (((q)->send == NULL) ? NULL : (q)->send->blocker.thread)
118 #else
119 /* Queue without priority enabled have no owner provision _at this time_ */
120 #define QUEUE_GET_THREAD(q) \
121 (NULL)
122 #endif
124 struct event_queue
126 struct thread_entry *queue; /* waiter list */
127 struct queue_event events[QUEUE_LENGTH]; /* list of events */
128 unsigned int volatile read; /* head of queue */
129 unsigned int volatile write; /* tail of queue */
130 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
131 struct queue_sender_list * volatile send; /* list of threads waiting for
132 reply to an event */
133 #ifdef HAVE_PRIORITY_SCHEDULING
134 struct blocker *blocker_p; /* priority inheritance info
135 for sync message senders */
136 #endif
137 #endif
138 IF_COP( struct corelock cl; ) /* multiprocessor sync */
141 struct mutex
143 struct thread_entry *queue; /* waiter list */
144 int recursion; /* lock owner recursion count */
145 #ifdef HAVE_PRIORITY_SCHEDULING
146 struct blocker blocker; /* priority inheritance info
147 for waiters */
148 bool no_preempt; /* don't allow higher-priority thread
149 to be scheduled even if woken */
150 #else
151 struct thread_entry *thread; /* Indicates owner thread - an owner
152 implies a locked state - same goes
153 for priority scheduling
154 (in blocker struct for that) */
155 #endif
156 IF_COP( struct corelock cl; ) /* multiprocessor sync */
159 #ifdef HAVE_SEMAPHORE_OBJECTS
160 struct semaphore
162 struct thread_entry *queue; /* Waiter list */
163 int volatile count; /* # of waits remaining before unsignaled */
164 int max; /* maximum # of waits to remain signaled */
165 IF_COP( struct corelock cl; ) /* multiprocessor sync */
167 #endif
169 /* global tick variable */
170 #if defined(CPU_PP) && defined(BOOTLOADER) && \
171 !defined(HAVE_BOOTLOADER_USB_MODE)
172 /* We don't enable interrupts in the PP bootloader unless USB mode is
173 enabled for it, so we need to fake the current_tick variable */
174 #define current_tick (signed)(USEC_TIMER/10000)
176 static inline void call_tick_tasks(void)
179 #else
180 extern volatile long current_tick;
182 /* inline helper for implementing target interrupt handler */
183 static inline void call_tick_tasks(void)
185 extern void (*tick_funcs[MAX_NUM_TICK_TASKS+1])(void);
186 void (**p)(void) = tick_funcs;
187 void (*fn)(void);
189 current_tick++;
191 for(fn = *p; fn != NULL; fn = *(++p))
193 fn();
196 #endif
198 /* kernel functions */
199 extern void kernel_init(void) INIT_ATTR;
200 extern void yield(void);
201 extern unsigned sleep(unsigned ticks);
202 int tick_add_task(void (*f)(void));
203 int tick_remove_task(void (*f)(void));
204 extern void tick_start(unsigned int interval_in_ms) INIT_ATTR;
206 #ifdef INCLUDE_TIMEOUT_API
207 struct timeout;
209 /* timeout callback type
210 * tmo - pointer to struct timeout associated with event
211 * return next interval or <= 0 to stop event
213 #define MAX_NUM_TIMEOUTS 8
214 typedef int (* timeout_cb_type)(struct timeout *tmo);
216 struct timeout
218 timeout_cb_type callback;/* callback - returning false cancels */
219 intptr_t data; /* data passed to callback */
220 long expires; /* expiration tick */
223 void timeout_register(struct timeout *tmo, timeout_cb_type callback,
224 int ticks, intptr_t data);
225 void timeout_cancel(struct timeout *tmo);
226 #endif /* INCLUDE_TIMEOUT_API */
228 #define STATE_NONSIGNALED 0
229 #define STATE_SIGNALED 1
231 #define OBJ_WAIT_TIMEDOUT (-1)
232 #define OBJ_WAIT_FAILED 0
233 #define OBJ_WAIT_SUCCEEDED 1
235 extern void queue_init(struct event_queue *q, bool register_queue);
236 extern void queue_delete(struct event_queue *q);
237 extern void queue_wait(struct event_queue *q, struct queue_event *ev);
238 extern void queue_wait_w_tmo(struct event_queue *q, struct queue_event *ev,
239 int ticks);
240 extern void queue_post(struct event_queue *q, long id, intptr_t data);
241 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
242 extern void queue_enable_queue_send(struct event_queue *q,
243 struct queue_sender_list *send,
244 unsigned int owner_id);
245 extern intptr_t queue_send(struct event_queue *q, long id, intptr_t data);
246 extern void queue_reply(struct event_queue *q, intptr_t retval);
247 extern bool queue_in_queue_send(struct event_queue *q);
248 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
249 extern bool queue_empty(const struct event_queue* q);
250 extern bool queue_peek(struct event_queue *q, struct queue_event *ev);
251 extern void queue_clear(struct event_queue* q);
252 extern void queue_remove_from_head(struct event_queue *q, long id);
253 extern int queue_count(const struct event_queue *q);
254 extern int queue_broadcast(long id, intptr_t data);
256 extern void mutex_init(struct mutex *m);
257 extern void mutex_lock(struct mutex *m);
258 extern void mutex_unlock(struct mutex *m);
259 #ifdef HAVE_PRIORITY_SCHEDULING
260 /* Deprecated temporary function to disable mutex preempting a thread on
261 * unlock - firmware/drivers/fat.c and a couple places in apps/buffering.c -
262 * reliance on it is a bug! */
263 static inline void mutex_set_preempt(struct mutex *m, bool preempt)
264 { m->no_preempt = !preempt; }
265 #else
266 /* Deprecated but needed for now - firmware/drivers/ata_mmc.c */
267 static inline bool mutex_test(const struct mutex *m)
268 { return m->thread != NULL; }
269 #endif /* HAVE_PRIORITY_SCHEDULING */
271 #ifdef HAVE_SEMAPHORE_OBJECTS
272 extern void semaphore_init(struct semaphore *s, int max, int start);
273 extern int semaphore_wait(struct semaphore *s, int timeout);
274 extern void semaphore_release(struct semaphore *s);
275 #endif /* HAVE_SEMAPHORE_OBJECTS */
277 #endif /* _KERNEL_H_ */