Blame SOURCES/v5-2-4-bluetooth-separate-HSP-and-HFP.patch

e44045
diff --git a/src/modules/bluetooth/backend-native.c b/src/modules/bluetooth/backend-native.c
e44045
index f2009bfd..9ec9244b 100644
e44045
--- a/src/modules/bluetooth/backend-native.c
e44045
+++ b/src/modules/bluetooth/backend-native.c
e44045
@@ -62,6 +62,7 @@ struct transport_data {
e44045
 #define BLUEZ_PROFILE_INTERFACE BLUEZ_SERVICE ".Profile1"
e44045
 
e44045
 #define HSP_AG_PROFILE "/Profile/HSPAGProfile"
e44045
+#define HFP_AG_PROFILE "/Profile/HFPAGProfile"
e44045
 #define HSP_HS_PROFILE "/Profile/HSPHSProfile"
e44045
 
e44045
 /* RFCOMM channel for HSP headset role
e44045
@@ -512,6 +513,8 @@ static DBusMessage *profile_new_connection(DBusConnection *conn, DBusMessage *m,
e44045
         p = PA_BLUETOOTH_PROFILE_HSP_HS;
e44045
     } else if (pa_streq(handler, HSP_HS_PROFILE)) {
e44045
         p = PA_BLUETOOTH_PROFILE_HFP_AG;
e44045
+    } else if (pa_streq(handler, HFP_AG_PROFILE)) {
e44045
+        p = PA_BLUETOOTH_PROFILE_HFP_HF;
e44045
     } else {
e44045
         pa_log_error("Invalid handler");
e44045
         goto fail;
e44045
@@ -589,7 +592,8 @@ static DBusHandlerResult profile_handler(DBusConnection *c, DBusMessage *m, void
e44045
 
e44045
     pa_log_debug("dbus: path=%s, interface=%s, member=%s", path, interface, member);
e44045
 
e44045
-    if (!pa_streq(path, HSP_AG_PROFILE) && !pa_streq(path, HSP_HS_PROFILE))
e44045
+    if (!pa_streq(path, HSP_AG_PROFILE) && !pa_streq(path, HSP_HS_PROFILE)
e44045
+        && !pa_streq(path, HFP_AG_PROFILE))
e44045
         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
e44045
 
e44045
     if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
e44045
@@ -634,6 +638,10 @@ static void profile_init(pa_bluetooth_backend *b, pa_bluetooth_profile_t profile
e44045
             object_name = HSP_HS_PROFILE;
e44045
             uuid = PA_BLUETOOTH_UUID_HSP_HS;
e44045
             break;
e44045
+        case PA_BLUETOOTH_PROFILE_HFP_HF:
e44045
+            object_name = HFP_AG_PROFILE;
e44045
+            uuid = PA_BLUETOOTH_UUID_HFP_AG;
e44045
+            break;
e44045
         default:
e44045
             pa_assert_not_reached();
e44045
             break;
e44045
@@ -653,6 +661,9 @@ static void profile_done(pa_bluetooth_backend *b, pa_bluetooth_profile_t profile
e44045
         case PA_BLUETOOTH_PROFILE_HFP_AG:
e44045
             dbus_connection_unregister_object_path(pa_dbus_connection_get(b->connection), HSP_HS_PROFILE);
e44045
             break;
e44045
+        case PA_BLUETOOTH_PROFILE_HFP_HF:
e44045
+            dbus_connection_unregister_object_path(pa_dbus_connection_get(b->connection), HFP_AG_PROFILE);
e44045
+            break;
e44045
         default:
e44045
             pa_assert_not_reached();
e44045
             break;
e44045
@@ -695,6 +706,8 @@ pa_bluetooth_backend *pa_bluetooth_native_backend_new(pa_core *c, pa_bluetooth_d
e44045
     if (enable_hs_role)
e44045
        profile_init(backend, PA_BLUETOOTH_PROFILE_HFP_AG);
e44045
     profile_init(backend, PA_BLUETOOTH_PROFILE_HSP_HS);
e44045
+    if (pa_bluetooth_discovery_get_enable_native_hfp_hf(y))
e44045
+        profile_init(backend, PA_BLUETOOTH_PROFILE_HFP_HF);
e44045
 
e44045
     return backend;
e44045
 }
e44045
@@ -707,6 +720,8 @@ void pa_bluetooth_native_backend_free(pa_bluetooth_backend *backend) {
e44045
     if (backend->enable_hs_role)
e44045
        profile_done(backend, PA_BLUETOOTH_PROFILE_HFP_AG);
e44045
     profile_done(backend, PA_BLUETOOTH_PROFILE_HSP_HS);
e44045
+    if (pa_bluetooth_discovery_get_enable_native_hfp_hf(backend->discovery))
e44045
+      profile_done(backend, PA_BLUETOOTH_PROFILE_HFP_HF);
e44045
 
e44045
     pa_dbus_connection_unref(backend->connection);
e44045
 
e44045
diff --git a/src/modules/bluetooth/bluez5-util.c b/src/modules/bluetooth/bluez5-util.c
e44045
index 4470f2ef..80a025d5 100644
e44045
--- a/src/modules/bluetooth/bluez5-util.c
e44045
+++ b/src/modules/bluetooth/bluez5-util.c
e44045
@@ -92,6 +92,7 @@ struct pa_bluetooth_discovery {
e44045
     int headset_backend;
e44045
     pa_bluetooth_backend *ofono_backend, *native_backend;
e44045
     PA_LLIST_HEAD(pa_dbus_pending, pending);
e44045
+    bool enable_native_hfp_hf;
e44045
 };
e44045
 
e44045
 static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusMessage *m,
e44045
@@ -169,14 +170,27 @@ static const char *transport_state_to_string(pa_bluetooth_transport_state_t stat
e44045
 }
e44045
 
e44045
 static bool device_supports_profile(pa_bluetooth_device *device, pa_bluetooth_profile_t profile) {
e44045
+    bool show_hfp, show_hsp, enable_native_hfp_hf;
e44045
+
e44045
+    enable_native_hfp_hf = pa_bluetooth_discovery_get_enable_native_hfp_hf(device->discovery);
e44045
+
e44045
+    if (enable_native_hfp_hf) {
e44045
+        show_hfp = pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_HFP_HF);
e44045
+        show_hsp = !show_hfp;
e44045
+    } else {
e44045
+        show_hfp = false;
e44045
+        show_hsp = true;
e44045
+    }
e44045
+
e44045
     switch (profile) {
e44045
         case PA_BLUETOOTH_PROFILE_A2DP_SINK:
e44045
             return !!pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_A2DP_SINK);
e44045
         case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
e44045
             return !!pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_A2DP_SOURCE);
e44045
         case PA_BLUETOOTH_PROFILE_HSP_HS:
e44045
-            return !!pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_HSP_HS)
e44045
-                || !!pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_HFP_HF);
e44045
+            return show_hsp && !!pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_HSP_HS);
e44045
+        case PA_BLUETOOTH_PROFILE_HFP_HF:
e44045
+            return show_hfp && !!pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_HFP_HF);
e44045
         case PA_BLUETOOTH_PROFILE_HFP_AG:
e44045
             return !!pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_HSP_AG)
e44045
                 || !!pa_hashmap_get(device->uuids, PA_BLUETOOTH_UUID_HFP_AG);
e44045
@@ -536,6 +550,14 @@ pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_path(pa_bluetooth_disc
e44045
     return NULL;
e44045
 }
e44045
 
e44045
+bool pa_bluetooth_discovery_get_enable_native_hfp_hf(pa_bluetooth_discovery *y)
e44045
+{
e44045
+    pa_assert(y);
e44045
+    pa_assert(PA_REFCNT_VALUE(y) > 0);
e44045
+
e44045
+    return y->enable_native_hfp_hf;
e44045
+}
e44045
+
e44045
 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_discovery *y, const char *remote, const char *local) {
e44045
     pa_bluetooth_device *d;
e44045
     void *state = NULL;
e44045
@@ -1306,6 +1328,8 @@ const char *pa_bluetooth_profile_to_string(pa_bluetooth_profile_t profile) {
e44045
             return "a2dp_source";
e44045
         case PA_BLUETOOTH_PROFILE_HSP_HS:
e44045
             return "headset_head_unit";
e44045
+        case PA_BLUETOOTH_PROFILE_HFP_HF:
e44045
+            return "headset_handsfree";
e44045
         case PA_BLUETOOTH_PROFILE_HFP_AG:
e44045
             return "headset_audio_gateway";
e44045
         case PA_BLUETOOTH_PROFILE_OFF:
e44045
@@ -1727,7 +1751,7 @@ static void endpoint_done(pa_bluetooth_discovery *y, pa_bluetooth_profile_t prof
e44045
     }
e44045
 }
e44045
 
e44045
-pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c, int headset_backend) {
e44045
+pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c, int headset_backend, bool enable_native_hfp_hf) {
e44045
     pa_bluetooth_discovery *y;
e44045
     DBusError err;
e44045
     DBusConnection *conn;
e44045
@@ -1737,6 +1761,7 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c, int headset_backe
e44045
     PA_REFCNT_INIT(y);
e44045
     y->core = c;
e44045
     y->headset_backend = headset_backend;
e44045
+    y->enable_native_hfp_hf = enable_native_hfp_hf;
e44045
     y->adapters = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL,
e44045
                                       (pa_free_cb_t) adapter_free);
e44045
     y->devices = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL,
e44045
diff --git a/src/modules/bluetooth/bluez5-util.h b/src/modules/bluetooth/bluez5-util.h
e44045
index 84c0c3f1..b077ca2c 100644
e44045
--- a/src/modules/bluetooth/bluez5-util.h
e44045
+++ b/src/modules/bluetooth/bluez5-util.h
e44045
@@ -47,6 +47,7 @@ typedef enum profile {
e44045
     PA_BLUETOOTH_PROFILE_A2DP_SINK,
e44045
     PA_BLUETOOTH_PROFILE_A2DP_SOURCE,
e44045
     PA_BLUETOOTH_PROFILE_HSP_HS,
e44045
+    PA_BLUETOOTH_PROFILE_HFP_HF,
e44045
     PA_BLUETOOTH_PROFILE_HFP_AG,
e44045
     PA_BLUETOOTH_PROFILE_OFF
e44045
 } pa_bluetooth_profile_t;
e44045
@@ -161,8 +162,9 @@ const char *pa_bluetooth_profile_to_string(pa_bluetooth_profile_t profile);
e44045
 #define HEADSET_BACKEND_NATIVE 1
e44045
 #define HEADSET_BACKEND_AUTO 2
e44045
 
e44045
-pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *core, int headset_backend);
e44045
+pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *core, int headset_backend, bool default_profile_hfp);
e44045
 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y);
e44045
 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y);
e44045
 void pa_bluetooth_discovery_set_ofono_running(pa_bluetooth_discovery *y, bool is_running);
e44045
+bool pa_bluetooth_discovery_get_enable_native_hfp_hf(pa_bluetooth_discovery *y);
e44045
 #endif
e44045
diff --git a/src/modules/bluetooth/module-bluetooth-policy.c b/src/modules/bluetooth/module-bluetooth-policy.c
e44045
index 316b9a82..b17c5d39 100644
e44045
--- a/src/modules/bluetooth/module-bluetooth-policy.c
e44045
+++ b/src/modules/bluetooth/module-bluetooth-policy.c
e44045
@@ -365,7 +365,8 @@ static pa_hook_result_t profile_available_hook_callback(pa_core *c, pa_card_prof
e44045
     /* Do not automatically switch profiles for headsets, just in case */
e44045
     /* TODO: remove a2dp and hsp when we remove BlueZ 4 support */
e44045
     if (pa_streq(profile->name, "hsp") || pa_streq(profile->name, "a2dp") || pa_streq(profile->name, "a2dp_sink") ||
e44045
-        pa_streq(profile->name, "headset_head_unit"))
e44045
+        pa_streq(profile->name, "headset_head_unit") ||
e44045
+        pa_streq(profile->name, "headset_handsfree"))
e44045
         return PA_HOOK_OK;
e44045
 
e44045
     is_active_profile = card->active_profile == profile;
e44045
diff --git a/src/modules/bluetooth/module-bluez5-device.c b/src/modules/bluetooth/module-bluez5-device.c
e44045
index d076fbad..d37ce9ce 100644
e44045
--- a/src/modules/bluetooth/module-bluez5-device.c
e44045
+++ b/src/modules/bluetooth/module-bluez5-device.c
e44045
@@ -258,6 +258,7 @@ static int sco_process_render(struct userdata *u) {
e44045
 
e44045
     pa_assert(u);
e44045
     pa_assert(u->profile == PA_BLUETOOTH_PROFILE_HSP_HS ||
e44045
+                u->profile == PA_BLUETOOTH_PROFILE_HFP_HF ||
e44045
                 u->profile == PA_BLUETOOTH_PROFILE_HFP_AG);
e44045
     pa_assert(u->sink);
e44045
 
e44045
@@ -318,6 +319,7 @@ static int sco_process_push(struct userdata *u) {
e44045
 
e44045
     pa_assert(u);
e44045
     pa_assert(u->profile == PA_BLUETOOTH_PROFILE_HSP_HS ||
e44045
+                u->profile == PA_BLUETOOTH_PROFILE_HFP_HF||
e44045
                 u->profile == PA_BLUETOOTH_PROFILE_HFP_AG);
e44045
     pa_assert(u->source);
e44045
     pa_assert(u->read_smoother);
e44045
@@ -784,7 +786,9 @@ static void transport_release(struct userdata *u) {
e44045
 
e44045
 /* Run from I/O thread */
e44045
 static void transport_config_mtu(struct userdata *u) {
e44045
-    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS || u->profile == PA_BLUETOOTH_PROFILE_HFP_AG) {
e44045
+    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_HF
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_AG) {
e44045
         u->read_block_size = u->read_link_mtu;
e44045
         u->write_block_size = u->write_link_mtu;
e44045
 
e44045
@@ -1004,7 +1008,8 @@ static int add_source(struct userdata *u) {
e44045
     data.namereg_fail = false;
e44045
     pa_proplist_sets(data.proplist, "bluetooth.protocol", pa_bluetooth_profile_to_string(u->profile));
e44045
     pa_source_new_data_set_sample_spec(&data, &u->sample_spec);
e44045
-    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS)
e44045
+    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_HF)
e44045
         pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
e44045
 
e44045
     connect_ports(u, &data, PA_DIRECTION_INPUT);
e44045
@@ -1016,6 +1021,7 @@ static int add_source(struct userdata *u) {
e44045
                 data.suspend_cause = PA_SUSPEND_USER;
e44045
                 break;
e44045
             case PA_BLUETOOTH_PROFILE_HSP_HS:
e44045
+            case PA_BLUETOOTH_PROFILE_HFP_HF:
e44045
                 /* u->stream_fd contains the error returned by the last transport_acquire()
e44045
                  * EAGAIN means we are waiting for a NewConnection signal */
e44045
                 if (u->stream_fd == -EAGAIN)
e44045
@@ -1039,7 +1045,9 @@ static int add_source(struct userdata *u) {
e44045
     u->source->userdata = u;
e44045
     u->source->parent.process_msg = source_process_msg;
e44045
 
e44045
-    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS || u->profile == PA_BLUETOOTH_PROFILE_HFP_AG) {
e44045
+    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_AG
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_HF) {
e44045
         pa_source_set_set_volume_callback(u->source, source_set_volume_cb);
e44045
         u->source->n_volume_steps = 16;
e44045
     }
e44045
@@ -1174,7 +1182,8 @@ static int add_sink(struct userdata *u) {
e44045
     data.namereg_fail = false;
e44045
     pa_proplist_sets(data.proplist, "bluetooth.protocol", pa_bluetooth_profile_to_string(u->profile));
e44045
     pa_sink_new_data_set_sample_spec(&data, &u->sample_spec);
e44045
-    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS)
e44045
+    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_HF)
e44045
         pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
e44045
 
e44045
     connect_ports(u, &data, PA_DIRECTION_OUTPUT);
e44045
@@ -1185,6 +1194,7 @@ static int add_sink(struct userdata *u) {
e44045
                 data.suspend_cause = PA_SUSPEND_USER;
e44045
                 break;
e44045
             case PA_BLUETOOTH_PROFILE_HSP_HS:
e44045
+            case PA_BLUETOOTH_PROFILE_HFP_HF:
e44045
                 /* u->stream_fd contains the error returned by the last transport_acquire()
e44045
                  * EAGAIN means we are waiting for a NewConnection signal */
e44045
                 if (u->stream_fd == -EAGAIN)
e44045
@@ -1210,7 +1220,9 @@ static int add_sink(struct userdata *u) {
e44045
     u->sink->userdata = u;
e44045
     u->sink->parent.process_msg = sink_process_msg;
e44045
 
e44045
-    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS || u->profile == PA_BLUETOOTH_PROFILE_HFP_AG) {
e44045
+    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_AG
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_HF) {
e44045
         pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
e44045
         u->sink->n_volume_steps = 16;
e44045
     }
e44045
@@ -1219,7 +1231,9 @@ static int add_sink(struct userdata *u) {
e44045
 
e44045
 /* Run from main thread */
e44045
 static void transport_config(struct userdata *u) {
e44045
-    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS || u->profile == PA_BLUETOOTH_PROFILE_HFP_AG) {
e44045
+    if (u->profile == PA_BLUETOOTH_PROFILE_HSP_HS
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_HF
e44045
+        || u->profile == PA_BLUETOOTH_PROFILE_HFP_AG) {
e44045
         u->sample_spec.format = PA_SAMPLE_S16LE;
e44045
         u->sample_spec.channels = 1;
e44045
         u->sample_spec.rate = 8000;
e44045
@@ -1370,6 +1384,7 @@ static pa_direction_t get_profile_direction(pa_bluetooth_profile_t p) {
e44045
         [PA_BLUETOOTH_PROFILE_A2DP_SINK] = PA_DIRECTION_OUTPUT,
e44045
         [PA_BLUETOOTH_PROFILE_A2DP_SOURCE] = PA_DIRECTION_INPUT,
e44045
         [PA_BLUETOOTH_PROFILE_HSP_HS] = PA_DIRECTION_INPUT | PA_DIRECTION_OUTPUT,
e44045
+        [PA_BLUETOOTH_PROFILE_HFP_HF] = PA_DIRECTION_INPUT | PA_DIRECTION_OUTPUT,
e44045
         [PA_BLUETOOTH_PROFILE_HFP_AG] = PA_DIRECTION_INPUT | PA_DIRECTION_OUTPUT,
e44045
         [PA_BLUETOOTH_PROFILE_OFF] = 0
e44045
     };
e44045
@@ -1874,7 +1889,20 @@ static pa_card_profile *create_card_profile(struct userdata *u, pa_bluetooth_pro
e44045
         break;
e44045
 
e44045
     case PA_BLUETOOTH_PROFILE_HSP_HS:
e44045
-        cp = pa_card_profile_new(name, _("Headset Head Unit (HSP/HFP)"), sizeof(pa_bluetooth_profile_t));
e44045
+        cp = pa_card_profile_new(name, _("Headset Head Unit (HSP)"), sizeof(pa_bluetooth_profile_t));
e44045
+        cp->priority = 20;
e44045
+        cp->n_sinks = 1;
e44045
+        cp->n_sources = 1;
e44045
+        cp->max_sink_channels = 1;
e44045
+        cp->max_source_channels = 1;
e44045
+        pa_hashmap_put(input_port->profiles, cp->name, cp);
e44045
+        pa_hashmap_put(output_port->profiles, cp->name, cp);
e44045
+
e44045
+        p = PA_CARD_PROFILE_DATA(cp);
e44045
+        break;
e44045
+
e44045
+    case PA_BLUETOOTH_PROFILE_HFP_HF:
e44045
+         cp = pa_card_profile_new(name, _("Headset Handsfree (HFP)"), sizeof(pa_bluetooth_profile_t));
e44045
         cp->priority = 20;
e44045
         cp->n_sinks = 1;
e44045
         cp->n_sources = 1;
e44045
@@ -1960,8 +1988,10 @@ static int uuid_to_profile(const char *uuid, pa_bluetooth_profile_t *_r) {
e44045
         *_r = PA_BLUETOOTH_PROFILE_A2DP_SINK;
e44045
     else if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SOURCE))
e44045
         *_r = PA_BLUETOOTH_PROFILE_A2DP_SOURCE;
e44045
-    else if (pa_streq(uuid, PA_BLUETOOTH_UUID_HSP_HS) || pa_streq(uuid, PA_BLUETOOTH_UUID_HFP_HF))
e44045
+    else if (pa_streq(uuid, PA_BLUETOOTH_UUID_HSP_HS))
e44045
         *_r = PA_BLUETOOTH_PROFILE_HSP_HS;
e44045
+    else if (pa_streq(uuid, PA_BLUETOOTH_UUID_HFP_HF))
e44045
+        *_r = PA_BLUETOOTH_PROFILE_HFP_HF;
e44045
     else if (pa_streq(uuid, PA_BLUETOOTH_UUID_HSP_AG) || pa_streq(uuid, PA_BLUETOOTH_UUID_HFP_AG))
e44045
         *_r = PA_BLUETOOTH_PROFILE_HFP_AG;
e44045
     else
e44045
@@ -1980,6 +2010,7 @@ static int add_card(struct userdata *u) {
e44045
     pa_bluetooth_profile_t *p;
e44045
     const char *uuid;
e44045
     void *state;
e44045
+    bool enable_native_hfp_hf, has_both;
e44045
 
e44045
     pa_assert(u);
e44045
     pa_assert(u->device);
e44045
@@ -2010,9 +2041,22 @@ static int add_card(struct userdata *u) {
e44045
 
e44045
     create_card_ports(u, data.ports);
e44045
 
e44045
+    enable_native_hfp_hf = pa_bluetooth_discovery_get_enable_native_hfp_hf(u->discovery);
e44045
+
e44045
+    has_both = enable_native_hfp_hf && pa_hashmap_get(d->uuids, PA_BLUETOOTH_UUID_HFP_HF) && pa_hashmap_get(d->uuids, PA_BLUETOOTH_UUID_HSP_HS);
e44045
     PA_HASHMAP_FOREACH(uuid, d->uuids, state) {
e44045
         pa_bluetooth_profile_t profile;
e44045
 
e44045
+        if (!enable_native_hfp_hf && pa_streq(uuid, PA_BLUETOOTH_UUID_HFP_HF)) {
e44045
+            pa_log_info("device supports HFP but disabling profile as requested");
e44045
+            continue;
e44045
+        }
e44045
+
e44045
+        if (has_both && pa_streq(uuid, PA_BLUETOOTH_UUID_HSP_HS)) {
e44045
+            pa_log_info("device support HSP and HFP, selecting HFP only");
e44045
+            continue;
e44045
+        }
e44045
+
e44045
         if (uuid_to_profile(uuid, &profile) < 0)
e44045
             continue;
e44045
 
e44045
diff --git a/src/modules/bluetooth/module-bluez5-discover.c b/src/modules/bluetooth/module-bluez5-discover.c
e44045
index c535ead4..bfb361ae 100644
e44045
--- a/src/modules/bluetooth/module-bluez5-discover.c
e44045
+++ b/src/modules/bluetooth/module-bluez5-discover.c
e44045
@@ -104,6 +104,7 @@ int pa__init(pa_module *m) {
e44045
     const char *headset_str;
e44045
     int headset_backend;
e44045
     bool autodetect_mtu;
e44045
+    bool enable_native_hfp_hf = true;
e44045
 
e44045
     pa_assert(m);
e44045
 
e44045
@@ -127,6 +128,9 @@ int pa__init(pa_module *m) {
e44045
     autodetect_mtu = false;
e44045
     if (pa_modargs_get_value_boolean(ma, "autodetect_mtu", &autodetect_mtu) < 0) {
e44045
         pa_log("Invalid boolean value for autodetect_mtu parameter");
e44045
+    }
e44045
+    if (pa_modargs_get_value_boolean(ma, "enable_native_hfp_hf", &enable_native_hfp_hf) < 0) {
e44045
+        pa_log("enable_native_hfp_hf must be true or false");
e44045
         goto fail;
e44045
     }
e44045
 
e44045
@@ -136,7 +140,7 @@ int pa__init(pa_module *m) {
e44045
     u->autodetect_mtu = autodetect_mtu;
e44045
     u->loaded_device_paths = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
e44045
 
e44045
-    if (!(u->discovery = pa_bluetooth_discovery_get(u->core, headset_backend)))
e44045
+    if (!(u->discovery = pa_bluetooth_discovery_get(u->core, headset_backend, enable_native_hfp_hf)))
e44045
         goto fail;
e44045
 
e44045
     u->device_connection_changed_slot =