Sansa Clip: Do not return HOLD status in button_read_device()
[kugel-rb.git] / firmware / usb.c
blob00e0bb360774c77f6bf0d074d99cfbebad1830af
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.h"
38 #include "button.h"
39 #include "sprintf.h"
40 #include "string.h"
41 #include "usb-target.h"
42 #ifdef HAVE_USBSTACK
43 #include "usb_core.h"
44 #endif
45 #ifdef IRIVER_H300_SERIES
46 #include "pcf50606.h" /* for pcf50606_usb_charging_... */
47 #endif
48 #include "logf.h"
50 /* Conditions under which we want the entire driver */
51 #if !defined(BOOTLOADER) || (CONFIG_CPU == SH7034) || \
52 (defined(TOSHIBA_GIGABEAT_S) && defined(USE_ROCKBOX_USB) && defined(USB_STORAGE)) || \
53 (defined(HAVE_USBSTACK) && (defined(CREATIVE_ZVx) || \
54 defined(CPU_TCC77X) || defined(CPU_TCC780X))) || \
55 (CONFIG_USBOTG == USBOTG_JZ4740)
56 #define USB_FULL_INIT
57 #endif
59 #ifdef HAVE_LCD_BITMAP
60 bool do_screendump_instead_of_usb = false;
61 #if defined(USB_FULL_INIT) && defined(BOOTLOADER)
62 static void screen_dump(void) {}
63 #else
64 void screen_dump(void); /* Nasty again. Defined in apps/ too */
65 #endif
66 #endif
68 #if !defined(SIMULATOR) && !defined(USB_NONE)
70 #define NUM_POLL_READINGS (HZ/5)
71 static int countdown;
73 static int usb_state;
75 #if (CONFIG_STORAGE & STORAGE_MMC) && defined(USB_FULL_INIT)
76 static int usb_mmc_countdown = 0;
77 #endif
79 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
80 needs updating */
81 #ifdef USB_FULL_INIT
82 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
83 static const char usb_thread_name[] = "usb";
84 static struct thread_entry *usb_thread_entry;
85 #endif
86 static struct event_queue usb_queue;
87 static int last_usb_status;
88 static bool usb_monitor_enabled;
89 #ifdef HAVE_USBSTACK
90 static bool exclusive_storage_access;
91 #endif
94 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
95 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
96 static int firewire_countdown;
97 static bool last_firewire_status;
98 #endif
100 #ifdef USB_FULL_INIT
101 #ifndef HAVE_USBSTACK
102 static void usb_slave_mode(bool on)
104 int rc;
106 if(on)
108 DEBUGF("Entering USB slave mode\n");
109 storage_soft_reset();
110 storage_init();
111 storage_enable(false);
112 usb_enable(true);
114 else
116 DEBUGF("Leaving USB slave mode\n");
118 /* Let the ISDx00 settle */
119 sleep(HZ*1);
121 usb_enable(false);
123 rc = storage_init();
124 if(rc)
125 panicf("storage: %d",rc);
127 rc = disk_mount_all();
128 if (rc <= 0) /* no partition */
129 panicf("mount: %d",rc);
133 #endif
135 static void try_reboot(void)
137 #ifdef HAVE_DISK_STORAGE
138 storage_sleepnow(); /* Immediately spindown the disk. */
139 sleep(HZ*2);
140 #endif
142 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
143 #if CONFIG_CPU == PP5020
144 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
145 #elif CONFIG_CPU == PP5022
146 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
147 #endif /* CONFIG_CPU */
148 #endif /* IPOD_ARCH */
150 system_reboot(); /* Reboot */
153 static void usb_thread(void)
155 int num_acks_to_expect = -1;
156 bool waiting_for_ack;
157 struct queue_event ev;
159 waiting_for_ack = false;
161 while(1)
163 queue_wait(&usb_queue, &ev);
164 switch(ev.id)
166 #ifdef USB_DRIVER_CLOSE
167 case USB_QUIT:
168 return;
169 #endif
170 #ifdef HAVE_USBSTACK
171 case USB_TRANSFER_COMPLETION:
172 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
173 break;
174 #endif
175 #ifdef HAVE_USB_POWER
176 case USB_POWERED:
177 usb_state = USB_POWERED;
178 break;
179 #endif
180 case USB_INSERTED:
181 #ifdef HAVE_LCD_BITMAP
182 if(do_screendump_instead_of_usb)
184 screen_dump();
186 else
187 #endif
188 #ifdef HAVE_USB_POWER
189 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
190 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
191 #else
192 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
193 #endif
195 usb_state = USB_POWERED;
196 #ifdef HAVE_USBSTACK
197 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,false);
198 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,true);
199 usb_enable(true);
200 #endif
202 else
203 #endif
205 #ifdef HAVE_USBSTACK
206 /* Set the state to USB_POWERED for now. if a real
207 connection is detected it will switch to USB_INSERTED */
208 usb_state = USB_POWERED;
209 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
210 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,false);
211 usb_enable(true);
212 #else
213 /* Tell all threads that they have to back off the ATA.
214 We subtract one for our own thread. */
215 num_acks_to_expect =
216 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
217 waiting_for_ack = true;
218 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
219 num_acks_to_expect);
220 #endif
222 break;
223 #ifdef HAVE_USBSTACK
224 case USB_REQUEST_DISK:
225 if(!waiting_for_ack)
227 /* Tell all threads that they have to back off the ATA.
228 We subtract one for our own thread. */
229 num_acks_to_expect =
230 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
231 waiting_for_ack = true;
232 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
233 num_acks_to_expect);
235 break;
236 case USB_RELEASE_DISK:
237 if(!waiting_for_ack)
239 /* Tell all threads that they have to back off the ATA.
240 We subtract one for our own thread. */
241 num_acks_to_expect =
242 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
243 waiting_for_ack = true;
244 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
245 num_acks_to_expect);
247 break;
248 #endif
249 case SYS_USB_CONNECTED_ACK:
250 if(waiting_for_ack)
252 num_acks_to_expect--;
253 if(num_acks_to_expect == 0)
255 DEBUGF("All threads have acknowledged the connect.\n");
256 #ifdef HAVE_USBSTACK
257 #ifndef USE_ROCKBOX_USB
258 /* until we have native mass-storage mode, we want to reboot on
259 usb host connect */
260 try_reboot();
261 #endif /* USE_ROCKBOX_USB */
262 #ifdef HAVE_PRIORITY_SCHEDULING
263 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
264 #endif
265 exclusive_storage_access = true;
267 #else
268 usb_slave_mode(true);
269 cpu_idle_mode(true);
270 #endif
271 usb_state = USB_INSERTED;
272 waiting_for_ack = false;
274 else
276 DEBUGF("usb: got ack, %d to go...\n",
277 num_acks_to_expect);
280 break;
282 case USB_EXTRACTED:
283 #ifdef HAVE_USBSTACK
284 usb_enable(false);
285 #ifdef HAVE_PRIORITY_SCHEDULING
286 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
287 #endif
288 #endif
289 #ifdef HAVE_LCD_BITMAP
290 if(do_screendump_instead_of_usb)
291 break;
292 #endif
293 #ifdef HAVE_USB_POWER
294 if(usb_state == USB_POWERED)
296 usb_state = USB_EXTRACTED;
297 break;
299 #endif
300 #ifndef HAVE_USBSTACK
301 if(usb_state == USB_INSERTED)
303 /* Only disable the USB mode if we really have enabled it
304 some threads might not have acknowledged the
305 insertion */
306 usb_slave_mode(false);
307 cpu_idle_mode(false);
309 #endif
311 usb_state = USB_EXTRACTED;
312 #ifdef HAVE_USBSTACK
313 if(exclusive_storage_access)
315 int rc = disk_mount_all();
316 if (rc <= 0) /* no partition */
317 panicf("mount: %d",rc);
318 exclusive_storage_access = false;
319 #endif
320 /* Tell all threads that we are back in business */
321 num_acks_to_expect =
322 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
323 waiting_for_ack = true;
324 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
325 num_acks_to_expect);
326 #ifdef HAVE_USBSTACK
328 #endif
329 break;
331 case SYS_USB_DISCONNECTED_ACK:
332 if(waiting_for_ack)
334 num_acks_to_expect--;
335 if(num_acks_to_expect == 0)
337 DEBUGF("All threads have acknowledged. "
338 "We're in business.\n");
339 waiting_for_ack = false;
341 else
343 DEBUGF("usb: got ack, %d to go...\n",
344 num_acks_to_expect);
347 break;
349 #ifdef HAVE_HOTSWAP
350 case SYS_HOTSWAP_INSERTED:
351 case SYS_HOTSWAP_EXTRACTED:
352 #ifdef HAVE_USBSTACK
353 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
354 #else
355 if(usb_state == USB_INSERTED)
357 usb_enable(false);
358 #if (CONFIG_STORAGE & STORAGE_MMC)
359 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
360 #endif
362 #endif
363 break;
365 case USB_REENABLE:
366 if(usb_state == USB_INSERTED)
367 usb_enable(true); /* reenable only if still inserted */
368 break;
369 #endif /* HAVE_HOTSWAP */
370 case USB_REQUEST_REBOOT:
371 #ifdef HAVE_USB_POWER
372 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
373 #endif
374 try_reboot();
375 break;
379 #endif
381 #ifdef HAVE_USBSTACK
382 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
384 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
386 #endif
388 #ifdef USB_FULL_INIT
389 static void usb_tick(void)
391 int current_status;
393 if(usb_monitor_enabled)
395 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
396 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
397 int current_firewire_status = firewire_detect();
398 if(current_firewire_status != last_firewire_status)
400 last_firewire_status = current_firewire_status;
401 firewire_countdown = NUM_POLL_READINGS;
403 else
405 /* Count down until it gets negative */
406 if(firewire_countdown >= 0)
407 firewire_countdown--;
409 /* Report to the thread if we have had 3 identical status
410 readings in a row */
411 if(firewire_countdown == 0)
413 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
416 #endif
418 current_status = usb_detect();
420 /* Only report when the status has changed */
421 if(current_status != last_usb_status)
423 last_usb_status = current_status;
424 countdown = NUM_POLL_READINGS;
426 else
428 /* Count down until it gets negative */
429 if(countdown >= 0)
430 countdown--;
432 /* Report to the thread if we have had 3 identical status
433 readings in a row */
434 if(countdown == 0)
436 queue_post(&usb_queue, current_status, 0);
440 #if (CONFIG_STORAGE & STORAGE_MMC)
441 if(usb_mmc_countdown > 0)
443 usb_mmc_countdown--;
444 if (usb_mmc_countdown == 0)
445 queue_post(&usb_queue, USB_REENABLE, 0);
447 #endif
449 #endif
451 void usb_acknowledge(long id)
453 queue_post(&usb_queue, id, 0);
456 void usb_init(void)
458 usb_state = USB_EXTRACTED;
459 #ifdef HAVE_USBSTACK
460 exclusive_storage_access = false;
461 #endif
462 usb_monitor_enabled = false;
463 countdown = -1;
465 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
466 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
467 firewire_countdown = -1;
468 last_firewire_status = false;
469 #endif
471 usb_init_device();
472 #ifdef USB_FULL_INIT
473 usb_enable(false);
474 #endif
476 /* We assume that the USB cable is extracted */
477 last_usb_status = USB_EXTRACTED;
479 #ifdef USB_FULL_INIT
480 queue_init(&usb_queue, true);
482 usb_thread_entry = create_thread(usb_thread, usb_stack,
483 sizeof(usb_stack), 0, usb_thread_name
484 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
486 tick_add_task(usb_tick);
487 #endif
491 void usb_wait_for_disconnect(struct event_queue *q)
493 struct queue_event ev;
495 /* Don't return until we get SYS_USB_DISCONNECTED */
496 while(1)
498 queue_wait(q, &ev);
499 if(ev.id == SYS_USB_DISCONNECTED)
501 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
502 return;
507 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
509 struct queue_event ev;
511 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
512 while(1)
514 queue_wait_w_tmo(q, &ev, ticks);
515 switch(ev.id)
517 case SYS_USB_DISCONNECTED:
518 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
519 return 0;
520 break;
521 case SYS_TIMEOUT:
522 return 1;
523 break;
528 void usb_start_monitoring(void)
530 usb_monitor_enabled = true;
533 #ifdef USB_DRIVER_CLOSE
534 void usb_close(void)
536 struct thread_entry *thread = usb_thread_entry;
537 usb_thread_entry = NULL;
539 if (thread == NULL)
540 return;
542 tick_remove_task(usb_tick);
543 usb_monitor_enabled = false;
545 queue_post(&usb_queue, USB_QUIT, 0);
546 thread_wait(thread);
548 #endif /* USB_DRIVER_CLOSE */
550 bool usb_inserted(void)
552 #ifdef HAVE_USB_POWER
553 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
554 #else
555 return usb_state == USB_INSERTED;
556 #endif
559 #ifdef HAVE_USBSTACK
560 void usb_request_exclusive_ata(void)
562 /* This is not really a clean place to start boosting the cpu. but it's
563 * currently the best one. We want to get rid of having to boost the cpu
564 * for usb anyway */
565 trigger_cpu_boost();
566 if(!exclusive_storage_access) {
567 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
571 void usb_release_exclusive_ata(void)
573 cancel_cpu_boost();
574 if(exclusive_storage_access) {
575 queue_post(&usb_queue, USB_RELEASE_DISK, 0);
576 exclusive_storage_access = false;
580 bool usb_exclusive_ata(void)
582 return exclusive_storage_access;
584 #endif
586 #ifdef HAVE_USB_POWER
587 bool usb_powered(void)
589 return usb_state == USB_POWERED;
592 #if CONFIG_CHARGING
593 bool usb_charging_enable(bool on)
595 bool rc = false;
596 #ifdef IRIVER_H300_SERIES
597 int irqlevel;
598 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
599 irqlevel = disable_irq_save();
600 pcf50606_set_usb_charging(on);
601 rc = on;
602 restore_irq(irqlevel);
603 #else
604 /* TODO: implement it for other targets... */
605 (void)on;
606 #endif
607 return rc;
610 bool usb_charging_enabled(void)
612 bool rc = false;
613 #ifdef IRIVER_H300_SERIES
614 /* TODO: read the state of the GPOOD2 register...
615 * (this also means to set the irq level here) */
616 rc = pcf50606_usb_charging_enabled();
617 #else
618 /* TODO: implement it for other targets... */
619 #endif
621 logf("usb charging %s", rc ? "enabled" : "disabled" );
622 return rc;
624 #endif
625 #endif
627 #else
629 #ifdef USB_NONE
630 bool usb_inserted(void)
632 return false;
634 #endif
636 /* Dummy simulator functions */
637 void usb_acknowledge(long id)
639 id = id;
642 void usb_init(void)
646 void usb_start_monitoring(void)
650 int usb_detect(void)
652 return USB_EXTRACTED;
655 void usb_wait_for_disconnect(struct event_queue *q)
657 (void)q;
660 #endif /* USB_NONE or SIMULATOR */