Skip to content

Commit d713f1f

Browse files
KAGA-KOKOkuba-moo
authored andcommitted
ptp: Split out PTP_PIN_SETFUNC ioctl code
Continue the ptp_ioctl() cleanup by splitting out the PTP_PIN_SETFUNC ioctl code into a helper function. Convert to lock guard while at it and remove the pointless memset of the pd::rsv because nothing uses it. No functional change intended. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Vadim Fedorenko <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent b246e09 commit d713f1f

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

drivers/ptp/ptp_chardev.c

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -415,16 +415,34 @@ static long ptp_pin_getfunc(struct ptp_clock *ptp, unsigned int cmd, void __user
415415
return copy_to_user(arg, &pd, sizeof(pd)) ? -EFAULT : 0;
416416
}
417417

418+
static long ptp_pin_setfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg)
419+
{
420+
struct ptp_clock_info *ops = ptp->info;
421+
struct ptp_pin_desc pd;
422+
unsigned int pin_index;
423+
424+
if (copy_from_user(&pd, arg, sizeof(pd)))
425+
return -EFAULT;
426+
427+
if (cmd == PTP_PIN_SETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv)))
428+
return -EINVAL;
429+
430+
if (pd.index >= ops->n_pins)
431+
return -EINVAL;
432+
433+
pin_index = array_index_nospec(pd.index, ops->n_pins);
434+
scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux)
435+
return ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
436+
}
437+
418438
long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd,
419439
unsigned long arg)
420440
{
421441
struct ptp_clock *ptp =
422442
container_of(pccontext->clk, struct ptp_clock, clock);
423-
struct ptp_clock_info *ops = ptp->info;
424443
struct timestamp_event_queue *tsevq;
425-
unsigned int i, pin_index;
426-
struct ptp_pin_desc pd;
427444
void __user *argptr;
445+
unsigned int i;
428446
int err = 0;
429447

430448
if (in_compat_syscall() && cmd != PTP_ENABLE_PPS && cmd != PTP_ENABLE_PPS2)
@@ -474,37 +492,9 @@ long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd,
474492

475493
case PTP_PIN_SETFUNC:
476494
case PTP_PIN_SETFUNC2:
477-
if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) {
478-
err = -EACCES;
479-
break;
480-
}
481-
if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
482-
err = -EFAULT;
483-
break;
484-
}
485-
if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
486-
|| pd.rsv[3] || pd.rsv[4])
487-
&& cmd == PTP_PIN_SETFUNC2) {
488-
err = -EINVAL;
489-
break;
490-
} else if (cmd == PTP_PIN_SETFUNC) {
491-
pd.rsv[0] = 0;
492-
pd.rsv[1] = 0;
493-
pd.rsv[2] = 0;
494-
pd.rsv[3] = 0;
495-
pd.rsv[4] = 0;
496-
}
497-
pin_index = pd.index;
498-
if (pin_index >= ops->n_pins) {
499-
err = -EINVAL;
500-
break;
501-
}
502-
pin_index = array_index_nospec(pin_index, ops->n_pins);
503-
if (mutex_lock_interruptible(&ptp->pincfg_mux))
504-
return -ERESTARTSYS;
505-
err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
506-
mutex_unlock(&ptp->pincfg_mux);
507-
break;
495+
if ((pccontext->fp->f_mode & FMODE_WRITE) == 0)
496+
return -EACCES;
497+
return ptp_pin_setfunc(ptp, cmd, argptr);
508498

509499
case PTP_MASK_CLEAR_ALL:
510500
bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS);

0 commit comments

Comments
 (0)