Lines Matching refs:wdata
45 struct wiimote_data *wdata = container_of(queue, struct wiimote_data,
50 spin_lock_irqsave(&wdata->queue.lock, flags);
52 while (wdata->queue.head != wdata->queue.tail) {
53 spin_unlock_irqrestore(&wdata->queue.lock, flags);
54 ret = wiimote_hid_send(wdata->hdev,
55 wdata->queue.outq[wdata->queue.tail].data,
56 wdata->queue.outq[wdata->queue.tail].size);
58 spin_lock_irqsave(&wdata->state.lock, flags);
59 wiimote_cmd_abort(wdata);
60 spin_unlock_irqrestore(&wdata->state.lock, flags);
62 spin_lock_irqsave(&wdata->queue.lock, flags);
64 wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE;
67 spin_unlock_irqrestore(&wdata->queue.lock, flags);
70 static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
77 hid_warn(wdata->hdev, "Sending too large output report\n");
79 spin_lock_irqsave(&wdata->queue.lock, flags);
93 spin_lock_irqsave(&wdata->queue.lock, flags);
95 memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count);
96 wdata->queue.outq[wdata->queue.head].size = count;
97 newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE;
99 if (wdata->queue.head == wdata->queue.tail) {
100 wdata->queue.head = newhead;
101 schedule_work(&wdata->queue.worker);
102 } else if (newhead != wdata->queue.tail) {
103 wdata->queue.head = newhead;
105 hid_warn(wdata->hdev, "Output queue is full");
112 wiimote_cmd_abort(wdata);
114 spin_unlock_irqrestore(&wdata->queue.lock, flags);
124 static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
126 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
130 void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
135 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
139 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
141 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
146 wiiproto_keep_rumble(wdata, &cmd[1]);
147 wiimote_queue(wdata, cmd, sizeof(cmd));
150 void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
155 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
157 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
171 wiiproto_keep_rumble(wdata, &cmd[1]);
172 wiimote_queue(wdata, cmd, sizeof(cmd));
194 static __u8 select_drm(struct wiimote_data *wdata)
196 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
199 ext = (wdata->state.flags & WIIPROTO_FLAG_EXT_USED) ||
200 (wdata->state.flags & WIIPROTO_FLAG_MP_USED);
203 if (wdata->state.devtype == WIIMOTE_DEV_BALANCE_BOARD) {
211 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
226 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
240 void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
244 if (wdata->state.flags & WIIPROTO_FLAG_DRM_LOCKED)
245 drm = wdata->state.drm;
247 drm = select_drm(wdata);
253 wdata->state.drm = drm;
254 wiiproto_keep_rumble(wdata, &cmd[1]);
255 wiimote_queue(wdata, cmd, sizeof(cmd));
258 void wiiproto_req_status(struct wiimote_data *wdata)
265 wiiproto_keep_rumble(wdata, &cmd[1]);
266 wiimote_queue(wdata, cmd, sizeof(cmd));
269 void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
272 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
276 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
278 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
280 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
283 void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
290 wiiproto_keep_rumble(wdata, &cmd[1]);
291 wiimote_queue(wdata, cmd, sizeof(cmd));
294 void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
301 wiiproto_keep_rumble(wdata, &cmd[1]);
302 wiimote_queue(wdata, cmd, sizeof(cmd));
305 #define wiiproto_req_wreg(wdata, os, buf, sz) \
306 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
308 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
309 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
311 static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
317 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
332 wiiproto_keep_rumble(wdata, &cmd[1]);
333 wiimote_queue(wdata, cmd, sizeof(cmd));
336 void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
342 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
357 wiiproto_keep_rumble(wdata, &cmd[1]);
358 wiimote_queue(wdata, cmd, sizeof(cmd));
362 int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
368 spin_lock_irqsave(&wdata->state.lock, flags);
369 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
370 wiiproto_req_wreg(wdata, offset, wmem, size);
371 spin_unlock_irqrestore(&wdata->state.lock, flags);
373 ret = wiimote_cmd_wait(wdata);
374 if (!ret && wdata->state.cmd_err)
381 ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
387 spin_lock_irqsave(&wdata->state.lock, flags);
388 wdata->state.cmd_read_size = size;
389 wdata->state.cmd_read_buf = rmem;
390 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
391 wiiproto_req_rreg(wdata, offset, size);
392 spin_unlock_irqrestore(&wdata->state.lock, flags);
394 ret = wiimote_cmd_wait(wdata);
396 spin_lock_irqsave(&wdata->state.lock, flags);
397 wdata->state.cmd_read_buf = NULL;
398 spin_unlock_irqrestore(&wdata->state.lock, flags);
401 if (wdata->state.cmd_read_size == 0)
404 ret = wdata->state.cmd_read_size;
411 static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
418 ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
424 ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
432 static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata, __u8 *rmem)
437 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
441 hid_dbg(wdata->hdev, "extension ID: %6phC\n", rmem);
466 static int wiimote_cmd_init_mp(struct wiimote_data *wdata)
473 ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem));
479 ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem));
487 static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype)
506 return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem));
510 static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem)
515 ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6);
519 hid_dbg(wdata->hdev, "motion plus ID: %6phC\n", rmem);
524 hid_info(wdata->hdev, "unknown motion plus ID: %6phC\n", rmem);
530 static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata)
536 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
540 hid_dbg(wdata->hdev, "mapped motion plus ID: %6phC\n", rmem);
620 static void wiimote_modules_load(struct wiimote_data *wdata,
638 wdata->input = input_allocate_device();
639 if (!wdata->input)
642 input_set_drvdata(wdata->input, wdata);
643 wdata->input->dev.parent = &wdata->hdev->dev;
644 wdata->input->id.bustype = wdata->hdev->bus;
645 wdata->input->id.vendor = wdata->hdev->vendor;
646 wdata->input->id.product = wdata->hdev->product;
647 wdata->input->id.version = wdata->hdev->version;
648 wdata->input->name = WIIMOTE_NAME;
656 ret = ops->probe(ops, wdata);
661 if (wdata->input) {
662 ret = input_register_device(wdata->input);
667 spin_lock_irq(&wdata->state.lock);
668 wdata->state.devtype = devtype;
669 spin_unlock_irq(&wdata->state.lock);
676 ops->remove(ops, wdata);
679 if (wdata->input) {
680 input_free_device(wdata->input);
681 wdata->input = NULL;
685 static void wiimote_modules_unload(struct wiimote_data *wdata)
691 mods = wiimote_devtype_mods[wdata->state.devtype];
693 spin_lock_irqsave(&wdata->state.lock, flags);
694 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
695 spin_unlock_irqrestore(&wdata->state.lock, flags);
701 if (wdata->input) {
702 input_get_device(wdata->input);
703 input_unregister_device(wdata->input);
709 ops->remove(ops, wdata);
712 if (wdata->input) {
713 input_put_device(wdata->input);
714 wdata->input = NULL;
720 static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext)
729 ret = ops->probe(ops, wdata);
734 spin_lock_irqsave(&wdata->state.lock, flags);
735 wdata->state.exttype = ext;
736 spin_unlock_irqrestore(&wdata->state.lock, flags);
739 static void wiimote_ext_unload(struct wiimote_data *wdata)
744 ops = wiimod_ext_table[wdata->state.exttype];
746 spin_lock_irqsave(&wdata->state.lock, flags);
747 wdata->state.exttype = WIIMOTE_EXT_UNKNOWN;
748 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
749 spin_unlock_irqrestore(&wdata->state.lock, flags);
752 ops->remove(ops, wdata);
755 static void wiimote_mp_load(struct wiimote_data *wdata)
764 ret = ops->probe(ops, wdata);
769 spin_lock_irqsave(&wdata->state.lock, flags);
770 wdata->state.mp = mode;
771 spin_unlock_irqrestore(&wdata->state.lock, flags);
774 static void wiimote_mp_unload(struct wiimote_data *wdata)
779 if (wdata->state.mp < 2)
784 spin_lock_irqsave(&wdata->state.lock, flags);
785 wdata->state.mp = 0;
786 wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
787 spin_unlock_irqrestore(&wdata->state.lock, flags);
790 ops->remove(ops, wdata);
809 static void wiimote_init_set_type(struct wiimote_data *wdata,
816 vendor = wdata->hdev->vendor;
817 product = wdata->hdev->product;
818 name = wdata->hdev->name;
854 hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
857 hid_info(wdata->hdev, "detected device: %s\n",
860 wiimote_modules_load(wdata, devtype);
863 static void wiimote_init_detect(struct wiimote_data *wdata)
869 wiimote_cmd_acquire_noint(wdata);
871 spin_lock_irq(&wdata->state.lock);
872 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
873 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
874 wiiproto_req_status(wdata);
875 spin_unlock_irq(&wdata->state.lock);
877 ret = wiimote_cmd_wait_noint(wdata);
881 spin_lock_irq(&wdata->state.lock);
882 ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
883 spin_unlock_irq(&wdata->state.lock);
888 wiimote_cmd_init_ext(wdata);
889 exttype = wiimote_cmd_read_ext(wdata, extdata);
892 wiimote_cmd_release(wdata);
893 wiimote_init_set_type(wdata, exttype);
896 spin_lock_irq(&wdata->state.lock);
897 if (!(wdata->state.flags & WIIPROTO_FLAG_BUILTIN_MP) &&
898 !(wdata->state.flags & WIIPROTO_FLAG_NO_MP))
899 mod_timer(&wdata->timer, jiffies + HZ * 4);
900 spin_unlock_irq(&wdata->state.lock);
909 static void wiimote_init_poll_mp(struct wiimote_data *wdata)
914 wiimote_cmd_acquire_noint(wdata);
915 wiimote_cmd_init_mp(wdata);
916 mp = wiimote_cmd_read_mp(wdata, mpdata);
917 wiimote_cmd_release(wdata);
921 if (!wdata->state.mp) {
922 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
923 wiimote_mp_load(wdata);
925 } else if (wdata->state.mp) {
926 wiimote_mp_unload(wdata);
929 mod_timer(&wdata->timer, jiffies + HZ * 4);
942 static bool wiimote_init_check(struct wiimote_data *wdata)
948 spin_lock_irq(&wdata->state.lock);
949 flags = wdata->state.flags;
950 spin_unlock_irq(&wdata->state.lock);
952 wiimote_cmd_acquire_noint(wdata);
961 if (wdata->state.exttype == WIIMOTE_EXT_NONE &&
962 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
963 type = wiimote_cmd_read_mp_mapped(wdata);
966 spin_lock_irq(&wdata->state.lock);
967 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
968 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED);
969 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
970 spin_unlock_irq(&wdata->state.lock);
973 hid_dbg(wdata->hdev, "state left: !EXT && MP\n");
987 wdata->state.exttype != WIIMOTE_EXT_NONE) {
988 type = wiimote_cmd_read_ext(wdata, data);
989 ret = type == wdata->state.exttype;
991 spin_lock_irq(&wdata->state.lock);
992 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
993 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
994 spin_unlock_irq(&wdata->state.lock);
997 hid_dbg(wdata->hdev, "state left: EXT && !MP\n");
1013 wdata->state.exttype == WIIMOTE_EXT_NONE) {
1014 type = wiimote_cmd_read_ext(wdata, data);
1015 ret = type == wdata->state.exttype;
1017 spin_lock_irq(&wdata->state.lock);
1018 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1019 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1020 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1021 spin_unlock_irq(&wdata->state.lock);
1024 hid_dbg(wdata->hdev, "state left: !EXT && !MP\n");
1040 if (wdata->state.exttype != WIIMOTE_EXT_NONE &&
1041 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
1042 type = wiimote_cmd_read_mp_mapped(wdata);
1047 spin_lock_irq(&wdata->state.lock);
1048 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1049 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1050 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1051 spin_unlock_irq(&wdata->state.lock);
1054 hid_dbg(wdata->hdev, "state left: EXT && MP\n");
1066 wiimote_cmd_release(wdata);
1071 wiimote_init_poll_mp(wdata);
1095 static void wiimote_init_hotplug(struct wiimote_data *wdata)
1101 hid_dbg(wdata->hdev, "detect extensions..\n");
1103 wiimote_cmd_acquire_noint(wdata);
1105 spin_lock_irq(&wdata->state.lock);
1108 flags = wdata->state.flags;
1111 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1112 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1114 spin_unlock_irq(&wdata->state.lock);
1117 wiimote_cmd_init_ext(wdata);
1121 wiimote_cmd_init_mp(wdata);
1122 mp = wiimote_cmd_read_mp(wdata, mpdata);
1124 exttype = wiimote_cmd_read_ext(wdata, extdata);
1126 wiimote_cmd_release(wdata);
1129 if (exttype != wdata->state.exttype) {
1131 wiimote_ext_unload(wdata);
1134 hid_info(wdata->hdev, "cannot detect extension; %6phC\n",
1137 spin_lock_irq(&wdata->state.lock);
1138 wdata->state.exttype = WIIMOTE_EXT_NONE;
1139 spin_unlock_irq(&wdata->state.lock);
1141 hid_info(wdata->hdev, "detected extension: %s\n",
1144 wiimote_ext_load(wdata, exttype);
1150 if (!wdata->state.mp) {
1151 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
1152 wiimote_mp_load(wdata);
1154 } else if (wdata->state.mp) {
1155 wiimote_mp_unload(wdata);
1164 wiimote_cmd_acquire_noint(wdata);
1165 wiimote_cmd_map_mp(wdata, exttype);
1166 wiimote_cmd_release(wdata);
1169 del_timer_sync(&wdata->timer);
1174 mod_timer(&wdata->timer, jiffies + HZ * 4);
1177 spin_lock_irq(&wdata->state.lock);
1181 wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE;
1182 if (wdata->state.exttype == WIIMOTE_EXT_NONE) {
1183 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1184 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1186 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1187 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1188 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1190 } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) {
1191 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1195 wiiproto_req_status(wdata);
1197 spin_unlock_irq(&wdata->state.lock);
1199 hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n",
1200 wdata->state.mp, wdata->state.exttype);
1205 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
1209 if (wdata->state.devtype == WIIMOTE_DEV_PENDING) {
1210 wiimote_init_detect(wdata);
1214 if (changed || !wiimote_init_check(wdata))
1215 wiimote_init_hotplug(wdata);
1218 kobject_uevent(&wdata->hdev->dev.kobj, KOBJ_CHANGE);
1221 void __wiimote_schedule(struct wiimote_data *wdata)
1223 if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING))
1224 schedule_work(&wdata->init_worker);
1227 static void wiimote_schedule(struct wiimote_data *wdata)
1231 spin_lock_irqsave(&wdata->state.lock, flags);
1232 __wiimote_schedule(wdata);
1233 spin_unlock_irqrestore(&wdata->state.lock, flags);
1238 struct wiimote_data *wdata = from_timer(wdata, t, timer);
1240 wiimote_schedule(wdata);
1245 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
1250 ops = wiimod_ext_table[wdata->state.exttype];
1252 ops->in_keys(wdata, payload);
1256 mods = wiimote_devtype_mods[wdata->state.devtype];
1260 ops->in_keys(wdata, payload);
1266 static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
1271 ops = wiimod_ext_table[wdata->state.exttype];
1273 ops->in_accel(wdata, payload);
1277 mods = wiimote_devtype_mods[wdata->state.devtype];
1281 ops->in_accel(wdata, payload);
1299 static void handler_ext(struct wiimote_data *wdata, const __u8 *payload,
1318 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1324 if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) {
1325 hid_dbg(wdata->hdev, "MP hotplug: 1\n");
1326 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1327 __wiimote_schedule(wdata);
1330 if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) {
1331 hid_dbg(wdata->hdev, "MP hotplug: 0\n");
1332 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1333 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1334 __wiimote_schedule(wdata);
1345 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp)
1349 ops = wiimod_ext_table[wdata->state.exttype];
1351 ops->in_mp(wdata, payload);
1354 ops->in_ext(wdata, payload);
1361 ops->in_mp(wdata, payload);
1364 ops->in_ext(wdata, payload);
1369 mods = wiimote_devtype_mods[wdata->state.devtype];
1373 ops->in_mp(wdata, payload);
1376 ops->in_ext(wdata, payload);
1382 #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
1383 #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
1384 #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
1385 #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
1387 static void handler_ir(struct wiimote_data *wdata, const __u8 *payload,
1393 ops = wiimod_ext_table[wdata->state.exttype];
1395 ops->in_ir(wdata, payload, packed, id);
1399 mods = wiimote_devtype_mods[wdata->state.devtype];
1403 ops->in_ir(wdata, payload, packed, id);
1410 static void handler_status_K(struct wiimote_data *wdata,
1413 handler_keys(wdata, payload);
1416 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1420 static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
1422 handler_status_K(wdata, payload);
1426 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) {
1427 hid_dbg(wdata->hdev, "EXT hotplug: 1\n");
1428 wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
1429 __wiimote_schedule(wdata);
1432 if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) {
1433 hid_dbg(wdata->hdev, "EXT hotplug: 0\n");
1434 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1435 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1436 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1437 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1438 __wiimote_schedule(wdata);
1442 wdata->state.cmd_battery = payload[5];
1443 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
1444 wiimote_cmd_complete(wdata);
1448 static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
1450 handler_keys(wdata, payload);
1453 static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
1459 handler_keys(wdata, payload);
1461 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
1464 else if (size > wdata->state.cmd_read_size)
1465 size = wdata->state.cmd_read_size;
1467 wdata->state.cmd_read_size = size;
1468 if (wdata->state.cmd_read_buf)
1469 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
1470 wiimote_cmd_complete(wdata);
1474 static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
1479 handler_keys(wdata, payload);
1481 if (wiimote_cmd_pending(wdata, cmd, 0)) {
1482 wdata->state.cmd_err = err;
1483 wiimote_cmd_complete(wdata);
1485 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
1490 static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
1492 handler_keys(wdata, payload);
1493 handler_accel(wdata, payload);
1496 static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
1498 handler_keys(wdata, payload);
1499 handler_ext(wdata, &payload[2], 8);
1502 static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
1504 handler_keys(wdata, payload);
1505 handler_accel(wdata, payload);
1506 ir_to_input0(wdata, &payload[5], false);
1507 ir_to_input1(wdata, &payload[8], false);
1508 ir_to_input2(wdata, &payload[11], false);
1509 ir_to_input3(wdata, &payload[14], false);
1512 static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
1514 handler_keys(wdata, payload);
1515 handler_ext(wdata, &payload[2], 19);
1518 static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
1520 handler_keys(wdata, payload);
1521 ir_to_input0(wdata, &payload[2], false);
1522 ir_to_input1(wdata, &payload[4], true);
1523 ir_to_input2(wdata, &payload[7], false);
1524 ir_to_input3(wdata, &payload[9], true);
1525 handler_ext(wdata, &payload[12], 9);
1528 static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
1530 handler_keys(wdata, payload);
1531 handler_accel(wdata, payload);
1532 handler_ext(wdata, &payload[5], 16);
1535 static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
1537 handler_keys(wdata, payload);
1538 handler_accel(wdata, payload);
1539 ir_to_input0(wdata, &payload[5], false);
1540 ir_to_input1(wdata, &payload[7], true);
1541 ir_to_input2(wdata, &payload[10], false);
1542 ir_to_input3(wdata, &payload[12], true);
1543 handler_ext(wdata, &payload[15], 6);
1546 static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
1548 handler_ext(wdata, payload, 21);
1551 static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
1553 handler_keys(wdata, payload);
1555 wdata->state.accel_split[0] = payload[2];
1556 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
1557 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
1559 ir_to_input0(wdata, &payload[3], false);
1560 ir_to_input1(wdata, &payload[12], false);
1563 static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
1567 handler_keys(wdata, payload);
1569 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
1570 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
1574 buf[2] = wdata->state.accel_split[0];
1576 buf[4] = wdata->state.accel_split[1];
1577 handler_accel(wdata, buf);
1579 ir_to_input2(wdata, &payload[3], false);
1580 ir_to_input3(wdata, &payload[12], false);
1586 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
1620 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1631 spin_lock_irqsave(&wdata->state.lock, flags);
1632 h->func(wdata, &raw_data[1]);
1633 spin_unlock_irqrestore(&wdata->state.lock, flags);
1649 struct wiimote_data *wdata = dev_to_wii(dev);
1653 spin_lock_irqsave(&wdata->state.lock, flags);
1654 type = wdata->state.exttype;
1655 spin_unlock_irqrestore(&wdata->state.lock, flags);
1682 struct wiimote_data *wdata = dev_to_wii(dev);
1685 wiimote_schedule(wdata);
1700 struct wiimote_data *wdata = dev_to_wii(dev);
1704 spin_lock_irqsave(&wdata->state.lock, flags);
1705 type = wdata->state.devtype;
1706 spin_unlock_irqrestore(&wdata->state.lock, flags);
1731 struct wiimote_data *wdata;
1733 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1734 if (!wdata)
1737 wdata->hdev = hdev;
1738 hid_set_drvdata(hdev, wdata);
1740 spin_lock_init(&wdata->queue.lock);
1741 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
1743 spin_lock_init(&wdata->state.lock);
1744 init_completion(&wdata->state.ready);
1745 mutex_init(&wdata->state.sync);
1746 wdata->state.drm = WIIPROTO_REQ_DRM_K;
1747 wdata->state.cmd_battery = 0xff;
1749 INIT_WORK(&wdata->init_worker, wiimote_init_worker);
1750 timer_setup(&wdata->timer, wiimote_init_timeout, 0);
1752 return wdata;
1755 static void wiimote_destroy(struct wiimote_data *wdata)
1759 wiidebug_deinit(wdata);
1762 spin_lock_irqsave(&wdata->state.lock, flags);
1763 wdata->state.flags |= WIIPROTO_FLAG_EXITING;
1764 spin_unlock_irqrestore(&wdata->state.lock, flags);
1766 cancel_work_sync(&wdata->init_worker);
1767 del_timer_sync(&wdata->timer);
1769 device_remove_file(&wdata->hdev->dev, &dev_attr_devtype);
1770 device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
1772 wiimote_mp_unload(wdata);
1773 wiimote_ext_unload(wdata);
1774 wiimote_modules_unload(wdata);
1775 cancel_work_sync(&wdata->queue.worker);
1776 hid_hw_close(wdata->hdev);
1777 hid_hw_stop(wdata->hdev);
1779 kfree(wdata);
1785 struct wiimote_data *wdata;
1790 wdata = wiimote_create(hdev);
1791 if (!wdata) {
1826 ret = wiidebug_init(wdata);
1833 wiimote_schedule(wdata);
1838 wiimote_destroy(wdata);
1842 device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
1848 input_free_device(wdata->ir);
1849 input_free_device(wdata->accel);
1850 kfree(wdata);
1856 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1859 wiimote_destroy(wdata);