From patchwork Sat Apr 13 10:47:45 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Herrmann X-Patchwork-Id: 2440311 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id A89BD3FD1A for ; Sat, 13 Apr 2013 10:48:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752703Ab3DMKsd (ORCPT ); Sat, 13 Apr 2013 06:48:33 -0400 Received: from mail-ea0-f176.google.com ([209.85.215.176]:53069 "EHLO mail-ea0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754048Ab3DMKsd (ORCPT ); Sat, 13 Apr 2013 06:48:33 -0400 Received: by mail-ea0-f176.google.com with SMTP id h10so1587342eaj.35 for ; Sat, 13 Apr 2013 03:48:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=LGm6Fu+606hrWHYkAMWrHc3G3mum5oTEyQAiHalysRw=; b=b6DCNSAdVkv1Ka5FqGXRaHUeRqTVrwvUrvcx68cM4Agj7b3HLdFaouobWGMBusC7AQ oBx1xh+amFEQv+DRkbkMfPuPbzVQiLbks776Df3FTAEDZqAUeRw0WYY53HYAZm2Xt8Yl 4vrK949p/n6Gyrd7UHfvvSbO/oUmJmqo1lIYrTxg/2miVVIfAfQfPLZYcL3+3k6dQUj1 Xl4+2Be/T537LZBASZHbJQzHVi/LaM/uwTtMh7ARZkK6EGwMrG404Xiw5KTOg7f356Dp ceXlhyJKH+70hSl9opRWUdqDyuHTczfW0xMtDANYk44ehph7WmEaJkPdjratCbyJJi5G ERCw== X-Received: by 10.14.223.137 with SMTP id v9mr8705866eep.4.1365850111624; Sat, 13 Apr 2013 03:48:31 -0700 (PDT) Received: from localhost.localdomain (stgt-5f71827d.pool.mediaWays.net. [95.113.130.125]) by mx.google.com with ESMTPS id t4sm15859796eel.0.2013.04.13.03.48.29 (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 13 Apr 2013 03:48:30 -0700 (PDT) From: David Herrmann To: linux-input@vger.kernel.org Cc: Jiri Kosina , David Herrmann Subject: [PATCH 04/21] HID: wiimote: add device detection Date: Sat, 13 Apr 2013 12:47:45 +0200 Message-Id: <1365850082-3585-5-git-send-email-dh.herrmann@gmail.com> X-Mailer: git-send-email 1.8.2.1 In-Reply-To: <1365850082-3585-1-git-send-email-dh.herrmann@gmail.com> References: <1365850082-3585-1-git-send-email-dh.herrmann@gmail.com> Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Nintendo produced many different devices that are internally based on the Wii Remote protocol but provide different peripherals. To support these devices, we need to schedule a device detection during initialization. Device detection includes requesting a status report, reading extension information and then evaluating which device we may be dealing with. We currently detect gen1 and gen2 Wii Remote devices. All other devices are marked as generic devices. More detections will be added later. In followup patches we will be using these device IDs to control which peripherals to initialize. For instance if a device is known to have no IR camera, there is no need to provide the IR input device nor trying to access IR registers. In fact, there are 3rd party devices that break if we try things like this (hurray!). The init_worker will be scheduled whenever we get hotplug events. This isn't implemented, yet and will be added later. However, we need to make sure that this worker can be called multiple times. Signed-off-by: David Herrmann --- drivers/hid/hid-wiimote-core.c | 153 ++++++++++++++++++++++++++++++++++++++++- drivers/hid/hid-wiimote.h | 37 +++++++++- 2 files changed, 188 insertions(+), 2 deletions(-) diff --git a/drivers/hid/hid-wiimote-core.c b/drivers/hid/hid-wiimote-core.c index 02656a8..2d56a08 100644 --- a/drivers/hid/hid-wiimote-core.c +++ b/drivers/hid/hid-wiimote-core.c @@ -399,6 +399,45 @@ ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem, return ret; } +/* requires the cmd-mutex to be held */ +static int wiimote_cmd_init_ext(struct wiimote_data *wdata) +{ + __u8 wmem; + int ret; + + /* initialize extension */ + wmem = 0x55; + ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem)); + if (ret) + return ret; + + /* disable default encryption */ + wmem = 0x0; + ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem)); + if (ret) + return ret; + + return 0; +} + +/* requires the cmd-mutex to be held */ +static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata) +{ + __u8 rmem[6]; + int ret; + + /* read extension ID */ + ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6); + if (ret != 6) + return WIIMOTE_EXT_NONE; + + if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff && + rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff) + return WIIMOTE_EXT_NONE; + + return WIIMOTE_EXT_UNKNOWN; +} + static int wiimote_battery_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) @@ -662,6 +701,105 @@ static void wiimote_ir_close(struct input_dev *dev) wiimote_init_ir(wdata, 0); } +/* device (re-)initialization and detection */ + +static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = { + [WIIMOTE_DEV_PENDING] = "Pending", + [WIIMOTE_DEV_UNKNOWN] = "Unknown", + [WIIMOTE_DEV_GENERIC] = "Generic", + [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)", + [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)", +}; + +/* Try to guess the device type based on all collected information. We + * first try to detect by static extension types, then VID/PID and the + * device name. If we cannot detect the device, we use + * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */ +static void wiimote_init_set_type(struct wiimote_data *wdata, + __u8 exttype) +{ + __u8 devtype = WIIMOTE_DEV_GENERIC; + __u16 vendor, product; + const char *name; + + vendor = wdata->hdev->vendor; + product = wdata->hdev->product; + name = wdata->hdev->name; + + if (vendor == USB_VENDOR_ID_NINTENDO) { + if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) { + devtype = WIIMOTE_DEV_GEN10; + goto done; + } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) { + devtype = WIIMOTE_DEV_GEN20; + goto done; + } + } + + if (!strcmp(name, "Nintendo RVL-CNT-01")) { + devtype = WIIMOTE_DEV_GEN10; + goto done; + } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) { + devtype = WIIMOTE_DEV_GEN20; + goto done; + } + +done: + if (devtype == WIIMOTE_DEV_GENERIC) + hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n", + name, vendor, product, exttype); + else + hid_info(wdata->hdev, "detected device: %s\n", + wiimote_devtype_names[devtype]); + + spin_lock_irq(&wdata->state.lock); + wdata->state.devtype = devtype; + spin_unlock_irq(&wdata->state.lock); +} + +static void wiimote_init_detect(struct wiimote_data *wdata) +{ + __u8 exttype = WIIMOTE_EXT_NONE; + bool ext; + int ret; + + wiimote_cmd_acquire_noint(wdata); + + spin_lock_irq(&wdata->state.lock); + wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0); + wiiproto_req_status(wdata); + spin_unlock_irq(&wdata->state.lock); + + ret = wiimote_cmd_wait_noint(wdata); + if (ret) + goto out_release; + + spin_lock_irq(&wdata->state.lock); + ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED; + spin_unlock_irq(&wdata->state.lock); + + if (!ext) + goto out_release; + + wiimote_cmd_init_ext(wdata); + exttype = wiimote_cmd_read_ext(wdata); + +out_release: + wiimote_cmd_release(wdata); + wiimote_init_set_type(wdata, exttype); +} + +static void wiimote_init_worker(struct work_struct *work) +{ + struct wiimote_data *wdata = container_of(work, struct wiimote_data, + init_worker); + + if (wdata->state.devtype == WIIMOTE_DEV_PENDING) + wiimote_init_detect(wdata); +} + +/* protocol handlers */ + static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) { input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], @@ -776,7 +914,14 @@ static void handler_status(struct wiimote_data *wdata, const __u8 *payload) { handler_status_K(wdata, payload); - wiiext_event(wdata, payload[2] & 0x02); + /* update extension status */ + if (payload[2] & 0x02) { + wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED; + wiiext_event(wdata, true); + } else { + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED; + wiiext_event(wdata, false); + } if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) { wdata->state.cmd_battery = payload[5]; @@ -1135,6 +1280,8 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev) mutex_init(&wdata->state.sync); wdata->state.drm = WIIPROTO_REQ_DRM_K; + INIT_WORK(&wdata->init_worker, wiimote_init_worker); + return wdata; err_ir: @@ -1157,6 +1304,7 @@ static void wiimote_destroy(struct wiimote_data *wdata) input_unregister_device(wdata->accel); input_unregister_device(wdata->ir); input_unregister_device(wdata->input); + cancel_work_sync(&wdata->init_worker); cancel_work_sync(&wdata->queue.worker); hid_hw_close(wdata->hdev); hid_hw_stop(wdata->hdev); @@ -1253,6 +1401,9 @@ static int wiimote_hid_probe(struct hid_device *hdev, wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); spin_unlock_irq(&wdata->state.lock); + /* schedule device detection */ + schedule_work(&wdata->init_worker); + return 0; err_free: diff --git a/drivers/hid/hid-wiimote.h b/drivers/hid/hid-wiimote.h index 2700d47..a3a449a 100644 --- a/drivers/hid/hid-wiimote.h +++ b/drivers/hid/hid-wiimote.h @@ -35,6 +35,8 @@ #define WIIPROTO_FLAG_IR_BASIC 0x40 #define WIIPROTO_FLAG_IR_EXT 0x80 #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */ +#define WIIPROTO_FLAG_EXT_PLUGGED 0x0100 + #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \ @@ -43,6 +45,21 @@ /* return flag for led \num */ #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) +enum wiimote_devtype { + WIIMOTE_DEV_PENDING, + WIIMOTE_DEV_UNKNOWN, + WIIMOTE_DEV_GENERIC, + WIIMOTE_DEV_GEN10, + WIIMOTE_DEV_GEN20, + WIIMOTE_DEV_NUM, +}; + +enum wiimote_exttype { + WIIMOTE_EXT_NONE, + WIIMOTE_EXT_UNKNOWN, + WIIMOTE_EXT_NUM, +}; + struct wiimote_buf { __u8 data[HID_MAX_BUFFER_SIZE]; size_t size; @@ -58,9 +75,10 @@ struct wiimote_queue { struct wiimote_state { spinlock_t lock; - __u8 flags; + __u16 flags; __u8 accel_split[2]; __u8 drm; + __u8 devtype; /* synchronous cmd requests */ struct mutex sync; @@ -87,6 +105,7 @@ struct wiimote_data { struct wiimote_queue queue; struct wiimote_state state; + struct work_struct init_worker; }; enum wiiproto_reqs { @@ -181,6 +200,11 @@ static inline int wiimote_cmd_acquire(struct wiimote_data *wdata) return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0; } +static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata) +{ + mutex_lock(&wdata->state.sync); +} + /* requires the state.lock spinlock to be held */ static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd, __u32 opt) @@ -208,4 +232,15 @@ static inline int wiimote_cmd_wait(struct wiimote_data *wdata) return 0; } +static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata) +{ + unsigned long ret; + + ret = wait_for_completion_timeout(&wdata->state.ready, HZ); + if (!ret) + return -EIO; + else + return 0; +} + #endif