The RockboxUtilityQt wiki page has been renamed to RockboxUtility
[Rockbox.git] / firmware / usb.c
blob75ee4759ca72f1493a8c6a278971e7d49686cb36
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 #ifndef HAVE_USBSTACK
92 /* Let the ISDx00 settle */
93 sleep(HZ*1);
94 #endif
96 usb_enable(false);
98 rc = ata_init();
99 if(rc)
101 /* fixme: can we remove this? (already such in main.c) */
102 char str[32];
103 lcd_clear_display();
104 snprintf(str, 31, "ATA error: %d", rc);
105 lcd_puts(0, 0, str);
106 lcd_puts(0, 1, "Press ON to debug");
107 lcd_update();
108 while(!(button_get(true) & BUTTON_REL)) {};
109 dbg_ports();
110 panicf("ata: %d",rc);
113 rc = disk_mount_all();
114 if (rc <= 0) /* no partition */
115 panicf("mount: %d",rc);
120 static void usb_thread(void)
122 int num_acks_to_expect = -1;
123 bool waiting_for_ack;
124 struct queue_event ev;
126 waiting_for_ack = false;
128 while(1)
130 queue_wait(&usb_queue, &ev);
131 switch(ev.id)
133 #ifdef HAVE_USB_POWER
134 case USB_POWERED:
135 usb_state = USB_POWERED;
136 break;
137 #endif
138 case USB_INSERTED:
139 #ifdef HAVE_LCD_BITMAP
140 if(do_screendump_instead_of_usb)
142 screen_dump();
144 else
145 #endif
146 #ifdef HAVE_USB_POWER
147 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
149 usb_state = USB_POWERED;
151 else
152 #endif
154 /* Tell all threads that they have to back off the ATA.
155 We subtract one for our own thread. */
156 num_acks_to_expect =
157 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
158 waiting_for_ack = true;
159 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
160 num_acks_to_expect);
162 break;
164 case SYS_USB_CONNECTED_ACK:
165 if(waiting_for_ack)
167 num_acks_to_expect--;
168 if(num_acks_to_expect == 0)
170 DEBUGF("All threads have acknowledged the connect.\n");
171 usb_slave_mode(true);
172 usb_state = USB_INSERTED;
173 cpu_idle_mode(true);
175 else
177 DEBUGF("usb: got ack, %d to go...\n",
178 num_acks_to_expect);
181 break;
183 case USB_EXTRACTED:
184 #ifdef HAVE_LCD_BITMAP
185 if(do_screendump_instead_of_usb)
186 break;
187 #endif
188 #ifdef HAVE_USB_POWER
189 if(usb_state == USB_POWERED)
191 usb_state = USB_EXTRACTED;
192 break;
194 #endif
195 if(usb_state == USB_INSERTED)
197 /* Only disable the USB mode if we really have enabled it
198 some threads might not have acknowledged the
199 insertion */
200 usb_slave_mode(false);
201 cpu_idle_mode(false);
204 usb_state = USB_EXTRACTED;
206 /* Tell all threads that we are back in business */
207 num_acks_to_expect =
208 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
209 waiting_for_ack = true;
210 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
211 num_acks_to_expect);
212 break;
214 case SYS_USB_DISCONNECTED_ACK:
215 if(waiting_for_ack)
217 num_acks_to_expect--;
218 if(num_acks_to_expect == 0)
220 DEBUGF("All threads have acknowledged. "
221 "We're in business.\n");
223 else
225 DEBUGF("usb: got ack, %d to go...\n",
226 num_acks_to_expect);
229 break;
231 #ifdef HAVE_MMC
232 case SYS_HOTSWAP_INSERTED:
233 case SYS_HOTSWAP_EXTRACTED:
234 if(usb_state == USB_INSERTED)
236 usb_enable(false);
237 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
239 break;
241 case USB_REENABLE:
242 if(usb_state == USB_INSERTED)
243 usb_enable(true); /* reenable only if still inserted */
244 break;
245 #endif
249 #endif
252 #ifndef BOOTLOADER
253 static void usb_tick(void)
255 int current_status;
257 if(usb_monitor_enabled)
259 current_status = usb_detect();
261 /* Only report when the status has changed */
262 if(current_status != last_usb_status)
264 last_usb_status = current_status;
265 countdown = NUM_POLL_READINGS;
267 else
269 /* Count down until it gets negative */
270 if(countdown >= 0)
271 countdown--;
273 /* Report to the thread if we have had 3 identical status
274 readings in a row */
275 if(countdown == 0)
277 queue_post(&usb_queue, current_status, 0);
281 #ifdef HAVE_MMC
282 if(usb_mmc_countdown > 0)
284 usb_mmc_countdown--;
285 if (usb_mmc_countdown == 0)
286 queue_post(&usb_queue, USB_REENABLE, 0);
288 #endif
290 #endif
292 void usb_acknowledge(long id)
294 queue_post(&usb_queue, id, 0);
297 void usb_init(void)
299 usb_state = USB_EXTRACTED;
300 usb_monitor_enabled = false;
301 countdown = -1;
303 usb_init_device();
304 usb_enable(false);
306 /* We assume that the USB cable is extracted */
307 last_usb_status = false;
309 #ifndef BOOTLOADER
310 queue_init(&usb_queue, true);
312 create_thread(usb_thread, usb_stack, sizeof(usb_stack), 0,
313 usb_thread_name IF_PRIO(, PRIORITY_SYSTEM)
314 IF_COP(, CPU));
316 tick_add_task(usb_tick);
317 #endif
321 void usb_wait_for_disconnect(struct event_queue *q)
323 struct queue_event ev;
325 /* Don't return until we get SYS_USB_DISCONNECTED */
326 while(1)
328 queue_wait(q, &ev);
329 if(ev.id == SYS_USB_DISCONNECTED)
331 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
332 return;
337 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
339 struct queue_event ev;
341 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
342 while(1)
344 queue_wait_w_tmo(q, &ev, ticks);
345 switch(ev.id)
347 case SYS_USB_DISCONNECTED:
348 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
349 return 0;
350 break;
351 case SYS_TIMEOUT:
352 return 1;
353 break;
358 void usb_start_monitoring(void)
360 usb_monitor_enabled = true;
363 bool usb_inserted(void)
365 #ifdef HAVE_USB_POWER
366 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
367 #else
368 return usb_state == USB_INSERTED;
369 #endif
372 #ifdef HAVE_USB_POWER
373 bool usb_powered(void)
375 return usb_state == USB_POWERED;
378 #if CONFIG_CHARGING
379 bool usb_charging_enable(bool on)
381 bool rc = false;
382 #ifdef IRIVER_H300_SERIES
383 int irqlevel;
384 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
385 irqlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
386 pcf50606_set_usb_charging(on);
387 rc = on;
388 (void)set_irq_level(irqlevel);
389 #else
390 /* TODO: implement it for other targets... */
391 (void)on;
392 #endif
393 return rc;
396 bool usb_charging_enabled(void)
398 bool rc = false;
399 #ifdef IRIVER_H300_SERIES
400 /* TODO: read the state of the GPOOD2 register...
401 * (this also means to set the irq level here) */
402 rc = pcf50606_usb_charging_enabled();
403 #else
404 /* TODO: implement it for other targets... */
405 #endif
407 logf("usb charging %s", rc ? "enabled" : "disabled" );
408 return rc;
410 #endif
411 #endif
413 #else
415 #ifdef USB_NONE
416 bool usb_inserted(void)
418 return false;
420 #endif
422 /* Dummy simulator functions */
423 void usb_acknowledge(long id)
425 id = id;
428 void usb_init(void)
432 void usb_start_monitoring(void)
436 int usb_detect(void)
438 return USB_EXTRACTED;
441 void usb_wait_for_disconnect(struct event_queue *q)
443 (void)q;
446 #endif /* USB_NONE or SIMULATOR */