Skip to content

Commit

Permalink
Bluetooth: hidp: merge 'send' functions into hidp_send_message()
Browse files Browse the repository at this point in the history
We handle skb buffers all over the place, even though we have
hidp_send_*_message() helpers. This creates a more generic
hidp_send_message() helper and uses it instead of dealing with transmit
queues directly everywhere.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Acked-by: Marcel Holtmann <marcel@holtmann.org>
Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>

Conflicts:
	net/bluetooth/hidp/core.c
  • Loading branch information
David Herrmann authored and RealJohnGalt committed Apr 27, 2016
1 parent 9af741b commit 2d2ec8b
Showing 1 changed file with 60 additions and 82 deletions.
142 changes: 60 additions & 82 deletions net/bluetooth/hidp/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static void __hidp_unlink_session(struct hidp_session *session)
module_put(THIS_MODULE);
}

static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
{
memset(ci, 0, sizeof(*ci));
bacpy(&ci->bdaddr, &session->bdaddr);
Expand Down Expand Up @@ -151,11 +151,56 @@ static void __hidp_copy_session(struct hidp_session *session, struct hidp_connin
}
}

/* assemble skb, queue message on @transmit and wake up the session thread */
static int hidp_send_message(struct hidp_session *session, struct socket *sock,
struct sk_buff_head *transmit, unsigned char hdr,
const unsigned char *data, int size)
{
struct sk_buff *skb;
struct sock *sk = sock->sk;

BT_DBG("session %p data %p size %d", session, data, size);

if (atomic_read(&session->terminate))
return -EIO;

skb = alloc_skb(size + 1, GFP_ATOMIC);
if (!skb) {
BT_ERR("Can't allocate memory for new frame");
return -ENOMEM;
}

*skb_put(skb, 1) = hdr;
if (data && size > 0)
memcpy(skb_put(skb, size), data, size);

skb_queue_tail(transmit, skb);
wake_up_interruptible(sk_sleep(sk));

return 0;
}

static int hidp_send_ctrl_message(struct hidp_session *session,
unsigned char hdr, const unsigned char *data,
int size)
{
return hidp_send_message(session, session->ctrl_sock,
&session->ctrl_transmit, hdr, data, size);
}

static int hidp_send_intr_message(struct hidp_session *session,
unsigned char hdr, const unsigned char *data,
int size)
{
return hidp_send_message(session, session->intr_sock,
&session->intr_transmit, hdr, data, size);
}

static int hidp_queue_event(struct hidp_session *session, struct input_dev *dev,
unsigned int type, unsigned int code, int value)
{
unsigned char newleds;
struct sk_buff *skb;
unsigned char hdr, data[2];

BT_DBG("session %p type %d code %d value %d", session, type, code, value);

Expand All @@ -173,21 +218,11 @@ static int hidp_queue_event(struct hidp_session *session, struct input_dev *dev,

session->leds = newleds;

skb = alloc_skb(3, GFP_ATOMIC);
if (!skb) {
BT_ERR("Can't allocate memory for new frame");
return -ENOMEM;
}

*skb_put(skb, 1) = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
*skb_put(skb, 1) = 0x01;
*skb_put(skb, 1) = newleds;
hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
data[0] = 0x01;
data[1] = newleds;

skb_queue_tail(&session->intr_transmit, skb);

hidp_schedule(session);

return 0;
return hidp_send_intr_message(session, hdr, data, 2);
}

static int hidp_hidinput_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Expand Down Expand Up @@ -260,76 +295,19 @@ static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
input_sync(dev);
}

static int __hidp_send_ctrl_message(struct hidp_session *session,
unsigned char hdr, unsigned char *data, int size)
{
struct sk_buff *skb;

BT_DBG("session %p data %p size %d", session, data, size);

skb = alloc_skb(size + 1, GFP_ATOMIC);
if (!skb) {
BT_ERR("Can't allocate memory for new frame");
return -ENOMEM;
}

*skb_put(skb, 1) = hdr;
if (data && size > 0)
memcpy(skb_put(skb, size), data, size);

skb_queue_tail(&session->ctrl_transmit, skb);

return 0;
}

static inline int hidp_send_ctrl_message(struct hidp_session *session,
unsigned char hdr, unsigned char *data, int size)
{
int err;

err = __hidp_send_ctrl_message(session, hdr, data, size);

hidp_schedule(session);

return err;
}

static int hidp_queue_report(struct hidp_session *session,
unsigned char *data, int size)
{
struct sk_buff *skb;

BT_DBG("session %p hid %p data %p size %d", session, session->hid, data, size);

skb = alloc_skb(size + 1, GFP_ATOMIC);
if (!skb) {
BT_ERR("Can't allocate memory for new frame");
return -ENOMEM;
}

*skb_put(skb, 1) = 0xa2;
if (size > 0)
memcpy(skb_put(skb, size), data, size);

skb_queue_tail(&session->intr_transmit, skb);

hidp_schedule(session);

return 0;
}

static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
{
unsigned char buf[32];
unsigned char buf[32], hdr;
int rsize;

rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
if (rsize > sizeof(buf))
return -EIO;

hid_output_report(report, buf);
hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;

return hidp_queue_report(session, buf, rsize);
return hidp_send_intr_message(session, hdr, buf, rsize);
}

static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count,
Expand Down Expand Up @@ -395,12 +373,12 @@ static void hidp_process_handshake(struct hidp_session *session,
case HIDP_HSHK_ERR_FATAL:
/* Device requests a reboot, as this is the only way this error
* can be recovered. */
__hidp_send_ctrl_message(session,
hidp_send_ctrl_message(session,
HIDP_TRANS_HID_CONTROL | HIDP_CTRL_SOFT_RESET, NULL, 0);
break;

default:
__hidp_send_ctrl_message(session,
hidp_send_ctrl_message(session,
HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
break;
}
Expand Down Expand Up @@ -445,7 +423,7 @@ static void hidp_process_data(struct hidp_session *session, struct sk_buff *skb,
break;

default:
__hidp_send_ctrl_message(session,
hidp_send_ctrl_message(session,
HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
}
}
Expand Down Expand Up @@ -477,7 +455,7 @@ static void hidp_recv_ctrl_frame(struct hidp_session *session,
break;

default:
__hidp_send_ctrl_message(session,
hidp_send_ctrl_message(session,
HIDP_TRANS_HANDSHAKE | HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
break;
}
Expand Down Expand Up @@ -1003,7 +981,7 @@ int hidp_get_connlist(struct hidp_connlist_req *req)

session = list_entry(p, struct hidp_session, list);

__hidp_copy_session(session, &ci);
hidp_copy_session(session, &ci);

if (copy_to_user(req->ci, &ci, sizeof(ci))) {
err = -EFAULT;
Expand All @@ -1030,7 +1008,7 @@ int hidp_get_conninfo(struct hidp_conninfo *ci)

session = __hidp_get_session(&ci->bdaddr);
if (session)
__hidp_copy_session(session, ci);
hidp_copy_session(session, ci);
else
err = -ENOENT;

Expand Down

0 comments on commit 2d2ec8b

Please sign in to comment.