Fixed crash when changing between id3 and file mode in wps.
[kugel-rb.git] / firmware / usb.c
blob7060ccfe236d70f24d646e14083e31dcc4b1a423
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 "cpu.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"
35 #ifdef HAVE_MMC
36 #include "ata_mmc.h"
37 #endif
39 extern void dbg_ports(void); /* NASTY! defined in apps/ */
41 #ifdef HAVE_LCD_BITMAP
42 bool do_screendump_instead_of_usb = false;
43 void screen_dump(void); /* Nasty again. Defined in apps/ too */
44 #endif
46 #define USB_REALLY_BRAVE
48 #if !defined(SIMULATOR) && !defined(USB_NONE)
50 /* Messages from usb_tick */
51 #define USB_INSERTED 1
52 #define USB_EXTRACTED 2
53 #ifdef HAVE_MMC
54 #define USB_REENABLE 3
55 #endif
57 /* Thread states */
58 #define EXTRACTING 1
59 #define EXTRACTED 2
60 #define INSERTED 3
61 #define INSERTING 4
63 /* The ADC tick reads one channel per tick, and we want to check 3 successive
64 readings on the USB voltage channel. This doesn't apply to the Player, but
65 debouncing the USB detection port won't hurt us either. */
66 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
67 static int countdown;
69 static int usb_state;
71 #ifdef HAVE_MMC
72 static int usb_mmc_countdown = 0;
73 #endif
75 /* FIXME: The extra 0x400 is consumed by fat_mount() when the fsinfo
76 needs updating */
77 static char usb_stack[DEFAULT_STACK_SIZE + 0x400];
78 static const char usb_thread_name[] = "usb";
79 static struct event_queue usb_queue;
80 static bool last_usb_status;
81 static bool usb_monitor_enabled;
83 static void usb_enable(bool on)
85 #ifdef USB_ENABLE_ONDIOSTYLE
86 PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
87 if(on)
89 #ifdef HAVE_MMC
90 mmc_select_clock(mmc_detect() ? 1 : 0);
91 #endif
92 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
93 and_b(~0x20, &PBDRH); /* old circuit needs SCK1 = low while on USB */
94 or_b(0x20, &PADRL); /* enable USB */
95 and_b(~0x08, &PADRL); /* assert card detect */
97 else
99 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
100 or_b(0x20, &PBDRH); /* reset SCK1 = high for old circuit */
101 and_b(~0x20, &PADRL); /* disable USB */
102 or_b(0x08, &PADRL); /* deassert card detect */
104 or_b(0x28, &PAIORL); /* output for USB enable and card detect */
105 #else /* standard HD Jukebox */
106 #ifdef HAVE_LCD_BITMAP
107 if(read_hw_mask() & USB_ACTIVE_HIGH)
108 on = !on;
109 #endif
110 if(on)
112 and_b(~0x04, &PADRH); /* enable USB */
114 else
116 or_b(0x04, &PADRH);
118 or_b(0x04, &PAIORH);
119 #endif
122 static void usb_slave_mode(bool on)
124 int rc;
126 if(on)
128 DEBUGF("Entering USB slave mode\n");
129 ata_soft_reset();
130 ata_init();
131 ata_standby(15);
132 ata_enable(false);
133 usb_enable(true);
135 else
137 DEBUGF("Leaving USB slave mode\n");
139 /* Let the ISDx00 settle */
140 sleep(HZ*1);
142 usb_enable(false);
144 rc = ata_init();
145 if(rc)
147 /* fixme: can we remove this? (already such in main.c) */
148 char str[32];
149 lcd_clear_display();
150 snprintf(str, 31, "ATA error: %d", rc);
151 lcd_puts(0, 0, str);
152 lcd_puts(0, 1, "Press ON to debug");
153 lcd_update();
154 while(!(button_get(true) & BUTTON_REL)) {};
155 dbg_ports();
156 panicf("ata: %d",rc);
159 rc = disk_mount_all();
160 if (rc <= 0) /* no partition */
161 panicf("mount: %d",rc);
166 static void usb_thread(void)
168 int num_acks_to_expect = -1;
169 bool waiting_for_ack;
170 struct event ev;
172 waiting_for_ack = false;
174 while(1)
176 queue_wait(&usb_queue, &ev);
177 switch(ev.id)
179 case USB_INSERTED:
180 #ifdef HAVE_LCD_BITMAP
181 if(do_screendump_instead_of_usb)
183 screen_dump();
185 else
187 #endif
188 /* Tell all threads that they have to back off the ATA.
189 We subtract one for our own thread. */
190 num_acks_to_expect =
191 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
192 waiting_for_ack = true;
193 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
194 num_acks_to_expect);
195 #ifdef HAVE_LCD_BITMAP
197 #endif
198 break;
200 case SYS_USB_CONNECTED_ACK:
201 if(waiting_for_ack)
203 num_acks_to_expect--;
204 if(num_acks_to_expect == 0)
206 DEBUGF("All threads have acknowledged the connect.\n");
207 #ifdef USB_REALLY_BRAVE
208 usb_slave_mode(true);
209 usb_state = USB_INSERTED;
210 #else
211 system_reboot();
212 #endif
214 else
216 DEBUGF("usb: got ack, %d to go...\n",
217 num_acks_to_expect);
220 break;
222 case USB_EXTRACTED:
223 #ifdef HAVE_LCD_BITMAP
224 if(!do_screendump_instead_of_usb)
226 #endif
227 if(usb_state == USB_INSERTED)
229 /* Only disable the USB mode if we really have enabled it
230 some threads might not have acknowledged the
231 insertion */
232 usb_slave_mode(false);
235 usb_state = USB_EXTRACTED;
237 /* Tell all threads that we are back in business */
238 num_acks_to_expect =
239 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
240 waiting_for_ack = true;
241 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
242 num_acks_to_expect);
243 #ifdef HAVE_LCD_CHARCELLS
244 lcd_icon(ICON_USB, false);
245 #endif
246 #ifdef HAVE_LCD_BITMAP
248 #endif
249 break;
251 case SYS_USB_DISCONNECTED_ACK:
252 if(waiting_for_ack)
254 num_acks_to_expect--;
255 if(num_acks_to_expect == 0)
257 DEBUGF("All threads have acknowledged. "
258 "We're in business.\n");
260 else
262 DEBUGF("usb: got ack, %d to go...\n",
263 num_acks_to_expect);
266 break;
268 #ifdef HAVE_MMC
269 case SYS_MMC_INSERTED:
270 case SYS_MMC_EXTRACTED:
271 if(usb_state == USB_INSERTED)
273 usb_enable(false);
274 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
276 break;
278 case USB_REENABLE:
279 if(usb_state == USB_INSERTED)
280 usb_enable(true); /* reenable only if still inserted */
281 break;
282 #endif
287 bool usb_detect(void)
289 bool current_status;
291 #ifdef USB_RECORDERSTYLE
292 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
293 #endif
294 #ifdef USB_FMRECORDERSTYLE
295 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
296 #endif
297 #ifdef USB_PLAYERSTYLE
298 current_status = (PADR & 0x8000)?false:true;
299 #endif
300 #ifdef IRIVER_H100
301 current_status = (GPIO1_READ & 0x80)?true:false;
302 #endif
303 return current_status;
307 static void usb_tick(void)
309 bool current_status;
311 if(usb_monitor_enabled)
313 current_status = usb_detect();
315 /* Only report when the status has changed */
316 if(current_status != last_usb_status)
318 last_usb_status = current_status;
319 countdown = NUM_POLL_READINGS;
321 else
323 /* Count down until it gets negative */
324 if(countdown >= 0)
325 countdown--;
327 /* Report to the thread if we have had 3 identical status
328 readings in a row */
329 if(countdown == 0)
331 if(current_status)
332 queue_post(&usb_queue, USB_INSERTED, NULL);
333 else
334 queue_post(&usb_queue, USB_EXTRACTED, NULL);
338 #ifdef HAVE_MMC
339 if(usb_mmc_countdown > 0)
341 usb_mmc_countdown--;
342 if (usb_mmc_countdown == 0)
343 queue_post(&usb_queue, USB_REENABLE, NULL);
345 #endif
348 void usb_acknowledge(int id)
350 queue_post(&usb_queue, id, NULL);
353 void usb_init(void)
355 usb_state = USB_EXTRACTED;
356 usb_monitor_enabled = false;
357 countdown = -1;
359 #ifdef IRIVER_H100
360 GPIO1_FUNCTION |= 0x80; /* GPIO39 is the USB detect input */
361 #endif
363 usb_enable(false);
365 /* We assume that the USB cable is extracted */
366 last_usb_status = false;
368 queue_init(&usb_queue);
369 create_thread(usb_thread, usb_stack, sizeof(usb_stack), usb_thread_name);
371 tick_add_task(usb_tick);
374 void usb_wait_for_disconnect(struct event_queue *q)
376 struct event ev;
378 /* Don't return until we get SYS_USB_DISCONNECTED */
379 while(1)
381 queue_wait(q, &ev);
382 if(ev.id == SYS_USB_DISCONNECTED)
384 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
385 return;
390 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
392 struct event ev;
394 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
395 while(1)
397 queue_wait_w_tmo(q, &ev, ticks);
398 switch(ev.id)
400 case SYS_USB_DISCONNECTED:
401 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
402 return 0;
403 break;
404 case SYS_TIMEOUT:
405 return 1;
406 break;
411 void usb_start_monitoring(void)
413 usb_monitor_enabled = true;
416 bool usb_inserted(void)
418 return usb_state == USB_INSERTED;
421 #else
423 #ifdef USB_NONE
424 bool usb_inserted(void)
426 return false;
428 #endif
430 /* Dummy simulator functions */
431 void usb_acknowledge(int id)
433 id = id;
436 void usb_init(void)
440 void usb_start_monitoring(void)
444 bool usb_detect(void)
446 return false;
449 #endif /* USB_NONE or SIMULATOR */