Check if a new version got installed after usb disconnect and ask if user wants to...
[Rockbox.git] / firmware / usb.c
blobf5deed4da6152074e21bd8ad1d3e8a73e3902ea3
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
47 #ifdef IRIVER_H300_SERIES
48 #include "pcf50606.h" /* for pcf50606_usb_charging_... */
49 #endif
50 #include "logf.h"
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 */
57 #endif
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
66 #ifdef HAVE_MMC
67 #define USB_REENABLE 3
68 #endif
69 #ifdef HAVE_USB_POWER
70 #define USB_POWERED 4
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
93 #endif
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;
106 #endif
108 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
109 needs updating */
110 #ifndef BOOTLOADER
111 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
112 static const char usb_thread_name[] = "usb";
113 #endif
114 static struct event_queue usb_queue;
115 static bool last_usb_status;
116 static bool usb_monitor_enabled;
118 #ifndef TARGET_TREE
119 void usb_enable(bool on)
121 #ifdef USB_ENABLE_ONDIOSTYLE
122 PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
123 if(on)
125 #ifdef HAVE_MMC
126 mmc_enable_int_flash_clock(!mmc_detect());
127 #endif
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 */
133 else
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 */
143 (void) on;
144 #elif defined(USB_X5M5_STYLE)
145 /* TODO X5 */
146 #elif defined(USB_GIGABEAT_STYLE)
147 /* TODO gigabeat */
148 #else
149 #ifdef HAVE_LCD_BITMAP
150 if(read_hw_mask() & USB_ACTIVE_HIGH)
151 on = !on;
152 #endif
153 if(on)
155 and_b(~0x04, &PADRH); /* enable USB */
157 else
159 or_b(0x04, &PADRH);
161 or_b(0x04, &PAIORH);
162 #endif
164 #endif
166 #ifndef BOOTLOADER
167 static void usb_slave_mode(bool on)
169 int rc;
171 if(on)
173 DEBUGF("Entering USB slave mode\n");
174 ata_soft_reset();
175 ata_init();
176 ata_enable(false);
177 usb_enable(true);
179 else
181 DEBUGF("Leaving USB slave mode\n");
183 /* Let the ISDx00 settle */
184 sleep(HZ*1);
186 usb_enable(false);
188 rc = ata_init();
189 if(rc)
191 /* fixme: can we remove this? (already such in main.c) */
192 char str[32];
193 lcd_clear_display();
194 snprintf(str, 31, "ATA error: %d", rc);
195 lcd_puts(0, 0, str);
196 lcd_puts(0, 1, "Press ON to debug");
197 lcd_update();
198 while(!(button_get(true) & BUTTON_REL)) {};
199 dbg_ports();
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;
214 struct event ev;
216 waiting_for_ack = false;
218 while(1)
220 queue_wait(&usb_queue, &ev);
221 switch(ev.id)
223 case USB_INSERTED:
224 #ifdef HAVE_LCD_BITMAP
225 if(do_screendump_instead_of_usb)
227 screen_dump();
229 else
230 #endif
231 #ifdef HAVE_USB_POWER
232 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
234 usb_state = USB_POWERED;
236 else
237 #endif
239 /* Tell all threads that they have to back off the ATA.
240 We subtract one for our own thread. */
241 num_acks_to_expect =
242 queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
243 waiting_for_ack = true;
244 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
245 num_acks_to_expect);
247 break;
249 case SYS_USB_CONNECTED_ACK:
250 if(waiting_for_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;
259 cpu_idle_mode(true);
260 #else
261 system_reboot();
262 #endif
264 else
266 DEBUGF("usb: got ack, %d to go...\n",
267 num_acks_to_expect);
270 break;
272 case USB_EXTRACTED:
273 #ifdef HAVE_LCD_BITMAP
274 if(do_screendump_instead_of_usb)
275 break;
276 #endif
277 #ifdef HAVE_USB_POWER
278 if(usb_state == USB_POWERED)
280 usb_state = USB_EXTRACTED;
281 break;
283 #endif
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
288 insertion */
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 */
296 num_acks_to_expect =
297 queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
298 waiting_for_ack = true;
299 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
300 num_acks_to_expect);
301 #ifdef HAVE_LCD_CHARCELLS
302 lcd_icon(ICON_USB, false);
303 #endif
304 break;
306 case SYS_USB_DISCONNECTED_ACK:
307 if(waiting_for_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");
315 else
317 DEBUGF("usb: got ack, %d to go...\n",
318 num_acks_to_expect);
321 break;
323 #ifdef HAVE_MMC
324 case SYS_MMC_INSERTED:
325 case SYS_MMC_EXTRACTED:
326 if(usb_state == USB_INSERTED)
328 usb_enable(false);
329 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
331 break;
333 case USB_REENABLE:
334 if(usb_state == USB_INSERTED)
335 usb_enable(true); /* reenable only if still inserted */
336 break;
337 #endif
341 #endif
343 #ifndef TARGET_TREE
344 bool usb_detect(void)
346 bool current_status;
348 #ifdef USB_RECORDERSTYLE
349 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
350 #endif
351 #ifdef USB_FMRECORDERSTYLE
352 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
353 #endif
354 #ifdef USB_PLAYERSTYLE
355 current_status = (PADR & 0x8000)?false:true;
356 #endif
357 #ifdef USB_ISP1582
358 /* TODO: Implement USB_ISP1582 */
359 current_status = false;
360 #endif
361 return current_status;
363 #endif
365 #ifndef BOOTLOADER
366 static void usb_tick(void)
368 bool current_status;
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;
380 else
382 /* Count down until it gets negative */
383 if(countdown >= 0)
384 countdown--;
386 /* Report to the thread if we have had 3 identical status
387 readings in a row */
388 if(countdown == 0)
390 if(current_status)
391 queue_post(&usb_queue, USB_INSERTED, 0);
392 else
393 queue_post(&usb_queue, USB_EXTRACTED, 0);
397 #ifdef HAVE_MMC
398 if(usb_mmc_countdown > 0)
400 usb_mmc_countdown--;
401 if (usb_mmc_countdown == 0)
402 queue_post(&usb_queue, USB_REENABLE, 0);
404 #endif
406 #endif
408 void usb_acknowledge(long id)
410 queue_post(&usb_queue, id, 0);
413 void usb_init(void)
415 usb_state = USB_EXTRACTED;
416 usb_monitor_enabled = false;
417 countdown = -1;
419 #ifdef TARGET_TREE
420 usb_init_device();
421 #endif
423 usb_enable(false);
425 /* We assume that the USB cable is extracted */
426 last_usb_status = false;
428 #ifndef BOOTLOADER
429 queue_init(&usb_queue, true);
430 queue_set_irq_safe(&usb_queue, true);
432 create_thread(usb_thread, usb_stack, sizeof(usb_stack),
433 usb_thread_name IF_PRIO(, PRIORITY_SYSTEM)
434 IF_COP(, CPU, false));
436 tick_add_task(usb_tick);
437 #endif
441 void usb_wait_for_disconnect(struct event_queue *q)
443 struct event ev;
445 /* Don't return until we get SYS_USB_DISCONNECTED */
446 while(1)
448 queue_wait(q, &ev);
449 if(ev.id == SYS_USB_DISCONNECTED)
451 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
452 return;
457 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
459 struct event ev;
461 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
462 while(1)
464 queue_wait_w_tmo(q, &ev, ticks);
465 switch(ev.id)
467 case SYS_USB_DISCONNECTED:
468 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
469 return 0;
470 break;
471 case SYS_TIMEOUT:
472 return 1;
473 break;
478 void usb_start_monitoring(void)
480 usb_monitor_enabled = true;
483 bool usb_inserted(void)
485 #ifdef HAVE_USB_POWER
486 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
487 #else
488 return usb_state == USB_INSERTED;
489 #endif
492 #ifdef HAVE_USB_POWER
493 bool usb_powered(void)
495 return usb_state == USB_POWERED;
498 #if CONFIG_CHARGING
499 bool usb_charging_enable(bool on)
501 bool rc = false;
502 #ifdef IRIVER_H300_SERIES
503 int irqlevel;
504 logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
505 irqlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
506 pcf50606_set_usb_charging(on);
507 rc = on;
508 (void)set_irq_level(irqlevel);
509 #else
510 /* TODO: implement it for other targets... */
511 (void)on;
512 #endif
513 return rc;
516 bool usb_charging_enabled(void)
518 bool rc = false;
519 #ifdef IRIVER_H300_SERIES
520 /* TODO: read the state of the GPOOD2 register...
521 * (this also means to set the irq level here) */
522 rc = pcf50606_usb_charging_enabled();
523 #else
524 /* TODO: implement it for other targets... */
525 #endif
527 logf("usb_charging_enabled: %s\n", rc ? "true" : "false" );
528 return rc;
530 #endif
531 #endif
533 #else
535 #ifdef USB_NONE
536 bool usb_inserted(void)
538 return false;
540 #endif
542 /* Dummy simulator functions */
543 void usb_acknowledge(long id)
545 id = id;
548 void usb_init(void)
552 void usb_start_monitoring(void)
556 bool usb_detect(void)
558 return false;
561 void usb_wait_for_disconnect(struct event_queue *q)
563 (void)q;
566 #endif /* USB_NONE or SIMULATOR */