fix H10 not rebooting on usb connect. Apologies to H10 users who enjoyed their free...
[kugel-rb.git] / firmware / usb.c
blobaa3ab0ae69b698104886c54b9c8a2d444d88aa70
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 * All files in this archive are subject to the GNU General Public License.
18 * See the file COPYING in the source tree root for full license agreement.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "config.h"
25 #include "cpu.h"
26 #include "kernel.h"
27 #include "thread.h"
28 #include "system.h"
29 #include "debug.h"
30 #include "ata.h"
31 #include "fat.h"
32 #include "disk.h"
33 #include "panic.h"
34 #include "lcd.h"
35 #include "usb.h"
36 #include "button.h"
37 #include "sprintf.h"
38 #include "string.h"
39 #include "usb-target.h"
40 #ifdef HAVE_USBSTACK
41 #include "usb_core.h"
42 #endif
43 #ifdef IRIVER_H300_SERIES
44 #include "pcf50606.h" /* for pcf50606_usb_charging_... */
45 #endif
46 #include "logf.h"
48 extern void dbg_ports(void); /* NASTY! defined in apps/ */
50 #ifdef HAVE_LCD_BITMAP
51 bool do_screendump_instead_of_usb = false;
52 void screen_dump(void); /* Nasty again. Defined in apps/ too */
53 #endif
55 #if !defined(SIMULATOR) && !defined(USB_NONE)
57 #define NUM_POLL_READINGS (HZ/5)
58 static int countdown;
60 static int usb_state;
62 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
63 static int usb_mmc_countdown = 0;
64 #endif
66 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
67 needs updating */
68 #ifndef BOOTLOADER
69 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
70 static const char usb_thread_name[] = "usb";
71 static struct thread_entry *usb_thread_entry;
72 #endif
73 static struct event_queue usb_queue;
74 static int last_usb_status;
75 static bool usb_monitor_enabled;
76 #ifdef HAVE_USBSTACK
77 static bool exclusive_ata_access;
78 #endif
81 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
82 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
83 static int firewire_countdown;
84 static bool last_firewire_status;
85 #endif
89 #ifndef BOOTLOADER
90 #ifndef HAVE_USBSTACK
91 static void usb_slave_mode(bool on)
93 int rc;
95 if(on)
97 DEBUGF("Entering USB slave mode\n");
98 ata_soft_reset();
99 ata_init();
100 ata_enable(false);
101 usb_enable(true);
103 else
105 DEBUGF("Leaving USB slave mode\n");
107 /* Let the ISDx00 settle */
108 sleep(HZ*1);
110 usb_enable(false);
112 rc = ata_init();
113 if(rc)
115 /* fixme: can we remove this? (already such in main.c) */
116 char str[32];
117 lcd_clear_display();
118 snprintf(str, 31, "ATA error: %d", rc);
119 lcd_puts(0, 0, str);
120 lcd_puts(0, 1, "Press ON to debug");
121 lcd_update();
122 while(!(button_get(true) & BUTTON_REL)) {};
123 dbg_ports();
124 panicf("ata: %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 #ifndef HAVE_FLASH_STORAGE
138 ata_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 HAVE_USBSTACK
167 case USB_TRANSFER_COMPLETION:
168 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
169 break;
170 #endif
171 #ifdef HAVE_USB_POWER
172 case USB_POWERED:
173 usb_state = USB_POWERED;
174 break;
175 #endif
176 case USB_INSERTED:
177 #ifdef HAVE_LCD_BITMAP
178 if(do_screendump_instead_of_usb)
180 screen_dump();
182 else
183 #endif
184 #ifdef HAVE_USB_POWER
185 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
186 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
187 #else
188 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
189 #endif
191 usb_state = USB_POWERED;
192 #ifdef HAVE_USBSTACK
193 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,false);
194 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,true);
195 usb_enable(true);
196 #endif
198 else
199 #endif
201 #ifdef HAVE_USBSTACK
202 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
203 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,false);
204 usb_enable(true);
205 #else
206 /* Tell all threads that they have to back off the ATA.
207 We subtract one for our own thread. */
208 num_acks_to_expect =
209 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
210 waiting_for_ack = true;
211 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
212 num_acks_to_expect);
213 #endif
215 break;
216 #ifdef HAVE_USBSTACK
217 case USB_REQUEST_DISK:
218 if(!waiting_for_ack)
220 /* Tell all threads that they have to back off the ATA.
221 We subtract one for our own thread. */
222 num_acks_to_expect =
223 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
224 waiting_for_ack = true;
225 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
226 num_acks_to_expect);
228 break;
229 case USB_RELEASE_DISK:
230 if(!waiting_for_ack)
232 /* Tell all threads that they have to back off the ATA.
233 We subtract one for our own thread. */
234 num_acks_to_expect =
235 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
236 waiting_for_ack = true;
237 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
238 num_acks_to_expect);
240 break;
241 #endif
242 case SYS_USB_CONNECTED_ACK:
243 if(waiting_for_ack)
245 num_acks_to_expect--;
246 if(num_acks_to_expect == 0)
248 DEBUGF("All threads have acknowledged the connect.\n");
249 #ifdef HAVE_USBSTACK
250 #ifndef USE_ROCKBOX_USB
251 /* until we have native mass-storage mode, we want to reboot on
252 usb host connect */
253 try_reboot();
254 #endif /* USE_ROCKBOX_USB */
255 #ifdef HAVE_PRIORITY_SCHEDULING
256 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
257 #endif
258 exclusive_ata_access = true;
260 #else
261 usb_slave_mode(true);
262 cpu_idle_mode(true);
263 #endif
264 usb_state = USB_INSERTED;
265 waiting_for_ack = false;
267 else
269 DEBUGF("usb: got ack, %d to go...\n",
270 num_acks_to_expect);
273 break;
275 case USB_EXTRACTED:
276 #ifdef HAVE_USBSTACK
277 usb_enable(false);
278 #ifdef HAVE_PRIORITY_SCHEDULING
279 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
280 #endif
281 #endif
282 #ifdef HAVE_LCD_BITMAP
283 if(do_screendump_instead_of_usb)
284 break;
285 #endif
286 #ifdef HAVE_USB_POWER
287 if(usb_state == USB_POWERED)
289 usb_state = USB_EXTRACTED;
290 break;
292 #endif
293 #ifndef HAVE_USBSTACK
294 if(usb_state == USB_INSERTED)
296 /* Only disable the USB mode if we really have enabled it
297 some threads might not have acknowledged the
298 insertion */
299 usb_slave_mode(false);
300 cpu_idle_mode(false);
302 #endif
304 usb_state = USB_EXTRACTED;
305 #ifdef HAVE_USBSTACK
306 if(exclusive_ata_access)
308 exclusive_ata_access = false;
309 #endif
310 /* Tell all threads that we are back in business */
311 num_acks_to_expect =
312 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
313 waiting_for_ack = true;
314 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
315 num_acks_to_expect);
316 #ifdef HAVE_USBSTACK
318 #endif
319 break;
321 case SYS_USB_DISCONNECTED_ACK:
322 if(waiting_for_ack)
324 num_acks_to_expect--;
325 if(num_acks_to_expect == 0)
327 DEBUGF("All threads have acknowledged. "
328 "We're in business.\n");
329 waiting_for_ack = false;
331 else
333 DEBUGF("usb: got ack, %d to go...\n",
334 num_acks_to_expect);
337 break;
339 #ifdef HAVE_HOTSWAP
340 case SYS_HOTSWAP_INSERTED:
341 case SYS_HOTSWAP_EXTRACTED:
342 #ifdef HAVE_USBSTACK
343 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
344 #else
345 if(usb_state == USB_INSERTED)
347 usb_enable(false);
348 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
350 break;
351 #endif
353 case USB_REENABLE:
354 if(usb_state == USB_INSERTED)
355 usb_enable(true); /* reenable only if still inserted */
356 break;
357 #endif
358 case USB_REQUEST_REBOOT:
359 #ifdef HAVE_USB_POWER
360 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
361 #endif
362 try_reboot();
363 break;
367 #endif
369 #ifdef HAVE_USBSTACK
370 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
372 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
374 #endif
376 #ifndef BOOTLOADER
377 static void usb_tick(void)
379 int current_status;
381 if(usb_monitor_enabled)
383 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
384 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
385 int current_firewire_status = firewire_detect();
386 if(current_firewire_status != last_firewire_status)
388 last_firewire_status = current_firewire_status;
389 firewire_countdown = NUM_POLL_READINGS;
391 else
393 /* Count down until it gets negative */
394 if(firewire_countdown >= 0)
395 firewire_countdown--;
397 /* Report to the thread if we have had 3 identical status
398 readings in a row */
399 if(firewire_countdown == 0)
401 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
404 #endif
406 current_status = usb_detect();
408 /* Only report when the status has changed */
409 if(current_status != last_usb_status)
411 last_usb_status = current_status;
412 countdown = NUM_POLL_READINGS;
414 else
416 /* Count down until it gets negative */
417 if(countdown >= 0)
418 countdown--;
420 /* Report to the thread if we have had 3 identical status
421 readings in a row */
422 if(countdown == 0)
424 queue_post(&usb_queue, current_status, 0);
428 #ifdef HAVE_MMC
429 if(usb_mmc_countdown > 0)
431 usb_mmc_countdown--;
432 if (usb_mmc_countdown == 0)
433 queue_post(&usb_queue, USB_REENABLE, 0);
435 #endif
437 #endif
439 void usb_acknowledge(long id)
441 queue_post(&usb_queue, id, 0);
444 void usb_init(void)
446 usb_state = USB_EXTRACTED;
447 #ifdef HAVE_USBSTACK
448 exclusive_ata_access = false;
449 #endif
450 usb_monitor_enabled = false;
451 countdown = -1;
453 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
454 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
455 firewire_countdown = -1;
456 last_firewire_status = false;
457 #endif
459 usb_init_device();
460 usb_enable(false);
462 /* We assume that the USB cable is extracted */
463 last_usb_status = USB_EXTRACTED;
465 #ifndef BOOTLOADER
466 queue_init(&usb_queue, true);
468 usb_thread_entry = create_thread(usb_thread, usb_stack,
469 sizeof(usb_stack), 0, usb_thread_name
470 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
472 tick_add_task(usb_tick);
473 #endif
477 void usb_wait_for_disconnect(struct event_queue *q)
479 struct queue_event ev;
481 /* Don't return until we get SYS_USB_DISCONNECTED */
482 while(1)
484 queue_wait(q, &ev);
485 if(ev.id == SYS_USB_DISCONNECTED)
487 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
488 return;
493 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
495 struct queue_event ev;
497 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
498 while(1)
500 queue_wait_w_tmo(q, &ev, ticks);
501 switch(ev.id)
503 case SYS_USB_DISCONNECTED:
504 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
505 return 0;
506 break;
507 case SYS_TIMEOUT:
508 return 1;
509 break;
514 void usb_start_monitoring(void)
516 usb_monitor_enabled = true;
519 bool usb_inserted(void)
521 #ifdef HAVE_USB_POWER
522 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
523 #else
524 return usb_state == USB_INSERTED;
525 #endif
528 #ifdef HAVE_USBSTACK
529 void usb_request_exclusive_ata(void)
531 /* This is not really a clean place to start boosting the cpu. but it's
532 * currently the best one. We want to get rid of having to boost the cpu
533 * for usb anyway */
534 trigger_cpu_boost();
535 if(!exclusive_ata_access) {
536 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
540 void usb_release_exclusive_ata(void)
542 cancel_cpu_boost();
543 if(exclusive_ata_access) {
544 queue_post(&usb_queue, USB_RELEASE_DISK, 0);
545 exclusive_ata_access = false;
549 bool usb_exclusive_ata(void)
551 return exclusive_ata_access;
553 #endif
555 #ifdef HAVE_USB_POWER
556 bool usb_powered(void)
558 return usb_state == USB_POWERED;
561 #if CONFIG_CHARGING
562 bool usb_charging_enable(bool on)
564 bool rc = false;
565 #ifdef IRIVER_H300_SERIES
566 int irqlevel;
567 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
568 irqlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
569 pcf50606_set_usb_charging(on);
570 rc = on;
571 (void)set_irq_level(irqlevel);
572 #else
573 /* TODO: implement it for other targets... */
574 (void)on;
575 #endif
576 return rc;
579 bool usb_charging_enabled(void)
581 bool rc = false;
582 #ifdef IRIVER_H300_SERIES
583 /* TODO: read the state of the GPOOD2 register...
584 * (this also means to set the irq level here) */
585 rc = pcf50606_usb_charging_enabled();
586 #else
587 /* TODO: implement it for other targets... */
588 #endif
590 logf("usb charging %s", rc ? "enabled" : "disabled" );
591 return rc;
593 #endif
594 #endif
596 #else
598 #ifdef USB_NONE
599 bool usb_inserted(void)
601 return false;
603 #endif
605 /* Dummy simulator functions */
606 void usb_acknowledge(long id)
608 id = id;
611 void usb_init(void)
615 void usb_start_monitoring(void)
619 int usb_detect(void)
621 return USB_EXTRACTED;
624 void usb_wait_for_disconnect(struct event_queue *q)
626 (void)q;
629 #endif /* USB_NONE or SIMULATOR */