2007-02-09 22:24:33 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
HIDP implementation for Linux Bluetooth stack (BlueZ).
|
|
|
|
Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License version 2 as
|
|
|
|
published by the Free Software Foundation;
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
|
|
|
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
|
2007-02-09 22:24:33 +08:00
|
|
|
CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
2005-04-17 06:20:36 +08:00
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2007-02-09 22:24:33 +08:00
|
|
|
ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
|
|
|
|
COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
|
2005-04-17 06:20:36 +08:00
|
|
|
SOFTWARE IS DISCLAIMED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __HIDP_H
|
|
|
|
#define __HIDP_H
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
2013-04-07 02:28:46 +08:00
|
|
|
#include <linux/kref.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <net/bluetooth/bluetooth.h>
|
2013-04-07 02:28:46 +08:00
|
|
|
#include <net/bluetooth/l2cap.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* HIDP header masks */
|
|
|
|
#define HIDP_HEADER_TRANS_MASK 0xf0
|
|
|
|
#define HIDP_HEADER_PARAM_MASK 0x0f
|
|
|
|
|
|
|
|
/* HIDP transaction types */
|
|
|
|
#define HIDP_TRANS_HANDSHAKE 0x00
|
|
|
|
#define HIDP_TRANS_HID_CONTROL 0x10
|
|
|
|
#define HIDP_TRANS_GET_REPORT 0x40
|
|
|
|
#define HIDP_TRANS_SET_REPORT 0x50
|
|
|
|
#define HIDP_TRANS_GET_PROTOCOL 0x60
|
|
|
|
#define HIDP_TRANS_SET_PROTOCOL 0x70
|
|
|
|
#define HIDP_TRANS_GET_IDLE 0x80
|
|
|
|
#define HIDP_TRANS_SET_IDLE 0x90
|
|
|
|
#define HIDP_TRANS_DATA 0xa0
|
|
|
|
#define HIDP_TRANS_DATC 0xb0
|
|
|
|
|
|
|
|
/* HIDP handshake results */
|
|
|
|
#define HIDP_HSHK_SUCCESSFUL 0x00
|
|
|
|
#define HIDP_HSHK_NOT_READY 0x01
|
|
|
|
#define HIDP_HSHK_ERR_INVALID_REPORT_ID 0x02
|
|
|
|
#define HIDP_HSHK_ERR_UNSUPPORTED_REQUEST 0x03
|
|
|
|
#define HIDP_HSHK_ERR_INVALID_PARAMETER 0x04
|
|
|
|
#define HIDP_HSHK_ERR_UNKNOWN 0x0e
|
|
|
|
#define HIDP_HSHK_ERR_FATAL 0x0f
|
|
|
|
|
|
|
|
/* HIDP control operation parameters */
|
|
|
|
#define HIDP_CTRL_NOP 0x00
|
|
|
|
#define HIDP_CTRL_HARD_RESET 0x01
|
|
|
|
#define HIDP_CTRL_SOFT_RESET 0x02
|
|
|
|
#define HIDP_CTRL_SUSPEND 0x03
|
|
|
|
#define HIDP_CTRL_EXIT_SUSPEND 0x04
|
|
|
|
#define HIDP_CTRL_VIRTUAL_CABLE_UNPLUG 0x05
|
|
|
|
|
|
|
|
/* HIDP data transaction headers */
|
|
|
|
#define HIDP_DATA_RTYPE_MASK 0x03
|
|
|
|
#define HIDP_DATA_RSRVD_MASK 0x0c
|
|
|
|
#define HIDP_DATA_RTYPE_OTHER 0x00
|
|
|
|
#define HIDP_DATA_RTYPE_INPUT 0x01
|
|
|
|
#define HIDP_DATA_RTYPE_OUPUT 0x02
|
|
|
|
#define HIDP_DATA_RTYPE_FEATURE 0x03
|
|
|
|
|
|
|
|
/* HIDP protocol header parameters */
|
|
|
|
#define HIDP_PROTO_BOOT 0x00
|
|
|
|
#define HIDP_PROTO_REPORT 0x01
|
|
|
|
|
|
|
|
/* HIDP ioctl defines */
|
|
|
|
#define HIDPCONNADD _IOW('H', 200, int)
|
|
|
|
#define HIDPCONNDEL _IOW('H', 201, int)
|
|
|
|
#define HIDPGETCONNLIST _IOR('H', 210, int)
|
|
|
|
#define HIDPGETCONNINFO _IOR('H', 211, int)
|
|
|
|
|
|
|
|
#define HIDP_VIRTUAL_CABLE_UNPLUG 0
|
|
|
|
#define HIDP_BOOT_PROTOCOL_MODE 1
|
|
|
|
#define HIDP_BLUETOOTH_VENDOR_ID 9
|
2011-04-06 16:43:23 +08:00
|
|
|
#define HIDP_WAITING_FOR_RETURN 10
|
2011-01-18 16:04:38 +08:00
|
|
|
#define HIDP_WAITING_FOR_SEND_ACK 11
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct hidp_connadd_req {
|
2011-03-21 21:20:01 +08:00
|
|
|
int ctrl_sock; /* Connected control socket */
|
|
|
|
int intr_sock; /* Connected interrupt socket */
|
2005-04-17 06:20:36 +08:00
|
|
|
__u16 parser;
|
|
|
|
__u16 rd_size;
|
2007-02-10 00:38:00 +08:00
|
|
|
__u8 __user *rd_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
__u8 country;
|
|
|
|
__u8 subclass;
|
|
|
|
__u16 vendor;
|
|
|
|
__u16 product;
|
|
|
|
__u16 version;
|
|
|
|
__u32 flags;
|
|
|
|
__u32 idle_to;
|
|
|
|
char name[128];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hidp_conndel_req {
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
__u32 flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hidp_conninfo {
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
__u32 flags;
|
|
|
|
__u16 state;
|
|
|
|
__u16 vendor;
|
|
|
|
__u16 product;
|
|
|
|
__u16 version;
|
|
|
|
char name[128];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hidp_connlist_req {
|
|
|
|
__u32 cnum;
|
|
|
|
struct hidp_conninfo __user *ci;
|
|
|
|
};
|
|
|
|
|
2013-04-07 02:28:46 +08:00
|
|
|
int hidp_connection_add(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock);
|
|
|
|
int hidp_connection_del(struct hidp_conndel_req *req);
|
2005-04-17 06:20:36 +08:00
|
|
|
int hidp_get_connlist(struct hidp_connlist_req *req);
|
|
|
|
int hidp_get_conninfo(struct hidp_conninfo *ci);
|
|
|
|
|
2013-04-07 02:28:46 +08:00
|
|
|
enum hidp_session_state {
|
|
|
|
HIDP_SESSION_IDLING,
|
HID: Bluetooth: hidp: register HID devices async
While l2cap_user callbacks are running, the whole hci_dev is locked. Even
if we would add more fine-grained locking to HCI core, it would still be
called from the non-reentrant rx work-queue and thus block the event
processing.
However, if we want to perform synchronous I/O during HID device
registration (eg., to perform device-detection), we need the HCI core
to be able to dispatch incoming data.
Therefore, we now move device-registration to a separate worker. The HCI
core can continue running and we add devices asynchronously in another
kernel thread. Device removal is synchronized and waits for the worker
to exit before calling the usual device removal functions.
If l2cap_user->remove is called before the thread registered the devices,
we set "terminate" to true and the thread will skip it. If
l2cap_user->remove is called after it, we notice this as the device
is no longer in HIDP_SESSION_PREPARING state and simply unregister the
device as we did before.
There is no new deadlock as we now call hidp_session_add_dev() with
one lock less held (the HCI lock) and it cannot itself call back into
HCI as it was called with the HCI-lock held before.
One might wonder whether this can block during device unregistration.
But we set "terminate" to true and wake the HIDP thread up _before_
unregistering the HID/input devices. Therefore, all pending HID I/O
operations are canceled. All further I/O attempts will fail with ENODEV
or EIO. So all latency we can get are few context-switches, but no
timeouts or blocking I/O waits!
This change also prepares for a long standing HID bug. All HID devices
that register power_supply devices need to be able to handle callbacks
during registration (a power_supply oddity that cannot easily be fixed).
So with this patch available, we can allow HID I/O during registration
by calling the recently introduced hid_device_io_start/stop helpers,
which currently are a no-op for bluetooth due to this locking.
Note that we cannot do the same for input devices. input-core doesn't
allow us to call input_event() asynchronously to input_register_device(),
which HID-core kindly allows (for good reasons).
Fixing input-core to allow this isn't as easy as it sounds and is,
beside simplifying HIDP, not really an improvement. Hence, we still
register input devices synchronously as we did before. Only HID devices
are registered asynchronously.
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Acked-by: Jiri Kosina <jkosina@suse.cz>
Acked-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Tested-by: Daniel Nicoletti <dantti12@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2013-05-23 19:10:25 +08:00
|
|
|
HIDP_SESSION_PREPARING,
|
2013-04-07 02:28:46 +08:00
|
|
|
HIDP_SESSION_RUNNING,
|
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* HIDP session defines */
|
|
|
|
struct hidp_session {
|
|
|
|
struct list_head list;
|
2013-04-07 02:28:46 +08:00
|
|
|
struct kref ref;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-04-07 02:28:46 +08:00
|
|
|
/* runtime management */
|
|
|
|
atomic_t state;
|
|
|
|
wait_queue_head_t state_queue;
|
|
|
|
atomic_t terminate;
|
|
|
|
struct task_struct *task;
|
|
|
|
unsigned long flags;
|
2009-08-23 05:22:15 +08:00
|
|
|
|
2013-04-07 02:28:46 +08:00
|
|
|
/* connection management */
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
struct l2cap_conn *conn;
|
|
|
|
struct l2cap_user user;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct socket *ctrl_sock;
|
|
|
|
struct socket *intr_sock;
|
2013-04-07 02:28:46 +08:00
|
|
|
struct sk_buff_head ctrl_transmit;
|
|
|
|
struct sk_buff_head intr_transmit;
|
2005-04-17 06:20:36 +08:00
|
|
|
uint ctrl_mtu;
|
|
|
|
uint intr_mtu;
|
2013-04-07 02:28:46 +08:00
|
|
|
unsigned long idle_to;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-04-07 02:28:46 +08:00
|
|
|
/* device management */
|
HID: Bluetooth: hidp: register HID devices async
While l2cap_user callbacks are running, the whole hci_dev is locked. Even
if we would add more fine-grained locking to HCI core, it would still be
called from the non-reentrant rx work-queue and thus block the event
processing.
However, if we want to perform synchronous I/O during HID device
registration (eg., to perform device-detection), we need the HCI core
to be able to dispatch incoming data.
Therefore, we now move device-registration to a separate worker. The HCI
core can continue running and we add devices asynchronously in another
kernel thread. Device removal is synchronized and waits for the worker
to exit before calling the usual device removal functions.
If l2cap_user->remove is called before the thread registered the devices,
we set "terminate" to true and the thread will skip it. If
l2cap_user->remove is called after it, we notice this as the device
is no longer in HIDP_SESSION_PREPARING state and simply unregister the
device as we did before.
There is no new deadlock as we now call hidp_session_add_dev() with
one lock less held (the HCI lock) and it cannot itself call back into
HCI as it was called with the HCI-lock held before.
One might wonder whether this can block during device unregistration.
But we set "terminate" to true and wake the HIDP thread up _before_
unregistering the HID/input devices. Therefore, all pending HID I/O
operations are canceled. All further I/O attempts will fail with ENODEV
or EIO. So all latency we can get are few context-switches, but no
timeouts or blocking I/O waits!
This change also prepares for a long standing HID bug. All HID devices
that register power_supply devices need to be able to handle callbacks
during registration (a power_supply oddity that cannot easily be fixed).
So with this patch available, we can allow HID I/O during registration
by calling the recently introduced hid_device_io_start/stop helpers,
which currently are a no-op for bluetooth due to this locking.
Note that we cannot do the same for input devices. input-core doesn't
allow us to call input_event() asynchronously to input_register_device(),
which HID-core kindly allows (for good reasons).
Fixing input-core to allow this isn't as easy as it sounds and is,
beside simplifying HIDP, not really an improvement. Hence, we still
register input devices synchronously as we did before. Only HID devices
are registered asynchronously.
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Acked-by: Jiri Kosina <jkosina@suse.cz>
Acked-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Tested-by: Daniel Nicoletti <dantti12@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2013-05-23 19:10:25 +08:00
|
|
|
struct work_struct dev_init;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct input_dev *input;
|
2007-02-18 06:58:49 +08:00
|
|
|
struct hid_device *hid;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct timer_list timer;
|
|
|
|
|
2013-04-07 02:28:46 +08:00
|
|
|
/* Report descriptor */
|
|
|
|
__u8 *rd_data;
|
|
|
|
uint rd_size;
|
|
|
|
|
|
|
|
/* session data */
|
|
|
|
unsigned char keys[8];
|
|
|
|
unsigned char leds;
|
2008-05-16 17:49:16 +08:00
|
|
|
|
2011-01-18 16:04:40 +08:00
|
|
|
/* Used in hidp_get_raw_report() */
|
|
|
|
int waiting_report_type; /* HIDP_DATA_RTYPE_* */
|
|
|
|
int waiting_report_number; /* -1 for not numbered */
|
|
|
|
struct mutex report_mutex;
|
|
|
|
struct sk_buff *report_return;
|
|
|
|
wait_queue_head_t report_queue;
|
|
|
|
|
2011-01-18 16:04:38 +08:00
|
|
|
/* Used in hidp_output_raw_report() */
|
|
|
|
int output_report_success; /* boolean */
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* HIDP init defines */
|
|
|
|
extern int __init hidp_init_sockets(void);
|
|
|
|
extern void __exit hidp_cleanup_sockets(void);
|
|
|
|
|
|
|
|
#endif /* __HIDP_H */
|