Bluetooth: New callback type for HCI requests
Subsystems (2)
v4 cover Bluetooth: New callback type for HCI requests
Hi,
v4: rebased (and tested) against latest bluetooth-next.git
v3: Make skb callback consistent with hci_cmd_sync_ev() semantics
v2: rebased (and tested) against latest bluetooth-next.git
Original cover letter:
This patch set adds a new callback type for HCI requests which allows passing the resulting command completion skb to the callback for further parsing. The set also converts the first two users for this new API, namely the synchronous requests as well as reading local OOB data.
The tricky part with passing the skb was the fact that typically HCI request callbacks are done after all other hci_event.c processing, while the skb we want to pass to the callback should be in pristine form (i.e. no skb_pull() or other modifications that hci_event.c handlers may do). To solve this the patches create a prestine copy of the skb with skb_clone() in the beginning of hci_event_packet() and move the callback access to a single place at the end of this function.
Johan
----------------------------------------------------------------
Johan Hedberg (7):
Bluetooth: Add clarifying comment to command status handling
Bluetooth: Add second hci_request callback option for full skb
Bluetooth: Convert hci_req_sync family of function to new request API
Bluetooth: Remove unneeded recv_event variable
Bluetooth: Remove unused hci_req_pending() function
Bluetooth: Move hci_get_cmd_complete() to hci_event.c
Bluetooth: Convert local OOB data reading to use HCI request
include/net/bluetooth/bluetooth.h | 3 + include/net/bluetooth/hci_core.h | 7 +- net/bluetooth/hci_core.c | 114 +++++++--------------------- net/bluetooth/hci_event.c | 151 ++++++++++++++++++++++++++----------- net/bluetooth/hci_request.c | 14 +++- net/bluetooth/hci_request.h | 5 +- net/bluetooth/mgmt.c | 105 ++++++++++++++++---------- 7 files changed, 220 insertions(+), 179 deletions(-)
PATCH v4 1/7 Bluetooth: Add clarifying comment to command status handling
From: Johan Hedberg <[email protected]>
When dealing with HCI command status events, the reasoning for trying to mark a request as complete if no specific event is being waited for and status was success is not self-evident. This patch adds a clarifying comment above the if-statement.
--- net/bluetooth/hci_event.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/bluetooth/hci_event.c +6 -0
@@ -3104,6 +3104,12 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)if (opcode !=HCI_OP_NOP )cancel_delayed_work (&hdev->cmd_timer); +/* Indicate request completion if the command failed. Also, if +* we're not waiting for a special event and we get a success +* command status we should try to flag the request as completed +* (since for this kind of commands there will not be a command +* complete event). +*/ if (ev->status || (hdev->sent_cmd && !bt_cb (hdev->sent_cmd)->req_event))hci_req_cmd_complete (hdev, opcode, ev->status);
Signature · 1 line
2.1.0
PATCH v4 2/7 Bluetooth: Add second hci_request callback option for full skb
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
This patch adds a second possible callback for HCI requests where the callback will receive the full skb of the last successfully completed HCI command. This API is useful for cases where we want to use a request to read some data and the existing hci_event.c handlers do not store it e.g. in the hci_dev struct.
The reason the patch is a bit bigger than just adding the new API is because the hci_req_cmd_complete() functions required some refactoring to enable it: now hci_req_cmd_complete() is simply used to request the callback pointers if any, and the actual calling of them happens from a single place at the end of hci_event_packet(). The reason for this is that we need to pass the original skb (without any skb_pull, etc modifications done to it) and it's simplest to keep track of it within the hci_event_packet() function.
--- include/net/bluetooth/bluetooth.h | 3 ++ net/bluetooth/hci_core.c | 30 +++++++--------- net/bluetooth/hci_event.c | 76 ++++++++++++++++++++++++++------------- net/bluetooth/hci_request.c | 14 +++++++- net/bluetooth/hci_request.h | 5 ++- 5 files changed, 84 insertions(+), 44 deletions(-)
include/net/bluetooth/bluetooth.h +3 -0
@@ -274,6 +274,8 @@ struct l2cap_ctrl {struct hci_dev ;typedef void (*hci_req_complete_t )(struct hci_dev *hdev,u8 status,u16 opcode); +typedef void (*hci_req_complete_skb_t )(struct hci_dev *hdev,u8 status, +u16 opcode,struct sk_buff *skb);struct bt_skb_cb {__u8 pkt_type;
@@ -284,6 +286,7 @@ struct bt_skb_cb { __u8 req_start:1;u8 req_event;hci_req_complete_t req_complete; +hci_req_complete_skb_t req_complete_skb;struct l2cap_chan *chan;struct l2cap_ctrl control;bdaddr_t bdaddr;
net/bluetooth/hci_core.c +12 -18
@@ -4272,9 +4272,10 @@ static void hci_resend_last(struct hci_dev *hdev)queue_work (hdev->workqueue, &hdev->cmd_work); } -void hci_req_cmd_complete (struct hci_dev *hdev,u16 opcode,u8 status) +void hci_req_cmd_complete (struct hci_dev *hdev,u16 opcode,u8 status, +hci_req_complete_t *req_complete, +hci_req_complete_skb_t *req_complete_skb) { -hci_req_complete_t req_complete =NULL ;struct sk_buff *skb;unsigned long flags;
@@ -4306,18 +4307,14 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)* callback would be found in hdev->sent_cmd instead of the * command queue (hdev->cmd_q). */ -if (hdev->sent_cmd) { - req_complete =bt_cb (hdev->sent_cmd)->req_complete; - -if (req_complete) { -/* We must set the complete callback to NULL to -* avoid calling the callback more than once if -* this function gets called again. -*/ -bt_cb (hdev->sent_cmd)->req_complete =NULL ; +if (bt_cb (hdev->sent_cmd)->req_complete) { + *req_complete =bt_cb (hdev->sent_cmd)->req_complete; +return ; + } - goto call_complete; - } +if (bt_cb (hdev->sent_cmd)->req_complete_skb) { + *req_complete_skb =bt_cb (hdev->sent_cmd)->req_complete_skb; +return ; }/* Remove all pending commands belonging to this request */
@@ -4328,14 +4325,11 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)break ; } - req_complete =bt_cb (skb)->req_complete; + *req_complete =bt_cb (skb)->req_complete; + *req_complete_skb =bt_cb (skb)->req_complete_skb;kfree_skb (skb); }spin_unlock_irqrestore (&hdev->cmd_q.lock, flags); - -call_complete: -if (req_complete) -req_complete (hdev, status, status ? opcode :HCI_OP_NOP ); }static void hci_rx_work (struct work_struct *work)
net/bluetooth/hci_event.c +52 -24
156 lines not on this page · raw message
net/bluetooth/hci_request.c +13 -1
@@ -34,7 +34,8 @@ void hci_req_init(struct hci_request *req, struct hci_dev *hdev) req->err =0 ; } -int hci_req_run (struct hci_request *req,hci_req_complete_t complete) +static int req_run (struct hci_request *req,hci_req_complete_t complete, +hci_req_complete_skb_t complete_skb) {struct hci_dev *hdev = req->hdev;struct sk_buff *skb;
@@ -56,6 +57,7 @@ int hci_req_run(struct hci_request *req, hci_req_complete_t complete) skb =skb_peek_tail (&req->cmd_q);bt_cb (skb)->req_complete = complete; +bt_cb (skb)->req_complete_skb = complete_skb;spin_lock_irqsave (&hdev->cmd_q.lock, flags);skb_queue_splice_tail (&req->cmd_q, &hdev->cmd_q);
@@ -66,6 +68,16 @@ int hci_req_run(struct hci_request *req, hci_req_complete_t complete)return 0 ; } +int hci_req_run (struct hci_request *req,hci_req_complete_t complete) +{ +return req_run (req, complete,NULL ); +} + +int hci_req_run_skb (struct hci_request *req,hci_req_complete_skb_t complete) +{ +return req_run (req,NULL , complete); +} +struct sk_buff *hci_prepare_cmd (struct hci_dev *hdev,u16 opcode,u32 plen,const void *param) {
net/bluetooth/hci_request.h +4 -1
@@ -32,11 +32,14 @@ struct hci_request {void hci_req_init (struct hci_request *req,struct hci_dev *hdev);int hci_req_run (struct hci_request *req,hci_req_complete_t complete); +int hci_req_run_skb (struct hci_request *req,hci_req_complete_skb_t complete);void hci_req_add (struct hci_request *req,u16 opcode,u32 plen,const void *param);void hci_req_add_ev (struct hci_request *req,u16 opcode,u32 plen,const void *param,u8 event); -void hci_req_cmd_complete (struct hci_dev *hdev,u16 opcode,u8 status); +void hci_req_cmd_complete (struct hci_dev *hdev,u16 opcode,u8 status, +hci_req_complete_t *req_complete, +hci_req_complete_skb_t *req_complete_skb);struct sk_buff *hci_prepare_cmd (struct hci_dev *hdev,u16 opcode,u32 plen,const void *param);
Signature · 1 line
2.1.0
PATCH v4 3/7 Bluetooth: Convert hci_req_sync family of functions to new request API
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Now that there's an API in place that allows passing the resulting skb to the request callback we can conveniently convert the hci_req_sync and related functions to use it. Since we still need to get the skb from the async callback into the sleeping _sync() function the patch adds another req_skb variable to hci_dev where the sync request state is tracked.
--- include/net/bluetooth/hci_core.h | 1 + net/bluetooth/hci_core.c | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
@@ -323,6 +323,7 @@ struct hci_dev {wait_queue_head_t req_wait_q;__u32 req_status;__u32 req_result; +struct sk_buff *req_skb;void *smp_data;void *smp_bredr_data;
net/bluetooth/hci_core.c +14 -14
@@ -141,13 +141,16 @@ static const struct file_operations dut_mode_fops = {/* ---- HCI requests ---- */ -static void hci_req_sync_complete (struct hci_dev *hdev,u8 result,u16 opcode) +static void hci_req_sync_complete (struct hci_dev *hdev,u8 result,u16 opcode, +struct sk_buff *skb) {BT_DBG ("%s result 0x%2.2x" , hdev->name, result);if (hdev->req_status ==HCI_REQ_PEND ) { hdev->req_result = result; hdev->req_status =HCI_REQ_DONE ; +if (skb) + hdev->req_skb =skb_get (skb);wake_up_interruptible (&hdev->req_wait_q); } }
@@ -164,18 +167,10 @@ static void hci_req_cancel(struct hci_dev *hdev, int err) }static struct sk_buff *hci_get_cmd_complete (struct hci_dev *hdev,u16 opcode, -u8 event) +u8 event,struct sk_buff *skb) {struct hci_ev_cmd_complete *ev;struct hci_event_hdr *hdr; -struct sk_buff *skb; - -hci_dev_lock (hdev); - - skb = hdev->recv_evt; - hdev->recv_evt =NULL ; - -hci_dev_unlock (hdev);if (!skb)return ERR_PTR (-ENODATA );
@@ -223,6 +218,7 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, {DECLARE_WAITQUEUE (wait, current);struct hci_request req; +struct sk_buff *skb;int err =0 ;BT_DBG ("%s" , hdev->name);
@@ -236,7 +232,7 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,add_wait_queue (&hdev->req_wait_q, &wait);set_current_state (TASK_INTERRUPTIBLE ); - err =hci_req_run (&req, hci_req_sync_complete); + err =hci_req_run_skb (&req, hci_req_sync_complete);if (err <0 ) {remove_wait_queue (&hdev->req_wait_q, &wait);set_current_state (TASK_RUNNING );
@@ -265,13 +261,17 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, } hdev->req_status = hdev->req_result =0 ; + skb = hdev->req_skb; + hdev->req_skb =NULL ;BT_DBG ("%s end: err %d" , hdev->name, err); -if (err <0 ) +if (err <0 ) { +kfree_skb (skb);return ERR_PTR (err); + } -return hci_get_cmd_complete (hdev, opcode, event); +return hci_get_cmd_complete (hdev, opcode, event, skb); }EXPORT_SYMBOL (__hci_cmd_sync_ev);
@@ -303,7 +303,7 @@ static int __hci_req_sync(struct hci_dev *hdev,add_wait_queue (&hdev->req_wait_q, &wait);set_current_state (TASK_INTERRUPTIBLE ); - err =hci_req_run (&req, hci_req_sync_complete); + err =hci_req_run_skb (&req, hci_req_sync_complete);if (err <0 ) { hdev->req_status =0 ;
Signature · 1 line
2.1.0
PATCH v4 4/7 Bluetooth: Remove unneeded recv_event variable
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Now that the synchronous HCI requests use the new API and a new private variable the recv_evt member of hci_dev is no-longer needed. This patch removes it.
--- include/net/bluetooth/hci_core.h | 1 - net/bluetooth/hci_core.c | 3 --- net/bluetooth/hci_event.c | 12 ------------ 3 files changed, 16 deletions(-)
include/net/bluetooth/hci_core.h +0 -1
@@ -315,7 +315,6 @@ struct hci_dev {struct sk_buff_head raw_q;struct sk_buff_head cmd_q; -struct sk_buff *recv_evt;struct sk_buff *sent_cmd;struct sk_buff *reassembly[NUM_REASSEMBLY ];
net/bluetooth/hci_core.c +0 -3
@@ -1690,9 +1690,6 @@ static int hci_dev_do_close(struct hci_dev *hdev) hdev->sent_cmd =NULL ; } -kfree_skb (hdev->recv_evt); - hdev->recv_evt =NULL ; -/* After this point our queues are empty * and no tasks are scheduled. */ hdev->close (hdev);
net/bluetooth/hci_event.c +0 -12
@@ -5041,18 +5041,6 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)u8 status =0 , event = hdr->evt;u16 opcode =HCI_OP_NOP ; -hci_dev_lock (hdev); - -/* Received events are (currently) only needed when a request is -* ongoing so avoid unnecessary memory allocation. -*/ -if (hci_req_pending (hdev)) { -kfree_skb (hdev->recv_evt); - hdev->recv_evt =skb_clone (skb,GFP_KERNEL ); - } - -hci_dev_unlock (hdev); -if (hdev->sent_cmd &&bt_cb (hdev->sent_cmd)->req_event == event) {struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data; opcode =__le16_to_cpu (cmd_hdr->opcode);
Signature · 1 line
2.1.0
PATCH v4 5/7 Bluetooth: Remove unused hci_req_pending() function
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The hci_req_pending() function has no users anymore, so simply remove it.
--- include/net/bluetooth/hci_core.h | 2 -- net/bluetooth/hci_core.c | 5 ----- 2 files changed, 7 deletions(-)
include/net/bluetooth/hci_core.h +0 -2
@@ -1267,8 +1267,6 @@ static inline int hci_check_conn_params(u16 min, u16 max, u16 latency,int hci_register_cb (struct hci_cb *hcb);int hci_unregister_cb (struct hci_cb *hcb); -bool hci_req_pending (struct hci_dev *hdev); -struct sk_buff *__hci_cmd_sync (struct hci_dev *hdev,u16 opcode,u32 plen,const void *param,u32 timeout); struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
net/bluetooth/hci_core.c +0 -5
@@ -3544,11 +3544,6 @@ static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb) } } -bool hci_req_pending (struct hci_dev *hdev) -{ -return (hdev->req_status ==HCI_REQ_PEND ); -} -/* Send HCI command */ int hci_send_cmd (struct hci_dev *hdev,__u16 opcode,__u32 plen,const void *param)
Signature · 1 line
2.1.0
PATCH v4 6/7 Bluetooth: Move hci_get_cmd_complete() to hci_event.c
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
To make the hci_req_run_skb() API consistent with hci_cmd_sync_ev() the callback should receive the cmd_complete parameters in the 'normal' case and the full HCI event if a special event was expected. This patch moves the hci_get_cmd_complete() function from hci_core.c to hci_event.c where it's used to strip the skb from the needed headers before passing it on to the callback.
--- net/bluetooth/hci_core.c | 52 ++++-------------------------------------- net/bluetooth/hci_event.c | 57 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 51 deletions(-)
net/bluetooth/hci_core.c +4 -48
@@ -166,53 +166,6 @@ static void hci_req_cancel(struct hci_dev *hdev, int err) } } -static struct sk_buff *hci_get_cmd_complete (struct hci_dev *hdev,u16 opcode, -u8 event,struct sk_buff *skb) -{ -struct hci_ev_cmd_complete *ev; -struct hci_event_hdr *hdr; - -if (!skb) -return ERR_PTR (-ENODATA ); - -if (skb->len <sizeof (*hdr)) { -BT_ERR ("Too short HCI event" ); - goto failed; - } - - hdr = (void *) skb->data; -skb_pull (skb,HCI_EVENT_HDR_SIZE ); - -if (event) { -if (hdr->evt != event) - goto failed; -return skb; - } - -if (hdr->evt !=HCI_EV_CMD_COMPLETE ) { -BT_DBG ("Last event is not cmd complete (0x%2.2x)" , hdr->evt); - goto failed; - } - -if (skb->len <sizeof (*ev)) { -BT_ERR ("Too short cmd_complete event" ); - goto failed; - } - - ev = (void *) skb->data; -skb_pull (skb,sizeof (*ev)); - -if (opcode ==__le16_to_cpu (ev->opcode)) -return skb; - -BT_DBG ("opcode doesn't match (0x%2.2x != 0x%2.2x)" , opcode, -__le16_to_cpu (ev->opcode)); - -failed: -kfree_skb (skb); -return ERR_PTR (-ENODATA ); -} -struct sk_buff *__hci_cmd_sync_ev (struct hci_dev *hdev,u16 opcode,u32 plen,const void *param,u8 event,u32 timeout) {
@@ -271,7 +224,10 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,return ERR_PTR (err); } -return hci_get_cmd_complete (hdev, opcode, event, skb); +if (!skb) +return ERR_PTR (-ENODATA ); + +return skb; }EXPORT_SYMBOL (__hci_cmd_sync_ev);
net/bluetooth/hci_event.c +54 -3
83 lines not on this page · raw message
Signature · 1 line
2.1.0
PATCH v4 7/7 Bluetooth: Convert local OOB data reading to use HCI request
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Now that there's a HCI request API available where the callback receives the resulting skb, we can convert the local OOB data reading to use this new API. This patch does the necessary update in mgmt.c (which also requires moving the callback higher up since it's now a static function) and removes the custom calls from hci_event.c that are no-longer necessary.
--- include/net/bluetooth/hci_core.h | 3 -- net/bluetooth/hci_event.c | 12 ----- net/bluetooth/mgmt.c | 105 ++++++++++++++++++++++++--------------- 3 files changed, 65 insertions(+), 55 deletions(-)
include/net/bluetooth/hci_core.h +0 -3
@@ -1376,9 +1376,6 @@ void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);void mgmt_set_class_of_dev_complete (struct hci_dev *hdev,u8 *dev_class,u8 status);void mgmt_set_local_name_complete (struct hci_dev *hdev,u8 *name,u8 status); -void mgmt_read_local_oob_data_complete (struct hci_dev *hdev,u8 *hash192, -u8 *rand192,u8 *hash256,u8 *rand256, -u8 status);void mgmt_device_found (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 link_type,u8 addr_type,u8 *dev_class,s8 rssi,u32 flags,u8 *eir,u16 eir_len,u8 *scan_rsp,u8 scan_rsp_len);
net/bluetooth/hci_event.c +0 -12
@@ -1045,11 +1045,6 @@ static void hci_cc_read_local_oob_data(struct hci_dev *hdev,struct hci_rp_read_local_oob_data *rp = (void *) skb->data;BT_DBG ("%s status 0x%2.2x" , hdev->name, rp->status); - -hci_dev_lock (hdev); -mgmt_read_local_oob_data_complete (hdev, rp->hash, rp->rand,NULL ,NULL , - rp->status); -hci_dev_unlock (hdev); } static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
@@ -1058,15 +1053,8 @@ static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;BT_DBG ("%s status 0x%2.2x" , hdev->name, rp->status); - -hci_dev_lock (hdev); -mgmt_read_local_oob_data_complete (hdev, rp->hash192, rp->rand192, - rp->hash256, rp->rand256, - rp->status); -hci_dev_unlock (hdev); } -static void hci_cc_le_set_random_addr (struct hci_dev *hdev,struct sk_buff *skb) {__u8 status = *((__u8 *) skb->data);
net/bluetooth/mgmt.c +65 -40
130 lines not on this page · raw message
Signature · 1 line
2.1.0