rbutil: include the qt accessible plugin into the mac bundle. (Enables screenreader...
[kugel-rb.git] / firmware / usb.c
blobab4f0403b9d6e557a7663076b01f368086a27b09
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 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
26 #include "config.h"
27 #include "cpu.h"
28 #include "kernel.h"
29 #include "thread.h"
30 #include "system.h"
31 #include "debug.h"
32 #include "ata.h"
33 #include "fat.h"
34 #include "disk.h"
35 #include "panic.h"
36 #include "lcd.h"
37 #include "usb.h"
38 #include "button.h"
39 #include "sprintf.h"
40 #include "string.h"
41 #include "usb-target.h"
42 #ifdef HAVE_USBSTACK
43 #include "usb_core.h"
44 #endif
45 #ifdef IRIVER_H300_SERIES
46 #include "pcf50606.h" /* for pcf50606_usb_charging_... */
47 #endif
48 #include "logf.h"
50 /* Conditions under which we want the entire driver */
51 #if !defined(BOOTLOADER) || (CONFIG_CPU == SH7034) || \
52 (defined(TOSHIBA_GIGABEAT_S) && defined(USE_ROCKBOX_USB) && defined(USB_STORAGE)) || \
53 (defined(CREATIVE_ZVx) && defined(HAVE_USBSTACK))
54 #define USB_FULL_INIT
55 #endif
57 #ifdef HAVE_LCD_BITMAP
58 bool do_screendump_instead_of_usb = false;
59 #if defined(USB_FULL_INIT) && defined(BOOTLOADER)
60 static void screen_dump(void) {}
61 #else
62 void screen_dump(void); /* Nasty again. Defined in apps/ too */
63 #endif
64 #endif
66 #if !defined(SIMULATOR) && !defined(USB_NONE)
68 #define NUM_POLL_READINGS (HZ/5)
69 static int countdown;
71 static int usb_state;
73 #if defined(HAVE_MMC) && defined(USB_FULL_INIT)
74 static int usb_mmc_countdown = 0;
75 #endif
77 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
78 needs updating */
79 #ifdef USB_FULL_INIT
80 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
81 static const char usb_thread_name[] = "usb";
82 static struct thread_entry *usb_thread_entry;
83 #endif
84 static struct event_queue usb_queue;
85 static int last_usb_status;
86 static bool usb_monitor_enabled;
87 #ifdef HAVE_USBSTACK
88 static bool exclusive_ata_access;
89 #endif
92 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
93 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
94 static int firewire_countdown;
95 static bool last_firewire_status;
96 #endif
98 #ifdef USB_FULL_INIT
99 #ifndef HAVE_USBSTACK
100 static void usb_slave_mode(bool on)
102 int rc;
104 if(on)
106 DEBUGF("Entering USB slave mode\n");
107 ata_soft_reset();
108 ata_init();
109 ata_enable(false);
110 usb_enable(true);
112 else
114 DEBUGF("Leaving USB slave mode\n");
116 /* Let the ISDx00 settle */
117 sleep(HZ*1);
119 usb_enable(false);
121 rc = ata_init();
122 if(rc)
123 panicf("ata: %d",rc);
125 rc = disk_mount_all();
126 if (rc <= 0) /* no partition */
127 panicf("mount: %d",rc);
131 #endif
133 static void try_reboot(void)
135 #ifdef HAVE_DISK_STORAGE
136 ata_sleepnow(); /* Immediately spindown the disk. */
137 sleep(HZ*2);
138 #endif
140 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
141 #if CONFIG_CPU == PP5020
142 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
143 #elif CONFIG_CPU == PP5022
144 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
145 #endif /* CONFIG_CPU */
146 #endif /* IPOD_ARCH */
148 system_reboot(); /* Reboot */
151 static void usb_thread(void)
153 int num_acks_to_expect = -1;
154 bool waiting_for_ack;
155 struct queue_event ev;
157 waiting_for_ack = false;
159 while(1)
161 queue_wait(&usb_queue, &ev);
162 switch(ev.id)
164 #ifdef USB_DRIVER_CLOSE
165 case USB_QUIT:
166 return;
167 #endif
168 #ifdef HAVE_USBSTACK
169 case USB_TRANSFER_COMPLETION:
170 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
171 break;
172 #endif
173 #ifdef HAVE_USB_POWER
174 case USB_POWERED:
175 usb_state = USB_POWERED;
176 break;
177 #endif
178 case USB_INSERTED:
179 #ifdef HAVE_LCD_BITMAP
180 if(do_screendump_instead_of_usb)
182 screen_dump();
184 else
185 #endif
186 #ifdef HAVE_USB_POWER
187 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
188 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
189 #else
190 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
191 #endif
193 usb_state = USB_POWERED;
194 #ifdef HAVE_USBSTACK
195 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,false);
196 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,true);
197 usb_enable(true);
198 #endif
200 else
201 #endif
203 #ifdef HAVE_USBSTACK
204 /* Set the state to USB_POWERED for now. if a real
205 connection is detected it will switch to USB_INSERTED */
206 usb_state = USB_POWERED;
207 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
208 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,false);
209 usb_enable(true);
210 #else
211 /* Tell all threads that they have to back off the ATA.
212 We subtract one for our own thread. */
213 num_acks_to_expect =
214 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
215 waiting_for_ack = true;
216 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
217 num_acks_to_expect);
218 #endif
220 break;
221 #ifdef HAVE_USBSTACK
222 case USB_REQUEST_DISK:
223 if(!waiting_for_ack)
225 /* Tell all threads that they have to back off the ATA.
226 We subtract one for our own thread. */
227 num_acks_to_expect =
228 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
229 waiting_for_ack = true;
230 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
231 num_acks_to_expect);
233 break;
234 case USB_RELEASE_DISK:
235 if(!waiting_for_ack)
237 /* Tell all threads that they have to back off the ATA.
238 We subtract one for our own thread. */
239 num_acks_to_expect =
240 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
241 waiting_for_ack = true;
242 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
243 num_acks_to_expect);
245 break;
246 #endif
247 case SYS_USB_CONNECTED_ACK:
248 if(waiting_for_ack)
250 num_acks_to_expect--;
251 if(num_acks_to_expect == 0)
253 DEBUGF("All threads have acknowledged the connect.\n");
254 #ifdef HAVE_USBSTACK
255 #ifndef USE_ROCKBOX_USB
256 /* until we have native mass-storage mode, we want to reboot on
257 usb host connect */
258 usb_enable(true);
259 try_reboot();
260 #endif /* USE_ROCKBOX_USB */
261 #ifdef HAVE_PRIORITY_SCHEDULING
262 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
263 #endif
264 exclusive_ata_access = true;
266 #else
267 usb_slave_mode(true);
268 cpu_idle_mode(true);
269 #endif
270 usb_state = USB_INSERTED;
271 waiting_for_ack = false;
273 else
275 DEBUGF("usb: got ack, %d to go...\n",
276 num_acks_to_expect);
279 break;
281 case USB_EXTRACTED:
282 #ifdef HAVE_USBSTACK
283 usb_enable(false);
284 #ifdef HAVE_PRIORITY_SCHEDULING
285 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
286 #endif
287 #endif
288 #ifdef HAVE_LCD_BITMAP
289 if(do_screendump_instead_of_usb)
290 break;
291 #endif
292 #ifdef HAVE_USB_POWER
293 if(usb_state == USB_POWERED)
295 usb_state = USB_EXTRACTED;
296 break;
298 #endif
299 #ifndef HAVE_USBSTACK
300 if(usb_state == USB_INSERTED)
302 /* Only disable the USB mode if we really have enabled it
303 some threads might not have acknowledged the
304 insertion */
305 usb_slave_mode(false);
306 cpu_idle_mode(false);
308 #endif
310 usb_state = USB_EXTRACTED;
311 #ifdef HAVE_USBSTACK
312 if(exclusive_ata_access)
314 int rc = disk_mount_all();
315 if (rc <= 0) /* no partition */
316 panicf("mount: %d",rc);
317 exclusive_ata_access = false;
318 #endif
319 /* Tell all threads that we are back in business */
320 num_acks_to_expect =
321 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
322 waiting_for_ack = true;
323 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
324 num_acks_to_expect);
325 #ifdef HAVE_USBSTACK
327 #endif
328 break;
330 case SYS_USB_DISCONNECTED_ACK:
331 if(waiting_for_ack)
333 num_acks_to_expect--;
334 if(num_acks_to_expect == 0)
336 DEBUGF("All threads have acknowledged. "
337 "We're in business.\n");
338 waiting_for_ack = false;
340 else
342 DEBUGF("usb: got ack, %d to go...\n",
343 num_acks_to_expect);
346 break;
348 #ifdef HAVE_HOTSWAP
349 case SYS_HOTSWAP_INSERTED:
350 case SYS_HOTSWAP_EXTRACTED:
351 #ifdef HAVE_USBSTACK
352 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
353 #else
354 if(usb_state == USB_INSERTED)
356 usb_enable(false);
357 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
359 #endif
360 break;
362 case USB_REENABLE:
363 if(usb_state == USB_INSERTED)
364 usb_enable(true); /* reenable only if still inserted */
365 break;
366 #endif /* HAVE_HOTSWAP */
367 case USB_REQUEST_REBOOT:
368 #ifdef HAVE_USB_POWER
369 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
370 #endif
371 try_reboot();
372 break;
376 #endif
378 #ifdef HAVE_USBSTACK
379 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
381 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
383 #endif
385 #ifdef USB_FULL_INIT
386 static void usb_tick(void)
388 int current_status;
390 if(usb_monitor_enabled)
392 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
393 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
394 int current_firewire_status = firewire_detect();
395 if(current_firewire_status != last_firewire_status)
397 last_firewire_status = current_firewire_status;
398 firewire_countdown = NUM_POLL_READINGS;
400 else
402 /* Count down until it gets negative */
403 if(firewire_countdown >= 0)
404 firewire_countdown--;
406 /* Report to the thread if we have had 3 identical status
407 readings in a row */
408 if(firewire_countdown == 0)
410 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
413 #endif
415 current_status = usb_detect();
417 /* Only report when the status has changed */
418 if(current_status != last_usb_status)
420 last_usb_status = current_status;
421 countdown = NUM_POLL_READINGS;
423 else
425 /* Count down until it gets negative */
426 if(countdown >= 0)
427 countdown--;
429 /* Report to the thread if we have had 3 identical status
430 readings in a row */
431 if(countdown == 0)
433 queue_post(&usb_queue, current_status, 0);
437 #ifdef HAVE_MMC
438 if(usb_mmc_countdown > 0)
440 usb_mmc_countdown--;
441 if (usb_mmc_countdown == 0)
442 queue_post(&usb_queue, USB_REENABLE, 0);
444 #endif
446 #endif
448 void usb_acknowledge(long id)
450 queue_post(&usb_queue, id, 0);
453 void usb_init(void)
455 usb_state = USB_EXTRACTED;
456 #ifdef HAVE_USBSTACK
457 exclusive_ata_access = false;
458 #endif
459 usb_monitor_enabled = false;
460 countdown = -1;
462 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
463 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
464 firewire_countdown = -1;
465 last_firewire_status = false;
466 #endif
468 usb_init_device();
469 #ifdef USB_FULL_INIT
470 usb_enable(false);
471 #endif
473 /* We assume that the USB cable is extracted */
474 last_usb_status = USB_EXTRACTED;
476 #ifdef USB_FULL_INIT
477 queue_init(&usb_queue, true);
479 usb_thread_entry = create_thread(usb_thread, usb_stack,
480 sizeof(usb_stack), 0, usb_thread_name
481 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
483 tick_add_task(usb_tick);
484 #endif
488 void usb_wait_for_disconnect(struct event_queue *q)
490 struct queue_event ev;
492 /* Don't return until we get SYS_USB_DISCONNECTED */
493 while(1)
495 queue_wait(q, &ev);
496 if(ev.id == SYS_USB_DISCONNECTED)
498 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
499 return;
504 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
506 struct queue_event ev;
508 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
509 while(1)
511 queue_wait_w_tmo(q, &ev, ticks);
512 switch(ev.id)
514 case SYS_USB_DISCONNECTED:
515 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
516 return 0;
517 break;
518 case SYS_TIMEOUT:
519 return 1;
520 break;
525 void usb_start_monitoring(void)
527 usb_monitor_enabled = true;
530 #ifdef USB_DRIVER_CLOSE
531 void usb_close(void)
533 struct thread_entry *thread = usb_thread_entry;
534 usb_thread_entry = NULL;
536 if (thread == NULL)
537 return;
539 tick_remove_task(usb_tick);
540 usb_monitor_enabled = false;
542 queue_post(&usb_queue, USB_QUIT, 0);
543 thread_wait(thread);
545 #endif /* USB_DRIVER_CLOSE */
547 bool usb_inserted(void)
549 #ifdef HAVE_USB_POWER
550 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
551 #else
552 return usb_state == USB_INSERTED;
553 #endif
556 #ifdef HAVE_USBSTACK
557 void usb_request_exclusive_ata(void)
559 /* This is not really a clean place to start boosting the cpu. but it's
560 * currently the best one. We want to get rid of having to boost the cpu
561 * for usb anyway */
562 trigger_cpu_boost();
563 if(!exclusive_ata_access) {
564 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
568 void usb_release_exclusive_ata(void)
570 cancel_cpu_boost();
571 if(exclusive_ata_access) {
572 queue_post(&usb_queue, USB_RELEASE_DISK, 0);
573 exclusive_ata_access = false;
577 bool usb_exclusive_ata(void)
579 return exclusive_ata_access;
581 #endif
583 #ifdef HAVE_USB_POWER
584 bool usb_powered(void)
586 return usb_state == USB_POWERED;
589 #if CONFIG_CHARGING
590 bool usb_charging_enable(bool on)
592 bool rc = false;
593 #ifdef IRIVER_H300_SERIES
594 int irqlevel;
595 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
596 irqlevel = disable_irq_save();
597 pcf50606_set_usb_charging(on);
598 rc = on;
599 restore_irq(irqlevel);
600 #else
601 /* TODO: implement it for other targets... */
602 (void)on;
603 #endif
604 return rc;
607 bool usb_charging_enabled(void)
609 bool rc = false;
610 #ifdef IRIVER_H300_SERIES
611 /* TODO: read the state of the GPOOD2 register...
612 * (this also means to set the irq level here) */
613 rc = pcf50606_usb_charging_enabled();
614 #else
615 /* TODO: implement it for other targets... */
616 #endif
618 logf("usb charging %s", rc ? "enabled" : "disabled" );
619 return rc;
621 #endif
622 #endif
624 #else
626 #ifdef USB_NONE
627 bool usb_inserted(void)
629 return false;
631 #endif
633 /* Dummy simulator functions */
634 void usb_acknowledge(long id)
636 id = id;
639 void usb_init(void)
643 void usb_start_monitoring(void)
647 int usb_detect(void)
649 return USB_EXTRACTED;
652 void usb_wait_for_disconnect(struct event_queue *q)
654 (void)q;
657 #endif /* USB_NONE or SIMULATOR */