Skip to content
2 changes: 1 addition & 1 deletion drivers/hid/ipts/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @rsp: The address that the received response will be copied to.
* @timeout: How many milliseconds the function will wait at most.
*
* A negative timeout means to wait forever.
* A timeout of 0 means to check once and return.
*
* Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
*/
Expand Down
67 changes: 67 additions & 0 deletions drivers/hid/ipts/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
#ifndef IPTS_CONTEXT_H
#define IPTS_CONTEXT_H

#include <linux/atomic.h>
#include <linux/completion.h>
#include <linux/device.h>
#include <linux/hid.h>
#include <linux/kref.h>
#include <linux/mei_cl_bus.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/wait.h>
#include <linux/workqueue.h>

#include "mei.h"
#include "resources.h"
Expand Down Expand Up @@ -43,10 +47,73 @@ struct ipts_context {
bool hid_active;
struct hid_device *hid;

/*
* Number of HID callbacks currently running driver code. Stopping waits, bounded,
* until it drops to 0.
*/
atomic_t hid_users;
wait_queue_head_t hid_users_wq;

/*
* Serializes a restart with remove. Restarts requested from a HID callback (the EDS1
* mode switch) run from restart_work, never inside the callback.
*/
struct mutex ctl_lock;
struct work_struct restart_work;
bool removing;

/*
* Freed when the last reference is dropped. Probe holds one (dropped by devres), and the
* receiver thread, the HID device, every holder of message_lock and a queued restart
* hold one each.
*/
struct kref refcount;

/*
* Users other than probe. ipts_remove() waits, bounded, for this to reach zero. That
* narrows the window in which the module code is freed under a user that is about to
* return, but does not close it: the last user wakes remove from inside its own put.
*/
atomic_t users;
wait_queue_head_t users_wq;

struct ipts_device_info info;
struct ipts_resources resources;

/*
* Set by a stop whose wait for running HID callbacks timed out: a callback that outlived
* the wait may have sent the device a command after it confirmed the stop. The buffers
* are then leaked instead of freed, by that stop or, if it kept everything, by the next.
*/
bool buffers_unconfirmed;

struct ipts_thread receiver_loop;
};

void ipts_context_release(struct kref *refcount);

/*
* Every user of the context (the receiver thread, the HID device, a holder of message_lock, a
* queued restart) takes a reference, so the context is freed only after the last user is gone.
*/
static inline void ipts_context_get(struct ipts_context *ipts)
{
if (!ipts)
return;

kref_get(&ipts->refcount);
atomic_inc(&ipts->users);
}

static inline void ipts_context_put(struct ipts_context *ipts)
{
if (!ipts)
return;

if (atomic_dec_and_test(&ipts->users))
wake_up_all(&ipts->users_wq);

kref_put(&ipts->refcount, ipts_context_release);
}

#endif /* IPTS_CONTEXT_H */
Loading