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 ****************************************************************************/
36 extern void dbg_ports(void); /* NASTY! defined in apps/ */
38 #ifdef HAVE_LCD_BITMAP
39 bool do_screendump_instead_of_usb
= false;
40 void screen_dump(void); /* Nasty again. Defined in apps/ too */
43 #define USB_REALLY_BRAVE
45 #if !defined(SIMULATOR) && !defined(USB_NONE)
47 /* Messages from usb_tick */
48 #define USB_INSERTED 1
49 #define USB_EXTRACTED 2
57 /* The ADC tick reads one channel per tick, and we want to check 3 successive
58 readings on the USB voltage channel. This doesn't apply to the Player, but
59 debouncing the USB detection port won't hurt us either. */
60 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
65 /* FIXME: The extra 0x400 is consumed by fat_mount() when the fsinfo
67 static char usb_stack
[DEFAULT_STACK_SIZE
+ 0x400];
68 static const char usb_thread_name
[] = "usb";
69 static struct event_queue usb_queue
;
70 static bool last_usb_status
;
71 static bool usb_monitor_enabled
;
73 static void usb_enable(bool on
)
75 #ifdef USB_ENABLE_ONDIOSTYLE
78 or_b(0x20, &PADRL
); /* enable USB */
79 and_b(~0x08, &PADRL
); /* assert card detect */
83 and_b(~0x20, &PADRL
); /* disable USB */
84 or_b(0x08, &PADRL
); /* deassert card detect */
86 PACR2
&= ~0x00C0; /* use PA3 as GPIO */
87 or_b(0x28, &PAIORL
); /* output for USB enable and card detect */
88 #else /* standard HD Jukebox */
89 #ifdef HAVE_LCD_BITMAP
90 if(read_hw_mask() & USB_ACTIVE_HIGH
)
95 and_b(~0x04, &PADRH
); /* enable USB */
105 static void usb_slave_mode(bool on
)
108 struct partinfo
* pinfo
;
112 DEBUGF("Entering USB slave mode\n");
122 DEBUGF("Leaving USB slave mode\n");
124 /* Let the ISDx00 settle */
134 snprintf(str
, 31, "ATA error: %d", rc
);
136 lcd_puts(0, 1, "Press ON to debug");
138 while(!(button_get(true) & BUTTON_REL
)) {};
140 panicf("ata: %d",rc
);
145 panicf("disk: NULL");
147 for ( i
=0; i
<4; i
++ ) {
148 rc
= fat_mount(pinfo
[i
].start
);
153 panicf("mount: %d",rc
);
157 static void usb_thread(void)
159 int num_acks_to_expect
= -1;
160 bool waiting_for_ack
;
163 waiting_for_ack
= false;
167 queue_wait(&usb_queue
, &ev
);
171 #ifdef HAVE_LCD_BITMAP
172 if(do_screendump_instead_of_usb
)
179 /* Tell all threads that they have to back off the ATA.
180 We subtract one for our own thread. */
182 queue_broadcast(SYS_USB_CONNECTED
, NULL
) - 1;
183 waiting_for_ack
= true;
184 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
186 #ifdef HAVE_LCD_BITMAP
191 case SYS_USB_CONNECTED_ACK
:
194 num_acks_to_expect
--;
195 if(num_acks_to_expect
== 0)
197 DEBUGF("All threads have acknowledged the connect.\n");
198 #ifdef USB_REALLY_BRAVE
199 usb_slave_mode(true);
200 usb_state
= USB_INSERTED
;
207 DEBUGF("usb: got ack, %d to go...\n",
214 #ifdef HAVE_LCD_BITMAP
215 if(!do_screendump_instead_of_usb
)
218 if(usb_state
== USB_INSERTED
)
220 /* Only disable the USB mode if we really have enabled it
221 some threads might not have acknowledged the
223 usb_slave_mode(false);
226 usb_state
= USB_EXTRACTED
;
228 /* Tell all threads that we are back in business */
230 queue_broadcast(SYS_USB_DISCONNECTED
, NULL
) - 1;
231 waiting_for_ack
= true;
232 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
234 #ifdef HAVE_LCD_CHARCELLS
235 lcd_icon(ICON_USB
, false);
237 #ifdef HAVE_LCD_BITMAP
242 case SYS_USB_DISCONNECTED_ACK
:
245 num_acks_to_expect
--;
246 if(num_acks_to_expect
== 0)
248 DEBUGF("All threads have acknowledged. "
249 "We're in business.\n");
253 DEBUGF("usb: got ack, %d to go...\n",
262 bool usb_detect(void)
266 #ifdef USB_RECORDERSTYLE
267 current_status
= (adc_read(ADC_USB_POWER
) > 500)?true:false;
269 #ifdef USB_FMRECORDERSTYLE
270 current_status
= (adc_read(ADC_USB_POWER
) <= 512)?true:false;
272 #ifdef USB_PLAYERSTYLE
273 current_status
= (PADR
& 0x8000)?false:true;
276 return current_status
;
280 static void usb_tick(void)
284 if(usb_monitor_enabled
)
286 current_status
= usb_detect();
288 /* Only report when the status has changed */
289 if(current_status
!= last_usb_status
)
291 last_usb_status
= current_status
;
292 countdown
= NUM_POLL_READINGS
;
296 /* Count down until it gets negative */
300 /* Report to the thread if we have had 3 identical status
305 queue_post(&usb_queue
, USB_INSERTED
, NULL
);
307 queue_post(&usb_queue
, USB_EXTRACTED
, NULL
);
313 void usb_acknowledge(int id
)
315 queue_post(&usb_queue
, id
, NULL
);
320 usb_state
= USB_EXTRACTED
;
321 usb_monitor_enabled
= false;
326 /* We assume that the USB cable is extracted */
327 last_usb_status
= false;
329 queue_init(&usb_queue
);
330 create_thread(usb_thread
, usb_stack
, sizeof(usb_stack
), usb_thread_name
);
332 tick_add_task(usb_tick
);
335 void usb_wait_for_disconnect(struct event_queue
*q
)
339 /* Don't return until we get SYS_USB_DISCONNECTED */
343 if(ev
.id
== SYS_USB_DISCONNECTED
)
345 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
351 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
355 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
358 queue_wait_w_tmo(q
, &ev
, ticks
);
361 case SYS_USB_DISCONNECTED
:
362 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
372 void usb_start_monitoring(void)
374 usb_monitor_enabled
= true;
377 bool usb_inserted(void)
379 return usb_state
== USB_INSERTED
;
385 bool usb_inserted(void)
391 /* Dummy simulator functions */
392 void usb_acknowledge(int id
)
401 void usb_start_monitoring(void)
405 bool usb_detect(void)
410 #endif /* USB_NONE or SIMULATOR */