Patch #1038325 by Markus Braun, prevents WPS from setting the volume higher than 100
[kugel-rb.git] / firmware / usb.c
blob6c14b75f197e90a15b68b50873ac95e1e1467892
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
20 #include "sh7034.h"
21 #include "kernel.h"
22 #include "thread.h"
23 #include "system.h"
24 #include "debug.h"
25 #include "ata.h"
26 #include "fat.h"
27 #include "disk.h"
28 #include "panic.h"
29 #include "lcd.h"
30 #include "adc.h"
31 #include "usb.h"
32 #include "button.h"
33 #include "sprintf.h"
34 #include "hwcompat.h"
36 extern void dbg_ports(void); /* NASTY! defined in apps/ */
38 #ifdef HAVE_LCD_BITMAP
39 bool do_screendump_instead_of_usb = false;
40 void screen_dump(void); /* Nasty again. Defined in apps/ too */
41 #endif
43 #define USB_REALLY_BRAVE
45 #if !defined(SIMULATOR) && !defined(USB_NONE)
47 /* Messages from usb_tick */
48 #define USB_INSERTED 1
49 #define USB_EXTRACTED 2
51 /* Thread states */
52 #define EXTRACTING 1
53 #define EXTRACTED 2
54 #define INSERTED 3
55 #define INSERTING 4
57 /* The ADC tick reads one channel per tick, and we want to check 3 successive
58 readings on the USB voltage channel. This doesn't apply to the Player, but
59 debouncing the USB detection port won't hurt us either. */
60 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
61 static int countdown;
63 static int usb_state;
65 /* FIXME: The extra 0x400 is consumed by fat_mount() when the fsinfo
66 needs updating */
67 static char usb_stack[DEFAULT_STACK_SIZE + 0x400];
68 static const char usb_thread_name[] = "usb";
69 static struct event_queue usb_queue;
70 static bool last_usb_status;
71 static bool usb_monitor_enabled;
73 static void usb_enable(bool on)
75 #ifdef USB_ENABLE_ONDIOSTYLE
76 if(on)
78 or_b(0x20, &PADRL); /* enable USB */
79 and_b(~0x08, &PADRL); /* assert card detect */
81 else
83 and_b(~0x20, &PADRL); /* disable USB */
84 or_b(0x08, &PADRL); /* deassert card detect */
86 PACR2 &= ~0x00C0; /* use PA3 as GPIO */
87 or_b(0x28, &PAIORL); /* output for USB enable and card detect */
88 #else /* standard HD Jukebox */
89 #ifdef HAVE_LCD_BITMAP
90 if(read_hw_mask() & USB_ACTIVE_HIGH)
91 on = !on;
92 #endif
93 if(on)
95 and_b(~0x04, &PADRH); /* enable USB */
97 else
99 or_b(0x04, &PADRH);
101 or_b(0x04, &PAIORH);
102 #endif
105 static void usb_slave_mode(bool on)
107 int rc;
108 struct partinfo* pinfo;
110 if(on)
112 DEBUGF("Entering USB slave mode\n");
113 ata_soft_reset();
114 ata_init();
115 ata_standby(15);
116 ata_enable(false);
117 usb_enable(true);
119 else
121 int i;
122 DEBUGF("Leaving USB slave mode\n");
124 /* Let the ISDx00 settle */
125 sleep(HZ*1);
127 usb_enable(false);
129 rc = ata_init();
130 if(rc)
132 char str[32];
133 lcd_clear_display();
134 snprintf(str, 31, "ATA error: %d", rc);
135 lcd_puts(0, 0, str);
136 lcd_puts(0, 1, "Press ON to debug");
137 lcd_update();
138 while(!(button_get(true) & BUTTON_REL)) {};
139 dbg_ports();
140 panicf("ata: %d",rc);
143 pinfo = disk_init();
144 if (!pinfo)
145 panicf("disk: NULL");
147 for ( i=0; i<4; i++ ) {
148 rc = fat_mount(pinfo[i].start);
149 if (!rc)
150 break;
152 if (i==4)
153 panicf("mount: %d",rc);
157 static void usb_thread(void)
159 int num_acks_to_expect = -1;
160 bool waiting_for_ack;
161 struct event ev;
163 waiting_for_ack = false;
165 while(1)
167 queue_wait(&usb_queue, &ev);
168 switch(ev.id)
170 case USB_INSERTED:
171 #ifdef HAVE_LCD_BITMAP
172 if(do_screendump_instead_of_usb)
174 screen_dump();
176 else
178 #endif
179 /* Tell all threads that they have to back off the ATA.
180 We subtract one for our own thread. */
181 num_acks_to_expect =
182 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
183 waiting_for_ack = true;
184 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
185 num_acks_to_expect);
186 #ifdef HAVE_LCD_BITMAP
188 #endif
189 break;
191 case SYS_USB_CONNECTED_ACK:
192 if(waiting_for_ack)
194 num_acks_to_expect--;
195 if(num_acks_to_expect == 0)
197 DEBUGF("All threads have acknowledged the connect.\n");
198 #ifdef USB_REALLY_BRAVE
199 usb_slave_mode(true);
200 usb_state = USB_INSERTED;
201 #else
202 system_reboot();
203 #endif
205 else
207 DEBUGF("usb: got ack, %d to go...\n",
208 num_acks_to_expect);
211 break;
213 case USB_EXTRACTED:
214 #ifdef HAVE_LCD_BITMAP
215 if(!do_screendump_instead_of_usb)
217 #endif
218 if(usb_state == USB_INSERTED)
220 /* Only disable the USB mode if we really have enabled it
221 some threads might not have acknowledged the
222 insertion */
223 usb_slave_mode(false);
226 usb_state = USB_EXTRACTED;
228 /* Tell all threads that we are back in business */
229 num_acks_to_expect =
230 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
231 waiting_for_ack = true;
232 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
233 num_acks_to_expect);
234 #ifdef HAVE_LCD_CHARCELLS
235 lcd_icon(ICON_USB, false);
236 #endif
237 #ifdef HAVE_LCD_BITMAP
239 #endif
240 break;
242 case SYS_USB_DISCONNECTED_ACK:
243 if(waiting_for_ack)
245 num_acks_to_expect--;
246 if(num_acks_to_expect == 0)
248 DEBUGF("All threads have acknowledged. "
249 "We're in business.\n");
251 else
253 DEBUGF("usb: got ack, %d to go...\n",
254 num_acks_to_expect);
257 break;
262 bool usb_detect(void)
264 bool current_status;
266 #ifdef USB_RECORDERSTYLE
267 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
268 #endif
269 #ifdef USB_FMRECORDERSTYLE
270 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
271 #endif
272 #ifdef USB_PLAYERSTYLE
273 current_status = (PADR & 0x8000)?false:true;
274 #endif
276 return current_status;
280 static void usb_tick(void)
282 bool current_status;
284 if(usb_monitor_enabled)
286 current_status = usb_detect();
288 /* Only report when the status has changed */
289 if(current_status != last_usb_status)
291 last_usb_status = current_status;
292 countdown = NUM_POLL_READINGS;
294 else
296 /* Count down until it gets negative */
297 if(countdown >= 0)
298 countdown--;
300 /* Report to the thread if we have had 3 identical status
301 readings in a row */
302 if(countdown == 0)
304 if(current_status)
305 queue_post(&usb_queue, USB_INSERTED, NULL);
306 else
307 queue_post(&usb_queue, USB_EXTRACTED, NULL);
313 void usb_acknowledge(int id)
315 queue_post(&usb_queue, id, NULL);
318 void usb_init(void)
320 usb_state = USB_EXTRACTED;
321 usb_monitor_enabled = false;
322 countdown = -1;
324 usb_enable(false);
326 /* We assume that the USB cable is extracted */
327 last_usb_status = false;
329 queue_init(&usb_queue);
330 create_thread(usb_thread, usb_stack, sizeof(usb_stack), usb_thread_name);
332 tick_add_task(usb_tick);
335 void usb_wait_for_disconnect(struct event_queue *q)
337 struct event ev;
339 /* Don't return until we get SYS_USB_DISCONNECTED */
340 while(1)
342 queue_wait(q, &ev);
343 if(ev.id == SYS_USB_DISCONNECTED)
345 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
346 return;
351 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
353 struct event ev;
355 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
356 while(1)
358 queue_wait_w_tmo(q, &ev, ticks);
359 switch(ev.id)
361 case SYS_USB_DISCONNECTED:
362 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
363 return 0;
364 break;
365 case SYS_TIMEOUT:
366 return 1;
367 break;
372 void usb_start_monitoring(void)
374 usb_monitor_enabled = true;
377 bool usb_inserted(void)
379 return usb_state == USB_INSERTED;
382 #else
384 #ifdef USB_NONE
385 bool usb_inserted(void)
387 return false;
389 #endif
391 /* Dummy simulator functions */
392 void usb_acknowledge(int id)
394 id = id;
397 void usb_init(void)
401 void usb_start_monitoring(void)
405 bool usb_detect(void)
407 return false;
410 #endif /* USB_NONE or SIMULATOR */