From patchwork Thu Feb 3 12:21:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 12734092 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87980C433F5 for ; Thu, 3 Feb 2022 12:21:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350489AbiBCMVT (ORCPT ); Thu, 3 Feb 2022 07:21:19 -0500 Received: from mail-bmxind01olkn2060.outbound.protection.outlook.com ([40.92.103.60]:56032 "EHLO IND01-BMX-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S229379AbiBCMVS (ORCPT ); Thu, 3 Feb 2022 07:21:18 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DtnwqqrpkxWmgCSRYAgNa3uGHkQQcW8LRLN4KFvFpMzrS1P7WZ9335eEEQMtU+jERkWGXf/iKZGizNs/9/jgpxXx+8YS8G3PvIE6uu1wpbHAwBmyHNz8I4RVs6jJeVMT9fPosCoQYNYzmEaHtGb/P8TtWmOlBk4uuH1uGpM+BmvOCRy6VNLWHpl4hY3BSEEgWa3VLJ4rUeIdBz4BCtGvne2LI4+eik4/UtC46MqAhl4knJcY8r4eHTZea1QFma15hBsiAtDyeN6bz5Tq4mCu8aFGV0PUhSSjs3UX4kYnXR4ft8L0EX2/vyDE8RVLfcIhMw0hZLbyJNUvgPJP95QzAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=+7is92ihNGd6dPnvuiggr8at82p5e/oKjnfVvb6maXk=; b=bLC1PHpNo4zSF9zr4SfLtA/5zbtnhg7+3E9oQ+xvSYOD3r7s+rN62dA3DLGfO8im+2cqCwYgG8UwAeGf7REA94I+6skyQPNwxI7cbqpnu69ocyfWOepUHVBMqHC7a4SMBNfZ4tSOKn8odETMWqcILC5cUEzWidXJWa9sLbLjNE2QCwEoOzWKA3RUkO8BtihxkgfDofqFtNEMD36Z5+8DwP+eTHnTzjTe88AWv3uj4Ttg//cYGzX5qmF/bd/nM7mYLxDgRXapWnHCezWOAY7AR27j0dSMK8nzAmr2L1ldEpXZEQ7djD/hVfUqXA7xkV09ja14hSLKlgTojds2tdCTsg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+7is92ihNGd6dPnvuiggr8at82p5e/oKjnfVvb6maXk=; b=MaBFQccwXhQ2vaW7Yu1gGEWBfKJbHSpgXxma17y3zDU+G8yl2sJgUboHfhIO8aiS7Y5aO4+xxP61X0W6mBMDRIEFdO4FrV5vV6jFUcqkHOc+7LGoprK61KE/bpkGT0UCc/949B7di+pGEhmxgxs15ahQVigLVOG7H+Ctu1B1MpDlN66OSws5gBgDErReKZZNXLpL0ngI//pFVgu4G+K0edEqJSqXx7daL5q8tJvjp71houxCnUu6oNlDjrs4MWlzRN7tbemKPy9zXZ0LGgBOEsfoGBwoLCIYVeIJ+QB3sblEDVRGNBc4SF4CLLt8+n/UjyMoLjaCN3sEnnmv/r+m1w== Received: from PNZPR01MB4415.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:1b::13) by MAXPR01MB2320.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:59::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.20; Thu, 3 Feb 2022 12:21:13 +0000 Received: from PNZPR01MB4415.INDPRD01.PROD.OUTLOOK.COM ([fe80::d19b:7cd1:3760:b055]) by PNZPR01MB4415.INDPRD01.PROD.OUTLOOK.COM ([fe80::d19b:7cd1:3760:b055%9]) with mapi id 15.20.4951.012; Thu, 3 Feb 2022 12:21:13 +0000 From: Aditya Garg To: "jkosina@suse.cz" , Jiri Kosina , "alexhenrie24@gmail.com" , "benjamin.tissoires@redhat.com" , "linux-input@vger.kernel.org" , Linux Kernel Mailing List CC: "paul@mrarm.io" , Aun-Ali Zaidi , Orlando Chamberlain Subject: [PATCH v3 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs. Thread-Topic: [PATCH v3 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs. Thread-Index: AQHYGPiIph7UJPakWU+x6MdA3b9YOw== Date: Thu, 3 Feb 2022 12:21:13 +0000 Message-ID: References: <67E7EA8B-CF21-4794-B7B4-96873EE70EF6@live.com> <1088ECFE-B356-4731-AC8A-09A4421DD7D1@live.com> In-Reply-To: <1088ECFE-B356-4731-AC8A-09A4421DD7D1@live.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [eeUxHezq7LijH3/azKQIrHRgjvQ4BZPrxouD7lvj9xgn2k28AS1Tq1tjNOkkibvG] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: fdd86464-bb04-4490-2aee-08d9e70fab4f x-ms-traffictypediagnostic: MAXPR01MB2320:EE_ x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: GpMzPP32Ti3tTCsLQU6MLRC0ihxEQJGh3J0AT+ElCHoWKKuhD2g9vf4DKlerz6ykVEzoyMx6DdLzsKx8SdicRsqH0rwsuv+oUuUwTKpk+fdHzDPbNPxifvHAr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3bR/o4WWVNhmpk6T/wA7FliI6GNTL9lNnVs6HCHseYgVTTys0Rqc0+fguMNHryUs9YR3P6bVpb5Xdmr/DDrVUeK61sCxtcnBeGJuHcQOXPkIteKetSkwgmoeCCoJMsZ0e6Vb2w+Rvi2hXnZTUKnIK2nmuOL2iJQGnwHBTs7A7uJuXJZvwyhCTw5UtIJDrJHkDqt5+eKXz0z/a3ZSJr+fAr0rfe5NaCWgwUaJg7z/HxEe5tNKHAP1MMtlLqPf6FXCRlbYeXiz2B+rqU29NGf2mYUFnUEik2eZrAMy1N9ipAbp0/Vv9XJn3bEQuoKtmzqOWIRIG6AgU1Hm1cVQrp7jtWuJbDsXKjUONQoRbKF2eneaVdQX80frFXshtRqh11jsKC1R0KilUoK8yyahAdQmYh+I85WXldI0nj2kUQ9p3rKGkGmggei16PWKJlWVWcyUDHy34WKbfKRDebCjmKTQY2cN6Rs6qxbt5srcCbAJKC5zXYOw== Content-ID: MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-42ed3.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PNZPR01MB4415.INDPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: fdd86464-bb04-4490-2aee-08d9e70fab4f X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Feb 2022 12:21:13.3472 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MAXPR01MB2320 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org From: Paul Pawlowski This patch introduces the requisite plumbing for supporting keyboard backlight on T2-attached, USB exposed models. The quirk mechanism was used to reuse the existing hid-apple driver. Signed-off-by: Paul Pawlowski Signed-off-by: Aun-Ali Zaidi Signed-off-by: Aditya Garg --- v2 :- Use better approach to map fn keys v3 :- Use BIT(10) for APPLE_BACKLIGHT_CTL since 0x0200 (BIT (9)) has been used. drivers/hid/hid-apple.c | 125 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c index 24802a4a6..c22d445a9 100644 --- a/drivers/hid/hid-apple.c +++ b/drivers/hid/hid-apple.c @@ -7,6 +7,7 @@ * Copyright (c) 2005 Michael Haboustak for Concept2, Inc * Copyright (c) 2006-2007 Jiri Kosina * Copyright (c) 2008 Jiri Slaby + * Copyright (c) 2019 Paul Pawlowski */ /* @@ -33,6 +34,7 @@ /* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */ #define APPLE_NUMLOCK_EMULATION BIT(8) #define APPLE_RDESC_BATTERY BIT(9) +#define APPLE_BACKLIGHT_CTL BIT(10) #define APPLE_FLAG_FKEY 0x01 @@ -61,6 +63,12 @@ MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. " "(For people who want to keep PC keyboard muscle memory. " "[0] = as-is, Mac layout, 1 = swapped, PC layout)"); +struct apple_sc_backlight { + struct led_classdev cdev; + struct hid_device *hdev; + unsigned short backlight_off, backlight_on_min, backlight_on_max; +}; + struct apple_sc { struct hid_device *hdev; unsigned long quirks; @@ -68,6 +76,7 @@ struct apple_sc { unsigned int fn_found; DECLARE_BITMAP(pressed_numlock, KEY_CNT); struct timer_list battery_timer; + struct apple_sc_backlight *backlight; }; struct apple_key_translation { @@ -76,6 +85,20 @@ struct apple_key_translation { u8 flags; }; +struct apple_backlight_config_report { + u8 report_id; + u8 version; + u16 backlight_off, backlight_on_min, backlight_on_max; +}; + +struct apple_backlight_set_report { + u8 report_id; + u8 version; + u16 backlight; + u16 rate; +}; + + static const struct apple_key_translation apple2021_fn_keys[] = { { KEY_BACKSPACE, KEY_DELETE }, { KEY_ENTER, KEY_INSERT }, @@ -530,6 +553,105 @@ static int apple_input_configured(struct hid_device *hdev, return 0; } +static bool apple_backlight_check_support(struct hid_device *hdev) +{ + int i; + unsigned int hid; + struct hid_report *report; + + list_for_each_entry(report, &hdev->report_enum[HID_INPUT_REPORT].report_list, list) { + for (i = 0; i < report->maxfield; i++) { + hid = report->field[i]->usage->hid; + if ((hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && (hid & HID_USAGE) == 0xf) + return true; + } + } + + return false; +} + +static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rate) +{ + int ret = 0; + struct apple_backlight_set_report *rep; + + rep = kmalloc(sizeof(*rep), GFP_KERNEL); + if (rep == NULL) + return -ENOMEM; + + rep->report_id = 0xB0; + rep->version = 1; + rep->backlight = value; + rep->rate = rate; + + ret = hid_hw_raw_request(hdev, 0xB0u, (u8 *) rep, sizeof(*rep), + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); + + kfree(rep); + return ret; +} + +static int apple_backlight_led_set(struct led_classdev *led_cdev, + enum led_brightness brightness) +{ + struct apple_sc_backlight *backlight = container_of(led_cdev, + struct apple_sc_backlight, cdev); + + return apple_backlight_set(backlight->hdev, brightness, 0); +} + +static int apple_backlight_init(struct hid_device *hdev) +{ + int ret; + struct apple_sc *asc = hid_get_drvdata(hdev); + struct apple_backlight_config_report *rep; + + if (!apple_backlight_check_support(hdev)) + return -EINVAL; + + rep = kmalloc(0x200, GFP_KERNEL); + if (rep == NULL) + return -ENOMEM; + + ret = hid_hw_raw_request(hdev, 0xBFu, (u8 *) rep, sizeof(*rep), + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); + if (ret < 0) { + hid_err(hdev, "backlight request failed: %d\n", ret); + goto cleanup_and_exit; + } + if (ret < 8 || rep->version != 1) { + hid_err(hdev, "backlight config struct: bad version %i\n", rep->version); + ret = -EINVAL; + goto cleanup_and_exit; + } + + hid_dbg(hdev, "backlight config: off=%u, on_min=%u, on_max=%u\n", + rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max); + + asc->backlight = devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_KERNEL); + if (!asc->backlight) { + ret = -ENOMEM; + goto cleanup_and_exit; + } + + asc->backlight->hdev = hdev; + asc->backlight->cdev.name = "apple::kbd_backlight"; + asc->backlight->cdev.max_brightness = rep->backlight_on_max; + asc->backlight->cdev.brightness_set_blocking = apple_backlight_led_set; + + ret = apple_backlight_set(hdev, 0, 0); + if (ret < 0) { + hid_err(hdev, "backlight set request failed: %d\n", ret); + goto cleanup_and_exit; + } + + ret = devm_led_classdev_register(&hdev->dev, &asc->backlight->cdev); + +cleanup_and_exit: + kfree(rep); + return ret; +} + static int apple_probe(struct hid_device *hdev, const struct hid_device_id *id) { @@ -565,6 +687,9 @@ static int apple_probe(struct hid_device *hdev, jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS)); apple_fetch_battery(hdev); + if (quirks & APPLE_BACKLIGHT_CTL) + apple_backlight_init(hdev); + return 0; }