Commit | Line | Data |
---|---|---|
fb51b443 DH |
1 | /* |
2 | * HID driver for Nintendo Wiimote devices | |
3 | * Copyright (c) 2011 David Herrmann | |
4 | */ | |
5 | ||
6 | /* | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the Free | |
9 | * Software Foundation; either version 2 of the License, or (at your option) | |
10 | * any later version. | |
11 | */ | |
12 | ||
29d28064 | 13 | #include <linux/completion.h> |
672bc4e0 | 14 | #include <linux/device.h> |
02fb72a0 | 15 | #include <linux/hid.h> |
672bc4e0 | 16 | #include <linux/input.h> |
23a5a4a3 | 17 | #include <linux/leds.h> |
fb51b443 | 18 | #include <linux/module.h> |
29d28064 | 19 | #include <linux/mutex.h> |
23c063cb | 20 | #include <linux/spinlock.h> |
02fb72a0 | 21 | #include "hid-ids.h" |
fb51b443 DH |
22 | |
23 | #define WIIMOTE_VERSION "0.1" | |
24 | #define WIIMOTE_NAME "Nintendo Wii Remote" | |
23c063cb DH |
25 | #define WIIMOTE_BUFSIZE 32 |
26 | ||
27 | struct wiimote_buf { | |
28 | __u8 data[HID_MAX_BUFFER_SIZE]; | |
29 | size_t size; | |
30 | }; | |
fb51b443 | 31 | |
32a0d9a5 DH |
32 | struct wiimote_state { |
33 | spinlock_t lock; | |
34 | __u8 flags; | |
efcf9188 | 35 | __u8 accel_split[2]; |
29d28064 DH |
36 | |
37 | /* synchronous cmd requests */ | |
38 | struct mutex sync; | |
39 | struct completion ready; | |
40 | int cmd; | |
41 | __u32 opt; | |
33e84013 DH |
42 | |
43 | /* results of synchronous requests */ | |
e3979a91 | 44 | __u8 cmd_battery; |
33e84013 | 45 | __u8 cmd_err; |
32a0d9a5 DH |
46 | }; |
47 | ||
e894d0e3 DH |
48 | struct wiimote_data { |
49 | struct hid_device *hdev; | |
672bc4e0 | 50 | struct input_dev *input; |
23a5a4a3 | 51 | struct led_classdev *leds[4]; |
98a558ae | 52 | struct input_dev *accel; |
f363e4f6 | 53 | struct input_dev *ir; |
23c063cb DH |
54 | |
55 | spinlock_t qlock; | |
56 | __u8 head; | |
57 | __u8 tail; | |
58 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; | |
59 | struct work_struct worker; | |
32a0d9a5 DH |
60 | |
61 | struct wiimote_state state; | |
e894d0e3 DH |
62 | }; |
63 | ||
c003ec21 DH |
64 | #define WIIPROTO_FLAG_LED1 0x01 |
65 | #define WIIPROTO_FLAG_LED2 0x02 | |
66 | #define WIIPROTO_FLAG_LED3 0x04 | |
67 | #define WIIPROTO_FLAG_LED4 0x08 | |
68 | #define WIIPROTO_FLAG_RUMBLE 0x10 | |
98a558ae | 69 | #define WIIPROTO_FLAG_ACCEL 0x20 |
f363e4f6 DH |
70 | #define WIIPROTO_FLAG_IR_BASIC 0x40 |
71 | #define WIIPROTO_FLAG_IR_EXT 0x80 | |
72 | #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */ | |
32a0d9a5 DH |
73 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
74 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) | |
f363e4f6 DH |
75 | #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \ |
76 | WIIPROTO_FLAG_IR_FULL) | |
db308346 | 77 | |
23a5a4a3 DH |
78 | /* return flag for led \num */ |
79 | #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) | |
80 | ||
1abb9ad3 | 81 | enum wiiproto_reqs { |
2cb5e4bc | 82 | WIIPROTO_REQ_NULL = 0x0, |
c003ec21 | 83 | WIIPROTO_REQ_RUMBLE = 0x10, |
db308346 | 84 | WIIPROTO_REQ_LED = 0x11, |
2cb5e4bc | 85 | WIIPROTO_REQ_DRM = 0x12, |
fc221cda | 86 | WIIPROTO_REQ_IR1 = 0x13, |
e3979a91 | 87 | WIIPROTO_REQ_SREQ = 0x15, |
be1ecd62 DH |
88 | WIIPROTO_REQ_WMEM = 0x16, |
89 | WIIPROTO_REQ_RMEM = 0x17, | |
fc221cda | 90 | WIIPROTO_REQ_IR2 = 0x1a, |
c87019e4 | 91 | WIIPROTO_REQ_STATUS = 0x20, |
be1ecd62 | 92 | WIIPROTO_REQ_DATA = 0x21, |
c87019e4 | 93 | WIIPROTO_REQ_RETURN = 0x22, |
1abb9ad3 | 94 | WIIPROTO_REQ_DRM_K = 0x30, |
efcf9188 | 95 | WIIPROTO_REQ_DRM_KA = 0x31, |
7336b9f9 | 96 | WIIPROTO_REQ_DRM_KE = 0x32, |
efcf9188 | 97 | WIIPROTO_REQ_DRM_KAI = 0x33, |
7336b9f9 | 98 | WIIPROTO_REQ_DRM_KEE = 0x34, |
efcf9188 | 99 | WIIPROTO_REQ_DRM_KAE = 0x35, |
f363e4f6 | 100 | WIIPROTO_REQ_DRM_KIE = 0x36, |
efcf9188 | 101 | WIIPROTO_REQ_DRM_KAIE = 0x37, |
7336b9f9 | 102 | WIIPROTO_REQ_DRM_E = 0x3d, |
efcf9188 DH |
103 | WIIPROTO_REQ_DRM_SKAI1 = 0x3e, |
104 | WIIPROTO_REQ_DRM_SKAI2 = 0x3f, | |
1abb9ad3 DH |
105 | }; |
106 | ||
107 | enum wiiproto_keys { | |
108 | WIIPROTO_KEY_LEFT, | |
109 | WIIPROTO_KEY_RIGHT, | |
110 | WIIPROTO_KEY_UP, | |
111 | WIIPROTO_KEY_DOWN, | |
112 | WIIPROTO_KEY_PLUS, | |
113 | WIIPROTO_KEY_MINUS, | |
114 | WIIPROTO_KEY_ONE, | |
115 | WIIPROTO_KEY_TWO, | |
116 | WIIPROTO_KEY_A, | |
117 | WIIPROTO_KEY_B, | |
118 | WIIPROTO_KEY_HOME, | |
119 | WIIPROTO_KEY_COUNT | |
120 | }; | |
121 | ||
122 | static __u16 wiiproto_keymap[] = { | |
123 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ | |
124 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ | |
125 | KEY_UP, /* WIIPROTO_KEY_UP */ | |
126 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ | |
127 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ | |
128 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ | |
129 | BTN_1, /* WIIPROTO_KEY_ONE */ | |
130 | BTN_2, /* WIIPROTO_KEY_TWO */ | |
131 | BTN_A, /* WIIPROTO_KEY_A */ | |
132 | BTN_B, /* WIIPROTO_KEY_B */ | |
133 | BTN_MODE, /* WIIPROTO_KEY_HOME */ | |
134 | }; | |
135 | ||
29d28064 DH |
136 | /* requires the state.lock spinlock to be held */ |
137 | static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd, | |
138 | __u32 opt) | |
139 | { | |
140 | return wdata->state.cmd == cmd && wdata->state.opt == opt; | |
141 | } | |
142 | ||
143 | /* requires the state.lock spinlock to be held */ | |
144 | static inline void wiimote_cmd_complete(struct wiimote_data *wdata) | |
145 | { | |
146 | wdata->state.cmd = WIIPROTO_REQ_NULL; | |
147 | complete(&wdata->state.ready); | |
148 | } | |
149 | ||
150 | static inline int wiimote_cmd_acquire(struct wiimote_data *wdata) | |
151 | { | |
152 | return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0; | |
153 | } | |
154 | ||
155 | /* requires the state.lock spinlock to be held */ | |
156 | static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd, | |
157 | __u32 opt) | |
158 | { | |
159 | INIT_COMPLETION(wdata->state.ready); | |
160 | wdata->state.cmd = cmd; | |
161 | wdata->state.opt = opt; | |
162 | } | |
163 | ||
164 | static inline void wiimote_cmd_release(struct wiimote_data *wdata) | |
165 | { | |
166 | mutex_unlock(&wdata->state.sync); | |
167 | } | |
168 | ||
169 | static inline int wiimote_cmd_wait(struct wiimote_data *wdata) | |
170 | { | |
171 | int ret; | |
172 | ||
173 | ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ); | |
174 | if (ret < 0) | |
175 | return -ERESTARTSYS; | |
176 | else if (ret == 0) | |
177 | return -EIO; | |
178 | else | |
179 | return 0; | |
180 | } | |
181 | ||
0c218f14 DH |
182 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
183 | size_t count) | |
184 | { | |
185 | __u8 *buf; | |
186 | ssize_t ret; | |
187 | ||
188 | if (!hdev->hid_output_raw_report) | |
189 | return -ENODEV; | |
190 | ||
191 | buf = kmemdup(buffer, count, GFP_KERNEL); | |
192 | if (!buf) | |
193 | return -ENOMEM; | |
194 | ||
195 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); | |
196 | ||
197 | kfree(buf); | |
198 | return ret; | |
199 | } | |
200 | ||
23c063cb DH |
201 | static void wiimote_worker(struct work_struct *work) |
202 | { | |
203 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, | |
204 | worker); | |
205 | unsigned long flags; | |
206 | ||
207 | spin_lock_irqsave(&wdata->qlock, flags); | |
208 | ||
209 | while (wdata->head != wdata->tail) { | |
210 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
211 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, | |
212 | wdata->outq[wdata->tail].size); | |
213 | spin_lock_irqsave(&wdata->qlock, flags); | |
214 | ||
215 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; | |
216 | } | |
217 | ||
218 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
219 | } | |
220 | ||
221 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, | |
222 | size_t count) | |
223 | { | |
224 | unsigned long flags; | |
225 | __u8 newhead; | |
226 | ||
227 | if (count > HID_MAX_BUFFER_SIZE) { | |
228 | hid_warn(wdata->hdev, "Sending too large output report\n"); | |
229 | return; | |
230 | } | |
231 | ||
232 | /* | |
233 | * Copy new request into our output queue and check whether the | |
234 | * queue is full. If it is full, discard this request. | |
235 | * If it is empty we need to start a new worker that will | |
236 | * send out the buffer to the hid device. | |
237 | * If the queue is not empty, then there must be a worker | |
238 | * that is currently sending out our buffer and this worker | |
239 | * will reschedule itself until the queue is empty. | |
240 | */ | |
241 | ||
242 | spin_lock_irqsave(&wdata->qlock, flags); | |
243 | ||
244 | memcpy(wdata->outq[wdata->head].data, buffer, count); | |
245 | wdata->outq[wdata->head].size = count; | |
246 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; | |
247 | ||
248 | if (wdata->head == wdata->tail) { | |
249 | wdata->head = newhead; | |
250 | schedule_work(&wdata->worker); | |
251 | } else if (newhead != wdata->tail) { | |
252 | wdata->head = newhead; | |
253 | } else { | |
254 | hid_warn(wdata->hdev, "Output queue is full"); | |
255 | } | |
256 | ||
257 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
258 | } | |
259 | ||
c003ec21 DH |
260 | /* |
261 | * This sets the rumble bit on the given output report if rumble is | |
262 | * currently enabled. | |
263 | * \cmd1 must point to the second byte in the output report => &cmd[1] | |
264 | * This must be called on nearly every output report before passing it | |
265 | * into the output queue! | |
266 | */ | |
267 | static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1) | |
268 | { | |
269 | if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE) | |
270 | *cmd1 |= 0x01; | |
271 | } | |
272 | ||
273 | static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) | |
274 | { | |
275 | __u8 cmd[2]; | |
276 | ||
277 | rumble = !!rumble; | |
278 | if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE)) | |
279 | return; | |
280 | ||
281 | if (rumble) | |
282 | wdata->state.flags |= WIIPROTO_FLAG_RUMBLE; | |
283 | else | |
284 | wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE; | |
285 | ||
286 | cmd[0] = WIIPROTO_REQ_RUMBLE; | |
287 | cmd[1] = 0; | |
288 | ||
289 | wiiproto_keep_rumble(wdata, &cmd[1]); | |
290 | wiimote_queue(wdata, cmd, sizeof(cmd)); | |
291 | } | |
292 | ||
db308346 DH |
293 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
294 | { | |
295 | __u8 cmd[2]; | |
296 | ||
32a0d9a5 DH |
297 | leds &= WIIPROTO_FLAGS_LEDS; |
298 | if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds) | |
299 | return; | |
300 | wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds; | |
301 | ||
db308346 DH |
302 | cmd[0] = WIIPROTO_REQ_LED; |
303 | cmd[1] = 0; | |
304 | ||
305 | if (leds & WIIPROTO_FLAG_LED1) | |
306 | cmd[1] |= 0x10; | |
307 | if (leds & WIIPROTO_FLAG_LED2) | |
308 | cmd[1] |= 0x20; | |
309 | if (leds & WIIPROTO_FLAG_LED3) | |
310 | cmd[1] |= 0x40; | |
311 | if (leds & WIIPROTO_FLAG_LED4) | |
312 | cmd[1] |= 0x80; | |
313 | ||
c003ec21 | 314 | wiiproto_keep_rumble(wdata, &cmd[1]); |
db308346 DH |
315 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
316 | } | |
317 | ||
2cb5e4bc DH |
318 | /* |
319 | * Check what peripherals of the wiimote are currently | |
320 | * active and select a proper DRM that supports all of | |
321 | * the requested data inputs. | |
322 | */ | |
323 | static __u8 select_drm(struct wiimote_data *wdata) | |
324 | { | |
f363e4f6 DH |
325 | __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR; |
326 | ||
327 | if (ir == WIIPROTO_FLAG_IR_BASIC) { | |
328 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) | |
329 | return WIIPROTO_REQ_DRM_KAIE; | |
330 | else | |
331 | return WIIPROTO_REQ_DRM_KIE; | |
332 | } else if (ir == WIIPROTO_FLAG_IR_EXT) { | |
333 | return WIIPROTO_REQ_DRM_KAI; | |
334 | } else if (ir == WIIPROTO_FLAG_IR_FULL) { | |
335 | return WIIPROTO_REQ_DRM_SKAI1; | |
336 | } else { | |
337 | if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) | |
338 | return WIIPROTO_REQ_DRM_KA; | |
339 | else | |
340 | return WIIPROTO_REQ_DRM_K; | |
341 | } | |
2cb5e4bc DH |
342 | } |
343 | ||
344 | static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) | |
345 | { | |
346 | __u8 cmd[3]; | |
347 | ||
348 | if (drm == WIIPROTO_REQ_NULL) | |
349 | drm = select_drm(wdata); | |
350 | ||
351 | cmd[0] = WIIPROTO_REQ_DRM; | |
352 | cmd[1] = 0; | |
353 | cmd[2] = drm; | |
354 | ||
c003ec21 | 355 | wiiproto_keep_rumble(wdata, &cmd[1]); |
2cb5e4bc DH |
356 | wiimote_queue(wdata, cmd, sizeof(cmd)); |
357 | } | |
358 | ||
e3979a91 DH |
359 | static void wiiproto_req_status(struct wiimote_data *wdata) |
360 | { | |
361 | __u8 cmd[2]; | |
362 | ||
363 | cmd[0] = WIIPROTO_REQ_SREQ; | |
364 | cmd[1] = 0; | |
365 | ||
366 | wiiproto_keep_rumble(wdata, &cmd[1]); | |
367 | wiimote_queue(wdata, cmd, sizeof(cmd)); | |
368 | } | |
369 | ||
98a558ae DH |
370 | static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) |
371 | { | |
372 | accel = !!accel; | |
373 | if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) | |
374 | return; | |
375 | ||
376 | if (accel) | |
377 | wdata->state.flags |= WIIPROTO_FLAG_ACCEL; | |
378 | else | |
379 | wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL; | |
380 | ||
381 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | |
382 | } | |
383 | ||
fc221cda DH |
384 | static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags) |
385 | { | |
386 | __u8 cmd[2]; | |
387 | ||
388 | cmd[0] = WIIPROTO_REQ_IR1; | |
389 | cmd[1] = flags; | |
390 | ||
391 | wiiproto_keep_rumble(wdata, &cmd[1]); | |
392 | wiimote_queue(wdata, cmd, sizeof(cmd)); | |
393 | } | |
394 | ||
395 | static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags) | |
396 | { | |
397 | __u8 cmd[2]; | |
398 | ||
399 | cmd[0] = WIIPROTO_REQ_IR2; | |
400 | cmd[1] = flags; | |
401 | ||
402 | wiiproto_keep_rumble(wdata, &cmd[1]); | |
403 | wiimote_queue(wdata, cmd, sizeof(cmd)); | |
404 | } | |
405 | ||
be1ecd62 DH |
406 | #define wiiproto_req_wreg(wdata, os, buf, sz) \ |
407 | wiiproto_req_wmem((wdata), false, (os), (buf), (sz)) | |
408 | ||
409 | #define wiiproto_req_weeprom(wdata, os, buf, sz) \ | |
410 | wiiproto_req_wmem((wdata), true, (os), (buf), (sz)) | |
411 | ||
412 | static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom, | |
413 | __u32 offset, const __u8 *buf, __u8 size) | |
414 | { | |
415 | __u8 cmd[22]; | |
416 | ||
417 | if (size > 16 || size == 0) { | |
418 | hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size); | |
419 | return; | |
420 | } | |
421 | ||
422 | memset(cmd, 0, sizeof(cmd)); | |
423 | cmd[0] = WIIPROTO_REQ_WMEM; | |
424 | cmd[2] = (offset >> 16) & 0xff; | |
425 | cmd[3] = (offset >> 8) & 0xff; | |
426 | cmd[4] = offset & 0xff; | |
427 | cmd[5] = size; | |
428 | memcpy(&cmd[6], buf, size); | |
429 | ||
430 | if (!eeprom) | |
431 | cmd[1] |= 0x04; | |
432 | ||
433 | wiiproto_keep_rumble(wdata, &cmd[1]); | |
434 | wiimote_queue(wdata, cmd, sizeof(cmd)); | |
435 | } | |
436 | ||
33e84013 DH |
437 | /* requries the cmd-mutex to be held */ |
438 | static int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, | |
439 | const __u8 *wmem, __u8 size) | |
440 | { | |
441 | unsigned long flags; | |
442 | int ret; | |
443 | ||
444 | spin_lock_irqsave(&wdata->state.lock, flags); | |
445 | wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0); | |
446 | wiiproto_req_wreg(wdata, offset, wmem, size); | |
447 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
448 | ||
449 | ret = wiimote_cmd_wait(wdata); | |
450 | if (!ret && wdata->state.cmd_err) | |
451 | ret = -EIO; | |
452 | ||
453 | return ret; | |
454 | } | |
455 | ||
fc221cda DH |
456 | static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode) |
457 | { | |
458 | int ret; | |
459 | unsigned long flags; | |
460 | __u8 format = 0; | |
461 | static const __u8 data_enable[] = { 0x01 }; | |
462 | static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01, | |
463 | 0x00, 0xaa, 0x00, 0x64 }; | |
464 | static const __u8 data_sens2[] = { 0x63, 0x03 }; | |
465 | static const __u8 data_fin[] = { 0x08 }; | |
466 | ||
467 | spin_lock_irqsave(&wdata->state.lock, flags); | |
468 | ||
469 | if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) { | |
470 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
471 | return 0; | |
472 | } | |
473 | ||
474 | if (mode == 0) { | |
475 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; | |
476 | wiiproto_req_ir1(wdata, 0); | |
477 | wiiproto_req_ir2(wdata, 0); | |
478 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | |
479 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
480 | return 0; | |
481 | } | |
482 | ||
483 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
484 | ||
485 | ret = wiimote_cmd_acquire(wdata); | |
486 | if (ret) | |
487 | return ret; | |
488 | ||
489 | /* send PIXEL CLOCK ENABLE cmd first */ | |
490 | spin_lock_irqsave(&wdata->state.lock, flags); | |
491 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0); | |
492 | wiiproto_req_ir1(wdata, 0x06); | |
493 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
494 | ||
495 | ret = wiimote_cmd_wait(wdata); | |
496 | if (ret) | |
497 | goto unlock; | |
498 | if (wdata->state.cmd_err) { | |
499 | ret = -EIO; | |
500 | goto unlock; | |
501 | } | |
502 | ||
503 | /* enable IR LOGIC */ | |
504 | spin_lock_irqsave(&wdata->state.lock, flags); | |
505 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0); | |
506 | wiiproto_req_ir2(wdata, 0x06); | |
507 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
508 | ||
509 | ret = wiimote_cmd_wait(wdata); | |
510 | if (ret) | |
511 | goto unlock; | |
512 | if (wdata->state.cmd_err) { | |
513 | ret = -EIO; | |
514 | goto unlock; | |
515 | } | |
516 | ||
517 | /* enable IR cam but do not make it send data, yet */ | |
518 | ret = wiimote_cmd_write(wdata, 0xb00030, data_enable, | |
519 | sizeof(data_enable)); | |
520 | if (ret) | |
521 | goto unlock; | |
522 | ||
523 | /* write first sensitivity block */ | |
524 | ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1, | |
525 | sizeof(data_sens1)); | |
526 | if (ret) | |
527 | goto unlock; | |
528 | ||
529 | /* write second sensitivity block */ | |
530 | ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2, | |
531 | sizeof(data_sens2)); | |
532 | if (ret) | |
533 | goto unlock; | |
534 | ||
535 | /* put IR cam into desired state */ | |
536 | switch (mode) { | |
537 | case WIIPROTO_FLAG_IR_FULL: | |
538 | format = 5; | |
539 | break; | |
540 | case WIIPROTO_FLAG_IR_EXT: | |
541 | format = 3; | |
542 | break; | |
543 | case WIIPROTO_FLAG_IR_BASIC: | |
544 | format = 1; | |
545 | break; | |
546 | } | |
547 | ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format)); | |
548 | if (ret) | |
549 | goto unlock; | |
550 | ||
551 | /* make IR cam send data */ | |
552 | ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin)); | |
553 | if (ret) | |
554 | goto unlock; | |
555 | ||
556 | /* request new DRM mode compatible to IR mode */ | |
557 | spin_lock_irqsave(&wdata->state.lock, flags); | |
558 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; | |
559 | wdata->state.flags |= mode & WIIPROTO_FLAGS_IR; | |
560 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | |
561 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
562 | ||
563 | unlock: | |
564 | wiimote_cmd_release(wdata); | |
565 | return ret; | |
566 | } | |
567 | ||
23a5a4a3 DH |
568 | static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev) |
569 | { | |
570 | struct wiimote_data *wdata; | |
571 | struct device *dev = led_dev->dev->parent; | |
572 | int i; | |
573 | unsigned long flags; | |
574 | bool value = false; | |
575 | ||
576 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); | |
577 | ||
578 | for (i = 0; i < 4; ++i) { | |
579 | if (wdata->leds[i] == led_dev) { | |
580 | spin_lock_irqsave(&wdata->state.lock, flags); | |
581 | value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1); | |
582 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
583 | break; | |
584 | } | |
585 | } | |
586 | ||
587 | return value ? LED_FULL : LED_OFF; | |
588 | } | |
589 | ||
590 | static void wiimote_leds_set(struct led_classdev *led_dev, | |
591 | enum led_brightness value) | |
592 | { | |
593 | struct wiimote_data *wdata; | |
594 | struct device *dev = led_dev->dev->parent; | |
595 | int i; | |
596 | unsigned long flags; | |
597 | __u8 state, flag; | |
598 | ||
599 | wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); | |
600 | ||
601 | for (i = 0; i < 4; ++i) { | |
602 | if (wdata->leds[i] == led_dev) { | |
603 | flag = WIIPROTO_FLAG_LED(i + 1); | |
604 | spin_lock_irqsave(&wdata->state.lock, flags); | |
605 | state = wdata->state.flags; | |
606 | if (value == LED_OFF) | |
607 | wiiproto_req_leds(wdata, state & ~flag); | |
608 | else | |
609 | wiiproto_req_leds(wdata, state | flag); | |
610 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
611 | break; | |
612 | } | |
613 | } | |
614 | } | |
3c1c2fce | 615 | |
d020be92 DH |
616 | static int wiimote_ff_play(struct input_dev *dev, void *data, |
617 | struct ff_effect *eff) | |
672bc4e0 | 618 | { |
d020be92 DH |
619 | struct wiimote_data *wdata = input_get_drvdata(dev); |
620 | __u8 value; | |
621 | unsigned long flags; | |
622 | ||
623 | /* | |
624 | * The wiimote supports only a single rumble motor so if any magnitude | |
625 | * is set to non-zero then we start the rumble motor. If both are set to | |
626 | * zero, we stop the rumble motor. | |
627 | */ | |
628 | ||
629 | if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) | |
630 | value = 1; | |
631 | else | |
632 | value = 0; | |
633 | ||
634 | spin_lock_irqsave(&wdata->state.lock, flags); | |
635 | wiiproto_req_rumble(wdata, value); | |
636 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
637 | ||
672bc4e0 DH |
638 | return 0; |
639 | } | |
640 | ||
26af1748 DH |
641 | static int wiimote_input_open(struct input_dev *dev) |
642 | { | |
643 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
644 | ||
645 | return hid_hw_open(wdata->hdev); | |
646 | } | |
647 | ||
648 | static void wiimote_input_close(struct input_dev *dev) | |
649 | { | |
650 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
651 | ||
652 | hid_hw_close(wdata->hdev); | |
653 | } | |
654 | ||
98a558ae DH |
655 | static int wiimote_accel_open(struct input_dev *dev) |
656 | { | |
657 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
658 | int ret; | |
659 | unsigned long flags; | |
660 | ||
661 | ret = hid_hw_open(wdata->hdev); | |
662 | if (ret) | |
663 | return ret; | |
664 | ||
665 | spin_lock_irqsave(&wdata->state.lock, flags); | |
666 | wiiproto_req_accel(wdata, true); | |
667 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
668 | ||
669 | return 0; | |
670 | } | |
671 | ||
672 | static void wiimote_accel_close(struct input_dev *dev) | |
673 | { | |
674 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
675 | unsigned long flags; | |
676 | ||
677 | spin_lock_irqsave(&wdata->state.lock, flags); | |
678 | wiiproto_req_accel(wdata, false); | |
679 | spin_unlock_irqrestore(&wdata->state.lock, flags); | |
680 | ||
681 | hid_hw_close(wdata->hdev); | |
682 | } | |
683 | ||
0370d7cb DH |
684 | static int wiimote_ir_open(struct input_dev *dev) |
685 | { | |
686 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
687 | int ret; | |
688 | ||
689 | ret = hid_hw_open(wdata->hdev); | |
690 | if (ret) | |
691 | return ret; | |
692 | ||
693 | ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC); | |
694 | if (ret) { | |
695 | hid_hw_close(wdata->hdev); | |
696 | return ret; | |
697 | } | |
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
702 | static void wiimote_ir_close(struct input_dev *dev) | |
703 | { | |
704 | struct wiimote_data *wdata = input_get_drvdata(dev); | |
705 | ||
706 | wiimote_init_ir(wdata, 0); | |
707 | hid_hw_close(wdata->hdev); | |
708 | } | |
709 | ||
1abb9ad3 DH |
710 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
711 | { | |
712 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], | |
713 | !!(payload[0] & 0x01)); | |
714 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], | |
715 | !!(payload[0] & 0x02)); | |
716 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], | |
717 | !!(payload[0] & 0x04)); | |
718 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], | |
719 | !!(payload[0] & 0x08)); | |
720 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], | |
721 | !!(payload[0] & 0x10)); | |
722 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], | |
723 | !!(payload[1] & 0x01)); | |
724 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], | |
725 | !!(payload[1] & 0x02)); | |
726 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], | |
727 | !!(payload[1] & 0x04)); | |
728 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], | |
729 | !!(payload[1] & 0x08)); | |
730 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], | |
731 | !!(payload[1] & 0x10)); | |
732 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], | |
733 | !!(payload[1] & 0x80)); | |
734 | input_sync(wdata->input); | |
735 | } | |
736 | ||
efcf9188 DH |
737 | static void handler_accel(struct wiimote_data *wdata, const __u8 *payload) |
738 | { | |
739 | __u16 x, y, z; | |
740 | ||
741 | if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) | |
742 | return; | |
743 | ||
744 | /* | |
745 | * payload is: BB BB XX YY ZZ | |
746 | * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ | |
747 | * contain the upper 8 bits of each value. The lower 2 bits are | |
748 | * contained in the buttons data BB BB. | |
749 | * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the | |
750 | * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y | |
751 | * accel value and bit 6 is the second bit of the Z value. | |
752 | * The first bit of Y and Z values is not available and always set to 0. | |
753 | * 0x200 is returned on no movement. | |
754 | */ | |
755 | ||
756 | x = payload[2] << 2; | |
757 | y = payload[3] << 2; | |
758 | z = payload[4] << 2; | |
759 | ||
760 | x |= (payload[0] >> 5) & 0x3; | |
761 | y |= (payload[1] >> 4) & 0x2; | |
762 | z |= (payload[1] >> 5) & 0x2; | |
763 | ||
764 | input_report_abs(wdata->accel, ABS_RX, x - 0x200); | |
765 | input_report_abs(wdata->accel, ABS_RY, y - 0x200); | |
766 | input_report_abs(wdata->accel, ABS_RZ, z - 0x200); | |
767 | input_sync(wdata->accel); | |
768 | } | |
769 | ||
eac39e7e DH |
770 | #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ |
771 | ABS_HAT0X, ABS_HAT0Y) | |
772 | #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ | |
773 | ABS_HAT1X, ABS_HAT1Y) | |
774 | #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ | |
775 | ABS_HAT2X, ABS_HAT2Y) | |
776 | #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ | |
777 | ABS_HAT3X, ABS_HAT3Y) | |
778 | ||
779 | static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir, | |
780 | bool packed, __u8 xid, __u8 yid) | |
781 | { | |
782 | __u16 x, y; | |
783 | ||
784 | if (!(wdata->state.flags & WIIPROTO_FLAGS_IR)) | |
785 | return; | |
786 | ||
787 | /* | |
788 | * Basic IR data is encoded into 3 bytes. The first two bytes are the | |
789 | * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits | |
790 | * of both. | |
791 | * If data is packed, then the 3rd byte is put first and slightly | |
792 | * reordered. This allows to interleave packed and non-packed data to | |
793 | * have two IR sets in 5 bytes instead of 6. | |
794 | * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev. | |
795 | */ | |
796 | ||
797 | if (packed) { | |
798 | x = ir[1] << 2; | |
799 | y = ir[2] << 2; | |
800 | ||
801 | x |= ir[0] & 0x3; | |
802 | y |= (ir[0] >> 2) & 0x3; | |
803 | } else { | |
804 | x = ir[0] << 2; | |
805 | y = ir[1] << 2; | |
806 | ||
807 | x |= (ir[2] >> 4) & 0x3; | |
808 | y |= (ir[2] >> 6) & 0x3; | |
809 | } | |
810 | ||
811 | input_report_abs(wdata->ir, xid, x); | |
812 | input_report_abs(wdata->ir, yid, y); | |
813 | } | |
efcf9188 | 814 | |
c87019e4 DH |
815 | static void handler_status(struct wiimote_data *wdata, const __u8 *payload) |
816 | { | |
817 | handler_keys(wdata, payload); | |
818 | ||
819 | /* on status reports the drm is reset so we need to resend the drm */ | |
820 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); | |
e3979a91 DH |
821 | |
822 | if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) { | |
823 | wdata->state.cmd_battery = payload[5]; | |
824 | wiimote_cmd_complete(wdata); | |
825 | } | |
c87019e4 DH |
826 | } |
827 | ||
be1ecd62 DH |
828 | static void handler_data(struct wiimote_data *wdata, const __u8 *payload) |
829 | { | |
830 | handler_keys(wdata, payload); | |
831 | } | |
832 | ||
c87019e4 DH |
833 | static void handler_return(struct wiimote_data *wdata, const __u8 *payload) |
834 | { | |
835 | __u8 err = payload[3]; | |
836 | __u8 cmd = payload[2]; | |
837 | ||
838 | handler_keys(wdata, payload); | |
839 | ||
33e84013 DH |
840 | if (wiimote_cmd_pending(wdata, cmd, 0)) { |
841 | wdata->state.cmd_err = err; | |
842 | wiimote_cmd_complete(wdata); | |
843 | } else if (err) { | |
c87019e4 DH |
844 | hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err, |
845 | cmd); | |
33e84013 | 846 | } |
c87019e4 DH |
847 | } |
848 | ||
efcf9188 DH |
849 | static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload) |
850 | { | |
851 | handler_keys(wdata, payload); | |
852 | handler_accel(wdata, payload); | |
853 | } | |
854 | ||
7336b9f9 DH |
855 | static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload) |
856 | { | |
857 | handler_keys(wdata, payload); | |
858 | } | |
859 | ||
efcf9188 DH |
860 | static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload) |
861 | { | |
862 | handler_keys(wdata, payload); | |
863 | handler_accel(wdata, payload); | |
eac39e7e DH |
864 | ir_to_input0(wdata, &payload[5], false); |
865 | ir_to_input1(wdata, &payload[8], false); | |
866 | ir_to_input2(wdata, &payload[11], false); | |
867 | ir_to_input3(wdata, &payload[14], false); | |
868 | input_sync(wdata->ir); | |
869 | } | |
870 | ||
7336b9f9 DH |
871 | static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload) |
872 | { | |
873 | handler_keys(wdata, payload); | |
874 | } | |
875 | ||
eac39e7e DH |
876 | static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload) |
877 | { | |
878 | handler_keys(wdata, payload); | |
879 | ir_to_input0(wdata, &payload[2], false); | |
880 | ir_to_input1(wdata, &payload[4], true); | |
881 | ir_to_input2(wdata, &payload[7], false); | |
882 | ir_to_input3(wdata, &payload[9], true); | |
883 | input_sync(wdata->ir); | |
efcf9188 DH |
884 | } |
885 | ||
886 | static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload) | |
887 | { | |
888 | handler_keys(wdata, payload); | |
889 | handler_accel(wdata, payload); | |
890 | } | |
891 | ||
892 | static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload) | |
893 | { | |
894 | handler_keys(wdata, payload); | |
895 | handler_accel(wdata, payload); | |
eac39e7e DH |
896 | ir_to_input0(wdata, &payload[5], false); |
897 | ir_to_input1(wdata, &payload[7], true); | |
898 | ir_to_input2(wdata, &payload[10], false); | |
899 | ir_to_input3(wdata, &payload[12], true); | |
900 | input_sync(wdata->ir); | |
efcf9188 DH |
901 | } |
902 | ||
7336b9f9 DH |
903 | static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload) |
904 | { | |
905 | } | |
906 | ||
efcf9188 DH |
907 | static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload) |
908 | { | |
909 | handler_keys(wdata, payload); | |
910 | ||
911 | wdata->state.accel_split[0] = payload[2]; | |
912 | wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20); | |
913 | wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80); | |
eac39e7e DH |
914 | |
915 | ir_to_input0(wdata, &payload[3], false); | |
916 | ir_to_input1(wdata, &payload[12], false); | |
917 | input_sync(wdata->ir); | |
efcf9188 DH |
918 | } |
919 | ||
920 | static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload) | |
921 | { | |
922 | __u8 buf[5]; | |
923 | ||
924 | handler_keys(wdata, payload); | |
925 | ||
926 | wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02); | |
927 | wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08); | |
928 | ||
929 | buf[0] = 0; | |
930 | buf[1] = 0; | |
931 | buf[2] = wdata->state.accel_split[0]; | |
932 | buf[3] = payload[2]; | |
933 | buf[4] = wdata->state.accel_split[1]; | |
934 | handler_accel(wdata, buf); | |
eac39e7e DH |
935 | |
936 | ir_to_input2(wdata, &payload[3], false); | |
937 | ir_to_input3(wdata, &payload[12], false); | |
938 | input_sync(wdata->ir); | |
efcf9188 DH |
939 | } |
940 | ||
a4d19197 DH |
941 | struct wiiproto_handler { |
942 | __u8 id; | |
943 | size_t size; | |
944 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); | |
945 | }; | |
946 | ||
947 | static struct wiiproto_handler handlers[] = { | |
c87019e4 | 948 | { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status }, |
be1ecd62 | 949 | { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data }, |
c87019e4 | 950 | { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return }, |
1abb9ad3 | 951 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
efcf9188 | 952 | { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA }, |
7336b9f9 | 953 | { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE }, |
efcf9188 | 954 | { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI }, |
7336b9f9 | 955 | { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE }, |
efcf9188 | 956 | { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE }, |
eac39e7e | 957 | { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE }, |
efcf9188 | 958 | { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE }, |
7336b9f9 | 959 | { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E }, |
efcf9188 DH |
960 | { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 }, |
961 | { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 }, | |
a4d19197 DH |
962 | { .id = 0 } |
963 | }; | |
964 | ||
02fb72a0 DH |
965 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
966 | u8 *raw_data, int size) | |
967 | { | |
4d36e975 | 968 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
a4d19197 DH |
969 | struct wiiproto_handler *h; |
970 | int i; | |
32a0d9a5 | 971 | unsigned long flags; |
7336b9f9 | 972 | bool handled = false; |
4d36e975 | 973 | |
02fb72a0 DH |
974 | if (size < 1) |
975 | return -EINVAL; | |
976 | ||
32a0d9a5 DH |
977 | spin_lock_irqsave(&wdata->state.lock, flags); |
978 | ||
a4d19197 DH |
979 | for (i = 0; handlers[i].id; ++i) { |
980 | h = &handlers[i]; | |
7336b9f9 | 981 | if (h->id == raw_data[0] && h->size < size) { |
a4d19197 | 982 | h->func(wdata, &raw_data[1]); |
7336b9f9 DH |
983 | handled = true; |
984 | } | |
a4d19197 DH |
985 | } |
986 | ||
7336b9f9 DH |
987 | if (!handled) |
988 | hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0], | |
989 | size); | |
990 | ||
32a0d9a5 DH |
991 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
992 | ||
02fb72a0 DH |
993 | return 0; |
994 | } | |
995 | ||
23a5a4a3 DH |
996 | static void wiimote_leds_destroy(struct wiimote_data *wdata) |
997 | { | |
998 | int i; | |
999 | struct led_classdev *led; | |
1000 | ||
1001 | for (i = 0; i < 4; ++i) { | |
1002 | if (wdata->leds[i]) { | |
1003 | led = wdata->leds[i]; | |
1004 | wdata->leds[i] = NULL; | |
1005 | led_classdev_unregister(led); | |
1006 | kfree(led); | |
1007 | } | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | static int wiimote_leds_create(struct wiimote_data *wdata) | |
1012 | { | |
1013 | int i, ret; | |
1014 | struct device *dev = &wdata->hdev->dev; | |
1015 | size_t namesz = strlen(dev_name(dev)) + 9; | |
1016 | struct led_classdev *led; | |
1017 | char *name; | |
1018 | ||
1019 | for (i = 0; i < 4; ++i) { | |
1020 | led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL); | |
1021 | if (!led) { | |
1022 | ret = -ENOMEM; | |
1023 | goto err; | |
1024 | } | |
1025 | name = (void*)&led[1]; | |
1026 | snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i); | |
1027 | led->name = name; | |
1028 | led->brightness = 0; | |
1029 | led->max_brightness = 1; | |
1030 | led->brightness_get = wiimote_leds_get; | |
1031 | led->brightness_set = wiimote_leds_set; | |
1032 | ||
1033 | ret = led_classdev_register(dev, led); | |
1034 | if (ret) { | |
1035 | kfree(led); | |
1036 | goto err; | |
1037 | } | |
1038 | wdata->leds[i] = led; | |
1039 | } | |
1040 | ||
1041 | return 0; | |
1042 | ||
1043 | err: | |
1044 | wiimote_leds_destroy(wdata); | |
1045 | return ret; | |
1046 | } | |
1047 | ||
e894d0e3 DH |
1048 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
1049 | { | |
1050 | struct wiimote_data *wdata; | |
1abb9ad3 | 1051 | int i; |
e894d0e3 DH |
1052 | |
1053 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); | |
1054 | if (!wdata) | |
1055 | return NULL; | |
1056 | ||
672bc4e0 | 1057 | wdata->input = input_allocate_device(); |
98a558ae DH |
1058 | if (!wdata->input) |
1059 | goto err; | |
672bc4e0 | 1060 | |
e894d0e3 DH |
1061 | wdata->hdev = hdev; |
1062 | hid_set_drvdata(hdev, wdata); | |
1063 | ||
672bc4e0 | 1064 | input_set_drvdata(wdata->input, wdata); |
26af1748 DH |
1065 | wdata->input->open = wiimote_input_open; |
1066 | wdata->input->close = wiimote_input_close; | |
672bc4e0 DH |
1067 | wdata->input->dev.parent = &wdata->hdev->dev; |
1068 | wdata->input->id.bustype = wdata->hdev->bus; | |
1069 | wdata->input->id.vendor = wdata->hdev->vendor; | |
1070 | wdata->input->id.product = wdata->hdev->product; | |
1071 | wdata->input->id.version = wdata->hdev->version; | |
1072 | wdata->input->name = WIIMOTE_NAME; | |
1073 | ||
1abb9ad3 DH |
1074 | set_bit(EV_KEY, wdata->input->evbit); |
1075 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) | |
1076 | set_bit(wiiproto_keymap[i], wdata->input->keybit); | |
1077 | ||
d020be92 | 1078 | set_bit(FF_RUMBLE, wdata->input->ffbit); |
98a558ae DH |
1079 | if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play)) |
1080 | goto err_input; | |
1081 | ||
1082 | wdata->accel = input_allocate_device(); | |
1083 | if (!wdata->accel) | |
1084 | goto err_input; | |
1085 | ||
1086 | input_set_drvdata(wdata->accel, wdata); | |
1087 | wdata->accel->open = wiimote_accel_open; | |
1088 | wdata->accel->close = wiimote_accel_close; | |
1089 | wdata->accel->dev.parent = &wdata->hdev->dev; | |
1090 | wdata->accel->id.bustype = wdata->hdev->bus; | |
1091 | wdata->accel->id.vendor = wdata->hdev->vendor; | |
1092 | wdata->accel->id.product = wdata->hdev->product; | |
1093 | wdata->accel->id.version = wdata->hdev->version; | |
1094 | wdata->accel->name = WIIMOTE_NAME " Accelerometer"; | |
1095 | ||
1096 | set_bit(EV_ABS, wdata->accel->evbit); | |
1097 | set_bit(ABS_RX, wdata->accel->absbit); | |
1098 | set_bit(ABS_RY, wdata->accel->absbit); | |
1099 | set_bit(ABS_RZ, wdata->accel->absbit); | |
1100 | input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4); | |
1101 | input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4); | |
1102 | input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4); | |
d020be92 | 1103 | |
f363e4f6 DH |
1104 | wdata->ir = input_allocate_device(); |
1105 | if (!wdata->ir) | |
1106 | goto err_ir; | |
1107 | ||
1108 | input_set_drvdata(wdata->ir, wdata); | |
0370d7cb DH |
1109 | wdata->ir->open = wiimote_ir_open; |
1110 | wdata->ir->close = wiimote_ir_close; | |
f363e4f6 DH |
1111 | wdata->ir->dev.parent = &wdata->hdev->dev; |
1112 | wdata->ir->id.bustype = wdata->hdev->bus; | |
1113 | wdata->ir->id.vendor = wdata->hdev->vendor; | |
1114 | wdata->ir->id.product = wdata->hdev->product; | |
1115 | wdata->ir->id.version = wdata->hdev->version; | |
1116 | wdata->ir->name = WIIMOTE_NAME " IR"; | |
1117 | ||
1118 | set_bit(EV_ABS, wdata->ir->evbit); | |
1119 | set_bit(ABS_HAT0X, wdata->ir->absbit); | |
1120 | set_bit(ABS_HAT0Y, wdata->ir->absbit); | |
1121 | set_bit(ABS_HAT1X, wdata->ir->absbit); | |
1122 | set_bit(ABS_HAT1Y, wdata->ir->absbit); | |
1123 | set_bit(ABS_HAT2X, wdata->ir->absbit); | |
1124 | set_bit(ABS_HAT2Y, wdata->ir->absbit); | |
1125 | set_bit(ABS_HAT3X, wdata->ir->absbit); | |
1126 | set_bit(ABS_HAT3Y, wdata->ir->absbit); | |
1127 | input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4); | |
1128 | input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4); | |
1129 | input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4); | |
1130 | input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4); | |
1131 | input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4); | |
1132 | input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4); | |
1133 | input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4); | |
1134 | input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4); | |
1135 | ||
23c063cb DH |
1136 | spin_lock_init(&wdata->qlock); |
1137 | INIT_WORK(&wdata->worker, wiimote_worker); | |
1138 | ||
32a0d9a5 | 1139 | spin_lock_init(&wdata->state.lock); |
29d28064 DH |
1140 | init_completion(&wdata->state.ready); |
1141 | mutex_init(&wdata->state.sync); | |
32a0d9a5 | 1142 | |
e894d0e3 | 1143 | return wdata; |
98a558ae | 1144 | |
f363e4f6 DH |
1145 | err_ir: |
1146 | input_free_device(wdata->accel); | |
98a558ae DH |
1147 | err_input: |
1148 | input_free_device(wdata->input); | |
1149 | err: | |
1150 | kfree(wdata); | |
1151 | return NULL; | |
e894d0e3 DH |
1152 | } |
1153 | ||
1154 | static void wiimote_destroy(struct wiimote_data *wdata) | |
1155 | { | |
23a5a4a3 | 1156 | wiimote_leds_destroy(wdata); |
3989ef6c | 1157 | |
98a558ae | 1158 | input_unregister_device(wdata->accel); |
f363e4f6 | 1159 | input_unregister_device(wdata->ir); |
3989ef6c DH |
1160 | input_unregister_device(wdata->input); |
1161 | cancel_work_sync(&wdata->worker); | |
1162 | hid_hw_stop(wdata->hdev); | |
1163 | ||
e894d0e3 DH |
1164 | kfree(wdata); |
1165 | } | |
1166 | ||
02fb72a0 DH |
1167 | static int wiimote_hid_probe(struct hid_device *hdev, |
1168 | const struct hid_device_id *id) | |
fb51b443 | 1169 | { |
e894d0e3 | 1170 | struct wiimote_data *wdata; |
02fb72a0 DH |
1171 | int ret; |
1172 | ||
e894d0e3 DH |
1173 | wdata = wiimote_create(hdev); |
1174 | if (!wdata) { | |
1175 | hid_err(hdev, "Can't alloc device\n"); | |
1176 | return -ENOMEM; | |
1177 | } | |
1178 | ||
02fb72a0 DH |
1179 | ret = hid_parse(hdev); |
1180 | if (ret) { | |
1181 | hid_err(hdev, "HID parse failed\n"); | |
e894d0e3 | 1182 | goto err; |
02fb72a0 DH |
1183 | } |
1184 | ||
1185 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); | |
1186 | if (ret) { | |
1187 | hid_err(hdev, "HW start failed\n"); | |
e894d0e3 | 1188 | goto err; |
02fb72a0 DH |
1189 | } |
1190 | ||
98a558ae | 1191 | ret = input_register_device(wdata->accel); |
672bc4e0 DH |
1192 | if (ret) { |
1193 | hid_err(hdev, "Cannot register input device\n"); | |
1194 | goto err_stop; | |
1195 | } | |
1196 | ||
f363e4f6 DH |
1197 | ret = input_register_device(wdata->ir); |
1198 | if (ret) { | |
1199 | hid_err(hdev, "Cannot register input device\n"); | |
1200 | goto err_ir; | |
1201 | } | |
1202 | ||
98a558ae DH |
1203 | ret = input_register_device(wdata->input); |
1204 | if (ret) { | |
1205 | hid_err(hdev, "Cannot register input device\n"); | |
1206 | goto err_input; | |
1207 | } | |
1208 | ||
23a5a4a3 | 1209 | ret = wiimote_leds_create(wdata); |
3989ef6c DH |
1210 | if (ret) |
1211 | goto err_free; | |
1212 | ||
02fb72a0 | 1213 | hid_info(hdev, "New device registered\n"); |
32a0d9a5 DH |
1214 | |
1215 | /* by default set led1 after device initialization */ | |
1216 | spin_lock_irq(&wdata->state.lock); | |
db308346 | 1217 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
32a0d9a5 DH |
1218 | spin_unlock_irq(&wdata->state.lock); |
1219 | ||
fb51b443 | 1220 | return 0; |
e894d0e3 | 1221 | |
3989ef6c DH |
1222 | err_free: |
1223 | wiimote_destroy(wdata); | |
1224 | return ret; | |
1225 | ||
98a558ae | 1226 | err_input: |
f363e4f6 DH |
1227 | input_unregister_device(wdata->ir); |
1228 | wdata->ir = NULL; | |
1229 | err_ir: | |
98a558ae DH |
1230 | input_unregister_device(wdata->accel); |
1231 | wdata->accel = NULL; | |
672bc4e0 DH |
1232 | err_stop: |
1233 | hid_hw_stop(hdev); | |
e894d0e3 | 1234 | err: |
f363e4f6 | 1235 | input_free_device(wdata->ir); |
98a558ae | 1236 | input_free_device(wdata->accel); |
672bc4e0 | 1237 | input_free_device(wdata->input); |
3989ef6c | 1238 | kfree(wdata); |
e894d0e3 | 1239 | return ret; |
fb51b443 DH |
1240 | } |
1241 | ||
02fb72a0 DH |
1242 | static void wiimote_hid_remove(struct hid_device *hdev) |
1243 | { | |
e894d0e3 DH |
1244 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
1245 | ||
02fb72a0 | 1246 | hid_info(hdev, "Device removed\n"); |
e894d0e3 | 1247 | wiimote_destroy(wdata); |
02fb72a0 DH |
1248 | } |
1249 | ||
1250 | static const struct hid_device_id wiimote_hid_devices[] = { | |
1251 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, | |
1252 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, | |
1253 | { } | |
1254 | }; | |
1255 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); | |
1256 | ||
1257 | static struct hid_driver wiimote_hid_driver = { | |
1258 | .name = "wiimote", | |
1259 | .id_table = wiimote_hid_devices, | |
1260 | .probe = wiimote_hid_probe, | |
1261 | .remove = wiimote_hid_remove, | |
1262 | .raw_event = wiimote_hid_event, | |
1263 | }; | |
1264 | ||
1265 | static int __init wiimote_init(void) | |
1266 | { | |
1267 | int ret; | |
1268 | ||
1269 | ret = hid_register_driver(&wiimote_hid_driver); | |
1270 | if (ret) | |
1271 | pr_err("Can't register wiimote hid driver\n"); | |
1272 | ||
1273 | return ret; | |
1274 | } | |
1275 | ||
fb51b443 DH |
1276 | static void __exit wiimote_exit(void) |
1277 | { | |
02fb72a0 | 1278 | hid_unregister_driver(&wiimote_hid_driver); |
fb51b443 DH |
1279 | } |
1280 | ||
1281 | module_init(wiimote_init); | |
1282 | module_exit(wiimote_exit); | |
1283 | MODULE_LICENSE("GPL"); | |
1284 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); | |
1285 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); | |
1286 | MODULE_VERSION(WIIMOTE_VERSION); |