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"
46 #include "screendump.h"
48 /* Conditions under which we want the entire driver */
49 #if !defined(BOOTLOADER) || (CONFIG_CPU == SH7034) || \
50 (defined(TOSHIBA_GIGABEAT_S) && defined(USE_ROCKBOX_USB) && defined(USB_STORAGE)) || \
51 (defined(HAVE_USBSTACK) && (defined(CREATIVE_ZVx) || \
52 defined(CPU_TCC77X) || defined(CPU_TCC780X))) || \
53 (CONFIG_USBOTG == USBOTG_JZ4740)
57 #ifdef HAVE_LCD_BITMAP
58 bool do_screendump_instead_of_usb
= false;
61 #if !defined(SIMULATOR) && !defined(USB_NONE)
65 #if (CONFIG_STORAGE & STORAGE_MMC) && defined(USB_FULL_INIT)
66 static int usb_mmc_countdown
= 0;
69 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
72 static long usb_stack
[(DEFAULT_STACK_SIZE
+ 0x800)/sizeof(long)];
73 static const char usb_thread_name
[] = "usb";
74 static unsigned int usb_thread_entry
= 0;
75 #ifndef USB_STATUS_BY_EVENT
76 #define NUM_POLL_READINGS (HZ/5)
78 #endif /* USB_STATUS_BY_EVENT */
79 #endif /* USB_FULL_INIT */
80 static struct event_queue usb_queue
;
81 static bool usb_monitor_enabled
;
82 static int last_usb_status
;
84 static bool exclusive_storage_access
;
87 #ifdef USB_FIREWIRE_HANDLING
88 static int firewire_countdown
;
89 static bool last_firewire_status
;
94 #if defined(USB_FIREWIRE_HANDLING) \
95 || (defined(HAVE_USBSTACK) && !defined(USE_ROCKBOX_USB))
96 static void try_reboot(void)
98 #ifdef HAVE_DISK_STORAGE
99 storage_sleepnow(); /* Immediately spindown the disk. */
103 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
104 #if CONFIG_CPU == PP5020
105 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
106 #elif CONFIG_CPU == PP5022
107 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
108 #endif /* CONFIG_CPU */
109 #endif /* IPOD_ARCH */
111 system_reboot(); /* Reboot */
113 #endif /* USB_FIRWIRE_HANDLING || (HAVE_USBSTACK && !USE_ROCKBOX_USB) */
116 /* inline since branch is chosen at compile time */
117 static inline void usb_slave_mode(bool on
)
119 #ifdef USE_ROCKBOX_USB
125 #ifdef HAVE_PRIORITY_SCHEDULING
126 thread_set_priority(THREAD_ID_CURRENT
, PRIORITY_REALTIME
);
130 else /* usb_state == USB_INSERTED (only!) */
132 #ifndef USB_DETECT_BY_DRV
135 #ifdef HAVE_PRIORITY_SCHEDULING
136 thread_set_priority(THREAD_ID_CURRENT
, PRIORITY_SYSTEM
);
138 /* Entered exclusive mode */
139 rc
= disk_mount_all();
140 if (rc
<= 0) /* no partition */
141 panicf("mount: %d",rc
);
145 #else /* !USB_ROCKBOX_USB */
148 /* until we have native mass-storage mode, we want to reboot on
152 #endif /* USE_ROCKBOX_USB */
155 void usb_signal_transfer_completion(
156 struct usb_transfer_completion_event_data
* event_data
)
158 queue_post(&usb_queue
, USB_TRANSFER_COMPLETION
, (intptr_t)event_data
);
160 #else /* !HAVE_USBSTACK */
161 /* inline since branch is chosen at compile time */
162 static inline void usb_slave_mode(bool on
)
168 DEBUGF("Entering USB slave mode\n");
169 storage_soft_reset();
171 storage_enable(false);
177 DEBUGF("Leaving USB slave mode\n");
179 cpu_idle_mode(false);
181 /* Let the ISDx00 settle */
188 panicf("storage: %d",rc
);
190 rc
= disk_mount_all();
191 if (rc
<= 0) /* no partition */
192 panicf("mount: %d",rc
);
195 #endif /* HAVE_USBSTACK */
197 #ifdef HAVE_USB_POWER
198 static inline bool usb_power_button(void)
200 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
201 return (button_status() & ~USBPOWER_BTN_IGNORE
) != USBPOWER_BUTTON
;
203 return (button_status() & ~USBPOWER_BTN_IGNORE
) == USBPOWER_BUTTON
;
207 #ifdef USB_FIREWIRE_HANDLING
208 static inline bool usb_reboot_button(void)
210 return (button_status() & ~USBPOWER_BTN_IGNORE
) != USBPOWER_BUTTON
;
213 #endif /* HAVE_USB_POWER */
215 static void usb_thread(void)
217 int num_acks_to_expect
= 0;
218 struct queue_event ev
;
222 queue_wait(&usb_queue
, &ev
);
225 #ifdef USB_DRIVER_CLOSE
230 case USB_TRANSFER_COMPLETION
:
231 usb_core_handle_transfer_completion(
232 (struct usb_transfer_completion_event_data
*)ev
.data
);
235 #ifdef USB_DETECT_BY_DRV
236 /* In this case, these events the handle cable insertion USB
237 * driver determines INSERTED/EXTRACTED state. */
239 /* Set the state to USB_POWERED for now and enable the driver
240 * to detect a bus reset only. If a bus reset is detected,
241 * USB_INSERTED will be received. */
242 usb_state
= USB_POWERED
;
248 /* This part shouldn't be obligatory for anything that can
249 * reliably detect removal of the data lines. USB_EXTRACTED
250 * could be posted on that event while bus power remains
252 queue_post(&usb_queue
, USB_EXTRACTED
, 0);
254 #endif /* USB_DETECT_BY_DRV */
256 #ifdef HAVE_LCD_BITMAP
257 if(do_screendump_instead_of_usb
)
259 usb_state
= USB_SCREENDUMP
;
261 #ifdef HAVE_REMOTE_LCD
262 remote_screen_dump();
267 #ifdef HAVE_USB_POWER
268 if(usb_power_button())
270 /* Only charging is desired */
271 usb_state
= USB_POWERED
;
273 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE
, false);
274 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY
, true);
279 #endif /* HAVE_USB_POWER */
281 #ifdef HAVE_USB_POWER
282 /* Set the state to USB_POWERED for now. If permission to connect
283 * by threads and storage is granted it will be changed to
285 usb_state
= USB_POWERED
;
287 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE
, true);
288 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY
, false);
290 /* Check any drivers enabled at this point for exclusive storage
291 * access requirements. */
292 exclusive_storage_access
= usb_core_any_exclusive_storage();
294 if(!exclusive_storage_access
)
299 #endif /* HAVE_USBSTACK */
300 /* Tell all threads that they have to back off the storage.
301 We subtract one for our own thread. */
302 num_acks_to_expect
= queue_broadcast(SYS_USB_CONNECTED
, 0) - 1;
303 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
304 num_acks_for_connect
);
307 case SYS_USB_CONNECTED_ACK
:
308 if(num_acks_to_expect
> 0 && --num_acks_to_expect
== 0)
310 DEBUGF("All threads have acknowledged the connect.\n");
311 usb_slave_mode(true);
312 usb_state
= USB_INSERTED
;
316 DEBUGF("usb: got ack, %d to go...\n",
322 #ifdef HAVE_LCD_BITMAP
323 if(usb_state
== USB_SCREENDUMP
)
325 usb_state
= USB_EXTRACTED
;
326 break; /* Connected for screendump only */
329 #ifndef HAVE_USBSTACK /* Stack must undo this if POWERED state was transitional */
330 #ifdef HAVE_USB_POWER
331 if(usb_state
== USB_POWERED
)
333 usb_state
= USB_EXTRACTED
;
337 #endif /* HAVE_USBSTACK */
338 if(usb_state
== USB_INSERTED
)
340 /* Only disable the USB mode if we really have enabled it
341 some threads might not have acknowledged the
343 usb_slave_mode(false);
346 usb_state
= USB_EXTRACTED
;
348 if(!exclusive_storage_access
)
350 #ifndef USB_DETECT_BY_DRV /* Disabled handling USB_UNPOWERED */
356 exclusive_storage_access
= false;
357 #endif /* HAVE_USBSTACK */
359 /* Tell all threads that we are back in business */
361 queue_broadcast(SYS_USB_DISCONNECTED
, 0) - 1;
362 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
366 case SYS_USB_DISCONNECTED_ACK
:
367 if(num_acks_to_expect
> 0 && --num_acks_to_expect
== 0)
369 DEBUGF("All threads have acknowledged. "
370 "We're in business.\n");
374 DEBUGF("usb: got ack, %d to go...\n",
380 case SYS_HOTSWAP_INSERTED
:
381 case SYS_HOTSWAP_EXTRACTED
:
383 usb_core_hotswap_event(1,ev
.id
== SYS_HOTSWAP_INSERTED
);
384 #else /* !HAVE_USBSTACK */
385 if(usb_state
== USB_INSERTED
)
388 #if (CONFIG_STORAGE & STORAGE_MMC)
389 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
390 #endif /* STORAGE_MMC */
392 #endif /* HAVE_USBSTACK */
395 #if (CONFIG_STORAGE & STORAGE_MMC)
397 if(usb_state
== USB_INSERTED
)
398 usb_enable(true); /* reenable only if still inserted */
400 #endif /* STORAGE_MMC */
401 #endif /* HAVE_HOTSWAP */
403 #ifdef USB_FIREWIRE_HANDLING
404 case USB_REQUEST_REBOOT
:
405 #ifdef HAVE_USB_POWER
406 if (usb_reboot_button())
410 #endif /* USB_FIREWIRE_HANDLING */
415 #ifdef USB_STATUS_BY_EVENT
416 void usb_status_event(int current_status
)
418 /* Caller isn't expected to filter for changes in status.
420 * USB_DETECT_BY_DRV: USB_POWERED, USB_UNPOWERED, USB_INSERTED (driver)
421 * else: USB_INSERTED, USB_EXTRACTED
423 if(usb_monitor_enabled
)
425 int oldstatus
= disable_irq_save(); /* Dual-use function */
427 if(last_usb_status
!= current_status
)
429 last_usb_status
= current_status
;
430 queue_post(&usb_queue
, current_status
, 0);
433 restore_irq(oldstatus
);
437 void usb_start_monitoring(void)
439 int oldstatus
= disable_irq_save(); /* Sync to event */
440 int status
= usb_detect();
442 usb_monitor_enabled
= true;
444 #ifdef USB_DETECT_BY_DRV
445 status
= (status
== USB_INSERTED
) ? USB_POWERED
: USB_UNPOWERED
;
447 usb_status_event(status
);
449 #ifdef USB_FIREWIRE_HANDLING
450 if (firewire_detect())
451 usb_firewire_connect_event();
454 restore_irq(oldstatus
);
457 #ifdef USB_FIREWIRE_HANDLING
458 void usb_firewire_connect_event(void)
460 queue_post(&usb_queue
, USB_REQUEST_REBOOT
, 0);
462 #endif /* USB_FIREWIRE_HANDLING */
463 #else /* !USB_STATUS_BY_EVENT */
464 static void usb_tick(void)
468 if(usb_monitor_enabled
)
470 #ifdef USB_FIREWIRE_HANDLING
471 int current_firewire_status
= firewire_detect();
472 if(current_firewire_status
!= last_firewire_status
)
474 last_firewire_status
= current_firewire_status
;
475 firewire_countdown
= NUM_POLL_READINGS
;
479 /* Count down until it gets negative */
480 if(firewire_countdown
>= 0)
481 firewire_countdown
--;
483 /* Report to the thread if we have had 3 identical status
485 if(firewire_countdown
== 0)
487 queue_post(&usb_queue
, USB_REQUEST_REBOOT
, 0);
490 #endif /* USB_FIREWIRE_HANDLING */
492 current_status
= usb_detect();
494 /* Only report when the status has changed */
495 if(current_status
!= last_usb_status
)
497 last_usb_status
= current_status
;
498 countdown
= NUM_POLL_READINGS
;
502 /* Count down until it gets negative */
506 /* Report to the thread if we have had 3 identical status
510 queue_post(&usb_queue
, current_status
, 0);
514 #if (CONFIG_STORAGE & STORAGE_MMC)
515 if(usb_mmc_countdown
> 0)
518 if (usb_mmc_countdown
== 0)
519 queue_post(&usb_queue
, USB_REENABLE
, 0);
524 void usb_start_monitoring(void)
526 usb_monitor_enabled
= true;
528 #endif /* USB_STATUS_BY_EVENT */
529 #endif /* USB_FULL_INIT */
531 void usb_acknowledge(long id
)
533 queue_post(&usb_queue
, id
, 0);
538 /* We assume that the USB cable is extracted */
539 usb_state
= USB_EXTRACTED
;
540 #ifdef USB_DETECT_BY_DRV
541 last_usb_status
= USB_UNPOWERED
;
543 last_usb_status
= USB_EXTRACTED
;
545 usb_monitor_enabled
= false;
548 exclusive_storage_access
= false;
551 #ifdef USB_FIREWIRE_HANDLING
552 firewire_countdown
= -1;
553 last_firewire_status
= false;
560 queue_init(&usb_queue
, true);
562 usb_thread_entry
= create_thread(usb_thread
, usb_stack
,
563 sizeof(usb_stack
), 0, usb_thread_name
564 IF_PRIO(, PRIORITY_SYSTEM
) IF_COP(, CPU
));
566 #ifndef USB_STATUS_BY_EVENT
568 tick_add_task(usb_tick
);
570 #endif /* USB_FULL_INIT */
573 void usb_wait_for_disconnect(struct event_queue
*q
)
576 struct queue_event ev
;
578 /* Don't return until we get SYS_USB_DISCONNECTED */
582 if(ev
.id
== SYS_USB_DISCONNECTED
)
584 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
590 #endif /* USB_FULL_INIT */
593 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
596 struct queue_event ev
;
598 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
601 queue_wait_w_tmo(q
, &ev
, ticks
);
604 case SYS_USB_DISCONNECTED
:
605 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
612 (void)q
; (void)ticks
;
614 #endif /* USB_FULL_INIT */
617 #ifdef USB_DRIVER_CLOSE
620 unsigned int thread
= usb_thread_entry
;
621 usb_thread_entry
= 0;
626 #ifndef USB_STATUS_BY_EVENT
627 tick_remove_task(usb_tick
);
629 usb_monitor_enabled
= false;
631 queue_post(&usb_queue
, USB_QUIT
, 0);
634 #endif /* USB_DRIVER_CLOSE */
636 bool usb_inserted(void)
638 #ifdef HAVE_USB_POWER
639 return usb_state
== USB_INSERTED
|| usb_state
== USB_POWERED
;
641 return usb_state
== USB_INSERTED
;
646 bool usb_exclusive_storage(void)
648 return exclusive_storage_access
;
652 #ifdef HAVE_USB_POWER
653 bool usb_powered(void)
655 return usb_state
== USB_POWERED
;
662 bool usb_inserted(void)
668 /* Dummy simulator functions */
669 void usb_acknowledge(long id
)
678 void usb_start_monitoring(void)
684 return USB_EXTRACTED
;
687 void usb_wait_for_disconnect(struct event_queue
*q
)
692 #endif /* USB_NONE or SIMULATOR */