1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
37 #include "usb-target.h"
45 #include "screendump.h"
47 /* Conditions under which we want the entire driver */
48 #if !defined(BOOTLOADER) || (CONFIG_CPU == SH7034) || \
49 (defined(HAVE_USBSTACK) && defined(HAVE_BOOTLOADER_USB_MODE)) || \
50 (defined(HAVE_USBSTACK) && (defined(CREATIVE_ZVx))) || \
51 (defined(HAVE_USBSTACK) && (defined(OLYMPUS_MROBE_500))) || \
52 (defined(HAVE_USBSTACK) && (defined(IPOD_NANO2G))) || \
53 defined(CPU_TCC77X) || defined(CPU_TCC780X) || \
54 (CONFIG_USBOTG == USBOTG_JZ4740) || \
55 (defined(USE_ROCKBOX_USB) && CONFIG_USBOTG == USBOTG_AS3525) || \
56 (defined(USE_ROCKBOX_USB) && CONFIG_USBOTG == USBOTG_AS3525v2)
60 #if defined(USB_DETECT_BY_DRV) || defined(USB_DETECT_BY_CORE)
61 /* These aren't treated differently here */
62 #define USB_DELAYED_INSERT
65 #ifdef HAVE_LCD_BITMAP
66 bool do_screendump_instead_of_usb
= false;
69 #if !defined(SIMULATOR) && !defined(USB_NONE)
71 /* We assume that the USB cable is extracted */
72 static int usb_state
= USB_EXTRACTED
;
74 #if (CONFIG_STORAGE & STORAGE_MMC) && defined(USB_FULL_INIT)
75 static int usb_mmc_countdown
= 0;
78 /* Make sure there's enough stack space for screendump */
80 static long usb_stack
[(DEFAULT_STACK_SIZE
+ DUMP_BMP_LINESIZE
)/sizeof(long)];
81 static const char usb_thread_name
[] = "usb";
82 static unsigned int usb_thread_entry
= 0;
83 static bool usb_monitor_enabled
= false;
84 #endif /* USB_FULL_INIT */
85 static struct event_queue usb_queue SHAREDBSS_ATTR
;
86 static bool exclusive_storage_access
= false;
88 static bool usb_hid
= true;
93 #if defined(USB_FIREWIRE_HANDLING) \
94 || (defined(HAVE_USBSTACK) && !defined(USE_ROCKBOX_USB))
95 static void try_reboot(void)
97 #ifdef HAVE_DISK_STORAGE
98 storage_sleepnow(); /* Immediately spindown the disk. */
102 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
103 #if CONFIG_CPU == PP5020
104 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
105 #elif CONFIG_CPU == PP5022
106 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
107 #endif /* CONFIG_CPU */
108 #endif /* IPOD_ARCH */
111 memcpy((void *)0x0002bf00, "diskmodehotstuff\1\0\0\0", 20);
114 system_reboot(); /* Reboot */
116 #endif /* USB_FIRWIRE_HANDLING || (HAVE_USBSTACK && !USE_ROCKBOX_USB) */
119 #ifdef HAVE_LCD_BITMAP
120 static inline bool usb_do_screendump(void)
122 if(do_screendump_instead_of_usb
)
124 usb_state
= USB_SCREENDUMP
;
126 #ifdef HAVE_REMOTE_LCD
127 remote_screen_dump();
134 #else /* !HAVE_LCD_BITMAP */
135 static inline bool usb_do_screendump(void)
139 #endif /* HAVE_LCD_BITMAP */
145 static inline void usb_handle_hotswap(long id
)
149 case SYS_HOTSWAP_INSERTED
:
150 case SYS_HOTSWAP_EXTRACTED
:
151 usb_core_hotswap_event(1, id
== SYS_HOTSWAP_INSERTED
);
154 /* Note: No MMC storage handling is needed with the stack atm. */
156 #endif /* HAVE_HOTSWAP */
158 static inline bool usb_configure_drivers(int for_state
)
163 #ifdef USB_ENABLE_STORAGE
164 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE
, false);
166 #ifdef USB_ENABLE_HID
167 #ifdef USB_ENABLE_CHARGING_ONLY
168 usb_core_enable_driver(USB_DRIVER_HID
, false);
170 usb_core_enable_driver(USB_DRIVER_HID
, true);
171 #endif /* USB_ENABLE_CHARGING_ONLY */
172 #endif /* USB_ENABLE_HID */
174 #ifdef USB_ENABLE_CHARGING_ONLY
175 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY
, true);
177 exclusive_storage_access
= false;
179 usb_attach(); /* Powered only: attach now. */
184 #ifdef USB_ENABLE_STORAGE
185 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE
, true);
187 #ifdef USB_ENABLE_HID
188 usb_core_enable_driver(USB_DRIVER_HID
, usb_hid
);
190 #ifdef USB_ENABLE_CHARGING_ONLY
191 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY
, false);
193 /* Check any drivers enabled at this point for exclusive storage
194 * access requirements. */
195 exclusive_storage_access
= usb_core_any_exclusive_storage();
197 if(exclusive_storage_access
)
200 usb_attach(); /* Not exclusive: attach now. */
205 if(exclusive_storage_access
)
206 usb_release_exclusive_storage();
214 #ifdef USE_ROCKBOX_USB
215 static inline void usb_slave_mode(bool on
)
222 #ifdef HAVE_PRIORITY_SCHEDULING
223 thread_set_priority(thread_self(), PRIORITY_REALTIME
);
228 else /* usb_state == USB_INSERTED (only!) */
231 #ifdef HAVE_PRIORITY_SCHEDULING
232 thread_set_priority(thread_self(), PRIORITY_SYSTEM
);
234 /* Entered exclusive mode */
235 rc
= disk_mount_all();
236 if(rc
<= 0) /* no partition */
237 panicf("mount: %d",rc
);
242 #else /* !USB_ROCKBOX_USB */
243 static inline void usb_slave_mode(bool on
)
247 /* until we have native mass-storage mode, we want to reboot on
252 #endif /* USE_ROCKBOX_USB */
254 void usb_signal_transfer_completion(
255 struct usb_transfer_completion_event_data
* event_data
)
257 queue_post(&usb_queue
, USB_TRANSFER_COMPLETION
, (intptr_t)event_data
);
260 #else /* !HAVE_USBSTACK */
263 static inline void usb_handle_hotswap(long id
)
265 #if (CONFIG_STORAGE & STORAGE_MMC)
268 case SYS_HOTSWAP_INSERTED
:
269 case SYS_HOTSWAP_EXTRACTED
:
270 if(usb_state
== USB_INSERTED
)
273 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
277 if(usb_state
== USB_INSERTED
)
278 usb_enable(true); /* reenable only if still inserted */
281 #endif /* STORAGE_MMC */
284 #endif /* HAVE_HOTSWAP */
286 static inline bool usb_configure_drivers(int for_state
)
291 exclusive_storage_access
= false;
294 exclusive_storage_access
= true;
297 if(exclusive_storage_access
)
298 usb_release_exclusive_storage();
305 static inline void usb_slave_mode(bool on
)
311 DEBUGF("Entering USB slave mode\n");
313 storage_soft_reset();
315 storage_enable(false);
321 DEBUGF("Leaving USB slave mode\n");
323 cpu_idle_mode(false);
325 /* Let the ISDx00 settle */
332 panicf("storage: %d",rc
);
335 rc
= disk_mount_all();
336 if(rc
<= 0) /* no partition */
337 panicf("mount: %d",rc
);
340 #endif /* HAVE_USBSTACK */
342 #ifdef HAVE_USB_POWER
343 static inline bool usb_power_button(void)
345 return (button_status() & ~USBPOWER_BTN_IGNORE
);
348 #ifdef USB_FIREWIRE_HANDLING
349 static inline bool usb_reboot_button(void)
351 return (button_status() & ~USBPOWER_BTN_IGNORE
);
354 #else /* !HAVE_USB_POWER */
355 static inline bool usb_power_button(void)
360 #ifdef USB_FIREWIRE_HANDLING
361 static inline bool usb_reboot_button(void)
366 #endif /* HAVE_USB_POWER */
368 #ifndef USB_DRIVER_CLOSE
369 static void usb_thread(void) NORETURN_ATTR
;
371 static void usb_thread(void)
373 #ifdef USB_DELAYED_INSERT
374 bool host_detected
= false;
376 int num_acks_to_expect
= 0;
377 long last_broadcast_tick
= current_tick
;
378 struct queue_event ev
;
382 queue_wait(&usb_queue
, &ev
);
385 /*** Main USB thread duties ***/
388 case USB_TRANSFER_COMPLETION
:
389 usb_core_handle_transfer_completion(
390 (struct usb_transfer_completion_event_data
*)ev
.data
);
392 #endif /* HAVE_USBSTACK */
395 #ifdef USB_DELAYED_INSERT
396 if(usb_state
!= USB_POWERED
)
400 break; /* Drivers configured but we're still USB_POWERED */
402 host_detected
= true;
403 #else /* !USB_DELAYED_INSERT */
404 if(usb_state
!= USB_EXTRACTED
)
407 if(usb_do_screendump())
410 usb_state
= USB_POWERED
;
411 #endif /* USB_DELAYED_INSERT */
413 if(usb_power_button())
415 /* Only charging is desired */
416 usb_configure_drivers(USB_POWERED
);
420 if(!usb_configure_drivers(USB_INSERTED
))
421 break; /* Exclusive storage access not required */
423 /* Tell all threads that they have to back off the storage.
424 We subtract one for our own thread. Expect an ACK for every
425 listener for each broadcast they received. If it has been too
426 long, the user might have entered a screen that didn't ACK
427 when inserting the cable, such as a debugging screen. In that
428 case, reset the count or else USB would be locked out until
429 rebooting because it most likely won't ever come. Simply
430 resetting to the most recent broadcast count is racy. */
431 if(TIME_AFTER(current_tick
, last_broadcast_tick
+ HZ
*5))
433 num_acks_to_expect
= 0;
434 last_broadcast_tick
= current_tick
;
437 num_acks_to_expect
+= queue_broadcast(SYS_USB_CONNECTED
, 0) - 1;
438 DEBUGF("USB inserted. Waiting for %d acks...\n",
441 /* Leave the state as USB_POWERED until the expected number of
442 ACKS are received. */
446 case SYS_USB_CONNECTED_ACK
:
447 if(num_acks_to_expect
> 0 && --num_acks_to_expect
== 0)
449 DEBUGF("All threads have acknowledged the connect.\n");
450 if(usb_state
== USB_POWERED
)
452 usb_slave_mode(true);
453 usb_state
= USB_INSERTED
;
458 DEBUGF("usb: got ack, %d to go...\n",
462 /* SYS_USB_CONNECTED_ACK */
464 #ifdef USB_DELAYED_INSERT
465 /* In this case, these events handle cable insertion. USB driver or
466 core determines USB_INSERTED. */
468 if(usb_state
!= USB_EXTRACTED
)
471 if(usb_do_screendump())
474 usb_state
= USB_POWERED
;
480 if(usb_state
== USB_POWERED
)
482 /* Fall-through - other legal states can be USB_INSERTED or
484 #endif /* USB_DELAYED_INSERT */
486 if(usb_state
== USB_EXTRACTED
)
489 /* Only disable the USB slave mode if we really have enabled
490 it. Some expected acks may not have been received. */
491 if(usb_state
== USB_INSERTED
)
492 usb_slave_mode(false);
494 usb_state
= USB_EXTRACTED
;
496 /* Ok to broadcast disconnect now */
497 usb_configure_drivers(USB_EXTRACTED
);
498 #ifdef USB_DELAYED_INSERT
499 host_detected
= false;
502 /* USB_UNPOWERED: USB_EXTRACTED: */
504 /*** Miscellaneous USB thread duties ***/
508 case SYS_HOTSWAP_INSERTED
:
509 case SYS_HOTSWAP_EXTRACTED
:
510 #if (CONFIG_STORAGE & STORAGE_MMC)
513 usb_handle_hotswap(ev
.id
);
517 #ifdef USB_FIREWIRE_HANDLING
518 case USB_REQUEST_REBOOT
:
519 if(usb_reboot_button())
525 #if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
526 case USB_CHARGER_UPDATE
:
527 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
532 #ifdef USB_DRIVER_CLOSE
540 #if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
541 void usb_charger_update(void)
543 queue_post(&usb_queue
, USB_CHARGER_UPDATE
, 0);
547 #ifdef USB_STATUS_BY_EVENT
548 void usb_status_event(int current_status
)
550 /* Caller isn't expected to filter for changes in status.
552 * USB_DETECT_BY_DRV/CORE: USB_POWERED, USB_UNPOWERED,
553 USB_INSERTED (driver/core)
554 * else: USB_INSERTED, USB_EXTRACTED
556 if(usb_monitor_enabled
)
558 int oldstatus
= disable_irq_save(); /* Dual-use function */
559 queue_remove_from_head(&usb_queue
, current_status
);
560 queue_post(&usb_queue
, current_status
, 0);
561 restore_irq(oldstatus
);
565 void usb_start_monitoring(void)
567 int oldstatus
= disable_irq_save(); /* Sync to event */
568 int status
= usb_detect();
570 usb_monitor_enabled
= true;
572 /* An event may have been missed because it was sent before monitoring
573 * was enabled due to the connector already having been inserted before
574 * before or during boot. */
575 #ifdef USB_DELAYED_INSERT
576 /* Filter the status - USB_INSERTED may happen later */
577 status
= (status
== USB_EXTRACTED
) ? USB_UNPOWERED
: USB_POWERED
;
579 usb_status_event(status
);
581 #ifdef USB_FIREWIRE_HANDLING
582 if(firewire_detect())
583 usb_firewire_connect_event();
586 restore_irq(oldstatus
);
589 #ifdef USB_FIREWIRE_HANDLING
590 void usb_firewire_connect_event(void)
592 queue_post(&usb_queue
, USB_REQUEST_REBOOT
, 0);
594 #endif /* USB_FIREWIRE_HANDLING */
596 #else /* !USB_STATUS_BY_EVENT */
598 static void usb_tick(void)
600 #define NUM_POLL_READINGS (HZ/5)
601 static int usb_countdown
= -1;
602 static int last_usb_status
= USB_EXTRACTED
;
603 #ifdef USB_FIREWIRE_HANDLING
604 static int firewire_countdown
= -1;
605 static int last_firewire_status
= false;
608 if(usb_monitor_enabled
)
610 #ifdef USB_FIREWIRE_HANDLING
611 int current_firewire_status
= firewire_detect();
612 if(current_firewire_status
!= last_firewire_status
)
614 last_firewire_status
= current_firewire_status
;
615 firewire_countdown
= NUM_POLL_READINGS
;
619 /* Count down until it gets negative */
620 if(firewire_countdown
>= 0)
621 firewire_countdown
--;
623 /* Report to the thread if we have had 3 identical status
625 if(firewire_countdown
== 0)
627 queue_post(&usb_queue
, USB_REQUEST_REBOOT
, 0);
630 #endif /* USB_FIREWIRE_HANDLING */
632 int current_status
= usb_detect();
634 /* Only report when the status has changed */
635 if(current_status
!= last_usb_status
)
637 last_usb_status
= current_status
;
638 usb_countdown
= NUM_POLL_READINGS
;
642 /* Count down until it gets negative */
643 if(usb_countdown
>= 0)
646 /* Report to the thread if we have had 3 identical status
648 if(usb_countdown
== 0)
650 queue_post(&usb_queue
, current_status
, 0);
654 #if (CONFIG_STORAGE & STORAGE_MMC)
655 if(usb_mmc_countdown
> 0)
658 if(usb_mmc_countdown
== 0)
659 queue_post(&usb_queue
, USB_REENABLE
, 0);
664 void usb_start_monitoring(void)
666 usb_monitor_enabled
= true;
668 #endif /* USB_STATUS_BY_EVENT */
669 #endif /* USB_FULL_INIT */
671 void usb_acknowledge(long id
)
673 queue_post(&usb_queue
, id
, 0);
683 queue_init(&usb_queue
, true);
685 usb_thread_entry
= create_thread(usb_thread
, usb_stack
,
686 sizeof(usb_stack
), 0, usb_thread_name
687 IF_PRIO(, PRIORITY_SYSTEM
) IF_COP(, CPU
));
689 #ifndef USB_STATUS_BY_EVENT
690 tick_add_task(usb_tick
);
692 #endif /* USB_FULL_INIT */
695 void usb_wait_for_disconnect(struct event_queue
*q
)
698 struct queue_event ev
;
700 /* Don't return until we get SYS_USB_DISCONNECTED */
704 if(ev
.id
== SYS_USB_DISCONNECTED
)
707 #endif /* USB_FULL_INIT */
711 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
714 struct queue_event ev
;
716 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
719 queue_wait_w_tmo(q
, &ev
, ticks
);
722 case SYS_USB_DISCONNECTED
:
728 #endif /* USB_FULL_INIT */
729 (void)q
; (void)ticks
;
733 #ifdef USB_DRIVER_CLOSE
736 unsigned int thread
= usb_thread_entry
;
737 usb_thread_entry
= 0;
742 #ifndef USB_STATUS_BY_EVENT
743 tick_remove_task(usb_tick
);
745 usb_monitor_enabled
= false;
747 queue_post(&usb_queue
, USB_QUIT
, 0);
750 #endif /* USB_DRIVER_CLOSE */
752 bool usb_inserted(void)
754 return usb_state
== USB_INSERTED
|| usb_state
== USB_POWERED
;
758 bool usb_exclusive_storage(void)
760 /* Storage isn't actually exclusive until slave mode has been entered */
761 return exclusive_storage_access
&& usb_state
== USB_INSERTED
;
763 #endif /* HAVE_USBSTACK */
765 int usb_release_exclusive_storage(void)
768 exclusive_storage_access
= false;
769 /* Tell all threads that we are back in business */
770 bccount
= queue_broadcast(SYS_USB_DISCONNECTED
, 0) - 1;
771 DEBUGF("USB extracted. Broadcast to %d threads...\n", bccount
);
775 #ifdef HAVE_USB_POWER
776 bool usb_powered(void)
778 return usb_state
== USB_POWERED
;
780 #endif /* HAVE_USB_POWER */
782 #ifdef USB_ENABLE_HID
783 void usb_set_hid(bool enable
)
786 usb_core_enable_driver(USB_DRIVER_HID
, usb_hid
);
788 #endif /* USB_ENABLE_HID */
790 #else /* SIMULATOR || USB_NONE */
793 bool usb_inserted(void)
797 #endif /* USB_NONE */
799 /* Dummy simulator functions */
800 void usb_acknowledge(long id
)
809 void usb_start_monitoring(void)
815 return USB_EXTRACTED
;
818 void usb_wait_for_disconnect(struct event_queue
*q
)
823 #endif /* !USB_NONE && !SIMULATOR */