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 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 */
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
)
112 and_b(~0x04, &PADRH
); /* enable USB */
122 static void usb_slave_mode(bool on
)
128 DEBUGF("Entering USB slave mode\n");
137 DEBUGF("Leaving USB slave mode\n");
139 /* Let the ISDx00 settle */
147 /* fixme: can we remove this? (already such in main.c) */
150 snprintf(str
, 31, "ATA error: %d", rc
);
152 lcd_puts(0, 1, "Press ON to debug");
154 while(!(button_get(true) & BUTTON_REL
)) {};
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
;
172 waiting_for_ack
= false;
176 queue_wait(&usb_queue
, &ev
);
180 #ifdef HAVE_LCD_BITMAP
181 if(do_screendump_instead_of_usb
)
188 /* Tell all threads that they have to back off the ATA.
189 We subtract one for our own thread. */
191 queue_broadcast(SYS_USB_CONNECTED
, NULL
) - 1;
192 waiting_for_ack
= true;
193 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
195 #ifdef HAVE_LCD_BITMAP
200 case SYS_USB_CONNECTED_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
;
216 DEBUGF("usb: got ack, %d to go...\n",
223 #ifdef HAVE_LCD_BITMAP
224 if(!do_screendump_instead_of_usb
)
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
232 usb_slave_mode(false);
235 usb_state
= USB_EXTRACTED
;
237 /* Tell all threads that we are back in business */
239 queue_broadcast(SYS_USB_DISCONNECTED
, NULL
) - 1;
240 waiting_for_ack
= true;
241 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
243 #ifdef HAVE_LCD_CHARCELLS
244 lcd_icon(ICON_USB
, false);
246 #ifdef HAVE_LCD_BITMAP
251 case SYS_USB_DISCONNECTED_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");
262 DEBUGF("usb: got ack, %d to go...\n",
269 case SYS_MMC_INSERTED
:
270 case SYS_MMC_EXTRACTED
:
271 if(usb_state
== USB_INSERTED
)
274 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
279 if(usb_state
== USB_INSERTED
)
280 usb_enable(true); /* reenable only if still inserted */
287 bool usb_detect(void)
291 #ifdef USB_RECORDERSTYLE
292 current_status
= (adc_read(ADC_USB_POWER
) > 500)?true:false;
294 #ifdef USB_FMRECORDERSTYLE
295 current_status
= (adc_read(ADC_USB_POWER
) <= 512)?true:false;
297 #ifdef USB_PLAYERSTYLE
298 current_status
= (PADR
& 0x8000)?false:true;
301 current_status
= (GPIO1_READ
& 0x80)?true:false;
303 return current_status
;
307 static void usb_tick(void)
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
;
323 /* Count down until it gets negative */
327 /* Report to the thread if we have had 3 identical status
332 queue_post(&usb_queue
, USB_INSERTED
, NULL
);
334 queue_post(&usb_queue
, USB_EXTRACTED
, NULL
);
339 if(usb_mmc_countdown
> 0)
342 if (usb_mmc_countdown
== 0)
343 queue_post(&usb_queue
, USB_REENABLE
, NULL
);
348 void usb_acknowledge(int id
)
350 queue_post(&usb_queue
, id
, NULL
);
355 usb_state
= USB_EXTRACTED
;
356 usb_monitor_enabled
= false;
360 GPIO1_FUNCTION
|= 0x80; /* GPIO39 is the USB detect input */
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
)
378 /* Don't return until we get SYS_USB_DISCONNECTED */
382 if(ev
.id
== SYS_USB_DISCONNECTED
)
384 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
390 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
394 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
397 queue_wait_w_tmo(q
, &ev
, ticks
);
400 case SYS_USB_DISCONNECTED
:
401 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
411 void usb_start_monitoring(void)
413 usb_monitor_enabled
= true;
416 bool usb_inserted(void)
418 return usb_state
== USB_INSERTED
;
424 bool usb_inserted(void)
430 /* Dummy simulator functions */
431 void usb_acknowledge(int id
)
440 void usb_start_monitoring(void)
444 bool usb_detect(void)
449 #endif /* USB_NONE or SIMULATOR */