1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2002 Björn Stenberg
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <atoi.h>
#include <timefuncs.h>
#include <ctype.h>
#include "debug.h"
#include "button.h"
#include "lcd.h"
#include "dir.h"
#include "file.h"
#include "kernel.h"
#include "sprintf.h"
#include "logf.h"
#include "screens.h"
#include "misc.h"
#include "mas.h"
#include "plugin.h"
#include "lang.h"
#include "keyboard.h"
#include "mpeg.h"
#include "buffer.h"
#include "mp3_playback.h"
#include "backlight.h"
#include "ata.h"
#include "talk.h"
#include "mp3data.h"
#include "powermgmt.h"
#include "system.h"
#include "sound.h"
#include "database.h"
#if (CONFIG_HWCODEC == MASNONE)
#include "pcm_playback.h"
#endif
#ifdef HAVE_LCD_BITMAP
#include "peakmeter.h"
#include "widgets.h"
#include "bmp.h"
#endif
#ifdef HAVE_REMOTE_LCD
#include "lcd-remote.h"
#endif
#if MEM >= 32
#define PLUGIN_BUFFER_SIZE 0xC0000
#else
#define PLUGIN_BUFFER_SIZE 0x8000
#endif
#ifdef SIMULATOR
static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
void *sim_plugin_load(char *plugin, int *fd);
void sim_plugin_close(int fd);
#else
#define sim_plugin_close(x)
extern unsigned char pluginbuf[];
#include "bitswap.h"
#endif
static bool plugin_loaded = false;
static int plugin_size = 0;
#ifndef SIMULATOR
static void (*pfn_timer)(void) = NULL; /* user timer handler */
#endif
static void (*pfn_tsr_exit)(void) = NULL; /* TSR exit callback */
static int plugin_test(int api_version, int model, int memsize);
static const struct plugin_api rockbox_api = {
PLUGIN_API_VERSION,
plugin_test,
/* lcd */
lcd_clear_display,
lcd_puts,
lcd_puts_scroll,
lcd_stop_scroll,
lcd_set_contrast,
#ifdef HAVE_LCD_CHARCELLS
lcd_define_pattern,
lcd_get_locked_pattern,
lcd_unlock_pattern,
lcd_putc,
lcd_put_cursor,
lcd_remove_cursor,
PREFIX(lcd_icon),
#else
lcd_putsxy,
lcd_puts_style,
lcd_puts_scroll_style,
lcd_bitmap,
lcd_drawline,
lcd_clearline,
lcd_drawpixel,
lcd_clearpixel,
lcd_setfont,
font_get,
lcd_clearrect,
lcd_fillrect,
lcd_drawrect,
lcd_invertrect,
lcd_getstringsize,
lcd_update,
lcd_update_rect,
scrollbar,
checkbox,
&lcd_framebuffer[0][0],
lcd_blit,
#ifndef SIMULATOR
lcd_roll,
#endif
#endif
backlight_on,
backlight_off,
backlight_set_timeout,
splash,
#ifdef HAVE_REMOTE_LCD
/* remote lcd */
lcd_remote_clear_display,
lcd_remote_puts,
lcd_remote_puts_scroll,
lcd_remote_stop_scroll,
lcd_remote_set_contrast,
lcd_remote_putsxy,
lcd_remote_puts_style,
lcd_remote_puts_scroll_style,
lcd_remote_bitmap,
lcd_remote_drawline,
lcd_remote_clearline,
lcd_remote_drawpixel,
lcd_remote_clearpixel,
lcd_remote_setfont,
font_get,
lcd_remote_clearrect,
lcd_remote_fillrect,
lcd_remote_drawrect,
lcd_remote_invertrect,
lcd_remote_getstringsize,
lcd_remote_update,
lcd_remote_update_rect,
lcd_remote_backlight_on,
lcd_remote_backlight_off,
&lcd_remote_framebuffer[0][0],
#endif
/* button */
button_get,
button_get_w_tmo,
button_status,
button_clear_queue,
#if CONFIG_KEYPAD == IRIVER_H100_PAD
button_hold,
#endif
/* file */
(open_func)PREFIX(open),
close,
(read_func)read,
PREFIX(lseek),
(creat_func)PREFIX(creat),
(write_func)write,
PREFIX(remove),
PREFIX(rename),
PREFIX(ftruncate),
PREFIX(filesize),
fdprintf,
read_line,
settings_parseline,
#ifndef SIMULATOR
ata_sleep,
#endif
/* dir */
PREFIX(opendir),
PREFIX(closedir),
PREFIX(readdir),
PREFIX(mkdir),
/* kernel/ system */
PREFIX(sleep),
yield,
¤t_tick,
default_event_handler,
default_event_handler_ex,
create_thread,
remove_thread,
reset_poweroff_timer,
#ifndef SIMULATOR
system_memory_guard,
&cpu_frequency,
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
cpu_boost,
#endif
#endif
/* strings and memory */
snprintf,
strcpy,
strncpy,
strlen,
strrchr,
strcmp,
strcasecmp,
strncasecmp,
memset,
memcpy,
_ctype_,
atoi,
strchr,
strcat,
memcmp,
strcasestr,
/* sound */
sound_set,
#ifndef SIMULATOR
mp3_play_data,
mp3_play_pause,
mp3_play_stop,
mp3_is_playing,
#if CONFIG_HWCODEC != MASNONE
bitswap,
#endif
#if CONFIG_HWCODEC == MASNONE
pcm_play_data,
pcm_play_stop,
pcm_set_frequency,
pcm_is_playing,
pcm_play_pause,
#endif
#endif
/* playback control */
PREFIX(audio_play),
audio_stop,
audio_pause,
audio_resume,
audio_next,
audio_prev,
audio_ff_rewind,
audio_next_track,
playlist_amount,
audio_status,
audio_has_changed_track,
audio_current_track,
audio_flush_and_reload_tracks,
audio_get_file_pos,
#if !defined(SIMULATOR) && (CONFIG_HWCODEC != MASNONE)
mpeg_get_last_header,
#endif
#if (CONFIG_HWCODEC == MAS3587F) || (CONFIG_HWCODEC == MAS3539F)
sound_set_pitch,
#endif
#if !defined(SIMULATOR) && (CONFIG_HWCODEC != MASNONE)
/* MAS communication */
mas_readmem,
mas_writemem,
mas_readreg,
mas_writereg,
#if (CONFIG_HWCODEC == MAS3587F) || (CONFIG_HWCODEC == MAS3539F)
mas_codec_writereg,
mas_codec_readreg,
#endif
#endif /* !simulator and HWCODEC != MASNONE */
/* tag database */
&tagdbheader,
&tagdb_fd,
&tagdb_initialized,
tagdb_init,
/* misc */
srand,
rand,
(qsort_func)qsort,
kbd_input,
get_time,
set_time,
plugin_get_buffer,
plugin_get_audio_buffer,
#ifndef SIMULATOR
plugin_register_timer,
plugin_unregister_timer,
#endif
plugin_tsr,
#if defined(DEBUG) || defined(SIMULATOR)
debugf,
#endif
&global_settings,
mp3info,
count_mp3_frames,
create_xing_header,
find_next_frame,
battery_level,
battery_level_safe,
#if (CONFIG_HWCODEC == MAS3587F) || (CONFIG_HWCODEC == MAS3539F)
peak_meter_scale_value,
peak_meter_set_use_dbfs,
peak_meter_get_use_dbfs,
#endif
#ifdef HAVE_LCD_BITMAP
read_bmp_file,
#endif
/* new stuff at the end, sort into place next time
the API gets incompatible */
#ifdef ROCKBOX_HAS_LOGF
logf,
#endif
};
#if CONFIG_HWCODEC == MASNONE
int codec_load_ram(char* pluginptr, size_t size, void *parameter, void* ptr2,
size_t bufwrap)
{
enum plugin_status (*plugin_start)(struct plugin_api* api, void* param);
int copy_n;
int status;
plugin_size = size;
if ((int)&pluginbuf != (int)pluginptr) {
/* zero out plugin buffer to ensure a properly zeroed bss area */
memset(pluginbuf, 0, PLUGIN_BUFFER_SIZE);
size = MIN(size, PLUGIN_BUFFER_SIZE);
copy_n = MIN(size, bufwrap);
memcpy(pluginbuf, pluginptr, copy_n);
size -= copy_n;
if (size > 0) {
memcpy(&pluginbuf[copy_n], ptr2, size);
}
}
plugin_start = (void*)&pluginbuf;
if (plugin_size <= 0) {
return -1;
}
plugin_loaded = true;
invalidate_icache();
status = plugin_start((struct plugin_api*) &rockbox_api, parameter);
plugin_loaded = false;
return status;
}
int codec_load_file(const char *plugin, void *parameter)
{
char msgbuf[80];
int fd;
int rc;
fd = open(plugin, O_RDONLY);
if (fd < 0) {
snprintf(msgbuf, sizeof(msgbuf)-1, "Couldn't load codec: %s", plugin);
logf("Codec load error:%d", fd);
splash(HZ*2, true, msgbuf);
return fd;
}
rc = read(fd, &pluginbuf[0], PLUGIN_BUFFER_SIZE);
close(fd);
if (rc <= 0) {
logf("Codec read error");
return PLUGIN_ERROR;
}
plugin_size = rc;
return codec_load_ram(pluginbuf, plugin_size, parameter, NULL, 0);
}
#endif
int plugin_load(const char* plugin, void* parameter)
{
enum plugin_status (*plugin_start)(struct plugin_api* api, void* param);
int rc;
#ifndef SIMULATOR
char buf[64];
#endif
int fd;
#ifdef HAVE_LCD_BITMAP
int xm,ym;
#endif
if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
{
pfn_tsr_exit(); /* force it to exit now */
pfn_tsr_exit = NULL;
}
#ifdef HAVE_LCD_BITMAP
lcd_clear_display();
xm = lcd_getxmargin();
ym = lcd_getymargin();
lcd_setmargins(0,0);
lcd_update();
#else
lcd_clear_display();
#endif
#ifdef SIMULATOR
plugin_start = sim_plugin_load((char *)plugin, &fd);
if(!plugin_start)
return -1;
#else
fd = open(plugin, O_RDONLY);
if (fd < 0) {
snprintf(buf, sizeof buf, str(LANG_PLUGIN_CANT_OPEN), plugin);
splash(HZ*2, true, buf);
return fd;
}
/* zero out plugin buffer to ensure a properly zeroed bss area */
memset(pluginbuf, 0, PLUGIN_BUFFER_SIZE);
plugin_start = (void*)&pluginbuf;
plugin_size = read(fd, plugin_start, PLUGIN_BUFFER_SIZE);
close(fd);
if (plugin_size < 0) {
/* read error */
snprintf(buf, sizeof buf, str(LANG_READ_FAILED), plugin);
splash(HZ*2, true, buf);
return -1;
}
if (plugin_size == 0) {
/* loaded a 0-byte plugin, implying it's not for this model */
splash(HZ*2, true, str(LANG_PLUGIN_WRONG_MODEL));
return -1;
}
#endif
plugin_loaded = true;
invalidate_icache();
rc = plugin_start((struct plugin_api*) &rockbox_api, parameter);
/* explicitly casting the pointer here to avoid touching every plugin. */
button_clear_queue();
plugin_loaded = false;
switch (rc) {
case PLUGIN_OK:
break;
case PLUGIN_USB_CONNECTED:
return PLUGIN_USB_CONNECTED;
case PLUGIN_WRONG_API_VERSION:
splash(HZ*2, true, str(LANG_PLUGIN_WRONG_VERSION));
break;
case PLUGIN_WRONG_MODEL:
splash(HZ*2, true, str(LANG_PLUGIN_WRONG_MODEL));
break;
default:
splash(HZ*2, true, str(LANG_PLUGIN_ERROR));
break;
}
sim_plugin_close(fd);
#ifdef HAVE_LCD_BITMAP
/* restore margins */
lcd_setmargins(xm,ym);
#endif
return PLUGIN_OK;
}
/* Returns a pointer to the portion of the plugin buffer that is not already
being used. If no plugin is loaded, returns the entire plugin buffer */
void* plugin_get_buffer(int* buffer_size)
{
int buffer_pos;
if (plugin_loaded)
{
if (plugin_size >= PLUGIN_BUFFER_SIZE)
return NULL;
*buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
buffer_pos = plugin_size;
}
else
{
*buffer_size = PLUGIN_BUFFER_SIZE;
buffer_pos = 0;
}
return &pluginbuf[buffer_pos];
}
/* Returns a pointer to the mp3 buffer.
Playback gets stopped, to avoid conflicts. */
void* plugin_get_audio_buffer(int* buffer_size)
{
audio_stop();
talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
*buffer_size = audiobufend - audiobuf;
return audiobuf;
}
#ifndef SIMULATOR
/* Register a periodic time callback, called every "cycles" CPU clocks.
Note that this function will be called in interrupt context! */
int plugin_register_timer(int cycles, int prio, void (*timer_callback)(void))
{
int phi = 0; /* bits for the prescaler */
int prescale = 1;
while (cycles > 0x10000)
{ /* work out the smallest prescaler that makes it fit */
phi++;
prescale *= 2;
cycles /= 2;
}
if (prescale > 8 || cycles == 0 || prio < 1 || prio > 15)
return 0; /* error, we can't do such period, bad argument */
#if CONFIG_CPU == SH7034
and_b(~0x10, &TSTR); /* Stop the timer 4 */
and_b(~0x10, &TSNC); /* No synchronization */
and_b(~0x10, &TMDR); /* Operate normally */
pfn_timer = timer_callback; /* install 2nd level ISR */
and_b(~0x01, &TSR4);
TIER4 = 0xF9; /* Enable GRA match interrupt */
GRA4 = (unsigned short)(cycles - 1);
TCR4 = 0x20 | phi; /* clear at GRA match, set prescaler */
IPRD = (IPRD & 0xFF0F) | prio << 4; /* interrupt priority */
or_b(0x10, &TSTR); /* start timer 4 */
#else
pfn_timer = timer_callback;
#endif
return cycles * prescale; /* return the actual period, in CPU clocks */
}
/* disable the user timer */
void plugin_unregister_timer(void)
{
#if CONFIG_CPU == SH7034
and_b(~0x10, &TSTR); /* stop the timer 4 */
IPRD = (IPRD & 0xFF0F); /* disable interrupt */
pfn_timer = NULL;
#endif
}
#if CONFIG_CPU == SH7034
/* interrupt handler for user timer */
#pragma interrupt
void IMIA4(void)
{
if (pfn_timer != NULL)
pfn_timer(); /* call the user timer function */
and_b(~0x01, &TSR4); /* clear the interrupt */
}
#endif /* CONFIG_CPU == SH7034 */
#endif /* #ifndef SIMULATOR */
/* The plugin wants to stay resident after leaving its main function, e.g.
runs from timer or own thread. The callback is registered to later
instruct it to free its resources before a new plugin gets loaded. */
void plugin_tsr(void (*exit_callback)(void))
{
pfn_tsr_exit = exit_callback; /* remember the callback for later */
}
static int plugin_test(int api_version, int model, int memsize)
{
if (api_version < PLUGIN_MIN_API_VERSION ||
api_version > PLUGIN_API_VERSION)
return PLUGIN_WRONG_API_VERSION;
if (model != MODEL)
return PLUGIN_WRONG_MODEL;
if (memsize != MEM)
return PLUGIN_WRONG_MODEL;
return PLUGIN_OK;
}
|