[Nexus 5 Developer Discussion [Developers Only]] z3DD3r:
I know another device - LG G2. Own bt driver can work, but it looks very complicated to me...

I don't see any race conditions here. Just a simple mistake. Only one variable is used instead of queue. Maybe this interface wasn't designed to handle such cases...

Fully agree with you. Maintaining all those changes will be painful...

Who knows. I'm using it cos BT_VND_OP_SET_AUDIO_STATE command actually call hw_set_SCO_codec and not the hw_sco_i2spcm_config. hw_set_SCO_codec sends one additional command to enable/disable WBS. It may be not necessary for Nexus 5, but it can be necessary for other devices. In any case i think we should make this behavior configurable.

This is very strange. Look at my logcat (LOS wit BT fix):

06-18 09:06:15.850 I/bt_hwcfg(7282): hw_config_continue_sco_setup: set SCO related hardware settings
06-18 09:06:15.850 I/bt_hwcfg(7282): hw_config_continue_sco_setup: set SCO codec: 0x1
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): buffer_alloc_cb pts: 0x9f483e60, size: 14
[B]06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): transmit_cb opcode: 0xfc7e, ptr: 0x9f483e60, cb: 0x8b2cfcf9 - First opcode from hw_set_SCO_codec[/B]
06-18 09:06:15.850 I/bt_hwcfg(7282): hw_config_continue_sco_setup: set SCO I2S/PCM codec: 0x1
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): buffer_alloc_cb pts: 0x9f483e60, size: 15
06-18 09:06:15.850 I/bt_hwcfg(7282): hw_config_continue_sco_setup: I2SPCM config {0x0, 0x0, 0x0, 0x4}
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): transmit_cb opcode: 0xfc6d, ptr: 0x9f483e60, cb: 0x8b2cfcf9
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): buffer_alloc_cb pts: 0x9f483e60, size: 16
06-18 09:06:15.850 I/bt_hwcfg(7282): hw_config_continue_sco_setup: SCO PCM configure {0x0, 0x4, 0x0, 0x0, 0x0}
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): transmit_cb opcode: 0xfc1c, ptr: 0x9f483e60, cb: 0x8b2cfcf9
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): buffer_alloc_cb pts: 0x9f483e60, size: 16
06-18 09:06:15.850 I/bt_hwcfg(7282): hw_config_continue_sco_setup: SCO PCM data format {0x0, 0x0, 0x3, 0x0, 0x0}
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): transmit_cb opcode: 0xfc1e, ptr: 0x9f483e60, cb: 0x8b2cfcf9
06-18 09:06:15.850 I/bt_hwcfg(7282): hw_config_continue_sco_setup: finish
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): firmware_config_cb result: 0
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): OnFirmwareConfigured result: 0
06-18 09:06:15.850 I/android.hardware.bluetooth@1.0-impl(7282): Firmware configured in 0.635s
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): OnFirmwareConfigured: lpm_timeout_ms 1500
06-18 09:06:15.850 I/bt_hci (7282): event_finish_startup
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): buffer_alloc_cb pts: 0x9f495958, size: 23
06-18 09:06:15.850 I/bt_core_module(7282): module_start_up Started module "hci_module"
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): transmit_cb opcode: 0xfc27, ptr: 0x9f495958, cb: 0x8b2cf4f9
06-18 09:06:15.850 D/android.hardware.bluetooth@1.0-impl(7282): OnFirmwareConfigured Calling StartLowPowerWatchdog()
06-18 09:06:15.851 D/android.hardware.bluetooth@1.0-impl(7282): sco_config_cb result: 0
06-18 09:06:15.851 D/android.hardware.bluetooth@1.0-impl(7282): OnScoConfigured result: 0
06-18 09:06:15.851 D/android.hardware.bluetooth@1.0-impl(7282): buffer_free_cb ptr: 0x9f483e50
[b]06-18 09:06:15.851 D/android.hardware.bluetooth@1.0-impl(7282): internal_command_event_match internal_command.opcode = FC27 opcode = fc7e - First event from bt controller[/b]
06-18 09:06:15.851 I/bt_osi_thread(7282): run_thread: thread id 7330, thread name bt_workqueue started
06-18 09:06:15.851 D/android.hardware.bluetooth@1.0-impl(7282): event_cb_ called
06-18 09:06:15.851 W/bt_hci (7282): filter_incoming_event command complete event with no matching command (opcode: 0xfc7e).
06-18 09:06:15.851 I/ (7282): [0618/090615.851280:INFO:btu_task.cc(107)] Bluetooth chip preload is complete
06-18 09:06:15.851 I/ (7282): [0618/090615.851843:INFO:gatt_api.cc(948)] GATT_Register 81818181-8181-8181-8181-818181818181
06-18 09:06:15.851 I/ (7282): [0618/090615.851972:INFO:gatt_api.cc(968)] allocated gatt_if=1
06-18 09:06:15.852 I/ (7282): [0618/090615.852038:INFO:gatt_api.cc(161)] GATTS_AddService
06-18 09:06:15.852 I/ (7282): [0618/090615.852094:INFO:gatt_api.cc(265)] GATTS_AddService: service parsed correctly, now starting
06-18 09:06:15.852 I/ (7282): [0618/090615.852276:INFO:gatt_api.cc(948)] GATT_Register 82828282-8282-8282-8282-828282828282
06-18 09:06:15.852 I/ (7282): [0618/090615.852333:INFO:gatt_api.cc(968)] allocated gatt_if=2
06-18 09:06:15.852 I/ (7282): [0618/090615.852402:INFO:gatt_api.cc(161)] GATTS_AddService
06-18 09:06:15.852 I/ (7282): [0618/090615.852455:INFO:gatt_api.cc(265)] GATTS_AddService: service parsed correctly, now starting
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_HCI : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_L2CAP : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_RFCOMM : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_AVDT : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_AVRC : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_A2D : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_BNEP : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_BTM : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_HID_HOST : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_PAN : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_SDP : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_SMP : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_HID_DEV : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_BTAPP : Level 2
06-18 09:06:15.852 I/bt_bte (7282): BTE_InitTraceLevels -- TRC_BTIF : Level 2
06-18 09:06:15.853 I/bt_osi_thread(7282): run_thread: thread id 7331, thread name btu message loop started
[B]06-18 09:06:15.853 D/android.hardware.bluetooth@1.0-impl(7282): internal_command_event_match internal_command.opcode = FC27 opcode = fc6d - Second one[/B]
06-18 09:06:15.853 D/android.hardware.bluetooth@1.0-impl(7282): event_cb_ called
06-18 09:06:15.853 W/bt_hci (7282): filter_incoming_event command complete event with no matching command (opcode: 0xfc6d).
06-18 09:06:15.853 D/android.hardware.bluetooth@1.0-impl(7282): internal_command_event_match internal_command.opcode = FC27 opcode = fc1c
06-18 09:06:15.853 D/android.hardware.bluetooth@1.0-impl(7282): event_cb_ called
06-18 09:06:15.853 W/bt_hci (7282): filter_incoming_event command complete event with no matching command (opcode: 0xfc1c).
06-18 09:06:15.853 D/android.hardware.bluetooth@1.0-impl(7282): internal_command_event_match internal_command.opcode = FC27 opcode = fc1e
06-18 09:06:15.853 D/android.hardware.bluetooth@1.0-impl(7282): event_cb_ called
06-18 09:06:15.853 W/bt_hci (7282): filter_incoming_event command complete event with no matching command (opcode: 0xfc1e).
06-18 09:06:15.853 D/android.hardware.bluetooth@1.0-impl(7282): internal_command_event_match internal_command.opcode = FC27 opcode = fc27
06-18 09:06:15.853 I/bt_osi_thread(7282): run_thread: thread id 7332, thread name module_wrapper started
06-18 09:06:15.853 D/android.hardware.bluetooth@1.0-impl(7282): internal_command.bc called

internal_command.opcode doesn't match to the opcode received from bt controller. All events related to sco configuration will be handled by HCI. But HCI doesn't know anything about these commands and events (filter_incoming_event command complete event with no matching command in the log). Actually FC27 is HCI_VSC_WRITE_SLEEP_MODE. Seems that someone else sends this opcode to controller. I think that this can happens only after firmware configuration. We can try to postpone call of the initialize_complete_cb_ until sco configuration finish.