Set svn:keywords for the new translation file.
[kugel-rb.git] / firmware / usb.c
blobd9d9f110319790627f0b4a5babf05928456e25f7
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(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)
57 #define USB_FULL_INIT
58 #endif
60 #ifdef HAVE_LCD_BITMAP
61 bool do_screendump_instead_of_usb = false;
62 #endif
64 #if !defined(SIMULATOR) && !defined(USB_NONE)
66 static int usb_state;
68 #if (CONFIG_STORAGE & STORAGE_MMC) && defined(USB_FULL_INIT)
69 static int usb_mmc_countdown = 0;
70 #endif
72 /* Make sure there's enough stack space for screendump */
73 #ifdef USB_FULL_INIT
74 static long usb_stack[(DEFAULT_STACK_SIZE + SECTOR_SIZE + DUMP_BMP_LINESIZE)/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 disk_unmount_all();
135 usb_attach();
137 else /* usb_state == USB_INSERTED (only!) */
139 #if !defined(USB_DETECT_BY_DRV) && !defined(USB_DETECT_BY_CORE)
140 usb_enable(false);
141 #endif
142 #ifdef HAVE_PRIORITY_SCHEDULING
143 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_SYSTEM);
144 #endif
145 /* Entered exclusive mode */
146 rc = disk_mount_all();
147 if (rc <= 0) /* no partition */
148 panicf("mount: %d",rc);
150 cancel_cpu_boost();
152 #else /* !USE_ROCKBOX_USB */
153 if (on)
155 /* until we have native mass-storage mode, we want to reboot on
156 usb host connect */
157 try_reboot();
159 #endif /* USE_ROCKBOX_USB */
162 void usb_signal_transfer_completion(
163 struct usb_transfer_completion_event_data* event_data)
165 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
167 #else /* !HAVE_USBSTACK */
168 /* inline since branch is chosen at compile time */
169 static inline void usb_slave_mode(bool on)
171 int rc;
173 if(on)
175 DEBUGF("Entering USB slave mode\n");
176 disk_unmount_all();
177 storage_soft_reset();
178 storage_init();
179 storage_enable(false);
180 usb_enable(true);
181 cpu_idle_mode(true);
183 else
185 DEBUGF("Leaving USB slave mode\n");
187 cpu_idle_mode(false);
189 /* Let the ISDx00 settle */
190 sleep(HZ*1);
192 usb_enable(false);
194 rc = storage_init();
195 if(rc)
196 panicf("storage: %d",rc);
198 rc = disk_mount_all();
199 if (rc <= 0) /* no partition */
200 panicf("mount: %d",rc);
203 #endif /* HAVE_USBSTACK */
205 #ifdef HAVE_USB_POWER
206 static inline bool usb_power_button(void)
208 #if (defined(IRIVER_H10) || defined (IRIVER_H10_5GB)) && !defined(USE_ROCKBOX_USB)
209 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
210 #else
211 return (button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON;
212 #endif
215 #ifdef USB_FIREWIRE_HANDLING
216 static inline bool usb_reboot_button(void)
218 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
220 #endif
221 #endif /* HAVE_USB_POWER */
223 #ifdef USB_ENABLE_HID
224 static bool usb_hid = true;
225 #endif
227 static void usb_thread(void)
229 int num_acks_to_expect = 0;
230 struct queue_event ev;
232 while(1)
234 queue_wait(&usb_queue, &ev);
235 switch(ev.id)
237 #ifdef USB_DRIVER_CLOSE
238 case USB_QUIT:
239 return;
240 #endif
241 #ifdef HAVE_USBSTACK
242 case USB_TRANSFER_COMPLETION:
243 usb_core_handle_transfer_completion(
244 (struct usb_transfer_completion_event_data*)ev.data);
245 break;
246 #endif
247 #if defined(USB_DETECT_BY_DRV) || defined(USB_DETECT_BY_CORE)
248 /* In this case, these events the handle cable insertion USB
249 * driver determines INSERTED/EXTRACTED state. */
250 case USB_POWERED:
251 /* Set the state to USB_POWERED for now and enable the driver
252 * to detect a bus reset only. If a bus reset is detected,
253 * USB_INSERTED will be received. */
254 usb_state = USB_POWERED;
255 usb_enable(true);
256 break;
258 case USB_UNPOWERED:
259 usb_enable(false);
260 /* This part shouldn't be obligatory for anything that can
261 * reliably detect removal of the data lines. USB_EXTRACTED
262 * could be posted on that event while bus power remains
263 * available. */
264 queue_post(&usb_queue, USB_EXTRACTED, 0);
265 break;
266 #endif /* USB_DETECT_BY_DRV || USB_DETECT_BY_CORE */
267 case USB_INSERTED:
268 #ifdef HAVE_LCD_BITMAP
269 if(do_screendump_instead_of_usb)
271 usb_state = USB_SCREENDUMP;
272 screen_dump();
273 #ifdef HAVE_REMOTE_LCD
274 remote_screen_dump();
275 #endif
276 break;
278 #endif
279 #ifdef HAVE_USB_POWER
280 if(usb_power_button())
282 /* Only charging is desired */
283 usb_state = USB_POWERED;
284 #ifdef HAVE_USBSTACK
285 #ifdef USB_ENABLE_STORAGE
286 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, false);
287 #endif
289 #ifdef USB_ENABLE_HID
290 #ifdef USB_ENABLE_CHARGING_ONLY
291 usb_core_enable_driver(USB_DRIVER_HID, false);
292 #else
293 usb_core_enable_driver(USB_DRIVER_HID, true);
294 #endif /* USB_ENABLE_CHARGING_ONLY */
295 #endif /* USB_ENABLE_HID */
297 #ifdef USB_ENABLE_CHARGING_ONLY
298 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, true);
299 #endif
300 usb_attach();
301 #endif
302 break;
304 #endif /* HAVE_USB_POWER */
305 #ifdef HAVE_USBSTACK
306 #ifdef HAVE_USB_POWER
307 /* Set the state to USB_POWERED for now. If permission to connect
308 * by threads and storage is granted it will be changed to
309 * USB_CONNECTED. */
310 usb_state = USB_POWERED;
311 #endif
312 #ifdef USB_ENABLE_STORAGE
313 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, true);
314 #endif
315 #ifdef USB_ENABLE_HID
316 usb_core_enable_driver(USB_DRIVER_HID, usb_hid);
317 #endif
318 #ifdef USB_ENABLE_CHARGING_ONLY
319 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, false);
320 #endif
322 /* Check any drivers enabled at this point for exclusive storage
323 * access requirements. */
324 exclusive_storage_access = usb_core_any_exclusive_storage();
326 if(!exclusive_storage_access)
328 usb_attach();
329 break;
331 #endif /* HAVE_USBSTACK */
332 /* Tell all threads that they have to back off the storage.
333 We subtract one for our own thread. */
334 num_acks_to_expect = queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
335 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
336 num_acks_to_expect);
337 break;
339 case SYS_USB_CONNECTED_ACK:
340 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
342 DEBUGF("All threads have acknowledged the connect.\n");
343 usb_slave_mode(true);
344 usb_state = USB_INSERTED;
346 else
348 DEBUGF("usb: got ack, %d to go...\n",
349 num_acks_to_expect);
351 break;
353 case USB_EXTRACTED:
354 #ifdef HAVE_LCD_BITMAP
355 if(usb_state == USB_SCREENDUMP)
357 usb_state = USB_EXTRACTED;
358 break; /* Connected for screendump only */
360 #endif
361 #ifndef HAVE_USBSTACK /* Stack must undo this if POWERED state was transitional */
362 #ifdef HAVE_USB_POWER
363 if(usb_state == USB_POWERED)
365 usb_state = USB_EXTRACTED;
366 break;
368 #endif
369 #endif /* HAVE_USBSTACK */
370 if(usb_state == USB_INSERTED)
372 /* Only disable the USB mode if we really have enabled it
373 some threads might not have acknowledged the
374 insertion */
375 usb_slave_mode(false);
378 usb_state = USB_EXTRACTED;
379 #ifdef HAVE_USBSTACK
380 if(!exclusive_storage_access)
382 #if !defined(USB_DETECT_BY_DRV) && !defined(USB_DETECT_BY_CORE)
383 /* Disabled handling USB_UNPOWERED */
384 usb_enable(false);
385 #endif
386 break;
389 #endif /* HAVE_USBSTACK */
390 num_acks_to_expect = usb_release_exclusive_storage();
392 break;
394 case SYS_USB_DISCONNECTED_ACK:
395 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
397 DEBUGF("All threads have acknowledged. "
398 "We're in business.\n");
400 else
402 DEBUGF("usb: got ack, %d to go...\n",
403 num_acks_to_expect);
405 break;
407 #ifdef HAVE_HOTSWAP
408 case SYS_HOTSWAP_INSERTED:
409 case SYS_HOTSWAP_EXTRACTED:
410 #ifdef HAVE_USBSTACK
411 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
412 #else /* !HAVE_USBSTACK */
413 if(usb_state == USB_INSERTED)
415 #if (CONFIG_STORAGE & STORAGE_MMC)
416 usb_enable(false);
417 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
418 #endif /* STORAGE_MMC */
420 #endif /* HAVE_USBSTACK */
421 break;
423 #if (CONFIG_STORAGE & STORAGE_MMC)
424 case USB_REENABLE:
425 if(usb_state == USB_INSERTED)
426 usb_enable(true); /* reenable only if still inserted */
427 break;
428 #endif /* STORAGE_MMC */
429 #endif /* HAVE_HOTSWAP */
431 #ifdef USB_FIREWIRE_HANDLING
432 case USB_REQUEST_REBOOT:
433 #ifdef HAVE_USB_POWER
434 if (usb_reboot_button())
435 #endif
436 try_reboot();
437 break;
438 #endif /* USB_FIREWIRE_HANDLING */
440 #if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
441 case USB_CHARGER_UPDATE:
442 usb_charging_maxcurrent_change(usb_charging_maxcurrent());
443 break;
444 #endif
449 #if defined(HAVE_USB_CHARGING_ENABLE) && defined(HAVE_USBSTACK)
450 void usb_charger_update(void)
452 queue_post(&usb_queue, USB_CHARGER_UPDATE, 0);
454 #endif
456 #ifdef USB_STATUS_BY_EVENT
457 void usb_status_event(int current_status)
459 /* Caller isn't expected to filter for changes in status.
460 * current_status:
461 * USB_DETECT_BY_DRV/CORE: USB_POWERED, USB_UNPOWERED,
462 USB_INSERTED (driver/core)
463 * else: USB_INSERTED, USB_EXTRACTED
465 if(usb_monitor_enabled)
467 int oldstatus = disable_irq_save(); /* Dual-use function */
469 if(last_usb_status != current_status)
471 last_usb_status = current_status;
472 queue_post(&usb_queue, current_status, 0);
475 restore_irq(oldstatus);
479 void usb_start_monitoring(void)
481 int oldstatus = disable_irq_save(); /* Sync to event */
482 int status = usb_detect();
484 usb_monitor_enabled = true;
486 /* An event may have been missed because it was sent before monitoring
487 * was enabled due to the connector already having been inserted before
488 * before or during boot. */
489 #if defined(USB_DETECT_BY_DRV) || defined(USB_DETECT_BY_CORE)
490 /* Filter the status - USB_INSERTED may happen later */
491 status = (status == USB_INSERTED) ? USB_POWERED : USB_UNPOWERED;
492 #endif
493 usb_status_event(status);
495 #ifdef USB_FIREWIRE_HANDLING
496 if (firewire_detect())
497 usb_firewire_connect_event();
498 #endif
500 restore_irq(oldstatus);
503 #ifdef USB_FIREWIRE_HANDLING
504 void usb_firewire_connect_event(void)
506 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
508 #endif /* USB_FIREWIRE_HANDLING */
509 #else /* !USB_STATUS_BY_EVENT */
510 static void usb_tick(void)
512 int current_status;
514 if(usb_monitor_enabled)
516 #ifdef USB_FIREWIRE_HANDLING
517 int current_firewire_status = firewire_detect();
518 if(current_firewire_status != last_firewire_status)
520 last_firewire_status = current_firewire_status;
521 firewire_countdown = NUM_POLL_READINGS;
523 else
525 /* Count down until it gets negative */
526 if(firewire_countdown >= 0)
527 firewire_countdown--;
529 /* Report to the thread if we have had 3 identical status
530 readings in a row */
531 if(firewire_countdown == 0)
533 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
536 #endif /* USB_FIREWIRE_HANDLING */
538 current_status = usb_detect();
540 /* Only report when the status has changed */
541 if(current_status != last_usb_status)
543 last_usb_status = current_status;
544 countdown = NUM_POLL_READINGS;
546 else
548 /* Count down until it gets negative */
549 if(countdown >= 0)
550 countdown--;
552 /* Report to the thread if we have had 3 identical status
553 readings in a row */
554 if(countdown == 0)
556 queue_post(&usb_queue, current_status, 0);
560 #if (CONFIG_STORAGE & STORAGE_MMC)
561 if(usb_mmc_countdown > 0)
563 usb_mmc_countdown--;
564 if (usb_mmc_countdown == 0)
565 queue_post(&usb_queue, USB_REENABLE, 0);
567 #endif
570 void usb_start_monitoring(void)
572 usb_monitor_enabled = true;
574 #endif /* USB_STATUS_BY_EVENT */
575 #endif /* USB_FULL_INIT */
577 void usb_acknowledge(long id)
579 queue_post(&usb_queue, id, 0);
582 void usb_init(void)
584 /* We assume that the USB cable is extracted */
585 usb_state = USB_EXTRACTED;
586 #if defined(USB_DETECT_BY_DRV) || defined(USB_DETECT_BY_CORE)
587 last_usb_status = USB_UNPOWERED;
588 #else
589 last_usb_status = USB_EXTRACTED;
590 #endif
591 usb_monitor_enabled = false;
593 #ifdef HAVE_USBSTACK
594 exclusive_storage_access = false;
595 #endif
597 #ifdef USB_FIREWIRE_HANDLING
598 firewire_countdown = -1;
599 last_firewire_status = false;
600 #endif
602 usb_init_device();
603 #ifdef USB_FULL_INIT
604 usb_enable(false);
606 queue_init(&usb_queue, true);
608 usb_thread_entry = create_thread(usb_thread, usb_stack,
609 sizeof(usb_stack), 0, usb_thread_name
610 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
612 #ifndef USB_STATUS_BY_EVENT
613 countdown = -1;
614 tick_add_task(usb_tick);
615 #endif
616 #endif /* USB_FULL_INIT */
619 void usb_wait_for_disconnect(struct event_queue *q)
621 #ifdef USB_FULL_INIT
622 struct queue_event ev;
624 /* Don't return until we get SYS_USB_DISCONNECTED */
625 while(1)
627 queue_wait(q, &ev);
628 if(ev.id == SYS_USB_DISCONNECTED)
630 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
631 return;
634 #else
635 (void)q;
636 #endif /* USB_FULL_INIT */
639 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
641 #ifdef USB_FULL_INIT
642 struct queue_event ev;
644 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
645 while(1)
647 queue_wait_w_tmo(q, &ev, ticks);
648 switch(ev.id)
650 case SYS_USB_DISCONNECTED:
651 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
652 return 0;
653 case SYS_TIMEOUT:
654 return 1;
657 #else
658 (void)q; (void)ticks;
659 return 0;
660 #endif /* USB_FULL_INIT */
663 #ifdef USB_DRIVER_CLOSE
664 void usb_close(void)
666 unsigned int thread = usb_thread_entry;
667 usb_thread_entry = 0;
669 if (thread == 0)
670 return;
672 #ifndef USB_STATUS_BY_EVENT
673 tick_remove_task(usb_tick);
674 #endif
675 usb_monitor_enabled = false;
677 queue_post(&usb_queue, USB_QUIT, 0);
678 thread_wait(thread);
680 #endif /* USB_DRIVER_CLOSE */
682 bool usb_inserted(void)
684 #ifdef HAVE_USB_POWER
685 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
686 #else
687 return usb_state == USB_INSERTED;
688 #endif
691 #ifdef HAVE_USBSTACK
692 bool usb_exclusive_storage(void)
694 return exclusive_storage_access;
696 #endif
698 int usb_release_exclusive_storage(void)
700 int num_acks_to_expect;
701 #ifdef HAVE_USBSTACK
702 exclusive_storage_access = false;
703 #endif /* HAVE_USBSTACK */
704 /* Tell all threads that we are back in business */
705 num_acks_to_expect =
706 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
707 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
708 num_acks_to_expect);
709 return num_acks_to_expect;
712 #ifdef HAVE_USB_POWER
713 bool usb_powered(void)
715 return usb_state == USB_POWERED;
717 #endif
719 #ifdef USB_ENABLE_HID
720 void usb_set_hid(bool enable)
722 usb_hid = enable;
723 usb_core_enable_driver(USB_DRIVER_HID, usb_hid);
725 #endif
727 #else
729 #ifdef USB_NONE
730 bool usb_inserted(void)
732 return false;
734 #endif
736 /* Dummy simulator functions */
737 void usb_acknowledge(long id)
739 id = id;
742 void usb_init(void)
746 void usb_start_monitoring(void)
750 int usb_detect(void)
752 return USB_EXTRACTED;
755 void usb_wait_for_disconnect(struct event_queue *q)
757 (void)q;
760 #endif /* USB_NONE or SIMULATOR */