Skip to contentSkip to conversation

Bluetooth: btusb: Use an error label for error paths

PATCH Superseded Rajat Jain 5 participants 8 messages Dec 14, 2016 – Dec 19, 2016 4 messages not in the archive

Subsystems (4)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Rajat Jain

author
#0 lore raw

PATCH 1/3 Bluetooth: btusb: Use an error label for error paths

Use a label to remove the repetetive cleanup, for error cases. (This label will also be used in subsequent patches).

---
 drivers/bluetooth/btusb.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/bluetooth/btusb.c +9 -10
index 2f633df..ce22cef 100644
@@ -2991,18 +2991,15 @@ static int btusb_probe(struct usb_interface *intf,
 		err = usb_set_interface(data->udev, 0, 0);
 		if (err < 0) {
 			BT_ERR("failed to set interface 0, alt 0 %d", err);
-			hci_free_dev(hdev);
-			return err;
+			goto out_free_dev;
 		}
 	}
 
 	if (data->isoc) {
 		err = usb_driver_claim_interface(&btusb_driver,
 						 data->isoc, data);
-		if (err < 0) {
-			hci_free_dev(hdev);
-			return err;
-		}
+		if (err < 0)
+			goto out_free_dev;
 	}
 
 #ifdef CONFIG_BT_HCIBTUSB_BCM
@@ -3016,14 +3013,16 @@ static int btusb_probe(struct usb_interface *intf,
 #endif
 
 	err = hci_register_dev(hdev);
-	if (err < 0) {
-		hci_free_dev(hdev);
-		return err;
-	}
+	if (err < 0)
+		goto out_free_dev;
 
 	usb_set_intfdata(intf, data);
 
 	return 0;
+
+out_free_dev:
+	hci_free_dev(hdev);
+	return err;
 }
 
 static void btusb_disconnect(struct usb_interface *intf)
Signature · 1 line
2.8.0.rc3.226.g39d4020
#2 lore raw

PATCH 2/3 Bluetooth: btusb: Add out-of-band wakeup support

In reply to a message not in the archive

Some BT chips (e.g. Marvell 8997) contain a wakeup pin that can be connected to a gpio on the CPU side, and can be used to wakeup the host out-of-band. This can be useful in situations where the in-band wakeup is not possible or not preferable (e.g. the in-band wakeup may require the USB host controller to remain active, and hence consuming more system power during system sleep).

The oob gpio interrupt to be used for wakeup on the CPU side, is read from the device tree node, (using standard interrupt descriptors). A devcie tree binding document is also added for the driver.

---
 Documentation/devicetree/bindings/net/btusb.txt | 38 ++++++++++++
 drivers/bluetooth/btusb.c                       | 82 +++++++++++++++++++++++++
 2 files changed, 120 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/btusb.txt
Documentation/devicetree/bindings/net/btusb.txt new file +38 -0
new file mode 100644
index 0000000..bb27f92
@@ -0,0 +1,38 @@
+Generic Bluetooth controller over USB (btusb driver)
+---------------------------------------------------
+
+Required properties:
+
+  - compatible : should comply with the format "usbVID,PID" specified in
+		 Documentation/devicetree/bindings/usb/usb-device.txt
+		 At the time of writing, the only OF supported devices
+		 (more may be added later) are:
+
+		  "usb1286,204e" (Marvell 8997)
+
+Optional properties:
+
+  - interrupt-parent: phandle of the parent interrupt controller
+  - interrupts : The first interrupt specified is the interrupt that shall be
+		 used for out-of-band wake-on-bt. Driver will request an irq
+		 based on this interrupt number. During system suspend, the irq
+		 will be enabled so that the bluetooth chip can wakeup host
+		 platform out of band. During system resume, the irq will be
+		 disabled to make sure unnecessary interrupt is not received.
+
+Example:
+
+Following example uses irq pin number 3 of gpio0 for out of band wake-on-bt:
+
+&usb_host1_ehci {
+    status = "okay";
+    #address-cells = <1>;
+    #size-cells = <0>;
+
+    mvl_bt1: bt@1 {
+	compatible = "usb1286,204e";
+	reg = <1>;
+	interrupt-parent = <&gpio0>;
+	interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+    };
+};
drivers/bluetooth/btusb.c +82 -0

124 lines not on this page · raw message

Signature · 1 line
2.8.0.rc3.226.g39d4020
#7 lore raw

PATCH 3/3 Bluetooth: btusb: Configure Marvel to use one of the pins for oob wakeup

In reply to a message not in the archive

The Marvell devices may have many gpio pins, and hence for wakeup on these out-of-band pins, the chip needs to be told which pin is to be used for wakeup, using an hci command.

Thus, we read the pin number etc from the device tree node and send a command to the chip.

---
Note that while I would have liked to name the compatible string as more like "marvell, usb8997-bt", the devicetrees/bindings/usb/usb-device.txt requires the compatible property to be of the form "usbVID,PID".

 .../{marvell-bt-sd8xxx.txt => marvell-bt-8xxx.txt} | 25 ++++++++-
 drivers/bluetooth/btusb.c                          | 59 ++++++++++++++++++++++
 2 files changed, 82 insertions(+), 2 deletions(-)
 rename Documentation/devicetree/bindings/net/{marvell-bt-sd8xxx.txt => marvell-bt-8xxx.txt} (76%)
Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +23 -2
similarity index 76%
rename from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt
rename to Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt
index 6a9a63c..471bef8 100644
@@ -1,4 +1,4 @@
-Marvell 8897/8997 (sd8897/sd8997) bluetooth SDIO devices
+Marvell 8897/8997 (sd8897/sd8997) bluetooth devices (SDIO or USB based)
 ------
 
 Required properties:
@@ -6,11 +6,13 @@ Required properties:
   - compatible : should be one of the following:
 	* "marvell,sd8897-bt"
 	* "marvell,sd8997-bt"
+	* "usb1286,204e"
 
 Optional properties:
 
   - marvell,cal-data: Calibration data downloaded to the device during
 		      initialization. This is an array of 28 values(u8).
+		      This is only applicable to SDIO devices.
 
   - marvell,wakeup-pin: It represents wakeup pin number of the bluetooth chip.
 		        firmware will use the pin to wakeup host system (u16).
@@ -29,7 +31,9 @@ Example:
 IRQ pin 119 is used as system wakeup source interrupt.
 wakeup pin 13 and gap 100ms are configured so that firmware can wakeup host
 using this device side pin and wakeup latency.
-calibration data is also available in below example.
+
+Example for SDIO device follows (calibration data is also available in
+below example).
 
 &mmc3 {
 	status = "okay";
@@ -54,3 +58,20 @@ calibration data is also available in below example.
 		marvell,wakeup-gap-ms = /bits/ 16 <0x64>;
 	};
 };
+
+Example for USB device:
+
+&usb_host1_ohci {
+    status = "okay";
+    #address-cells = <1>;
+    #size-cells = <0>;
+
+    mvl_bt1: bt@1 {
+	compatible = "usb1286,204e";
+	reg = <1>;
+	interrupt-parent = <&gpio0>;
+	interrupts = <119 IRQ_TYPE_LEVEL_LOW>;
+	marvell,wakeup-pin = /bits/ 16 <0x0d>;
+	marvell,wakeup-gap-ms = /bits/ 16 <0x64>;
+    };
+};
drivers/bluetooth/btusb.c +59 -0

71 lines not on this page · raw message

Signature · 1 line
2.8.0.rc3.226.g39d4020
#4 lore raw

Re: [PATCH 2/3] Bluetooth: btusb: Add out-of-band wakeup support

In reply to a message not in the archive

Hi,

Quoted 35 lines · Rajat Jain

Some BT chips (e.g. Marvell 8997) contain a wakeup pin that can be connected to a gpio on the CPU side, and can be used to wakeup the host out-of-band. This can be useful in situations where the in-band wakeup is not possible or not preferable (e.g. the in-band wakeup may require the USB host controller to remain active, and hence consuming more system power during system sleep).

The oob gpio interrupt to be used for wakeup on the CPU side, is read from the device tree node, (using standard interrupt descriptors). A devcie tree binding document is also added for the driver.

---
 Documentation/devicetree/bindings/net/btusb.txt | 38 ++++++++++++
 drivers/bluetooth/btusb.c                       | 82 +++++++++++++++++++++++++
 2 files changed, 120 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/btusb.txt
Documentation/devicetree/bindings/net/btusb.txt new file +11 -0
new file mode 100644
index 0000000..bb27f92
@@ -0,0 +1,38 @@
+Generic Bluetooth controller over USB (btusb driver)
+---------------------------------------------------
+
+Required properties:
+
+  - compatible : should comply with the format "usbVID,PID" specified in
+		 Documentation/devicetree/bindings/usb/usb-device.txt
+		 At the time of writing, the only OF supported devices
+		 (more may be added later) are:
+
+		  "usb1286,204e" (Marvell 8997)

Documentation/devicetree/bindings/net/btusb.txt line 21

Quoted 10 lines
Documentation/devicetree/bindings/net/btusb.txt new file +10 -0
+
+Optional properties:
+
+  - interrupt-parent: phandle of the parent interrupt controller
+  - interrupts : The first interrupt specified is the interrupt that shall be
+		 used for out-of-band wake-on-bt. Driver will request an irq
+		 based on this interrupt number. During system suspend, the irq
+		 will be enabled so that the bluetooth chip can wakeup host
+		 platform out of band. During system resume, the irq will be
+		 disabled to make sure unnecessary interrupt is not received.

Might it be worthwhile to define an 'interrupt-names' property (e.g., = "wakeup") to help future-proof this?

Quoted 73 lines
Documentation/devicetree/bindings/net/btusb.txt new file +17 -0
+
+Example:
+
+Following example uses irq pin number 3 of gpio0 for out of band wake-on-bt:
+
+&usb_host1_ehci {
+    status = "okay";
+    #address-cells = <1>;
+    #size-cells = <0>;
+
+    mvl_bt1: bt@1 {
+	compatible = "usb1286,204e";
+	reg = <1>;
+	interrupt-parent = <&gpio0>;
+	interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+    };
+};
drivers/bluetooth/btusb.c +27 -0
index ce22cef..32a6f22 100644
@@ -24,6 +24,8 @@
 #include <linux/module.h>
 #include <linux/usb.h>
 #include <linux/firmware.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>
 #include <asm/unaligned.h>
 
 #include <net/bluetooth/bluetooth.h>
@@ -369,6 +371,7 @@ static const struct usb_device_id blacklist_table[] = {
 #define BTUSB_BOOTING		9
 #define BTUSB_RESET_RESUME	10
 #define BTUSB_DIAG_RUNNING	11
+#define BTUSB_OOB_WAKE_DISABLED	12
 
 struct btusb_data {
 	struct hci_dev       *hdev;
@@ -416,6 +419,8 @@ struct btusb_data {
 	int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
 
 	int (*setup_on_usb)(struct hci_dev *hdev);
+
+	int oob_wake_irq;   /* irq for out-of-band wake-on-bt */
 };
 
 static inline void btusb_free_frags(struct btusb_data *data)
@@ -2728,6 +2733,65 @@ static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
 }
 #endif
 
+#ifdef CONFIG_PM
+static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
+{
+	struct btusb_data *data = priv;
+
+	/* Disable only if not already disabled (keep it balanced) */
+	if (!test_and_set_bit(BTUSB_OOB_WAKE_DISABLED, &data->flags)) {
+		disable_irq_wake(irq);
+		disable_irq_nosync(irq);
+	}
+	pm_wakeup_event(&data->udev->dev, 0);
+	return IRQ_HANDLED;
+}
+
+static const struct of_device_id btusb_match_table[] = {
+	{ .compatible = "usb1286,204e" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, btusb_match_table);
+
+/* Use an oob wakeup pin? */
+static int btusb_config_oob_wake(struct hci_dev *hdev)

drivers/bluetooth/btusb.c static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable) · line 2767

Quoted 10 lines
drivers/bluetooth/btusb.c +10 -0
+{
+	struct btusb_data *data = hci_get_drvdata(hdev);
+	struct device *dev = &data->udev->dev;
+	int irq, ret;
+
+	if (!of_match_device(btusb_match_table, dev))
+		return 0;
+
+	/* Move on if no IRQ specified */
+	irq = irq_of_parse_and_map(dev->of_node, 0);

Better to use of_irq_get{,_byname}(), no?

drivers/bluetooth/btusb.c static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable) · line 2776

Quoted 9 lines
drivers/bluetooth/btusb.c +9 -0
+	if (!irq) {
+		bt_dev_dbg(hdev, "%s: no oob wake irq in DT", __func__);
+		return 0;
+	}
+
+	set_bit(BTUSB_OOB_WAKE_DISABLED, &data->flags);
+
+	ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
+			       IRQF_TRIGGER_LOW, "oob wake-on-bt", data);

You're assuming this is level-triggered, and active-low? Can't this just be specified in the device tree and just pass 0 here?

Also, it seems like it would be a lot more convenient if we could treat this as edge-triggered, so we don't have to do the set/clear flags, disable IRQ, etc., dance. You'd just have to change the device tree definition. Is there any downside to doing that?

It would also then be a better candidate for using something like dev_pm_set_dedicated_wake_irq() (although last time I tried using that, it didn't do so great if you don't have autosuspend enabled -- but I think there are patches outstanding for that; so maybe not yet).

Quoted 4 lines
drivers/bluetooth/btusb.c +4 -0
+	if (ret) {
+		bt_dev_err(hdev, "%s: irq request failed", __func__);
+		return ret;
+	}

drivers/bluetooth/btusb.c static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable) · line 2790

Quoted 10 lines
drivers/bluetooth/btusb.c +10 -0
+
+	ret = device_init_wakeup(dev, true);
+	if (ret) {
+		bt_dev_err(hdev, "%s: failed to init_wakeup\n", __func__);
+		return ret;
+	}
+
+	data->oob_wake_irq = irq;
+	disable_irq(irq);
+	bt_dev_info(hdev, "oob wake-on-bt configured at irq %u\n", irq);

oob and bt are typically capitalized in strings. And maybe irq too. Also, you declared irq as 'int', so %d instead of %u.

Brian

Quoted 49 lines
drivers/bluetooth/btusb.c +22 -0
+	return 0;
+}
+#endif
+
 static int btusb_probe(struct usb_interface *intf,
 		       const struct usb_device_id *id)
 {
@@ -2849,6 +2913,11 @@ static int btusb_probe(struct usb_interface *intf,
 	hdev->send   = btusb_send_frame;
 	hdev->notify = btusb_notify;
 
+#ifdef CONFIG_PM
+	err = btusb_config_oob_wake(hdev);
+	if (err)
+		goto out_free_dev;
+#endif
 	if (id->driver_info & BTUSB_CW6622)
 		set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
 
@@ -3089,6 +3158,12 @@ static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
 	btusb_stop_traffic(data);
 	usb_kill_anchored_urbs(&data->tx_anchor);
 
+	if (data->oob_wake_irq) {
+		clear_bit(BTUSB_OOB_WAKE_DISABLED, &data->flags);
+		enable_irq(data->oob_wake_irq);
+		enable_irq_wake(data->oob_wake_irq);
+	}
+
 	/* Optionally request a device reset on resume, but only when
 	 * wakeups are disabled. If wakeups are enabled we assume the
 	 * device will stay powered up throughout suspend.
@@ -3126,6 +3201,13 @@ static int btusb_resume(struct usb_interface *intf)
 	if (--data->suspend_count)
 		return 0;
 
+	/* Disable only if not already disabled (keep it balanced) */
+	if (data->oob_wake_irq &&
+	    !test_and_set_bit(BTUSB_OOB_WAKE_DISABLED, &data->flags)) {
+		disable_irq_wake(data->oob_wake_irq);
+		disable_irq(data->oob_wake_irq);
+	}
+
 	if (!test_bit(HCI_RUNNING, &hdev->flags))
 		goto done;
 
Signature · 1 line
2.8.0.rc3.226.g39d4020
#9 lore raw

Re: [PATCH 3/3] Bluetooth: btusb: Configure Marvel to use one of the pins for oob wakeup

In reply to a message not in the archive

Hi Rajat,
=20
On mer., d=C3=A9c. 14 2016, Rajat Jain <[email protected]> wrote:

In your title unless you speak about the comic books you should do a s/Marvel/Marvell/ :)

Gregory

Quoted 7 lines

The Marvell devices may have many gpio pins, and hence for wakeup on these out-of-band pins, the chip needs to be told which pin is to be used for wakeup, using an hci command.

Thus, we read the pin number etc from the device tree node and send a command to the chip.

Quoted 8 lines

---
Note that while I would have liked to name the compatible string as more like "marvell, usb8997-bt", the devicetrees/bindings/usb/usb-device.txt requires the compatible property to be of the form "usbVID,PID".

 .../{marvell-bt-sd8xxx.txt =3D> marvell-bt-8xxx.txt} | 25 ++++++++-

drivers/bluetooth/btusb.c | 59 ++++++++++++++++=

++++++

Quoted 2 lines
 2 files changed, 82 insertions(+), 2 deletions(-)
 rename Documentation/devicetree/bindings/net/{marvell-bt-sd8xxx.txt =3D>=

marvell-bt-8xxx.txt} (76%)

=

Quoted 2 lines
= renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +0 -0

b/Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt

Quoted 23 lines

similarity index 76%
rename from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt rename to Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt index 6a9a63c..471bef8 100644

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +1 -1
@@ -1,4 +1,4 @@
-Marvell 8897/8997 (sd8897/sd8997) bluetooth SDIO devices
+Marvell 8897/8997 (sd8897/sd8997) bluetooth devices (SDIO or USB based)
 ------

=20=20
Required properties:

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +1 -0
@@ -6,11 +6,13 @@ Required properties:
   - compatible : should be one of the following:
 	* "marvell,sd8897-bt"
 	* "marvell,sd8997-bt"
+	* "usb1286,204e"

=20=20
Optional properties:
=20=20
- marvell,cal-data: Calibration data downloaded to the device during

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +1 -0
 		      initialization. This is an array of 28 values(u8).
+		      This is only applicable to SDIO devices.
Quoted 2 lines

=20=20
- marvell,wakeup-pin: It represents wakeup pin number of the bluetooth=

chip.

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt Example: · line 32

Quoted 4 lines
Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +0 -0
 		        firmware will use the pin to wakeup host system (u16).
@@ -29,7 +31,9 @@ Example:
 IRQ pin 119 is used as system wakeup source interrupt.
 wakeup pin 13 and gap 100ms are configured so that firmware can wakeup h=

ost

drivers/bluetooth/btusb.c static int btusb_shutdown_intel(struct hci_dev *hd=

Quoted 34 lines
Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +3 -1
 using this device side pin and wakeup latency.
-calibration data is also available in below example.
+
+Example for SDIO device follows (calibration data is also available in
+below example).

=20=20
&mmc3 {

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +17 -0
 	status =3D "okay";
@@ -54,3 +58,20 @@ calibration data is also available in below example.
 		marvell,wakeup-gap-ms =3D /bits/ 16 <0x64>;
 	};
 };
+
+Example for USB device:
+
+&usb_host1_ohci {
+    status =3D "okay";
+    #address-cells =3D <1>;
+    #size-cells =3D <0>;
+
+    mvl_bt1: bt@1 {
+	compatible =3D "usb1286,204e";
+	reg =3D <1>;
+	interrupt-parent =3D <&gpio0>;
+	interrupts =3D <119 IRQ_TYPE_LEVEL_LOW>;
+	marvell,wakeup-pin =3D /bits/ 16 <0x0d>;
+	marvell,wakeup-gap-ms =3D /bits/ 16 <0x64>;
+    };
+};
drivers/bluetooth/btusb.c +0 -0
index 32a6f22..99d7f6d 100644
@@ -2343,6 +2343,58 @@ static int btusb_shutdown_intel(struct hci_dev *hd=

ev)

drivers/bluetooth/btusb.c static int btusb_shutdown_intel(struct hci_dev *hdev)

Quoted 11 lines
drivers/bluetooth/btusb.c +0 -0
 	return 0;
 }

=20=20

drivers/bluetooth/btusb.c +8 -0
+#ifdef CONFIG_PM
+static const struct of_device_id mvl_oob_wake_match_table[] =3D {
+	{ .compatible =3D "usb1286,204e" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, mvl_oob_wake_match_table);
+
+/* Configure an out-of-band gpio as wake-up pin, if specified in device =

tree */

Quoted 13 lines
drivers/bluetooth/btusb.c +13 -0
+static int marvell_config_oob_wake(struct hci_dev *hdev)
+{
+	struct sk_buff *skb;
+	struct btusb_data *data =3D hci_get_drvdata(hdev);
+	struct device *dev =3D &data->udev->dev;
+	u16 pin, gap, opcode;
+	int ret;
+	u8 cmd[5];
+
+	if (!of_match_device(mvl_oob_wake_match_table, dev))
+		return 0;
+
+	if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||

drivers/bluetooth/btusb.c static int btusb_shutdown_intel(struct hci_dev *hdev)

Quoted 10 lines
drivers/bluetooth/btusb.c +10 -0
+	    of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
+		return -EINVAL;
+
+	/* Vendor specific command to configure a GPIO as wake-up pin */
+	opcode =3D hci_opcode_pack(0x3F, 0x59);
+	cmd[0] =3D opcode & 0xFF;
+	cmd[1] =3D opcode >> 8;
+	cmd[2] =3D 2; /* length of parameters that follow */
+	cmd[3] =3D pin;
+	cmd[4] =3D gap; /* time in ms, for which wakeup pin should be asserted =

*/

Quoted 39 lines
drivers/bluetooth/btusb.c +28 -1
+
+	skb =3D bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
+	if (!skb) {
+		bt_dev_err(hdev, "%s: No memory\n", __func__);
+		return -ENOMEM;
+	}
+
+	memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
+	hci_skb_pkt_type(skb) =3D HCI_COMMAND_PKT;
+
+	ret =3D btusb_send_frame(hdev, skb);
+	if (ret) {
+		bt_dev_err(hdev, "%s: configuration failed\n", __func__);
+		kfree_skb(skb);
+		return ret;
+	}
+
+	return 0;
+}
+#endif
+
 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
 				    const bdaddr_t *bdaddr)
 {
@@ -2917,6 +2969,13 @@ static int btusb_probe(struct usb_interface *intf,
 	err =3D btusb_config_oob_wake(hdev);
 	if (err)
 		goto out_free_dev;
+
+	/* Marvel devices may need a specific chip configuration */
+	if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
+		err =3D marvell_config_oob_wake(hdev);
+		if (err)
+			goto out_free_dev;
+	}
 #endif
 	if (id->driver_info & BTUSB_CW6622)
 		set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
--=20
Quoted 2 lines

2.8.0.rc3.226.g39d4020

--=20
Gregory Clement, Free Electrons
Kernel, drivers, real-time and embedded Linux
development, consulting, training and support.
http://free-electrons.com

author
#10 lore raw

Re: [PATCH 3/3] Bluetooth: btusb: Configure Marvel to use one of the pins for oob wakeup

Quoted 7 lines · Gregory CLEMENT

Hi Rajat,

On mer., déc. 14 2016, Rajat Jain <[email protected]> wrote:

In your title unless you speak about the comic books you should do a s/Marvel/Marvell/ :)

Oops :-) Will do, thanks!

Quoted 178 lines

Gregory

Quoted 7 lines

The Marvell devices may have many gpio pins, and hence for wakeup on these out-of-band pins, the chip needs to be told which pin is to be used for wakeup, using an hci command.

Thus, we read the pin number etc from the device tree node and send a command to the chip.

Quoted 8 lines

---
Note that while I would have liked to name the compatible string as more like "marvell, usb8997-bt", the devicetrees/bindings/usb/usb-device.txt requires the compatible property to be of the form "usbVID,PID".

 .../{marvell-bt-sd8xxx.txt => marvell-bt-8xxx.txt} | 25 ++++++++-
 drivers/bluetooth/btusb.c                          | 59

++++++++++++++++++++++

Quoted 2 lines
 2 files changed, 82 insertions(+), 2 deletions(-)
 rename Documentation/devicetree/bindings/net/{marvell-bt-sd8xxx.txt =>

marvell-bt-8xxx.txt} (76%)

Review

Quoted 2 lines
(quoted diff) +0 -0

b/Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt

Quoted 15 lines

similarity index 76%
rename from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt rename to Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt index 6a9a63c..471bef8 100644

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +1 -1
@@ -1,4 +1,4 @@
-Marvell 8897/8997 (sd8897/sd8997) bluetooth SDIO devices
+Marvell 8897/8997 (sd8897/sd8997) bluetooth devices (SDIO or USB based)
 ------
 
 Required properties:
@@ -6,11 +6,13 @@ Required properties:
   - compatible : should be one of the following:
      * "marvell,sd8897-bt"

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt Required properties: · line 17

Quoted 10 lines
Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +2 -0
      * "marvell,sd8997-bt"
+     * "usb1286,204e"
 
 Optional properties:
 
   - marvell,cal-data: Calibration data downloaded to the device during
                    initialization. This is an array of 28 values(u8).
+                   This is only applicable to SDIO devices.
 
   - marvell,wakeup-pin: It represents wakeup pin number of the

bluetooth chip.

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt Required properties: · line 18

Quoted 1 line
Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +0 -0
                      firmware will use the pin to wakeup host system

(u16).

Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt Example: · line 32

Quoted 3 lines
Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +0 -0
@@ -29,7 +31,9 @@ Example:
 IRQ pin 119 is used as system wakeup source interrupt.
 wakeup pin 13 and gap 100ms are configured so that firmware can wakeup

host

drivers/bluetooth/btusb.c static int btusb_shutdown_intel(struct hci_dev

Quoted 34 lines
Documentation/devicetree/bindings/net/marvell-bt-8xxx.txt renamed from Documentation/devicetree/bindings/net/marvell-bt-sd8xxx.txt +20 -1
 using this device side pin and wakeup latency.
-calibration data is also available in below example.
+
+Example for SDIO device follows (calibration data is also available in
+below example).
 
 &mmc3 {
      status = "okay";
@@ -54,3 +58,20 @@ calibration data is also available in below example.
              marvell,wakeup-gap-ms = /bits/ 16 <0x64>;
      };
 };
+
+Example for USB device:
+
+&usb_host1_ohci {
+    status = "okay";
+    #address-cells = <1>;
+    #size-cells = <0>;
+
+    mvl_bt1: bt@1 {
+     compatible = "usb1286,204e";
+     reg = <1>;
+     interrupt-parent = <&gpio0>;
+     interrupts = <119 IRQ_TYPE_LEVEL_LOW>;
+     marvell,wakeup-pin = /bits/ 16 <0x0d>;
+     marvell,wakeup-gap-ms = /bits/ 16 <0x64>;
+    };
+};
drivers/bluetooth/btusb.c +0 -0
index 32a6f22..99d7f6d 100644
@@ -2343,6 +2343,58 @@ static int btusb_shutdown_intel(struct hci_dev

*hdev)

drivers/bluetooth/btusb.c static int btusb_shutdown_intel(struct hci_dev *hdev)

Quoted 11 lines
drivers/bluetooth/btusb.c +8 -0
      return 0;
 }
 
+#ifdef CONFIG_PM
+static const struct of_device_id mvl_oob_wake_match_table[] = {
+     { .compatible = "usb1286,204e" },
+     { }
+};
+MODULE_DEVICE_TABLE(of, mvl_oob_wake_match_table);
+
+/* Configure an out-of-band gpio as wake-up pin, if specified in device

tree */

drivers/bluetooth/btusb.c static int btusb_shutdown_intel(struct hci_dev *hdev)

Quoted 13 lines
drivers/bluetooth/btusb.c +13 -0
+static int marvell_config_oob_wake(struct hci_dev *hdev)
+{
+     struct sk_buff *skb;
+     struct btusb_data *data = hci_get_drvdata(hdev);
+     struct device *dev = &data->udev->dev;
+     u16 pin, gap, opcode;
+     int ret;
+     u8 cmd[5];
+
+     if (!of_match_device(mvl_oob_wake_match_table, dev))
+             return 0;
+
+     if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin",

&pin) ||

drivers/bluetooth/btusb.c static int btusb_shutdown_intel(struct hci_dev · line 2367

Quoted 1 line
drivers/bluetooth/btusb.c +1 -0
+         of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms",

&gap))

drivers/bluetooth/btusb.c static int btusb_shutdown_intel(struct hci_dev *hdev)

Quoted 9 lines
drivers/bluetooth/btusb.c +9 -0
+             return -EINVAL;
+
+     /* Vendor specific command to configure a GPIO as wake-up pin */
+     opcode = hci_opcode_pack(0x3F, 0x59);
+     cmd[0] = opcode & 0xFF;
+     cmd[1] = opcode >> 8;
+     cmd[2] = 2; /* length of parameters that follow */
+     cmd[3] = pin;
+     cmd[4] = gap; /* time in ms, for which wakeup pin should be

asserted */

Quoted 39 lines
drivers/bluetooth/btusb.c +28 -1
+
+     skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
+     if (!skb) {
+             bt_dev_err(hdev, "%s: No memory\n", __func__);
+             return -ENOMEM;
+     }
+
+     memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
+     hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
+
+     ret = btusb_send_frame(hdev, skb);
+     if (ret) {
+             bt_dev_err(hdev, "%s: configuration failed\n", __func__);
+             kfree_skb(skb);
+             return ret;
+     }
+
+     return 0;
+}
+#endif
+
 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
                                  const bdaddr_t *bdaddr)
 {
@@ -2917,6 +2969,13 @@ static int btusb_probe(struct usb_interface *intf,
      err = btusb_config_oob_wake(hdev);
      if (err)
              goto out_free_dev;
+
+     /* Marvel devices may need a specific chip configuration */
+     if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
+             err = marvell_config_oob_wake(hdev);
+             if (err)
+                     goto out_free_dev;
+     }
 #endif
      if (id->driver_info & BTUSB_CW6622)
              set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
--
Quoted 2 lines

2.8.0.rc3.226.g39d4020

--
Gregory Clement, Free Electrons
Kernel, drivers, real-time and embedded Linux
development, consulting, training and support.
http://free-electrons.com

#6 lore raw

Re: [PATCH 2/3] Bluetooth: btusb: Add out-of-band wakeup support

In reply to a message not in the archive

Hi Brian,

I've just posted a v2 patchset after taking care of your your comments. Please see inline below.

Quoted 43 lines · Brian Norris

Hi,

Quoted 40 lines · Rajat Jain

Some BT chips (e.g. Marvell 8997) contain a wakeup pin that can be connected to a gpio on the CPU side, and can be used to wakeup the host out-of-band. This can be useful in situations where the in-band wakeup is not possible or not preferable (e.g. the in-band wakeup may require the USB host controller to remain active, and hence consuming more system power during system sleep).

The oob gpio interrupt to be used for wakeup on the CPU side, is read from the device tree node, (using standard interrupt descriptors). A devcie tree binding document is also added for the driver.

---
 Documentation/devicetree/bindings/net/btusb.txt | 38 ++++++++++++
 drivers/bluetooth/btusb.c                       | 82 +++++++++++++++++++++++++
 2 files changed, 120 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/btusb.txt
Documentation/devicetree/bindings/net/btusb.txt new file +16 -0
new file mode 100644
index 0000000..bb27f92
@@ -0,0 +1,38 @@
+Generic Bluetooth controller over USB (btusb driver)
+---------------------------------------------------
+
+Required properties:
+
+  - compatible : should comply with the format "usbVID,PID" specified in
+              Documentation/devicetree/bindings/usb/usb-device.txt
+              At the time of writing, the only OF supported devices
+              (more may be added later) are:
+
+               "usb1286,204e" (Marvell 8997)
+
+Optional properties:
+
+  - interrupt-parent: phandle of the parent interrupt controller
+  - interrupts : The first interrupt specified is the interrupt that shall be
Quoted 8 lines

Documentation/devicetree/bindings/net/btusb.txt line 21

Quoted 5 lines
Documentation/devicetree/bindings/net/btusb.txt new file +5 -0
+              used for out-of-band wake-on-bt. Driver will request an irq
+              based on this interrupt number. During system suspend, the irq
+              will be enabled so that the bluetooth chip can wakeup host
+              platform out of band. During system resume, the irq will be
+              disabled to make sure unnecessary interrupt is not received.

Might it be worthwhile to define an 'interrupt-names' property (e.g., = "wakeup") to help future-proof this?

Good idea, I've used the same.

Quoted 78 lines
Quoted 78 lines
Documentation/devicetree/bindings/net/btusb.txt new file +17 -0
+
+Example:
+
+Following example uses irq pin number 3 of gpio0 for out of band wake-on-bt:
+
+&usb_host1_ehci {
+    status = "okay";
+    #address-cells = <1>;
+    #size-cells = <0>;
+
+    mvl_bt1: bt@1 {
+     compatible = "usb1286,204e";
+     reg = <1>;
+     interrupt-parent = <&gpio0>;
+     interrupts = <3 IRQ_TYPE_LEVEL_LOW>;
+    };
+};
drivers/bluetooth/btusb.c +31 -0
index ce22cef..32a6f22 100644
@@ -24,6 +24,8 @@
 #include <linux/module.h>
 #include <linux/usb.h>
 #include <linux/firmware.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>
 #include <asm/unaligned.h>
 
 #include <net/bluetooth/bluetooth.h>
@@ -369,6 +371,7 @@ static const struct usb_device_id blacklist_table[] = {
 #define BTUSB_BOOTING                9
 #define BTUSB_RESET_RESUME   10
 #define BTUSB_DIAG_RUNNING   11
+#define BTUSB_OOB_WAKE_DISABLED      12
 
 struct btusb_data {
      struct hci_dev       *hdev;
@@ -416,6 +419,8 @@ struct btusb_data {
      int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
 
      int (*setup_on_usb)(struct hci_dev *hdev);
+
+     int oob_wake_irq;   /* irq for out-of-band wake-on-bt */
 };
 
 static inline void btusb_free_frags(struct btusb_data *data)
@@ -2728,6 +2733,65 @@ static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
 }
 #endif
 
+#ifdef CONFIG_PM
+static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
+{
+     struct btusb_data *data = priv;
+
+     /* Disable only if not already disabled (keep it balanced) */
+     if (!test_and_set_bit(BTUSB_OOB_WAKE_DISABLED, &data->flags)) {
+             disable_irq_wake(irq);
+             disable_irq_nosync(irq);
+     }
+     pm_wakeup_event(&data->udev->dev, 0);
+     return IRQ_HANDLED;
+}
+
+static const struct of_device_id btusb_match_table[] = {
+     { .compatible = "usb1286,204e" },
+     { }
+};
+MODULE_DEVICE_TABLE(of, btusb_match_table);
+
+/* Use an oob wakeup pin? */
+static int btusb_config_oob_wake(struct hci_dev *hdev)
+{
+     struct btusb_data *data = hci_get_drvdata(hdev);
+     struct device *dev = &data->udev->dev;
+     int irq, ret;
Quoted 8 lines

drivers/bluetooth/btusb.c static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable) · line 2767

Quoted 6 lines
drivers/bluetooth/btusb.c +6 -0
+
+     if (!of_match_device(btusb_match_table, dev))
+             return 0;
+
+     /* Move on if no IRQ specified */
+     irq = irq_of_parse_and_map(dev->of_node, 0);

Better to use of_irq_get{,_byname}(), no?

I've used_irq_get_byname().

Quoted 14 lines

drivers/bluetooth/btusb.c static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable) · line 2776

Quoted 10 lines
drivers/bluetooth/btusb.c +9 -0
+     if (!irq) {
+             bt_dev_dbg(hdev, "%s: no oob wake irq in DT", __func__);
+             return 0;
+     }
+
+     set_bit(BTUSB_OOB_WAKE_DISABLED, &data->flags);
+
+     ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
+                            IRQF_TRIGGER_LOW, "oob wake-on-bt", data);

You're assuming this is level-triggered, and active-low? Can't this just be specified in the device tree and just pass 0 here?

Quoted 4 lines

Also, it seems like it would be a lot more convenient if we could treat this as edge-triggered, so we don't have to do the set/clear flags, disable IRQ, etc., dance. You'd just have to change the device tree definition. Is there any downside to doing that?

Now, I don't put any assumptions in the driver and use whatever is specified in the device tree. So platforms can do whatever they want (However, I think configuring as edge triggered in the device tree (if some platform chooses to) may leave some corner cases where the interrupt might be missed if it was already asserted for some reason).

Thanks,

Rajat

Quoted 75 lines

It would also then be a better candidate for using something like dev_pm_set_dedicated_wake_irq() (although last time I tried using that, it didn't do so great if you don't have autosuspend enabled -- but I think there are patches outstanding for that; so maybe not yet).

Quoted 4 lines
drivers/bluetooth/btusb.c +4 -0
+     if (ret) {
+             bt_dev_err(hdev, "%s: irq request failed", __func__);
+             return ret;
+     }

drivers/bluetooth/btusb.c static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable) · line 2790

Quoted 10 lines
drivers/bluetooth/btusb.c +10 -0
+
+     ret = device_init_wakeup(dev, true);
+     if (ret) {
+             bt_dev_err(hdev, "%s: failed to init_wakeup\n", __func__);
+             return ret;
+     }
+
+     data->oob_wake_irq = irq;
+     disable_irq(irq);
+     bt_dev_info(hdev, "oob wake-on-bt configured at irq %u\n", irq);

oob and bt are typically capitalized in strings. And maybe irq too. Also, you declared irq as 'int', so %d instead of %u.

Brian

Quoted 49 lines
drivers/bluetooth/btusb.c +22 -1
+     return 0;
+}
+#endif
+
 static int btusb_probe(struct usb_interface *intf,
                     const struct usb_device_id *id)
 {
@@ -2849,6 +2913,11 @@ static int btusb_probe(struct usb_interface *intf,
      hdev->send   = btusb_send_frame;
      hdev->notify = btusb_notify;
 
+#ifdef CONFIG_PM
+     err = btusb_config_oob_wake(hdev);
+     if (err)
+             goto out_free_dev;
+#endif
      if (id->driver_info & BTUSB_CW6622)
              set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
 
@@ -3089,6 +3158,12 @@ static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
      btusb_stop_traffic(data);
      usb_kill_anchored_urbs(&data->tx_anchor);
 
+     if (data->oob_wake_irq) {
+             clear_bit(BTUSB_OOB_WAKE_DISABLED, &data->flags);
+             enable_irq(data->oob_wake_irq);
+             enable_irq_wake(data->oob_wake_irq);
+     }
+
      /* Optionally request a device reset on resume, but only when
       * wakeups are disabled. If wakeups are enabled we assume the
       * device will stay powered up throughout suspend.
@@ -3126,6 +3201,13 @@ static int btusb_resume(struct usb_interface *intf)
      if (--data->suspend_count)
              return 0;
 
+     /* Disable only if not already disabled (keep it balanced) */
+     if (data->oob_wake_irq &&
+         !test_and_set_bit(BTUSB_OOB_WAKE_DISABLED, &data->flags)) {
+             disable_irq_wake(data->oob_wake_irq);
+             disable_irq(data->oob_wake_irq);
+     }
+
      if (!test_bit(HCI_RUNNING, &hdev->flags))
              goto done;
 
--

2.8.0.rc3.226.g39d4020

maintainer Acked-by on v1 3/3
#11 lore raw

Re: [PATCH 3/3] Bluetooth: btusb: Configure Marvel to use one of the pins for oob wakeup

In reply to Rajat Jain
Quoted 7 lines · Rajat Jain

The Marvell devices may have many gpio pins, and hence for wakeup on these out-of-band pins, the chip needs to be told which pin is to be used for wakeup, using an hci command.

Thus, we read the pin number etc from the device tree node and send a command to the chip.

Quoted 7 lines

---
Note that while I would have liked to name the compatible string as more like "marvell, usb8997-bt", the devicetrees/bindings/usb/usb-device.txt requires the compatible property to be of the form "usbVID,PID".

 .../{marvell-bt-sd8xxx.txt => marvell-bt-8xxx.txt} | 25 ++++++++-
Quoted 3 lines
 drivers/bluetooth/btusb.c                          | 59 ++++++++++++++++++++++
 2 files changed, 82 insertions(+), 2 deletions(-)
 rename Documentation/devicetree/bindings/net/{marvell-bt-sd8xxx.txt => marvell-bt-8xxx.txt} (76%)