^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * U2F Zero LED and RNG driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2018 Andrej Shadura <andrew@shadura.me>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Loosely based on drivers/hid/hid-led.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * and drivers/usb/misc/chaoskey.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * modify it under the terms of the GNU General Public License as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * published by the Free Software Foundation, version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/hidraw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "usbhid/usbhid.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define DRIVER_SHORT "u2fzero"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define HID_REPORT_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* We only use broadcast (CID-less) messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define CID_BROADCAST 0xffffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct u2f_hid_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u32 cid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u8 cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u8 bcnth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) u8 bcntl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u8 data[HID_REPORT_SIZE - 7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) } init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u8 seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u8 data[HID_REPORT_SIZE - 5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) } cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct u2f_hid_report {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u8 report_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct u2f_hid_msg msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define U2F_HID_MSG_LEN(f) (size_t)(((f).init.bcnth << 8) + (f).init.bcntl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* Custom extensions to the U2FHID protocol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define U2F_CUSTOM_GET_RNG 0x21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define U2F_CUSTOM_WINK 0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct u2fzero_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct hid_device *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct urb *urb; /* URB for the RNG data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct led_classdev ldev; /* Embedded struct for led */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct hwrng hwrng; /* Embedded struct for hwrng */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) char *led_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) char *rng_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) u8 *buf_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) u8 *buf_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) bool present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int u2fzero_send(struct u2fzero_device *dev, struct u2f_hid_report *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) memcpy(dev->buf_out, req, sizeof(struct u2f_hid_report));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ret = hid_hw_output_report(dev->hdev, dev->buf_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) sizeof(struct u2f_hid_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return ret == sizeof(struct u2f_hid_msg) ? 0 : -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct u2fzero_transfer_context {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct completion done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void u2fzero_read_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct u2fzero_transfer_context *ctx = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ctx->status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) complete(&ctx->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int u2fzero_recv(struct u2fzero_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct u2f_hid_report *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct u2f_hid_msg *resp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct hid_device *hdev = dev->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct u2fzero_transfer_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) memcpy(dev->buf_out, req, sizeof(struct u2f_hid_report));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) dev->urb->context = &ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) init_completion(&ctx.done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ret = usb_submit_urb(dev->urb, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (unlikely(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) hid_err(hdev, "usb_submit_urb failed: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ret = hid_hw_output_report(dev->hdev, dev->buf_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) sizeof(struct u2f_hid_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) hid_err(hdev, "hid_hw_output_report failed: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = (wait_for_completion_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) &ctx.done, msecs_to_jiffies(USB_CTRL_SET_TIMEOUT)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) usb_kill_urb(dev->urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) hid_err(hdev, "urb submission timed out");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ret = dev->urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) memcpy(resp, dev->buf_in, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int u2fzero_blink(struct led_classdev *ldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct u2fzero_device *dev = container_of(ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct u2fzero_device, ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct u2f_hid_report req = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .report_type = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .msg.cid = CID_BROADCAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .msg.init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .cmd = U2F_CUSTOM_WINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .bcnth = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .bcntl = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .data = {0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return u2fzero_send(dev, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int u2fzero_brightness_set(struct led_classdev *ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ldev->brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return u2fzero_blink(ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int u2fzero_rng_read(struct hwrng *rng, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) size_t max, bool wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct u2fzero_device *dev = container_of(rng,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct u2fzero_device, hwrng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct u2f_hid_report req = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .report_type = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .msg.cid = CID_BROADCAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .msg.init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .cmd = U2F_CUSTOM_GET_RNG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .bcnth = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .bcntl = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .data = {0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct u2f_hid_msg resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) size_t actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* valid packets must have a correct header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int min_length = offsetof(struct u2f_hid_msg, init.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (!dev->present) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) hid_dbg(dev->hdev, "device not present");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ret = u2fzero_recv(dev, &req, &resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /* ignore errors or packets without data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (ret < min_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /* only take the minimum amount of data it is safe to take */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) actual_length = min3((size_t)ret - min_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) U2F_HID_MSG_LEN(resp), max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) memcpy(data, resp.init.data, actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int u2fzero_init_led(struct u2fzero_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) unsigned int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) dev->led_name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) "%s%u", DRIVER_SHORT, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (dev->led_name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dev->ldev.name = dev->led_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dev->ldev.max_brightness = LED_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) dev->ldev.flags = LED_HW_PLUGGABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) dev->ldev.brightness_set_blocking = u2fzero_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return devm_led_classdev_register(&dev->hdev->dev, &dev->ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int u2fzero_init_hwrng(struct u2fzero_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) unsigned int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) dev->rng_name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) "%s-rng%u", DRIVER_SHORT, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (dev->rng_name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) dev->hwrng.name = dev->rng_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) dev->hwrng.read = u2fzero_rng_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dev->hwrng.quality = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return devm_hwrng_register(&dev->hdev->dev, &dev->hwrng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static int u2fzero_fill_in_urb(struct u2fzero_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct hid_device *hdev = dev->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct usbhid_device *usbhid = hdev->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) unsigned int pipe_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct usb_host_endpoint *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (dev->hdev->bus != BUS_USB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) udev = hid_to_usb_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (!usbhid->urbout || !usbhid->urbin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ep = usb_pipe_endpoint(udev, usbhid->urbin->pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dev->urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!dev->urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) pipe_in = (usbhid->urbin->pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) usb_fill_int_urb(dev->urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) pipe_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) dev->buf_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) HID_REPORT_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) u2fzero_read_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ep->desc.bInterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int u2fzero_probe(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct u2fzero_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) unsigned int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!hid_is_usb(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) dev->buf_out = devm_kmalloc(&hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) sizeof(struct u2f_hid_report), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (dev->buf_out == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) dev->buf_in = devm_kmalloc(&hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) sizeof(struct u2f_hid_msg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (dev->buf_in == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ret = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) dev->hdev = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) hid_set_drvdata(hdev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) mutex_init(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) u2fzero_fill_in_urb(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) dev->present = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) minor = ((struct hidraw *) hdev->hidraw)->minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) ret = u2fzero_init_led(dev, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) hid_info(hdev, "U2F Zero LED initialised\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ret = u2fzero_init_hwrng(dev, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) hid_info(hdev, "U2F Zero RNG initialised\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static void u2fzero_remove(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct u2fzero_device *dev = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) dev->present = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) usb_poison_urb(dev->urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) usb_free_urb(dev->urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static const struct hid_device_id u2fzero_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) USB_DEVICE_ID_U2F_ZERO) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) MODULE_DEVICE_TABLE(hid, u2fzero_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static struct hid_driver u2fzero_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) .name = "hid-" DRIVER_SHORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) .probe = u2fzero_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .remove = u2fzero_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) .id_table = u2fzero_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) module_hid_driver(u2fzero_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) MODULE_AUTHOR("Andrej Shadura <andrew@shadura.me>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) MODULE_DESCRIPTION("U2F Zero LED and RNG driver");