Lines Matching defs: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);
469 static int wiimote_cmd_init_mp(struct wiimote_data *wdata)
476 ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem));
482 ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem));
490 static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype)
510 return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem));
514 static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem)
519 ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6);
523 hid_dbg(wdata->hdev, "motion plus ID: %6phC\n", rmem);
528 hid_info(wdata->hdev, "unknown motion plus ID: %6phC\n", rmem);
534 static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata)
540 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
544 hid_dbg(wdata->hdev, "mapped motion plus ID: %6phC\n", rmem);
624 static void wiimote_modules_load(struct wiimote_data *wdata,
642 wdata->input = input_allocate_device();
643 if (!wdata->input)
646 input_set_drvdata(wdata->input, wdata);
647 wdata->input->dev.parent = &wdata->hdev->dev;
648 wdata->input->id.bustype = wdata->hdev->bus;
649 wdata->input->id.vendor = wdata->hdev->vendor;
650 wdata->input->id.product = wdata->hdev->product;
651 wdata->input->id.version = wdata->hdev->version;
652 wdata->input->name = WIIMOTE_NAME;
660 ret = ops->probe(ops, wdata);
665 if (wdata->input) {
666 ret = input_register_device(wdata->input);
671 spin_lock_irq(&wdata->state.lock);
672 wdata->state.devtype = devtype;
673 spin_unlock_irq(&wdata->state.lock);
680 ops->remove(ops, wdata);
683 if (wdata->input) {
684 input_free_device(wdata->input);
685 wdata->input = NULL;
689 static void wiimote_modules_unload(struct wiimote_data *wdata)
695 mods = wiimote_devtype_mods[wdata->state.devtype];
697 spin_lock_irqsave(&wdata->state.lock, flags);
698 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
699 spin_unlock_irqrestore(&wdata->state.lock, flags);
705 if (wdata->input) {
706 input_get_device(wdata->input);
707 input_unregister_device(wdata->input);
713 ops->remove(ops, wdata);
716 if (wdata->input) {
717 input_put_device(wdata->input);
718 wdata->input = NULL;
724 static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext)
733 ret = ops->probe(ops, wdata);
738 spin_lock_irqsave(&wdata->state.lock, flags);
739 wdata->state.exttype = ext;
740 spin_unlock_irqrestore(&wdata->state.lock, flags);
743 static void wiimote_ext_unload(struct wiimote_data *wdata)
748 ops = wiimod_ext_table[wdata->state.exttype];
750 spin_lock_irqsave(&wdata->state.lock, flags);
751 wdata->state.exttype = WIIMOTE_EXT_UNKNOWN;
752 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
753 spin_unlock_irqrestore(&wdata->state.lock, flags);
756 ops->remove(ops, wdata);
759 static void wiimote_mp_load(struct wiimote_data *wdata)
768 ret = ops->probe(ops, wdata);
773 spin_lock_irqsave(&wdata->state.lock, flags);
774 wdata->state.mp = mode;
775 spin_unlock_irqrestore(&wdata->state.lock, flags);
778 static void wiimote_mp_unload(struct wiimote_data *wdata)
783 if (wdata->state.mp < 2)
788 spin_lock_irqsave(&wdata->state.lock, flags);
789 wdata->state.mp = 0;
790 wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
791 spin_unlock_irqrestore(&wdata->state.lock, flags);
794 ops->remove(ops, wdata);
813 static void wiimote_init_set_type(struct wiimote_data *wdata,
820 vendor = wdata->hdev->vendor;
821 product = wdata->hdev->product;
822 name = wdata->hdev->name;
858 hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
861 hid_info(wdata->hdev, "detected device: %s\n",
864 wiimote_modules_load(wdata, devtype);
867 static void wiimote_init_detect(struct wiimote_data *wdata)
873 wiimote_cmd_acquire_noint(wdata);
875 spin_lock_irq(&wdata->state.lock);
876 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
877 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
878 wiiproto_req_status(wdata);
879 spin_unlock_irq(&wdata->state.lock);
881 ret = wiimote_cmd_wait_noint(wdata);
885 spin_lock_irq(&wdata->state.lock);
886 ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
887 spin_unlock_irq(&wdata->state.lock);
892 wiimote_cmd_init_ext(wdata);
893 exttype = wiimote_cmd_read_ext(wdata, extdata);
896 wiimote_cmd_release(wdata);
897 wiimote_init_set_type(wdata, exttype);
900 spin_lock_irq(&wdata->state.lock);
901 if (!(wdata->state.flags & WIIPROTO_FLAG_BUILTIN_MP) &&
902 !(wdata->state.flags & WIIPROTO_FLAG_NO_MP))
903 mod_timer(&wdata->timer, jiffies + HZ * 4);
904 spin_unlock_irq(&wdata->state.lock);
913 static void wiimote_init_poll_mp(struct wiimote_data *wdata)
918 wiimote_cmd_acquire_noint(wdata);
919 wiimote_cmd_init_mp(wdata);
920 mp = wiimote_cmd_read_mp(wdata, mpdata);
921 wiimote_cmd_release(wdata);
925 if (!wdata->state.mp) {
926 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
927 wiimote_mp_load(wdata);
929 } else if (wdata->state.mp) {
930 wiimote_mp_unload(wdata);
933 mod_timer(&wdata->timer, jiffies + HZ * 4);
946 static bool wiimote_init_check(struct wiimote_data *wdata)
952 spin_lock_irq(&wdata->state.lock);
953 flags = wdata->state.flags;
954 spin_unlock_irq(&wdata->state.lock);
956 wiimote_cmd_acquire_noint(wdata);
965 if (wdata->state.exttype == WIIMOTE_EXT_NONE &&
966 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
967 type = wiimote_cmd_read_mp_mapped(wdata);
970 spin_lock_irq(&wdata->state.lock);
971 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
972 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED);
973 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
974 spin_unlock_irq(&wdata->state.lock);
977 hid_dbg(wdata->hdev, "state left: !EXT && MP\n");
991 wdata->state.exttype != WIIMOTE_EXT_NONE) {
992 type = wiimote_cmd_read_ext(wdata, data);
993 ret = type == wdata->state.exttype;
995 spin_lock_irq(&wdata->state.lock);
996 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
997 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
998 spin_unlock_irq(&wdata->state.lock);
1001 hid_dbg(wdata->hdev, "state left: EXT && !MP\n");
1017 wdata->state.exttype == WIIMOTE_EXT_NONE) {
1018 type = wiimote_cmd_read_ext(wdata, data);
1019 ret = type == wdata->state.exttype;
1021 spin_lock_irq(&wdata->state.lock);
1022 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1023 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1024 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1025 spin_unlock_irq(&wdata->state.lock);
1028 hid_dbg(wdata->hdev, "state left: !EXT && !MP\n");
1044 if (wdata->state.exttype != WIIMOTE_EXT_NONE &&
1045 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
1046 type = wiimote_cmd_read_mp_mapped(wdata);
1051 spin_lock_irq(&wdata->state.lock);
1052 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1053 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1054 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1055 spin_unlock_irq(&wdata->state.lock);
1058 hid_dbg(wdata->hdev, "state left: EXT && MP\n");
1070 wiimote_cmd_release(wdata);
1075 wiimote_init_poll_mp(wdata);
1100 static void wiimote_init_hotplug(struct wiimote_data *wdata)
1106 hid_dbg(wdata->hdev, "detect extensions..\n");
1108 wiimote_cmd_acquire_noint(wdata);
1110 spin_lock_irq(&wdata->state.lock);
1113 flags = wdata->state.flags;
1116 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1117 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1119 spin_unlock_irq(&wdata->state.lock);
1122 wiimote_cmd_init_ext(wdata);
1126 wiimote_cmd_init_mp(wdata);
1127 mp = wiimote_cmd_read_mp(wdata, mpdata);
1129 exttype = wiimote_cmd_read_ext(wdata, extdata);
1131 wiimote_cmd_release(wdata);
1134 if (exttype != wdata->state.exttype) {
1136 wiimote_ext_unload(wdata);
1139 hid_info(wdata->hdev, "cannot detect extension; %6phC\n",
1142 spin_lock_irq(&wdata->state.lock);
1143 wdata->state.exttype = WIIMOTE_EXT_NONE;
1144 spin_unlock_irq(&wdata->state.lock);
1146 hid_info(wdata->hdev, "detected extension: %s\n",
1149 wiimote_ext_load(wdata, exttype);
1155 if (!wdata->state.mp) {
1156 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
1157 wiimote_mp_load(wdata);
1159 } else if (wdata->state.mp) {
1160 wiimote_mp_unload(wdata);
1169 wiimote_cmd_acquire_noint(wdata);
1170 wiimote_cmd_map_mp(wdata, exttype);
1171 wiimote_cmd_release(wdata);
1174 del_timer_sync(&wdata->timer);
1179 mod_timer(&wdata->timer, jiffies + HZ * 4);
1182 spin_lock_irq(&wdata->state.lock);
1186 wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE;
1187 if (wdata->state.exttype == WIIMOTE_EXT_NONE) {
1188 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1189 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1191 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1192 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1193 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1195 } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) {
1196 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1200 wiiproto_req_status(wdata);
1202 spin_unlock_irq(&wdata->state.lock);
1204 hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n",
1205 wdata->state.mp, wdata->state.exttype);
1210 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
1214 if (wdata->state.devtype == WIIMOTE_DEV_PENDING) {
1215 wiimote_init_detect(wdata);
1219 if (changed || !wiimote_init_check(wdata))
1220 wiimote_init_hotplug(wdata);
1223 kobject_uevent(&wdata->hdev->dev.kobj, KOBJ_CHANGE);
1226 void __wiimote_schedule(struct wiimote_data *wdata)
1228 if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING))
1229 schedule_work(&wdata->init_worker);
1232 static void wiimote_schedule(struct wiimote_data *wdata)
1236 spin_lock_irqsave(&wdata->state.lock, flags);
1237 __wiimote_schedule(wdata);
1238 spin_unlock_irqrestore(&wdata->state.lock, flags);
1243 struct wiimote_data *wdata = from_timer(wdata, t, timer);
1245 wiimote_schedule(wdata);
1250 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
1255 ops = wiimod_ext_table[wdata->state.exttype];
1257 ops->in_keys(wdata, payload);
1261 mods = wiimote_devtype_mods[wdata->state.devtype];
1265 ops->in_keys(wdata, payload);
1271 static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
1276 ops = wiimod_ext_table[wdata->state.exttype];
1278 ops->in_accel(wdata, payload);
1282 mods = wiimote_devtype_mods[wdata->state.devtype];
1286 ops->in_accel(wdata, payload);
1304 static void handler_ext(struct wiimote_data *wdata, const __u8 *payload,
1323 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1329 if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) {
1330 hid_dbg(wdata->hdev, "MP hotplug: 1\n");
1331 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1332 __wiimote_schedule(wdata);
1335 if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) {
1336 hid_dbg(wdata->hdev, "MP hotplug: 0\n");
1337 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1338 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1339 __wiimote_schedule(wdata);
1350 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp)
1354 ops = wiimod_ext_table[wdata->state.exttype];
1356 ops->in_mp(wdata, payload);
1359 ops->in_ext(wdata, payload);
1366 ops->in_mp(wdata, payload);
1369 ops->in_ext(wdata, payload);
1374 mods = wiimote_devtype_mods[wdata->state.devtype];
1378 ops->in_mp(wdata, payload);
1381 ops->in_ext(wdata, payload);
1387 #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
1388 #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
1389 #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
1390 #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
1392 static void handler_ir(struct wiimote_data *wdata, const __u8 *payload,
1398 ops = wiimod_ext_table[wdata->state.exttype];
1400 ops->in_ir(wdata, payload, packed, id);
1404 mods = wiimote_devtype_mods[wdata->state.devtype];
1408 ops->in_ir(wdata, payload, packed, id);
1415 static void handler_status_K(struct wiimote_data *wdata,
1418 handler_keys(wdata, payload);
1421 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1425 static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
1427 handler_status_K(wdata, payload);
1431 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) {
1432 hid_dbg(wdata->hdev, "EXT hotplug: 1\n");
1433 wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
1434 __wiimote_schedule(wdata);
1437 if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) {
1438 hid_dbg(wdata->hdev, "EXT hotplug: 0\n");
1439 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1440 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1441 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1442 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1443 __wiimote_schedule(wdata);
1447 wdata->state.cmd_battery = payload[5];
1448 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
1449 wiimote_cmd_complete(wdata);
1453 static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
1455 handler_keys(wdata, payload);
1458 static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
1464 handler_keys(wdata, payload);
1466 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
1469 else if (size > wdata->state.cmd_read_size)
1470 size = wdata->state.cmd_read_size;
1472 wdata->state.cmd_read_size = size;
1473 if (wdata->state.cmd_read_buf)
1474 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
1475 wiimote_cmd_complete(wdata);
1479 static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
1484 handler_keys(wdata, payload);
1486 if (wiimote_cmd_pending(wdata, cmd, 0)) {
1487 wdata->state.cmd_err = err;
1488 wiimote_cmd_complete(wdata);
1490 hid_warn(wdata->hdev, "Remote error %u on req %u\n", err,
1495 static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
1497 handler_keys(wdata, payload);
1498 handler_accel(wdata, payload);
1501 static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
1503 handler_keys(wdata, payload);
1504 handler_ext(wdata, &payload[2], 8);
1507 static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
1509 handler_keys(wdata, payload);
1510 handler_accel(wdata, payload);
1511 ir_to_input0(wdata, &payload[5], false);
1512 ir_to_input1(wdata, &payload[8], false);
1513 ir_to_input2(wdata, &payload[11], false);
1514 ir_to_input3(wdata, &payload[14], false);
1517 static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
1519 handler_keys(wdata, payload);
1520 handler_ext(wdata, &payload[2], 19);
1523 static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
1525 handler_keys(wdata, payload);
1526 ir_to_input0(wdata, &payload[2], false);
1527 ir_to_input1(wdata, &payload[4], true);
1528 ir_to_input2(wdata, &payload[7], false);
1529 ir_to_input3(wdata, &payload[9], true);
1530 handler_ext(wdata, &payload[12], 9);
1533 static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
1535 handler_keys(wdata, payload);
1536 handler_accel(wdata, payload);
1537 handler_ext(wdata, &payload[5], 16);
1540 static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
1542 handler_keys(wdata, payload);
1543 handler_accel(wdata, payload);
1544 ir_to_input0(wdata, &payload[5], false);
1545 ir_to_input1(wdata, &payload[7], true);
1546 ir_to_input2(wdata, &payload[10], false);
1547 ir_to_input3(wdata, &payload[12], true);
1548 handler_ext(wdata, &payload[15], 6);
1551 static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
1553 handler_ext(wdata, payload, 21);
1556 static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
1558 handler_keys(wdata, payload);
1560 wdata->state.accel_split[0] = payload[2];
1561 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
1562 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
1564 ir_to_input0(wdata, &payload[3], false);
1565 ir_to_input1(wdata, &payload[12], false);
1568 static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
1572 handler_keys(wdata, payload);
1574 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
1575 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
1579 buf[2] = wdata->state.accel_split[0];
1581 buf[4] = wdata->state.accel_split[1];
1582 handler_accel(wdata, buf);
1584 ir_to_input2(wdata, &payload[3], false);
1585 ir_to_input3(wdata, &payload[12], false);
1591 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
1625 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1636 spin_lock_irqsave(&wdata->state.lock, flags);
1637 h->func(wdata, &raw_data[1]);
1638 spin_unlock_irqrestore(&wdata->state.lock, flags);
1654 struct wiimote_data *wdata = dev_to_wii(dev);
1658 spin_lock_irqsave(&wdata->state.lock, flags);
1659 type = wdata->state.exttype;
1660 spin_unlock_irqrestore(&wdata->state.lock, flags);
1689 struct wiimote_data *wdata = dev_to_wii(dev);
1692 wiimote_schedule(wdata);
1707 struct wiimote_data *wdata = dev_to_wii(dev);
1711 spin_lock_irqsave(&wdata->state.lock, flags);
1712 type = wdata->state.devtype;
1713 spin_unlock_irqrestore(&wdata->state.lock, flags);
1738 struct wiimote_data *wdata;
1740 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1741 if (!wdata)
1744 wdata->hdev = hdev;
1745 hid_set_drvdata(hdev, wdata);
1747 spin_lock_init(&wdata->queue.lock);
1748 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
1750 spin_lock_init(&wdata->state.lock);
1751 init_completion(&wdata->state.ready);
1752 mutex_init(&wdata->state.sync);
1753 wdata->state.drm = WIIPROTO_REQ_DRM_K;
1754 wdata->state.cmd_battery = 0xff;
1756 INIT_WORK(&wdata->init_worker, wiimote_init_worker);
1757 timer_setup(&wdata->timer, wiimote_init_timeout, 0);
1759 return wdata;
1762 static void wiimote_destroy(struct wiimote_data *wdata)
1766 wiidebug_deinit(wdata);
1769 spin_lock_irqsave(&wdata->state.lock, flags);
1770 wdata->state.flags |= WIIPROTO_FLAG_EXITING;
1771 spin_unlock_irqrestore(&wdata->state.lock, flags);
1773 cancel_work_sync(&wdata->init_worker);
1774 timer_shutdown_sync(&wdata->timer);
1776 device_remove_file(&wdata->hdev->dev, &dev_attr_devtype);
1777 device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
1779 wiimote_mp_unload(wdata);
1780 wiimote_ext_unload(wdata);
1781 wiimote_modules_unload(wdata);
1782 cancel_work_sync(&wdata->queue.worker);
1783 hid_hw_close(wdata->hdev);
1784 hid_hw_stop(wdata->hdev);
1786 kfree(wdata);
1792 struct wiimote_data *wdata;
1797 wdata = wiimote_create(hdev);
1798 if (!wdata) {
1833 ret = wiidebug_init(wdata);
1840 wiimote_schedule(wdata);
1845 wiimote_destroy(wdata);
1849 device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
1855 input_free_device(wdata->ir);
1856 input_free_device(wdata->accel);
1857 kfree(wdata);
1863 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1866 wiimote_destroy(wdata);