Fixed hidden recording status bar
[kugel-rb.git] / firmware / usb.c
blob2cd5c9b93be3fbc8d173d23a2c95593b3ade030f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
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 ****************************************************************************/
19 #include "config.h"
20 #include "sh7034.h"
21 #include "kernel.h"
22 #include "thread.h"
23 #include "system.h"
24 #include "debug.h"
25 #include "ata.h"
26 #include "fat.h"
27 #include "disk.h"
28 #include "panic.h"
29 #include "lcd.h"
30 #include "adc.h"
31 #include "usb.h"
32 #include "button.h"
33 #include "sprintf.h"
34 #include "hwcompat.h"
36 extern void dbg_ports(void); /* NASTY! defined in apps/ */
38 #define USB_REALLY_BRAVE
41 #ifndef SIMULATOR
43 /* Messages from usb_tick */
44 #define USB_INSERTED 1
45 #define USB_EXTRACTED 2
47 /* Thread states */
48 #define EXTRACTING 1
49 #define EXTRACTED 2
50 #define INSERTED 3
51 #define INSERTING 4
53 /* The ADC tick reads one channel per tick, and we want to check 3 successive
54 readings on the USB voltage channel. This doesn't apply to the Player, but
55 debouncing the USB detection port won't hurt us either. */
56 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
57 static int countdown;
59 static int usb_state;
61 static char usb_stack[DEFAULT_STACK_SIZE];
62 static char usb_thread_name[] = "usb";
63 static struct event_queue usb_queue;
64 static bool last_usb_status;
65 static bool usb_monitor_enabled;
67 static void usb_enable(bool on)
69 #ifdef HAVE_LCD_BITMAP
70 if(read_hw_mask() & USB_ACTIVE_HIGH)
71 on = !on;
72 #endif
74 if(on)
75 PADR &= ~0x400; /* enable USB */
76 else
77 PADR |= 0x400;
78 PAIOR |= 0x400;
81 static void usb_slave_mode(bool on)
83 int rc;
84 struct partinfo* pinfo;
86 if(on)
88 DEBUGF("Entering USB slave mode\n");
89 ata_soft_reset();
90 ata_init();
91 ata_standby(15);
92 ata_enable(false);
93 usb_enable(true);
95 else
97 DEBUGF("Leaving USB slave mode\n");
99 /* Let the ISDx00 settle */
100 sleep(HZ*1);
102 usb_enable(false);
104 rc = ata_init();
105 if(rc)
107 char str[32];
108 lcd_clear_display();
109 snprintf(str, 31, "ATA error: %d", rc);
110 lcd_puts(0, 0, str);
111 lcd_puts(0, 1, "Press ON to debug");
112 lcd_update();
113 while(button_get(true) != BUTTON_ON) {};
114 dbg_ports();
115 panicf("ata: %d",rc);
118 pinfo = disk_init();
119 if (!pinfo)
120 panicf("disk: NULL");
122 rc = fat_mount(pinfo[0].start);
123 if(rc)
124 panicf("mount: %d",rc);
128 static void usb_thread(void)
130 int num_acks_to_expect = -1;
131 bool waiting_for_ack;
132 struct event ev;
134 waiting_for_ack = false;
136 while(1)
138 queue_wait(&usb_queue, &ev);
139 switch(ev.id)
141 case USB_INSERTED:
142 /* Tell all threads that they have to back off the ATA.
143 We subtract one for our own thread. */
144 num_acks_to_expect =
145 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
146 waiting_for_ack = true;
147 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
148 num_acks_to_expect);
149 break;
151 case SYS_USB_CONNECTED_ACK:
152 if(waiting_for_ack)
154 num_acks_to_expect--;
155 if(num_acks_to_expect == 0)
157 DEBUGF("All threads have acknowledged the connect.\n");
158 #ifdef USB_REALLY_BRAVE
159 usb_slave_mode(true);
160 usb_state = USB_INSERTED;
161 #else
162 system_reboot();
163 #endif
165 else
167 DEBUGF("usb: got ack, %d to go...\n",
168 num_acks_to_expect);
171 break;
173 case USB_EXTRACTED:
174 if(usb_state == USB_INSERTED)
176 /* Only disable the USB mode if we really have enabled it
177 some threads might not have acknowledged the
178 insertion */
179 usb_slave_mode(false);
182 usb_state = USB_EXTRACTED;
184 /* Tell all threads that we are back in business */
185 num_acks_to_expect =
186 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
187 waiting_for_ack = true;
188 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
189 num_acks_to_expect);
190 #ifdef HAVE_LCD_CHARCELLS
191 lcd_icon(ICON_USB, false);
192 #endif
193 break;
195 case SYS_USB_DISCONNECTED_ACK:
196 if(waiting_for_ack)
198 num_acks_to_expect--;
199 if(num_acks_to_expect == 0)
201 DEBUGF("All threads have acknowledged. "
202 "We're in business.\n");
204 else
206 DEBUGF("usb: got ack, %d to go...\n",
207 num_acks_to_expect);
210 break;
215 static void usb_tick(void)
217 bool current_status;
219 if(usb_monitor_enabled)
221 #ifdef ARCHOS_RECORDER
222 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
223 #else
224 #ifdef ARCHOS_FMRECORDER
225 current_status = (adc_read(ADC_USB_POWER) < 512)?true:false;
226 #else
227 current_status = (PADR & 0x8000)?false:true;
228 #endif
229 #endif
231 /* Only report when the status has changed */
232 if(current_status != last_usb_status)
234 last_usb_status = current_status;
235 countdown = NUM_POLL_READINGS;
237 else
239 /* Count down until it gets negative */
240 if(countdown >= 0)
241 countdown--;
243 /* Report to the thread if we have had 3 identical status
244 readings in a row */
245 if(countdown == 0)
247 if(current_status)
248 queue_post(&usb_queue, USB_INSERTED, NULL);
249 else
250 queue_post(&usb_queue, USB_EXTRACTED, NULL);
256 void usb_acknowledge(int id)
258 queue_post(&usb_queue, id, NULL);
261 void usb_init(void)
263 usb_state = USB_EXTRACTED;
264 usb_monitor_enabled = false;
265 countdown = -1;
267 usb_enable(false);
269 /* We assume that the USB cable is extracted */
270 last_usb_status = false;
272 queue_init(&usb_queue);
273 create_thread(usb_thread, usb_stack, sizeof(usb_stack), usb_thread_name);
275 tick_add_task(usb_tick);
278 void usb_wait_for_disconnect(struct event_queue *q)
280 struct event ev;
282 /* Don't return until we get SYS_USB_DISCONNECTED */
283 while(1)
285 queue_wait(q, &ev);
286 if(ev.id == SYS_USB_DISCONNECTED)
288 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
289 return;
294 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
296 struct event ev;
298 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
299 while(1)
301 queue_wait_w_tmo(q, &ev, ticks);
302 switch(ev.id)
304 case SYS_USB_DISCONNECTED:
305 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
306 return 0;
307 break;
308 case SYS_TIMEOUT:
309 return 1;
310 break;
315 void usb_start_monitoring(void)
317 usb_monitor_enabled = true;
320 bool usb_inserted(void)
322 return usb_state == USB_INSERTED;
325 #else
327 /* Dummy simulator functions */
328 void usb_acknowledge(int id)
330 id = id;
333 void usb_init(void)
337 void usb_start_monitoring(void)
341 #endif