fix some small glitches for backup:
[Rockbox.git] / firmware / usb.c
blob90991e25fd630b031873d2a6848e82c104fb1b19
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 * 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 ****************************************************************************/
24 #include "config.h"
25 #include "cpu.h"
26 #include "kernel.h"
27 #include "thread.h"
28 #include "system.h"
29 #include "debug.h"
30 #include "ata.h"
31 #include "fat.h"
32 #include "disk.h"
33 #include "panic.h"
34 #include "lcd.h"
35 #include "usb.h"
36 #include "button.h"
37 #include "sprintf.h"
38 #include "string.h"
39 #include "usb-target.h"
40 #ifdef HAVE_USBSTACK
41 #include "usb_core.h"
42 #endif
43 #ifdef IRIVER_H300_SERIES
44 #include "pcf50606.h" /* for pcf50606_usb_charging_... */
45 #endif
46 #include "logf.h"
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))
52 #define USB_FULL_INIT
53 #endif
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) {}
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)
124 /* fixme: can we remove this? (already such in main.c) */
125 char str[32];
126 lcd_clear_display();
127 snprintf(str, 31, "ATA error: %d", rc);
128 lcd_puts(0, 0, str);
129 lcd_puts(0, 1, "Press ON to debug");
130 lcd_update();
131 while(!(button_get(true) & BUTTON_REL)) {};
132 dbg_ports();
133 panicf("ata: %d",rc);
136 rc = disk_mount_all();
137 if (rc <= 0) /* no partition */
138 panicf("mount: %d",rc);
142 #endif
144 static void try_reboot(void)
146 #ifndef HAVE_FLASH_STORAGE
147 ata_sleepnow(); /* Immediately spindown the disk. */
148 sleep(HZ*2);
149 #endif
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;
170 while(1)
172 queue_wait(&usb_queue, &ev);
173 switch(ev.id)
175 #ifdef USB_DRIVER_CLOSE
176 case USB_QUIT:
177 return;
178 #endif
179 #ifdef HAVE_USBSTACK
180 case USB_TRANSFER_COMPLETION:
181 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
182 break;
183 #endif
184 #ifdef HAVE_USB_POWER
185 case USB_POWERED:
186 usb_state = USB_POWERED;
187 break;
188 #endif
189 case USB_INSERTED:
190 #ifdef HAVE_LCD_BITMAP
191 if(do_screendump_instead_of_usb)
193 screen_dump();
195 else
196 #endif
197 #ifdef HAVE_USB_POWER
198 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
199 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
200 #else
201 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
202 #endif
204 usb_state = USB_POWERED;
205 #ifdef HAVE_USBSTACK
206 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,false);
207 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,true);
208 usb_enable(true);
209 #endif
211 else
212 #endif
214 #ifdef HAVE_USBSTACK
215 /* Set the state to USB_POWERED for now. if a real
216 connection is detected it will switch to USB_INSERTED */
217 usb_state = USB_POWERED;
218 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
219 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,false);
220 usb_enable(true);
221 #else
222 /* Tell all threads that they have to back off the ATA.
223 We subtract one for our own thread. */
224 num_acks_to_expect =
225 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
226 waiting_for_ack = true;
227 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
228 num_acks_to_expect);
229 #endif
231 break;
232 #ifdef HAVE_USBSTACK
233 case USB_REQUEST_DISK:
234 if(!waiting_for_ack)
236 /* Tell all threads that they have to back off the ATA.
237 We subtract one for our own thread. */
238 num_acks_to_expect =
239 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
240 waiting_for_ack = true;
241 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
242 num_acks_to_expect);
244 break;
245 case USB_RELEASE_DISK:
246 if(!waiting_for_ack)
248 /* Tell all threads that they have to back off the ATA.
249 We subtract one for our own thread. */
250 num_acks_to_expect =
251 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
252 waiting_for_ack = true;
253 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
254 num_acks_to_expect);
256 break;
257 #endif
258 case SYS_USB_CONNECTED_ACK:
259 if(waiting_for_ack)
261 num_acks_to_expect--;
262 if(num_acks_to_expect == 0)
264 DEBUGF("All threads have acknowledged the connect.\n");
265 #ifdef HAVE_USBSTACK
266 #ifndef USE_ROCKBOX_USB
267 /* until we have native mass-storage mode, we want to reboot on
268 usb host connect */
269 usb_enable(true);
270 try_reboot();
271 #endif /* USE_ROCKBOX_USB */
272 #ifdef HAVE_PRIORITY_SCHEDULING
273 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
274 #endif
275 exclusive_ata_access = true;
277 #else
278 usb_slave_mode(true);
279 cpu_idle_mode(true);
280 #endif
281 usb_state = USB_INSERTED;
282 waiting_for_ack = false;
284 else
286 DEBUGF("usb: got ack, %d to go...\n",
287 num_acks_to_expect);
290 break;
292 case USB_EXTRACTED:
293 #ifdef HAVE_USBSTACK
294 usb_enable(false);
295 #ifdef HAVE_PRIORITY_SCHEDULING
296 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
297 #endif
298 #endif
299 #ifdef HAVE_LCD_BITMAP
300 if(do_screendump_instead_of_usb)
301 break;
302 #endif
303 #ifdef HAVE_USB_POWER
304 if(usb_state == USB_POWERED)
306 usb_state = USB_EXTRACTED;
307 break;
309 #endif
310 #ifndef HAVE_USBSTACK
311 if(usb_state == USB_INSERTED)
313 /* Only disable the USB mode if we really have enabled it
314 some threads might not have acknowledged the
315 insertion */
316 usb_slave_mode(false);
317 cpu_idle_mode(false);
319 #endif
321 usb_state = USB_EXTRACTED;
322 #ifdef HAVE_USBSTACK
323 if(exclusive_ata_access)
325 exclusive_ata_access = false;
326 #endif
327 /* Tell all threads that we are back in business */
328 num_acks_to_expect =
329 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
330 waiting_for_ack = true;
331 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
332 num_acks_to_expect);
333 #ifdef HAVE_USBSTACK
335 #endif
336 break;
338 case SYS_USB_DISCONNECTED_ACK:
339 if(waiting_for_ack)
341 num_acks_to_expect--;
342 if(num_acks_to_expect == 0)
344 DEBUGF("All threads have acknowledged. "
345 "We're in business.\n");
346 waiting_for_ack = false;
348 else
350 DEBUGF("usb: got ack, %d to go...\n",
351 num_acks_to_expect);
354 break;
356 #ifdef HAVE_HOTSWAP
357 case SYS_HOTSWAP_INSERTED:
358 case SYS_HOTSWAP_EXTRACTED:
359 #ifdef HAVE_USBSTACK
360 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
361 #else
362 if(usb_state == USB_INSERTED)
364 usb_enable(false);
365 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
367 break;
368 #endif
370 case USB_REENABLE:
371 if(usb_state == USB_INSERTED)
372 usb_enable(true); /* reenable only if still inserted */
373 break;
374 #endif
375 case USB_REQUEST_REBOOT:
376 #ifdef HAVE_USB_POWER
377 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
378 #endif
379 try_reboot();
380 break;
384 #endif
386 #ifdef HAVE_USBSTACK
387 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
389 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
391 #endif
393 #ifdef USB_FULL_INIT
394 static void usb_tick(void)
396 int current_status;
398 if(usb_monitor_enabled)
400 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
401 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
402 int current_firewire_status = firewire_detect();
403 if(current_firewire_status != last_firewire_status)
405 last_firewire_status = current_firewire_status;
406 firewire_countdown = NUM_POLL_READINGS;
408 else
410 /* Count down until it gets negative */
411 if(firewire_countdown >= 0)
412 firewire_countdown--;
414 /* Report to the thread if we have had 3 identical status
415 readings in a row */
416 if(firewire_countdown == 0)
418 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
421 #endif
423 current_status = usb_detect();
425 /* Only report when the status has changed */
426 if(current_status != last_usb_status)
428 last_usb_status = current_status;
429 countdown = NUM_POLL_READINGS;
431 else
433 /* Count down until it gets negative */
434 if(countdown >= 0)
435 countdown--;
437 /* Report to the thread if we have had 3 identical status
438 readings in a row */
439 if(countdown == 0)
441 queue_post(&usb_queue, current_status, 0);
445 #ifdef HAVE_MMC
446 if(usb_mmc_countdown > 0)
448 usb_mmc_countdown--;
449 if (usb_mmc_countdown == 0)
450 queue_post(&usb_queue, USB_REENABLE, 0);
452 #endif
454 #endif
456 void usb_acknowledge(long id)
458 queue_post(&usb_queue, id, 0);
461 void usb_init(void)
463 usb_state = USB_EXTRACTED;
464 #ifdef HAVE_USBSTACK
465 exclusive_ata_access = false;
466 #endif
467 usb_monitor_enabled = false;
468 countdown = -1;
470 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
471 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
472 firewire_countdown = -1;
473 last_firewire_status = false;
474 #endif
476 usb_init_device();
477 #ifdef USB_FULL_INIT
478 usb_enable(false);
479 #endif
481 /* We assume that the USB cable is extracted */
482 last_usb_status = USB_EXTRACTED;
484 #ifdef USB_FULL_INIT
485 queue_init(&usb_queue, true);
487 usb_thread_entry = create_thread(usb_thread, usb_stack,
488 sizeof(usb_stack), 0, usb_thread_name
489 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
491 tick_add_task(usb_tick);
492 #endif
496 void usb_wait_for_disconnect(struct event_queue *q)
498 struct queue_event ev;
500 /* Don't return until we get SYS_USB_DISCONNECTED */
501 while(1)
503 queue_wait(q, &ev);
504 if(ev.id == SYS_USB_DISCONNECTED)
506 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
507 return;
512 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
514 struct queue_event ev;
516 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
517 while(1)
519 queue_wait_w_tmo(q, &ev, ticks);
520 switch(ev.id)
522 case SYS_USB_DISCONNECTED:
523 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
524 return 0;
525 break;
526 case SYS_TIMEOUT:
527 return 1;
528 break;
533 void usb_start_monitoring(void)
535 usb_monitor_enabled = true;
538 #ifdef USB_DRIVER_CLOSE
539 void usb_close(void)
541 struct thread_entry *thread = usb_thread_entry;
542 usb_thread_entry = NULL;
544 if (thread == NULL)
545 return;
547 tick_remove_task(usb_tick);
548 usb_monitor_enabled = false;
550 queue_post(&usb_queue, USB_QUIT, 0);
551 thread_wait(thread);
553 #endif /* USB_DRIVER_CLOSE */
555 bool usb_inserted(void)
557 #ifdef HAVE_USB_POWER
558 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
559 #else
560 return usb_state == USB_INSERTED;
561 #endif
564 #ifdef HAVE_USBSTACK
565 void usb_request_exclusive_ata(void)
567 /* This is not really a clean place to start boosting the cpu. but it's
568 * currently the best one. We want to get rid of having to boost the cpu
569 * for usb anyway */
570 trigger_cpu_boost();
571 if(!exclusive_ata_access) {
572 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
576 void usb_release_exclusive_ata(void)
578 cancel_cpu_boost();
579 if(exclusive_ata_access) {
580 queue_post(&usb_queue, USB_RELEASE_DISK, 0);
581 exclusive_ata_access = false;
585 bool usb_exclusive_ata(void)
587 return exclusive_ata_access;
589 #endif
591 #ifdef HAVE_USB_POWER
592 bool usb_powered(void)
594 return usb_state == USB_POWERED;
597 #if CONFIG_CHARGING
598 bool usb_charging_enable(bool on)
600 bool rc = false;
601 #ifdef IRIVER_H300_SERIES
602 int irqlevel;
603 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
604 irqlevel = disable_irq_save();
605 pcf50606_set_usb_charging(on);
606 rc = on;
607 restore_irq(irqlevel);
608 #else
609 /* TODO: implement it for other targets... */
610 (void)on;
611 #endif
612 return rc;
615 bool usb_charging_enabled(void)
617 bool rc = false;
618 #ifdef IRIVER_H300_SERIES
619 /* TODO: read the state of the GPOOD2 register...
620 * (this also means to set the irq level here) */
621 rc = pcf50606_usb_charging_enabled();
622 #else
623 /* TODO: implement it for other targets... */
624 #endif
626 logf("usb charging %s", rc ? "enabled" : "disabled" );
627 return rc;
629 #endif
630 #endif
632 #else
634 #ifdef USB_NONE
635 bool usb_inserted(void)
637 return false;
639 #endif
641 /* Dummy simulator functions */
642 void usb_acknowledge(long id)
644 id = id;
647 void usb_init(void)
651 void usb_start_monitoring(void)
655 int usb_detect(void)
657 return USB_EXTRACTED;
660 void usb_wait_for_disconnect(struct event_queue *q)
662 (void)q;
665 #endif /* USB_NONE or SIMULATOR */