Gigabeat S: Add remote control reading and proper headphone insert detection. We...
[kugel-rb.git] / firmware / usb.c
bloba1e552a3217997c22dac5c1e6d7f82e513abe267
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"
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_STORAGE)) || \
50 (defined(HAVE_USBSTACK) && (defined(CREATIVE_ZVx) || \
51 defined(CPU_TCC77X) || defined(CPU_TCC780X))) || \
52 (CONFIG_USBOTG == USBOTG_JZ4740)
53 #define USB_FULL_INIT
54 #endif
56 #ifdef HAVE_LCD_BITMAP
57 bool do_screendump_instead_of_usb = false;
58 #if defined(USB_FULL_INIT) && defined(BOOTLOADER)
59 static void screen_dump(void) {}
60 #else
61 void screen_dump(void); /* Nasty again. Defined in apps/ too */
62 #endif
63 #endif
65 #if !defined(SIMULATOR) && !defined(USB_NONE)
67 static int usb_state;
69 #if (CONFIG_STORAGE & STORAGE_MMC) && defined(USB_FULL_INIT)
70 static int usb_mmc_countdown = 0;
71 #endif
73 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
74 needs updating */
75 #ifdef USB_FULL_INIT
76 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
77 static const char usb_thread_name[] = "usb";
78 static unsigned int usb_thread_entry = 0;
79 #ifndef USB_STATUS_BY_EVENT
80 #define NUM_POLL_READINGS (HZ/5)
81 static int countdown;
82 #endif /* USB_STATUS_BY_EVENT */
83 #endif /* USB_FULL_INIT */
84 static struct event_queue usb_queue;
85 static bool usb_monitor_enabled;
86 static int last_usb_status;
87 #ifdef HAVE_USBSTACK
88 static bool exclusive_storage_access;
89 #endif
91 #ifdef USB_FIREWIRE_HANDLING
92 static int firewire_countdown;
93 static bool last_firewire_status;
94 #endif
96 #ifdef USB_FULL_INIT
98 #if defined(USB_FIREWIRE_HANDLING) \
99 || (defined(HAVE_USBSTACK) && !defined(USE_ROCKBOX_USB))
100 static void try_reboot(void)
102 #ifdef HAVE_DISK_STORAGE
103 storage_sleepnow(); /* Immediately spindown the disk. */
104 sleep(HZ*2);
105 #endif
107 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
108 #if CONFIG_CPU == PP5020
109 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
110 #elif CONFIG_CPU == PP5022
111 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
112 #endif /* CONFIG_CPU */
113 #endif /* IPOD_ARCH */
115 system_reboot(); /* Reboot */
117 #endif /* USB_FIRWIRE_HANDLING || (HAVE_USBSTACK && !USE_ROCKBOX_USB) */
119 #ifdef HAVE_USBSTACK
120 /* inline since branch is chosen at compile time */
121 static inline void usb_slave_mode(bool on)
123 #ifdef USE_ROCKBOX_USB
124 int rc;
126 if (on)
128 trigger_cpu_boost();
129 #ifdef HAVE_PRIORITY_SCHEDULING
130 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_REALTIME);
131 #endif
132 usb_attach();
134 else /* usb_state == USB_INSERTED (only!) */
136 #ifndef USB_DETECT_BY_DRV
137 usb_enable(false);
138 #endif
139 #ifdef HAVE_PRIORITY_SCHEDULING
140 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_SYSTEM);
141 #endif
142 /* Entered exclusive mode */
143 rc = disk_mount_all();
144 if (rc <= 0) /* no partition */
145 panicf("mount: %d",rc);
147 cancel_cpu_boost();
149 #else /* !USB_ROCKBOX_USB */
150 if (on)
152 /* until we have native mass-storage mode, we want to reboot on
153 usb host connect */
154 try_reboot();
156 #endif /* USE_ROCKBOX_USB */
159 void usb_signal_transfer_completion(
160 struct usb_transfer_completion_event_data* event_data)
162 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
164 #else /* !HAVE_USBSTACK */
165 /* inline since branch is chosen at compile time */
166 static inline void usb_slave_mode(bool on)
168 int rc;
170 if(on)
172 DEBUGF("Entering USB slave mode\n");
173 storage_soft_reset();
174 storage_init();
175 storage_enable(false);
176 usb_enable(true);
177 cpu_idle_mode(true);
179 else
181 DEBUGF("Leaving USB slave mode\n");
183 cpu_idle_mode(false);
185 /* Let the ISDx00 settle */
186 sleep(HZ*1);
188 usb_enable(false);
190 rc = storage_init();
191 if(rc)
192 panicf("storage: %d",rc);
194 rc = disk_mount_all();
195 if (rc <= 0) /* no partition */
196 panicf("mount: %d",rc);
199 #endif /* HAVE_USBSTACK */
201 #ifdef HAVE_USB_POWER
202 static inline bool usb_power_button(void)
204 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
205 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
206 #else
207 return (button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON;
208 #endif
211 #ifdef USB_FIREWIRE_HANDLING
212 static inline bool usb_reboot_button(void)
214 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
216 #endif
217 #endif /* HAVE_USB_POWER */
219 static void usb_thread(void)
221 int num_acks_to_expect = 0;
222 struct queue_event ev;
224 while(1)
226 queue_wait(&usb_queue, &ev);
227 switch(ev.id)
229 #ifdef USB_DRIVER_CLOSE
230 case USB_QUIT:
231 return;
232 #endif
233 #ifdef HAVE_USBSTACK
234 case USB_TRANSFER_COMPLETION:
235 usb_core_handle_transfer_completion(
236 (struct usb_transfer_completion_event_data*)ev.data);
237 break;
238 #endif
239 #ifdef USB_DETECT_BY_DRV
240 /* In this case, these events the handle cable insertion USB
241 * driver determines INSERTED/EXTRACTED state. */
242 case USB_POWERED:
243 /* Set the state to USB_POWERED for now and enable the driver
244 * to detect a bus reset only. If a bus reset is detected,
245 * USB_INSERTED will be received. */
246 usb_state = USB_POWERED;
247 usb_enable(true);
248 break;
250 case USB_UNPOWERED:
251 usb_enable(false);
252 /* This part shouldn't be obligatory for anything that can
253 * reliably detect removal of the data lines. USB_EXTRACTED
254 * could be posted on that event while bus power remains
255 * available. */
256 queue_post(&usb_queue, USB_EXTRACTED, 0);
257 break;
258 #endif /* USB_DETECT_BY_DRV */
259 case USB_INSERTED:
260 #ifdef HAVE_LCD_BITMAP
261 if(do_screendump_instead_of_usb)
263 usb_state = USB_SCREENDUMP;
264 screen_dump();
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 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, false);
275 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, true);
276 usb_attach();
277 #endif
278 break;
280 #endif /* HAVE_USB_POWER */
281 #ifdef HAVE_USBSTACK
282 #ifdef HAVE_USB_POWER
283 /* Set the state to USB_POWERED for now. If permission to connect
284 * by threads and storage is granted it will be changed to
285 * USB_CONNECTED. */
286 usb_state = USB_POWERED;
287 #endif
288 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, true);
289 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, false);
291 /* Check any drivers enabled at this point for exclusive storage
292 * access requirements. */
293 exclusive_storage_access = usb_core_any_exclusive_storage();
295 if(!exclusive_storage_access)
297 usb_attach();
298 break;
300 #endif /* HAVE_USBSTACK */
301 /* Tell all threads that they have to back off the storage.
302 We subtract one for our own thread. */
303 num_acks_to_expect = queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
304 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
305 num_acks_for_connect);
306 break;
308 case SYS_USB_CONNECTED_ACK:
309 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
311 DEBUGF("All threads have acknowledged the connect.\n");
312 usb_slave_mode(true);
313 usb_state = USB_INSERTED;
315 else
317 DEBUGF("usb: got ack, %d to go...\n",
318 num_acks_to_expect);
320 break;
322 case USB_EXTRACTED:
323 #ifdef HAVE_LCD_BITMAP
324 if(usb_state == USB_SCREENDUMP)
326 usb_state = USB_EXTRACTED;
327 break; /* Connected for screendump only */
329 #endif
330 #ifndef HAVE_USBSTACK /* Stack must undo this if POWERED state was transitional */
331 #ifdef HAVE_USB_POWER
332 if(usb_state == USB_POWERED)
334 usb_state = USB_EXTRACTED;
335 break;
337 #endif
338 #endif /* HAVE_USBSTACK */
339 if(usb_state == USB_INSERTED)
341 /* Only disable the USB mode if we really have enabled it
342 some threads might not have acknowledged the
343 insertion */
344 usb_slave_mode(false);
347 usb_state = USB_EXTRACTED;
348 #ifdef HAVE_USBSTACK
349 if(!exclusive_storage_access)
351 #ifndef USB_DETECT_BY_DRV /* Disabled handling USB_UNPOWERED */
352 usb_enable(false);
353 #endif
354 break;
357 exclusive_storage_access = false;
358 #endif /* HAVE_USBSTACK */
360 /* Tell all threads that we are back in business */
361 num_acks_to_expect =
362 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
363 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
364 num_acks_to_expect);
365 break;
367 case SYS_USB_DISCONNECTED_ACK:
368 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
370 DEBUGF("All threads have acknowledged. "
371 "We're in business.\n");
373 else
375 DEBUGF("usb: got ack, %d to go...\n",
376 num_acks_to_expect);
378 break;
380 #ifdef HAVE_HOTSWAP
381 case SYS_HOTSWAP_INSERTED:
382 case SYS_HOTSWAP_EXTRACTED:
383 #ifdef HAVE_USBSTACK
384 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
385 #else /* !HAVE_USBSTACK */
386 if(usb_state == USB_INSERTED)
388 usb_enable(false);
389 #if (CONFIG_STORAGE & STORAGE_MMC)
390 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
391 #endif /* STORAGE_MMC */
393 #endif /* HAVE_USBSTACK */
394 break;
396 #if (CONFIG_STORAGE & STORAGE_MMC)
397 case USB_REENABLE:
398 if(usb_state == USB_INSERTED)
399 usb_enable(true); /* reenable only if still inserted */
400 break;
401 #endif /* STORAGE_MMC */
402 #endif /* HAVE_HOTSWAP */
404 #ifdef USB_FIREWIRE_HANDLING
405 case USB_REQUEST_REBOOT:
406 #ifdef HAVE_USB_POWER
407 if (usb_reboot_button())
408 #endif
409 try_reboot();
410 break;
411 #endif /* USB_FIREWIRE_HANDLING */
416 #ifdef USB_STATUS_BY_EVENT
417 void usb_status_event(int current_status)
419 /* Caller isn't expected to filter for changes in status.
420 * current_status:
421 * USB_DETECT_BY_DRV: USB_POWERED, USB_UNPOWERED, USB_INSERTED (driver)
422 * else: USB_INSERTED, USB_EXTRACTED
424 if(usb_monitor_enabled)
426 int oldstatus = disable_irq_save(); /* Dual-use function */
428 if(last_usb_status != current_status)
430 last_usb_status = current_status;
431 queue_post(&usb_queue, current_status, 0);
434 restore_irq(oldstatus);
438 void usb_start_monitoring(void)
440 int oldstatus = disable_irq_save(); /* Sync to event */
441 int status = usb_detect();
443 usb_monitor_enabled = true;
445 #ifdef USB_DETECT_BY_DRV
446 status = (status == USB_INSERTED) ? USB_POWERED : USB_UNPOWERED;
447 #endif
448 usb_status_event(status);
450 #ifdef USB_FIREWIRE_HANDLING
451 if (firewire_detect())
452 usb_firewire_connect_event();
453 #endif
455 restore_irq(oldstatus);
458 #ifdef USB_FIREWIRE_HANDLING
459 void usb_firewire_connect_event(void)
461 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
463 #endif /* USB_FIREWIRE_HANDLING */
464 #else /* !USB_STATUS_BY_EVENT */
465 static void usb_tick(void)
467 int current_status;
469 if(usb_monitor_enabled)
471 #ifdef USB_FIREWIRE_HANDLING
472 int current_firewire_status = firewire_detect();
473 if(current_firewire_status != last_firewire_status)
475 last_firewire_status = current_firewire_status;
476 firewire_countdown = NUM_POLL_READINGS;
478 else
480 /* Count down until it gets negative */
481 if(firewire_countdown >= 0)
482 firewire_countdown--;
484 /* Report to the thread if we have had 3 identical status
485 readings in a row */
486 if(firewire_countdown == 0)
488 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
491 #endif /* USB_FIREWIRE_HANDLING */
493 current_status = usb_detect();
495 /* Only report when the status has changed */
496 if(current_status != last_usb_status)
498 last_usb_status = current_status;
499 countdown = NUM_POLL_READINGS;
501 else
503 /* Count down until it gets negative */
504 if(countdown >= 0)
505 countdown--;
507 /* Report to the thread if we have had 3 identical status
508 readings in a row */
509 if(countdown == 0)
511 queue_post(&usb_queue, current_status, 0);
515 #if (CONFIG_STORAGE & STORAGE_MMC)
516 if(usb_mmc_countdown > 0)
518 usb_mmc_countdown--;
519 if (usb_mmc_countdown == 0)
520 queue_post(&usb_queue, USB_REENABLE, 0);
522 #endif
525 void usb_start_monitoring(void)
527 usb_monitor_enabled = true;
529 #endif /* USB_STATUS_BY_EVENT */
530 #endif /* USB_FULL_INIT */
532 void usb_acknowledge(long id)
534 queue_post(&usb_queue, id, 0);
537 void usb_init(void)
539 /* We assume that the USB cable is extracted */
540 usb_state = USB_EXTRACTED;
541 #ifdef USB_DETECT_BY_DRV
542 last_usb_status = USB_UNPOWERED;
543 #else
544 last_usb_status = USB_EXTRACTED;
545 #endif
546 usb_monitor_enabled = false;
548 #ifdef HAVE_USBSTACK
549 exclusive_storage_access = false;
550 #endif
552 #ifdef USB_FIREWIRE_HANDLING
553 firewire_countdown = -1;
554 last_firewire_status = false;
555 #endif
557 usb_init_device();
558 #ifdef USB_FULL_INIT
559 usb_enable(false);
561 queue_init(&usb_queue, true);
563 usb_thread_entry = create_thread(usb_thread, usb_stack,
564 sizeof(usb_stack), 0, usb_thread_name
565 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
567 #ifndef USB_STATUS_BY_EVENT
568 countdown = -1;
569 tick_add_task(usb_tick);
570 #endif
571 #endif /* USB_FULL_INIT */
574 void usb_wait_for_disconnect(struct event_queue *q)
576 #ifdef USB_FULL_INIT
577 struct queue_event ev;
579 /* Don't return until we get SYS_USB_DISCONNECTED */
580 while(1)
582 queue_wait(q, &ev);
583 if(ev.id == SYS_USB_DISCONNECTED)
585 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
586 return;
589 #else
590 (void)q;
591 #endif /* USB_FULL_INIT */
594 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
596 #ifdef USB_FULL_INIT
597 struct queue_event ev;
599 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
600 while(1)
602 queue_wait_w_tmo(q, &ev, ticks);
603 switch(ev.id)
605 case SYS_USB_DISCONNECTED:
606 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
607 return 0;
608 case SYS_TIMEOUT:
609 return 1;
612 #else
613 (void)q; (void)ticks;
614 return 0;
615 #endif /* USB_FULL_INIT */
618 #ifdef USB_DRIVER_CLOSE
619 void usb_close(void)
621 unsigned int thread = usb_thread_entry;
622 usb_thread_entry = 0;
624 if (thread == 0)
625 return;
627 #ifndef USB_STATUS_BY_EVENT
628 tick_remove_task(usb_tick);
629 #endif
630 usb_monitor_enabled = false;
632 queue_post(&usb_queue, USB_QUIT, 0);
633 thread_wait(thread);
635 #endif /* USB_DRIVER_CLOSE */
637 bool usb_inserted(void)
639 #ifdef HAVE_USB_POWER
640 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
641 #else
642 return usb_state == USB_INSERTED;
643 #endif
646 #ifdef HAVE_USBSTACK
647 bool usb_exclusive_storage(void)
649 return exclusive_storage_access;
651 #endif
653 #ifdef HAVE_USB_POWER
654 bool usb_powered(void)
656 return usb_state == USB_POWERED;
658 #endif
660 #else
662 #ifdef USB_NONE
663 bool usb_inserted(void)
665 return false;
667 #endif
669 /* Dummy simulator functions */
670 void usb_acknowledge(long id)
672 id = id;
675 void usb_init(void)
679 void usb_start_monitoring(void)
683 int usb_detect(void)
685 return USB_EXTRACTED;
688 void usb_wait_for_disconnect(struct event_queue *q)
690 (void)q;
693 #endif /* USB_NONE or SIMULATOR */