Make it not reboot when plugged into a wall charger with USE_ROCKBOX_USB diabled...
[Rockbox.git] / firmware / usb.c
blob725dd5ed1d913d9031db5e4f45f2a12977b0e0cf
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 static bool exclusive_disk;
79 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
80 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
81 static int firewire_countdown;
82 static bool last_firewire_status;
83 #endif
87 #ifndef BOOTLOADER
88 #ifndef HAVE_USBSTACK
89 static void usb_slave_mode(bool on)
91 int rc;
93 if(on)
95 DEBUGF("Entering USB slave mode\n");
96 ata_soft_reset();
97 ata_init();
98 ata_enable(false);
99 usb_enable(true);
101 else
103 DEBUGF("Leaving USB slave mode\n");
105 /* Let the ISDx00 settle */
106 sleep(HZ*1);
108 usb_enable(false);
110 rc = ata_init();
111 if(rc)
113 /* fixme: can we remove this? (already such in main.c) */
114 char str[32];
115 lcd_clear_display();
116 snprintf(str, 31, "ATA error: %d", rc);
117 lcd_puts(0, 0, str);
118 lcd_puts(0, 1, "Press ON to debug");
119 lcd_update();
120 while(!(button_get(true) & BUTTON_REL)) {};
121 dbg_ports();
122 panicf("ata: %d",rc);
125 rc = disk_mount_all();
126 if (rc <= 0) /* no partition */
127 panicf("mount: %d",rc);
131 #endif
133 static void try_reboot(void)
135 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
136 if (button_status()==BUTTON_RIGHT)
137 #endif /* defined(IRIVER_H10) || defined (IRIVER_H10_5GB) */
139 #ifndef HAVE_FLASH_STORAGE
140 ata_sleepnow(); /* Immediately spindown the disk. */
141 sleep(HZ*2);
142 #endif
144 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
145 #if CONFIG_CPU == PP5020
146 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
147 #elif CONFIG_CPU == PP5022
148 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
149 #endif /* CONFIG_CPU */
150 #endif /* IPOD_ARCH */
152 system_reboot(); /* Reboot */
156 static void usb_thread(void)
158 int num_acks_to_expect = -1;
159 bool waiting_for_ack;
160 struct queue_event ev;
162 waiting_for_ack = false;
164 while(1)
166 queue_wait(&usb_queue, &ev);
167 switch(ev.id)
169 #ifdef HAVE_USBSTACK
170 case USB_TRANSFER_COMPLETION:
171 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
172 break;
173 #endif
174 #ifdef HAVE_USB_POWER
175 case USB_POWERED:
176 usb_state = USB_POWERED;
177 break;
178 #endif
179 case USB_INSERTED:
180 #ifdef HAVE_LCD_BITMAP
181 if(do_screendump_instead_of_usb)
183 screen_dump();
185 else
186 #endif
187 #ifdef HAVE_USB_POWER
188 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
190 usb_state = USB_POWERED;
191 #ifdef HAVE_USBSTACK
192 usb_core_enable_protocol(USB_DRIVER_MASS_STORAGE,false);
193 usb_core_enable_protocol(USB_DRIVER_SERIAL,false);/* TODO: add debug setting */
194 usb_core_enable_protocol(USB_DRIVER_CHARGING_ONLY,true);
195 usb_enable(true);
196 #endif
198 else
199 #endif
201 #ifdef HAVE_USBSTACK
202 usb_core_enable_protocol(USB_DRIVER_MASS_STORAGE,true);
203 usb_core_enable_protocol(USB_DRIVER_SERIAL,false);/* TODO: add debug setting */
204 usb_core_enable_protocol(USB_DRIVER_CHARGING_ONLY,false);
205 usb_enable(true);
206 #else
207 /* Tell all threads that they have to back off the ATA.
208 We subtract one for our own thread. */
209 num_acks_to_expect =
210 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
211 waiting_for_ack = true;
212 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
213 num_acks_to_expect);
214 #endif
216 break;
217 #ifdef HAVE_USBSTACK
218 case USB_REQUEST_DISK:
219 if(!waiting_for_ack)
221 /* Tell all threads that they have to back off the ATA.
222 We subtract one for our own thread. */
223 num_acks_to_expect =
224 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
225 waiting_for_ack = true;
226 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
227 num_acks_to_expect);
229 break;
230 #endif
231 case SYS_USB_CONNECTED_ACK:
232 if(waiting_for_ack)
234 num_acks_to_expect--;
235 if(num_acks_to_expect == 0)
237 DEBUGF("All threads have acknowledged the connect.\n");
238 #ifdef HAVE_USBSTACK
239 #ifndef USE_ROCKBOX_USB
240 /* until we have native mass-storage mode, we want to reboot on
241 usb host connect */
242 try_reboot();
243 #endif /* USE_ROCKBOX_USB */
244 #ifdef HAVE_PRIORITY_SCHEDULING
245 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
246 exclusive_disk = true;
247 #endif
249 #else
250 usb_slave_mode(true);
251 cpu_idle_mode(true);
252 #endif
253 usb_state = USB_INSERTED;
254 waiting_for_ack = false;
256 else
258 DEBUGF("usb: got ack, %d to go...\n",
259 num_acks_to_expect);
262 break;
264 case USB_EXTRACTED:
265 #ifdef HAVE_USBSTACK
266 usb_enable(false);
267 exclusive_disk = false;
268 #ifdef HAVE_PRIORITY_SCHEDULING
269 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
270 #endif
271 #endif
272 #ifdef HAVE_LCD_BITMAP
273 if(do_screendump_instead_of_usb)
274 break;
275 #endif
276 #ifdef HAVE_USB_POWER
277 if(usb_state == USB_POWERED)
279 usb_state = USB_EXTRACTED;
280 break;
282 #endif
283 #ifndef HAVE_USBSTACK
284 if(usb_state == USB_INSERTED)
286 /* Only disable the USB mode if we really have enabled it
287 some threads might not have acknowledged the
288 insertion */
289 usb_slave_mode(false);
290 cpu_idle_mode(false);
292 #endif
294 usb_state = USB_EXTRACTED;
296 /* Tell all threads that we are back in business */
297 num_acks_to_expect =
298 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
299 waiting_for_ack = true;
300 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
301 num_acks_to_expect);
302 break;
304 case SYS_USB_DISCONNECTED_ACK:
305 if(waiting_for_ack)
307 num_acks_to_expect--;
308 if(num_acks_to_expect == 0)
310 DEBUGF("All threads have acknowledged. "
311 "We're in business.\n");
312 waiting_for_ack = false;
314 else
316 DEBUGF("usb: got ack, %d to go...\n",
317 num_acks_to_expect);
320 break;
322 #ifdef HAVE_MMC
323 case SYS_HOTSWAP_INSERTED:
324 case SYS_HOTSWAP_EXTRACTED:
325 if(usb_state == USB_INSERTED)
327 usb_enable(false);
328 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
330 break;
332 case USB_REENABLE:
333 if(usb_state == USB_INSERTED)
334 usb_enable(true); /* reenable only if still inserted */
335 break;
336 #endif
337 case USB_REQUEST_REBOOT:
338 #ifdef HAVE_USB_POWER
339 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
340 #endif
341 try_reboot();
342 break;
346 #endif
348 #ifdef HAVE_USBSTACK
349 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
351 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
353 #endif
355 #ifndef BOOTLOADER
356 static void usb_tick(void)
358 int current_status;
360 if(usb_monitor_enabled)
362 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
363 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
364 int current_firewire_status = firewire_detect();
365 if(current_firewire_status != last_firewire_status)
367 last_firewire_status = current_firewire_status;
368 firewire_countdown = NUM_POLL_READINGS;
370 else
372 /* Count down until it gets negative */
373 if(firewire_countdown >= 0)
374 firewire_countdown--;
376 /* Report to the thread if we have had 3 identical status
377 readings in a row */
378 if(firewire_countdown == 0)
380 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
383 #endif
385 current_status = usb_detect();
387 /* Only report when the status has changed */
388 if(current_status != last_usb_status)
390 last_usb_status = current_status;
391 countdown = NUM_POLL_READINGS;
393 else
395 /* Count down until it gets negative */
396 if(countdown >= 0)
397 countdown--;
399 /* Report to the thread if we have had 3 identical status
400 readings in a row */
401 if(countdown == 0)
403 queue_post(&usb_queue, current_status, 0);
407 #ifdef HAVE_MMC
408 if(usb_mmc_countdown > 0)
410 usb_mmc_countdown--;
411 if (usb_mmc_countdown == 0)
412 queue_post(&usb_queue, USB_REENABLE, 0);
414 #endif
416 #endif
418 void usb_acknowledge(long id)
420 queue_post(&usb_queue, id, 0);
423 void usb_init(void)
425 usb_state = USB_EXTRACTED;
426 exclusive_disk = false;
427 usb_monitor_enabled = false;
428 countdown = -1;
430 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
431 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
432 firewire_countdown = -1;
433 last_firewire_status = false;
434 #endif
436 usb_init_device();
437 usb_enable(false);
439 /* We assume that the USB cable is extracted */
440 last_usb_status = USB_EXTRACTED;
442 #ifndef BOOTLOADER
443 queue_init(&usb_queue, true);
445 usb_thread_entry = create_thread(usb_thread, usb_stack,
446 sizeof(usb_stack), 0, usb_thread_name
447 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
449 tick_add_task(usb_tick);
450 #endif
454 void usb_wait_for_disconnect(struct event_queue *q)
456 struct queue_event ev;
458 /* Don't return until we get SYS_USB_DISCONNECTED */
459 while(1)
461 queue_wait(q, &ev);
462 if(ev.id == SYS_USB_DISCONNECTED)
464 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
465 return;
470 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
472 struct queue_event ev;
474 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
475 while(1)
477 queue_wait_w_tmo(q, &ev, ticks);
478 switch(ev.id)
480 case SYS_USB_DISCONNECTED:
481 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
482 return 0;
483 break;
484 case SYS_TIMEOUT:
485 return 1;
486 break;
491 void usb_start_monitoring(void)
493 usb_monitor_enabled = true;
496 bool usb_inserted(void)
498 #ifdef HAVE_USB_POWER
499 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
500 #else
501 return usb_state == USB_INSERTED;
502 #endif
505 #ifdef HAVE_USBSTACK
506 void usb_request_exclusive_ata(void)
508 if(!exclusive_disk) {
509 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
513 bool usb_exclusive_ata(void)
515 return exclusive_disk;
517 #endif
519 #ifdef HAVE_USB_POWER
520 bool usb_powered(void)
522 return usb_state == USB_POWERED;
525 #if CONFIG_CHARGING
526 bool usb_charging_enable(bool on)
528 bool rc = false;
529 #ifdef IRIVER_H300_SERIES
530 int irqlevel;
531 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
532 irqlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
533 pcf50606_set_usb_charging(on);
534 rc = on;
535 (void)set_irq_level(irqlevel);
536 #else
537 /* TODO: implement it for other targets... */
538 (void)on;
539 #endif
540 return rc;
543 bool usb_charging_enabled(void)
545 bool rc = false;
546 #ifdef IRIVER_H300_SERIES
547 /* TODO: read the state of the GPOOD2 register...
548 * (this also means to set the irq level here) */
549 rc = pcf50606_usb_charging_enabled();
550 #else
551 /* TODO: implement it for other targets... */
552 #endif
554 logf("usb charging %s", rc ? "enabled" : "disabled" );
555 return rc;
557 #endif
558 #endif
560 #else
562 #ifdef USB_NONE
563 bool usb_inserted(void)
565 return false;
567 #endif
569 /* Dummy simulator functions */
570 void usb_acknowledge(long id)
572 id = id;
575 void usb_init(void)
579 void usb_start_monitoring(void)
583 int usb_detect(void)
585 return USB_EXTRACTED;
588 void usb_wait_for_disconnect(struct event_queue *q)
590 (void)q;
593 #endif /* USB_NONE or SIMULATOR */