8 #include "definitions.h"
9 #include "wiiuse_internal.h"
12 #include "lwp_wkspace.h"
14 #define MAX_COMMANDS 0x100
16 static vu32
* const _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_SPEAKER
|WIIMOTE_STATE_SPEAKER_INIT
));
42 WIIMOTE_DISABLE_STATE(wm
, (WIIMOTE_STATE_EXP
|WIIMOTE_STATE_EXP_HANDSHAKE
|WIIMOTE_STATE_EXP_FAILED
));
43 WIIMOTE_DISABLE_STATE(wm
,(WIIMOTE_STATE_CONNECTED
|WIIMOTE_STATE_HANDSHAKE
|WIIMOTE_STATE_HANDSHAKE_COMPLETE
));
46 __lwp_queue_append(&wm
->cmdq
,&wm
->cmd_head
->node
);
47 wm
->cmd_head
= wm
->cmd_head
->next
;
51 if(wm
->event_cb
) wm
->event_cb(wm
,WIIUSE_DISCONNECT
);
57 static s32
__wiiuse_receive(void *arg
,void *buffer
,u16 len
)
59 struct wiimote_listen_t
*wml
= (struct wiimote_listen_t
*)arg
;
60 struct wiimote_t
*wm
= wml
->wm
;
62 if(!wm
|| !buffer
|| len
==0) return ERR_OK
;
64 //printf("__wiiuse_receive[%02x]\n",*(char*)buffer);
65 wm
->event
= WIIUSE_NONE
;
67 memcpy(wm
->event_buf
,buffer
,len
);
68 memset(&(wm
->event_buf
[len
]),0,(MAX_PAYLOAD
- len
));
71 if(wm
->event
!=WIIUSE_NONE
) {
72 if(wm
->event_cb
) wm
->event_cb(wm
,wm
->event
);
78 static s32
__wiiuse_connected(void *arg
,struct bte_pcb
*pcb
,u8 err
)
80 struct wiimote_listen_t
*wml
= (struct wiimote_listen_t
*)arg
;
83 wm
= wml
->assign_cb(&wml
->bdaddr
);
86 bte_disconnect(wml
->sock
);
93 wm
->bdaddr
= wml
->bdaddr
;
95 //printf("__wiiuse_connected()\n");
96 WIIMOTE_ENABLE_STATE(wm
,(WIIMOTE_STATE_CONNECTED
|WIIMOTE_STATE_HANDSHAKE
));
98 wm
->handshake_state
= 0;
99 wiiuse_handshake(wm
,NULL
,0);
104 static s32
__wiiuse_sent(void *arg
,struct bte_pcb
*pcb
,u8 err
)
106 struct cmd_blk_t
*cmd
= NULL
;
107 struct wiimote_listen_t
*wml
= (struct wiimote_listen_t
*)arg
;
108 struct wiimote_t
*wm
= wml
->wm
;
110 if(!wm
) return ERR_OK
;
114 if(!cmd
) return ERR_OK
;
115 if(cmd
->state
!=CMD_SENT
) return ERR_OK
;
117 switch(cmd
->data
[0]) {
118 case WM_CMD_CTRL_STATUS
:
119 case WM_CMD_WRITE_DATA
:
120 case WM_CMD_READ_DATA
:
123 wm
->cmd_head
= cmd
->next
;
125 cmd
->state
= CMD_DONE
;
126 if(cmd
->cb
) cmd
->cb(wm
,NULL
,0);
128 __lwp_queue_append(&wm
->cmdq
,&cmd
->node
);
129 wiiuse_send_next_command(wm
);
135 void __wiiuse_sensorbar_enable(int enable
)
140 level
= IRQ_Disable();
141 val
= (ACR_ReadReg(0xc0)&~0x100);
142 if(enable
) val
|= 0x100;
143 ACR_WriteReg(0xc0,val
);
147 int wiiuse_register(struct wiimote_listen_t
*wml
, struct bd_addr
*bdaddr
, struct wiimote_t
*(*assign_cb
)(struct bd_addr
*bdaddr
))
151 if(!wml
|| !bdaddr
|| !assign_cb
) return 0;
154 wml
->bdaddr
= *bdaddr
;
155 wml
->sock
= bte_new();
156 wml
->assign_cb
= assign_cb
;
157 if(wml
->sock
==NULL
) return 0;
159 bte_arg(wml
->sock
,wml
);
160 bte_received(wml
->sock
,__wiiuse_receive
);
161 bte_disconnected(wml
->sock
,__wiiuse_disconnected
);
163 err
= bte_registerdeviceasync(wml
->sock
,bdaddr
,__wiiuse_connected
);
164 if(err
==ERR_OK
) return 1;
169 void wiiuse_disconnect(struct wiimote_t
*wm
)
171 if(wm
==NULL
|| wm
->sock
==NULL
) return;
173 WIIMOTE_DISABLE_STATE(wm
,WIIMOTE_STATE_CONNECTED
);
174 bte_disconnect(wm
->sock
);
177 void wiiuse_sensorbar_enable(int enable
)
179 __wiiuse_sensorbar_enable(enable
);
182 void wiiuse_init_cmd_queue(struct wiimote_t
*wm
)
187 size
= (MAX_COMMANDS
*sizeof(struct cmd_blk_t
));
188 buffer
= __lwp_wkspace_allocate(size
);
189 __lwp_queue_initialize(&wm
->cmdq
,buffer
,MAX_COMMANDS
,sizeof(struct cmd_blk_t
));
192 int wiiuse_io_write(struct wiimote_t
*wm
,ubyte
*buf
,int len
)
195 return bte_sendmessageasync(wm
->sock
,buf
,len
,__wiiuse_sent
);