catch up with upsteam
[libogc.git] / wiiuse / io.c
blob1240fccde59e080eff3ad4ba0f301327322ac8dc
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "definitions.h"
5 #include "wiiuse_internal.h"
6 #include "nunchuk.h"
7 #include "classic.h"
8 #include "guitar_hero_3.h"
9 #include "wiiboard.h"
10 #include "motion_plus.h"
11 #include "io.h"
12 #include "lwp_wkspace.h"
14 void wiiuse_handshake(struct wiimote_t *wm,ubyte *data,uword len)
16 ubyte *buf = NULL;
17 struct accel_t *accel = &wm->accel_calib;
19 //printf("wiiuse_handshake(%d,%p,%d)\n",wm->handshake_state,data,len);
21 switch(wm->handshake_state) {
22 case 0:
23 wm->handshake_state++;
25 wiiuse_set_leds(wm,WIIMOTE_LED_NONE,NULL);
27 buf = __lwp_wkspace_allocate(sizeof(ubyte)*8);
28 wiiuse_read_data(wm,buf,WM_MEM_OFFSET_CALIBRATION,7,wiiuse_handshake);
29 break;
30 case 1:
31 wm->handshake_state++;
33 accel->cal_zero.x = ((data[0]<<2)|((data[3]>>4)&3));
34 accel->cal_zero.y = ((data[1]<<2)|((data[3]>>2)&3));
35 accel->cal_zero.z = ((data[2]<<2)|(data[3]&3));
37 accel->cal_g.x = (((data[4]<<2)|((data[7]>>4)&3)) - accel->cal_zero.x);
38 accel->cal_g.y = (((data[5]<<2)|((data[7]>>2)&3)) - accel->cal_zero.y);
39 accel->cal_g.z = (((data[6]<<2)|(data[7]&3)) - accel->cal_zero.z);
40 __lwp_wkspace_free(data);
42 WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
43 WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE);
45 wm->event = WIIUSE_CONNECT;
46 wiiuse_status(wm,NULL);
47 break;
48 default:
49 break;
54 void wiiuse_handshake_expansion_start(struct wiimote_t *wm)
56 if(WIIMOTE_IS_SET(wm,WIIMOTE_STATE_EXP) || WIIMOTE_IS_SET(wm,WIIMOTE_STATE_EXP_FAILED) || WIIMOTE_IS_SET(wm,WIIMOTE_STATE_EXP_HANDSHAKE))
57 return;
59 wm->expansion_state = 0;
60 WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_EXP_HANDSHAKE);
61 wiiuse_handshake_expansion(wm, NULL, 0);
64 void wiiuse_handshake_expansion(struct wiimote_t *wm,ubyte *data,uword len)
66 int id;
67 ubyte val;
68 ubyte *buf = NULL;
70 switch(wm->expansion_state) {
71 /* These two initialization writes disable the encryption */
72 case 0:
73 wm->expansion_state = 1;
74 val = 0x55;
75 wiiuse_write_data(wm,WM_EXP_MEM_ENABLE1,&val,1,wiiuse_handshake_expansion);
76 break;
77 case 1:
78 wm->expansion_state = 2;
79 val = 0x00;
80 wiiuse_write_data(wm,WM_EXP_MEM_ENABLE2,&val,1,wiiuse_handshake_expansion);
81 break;
82 case 2:
83 wm->expansion_state = 3;
84 buf = __lwp_wkspace_allocate(sizeof(ubyte)*EXP_HANDSHAKE_LEN);
85 wiiuse_read_data(wm,buf,WM_EXP_MEM_CALIBR,EXP_HANDSHAKE_LEN,wiiuse_handshake_expansion);
86 break;
87 case 3:
88 if(!data || !len) return;
89 id = BIG_ENDIAN_LONG(*(int*)(&data[220]));
91 switch(id) {
92 case EXP_ID_CODE_NUNCHUK:
93 if(!nunchuk_handshake(wm,&wm->exp.nunchuk,data,len)) return;
94 break;
95 case EXP_ID_CODE_CLASSIC_CONTROLLER:
96 case EXP_ID_CODE_CLASSIC_CONTROLLER_NYKOWING:
97 case EXP_ID_CODE_CLASSIC_CONTROLLER_NYKOWING2:
98 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC:
99 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC2:
100 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC3:
101 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC4:
102 case EXP_ID_CODE_CLASSIC_CONTROLLER_GENERIC5:
103 if(!classic_ctrl_handshake(wm,&wm->exp.classic,data,len)) return;
104 break;
105 case EXP_ID_CODE_GUITAR:
106 if(!guitar_hero_3_handshake(wm,&wm->exp.gh3,data,len)) return;
107 break;
108 case EXP_ID_CODE_WIIBOARD:
109 if(!wii_board_handshake(wm,&wm->exp.wb,data,len)) return;
110 break;
111 default:
112 WIIMOTE_DISABLE_STATE(wm,WIIMOTE_STATE_EXP_HANDSHAKE);
113 WIIMOTE_ENABLE_STATE(wm,WIIMOTE_STATE_EXP_FAILED);
114 __lwp_wkspace_free(data);
115 wiiuse_status(wm,NULL);
116 return;
118 __lwp_wkspace_free(data);
120 WIIMOTE_DISABLE_STATE(wm,WIIMOTE_STATE_EXP_HANDSHAKE);
121 WIIMOTE_ENABLE_STATE(wm,WIIMOTE_STATE_EXP);
122 wiiuse_set_ir_mode(wm);
123 wiiuse_status(wm,NULL);
124 break;
128 void wiiuse_disable_expansion(struct wiimote_t *wm)
130 if(!WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP)) return;
132 /* tell the associated module the expansion was removed */
133 switch(wm->exp.type) {
134 case EXP_NUNCHUK:
135 nunchuk_disconnected(&wm->exp.nunchuk);
136 wm->event = WIIUSE_NUNCHUK_REMOVED;
137 break;
138 case EXP_CLASSIC:
139 classic_ctrl_disconnected(&wm->exp.classic);
140 wm->event = WIIUSE_CLASSIC_CTRL_REMOVED;
141 break;
142 case EXP_GUITAR_HERO_3:
143 guitar_hero_3_disconnected(&wm->exp.gh3);
144 wm->event = WIIUSE_GUITAR_HERO_3_CTRL_REMOVED;
145 break;
146 case EXP_WII_BOARD:
147 wii_board_disconnected(&wm->exp.wb);
148 wm->event = WIIUSE_WII_BOARD_REMOVED;
149 break;
150 case EXP_MOTION_PLUS:
151 motion_plus_disconnected(&wm->exp.mp);
152 wm->event = WIIUSE_MOTION_PLUS_REMOVED;
153 break;
155 default:
156 break;
159 WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_EXP);
160 wm->exp.type = EXP_NONE;
162 wiiuse_set_ir_mode(wm);
163 wiiuse_status(wm,NULL);