Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp4849892pxb; Sun, 6 Feb 2022 05:32:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJz5dmiuvc6SqKByKCEs1MwmJEgOhcvVO4mJ2eSR5FJV7wcNgjG34K0qIK9sg+PfCi6j7/yH X-Received: by 2002:a05:6402:1e92:: with SMTP id f18mr8869441edf.128.1644154355960; Sun, 06 Feb 2022 05:32:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1644154355; cv=pass; d=google.com; s=arc-20160816; b=a+mT6Amr5++NEMuFykZapxfu/ouvhj0u7c7mrUq/8wz36tx8KwKBR/MDhXcAO9HUfi ujcwXsAtcvC0azgzvOFoLnZAw0aOJUD46ZxgZ7uYqTcfqmPSK+9rYVMSYUkCL3+oJRps kLlmZeYgchBFZNbPan4crNebIAfPoCWLmSMO/kzy0uH9weoUzjWxqljkxCRTL+NwtHEA 7nnxMoYxhqzxbuuDFbZ0Gu5URVsT4IpjWN0prkUzAlMN02jCn/BWvmGAnB7HLFCV9XOy DuF/Jo2Z3Oo6d8zQzC5kxAUhNcGekxOYvSP4txdu0Ruk3z9xLdz/aDLCwuk1qazwHbuN juXQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:references :message-id:date:thread-index:thread-topic:subject:cc:to:from :dkim-signature; bh=+7is92ihNGd6dPnvuiggr8at82p5e/oKjnfVvb6maXk=; b=SW/R5v/3YWFCcq+6qFligqUXY4W2GHE1XC4lWS3i1r8GUa8oRbn9i9TXzp99tCnZZB 1uicrIHOJ7QzrZma90cyCEh+GNTKs6pWk1+G7lU/+Ae6D0IOnV0fzOwztiWwMBu7sRhP HzgLjI72hHwl5sGsNqqzjC8RN2vABnp4V3mDgE4CrDOB5UEEI3RJXJ6Io2F4mZXnfN/m 1nnFzZEY87QwoUJfzeuK7sDZEpT1VpjHqxmxnlkb9oK2U17XEa9lru2naY3ELvzCz+2V XqccezsI+qPTQ4ntIFqyCHa7XoS+NNWZiYgJf0CR2Aj0R6/8jzfY0jZa6vr/0+LIANjb iOHw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@live.com header.s=selector1 header.b=MaBFQccw; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=live.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l11si3213762eda.386.2022.02.06.05.32.11; Sun, 06 Feb 2022 05:32:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@live.com header.s=selector1 header.b=MaBFQccw; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=live.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350510AbiBCMVU (ORCPT + 99 others); Thu, 3 Feb 2022 07:21:20 -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+fdHzDPbNPxifvHArf6zIm8uROKkpEq5M7oOcthVD4A4dpDZTaKUsJlu557LRCHQC0g/2lQ2KskRc8EAglgpQvSzLc5HJXQ+gjfhcku4X20P3f0p39ulfBJF/0GDe4RpZY2WNtuF5gRJibQhbNZHxc9oLytPe+WYPOh4gkuE6u1y83gYnpltcYOAqWwEQFm8uSgpqdNvP2XLwGidxNDxWr21cX1uYYmivcBucKtE8NrReW/ba3NDpAODx5jzsZUAfhvSDHzOYtpIKS8v+XQ2Ur6GYSHMEFZGJ+c0HZ9KcyYuM4/TpWtiU7NBnQY6KMDaf/zHFGtuYobwM3hCIPU7c+DJs5tUHeR7F78MpCGYNJKaEzT2IVJoM+Q3drzBh83XPCjNgdI2mpNCjo0JdZYS0V79zndynzv4kk0YeF2eDM+fr78y37BFezdOECYWubBBDgNubfbMH2hL00R7WpMkQYpag3GFHbWiXbFUAzQjxGfpng== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?qYX3W4q3uX6QD8c5tbECTcw7T9M+zkMKImvvJSIfK3O2hWWvCjIpMzaHHBce?= =?us-ascii?Q?wktyo7di5jkIUODjQc3hR7B0SompqsOKZ1DQqQf4gpwSmw/Jrt5j1KvAqs3G?= =?us-ascii?Q?MGrZViBzjHnRzhITaByg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KTQY2?= =?us-ascii?Q?cN6Rs6qxbt5srcCbAJKC5zXYOw=3D=3D?= Content-Type: text/plain; charset="us-ascii" Content-ID: Content-Transfer-Encoding: quoted-printable 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-kernel@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 u= sed. 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 */ =20 /* @@ -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) =20 #define APPLE_FLAG_FKEY 0x01 =20 @@ -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] =3D as-is, Mac layout, 1 =3D swapped, PC layout)"); =20 +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; }; =20 struct apple_key_translation { @@ -76,6 +85,20 @@ struct apple_key_translation { u8 flags; }; =20 +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[] =3D { { KEY_BACKSPACE, KEY_DELETE }, { KEY_ENTER, KEY_INSERT }, @@ -530,6 +553,105 @@ static int apple_input_configured(struct hid_device *= hdev, return 0; } =20 +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_l= ist, list) { + for (i =3D 0; i < report->maxfield; i++) { + hid =3D report->field[i]->usage->hid; + if ((hid & HID_USAGE_PAGE) =3D=3D HID_UP_MSVENDOR && (hid & HID_USAGE) = =3D=3D 0xf) + return true; + } + } + + return false; +} + +static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rat= e) +{ + int ret =3D 0; + struct apple_backlight_set_report *rep; + + rep =3D kmalloc(sizeof(*rep), GFP_KERNEL); + if (rep =3D=3D NULL) + return -ENOMEM; + + rep->report_id =3D 0xB0; + rep->version =3D 1; + rep->backlight =3D value; + rep->rate =3D rate; + + ret =3D 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 =3D 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 =3D hid_get_drvdata(hdev); + struct apple_backlight_config_report *rep; + + if (!apple_backlight_check_support(hdev)) + return -EINVAL; + + rep =3D kmalloc(0x200, GFP_KERNEL); + if (rep =3D=3D NULL) + return -ENOMEM; + + ret =3D 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 !=3D 1) { + hid_err(hdev, "backlight config struct: bad version %i\n", rep->version)= ; + ret =3D -EINVAL; + goto cleanup_and_exit; + } + + hid_dbg(hdev, "backlight config: off=3D%u, on_min=3D%u, on_max=3D%u\n", + rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max); + + asc->backlight =3D devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_= KERNEL); + if (!asc->backlight) { + ret =3D -ENOMEM; + goto cleanup_and_exit; + } + + asc->backlight->hdev =3D hdev; + asc->backlight->cdev.name =3D "apple::kbd_backlight"; + asc->backlight->cdev.max_brightness =3D rep->backlight_on_max; + asc->backlight->cdev.brightness_set_blocking =3D apple_backlight_led_set; + + ret =3D apple_backlight_set(hdev, 0, 0); + if (ret < 0) { + hid_err(hdev, "backlight set request failed: %d\n", ret); + goto cleanup_and_exit; + } + + ret =3D 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); =20 + if (quirks & APPLE_BACKLIGHT_CTL) + apple_backlight_init(hdev); + return 0; } =20 --=20 2.25.1