New weird syntax
[kugel-rb.git] / firmware / usb.c
blob181b1ead1a8ae4608e0b9a9154d7741e0f7870e5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
19 #include "config.h"
20 #include "cpu.h"
21 #include "kernel.h"
22 #include "thread.h"
23 #include "system.h"
24 #include "debug.h"
25 #include "ata.h"
26 #include "fat.h"
27 #include "disk.h"
28 #include "panic.h"
29 #include "lcd.h"
30 #include "adc.h"
31 #include "usb.h"
32 #include "button.h"
33 #include "sprintf.h"
34 #include "hwcompat.h"
35 #ifdef HAVE_MMC
36 #include "ata_mmc.h"
37 #endif
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 */
44 #endif
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
53 #ifdef HAVE_MMC
54 #define USB_REENABLE 3
55 #endif
57 /* Thread states */
58 #define EXTRACTING 1
59 #define EXTRACTED 2
60 #define INSERTED 3
61 #define INSERTING 4
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)
67 static int countdown;
69 static int usb_state;
71 #ifdef HAVE_MMC
72 static int usb_mmc_countdown = 0;
73 #endif
75 /* FIXME: The extra 0x400 is consumed by fat_mount() when the fsinfo
76 needs updating */
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 */
87 if(on)
89 #ifdef HAVE_MMC
90 mmc_select_clock(mmc_detect() ? 1 : 0);
91 #endif
92 or_b(0x20, &PADRL); /* enable USB */
93 and_b(~0x08, &PADRL); /* assert card detect */
95 else
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)
104 on = !on;
105 #endif
106 if(on)
108 and_b(~0x04, &PADRH); /* enable USB */
110 else
112 or_b(0x04, &PADRH);
114 or_b(0x04, &PAIORH);
115 #endif
118 static void usb_slave_mode(bool on)
120 int rc;
121 struct partinfo* pinfo;
123 if(on)
125 DEBUGF("Entering USB slave mode\n");
126 ata_soft_reset();
127 ata_init();
128 ata_standby(15);
129 ata_enable(false);
130 usb_enable(true);
132 else
134 int i;
135 DEBUGF("Leaving USB slave mode\n");
137 /* Let the ISDx00 settle */
138 sleep(HZ*1);
140 usb_enable(false);
142 rc = ata_init();
143 if(rc)
145 char str[32];
146 lcd_clear_display();
147 snprintf(str, 31, "ATA error: %d", rc);
148 lcd_puts(0, 0, str);
149 lcd_puts(0, 1, "Press ON to debug");
150 lcd_update();
151 while(!(button_get(true) & BUTTON_REL)) {};
152 dbg_ports();
153 panicf("ata: %d",rc);
156 pinfo = disk_init();
157 if (!pinfo)
158 panicf("disk: NULL");
160 for ( i=0; i<4; i++ ) {
161 rc = fat_mount(pinfo[i].start);
162 if (!rc)
163 break;
165 if (i==4)
166 panicf("mount: %d",rc);
170 static void usb_thread(void)
172 int num_acks_to_expect = -1;
173 bool waiting_for_ack;
174 struct event ev;
176 waiting_for_ack = false;
178 while(1)
180 queue_wait(&usb_queue, &ev);
181 switch(ev.id)
183 case USB_INSERTED:
184 #ifdef HAVE_LCD_BITMAP
185 if(do_screendump_instead_of_usb)
187 screen_dump();
189 else
191 #endif
192 /* Tell all threads that they have to back off the ATA.
193 We subtract one for our own thread. */
194 num_acks_to_expect =
195 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
196 waiting_for_ack = true;
197 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
198 num_acks_to_expect);
199 #ifdef HAVE_LCD_BITMAP
201 #endif
202 break;
204 case SYS_USB_CONNECTED_ACK:
205 if(waiting_for_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;
214 #else
215 system_reboot();
216 #endif
218 else
220 DEBUGF("usb: got ack, %d to go...\n",
221 num_acks_to_expect);
224 break;
226 case USB_EXTRACTED:
227 #ifdef HAVE_LCD_BITMAP
228 if(!do_screendump_instead_of_usb)
230 #endif
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
235 insertion */
236 usb_slave_mode(false);
239 usb_state = USB_EXTRACTED;
241 /* Tell all threads that we are back in business */
242 num_acks_to_expect =
243 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
244 waiting_for_ack = true;
245 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
246 num_acks_to_expect);
247 #ifdef HAVE_LCD_CHARCELLS
248 lcd_icon(ICON_USB, false);
249 #endif
250 #ifdef HAVE_LCD_BITMAP
252 #endif
253 break;
255 case SYS_USB_DISCONNECTED_ACK:
256 if(waiting_for_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");
264 else
266 DEBUGF("usb: got ack, %d to go...\n",
267 num_acks_to_expect);
270 break;
272 #ifdef HAVE_MMC
273 case SYS_MMC_INSERTED:
274 case SYS_MMC_EXTRACTED:
275 if(usb_state == USB_INSERTED)
277 usb_enable(false);
278 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
280 break;
282 case USB_REENABLE:
283 if(usb_state == USB_INSERTED)
284 usb_enable(true); /* reenable only if still inserted */
285 break;
286 #endif
291 bool usb_detect(void)
293 bool current_status;
295 #ifdef USB_RECORDERSTYLE
296 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
297 #endif
298 #ifdef USB_FMRECORDERSTYLE
299 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
300 #endif
301 #ifdef USB_PLAYERSTYLE
302 current_status = (PADR & 0x8000)?false:true;
303 #endif
304 #ifdef IRIVER_H100
305 current_status = (GPIO1_READ & 0x80)?true:false;
306 #endif
307 return current_status;
311 static void usb_tick(void)
313 bool current_status;
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;
325 else
327 /* Count down until it gets negative */
328 if(countdown >= 0)
329 countdown--;
331 /* Report to the thread if we have had 3 identical status
332 readings in a row */
333 if(countdown == 0)
335 if(current_status)
336 queue_post(&usb_queue, USB_INSERTED, NULL);
337 else
338 queue_post(&usb_queue, USB_EXTRACTED, NULL);
342 #ifdef HAVE_MMC
343 if(usb_mmc_countdown > 0)
345 usb_mmc_countdown--;
346 if (usb_mmc_countdown == 0)
347 queue_post(&usb_queue, USB_REENABLE, NULL);
349 #endif
352 void usb_acknowledge(int id)
354 queue_post(&usb_queue, id, NULL);
357 void usb_init(void)
359 usb_state = USB_EXTRACTED;
360 usb_monitor_enabled = false;
361 countdown = -1;
363 #ifdef IRIVER_H100
364 GPIO1_FUNCTION |= 0x80; /* GPIO39 is the USB detect input */
365 #endif
367 usb_enable(false);
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)
380 struct event ev;
382 /* Don't return until we get SYS_USB_DISCONNECTED */
383 while(1)
385 queue_wait(q, &ev);
386 if(ev.id == SYS_USB_DISCONNECTED)
388 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
389 return;
394 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
396 struct event ev;
398 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
399 while(1)
401 queue_wait_w_tmo(q, &ev, ticks);
402 switch(ev.id)
404 case SYS_USB_DISCONNECTED:
405 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
406 return 0;
407 break;
408 case SYS_TIMEOUT:
409 return 1;
410 break;
415 void usb_start_monitoring(void)
417 usb_monitor_enabled = true;
420 bool usb_inserted(void)
422 return usb_state == USB_INSERTED;
425 #else
427 #ifdef USB_NONE
428 bool usb_inserted(void)
430 return false;
432 #endif
434 /* Dummy simulator functions */
435 void usb_acknowledge(int id)
437 id = id;
440 void usb_init(void)
444 void usb_start_monitoring(void)
448 bool usb_detect(void)
450 return false;
453 #endif /* USB_NONE or SIMULATOR */