New USB stack
[kugel-rb.git] / apps / main.c
blob0ce8bcc7acadbda03080701f7bb7a8d1d8dcbca8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 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 ****************************************************************************/
19 #include "config.h"
21 #include "ata.h"
22 #include "ata_idle_notify.h"
23 #include "disk.h"
24 #include "fat.h"
25 #include "lcd.h"
26 #include "rtc.h"
27 #include "debug.h"
28 #include "led.h"
29 #include "kernel.h"
30 #include "button.h"
31 #include "tree.h"
32 #include "filetypes.h"
33 #include "panic.h"
34 #include "menu.h"
35 #include "system.h"
36 #include "usb.h"
37 #include "powermgmt.h"
38 #include "adc.h"
39 #include "i2c.h"
40 #ifndef DEBUG
41 #include "serial.h"
42 #endif
43 #include "audio.h"
44 #include "mp3_playback.h"
45 #include "thread.h"
46 #include "settings.h"
47 #include "backlight.h"
48 #include "status.h"
49 #include "debug_menu.h"
50 #include "version.h"
51 #include "sprintf.h"
52 #include "font.h"
53 #include "language.h"
54 #include "gwps.h"
55 #include "playlist.h"
56 #include "buffer.h"
57 #include "rolo.h"
58 #include "screens.h"
59 #include "power.h"
60 #include "talk.h"
61 #include "plugin.h"
62 #include "misc.h"
63 #include "dircache.h"
64 #ifdef HAVE_TAGCACHE
65 #include "tagcache.h"
66 #include "tagtree.h"
67 #endif
68 #include "lang.h"
69 #include "string.h"
70 #include "splash.h"
71 #include "eeprom_settings.h"
72 #include "scrobbler.h"
73 #include "icon.h"
75 #if (CONFIG_CODEC == SWCODEC)
76 #include "playback.h"
77 #include "pcmbuf.h"
78 #else
79 #define pcmbuf_init()
80 #endif
81 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
82 #include "pcm_record.h"
83 #endif
85 #ifdef BUTTON_REC
86 #define SETTINGS_RESET BUTTON_REC
87 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
88 #define SETTINGS_RESET BUTTON_A
89 #endif
91 #if CONFIG_TUNER
92 #include "radio.h"
93 #endif
94 #ifdef HAVE_MMC
95 #include "ata_mmc.h"
96 #endif
98 #ifdef HAVE_REMOTE_LCD
99 #include "lcd-remote.h"
100 #endif
102 #if CONFIG_USBOTG == USBOTG_ISP1362
103 #include "isp1362.h"
104 #endif
106 #if CONFIG_USBOTG == USBOTG_M5636
107 #include "m5636.h"
108 #endif
110 #include "cuesheet.h"
112 #ifdef SIMULATOR
113 #include "system-sdl.h"
114 #endif
116 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
118 const char appsversion[]=APPSVERSION;
120 static void init(void);
122 #ifdef SIMULATOR
123 void app_main(void)
124 #else
125 static void app_main(void)
126 #endif
128 init();
129 tree_gui_init();
130 root_menu();
133 static int init_dircache(bool preinit)
135 #ifdef HAVE_DIRCACHE
136 int result = 0;
137 bool clear = false;
139 if (preinit)
140 dircache_init();
142 if (!global_settings.dircache)
143 return 0;
145 # ifdef HAVE_EEPROM_SETTINGS
146 if (firmware_settings.initialized && firmware_settings.disk_clean
147 && preinit)
149 result = dircache_load();
151 if (result < 0)
153 firmware_settings.disk_clean = false;
154 if (global_status.dircache_size <= 0)
156 /* This will be in default language, settings are not
157 applied yet. Not really any easy way to fix that. */
158 gui_syncsplash(0, str(LANG_SCANNING_DISK));
159 clear = true;
162 dircache_build(global_status.dircache_size);
165 else
166 # endif
168 if (preinit)
169 return -1;
171 if (!dircache_is_enabled()
172 && !dircache_is_initializing())
174 if (global_status.dircache_size <= 0)
176 gui_syncsplash(0, str(LANG_SCANNING_DISK));
177 clear = true;
179 result = dircache_build(global_status.dircache_size);
182 if (result < 0)
184 /* Initialization of dircache failed. Manual action is
185 * necessary to enable dircache again.
187 gui_syncsplash(0, "Dircache failed, disabled. Result: %d", result);
188 global_settings.dircache = false;
193 if (clear)
195 backlight_on();
196 show_logo();
197 global_status.dircache_size = dircache_get_cache_size();
198 status_save();
201 return result;
202 #else
203 (void)preinit;
204 return 0;
205 #endif
208 #ifdef HAVE_TAGCACHE
209 static void init_tagcache(void)
211 bool clear = false;
213 tagcache_init();
215 while (!tagcache_is_initialized())
217 #ifdef HAVE_LCD_CHARCELLS
218 char buf[32];
219 #endif
220 int ret = tagcache_get_commit_step();
222 if (ret > 0)
224 #if CONFIG_CODEC == SWCODEC
225 /* hwcodec can't use voice here, as the database commit
226 * uses the audio buffer. */
227 static long talked_tick = 0;
228 if(global_settings.talk_menu
229 && (talked_tick == 0
230 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
232 talked_tick = current_tick;
233 talk_id(LANG_TAGCACHE_INIT, false);
234 talk_number(ret, true);
235 talk_id(VOICE_OF, true);
236 talk_number(tagcache_get_max_commit_step(), true);
238 #endif
239 #ifdef HAVE_LCD_BITMAP
240 gui_syncsplash(0, "%s [%d/%d]",
241 str(LANG_TAGCACHE_INIT), ret,
242 tagcache_get_max_commit_step());
243 #else
244 lcd_double_height(false);
245 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
246 tagcache_get_max_commit_step());
247 lcd_puts(0, 1, buf);
248 lcd_update();
249 #endif
250 clear = true;
252 sleep(HZ/4);
254 tagtree_init();
256 if (clear)
258 backlight_on();
259 show_logo();
262 #endif
264 #ifdef SIMULATOR
266 static void init(void)
268 init_threads();
269 buffer_init();
270 set_irq_level(0);
271 lcd_init();
272 #ifdef HAVE_REMOTE_LCD
273 lcd_remote_init();
274 #endif
275 font_init();
276 show_logo();
277 button_init();
278 backlight_init();
279 lang_init();
280 #ifdef DEBUG
281 debug_init();
282 #endif
283 /* Must be done before any code uses the multi-screen APi */
284 screen_access_init();
285 gui_syncstatusbar_init(&statusbars);
286 ata_init();
287 settings_reset();
288 settings_load(SETTINGS_ALL);
289 gui_sync_wps_init();
290 settings_apply();
291 init_dircache(true);
292 init_dircache(false);
293 #ifdef HAVE_TAGCACHE
294 init_tagcache();
295 #endif
296 sleep(HZ/2);
297 tree_mem_init();
298 filetype_init();
299 playlist_init();
301 #if CONFIG_CODEC != SWCODEC
302 mp3_init( global_settings.volume,
303 global_settings.bass,
304 global_settings.treble,
305 global_settings.balance,
306 global_settings.loudness,
307 global_settings.avc,
308 global_settings.channel_config,
309 global_settings.stereo_width,
310 global_settings.mdb_strength,
311 global_settings.mdb_harmonics,
312 global_settings.mdb_center,
313 global_settings.mdb_shape,
314 global_settings.mdb_enable,
315 global_settings.superbass);
317 /* audio_init must to know the size of voice buffer so init voice first */
318 talk_init();
319 #endif /* CONFIG_CODEC != SWCODEC */
321 scrobbler_init();
322 cuesheet_init();
324 audio_init();
325 button_clear_queue(); /* Empty the keyboard buffer */
328 #else
330 static void init(void)
332 int rc;
333 bool mounted = false;
334 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
335 /* if nobody initialized ATA before, I consider this a cold start */
336 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
337 #endif
339 system_init();
340 kernel_init();
342 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
343 set_cpu_frequency(CPUFREQ_NORMAL);
344 #ifdef CPU_COLDFIRE
345 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
346 #endif
347 cpu_boost(true);
348 #endif
350 buffer_init();
352 settings_reset();
354 power_init();
356 set_irq_level(0);
357 #ifdef CPU_ARM
358 set_fiq_status(FIQ_ENABLED);
359 #endif
360 lcd_init();
361 #ifdef HAVE_REMOTE_LCD
362 lcd_remote_init();
363 #endif
364 font_init();
366 show_logo();
367 lang_init();
369 #ifdef DEBUG
370 debug_init();
371 #else
372 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
373 serial_setup();
374 #endif
375 #endif
377 i2c_init();
379 #if CONFIG_RTC
380 rtc_init();
381 #endif
382 #ifdef HAVE_RTC_RAM
383 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
384 #endif
386 adc_init();
388 usb_init();
389 #if CONFIG_USBOTG == USBOTG_ISP1362
390 isp1362_init();
391 #elif CONFIG_USBOTG == USBOTG_M5636
392 m5636_init();
393 #endif
395 backlight_init();
397 button_init();
399 powermgmt_init();
401 #if CONFIG_TUNER
402 radio_init();
403 #endif
405 /* Must be done before any code uses the multi-screen APi */
406 screen_access_init();
407 gui_syncstatusbar_init(&statusbars);
409 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
410 if (coldstart && charger_inserted()
411 && !global_settings.car_adapter_mode
412 #ifdef ATA_POWER_PLAYERSTYLE
413 && !ide_powered() /* relies on probing result from bootloader */
414 #endif
417 rc = charging_screen(); /* display a "charging" screen */
418 if (rc == 1) /* charger removed */
419 power_off();
420 /* "On" pressed or USB connected: proceed */
421 show_logo(); /* again, to provide better visual feedback */
423 #endif
425 ata_idle_notify_init();
426 rc = ata_init();
427 if(rc)
429 #ifdef HAVE_LCD_BITMAP
430 char str[32];
431 lcd_clear_display();
432 snprintf(str, 31, "ATA error: %d", rc);
433 lcd_puts(0, 1, str);
434 lcd_puts(0, 3, "Press ON to debug");
435 lcd_update();
436 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
437 dbg_ports();
438 #endif
439 panicf("ata: %d", rc);
442 #ifdef HAVE_EEPROM_SETTINGS
443 eeprom_settings_init();
444 #endif
446 usb_start_monitoring();
447 while (usb_detect() == USB_INSERTED)
449 #ifdef HAVE_EEPROM_SETTINGS
450 firmware_settings.disk_clean = false;
451 #endif
452 /* enter USB mode early, before trying to mount */
453 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
454 #ifdef HAVE_MMC
455 if (!mmc_touched() ||
456 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
457 #endif
459 usb_screen();
460 mounted = true; /* mounting done @ end of USB mode */
462 #ifdef HAVE_USB_POWER
463 if (usb_powered()) /* avoid deadlock */
464 break;
465 #endif
468 if (!mounted)
470 rc = disk_mount_all();
471 if (rc<=0)
473 lcd_clear_display();
474 lcd_puts(0, 0, "No partition");
475 lcd_puts(0, 1, "found.");
476 #ifdef HAVE_LCD_BITMAP
477 lcd_puts(0, 2, "Insert USB cable");
478 lcd_puts(0, 3, "and fix it.");
479 #endif
480 lcd_update();
482 while(button_get(true) != SYS_USB_CONNECTED) {};
483 usb_screen();
484 system_reboot();
488 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
489 (CONFIG_KEYPAD == IRIVER_H10_PAD)
490 #ifdef SETTINGS_RESET
491 /* Reset settings if holding the reset button. (Rec on Archos,
492 A on Gigabeat) */
493 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
494 #else
495 /* Reset settings if the hold button is turned on */
496 if (button_hold())
497 #endif
499 gui_syncsplash(HZ*2, str(LANG_RESET_DONE_CLEAR));
500 settings_reset();
502 else
503 #endif
504 settings_load(SETTINGS_ALL);
506 if (init_dircache(true) < 0)
508 #ifdef HAVE_TAGCACHE
509 remove(TAGCACHE_STATEFILE);
510 #endif
513 gui_sync_wps_init();
514 settings_apply();
515 init_dircache(false);
516 #ifdef HAVE_TAGCACHE
517 init_tagcache();
518 #endif
520 #ifdef HAVE_EEPROM_SETTINGS
521 if (firmware_settings.initialized)
523 /* In case we crash. */
524 firmware_settings.disk_clean = false;
525 eeprom_settings_store();
527 #endif
528 status_init();
529 playlist_init();
530 tree_mem_init();
531 filetype_init();
532 scrobbler_init();
533 cuesheet_init();
535 #if CONFIG_CODEC != SWCODEC
536 /* No buffer allocation (see buffer.c) may take place after the call to
537 audio_init() since the mpeg thread takes the rest of the buffer space */
538 mp3_init( global_settings.volume,
539 global_settings.bass,
540 global_settings.treble,
541 global_settings.balance,
542 global_settings.loudness,
543 global_settings.avc,
544 global_settings.channel_config,
545 global_settings.stereo_width,
546 global_settings.mdb_strength,
547 global_settings.mdb_harmonics,
548 global_settings.mdb_center,
549 global_settings.mdb_shape,
550 global_settings.mdb_enable,
551 global_settings.superbass);
553 /* audio_init must to know the size of voice buffer so init voice first */
554 talk_init();
555 #endif /* CONFIG_CODEC != SWCODEC */
557 audio_init();
559 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
560 pcm_rec_init();
561 #endif
563 /* runtime database has to be initialized after audio_init() */
564 cpu_boost(false);
566 #ifdef AUTOROCK
568 int fd;
569 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
571 fd = open(filename, O_RDONLY);
572 if(fd >= 0) /* no complaint if it doesn't exist */
574 close(fd);
575 plugin_load((char*)filename, NULL); /* start if it does */
578 #endif /* #ifdef AUTOROCK */
580 #if CONFIG_CHARGING
581 car_adapter_mode_init();
582 #endif
585 #ifdef CPU_PP
586 void cop_main(void)
588 /* This is the entry point for the coprocessor
589 Anyone not running an upgraded bootloader will never reach this point,
590 so it should not be assumed that the coprocessor be usable even on
591 platforms which support it.
593 A kernel thread is initially setup on the coprocessor and immediately
594 destroyed for purposes of continuity. The cop sits idle until at least
595 one thread exists on it. */
597 /* 3G doesn't have Rolo or dual core support yet */
598 #if NUM_CORES > 1
599 system_init();
600 kernel_init();
601 /* This should never be reached */
602 #endif
603 while(1) {
604 COP_CTL = PROC_SLEEP;
607 #endif /* CPU_PP */
609 int main(void)
611 app_main();
613 while(1) {
614 #if (CONFIG_LED == LED_REAL)
615 led(true); sleep(HZ/10);
616 led(false); sleep(HZ/10);
617 #endif
619 return 0;
621 #endif