Do some #ifdef'ing to make the Player happy.
[kugel-rb.git] / firmware / usb.c
blob07683ce7b1277afb11f7802ef513309c21ba0960
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 "sprintf.h"
41 #include "string.h"
42 #ifdef HAVE_USBSTACK
43 #include "usb_core.h"
44 #endif
45 #include "logf.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_ENABLE_STORAGE)) || \
51 (defined(HAVE_USBSTACK) && (defined(CREATIVE_ZVx)) || \
52 (defined(HAVE_USBSTACK) && (defined(OLYMPUS_MROBE_500)) || \
53 defined(CPU_TCC77X) || defined(CPU_TCC780X))) || \
54 (CONFIG_USBOTG == USBOTG_JZ4740)
55 #define USB_FULL_INIT
56 #endif
58 #ifdef HAVE_LCD_BITMAP
59 bool do_screendump_instead_of_usb = false;
60 #endif
62 #if !defined(SIMULATOR) && !defined(USB_NONE)
64 static int usb_state;
66 #if (CONFIG_STORAGE & STORAGE_MMC) && defined(USB_FULL_INIT)
67 static int usb_mmc_countdown = 0;
68 #endif
70 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
71 needs updating */
72 #ifdef USB_FULL_INIT
73 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
74 static const char usb_thread_name[] = "usb";
75 static unsigned int usb_thread_entry = 0;
76 #ifndef USB_STATUS_BY_EVENT
77 #define NUM_POLL_READINGS (HZ/5)
78 static int countdown;
79 #endif /* USB_STATUS_BY_EVENT */
80 #endif /* USB_FULL_INIT */
81 static struct event_queue usb_queue;
82 static bool usb_monitor_enabled;
83 static int last_usb_status;
84 #ifdef HAVE_USBSTACK
85 static bool exclusive_storage_access;
86 #endif
88 #ifdef USB_FIREWIRE_HANDLING
89 static int firewire_countdown;
90 static bool last_firewire_status;
91 #endif
93 #ifdef USB_FULL_INIT
95 #if defined(USB_FIREWIRE_HANDLING) \
96 || (defined(HAVE_USBSTACK) && !defined(USE_ROCKBOX_USB))
97 static void try_reboot(void)
99 #ifdef HAVE_DISK_STORAGE
100 storage_sleepnow(); /* Immediately spindown the disk. */
101 sleep(HZ*2);
102 #endif
104 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
105 #if CONFIG_CPU == PP5020
106 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
107 #elif CONFIG_CPU == PP5022
108 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
109 #endif /* CONFIG_CPU */
110 #endif /* IPOD_ARCH */
112 system_reboot(); /* Reboot */
114 #endif /* USB_FIRWIRE_HANDLING || (HAVE_USBSTACK && !USE_ROCKBOX_USB) */
116 #ifdef HAVE_USBSTACK
117 /* inline since branch is chosen at compile time */
118 static inline void usb_slave_mode(bool on)
120 #ifdef USE_ROCKBOX_USB
121 int rc;
123 if (on)
125 trigger_cpu_boost();
126 #ifdef HAVE_PRIORITY_SCHEDULING
127 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_REALTIME);
128 #endif
129 usb_attach();
131 else /* usb_state == USB_INSERTED (only!) */
133 #ifndef USB_DETECT_BY_DRV
134 usb_enable(false);
135 #endif
136 #ifdef HAVE_PRIORITY_SCHEDULING
137 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_SYSTEM);
138 #endif
139 /* Entered exclusive mode */
140 rc = disk_mount_all();
141 if (rc <= 0) /* no partition */
142 panicf("mount: %d",rc);
144 cancel_cpu_boost();
146 #else /* !USB_ROCKBOX_USB */
147 if (on)
149 /* until we have native mass-storage mode, we want to reboot on
150 usb host connect */
151 try_reboot();
153 #endif /* USE_ROCKBOX_USB */
156 void usb_signal_transfer_completion(
157 struct usb_transfer_completion_event_data* event_data)
159 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
161 #else /* !HAVE_USBSTACK */
162 /* inline since branch is chosen at compile time */
163 static inline void usb_slave_mode(bool on)
165 int rc;
167 if(on)
169 DEBUGF("Entering USB slave mode\n");
170 storage_soft_reset();
171 storage_init();
172 storage_enable(false);
173 usb_enable(true);
174 cpu_idle_mode(true);
176 else
178 DEBUGF("Leaving USB slave mode\n");
180 cpu_idle_mode(false);
182 /* Let the ISDx00 settle */
183 sleep(HZ*1);
185 usb_enable(false);
187 rc = storage_init();
188 if(rc)
189 panicf("storage: %d",rc);
191 rc = disk_mount_all();
192 if (rc <= 0) /* no partition */
193 panicf("mount: %d",rc);
196 #endif /* HAVE_USBSTACK */
198 #ifdef HAVE_USB_POWER
199 static inline bool usb_power_button(void)
201 #if (defined(IRIVER_H10) || defined (IRIVER_H10_5GB)) && !defined(USE_ROCKBOX_USB)
202 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
203 #else
204 return (button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON;
205 #endif
208 #ifdef USB_FIREWIRE_HANDLING
209 static inline bool usb_reboot_button(void)
211 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
213 #endif
214 #endif /* HAVE_USB_POWER */
216 static void usb_thread(void)
218 int num_acks_to_expect = 0;
219 struct queue_event ev;
221 while(1)
223 queue_wait(&usb_queue, &ev);
224 switch(ev.id)
226 #ifdef USB_DRIVER_CLOSE
227 case USB_QUIT:
228 return;
229 #endif
230 #ifdef HAVE_USBSTACK
231 case USB_TRANSFER_COMPLETION:
232 usb_core_handle_transfer_completion(
233 (struct usb_transfer_completion_event_data*)ev.data);
234 break;
235 #endif
236 #ifdef USB_DETECT_BY_DRV
237 /* In this case, these events the handle cable insertion USB
238 * driver determines INSERTED/EXTRACTED state. */
239 case USB_POWERED:
240 /* Set the state to USB_POWERED for now and enable the driver
241 * to detect a bus reset only. If a bus reset is detected,
242 * USB_INSERTED will be received. */
243 usb_state = USB_POWERED;
244 usb_enable(true);
245 break;
247 case USB_UNPOWERED:
248 usb_enable(false);
249 /* This part shouldn't be obligatory for anything that can
250 * reliably detect removal of the data lines. USB_EXTRACTED
251 * could be posted on that event while bus power remains
252 * available. */
253 queue_post(&usb_queue, USB_EXTRACTED, 0);
254 break;
255 #endif /* USB_DETECT_BY_DRV */
256 case USB_INSERTED:
257 #ifdef HAVE_LCD_BITMAP
258 if(do_screendump_instead_of_usb)
260 usb_state = USB_SCREENDUMP;
261 screen_dump();
262 #ifdef HAVE_REMOTE_LCD
263 remote_screen_dump();
264 #endif
265 break;
267 #endif
268 #ifdef HAVE_USB_POWER
269 if(usb_power_button())
271 /* Only charging is desired */
272 usb_state = USB_POWERED;
273 #ifdef HAVE_USBSTACK
274 #ifdef USB_ENABLE_STORAGE
275 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, false);
276 #endif
278 #ifdef USB_ENABLE_HID
279 #ifdef USB_ENABLE_CHARGING_ONLY
280 usb_core_enable_driver(USB_DRIVER_HID, false);
281 #else
282 usb_core_enable_driver(USB_DRIVER_HID, true);
283 #endif /* USB_ENABLE_CHARGING_ONLY */
284 #endif /* USB_ENABLE_HID */
286 #ifdef USB_ENABLE_CHARGING_ONLY
287 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, true);
288 #endif
289 usb_attach();
290 #endif
291 break;
293 #endif /* HAVE_USB_POWER */
294 #ifdef HAVE_USBSTACK
295 #ifdef HAVE_USB_POWER
296 /* Set the state to USB_POWERED for now. If permission to connect
297 * by threads and storage is granted it will be changed to
298 * USB_CONNECTED. */
299 usb_state = USB_POWERED;
300 #endif
301 #ifdef USB_ENABLE_STORAGE
302 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, true);
303 #endif
304 #ifdef USB_ENABLE_HID
305 usb_core_enable_driver(USB_DRIVER_HID, true);
306 #endif
307 #ifdef USB_ENABLE_CHARGING_ONLY
308 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, false);
309 #endif
311 /* Check any drivers enabled at this point for exclusive storage
312 * access requirements. */
313 exclusive_storage_access = usb_core_any_exclusive_storage();
315 if(!exclusive_storage_access)
317 usb_attach();
318 break;
320 #endif /* HAVE_USBSTACK */
321 /* Tell all threads that they have to back off the storage.
322 We subtract one for our own thread. */
323 num_acks_to_expect = queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
324 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
325 num_acks_to_expect);
326 break;
328 case SYS_USB_CONNECTED_ACK:
329 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
331 DEBUGF("All threads have acknowledged the connect.\n");
332 usb_slave_mode(true);
333 usb_state = USB_INSERTED;
335 else
337 DEBUGF("usb: got ack, %d to go...\n",
338 num_acks_to_expect);
340 break;
342 case USB_EXTRACTED:
343 #ifdef HAVE_LCD_BITMAP
344 if(usb_state == USB_SCREENDUMP)
346 usb_state = USB_EXTRACTED;
347 break; /* Connected for screendump only */
349 #endif
350 #ifndef HAVE_USBSTACK /* Stack must undo this if POWERED state was transitional */
351 #ifdef HAVE_USB_POWER
352 if(usb_state == USB_POWERED)
354 usb_state = USB_EXTRACTED;
355 break;
357 #endif
358 #endif /* HAVE_USBSTACK */
359 if(usb_state == USB_INSERTED)
361 /* Only disable the USB mode if we really have enabled it
362 some threads might not have acknowledged the
363 insertion */
364 usb_slave_mode(false);
367 usb_state = USB_EXTRACTED;
368 #ifdef HAVE_USBSTACK
369 if(!exclusive_storage_access)
371 #ifndef USB_DETECT_BY_DRV /* Disabled handling USB_UNPOWERED */
372 usb_enable(false);
373 #endif
374 break;
377 #endif /* HAVE_USBSTACK */
378 num_acks_to_expect = usb_release_exclusive_storage();
380 break;
382 case SYS_USB_DISCONNECTED_ACK:
383 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
385 DEBUGF("All threads have acknowledged. "
386 "We're in business.\n");
388 else
390 DEBUGF("usb: got ack, %d to go...\n",
391 num_acks_to_expect);
393 break;
395 #ifdef HAVE_HOTSWAP
396 case SYS_HOTSWAP_INSERTED:
397 case SYS_HOTSWAP_EXTRACTED:
398 #ifdef HAVE_USBSTACK
399 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
400 #else /* !HAVE_USBSTACK */
401 if(usb_state == USB_INSERTED)
403 usb_enable(false);
404 #if (CONFIG_STORAGE & STORAGE_MMC)
405 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
406 #endif /* STORAGE_MMC */
408 #endif /* HAVE_USBSTACK */
409 break;
411 #if (CONFIG_STORAGE & STORAGE_MMC)
412 case USB_REENABLE:
413 if(usb_state == USB_INSERTED)
414 usb_enable(true); /* reenable only if still inserted */
415 break;
416 #endif /* STORAGE_MMC */
417 #endif /* HAVE_HOTSWAP */
419 #ifdef USB_FIREWIRE_HANDLING
420 case USB_REQUEST_REBOOT:
421 #ifdef HAVE_USB_POWER
422 if (usb_reboot_button())
423 #endif
424 try_reboot();
425 break;
426 #endif /* USB_FIREWIRE_HANDLING */
431 #ifdef USB_STATUS_BY_EVENT
432 void usb_status_event(int current_status)
434 /* Caller isn't expected to filter for changes in status.
435 * current_status:
436 * USB_DETECT_BY_DRV: USB_POWERED, USB_UNPOWERED, USB_INSERTED (driver)
437 * else: USB_INSERTED, USB_EXTRACTED
439 if(usb_monitor_enabled)
441 int oldstatus = disable_irq_save(); /* Dual-use function */
443 if(last_usb_status != current_status)
445 last_usb_status = current_status;
446 queue_post(&usb_queue, current_status, 0);
449 restore_irq(oldstatus);
453 void usb_start_monitoring(void)
455 int oldstatus = disable_irq_save(); /* Sync to event */
456 int status = usb_detect();
458 usb_monitor_enabled = true;
460 #ifdef USB_DETECT_BY_DRV
461 status = (status == USB_INSERTED) ? USB_POWERED : USB_UNPOWERED;
462 #endif
463 usb_status_event(status);
465 #ifdef USB_FIREWIRE_HANDLING
466 if (firewire_detect())
467 usb_firewire_connect_event();
468 #endif
470 restore_irq(oldstatus);
473 #ifdef USB_FIREWIRE_HANDLING
474 void usb_firewire_connect_event(void)
476 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
478 #endif /* USB_FIREWIRE_HANDLING */
479 #else /* !USB_STATUS_BY_EVENT */
480 static void usb_tick(void)
482 int current_status;
484 if(usb_monitor_enabled)
486 #ifdef USB_FIREWIRE_HANDLING
487 int current_firewire_status = firewire_detect();
488 if(current_firewire_status != last_firewire_status)
490 last_firewire_status = current_firewire_status;
491 firewire_countdown = NUM_POLL_READINGS;
493 else
495 /* Count down until it gets negative */
496 if(firewire_countdown >= 0)
497 firewire_countdown--;
499 /* Report to the thread if we have had 3 identical status
500 readings in a row */
501 if(firewire_countdown == 0)
503 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
506 #endif /* USB_FIREWIRE_HANDLING */
508 current_status = usb_detect();
510 /* Only report when the status has changed */
511 if(current_status != last_usb_status)
513 last_usb_status = current_status;
514 countdown = NUM_POLL_READINGS;
516 else
518 /* Count down until it gets negative */
519 if(countdown >= 0)
520 countdown--;
522 /* Report to the thread if we have had 3 identical status
523 readings in a row */
524 if(countdown == 0)
526 queue_post(&usb_queue, current_status, 0);
530 #if (CONFIG_STORAGE & STORAGE_MMC)
531 if(usb_mmc_countdown > 0)
533 usb_mmc_countdown--;
534 if (usb_mmc_countdown == 0)
535 queue_post(&usb_queue, USB_REENABLE, 0);
537 #endif
540 void usb_start_monitoring(void)
542 usb_monitor_enabled = true;
544 #endif /* USB_STATUS_BY_EVENT */
545 #endif /* USB_FULL_INIT */
547 void usb_acknowledge(long id)
549 queue_post(&usb_queue, id, 0);
552 void usb_init(void)
554 /* We assume that the USB cable is extracted */
555 usb_state = USB_EXTRACTED;
556 #ifdef USB_DETECT_BY_DRV
557 last_usb_status = USB_UNPOWERED;
558 #else
559 last_usb_status = USB_EXTRACTED;
560 #endif
561 usb_monitor_enabled = false;
563 #ifdef HAVE_USBSTACK
564 exclusive_storage_access = false;
565 #endif
567 #ifdef USB_FIREWIRE_HANDLING
568 firewire_countdown = -1;
569 last_firewire_status = false;
570 #endif
572 usb_init_device();
573 #ifdef USB_FULL_INIT
574 usb_enable(false);
576 queue_init(&usb_queue, true);
578 usb_thread_entry = create_thread(usb_thread, usb_stack,
579 sizeof(usb_stack), 0, usb_thread_name
580 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
582 #ifndef USB_STATUS_BY_EVENT
583 countdown = -1;
584 tick_add_task(usb_tick);
585 #endif
586 #endif /* USB_FULL_INIT */
589 void usb_wait_for_disconnect(struct event_queue *q)
591 #ifdef USB_FULL_INIT
592 struct queue_event ev;
594 /* Don't return until we get SYS_USB_DISCONNECTED */
595 while(1)
597 queue_wait(q, &ev);
598 if(ev.id == SYS_USB_DISCONNECTED)
600 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
601 return;
604 #else
605 (void)q;
606 #endif /* USB_FULL_INIT */
609 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
611 #ifdef USB_FULL_INIT
612 struct queue_event ev;
614 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
615 while(1)
617 queue_wait_w_tmo(q, &ev, ticks);
618 switch(ev.id)
620 case SYS_USB_DISCONNECTED:
621 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
622 return 0;
623 case SYS_TIMEOUT:
624 return 1;
627 #else
628 (void)q; (void)ticks;
629 return 0;
630 #endif /* USB_FULL_INIT */
633 #ifdef USB_DRIVER_CLOSE
634 void usb_close(void)
636 unsigned int thread = usb_thread_entry;
637 usb_thread_entry = 0;
639 if (thread == 0)
640 return;
642 #ifndef USB_STATUS_BY_EVENT
643 tick_remove_task(usb_tick);
644 #endif
645 usb_monitor_enabled = false;
647 queue_post(&usb_queue, USB_QUIT, 0);
648 thread_wait(thread);
650 #endif /* USB_DRIVER_CLOSE */
652 bool usb_inserted(void)
654 #ifdef HAVE_USB_POWER
655 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
656 #else
657 return usb_state == USB_INSERTED;
658 #endif
661 #ifdef HAVE_USBSTACK
662 bool usb_exclusive_storage(void)
664 return exclusive_storage_access;
666 #endif
668 int usb_release_exclusive_storage(void)
670 int num_acks_to_expect;
671 #ifdef HAVE_USBSTACK
672 exclusive_storage_access = false;
673 #endif /* HAVE_USBSTACK */
674 /* Tell all threads that we are back in business */
675 num_acks_to_expect =
676 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
677 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
678 num_acks_to_expect);
679 return num_acks_to_expect;
682 #ifdef HAVE_USB_POWER
683 bool usb_powered(void)
685 return usb_state == USB_POWERED;
687 #endif
689 #else
691 #ifdef USB_NONE
692 bool usb_inserted(void)
694 return false;
696 #endif
698 /* Dummy simulator functions */
699 void usb_acknowledge(long id)
701 id = id;
704 void usb_init(void)
708 void usb_start_monitoring(void)
712 int usb_detect(void)
714 return USB_EXTRACTED;
717 void usb_wait_for_disconnect(struct event_queue *q)
719 (void)q;
722 #endif /* USB_NONE or SIMULATOR */