1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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 */
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
54 #define USB_REENABLE 3
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)
72 static int usb_mmc_countdown
= 0;
75 /* FIXME: The extra 0x400 is consumed by fat_mount() when the fsinfo
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 */
90 mmc_select_clock(mmc_detect() ? 1 : 0);
92 or_b(0x20, &PADRL
); /* enable USB */
93 and_b(~0x08, &PADRL
); /* assert card detect */
97 and_b(~0x20, &PADRL
); /* disable USB */
98 or_b(0x08, &PADRL
); /* deassert card detect */
100 or_b(0x28, &PAIORL
); /* output for USB enable and card detect */
101 #else /* standard HD Jukebox */
102 #ifdef HAVE_LCD_BITMAP
103 if(read_hw_mask() & USB_ACTIVE_HIGH
)
108 and_b(~0x04, &PADRH
); /* enable USB */
118 static void usb_slave_mode(bool on
)
121 struct partinfo
* pinfo
;
125 DEBUGF("Entering USB slave mode\n");
135 DEBUGF("Leaving USB slave mode\n");
137 /* Let the ISDx00 settle */
147 snprintf(str
, 31, "ATA error: %d", rc
);
149 lcd_puts(0, 1, "Press ON to debug");
151 while(!(button_get(true) & BUTTON_REL
)) {};
153 panicf("ata: %d",rc
);
158 panicf("disk: NULL");
160 for ( i
=0; i
<4; i
++ ) {
161 rc
= fat_mount(pinfo
[i
].start
);
166 panicf("mount: %d",rc
);
170 static void usb_thread(void)
172 int num_acks_to_expect
= -1;
173 bool waiting_for_ack
;
176 waiting_for_ack
= false;
180 queue_wait(&usb_queue
, &ev
);
184 #ifdef HAVE_LCD_BITMAP
185 if(do_screendump_instead_of_usb
)
192 /* Tell all threads that they have to back off the ATA.
193 We subtract one for our own thread. */
195 queue_broadcast(SYS_USB_CONNECTED
, NULL
) - 1;
196 waiting_for_ack
= true;
197 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
199 #ifdef HAVE_LCD_BITMAP
204 case SYS_USB_CONNECTED_ACK
:
207 num_acks_to_expect
--;
208 if(num_acks_to_expect
== 0)
210 DEBUGF("All threads have acknowledged the connect.\n");
211 #ifdef USB_REALLY_BRAVE
212 usb_slave_mode(true);
213 usb_state
= USB_INSERTED
;
220 DEBUGF("usb: got ack, %d to go...\n",
227 #ifdef HAVE_LCD_BITMAP
228 if(!do_screendump_instead_of_usb
)
231 if(usb_state
== USB_INSERTED
)
233 /* Only disable the USB mode if we really have enabled it
234 some threads might not have acknowledged the
236 usb_slave_mode(false);
239 usb_state
= USB_EXTRACTED
;
241 /* Tell all threads that we are back in business */
243 queue_broadcast(SYS_USB_DISCONNECTED
, NULL
) - 1;
244 waiting_for_ack
= true;
245 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
247 #ifdef HAVE_LCD_CHARCELLS
248 lcd_icon(ICON_USB
, false);
250 #ifdef HAVE_LCD_BITMAP
255 case SYS_USB_DISCONNECTED_ACK
:
258 num_acks_to_expect
--;
259 if(num_acks_to_expect
== 0)
261 DEBUGF("All threads have acknowledged. "
262 "We're in business.\n");
266 DEBUGF("usb: got ack, %d to go...\n",
273 case SYS_MMC_INSERTED
:
274 case SYS_MMC_EXTRACTED
:
275 if(usb_state
== USB_INSERTED
)
278 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
283 if(usb_state
== USB_INSERTED
)
284 usb_enable(true); /* reenable only if still inserted */
291 bool usb_detect(void)
295 #ifdef USB_RECORDERSTYLE
296 current_status
= (adc_read(ADC_USB_POWER
) > 500)?true:false;
298 #ifdef USB_FMRECORDERSTYLE
299 current_status
= (adc_read(ADC_USB_POWER
) <= 512)?true:false;
301 #ifdef USB_PLAYERSTYLE
302 current_status
= (PADR
& 0x8000)?false:true;
305 current_status
= (GPIO1_READ
& 0x80)?true:false;
307 return current_status
;
311 static void usb_tick(void)
315 if(usb_monitor_enabled
)
317 current_status
= usb_detect();
319 /* Only report when the status has changed */
320 if(current_status
!= last_usb_status
)
322 last_usb_status
= current_status
;
323 countdown
= NUM_POLL_READINGS
;
327 /* Count down until it gets negative */
331 /* Report to the thread if we have had 3 identical status
336 queue_post(&usb_queue
, USB_INSERTED
, NULL
);
338 queue_post(&usb_queue
, USB_EXTRACTED
, NULL
);
343 if(usb_mmc_countdown
> 0)
346 if (usb_mmc_countdown
== 0)
347 queue_post(&usb_queue
, USB_REENABLE
, NULL
);
352 void usb_acknowledge(int id
)
354 queue_post(&usb_queue
, id
, NULL
);
359 usb_state
= USB_EXTRACTED
;
360 usb_monitor_enabled
= false;
364 GPIO1_FUNCTION
|= 0x80; /* GPIO39 is the USB detect input */
369 /* We assume that the USB cable is extracted */
370 last_usb_status
= false;
372 queue_init(&usb_queue
);
373 create_thread(usb_thread
, usb_stack
, sizeof(usb_stack
), usb_thread_name
);
375 tick_add_task(usb_tick
);
378 void usb_wait_for_disconnect(struct event_queue
*q
)
382 /* Don't return until we get SYS_USB_DISCONNECTED */
386 if(ev
.id
== SYS_USB_DISCONNECTED
)
388 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
394 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
398 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
401 queue_wait_w_tmo(q
, &ev
, ticks
);
404 case SYS_USB_DISCONNECTED
:
405 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
415 void usb_start_monitoring(void)
417 usb_monitor_enabled
= true;
420 bool usb_inserted(void)
422 return usb_state
== USB_INSERTED
;
428 bool usb_inserted(void)
434 /* Dummy simulator functions */
435 void usb_acknowledge(int id
)
444 void usb_start_monitoring(void)
448 bool usb_detect(void)
453 #endif /* USB_NONE or SIMULATOR */