lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210511182023.730524-3-jose.exposito89@gmail.com>
Date:   Tue, 11 May 2021 20:20:21 +0200
From:   José Expósito <jose.exposito89@...il.com>
To:     jikos@...nel.org
Cc:     benjamin.tissoires@...hat.com, linux-input@...r.kernel.org,
        linux-kernel@...r.kernel.org,
        José Expósito <jose.exposito89@...il.com>
Subject: [PATCH 3/5] HID: magicmouse: Magic Trackpad 2 USB battery capacity

Report the battery capacity percentage for the Apple Magic Trackpad 2
when it is connected over USB.

Signed-off-by: José Expósito <jose.exposito89@...il.com>
---
 drivers/hid/hid-magicmouse.c | 136 +++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)

diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
index d4a58dd6d2b8..ea8a85767c39 100644
--- a/drivers/hid/hid-magicmouse.c
+++ b/drivers/hid/hid-magicmouse.c
@@ -16,6 +16,7 @@
 #include <linux/input/mt.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#include <linux/usb.h>
 #include <linux/workqueue.h>
 
 #include "hid-ids.h"
@@ -58,6 +59,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
 #define MOUSE2_REPORT_ID   0x12
 #define DOUBLE_REPORT_ID   0xf7
 #define BT_BATTERY_REPORT_ID 0x90
+#define USB_BATTERY_EP_ADDR  0x81
 
 /* These definitions are not precise, but they're close enough.  (Bits
  * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
@@ -142,6 +144,10 @@ struct magicmouse_sc {
 		struct power_supply *ps;
 		struct power_supply_desc ps_desc;
 		int capacity;
+		struct urb *urb;
+		u8 *urb_buf;
+		int urb_buf_size;
+		dma_addr_t urb_buf_dma;
 	} battery;
 };
 
@@ -231,6 +237,112 @@ static int magicmouse_battery_get_property(struct power_supply *psy,
 	return ret;
 }
 
+static void magicmouse_battery_usb_urb_complete(struct urb *urb)
+{
+	struct magicmouse_sc *msc = urb->context;
+	int ret;
+
+	switch (urb->status) {
+	case 0:
+		msc->battery.capacity = msc->battery.urb_buf[2];
+		break;
+	case -EOVERFLOW:
+		hid_err(msc->hdev, "URB overflow\n");
+		fallthrough;
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+		return;
+	default:
+		break;
+	}
+
+	ret = usb_submit_urb(msc->battery.urb, GFP_ATOMIC);
+	if (ret)
+		hid_err(msc->hdev, "unable to submit URB, %d\n", ret);
+}
+
+static int magicmouse_battery_usb_probe(struct magicmouse_sc *msc)
+{
+	struct usb_interface *iface = to_usb_interface(msc->hdev->dev.parent);
+	struct usb_device *usbdev = interface_to_usbdev(iface);
+	struct usb_host_endpoint *endpoint = NULL;
+	u8 ep_address;
+	unsigned int pipe = 0;
+	int i, ret;
+
+	if (!magicmouse_can_report_battery_vendor(msc, USB_VENDOR_ID_APPLE))
+		return -EINVAL;
+
+	for (i = 0; i < sizeof(usbdev->ep_in); i++) {
+		endpoint = usbdev->ep_in[i];
+		if (endpoint) {
+			ep_address = endpoint->desc.bEndpointAddress;
+			if (ep_address == USB_BATTERY_EP_ADDR)
+				break;
+		}
+	}
+
+	if (!endpoint) {
+		hid_err(msc->hdev, "endpoint with address %d not found\n",
+			USB_BATTERY_EP_ADDR);
+		ret = -EIO;
+		goto exit;
+	}
+
+	msc->battery.urb = usb_alloc_urb(0, GFP_ATOMIC);
+	if (!msc->battery.urb) {
+		hid_err(msc->hdev, "unable to alloc URB, ENOMEM\n");
+		ret = -ENOMEM;
+		goto exit;
+	}
+
+	pipe = usb_rcvintpipe(usbdev, endpoint->desc.bEndpointAddress);
+	if (pipe == 0) {
+		hid_err(msc->hdev, "unable to create USB rcvintpipe\n");
+		ret = -EIO;
+		goto err_free_urb;
+	}
+
+	msc->battery.urb_buf_size = endpoint->desc.wMaxPacketSize;
+	msc->battery.urb_buf_dma = msc->battery.urb->transfer_dma;
+	msc->battery.urb_buf = usb_alloc_coherent(usbdev,
+			       msc->battery.urb_buf_size, GFP_ATOMIC,
+			       &msc->battery.urb_buf_dma);
+	if (!msc->battery.urb_buf) {
+		hid_err(msc->hdev, "unable to alloc URB buffer, ENOMEM\n");
+		ret = -ENOMEM;
+		goto err_free_urb;
+	}
+
+	usb_fill_int_urb(msc->battery.urb, usbdev, pipe, msc->battery.urb_buf,
+			 msc->battery.urb_buf_size,
+			 magicmouse_battery_usb_urb_complete, msc,
+			 endpoint->desc.bInterval);
+
+	ret = usb_submit_urb(msc->battery.urb, GFP_ATOMIC);
+	if (ret) {
+		hid_err(msc->hdev, "unable to submit URB, %d\n", ret);
+		goto err_free_urb_buf;
+	}
+
+	return 0;
+
+err_free_urb_buf:
+	usb_free_coherent(usbdev, msc->battery.urb_buf_size,
+			  msc->battery.urb_buf, msc->battery.urb_buf_dma);
+
+err_free_urb:
+	usb_free_urb(msc->battery.urb);
+
+exit:
+	msc->battery.urb = NULL;
+	msc->battery.urb_buf = NULL;
+	msc->battery.urb_buf_size = 0;
+
+	return ret;
+}
+
 static int magicmouse_battery_probe(struct hid_device *hdev)
 {
 	struct magicmouse_sc *msc = hid_get_drvdata(hdev);
@@ -269,6 +381,12 @@ static int magicmouse_battery_probe(struct hid_device *hdev)
 		return ret;
 	}
 
+	if (msc->input->id.vendor == USB_VENDOR_ID_APPLE) {
+		ret = magicmouse_battery_usb_probe(msc);
+		if (ret)
+			return ret;
+	}
+
 	return 0;
 }
 
@@ -923,7 +1041,25 @@ static int magicmouse_probe(struct hid_device *hdev,
 static void magicmouse_remove(struct hid_device *hdev)
 {
 	struct magicmouse_sc *msc = hid_get_drvdata(hdev);
+	struct usb_interface *iface;
+	struct usb_device *usbdev;
 	cancel_delayed_work_sync(&msc->work);
+
+	if (msc &&
+	    magicmouse_can_report_battery_vendor(msc, USB_VENDOR_ID_APPLE) &&
+	    msc->battery.urb && msc->battery.urb_buf) {
+		iface = to_usb_interface(hdev->dev.parent);
+		usbdev = interface_to_usbdev(iface);
+
+		usb_kill_urb(msc->battery.urb);
+
+		usb_free_coherent(usbdev, msc->battery.urb_buf_size,
+				  msc->battery.urb_buf,
+				  msc->battery.urb_buf_dma);
+
+		usb_free_urb(msc->battery.urb);
+	}
+
 	hid_hw_stop(hdev);
 }
 
-- 
2.25.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ