Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth Problem #29

Open
peterychuang opened this issue Aug 5, 2017 · 309 comments
Open

Bluetooth Problem #29

peterychuang opened this issue Aug 5, 2017 · 309 comments

Comments

@peterychuang
Copy link
Contributor

So I was poking around the Bluetooth, and I noticed that the DSDT contains this:

If (!OSDW ())
{
    Return (UBUF) /* \_SB_.PCI0.URT0.BLTH._CRS.UBUF */
}

Return (ABUF) /* \_SB_.PCI0.URT0.BLTH._CRS.ABUF */

This might be a stupid question, but isn't this bit the same as the SPI stuff that used to require patching to get the keyboard and touchpad working?

@roadrunner2
Copy link
Contributor

@peterychuang The question is far from stupid: you're quite right, this looks very much like SPI stuff and it looks like it would benefit from the generic apple-properties work @l1k has done.

However, on closer examination it turns out the issue is not the missing acpi serial-bus resource, but the missing interrupt information: there's neither an interrupt-resource in the _CRS, nor is there a _GPE name like there is for the SPI device, and hence we get these hci_bcm errors in dmesg:

hci_bcm BCM2E7C:00: BCM irq: -22
hci_bcm BCM2E7C:00: invalid platform data
hci_bcm: probe of BCM2E7C:00 failed with error -22

In fact, I can't find any interrupt info whatsoever for the bluetooth device in the DSDT.

So the first thing to get bluetooth running is to figure out what interrupt it uses. After that, we'll need some small patches to set the oper_speed from the baud apple property, and probably something to call the power-down, power-up, etc acpi methods appropriately (however, I know next to nothing about bluetooth and their drivers in Linux, so take this with a large grain of salt).

@l1k
Copy link

l1k commented Aug 6, 2017

Yes I did mention in the commit message of l1k/linux@9ae5c93 that it'll be needed for Bluetooth as well. :-)

Looking at the schematics of the MacBook8,1 I can't find a Bluetooth interrupt pin and apparently none is needed. A quick Google search turns up this:

To a host system, the UART appears as an 8-bit input and output port that it can read from and write to. Whenever the host has data to be sent, it just sends these data to the UART in byte format (8-bit wide), whenever the UART receives data from another serial device it will buffer these data in its FIFO (again 8-bit wide), then it will indicate the availability of these data to the host through an internal register bit, or through a hardware interrupt signal.

So the interrupt is coming from the UART on behalf of the Bluetooth device once data is received into the FIFO.

What's much more interesting are the power management methods which could be used to extend battery life:

  • BTPU = BlueTooth Power Up
  • BTPD = BlueTooth Power Down
  • BTRS = BlueTooth Reset (power down followed by power up)
  • BTLP = BlueTooth Low Power (takes argument 0 or 1, apparently toggles sleep mode)

The MBP13,3 has an additional BTRB method. No idea. The methods are called from IOBluetoothHostControllerUARTTransport.kext and various others.

@l1k
Copy link

l1k commented Aug 6, 2017

Ah looking at hci_bcm.c I realize this is not about an interrupt for data reception but for host wakeup. On the MB8,1 that pin is called SMC_PME_S4_WAKE_L and goes into the SMC, the SMC then wakes the system. So this happens transparently to the OS on MacBooks and the driver need not care about it.

The invalid platform data error is emitted because neither the device_wakeup nor shutdown GPIO descriptors are set. On MacBooks the driver should instead detect presence of the BTLP and BTPD/BTPU methods. The BTLP method should be called for device wakeup and BTPD/BTPU for shutdown. The driver should cache the ACPI handles of the methods on probe and invoke them from bcm_gpio_set_power(), bcm_suspend_device() and bcm_resume_device().

Actually this looks fairly straightforward.

@roadrunner2
Copy link
Contributor

Ah, silly me, didn't actually look at who was using the interrupt for what. Ok, that indeed simplifies things then.

Regarding sleep and wakeup, the DSDT seems to indicate that BTLP should be called for both, at least on Windows:

    Method (_PTS, 1, NotSerialized)  // _PTS: Prepare To Sleep
        ...
        If (!OSDW ())
        {
            If (Arg0 == 0x03)
            {
                \_SB.PCI0.URT0.BLTH.BTLP (One)
                Sleep (0x03E8)
                \_SB.PCI0.LPCB.EC.EWPM = One
                \_SB.PCI0.LPCB.EC.EWDK = One
            }
        }
    }

    Method (_WAK, 1, NotSerialized)  // _WAK: Wake
    {
        ...
        If (OSDW ()) {}
        ElseIf (Arg0 == 0x03)
        {
            \_SB.PCI0.URT0.BLTH.BTLP (Zero)
        }
    }

This seems a bit odd, though: I (probably naïvely) would expect the runtime PM (bcm_suspend_device and bcm_resume_device) should be calling BTLP, and the system sleep (bcm_suspend and bcm_resume) to call BTPD/BTPU.

And yes, I haven't been able to figure out what the BTRB method is supposed to be for either.

@l1k
Copy link

l1k commented Aug 6, 2017

I guess BTLP puts the device in a low power state and enables wakeup. Based on the DSDT snippet you included above it seems Apple also wants this to work in sleep state S3, e.g. to wake the system upon key press on a Bluetooth keyboard. Makes sense to me. For some reason BTLP is not called in the DSDT of the MacBook8,1, this might be a bug.

In any case hci_bcm.c calls BTLP both for runtime suspend as well as system sleep, which is in line with what Apple does in the DSDT, so it seems fine to me. The hci_bcm.c driver only toggles the power enable pin (BTPU/BTPD) upon open() and close() of the device, so apparently it's powered down if Bluetooth isn't used at all.

@peterychuang
Copy link
Contributor Author

peterychuang commented Aug 6, 2017

In my machine (14,1), I actually can't find the \_SB.PCI0.URT0.BLTH.BTLP bits @roadrunner2 had.

In case this helps, this is (maybe) all the bluetooth related stuff from DSDT in my machine.

@roadrunner2
Copy link
Contributor

The behaviours seem to be a bit all over the map, then: Looking at MacBook8,1 and MacBook9,1 DSDT's I see that _PTS and _WAK call BTPD and BTPU on the 8, respectively, but on the 9 there are no calls to bluetooth there at all, like on the 14,1.

@l1k I'm slightly confused by your 2nd paragraph about hci_bcm.c: are you saying there's already code there that calls these BTxx acpi methods?

@l1k
Copy link

l1k commented Aug 7, 2017

Sorry for not being clear, let me try again. :-)

The combined WiFi/BT chip on these machines has 3 GPIO pins of interest for BT power management: power enable (toggled by BTPU/BTPD), device wake (toggled by BTLP, active low) and host wake (going out of the chip and into the SMC for system wake). I meant to say hci_bcm.c already contains all the necessary code to toggle the GPIO pins, however it assumes that the pins are directly toggled by the driver via the GPIO subsystem. That assumption is not correct on Macs where Apple invented their own abstraction (consisting of the three ACPI methods) to hide the specific pins used on the PCH (which may vary between models).

The driver seems to do the right thing by enabling device wake both for runtime suspend as well as system sleep, I wouldn't worry too much about presence or nonpresence of BTLP in _PTS and _WAK. It's possible that they're invoking it from their Bootcamp driver on newer models, or they just forgot to enable BT power saving on Windows.

So all that's needed is to search for the BTPU/BTPD/BTLP methods in the ->probe hook if x86_apple_machine is true, cache them in struct bcm_device and amend all the places where gpiod_set_value() is called to invoke the appropriate ACPI methods (again, if x86_apple_machine is true).

And perhaps it's also necessary to fetch device properties and adjust baud rate etc.

Let me know if my answer is still too cryptic. :-)

@roadrunner2
Copy link
Contributor

Thanks for the elaboration. Ok, so that basically matches my thinking.

@l1k
Copy link

l1k commented Aug 13, 2017

I don't know if anyone has started working on this, but in case noone has I've cooked up this branch. Top-most commit adds support for Apple's Bluetooth ACPI methods, the three preceding commits are from the spi_properties material queued for 4.14. Perhaps someone can test if this works, I don't have one of the affected machines. Thanks.

@Dunedan
Copy link
Owner

Dunedan commented Aug 13, 2017

@l1k: I tried out your patch, but it still doesn't work. In fact it doesn't look different to the behavior before, printing the same error messages @roadrunner2 mentioned above. Any idea how to debug this?

@peterychuang
Copy link
Contributor Author

peterychuang commented Aug 13, 2017

@l1k I can confirm the patch doesn't work. Below is part of the dmesg:

[    7.708006] Bluetooth: Core ver 2.22
[    7.708051] Bluetooth: HCI device and connection manager initialized
[    7.708054] Bluetooth: HCI socket layer initialized
[    7.708056] Bluetooth: L2CAP socket layer initialized
[    7.708063] Bluetooth: SCO socket layer initialized
[    7.712788] Bluetooth: HCI UART driver ver 2.3
[    7.712789] Bluetooth: HCI UART protocol H4 registered
[    7.712790] Bluetooth: HCI UART protocol BCSP registered
[    7.712790] Bluetooth: HCI UART protocol ATH3K registered
[    7.712791] Bluetooth: HCI UART protocol Three-wire (H5) registered
[    7.712817] Bluetooth: HCI UART protocol Intel registered
[    7.717682] dw-apb-uart.2: ttyS0 at MMIO 0x9282b000 (irq = 20, base_baud = 3000000) is a 16550A
[    7.753856] hci_bcm BCM2E7C:00: BCM irq: -22
[    7.753858] hci_bcm BCM2E7C:00: invalid platform data
[    7.753871] hci_bcm: probe of BCM2E7C:00 failed with error -22
[    7.755161] Bluetooth: HCI UART protocol Broadcom registered
[    7.755161] Bluetooth: HCI UART protocol QCA registered
[    7.755162] Bluetooth: HCI UART protocol AG6XX registered
[   10.383828] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[   10.383829] Bluetooth: BNEP filters: protocol multicast
[   10.383831] Bluetooth: BNEP socket layer initialized

Edit: some additional error messages while running btattach --bredr /dev/ttyS0 -P bcm

[ 3970.771762] Bluetooth: hci0 command 0xfc45 tx timeout
[ 3978.665055] Bluetooth: hci0: BCM: failed to write clock (-110)
[ 3980.798334] Bluetooth: hci0 command 0x0c03 tx timeout
[ 3988.691712] Bluetooth: hci0: BCM: Reset failed (-110)

@l1k
Copy link

l1k commented Aug 13, 2017

@Dunedan @peterychuang: Thanks for testing, looking at the code with a fresh pair of eyeballs I noticed that I had botched the return code of bcm_apple_probe(), it would return 1 on success and the caller was expecting 0 on success. Should be fixed now and I've added a commit on top to emit some debug printks. Thanks!

@peterychuang
Copy link
Contributor Author

@l1k unfortunately it's still not working. Here are the error messages, including the new debug messages from your last commit:

[    9.902071] Bluetooth: Core ver 2.22
[    9.902082] Bluetooth: HCI device and connection manager initialized
[    9.902084] Bluetooth: HCI socket layer initialized
[    9.902086] Bluetooth: L2CAP socket layer initialized
[    9.902090] Bluetooth: SCO socket layer initialized
[    9.908164] Bluetooth: HCI UART driver ver 2.3
[    9.908166] Bluetooth: HCI UART protocol H4 registered
[    9.908166] Bluetooth: HCI UART protocol BCSP registered
[    9.908167] Bluetooth: HCI UART protocol ATH3K registered
[    9.908167] Bluetooth: HCI UART protocol Three-wire (H5) registered
[    9.908189] Bluetooth: HCI UART protocol Intel registered
[    9.923770] hci_bcm BCM2E7C:00: BCM irq: -22
[    9.923773] hci_bcm BCM2E7C:00: oper_speed=3000000
[    9.923792] hci_bcm BCM2E7C:00: BCM2E7C:00 device registered.
[    9.944515] Bluetooth: HCI UART protocol Broadcom registered
[    9.944517] Bluetooth: HCI UART protocol QCA registered
[    9.944518] Bluetooth: HCI UART protocol AG6XX registered
[   10.146949] dw-apb-uart.2: ttyS0 at MMIO 0x9282b000 (irq = 20, base_baud = 3000000) is a 16550A
[   12.409042] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[   12.409044] Bluetooth: BNEP filters: protocol multicast
[   12.409047] Bluetooth: BNEP socket layer initialized

The invalid platform data seems to have gone.

@l1k
Copy link

l1k commented Aug 13, 2017

@peterychuang: Looks good! The BCM irq: -22 message is normal, we just don't need the IRQ on Macs. What exactly is not working?

BTW I notice that the baud rate (3000000) was already reported by dw-apb-uart without this patch. Perhaps it's not necessary to retrieve the baud device property...

@l1k
Copy link

l1k commented Aug 13, 2017

Lest I forget, for the folks with a MacBook8,1: There's a peculiarity on that machine where the UART0 is attached not only to the Bluetooth controller, but also to a debug serial port on the SSD. There's a mux to switch the UART lines between the two, controlled by GPIO36 on the PCH. There is no ACPI method to control this pin directly. I assume it's set by the BIOS but there's a possibility that this GPIO needs to be toggled before Bluetooth will work.

This oddity is not present in the MBP13,3 ACPI dump. I don't have any others for these newer machines.

@christophgysin
Copy link
Contributor

I just tried the patch on a MBP13,1 and the platform error is gone. /sys/class/bluetooth is still empty though.

I added my DSDT in #30 if that's any help.

@peterychuang
Copy link
Contributor Author

@l1k At the moment, I'm not sure. On the surface, the behaviour of the the machine remains more or less unchanged, though I guess we're on the right track.

@roadrunner2
Copy link
Contributor

I had tried something like this a week ago (well, just disabled the check that caused the error) and also found that /sys/class/bluetooth/ stayed empty - haven't had the time to dig further yet, though.

@peterychuang
Copy link
Contributor Author

/sys/class/bluetooth has stuff inside when I run btattach --bredr /dev/ttyS0 -P bcm. Not that bluetooth works in that circumstance though.

@leifliddy
Copy link

leifliddy commented Sep 21, 2017

dmesg output from MacBook9,1
**added print statements so I could follow the flow of the driver (start with xxx)

[    4.407901] SELinux:  Class bluetooth_socket not defined in policy.
[    5.039534] Bluetooth: Core ver 2.22
[    5.039548] Bluetooth: HCI device and connection manager initialized
[    5.039551] Bluetooth: HCI socket layer initialized
[    5.039554] Bluetooth: L2CAP socket layer initialized
[    5.039561] Bluetooth: SCO socket layer initialized
[    5.259673] Bluetooth: HCI UART driver ver 2.3
[    5.259674] Bluetooth: HCI UART protocol H4 registered
[    5.259674] Bluetooth: HCI UART protocol BCSP registered
[    5.259690] Bluetooth: HCI UART protocol LL registered
[    5.259690] Bluetooth: HCI UART protocol ATH3K registered
[    5.259691] Bluetooth: HCI UART protocol Three-wire (H5) registered
[    5.259715] Bluetooth: HCI UART protocol Intel registered
[    5.259731] Bluetooth: xxx bcm_probe start...
[    5.259732] Bluetooth: xxx bcm_acpi_probe start...
[    5.259733] Bluetooth: xxx bcm_platform_probe start...
[    5.259852] hci_bcm BCM2E7C:00: BCM irq: -22
[    5.259852] Bluetooth: xxx bcm_apple_probe start...
[    5.259854] hci_bcm BCM2E7C:00: oper_speed=3000000
[    5.259878] Bluetooth: xxx bcm_resource start...
[    5.259880] hci_bcm BCM2E7C:00: BCM2E7C:00 device registered.
[    5.259881] Bluetooth: xxx bcm_apple_set_power 1
[    5.272356] dw-apb-uart.2: ttyS4 at MMIO 0xc182b000 (irq = 21, base_baud = 115200) is a 16550A
[    5.276115] Bluetooth: HCI UART protocol Broadcom registered
[    5.276116] Bluetooth: HCI UART protocol QCA registered
[    5.276117] Bluetooth: HCI UART protocol AG6XX registered
[    5.276118] Bluetooth: HCI UART protocol Marvell registered
[    5.288794] dw-apb-uart.3: ttyS5 at MMIO 0xc182c000 (irq = 20, base_baud = 3000000) is a 16550A
[   46.903513] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[   46.903517] Bluetooth: BNEP filters: protocol multicast
[   46.903521] Bluetooth: BNEP socket layer initialized

[root@mac ~]# btattach --bredr /dev/ttyS5 -P bcm
Attaching Primary controller to /dev/ttyS5
Switched line discipline from 0 to 15
Device index 0 attached

[  287.094406] Bluetooth: xxx bcm_open start...
[  287.094420] Bluetooth: (null): hu ffff9a718ec643c0
[  287.094426] Bluetooth: xxx bcm_open mutex_lock
[  287.094429] Bluetooth: xxx bcm_open mutex_unlock
[  287.095790] Bluetooth: xxx bcm_setup start...
[  287.095801] Bluetooth: hci0: hu ffff9a718ec643c0
[  289.149877] Bluetooth: hci0 command 0x0c03 tx timeout
[  297.469902] Bluetooth: hci0: BCM: Reset failed (-110)
[  297.469906] Bluetooth: xxx bcm_setup bcm_initialize return err...

it's failing on this function in hci_bcm.c :
err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));

if you drill down a bit, the function it's actually failing on is the HCI reset function defined in btbcm.c
skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);

**the purpose of this function is to switch the controller into full HCI mode

@roadrunner2
Copy link
Contributor

I took another stab at this and got it mostly working, at least on my laptop - see l1k/linux#1.

In addition to the above kernel patches, I am using the following patch (this assumes you have the systemd [email protected]):

--- /usr/libexec/bluetooth/btattach-bcm-service.sh.orig	2017-09-12 07:27:30.000000000 -0700
+++ /usr/libexec/bluetooth/btattach-bcm-service.sh	2017-09-22 21:20:17.616533796 -0700
@@ -15,6 +15,12 @@
 BT_DEV="$(readlink -f $BT_DEV)"
 UART_DEV="$(dirname $BT_DEV)"
 
+# Handle intel-lpss UART devices
+DW_DEV=$(ls -d "$UART_DEV"/dw-apb-uart.* 2>/dev/null)
+if [[ -d "$DW_DEV" ]] ; then
+	UART_DEV="$DW_DEV"
+fi
+
 # Stupid GPD-pocket has USB BT with id 0000:0000, but still claims to have
 # an uart attached bt
 if [ "$1" = "BCM2E7E:00" ] && lsusb | grep -q "ID 0000:0000"; then

With this the bluetooth device is automatically attached on boot, and can be manually attached/detached via

sudo systemctl start btattach-bcm@BCM2E7C:00
sudo systemctl stop btattach-bcm@BCM2E7C:00

Of course, already noted by @leifliddy above, you can also just run this command:

sudo btattach --bredr /dev/ttyS5 -P bcm

@leifliddy The reason for the timeout (-110 is -ETIMEDOUT) is the wrong baudrate being used, which is fixed by the 2nd commit in my pull request.

Lastly, some issues I noticed so far:

  • If the gnome bluetooth settings dialog is open, then audio may stutter and file transfers are really slow (I think it's the scanning that it's doing when that dialog is open that is causing this)
  • The following error can be found in the kernel log, but unsure of its significance:
Sep 23 11:21:59 schlepptouch kernel: DMAR: Allocating domain for idma64.2 failed
Sep 23 11:21:59 schlepptouch kernel: ttyS5 - failed to request DMA
  • attaching sometimes still fails with -110 (timeout) - re-attaching generally succeeds (the -16 (EBUSY) error can be ignored)

@peterychuang
Copy link
Contributor Author

@roadrunner2 To which kernel version am I supposed to apply the patches? I think because I am playing with linux-next, the patches aren't working just yet.

@christophgysin
Copy link
Contributor

@peterychuang The patches are on top of https://github.com/l1k/linux/tree/hci_bcm, which is based on 4.13.0-rc4.

@peterychuang
Copy link
Contributor Author

@christophgysin thanks. I've made some changes to this commit in order to apply it to linux-next, then I applied the patches by @roadrunner2. I guess I've done something wrong somewhere.

@roadrunner2
Copy link
Contributor

@peterychuang, @christophgysin: I've been testing on 4.13.2 and 4.13.3. I also just pushed a hci_bcm-4.13 branch (currently based on v4.13.3) to make it easier to pull.

There have been a few small changes in 4.14 which may affect these patches, besides the fact that the first three in the set from @l1k are not necessary (they are backporting the properties work that is now part of 4.14). I'm looking into redoing the patches for 4.14 and will push another branch when done.

@leifliddy
Copy link

leifliddy commented Sep 24, 2017

great work roadrunner2!!! It's working!!!!
I wasn't aware there was a [email protected], it's now working with your patch!

dmesg output from MacBook9,1 (running kernel 4.14-rc1 with l1k's and roadrunner2's bluetooth patches applied)

[    7.242471] Bluetooth: xxx bcm_open start...
[    7.242473] Bluetooth: (null): hu ffff9fcb63f98400
[    7.242473] Bluetooth: xxx bcm_open mutex_lock
[    7.242475] Bluetooth: BCM2E7C:00: xxx bcm_open bcm_gpio_set_power ffff9fcb61992200 true
[    7.242476] Bluetooth: xxx bcm_apple_set_power 1
[    7.256019] Bluetooth: xxx bcm_open mutex_unlock
[    7.258535] Bluetooth: xxx bcm_set_baudrate start...
[    7.258873] Bluetooth: hci0: BCM: failed to write update baudrate (-16)
[    7.258874] Bluetooth: xxx bcm_setup start...
[    7.258875] Bluetooth: hci0: hu ffff9fcb63f98400
[    7.258875] Bluetooth: xxx bcm_setup btbcm_set_bdaddr set
[    7.371575] Bluetooth: hci0: BCM: chip id 92
[    7.376021] Bluetooth: hci0: BCM: features 0x2f
[    7.380905] Bluetooth: hci0: BCM4350C0 UART 37.4 MHz Gamay Murata UHE
[    7.383183] Bluetooth: hci0: BCM (003.001.082) build 1288
[    7.387472] Bluetooth: xxx bcm_setup firmware...
[    7.389561] bluetooth hci0: Direct firmware load for brcm/BCM.hcd failed with error -2
[    7.391364] Bluetooth: hci0: BCM: Patch brcm/BCM.hcd not found
[    7.478187] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[    7.480965] Bluetooth: BNEP filters: protocol multicast
[    7.483563] Bluetooth: BNEP socket layer initialized

If I unattach and reattach the bluetooth device. this error disappears:
hci0: BCM: failed to write update baudrate (-16)

my bluetooth manager (blueberry --based on gnome-bluetooth) can now pair my bluetooth mouse + speaker without issue!!
all bluetoothctl functions work as well.

I'm experiencing the same audio stuttering issue while the bluetooth setting window is open.
However, I don't see any of those 'DMAR: Allocating domain for idma64.2 failed' or 'ttyS5 - failed to request DMA' errors in the dmesg output.

hciconfig doesn't show the controller for some reason
[root@mac ~]# hciconfig -a
Can't get device list: Operation not supported

@roadrunner2
Copy link
Contributor

I built and tested 4.14-rc2, and pushed as the hci_bcm-4.14 branch. Other than resolving some conflicts, nothing really needed changing. @peterychuang, I suggest resetting your tree and cherry-picking the last 6 commits (v4.14-rc..hci_bcm-4.14). If that still doesn't work, then we'll need at least to see the dmesg output from when you run sudo rmmod hci_uart; sudo modprobe hci_uart dyndbg=+p.

@leifliddy Thanks for testing and feedback. Interesting that you don't see the DMA errors - which chipset exactly provides UART on your MB? (lscpi -nn | grep UART). Also not sure why hciconfig isn't working for you - needless to say it works fine for me.

Lastly, previous to your edit you were talking about firmware loading: I've been ignoring that so far, but I don't really know if there's anything critical there.

@peterychuang
Copy link
Contributor Author

thanks @roadrunner2, unfortunately it isn't working.

The strange thing in linux-next, both with and without the patches, is that in dmesg | grep Bluetooth, this is what I find:

[ 1282.752054] Bluetooth: HCI UART driver ver 2.3
[ 1282.752056] Bluetooth: HCI UART protocol H4 registered
[ 1282.752057] Bluetooth: HCI UART protocol BCSP registered
[ 1282.752057] Bluetooth: HCI UART protocol ATH3K registered
[ 1282.752058] Bluetooth: HCI UART protocol Three-wire (H5) registered
[ 1282.752088] Bluetooth: HCI UART protocol Intel registered
[ 1282.752088] Bluetooth: HCI UART protocol QCA registered
[ 1282.752089] Bluetooth: HCI UART protocol AG6XX registered

I would think something like Bluetooth: HCI UART protocol Broadcom registered should appear, but nope. In any case, that is perhaps why running sudo btattach --bredr /dev/ttyS0 -P bcm gives me this:

Attaching Primary controller to /dev/ttyS0
Switched line discipline from 0 to 15
Failed to set protocol: Protocol not supported
No controller attached

With the patches, dmesg with dyndbg=+p enabled on hci_uart now has something like these two lines:

[   56.017372] tty ffff880204c39800
[   56.017420] tty ffff880204c39800

If I run sudo btattach --bredr /dev/ttyS0 instead (i.e. without -P bcm, these additional lines appear:

[   65.490535] tty ffff88023dccf800


[   65.490546] hu ffff8802616e5600


[   65.491031] hci0 ffff880204d42000
[   65.491040] hci0: type 1 len 3
[   65.491042] hu ffff8802616e5600 skb ffff88020ca3d700



[   65.577815] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[   65.577819] Bluetooth: BNEP filters: protocol multicast
[   65.577825] Bluetooth: BNEP socket layer initialized

Perhaps I have unknowingly done something wrong when I compile the kernel, or perhaps the 2017 machines are slightly different. I haven't got time to investigate it yet.

@christophgysin
Copy link
Contributor

@peterychuang Maybe you are missing CONFIG_BT_HCIUART_BCM=y?

@MyopicPaideia
Copy link

MyopicPaideia commented Feb 4, 2020

Thank you for the quick reply, @christophgysin !

At least getting further in now...getting these make errors at the end of the script:

make -C /lib/modules/5.4.15-2-MANJARO/build M=/home/dlg/macbook12-bluetooth-driver/build/bluetooth-5.4.15 modules
make[1]: Entering directory '/usr/lib/modules/5.4.15-2-MANJARO/build'
make[1]: *** No rule to make target 'modules'.  Stop.
make[1]: Leaving directory '/usr/lib/modules/5.4.15-2-MANJARO/build'
make: *** [Makefile:19: all] Error 2
cp hci_uart.ko /lib/modules/5.4.15-2-MANJARO/updates
cp: cannot stat 'hci_uart.ko': No such file or directory
make: *** [Makefile:25: install] Error 1

contents of /lib/modules/5.4.15-2-MANJARO/updates
total 0

Looks like hci_uart.ko is not getting made due to "no rule to make target 'modules'"?

@christophgysin
Copy link
Contributor

Looks like you also need to install kernel headers: pacman -S linux54-headers

@MyopicPaideia
Copy link

MyopicPaideia commented Feb 4, 2020

@christophgysin you are the man! My bad for not installing the kernal headers package in the first place as instructed originally by @leifliddy in his README. As there were no ARCHLinux instructions I stupidly ignored that part.

So for ARCH we do:

pacman -S base-devel

and

pacman -S linux54-headers (or whichever kernal you are on)

With all the correct packages installed, the script works like a charm, bluetooth works!

Thank you very much for the script @leifliddy, and thank you @christophgysin for being patient and responsive with a newbie. All the best!

@Dunedan
Copy link
Owner

Dunedan commented Feb 4, 2020

Please don't use this thread to discuss Linux distribution specific issues. This issue is about upstreaming Bluetooth support (as well as the whole project is about upstreaming hardware support).
If you want to discuss distribution specific topics I suggest you either use a discussion medium provided by the distribution in question or use the Gitter chat for that: https://gitter.im/mbp-2016-linux/Lobby

@leifliddy
Copy link

leifliddy commented Feb 23, 2020

Something changed w/ kernel 5.5.x
The bluetooth controller isn't coming up.
hci_uart doesn't seem to do anything as there are no registered serial devices present

kernel 5.5.x

kernel: dw-apb-uart.2: ttyS4 at MMIO 0xc182b000 (irq = 21, base_baud = 115200) is a 16550A
kernel: dw-apb-uart.3: ttyS5 at MMIO 0xc182c000 (irq = 20, base_baud = 3000000) is a 16550A
kernel: hci_uart: loading out-of-tree module taints kernel.
kernel: hci_uart: module verification failed: signature and/or required key missing - tainting kernel

kernel 5.4.x

kernel: dw-apb-uart.2: ttyS4 at MMIO 0xc182b000 (irq = 21, base_baud = 115200) is a 16550A
kernel: serial serial0: tty port ttyS4 registered
kernel: dw-apb-uart.3: ttyS5 at MMIO 0xc182c000 (irq = 20, base_baud = 3000000) is a 16550A
kernel: serial serial1: tty port ttyS5 registered
Jan 09 21:24:52 mac.example.com kernel: hci_uart: loading out-of-tree module taints kernel.
Jan 09 21:24:52 mac.example.com kernel: hci_uart: module verification failed: signature and/or required key missing - tainting kernel
kernel: hci_uart_bcm serial1-0: Unexpected ACPI gpio_int_idx: -1
kernel: hci_uart_bcm serial1-0: Unexpected number of ACPI GPIOs: 0
kernel: hci_uart_bcm serial1-0: No reset resource, using default baud rate

I'm not sure what changed, possibly something w/ the serdev or the 8250 driver....

@roadrunner2
Copy link
Contributor

commit 33364d63c75d is the cause for the breakage in 5.5. I've already submitted a patch - however, it looks like the kernel devs want more extensive cleanups first, and I don't know how long it'll take me to A) understand exactly what they want, and B) implement it, so it might take a bit before the patch is accepted and makes it into the stable tree(s) 😞 .

@leifliddy
Copy link

leifliddy commented Feb 29, 2020

I'm glad you're on top of this : ) It sucks that the root of this issue is Apple's non-conformance to established standards. It looks like Andy's patch will establish the framework for adding these sorts of quirks in a non-intrusive way. Hopefully, it gets accepted...

@roadrunner2
Copy link
Contributor

The patch has made its way into 5.5.9 and 5.6-rc5 now, so your distro's next kernel update should get bluetooth working again.

@MyopicPaideia
Copy link

Hello again, gents - have done a reinstall and just tried to install this patch again, on my 2016 13,1 machine, it gave the same make error:

make -C /lib/modules/5.5.13-arch1-1/build M=/home/dlg/clones/macbook12-bluetooth-driver/build/bluetooth-5.5.13 modules
make[1]: *** /lib/modules/5.5.13-arch1-1/build: No such file or directory.  Stop.
make: *** [Makefile:19: all] Error 2
cp hci_uart.ko /lib/modules/5.5.13-arch1-1/updates
cp: cannot stat 'hci_uart.ko': No such file or directory
make: *** [Makefile:25: install] Error 1

contents of /lib/modules/5.5.13-arch1-1/updates
ls: cannot access '/lib/modules/5.5.13-arch1-1/updates': No such file or directory

Here is my relevant dmesg:

[dlg@gnuMBP131 bspwm]$ dmesg | grep hci0
[    5.559405] Bluetooth: hci0: command 0xfc18 tx timeout
[   13.452845] Bluetooth: hci0: BCM: failed to write update baudrate (-110)
[   13.452852] Bluetooth: hci0: Failed to set baudrate
[   15.586177] Bluetooth: hci0: command 0x0c03 tx timeout
[   23.479510] Bluetooth: hci0: BCM: Reset failed (-110)

I have all the latest headers, and am on 5.5.13-arch2-1

Sorry to beat a dead horse if I'm being an idiot.

@leifliddy
Copy link

leifliddy commented Apr 1, 2020

you have to create the updates directory
mkdir /lib/modules/$(uname -r)/updates
If you cloned the https://github.com/leifliddy/macbook12-bluetooth-driver repo, that should be in the script.
[[ ! -d $update_dir ]] && mkdir $update_dir

@MyopicPaideia
Copy link

Ok, but my path has arch2-1. not arch1-1:

/lib/modules/5.5.13-arch2-1

Should I just create the arch1-1 path that the script wants or does the script need tweaking to point to the arch2-1 directory?

@leifliddy
Copy link

leifliddy commented Apr 2, 2020

It should match whatever's in your Makefile
You can clearly see in your output that it's trying to copy the module to
/lib/modules/5.5.13-arch1-1/updates which doesn't exist.

cp hci_uart.ko /lib/modules/5.5.13-arch1-1/updates
cp: cannot stat 'hci_uart.ko': No such file or directory

How is your Makefile determining that location?

@MyopicPaideia
Copy link

If I am determining your script correctly:

kernel_version=$(uname -r | cut -d '-' -f1)          #ie 5.2.8
major_version=$(echo $kernel_version | cut -d '.' -f1)
minor_version=$(echo $kernel_version | cut -d '.' -f2)
kernel_short_version="$major_version.$minor_version" #ie 5.2

build_dir='build'
update_dir="/lib/modules/$(uname -r)/updates"

@leifliddy
Copy link

That's not the Makefile, you should be able to find it, it's a file called Makefile

@MyopicPaideia
Copy link

MyopicPaideia commented Apr 2, 2020

Ok, this is getting weird now.

[dlg@gnuMBP131 bluetooth-5.5.13]$ cat Makefile

obj-m 	+= hci_uart.o

hci_uart-y	:= hci_ldisc.o
hci_uart-m 	+= hci_serdev.o
hci_uart-m 	+= hci_h4.o
hci_uart-m	+= hci_bcsp.o
hci_uart-m	+= hci_ll.o
hci_uart-m	+= hci_ath.o
hci_uart-m	+= hci_h5.o
hci_uart-m	+= hci_intel.o
hci_uart-m	+= hci_bcm.o
hci_uart-m	+= hci_qca.o
hci_uart-m	+= hci_ag6xx.o
hci_uart-m	+= hci_mrvl.o
hci_uart-objs	:= $(hci_uart-y)


all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

install:
	cp hci_uart.ko /lib/modules/$(shell uname -r)/updates
	depmod -a

But - this:

uname -r
5.5.13-arch1-1

So why do I have something else (5.5.13-arch2-1) in my /lib/modules ??

@leifliddy
Copy link

leifliddy commented Apr 2, 2020

Normally uname -r corresponds to your (active) kernel-release version. I've never used arch linux before, maybe there's some non-standard stuff going on. You might have to modify the install script and the Makefile to suit the naming conventions used by your distribution.

@ghost
Copy link

ghost commented Apr 2, 2020

Looks to me like he installed a new kernel and didn't boot it.

@MyopicPaideia
Copy link

Ok, I will look into it. Thanks for your time and help.

Here's the latest linux headers:
https://www.archlinux.org/packages/core/x86_64/linux-headers/

@DarkerMatter - you may be a genius for pointing out an obvious possibility! Be right back...

@MyopicPaideia
Copy link

....and it worked....

Thank you @DarkerMatter for your quick but insightful contribution, and thank you @leifliddy for helping.

For future reference, should I be bringing script error messages somewhere else for help? It seems like this was just a discussion about how to get the script to run on my particular setup...

@jso8910
Copy link

jso8910 commented Dec 10, 2020

This isn't a great question, but I want to know how to apply this patch. I don't really have wired earbuds anymore, and this is the only thing stopping me from using Linux a ton (storage, sound, etc are no longer issues).

@Artawower
Copy link

This isn't a great question, but I want to know how to apply this patch. I don't really have wired earbuds anymore, and this is the only thing stopping me from using Linux a ton (storage, sound, etc are no longer issues).

Did your find patch and how to apply it? I have same problem, and its also onсe moment that prevent migration to manjaro..

@leifliddy
Copy link

This isn't a great question, but I want to know how to apply this patch. I don't really have wired earbuds anymore, and this is the only thing stopping me from using Linux a ton (storage, sound, etc are no longer issues).

Did your find patch and how to apply it? I have same problem, and its also onсe moment that prevent migration to manjaro..

Have you tried https://github.com/leifliddy/macbook12-bluetooth-driver ?

@yerke
Copy link

yerke commented Nov 7, 2021

https://github.com/leifliddy/macbook12-bluetooth-driver worked perfectly for my MBP13,1. Thanks @leifliddy!

@adam-macgill
Copy link

uname -r -> 5.11.0-40-generic ubuntu 20 on macbook r12 early 2015
./install.bluetooth.sh results in the following error :

.....
make -C /lib/modules/5.11.0-40-generic/build M=/macbook12-bluetooth-driver/build/bluetooth-5.11.0 modules
make[1]: Entering directory '/usr/src/linux-headers-5.11.0-40-generic'
CC [M] /macbook12-bluetooth-driver/build/bluetooth-5.11.0/hci_ldisc.o
/macbook12-bluetooth-driver/build/bluetooth-5.11.0/hci_ldisc.c: In function ‘hci_uart_init’:
/macbook12-bluetooth-driver/build/bluetooth-5.11.0/hci_ldisc.c:836:23: error: assignment to ‘ssize_t (*)(struct tty_struct *, struct file *, unsigned char , size_t, void **, long unsigned int)’ {aka ‘long int ()(struct tty_struct *, struct file *, unsigned char , long unsigned int, void **, long unsigned int)’} from incompatible pointer type ‘ssize_t ()(struct tty_struct *, struct file *, unsigned char , size_t)’ {aka ‘long int ()(struct tty_struct *, struct file *, unsigned char *, long unsigned int)’} [-Werror=incompatible-pointer-types]
836 | hci_uart_ldisc.read = hci_uart_tty_read;
| ^
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:288: /macbook12-bluetooth-driver/build/bluetooth-5.11.0/hci_ldisc.o] Error 1
make[1]: *** [Makefile:1849: /macbook12-bluetooth-driver/build/bluetooth-5.11.0] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.11.0-40-generic'
make: *** [Makefile:19: all] Error 2
cp hci_uart.ko /lib/modules/5.11.0-40-generic/updates
cp: cannot stat 'hci_uart.ko': No such file or directory
make: *** [Makefile:26: install] Error 1

contents of /lib/modules/5.11.0-40-generic/updates
total 0

Any advice welcome .

@leifliddy
Copy link

That question has already been answered here along with an explanation of why I'm not updating the installer script to accommodate this.

leifliddy/macbook12-bluetooth-driver#15

You'll need install it like this until your Debian-based OS updates their kernel to 5.11.3

./install.bluetooth.sh
cd build/bluetooth-5.11.0
sed -i 's/size_t nr/size_t nt, void **cookie, unsigned long offset/' hci_ldisc.c
make
make install 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests