load a keymap in KEYBOARD_Init() based on the contents of "/wiikbd.map"
[libogc.git] / wiiuse / io_wii.c
blob67c94d290dee6cb3ba435ad95a048c036e6dc2bf
1 #ifdef GEKKO
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
8 #include "definitions.h"
9 #include "wiiuse_internal.h"
10 #include "events.h"
11 #include "io.h"
12 #include "lwp_wkspace.h"
14 #define MAX_COMMANDS 0x20
16 static vu32 *_ipcReg = (u32*)0xCD000000;
18 extern void parse_event(struct wiimote_t *wm);
19 extern void idle_cycle(struct wiimote_t* wm);
20 extern void hexdump(void *d, int len);
22 static __inline__ u32 ACR_ReadReg(u32 reg)
24 return _ipcReg[reg>>2];
27 static __inline__ void ACR_WriteReg(u32 reg,u32 val)
29 _ipcReg[reg>>2] = val;
32 static s32 __wiiuse_disconnected(void *arg,struct bte_pcb *pcb,u8 err)
34 struct wiimote_listen_t *wml = (struct wiimote_listen_t*)arg;
35 struct wiimote_t *wm = wml->wm;
37 if(!wm) return ERR_OK;
39 //printf("wiimote disconnected\n");
40 WIIMOTE_DISABLE_STATE(wm, (WIIMOTE_STATE_IR|WIIMOTE_STATE_IR_INIT));
41 WIIMOTE_DISABLE_STATE(wm, (WIIMOTE_STATE_EXP|WIIMOTE_STATE_EXP_HANDSHAKE|WIIMOTE_STATE_EXP_FAILED));
42 WIIMOTE_DISABLE_STATE(wm,(WIIMOTE_STATE_CONNECTED|WIIMOTE_STATE_HANDSHAKE|WIIMOTE_STATE_HANDSHAKE_COMPLETE));
44 while(wm->cmd_head) {
45 __lwp_queue_append(&wm->cmdq,&wm->cmd_head->node);
46 wm->cmd_head = wm->cmd_head->next;
48 wm->cmd_tail = NULL;
50 if(wm->event_cb) wm->event_cb(wm,WIIUSE_DISCONNECT);
52 wml->wm = NULL;
53 return ERR_OK;
56 static s32 __wiiuse_receive(void *arg,void *buffer,u16 len)
58 struct wiimote_listen_t *wml = (struct wiimote_listen_t*)arg;
59 struct wiimote_t *wm = wml->wm;
61 if(!wm || !buffer || len==0) return ERR_OK;
63 //printf("__wiiuse_receive[%02x]\n",*(char*)buffer);
64 wm->event = WIIUSE_NONE;
66 memcpy(wm->event_buf,buffer,len);
67 memset(&(wm->event_buf[len]),0,(MAX_PAYLOAD - len));
68 parse_event(wm);
70 if(wm->event!=WIIUSE_NONE) {
71 if(wm->event_cb) wm->event_cb(wm,wm->event);
73 return ERR_OK;
76 static s32 __wiiuse_connected(void *arg,struct bte_pcb *pcb,u8 err)
78 struct wiimote_listen_t *wml = (struct wiimote_listen_t*)arg;
79 struct wiimote_t *wm;
81 wm = wml->assign_cb(&wml->bdaddr);
83 if(!wm) {
84 bte_disconnect(wml->sock);
85 return ERR_OK;
88 wml->wm = wm;
90 wm->sock = wml->sock;
91 wm->bdaddr = wml->bdaddr;
93 //printf("__wiiuse_connected()\n");
94 WIIMOTE_ENABLE_STATE(wm,(WIIMOTE_STATE_CONNECTED|WIIMOTE_STATE_HANDSHAKE));
96 wm->handshake_state = 0;
97 wiiuse_handshake(wm,NULL,0);
99 return ERR_OK;
102 static s32 __wiiuse_sent(void *arg,struct bte_pcb *pcb,u8 err)
104 struct cmd_blk_t *cmd = NULL;
105 struct wiimote_listen_t *wml = (struct wiimote_listen_t*)arg;
106 struct wiimote_t *wm = wml->wm;
108 if(!wm) return ERR_OK;
110 cmd = wm->cmd_head;
112 if(!cmd) return ERR_OK;
113 if(cmd->state!=CMD_SENT) return ERR_OK;
115 //printf("__wiiuse_sent(%p,%02x)\n",cmd,cmd->data[0]);
117 switch(cmd->data[0]) {
118 case WM_CMD_CTRL_STATUS:
119 case WM_CMD_WRITE_DATA:
120 case WM_CMD_READ_DATA:
121 case WM_CMD_STREAM_DATA:
122 return ERR_OK;
123 default:
124 wm->cmd_head = cmd->next;
126 cmd->state = CMD_DONE;
127 if(cmd->cb) cmd->cb(wm,NULL,0);
129 __lwp_queue_append(&wm->cmdq,&cmd->node);
130 wiiuse_send_next_command(wm);
131 break;
133 return ERR_OK;
136 void __wiiuse_sensorbar_enable(int enable)
138 u32 val;
139 u32 level;
141 level = IRQ_Disable();
142 val = (ACR_ReadReg(0xc0)&~0x100);
143 if(enable) val |= 0x100;
144 ACR_WriteReg(0xc0,val);
145 IRQ_Restore(level);
148 int wiiuse_register(struct wiimote_listen_t *wml, struct bd_addr *bdaddr, struct wiimote_t *(*assign_cb)(struct bd_addr *bdaddr))
150 s32 err;
152 if(!wml || !bdaddr || !assign_cb) return 0;
154 wml->wm = NULL;
155 wml->bdaddr = *bdaddr;
156 wml->sock = bte_new();
157 wml->assign_cb = assign_cb;
158 if(wml->sock==NULL) return 0;
160 bte_arg(wml->sock,wml);
161 bte_received(wml->sock,__wiiuse_receive);
162 bte_disconnected(wml->sock,__wiiuse_disconnected);
164 err = bte_registerdeviceasync(wml->sock,bdaddr,__wiiuse_connected);
165 if(err==ERR_OK) return 1;
167 return 0;
170 void wiiuse_disconnect(struct wiimote_t *wm)
172 if(wm==NULL || wm->sock==NULL) return;
174 WIIMOTE_DISABLE_STATE(wm,WIIMOTE_STATE_CONNECTED);
175 bte_disconnect(wm->sock);
178 void wiiuse_sensorbar_enable(int enable)
180 __wiiuse_sensorbar_enable(enable);
183 void wiiuse_init_cmd_queue(struct wiimote_t *wm)
185 u8 *buffer;
186 u32 size;
188 size = (MAX_COMMANDS*sizeof(struct cmd_blk_t));
189 buffer = __lwp_wkspace_allocate(size);
190 __lwp_queue_initialize(&wm->cmdq,buffer,MAX_COMMANDS,sizeof(struct cmd_blk_t));
193 int wiiuse_io_write(struct wiimote_t *wm,ubyte *buf,int len)
195 if(wm->sock)
196 return bte_sendmessageasync(wm->sock,buf,len,__wiiuse_sent);
198 return ERR_CONN;
201 #endif