1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
26 /* wrap-safe macros for tick comparison */
27 #define TIME_AFTER(a,b) ((long)(b) - (long)(a) < 0)
28 #define TIME_BEFORE(a,b) TIME_AFTER(b,a)
30 #define HZ 100 /* number of ticks per second */
32 #define MAX_NUM_TICK_TASKS 8
34 #define QUEUE_LENGTH 16 /* MUST be a power of 2 */
35 #define QUEUE_LENGTH_MASK (QUEUE_LENGTH - 1)
37 /* System defined message ID's - |sign bit = 1|class|id| */
38 /* Event class list */
39 #define SYS_EVENT_CLS_QUEUE 0
40 #define SYS_EVENT_CLS_USB 1
41 #define SYS_EVENT_CLS_POWER 2
42 #define SYS_EVENT_CLS_FILESYS 3
43 #define SYS_EVENT_CLS_PLUG 4
44 /* make sure SYS_EVENT_CLS_BITS has enough range */
46 /* Bit 31->|S|c...c|i...i| */
47 #define SYS_EVENT ((long)(int)(1 << 31))
48 #define SYS_EVENT_CLS_BITS (3)
49 #define SYS_EVENT_CLS_SHIFT (31-SYS_EVENT_CLS_BITS)
50 #define SYS_EVENT_CLS_MASK (((1l << SYS_EVENT_CLS_BITS)-1) << SYS_EVENT_SHIFT)
51 #define MAKE_SYS_EVENT(cls, id) (SYS_EVENT | ((long)(cls) << SYS_EVENT_CLS_SHIFT) | (long)(id))
52 /* Macros for extracting codes */
53 #define SYS_EVENT_CLS(e) (((e) & SYS_EVENT_CLS_MASK) >> SYS_EVENT_SHIFT)
54 #define SYS_EVENT_ID(e) ((e) & ~(SYS_EVENT|SYS_EVENT_CLS_MASK))
56 #define SYS_TIMEOUT MAKE_SYS_EVENT(SYS_EVENT_CLS_QUEUE, 0)
57 #define SYS_USB_CONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 0)
58 #define SYS_USB_CONNECTED_ACK MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 1)
59 #define SYS_USB_DISCONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 2)
60 #define SYS_USB_DISCONNECTED_ACK MAKE_SYS_EVENT(SYS_EVENT_CLS_USB, 3)
61 #define SYS_POWEROFF MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 0)
62 #define SYS_CHARGER_CONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 1)
63 #define SYS_CHARGER_DISCONNECTED MAKE_SYS_EVENT(SYS_EVENT_CLS_POWER, 2)
64 #define SYS_FS_CHANGED MAKE_SYS_EVENT(SYS_EVENT_CLS_FILESYS, 0)
65 #define SYS_HOTSWAP_INSERTED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 0)
66 #define SYS_HOTSWAP_EXTRACTED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 1)
67 #define SYS_PHONE_PLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 2)
68 #define SYS_PHONE_UNPLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 3)
69 #define SYS_REMOTE_PLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 4)
70 #define SYS_REMOTE_UNPLUGGED MAKE_SYS_EVENT(SYS_EVENT_CLS_PLUG, 5)
78 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
79 struct queue_sender_list
81 /* If non-NULL, there is a thread waiting for the corresponding event */
82 /* Must be statically allocated to put in non-cached ram. */
83 struct thread_entry
*senders
[QUEUE_LENGTH
];
84 /* Send info for last message dequeued or NULL if replied or not sent */
85 struct thread_entry
*curr_sender
;
87 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
91 struct event events
[QUEUE_LENGTH
];
92 struct thread_entry
*thread
;
98 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
99 struct queue_sender_list
*send
;
106 struct thread_entry
*thread
;
109 /* global tick variable */
110 #if defined(CPU_PP) && defined(BOOTLOADER)
111 /* We don't enable interrupts in the iPod bootloader, so we need to fake
112 the current_tick variable */
113 #define current_tick (signed)(USEC_TIMER/10000)
115 extern volatile long current_tick
;
119 #define sleep(x) sim_sleep(x)
122 /* kernel functions */
123 extern void kernel_init(void);
124 extern void yield(void);
125 extern void sleep(int ticks
);
126 int tick_add_task(void (*f
)(void));
127 int tick_remove_task(void (*f
)(void));
131 /* timeout callback type
132 * tmo - pointer to struct timeout associated with event
134 typedef bool (* timeout_cb_type
)(struct timeout
*tmo
);
138 /* for use by callback/internal - read/write */
139 timeout_cb_type callback
;/* callback - returning false cancels */
140 int ticks
; /* timeout period in ticks */
141 intptr_t data
; /* data passed to callback */
142 /* internal use - read-only */
143 const struct timeout
* const next
; /* next timeout in list */
144 const long expires
; /* expiration tick */
147 void timeout_register(struct timeout
*tmo
, timeout_cb_type callback
,
148 int ticks
, intptr_t data
);
149 void timeout_cancel(struct timeout
*tmo
);
151 extern void queue_init(struct event_queue
*q
, bool register_queue
);
153 extern void queue_set_irq_safe(struct event_queue
*q
, bool state
);
155 #define queue_set_irq_safe(q,state)
157 extern void queue_delete(struct event_queue
*q
);
158 extern void queue_wait(struct event_queue
*q
, struct event
*ev
);
159 extern void queue_wait_w_tmo(struct event_queue
*q
, struct event
*ev
, int ticks
);
160 extern void queue_post(struct event_queue
*q
, long id
, intptr_t data
);
161 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
162 extern void queue_enable_queue_send(struct event_queue
*q
, struct queue_sender_list
*send
);
163 extern intptr_t queue_send(struct event_queue
*q
, long id
, intptr_t data
);
164 extern void queue_reply(struct event_queue
*q
, intptr_t retval
);
165 extern bool queue_in_queue_send(struct event_queue
*q
);
166 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
167 extern bool queue_empty(const struct event_queue
* q
);
168 extern void queue_clear(struct event_queue
* q
);
169 extern void queue_remove_from_head(struct event_queue
*q
, long id
);
170 extern int queue_count(const struct event_queue
*q
);
171 extern int queue_broadcast(long id
, intptr_t data
);
173 extern void mutex_init(struct mutex
*m
);
174 static inline void spinlock_init(struct mutex
*m
)
175 { mutex_init(m
); } /* Same thing for now */
176 extern void mutex_lock(struct mutex
*m
);
177 extern void mutex_unlock(struct mutex
*m
);
178 extern void spinlock_lock(struct mutex
*m
);
179 extern void spinlock_unlock(struct mutex
*m
);
180 extern void tick_start(unsigned int interval_in_ms
);
182 #define IS_SYSEVENT(ev) ((ev & SYS_EVENT) == SYS_EVENT)