Bootloaders don't need idle callbacks.
[kugel-rb.git] / firmware / usb.c
blob8bd11e4ec12204e6b69c0b3ff0da1fff41e15fd3
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) || \
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 extern void dbg_ports(void); /* NASTY! defined in apps/ */
59 #ifdef HAVE_LCD_BITMAP
60 bool do_screendump_instead_of_usb = false;
61 #if defined(USB_FULL_INIT) && defined(BOOTLOADER)
62 static void screen_dump(void) {}
63 #else
64 void screen_dump(void); /* Nasty again. Defined in apps/ too */
65 #endif
66 #endif
68 #if !defined(SIMULATOR) && !defined(USB_NONE)
70 #define NUM_POLL_READINGS (HZ/5)
71 static int countdown;
73 static int usb_state;
75 #if defined(HAVE_MMC) && defined(USB_FULL_INIT)
76 static int usb_mmc_countdown = 0;
77 #endif
79 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
80 needs updating */
81 #ifdef USB_FULL_INIT
82 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
83 static const char usb_thread_name[] = "usb";
84 static struct thread_entry *usb_thread_entry;
85 #endif
86 static struct event_queue usb_queue;
87 static int last_usb_status;
88 static bool usb_monitor_enabled;
89 #ifdef HAVE_USBSTACK
90 static bool exclusive_ata_access;
91 #endif
94 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
95 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
96 static int firewire_countdown;
97 static bool last_firewire_status;
98 #endif
100 #ifdef USB_FULL_INIT
101 #ifndef HAVE_USBSTACK
102 static void usb_slave_mode(bool on)
104 int rc;
106 if(on)
108 DEBUGF("Entering USB slave mode\n");
109 ata_soft_reset();
110 ata_init();
111 ata_enable(false);
112 usb_enable(true);
114 else
116 DEBUGF("Leaving USB slave mode\n");
118 /* Let the ISDx00 settle */
119 sleep(HZ*1);
121 usb_enable(false);
123 rc = ata_init();
124 if(rc)
126 /* fixme: can we remove this? (already such in main.c) */
127 char str[32];
128 lcd_clear_display();
129 snprintf(str, 31, "ATA error: %d", rc);
130 lcd_puts(0, 0, str);
131 lcd_puts(0, 1, "Press ON to debug");
132 lcd_update();
133 while(!(button_get(true) & BUTTON_REL)) {};
134 dbg_ports();
135 panicf("ata: %d",rc);
138 rc = disk_mount_all();
139 if (rc <= 0) /* no partition */
140 panicf("mount: %d",rc);
144 #endif
146 static void try_reboot(void)
148 #ifndef HAVE_FLASH_STORAGE
149 ata_sleepnow(); /* Immediately spindown the disk. */
150 sleep(HZ*2);
151 #endif
153 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
154 #if CONFIG_CPU == PP5020
155 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
156 #elif CONFIG_CPU == PP5022
157 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
158 #endif /* CONFIG_CPU */
159 #endif /* IPOD_ARCH */
161 system_reboot(); /* Reboot */
164 static void usb_thread(void)
166 int num_acks_to_expect = -1;
167 bool waiting_for_ack;
168 struct queue_event ev;
170 waiting_for_ack = false;
172 while(1)
174 queue_wait(&usb_queue, &ev);
175 switch(ev.id)
177 #ifdef USB_DRIVER_CLOSE
178 case USB_QUIT:
179 return;
180 #endif
181 #ifdef HAVE_USBSTACK
182 case USB_TRANSFER_COMPLETION:
183 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
184 break;
185 #endif
186 #ifdef HAVE_USB_POWER
187 case USB_POWERED:
188 usb_state = USB_POWERED;
189 break;
190 #endif
191 case USB_INSERTED:
192 #ifdef HAVE_LCD_BITMAP
193 if(do_screendump_instead_of_usb)
195 screen_dump();
197 else
198 #endif
199 #ifdef HAVE_USB_POWER
200 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
201 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
202 #else
203 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
204 #endif
206 usb_state = USB_POWERED;
207 #ifdef HAVE_USBSTACK
208 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,false);
209 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,true);
210 usb_enable(true);
211 #endif
213 else
214 #endif
216 #ifdef HAVE_USBSTACK
217 /* Set the state to USB_POWERED for now. if a real
218 connection is detected it will switch to USB_INSERTED */
219 usb_state = USB_POWERED;
220 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
221 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,false);
222 usb_enable(true);
223 #else
224 /* Tell all threads that they have to back off the ATA.
225 We subtract one for our own thread. */
226 num_acks_to_expect =
227 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
228 waiting_for_ack = true;
229 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
230 num_acks_to_expect);
231 #endif
233 break;
234 #ifdef HAVE_USBSTACK
235 case USB_REQUEST_DISK:
236 if(!waiting_for_ack)
238 /* Tell all threads that they have to back off the ATA.
239 We subtract one for our own thread. */
240 num_acks_to_expect =
241 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
242 waiting_for_ack = true;
243 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
244 num_acks_to_expect);
246 break;
247 case USB_RELEASE_DISK:
248 if(!waiting_for_ack)
250 /* Tell all threads that they have to back off the ATA.
251 We subtract one for our own thread. */
252 num_acks_to_expect =
253 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
254 waiting_for_ack = true;
255 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
256 num_acks_to_expect);
258 break;
259 #endif
260 case SYS_USB_CONNECTED_ACK:
261 if(waiting_for_ack)
263 num_acks_to_expect--;
264 if(num_acks_to_expect == 0)
266 DEBUGF("All threads have acknowledged the connect.\n");
267 #ifdef HAVE_USBSTACK
268 #ifndef USE_ROCKBOX_USB
269 /* until we have native mass-storage mode, we want to reboot on
270 usb host connect */
271 usb_enable(true);
272 try_reboot();
273 #endif /* USE_ROCKBOX_USB */
274 #ifdef HAVE_PRIORITY_SCHEDULING
275 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
276 #endif
277 exclusive_ata_access = true;
279 #else
280 usb_slave_mode(true);
281 cpu_idle_mode(true);
282 #endif
283 usb_state = USB_INSERTED;
284 waiting_for_ack = false;
286 else
288 DEBUGF("usb: got ack, %d to go...\n",
289 num_acks_to_expect);
292 break;
294 case USB_EXTRACTED:
295 #ifdef HAVE_USBSTACK
296 usb_enable(false);
297 #ifdef HAVE_PRIORITY_SCHEDULING
298 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
299 #endif
300 #endif
301 #ifdef HAVE_LCD_BITMAP
302 if(do_screendump_instead_of_usb)
303 break;
304 #endif
305 #ifdef HAVE_USB_POWER
306 if(usb_state == USB_POWERED)
308 usb_state = USB_EXTRACTED;
309 break;
311 #endif
312 #ifndef HAVE_USBSTACK
313 if(usb_state == USB_INSERTED)
315 /* Only disable the USB mode if we really have enabled it
316 some threads might not have acknowledged the
317 insertion */
318 usb_slave_mode(false);
319 cpu_idle_mode(false);
321 #endif
323 usb_state = USB_EXTRACTED;
324 #ifdef HAVE_USBSTACK
325 if(exclusive_ata_access)
327 int rc = disk_mount_all();
328 if (rc <= 0) /* no partition */
329 panicf("mount: %d",rc);
330 exclusive_ata_access = false;
331 #endif
332 /* Tell all threads that we are back in business */
333 num_acks_to_expect =
334 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
335 waiting_for_ack = true;
336 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
337 num_acks_to_expect);
338 #ifdef HAVE_USBSTACK
340 #endif
341 break;
343 case SYS_USB_DISCONNECTED_ACK:
344 if(waiting_for_ack)
346 num_acks_to_expect--;
347 if(num_acks_to_expect == 0)
349 DEBUGF("All threads have acknowledged. "
350 "We're in business.\n");
351 waiting_for_ack = false;
353 else
355 DEBUGF("usb: got ack, %d to go...\n",
356 num_acks_to_expect);
359 break;
361 #ifdef HAVE_HOTSWAP
362 case SYS_HOTSWAP_INSERTED:
363 case SYS_HOTSWAP_EXTRACTED:
364 #ifdef HAVE_USBSTACK
365 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
366 #else
367 if(usb_state == USB_INSERTED)
369 usb_enable(false);
370 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
372 #endif
373 break;
375 case USB_REENABLE:
376 if(usb_state == USB_INSERTED)
377 usb_enable(true); /* reenable only if still inserted */
378 break;
379 #endif /* HAVE_HOTSWAP */
380 case USB_REQUEST_REBOOT:
381 #ifdef HAVE_USB_POWER
382 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
383 #endif
384 try_reboot();
385 break;
389 #endif
391 #ifdef HAVE_USBSTACK
392 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
394 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
396 #endif
398 #ifdef USB_FULL_INIT
399 static void usb_tick(void)
401 int current_status;
403 if(usb_monitor_enabled)
405 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
406 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
407 int current_firewire_status = firewire_detect();
408 if(current_firewire_status != last_firewire_status)
410 last_firewire_status = current_firewire_status;
411 firewire_countdown = NUM_POLL_READINGS;
413 else
415 /* Count down until it gets negative */
416 if(firewire_countdown >= 0)
417 firewire_countdown--;
419 /* Report to the thread if we have had 3 identical status
420 readings in a row */
421 if(firewire_countdown == 0)
423 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
426 #endif
428 current_status = usb_detect();
430 /* Only report when the status has changed */
431 if(current_status != last_usb_status)
433 last_usb_status = current_status;
434 countdown = NUM_POLL_READINGS;
436 else
438 /* Count down until it gets negative */
439 if(countdown >= 0)
440 countdown--;
442 /* Report to the thread if we have had 3 identical status
443 readings in a row */
444 if(countdown == 0)
446 queue_post(&usb_queue, current_status, 0);
450 #ifdef HAVE_MMC
451 if(usb_mmc_countdown > 0)
453 usb_mmc_countdown--;
454 if (usb_mmc_countdown == 0)
455 queue_post(&usb_queue, USB_REENABLE, 0);
457 #endif
459 #endif
461 void usb_acknowledge(long id)
463 queue_post(&usb_queue, id, 0);
466 void usb_init(void)
468 usb_state = USB_EXTRACTED;
469 #ifdef HAVE_USBSTACK
470 exclusive_ata_access = false;
471 #endif
472 usb_monitor_enabled = false;
473 countdown = -1;
475 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
476 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
477 firewire_countdown = -1;
478 last_firewire_status = false;
479 #endif
481 usb_init_device();
482 #ifdef USB_FULL_INIT
483 usb_enable(false);
484 #endif
486 /* We assume that the USB cable is extracted */
487 last_usb_status = USB_EXTRACTED;
489 #ifdef USB_FULL_INIT
490 queue_init(&usb_queue, true);
492 usb_thread_entry = create_thread(usb_thread, usb_stack,
493 sizeof(usb_stack), 0, usb_thread_name
494 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
496 tick_add_task(usb_tick);
497 #endif
501 void usb_wait_for_disconnect(struct event_queue *q)
503 struct queue_event ev;
505 /* Don't return until we get SYS_USB_DISCONNECTED */
506 while(1)
508 queue_wait(q, &ev);
509 if(ev.id == SYS_USB_DISCONNECTED)
511 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
512 return;
517 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
519 struct queue_event ev;
521 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
522 while(1)
524 queue_wait_w_tmo(q, &ev, ticks);
525 switch(ev.id)
527 case SYS_USB_DISCONNECTED:
528 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
529 return 0;
530 break;
531 case SYS_TIMEOUT:
532 return 1;
533 break;
538 void usb_start_monitoring(void)
540 usb_monitor_enabled = true;
543 #ifdef USB_DRIVER_CLOSE
544 void usb_close(void)
546 struct thread_entry *thread = usb_thread_entry;
547 usb_thread_entry = NULL;
549 if (thread == NULL)
550 return;
552 tick_remove_task(usb_tick);
553 usb_monitor_enabled = false;
555 queue_post(&usb_queue, USB_QUIT, 0);
556 thread_wait(thread);
558 #endif /* USB_DRIVER_CLOSE */
560 bool usb_inserted(void)
562 #ifdef HAVE_USB_POWER
563 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
564 #else
565 return usb_state == USB_INSERTED;
566 #endif
569 #ifdef HAVE_USBSTACK
570 void usb_request_exclusive_ata(void)
572 /* This is not really a clean place to start boosting the cpu. but it's
573 * currently the best one. We want to get rid of having to boost the cpu
574 * for usb anyway */
575 trigger_cpu_boost();
576 if(!exclusive_ata_access) {
577 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
581 void usb_release_exclusive_ata(void)
583 cancel_cpu_boost();
584 if(exclusive_ata_access) {
585 queue_post(&usb_queue, USB_RELEASE_DISK, 0);
586 exclusive_ata_access = false;
590 bool usb_exclusive_ata(void)
592 return exclusive_ata_access;
594 #endif
596 #ifdef HAVE_USB_POWER
597 bool usb_powered(void)
599 return usb_state == USB_POWERED;
602 #if CONFIG_CHARGING
603 bool usb_charging_enable(bool on)
605 bool rc = false;
606 #ifdef IRIVER_H300_SERIES
607 int irqlevel;
608 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
609 irqlevel = disable_irq_save();
610 pcf50606_set_usb_charging(on);
611 rc = on;
612 restore_irq(irqlevel);
613 #else
614 /* TODO: implement it for other targets... */
615 (void)on;
616 #endif
617 return rc;
620 bool usb_charging_enabled(void)
622 bool rc = false;
623 #ifdef IRIVER_H300_SERIES
624 /* TODO: read the state of the GPOOD2 register...
625 * (this also means to set the irq level here) */
626 rc = pcf50606_usb_charging_enabled();
627 #else
628 /* TODO: implement it for other targets... */
629 #endif
631 logf("usb charging %s", rc ? "enabled" : "disabled" );
632 return rc;
634 #endif
635 #endif
637 #else
639 #ifdef USB_NONE
640 bool usb_inserted(void)
642 return false;
644 #endif
646 /* Dummy simulator functions */
647 void usb_acknowledge(long id)
649 id = id;
652 void usb_init(void)
656 void usb_start_monitoring(void)
660 int usb_detect(void)
662 return USB_EXTRACTED;
665 void usb_wait_for_disconnect(struct event_queue *q)
667 (void)q;
670 #endif /* USB_NONE or SIMULATOR */