Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  HID driver for Lenovo:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *  - ThinkPad USB Keyboard with TrackPoint (tpkbd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  - ThinkPad Compact Bluetooth Keyboard with TrackPoint (cptkbd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  - ThinkPad Compact USB Keyboard with TrackPoint (cptkbd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *  Copyright (c) 2012 Bernhard Seibold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  Copyright (c) 2014 Jamie Lentin <jm@lentin.co.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * Linux IBM/Lenovo Scrollpoint mouse driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * - IBM Scrollpoint III
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * - IBM Scrollpoint Pro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * - IBM Scrollpoint Optical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * - IBM Scrollpoint Optical 800dpi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * - IBM Scrollpoint Optical 800dpi Pro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * - Lenovo Scrollpoint Optical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *  Copyright (c) 2012 Peter De Wachter <pdewacht@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *  Copyright (c) 2018 Peter Ganzhorn <peter.ganzhorn@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) /* Userspace expects F20 for mic-mute KEY_MICMUTE does not work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define LENOVO_KEY_MICMUTE KEY_F20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) struct lenovo_drvdata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	u8 led_report[3]; /* Must be first for proper alignment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	int led_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	struct mutex led_report_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	struct led_classdev led_mute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	struct led_classdev led_micmute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	struct work_struct fn_lock_sync_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	struct hid_device *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	int press_to_select;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	int dragging;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	int release_to_select;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	int select_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	int sensitivity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	int press_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	u8 middlebutton_state; /* 0:Up, 1:Down (undecided), 2:Scrolling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	bool fn_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define TP10UBKBD_LED_OUTPUT_REPORT	9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define TP10UBKBD_FN_LOCK_LED		0x54
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define TP10UBKBD_MUTE_LED		0x64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define TP10UBKBD_MICMUTE_LED		0x74
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define TP10UBKBD_LED_OFF		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define TP10UBKBD_LED_ON		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 				    enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	struct lenovo_drvdata *data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	mutex_lock(&data->led_report_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	data->led_report[0] = TP10UBKBD_LED_OUTPUT_REPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	data->led_report[1] = led_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	data->led_report[2] = value ? TP10UBKBD_LED_ON : TP10UBKBD_LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	ret = hid_hw_raw_request(hdev, data->led_report[0], data->led_report, 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 				 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	if (ret != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		if (ret != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			hid_err(hdev, "Set LED output report error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		ret = ret < 0 ? ret : -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	mutex_unlock(&data->led_report_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) static void lenovo_tp10ubkbd_sync_fn_lock(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct lenovo_drvdata *data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		container_of(work, struct lenovo_drvdata, fn_lock_sync_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	lenovo_led_set_tp10ubkbd(data->hdev, TP10UBKBD_FN_LOCK_LED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 				 data->fn_lock);
^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 const __u8 lenovo_pro_dock_need_fixup_collection[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	0x05, 0x88,		/* Usage Page (Vendor Usage Page 0x88)	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	0x09, 0x01,		/* Usage (Vendor Usage 0x01)		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	0xa1, 0x01,		/* Collection (Application)		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	0x85, 0x04,		/*  Report ID (4)			*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	0x19, 0x00,		/*  Usage Minimum (0)			*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	0x2a, 0xff, 0xff,	/*  Usage Maximum (65535)		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) static __u8 *lenovo_report_fixup(struct hid_device *hdev, __u8 *rdesc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		unsigned int *rsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	case USB_DEVICE_ID_LENOVO_TPPRODOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		/* the fixups that need to be done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		 *   - get a reasonable usage max for the vendor collection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		 *     0x8801 from the report ID 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		if (*rsize >= 153 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		    memcmp(&rdesc[140], lenovo_pro_dock_need_fixup_collection,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 			  sizeof(lenovo_pro_dock_need_fixup_collection)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 			rdesc[151] = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 			rdesc[152] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	return rdesc;
^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) static int lenovo_input_mapping_tpkbd(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		struct hid_input *hi, struct hid_field *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		struct hid_usage *usage, unsigned long **bit, int *max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	if (usage->hid == (HID_UP_BUTTON | 0x0010)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		/* This sub-device contains trackpoint, mark it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		hid_set_drvdata(hdev, (void *)1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		map_key_clear(LENOVO_KEY_MICMUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) static int lenovo_input_mapping_cptkbd(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		struct hid_input *hi, struct hid_field *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		struct hid_usage *usage, unsigned long **bit, int *max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	/* HID_UP_LNVENDOR = USB, HID_UP_MSVENDOR = BT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	    (usage->hid & HID_USAGE_PAGE) == HID_UP_LNVENDOR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		switch (usage->hid & HID_USAGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		case 0x00f1: /* Fn-F4: Mic mute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 			map_key_clear(LENOVO_KEY_MICMUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		case 0x00f2: /* Fn-F5: Brightness down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 			map_key_clear(KEY_BRIGHTNESSDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		case 0x00f3: /* Fn-F6: Brightness up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			map_key_clear(KEY_BRIGHTNESSUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		case 0x00f4: /* Fn-F7: External display (projector) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 			map_key_clear(KEY_SWITCHVIDEOMODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		case 0x00f5: /* Fn-F8: Wireless */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			map_key_clear(KEY_WLAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		case 0x00f6: /* Fn-F9: Control panel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			map_key_clear(KEY_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		case 0x00f8: /* Fn-F11: View open applications (3 boxes) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 			map_key_clear(KEY_SCALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		case 0x00f9: /* Fn-F12: Open My computer (6 boxes) USB-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			/* NB: This mapping is invented in raw_event below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 			map_key_clear(KEY_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		case 0x00fa: /* Fn-Esc: Fn-lock toggle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			map_key_clear(KEY_FN_ESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		case 0x00fb: /* Middle mouse button (in native mode) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 			map_key_clear(BTN_MIDDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	/* Compatibility middle/wheel mappings should be ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	if (usage->hid == HID_GD_WHEEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 			(usage->hid & HID_USAGE) == 0x003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 			(usage->hid & HID_USAGE) == 0x238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	/* Map wheel emulation reports: 0xffa1 = USB, 0xff10 = BT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if ((usage->hid & HID_USAGE_PAGE) == 0xff100000 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	    (usage->hid & HID_USAGE_PAGE) == 0xffa10000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		field->flags |= HID_MAIN_ITEM_RELATIVE | HID_MAIN_ITEM_VARIABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		field->logical_minimum = -127;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		field->logical_maximum = 127;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		switch (usage->hid & HID_USAGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		case 0x0000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		case 0x0001:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			hid_map_usage(hi, usage, bit, max, EV_REL, REL_WHEEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		}
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) static int lenovo_input_mapping_scrollpoint(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		struct hid_input *hi, struct hid_field *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		struct hid_usage *usage, unsigned long **bit, int *max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (usage->hid == HID_GD_Z) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		hid_map_usage(hi, usage, bit, max, EV_REL, REL_HWHEEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) static int lenovo_input_mapping_tp10_ultrabook_kbd(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		struct hid_input *hi, struct hid_field *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		struct hid_usage *usage, unsigned long **bit, int *max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	 * The ThinkPad 10 Ultrabook Keyboard uses 0x000c0001 usage for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	 * a bunch of keys which have no standard consumer page code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	if (usage->hid == 0x000c0001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		switch (usage->usage_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		case 8: /* Fn-Esc: Fn-lock toggle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 			map_key_clear(KEY_FN_ESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		case 9: /* Fn-F4: Mic mute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			map_key_clear(LENOVO_KEY_MICMUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		case 10: /* Fn-F7: Control panel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			map_key_clear(KEY_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		case 11: /* Fn-F8: Search (magnifier glass) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			map_key_clear(KEY_SEARCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		case 12: /* Fn-F10: Open My computer (6 boxes) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			map_key_clear(KEY_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	 * The Ultrabook Keyboard sends a spurious F23 key-press when resuming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	 * from suspend and it does not actually have a F23 key, ignore it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	if (usage->hid == 0x00070072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) static int lenovo_input_mapping(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		struct hid_input *hi, struct hid_field *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		struct hid_usage *usage, unsigned long **bit, int *max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	case USB_DEVICE_ID_LENOVO_TPKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		return lenovo_input_mapping_tpkbd(hdev, hi, field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 							usage, bit, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	case USB_DEVICE_ID_LENOVO_CUSBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	case USB_DEVICE_ID_LENOVO_CBTKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		return lenovo_input_mapping_cptkbd(hdev, hi, field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 							usage, bit, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	case USB_DEVICE_ID_IBM_SCROLLPOINT_III:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	case USB_DEVICE_ID_IBM_SCROLLPOINT_PRO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	case USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	case USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	case USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		return lenovo_input_mapping_scrollpoint(hdev, hi, field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 							usage, bit, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 							       usage, bit, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) #undef map_key_clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) /* Send a config command to the keyboard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) static int lenovo_send_cmd_cptkbd(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			unsigned char byte2, unsigned char byte3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	buf = kzalloc(3, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (!buf)
^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) 	buf[0] = 0x18;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	buf[1] = byte2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	buf[2] = byte3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	case USB_DEVICE_ID_LENOVO_CUSBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		ret = hid_hw_raw_request(hdev, 0x13, buf, 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 					HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	case USB_DEVICE_ID_LENOVO_CBTKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		ret = hid_hw_output_report(hdev, buf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	return ret < 0 ? ret : 0; /* BT returns 0, USB returns sizeof(buf) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) static void lenovo_features_set_cptkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	ret = lenovo_send_cmd_cptkbd(hdev, 0x05, cptkbd_data->fn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		hid_err(hdev, "Fn-lock setting failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	ret = lenovo_send_cmd_cptkbd(hdev, 0x02, cptkbd_data->sensitivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		hid_err(hdev, "Sensitivity setting failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) static ssize_t attr_fn_lock_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	struct lenovo_drvdata *data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	return snprintf(buf, PAGE_SIZE, "%u\n", data->fn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) static ssize_t attr_fn_lock_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	struct lenovo_drvdata *data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	int value, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	if (kstrtoint(buf, 10, &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	if (value < 0 || value > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	data->fn_lock = !!value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	case USB_DEVICE_ID_LENOVO_CUSBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	case USB_DEVICE_ID_LENOVO_CBTKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		lenovo_features_set_cptkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) static ssize_t attr_sensitivity_show_cptkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	return snprintf(buf, PAGE_SIZE, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		cptkbd_data->sensitivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) static ssize_t attr_sensitivity_store_cptkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	cptkbd_data->sensitivity = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	lenovo_features_set_cptkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) static struct device_attribute dev_attr_fn_lock =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	__ATTR(fn_lock, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			attr_fn_lock_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 			attr_fn_lock_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) static struct device_attribute dev_attr_sensitivity_cptkbd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	__ATTR(sensitivity, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 			attr_sensitivity_show_cptkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			attr_sensitivity_store_cptkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) static struct attribute *lenovo_attributes_cptkbd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	&dev_attr_fn_lock.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	&dev_attr_sensitivity_cptkbd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) static const struct attribute_group lenovo_attr_group_cptkbd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	.attrs = lenovo_attributes_cptkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) static int lenovo_raw_event(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			struct hid_report *report, u8 *data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	 * its own key is outside the usage page range. Remove extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	 * keypresses and remap to inside usage page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	if (unlikely(hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			&& size == 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 			&& data[0] == 0x15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			&& data[1] == 0x94
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			&& data[2] == 0x01)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		data[1] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		data[2] = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) static int lenovo_event_tp10ubkbd(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		struct hid_field *field, struct hid_usage *usage, __s32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	struct lenovo_drvdata *data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (usage->type == EV_KEY && usage->code == KEY_FN_ESC && value == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		 * The user has toggled the Fn-lock state. Toggle our own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		 * cached value of it and sync our value to the keyboard to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		 * ensure things are in sync (the sycning should be a no-op).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		data->fn_lock = !data->fn_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		schedule_work(&data->fn_lock_sync_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) static int lenovo_event_cptkbd(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		struct hid_field *field, struct hid_usage *usage, __s32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	struct lenovo_drvdata *cptkbd_data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	/* "wheel" scroll events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	if (usage->type == EV_REL && (usage->code == REL_WHEEL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			usage->code == REL_HWHEEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		/* Scroll events disable middle-click event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		cptkbd_data->middlebutton_state = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	/* Middle click events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	if (usage->type == EV_KEY && usage->code == BTN_MIDDLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		if (value == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 			cptkbd_data->middlebutton_state = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		} else if (value == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			if (cptkbd_data->middlebutton_state == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 				/* No scrolling inbetween, send middle-click */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 				input_event(field->hidinput->input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 					EV_KEY, BTN_MIDDLE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 				input_sync(field->hidinput->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 				input_event(field->hidinput->input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 					EV_KEY, BTN_MIDDLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 				input_sync(field->hidinput->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			cptkbd_data->middlebutton_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		struct hid_usage *usage, __s32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (!hid_get_drvdata(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	case USB_DEVICE_ID_LENOVO_CUSBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	case USB_DEVICE_ID_LENOVO_CBTKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		return lenovo_event_cptkbd(hdev, field, usage, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		return lenovo_event_tp10ubkbd(hdev, field, usage, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) static int lenovo_features_set_tpkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct hid_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	report->field[0]->value[0]  = data_pointer->press_to_select   ? 0x01 : 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	report->field[0]->value[0] |= data_pointer->dragging          ? 0x04 : 0x08;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	report->field[0]->value[0] |= data_pointer->release_to_select ? 0x10 : 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	report->field[0]->value[0] |= data_pointer->select_right      ? 0x80 : 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	report->field[1]->value[0] = 0x03; // unknown setting, imitate windows driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	report->field[2]->value[0] = data_pointer->sensitivity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	report->field[3]->value[0] = data_pointer->press_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) static ssize_t attr_press_to_select_show_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->press_to_select);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) static ssize_t attr_press_to_select_store_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	if (kstrtoint(buf, 10, &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	if (value < 0 || value > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	data_pointer->press_to_select = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	lenovo_features_set_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) static ssize_t attr_dragging_show_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->dragging);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) static ssize_t attr_dragging_store_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	if (kstrtoint(buf, 10, &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	if (value < 0 || value > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	data_pointer->dragging = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	lenovo_features_set_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) static ssize_t attr_release_to_select_show_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->release_to_select);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) static ssize_t attr_release_to_select_store_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	if (kstrtoint(buf, 10, &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	if (value < 0 || value > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	data_pointer->release_to_select = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	lenovo_features_set_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) static ssize_t attr_select_right_show_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	return snprintf(buf, PAGE_SIZE, "%u\n", data_pointer->select_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) static ssize_t attr_select_right_store_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (kstrtoint(buf, 10, &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	if (value < 0 || value > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	data_pointer->select_right = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	lenovo_features_set_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) static ssize_t attr_sensitivity_show_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	return snprintf(buf, PAGE_SIZE, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		data_pointer->sensitivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) static ssize_t attr_sensitivity_store_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	data_pointer->sensitivity = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	lenovo_features_set_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) static ssize_t attr_press_speed_show_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	return snprintf(buf, PAGE_SIZE, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		data_pointer->press_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) static ssize_t attr_press_speed_store_tpkbd(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	if (kstrtoint(buf, 10, &value) || value < 1 || value > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	data_pointer->press_speed = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	lenovo_features_set_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) static struct device_attribute dev_attr_press_to_select_tpkbd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	__ATTR(press_to_select, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			attr_press_to_select_show_tpkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			attr_press_to_select_store_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static struct device_attribute dev_attr_dragging_tpkbd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	__ATTR(dragging, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			attr_dragging_show_tpkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			attr_dragging_store_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) static struct device_attribute dev_attr_release_to_select_tpkbd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	__ATTR(release_to_select, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			attr_release_to_select_show_tpkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 			attr_release_to_select_store_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) static struct device_attribute dev_attr_select_right_tpkbd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	__ATTR(select_right, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 			attr_select_right_show_tpkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 			attr_select_right_store_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) static struct device_attribute dev_attr_sensitivity_tpkbd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	__ATTR(sensitivity, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 			attr_sensitivity_show_tpkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			attr_sensitivity_store_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) static struct device_attribute dev_attr_press_speed_tpkbd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	__ATTR(press_speed, S_IWUSR | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			attr_press_speed_show_tpkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			attr_press_speed_store_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) static struct attribute *lenovo_attributes_tpkbd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	&dev_attr_press_to_select_tpkbd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	&dev_attr_dragging_tpkbd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	&dev_attr_release_to_select_tpkbd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	&dev_attr_select_right_tpkbd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	&dev_attr_sensitivity_tpkbd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	&dev_attr_press_speed_tpkbd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) static const struct attribute_group lenovo_attr_group_tpkbd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	.attrs = lenovo_attributes_tpkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) static void lenovo_led_set_tpkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	struct hid_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	report = hdev->report_enum[HID_OUTPUT_REPORT].report_id_hash[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	report->field[0]->value[0] = (data_pointer->led_state >> 0) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	report->field[0]->value[1] = (data_pointer->led_state >> 1) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static enum led_brightness lenovo_led_brightness_get(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 			struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	struct device *dev = led_cdev->dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	int led_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (led_cdev == &data_pointer->led_micmute)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		led_nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	return data_pointer->led_state & (1 << led_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 				? LED_FULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 				: LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 			enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	struct device *dev = led_cdev->dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	struct hid_device *hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	u8 tp10ubkbd_led[] = { TP10UBKBD_MUTE_LED, TP10UBKBD_MICMUTE_LED };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	int led_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (led_cdev == &data_pointer->led_micmute)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		led_nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	if (value == LED_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		data_pointer->led_state &= ~(1 << led_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		data_pointer->led_state |= 1 << led_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	case USB_DEVICE_ID_LENOVO_TPKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		lenovo_led_set_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) static int lenovo_register_leds(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct lenovo_drvdata *data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	size_t name_sz = strlen(dev_name(&hdev->dev)) + 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	char *name_mute, *name_micm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	name_mute = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	name_micm = devm_kzalloc(&hdev->dev, name_sz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (name_mute == NULL || name_micm == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		hid_err(hdev, "Could not allocate memory for led data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	snprintf(name_mute, name_sz, "%s:amber:mute", dev_name(&hdev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	snprintf(name_micm, name_sz, "%s:amber:micmute", dev_name(&hdev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	data->led_mute.name = name_mute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	data->led_mute.brightness_get = lenovo_led_brightness_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	data->led_mute.brightness_set_blocking = lenovo_led_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	data->led_mute.flags = LED_HW_PLUGGABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	data->led_mute.dev = &hdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	ret = led_classdev_register(&hdev->dev, &data->led_mute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	data->led_micmute.name = name_micm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	data->led_micmute.brightness_get = lenovo_led_brightness_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	data->led_micmute.brightness_set_blocking = lenovo_led_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	data->led_micmute.flags = LED_HW_PLUGGABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	data->led_micmute.dev = &hdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	ret = led_classdev_register(&hdev->dev, &data->led_micmute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		led_classdev_unregister(&data->led_mute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) static int lenovo_probe_tpkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	struct lenovo_drvdata *data_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	 * Only register extra settings against subdevice where input_mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	 * set drvdata to 1, i.e. the trackpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	if (!hid_get_drvdata(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	hid_set_drvdata(hdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	/* Validate required reports. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		if (!hid_validate_values(hdev, HID_FEATURE_REPORT, 4, i, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 3, 0, 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	data_pointer = devm_kzalloc(&hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				    sizeof(struct lenovo_drvdata),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 				    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	if (data_pointer == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		hid_err(hdev, "Could not allocate memory for driver data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	// set same default values as windows driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	data_pointer->sensitivity = 0xa0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	data_pointer->press_speed = 0x38;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	hid_set_drvdata(hdev, data_pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	ret = lenovo_register_leds(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	lenovo_features_set_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) static int lenovo_probe_cptkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	struct lenovo_drvdata *cptkbd_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	/* All the custom action happens on the USBMOUSE device for USB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	if (hdev->product == USB_DEVICE_ID_LENOVO_CUSBKBD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			&& hdev->type != HID_TYPE_USBMOUSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		hid_dbg(hdev, "Ignoring keyboard half of device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	cptkbd_data = devm_kzalloc(&hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 					sizeof(*cptkbd_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	if (cptkbd_data == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		hid_err(hdev, "can't alloc keyboard descriptor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	hid_set_drvdata(hdev, cptkbd_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	 * Tell the keyboard a driver understands it, and turn F7, F9, F11 into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	 * regular keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	ret = lenovo_send_cmd_cptkbd(hdev, 0x01, 0x03);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		hid_warn(hdev, "Failed to switch F7/9/11 mode: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	/* Switch middle button to native mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	ret = lenovo_send_cmd_cptkbd(hdev, 0x09, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		hid_warn(hdev, "Failed to switch middle button: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	/* Set keyboard settings to known state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	cptkbd_data->middlebutton_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	cptkbd_data->fn_lock = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	cptkbd_data->sensitivity = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	lenovo_features_set_cptkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_cptkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		hid_warn(hdev, "Could not create sysfs group: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) static struct attribute *lenovo_attributes_tp10ubkbd[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	&dev_attr_fn_lock.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) static const struct attribute_group lenovo_attr_group_tp10ubkbd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	.attrs = lenovo_attributes_tp10ubkbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	struct lenovo_drvdata *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	/* All the custom action happens on the USBMOUSE device for USB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	if (hdev->type != HID_TYPE_USBMOUSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	data = devm_kzalloc(&hdev->dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	mutex_init(&data->led_report_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	INIT_WORK(&data->fn_lock_sync_work, lenovo_tp10ubkbd_sync_fn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	data->hdev = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	hid_set_drvdata(hdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	 * The Thinkpad 10 ultrabook USB kbd dock's Fn-lock defaults to on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	 * We cannot read the state, only set it, so we force it to on here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	 * (which should be a no-op) to make sure that our state matches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	 * keyboard's FN-lock state. This is the same as what Windows does.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	data->fn_lock = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	ret = lenovo_register_leds(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) static int lenovo_probe(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	ret = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		hid_err(hdev, "hid_parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		hid_err(hdev, "hid_hw_start failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	case USB_DEVICE_ID_LENOVO_TPKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		ret = lenovo_probe_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	case USB_DEVICE_ID_LENOVO_CUSBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	case USB_DEVICE_ID_LENOVO_CBTKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		ret = lenovo_probe_cptkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		ret = lenovo_probe_tp10ubkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		goto err_hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) err_hid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) static void lenovo_remove_tpkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	struct lenovo_drvdata *data_pointer = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	 * Only the trackpoint half of the keyboard has drvdata and stuff that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	 * needs unregistering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	if (data_pointer == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	sysfs_remove_group(&hdev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			&lenovo_attr_group_tpkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	led_classdev_unregister(&data_pointer->led_micmute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	led_classdev_unregister(&data_pointer->led_mute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) static void lenovo_remove_cptkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	sysfs_remove_group(&hdev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			&lenovo_attr_group_cptkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) static void lenovo_remove_tp10ubkbd(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	struct lenovo_drvdata *data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	led_classdev_unregister(&data->led_micmute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	led_classdev_unregister(&data->led_mute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	sysfs_remove_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	cancel_work_sync(&data->fn_lock_sync_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) static void lenovo_remove(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	case USB_DEVICE_ID_LENOVO_TPKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		lenovo_remove_tpkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	case USB_DEVICE_ID_LENOVO_CUSBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	case USB_DEVICE_ID_LENOVO_CBTKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		lenovo_remove_cptkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		lenovo_remove_tp10ubkbd(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) static int lenovo_input_configured(struct hid_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		struct hid_input *hi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	switch (hdev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		case USB_DEVICE_ID_LENOVO_TPKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		case USB_DEVICE_ID_LENOVO_CUSBKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		case USB_DEVICE_ID_LENOVO_CBTKBD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 			if (test_bit(EV_REL, hi->input->evbit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 				/* set only for trackpoint device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 				__set_bit(INPUT_PROP_POINTER, hi->input->propbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 				__set_bit(INPUT_PROP_POINTING_STICK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 						hi->input->propbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) static const struct hid_device_id lenovo_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	{ HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPKBD) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	{ HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CUSBKBD) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_CBTKBD) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	{ HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TPPRODOCK) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	{ HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_III) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	{ HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_PRO) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	{ HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_OPTICAL) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	{ HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	{ HID_USB_DEVICE(USB_VENDOR_ID_IBM, USB_DEVICE_ID_IBM_SCROLLPOINT_800DPI_OPTICAL_PRO) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	{ HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_SCROLLPOINT_OPTICAL) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	{ HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_TP10UBKBD) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) MODULE_DEVICE_TABLE(hid, lenovo_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) static struct hid_driver lenovo_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	.name = "lenovo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	.id_table = lenovo_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	.input_configured = lenovo_input_configured,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	.input_mapping = lenovo_input_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	.probe = lenovo_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	.remove = lenovo_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	.raw_event = lenovo_raw_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	.event = lenovo_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	.report_fixup = lenovo_report_fixup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) module_hid_driver(lenovo_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) MODULE_LICENSE("GPL");