Commit Graph

312 Commits

Author SHA1 Message Date
Luiz Augusto von Dentz
9e0bc3e63c build: Fix missing include
This fixes the following build error:

profiles/input/manager.c:102:4: error: implicit declaration of function ‘free’ [-Werror=implicit-function-declaration]
  102 |    free(uhid_enabled);
      |    ^~~~
2024-10-30 09:33:59 -04:00
Luiz Augusto von Dentz
1782bfd791 input: Add support for UserspaceHID=persist
This adds support for persist mode via input.conf:UserspaceHID but
don't default to it since this appears to create more problems than
it solves.

Fixes: https://github.com/bluez/bluez/issues/983
Fixes: https://github.com/bluez/bluez/issues/977
Fixes: https://github.com/bluez/bluez/issues/949
2024-10-28 10:40:31 -04:00
Luiz Augusto von Dentz
d5c68dbed1 hog-lib: Add logging when bt_uhid_new fails
If bt_uhid_new fails there is likely a problem with uHID module or
bluetoothd don't have previleges to access /dev/uhid.
2024-08-19 16:38:53 -04:00
Luiz Augusto von Dentz
1cc3cf08c4 hog-lib: Fix calling bt_uhid_destroy with invalid instance
If the hog->uhid cannot be created then it is pointless to call
bt_uhid_destroy as it will likely just produce bogus output as in:

https://github.com/bluez/bluez/issues/529#issuecomment-2297350805
2024-08-19 16:32:27 -04:00
Luiz Augusto von Dentz
5ebaeab416 hog-lib: Fix passing wrong parameters to bt_uhid_get_report_reply
The parameters of bt_uhid_get_report_reply are number followed by
status, not status followed by number, so the order needs to be
swapped.

Fixes: https://github.com/bluez/bluez/issues/880
2024-06-24 16:12:50 -04:00
Luiz Augusto von Dentz
e4f6c68f2a shared/uhid: Add special handling for non-keyboards
This adds special handling for non-keyboards so they are destroyed when
disconnected rather than keeping their device node around.

Fixes: https://github.com/bluez/bluez/issues/848
2024-05-20 14:14:11 -04:00
Luiz Augusto von Dentz
db383a5b4a hog-lib: Add force parameter to bt_hog_detach
This adds force parameter to bt_hog_detach which indicates if the
bt_uhid_destroy shall be called.
2024-05-20 14:14:06 -04:00
Luiz Augusto von Dentz
b163e2bd03 hog-lib: Destroy uHID device if there is traffic while disconnected
This attempts to destroy input device if there is an attempt to
communicate with it while disconnected.
2024-04-10 09:57:03 -04:00
Luiz Augusto von Dentz
528f5a8c7d input/device: Make use of bt_uhid_replay
This makes use of bt_uhid_replay to replay the GET/SET_REPORT messages
recorded as it offer the same functionality as hidp_replay.
2024-04-10 09:56:55 -04:00
Luiz Augusto von Dentz
a78c839b5d hog-lib: Make use of bt_uhid_replay
This makes use of bt_uhid_replay to replay the GET/SET_REPORT messages
recorded during the first time the device is created.
2024-04-10 09:56:47 -04:00
Luiz Augusto von Dentz
c6dea88698 input/device: Add replay support
This adds replay support when uhid is in use and the input node is keep
while disconnected:

Fixes: https://github.com/bluez/bluez/issues/777
2024-04-05 17:11:27 -04:00
Luiz Augusto von Dentz
94c4f445af input.conf: Change IdleTimeout unit to be in seconds
This changes IdleTimeout unit to be in seconds instead of in minutes
which offer better granularity.
2024-04-05 10:58:05 -04:00
Luiz Augusto von Dentz
ea96d7d18b input/device: Fix not handling IdleTimeout when uhid is in use
When uhid is in use IdleTimeout was not taking any effect, this also
attempt to force the destroy the input device node to make it useful
for users that don't want to keep the input node forever.
2024-04-05 10:58:05 -04:00
Luiz Augusto von Dentz
b8ad3490a3 input/device: Force UHID_DESTROY on error
If we cannot sent a message back to the device destroy the UHID device
since it is likely that the driver is trying to reach the remote device
which is no longer connected.
2024-03-20 10:51:48 +00:00
Luiz Augusto von Dentz
c0c9e462be input/device: Use bt_uhid functions
This makes use of bt_uhid function instead of directly submitting
events directly using bt_uhid_send.

Fixes: https://github.com/bluez/bluez/issues/771
2024-03-11 18:42:38 -04:00
Luiz Augusto von Dentz
256d0b594d hog-lib: Use bt_uhid functions
This makes use of bt_uhid function instead of directly submitting
events directly using bt_uhid_send.
2024-03-11 15:54:31 -04:00
Luiz Augusto von Dentz
ee880bee85 input/device: Don't destroy UHID device on disconnect
This makes classic HID behave like HoG which keeps the UHID device
around while disconnected so it doesn't have to be recreated on every
reconnection.
2024-02-14 13:56:32 -05:00
Luiz Augusto von Dentz
9698870015 input.conf: Make UserspaceHID defaults to true
This makes UserspaceHID defaults to true so the plugin has more control
over the input device lifetime.
2024-02-14 13:55:18 -05:00
Luiz Augusto von Dentz
247ae85248 hog-lib: Don't destroy UHID device on detach
This makes bt_hog_detach not to destroy UHID device which means the
device node don't need to be recreated in case of reconnections which
speeds up the process.

Fixes: https://github.com/bluez/bluez/issues/737
2024-02-14 13:55:18 -05:00
Emil Velikov
1313351f71 profiles: remove unused suspend-dummy.c
The file has been used for about 8 years now - see commit
fb55b7a6ab ("profiles/hog: Use no suspend
support instead of the dummy FIFO").

Inspired by a Debian patch by Nobuhiro Iwamatsu, which was changing the
/tmp/hogsuspend socket patch to /run. Looking through the codebase we
have a few more sockets that could use a to /run fix, but that will
follow-up at another day.

Cc: Nobuhiro Iwamatsu <iwamatsu@debian.org>
2024-02-12 17:35:33 -05:00
Luiz Augusto von Dentz
afb47b13c6 input: Fix smatch warning
This fixes the following warning:

profiles/input/device.c:165:26: warning: Variable length array is used.
2023-10-10 16:41:17 -07:00
Luiz Augusto von Dentz
25a471a83e input.conf: Change default of ClassicBondedOnly
This changes the default of ClassicBondedOnly since defaulting to false
is not inline with HID specification which mandates the of Security Mode
4:

BLUETOOTH SPECIFICATION Page 84 of 123
Human Interface Device (HID) Profile:

  5.4.3.4.2 Security Modes
  Bluetooth HID Hosts shall use Security Mode 4 when interoperating with
  Bluetooth HID devices that are compliant to the Bluetooth Core
  Specification v2.1+EDR[6].
2023-10-10 16:41:17 -07:00
Luiz Augusto von Dentz
3a9c637010 input: Fix .device_probe failing if SDP record is not found
Due to changes introduced by 67a26abe53
("profile: Add probe_on_discover flag") profiles may get probed when
their profile UUID are discovered, rather than resolved, which means
the SDP record may not be available.

Fixes: https://github.com/bluez/bluez/issues/614
2023-10-10 16:41:17 -07:00
Luiz Augusto von Dentz
2f4c576ad2 hog-lib: Fix not handling BT_ATT_OP_HANDLE_NFY_MULT
This is a temporary fix for not handling BT_ATT_OP_HANDLE_NFY_MULT in
GAttr so the code will use g_attrib_attach_client to attach the
bt_gatt_client instance which is then used to register notifications
including those sent with BT_ATT_OP_HANDLE_NFY_MULT.

Fixes: https://github.com/bluez/bluez/issues/71
2023-01-05 14:01:57 -08:00
Luiz Augusto von Dentz
60663d4af3 hog-lib: Fix scan-build error
This fixes the following errors:
profiles/input/hog-lib.c:600:19: warning: Access to field 'handle'
results in a dereference of a null pointer (loaded from variable 'chr')
        report->handle = chr->handle;
                         ^~~~~~~~~~~
profiles/input/hog-lib.c:637:11: warning: Access to field 'value_handle'
results in a dereference of a null pointer (loaded from variable 'chr')
                start = chr->value_handle + 1;
                        ^~~~~~~~~~~~~~~~~
profiles/input/hog-lib.c:1240:11: warning: Access to field 'value_handle'
results in a dereference of a null pointer (loaded from variable 'chr')
                start = chr->value_handle + 1;
                        ^~~~~~~~~~~~~~~~~
2022-08-16 15:04:12 -07:00
Luiz Augusto von Dentz
952c08ff50 hog-lib: Don't restrict Report MAP size
Although HIDS spec is quite clear the Report MAP shall be limited to
512 bytes it doesn't seem OS do enforce that on the profile/client side
and since there isn't any qualification test enforcing it either there
are quite many devices which uses Report MAP bigger that 512 bytes
(e.g.: Brydge W-Touch and Lenovo Duet 3 BT Folio).

https://github.com/bluez/bluez/issues/377
2022-08-16 14:16:19 -07:00
Archie Pusaka
c159d790e8 input/device: Notify failure if ctrl disconnect when waiting intr
On some rare occasions, the peer HID device might disconnect the ctrl
channel when we are trying to connect the intr channel. If this
happens, interrupt_connect_cb() will not be called by btio, and we
will be stuck in "connecting" state. Any future connection attempt to
the peer device will fail because of "busy".

This patch prevents that by checking if we need to report connection
failure when the ctrl channel is disconnected.

Reviewed-by: Sonny Sasaka <sonnysasaka@chromium.org>
2022-05-18 15:19:43 -07:00
Luiz Augusto von Dentz
c7955b2099 hog-lib: Fix not reading report_map of instances
If there is multiple instances the gatt_db of the instances was not
initialized causing the report_map_attr to be NULL which prevents the
report_map to be read and uhid device to be created.

Fixes: https://github.com/bluez/bluez/issues/298
2022-05-12 18:04:20 -07:00
Luiz Augusto von Dentz
c535623a71 hog-lib: Check if Report ID is set before prepending it
Before prepending the Report ID check if it is non-zero:

BLUETOOTH SPECIFICATION Page 16 of 26
HID Service Specification

Report ID shall be nonzero in a Report Reference characteristic
descriptor where there is more than one instance of the Report
characteristic for any given Report Type.

Fixes: https://www.spinics.net/lists/linux-bluetooth/msg97262.html
2022-04-22 13:03:34 -07:00
Luiz Augusto von Dentz
d91fe6994f hog-lib: Fix not waiting for UHID_START
With use of UHID_CREATE2 the code needs to wait for UHID_START in order
to know if the reports are numbered or not.

Fixes: https://github.com/bluez/bluez/issues/298
2022-02-25 15:35:04 -08:00
Tedd Ho-Jeong An
002c0c9eda profiles: Fix the reusing gerror without re-initialization
When the GError variable is freeed with g_error_free(), it is not set to
NULL and reusing the same variable again can cause the seg_fault because
it is still pointing the old memory address which is freed.

This patch relaces the g_error_free() to g_clear_error() which frees the
variable and set it to NULL if the variable is used in the function
again.

Fixes: 4ad622d592 ("profiles/a2dp: Fix unchecked return value")
2022-02-11 14:17:27 -08:00
Luiz Augusto von Dentz
0516d2527e hog-lib: Make use of UHID_CREATE2
This convert the use of UHID_CREATE to UHID_CREATE2 which gives back
the information of reports being numbered or not thus avoiding the need
to parse the report map in order to check if there are a report number
on them since UHID_START gives that information:

UHID_START:
  This is sent when the HID device is started. Consider this as an
  answer to UHID_CREATE2. This is always the first event that is sent.
  Note that this event might not be available immediately after
  write(UHID_CREATE2) returns.
  Device drivers might require delayed setups.
  This event contains a payload of type uhid_start_req. The "dev_flags"
  field describes special behaviors of a device. The following flags
  are defined:

      - UHID_DEV_NUMBERED_FEATURE_REPORTS
      - UHID_DEV_NUMBERED_OUTPUT_REPORTS
      - UHID_DEV_NUMBERED_INPUT_REPORTS

  Each of these flags defines whether a given report-type uses numbered
  reports. If numbered reports are used for a type, all messages from
  the kernel already have the report-number as prefix. Otherwise, no
  prefix is added by the kernel. For messages sent by user-space to the
  kernel, you must adjust the prefixes according to these flags
2022-01-18 16:18:29 -08:00
Luiz Augusto von Dentz
33d13bbc57 hog: Add input queue while uhid device has not been created
Since report map is only read after all reports it is possible to start
receiving notifications before uhid has been created, so this adds a
queue to store the events while the report map is pending and once uhid
has been created dequeue the events.
2022-01-13 13:34:53 -08:00
Luiz Augusto von Dentz
7fe38a17f6 hog: Fix read order of attributes
The Report Map must be read after all other attributes otherwise the
Kernel driver may start using UHID_SET_REPORT which requires the
report->id to be known in order to resolve the attribute to send to.

Fixes: https://github.com/bluez/bluez/issues/220
2022-01-13 13:34:53 -08:00
Luiz Augusto von Dentz
cfab569484 build: Replace use of g_memdup with util_memdup
This replaces the uses of g_memdup with util_memdup since the former has
been deprecated:

  warning: ‘g_memdup’ is deprecated: Use 'g_memdup2' instead
  [-Wdeprecated-declarations]

g_memdup2 requires bumping glib version which would likely have its
own problems thus why util_memdup was introduced.
2022-01-06 12:58:39 -08:00
Luiz Augusto von Dentz
b312fa0c9f uhid: Remove local copy of uhid header
uhid.h is part of kernel uapi nowadays so it can be included
directly from linux/uhid.h so this removes the local copy to use it
instead.
2021-11-22 13:49:18 -08:00
Tedd Ho-Jeong An
cd75918d21 profiles/input: Fix unchecked return value
This patch fixes the unchecked return value(CWE-252) issues reported by
the Coverity.
2021-10-18 15:31:55 -07:00
Dmitry Torokhov
b5b7e6203b input/hog-lib: do not silently ignore missing connection in read_char()
Currently we silently ignore missing connection (attrib) in read_char(),
but not in the other GATT interfaces (such as write_char, discover_desc,
etc). The code should avoid calling read_char() when there is no active
connection instead, and logging errors will help us trace the offenders.
2021-04-08 10:36:23 -07:00
Dmitry Torokhov
8f15296d8b input/hog-lib: avoid scanning characteristics too early
We need to have active connection to fully discover a HOG instance,
and in the chain

	bt_hog_new()->
		gatt_db_foreach_service()->
		foreach_hog_service()->
		hog_attach_instance()

we have not set up hog->attrib yet. So let's skip calling
foreach_hog_chrc() from hog_attach_instance(), especially since
we will be calling bt_hog_attach() pretty much immediately after
bt_hog_new(), and we will be discovering characteristics there anyway.
2021-04-08 10:36:23 -07:00
Dmitry Torokhov
d971ca5ca3 input/hog-lib: add error handling when calling into gatt
When calling gatt_write_char(), gatt_read_char(), etc, id == 0 indicates
error. Let's recognize this fact and log it instead of queueing request
that will never be completed.
2021-04-08 10:36:22 -07:00
Luiz Augusto von Dentz
3f4039f43b hog-lib: Fix crash when receiving UHID_GET_REPORT
If UHID_GET_REPORT is received but a report cannot be found, etc, the
would pass bt_hog as user_data instead of report to get_report_cb
leading to a crash.

Fixes https://github.com/bluez/bluez/issues/112
2021-03-31 14:44:44 -07:00
Frédéric Danis
389bcb2b78 profiles: Use timeout_add_seconds
Replace calls to g_timeout_add_seconds() by the timeout_add_seconds()
wrapper which takes care of 0 delay.
2021-03-16 10:56:36 -07:00
Brian Gix
0fea1b365a profiles: Cleanup deprecated symbolic file permissions 2021-02-23 16:59:46 -08:00
Szymon Janc
61745d2bb8 sixaxis: Fix Bluetooth PS3 clone joypad being named like the original
When cable pairing a PS3 clone device, we should try and keep the USB device
name to create a new btd_device so that the joypad is named after its USB name
when connecting through Bluetooth.

If that isn't done, "Shanwan" clone joypads are named like the genuine joypads, and
kernel Bluetooth quirks aren't applied.

gh-issue: https://github.com/bluez/bluez/issues/46
2021-02-22 09:22:59 +01:00
Sonny Sasaka
f21dfebe76 input/hog: Fix double registering report value callbacks
In commit 23b69ab3e4 ("input/hog: Cache the HID report map"), we
optimized HOG reconnection by registering report value callbacks early,
but there was a bug where we also re-register the same report value
callbacks after at CCC write callback. We should handle this case by
avoiding the second callback register if we know we have done it
earlier.
2021-01-25 13:52:58 -08:00
Sonny Sasaka
23b69ab3e4 input/hog: Cache the HID report map
To optimize BLE HID devices reconnection response, we can cache the
report map so that the subsequent reconnections do not need round trip
time to read the report map.
2020-12-14 15:04:36 -08:00
Sonny Sasaka
5fb9c9feb1 Revert "input/hog: Remove HID device after HoG device disconnects"
This reverts commit d6cafa1f0c.

In commit d6cafa1f0c ("input/hog: Remove HID device after HoG device
disconnects"), the bt_hog structure is destroyed in order to fix a bug
where the UHID connection is not destroyed. This fix has the cost of
increasing reconnection time because every reconnection would need to
re-read the report map again. An improvement to this fix is, instead of
removing the bt_hog structure, we can just destroy the UHID with
UHID_DESTROY event and use the existing bt_hog structure to keep the
cache of the report map to avoid re-reading the report map at
reconnection.
2020-12-14 15:04:23 -08:00
Dean Camera
35a2c50437 hog: Fix stripping off read report bytes
If the HID subsystem requests a HID report to be read from the
device, we currently incorrectly strip off the first byte of the
response, if the device has report IDs set in the HID report
descriptor.

This is incorrect; unlike USB HID, the report ID is *not* included
in the HOG profile's HID reports, and instead exists out of band
in a descriptor on the report's bluetooth characteristic in the
device.

In this patch, we remove the erroneous stripping of the first
byte of the report, and (if report IDs are enabled) prepend the
report ID to the front of the result. This makes the HID report
returned indentical in format to that of a USB HID report, so
that the upper HID drivers can consume HOG device reports in the
same way as USB.
2020-11-20 09:46:02 -08:00
Tedd Ho-Jeong An
e59926d762 profiles: Add SPDX License Identifier
This patch adds SPDX License Identifier and removes the license text.

-------------------------------------
       License            COUNT
-------------------------------------
 GPL-2.0-or-later     :     72
 LGPL-2.1-or-later    :      6

License: GPL-2.0-or-later
   profiles/deviceinfo/dis.c
   profiles/deviceinfo/deviceinfo.c
   profiles/health/hdp_types.h
   profiles/health/mcap.c
   profiles/health/hdp_manager.c
   profiles/health/hdp_main.c
   profiles/health/mcap.h
   profiles/health/hdp_util.h
   profiles/health/hdp_manager.h
   profiles/health/hdp_util.c
   profiles/health/hdp.c
   profiles/health/hdp.h
   profiles/network/server.c
   profiles/network/connection.h
   profiles/network/connection.c
   profiles/network/manager.c
   profiles/network/bnep.c
   profiles/network/server.h
   profiles/network/bnep.h
   profiles/battery/battery.c
   profiles/cups/cups.h
   profiles/cups/sdp.c
   profiles/cups/main.c
   profiles/cups/spp.c
   profiles/cups/hcrp.c
   profiles/scanparam/scpp.c
   profiles/scanparam/scan.c
   profiles/audio/transport.h
   profiles/audio/avdtp.h
   profiles/audio/source.c
   profiles/audio/player.c
   profiles/audio/avctp.c
   profiles/audio/control.h
   profiles/audio/a2dp.c
   profiles/audio/source.h
   profiles/audio/transport.c
   profiles/audio/sink.c
   profiles/audio/avrcp.h
   profiles/audio/avdtp.c
   profiles/audio/a2dp.h
   profiles/audio/avrcp.c
   profiles/audio/media.h
   profiles/audio/avctp.h
   profiles/audio/control.c
   profiles/audio/sink.h
   profiles/audio/player.h
   profiles/audio/media.c
   profiles/sap/server.c
   profiles/sap/sap-dummy.c
   profiles/sap/main.c
   profiles/sap/sap.h
   profiles/sap/manager.c
   profiles/sap/server.h
   profiles/sap/manager.h
   profiles/iap/main.c
   profiles/gap/gas.c
   profiles/midi/midi.c
   profiles/midi/libmidi.c
   profiles/midi/libmidi.h
   profiles/input/server.c
   profiles/input/suspend.h
   profiles/input/uhid_copy.h
   profiles/input/device.c
   profiles/input/hog.c
   profiles/input/device.h
   profiles/input/hidp_defs.h
   profiles/input/manager.c
   profiles/input/suspend-none.c
   profiles/input/sixaxis.h
   profiles/input/hog-lib.c
   profiles/input/suspend-dummy.c
   profiles/input/server.h

License: LGPL-2.1-or-later
   profiles/deviceinfo/dis.h
   profiles/battery/bas.h
   profiles/battery/bas.c
   profiles/scanparam/scpp.h
   profiles/audio/a2dp-codecs.h
   profiles/input/hog-lib.h
2020-09-21 16:19:36 -07:00
Sonny Sasaka
eb4b99a433 input/device: Unregister all UHID event listeners at UHID_DESTROY
When destroying UHID, we should also unregister all event listeners so
that they don't get double registered at reconnection. It fixes a bug
where battery report is not available to kernel after reconnection and
also prevents memory leak.

Tested with Logitech M535 mouse:
* Connect mouse to the device running BlueZ
* cat /sys/class/power_supply/hid-{addr}-battery/capacity # works
* Disconnect mouse
* Reconnect mouse
* cat /sys/class/power_supply/hid-{addr}-battery/capacity # still works
2020-08-21 10:50:11 -07:00