Make it clear that the player has to be restarted after initializing the database
[Rockbox.git] / firmware / usb.c
blob651c17a4b3f9c0d7f1b763cf1e04e436c986cd9f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * iPod driver based on code from the ipodlinux project - http://ipodlinux.org
13 * Adapted for Rockbox in January 2006
14 * Original file: podzilla/usb.c
15 * Copyright (C) 2005 Adam Johnston
17 * All files in this archive are subject to the GNU General Public License.
18 * See the file COPYING in the source tree root for full license agreement.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "config.h"
25 #include "cpu.h"
26 #include "kernel.h"
27 #include "thread.h"
28 #include "system.h"
29 #include "debug.h"
30 #include "ata.h"
31 #include "fat.h"
32 #include "disk.h"
33 #include "panic.h"
34 #include "lcd.h"
35 #include "adc.h"
36 #include "usb.h"
37 #include "button.h"
38 #include "sprintf.h"
39 #include "string.h"
40 #include "hwcompat.h"
41 #ifdef HAVE_MMC
42 #include "ata_mmc.h"
43 #endif
44 #ifdef TARGET_TREE
45 #include "usb-target.h"
46 #endif
48 extern void dbg_ports(void); /* NASTY! defined in apps/ */
50 #ifdef HAVE_LCD_BITMAP
51 bool do_screendump_instead_of_usb = false;
52 void screen_dump(void); /* Nasty again. Defined in apps/ too */
53 #endif
55 #define USB_REALLY_BRAVE
57 #if !defined(SIMULATOR) && !defined(USB_NONE)
59 /* Messages from usb_tick and thread states */
60 #define USB_INSERTED 1
61 #define USB_EXTRACTED 2
62 #ifdef HAVE_MMC
63 #define USB_REENABLE 3
64 #endif
65 #ifdef HAVE_USB_POWER
66 #define USB_POWERED 4
68 #if CONFIG_KEYPAD == RECORDER_PAD
69 #define USBPOWER_BUTTON BUTTON_F1
70 #define USBPOWER_BTN_IGNORE BUTTON_ON
71 #elif CONFIG_KEYPAD == ONDIO_PAD
72 #define USBPOWER_BUTTON BUTTON_MENU
73 #define USBPOWER_BTN_IGNORE BUTTON_OFF
74 #elif (CONFIG_KEYPAD == IPOD_4G_PAD)
75 #define USBPOWER_BUTTON BUTTON_MENU
76 #define USBPOWER_BTN_IGNORE BUTTON_PLAY
77 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
78 #define USBPOWER_BUTTON BUTTON_REC
79 #define USBPOWER_BTN_IGNORE BUTTON_ON
80 #elif CONFIG_KEYPAD == GIGABEAT_PAD
81 #define USBPOWER_BUTTON BUTTON_MENU
82 #define USBPOWER_BTN_IGNORE BUTTON_POWER
83 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
84 #define USBPOWER_BUTTON BUTTON_NONE
85 #define USBPOWER_BTN_IGNORE BUTTON_POWER
86 #elif CONFIG_KEYPAD == SANSA_E200_PAD
87 #define USBPOWER_BUTTON BUTTON_SELECT
88 #define USBPOWER_BTN_IGNORE BUTTON_POWER
89 #endif
90 #endif /* HAVE_USB_POWER */
92 /* The ADC tick reads one channel per tick, and we want to check 3 successive
93 readings on the USB voltage channel. This doesn't apply to the Player, but
94 debouncing the USB detection port won't hurt us either. */
95 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
96 static int countdown;
98 static int usb_state;
100 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
101 static int usb_mmc_countdown = 0;
102 #endif
104 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
105 needs updating */
106 #ifndef BOOTLOADER
107 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
108 static const char usb_thread_name[] = "usb";
109 #endif
110 static struct event_queue usb_queue;
111 static bool last_usb_status;
112 static bool usb_monitor_enabled;
114 #ifndef TARGET_TREE
115 void usb_enable(bool on)
117 #ifdef USB_ENABLE_ONDIOSTYLE
118 PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
119 if(on)
121 #ifdef HAVE_MMC
122 mmc_enable_int_flash_clock(!mmc_detect());
123 #endif
124 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
125 and_b(~0x20, &PBDRH); /* old circuit needs SCK1 = low while on USB */
126 or_b(0x20, &PADRL); /* enable USB */
127 and_b(~0x08, &PADRL); /* assert card detect */
129 else
131 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
132 or_b(0x20, &PBDRH); /* reset SCK1 = high for old circuit */
133 and_b(~0x20, &PADRL); /* disable USB */
134 or_b(0x08, &PADRL); /* deassert card detect */
136 or_b(0x28, &PAIORL); /* output for USB enable and card detect */
137 #elif defined(USB_ISP1582)
138 /* TODO: Implement USB_ISP1582 */
139 (void) on;
140 #elif defined(USB_X5STYLE)
141 /* TODO X5 */
142 #elif defined(USB_GIGABEAT_STYLE)
143 /* TODO gigabeat */
144 #else
145 #ifdef HAVE_LCD_BITMAP
146 if(read_hw_mask() & USB_ACTIVE_HIGH)
147 on = !on;
148 #endif
149 if(on)
151 and_b(~0x04, &PADRH); /* enable USB */
153 else
155 or_b(0x04, &PADRH);
157 or_b(0x04, &PAIORH);
158 #endif
160 #endif
162 #ifndef BOOTLOADER
163 static void usb_slave_mode(bool on)
165 int rc;
167 if(on)
169 DEBUGF("Entering USB slave mode\n");
170 ata_soft_reset();
171 ata_init();
172 ata_enable(false);
173 usb_enable(true);
175 else
177 DEBUGF("Leaving USB slave mode\n");
179 /* Let the ISDx00 settle */
180 sleep(HZ*1);
182 usb_enable(false);
184 rc = ata_init();
185 if(rc)
187 /* fixme: can we remove this? (already such in main.c) */
188 char str[32];
189 lcd_clear_display();
190 snprintf(str, 31, "ATA error: %d", rc);
191 lcd_puts(0, 0, str);
192 lcd_puts(0, 1, "Press ON to debug");
193 lcd_update();
194 while(!(button_get(true) & BUTTON_REL)) {};
195 dbg_ports();
196 panicf("ata: %d",rc);
199 rc = disk_mount_all();
200 if (rc <= 0) /* no partition */
201 panicf("mount: %d",rc);
206 static void usb_thread(void)
208 int num_acks_to_expect = -1;
209 bool waiting_for_ack;
210 struct event ev;
212 waiting_for_ack = false;
214 while(1)
216 queue_wait(&usb_queue, &ev);
217 switch(ev.id)
219 case USB_INSERTED:
220 #ifdef HAVE_LCD_BITMAP
221 if(do_screendump_instead_of_usb)
223 screen_dump();
225 else
226 #endif
227 #ifdef HAVE_USB_POWER
228 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
230 usb_state = USB_POWERED;
232 else
233 #endif
235 /* Tell all threads that they have to back off the ATA.
236 We subtract one for our own thread. */
237 num_acks_to_expect =
238 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
239 waiting_for_ack = true;
240 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
241 num_acks_to_expect);
243 break;
245 case SYS_USB_CONNECTED_ACK:
246 if(waiting_for_ack)
248 num_acks_to_expect--;
249 if(num_acks_to_expect == 0)
251 DEBUGF("All threads have acknowledged the connect.\n");
252 #ifdef USB_REALLY_BRAVE
253 usb_slave_mode(true);
254 usb_state = USB_INSERTED;
255 cpu_idle_mode(true);
256 #else
257 system_reboot();
258 #endif
260 else
262 DEBUGF("usb: got ack, %d to go...\n",
263 num_acks_to_expect);
266 break;
268 case USB_EXTRACTED:
269 #ifdef HAVE_LCD_BITMAP
270 if(do_screendump_instead_of_usb)
271 break;
272 #endif
273 #ifdef HAVE_USB_POWER
274 if(usb_state == USB_POWERED)
276 usb_state = USB_EXTRACTED;
277 break;
279 #endif
280 if(usb_state == USB_INSERTED)
282 /* Only disable the USB mode if we really have enabled it
283 some threads might not have acknowledged the
284 insertion */
285 usb_slave_mode(false);
286 cpu_idle_mode(false);
289 usb_state = USB_EXTRACTED;
291 /* Tell all threads that we are back in business */
292 num_acks_to_expect =
293 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
294 waiting_for_ack = true;
295 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
296 num_acks_to_expect);
297 #ifdef HAVE_LCD_CHARCELLS
298 lcd_icon(ICON_USB, false);
299 #endif
300 break;
302 case SYS_USB_DISCONNECTED_ACK:
303 if(waiting_for_ack)
305 num_acks_to_expect--;
306 if(num_acks_to_expect == 0)
308 DEBUGF("All threads have acknowledged. "
309 "We're in business.\n");
311 else
313 DEBUGF("usb: got ack, %d to go...\n",
314 num_acks_to_expect);
317 break;
319 #ifdef HAVE_MMC
320 case SYS_MMC_INSERTED:
321 case SYS_MMC_EXTRACTED:
322 if(usb_state == USB_INSERTED)
324 usb_enable(false);
325 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
327 break;
329 case USB_REENABLE:
330 if(usb_state == USB_INSERTED)
331 usb_enable(true); /* reenable only if still inserted */
332 break;
333 #endif
337 #endif
339 #ifndef TARGET_TREE
340 bool usb_detect(void)
342 bool current_status;
344 #ifdef USB_RECORDERSTYLE
345 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
346 #endif
347 #ifdef USB_FMRECORDERSTYLE
348 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
349 #endif
350 #ifdef USB_PLAYERSTYLE
351 current_status = (PADR & 0x8000)?false:true;
352 #endif
353 #ifdef USB_ISP1582
354 /* TODO: Implement USB_ISP1582 */
355 current_status = false;
356 #endif
357 return current_status;
359 #endif
361 #ifndef BOOTLOADER
362 static void usb_tick(void)
364 bool current_status;
366 if(usb_monitor_enabled)
368 current_status = usb_detect();
370 /* Only report when the status has changed */
371 if(current_status != last_usb_status)
373 last_usb_status = current_status;
374 countdown = NUM_POLL_READINGS;
376 else
378 /* Count down until it gets negative */
379 if(countdown >= 0)
380 countdown--;
382 /* Report to the thread if we have had 3 identical status
383 readings in a row */
384 if(countdown == 0)
386 if(current_status)
387 queue_post(&usb_queue, USB_INSERTED, 0);
388 else
389 queue_post(&usb_queue, USB_EXTRACTED, 0);
393 #ifdef HAVE_MMC
394 if(usb_mmc_countdown > 0)
396 usb_mmc_countdown--;
397 if (usb_mmc_countdown == 0)
398 queue_post(&usb_queue, USB_REENABLE, 0);
400 #endif
402 #endif
404 void usb_acknowledge(long id)
406 queue_post(&usb_queue, id, 0);
409 void usb_init(void)
411 usb_state = USB_EXTRACTED;
412 usb_monitor_enabled = false;
413 countdown = -1;
415 #ifdef TARGET_TREE
416 usb_init_device();
417 #endif
419 usb_enable(false);
421 /* We assume that the USB cable is extracted */
422 last_usb_status = false;
424 #ifndef BOOTLOADER
425 queue_init(&usb_queue, true);
426 create_thread(usb_thread, usb_stack, sizeof(usb_stack),
427 usb_thread_name IF_PRIO(, PRIORITY_SYSTEM));
429 tick_add_task(usb_tick);
430 #endif
434 void usb_wait_for_disconnect(struct event_queue *q)
436 struct event ev;
438 /* Don't return until we get SYS_USB_DISCONNECTED */
439 while(1)
441 queue_wait(q, &ev);
442 if(ev.id == SYS_USB_DISCONNECTED)
444 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
445 return;
450 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
452 struct event ev;
454 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
455 while(1)
457 queue_wait_w_tmo(q, &ev, ticks);
458 switch(ev.id)
460 case SYS_USB_DISCONNECTED:
461 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
462 return 0;
463 break;
464 case SYS_TIMEOUT:
465 return 1;
466 break;
471 void usb_start_monitoring(void)
473 usb_monitor_enabled = true;
476 bool usb_inserted(void)
478 #ifdef HAVE_USB_POWER
479 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
480 #else
481 return usb_state == USB_INSERTED;
482 #endif
485 #ifdef HAVE_USB_POWER
486 bool usb_powered(void)
488 return usb_state == USB_POWERED;
490 #endif
492 #else
494 #ifdef USB_NONE
495 bool usb_inserted(void)
497 return false;
499 #endif
501 /* Dummy simulator functions */
502 void usb_acknowledge(long id)
504 id = id;
507 void usb_init(void)
511 void usb_start_monitoring(void)
515 bool usb_detect(void)
517 return false;
520 void usb_wait_for_disconnect(struct event_queue *q)
522 (void)q;
525 #endif /* USB_NONE or SIMULATOR */