Oops. Should commit patches correctly.
[Rockbox.git] / firmware / usb.c
blob3cd93d8be5e02aff15c888b3d423cc5a121702da
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 #if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
138 if (button_status()==BUTTON_RIGHT)
139 #endif /* defined(IRIVER_H10) || defined (IRIVER_H10_5GB) */
141 #ifndef HAVE_FLASH_STORAGE
142 ata_sleepnow(); /* Immediately spindown the disk. */
143 sleep(HZ*2);
144 #endif
146 #ifdef IPOD_ARCH /* The following code is based on ipodlinux */
147 #if CONFIG_CPU == PP5020
148 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
149 #elif CONFIG_CPU == PP5022
150 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
151 #endif /* CONFIG_CPU */
152 #endif /* IPOD_ARCH */
154 system_reboot(); /* Reboot */
158 static void usb_thread(void)
160 int num_acks_to_expect = -1;
161 bool waiting_for_ack;
162 struct queue_event ev;
164 waiting_for_ack = false;
166 while(1)
168 queue_wait(&usb_queue, &ev);
169 switch(ev.id)
171 #ifdef HAVE_USBSTACK
172 case USB_TRANSFER_COMPLETION:
173 usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
174 break;
175 #endif
176 #ifdef HAVE_USB_POWER
177 case USB_POWERED:
178 usb_state = USB_POWERED;
179 break;
180 #endif
181 case USB_INSERTED:
182 #ifdef HAVE_LCD_BITMAP
183 if(do_screendump_instead_of_usb)
185 screen_dump();
187 else
188 #endif
189 #ifdef HAVE_USB_POWER
190 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
192 usb_state = USB_POWERED;
193 #ifdef HAVE_USBSTACK
194 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,false);
195 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,true);
196 usb_enable(true);
197 #endif
199 else
200 #endif
202 #ifdef HAVE_USBSTACK
203 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
204 usb_core_enable_driver(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 case USB_RELEASE_DISK:
231 if(!waiting_for_ack)
233 /* Tell all threads that they have to back off the ATA.
234 We subtract one for our own thread. */
235 num_acks_to_expect =
236 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
237 waiting_for_ack = true;
238 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
239 num_acks_to_expect);
241 break;
242 #endif
243 case SYS_USB_CONNECTED_ACK:
244 if(waiting_for_ack)
246 num_acks_to_expect--;
247 if(num_acks_to_expect == 0)
249 DEBUGF("All threads have acknowledged the connect.\n");
250 #ifdef HAVE_USBSTACK
251 #ifndef USE_ROCKBOX_USB
252 /* until we have native mass-storage mode, we want to reboot on
253 usb host connect */
254 try_reboot();
255 #endif /* USE_ROCKBOX_USB */
256 #ifdef HAVE_PRIORITY_SCHEDULING
257 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
258 exclusive_ata_access = true;
259 #endif
261 #else
262 usb_slave_mode(true);
263 cpu_idle_mode(true);
264 #endif
265 usb_state = USB_INSERTED;
266 waiting_for_ack = false;
268 else
270 DEBUGF("usb: got ack, %d to go...\n",
271 num_acks_to_expect);
274 break;
276 case USB_EXTRACTED:
277 #ifdef HAVE_USBSTACK
278 usb_enable(false);
279 #ifdef HAVE_PRIORITY_SCHEDULING
280 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
281 #endif
282 #endif
283 #ifdef HAVE_LCD_BITMAP
284 if(do_screendump_instead_of_usb)
285 break;
286 #endif
287 #ifdef HAVE_USB_POWER
288 if(usb_state == USB_POWERED)
290 usb_state = USB_EXTRACTED;
291 break;
293 #endif
294 #ifndef HAVE_USBSTACK
295 if(usb_state == USB_INSERTED)
297 /* Only disable the USB mode if we really have enabled it
298 some threads might not have acknowledged the
299 insertion */
300 usb_slave_mode(false);
301 cpu_idle_mode(false);
303 #endif
305 usb_state = USB_EXTRACTED;
306 #ifdef HAVE_USBSTACK
307 if(exclusive_ata_access)
309 exclusive_ata_access = false;
310 #endif
311 /* Tell all threads that we are back in business */
312 num_acks_to_expect =
313 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
314 waiting_for_ack = true;
315 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
316 num_acks_to_expect);
317 #ifdef HAVE_USBSTACK
319 #endif
320 break;
322 case SYS_USB_DISCONNECTED_ACK:
323 if(waiting_for_ack)
325 num_acks_to_expect--;
326 if(num_acks_to_expect == 0)
328 DEBUGF("All threads have acknowledged. "
329 "We're in business.\n");
330 waiting_for_ack = false;
332 else
334 DEBUGF("usb: got ack, %d to go...\n",
335 num_acks_to_expect);
338 break;
340 #ifdef HAVE_HOTSWAP
341 case SYS_HOTSWAP_INSERTED:
342 case SYS_HOTSWAP_EXTRACTED:
343 #ifdef HAVE_USBSTACK
344 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
345 #else
346 if(usb_state == USB_INSERTED)
348 usb_enable(false);
349 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
351 break;
352 #endif
354 case USB_REENABLE:
355 if(usb_state == USB_INSERTED)
356 usb_enable(true); /* reenable only if still inserted */
357 break;
358 #endif
359 case USB_REQUEST_REBOOT:
360 #ifdef HAVE_USB_POWER
361 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
362 #endif
363 try_reboot();
364 break;
368 #endif
370 #ifdef HAVE_USBSTACK
371 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
373 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
375 #endif
377 #ifndef BOOTLOADER
378 static void usb_tick(void)
380 int current_status;
382 if(usb_monitor_enabled)
384 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
385 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
386 int current_firewire_status = firewire_detect();
387 if(current_firewire_status != last_firewire_status)
389 last_firewire_status = current_firewire_status;
390 firewire_countdown = NUM_POLL_READINGS;
392 else
394 /* Count down until it gets negative */
395 if(firewire_countdown >= 0)
396 firewire_countdown--;
398 /* Report to the thread if we have had 3 identical status
399 readings in a row */
400 if(firewire_countdown == 0)
402 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
405 #endif
407 current_status = usb_detect();
409 /* Only report when the status has changed */
410 if(current_status != last_usb_status)
412 last_usb_status = current_status;
413 countdown = NUM_POLL_READINGS;
415 else
417 /* Count down until it gets negative */
418 if(countdown >= 0)
419 countdown--;
421 /* Report to the thread if we have had 3 identical status
422 readings in a row */
423 if(countdown == 0)
425 queue_post(&usb_queue, current_status, 0);
429 #ifdef HAVE_MMC
430 if(usb_mmc_countdown > 0)
432 usb_mmc_countdown--;
433 if (usb_mmc_countdown == 0)
434 queue_post(&usb_queue, USB_REENABLE, 0);
436 #endif
438 #endif
440 void usb_acknowledge(long id)
442 queue_post(&usb_queue, id, 0);
445 void usb_init(void)
447 usb_state = USB_EXTRACTED;
448 #ifdef HAVE_USBSTACK
449 exclusive_ata_access = false;
450 #endif
451 usb_monitor_enabled = false;
452 countdown = -1;
454 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
455 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
456 firewire_countdown = -1;
457 last_firewire_status = false;
458 #endif
460 usb_init_device();
461 usb_enable(false);
463 /* We assume that the USB cable is extracted */
464 last_usb_status = USB_EXTRACTED;
466 #ifndef BOOTLOADER
467 queue_init(&usb_queue, true);
469 usb_thread_entry = create_thread(usb_thread, usb_stack,
470 sizeof(usb_stack), 0, usb_thread_name
471 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
473 tick_add_task(usb_tick);
474 #endif
478 void usb_wait_for_disconnect(struct event_queue *q)
480 struct queue_event ev;
482 /* Don't return until we get SYS_USB_DISCONNECTED */
483 while(1)
485 queue_wait(q, &ev);
486 if(ev.id == SYS_USB_DISCONNECTED)
488 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
489 return;
494 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
496 struct queue_event ev;
498 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
499 while(1)
501 queue_wait_w_tmo(q, &ev, ticks);
502 switch(ev.id)
504 case SYS_USB_DISCONNECTED:
505 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
506 return 0;
507 break;
508 case SYS_TIMEOUT:
509 return 1;
510 break;
515 void usb_start_monitoring(void)
517 usb_monitor_enabled = true;
520 bool usb_inserted(void)
522 #ifdef HAVE_USB_POWER
523 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
524 #else
525 return usb_state == USB_INSERTED;
526 #endif
529 #ifdef HAVE_USBSTACK
530 void usb_request_exclusive_ata(void)
532 if(!exclusive_ata_access) {
533 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
537 void usb_release_exclusive_ata(void)
539 if(exclusive_ata_access) {
540 queue_post(&usb_queue, USB_RELEASE_DISK, 0);
541 exclusive_ata_access = false;
545 bool usb_exclusive_ata(void)
547 return exclusive_ata_access;
549 #endif
551 #ifdef HAVE_USB_POWER
552 bool usb_powered(void)
554 return usb_state == USB_POWERED;
557 #if CONFIG_CHARGING
558 bool usb_charging_enable(bool on)
560 bool rc = false;
561 #ifdef IRIVER_H300_SERIES
562 int irqlevel;
563 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
564 irqlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
565 pcf50606_set_usb_charging(on);
566 rc = on;
567 (void)set_irq_level(irqlevel);
568 #else
569 /* TODO: implement it for other targets... */
570 (void)on;
571 #endif
572 return rc;
575 bool usb_charging_enabled(void)
577 bool rc = false;
578 #ifdef IRIVER_H300_SERIES
579 /* TODO: read the state of the GPOOD2 register...
580 * (this also means to set the irq level here) */
581 rc = pcf50606_usb_charging_enabled();
582 #else
583 /* TODO: implement it for other targets... */
584 #endif
586 logf("usb charging %s", rc ? "enabled" : "disabled" );
587 return rc;
589 #endif
590 #endif
592 #else
594 #ifdef USB_NONE
595 bool usb_inserted(void)
597 return false;
599 #endif
601 /* Dummy simulator functions */
602 void usb_acknowledge(long id)
604 id = id;
607 void usb_init(void)
611 void usb_start_monitoring(void)
615 int usb_detect(void)
617 return USB_EXTRACTED;
620 void usb_wait_for_disconnect(struct event_queue *q)
622 (void)q;
625 #endif /* USB_NONE or SIMULATOR */