Smaller & faster grey blitting on greyscale ipods, based on an idea by Mike Sevakis...
[kugel-rb.git] / firmware / usb.c
blobc615e97d2b643f21891efb4d6e679cfef56a9515
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(TOSHIBA_GIGABEAT_S) && defined(USE_ROCKBOX_USB) && defined(USB_ENABLE_STORAGE)) || \
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 #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 #ifdef IPOD_NANO2G
113 memcpy((void *)0x0002bf00, "diskmodehotstuff\1\0\0\0", 20);
114 #endif
116 system_reboot(); /* Reboot */
118 #endif /* USB_FIRWIRE_HANDLING || (HAVE_USBSTACK && !USE_ROCKBOX_USB) */
120 #ifdef HAVE_USBSTACK
121 /* inline since branch is chosen at compile time */
122 static inline void usb_slave_mode(bool on)
124 #ifdef USE_ROCKBOX_USB
125 int rc;
127 if (on)
129 trigger_cpu_boost();
130 #ifdef HAVE_PRIORITY_SCHEDULING
131 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_REALTIME);
132 #endif
133 usb_attach();
135 else /* usb_state == USB_INSERTED (only!) */
137 #ifndef USB_DETECT_BY_DRV
138 usb_enable(false);
139 #endif
140 #ifdef HAVE_PRIORITY_SCHEDULING
141 thread_set_priority(THREAD_ID_CURRENT, PRIORITY_SYSTEM);
142 #endif
143 /* Entered exclusive mode */
144 rc = disk_mount_all();
145 if (rc <= 0) /* no partition */
146 panicf("mount: %d",rc);
148 cancel_cpu_boost();
150 #else /* !USE_ROCKBOX_USB */
151 if (on)
153 /* until we have native mass-storage mode, we want to reboot on
154 usb host connect */
155 try_reboot();
157 #endif /* USE_ROCKBOX_USB */
160 void usb_signal_transfer_completion(
161 struct usb_transfer_completion_event_data* event_data)
163 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
165 #else /* !HAVE_USBSTACK */
166 /* inline since branch is chosen at compile time */
167 static inline void usb_slave_mode(bool on)
169 int rc;
171 if(on)
173 DEBUGF("Entering USB slave mode\n");
174 storage_soft_reset();
175 storage_init();
176 storage_enable(false);
177 usb_enable(true);
178 cpu_idle_mode(true);
180 else
182 DEBUGF("Leaving USB slave mode\n");
184 cpu_idle_mode(false);
186 /* Let the ISDx00 settle */
187 sleep(HZ*1);
189 usb_enable(false);
191 rc = storage_init();
192 if(rc)
193 panicf("storage: %d",rc);
195 rc = disk_mount_all();
196 if (rc <= 0) /* no partition */
197 panicf("mount: %d",rc);
200 #endif /* HAVE_USBSTACK */
202 #ifdef HAVE_USB_POWER
203 static inline bool usb_power_button(void)
205 #if (defined(IRIVER_H10) || defined (IRIVER_H10_5GB)) && !defined(USE_ROCKBOX_USB)
206 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
207 #else
208 return (button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON;
209 #endif
212 #ifdef USB_FIREWIRE_HANDLING
213 static inline bool usb_reboot_button(void)
215 return (button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON;
217 #endif
218 #endif /* HAVE_USB_POWER */
220 #ifdef USB_ENABLE_HID
221 static bool usb_hid = true;
222 #endif
224 static void usb_thread(void)
226 int num_acks_to_expect = 0;
227 struct queue_event ev;
229 while(1)
231 queue_wait(&usb_queue, &ev);
232 switch(ev.id)
234 #ifdef USB_DRIVER_CLOSE
235 case USB_QUIT:
236 return;
237 #endif
238 #ifdef HAVE_USBSTACK
239 case USB_TRANSFER_COMPLETION:
240 usb_core_handle_transfer_completion(
241 (struct usb_transfer_completion_event_data*)ev.data);
242 break;
243 #endif
244 #ifdef USB_DETECT_BY_DRV
245 /* In this case, these events the handle cable insertion USB
246 * driver determines INSERTED/EXTRACTED state. */
247 case USB_POWERED:
248 /* Set the state to USB_POWERED for now and enable the driver
249 * to detect a bus reset only. If a bus reset is detected,
250 * USB_INSERTED will be received. */
251 usb_state = USB_POWERED;
252 usb_enable(true);
253 break;
255 case USB_UNPOWERED:
256 usb_enable(false);
257 /* This part shouldn't be obligatory for anything that can
258 * reliably detect removal of the data lines. USB_EXTRACTED
259 * could be posted on that event while bus power remains
260 * available. */
261 queue_post(&usb_queue, USB_EXTRACTED, 0);
262 break;
263 #endif /* USB_DETECT_BY_DRV */
264 case USB_INSERTED:
265 #ifdef HAVE_LCD_BITMAP
266 if(do_screendump_instead_of_usb)
268 usb_state = USB_SCREENDUMP;
269 screen_dump();
270 #ifdef HAVE_REMOTE_LCD
271 remote_screen_dump();
272 #endif
273 break;
275 #endif
276 #ifdef HAVE_USB_POWER
277 if(usb_power_button())
279 /* Only charging is desired */
280 usb_state = USB_POWERED;
281 #ifdef HAVE_USBSTACK
282 #ifdef USB_ENABLE_STORAGE
283 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, false);
284 #endif
286 #ifdef USB_ENABLE_HID
287 #ifdef USB_ENABLE_CHARGING_ONLY
288 usb_core_enable_driver(USB_DRIVER_HID, false);
289 #else
290 usb_core_enable_driver(USB_DRIVER_HID, true);
291 #endif /* USB_ENABLE_CHARGING_ONLY */
292 #endif /* USB_ENABLE_HID */
294 #ifdef USB_ENABLE_CHARGING_ONLY
295 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, true);
296 #endif
297 usb_attach();
298 #endif
299 break;
301 #endif /* HAVE_USB_POWER */
302 #ifdef HAVE_USBSTACK
303 #ifdef HAVE_USB_POWER
304 /* Set the state to USB_POWERED for now. If permission to connect
305 * by threads and storage is granted it will be changed to
306 * USB_CONNECTED. */
307 usb_state = USB_POWERED;
308 #endif
309 #ifdef USB_ENABLE_STORAGE
310 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, true);
311 #endif
312 #ifdef USB_ENABLE_HID
313 usb_core_enable_driver(USB_DRIVER_HID, usb_hid);
314 #endif
315 #ifdef USB_ENABLE_CHARGING_ONLY
316 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, false);
317 #endif
319 /* Check any drivers enabled at this point for exclusive storage
320 * access requirements. */
321 exclusive_storage_access = usb_core_any_exclusive_storage();
323 if(!exclusive_storage_access)
325 usb_attach();
326 break;
328 #endif /* HAVE_USBSTACK */
329 /* Tell all threads that they have to back off the storage.
330 We subtract one for our own thread. */
331 num_acks_to_expect = queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
332 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
333 num_acks_to_expect);
334 break;
336 case SYS_USB_CONNECTED_ACK:
337 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
339 DEBUGF("All threads have acknowledged the connect.\n");
340 usb_slave_mode(true);
341 usb_state = USB_INSERTED;
343 else
345 DEBUGF("usb: got ack, %d to go...\n",
346 num_acks_to_expect);
348 break;
350 case USB_EXTRACTED:
351 #ifdef HAVE_LCD_BITMAP
352 if(usb_state == USB_SCREENDUMP)
354 usb_state = USB_EXTRACTED;
355 break; /* Connected for screendump only */
357 #endif
358 #ifndef HAVE_USBSTACK /* Stack must undo this if POWERED state was transitional */
359 #ifdef HAVE_USB_POWER
360 if(usb_state == USB_POWERED)
362 usb_state = USB_EXTRACTED;
363 break;
365 #endif
366 #endif /* HAVE_USBSTACK */
367 if(usb_state == USB_INSERTED)
369 /* Only disable the USB mode if we really have enabled it
370 some threads might not have acknowledged the
371 insertion */
372 usb_slave_mode(false);
375 usb_state = USB_EXTRACTED;
376 #ifdef HAVE_USBSTACK
377 if(!exclusive_storage_access)
379 #ifndef USB_DETECT_BY_DRV /* Disabled handling USB_UNPOWERED */
380 usb_enable(false);
381 #endif
382 break;
385 #endif /* HAVE_USBSTACK */
386 num_acks_to_expect = usb_release_exclusive_storage();
388 break;
390 case SYS_USB_DISCONNECTED_ACK:
391 if(num_acks_to_expect > 0 && --num_acks_to_expect == 0)
393 DEBUGF("All threads have acknowledged. "
394 "We're in business.\n");
396 else
398 DEBUGF("usb: got ack, %d to go...\n",
399 num_acks_to_expect);
401 break;
403 #ifdef HAVE_HOTSWAP
404 case SYS_HOTSWAP_INSERTED:
405 case SYS_HOTSWAP_EXTRACTED:
406 #ifdef HAVE_USBSTACK
407 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
408 #else /* !HAVE_USBSTACK */
409 if(usb_state == USB_INSERTED)
411 usb_enable(false);
412 #if (CONFIG_STORAGE & STORAGE_MMC)
413 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
414 #endif /* STORAGE_MMC */
416 #endif /* HAVE_USBSTACK */
417 break;
419 #if (CONFIG_STORAGE & STORAGE_MMC)
420 case USB_REENABLE:
421 if(usb_state == USB_INSERTED)
422 usb_enable(true); /* reenable only if still inserted */
423 break;
424 #endif /* STORAGE_MMC */
425 #endif /* HAVE_HOTSWAP */
427 #ifdef USB_FIREWIRE_HANDLING
428 case USB_REQUEST_REBOOT:
429 #ifdef HAVE_USB_POWER
430 if (usb_reboot_button())
431 #endif
432 try_reboot();
433 break;
434 #endif /* USB_FIREWIRE_HANDLING */
439 #ifdef USB_STATUS_BY_EVENT
440 void usb_status_event(int current_status)
442 /* Caller isn't expected to filter for changes in status.
443 * current_status:
444 * USB_DETECT_BY_DRV: USB_POWERED, USB_UNPOWERED, USB_INSERTED (driver)
445 * else: USB_INSERTED, USB_EXTRACTED
447 if(usb_monitor_enabled)
449 int oldstatus = disable_irq_save(); /* Dual-use function */
451 if(last_usb_status != current_status)
453 last_usb_status = current_status;
454 queue_post(&usb_queue, current_status, 0);
457 restore_irq(oldstatus);
461 void usb_start_monitoring(void)
463 int oldstatus = disable_irq_save(); /* Sync to event */
464 int status = usb_detect();
466 usb_monitor_enabled = true;
468 #ifdef USB_DETECT_BY_DRV
469 status = (status == USB_INSERTED) ? USB_POWERED : USB_UNPOWERED;
470 #endif
471 usb_status_event(status);
473 #ifdef USB_FIREWIRE_HANDLING
474 if (firewire_detect())
475 usb_firewire_connect_event();
476 #endif
478 restore_irq(oldstatus);
481 #ifdef USB_FIREWIRE_HANDLING
482 void usb_firewire_connect_event(void)
484 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
486 #endif /* USB_FIREWIRE_HANDLING */
487 #else /* !USB_STATUS_BY_EVENT */
488 static void usb_tick(void)
490 int current_status;
492 if(usb_monitor_enabled)
494 #ifdef USB_FIREWIRE_HANDLING
495 int current_firewire_status = firewire_detect();
496 if(current_firewire_status != last_firewire_status)
498 last_firewire_status = current_firewire_status;
499 firewire_countdown = NUM_POLL_READINGS;
501 else
503 /* Count down until it gets negative */
504 if(firewire_countdown >= 0)
505 firewire_countdown--;
507 /* Report to the thread if we have had 3 identical status
508 readings in a row */
509 if(firewire_countdown == 0)
511 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
514 #endif /* USB_FIREWIRE_HANDLING */
516 current_status = usb_detect();
518 /* Only report when the status has changed */
519 if(current_status != last_usb_status)
521 last_usb_status = current_status;
522 countdown = NUM_POLL_READINGS;
524 else
526 /* Count down until it gets negative */
527 if(countdown >= 0)
528 countdown--;
530 /* Report to the thread if we have had 3 identical status
531 readings in a row */
532 if(countdown == 0)
534 queue_post(&usb_queue, current_status, 0);
538 #if (CONFIG_STORAGE & STORAGE_MMC)
539 if(usb_mmc_countdown > 0)
541 usb_mmc_countdown--;
542 if (usb_mmc_countdown == 0)
543 queue_post(&usb_queue, USB_REENABLE, 0);
545 #endif
548 void usb_start_monitoring(void)
550 usb_monitor_enabled = true;
552 #endif /* USB_STATUS_BY_EVENT */
553 #endif /* USB_FULL_INIT */
555 void usb_acknowledge(long id)
557 queue_post(&usb_queue, id, 0);
560 void usb_init(void)
562 /* We assume that the USB cable is extracted */
563 usb_state = USB_EXTRACTED;
564 #ifdef USB_DETECT_BY_DRV
565 last_usb_status = USB_UNPOWERED;
566 #else
567 last_usb_status = USB_EXTRACTED;
568 #endif
569 usb_monitor_enabled = false;
571 #ifdef HAVE_USBSTACK
572 exclusive_storage_access = false;
573 #endif
575 #ifdef USB_FIREWIRE_HANDLING
576 firewire_countdown = -1;
577 last_firewire_status = false;
578 #endif
580 usb_init_device();
581 #ifdef USB_FULL_INIT
582 usb_enable(false);
584 queue_init(&usb_queue, true);
586 usb_thread_entry = create_thread(usb_thread, usb_stack,
587 sizeof(usb_stack), 0, usb_thread_name
588 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
590 #ifndef USB_STATUS_BY_EVENT
591 countdown = -1;
592 tick_add_task(usb_tick);
593 #endif
594 #endif /* USB_FULL_INIT */
597 void usb_wait_for_disconnect(struct event_queue *q)
599 #ifdef USB_FULL_INIT
600 struct queue_event ev;
602 /* Don't return until we get SYS_USB_DISCONNECTED */
603 while(1)
605 queue_wait(q, &ev);
606 if(ev.id == SYS_USB_DISCONNECTED)
608 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
609 return;
612 #else
613 (void)q;
614 #endif /* USB_FULL_INIT */
617 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
619 #ifdef USB_FULL_INIT
620 struct queue_event ev;
622 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
623 while(1)
625 queue_wait_w_tmo(q, &ev, ticks);
626 switch(ev.id)
628 case SYS_USB_DISCONNECTED:
629 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
630 return 0;
631 case SYS_TIMEOUT:
632 return 1;
635 #else
636 (void)q; (void)ticks;
637 return 0;
638 #endif /* USB_FULL_INIT */
641 #ifdef USB_DRIVER_CLOSE
642 void usb_close(void)
644 unsigned int thread = usb_thread_entry;
645 usb_thread_entry = 0;
647 if (thread == 0)
648 return;
650 #ifndef USB_STATUS_BY_EVENT
651 tick_remove_task(usb_tick);
652 #endif
653 usb_monitor_enabled = false;
655 queue_post(&usb_queue, USB_QUIT, 0);
656 thread_wait(thread);
658 #endif /* USB_DRIVER_CLOSE */
660 bool usb_inserted(void)
662 #ifdef HAVE_USB_POWER
663 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
664 #else
665 return usb_state == USB_INSERTED;
666 #endif
669 #ifdef HAVE_USBSTACK
670 bool usb_exclusive_storage(void)
672 return exclusive_storage_access;
674 #endif
676 int usb_release_exclusive_storage(void)
678 int num_acks_to_expect;
679 #ifdef HAVE_USBSTACK
680 exclusive_storage_access = false;
681 #endif /* HAVE_USBSTACK */
682 /* Tell all threads that we are back in business */
683 num_acks_to_expect =
684 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
685 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
686 num_acks_to_expect);
687 return num_acks_to_expect;
690 #ifdef HAVE_USB_POWER
691 bool usb_powered(void)
693 return usb_state == USB_POWERED;
695 #endif
697 #ifdef USB_ENABLE_HID
698 void usb_set_hid(bool enable)
700 usb_hid = enable;
701 usb_core_enable_driver(USB_DRIVER_HID, usb_hid);
703 #endif
705 #else
707 #ifdef USB_NONE
708 bool usb_inserted(void)
710 return false;
712 #endif
714 /* Dummy simulator functions */
715 void usb_acknowledge(long id)
717 id = id;
720 void usb_init(void)
724 void usb_start_monitoring(void)
728 int usb_detect(void)
730 return USB_EXTRACTED;
733 void usb_wait_for_disconnect(struct event_queue *q)
735 (void)q;
738 #endif /* USB_NONE or SIMULATOR */