drc: rework jump_out lists
[pcsx_rearmed.git] / frontend / plat_pollux.c
CommitLineData
55b0eeea 1/*
c02c3b65 2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2012
55b0eeea 3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
c02c3b65 6 *
7 * GPH claims:
8 * Main Memory : Wiz = 0 ~ 2A00000, Caanoo = 0 ~ 5600000 (86M)
9 * Frame Buffer : Wiz = 2A00000 ~ 2E00000 (4M), Caanoo = 5600000 ~ 5700000 (1M)
10 * Sound Buffer : Wiz = 2E00000 ~ 3000000 (2M), Caanoo = 5700000 ~ 5800000 (1M)
11 * YUV Buffer : Caanoo = 5800000 ~ 6000000 (8M)
12 * 3D Buffer : Wiz = 3000000 ~ 4000000 (16M),Caanoo = 6000000 ~ 8000000 (32M)
13 *
14 * Caanoo dram column (or row?) is 1024 bytes?
15 *
16 * pollux display array:
17 * 27:24 | 23:18 | 17:11 | 10:6 | 5:0
18 * seg | y[11:5] | x[11:6] | y[4:0] | x[5:0]
19 * | blk_index[12:0] | block[10:0]
55b0eeea 20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <sys/ioctl.h>
29#include <unistd.h>
30#include <linux/fb.h>
31#include <sys/mman.h>
221be40d 32#include <linux/soundcard.h>
b07c18e8 33#include <linux/input.h>
55b0eeea 34
cc56203b 35#include "libpicofe/plat.h"
36#include "libpicofe/input.h"
37#include "libpicofe/gp2x/in_gp2x.h"
38#include "libpicofe/gp2x/soc_pollux.h"
39#include "libpicofe/linux/in_evdev.h"
40#include "libpicofe/menu.h"
55b0eeea 41#include "warm/warm.h"
42#include "plugin_lib.h"
221be40d 43#include "pl_gun_ts.h"
02ee7e24 44#include "blit320.h"
faf2b2aa 45#include "in_tsbutton.h"
55b0eeea 46#include "main.h"
47#include "menu.h"
48#include "plat.h"
c82f907a 49#include "cspace.h"
c02c3b65 50#include "../libpcsxcore/psxmem_map.h"
55b0eeea 51
221be40d 52
cc56203b 53static int fbdev = -1;
55b0eeea 54static void *fb_vaddrs[2];
55static unsigned int fb_paddrs[2];
56static int fb_work_buf;
cc56203b 57static int have_warm;
0b6c6da8 58#define FB_VRAM_SIZE (320*240*2*2*2) // 2 buffers with space for 24bpp mode
c02c3b65 59static unsigned int uppermem_pbase, vram_pbase;
55b0eeea 60
61static unsigned short *psx_vram;
02ee7e24 62static int psx_step, psx_width, psx_height, psx_bpp;
a72ac803 63static int psx_offset_x, psx_offset_y, psx_src_width, psx_src_height;
41f55c9f 64static int fb_offset_x, fb_offset_y;
55b0eeea 65
9b4bd105 66static void caanoo_init(void);
b07c18e8 67static void wiz_init(void);
68
9b4bd105 69
b07c18e8 70static const struct in_default_bind in_evdev_defbinds[] = {
71 { KEY_UP, IN_BINDTYPE_PLAYER12, DKEY_UP },
72 { KEY_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN },
73 { KEY_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT },
74 { KEY_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
75 { BTN_TOP, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
76 { BTN_THUMB, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
77 { BTN_THUMB2, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
78 { BTN_TRIGGER, IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
79 { BTN_BASE3, IN_BINDTYPE_PLAYER12, DKEY_START },
80 { BTN_BASE4, IN_BINDTYPE_PLAYER12, DKEY_SELECT },
81 { BTN_TOP2, IN_BINDTYPE_PLAYER12, DKEY_L1 },
82 { BTN_PINKIE, IN_BINDTYPE_PLAYER12, DKEY_R1 },
83 { BTN_BASE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
84 { 0, 0, 0 },
85};
9b4bd105 86
bcfc48e3 87static const struct menu_keymap key_pbtn_map[] =
88{
89 { KEY_UP, PBTN_UP },
90 { KEY_DOWN, PBTN_DOWN },
91 { KEY_LEFT, PBTN_LEFT },
92 { KEY_RIGHT, PBTN_RIGHT },
93 /* Caanoo */
94 { BTN_THUMB2, PBTN_MOK },
95 { BTN_THUMB, PBTN_MBACK },
96 { BTN_TRIGGER, PBTN_MA2 },
97 { BTN_TOP, PBTN_MA3 },
98 { BTN_BASE, PBTN_MENU },
99 { BTN_TOP2, PBTN_L },
100 { BTN_PINKIE, PBTN_R },
101 /* "normal" keyboards */
102 { KEY_ENTER, PBTN_MOK },
103 { KEY_ESC, PBTN_MBACK },
104 { KEY_SEMICOLON, PBTN_MA2 },
105 { KEY_APOSTROPHE, PBTN_MA3 },
106 { KEY_BACKSLASH, PBTN_MENU },
107 { KEY_LEFTBRACE, PBTN_L },
108 { KEY_RIGHTBRACE, PBTN_R },
109};
110
111static const struct in_pdata gp2x_evdev_pdata = {
112 .defbinds = in_evdev_defbinds,
113 .key_map = key_pbtn_map,
114 .kmap_size = sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]),
115};
116
55b0eeea 117static void *fb_flip(void)
118{
0b6c6da8 119 memregl[0x406C>>2] = memregl[0x446C>>2] = fb_paddrs[fb_work_buf];
55b0eeea 120 memregl[0x4058>>2] |= 0x10;
0b6c6da8 121 memregl[0x4458>>2] |= 0x10;
55b0eeea 122 fb_work_buf ^= 1;
123 return fb_vaddrs[fb_work_buf];
124}
125
126static void pollux_changemode(int bpp, int is_bgr)
127{
128 int code = 0, bytes = 2;
129 unsigned int r;
130
131 printf("changemode: %dbpp %s\n", bpp, is_bgr ? "bgr" : "rgb");
132
133 memregl[0x4004>>2] = 0x00ef013f;
134 memregl[0x4000>>2] |= 1 << 3;
135
136 switch (bpp)
137 {
138 case 8:
139 code = 0x443a;
140 bytes = 1;
141 break;
142 case 16:
143 code = is_bgr ? 0xc342 : 0x4432;
144 bytes = 2;
145 break;
146 case 24:
147 code = is_bgr ? 0xc653 : 0x4653;
148 bytes = 3;
149 break;
150 default:
151 printf("unhandled bpp request: %d\n", bpp);
152 return;
153 }
154
0b6c6da8 155 // program both MLCs so that TV-out works
156 memregl[0x405c>>2] = memregl[0x445c>>2] = bytes;
157 memregl[0x4060>>2] = memregl[0x4460>>2] = 320 * bytes;
55b0eeea 158
159 r = memregl[0x4058>>2];
160 r = (r & 0xffff) | (code << 16) | 0x10;
161 memregl[0x4058>>2] = r;
0b6c6da8 162
163 r = memregl[0x4458>>2];
164 r = (r & 0xffff) | (code << 16) | 0x10;
165 memregl[0x4458>>2] = r;
55b0eeea 166}
167
cc56203b 168static int cpu_clock_wrapper(int mhz)
55b0eeea 169{
55b0eeea 170 // stupid pll share hack - must restart audio
cc56203b 171 int pollux_cpu_clock_set(int cpu_clock);
55b0eeea 172 extern long SPUopen(void);
173 extern long SPUclose(void);
cc56203b 174
175 pollux_cpu_clock_set(mhz);
55b0eeea 176 SPUclose();
177 SPUopen();
178
179 return 0;
180}
181
55b0eeea 182#define TIMER_BASE3 0x1980
183#define TIMER_REG(x) memregl[(TIMER_BASE3 + x) >> 2]
184
185static __attribute__((unused)) unsigned int timer_get(void)
186{
187 TIMER_REG(0x08) |= 0x48; /* run timer, latch value */
188 return TIMER_REG(0);
189}
190
55b0eeea 191void plat_video_menu_enter(int is_rom_loaded)
192{
193 if (pl_vout_buf != NULL) {
194 if (psx_bpp == 16)
195 // have to do rgb conversion for menu bg
196 bgr555_to_rgb565(pl_vout_buf, pl_vout_buf, 320*240*2);
197 else
198 memset(pl_vout_buf, 0, 320*240*2);
199 }
200
201 pollux_changemode(16, 0);
202}
203
204void plat_video_menu_begin(void)
205{
206}
207
208void plat_video_menu_end(void)
209{
210 g_menuscreen_ptr = fb_flip();
211}
212
213void plat_video_menu_leave(void)
214{
55b0eeea 215 if (psx_vram == NULL) {
216 fprintf(stderr, "GPU plugin did not provide vram\n");
217 exit(1);
218 }
219
221be40d 220 if (gp2x_dev_id == GP2X_DEV_CAANOO)
221 in_set_config_int(in_name_to_id("evdev:pollux-analog"),
55b0eeea 222 IN_CFG_ABS_DEAD_ZONE, analog_deadzone);
41f55c9f 223
224 memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
225 g_menuscreen_ptr = fb_flip();
ddc0a02a 226 memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
55b0eeea 227}
228
47821672 229void *plat_prepare_screenshot(int *w, int *h, int *bpp)
230{
231 bgr555_to_rgb565(pl_vout_buf, pl_vout_buf, 320*240*2);
232 *w = 320;
233 *h = 240;
234 *bpp = psx_bpp;
235 return pl_vout_buf;
236}
237
a805c855 238void plat_minimize(void)
239{
240}
241
55b0eeea 242static void spend_cycles(int loops)
243{
244 asm volatile (
245 " mov r0,%0 ;\n"
246 "0: subs r0,r0,#1 ;\n"
247 " bgt 0b"
248 :: "r" (loops) : "cc", "r0");
249}
250
251#define DMA_BASE6 0x0300
252#define DMA_REG(x) memregl[(DMA_BASE6 + x) >> 2]
253
254/* this takes ~1.5ms, while ldm/stm ~1.95ms */
02783d0b 255static void raw_blit_dma(int doffs, const void *vram, int w, int h,
256 int sstride, int bgr24)
55b0eeea 257{
02783d0b 258 unsigned int pixel_offset = (unsigned short *)vram - psx_vram;
41f55c9f 259 unsigned int dst = fb_paddrs[fb_work_buf] +
260 (fb_offset_y * 320 + fb_offset_x) * psx_bpp / 8;
fa56d360 261 int spsx_line = pixel_offset / 1024 + psx_offset_y;
262 int spsx_offset = (pixel_offset + psx_offset_x) & 0x3f8;
c02c3b65 263 unsigned int vram_byte_pos, vram_byte_step;
55b0eeea 264 int dst_stride = 320 * psx_bpp / 8;
a72ac803 265 int len = psx_src_width * psx_bpp / 8;
55b0eeea 266 int i;
267
c02c3b65 268 //warm_cache_op_all(WOP_D_CLEAN);
55b0eeea 269
270 dst &= ~7;
271 len &= ~7;
272
273 if (DMA_REG(0x0c) & 0x90000) {
274 printf("already runnig DMA?\n");
275 DMA_REG(0x0c) = 0x100000;
276 }
277 if ((DMA_REG(0x2c) & 0x0f) < 5) {
278 printf("DMA queue busy?\n");
279 DMA_REG(0x24) = 1;
280 }
281
c02c3b65 282 vram_byte_pos = vram_pbase;
283 vram_byte_pos += (spsx_line & 511) * 2 * 1024 + spsx_offset * 2;
284 vram_byte_step = psx_step * 2 * 1024;
285
286 for (i = psx_src_height; i > 0;
287 i--, vram_byte_pos += vram_byte_step, dst += dst_stride)
288 {
55b0eeea 289 while ((DMA_REG(0x2c) & 0x0f) < 4)
290 spend_cycles(10);
291
292 // XXX: it seems we must always set all regs, what is autoincrement there for?
293 DMA_REG(0x20) = 1; // queue wait cmd
c02c3b65 294 DMA_REG(0x10) = vram_byte_pos; // DMA src
55b0eeea 295 DMA_REG(0x14) = dst; // DMA dst
296 DMA_REG(0x18) = len - 1; // len
297 DMA_REG(0x1c) = 0x80000; // go
298 }
55b0eeea 299}
300
02ee7e24 301#define make_flip_func(name, blitfunc) \
02783d0b 302static void name(int doffs, const void *vram_, int w, int h, int sstride, int bgr24) \
02ee7e24 303{ \
fa56d360 304 const unsigned short *vram = vram_; \
02ee7e24 305 unsigned char *dst = (unsigned char *)g_menuscreen_ptr + \
306 (fb_offset_y * 320 + fb_offset_x) * psx_bpp / 8; \
02ee7e24 307 int dst_stride = 320 * psx_bpp / 8; \
a72ac803 308 int len = psx_src_width * psx_bpp / 8; \
02ee7e24 309 int i; \
310 \
fa56d360 311 vram += psx_offset_y * 1024 + psx_offset_x; \
d659b045 312 vram = (void *)((long)vram & ~3); \
fa56d360 313 for (i = psx_src_height; i > 0; i--, vram += psx_step * 1024, dst += dst_stride)\
314 blitfunc(dst, vram, len); \
02ee7e24 315}
316
02783d0b 317make_flip_func(raw_blit_soft, memcpy)
318make_flip_func(raw_blit_soft_368, blit320_368)
319make_flip_func(raw_blit_soft_512, blit320_512)
320make_flip_func(raw_blit_soft_640, blit320_640)
02ee7e24 321
6469a8c4 322void *plat_gvideo_set_mode(int *w_, int *h_, int *bpp_)
55b0eeea 323{
02ee7e24 324 int poff_w, poff_h, w_max;
6469a8c4 325 int w = *w_, h = *h_, bpp = *bpp_;
55b0eeea 326
a72ac803 327 if (!w || !h || !bpp)
02ee7e24 328 return NULL;
55b0eeea 329
02ee7e24 330 printf("psx mode: %dx%d@%d\n", w, h, bpp);
a72ac803 331 psx_width = w;
332 psx_height = h;
333 psx_bpp = bpp;
55b0eeea 334
a72ac803 335 switch (w + (bpp != 16) + !soft_scaling) {
02ee7e24 336 case 640:
02783d0b 337 pl_plat_blit = raw_blit_soft_640;
02ee7e24 338 w_max = 640;
339 break;
340 case 512:
02783d0b 341 pl_plat_blit = raw_blit_soft_512;
02ee7e24 342 w_max = 512;
343 break;
344 case 384:
345 case 368:
02783d0b 346 pl_plat_blit = raw_blit_soft_368;
02ee7e24 347 w_max = 368;
348 break;
349 default:
02783d0b 350 pl_plat_blit = have_warm ? raw_blit_dma : raw_blit_soft;
02ee7e24 351 w_max = 320;
352 break;
55b0eeea 353 }
354
02ee7e24 355 psx_step = 1;
356 if (h > 256) {
357 psx_step = 2;
358 h /= 2;
359 }
360
361 poff_w = poff_h = 0;
362 if (w > w_max) {
363 poff_w = w / 2 - w_max / 2;
364 w = w_max;
365 }
366 fb_offset_x = 0;
367 if (w < 320)
368 fb_offset_x = 320/2 - w / 2;
369 if (h > 240) {
370 poff_h = h / 2 - 240/2;
371 h = 240;
372 }
373 fb_offset_y = 240/2 - h / 2;
374
6983a9ae 375 psx_offset_x = poff_w * psx_bpp/8 / 2;
02ee7e24 376 psx_offset_y = poff_h;
a72ac803 377 psx_src_width = w;
378 psx_src_height = h;
02ee7e24 379
380 if (fb_offset_x || fb_offset_y) {
381 // not fullscreen, must clear borders
382 memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
383 g_menuscreen_ptr = fb_flip();
384 memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
385 }
386
387 pollux_changemode(bpp, 1);
388
221be40d 389 pl_set_gun_rect(fb_offset_x, fb_offset_y, w > 320 ? 320 : w, h);
390
6469a8c4 391 // adjust for hud
392 *w_ = 320;
393 *h_ = fb_offset_y + psx_src_height;
394
02783d0b 395 return g_menuscreen_ptr;
55b0eeea 396}
397
6469a8c4 398/* not really used, we do raw_flip */
ab423939 399void plat_gvideo_open(int is_pal)
6469a8c4 400{
401}
402
403void *plat_gvideo_flip(void)
41f55c9f 404{
02783d0b 405 g_menuscreen_ptr = fb_flip();
406 return g_menuscreen_ptr;
41f55c9f 407}
408
6469a8c4 409void plat_gvideo_close(void)
410{
411}
412
c02c3b65 413static void *pl_emu_mmap(unsigned long addr, size_t size, int is_fixed,
414 enum psxMapTag tag)
415{
416 unsigned int pbase;
417 void *retval;
418 int ret;
419
420 if (!have_warm)
421 goto basic_map;
422
423 switch (tag) {
424 case MAP_TAG_RAM:
425 if (size > 0x400000) {
426 fprintf(stderr, "unexpected ram map request: %08lx %x\n",
427 addr, size);
428 exit(1);
429 }
430 pbase = (uppermem_pbase + 0xffffff) & ~0xffffff;
431 pbase += 0x400000;
432 retval = (void *)addr;
433 ret = warm_mmap_section(retval, pbase, size, WCB_C_BIT);
434 if (ret != 0) {
435 fprintf(stderr, "ram section map failed\n");
436 exit(1);
437 }
438 goto out;
439 case MAP_TAG_VRAM:
440 if (size > 0x400000) {
441 fprintf(stderr, "unexpected vram map request: %08lx %x\n",
442 addr, size);
443 exit(1);
444 }
445 if (addr == 0)
446 addr = 0x60000000;
447 vram_pbase = (uppermem_pbase + 0xffffff) & ~0xffffff;
448 retval = (void *)addr;
449
450 ret = warm_mmap_section(retval, vram_pbase, size, WCB_C_BIT);
451 if (ret != 0) {
452 fprintf(stderr, "vram section map failed\n");
453 exit(1);
454 }
455 goto out;
456 case MAP_TAG_LUTS:
457 // mostly for Wiz to not run out of RAM
458 if (size > 0x800000) {
459 fprintf(stderr, "unexpected LUT map request: %08lx %x\n",
460 addr, size);
461 exit(1);
462 }
463 pbase = (uppermem_pbase + 0xffffff) & ~0xffffff;
464 pbase += 0x800000;
465 retval = (void *)addr;
466 ret = warm_mmap_section(retval, pbase, size, WCB_C_BIT);
467 if (ret != 0) {
468 fprintf(stderr, "LUT section map failed\n");
469 exit(1);
470 }
471 goto out;
472 default:
473 break;
474 }
475
476basic_map:
477 retval = plat_mmap(addr, size, 0, is_fixed);
478
479out:
480 if (tag == MAP_TAG_VRAM)
481 psx_vram = retval;
482 return retval;
483}
484
485static void pl_emu_munmap(void *ptr, size_t size, enum psxMapTag tag)
486{
487 switch (tag) {
488 case MAP_TAG_RAM:
489 case MAP_TAG_VRAM:
490 case MAP_TAG_LUTS:
491 warm_munmap_section(ptr, size);
492 break;
493 default:
494 plat_munmap(ptr, size);
495 break;
496 }
497}
498
55b0eeea 499void plat_init(void)
500{
501 const char *main_fb_name = "/dev/fb0";
502 struct fb_fix_screeninfo fbfix;
cc56203b 503 int ret;
55b0eeea 504
cc56203b 505 plat_target_init();
0b6c6da8 506
55b0eeea 507 fbdev = open(main_fb_name, O_RDWR);
508 if (fbdev == -1) {
509 fprintf(stderr, "%s: ", main_fb_name);
510 perror("open");
511 exit(1);
512 }
513
514 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
515 if (ret == -1) {
516 perror("ioctl(fbdev) failed");
517 exit(1);
518 }
c02c3b65 519 uppermem_pbase = fbfix.smem_start;
520
55b0eeea 521 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
522 fb_paddrs[0] = fbfix.smem_start;
523 fb_paddrs[1] = fb_paddrs[0] + 320*240*4; // leave space for 24bpp
524
c02c3b65 525 ret = warm_init();
526 have_warm = (ret == 0);
527
528 if (have_warm) {
529 // map fb as write-through cached section
530 fb_vaddrs[0] = (void *)0x7fe00000;
531 ret = warm_mmap_section(fb_vaddrs[0], fb_paddrs[0],
532 FB_VRAM_SIZE, WCB_C_BIT);
533 if (ret != 0) {
534 fprintf(stderr, "fb section map failed\n");
535 fb_vaddrs[0] = NULL;
536
537 // we could continue but it would just get messy
538 exit(1);
539 }
540 }
541 if (fb_vaddrs[0] == NULL) {
542 fb_vaddrs[0] = mmap(0, FB_VRAM_SIZE, PROT_READ|PROT_WRITE,
55b0eeea 543 MAP_SHARED, memdev, fb_paddrs[0]);
c02c3b65 544 if (fb_vaddrs[0] == MAP_FAILED) {
545 perror("mmap(fb_vaddrs) failed");
546 exit(1);
547 }
548
549 memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
550 warm_change_cb_range(WCB_C_BIT, 1, fb_vaddrs[0], FB_VRAM_SIZE);
55b0eeea 551 }
c02c3b65 552 printf(" mapped @%p\n", fb_vaddrs[0]);
553
55b0eeea 554 fb_vaddrs[1] = (char *)fb_vaddrs[0] + 320*240*4;
555
a72ac803 556 memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
cc56203b 557
55b0eeea 558 pollux_changemode(16, 0);
d659b045 559 g_menuscreen_w = g_menuscreen_pp = 320;
55b0eeea 560 g_menuscreen_h = 240;
561 g_menuscreen_ptr = fb_flip();
562
55b0eeea 563 /* setup DMA */
564 DMA_REG(0x0c) = 0x20000; // pending IRQ clear
565
9b4bd105 566 in_tsbutton_init();
bcfc48e3 567 in_evdev_init(&gp2x_evdev_pdata);
9b4bd105 568 if (gp2x_dev_id == GP2X_DEV_CAANOO)
569 caanoo_init();
b07c18e8 570 else
571 wiz_init();
9b4bd105 572
02783d0b 573 pl_plat_blit = have_warm ? raw_blit_dma : raw_blit_soft;
55b0eeea 574
a72ac803 575 psx_src_width = 320;
576 psx_src_height = 240;
55b0eeea 577 psx_bpp = 16;
bb88ec28 578
579 pl_rearmed_cbs.screen_w = 320;
580 pl_rearmed_cbs.screen_h = 240;
cc56203b 581
582 plat_target_setup_input();
583
584 plat_target.cpu_clock_set = cpu_clock_wrapper;
c02c3b65 585
586 psxMapHook = pl_emu_mmap;
587 psxUnmapHook = pl_emu_munmap;
55b0eeea 588}
589
590void plat_finish(void)
591{
592 warm_finish();
0b6c6da8 593 memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
0b6c6da8 594 munmap(fb_vaddrs[0], FB_VRAM_SIZE);
595 close(fbdev);
cc56203b 596 plat_target_finish();
55b0eeea 597}
598
55b0eeea 599/* Caanoo stuff, perhaps move later */
55b0eeea 600static const char * const caanoo_keys[KEY_MAX + 1] = {
601 [0 ... KEY_MAX] = NULL,
602 [KEY_UP] = "Up",
603 [KEY_LEFT] = "Left",
604 [KEY_RIGHT] = "Right",
605 [KEY_DOWN] = "Down",
606 [BTN_TRIGGER] = "A",
607 [BTN_THUMB] = "X",
608 [BTN_THUMB2] = "B",
609 [BTN_TOP] = "Y",
610 [BTN_TOP2] = "L",
611 [BTN_PINKIE] = "R",
612 [BTN_BASE] = "Home",
613 [BTN_BASE2] = "Lock",
614 [BTN_BASE3] = "I",
615 [BTN_BASE4] = "II",
616 [BTN_BASE5] = "Push",
617};
618
b944a30e 619struct haptic_data {
620 int count;
621 struct {
622 short time, strength;
623 } actions[120];
624};
625
626#define HAPTIC_IOCTL_MAGIC 'I'
627#define HAPTIC_PLAY_PATTERN _IOW(HAPTIC_IOCTL_MAGIC, 4, struct haptic_data)
628#define HAPTIC_INDIVIDUAL_MODE _IOW(HAPTIC_IOCTL_MAGIC, 5, unsigned int)
629#define HAPTIC_SET_VIB_LEVEL _IOW(HAPTIC_IOCTL_MAGIC, 9, unsigned int)
630
631static int hapticdev = -1;
3a40ff14 632static struct haptic_data haptic_seq[2];
b944a30e 633
3a40ff14 634static int haptic_read(const char *fname, struct haptic_data *data)
b944a30e 635{
636 int i, ret, v1, v2;
637 char buf[128], *p;
638 FILE *f;
639
3a40ff14 640 f = fopen(fname, "r");
b944a30e 641 if (f == NULL) {
6469a8c4 642 fprintf(stderr, "fopen(%s)", fname);
3a40ff14 643 perror("");
b944a30e 644 return -1;
645 }
646
3a40ff14 647 for (i = 0; i < sizeof(data->actions) / sizeof(data->actions[0]); ) {
b944a30e 648 p = fgets(buf, sizeof(buf), f);
649 if (p == NULL)
650 break;
651 while (*p != 0 && *p == ' ')
652 p++;
653 if (*p == 0 || *p == ';' || *p == '#')
654 continue;
655
656 ret = sscanf(buf, "%d %d", &v1, &v2);
657 if (ret != 2) {
658 fprintf(stderr, "can't parse: %s", buf);
659 continue;
660 }
661
3a40ff14 662 data->actions[i].time = v1;
663 data->actions[i].strength = v2;
b944a30e 664 i++;
665 }
666 fclose(f);
667
668 if (i == 0) {
3a40ff14 669 fprintf(stderr, "bad haptic file: %s\n", fname);
b944a30e 670 return -1;
671 }
3a40ff14 672 data->count = i;
673
674 return 0;
675}
676
677static int haptic_init(void)
678{
679 int ret, i;
680
681 ret = haptic_read("haptic_w.cfg", &haptic_seq[0]);
682 if (ret != 0)
683 return -1;
684 ret = haptic_read("haptic_s.cfg", &haptic_seq[1]);
685 if (ret != 0)
686 return -1;
b944a30e 687
688 hapticdev = open("/dev/isa1200", O_RDWR | O_NONBLOCK);
689 if (hapticdev == -1) {
690 perror("open(/dev/isa1200)");
691 return -1;
692 }
693
694 i = 0;
695 ret = ioctl(hapticdev, HAPTIC_INDIVIDUAL_MODE, &i); /* use 2 of them */
696 i = 3;
697 ret |= ioctl(hapticdev, HAPTIC_SET_VIB_LEVEL, &i); /* max */
698 if (ret != 0) {
699 fprintf(stderr, "haptic ioctls failed\n");
700 close(hapticdev);
701 hapticdev = -1;
702 return -1;
703 }
704
705 return 0;
706}
707
d71c7095 708void plat_trigger_vibrate(int pad, int low, int high)
b944a30e 709{
d71c7095 710 int is_strong;
b944a30e 711 int ret;
712
d71c7095 713 if (low == 0 && high == 0)
714 return;
715 is_strong = (high >= 0xf0);
716
b944a30e 717 if (hapticdev == -2)
718 return; // it's broken
719 if (hapticdev < 0) {
720 ret = haptic_init();
721 if (ret < 0) {
722 hapticdev = -2;
723 return;
724 }
725 }
726
3a40ff14 727 ioctl(hapticdev, HAPTIC_PLAY_PATTERN, &haptic_seq[!!is_strong]);
b944a30e 728}
729
b07c18e8 730static void caanoo_init(void)
731{
732 in_probe();
733 in_set_config(in_name_to_id("evdev:pollux-analog"), IN_CFG_KEY_NAMES,
734 caanoo_keys, sizeof(caanoo_keys));
735}
736
221be40d 737/* Wiz stuff */
b07c18e8 738static const struct in_default_bind in_gp2x_defbinds[] =
221be40d 739{
740 /* MXYZ SACB RLDU */
741 { GP2X_BTN_UP, IN_BINDTYPE_PLAYER12, DKEY_UP },
742 { GP2X_BTN_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN },
743 { GP2X_BTN_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT },
744 { GP2X_BTN_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
745 { GP2X_BTN_X, IN_BINDTYPE_PLAYER12, DKEY_CROSS },
746 { GP2X_BTN_B, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
747 { GP2X_BTN_A, IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
748 { GP2X_BTN_Y, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
749 { GP2X_BTN_L, IN_BINDTYPE_PLAYER12, DKEY_L1 },
750 { GP2X_BTN_R, IN_BINDTYPE_PLAYER12, DKEY_R1 },
751 { GP2X_BTN_START, IN_BINDTYPE_PLAYER12, DKEY_START },
752 { GP2X_BTN_SELECT, IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
753 { GP2X_BTN_VOL_UP, IN_BINDTYPE_EMU, SACTION_VOLUME_UP },
754 { GP2X_BTN_VOL_DOWN, IN_BINDTYPE_EMU, SACTION_VOLUME_DOWN },
755 { 0, 0, 0 },
756};
757
221be40d 758// unused dummy for in_gp2x
759volatile unsigned short *gp2x_memregs;
760
b07c18e8 761static void wiz_init(void)
55b0eeea 762{
b07c18e8 763 in_gp2x_init(in_gp2x_defbinds);
764 in_probe();
55b0eeea 765}