Cleanup the C200 keymap a bit, fixes the time settings screen for now.
[Rockbox.git] / firmware / usb.c
blob876a5e4ec299f670d1fb6f727b4615a0e6af47d3
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 "usb.h"
36 #include "button.h"
37 #include "sprintf.h"
38 #include "string.h"
39 #include "usb-target.h"
40 #ifdef IRIVER_H300_SERIES
41 #include "pcf50606.h" /* for pcf50606_usb_charging_... */
42 #endif
43 #include "logf.h"
45 extern void dbg_ports(void); /* NASTY! defined in apps/ */
47 #ifdef HAVE_LCD_BITMAP
48 bool do_screendump_instead_of_usb = false;
49 void screen_dump(void); /* Nasty again. Defined in apps/ too */
50 #endif
52 #if !defined(SIMULATOR) && !defined(USB_NONE)
54 #define NUM_POLL_READINGS (HZ/5)
55 static int countdown;
57 static int usb_state;
59 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
60 static int usb_mmc_countdown = 0;
61 #endif
63 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
64 needs updating */
65 #ifndef BOOTLOADER
66 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
67 static const char usb_thread_name[] = "usb";
68 #endif
69 static struct event_queue usb_queue;
70 static int last_usb_status;
71 static bool usb_monitor_enabled;
74 #ifndef BOOTLOADER
75 static void usb_slave_mode(bool on)
77 int rc;
79 if(on)
81 DEBUGF("Entering USB slave mode\n");
82 ata_soft_reset();
83 ata_init();
84 ata_enable(false);
85 usb_enable(true);
87 else
89 DEBUGF("Leaving USB slave mode\n");
91 /* Let the ISDx00 settle */
92 sleep(HZ*1);
94 usb_enable(false);
96 rc = ata_init();
97 if(rc)
99 /* fixme: can we remove this? (already such in main.c) */
100 char str[32];
101 lcd_clear_display();
102 snprintf(str, 31, "ATA error: %d", rc);
103 lcd_puts(0, 0, str);
104 lcd_puts(0, 1, "Press ON to debug");
105 lcd_update();
106 while(!(button_get(true) & BUTTON_REL)) {};
107 dbg_ports();
108 panicf("ata: %d",rc);
111 rc = disk_mount_all();
112 if (rc <= 0) /* no partition */
113 panicf("mount: %d",rc);
118 static void usb_thread(void)
120 int num_acks_to_expect = -1;
121 bool waiting_for_ack;
122 struct event ev;
124 waiting_for_ack = false;
126 while(1)
128 queue_wait(&usb_queue, &ev);
129 switch(ev.id)
131 #ifdef HAVE_USB_POWER
132 case USB_POWERED:
133 usb_state = USB_POWERED;
134 break;
135 #endif
136 case USB_INSERTED:
137 #ifdef HAVE_LCD_BITMAP
138 if(do_screendump_instead_of_usb)
140 screen_dump();
142 else
143 #endif
144 #ifdef HAVE_USB_POWER
145 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
147 usb_state = USB_POWERED;
149 else
150 #endif
152 /* Tell all threads that they have to back off the ATA.
153 We subtract one for our own thread. */
154 num_acks_to_expect =
155 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
156 waiting_for_ack = true;
157 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
158 num_acks_to_expect);
160 break;
162 case SYS_USB_CONNECTED_ACK:
163 if(waiting_for_ack)
165 num_acks_to_expect--;
166 if(num_acks_to_expect == 0)
168 DEBUGF("All threads have acknowledged the connect.\n");
169 usb_slave_mode(true);
170 usb_state = USB_INSERTED;
171 cpu_idle_mode(true);
173 else
175 DEBUGF("usb: got ack, %d to go...\n",
176 num_acks_to_expect);
179 break;
181 case USB_EXTRACTED:
182 #ifdef HAVE_LCD_BITMAP
183 if(do_screendump_instead_of_usb)
184 break;
185 #endif
186 #ifdef HAVE_USB_POWER
187 if(usb_state == USB_POWERED)
189 usb_state = USB_EXTRACTED;
190 break;
192 #endif
193 if(usb_state == USB_INSERTED)
195 /* Only disable the USB mode if we really have enabled it
196 some threads might not have acknowledged the
197 insertion */
198 usb_slave_mode(false);
199 cpu_idle_mode(false);
202 usb_state = USB_EXTRACTED;
204 /* Tell all threads that we are back in business */
205 num_acks_to_expect =
206 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
207 waiting_for_ack = true;
208 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
209 num_acks_to_expect);
210 break;
212 case SYS_USB_DISCONNECTED_ACK:
213 if(waiting_for_ack)
215 num_acks_to_expect--;
216 if(num_acks_to_expect == 0)
218 DEBUGF("All threads have acknowledged. "
219 "We're in business.\n");
221 else
223 DEBUGF("usb: got ack, %d to go...\n",
224 num_acks_to_expect);
227 break;
229 #ifdef HAVE_MMC
230 case SYS_HOTSWAP_INSERTED:
231 case SYS_HOTSWAP_EXTRACTED:
232 if(usb_state == USB_INSERTED)
234 usb_enable(false);
235 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
237 break;
239 case USB_REENABLE:
240 if(usb_state == USB_INSERTED)
241 usb_enable(true); /* reenable only if still inserted */
242 break;
243 #endif
247 #endif
250 #ifndef BOOTLOADER
251 static void usb_tick(void)
253 int current_status;
255 if(usb_monitor_enabled)
257 current_status = usb_detect();
259 /* Only report when the status has changed */
260 if(current_status != last_usb_status)
262 last_usb_status = current_status;
263 countdown = NUM_POLL_READINGS;
265 else
267 /* Count down until it gets negative */
268 if(countdown >= 0)
269 countdown--;
271 /* Report to the thread if we have had 3 identical status
272 readings in a row */
273 if(countdown == 0)
275 queue_post(&usb_queue, current_status, 0);
279 #ifdef HAVE_MMC
280 if(usb_mmc_countdown > 0)
282 usb_mmc_countdown--;
283 if (usb_mmc_countdown == 0)
284 queue_post(&usb_queue, USB_REENABLE, 0);
286 #endif
288 #endif
290 void usb_acknowledge(long id)
292 queue_post(&usb_queue, id, 0);
295 void usb_init(void)
297 usb_state = USB_EXTRACTED;
298 usb_monitor_enabled = false;
299 countdown = -1;
301 usb_init_device();
302 usb_enable(false);
304 /* We assume that the USB cable is extracted */
305 last_usb_status = false;
307 #ifndef BOOTLOADER
308 queue_init(&usb_queue, true);
309 queue_set_irq_safe(&usb_queue, true);
311 create_thread(usb_thread, usb_stack, sizeof(usb_stack),
312 usb_thread_name IF_PRIO(, PRIORITY_SYSTEM)
313 IF_COP(, CPU, false));
315 tick_add_task(usb_tick);
316 #endif
320 void usb_wait_for_disconnect(struct event_queue *q)
322 struct event ev;
324 /* Don't return until we get SYS_USB_DISCONNECTED */
325 while(1)
327 queue_wait(q, &ev);
328 if(ev.id == SYS_USB_DISCONNECTED)
330 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
331 return;
336 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
338 struct event ev;
340 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
341 while(1)
343 queue_wait_w_tmo(q, &ev, ticks);
344 switch(ev.id)
346 case SYS_USB_DISCONNECTED:
347 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
348 return 0;
349 break;
350 case SYS_TIMEOUT:
351 return 1;
352 break;
357 void usb_start_monitoring(void)
359 usb_monitor_enabled = true;
362 bool usb_inserted(void)
364 #ifdef HAVE_USB_POWER
365 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
366 #else
367 return usb_state == USB_INSERTED;
368 #endif
371 #ifdef HAVE_USB_POWER
372 bool usb_powered(void)
374 return usb_state == USB_POWERED;
377 #if CONFIG_CHARGING
378 bool usb_charging_enable(bool on)
380 bool rc = false;
381 #ifdef IRIVER_H300_SERIES
382 int irqlevel;
383 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
384 irqlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
385 pcf50606_set_usb_charging(on);
386 rc = on;
387 (void)set_irq_level(irqlevel);
388 #else
389 /* TODO: implement it for other targets... */
390 (void)on;
391 #endif
392 return rc;
395 bool usb_charging_enabled(void)
397 bool rc = false;
398 #ifdef IRIVER_H300_SERIES
399 /* TODO: read the state of the GPOOD2 register...
400 * (this also means to set the irq level here) */
401 rc = pcf50606_usb_charging_enabled();
402 #else
403 /* TODO: implement it for other targets... */
404 #endif
406 logf("usb_charging_enabled: %s\n", rc ? "true" : "false" );
407 return rc;
409 #endif
410 #endif
412 #else
414 #ifdef USB_NONE
415 bool usb_inserted(void)
417 return false;
419 #endif
421 /* Dummy simulator functions */
422 void usb_acknowledge(long id)
424 id = id;
427 void usb_init(void)
431 void usb_start_monitoring(void)
435 int usb_detect(void)
437 return USB_EXTRACTED;
440 void usb_wait_for_disconnect(struct event_queue *q)
442 (void)q;
445 #endif /* USB_NONE or SIMULATOR */