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 * 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 ****************************************************************************/
39 #include "usb-target.h"
43 #ifdef IRIVER_H300_SERIES
44 #include "pcf50606.h" /* for pcf50606_usb_charging_... */
48 /* Conditions under which we want the entire driver */
49 #if !defined(BOOTLOADER) || \
50 (defined(TOSHIBA_GIGABEAT_S) && defined(USE_ROCKBOX_USB) && defined(USB_STORAGE)) || \
51 (defined(CREATIVE_ZVM) && defined(HAVE_USBSTACK))
55 extern void dbg_ports(void); /* NASTY! defined in apps/ */
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) {}
62 void screen_dump(void); /* Nasty again. Defined in apps/ too */
66 #if !defined(SIMULATOR) && !defined(USB_NONE)
68 #define NUM_POLL_READINGS (HZ/5)
73 #if defined(HAVE_MMC) && defined(USB_FULL_INIT)
74 static int usb_mmc_countdown
= 0;
77 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
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
;
84 static struct event_queue usb_queue
;
85 static int last_usb_status
;
86 static bool usb_monitor_enabled
;
88 static bool exclusive_ata_access
;
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
;
100 static void usb_slave_mode(bool on
)
106 DEBUGF("Entering USB slave mode\n");
114 DEBUGF("Leaving USB slave mode\n");
116 /* Let the ISDx00 settle */
124 /* fixme: can we remove this? (already such in main.c) */
127 snprintf(str
, 31, "ATA error: %d", rc
);
129 lcd_puts(0, 1, "Press ON to debug");
131 while(!(button_get(true) & BUTTON_REL
)) {};
133 panicf("ata: %d",rc
);
136 rc
= disk_mount_all();
137 if (rc
<= 0) /* no partition */
138 panicf("mount: %d",rc
);
144 static void try_reboot(void)
146 #ifndef HAVE_FLASH_STORAGE
147 ata_sleepnow(); /* Immediately spindown the disk. */
151 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
152 #if CONFIG_CPU == PP5020
153 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
154 #elif CONFIG_CPU == PP5022
155 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
156 #endif /* CONFIG_CPU */
157 #endif /* IPOD_ARCH */
159 system_reboot(); /* Reboot */
162 static void usb_thread(void)
164 int num_acks_to_expect
= -1;
165 bool waiting_for_ack
;
166 struct queue_event ev
;
168 waiting_for_ack
= false;
172 queue_wait(&usb_queue
, &ev
);
176 case USB_TRANSFER_COMPLETION
:
177 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data
*)ev
.data
);
180 #ifdef HAVE_USB_POWER
182 usb_state
= USB_POWERED
;
186 #ifdef HAVE_LCD_BITMAP
187 if(do_screendump_instead_of_usb
)
193 #ifdef HAVE_USB_POWER
194 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
195 if((button_status() & ~USBPOWER_BTN_IGNORE
) != USBPOWER_BUTTON
)
197 if((button_status() & ~USBPOWER_BTN_IGNORE
) == USBPOWER_BUTTON
)
200 usb_state
= USB_POWERED
;
202 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE
,false);
203 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY
,true);
211 /* Set the state to USB_POWERED for now. if a real
212 connection is detected it will switch to USB_INSERTED */
213 usb_state
= USB_POWERED
;
214 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE
,true);
215 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY
,false);
218 /* Tell all threads that they have to back off the ATA.
219 We subtract one for our own thread. */
221 queue_broadcast(SYS_USB_CONNECTED
, 0) - 1;
222 waiting_for_ack
= true;
223 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
229 case USB_REQUEST_DISK
:
232 /* Tell all threads that they have to back off the ATA.
233 We subtract one for our own thread. */
235 queue_broadcast(SYS_USB_CONNECTED
, 0) - 1;
236 waiting_for_ack
= true;
237 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
241 case USB_RELEASE_DISK
:
244 /* Tell all threads that they have to back off the ATA.
245 We subtract one for our own thread. */
247 queue_broadcast(SYS_USB_DISCONNECTED
, 0) - 1;
248 waiting_for_ack
= true;
249 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
254 case SYS_USB_CONNECTED_ACK
:
257 num_acks_to_expect
--;
258 if(num_acks_to_expect
== 0)
260 DEBUGF("All threads have acknowledged the connect.\n");
262 #ifndef USE_ROCKBOX_USB
263 /* until we have native mass-storage mode, we want to reboot on
267 #endif /* USE_ROCKBOX_USB */
268 #ifdef HAVE_PRIORITY_SCHEDULING
269 thread_set_priority(usb_thread_entry
,PRIORITY_REALTIME
);
271 exclusive_ata_access
= true;
274 usb_slave_mode(true);
277 usb_state
= USB_INSERTED
;
278 waiting_for_ack
= false;
282 DEBUGF("usb: got ack, %d to go...\n",
291 #ifdef HAVE_PRIORITY_SCHEDULING
292 thread_set_priority(usb_thread_entry
,PRIORITY_SYSTEM
);
295 #ifdef HAVE_LCD_BITMAP
296 if(do_screendump_instead_of_usb
)
299 #ifdef HAVE_USB_POWER
300 if(usb_state
== USB_POWERED
)
302 usb_state
= USB_EXTRACTED
;
306 #ifndef HAVE_USBSTACK
307 if(usb_state
== USB_INSERTED
)
309 /* Only disable the USB mode if we really have enabled it
310 some threads might not have acknowledged the
312 usb_slave_mode(false);
313 cpu_idle_mode(false);
317 usb_state
= USB_EXTRACTED
;
319 if(exclusive_ata_access
)
321 exclusive_ata_access
= false;
323 /* Tell all threads that we are back in business */
325 queue_broadcast(SYS_USB_DISCONNECTED
, 0) - 1;
326 waiting_for_ack
= true;
327 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
334 case SYS_USB_DISCONNECTED_ACK
:
337 num_acks_to_expect
--;
338 if(num_acks_to_expect
== 0)
340 DEBUGF("All threads have acknowledged. "
341 "We're in business.\n");
342 waiting_for_ack
= false;
346 DEBUGF("usb: got ack, %d to go...\n",
353 case SYS_HOTSWAP_INSERTED
:
354 case SYS_HOTSWAP_EXTRACTED
:
356 usb_core_hotswap_event(1,ev
.id
== SYS_HOTSWAP_INSERTED
);
358 if(usb_state
== USB_INSERTED
)
361 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
367 if(usb_state
== USB_INSERTED
)
368 usb_enable(true); /* reenable only if still inserted */
371 case USB_REQUEST_REBOOT
:
372 #ifdef HAVE_USB_POWER
373 if((button_status() & ~USBPOWER_BTN_IGNORE
) != USBPOWER_BUTTON
)
383 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data
* event_data
)
385 queue_post(&usb_queue
, USB_TRANSFER_COMPLETION
, (intptr_t)event_data
);
390 static void usb_tick(void)
394 if(usb_monitor_enabled
)
396 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
397 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
398 int current_firewire_status
= firewire_detect();
399 if(current_firewire_status
!= last_firewire_status
)
401 last_firewire_status
= current_firewire_status
;
402 firewire_countdown
= NUM_POLL_READINGS
;
406 /* Count down until it gets negative */
407 if(firewire_countdown
>= 0)
408 firewire_countdown
--;
410 /* Report to the thread if we have had 3 identical status
412 if(firewire_countdown
== 0)
414 queue_post(&usb_queue
, USB_REQUEST_REBOOT
, 0);
419 current_status
= usb_detect();
421 /* Only report when the status has changed */
422 if(current_status
!= last_usb_status
)
424 last_usb_status
= current_status
;
425 countdown
= NUM_POLL_READINGS
;
429 /* Count down until it gets negative */
433 /* Report to the thread if we have had 3 identical status
437 queue_post(&usb_queue
, current_status
, 0);
442 if(usb_mmc_countdown
> 0)
445 if (usb_mmc_countdown
== 0)
446 queue_post(&usb_queue
, USB_REENABLE
, 0);
452 void usb_acknowledge(long id
)
454 queue_post(&usb_queue
, id
, 0);
459 usb_state
= USB_EXTRACTED
;
461 exclusive_ata_access
= false;
463 usb_monitor_enabled
= false;
466 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
467 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
468 firewire_countdown
= -1;
469 last_firewire_status
= false;
477 /* We assume that the USB cable is extracted */
478 last_usb_status
= USB_EXTRACTED
;
481 queue_init(&usb_queue
, true);
483 usb_thread_entry
= create_thread(usb_thread
, usb_stack
,
484 sizeof(usb_stack
), 0, usb_thread_name
485 IF_PRIO(, PRIORITY_SYSTEM
) IF_COP(, CPU
));
487 tick_add_task(usb_tick
);
492 void usb_wait_for_disconnect(struct event_queue
*q
)
494 struct queue_event ev
;
496 /* Don't return until we get SYS_USB_DISCONNECTED */
500 if(ev
.id
== SYS_USB_DISCONNECTED
)
502 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
508 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
510 struct queue_event ev
;
512 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
515 queue_wait_w_tmo(q
, &ev
, ticks
);
518 case SYS_USB_DISCONNECTED
:
519 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
529 void usb_start_monitoring(void)
531 usb_monitor_enabled
= true;
534 #ifdef TOSHIBA_GIGABEAT_S
535 void usb_stop_monitoring(void)
537 tick_remove_task(usb_tick
);
538 usb_monitor_enabled
= false;
542 bool usb_inserted(void)
544 #ifdef HAVE_USB_POWER
545 return usb_state
== USB_INSERTED
|| usb_state
== USB_POWERED
;
547 return usb_state
== USB_INSERTED
;
552 void usb_request_exclusive_ata(void)
554 /* This is not really a clean place to start boosting the cpu. but it's
555 * currently the best one. We want to get rid of having to boost the cpu
558 if(!exclusive_ata_access
) {
559 queue_post(&usb_queue
, USB_REQUEST_DISK
, 0);
563 void usb_release_exclusive_ata(void)
566 if(exclusive_ata_access
) {
567 queue_post(&usb_queue
, USB_RELEASE_DISK
, 0);
568 exclusive_ata_access
= false;
572 bool usb_exclusive_ata(void)
574 return exclusive_ata_access
;
578 #ifdef HAVE_USB_POWER
579 bool usb_powered(void)
581 return usb_state
== USB_POWERED
;
585 bool usb_charging_enable(bool on
)
588 #ifdef IRIVER_H300_SERIES
590 logf("usb_charging_enable(%s)\n", on
? "on" : "off" );
591 irqlevel
= disable_irq_save();
592 pcf50606_set_usb_charging(on
);
594 restore_irq(irqlevel
);
596 /* TODO: implement it for other targets... */
602 bool usb_charging_enabled(void)
605 #ifdef IRIVER_H300_SERIES
606 /* TODO: read the state of the GPOOD2 register...
607 * (this also means to set the irq level here) */
608 rc
= pcf50606_usb_charging_enabled();
610 /* TODO: implement it for other targets... */
613 logf("usb charging %s", rc
? "enabled" : "disabled" );
622 bool usb_inserted(void)
628 /* Dummy simulator functions */
629 void usb_acknowledge(long id
)
638 void usb_start_monitoring(void)
644 return USB_EXTRACTED
;
647 void usb_wait_for_disconnect(struct event_queue
*q
)
652 #endif /* USB_NONE or SIMULATOR */