1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
45 #include "usb-target.h"
47 #ifdef IRIVER_H300_SERIES
48 #include "pcf50606.h" /* for pcf50606_usb_charging_... */
52 extern void dbg_ports(void); /* NASTY! defined in apps/ */
54 #ifdef HAVE_LCD_BITMAP
55 bool do_screendump_instead_of_usb
= false;
56 void screen_dump(void); /* Nasty again. Defined in apps/ too */
59 #define USB_REALLY_BRAVE
61 #if !defined(SIMULATOR) && !defined(USB_NONE)
63 /* Messages from usb_tick and thread states */
64 #define USB_INSERTED 1
65 #define USB_EXTRACTED 2
67 #define USB_REENABLE 3
72 #if CONFIG_KEYPAD == RECORDER_PAD
73 #define USBPOWER_BUTTON BUTTON_F1
74 #define USBPOWER_BTN_IGNORE BUTTON_ON
75 #elif CONFIG_KEYPAD == ONDIO_PAD
76 #define USBPOWER_BUTTON BUTTON_MENU
77 #define USBPOWER_BTN_IGNORE BUTTON_OFF
78 #elif (CONFIG_KEYPAD == IPOD_4G_PAD)
79 #define USBPOWER_BUTTON BUTTON_MENU
80 #define USBPOWER_BTN_IGNORE BUTTON_PLAY
81 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
82 #define USBPOWER_BUTTON BUTTON_REC
83 #define USBPOWER_BTN_IGNORE BUTTON_ON
84 #elif CONFIG_KEYPAD == GIGABEAT_PAD
85 #define USBPOWER_BUTTON BUTTON_MENU
86 #define USBPOWER_BTN_IGNORE BUTTON_POWER
87 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
88 #define USBPOWER_BUTTON BUTTON_NONE
89 #define USBPOWER_BTN_IGNORE BUTTON_POWER
90 #elif CONFIG_KEYPAD == SANSA_E200_PAD
91 #define USBPOWER_BUTTON BUTTON_SELECT
92 #define USBPOWER_BTN_IGNORE BUTTON_POWER
94 #endif /* HAVE_USB_POWER */
96 /* The ADC tick reads one channel per tick, and we want to check 3 successive
97 readings on the USB voltage channel. This doesn't apply to the Player, but
98 debouncing the USB detection port won't hurt us either. */
99 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
100 static int countdown
;
102 static int usb_state
;
104 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
105 static int usb_mmc_countdown
= 0;
108 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
111 static long usb_stack
[(DEFAULT_STACK_SIZE
+ 0x800)/sizeof(long)];
112 static const char usb_thread_name
[] = "usb";
114 static struct event_queue usb_queue
;
115 static bool last_usb_status
;
116 static bool usb_monitor_enabled
;
119 void usb_enable(bool on
)
121 #ifdef USB_ENABLE_ONDIOSTYLE
122 PACR2
&= ~0x04C0; /* use PA3, PA5 as GPIO */
126 mmc_enable_int_flash_clock(!mmc_detect());
128 if (!(read_hw_mask() & MMC_CLOCK_POLARITY
))
129 and_b(~0x20, &PBDRH
); /* old circuit needs SCK1 = low while on USB */
130 or_b(0x20, &PADRL
); /* enable USB */
131 and_b(~0x08, &PADRL
); /* assert card detect */
135 if (!(read_hw_mask() & MMC_CLOCK_POLARITY
))
136 or_b(0x20, &PBDRH
); /* reset SCK1 = high for old circuit */
137 and_b(~0x20, &PADRL
); /* disable USB */
138 or_b(0x08, &PADRL
); /* deassert card detect */
140 or_b(0x28, &PAIORL
); /* output for USB enable and card detect */
141 #elif defined(USB_ISP1582)
142 /* TODO: Implement USB_ISP1582 */
144 #elif defined(USB_X5M5_STYLE)
146 #elif defined(USB_GIGABEAT_STYLE)
149 #ifdef HAVE_LCD_BITMAP
150 if(read_hw_mask() & USB_ACTIVE_HIGH
)
155 and_b(~0x04, &PADRH
); /* enable USB */
167 static void usb_slave_mode(bool on
)
173 DEBUGF("Entering USB slave mode\n");
181 DEBUGF("Leaving USB slave mode\n");
183 /* Let the ISDx00 settle */
191 /* fixme: can we remove this? (already such in main.c) */
194 snprintf(str
, 31, "ATA error: %d", rc
);
196 lcd_puts(0, 1, "Press ON to debug");
198 while(!(button_get(true) & BUTTON_REL
)) {};
200 panicf("ata: %d",rc
);
203 rc
= disk_mount_all();
204 if (rc
<= 0) /* no partition */
205 panicf("mount: %d",rc
);
210 static void usb_thread(void)
212 int num_acks_to_expect
= -1;
213 bool waiting_for_ack
;
216 waiting_for_ack
= false;
220 queue_wait(&usb_queue
, &ev
);
224 #ifdef HAVE_LCD_BITMAP
225 if(do_screendump_instead_of_usb
)
231 #ifdef HAVE_USB_POWER
232 if((button_status() & ~USBPOWER_BTN_IGNORE
) == USBPOWER_BUTTON
)
234 usb_state
= USB_POWERED
;
239 /* Tell all threads that they have to back off the ATA.
240 We subtract one for our own thread. */
242 queue_broadcast(SYS_USB_CONNECTED
, 0) - 1;
243 waiting_for_ack
= true;
244 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
249 case SYS_USB_CONNECTED_ACK
:
252 num_acks_to_expect
--;
253 if(num_acks_to_expect
== 0)
255 DEBUGF("All threads have acknowledged the connect.\n");
256 #ifdef USB_REALLY_BRAVE
257 usb_slave_mode(true);
258 usb_state
= USB_INSERTED
;
266 DEBUGF("usb: got ack, %d to go...\n",
273 #ifdef HAVE_LCD_BITMAP
274 if(do_screendump_instead_of_usb
)
277 #ifdef HAVE_USB_POWER
278 if(usb_state
== USB_POWERED
)
280 usb_state
= USB_EXTRACTED
;
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
289 usb_slave_mode(false);
290 cpu_idle_mode(false);
293 usb_state
= USB_EXTRACTED
;
295 /* Tell all threads that we are back in business */
297 queue_broadcast(SYS_USB_DISCONNECTED
, 0) - 1;
298 waiting_for_ack
= true;
299 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
301 #ifdef HAVE_LCD_CHARCELLS
302 lcd_icon(ICON_USB
, false);
306 case SYS_USB_DISCONNECTED_ACK
:
309 num_acks_to_expect
--;
310 if(num_acks_to_expect
== 0)
312 DEBUGF("All threads have acknowledged. "
313 "We're in business.\n");
317 DEBUGF("usb: got ack, %d to go...\n",
324 case SYS_MMC_INSERTED
:
325 case SYS_MMC_EXTRACTED
:
326 if(usb_state
== USB_INSERTED
)
329 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
334 if(usb_state
== USB_INSERTED
)
335 usb_enable(true); /* reenable only if still inserted */
344 bool usb_detect(void)
348 #ifdef USB_RECORDERSTYLE
349 current_status
= (adc_read(ADC_USB_POWER
) > 500)?true:false;
351 #ifdef USB_FMRECORDERSTYLE
352 current_status
= (adc_read(ADC_USB_POWER
) <= 512)?true:false;
354 #ifdef USB_PLAYERSTYLE
355 current_status
= (PADR
& 0x8000)?false:true;
358 /* TODO: Implement USB_ISP1582 */
359 current_status
= false;
361 return current_status
;
366 static void usb_tick(void)
370 if(usb_monitor_enabled
)
372 current_status
= usb_detect();
374 /* Only report when the status has changed */
375 if(current_status
!= last_usb_status
)
377 last_usb_status
= current_status
;
378 countdown
= NUM_POLL_READINGS
;
382 /* Count down until it gets negative */
386 /* Report to the thread if we have had 3 identical status
391 queue_post(&usb_queue
, USB_INSERTED
, 0);
393 queue_post(&usb_queue
, USB_EXTRACTED
, 0);
398 if(usb_mmc_countdown
> 0)
401 if (usb_mmc_countdown
== 0)
402 queue_post(&usb_queue
, USB_REENABLE
, 0);
408 void usb_acknowledge(long id
)
410 queue_post(&usb_queue
, id
, 0);
415 usb_state
= USB_EXTRACTED
;
416 usb_monitor_enabled
= false;
425 /* We assume that the USB cable is extracted */
426 last_usb_status
= false;
429 queue_init(&usb_queue
, true);
430 create_thread(usb_thread
, usb_stack
, sizeof(usb_stack
),
431 usb_thread_name
IF_PRIO(, PRIORITY_SYSTEM
)
432 IF_COP(, CPU
, false));
434 tick_add_task(usb_tick
);
439 void usb_wait_for_disconnect(struct event_queue
*q
)
443 /* Don't return until we get SYS_USB_DISCONNECTED */
447 if(ev
.id
== SYS_USB_DISCONNECTED
)
449 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
455 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
459 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
462 queue_wait_w_tmo(q
, &ev
, ticks
);
465 case SYS_USB_DISCONNECTED
:
466 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
476 void usb_start_monitoring(void)
478 usb_monitor_enabled
= true;
481 bool usb_inserted(void)
483 #ifdef HAVE_USB_POWER
484 return usb_state
== USB_INSERTED
|| usb_state
== USB_POWERED
;
486 return usb_state
== USB_INSERTED
;
490 #ifdef HAVE_USB_POWER
491 bool usb_powered(void)
493 return usb_state
== USB_POWERED
;
497 bool usb_charging_enable(bool on
)
500 #ifdef IRIVER_H300_SERIES
502 logf("usb_charging_enable(%s)\n", on
? "on" : "off" );
503 irqlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
504 pcf50606_set_usb_charging(on
);
506 (void)set_irq_level(irqlevel
);
508 /* TODO: implement it for other targets... */
514 bool usb_charging_enabled(void)
517 #ifdef IRIVER_H300_SERIES
518 /* TODO: read the state of the GPOOD2 register...
519 * (this also means to set the irq level here) */
520 rc
= pcf50606_usb_charging_enabled();
522 /* TODO: implement it for other targets... */
525 logf("usb_charging_enabled: %s\n", rc
? "true" : "false" );
534 bool usb_inserted(void)
540 /* Dummy simulator functions */
541 void usb_acknowledge(long id
)
550 void usb_start_monitoring(void)
554 bool usb_detect(void)
559 void usb_wait_for_disconnect(struct event_queue
*q
)
564 #endif /* USB_NONE or SIMULATOR */