bulgarian update by Hristo Kovachev
[Rockbox.git] / firmware / usb.c
blob6cc3337d82d8ee66239b20e0695a858190b50ec7
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 /* Conditions under which we want the entire driver */
49 #if !defined(BOOTLOADER) || \
50 (defined(TOSHIBA_GIGABEAT_S) && defined(USE_ROCKBOX_USB) && defined(USB_STORAGE))
51 #define USB_FULL_INIT
52 #endif
54 extern void dbg_ports(void); /* NASTY! defined in apps/ */
56 #ifdef HAVE_LCD_BITMAP
57 bool do_screendump_instead_of_usb = false;
58 #if defined(USB_FULL_INIT) && defined(BOOTLOADER)
59 static void screen_dump(void) {}
60 #else
61 void screen_dump(void); /* Nasty again. Defined in apps/ too */
62 #endif
63 #endif
65 #if !defined(SIMULATOR) && !defined(USB_NONE)
67 #define NUM_POLL_READINGS (HZ/5)
68 static int countdown;
70 static int usb_state;
72 #if defined(HAVE_MMC) && defined(USB_FULL_INIT)
73 static int usb_mmc_countdown = 0;
74 #endif
76 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
77 needs updating */
78 #ifdef USB_FULL_INIT
79 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
80 static const char usb_thread_name[] = "usb";
81 static struct thread_entry *usb_thread_entry;
82 #endif
83 static struct event_queue usb_queue;
84 static int last_usb_status;
85 static bool usb_monitor_enabled;
86 #ifdef HAVE_USBSTACK
87 static bool exclusive_ata_access;
88 #endif
91 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
92 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
93 static int firewire_countdown;
94 static bool last_firewire_status;
95 #endif
97 #ifdef USB_FULL_INIT
98 #ifndef HAVE_USBSTACK
99 static void usb_slave_mode(bool on)
101 int rc;
103 if(on)
105 DEBUGF("Entering USB slave mode\n");
106 ata_soft_reset();
107 ata_init();
108 ata_enable(false);
109 usb_enable(true);
111 else
113 DEBUGF("Leaving USB slave mode\n");
115 /* Let the ISDx00 settle */
116 sleep(HZ*1);
118 usb_enable(false);
120 rc = ata_init();
121 if(rc)
123 /* fixme: can we remove this? (already such in main.c) */
124 char str[32];
125 lcd_clear_display();
126 snprintf(str, 31, "ATA error: %d", rc);
127 lcd_puts(0, 0, str);
128 lcd_puts(0, 1, "Press ON to debug");
129 lcd_update();
130 while(!(button_get(true) & BUTTON_REL)) {};
131 dbg_ports();
132 panicf("ata: %d",rc);
135 rc = disk_mount_all();
136 if (rc <= 0) /* no partition */
137 panicf("mount: %d",rc);
141 #endif
143 static void try_reboot(void)
145 #ifndef HAVE_FLASH_STORAGE
146 ata_sleepnow(); /* Immediately spindown the disk. */
147 sleep(HZ*2);
148 #endif
150 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
151 #if CONFIG_CPU == PP5020
152 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
153 #elif CONFIG_CPU == PP5022
154 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
155 #endif /* CONFIG_CPU */
156 #endif /* IPOD_ARCH */
158 system_reboot(); /* Reboot */
161 static void usb_thread(void)
163 int num_acks_to_expect = -1;
164 bool waiting_for_ack;
165 struct queue_event ev;
167 waiting_for_ack = false;
169 while(1)
171 queue_wait(&usb_queue, &ev);
172 switch(ev.id)
174 #ifdef HAVE_USBSTACK
175 case USB_TRANSFER_COMPLETION:
176 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
177 break;
178 #endif
179 #ifdef HAVE_USB_POWER
180 case USB_POWERED:
181 usb_state = USB_POWERED;
182 break;
183 #endif
184 case USB_INSERTED:
185 #ifdef HAVE_LCD_BITMAP
186 if(do_screendump_instead_of_usb)
188 screen_dump();
190 else
191 #endif
192 #ifdef HAVE_USB_POWER
193 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
194 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
195 #else
196 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
197 #endif
199 usb_state = USB_POWERED;
200 #ifdef HAVE_USBSTACK
201 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,false);
202 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,true);
203 usb_enable(true);
204 #endif
206 else
207 #endif
209 #ifdef HAVE_USBSTACK
210 /* Set the state to USB_POWERED for now. if a real
211 connection is detected it will switch to USB_INSERTED */
212 usb_state = USB_POWERED;
213 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
214 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,false);
215 usb_enable(true);
216 #else
217 /* Tell all threads that they have to back off the ATA.
218 We subtract one for our own thread. */
219 num_acks_to_expect =
220 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
221 waiting_for_ack = true;
222 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
223 num_acks_to_expect);
224 #endif
226 break;
227 #ifdef HAVE_USBSTACK
228 case USB_REQUEST_DISK:
229 if(!waiting_for_ack)
231 /* Tell all threads that they have to back off the ATA.
232 We subtract one for our own thread. */
233 num_acks_to_expect =
234 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
235 waiting_for_ack = true;
236 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
237 num_acks_to_expect);
239 break;
240 case USB_RELEASE_DISK:
241 if(!waiting_for_ack)
243 /* Tell all threads that they have to back off the ATA.
244 We subtract one for our own thread. */
245 num_acks_to_expect =
246 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
247 waiting_for_ack = true;
248 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
249 num_acks_to_expect);
251 break;
252 #endif
253 case SYS_USB_CONNECTED_ACK:
254 if(waiting_for_ack)
256 num_acks_to_expect--;
257 if(num_acks_to_expect == 0)
259 DEBUGF("All threads have acknowledged the connect.\n");
260 #ifdef HAVE_USBSTACK
261 #ifndef USE_ROCKBOX_USB
262 /* until we have native mass-storage mode, we want to reboot on
263 usb host connect */
264 usb_enable(true);
265 try_reboot();
266 #endif /* USE_ROCKBOX_USB */
267 #ifdef HAVE_PRIORITY_SCHEDULING
268 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
269 #endif
270 exclusive_ata_access = true;
272 #else
273 usb_slave_mode(true);
274 cpu_idle_mode(true);
275 #endif
276 usb_state = USB_INSERTED;
277 waiting_for_ack = false;
279 else
281 DEBUGF("usb: got ack, %d to go...\n",
282 num_acks_to_expect);
285 break;
287 case USB_EXTRACTED:
288 #ifdef HAVE_USBSTACK
289 usb_enable(false);
290 #ifdef HAVE_PRIORITY_SCHEDULING
291 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
292 #endif
293 #endif
294 #ifdef HAVE_LCD_BITMAP
295 if(do_screendump_instead_of_usb)
296 break;
297 #endif
298 #ifdef HAVE_USB_POWER
299 if(usb_state == USB_POWERED)
301 usb_state = USB_EXTRACTED;
302 break;
304 #endif
305 #ifndef HAVE_USBSTACK
306 if(usb_state == USB_INSERTED)
308 /* Only disable the USB mode if we really have enabled it
309 some threads might not have acknowledged the
310 insertion */
311 usb_slave_mode(false);
312 cpu_idle_mode(false);
314 #endif
316 usb_state = USB_EXTRACTED;
317 #ifdef HAVE_USBSTACK
318 if(exclusive_ata_access)
320 exclusive_ata_access = false;
321 #endif
322 /* Tell all threads that we are back in business */
323 num_acks_to_expect =
324 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
325 waiting_for_ack = true;
326 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
327 num_acks_to_expect);
328 #ifdef HAVE_USBSTACK
330 #endif
331 break;
333 case SYS_USB_DISCONNECTED_ACK:
334 if(waiting_for_ack)
336 num_acks_to_expect--;
337 if(num_acks_to_expect == 0)
339 DEBUGF("All threads have acknowledged. "
340 "We're in business.\n");
341 waiting_for_ack = false;
343 else
345 DEBUGF("usb: got ack, %d to go...\n",
346 num_acks_to_expect);
349 break;
351 #ifdef HAVE_HOTSWAP
352 case SYS_HOTSWAP_INSERTED:
353 case SYS_HOTSWAP_EXTRACTED:
354 #ifdef HAVE_USBSTACK
355 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
356 #else
357 if(usb_state == USB_INSERTED)
359 usb_enable(false);
360 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
362 break;
363 #endif
365 case USB_REENABLE:
366 if(usb_state == USB_INSERTED)
367 usb_enable(true); /* reenable only if still inserted */
368 break;
369 #endif
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 #ifdef HAVE_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_ata_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 TOSHIBA_GIGABEAT_S
534 void usb_stop_monitoring(void)
536 tick_remove_task(usb_tick);
537 usb_monitor_enabled = false;
539 #endif
541 bool usb_inserted(void)
543 #ifdef HAVE_USB_POWER
544 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
545 #else
546 return usb_state == USB_INSERTED;
547 #endif
550 #ifdef HAVE_USBSTACK
551 void usb_request_exclusive_ata(void)
553 /* This is not really a clean place to start boosting the cpu. but it's
554 * currently the best one. We want to get rid of having to boost the cpu
555 * for usb anyway */
556 trigger_cpu_boost();
557 if(!exclusive_ata_access) {
558 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
562 void usb_release_exclusive_ata(void)
564 cancel_cpu_boost();
565 if(exclusive_ata_access) {
566 queue_post(&usb_queue, USB_RELEASE_DISK, 0);
567 exclusive_ata_access = false;
571 bool usb_exclusive_ata(void)
573 return exclusive_ata_access;
575 #endif
577 #ifdef HAVE_USB_POWER
578 bool usb_powered(void)
580 return usb_state == USB_POWERED;
583 #if CONFIG_CHARGING
584 bool usb_charging_enable(bool on)
586 bool rc = false;
587 #ifdef IRIVER_H300_SERIES
588 int irqlevel;
589 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
590 irqlevel = disable_irq_save();
591 pcf50606_set_usb_charging(on);
592 rc = on;
593 restore_irq(irqlevel);
594 #else
595 /* TODO: implement it for other targets... */
596 (void)on;
597 #endif
598 return rc;
601 bool usb_charging_enabled(void)
603 bool rc = false;
604 #ifdef IRIVER_H300_SERIES
605 /* TODO: read the state of the GPOOD2 register...
606 * (this also means to set the irq level here) */
607 rc = pcf50606_usb_charging_enabled();
608 #else
609 /* TODO: implement it for other targets... */
610 #endif
612 logf("usb charging %s", rc ? "enabled" : "disabled" );
613 return rc;
615 #endif
616 #endif
618 #else
620 #ifdef USB_NONE
621 bool usb_inserted(void)
623 return false;
625 #endif
627 /* Dummy simulator functions */
628 void usb_acknowledge(long id)
630 id = id;
633 void usb_init(void)
637 void usb_start_monitoring(void)
641 int usb_detect(void)
643 return USB_EXTRACTED;
646 void usb_wait_for_disconnect(struct event_queue *q)
648 (void)q;
651 #endif /* USB_NONE or SIMULATOR */