Bug 99881

Summary: 2015 Apple bluetooth keybord and magic mouse support
Product: Drivers Reporter: aayla.secura.1138
Component: BluetoothAssignee: linux-bluetooth (linux-bluetooth)
Status: NEW ---    
Severity: normal CC: alexzavistovi4, auxsvr, chad, compnerd, comzeradd, contact, daquinons, Derarnold, fabiofalci, guy, kernel, kevin, koyeung, luiz.dentz, mansour, me, michael.j.schlies, mike, mike, mnjonesmcp, ncopa, per.edin, peteryuchuang, ptitjes, seymartens, stalkerg, stephane, stephen, tomas, v
Priority: P1    
Hardware: All   
OS: Linux   
Kernel Version: 4.0.4+ Subsystem:
Regression: No Bisected commit-id:
Attachments: Patch for bluetooth
Binding of MM2 to hid-magicmouse
OSX Packet Logger Magic Mouse 2
attachment-8611-0.html
Patch to makes bluetooth Magic Keyboard use hid-apple driver instead of hid-generic
Fix Fn key on bluetooth Apple Magic Keyboard

Description aayla.secura.1138 2015-06-12 23:39:27 UTC
I feel this is somewhat related to bug 96771 . I own a new (2015) MacBook Pro + bluetooth keyboard + bluetooth magic mouse and the fn key on either the bt keyboard or the internal one did not work. Nor did scrolling and right-click on the trackpad, nor scrolling on the mouse. The two patches from comment 26 in bug 96771 solved the problems with the internal keyboard and the trackpad - many thanks for that! The bluetooth devices don't seem to be supported though. I'm not 100% sure which drivers SHOULD be responsible for them - my guess is hid-magicmouse for the mouse and hid-apple for the bluetooth keyboard. Also, I'm guessing adding support for the keyboard should be as straightforward as copy-pasting the added code for the internal one but with a different USB ID. Don't know about mouse scrolling though. Here's the lsusb output for the device:


Bus 001 Device 002: ID 05ac:8290 Apple, Inc. 
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.01
  bDeviceClass          239 Miscellaneous Device
  bDeviceSubClass         2 
  bDeviceProtocol         1 Interface Association
  bMaxPacketSize0        64
  idVendor           0x05ac Apple, Inc.
  idProduct          0x8290 
  bcdDevice            0.90
  iManufacturer           1 Broadcom Corp.
  iProduct                2 Bluetooth USB Host Controller
  iSerial                 0 
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength          276
    bNumInterfaces          6
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xe0
      Self Powered
      Remote Wakeup
    MaxPower                0mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      1 Boot Interface Subclass
      bInterfaceProtocol      1 Keyboard
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.11
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      56
         Report Descriptors: 
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x85  EP 5 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval              10
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      1 Boot Interface Subclass
      bInterfaceProtocol      2 Mouse
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.11
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      52
         Report Descriptors: 
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x86  EP 6 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval              10
    Interface Association:
      bLength                 8
      bDescriptorType        11
      bFirstInterface         2
      bInterfaceCount         4
      bFunctionClass        255 Vendor Specific Class
      bFunctionSubClass       1 
      bFunctionProtocol       1 
      iFunction               0 
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        2
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass      1 
      bInterfaceProtocol      1 
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0010  1x 16 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        3
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0000  1x 0 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0000  1x 0 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        3
      bAlternateSetting       1
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0009  1x 9 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0009  1x 9 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        3
      bAlternateSetting       2
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0011  1x 17 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0011  1x 17 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        3
      bAlternateSetting       3
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0019  1x 25 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0019  1x 25 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        3
      bAlternateSetting       4
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0021  1x 33 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0021  1x 33 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        3
      bAlternateSetting       5
      bNumEndpoints           2
      bInterfaceClass       224 Wireless
      bInterfaceSubClass      1 Radio Frequency
      bInterfaceProtocol      1 Bluetooth
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0031  1x 49 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x03  EP 3 OUT
        bmAttributes            1
          Transfer Type            Isochronous
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0031  1x 49 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        4
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass    255 Vendor Specific Subclass
      bInterfaceProtocol    255 Vendor Specific Protocol
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x84  EP 4 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0020  1x 32 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x04  EP 4 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0020  1x 32 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        5
      bAlternateSetting       0
      bNumEndpoints           0
      bInterfaceClass       254 Application Specific Interface
      bInterfaceSubClass      1 Device Firmware Update
      bInterfaceProtocol      1 
      iInterface              0 
      Device Firmware Upgrade Interface Descriptor:
        bLength                             9
        bDescriptorType                    33
        bmAttributes                        5
          Will Not Detach
          Manifestation Tolerant
          Upload Unsupported
          Download Supported
        wDetachTimeout                   5000 milliseconds
        wTransferSize                      64 bytes
        bcdDFUVersion                   1.10
Binary Object Store Descriptor:
  bLength                 5
  bDescriptorType        15
  wTotalLength           12
  bNumDeviceCaps          1
  USB 2.0 Extension Device Capability:
    bLength                 7
    bDescriptorType        16
    bDevCapabilityType      2
    bmAttributes   0x0000841e
      BESL Link Power Management (LPM) Supported
    BESL value     1024 us 
    Deep BESL value    32768 us 
Device Status:     0x0003
  Self Powered
  Remote Wakeup Enabled

Can they be made to work?
Comment 1 Didier 'Ptitjes' 2015-08-27 16:05:03 UTC
Same chip in MacBookPro11,4 and MacBookPro11,5.
Comment 2 aayla.secura.1138 2015-08-29 03:14:26 UTC
I'm sorry, not sure I understand... I am talking about the bluetooth keyboard and mouse, what do they have to do with MBP?
Comment 3 Didier 'Ptitjes' 2015-08-29 09:34:42 UTC
Hi Aayla,

Sorry if I misunderstood you. I thought you were talking about the overall Bluetooth support as the ticket is registered to the 'Bluetooth' component.

When Bluetooth is not enabled, the use of a Bluetooth keyboard and mouse (whatever the brand) is accomplished through the bluetooth chip directly with some special Apple circuitry and the keyboard and mouse are seen by Linux as a USB keyboard and a USB mouse. This is done to have early keyboard and mouse support during boot (for instance you use the arrows keys in Refind).

However the keyboard and mouse are seen like standard USB keyboard, and I'm not sure it can be recognized as apple hardware.

Anyway, as I have a apple bluetooth keyboard and trackpad, I experience the same problem. As the Bluetooth chip is not supported, I only have basic support of the keyboard and trackpad, so no two finger scrolling nor special keys (as Fn is always on like on a PC keyboard).
Comment 4 aayla.secura.1138 2015-08-29 10:13:30 UTC
I see... I read somewhere that the problem is indeed in the fact that the bluetooth controller is run in HID mode and not in HCI but I couldn't get it to switch (with udev's hid2hci)...
Comment 5 Vadim Dyadkin 2015-09-28 19:57:45 UTC
Created attachment 188821 [details]
Patch for bluetooth

This hacky patch helps, after that bluetooth works like a charm.
Comment 6 Vadim Dyadkin 2015-09-28 19:59:07 UTC
I took it from here:
http://www.spinics.net/lists/linux-bluetooth/msg64098.html
Comment 7 aayla.secura.1138 2015-09-30 02:43:42 UTC
Unfortunately that did not work - bluetooth device is still detected and listed as using btusb driver but it simply receives nothing from the keyboard or mouse...
Comment 8 Vadim Dyadkin 2015-09-30 08:25:47 UTC
It is strange, it works for me as expected, I could pair both my keyboard and mouse, and I can make the tethering network from my smartphone.
Comment 9 aayla.secura.1138 2015-09-30 09:11:46 UTC
Thethering from the smartphone is handled by a different driver and it works for me as well (has always worked). But this keyboard... To be honest, I'm sick of trying and am tempted to just close the bug and either stick to the internal laptop one (as I am at the moment) or just get a normal wireless keyboard.

P.S. What kernel version are you using? I'm still on 4.0.5 due to issues with the graphics that appeared in later versions and haven't been resolved.
Comment 10 Vadim Dyadkin 2015-09-30 09:24:05 UTC
I forgot to mention, I do not use Apple devices, my mouse is Logitech M555b, and my keyboard is Logitech K810.

I am using the latest released kernel 4.2.1.
I do not experience any issues with the graphics, moreover, I do not need to use kernel command line arguments like i915.enable_ips=0 and intel_iommu=igfx_off anymore as I did in 4.0 and 4.1.
Comment 11 aayla.secura.1138 2015-09-30 09:26:35 UTC
Oh well, that may explain why, I'm pretty sure any other BT keyboard would work although I haven't tried.
Thanks, I'll give the latest version a go, hopefully the graphics would work!
Comment 12 aayla.secura.1138 2015-10-06 01:02:39 UTC
So using kernel 4.2.2 - same thing:
Using unmodified souurces the BT controller is picked up by usbhid driver with which the apple BT keyboard works but not the FN button (and mouse scrolling does not work on the magic mouse), which was the reason I started this bug.
Using the patch from comment 5, the BT controller is picked up by btusb but then the apple BT keyboard does not work AT ALL (tried both building btusb into the kernel as well as as a module - no difference).
Comment 13 Michael Schlies 2016-02-11 04:08:29 UTC
Created attachment 203441 [details]
Binding of MM2 to hid-magicmouse

This only binds the magic mouse 2 to the hid-magicmouse module, scrolling still doesn't work but it is a first step. I will pair with one of my macs and start trying to capture the scrolling. First guess would indicate that scrolling, force touch, and other gestures are all sent over BTLE.
Comment 14 Mansour Behabadi 2016-04-04 15:33:24 UTC
I have some details on the difference between the Fn key on Apple Magic Keyboard (the new one) and Apple Wireless Keyboard (the old one). Both are bluetooth keyboards. I used Wireshark to sniff HID packets over Bluetooth.

In the old keyboard, Fn key sends a HID packet with protocol code 0x11 (unknown to Wireshark) and a single byte payload of 0x10 for key down and 0x00 for key up.

In the new keyboard, Fn key sends a HID packet with protocol code 0x01 (ie keyboard) and value of the *7th* key set to 0x02 for key down, and 0x00 for key up.

Note the 7th key. Wireshark only shows up to 6 keycodes in a keyboard HID packet which I assume is the standard, but the new keyboard sends a 7th keycode for the Fn key.

Hope this helps.
Comment 15 Michael Schlies 2016-04-08 15:43:02 UTC
I would LOVE to get my hands on that wireshark. Is it full bluetooth or BLE by any chance?

Thanks,

Michael Schlies

(In reply to Mansour Behabadi from comment #14)
> I have some details on the difference between the Fn key on Apple Magic
> Keyboard (the new one) and Apple Wireless Keyboard (the old one). Both are
> bluetooth keyboards. I used Wireshark to sniff HID packets over Bluetooth.
> 
> In the old keyboard, Fn key sends a HID packet with protocol code 0x11
> (unknown to Wireshark) and a single byte payload of 0x10 for key down and
> 0x00 for key up.
> 
> In the new keyboard, Fn key sends a HID packet with protocol code 0x01 (ie
> keyboard) and value of the *7th* key set to 0x02 for key down, and 0x00 for
> key up.
> 
> Note the 7th key. Wireshark only shows up to 6 keycodes in a keyboard HID
> packet which I assume is the standard, but the new keyboard sends a 7th
> keycode for the Fn key.
> 
> Hope this helps.
Comment 16 Mansour Behabadi 2016-04-08 22:38:45 UTC
(In reply to Michael Schlies from comment #15)
> I would LOVE to get my hands on that wireshark. Is it full bluetooth or BLE
> by any chance?

I have the keyboard for another day or two and I can capture and upload any scenarios that helps this issue. Just let me know what you need.

I don't recall seeing any BLE stuff but I could be wrong.
Comment 17 Michael Schlies 2016-04-09 02:21:31 UTC
Actually, do you happen to have captures from the mouse or trackpad? I think I got the Fn key working this afternoon.

Thanks!
Comment 18 Mansour Behabadi 2016-04-09 02:28:08 UTC
> Actually, do you happen to have captures from the mouse or trackpad?

Unfortunately I don't own those.

> I think I got the Fn key working this afternoon.

Awesome!
Comment 19 aayla.secura.1138 2016-04-09 02:33:12 UTC
I have the 2015 magic mouse and keyboard and I would like to help, but I don't really know how to capture HID packets. I've only used the likes of showkey. I also don't currently have access to them, since I'm away from my office until at some point next week, but if someone refers me to some links on how I can help with that, I'd be glad.
Comment 20 Michael Schlies 2016-04-09 02:46:33 UTC
I have all three devices, I just can't say I have done bluetooth captures of them and I think the trackpad and mouse are going to be the most difficult of these to setup.
Comment 21 derarnold 2016-04-18 10:04:59 UTC
Hello Guys.

I have set a bug bounty over at bountysource for a fix for this issue.

https://www.bountysource.com/issues/33063419-2015-apple-bluetooth-keybord-and-magic-mouse-support

Thank you for your guys help!

Oliver
Comment 22 bender 2016-04-21 19:51:18 UTC
I've got Packet Logger files for the magic mouse 2 if it helps but I am not sure what of the logs should be uploaded. I am a noob to posting here. Can i post a link to the whole file or is that frowned upon?
Comment 23 bender 2016-04-21 19:53:23 UTC
Created attachment 213591 [details]
OSX Packet Logger Magic Mouse 2
Comment 24 bender 2016-04-21 19:58:52 UTC
Disregard my question.
So I came into work on Monday to find my right click not working on my OEM mouse. I swapped with like 3 other mice and all had other issues. All hardware related. Amazon Prime Now had a MM2 so i figured i would try it out. Now I'm stuck with no scrolling. Hopefully these packet logs will help whoever can fix this. If i were more versed in this sort of thing I would give it a shot. I will also be throwing in on the bounty to help maybe speed this along.
Comment 25 Michael Schlies 2016-04-25 09:35:57 UTC
While the packet captures are otherwise appreciated I took some very tightly scoped ones (roughly 10 captures for each of the 3 devices).
Comment 26 bender 2016-04-25 20:08:24 UTC
I swapped my mm2 with a friend for a mm1 and it works as described by most posts online. I can't deal with it so i returned it for a trackpad 2. I am on xubuntu 16.04 lts. If anyone wants me to test anything or grab any logs let me know. Currently I have movement a left button click. Nothing else.
Comment 27 bender 2016-06-30 15:37:05 UTC
Any Progress on this? I hope to have a working touchpad soon.
Comment 28 Stephane Wirtel 2016-08-03 22:37:44 UTC
Michael, have you continued to work on this issue ?

Is there any progress ?

Thanks
Comment 29 Stephane Wirtel 2016-09-14 15:49:26 UTC
Any progress ?
Comment 30 Nikos Roussos 2017-05-01 12:22:47 UTC
No progress here I guess?

I just tried a new set of apple bluetooth keyboard and magic mouse and problems still persist (scroll, Fn, etc).
Comment 31 Tomas Sandven 2017-07-10 07:46:50 UTC
Just tested connecting a Magic Keyboard A1644 to Debian 9, kernel 4.9.0-3-amd63, over Bluetooth. It uses the driver hid_generic, not hid_apple, and the "fn" keys don't work.

I added a bounty on bountysource: https://www.bountysource.com/issues/33063419-2015-apple-bluetooth-keybord-and-magic-mouse-support
Comment 32 Luiz Von Dentz 2017-07-10 14:22:34 UTC
So what the pid and vid over bluetooth, you should be able to figure this out in bluetoothd logs, e.g: 

bluetoothd[30701]: profiles/input/hog.c:hog_device_accept() name=Arc Touch Mouse SE vendor=0x45E, product=0x7F3, version=0x1
Comment 33 Nikos Roussos 2017-07-10 17:50:01 UTC
If it helps, from bluetoothctl:


info:
Device
	Name: Magic Mouse 2
	Alias: Magic Mouse 2
	Class: 0x002580
	Icon: input-mouse
	Paired: yes
	Trusted: yes
	Blocked: no
	Connected: yes
	LegacyPairing: no
	UUID: Human Interface Device... (00001124-0000-1000-8000-00805f9b34fb)
	UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
	Modalias: bluetooth:v004Cp0269d0064
	ManufacturerData Key: 0x004c
	ManufacturerData Value: 0x41
	ManufacturerData Value: 0x75
	ManufacturerData Value: 0x67
	ManufacturerData Value: 0x20
	ManufacturerData Value: 0x33
	ManufacturerData Value: 0x31
	ManufacturerData Value: 0x20
	ManufacturerData Value: 0x32
	ManufacturerData Value: 0x30
	ManufacturerData Value: 0x31
	ManufacturerData Value: 0x35
	ManufacturerData Value: 0x00
	ManufacturerData Value: 0x20
	ManufacturerData Value: 0x31
	ManufacturerData Value: 0x37
	ManufacturerData Value: 0x3a
	ManufacturerData Value: 0x31
	ManufacturerData Value: 0x31
	ManufacturerData Value: 0x3a
	ManufacturerData Value: 0x31
	ManufacturerData Value: 0x37
	ManufacturerData Value: 0x00
Comment 34 Guy Thouret 2017-07-10 18:24:20 UTC
Created attachment 257447 [details]
attachment-8611-0.html

I spent a bit of time on this a few months ago and my conclusion was that
it's not just a case of adding the ids to the existing hid-apple driver.
I've got some Wireshark captures from Mac OS that showed Fn key emitting a
keycode that is different from other magic keyboard models.  I also suspect
that Magic Mouse requires a command to be sent to it that enables gestures
as there is no bluetooth tx from the mouse for gestures when running with
hid-generic.  I use both of these devices daily under Ubuntu 17.04 on a MBP
2015.  I'll spend a bit of time and see if I can get the Fn key mapped in
hid-apple.  If I can be of assistance to anyone for testing please let me
know.

On 10 July 2017 at 18:50, <bugzilla-daemon@bugzilla.kernel.org> wrote:

> https://bugzilla.kernel.org/show_bug.cgi?id=99881
>
> --- Comment #33 from Nikos Roussos (comzeradd@fedoraproject.org) ---
> If it helps, from bluetoothctl:
>
>
> info:
> Device
>         Name: Magic Mouse 2
>         Alias: Magic Mouse 2
>         Class: 0x002580
>         Icon: input-mouse
>         Paired: yes
>         Trusted: yes
>         Blocked: no
>         Connected: yes
>         LegacyPairing: no
>         UUID: Human Interface Device... (00001124-0000-1000-8000-
> 00805f9b34fb)
>         UUID: PnP Information           (00001200-0000-1000-8000-
> 00805f9b34fb)
>         Modalias: bluetooth:v004Cp0269d0064
>         ManufacturerData Key: 0x004c
>         ManufacturerData Value: 0x41
>         ManufacturerData Value: 0x75
>         ManufacturerData Value: 0x67
>         ManufacturerData Value: 0x20
>         ManufacturerData Value: 0x33
>         ManufacturerData Value: 0x31
>         ManufacturerData Value: 0x20
>         ManufacturerData Value: 0x32
>         ManufacturerData Value: 0x30
>         ManufacturerData Value: 0x31
>         ManufacturerData Value: 0x35
>         ManufacturerData Value: 0x00
>         ManufacturerData Value: 0x20
>         ManufacturerData Value: 0x31
>         ManufacturerData Value: 0x37
>         ManufacturerData Value: 0x3a
>         ManufacturerData Value: 0x31
>         ManufacturerData Value: 0x31
>         ManufacturerData Value: 0x3a
>         ManufacturerData Value: 0x31
>         ManufacturerData Value: 0x37
>         ManufacturerData Value: 0x00
>
> --
> You are receiving this mail because:
> You are on the CC list for the bug.
>
Comment 35 Luiz Von Dentz 2017-07-10 18:45:33 UTC
Either way there should be a hid driver to driver all of this, be it in hid-apple or a new driver has nothing to do with Bluetooth really.
Comment 36 Tomas Sandven 2017-07-12 04:30:14 UTC
dmesg log when I connect the Magic Keyboard A1644 (mac address replaced with x):

[144510.510615] hid-generic 0005:004C:0267.0006: unknown main item tag 0x0
[144510.510687] input: Magic Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9:1.0/bluetooth/hci0/hci0:256/0005:004C:0267.0006/input/input23
[144510.510934] hid-generic 0005:004C:0267.0006: input,hidraw1: BLUETOOTH HID v0.66 Keyboard [Magic Keyboard] on xx:xx:xx:xx:xx:xx

Output from bluetoothctl info:

[Magic Keyboard]# info 2C:33:61:E5:1A:F8
Device 2C:33:61:E5:1A:F8
	Name: Magic Keyboard
	Alias: Magic Keyboard
	Class: 0x000540
	Icon: input-keyboard
	Paired: yes
	Trusted: yes
	Blocked: no
	Connected: yes
	LegacyPairing: no
	UUID: Human Interface Device... (00001124-0000-1000-8000-00805f9b34fb)
	UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
	Modalias: bluetooth:v004Cp0267d0066

---

Let me know if I can do anything to help.
Comment 37 Tomas Sandven 2017-07-12 04:32:01 UTC
I realize I hid the MAC address from the dmesg output, then forgot to do the same for the bluetoothctl output. I can't find an edit button and it probably doesn't even matter, so what ever :)
Comment 38 ncopa 2017-09-04 11:24:56 UTC
(In reply to Tomas Sandven from comment #36)
> [144510.510615] hid-generic 0005:004C:0267.0006: unknown main item tag 0x0
> [144510.510687] input: Magic Keyboard as

I have same keyboard (device Id 0x0267). What is interesting here is that vendor ID is 0x004c when connect via bluetooth, but 0x05ac when connected via USB.

It looks that 0x004c seems to be vendor id for Apple in some bluetooth spec: https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers

A hack to fix it would be something like:

````
diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index 25b7bd56ae11..528ec847cc03 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -472,6 +472,8 @@ static const struct hid_device_id apple_devices[] = {
                .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
        { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_ANSI),
                .driver_data = APPLE_HAS_FN },
+       { HID_USB_DEVICE(0x004c, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_ANSI),
+               .driver_data = APPLE_HAS_FN },
        { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
                .driver_data = APPLE_HAS_FN },
        { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
````

I suppose we need to define the vendor id 0x004c properly in drivers/hid/hid-ids.h but I don't know how? as BLUETOOTH_VENDOR_ID_APPLE?
Comment 39 Guy Thouret 2017-09-04 12:24:18 UTC
(In reply to ncopa from comment #38)
> (In reply to Tomas Sandven from comment #36)
> > [144510.510615] hid-generic 0005:004C:0267.0006: unknown main item tag 0x0
> > [144510.510687] input: Magic Keyboard as
> 
> I have same keyboard (device Id 0x0267). What is interesting here is that
> vendor ID is 0x004c when connect via bluetooth, but 0x05ac when connected
> via USB.
> 
> It looks that 0x004c seems to be vendor id for Apple in some bluetooth spec:
> https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers
> 
> A hack to fix it would be something like:
> 
> ````
> diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
> index 25b7bd56ae11..528ec847cc03 100644
> --- a/drivers/hid/hid-apple.c
> +++ b/drivers/hid/hid-apple.c
> @@ -472,6 +472,8 @@ static const struct hid_device_id apple_devices[] = {
>                 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
>         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
> USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_ANSI),
>                 .driver_data = APPLE_HAS_FN },
> +       { HID_USB_DEVICE(0x004c, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_ANSI),
> +               .driver_data = APPLE_HAS_FN },
>         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
> USB_DEVICE_ID_APPLE_WELLSPRING_ANSI),
>                 .driver_data = APPLE_HAS_FN },
>         { HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
> USB_DEVICE_ID_APPLE_WELLSPRING_ISO),
> ````
> 
> I suppose we need to define the vendor id 0x004c properly in
> drivers/hid/hid-ids.h but I don't know how? as BLUETOOTH_VENDOR_ID_APPLE?

I had tried exactly that and had no luck.  I just assumed the keyboard has two completely different modes of operation - one for USB and one for BT.  In USB mode the keyboard operates flawlessly.
Comment 40 ncopa 2017-09-04 13:36:05 UTC
Created attachment 258189 [details]
Patch to makes bluetooth Magic Keyboard use hid-apple driver instead of hid-generic

This patch makes the Magic Keyboard (vendor id 0x004c, device id 0x0267) use hid-apple instead of hid-generic.

The Fn key does not work yet though.
Comment 41 ncopa 2017-09-04 20:01:52 UTC
Created attachment 258205 [details]
Fix Fn key on bluetooth Apple Magic Keyboard

This patch fixes the Fn Key on Apple Magic Keyboard when connected via bluetooth.
Comment 42 Guy Thouret 2017-09-05 13:50:56 UTC
I can confirm the patch from ncopa makes the Fn key and Media/Function keys work on the Apple Magic Keyboard 2 (A1644).

@ncopa What's the significance of usage->hid hack for this model?  I'm not familiar with the hid api and was missing that from my previous attempt at support for this model.
Comment 43 ncopa 2017-09-05 14:31:10 UTC
(In reply to Guy Thouret from comment #42)
> I can confirm the patch from ncopa makes the Fn key and Media/Function keys
> work on the Apple Magic Keyboard 2 (A1644).
> 
> @ncopa What's the significance of usage->hid hack for this model?  I'm not
> familiar with the hid api and was missing that from my previous attempt at
> support for this model.

I don't know to be honest and this is my first meeting with hid api too. It appears to be a vendor specific quirk to set up the Fn key mapping. Apparently there was some previous quirk for older models that has been there for long time (according git log).

I discovered it while doing some printf debugging around things that had KEY_FN and analyzing the git log of hid-input.c.
Comment 44 jan.tumanov 2017-09-12 12:37:08 UTC
I can also confirm the patch from ncopa makes the Fn key and Media/Function keys work on the Apple Magic Keyboard 2 (A1644).
Comment 45 Stephane Wirtel 2017-09-13 14:20:27 UTC
and when do you think we could have this patch in the mainstream ?

Thank you
Comment 46 ncopa 2017-09-13 16:20:14 UTC
I sent the patch upstream:

http://www.spinics.net/lists/linux-input/msg53006.html
Comment 47 Stijn Martens 2018-05-08 22:41:02 UTC
It seems this is now broken again, I have this problem in 4.17-0-rc4

My Fn-keys are not working over Bluetooth, they are working over USB.

USB uses driver: usbhid
Bluetooth uses driver: hid-generic
Not sure if this is part of the problem.

Initially I described the problem here:
https://askubuntu.com/questions/1033788/apple-keyboard-fn-not-working-with-bluetooth-but-works-with-usb
Comment 48 kernel 2018-06-06 18:10:55 UTC
I am also noticing the same issue as Stijn Martens. When my keyboard is connected via USB it works as expected, but over bluetooth, hid_apple isn't being used.

John
Comment 49 ncopa 2018-06-07 12:32:27 UTC
Patch in comment #41 should fix it. It is not applied to git yet. There have been no comment on the patch i sent either.
Comment 50 Stijn Martens 2018-06-07 12:36:10 UTC
I tried manually applying the patch on a newer version and compiling it. But it did not work out. First time trying to change & compile Linux myself.

The patch may need to be updated before it can be accepted.
Comment 51 Zhuravlev Uriy 2018-07-05 00:13:16 UTC
(In reply to ncopa from comment #49)
> Patch in comment #41 should fix it. It is not applied to git yet. There have
> been no comment on the patch i sent either.

I suppose you should have added hid-apple maintainers to Cc, without it maintainer like Dmitriy just skip this patch. (it's just from my experience)
Also, please, update patch to modern kernel version probably I can write something to maillist too.
Comment 52 ncopa 2018-10-17 12:11:55 UTC
This is partially fixed with
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/drivers/hid?id=ee345492437043a79db058a3d4f029ebcb52089a

However, the Fn key still does not work. Apparently it still picks hid-generic driver:

[107893.090949] hid-generic 0005:004C:0267.0007: unknown main item tag 0x0
[107893.091122] input: Magic Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8:1.0/bluetooth/hci0/hci0:256/0005:004C:0267.0007/input/input24
[107893.091794] hid-generic 0005:004C:0267.0007: input,hidraw3: BLUETOOTH HID v0.67 Keyboard [Magic Keyboard] on a4:34:d9:5a:c7:14
Comment 53 Fabio Falci 2018-10-17 14:05:10 UTC
It's working for me:


[   22.260447] apple 0005:004C:0267.0007: unknown main item tag 0x0
[   22.260626] input: FabsKeyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.2/bluetooth/hci0/hci0:12/0005:004C:0267.0007/input/input18
[   22.261181] apple 0005:004C:0267.0007: input,hidraw6: BLUETOOTH HID v0.67 Keyboard [FabsKeyboard] on a0:99:9b:19:24:18


Fn key is working, using bluetooth.

Now I'm able to use:

$ cat /etc/modprobe.d/hid_apple.conf
options hid_apple swap_opt_cmd=1
options hid_apple fnmode=2
options hid_apple iso_layout=0


Which before only worked when cabled.
Comment 54 ncopa 2018-10-17 15:55:01 UTC
It did not work on 4.14.76 for me.
(In reply to Fabio Falci from comment #53)
> It's working for me:
> 
> 
> [   22.260447] apple 0005:004C:0267.0007: unknown main item tag 0x0
> [   22.260626] input: FabsKeyboard as
> /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.2/bluetooth/hci0/hci0:12/
> 0005:004C:0267.0007/input/input18
> [   22.261181] apple 0005:004C:0267.0007: input,hidraw6: BLUETOOTH HID v0.67
> Keyboard [FabsKeyboard] on a0:99:9b:19:24:18
> 
> Fn key is working, using bluetooth.

What kernel is it? It did not work for me on 4.14.76.
Comment 55 Fabio Falci 2018-10-17 15:57:20 UTC
$ uname -a
Linux fabs 4.18.14-arch1-1-ARCH #1 SMP PREEMPT Sat Oct 13 13:42:37 UTC 2018 x86_64 GNU/Linux
Comment 56 ncopa 2018-10-18 06:49:46 UTC
I think an additional patch is needed for kernel pre 4.16.
Comment 57 ncopa 2018-10-18 06:50:40 UTC
(In reply to ncopa from comment #56)
> I think an additional patch is needed for kernel pre 4.16.

I mean pre 4.17. So for 4.16 kernels and older.
Comment 58 Martin 2019-01-24 22:29:34 UTC
Is there any news on this? Was the patch for the fn-key merged in a later kernel?
Comment 59 kevin 2019-07-07 01:08:08 UTC
Is this issue still being addressed?

I'm on 4.15.0, and the function key doesn't work over Bluetooth.
Comment 60 Per Edin 2019-11-20 10:52:46 UTC
Still experiencing this problem over bluetooth on

Linux z 5.3.0-23-generic #25-Ubuntu SMP Tue Nov 12 09:22:33 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Was the patch ever merged? Or was it merged for v4.x only?
Comment 61 Fabio Falci 2019-11-21 10:41:23 UTC
I can confirm that it has worked, as my previous messages say, at least on 4.18.14.


However it stopped working at some point, unfortunately, I was switching between laptops at the time and don't have access to my previous one where it worked. On my new laptop, it doesn't work. 


So not sure what version it has stopped working but I'd say that it worked on 5.x.
Comment 62 Per Edin 2019-11-21 13:25:58 UTC
Just an update, it *does* work on another machine with a different keyboard, running:

Linux y 5.3.0-19-generic #20-Ubuntu SMP Fri Oct 18 09:04:39 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

I downgraded the -23 machine to -19 (debian revision) without change.

Could it be firmware differences in the two keyboards?
Comment 63 Josh Goebel 2020-01-11 08:02:19 UTC
I have a version 1.0 identifying Apple Magic Keyboard 2.  From what I can tell (at a quick glance) it's descriptor does NOT match what the kernel is expecting.

The kernel is expecting either 0xFF000003 or 0x00FF0003 for Usage HID.  From my reading of the description it seems like it would be: 0xFF010003.

Seems we need at add another Usage HID variant?

Relevant descriptor dump:

```
     Report Count (1),
        Report Size (1),
        Logical Minimum (0),
        Logical Maximum (1),
        Usage Page (Consumer),                      ; Consumer (0Ch)
        Usage (Eject),                              ; Eject (B8h, one-shot control)
        Input (Variable),
        Report Count (1),
        Report Size (1),
        Usage Page (FF01h),                         ; FF01h, vendor-defined
        Usage (03h),
        Input (Variable),
```

Sorry if this is formatted terribly.  Not matter what I do the Preview looks terrible and this is my first time posting here.
Comment 64 Mansour Behabadi 2020-01-29 04:07:57 UTC
The firmware of my Magic Keyboard (A1644) got updated to version 0x0100 as reported by System Information on macOSX. The keyboard used to work fine in bluetooth mode with fn-mode behavior adjustable through /sys/module/hid_apple/parameters/fnmode. However, after the firmware update, the fn key no longer works as expected. Instead, pressing the fn alone turns the screen brightness low and it can no longer be configured via the fnmode parameter nor does it function in any other way.
Comment 65 Mansour Behabadi 2020-01-29 05:17:20 UTC
Sure enough, reading out the report descriptor, I'm getting an extra key mapped to BrightnessDown:


$ cat /sys/kernel/debug/hid/0005\:004C\:0267.0005/rdesc

...

ff01.0003 ---> Key.BrightnessDown

...
Comment 67 Dancon 2020-02-15 12:09:08 UTC
(In reply to Mansour Behabadi from comment #66)
> Submitted a patch to the kernel: https://lkml.org/lkml/2020/1/29/61
> potentially fixing https://bugzilla.kernel.org/show_bug.cgi?id=99881#c62 ,
> https://bugzilla.kernel.org/show_bug.cgi?id=99881#c63 and
> https://bugzilla.kernel.org/show_bug.cgi?id=99881#c64 .

Thank u so much. It's save my day.

Linux 5.2.1-050201-generic, this patch fix fn key for MLA22
Comment 68 Mansour Behabadi 2020-03-22 11:24:03 UTC
This patch [1] is now available in mainline, stable and longterm releases.


[1]: https://bugzilla.kernel.org/show_bug.cgi?id=99881#c66
Comment 69 David 2020-06-01 18:06:16 UTC
I'm running 5.6.14-300 in Fedora 32.
 
I'm currently having problems with the Magic Keyboard 2, but only when using Bluetooth. It works fine when connected via USB.
 
The problem is that it's not picking up the driver settings. The one I'm trying to set is `iso_layout`, as I'm having the typical <> keys wrongly mapped. No matter what option I set on /parameters/iso_layout, the behaviour is still the same. It's like the driver is not correctly loaded.
 
When I connect it via USB, I can see it reacting to the changes in /parameters/iso_layout correctly in real time, but not when using the Bluetooth connection.
 
dmesg | grep input

```
[   46.012620] input: Magic Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-10/1-10:1.0/bluetooth/hci0/hci0:256/0005:004C:0267.0006/input/input27
[   46.013565] input: Magic Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-10/1-10:1.0/bluetooth/hci0/hci0:256/0005:004C:0267.0006/input/input28
[   46.013867] hid-generic 0005:004C:0267.0006: input,hidraw5: BLUETOOTH HID v1.00 Keyboard [Magic Keyboard] on 4c:1d:96:7c:7b:70
[   46.067136] input: Magic Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-10/1-10:1.0/bluetooth/hci0/hci0:256/0005:004C:0267.0006/input/input29
[   46.067530] apple 0005:004C:0267.0006: input,hidraw5: BLUETOOTH HID v1.00 Keyboard [Magic Keyboard] on 4c:1d:96:7c:7b:70

```