Skip to content

Commit 85f5b35

Browse files
committed
Input: serio_raw - explicitly mark disconnected ports as dead
Instead of relying on setting serio_raw->serio to NULL upon disconnecting ports mark them explicitly as "dead". Also take and carry reference to underlying serio port to make sure it does not go away until we are done with it. Reviewed-by: Wanlong Gao <gaowanlong@cn.fujitsu.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 15a564d commit 85f5b35

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

‎drivers/input/serio/serio_raw.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct serio_raw {
4040
wait_queue_head_t wait;
4141
struct list_head client_list;
4242
struct list_head node;
43+
bool dead;
4344
};
4445

4546
struct serio_raw_client {
@@ -91,7 +92,7 @@ static int serio_raw_open(struct inode *inode, struct file *file)
9192
goto out;
9293
}
9394

94-
if (!serio_raw->serio) {
95+
if (serio_raw->dead) {
9596
retval = -ENODEV;
9697
goto out;
9798
}
@@ -123,6 +124,8 @@ static void serio_raw_cleanup(struct kref *kref)
123124

124125
misc_deregister(&serio_raw->dev);
125126
list_del_init(&serio_raw->node);
127+
128+
put_device(&serio_raw->serio->dev);
126129
kfree(serio_raw);
127130
}
128131

@@ -164,19 +167,18 @@ static ssize_t serio_raw_read(struct file *file, char __user *buffer,
164167
char uninitialized_var(c);
165168
ssize_t retval = 0;
166169

167-
if (!serio_raw->serio)
170+
if (serio_raw->dead)
168171
return -ENODEV;
169172

170173
if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
171174
return -EAGAIN;
172175

173176
retval = wait_event_interruptible(serio_raw->wait,
174-
serio_raw->head != serio_raw->tail ||
175-
!serio_raw->serio);
177+
serio_raw->head != serio_raw->tail || serio_raw->dead);
176178
if (retval)
177179
return retval;
178180

179-
if (!serio_raw->serio)
181+
if (serio_raw->dead)
180182
return -ENODEV;
181183

182184
while (retval < count && serio_raw_fetch_byte(serio_raw, &c)) {
@@ -201,7 +203,7 @@ static ssize_t serio_raw_write(struct file *file, const char __user *buffer,
201203
if (retval)
202204
return retval;
203205

204-
if (!serio_raw->serio) {
206+
if (serio_raw->dead) {
205207
retval = -ENODEV;
206208
goto out;
207209
}
@@ -291,10 +293,12 @@ static int serio_raw_connect(struct serio *serio, struct serio_driver *drv)
291293
snprintf(serio_raw->name, sizeof(serio_raw->name),
292294
"serio_raw%d", serio_raw_no++);
293295
kref_init(&serio_raw->kref);
294-
serio_raw->serio = serio;
295296
INIT_LIST_HEAD(&serio_raw->client_list);
296297
init_waitqueue_head(&serio_raw->wait);
297298

299+
serio_raw->serio = serio;
300+
get_device(&serio->dev);
301+
298302
serio_set_drvdata(serio, serio_raw);
299303

300304
err = serio_open(serio, drv);
@@ -330,6 +334,7 @@ static int serio_raw_connect(struct serio *serio, struct serio_driver *drv)
330334
list_del_init(&serio_raw->node);
331335
out_free:
332336
serio_set_drvdata(serio, NULL);
337+
put_device(&serio->dev);
333338
kfree(serio_raw);
334339
out:
335340
mutex_unlock(&serio_raw_mutex);
@@ -365,7 +370,7 @@ static void serio_raw_disconnect(struct serio *serio)
365370
serio_close(serio);
366371
serio_set_drvdata(serio, NULL);
367372

368-
serio_raw->serio = NULL;
373+
serio_raw->dead = true;
369374
wake_up_interruptible(&serio_raw->wait);
370375
kref_put(&serio_raw->kref, serio_raw_cleanup);
371376

0 commit comments

Comments
 (0)