fix another keymap (FS#10237 by Tomer Shalev)
[kugel-rb.git] / firmware / usb.c
blobf918f19bc4ae895e9766340a6559a36cb58ddb3d
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(CPU_TCC77X) || defined(CPU_TCC780X))) || \
53 (CONFIG_USBOTG == USBOTG_JZ4740)
54 #define USB_FULL_INIT
55 #endif
57 #ifdef HAVE_LCD_BITMAP
58 bool do_screendump_instead_of_usb = false;
59 #endif
61 #if !defined(SIMULATOR) && !defined(USB_NONE)
63 static int usb_state;
65 #if (CONFIG_STORAGE & STORAGE_MMC) && defined(USB_FULL_INIT)
66 static int usb_mmc_countdown = 0;
67 #endif
69 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
70 needs updating */
71 #ifdef USB_FULL_INIT
72 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
73 static const char usb_thread_name[] = "usb";
74 static unsigned int usb_thread_entry = 0;
75 #ifndef USB_STATUS_BY_EVENT
76 #define NUM_POLL_READINGS (HZ/5)
77 static int countdown;
78 #endif /* USB_STATUS_BY_EVENT */
79 #endif /* USB_FULL_INIT */
80 static struct event_queue usb_queue;
81 static bool usb_monitor_enabled;
82 static int last_usb_status;
83 #ifdef HAVE_USBSTACK
84 static bool exclusive_storage_access;
85 #endif
87 #ifdef USB_FIREWIRE_HANDLING
88 static int firewire_countdown;
89 static bool last_firewire_status;
90 #endif
92 #ifdef USB_FULL_INIT
94 #if defined(USB_FIREWIRE_HANDLING) \
95 || (defined(HAVE_USBSTACK) && !defined(USE_ROCKBOX_USB))
96 static void try_reboot(void)
98 #ifdef HAVE_DISK_STORAGE
99 storage_sleepnow(); /* Immediately spindown the disk. */
100 sleep(HZ*2);
101 #endif
103 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
104 #if CONFIG_CPU == PP5020
105 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
106 #elif CONFIG_CPU == PP5022
107 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
108 #endif /* CONFIG_CPU */
109 #endif /* IPOD_ARCH */
111 system_reboot(); /* Reboot */
113 #endif /* USB_FIRWIRE_HANDLING || (HAVE_USBSTACK && !USE_ROCKBOX_USB) */
115 #ifdef HAVE_USBSTACK
116 /* inline since branch is chosen at compile time */
117 static inline void usb_slave_mode(bool on)
119 #ifdef USE_ROCKBOX_USB
120 int rc;
122 if (on)
124 trigger_cpu_boost();
125 #ifdef HAVE_PRIORITY_SCHEDULING
126 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_REALTIME);
127 #endif
128 usb_attach();
130 else /* usb_state == USB_INSERTED (only!) */
132 #ifndef USB_DETECT_BY_DRV
133 usb_enable(false);
134 #endif
135 #ifdef HAVE_PRIORITY_SCHEDULING
136 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_SYSTEM);
137 #endif
138 /* Entered exclusive mode */
139 rc = disk_mount_all();
140 if (rc <= 0) /* no partition */
141 panicf("mount: %d",rc);
143 cancel_cpu_boost();
145 #else /* !USB_ROCKBOX_USB */
146 if (on)
148 /* until we have native mass-storage mode, we want to reboot on
149 usb host connect */
150 try_reboot();
152 #endif /* USE_ROCKBOX_USB */
155 void usb_signal_transfer_completion(
156 struct usb_transfer_completion_event_data* event_data)
158 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
160 #else /* !HAVE_USBSTACK */
161 /* inline since branch is chosen at compile time */
162 static inline void usb_slave_mode(bool on)
164 int rc;
166 if(on)
168 DEBUGF("Entering USB slave mode\n");
169 storage_soft_reset();
170 storage_init();
171 storage_enable(false);
172 usb_enable(true);
173 cpu_idle_mode(true);
175 else
177 DEBUGF("Leaving USB slave mode\n");
179 cpu_idle_mode(false);
181 /* Let the ISDx00 settle */
182 sleep(HZ*1);
184 usb_enable(false);
186 rc = storage_init();
187 if(rc)
188 panicf("storage: %d",rc);
190 rc = disk_mount_all();
191 if (rc <= 0) /* no partition */
192 panicf("mount: %d",rc);
195 #endif /* HAVE_USBSTACK */
197 #ifdef HAVE_USB_POWER
198 static inline bool usb_power_button(void)
200 #if (defined(IRIVER_H10) || defined (IRIVER_H10_5GB)) && !defined(USE_ROCKBOX_USB)
201 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
202 #else
203 return (button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON;
204 #endif
207 #ifdef USB_FIREWIRE_HANDLING
208 static inline bool usb_reboot_button(void)
210 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
212 #endif
213 #endif /* HAVE_USB_POWER */
215 static void usb_thread(void)
217 int num_acks_to_expect = 0;
218 struct queue_event ev;
220 while(1)
222 queue_wait(&usb_queue, &ev);
223 switch(ev.id)
225 #ifdef USB_DRIVER_CLOSE
226 case USB_QUIT:
227 return;
228 #endif
229 #ifdef HAVE_USBSTACK
230 case USB_TRANSFER_COMPLETION:
231 usb_core_handle_transfer_completion(
232 (struct usb_transfer_completion_event_data*)ev.data);
233 break;
234 #endif
235 #ifdef USB_DETECT_BY_DRV
236 /* In this case, these events the handle cable insertion USB
237 * driver determines INSERTED/EXTRACTED state. */
238 case USB_POWERED:
239 /* Set the state to USB_POWERED for now and enable the driver
240 * to detect a bus reset only. If a bus reset is detected,
241 * USB_INSERTED will be received. */
242 usb_state = USB_POWERED;
243 usb_enable(true);
244 break;
246 case USB_UNPOWERED:
247 usb_enable(false);
248 /* This part shouldn't be obligatory for anything that can
249 * reliably detect removal of the data lines. USB_EXTRACTED
250 * could be posted on that event while bus power remains
251 * available. */
252 queue_post(&usb_queue, USB_EXTRACTED, 0);
253 break;
254 #endif /* USB_DETECT_BY_DRV */
255 case USB_INSERTED:
256 #ifdef HAVE_LCD_BITMAP
257 if(do_screendump_instead_of_usb)
259 usb_state = USB_SCREENDUMP;
260 screen_dump();
261 #ifdef HAVE_REMOTE_LCD
262 remote_screen_dump();
263 #endif
264 break;
266 #endif
267 #ifdef HAVE_USB_POWER
268 if(usb_power_button())
270 /* Only charging is desired */
271 usb_state = USB_POWERED;
272 #ifdef HAVE_USBSTACK
273 #ifdef USB_ENABLE_STORAGE
274 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, false);
275 #endif
277 #ifdef USB_ENABLE_HID
278 #ifdef USB_ENABLE_CHARGING_ONLY
279 usb_core_enable_driver(USB_DRIVER_HID, false);
280 #else
281 usb_core_enable_driver(USB_DRIVER_HID, true);
282 #endif /* USB_ENABLE_CHARGING_ONLY */
283 #endif /* USB_ENABLE_HID */
285 #ifdef USB_ENABLE_CHARGING_ONLY
286 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, true);
287 #endif
288 usb_attach();
289 #endif
290 break;
292 #endif /* HAVE_USB_POWER */
293 #ifdef HAVE_USBSTACK
294 #ifdef HAVE_USB_POWER
295 /* Set the state to USB_POWERED for now. If permission to connect
296 * by threads and storage is granted it will be changed to
297 * USB_CONNECTED. */
298 usb_state = USB_POWERED;
299 #endif
300 #ifdef USB_ENABLE_STORAGE
301 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, true);
302 #endif
303 #ifdef USB_ENABLE_HID
304 usb_core_enable_driver(USB_DRIVER_HID, true);
305 #endif
306 #ifdef USB_ENABLE_CHARGING_ONLY
307 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, false);
308 #endif
310 /* Check any drivers enabled at this point for exclusive storage
311 * access requirements. */
312 exclusive_storage_access = usb_core_any_exclusive_storage();
314 if(!exclusive_storage_access)
316 usb_attach();
317 break;
319 #endif /* HAVE_USBSTACK */
320 /* Tell all threads that they have to back off the storage.
321 We subtract one for our own thread. */
322 num_acks_to_expect = queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
323 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
324 num_acks_to_expect);
325 break;
327 case SYS_USB_CONNECTED_ACK:
328 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
330 DEBUGF("All threads have acknowledged the connect.\n");
331 usb_slave_mode(true);
332 usb_state = USB_INSERTED;
334 else
336 DEBUGF("usb: got ack, %d to go...\n",
337 num_acks_to_expect);
339 break;
341 case USB_EXTRACTED:
342 #ifdef HAVE_LCD_BITMAP
343 if(usb_state == USB_SCREENDUMP)
345 usb_state = USB_EXTRACTED;
346 break; /* Connected for screendump only */
348 #endif
349 #ifndef HAVE_USBSTACK /* Stack must undo this if POWERED state was transitional */
350 #ifdef HAVE_USB_POWER
351 if(usb_state == USB_POWERED)
353 usb_state = USB_EXTRACTED;
354 break;
356 #endif
357 #endif /* HAVE_USBSTACK */
358 if(usb_state == USB_INSERTED)
360 /* Only disable the USB mode if we really have enabled it
361 some threads might not have acknowledged the
362 insertion */
363 usb_slave_mode(false);
366 usb_state = USB_EXTRACTED;
367 #ifdef HAVE_USBSTACK
368 if(!exclusive_storage_access)
370 #ifndef USB_DETECT_BY_DRV /* Disabled handling USB_UNPOWERED */
371 usb_enable(false);
372 #endif
373 break;
376 #endif /* HAVE_USBSTACK */
377 num_acks_to_expect = usb_release_exclusive_storage();
379 break;
381 case SYS_USB_DISCONNECTED_ACK:
382 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
384 DEBUGF("All threads have acknowledged. "
385 "We're in business.\n");
387 else
389 DEBUGF("usb: got ack, %d to go...\n",
390 num_acks_to_expect);
392 break;
394 #ifdef HAVE_HOTSWAP
395 case SYS_HOTSWAP_INSERTED:
396 case SYS_HOTSWAP_EXTRACTED:
397 #ifdef HAVE_USBSTACK
398 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
399 #else /* !HAVE_USBSTACK */
400 if(usb_state == USB_INSERTED)
402 usb_enable(false);
403 #if (CONFIG_STORAGE & STORAGE_MMC)
404 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
405 #endif /* STORAGE_MMC */
407 #endif /* HAVE_USBSTACK */
408 break;
410 #if (CONFIG_STORAGE & STORAGE_MMC)
411 case USB_REENABLE:
412 if(usb_state == USB_INSERTED)
413 usb_enable(true); /* reenable only if still inserted */
414 break;
415 #endif /* STORAGE_MMC */
416 #endif /* HAVE_HOTSWAP */
418 #ifdef USB_FIREWIRE_HANDLING
419 case USB_REQUEST_REBOOT:
420 #ifdef HAVE_USB_POWER
421 if (usb_reboot_button())
422 #endif
423 try_reboot();
424 break;
425 #endif /* USB_FIREWIRE_HANDLING */
430 #ifdef USB_STATUS_BY_EVENT
431 void usb_status_event(int current_status)
433 /* Caller isn't expected to filter for changes in status.
434 * current_status:
435 * USB_DETECT_BY_DRV: USB_POWERED, USB_UNPOWERED, USB_INSERTED (driver)
436 * else: USB_INSERTED, USB_EXTRACTED
438 if(usb_monitor_enabled)
440 int oldstatus = disable_irq_save(); /* Dual-use function */
442 if(last_usb_status != current_status)
444 last_usb_status = current_status;
445 queue_post(&usb_queue, current_status, 0);
448 restore_irq(oldstatus);
452 void usb_start_monitoring(void)
454 int oldstatus = disable_irq_save(); /* Sync to event */
455 int status = usb_detect();
457 usb_monitor_enabled = true;
459 #ifdef USB_DETECT_BY_DRV
460 status = (status == USB_INSERTED) ? USB_POWERED : USB_UNPOWERED;
461 #endif
462 usb_status_event(status);
464 #ifdef USB_FIREWIRE_HANDLING
465 if (firewire_detect())
466 usb_firewire_connect_event();
467 #endif
469 restore_irq(oldstatus);
472 #ifdef USB_FIREWIRE_HANDLING
473 void usb_firewire_connect_event(void)
475 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
477 #endif /* USB_FIREWIRE_HANDLING */
478 #else /* !USB_STATUS_BY_EVENT */
479 static void usb_tick(void)
481 int current_status;
483 if(usb_monitor_enabled)
485 #ifdef USB_FIREWIRE_HANDLING
486 int current_firewire_status = firewire_detect();
487 if(current_firewire_status != last_firewire_status)
489 last_firewire_status = current_firewire_status;
490 firewire_countdown = NUM_POLL_READINGS;
492 else
494 /* Count down until it gets negative */
495 if(firewire_countdown >= 0)
496 firewire_countdown--;
498 /* Report to the thread if we have had 3 identical status
499 readings in a row */
500 if(firewire_countdown == 0)
502 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
505 #endif /* USB_FIREWIRE_HANDLING */
507 current_status = usb_detect();
509 /* Only report when the status has changed */
510 if(current_status != last_usb_status)
512 last_usb_status = current_status;
513 countdown = NUM_POLL_READINGS;
515 else
517 /* Count down until it gets negative */
518 if(countdown >= 0)
519 countdown--;
521 /* Report to the thread if we have had 3 identical status
522 readings in a row */
523 if(countdown == 0)
525 queue_post(&usb_queue, current_status, 0);
529 #if (CONFIG_STORAGE & STORAGE_MMC)
530 if(usb_mmc_countdown > 0)
532 usb_mmc_countdown--;
533 if (usb_mmc_countdown == 0)
534 queue_post(&usb_queue, USB_REENABLE, 0);
536 #endif
539 void usb_start_monitoring(void)
541 usb_monitor_enabled = true;
543 #endif /* USB_STATUS_BY_EVENT */
544 #endif /* USB_FULL_INIT */
546 void usb_acknowledge(long id)
548 queue_post(&usb_queue, id, 0);
551 void usb_init(void)
553 /* We assume that the USB cable is extracted */
554 usb_state = USB_EXTRACTED;
555 #ifdef USB_DETECT_BY_DRV
556 last_usb_status = USB_UNPOWERED;
557 #else
558 last_usb_status = USB_EXTRACTED;
559 #endif
560 usb_monitor_enabled = false;
562 #ifdef HAVE_USBSTACK
563 exclusive_storage_access = false;
564 #endif
566 #ifdef USB_FIREWIRE_HANDLING
567 firewire_countdown = -1;
568 last_firewire_status = false;
569 #endif
571 usb_init_device();
572 #ifdef USB_FULL_INIT
573 usb_enable(false);
575 queue_init(&usb_queue, true);
577 usb_thread_entry = create_thread(usb_thread, usb_stack,
578 sizeof(usb_stack), 0, usb_thread_name
579 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
581 #ifndef USB_STATUS_BY_EVENT
582 countdown = -1;
583 tick_add_task(usb_tick);
584 #endif
585 #endif /* USB_FULL_INIT */
588 void usb_wait_for_disconnect(struct event_queue *q)
590 #ifdef USB_FULL_INIT
591 struct queue_event ev;
593 /* Don't return until we get SYS_USB_DISCONNECTED */
594 while(1)
596 queue_wait(q, &ev);
597 if(ev.id == SYS_USB_DISCONNECTED)
599 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
600 return;
603 #else
604 (void)q;
605 #endif /* USB_FULL_INIT */
608 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
610 #ifdef USB_FULL_INIT
611 struct queue_event ev;
613 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
614 while(1)
616 queue_wait_w_tmo(q, &ev, ticks);
617 switch(ev.id)
619 case SYS_USB_DISCONNECTED:
620 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
621 return 0;
622 case SYS_TIMEOUT:
623 return 1;
626 #else
627 (void)q; (void)ticks;
628 return 0;
629 #endif /* USB_FULL_INIT */
632 #ifdef USB_DRIVER_CLOSE
633 void usb_close(void)
635 unsigned int thread = usb_thread_entry;
636 usb_thread_entry = 0;
638 if (thread == 0)
639 return;
641 #ifndef USB_STATUS_BY_EVENT
642 tick_remove_task(usb_tick);
643 #endif
644 usb_monitor_enabled = false;
646 queue_post(&usb_queue, USB_QUIT, 0);
647 thread_wait(thread);
649 #endif /* USB_DRIVER_CLOSE */
651 bool usb_inserted(void)
653 #ifdef HAVE_USB_POWER
654 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
655 #else
656 return usb_state == USB_INSERTED;
657 #endif
660 #ifdef HAVE_USBSTACK
661 bool usb_exclusive_storage(void)
663 return exclusive_storage_access;
665 #endif
667 int usb_release_exclusive_storage(void)
669 int num_acks_to_expect;
670 #ifdef HAVE_USBSTACK
671 exclusive_storage_access = false;
672 #endif /* HAVE_USBSTACK */
673 /* Tell all threads that we are back in business */
674 num_acks_to_expect =
675 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
676 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
677 num_acks_to_expect);
678 return num_acks_to_expect;
681 #ifdef HAVE_USB_POWER
682 bool usb_powered(void)
684 return usb_state == USB_POWERED;
686 #endif
688 #else
690 #ifdef USB_NONE
691 bool usb_inserted(void)
693 return false;
695 #endif
697 /* Dummy simulator functions */
698 void usb_acknowledge(long id)
700 id = id;
703 void usb_init(void)
707 void usb_start_monitoring(void)
711 int usb_detect(void)
713 return USB_EXTRACTED;
716 void usb_wait_for_disconnect(struct event_queue *q)
718 (void)q;
721 #endif /* USB_NONE or SIMULATOR */