Of course there's no mdelay on c200(v1), so just use udelay
[kugel-rb.git] / firmware / usb.c
blobbaf6af7d698ae840cef25dff82caf805ae581cf6
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 "storage.h"
33 #include "fat.h"
34 #include "disk.h"
35 #include "panic.h"
36 #include "lcd.h"
37 #include "usb-target.h"
38 #include "usb.h"
39 #include "button.h"
40 #include "string.h"
41 #ifdef HAVE_USBSTACK
42 #include "usb_core.h"
43 #endif
44 #include "logf.h"
45 #include "screendump.h"
47 /* Conditions under which we want the entire driver */
48 #if !defined(BOOTLOADER) || (CONFIG_CPU == SH7034) || \
49 (defined(TOSHIBA_GIGABEAT_S) && defined(USE_ROCKBOX_USB) && defined(USB_ENABLE_STORAGE)) || \
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 #define USB_FULL_INIT
57 #endif
59 #ifdef HAVE_LCD_BITMAP
60 bool do_screendump_instead_of_usb = false;
61 #endif
63 #if !defined(SIMULATOR) && !defined(USB_NONE)
65 static int usb_state;
67 #if (CONFIG_STORAGE & STORAGE_MMC) && defined(USB_FULL_INIT)
68 static int usb_mmc_countdown = 0;
69 #endif
71 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
72 needs updating */
73 #ifdef USB_FULL_INIT
74 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
75 static const char usb_thread_name[] = "usb";
76 static unsigned int usb_thread_entry = 0;
77 #ifndef USB_STATUS_BY_EVENT
78 #define NUM_POLL_READINGS (HZ/5)
79 static int countdown;
80 #endif /* USB_STATUS_BY_EVENT */
81 #endif /* USB_FULL_INIT */
82 static struct event_queue usb_queue;
83 static bool usb_monitor_enabled;
84 static int last_usb_status;
85 #ifdef HAVE_USBSTACK
86 static bool exclusive_storage_access;
87 #endif
89 #ifdef USB_FIREWIRE_HANDLING
90 static int firewire_countdown;
91 static bool last_firewire_status;
92 #endif
94 #ifdef USB_FULL_INIT
96 #if defined(USB_FIREWIRE_HANDLING) \
97 || (defined(HAVE_USBSTACK) && !defined(USE_ROCKBOX_USB))
98 static void try_reboot(void)
100 #ifdef HAVE_DISK_STORAGE
101 storage_sleepnow(); /* Immediately spindown the disk. */
102 sleep(HZ*2);
103 #endif
105 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
106 #if CONFIG_CPU == PP5020
107 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
108 #elif CONFIG_CPU == PP5022
109 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
110 #endif /* CONFIG_CPU */
111 #endif /* IPOD_ARCH */
113 #ifdef IPOD_NANO2G
114 memcpy((void *)0x0002bf00, "diskmodehotstuff\1\0\0\0", 20);
115 #endif
117 system_reboot(); /* Reboot */
119 #endif /* USB_FIRWIRE_HANDLING || (HAVE_USBSTACK && !USE_ROCKBOX_USB) */
121 #ifdef HAVE_USBSTACK
122 /* inline since branch is chosen at compile time */
123 static inline void usb_slave_mode(bool on)
125 #ifdef USE_ROCKBOX_USB
126 int rc;
128 if (on)
130 trigger_cpu_boost();
131 #ifdef HAVE_PRIORITY_SCHEDULING
132 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_REALTIME);
133 #endif
134 usb_attach();
136 else /* usb_state == USB_INSERTED (only!) */
138 #ifndef USB_DETECT_BY_DRV
139 usb_enable(false);
140 #endif
141 #ifdef HAVE_PRIORITY_SCHEDULING
142 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_SYSTEM);
143 #endif
144 /* Entered exclusive mode */
145 rc = disk_mount_all();
146 if (rc <= 0) /* no partition */
147 panicf("mount: %d",rc);
149 cancel_cpu_boost();
151 #else /* !USE_ROCKBOX_USB */
152 if (on)
154 /* until we have native mass-storage mode, we want to reboot on
155 usb host connect */
156 try_reboot();
158 #endif /* USE_ROCKBOX_USB */
161 void usb_signal_transfer_completion(
162 struct usb_transfer_completion_event_data* event_data)
164 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
166 #else /* !HAVE_USBSTACK */
167 /* inline since branch is chosen at compile time */
168 static inline void usb_slave_mode(bool on)
170 int rc;
172 if(on)
174 DEBUGF("Entering USB slave mode\n");
175 storage_soft_reset();
176 storage_init();
177 storage_enable(false);
178 usb_enable(true);
179 cpu_idle_mode(true);
181 else
183 DEBUGF("Leaving USB slave mode\n");
185 cpu_idle_mode(false);
187 /* Let the ISDx00 settle */
188 sleep(HZ*1);
190 usb_enable(false);
192 rc = storage_init();
193 if(rc)
194 panicf("storage: %d",rc);
196 rc = disk_mount_all();
197 if (rc <= 0) /* no partition */
198 panicf("mount: %d",rc);
201 #endif /* HAVE_USBSTACK */
203 #ifdef HAVE_USB_POWER
204 static inline bool usb_power_button(void)
206 #if (defined(IRIVER_H10) || defined (IRIVER_H10_5GB)) && !defined(USE_ROCKBOX_USB)
207 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
208 #else
209 return (button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON;
210 #endif
213 #ifdef USB_FIREWIRE_HANDLING
214 static inline bool usb_reboot_button(void)
216 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
218 #endif
219 #endif /* HAVE_USB_POWER */
221 #ifdef USB_ENABLE_HID
222 static bool usb_hid = true;
223 #endif
225 static void usb_thread(void)
227 int num_acks_to_expect = 0;
228 struct queue_event ev;
230 while(1)
232 queue_wait(&usb_queue, &ev);
233 switch(ev.id)
235 #ifdef USB_DRIVER_CLOSE
236 case USB_QUIT:
237 return;
238 #endif
239 #ifdef HAVE_USBSTACK
240 case USB_TRANSFER_COMPLETION:
241 usb_core_handle_transfer_completion(
242 (struct usb_transfer_completion_event_data*)ev.data);
243 break;
244 #endif
245 #ifdef USB_DETECT_BY_DRV
246 /* In this case, these events the handle cable insertion USB
247 * driver determines INSERTED/EXTRACTED state. */
248 case USB_POWERED:
249 /* Set the state to USB_POWERED for now and enable the driver
250 * to detect a bus reset only. If a bus reset is detected,
251 * USB_INSERTED will be received. */
252 usb_state = USB_POWERED;
253 usb_enable(true);
254 break;
256 case USB_UNPOWERED:
257 usb_enable(false);
258 /* This part shouldn't be obligatory for anything that can
259 * reliably detect removal of the data lines. USB_EXTRACTED
260 * could be posted on that event while bus power remains
261 * available. */
262 queue_post(&usb_queue, USB_EXTRACTED, 0);
263 break;
264 #endif /* USB_DETECT_BY_DRV */
265 case USB_INSERTED:
266 #ifdef HAVE_LCD_BITMAP
267 if(do_screendump_instead_of_usb)
269 usb_state = USB_SCREENDUMP;
270 screen_dump();
271 #ifdef HAVE_REMOTE_LCD
272 remote_screen_dump();
273 #endif
274 break;
276 #endif
277 #ifdef HAVE_USB_POWER
278 if(usb_power_button())
280 /* Only charging is desired */
281 usb_state = USB_POWERED;
282 #ifdef HAVE_USBSTACK
283 #ifdef USB_ENABLE_STORAGE
284 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, false);
285 #endif
287 #ifdef USB_ENABLE_HID
288 #ifdef USB_ENABLE_CHARGING_ONLY
289 usb_core_enable_driver(USB_DRIVER_HID, false);
290 #else
291 usb_core_enable_driver(USB_DRIVER_HID, true);
292 #endif /* USB_ENABLE_CHARGING_ONLY */
293 #endif /* USB_ENABLE_HID */
295 #ifdef USB_ENABLE_CHARGING_ONLY
296 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, true);
297 #endif
298 usb_attach();
299 #endif
300 break;
302 #endif /* HAVE_USB_POWER */
303 #ifdef HAVE_USBSTACK
304 #ifdef HAVE_USB_POWER
305 /* Set the state to USB_POWERED for now. If permission to connect
306 * by threads and storage is granted it will be changed to
307 * USB_CONNECTED. */
308 usb_state = USB_POWERED;
309 #endif
310 #ifdef USB_ENABLE_STORAGE
311 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, true);
312 #endif
313 #ifdef USB_ENABLE_HID
314 usb_core_enable_driver(USB_DRIVER_HID, usb_hid);
315 #endif
316 #ifdef USB_ENABLE_CHARGING_ONLY
317 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, false);
318 #endif
320 /* Check any drivers enabled at this point for exclusive storage
321 * access requirements. */
322 exclusive_storage_access = usb_core_any_exclusive_storage();
324 if(!exclusive_storage_access)
326 usb_attach();
327 break;
329 #endif /* HAVE_USBSTACK */
330 /* Tell all threads that they have to back off the storage.
331 We subtract one for our own thread. */
332 num_acks_to_expect = queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
333 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
334 num_acks_to_expect);
335 break;
337 case SYS_USB_CONNECTED_ACK:
338 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
340 DEBUGF("All threads have acknowledged the connect.\n");
341 usb_slave_mode(true);
342 usb_state = USB_INSERTED;
344 else
346 DEBUGF("usb: got ack, %d to go...\n",
347 num_acks_to_expect);
349 break;
351 case USB_EXTRACTED:
352 #ifdef HAVE_LCD_BITMAP
353 if(usb_state == USB_SCREENDUMP)
355 usb_state = USB_EXTRACTED;
356 break; /* Connected for screendump only */
358 #endif
359 #ifndef HAVE_USBSTACK /* Stack must undo this if POWERED state was transitional */
360 #ifdef HAVE_USB_POWER
361 if(usb_state == USB_POWERED)
363 usb_state = USB_EXTRACTED;
364 break;
366 #endif
367 #endif /* HAVE_USBSTACK */
368 if(usb_state == USB_INSERTED)
370 /* Only disable the USB mode if we really have enabled it
371 some threads might not have acknowledged the
372 insertion */
373 usb_slave_mode(false);
376 usb_state = USB_EXTRACTED;
377 #ifdef HAVE_USBSTACK
378 if(!exclusive_storage_access)
380 #ifndef USB_DETECT_BY_DRV /* Disabled handling USB_UNPOWERED */
381 usb_enable(false);
382 #endif
383 break;
386 #endif /* HAVE_USBSTACK */
387 num_acks_to_expect = usb_release_exclusive_storage();
389 break;
391 case SYS_USB_DISCONNECTED_ACK:
392 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
394 DEBUGF("All threads have acknowledged. "
395 "We're in business.\n");
397 else
399 DEBUGF("usb: got ack, %d to go...\n",
400 num_acks_to_expect);
402 break;
404 #ifdef HAVE_HOTSWAP
405 case SYS_HOTSWAP_INSERTED:
406 case SYS_HOTSWAP_EXTRACTED:
407 #ifdef HAVE_USBSTACK
408 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
409 #else /* !HAVE_USBSTACK */
410 if(usb_state == USB_INSERTED)
412 #if (CONFIG_STORAGE & STORAGE_MMC)
413 usb_enable(false);
414 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
415 #endif /* STORAGE_MMC */
417 #endif /* HAVE_USBSTACK */
418 break;
420 #if (CONFIG_STORAGE & STORAGE_MMC)
421 case USB_REENABLE:
422 if(usb_state == USB_INSERTED)
423 usb_enable(true); /* reenable only if still inserted */
424 break;
425 #endif /* STORAGE_MMC */
426 #endif /* HAVE_HOTSWAP */
428 #ifdef USB_FIREWIRE_HANDLING
429 case USB_REQUEST_REBOOT:
430 #ifdef HAVE_USB_POWER
431 if (usb_reboot_button())
432 #endif
433 try_reboot();
434 break;
435 #endif /* USB_FIREWIRE_HANDLING */
437 #if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
438 case USB_CHARGER_UPDATE:
439 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
440 break;
441 #endif
446 #if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
447 void usb_charger_update(void)
449 queue_post(&usb_queue, USB_CHARGER_UPDATE, 0);
451 #endif
453 #ifdef USB_STATUS_BY_EVENT
454 void usb_status_event(int current_status)
456 /* Caller isn't expected to filter for changes in status.
457 * current_status:
458 * USB_DETECT_BY_DRV: USB_POWERED, USB_UNPOWERED, USB_INSERTED (driver)
459 * else: USB_INSERTED, USB_EXTRACTED
461 if(usb_monitor_enabled)
463 int oldstatus = disable_irq_save(); /* Dual-use function */
465 if(last_usb_status != current_status)
467 last_usb_status = current_status;
468 queue_post(&usb_queue, current_status, 0);
471 restore_irq(oldstatus);
475 void usb_start_monitoring(void)
477 int oldstatus = disable_irq_save(); /* Sync to event */
478 int status = usb_detect();
480 usb_monitor_enabled = true;
482 #ifdef USB_DETECT_BY_DRV
483 status = (status == USB_INSERTED) ? USB_POWERED : USB_UNPOWERED;
484 #endif
485 usb_status_event(status);
487 #ifdef USB_FIREWIRE_HANDLING
488 if (firewire_detect())
489 usb_firewire_connect_event();
490 #endif
492 restore_irq(oldstatus);
495 #ifdef USB_FIREWIRE_HANDLING
496 void usb_firewire_connect_event(void)
498 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
500 #endif /* USB_FIREWIRE_HANDLING */
501 #else /* !USB_STATUS_BY_EVENT */
502 static void usb_tick(void)
504 int current_status;
506 if(usb_monitor_enabled)
508 #ifdef USB_FIREWIRE_HANDLING
509 int current_firewire_status = firewire_detect();
510 if(current_firewire_status != last_firewire_status)
512 last_firewire_status = current_firewire_status;
513 firewire_countdown = NUM_POLL_READINGS;
515 else
517 /* Count down until it gets negative */
518 if(firewire_countdown >= 0)
519 firewire_countdown--;
521 /* Report to the thread if we have had 3 identical status
522 readings in a row */
523 if(firewire_countdown == 0)
525 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
528 #endif /* USB_FIREWIRE_HANDLING */
530 current_status = usb_detect();
532 /* Only report when the status has changed */
533 if(current_status != last_usb_status)
535 last_usb_status = current_status;
536 countdown = NUM_POLL_READINGS;
538 else
540 /* Count down until it gets negative */
541 if(countdown >= 0)
542 countdown--;
544 /* Report to the thread if we have had 3 identical status
545 readings in a row */
546 if(countdown == 0)
548 queue_post(&usb_queue, current_status, 0);
552 #if (CONFIG_STORAGE & STORAGE_MMC)
553 if(usb_mmc_countdown > 0)
555 usb_mmc_countdown--;
556 if (usb_mmc_countdown == 0)
557 queue_post(&usb_queue, USB_REENABLE, 0);
559 #endif
562 void usb_start_monitoring(void)
564 usb_monitor_enabled = true;
566 #endif /* USB_STATUS_BY_EVENT */
567 #endif /* USB_FULL_INIT */
569 void usb_acknowledge(long id)
571 queue_post(&usb_queue, id, 0);
574 void usb_init(void)
576 /* We assume that the USB cable is extracted */
577 usb_state = USB_EXTRACTED;
578 #ifdef USB_DETECT_BY_DRV
579 last_usb_status = USB_UNPOWERED;
580 #else
581 last_usb_status = USB_EXTRACTED;
582 #endif
583 usb_monitor_enabled = false;
585 #ifdef HAVE_USBSTACK
586 exclusive_storage_access = false;
587 #endif
589 #ifdef USB_FIREWIRE_HANDLING
590 firewire_countdown = -1;
591 last_firewire_status = false;
592 #endif
594 usb_init_device();
595 #ifdef USB_FULL_INIT
596 usb_enable(false);
598 queue_init(&usb_queue, true);
600 usb_thread_entry = create_thread(usb_thread, usb_stack,
601 sizeof(usb_stack), 0, usb_thread_name
602 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
604 #ifndef USB_STATUS_BY_EVENT
605 countdown = -1;
606 tick_add_task(usb_tick);
607 #endif
608 #endif /* USB_FULL_INIT */
611 void usb_wait_for_disconnect(struct event_queue *q)
613 #ifdef USB_FULL_INIT
614 struct queue_event ev;
616 /* Don't return until we get SYS_USB_DISCONNECTED */
617 while(1)
619 queue_wait(q, &ev);
620 if(ev.id == SYS_USB_DISCONNECTED)
622 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
623 return;
626 #else
627 (void)q;
628 #endif /* USB_FULL_INIT */
631 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
633 #ifdef USB_FULL_INIT
634 struct queue_event ev;
636 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
637 while(1)
639 queue_wait_w_tmo(q, &ev, ticks);
640 switch(ev.id)
642 case SYS_USB_DISCONNECTED:
643 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
644 return 0;
645 case SYS_TIMEOUT:
646 return 1;
649 #else
650 (void)q; (void)ticks;
651 return 0;
652 #endif /* USB_FULL_INIT */
655 #ifdef USB_DRIVER_CLOSE
656 void usb_close(void)
658 unsigned int thread = usb_thread_entry;
659 usb_thread_entry = 0;
661 if (thread == 0)
662 return;
664 #ifndef USB_STATUS_BY_EVENT
665 tick_remove_task(usb_tick);
666 #endif
667 usb_monitor_enabled = false;
669 queue_post(&usb_queue, USB_QUIT, 0);
670 thread_wait(thread);
672 #endif /* USB_DRIVER_CLOSE */
674 bool usb_inserted(void)
676 #ifdef HAVE_USB_POWER
677 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
678 #else
679 return usb_state == USB_INSERTED;
680 #endif
683 #ifdef HAVE_USBSTACK
684 bool usb_exclusive_storage(void)
686 return exclusive_storage_access;
688 #endif
690 int usb_release_exclusive_storage(void)
692 int num_acks_to_expect;
693 #ifdef HAVE_USBSTACK
694 exclusive_storage_access = false;
695 #endif /* HAVE_USBSTACK */
696 /* Tell all threads that we are back in business */
697 num_acks_to_expect =
698 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
699 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
700 num_acks_to_expect);
701 return num_acks_to_expect;
704 #ifdef HAVE_USB_POWER
705 bool usb_powered(void)
707 return usb_state == USB_POWERED;
709 #endif
711 #ifdef USB_ENABLE_HID
712 void usb_set_hid(bool enable)
714 usb_hid = enable;
715 usb_core_enable_driver(USB_DRIVER_HID, usb_hid);
717 #endif
719 #else
721 #ifdef USB_NONE
722 bool usb_inserted(void)
724 return false;
726 #endif
728 /* Dummy simulator functions */
729 void usb_acknowledge(long id)
731 id = id;
734 void usb_init(void)
738 void usb_start_monitoring(void)
742 int usb_detect(void)
744 return USB_EXTRACTED;
747 void usb_wait_for_disconnect(struct event_queue *q)
749 (void)q;
752 #endif /* USB_NONE or SIMULATOR */