First commit:
[kugel-rb.git] / firmware / usb.c
blob99b2f646124349408cb10b320dff0b4302422211
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 /* Set the state to USB_POWERED for now. if a real
203 connection is detected it will switch to USB_INSERTED */
204 usb_state = USB_POWERED;
205 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
206 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,false);
207 usb_enable(true);
208 #else
209 /* Tell all threads that they have to back off the ATA.
210 We subtract one for our own thread. */
211 num_acks_to_expect =
212 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
213 waiting_for_ack = true;
214 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
215 num_acks_to_expect);
216 #endif
218 break;
219 #ifdef HAVE_USBSTACK
220 case USB_REQUEST_DISK:
221 if(!waiting_for_ack)
223 /* Tell all threads that they have to back off the ATA.
224 We subtract one for our own thread. */
225 num_acks_to_expect =
226 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
227 waiting_for_ack = true;
228 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
229 num_acks_to_expect);
231 break;
232 case USB_RELEASE_DISK:
233 if(!waiting_for_ack)
235 /* Tell all threads that they have to back off the ATA.
236 We subtract one for our own thread. */
237 num_acks_to_expect =
238 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
239 waiting_for_ack = true;
240 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
241 num_acks_to_expect);
243 break;
244 #endif
245 case SYS_USB_CONNECTED_ACK:
246 if(waiting_for_ack)
248 num_acks_to_expect--;
249 if(num_acks_to_expect == 0)
251 DEBUGF("All threads have acknowledged the connect.\n");
252 #ifdef HAVE_USBSTACK
253 #ifndef USE_ROCKBOX_USB
254 /* until we have native mass-storage mode, we want to reboot on
255 usb host connect */
256 usb_enable(true);
257 try_reboot();
258 #endif /* USE_ROCKBOX_USB */
259 #ifdef HAVE_PRIORITY_SCHEDULING
260 thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
261 #endif
262 exclusive_ata_access = true;
264 #else
265 usb_slave_mode(true);
266 cpu_idle_mode(true);
267 #endif
268 usb_state = USB_INSERTED;
269 waiting_for_ack = false;
271 else
273 DEBUGF("usb: got ack, %d to go...\n",
274 num_acks_to_expect);
277 break;
279 case USB_EXTRACTED:
280 #ifdef HAVE_USBSTACK
281 usb_enable(false);
282 #ifdef HAVE_PRIORITY_SCHEDULING
283 thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
284 #endif
285 #endif
286 #ifdef HAVE_LCD_BITMAP
287 if(do_screendump_instead_of_usb)
288 break;
289 #endif
290 #ifdef HAVE_USB_POWER
291 if(usb_state == USB_POWERED)
293 usb_state = USB_EXTRACTED;
294 break;
296 #endif
297 #ifndef HAVE_USBSTACK
298 if(usb_state == USB_INSERTED)
300 /* Only disable the USB mode if we really have enabled it
301 some threads might not have acknowledged the
302 insertion */
303 usb_slave_mode(false);
304 cpu_idle_mode(false);
306 #endif
308 usb_state = USB_EXTRACTED;
309 #ifdef HAVE_USBSTACK
310 if(exclusive_ata_access)
312 exclusive_ata_access = false;
313 #endif
314 /* Tell all threads that we are back in business */
315 num_acks_to_expect =
316 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
317 waiting_for_ack = true;
318 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
319 num_acks_to_expect);
320 #ifdef HAVE_USBSTACK
322 #endif
323 break;
325 case SYS_USB_DISCONNECTED_ACK:
326 if(waiting_for_ack)
328 num_acks_to_expect--;
329 if(num_acks_to_expect == 0)
331 DEBUGF("All threads have acknowledged. "
332 "We're in business.\n");
333 waiting_for_ack = false;
335 else
337 DEBUGF("usb: got ack, %d to go...\n",
338 num_acks_to_expect);
341 break;
343 #ifdef HAVE_HOTSWAP
344 case SYS_HOTSWAP_INSERTED:
345 case SYS_HOTSWAP_EXTRACTED:
346 #ifdef HAVE_USBSTACK
347 usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
348 #else
349 if(usb_state == USB_INSERTED)
351 usb_enable(false);
352 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
354 break;
355 #endif
357 case USB_REENABLE:
358 if(usb_state == USB_INSERTED)
359 usb_enable(true); /* reenable only if still inserted */
360 break;
361 #endif
362 case USB_REQUEST_REBOOT:
363 #ifdef HAVE_USB_POWER
364 if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
365 #endif
366 try_reboot();
367 break;
371 #endif
373 #ifdef HAVE_USBSTACK
374 void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
376 queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
378 #endif
380 #ifndef BOOTLOADER
381 static void usb_tick(void)
383 int current_status;
385 if(usb_monitor_enabled)
387 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
388 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
389 int current_firewire_status = firewire_detect();
390 if(current_firewire_status != last_firewire_status)
392 last_firewire_status = current_firewire_status;
393 firewire_countdown = NUM_POLL_READINGS;
395 else
397 /* Count down until it gets negative */
398 if(firewire_countdown >= 0)
399 firewire_countdown--;
401 /* Report to the thread if we have had 3 identical status
402 readings in a row */
403 if(firewire_countdown == 0)
405 queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
408 #endif
410 current_status = usb_detect();
412 /* Only report when the status has changed */
413 if(current_status != last_usb_status)
415 last_usb_status = current_status;
416 countdown = NUM_POLL_READINGS;
418 else
420 /* Count down until it gets negative */
421 if(countdown >= 0)
422 countdown--;
424 /* Report to the thread if we have had 3 identical status
425 readings in a row */
426 if(countdown == 0)
428 queue_post(&usb_queue, current_status, 0);
432 #ifdef HAVE_MMC
433 if(usb_mmc_countdown > 0)
435 usb_mmc_countdown--;
436 if (usb_mmc_countdown == 0)
437 queue_post(&usb_queue, USB_REENABLE, 0);
439 #endif
441 #endif
443 void usb_acknowledge(long id)
445 queue_post(&usb_queue, id, 0);
448 void usb_init(void)
450 usb_state = USB_EXTRACTED;
451 #ifdef HAVE_USBSTACK
452 exclusive_ata_access = false;
453 #endif
454 usb_monitor_enabled = false;
455 countdown = -1;
457 #if defined(IPOD_COLOR) || defined(IPOD_4G) \
458 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
459 firewire_countdown = -1;
460 last_firewire_status = false;
461 #endif
463 usb_init_device();
464 #ifndef BOOTLOADER
465 usb_enable(false);
466 #endif
468 /* We assume that the USB cable is extracted */
469 last_usb_status = USB_EXTRACTED;
471 #ifndef BOOTLOADER
472 queue_init(&usb_queue, true);
474 usb_thread_entry = create_thread(usb_thread, usb_stack,
475 sizeof(usb_stack), 0, usb_thread_name
476 IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
478 tick_add_task(usb_tick);
479 #endif
483 void usb_wait_for_disconnect(struct event_queue *q)
485 struct queue_event ev;
487 /* Don't return until we get SYS_USB_DISCONNECTED */
488 while(1)
490 queue_wait(q, &ev);
491 if(ev.id == SYS_USB_DISCONNECTED)
493 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
494 return;
499 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
501 struct queue_event ev;
503 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
504 while(1)
506 queue_wait_w_tmo(q, &ev, ticks);
507 switch(ev.id)
509 case SYS_USB_DISCONNECTED:
510 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
511 return 0;
512 break;
513 case SYS_TIMEOUT:
514 return 1;
515 break;
520 void usb_start_monitoring(void)
522 usb_monitor_enabled = true;
525 bool usb_inserted(void)
527 #ifdef HAVE_USB_POWER
528 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
529 #else
530 return usb_state == USB_INSERTED;
531 #endif
534 #ifdef HAVE_USBSTACK
535 void usb_request_exclusive_ata(void)
537 /* This is not really a clean place to start boosting the cpu. but it's
538 * currently the best one. We want to get rid of having to boost the cpu
539 * for usb anyway */
540 trigger_cpu_boost();
541 if(!exclusive_ata_access) {
542 queue_post(&usb_queue, USB_REQUEST_DISK, 0);
546 void usb_release_exclusive_ata(void)
548 cancel_cpu_boost();
549 if(exclusive_ata_access) {
550 queue_post(&usb_queue, USB_RELEASE_DISK, 0);
551 exclusive_ata_access = false;
555 bool usb_exclusive_ata(void)
557 return exclusive_ata_access;
559 #endif
561 #ifdef HAVE_USB_POWER
562 bool usb_powered(void)
564 return usb_state == USB_POWERED;
567 #if CONFIG_CHARGING
568 bool usb_charging_enable(bool on)
570 bool rc = false;
571 #ifdef IRIVER_H300_SERIES
572 int irqlevel;
573 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
574 irqlevel = disable_irq_save();
575 pcf50606_set_usb_charging(on);
576 rc = on;
577 restore_irq(irqlevel);
578 #else
579 /* TODO: implement it for other targets... */
580 (void)on;
581 #endif
582 return rc;
585 bool usb_charging_enabled(void)
587 bool rc = false;
588 #ifdef IRIVER_H300_SERIES
589 /* TODO: read the state of the GPOOD2 register...
590 * (this also means to set the irq level here) */
591 rc = pcf50606_usb_charging_enabled();
592 #else
593 /* TODO: implement it for other targets... */
594 #endif
596 logf("usb charging %s", rc ? "enabled" : "disabled" );
597 return rc;
599 #endif
600 #endif
602 #else
604 #ifdef USB_NONE
605 bool usb_inserted(void)
607 return false;
609 #endif
611 /* Dummy simulator functions */
612 void usb_acknowledge(long id)
614 id = id;
617 void usb_init(void)
621 void usb_start_monitoring(void)
625 int usb_detect(void)
627 return USB_EXTRACTED;
630 void usb_wait_for_disconnect(struct event_queue *q)
632 (void)q;
635 #endif /* USB_NONE or SIMULATOR */