Skip to content

Commit 6b2bbb0

Browse files
Hans Verkuilmchehab
authored andcommitted
media: cec: rework the cec event handling
Event handling was always fairly simplistic since there were only two events. With the addition of pin events this needed to be redesigned. The state_change and lost_msgs events are now core events with the guarantee that the last state is always available. The new pin events are a queue of events (up to 64 for each event) and the oldest event will be dropped if the application cannot keep up. Lost events are marked with a new event flag. Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Reviewed-by: Maxime Ripard <maxime.ripard@free-electrons.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
1 parent 6303d97 commit 6b2bbb0

4 files changed

Lines changed: 124 additions & 69 deletions

File tree

‎drivers/media/cec/cec-adap.c‎

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,62 @@ static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr
7878
* Queue a new event for this filehandle. If ts == 0, then set it
7979
* to the current time.
8080
*
81-
* The two events that are currently defined do not need to keep track
82-
* of intermediate events, so no actual queue of events is needed,
83-
* instead just store the latest state and the total number of lost
84-
* messages.
85-
*
86-
* Should new events be added in the future that require intermediate
87-
* results to be queued as well, then a proper queue data structure is
88-
* required. But until then, just keep it simple.
81+
* We keep a queue of at most max_event events where max_event differs
82+
* per event. If the queue becomes full, then drop the oldest event and
83+
* keep track of how many events we've dropped.
8984
*/
9085
void cec_queue_event_fh(struct cec_fh *fh,
9186
const struct cec_event *new_ev, u64 ts)
9287
{
93-
struct cec_event *ev = &fh->events[new_ev->event - 1];
88+
static const u8 max_events[CEC_NUM_EVENTS] = {
89+
1, 1, 64, 64,
90+
};
91+
struct cec_event_entry *entry;
92+
unsigned int ev_idx = new_ev->event - 1;
93+
94+
if (WARN_ON(ev_idx >= ARRAY_SIZE(fh->events)))
95+
return;
9496

9597
if (ts == 0)
9698
ts = ktime_get_ns();
9799

98100
mutex_lock(&fh->lock);
99-
if (new_ev->event == CEC_EVENT_LOST_MSGS &&
100-
fh->pending_events & (1 << new_ev->event)) {
101-
/*
102-
* If there is already a lost_msgs event, then just
103-
* update the lost_msgs count. This effectively
104-
* merges the old and new events into one.
105-
*/
106-
ev->lost_msgs.lost_msgs += new_ev->lost_msgs.lost_msgs;
107-
goto unlock;
108-
}
101+
if (ev_idx < CEC_NUM_CORE_EVENTS)
102+
entry = &fh->core_events[ev_idx];
103+
else
104+
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
105+
if (entry) {
106+
if (new_ev->event == CEC_EVENT_LOST_MSGS &&
107+
fh->queued_events[ev_idx]) {
108+
entry->ev.lost_msgs.lost_msgs +=
109+
new_ev->lost_msgs.lost_msgs;
110+
goto unlock;
111+
}
112+
entry->ev = *new_ev;
113+
entry->ev.ts = ts;
114+
115+
if (fh->queued_events[ev_idx] < max_events[ev_idx]) {
116+
/* Add new msg at the end of the queue */
117+
list_add_tail(&entry->list, &fh->events[ev_idx]);
118+
fh->queued_events[ev_idx]++;
119+
fh->total_queued_events++;
120+
goto unlock;
121+
}
109122

110-
/*
111-
* Intermediate states are not interesting, so just
112-
* overwrite any older event.
113-
*/
114-
*ev = *new_ev;
115-
ev->ts = ts;
116-
fh->pending_events |= 1 << new_ev->event;
123+
if (ev_idx >= CEC_NUM_CORE_EVENTS) {
124+
list_add_tail(&entry->list, &fh->events[ev_idx]);
125+
/* drop the oldest event */
126+
entry = list_first_entry(&fh->events[ev_idx],
127+
struct cec_event_entry, list);
128+
list_del(&entry->list);
129+
kfree(entry);
130+
}
131+
}
132+
/* Mark that events were lost */
133+
entry = list_first_entry_or_null(&fh->events[ev_idx],
134+
struct cec_event_entry, list);
135+
if (entry)
136+
entry->ev.flags |= CEC_EVENT_FL_DROPPED_EVENTS;
117137

118138
unlock:
119139
mutex_unlock(&fh->lock);
@@ -134,46 +154,50 @@ static void cec_queue_event(struct cec_adapter *adap,
134154
}
135155

136156
/*
137-
* Queue a new message for this filehandle. If there is no more room
138-
* in the queue, then send the LOST_MSGS event instead.
157+
* Queue a new message for this filehandle.
158+
*
159+
* We keep a queue of at most CEC_MAX_MSG_RX_QUEUE_SZ messages. If the
160+
* queue becomes full, then drop the oldest message and keep track
161+
* of how many messages we've dropped.
139162
*/
140163
static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
141164
{
142-
static const struct cec_event ev_lost_msg = {
143-
.ts = 0,
165+
static const struct cec_event ev_lost_msgs = {
144166
.event = CEC_EVENT_LOST_MSGS,
145-
.flags = 0,
146-
{
147-
.lost_msgs.lost_msgs = 1,
148-
},
167+
.lost_msgs.lost_msgs = 1,
149168
};
150169
struct cec_msg_entry *entry;
151170

152171
mutex_lock(&fh->lock);
153172
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
154-
if (!entry)
155-
goto lost_msgs;
156-
157-
entry->msg = *msg;
158-
/* Add new msg at the end of the queue */
159-
list_add_tail(&entry->list, &fh->msgs);
173+
if (entry) {
174+
entry->msg = *msg;
175+
/* Add new msg at the end of the queue */
176+
list_add_tail(&entry->list, &fh->msgs);
177+
178+
if (fh->queued_msgs < CEC_MAX_MSG_RX_QUEUE_SZ) {
179+
/* All is fine if there is enough room */
180+
fh->queued_msgs++;
181+
mutex_unlock(&fh->lock);
182+
wake_up_interruptible(&fh->wait);
183+
return;
184+
}
160185

161-
/*
162-
* if the queue now has more than CEC_MAX_MSG_RX_QUEUE_SZ
163-
* messages, drop the oldest one and send a lost message event.
164-
*/
165-
if (fh->queued_msgs == CEC_MAX_MSG_RX_QUEUE_SZ) {
186+
/*
187+
* if the message queue is full, then drop the oldest one and
188+
* send a lost message event.
189+
*/
190+
entry = list_first_entry(&fh->msgs, struct cec_msg_entry, list);
166191
list_del(&entry->list);
167-
goto lost_msgs;
192+
kfree(entry);
168193
}
169-
fh->queued_msgs++;
170194
mutex_unlock(&fh->lock);
171-
wake_up_interruptible(&fh->wait);
172-
return;
173195

174-
lost_msgs:
175-
mutex_unlock(&fh->lock);
176-
cec_queue_event_fh(fh, &ev_lost_msg, 0);
196+
/*
197+
* We lost a message, either because kmalloc failed or the queue
198+
* was full.
199+
*/
200+
cec_queue_event_fh(fh, &ev_lost_msgs, ktime_get_ns());
177201
}
178202

179203
/*

‎drivers/media/cec/cec-api.c‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static unsigned int cec_poll(struct file *filp,
5757
res |= POLLOUT | POLLWRNORM;
5858
if (fh->queued_msgs)
5959
res |= POLLIN | POLLRDNORM;
60-
if (fh->pending_events)
60+
if (fh->total_queued_events)
6161
res |= POLLPRI;
6262
poll_wait(filp, &fh->wait, poll);
6363
mutex_unlock(&adap->lock);
@@ -289,39 +289,47 @@ static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
289289
static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
290290
bool block, struct cec_event __user *parg)
291291
{
292-
struct cec_event *ev = NULL;
292+
struct cec_event_entry *ev = NULL;
293293
u64 ts = ~0ULL;
294294
unsigned int i;
295+
unsigned int ev_idx;
295296
long err = 0;
296297

297298
mutex_lock(&fh->lock);
298-
while (!fh->pending_events && block) {
299+
while (!fh->total_queued_events && block) {
299300
mutex_unlock(&fh->lock);
300-
err = wait_event_interruptible(fh->wait, fh->pending_events);
301+
err = wait_event_interruptible(fh->wait,
302+
fh->total_queued_events);
301303
if (err)
302304
return err;
303305
mutex_lock(&fh->lock);
304306
}
305307

306308
/* Find the oldest event */
307309
for (i = 0; i < CEC_NUM_EVENTS; i++) {
308-
if (fh->pending_events & (1 << (i + 1)) &&
309-
fh->events[i].ts <= ts) {
310-
ev = &fh->events[i];
311-
ts = ev->ts;
310+
struct cec_event_entry *entry =
311+
list_first_entry_or_null(&fh->events[i],
312+
struct cec_event_entry, list);
313+
314+
if (entry && entry->ev.ts <= ts) {
315+
ev = entry;
316+
ev_idx = i;
317+
ts = ev->ev.ts;
312318
}
313319
}
320+
314321
if (!ev) {
315322
err = -EAGAIN;
316323
goto unlock;
317324
}
325+
list_del(&ev->list);
318326

319-
if (copy_to_user(parg, ev, sizeof(*ev))) {
327+
if (copy_to_user(parg, &ev->ev, sizeof(ev->ev)))
320328
err = -EFAULT;
321-
goto unlock;
322-
}
323-
324-
fh->pending_events &= ~(1 << ev->event);
329+
if (ev_idx >= CEC_NUM_CORE_EVENTS)
330+
kfree(ev);
331+
fh->queued_events[ev_idx]--;
332+
fh->total_queued_events--;
325333

326334
unlock:
327335
mutex_unlock(&fh->lock);
@@ -495,13 +503,16 @@ static int cec_open(struct inode *inode, struct file *filp)
495503
.event = CEC_EVENT_STATE_CHANGE,
496504
.flags = CEC_EVENT_FL_INITIAL_STATE,
497505
};
506+
unsigned int i;
498507
int err;
499508

500509
if (!fh)
501510
return -ENOMEM;
502511

503512
INIT_LIST_HEAD(&fh->msgs);
504513
INIT_LIST_HEAD(&fh->xfer_list);
514+
for (i = 0; i < CEC_NUM_EVENTS; i++)
515+
INIT_LIST_HEAD(&fh->events[i]);
505516
mutex_init(&fh->lock);
506517
init_waitqueue_head(&fh->wait);
507518

@@ -544,6 +555,7 @@ static int cec_release(struct inode *inode, struct file *filp)
544555
struct cec_devnode *devnode = cec_devnode_data(filp);
545556
struct cec_adapter *adap = to_cec_adapter(devnode);
546557
struct cec_fh *fh = filp->private_data;
558+
unsigned int i;
547559

548560
mutex_lock(&adap->lock);
549561
if (adap->cec_initiator == fh)
@@ -585,6 +597,16 @@ static int cec_release(struct inode *inode, struct file *filp)
585597
list_del(&entry->list);
586598
kfree(entry);
587599
}
600+
for (i = CEC_NUM_CORE_EVENTS; i < CEC_NUM_EVENTS; i++) {
601+
while (!list_empty(&fh->events[i])) {
602+
struct cec_event_entry *entry =
603+
list_first_entry(&fh->events[i],
604+
struct cec_event_entry, list);
605+
606+
list_del(&entry->list);
607+
kfree(entry);
608+
}
609+
}
588610
kfree(fh);
589611

590612
cec_put_device(devnode);

‎include/media/cec.h‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ struct cec_msg_entry {
8181
struct cec_msg msg;
8282
};
8383

84-
#define CEC_NUM_EVENTS CEC_EVENT_LOST_MSGS
84+
struct cec_event_entry {
85+
struct list_head list;
86+
struct cec_event ev;
87+
};
88+
89+
#define CEC_NUM_CORE_EVENTS 2
90+
#define CEC_NUM_EVENTS CEC_EVENT_PIN_HIGH
8591

8692
struct cec_fh {
8793
struct list_head list;
@@ -92,9 +98,11 @@ struct cec_fh {
9298

9399
/* Events */
94100
wait_queue_head_t wait;
95-
unsigned int pending_events;
96-
struct cec_event events[CEC_NUM_EVENTS];
97101
struct mutex lock;
102+
struct list_head events[CEC_NUM_EVENTS]; /* queued events */
103+
u8 queued_events[CEC_NUM_EVENTS];
104+
unsigned int total_queued_events;
105+
struct cec_event_entry core_events[CEC_NUM_CORE_EVENTS];
98106
struct list_head msgs; /* queued messages */
99107
unsigned int queued_msgs;
100108
};

‎include/uapi/linux/cec.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ struct cec_log_addrs {
412412
#define CEC_EVENT_PIN_HIGH 4
413413

414414
#define CEC_EVENT_FL_INITIAL_STATE (1 << 0)
415+
#define CEC_EVENT_FL_DROPPED_EVENTS (1 << 1)
415416

416417
/**
417418
* struct cec_event_state_change - used when the CEC adapter changes state.
@@ -424,7 +425,7 @@ struct cec_event_state_change {
424425
};
425426

426427
/**
427-
* struct cec_event_lost_msgs - tells you how many messages were lost due.
428+
* struct cec_event_lost_msgs - tells you how many messages were lost.
428429
* @lost_msgs: how many messages were lost.
429430
*/
430431
struct cec_event_lost_msgs {

0 commit comments

Comments
 (0)