First stab at porting rbutil to Qt4. Currently only installing a current or archived...
[Rockbox.git] / firmware / usb.c
blob9e9cb779191ad265a6b94e4da0b1789f31fd0207
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 /* Messages from usb_tick and thread states */
55 #define USB_INSERTED 1
56 #define USB_EXTRACTED 2
57 #ifdef HAVE_MMC
58 #define USB_REENABLE 3
59 #endif
60 #ifdef HAVE_USB_POWER
61 #define USB_POWERED 4
63 #if CONFIG_KEYPAD == RECORDER_PAD
64 #define USBPOWER_BUTTON BUTTON_F1
65 #define USBPOWER_BTN_IGNORE BUTTON_ON
66 #elif CONFIG_KEYPAD == ONDIO_PAD
67 #define USBPOWER_BUTTON BUTTON_MENU
68 #define USBPOWER_BTN_IGNORE BUTTON_OFF
69 #elif (CONFIG_KEYPAD == IPOD_4G_PAD)
70 #define USBPOWER_BUTTON BUTTON_MENU
71 #define USBPOWER_BTN_IGNORE BUTTON_PLAY
72 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
73 #define USBPOWER_BUTTON BUTTON_REC
74 #define USBPOWER_BTN_IGNORE BUTTON_ON
75 #elif CONFIG_KEYPAD == GIGABEAT_PAD
76 #define USBPOWER_BUTTON BUTTON_MENU
77 #define USBPOWER_BTN_IGNORE BUTTON_POWER
78 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
79 #define USBPOWER_BUTTON BUTTON_NONE
80 #define USBPOWER_BTN_IGNORE BUTTON_POWER
81 #elif CONFIG_KEYPAD == SANSA_E200_PAD
82 #define USBPOWER_BUTTON BUTTON_SELECT
83 #define USBPOWER_BTN_IGNORE BUTTON_POWER
84 #endif
85 #endif /* HAVE_USB_POWER */
87 #define NUM_POLL_READINGS (HZ/5)
88 static int countdown;
90 static int usb_state;
92 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
93 static int usb_mmc_countdown = 0;
94 #endif
96 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
97 needs updating */
98 #ifndef BOOTLOADER
99 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
100 static const char usb_thread_name[] = "usb";
101 #endif
102 static struct event_queue usb_queue;
103 static bool last_usb_status;
104 static bool usb_monitor_enabled;
107 #ifndef BOOTLOADER
108 static void usb_slave_mode(bool on)
110 int rc;
112 if(on)
114 DEBUGF("Entering USB slave mode\n");
115 ata_soft_reset();
116 ata_init();
117 ata_enable(false);
118 usb_enable(true);
120 else
122 DEBUGF("Leaving USB slave mode\n");
124 /* Let the ISDx00 settle */
125 sleep(HZ*1);
127 usb_enable(false);
129 rc = ata_init();
130 if(rc)
132 /* fixme: can we remove this? (already such in main.c) */
133 char str[32];
134 lcd_clear_display();
135 snprintf(str, 31, "ATA error: %d", rc);
136 lcd_puts(0, 0, str);
137 lcd_puts(0, 1, "Press ON to debug");
138 lcd_update();
139 while(!(button_get(true) & BUTTON_REL)) {};
140 dbg_ports();
141 panicf("ata: %d",rc);
144 rc = disk_mount_all();
145 if (rc <= 0) /* no partition */
146 panicf("mount: %d",rc);
151 static void usb_thread(void)
153 int num_acks_to_expect = -1;
154 bool waiting_for_ack;
155 struct event ev;
157 waiting_for_ack = false;
159 while(1)
161 queue_wait(&usb_queue, &ev);
162 switch(ev.id)
164 case USB_INSERTED:
165 #ifdef HAVE_LCD_BITMAP
166 if(do_screendump_instead_of_usb)
168 screen_dump();
170 else
171 #endif
172 #ifdef HAVE_USB_POWER
173 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
175 usb_state = USB_POWERED;
177 else
178 #endif
180 /* Tell all threads that they have to back off the ATA.
181 We subtract one for our own thread. */
182 num_acks_to_expect =
183 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
184 waiting_for_ack = true;
185 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
186 num_acks_to_expect);
188 break;
190 case SYS_USB_CONNECTED_ACK:
191 if(waiting_for_ack)
193 num_acks_to_expect--;
194 if(num_acks_to_expect == 0)
196 DEBUGF("All threads have acknowledged the connect.\n");
197 usb_slave_mode(true);
198 usb_state = USB_INSERTED;
199 cpu_idle_mode(true);
201 else
203 DEBUGF("usb: got ack, %d to go...\n",
204 num_acks_to_expect);
207 break;
209 case USB_EXTRACTED:
210 #ifdef HAVE_LCD_BITMAP
211 if(do_screendump_instead_of_usb)
212 break;
213 #endif
214 #ifdef HAVE_USB_POWER
215 if(usb_state == USB_POWERED)
217 usb_state = USB_EXTRACTED;
218 break;
220 #endif
221 if(usb_state == USB_INSERTED)
223 /* Only disable the USB mode if we really have enabled it
224 some threads might not have acknowledged the
225 insertion */
226 usb_slave_mode(false);
227 cpu_idle_mode(false);
230 usb_state = USB_EXTRACTED;
232 /* Tell all threads that we are back in business */
233 num_acks_to_expect =
234 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
235 waiting_for_ack = true;
236 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
237 num_acks_to_expect);
238 break;
240 case SYS_USB_DISCONNECTED_ACK:
241 if(waiting_for_ack)
243 num_acks_to_expect--;
244 if(num_acks_to_expect == 0)
246 DEBUGF("All threads have acknowledged. "
247 "We're in business.\n");
249 else
251 DEBUGF("usb: got ack, %d to go...\n",
252 num_acks_to_expect);
255 break;
257 #ifdef HAVE_MMC
258 case SYS_HOTSWAP_INSERTED:
259 case SYS_HOTSWAP_EXTRACTED:
260 if(usb_state == USB_INSERTED)
262 usb_enable(false);
263 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
265 break;
267 case USB_REENABLE:
268 if(usb_state == USB_INSERTED)
269 usb_enable(true); /* reenable only if still inserted */
270 break;
271 #endif
275 #endif
278 #ifndef BOOTLOADER
279 static void usb_tick(void)
281 bool current_status;
283 if(usb_monitor_enabled)
285 current_status = usb_detect();
287 /* Only report when the status has changed */
288 if(current_status != last_usb_status)
290 last_usb_status = current_status;
291 countdown = NUM_POLL_READINGS;
293 else
295 /* Count down until it gets negative */
296 if(countdown >= 0)
297 countdown--;
299 /* Report to the thread if we have had 3 identical status
300 readings in a row */
301 if(countdown == 0)
303 if(current_status)
304 queue_post(&usb_queue, USB_INSERTED, 0);
305 else
306 queue_post(&usb_queue, USB_EXTRACTED, 0);
310 #ifdef HAVE_MMC
311 if(usb_mmc_countdown > 0)
313 usb_mmc_countdown--;
314 if (usb_mmc_countdown == 0)
315 queue_post(&usb_queue, USB_REENABLE, 0);
317 #endif
319 #endif
321 void usb_acknowledge(long id)
323 queue_post(&usb_queue, id, 0);
326 void usb_init(void)
328 usb_state = USB_EXTRACTED;
329 usb_monitor_enabled = false;
330 countdown = -1;
332 usb_init_device();
333 usb_enable(false);
335 /* We assume that the USB cable is extracted */
336 last_usb_status = false;
338 #ifndef BOOTLOADER
339 queue_init(&usb_queue, true);
340 queue_set_irq_safe(&usb_queue, true);
342 create_thread(usb_thread, usb_stack, sizeof(usb_stack),
343 usb_thread_name IF_PRIO(, PRIORITY_SYSTEM)
344 IF_COP(, CPU, false));
346 tick_add_task(usb_tick);
347 #endif
351 void usb_wait_for_disconnect(struct event_queue *q)
353 struct event ev;
355 /* Don't return until we get SYS_USB_DISCONNECTED */
356 while(1)
358 queue_wait(q, &ev);
359 if(ev.id == SYS_USB_DISCONNECTED)
361 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
362 return;
367 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
369 struct event ev;
371 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
372 while(1)
374 queue_wait_w_tmo(q, &ev, ticks);
375 switch(ev.id)
377 case SYS_USB_DISCONNECTED:
378 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
379 return 0;
380 break;
381 case SYS_TIMEOUT:
382 return 1;
383 break;
388 void usb_start_monitoring(void)
390 usb_monitor_enabled = true;
393 bool usb_inserted(void)
395 #ifdef HAVE_USB_POWER
396 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
397 #else
398 return usb_state == USB_INSERTED;
399 #endif
402 #ifdef HAVE_USB_POWER
403 bool usb_powered(void)
405 return usb_state == USB_POWERED;
408 #if CONFIG_CHARGING
409 bool usb_charging_enable(bool on)
411 bool rc = false;
412 #ifdef IRIVER_H300_SERIES
413 int irqlevel;
414 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
415 irqlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
416 pcf50606_set_usb_charging(on);
417 rc = on;
418 (void)set_irq_level(irqlevel);
419 #else
420 /* TODO: implement it for other targets... */
421 (void)on;
422 #endif
423 return rc;
426 bool usb_charging_enabled(void)
428 bool rc = false;
429 #ifdef IRIVER_H300_SERIES
430 /* TODO: read the state of the GPOOD2 register...
431 * (this also means to set the irq level here) */
432 rc = pcf50606_usb_charging_enabled();
433 #else
434 /* TODO: implement it for other targets... */
435 #endif
437 logf("usb_charging_enabled: %s\n", rc ? "true" : "false" );
438 return rc;
440 #endif
441 #endif
443 #else
445 #ifdef USB_NONE
446 bool usb_inserted(void)
448 return false;
450 #endif
452 /* Dummy simulator functions */
453 void usb_acknowledge(long id)
455 id = id;
458 void usb_init(void)
462 void usb_start_monitoring(void)
466 bool usb_detect(void)
468 return false;
471 void usb_wait_for_disconnect(struct event_queue *q)
473 (void)q;
476 #endif /* USB_NONE or SIMULATOR */