8 #include "definitions.h"
9 #include "wiiuse_internal.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
));
45 __lwp_queue_append(&wm
->cmdq
,&wm
->cmd_head
->node
);
46 wm
->cmd_head
= wm
->cmd_head
->next
;
50 if(wm
->event_cb
) wm
->event_cb(wm
,WIIUSE_DISCONNECT
);
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
));
70 if(wm
->event
!=WIIUSE_NONE
) {
71 if(wm
->event_cb
) wm
->event_cb(wm
,wm
->event
);
76 static s32
__wiiuse_connected(void *arg
,struct bte_pcb
*pcb
,u8 err
)
78 struct wiimote_listen_t
*wml
= (struct wiimote_listen_t
*)arg
;
81 wm
= wml
->assign_cb(&wml
->bdaddr
);
84 bte_disconnect(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);
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
;
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
:
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
);
136 void __wiiuse_sensorbar_enable(int enable
)
141 level
= IRQ_Disable();
142 val
= (ACR_ReadReg(0xc0)&~0x100);
143 if(enable
) val
|= 0x100;
144 ACR_WriteReg(0xc0,val
);
148 int wiiuse_register(struct wiimote_listen_t
*wml
, struct bd_addr
*bdaddr
, struct wiimote_t
*(*assign_cb
)(struct bd_addr
*bdaddr
))
152 if(!wml
|| !bdaddr
|| !assign_cb
) return 0;
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;
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
)
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
)
196 return bte_sendmessageasync(wm
->sock
,buf
,len
,__wiiuse_sent
);