Skip to content

Commit 615cd3f

Browse files
Alphixmchehab
authored andcommitted
[media] media: lirc_dev: make better use of file->private_data
By making better use of file->private_data in lirc_dev we can avoid digging around in the irctls[] array, thereby simplifying the code. External drivers need to use lirc_get_pdata() instead of mucking around in file->private_data. The newly introduced lirc_init_pdata() function isn't very elegant, but it's a stopgap measure which can be removed once lirc_zilog is converted to rc-core. Signed-off-by: David Härdeman <[email protected]> Signed-off-by: Sean Young <[email protected]> Signed-off-by: Mauro Carvalho Chehab <[email protected]>
1 parent 0510d81 commit 615cd3f

File tree

3 files changed

+33
-93
lines changed

3 files changed

+33
-93
lines changed

drivers/media/rc/lirc_dev.c

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -247,62 +247,37 @@ EXPORT_SYMBOL(lirc_unregister_driver);
247247

248248
int lirc_dev_fop_open(struct inode *inode, struct file *file)
249249
{
250-
struct irctl *ir;
250+
struct irctl *ir = container_of(inode->i_cdev, struct irctl, cdev);
251251
int retval;
252252

253-
if (iminor(inode) >= MAX_IRCTL_DEVICES) {
254-
pr_err("open result for %d is -ENODEV\n", iminor(inode));
255-
return -ENODEV;
256-
}
257-
258-
if (mutex_lock_interruptible(&lirc_dev_lock))
259-
return -ERESTARTSYS;
260-
261-
ir = irctls[iminor(inode)];
262-
mutex_unlock(&lirc_dev_lock);
263-
264-
if (!ir) {
265-
retval = -ENODEV;
266-
goto error;
267-
}
268-
269253
dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
270254

271-
if (ir->open) {
272-
retval = -EBUSY;
273-
goto error;
274-
}
255+
if (ir->open)
256+
return -EBUSY;
275257

276258
if (ir->d.rdev) {
277259
retval = rc_open(ir->d.rdev);
278260
if (retval)
279-
goto error;
261+
return retval;
280262
}
281263

282264
if (ir->buf)
283265
lirc_buffer_clear(ir->buf);
284266

285267
ir->open++;
286268

269+
lirc_init_pdata(inode, file);
287270
nonseekable_open(inode, file);
288271

289272
return 0;
290-
291-
error:
292-
return retval;
293273
}
294274
EXPORT_SYMBOL(lirc_dev_fop_open);
295275

296276
int lirc_dev_fop_close(struct inode *inode, struct file *file)
297277
{
298-
struct irctl *ir = irctls[iminor(inode)];
278+
struct irctl *ir = file->private_data;
299279
int ret;
300280

301-
if (!ir) {
302-
pr_err("called with invalid irctl\n");
303-
return -EINVAL;
304-
}
305-
306281
ret = mutex_lock_killable(&lirc_dev_lock);
307282
WARN_ON(ret);
308283

@@ -318,14 +293,9 @@ EXPORT_SYMBOL(lirc_dev_fop_close);
318293

319294
unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
320295
{
321-
struct irctl *ir = irctls[iminor(file_inode(file))];
296+
struct irctl *ir = file->private_data;
322297
unsigned int ret;
323298

324-
if (!ir) {
325-
pr_err("called with invalid irctl\n");
326-
return POLLERR;
327-
}
328-
329299
if (!ir->attached)
330300
return POLLHUP | POLLERR;
331301

@@ -348,14 +318,9 @@ EXPORT_SYMBOL(lirc_dev_fop_poll);
348318

349319
long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
350320
{
321+
struct irctl *ir = file->private_data;
351322
__u32 mode;
352323
int result = 0;
353-
struct irctl *ir = irctls[iminor(file_inode(file))];
354-
355-
if (!ir) {
356-
pr_err("no irctl found!\n");
357-
return -ENODEV;
358-
}
359324

360325
dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
361326
ir->d.name, ir->d.minor, cmd);
@@ -432,16 +397,11 @@ ssize_t lirc_dev_fop_read(struct file *file,
432397
size_t length,
433398
loff_t *ppos)
434399
{
435-
struct irctl *ir = irctls[iminor(file_inode(file))];
400+
struct irctl *ir = file->private_data;
436401
unsigned char *buf;
437402
int ret = 0, written = 0;
438403
DECLARE_WAITQUEUE(wait, current);
439404

440-
if (!ir) {
441-
pr_err("called with invalid irctl\n");
442-
return -ENODEV;
443-
}
444-
445405
if (!LIRC_CAN_REC(ir->d.features))
446406
return -EINVAL;
447407

@@ -532,9 +492,19 @@ ssize_t lirc_dev_fop_read(struct file *file,
532492
}
533493
EXPORT_SYMBOL(lirc_dev_fop_read);
534494

495+
void lirc_init_pdata(struct inode *inode, struct file *file)
496+
{
497+
struct irctl *ir = container_of(inode->i_cdev, struct irctl, cdev);
498+
499+
file->private_data = ir;
500+
}
501+
EXPORT_SYMBOL(lirc_init_pdata);
502+
535503
void *lirc_get_pdata(struct file *file)
536504
{
537-
return irctls[iminor(file_inode(file))]->d.data;
505+
struct irctl *ir = file->private_data;
506+
507+
return ir->d.data;
538508
}
539509
EXPORT_SYMBOL(lirc_get_pdata);
540510

drivers/staging/media/lirc/lirc_zilog.c

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ static int fw_load(struct IR_tx *tx)
879879
static ssize_t read(struct file *filep, char __user *outbuf, size_t n,
880880
loff_t *ppos)
881881
{
882-
struct IR *ir = filep->private_data;
882+
struct IR *ir = lirc_get_pdata(filep);
883883
struct IR_rx *rx;
884884
struct lirc_buffer *rbuf = ir->l.rbuf;
885885
int ret = 0, written = 0, retries = 0;
@@ -1089,7 +1089,7 @@ static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
10891089
static ssize_t write(struct file *filep, const char __user *buf, size_t n,
10901090
loff_t *ppos)
10911091
{
1092-
struct IR *ir = filep->private_data;
1092+
struct IR *ir = lirc_get_pdata(filep);
10931093
struct IR_tx *tx;
10941094
size_t i;
10951095
int failures = 0;
@@ -1197,7 +1197,7 @@ static ssize_t write(struct file *filep, const char __user *buf, size_t n,
11971197
/* copied from lirc_dev */
11981198
static unsigned int poll(struct file *filep, poll_table *wait)
11991199
{
1200-
struct IR *ir = filep->private_data;
1200+
struct IR *ir = lirc_get_pdata(filep);
12011201
struct IR_rx *rx;
12021202
struct lirc_buffer *rbuf = ir->l.rbuf;
12031203
unsigned int ret;
@@ -1230,7 +1230,7 @@ static unsigned int poll(struct file *filep, poll_table *wait)
12301230

12311231
static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
12321232
{
1233-
struct IR *ir = filep->private_data;
1233+
struct IR *ir = lirc_get_pdata(filep);
12341234
unsigned long __user *uptr = (unsigned long __user *)arg;
12351235
int result;
12361236
unsigned long mode, features;
@@ -1280,61 +1280,26 @@ static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
12801280
return result;
12811281
}
12821282

1283-
static struct IR *get_ir_device_by_minor(unsigned int minor)
1284-
{
1285-
struct IR *ir;
1286-
struct IR *ret = NULL;
1287-
1288-
mutex_lock(&ir_devices_lock);
1289-
1290-
if (!list_empty(&ir_devices_list)) {
1291-
list_for_each_entry(ir, &ir_devices_list, list) {
1292-
if (ir->l.minor == minor) {
1293-
ret = get_ir_device(ir, true);
1294-
break;
1295-
}
1296-
}
1297-
}
1298-
1299-
mutex_unlock(&ir_devices_lock);
1300-
return ret;
1301-
}
1302-
13031283
/*
1304-
* Open the IR device. Get hold of our IR structure and
1305-
* stash it in private_data for the file
1284+
* Open the IR device.
13061285
*/
13071286
static int open(struct inode *node, struct file *filep)
13081287
{
13091288
struct IR *ir;
1310-
unsigned int minor = MINOR(node->i_rdev);
1311-
1312-
/* find our IR struct */
1313-
ir = get_ir_device_by_minor(minor);
13141289

1315-
if (!ir)
1316-
return -ENODEV;
1290+
lirc_init_pdata(node, filep);
1291+
ir = lirc_get_pdata(filep);
13171292

13181293
atomic_inc(&ir->open_count);
13191294

1320-
/* stash our IR struct */
1321-
filep->private_data = ir;
1322-
13231295
nonseekable_open(node, filep);
13241296
return 0;
13251297
}
13261298

13271299
/* Close the IR device */
13281300
static int close(struct inode *node, struct file *filep)
13291301
{
1330-
/* find our IR struct */
1331-
struct IR *ir = filep->private_data;
1332-
1333-
if (!ir) {
1334-
pr_err("ir: %s: no private_data attached to the file!\n",
1335-
__func__);
1336-
return -ENODEV;
1337-
}
1302+
struct IR *ir = lirc_get_pdata(filep);
13381303

13391304
atomic_dec(&ir->open_count);
13401305

@@ -1489,6 +1454,8 @@ static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
14891454
*/
14901455
ir->l.rbuf = &ir->rbuf;
14911456
ir->l.dev = &adap->dev;
1457+
/* This will be returned by lirc_get_pdata() */
1458+
ir->l.data = ir;
14921459
ret = lirc_buffer_init(ir->l.rbuf,
14931460
ir->l.chunk_size, ir->l.buffer_size);
14941461
if (ret)

include/media/lirc_dev.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ int lirc_register_driver(struct lirc_driver *d);
187187

188188
void lirc_unregister_driver(struct lirc_driver *d);
189189

190+
/* Must be called in the open fop before lirc_get_pdata() can be used */
191+
void lirc_init_pdata(struct inode *inode, struct file *file);
192+
190193
/* Returns the private data stored in the lirc_driver
191194
* associated with the given device file pointer.
192195
*/

0 commit comments

Comments
 (0)