1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009 by Jens Arnold
11 * Copyright (C) 2011 by Thomas Martitz
13 * Rockbox simulator specific tasks
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
27 #include "screendump.h"
32 static void sim_thread(void);
33 static long sim_thread_stack
[DEFAULT_STACK_SIZE
/sizeof(long)];
34 /* stack isn't actually used in the sim */
35 static const char sim_thread_name
[] = "sim";
36 static struct event_queue sim_queue
;
38 /* possible events for the sim thread */
47 struct queue_event ev
;
48 long last_broadcast_tick
= current_tick
;
49 int num_acks_to_expect
= 0;
53 queue_wait(&sim_queue
, &ev
);
58 #ifdef HAVE_REMOTE_LCD
62 case SIM_USB_INSERTED
:
63 /* from firmware/usb.c: */
64 /* Tell all threads that they have to back off the storage.
65 We subtract one for our own thread. Expect an ACK for every
66 listener for each broadcast they received. If it has been too
67 long, the user might have entered a screen that didn't ACK
68 when inserting the cable, such as a debugging screen. In that
69 case, reset the count or else USB would be locked out until
70 rebooting because it most likely won't ever come. Simply
71 resetting to the most recent broadcast count is racy. */
72 if(TIME_AFTER(current_tick
, last_broadcast_tick
+ HZ
*5))
74 num_acks_to_expect
= 0;
75 last_broadcast_tick
= current_tick
;
78 num_acks_to_expect
+= queue_broadcast(SYS_USB_CONNECTED
, 0) - 1;
79 DEBUGF("USB inserted. Waiting for %d acks...\n",
82 case SYS_USB_CONNECTED_ACK
:
83 if(num_acks_to_expect
> 0 && --num_acks_to_expect
== 0)
85 DEBUGF("All threads have acknowledged the connect.\n");
89 DEBUGF("usb: got ack, %d to go...\n",
93 case SIM_USB_EXTRACTED
:
94 /* in usb.c, this is only done for exclusive storage
95 * do it here anyway but don't depend on the acks */
96 queue_broadcast(SYS_USB_DISCONNECTED
, 0);
99 DEBUGF("sim_tasks: unhandled event: %ld\n", ev
.id
);
105 void sim_tasks_init(void)
107 queue_init(&sim_queue
, false);
109 create_thread(sim_thread
, sim_thread_stack
, sizeof(sim_thread_stack
), 0,
110 sim_thread_name
IF_PRIO(,PRIORITY_BACKGROUND
) IF_COP(,CPU
));
113 void sim_trigger_screendump(void)
115 queue_post(&sim_queue
, SIM_SCREENDUMP
, 0);
118 static bool is_usb_inserted
;
119 void sim_trigger_usb(bool inserted
)
122 queue_post(&sim_queue
, SIM_USB_INSERTED
, 0);
124 queue_post(&sim_queue
, SIM_USB_EXTRACTED
, 0);
125 is_usb_inserted
= inserted
;
130 return is_usb_inserted
? USB_INSERTED
: USB_EXTRACTED
;
137 void usb_start_monitoring(void)
141 void usb_acknowledge(long id
)
143 queue_post(&sim_queue
, id
, 0);
146 void usb_wait_for_disconnect(struct event_queue
*q
)
148 struct queue_event ev
;
150 /* Don't return until we get SYS_USB_DISCONNECTED */
154 if(ev
.id
== SYS_USB_DISCONNECTED
)