Fix remote screen check in graphic equalizer, so that it can be used on logf-enabled...
[Rockbox.git] / firmware / usb.c
bloba0ffef10111a552a301e5d8f4205a3e3f7a0b4d7
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 "adc.h"
36 #include "usb.h"
37 #include "button.h"
38 #include "sprintf.h"
39 #include "string.h"
40 #include "hwcompat.h"
41 #ifdef HAVE_MMC
42 #include "ata_mmc.h"
43 #endif
44 #ifdef TARGET_TREE
45 #include "usb-target.h"
46 #endif
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 #define USB_REALLY_BRAVE
57 #if !defined(SIMULATOR) && !defined(USB_NONE)
59 /* Messages from usb_tick and thread states */
60 #define USB_INSERTED 1
61 #define USB_EXTRACTED 2
62 #ifdef HAVE_MMC
63 #define USB_REENABLE 3
64 #endif
65 #ifdef HAVE_USB_POWER
66 #define USB_POWERED 4
68 #if CONFIG_KEYPAD == RECORDER_PAD
69 #define USBPOWER_BUTTON BUTTON_F1
70 #define USBPOWER_BTN_IGNORE BUTTON_ON
71 #elif CONFIG_KEYPAD == ONDIO_PAD
72 #define USBPOWER_BUTTON BUTTON_MENU
73 #define USBPOWER_BTN_IGNORE BUTTON_OFF
74 #elif (CONFIG_KEYPAD == IPOD_4G_PAD)
75 #define USBPOWER_BUTTON BUTTON_MENU
76 #define USBPOWER_BTN_IGNORE BUTTON_PLAY
77 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
78 #define USBPOWER_BUTTON BUTTON_REC
79 #define USBPOWER_BTN_IGNORE BUTTON_ON
80 #elif CONFIG_KEYPAD == GIGABEAT_PAD
81 #define USBPOWER_BUTTON BUTTON_MENU
82 #define USBPOWER_BTN_IGNORE BUTTON_POWER
83 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
84 #define USBPOWER_BUTTON BUTTON_NONE
85 #define USBPOWER_BTN_IGNORE BUTTON_POWER
86 #endif
87 #endif /* HAVE_USB_POWER */
89 /* The ADC tick reads one channel per tick, and we want to check 3 successive
90 readings on the USB voltage channel. This doesn't apply to the Player, but
91 debouncing the USB detection port won't hurt us either. */
92 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
93 static int countdown;
95 static int usb_state;
97 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
98 static int usb_mmc_countdown = 0;
99 #endif
101 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
102 needs updating */
103 #ifndef BOOTLOADER
104 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
105 static const char usb_thread_name[] = "usb";
106 #endif
107 static struct event_queue usb_queue;
108 static bool last_usb_status;
109 static bool usb_monitor_enabled;
111 #ifndef TARGET_TREE
112 void usb_enable(bool on)
114 #ifdef USB_ENABLE_ONDIOSTYLE
115 PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
116 if(on)
118 #ifdef HAVE_MMC
119 mmc_enable_int_flash_clock(!mmc_detect());
120 #endif
121 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
122 and_b(~0x20, &PBDRH); /* old circuit needs SCK1 = low while on USB */
123 or_b(0x20, &PADRL); /* enable USB */
124 and_b(~0x08, &PADRL); /* assert card detect */
126 else
128 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
129 or_b(0x20, &PBDRH); /* reset SCK1 = high for old circuit */
130 and_b(~0x20, &PADRL); /* disable USB */
131 or_b(0x08, &PADRL); /* deassert card detect */
133 or_b(0x28, &PAIORL); /* output for USB enable and card detect */
134 #elif defined(USB_GMINISTYLE)
136 int smsc_ver = smsc_version();
137 if (on) {
138 if (smsc_ver < 4) {
139 P6 &= ~0x04;
140 P10 &= ~0x20;
142 smsc_delay();
144 P6 |= 0x08;
145 P10 |= 0x20;
147 smsc_delay();
149 P6 |= 0x10;
150 } else {
151 P6 &= ~0x10;
152 if (smsc_ver < 4) {
153 P6 &= ~0x04;
154 P10 &= ~0x20;
158 #elif defined(USB_ISP1582)
159 /* TODO: Implement USB_ISP1582 */
160 (void) on;
161 #elif defined(USB_X5STYLE)
162 /* TODO X5 */
163 #elif defined(USB_GIGABEAT_STYLE)
164 /* TODO gigabeat */
165 #else
166 #ifdef HAVE_LCD_BITMAP
167 if(read_hw_mask() & USB_ACTIVE_HIGH)
168 on = !on;
169 #endif
170 if(on)
172 and_b(~0x04, &PADRH); /* enable USB */
174 else
176 or_b(0x04, &PADRH);
178 or_b(0x04, &PAIORH);
179 #endif
181 #endif
183 #ifndef BOOTLOADER
184 static void usb_slave_mode(bool on)
186 int rc;
188 if(on)
190 DEBUGF("Entering USB slave mode\n");
191 ata_soft_reset();
192 ata_init();
193 ata_enable(false);
194 usb_enable(true);
196 else
198 DEBUGF("Leaving USB slave mode\n");
200 /* Let the ISDx00 settle */
201 sleep(HZ*1);
203 usb_enable(false);
205 rc = ata_init();
206 if(rc)
208 /* fixme: can we remove this? (already such in main.c) */
209 char str[32];
210 lcd_clear_display();
211 snprintf(str, 31, "ATA error: %d", rc);
212 lcd_puts(0, 0, str);
213 lcd_puts(0, 1, "Press ON to debug");
214 lcd_update();
215 while(!(button_get(true) & BUTTON_REL)) {};
216 dbg_ports();
217 panicf("ata: %d",rc);
220 rc = disk_mount_all();
221 if (rc <= 0) /* no partition */
222 panicf("mount: %d",rc);
227 static void usb_thread(void)
229 int num_acks_to_expect = -1;
230 bool waiting_for_ack;
231 struct event ev;
233 waiting_for_ack = false;
235 while(1)
237 queue_wait(&usb_queue, &ev);
238 switch(ev.id)
240 case USB_INSERTED:
241 #ifdef HAVE_LCD_BITMAP
242 if(do_screendump_instead_of_usb)
244 screen_dump();
246 else
247 #endif
248 #ifdef HAVE_USB_POWER
249 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
251 usb_state = USB_POWERED;
253 else
254 #endif
256 /* Tell all threads that they have to back off the ATA.
257 We subtract one for our own thread. */
258 num_acks_to_expect =
259 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
260 waiting_for_ack = true;
261 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
262 num_acks_to_expect);
264 break;
266 case SYS_USB_CONNECTED_ACK:
267 if(waiting_for_ack)
269 num_acks_to_expect--;
270 if(num_acks_to_expect == 0)
272 DEBUGF("All threads have acknowledged the connect.\n");
273 #ifdef USB_REALLY_BRAVE
274 usb_slave_mode(true);
275 usb_state = USB_INSERTED;
276 cpu_idle_mode(true);
277 #else
278 system_reboot();
279 #endif
281 else
283 DEBUGF("usb: got ack, %d to go...\n",
284 num_acks_to_expect);
287 break;
289 case USB_EXTRACTED:
290 #ifdef HAVE_LCD_BITMAP
291 if(do_screendump_instead_of_usb)
292 break;
293 #endif
294 #ifdef HAVE_USB_POWER
295 if(usb_state == USB_POWERED)
297 usb_state = USB_EXTRACTED;
298 break;
300 #endif
301 if(usb_state == USB_INSERTED)
303 /* Only disable the USB mode if we really have enabled it
304 some threads might not have acknowledged the
305 insertion */
306 usb_slave_mode(false);
307 cpu_idle_mode(false);
310 usb_state = USB_EXTRACTED;
312 /* Tell all threads that we are back in business */
313 num_acks_to_expect =
314 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
315 waiting_for_ack = true;
316 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
317 num_acks_to_expect);
318 #ifdef HAVE_LCD_CHARCELLS
319 lcd_icon(ICON_USB, false);
320 #endif
321 break;
323 case SYS_USB_DISCONNECTED_ACK:
324 if(waiting_for_ack)
326 num_acks_to_expect--;
327 if(num_acks_to_expect == 0)
329 DEBUGF("All threads have acknowledged. "
330 "We're in business.\n");
332 else
334 DEBUGF("usb: got ack, %d to go...\n",
335 num_acks_to_expect);
338 break;
340 #ifdef HAVE_MMC
341 case SYS_MMC_INSERTED:
342 case SYS_MMC_EXTRACTED:
343 if(usb_state == USB_INSERTED)
345 usb_enable(false);
346 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
348 break;
350 case USB_REENABLE:
351 if(usb_state == USB_INSERTED)
352 usb_enable(true); /* reenable only if still inserted */
353 break;
354 #endif
358 #endif
360 #ifndef TARGET_TREE
361 bool usb_detect(void)
363 bool current_status;
365 #ifdef USB_RECORDERSTYLE
366 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
367 #endif
368 #ifdef USB_FMRECORDERSTYLE
369 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
370 #endif
371 #ifdef USB_PLAYERSTYLE
372 current_status = (PADR & 0x8000)?false:true;
373 #endif
374 #ifdef USB_GMINISTYLE
375 current_status = (P5 & 0x10)?true:false;
376 #endif
377 #ifdef USB_IPODSTYLE
378 /* The following check is in the ipodlinux source, with the
379 comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
380 if (USB2D_IDENT != 0x22FA05) {
381 return false;
383 current_status = (USB_STATUS & 0x800)?true:false;
384 #endif
385 #ifdef USB_ISP1582
386 /* TODO: Implement USB_ISP1582 */
387 current_status = false;
388 #endif
389 return current_status;
391 #endif
393 #ifndef BOOTLOADER
394 static void usb_tick(void)
396 bool current_status;
398 #ifdef USB_GMINISTYLE
399 /* Keep usb chip in usb state (?) */
400 if (P5 & 0x10) {
401 if ((P10 & 0x20) == 0 || (P6 & 0x08) == 0) {
402 if (smsc_version() < 4) {
403 P6 |= 0x08;
404 P10 |= 0x20;
408 #endif
410 if(usb_monitor_enabled)
412 current_status = usb_detect();
414 /* Only report when the status has changed */
415 if(current_status != last_usb_status)
417 last_usb_status = current_status;
418 countdown = NUM_POLL_READINGS;
420 else
422 /* Count down until it gets negative */
423 if(countdown >= 0)
424 countdown--;
426 /* Report to the thread if we have had 3 identical status
427 readings in a row */
428 if(countdown == 0)
430 if(current_status)
431 queue_post(&usb_queue, USB_INSERTED, NULL);
432 else
433 queue_post(&usb_queue, USB_EXTRACTED, NULL);
437 #ifdef HAVE_MMC
438 if(usb_mmc_countdown > 0)
440 usb_mmc_countdown--;
441 if (usb_mmc_countdown == 0)
442 queue_post(&usb_queue, USB_REENABLE, NULL);
444 #endif
446 #endif
448 void usb_acknowledge(long id)
450 queue_post(&usb_queue, id, NULL);
453 void usb_init(void)
455 usb_state = USB_EXTRACTED;
456 usb_monitor_enabled = false;
457 countdown = -1;
459 #ifdef TARGET_TREE
460 usb_init_device();
461 #endif
463 usb_enable(false);
465 /* We assume that the USB cable is extracted */
466 last_usb_status = false;
468 #ifndef BOOTLOADER
469 queue_init(&usb_queue, true);
470 create_thread(usb_thread, usb_stack, sizeof(usb_stack),
471 usb_thread_name IF_PRIO(, PRIORITY_SYSTEM));
473 tick_add_task(usb_tick);
474 #endif
478 void usb_wait_for_disconnect(struct event_queue *q)
480 struct 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 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_USB_POWER
530 bool usb_powered(void)
532 return usb_state == USB_POWERED;
534 #endif
536 #else
538 #ifdef USB_NONE
539 bool usb_inserted(void)
541 return false;
543 #endif
545 /* Dummy simulator functions */
546 void usb_acknowledge(long id)
548 id = id;
551 void usb_init(void)
555 void usb_start_monitoring(void)
559 bool usb_detect(void)
561 return false;
564 void usb_wait_for_disconnect(struct event_queue *q)
566 (void)q;
569 #endif /* USB_NONE or SIMULATOR */