Commit 8f5de6fb authored by Xabier Marquiegui's avatar Xabier Marquiegui Committed by David S. Miller

ptp: support multiple timestamp event readers

Use linked lists to create one event queue per open file. This enables
simultaneous readers for timestamp event queues.
Signed-off-by: default avatarXabier Marquiegui <reibax@gmail.com>
Suggested-by: default avatarRichard Cochran <richardcochran@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent d26ab5a3
...@@ -103,6 +103,31 @@ int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin, ...@@ -103,6 +103,31 @@ int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode) int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode)
{ {
struct ptp_clock *ptp =
container_of(pccontext->clk, struct ptp_clock, clock);
struct timestamp_event_queue *queue;
queue = kzalloc(sizeof(*queue), GFP_KERNEL);
if (!queue)
return -EINVAL;
spin_lock_init(&queue->lock);
list_add_tail(&queue->qlist, &ptp->tsevqs);
pccontext->private_clkdata = queue;
return 0;
}
int ptp_release(struct posix_clock_context *pccontext)
{
struct timestamp_event_queue *queue = pccontext->private_clkdata;
unsigned long flags;
if (queue) {
pccontext->private_clkdata = NULL;
spin_lock_irqsave(&queue->lock, flags);
list_del(&queue->qlist);
spin_unlock_irqrestore(&queue->lock, flags);
kfree(queue);
}
return 0; return 0;
} }
...@@ -441,10 +466,11 @@ __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp, ...@@ -441,10 +466,11 @@ __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp,
container_of(pccontext->clk, struct ptp_clock, clock); container_of(pccontext->clk, struct ptp_clock, clock);
struct timestamp_event_queue *queue; struct timestamp_event_queue *queue;
poll_wait(fp, &ptp->tsev_wq, wait); queue = pccontext->private_clkdata;
if (!queue)
return EPOLLERR;
/* Extract only the first element in the queue list */ poll_wait(fp, &ptp->tsev_wq, wait);
queue = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue, qlist);
return queue_cnt(queue) ? EPOLLIN : 0; return queue_cnt(queue) ? EPOLLIN : 0;
} }
...@@ -462,36 +488,36 @@ ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags, ...@@ -462,36 +488,36 @@ ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags,
size_t qcnt, i; size_t qcnt, i;
int result; int result;
/* Extract only the first element in the queue list */ queue = pccontext->private_clkdata;
queue = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue, if (!queue) {
qlist); result = -EINVAL;
goto exit;
}
if (cnt % sizeof(struct ptp_extts_event) != 0) if (cnt % sizeof(struct ptp_extts_event) != 0) {
return -EINVAL; result = -EINVAL;
goto exit;
}
if (cnt > EXTTS_BUFSIZE) if (cnt > EXTTS_BUFSIZE)
cnt = EXTTS_BUFSIZE; cnt = EXTTS_BUFSIZE;
cnt = cnt / sizeof(struct ptp_extts_event); cnt = cnt / sizeof(struct ptp_extts_event);
if (mutex_lock_interruptible(&ptp->tsevq_mux))
return -ERESTARTSYS;
if (wait_event_interruptible(ptp->tsev_wq, if (wait_event_interruptible(ptp->tsev_wq,
ptp->defunct || queue_cnt(queue))) { ptp->defunct || queue_cnt(queue))) {
mutex_unlock(&ptp->tsevq_mux);
return -ERESTARTSYS; return -ERESTARTSYS;
} }
if (ptp->defunct) { if (ptp->defunct) {
mutex_unlock(&ptp->tsevq_mux); result = -ENODEV;
return -ENODEV; goto exit;
} }
event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
if (!event) { if (!event) {
mutex_unlock(&ptp->tsevq_mux); result = -ENOMEM;
return -ENOMEM; goto exit;
} }
spin_lock_irqsave(&queue->lock, flags); spin_lock_irqsave(&queue->lock, flags);
...@@ -510,12 +536,16 @@ ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags, ...@@ -510,12 +536,16 @@ ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags,
cnt = cnt * sizeof(struct ptp_extts_event); cnt = cnt * sizeof(struct ptp_extts_event);
mutex_unlock(&ptp->tsevq_mux);
result = cnt; result = cnt;
if (copy_to_user(buf, event, cnt)) if (copy_to_user(buf, event, cnt)) {
result = -EFAULT; result = -EFAULT;
goto free_event;
}
free_event:
kfree(event); kfree(event);
exit:
if (result < 0)
ptp_release(pccontext);
return result; return result;
} }
...@@ -162,6 +162,7 @@ static struct posix_clock_operations ptp_clock_ops = { ...@@ -162,6 +162,7 @@ static struct posix_clock_operations ptp_clock_ops = {
.clock_settime = ptp_clock_settime, .clock_settime = ptp_clock_settime,
.ioctl = ptp_ioctl, .ioctl = ptp_ioctl,
.open = ptp_open, .open = ptp_open,
.release = ptp_release,
.poll = ptp_poll, .poll = ptp_poll,
.read = ptp_read, .read = ptp_read,
}; };
...@@ -174,7 +175,6 @@ static void ptp_clock_release(struct device *dev) ...@@ -174,7 +175,6 @@ static void ptp_clock_release(struct device *dev)
ptp_cleanup_pin_groups(ptp); ptp_cleanup_pin_groups(ptp);
kfree(ptp->vclock_index); kfree(ptp->vclock_index);
mutex_destroy(&ptp->tsevq_mux);
mutex_destroy(&ptp->pincfg_mux); mutex_destroy(&ptp->pincfg_mux);
mutex_destroy(&ptp->n_vclocks_mux); mutex_destroy(&ptp->n_vclocks_mux);
/* Delete first entry */ /* Delete first entry */
...@@ -242,9 +242,8 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ...@@ -242,9 +242,8 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
queue = kzalloc(sizeof(*queue), GFP_KERNEL); queue = kzalloc(sizeof(*queue), GFP_KERNEL);
if (!queue) if (!queue)
goto no_memory_queue; goto no_memory_queue;
spin_lock_init(&queue->lock);
list_add_tail(&queue->qlist, &ptp->tsevqs); list_add_tail(&queue->qlist, &ptp->tsevqs);
mutex_init(&ptp->tsevq_mux); spin_lock_init(&queue->lock);
mutex_init(&ptp->pincfg_mux); mutex_init(&ptp->pincfg_mux);
mutex_init(&ptp->n_vclocks_mux); mutex_init(&ptp->n_vclocks_mux);
init_waitqueue_head(&ptp->tsev_wq); init_waitqueue_head(&ptp->tsev_wq);
...@@ -345,7 +344,6 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ...@@ -345,7 +344,6 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
if (ptp->kworker) if (ptp->kworker)
kthread_destroy_worker(ptp->kworker); kthread_destroy_worker(ptp->kworker);
kworker_err: kworker_err:
mutex_destroy(&ptp->tsevq_mux);
mutex_destroy(&ptp->pincfg_mux); mutex_destroy(&ptp->pincfg_mux);
mutex_destroy(&ptp->n_vclocks_mux); mutex_destroy(&ptp->n_vclocks_mux);
list_del(&queue->qlist); list_del(&queue->qlist);
......
...@@ -38,7 +38,6 @@ struct ptp_clock { ...@@ -38,7 +38,6 @@ struct ptp_clock {
struct pps_device *pps_source; struct pps_device *pps_source;
long dialed_frequency; /* remembers the frequency adjustment */ long dialed_frequency; /* remembers the frequency adjustment */
struct list_head tsevqs; /* timestamp fifo list */ struct list_head tsevqs; /* timestamp fifo list */
struct mutex tsevq_mux; /* one process at a time reading the fifo */
struct mutex pincfg_mux; /* protect concurrent info->pin_config access */ struct mutex pincfg_mux; /* protect concurrent info->pin_config access */
wait_queue_head_t tsev_wq; wait_queue_head_t tsev_wq;
int defunct; /* tells readers to go away when clock is being removed */ int defunct; /* tells readers to go away when clock is being removed */
......
...@@ -81,15 +81,15 @@ static ssize_t extts_fifo_show(struct device *dev, ...@@ -81,15 +81,15 @@ static ssize_t extts_fifo_show(struct device *dev,
size_t qcnt; size_t qcnt;
int cnt = 0; int cnt = 0;
cnt = list_count_nodes(&ptp->tsevqs);
if (cnt <= 0)
goto out;
/* The sysfs fifo will always draw from the fist queue */ /* The sysfs fifo will always draw from the fist queue */
queue = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue, queue = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue,
qlist); qlist);
memset(&event, 0, sizeof(event)); memset(&event, 0, sizeof(event));
if (mutex_lock_interruptible(&ptp->tsevq_mux))
return -ERESTARTSYS;
spin_lock_irqsave(&queue->lock, flags); spin_lock_irqsave(&queue->lock, flags);
qcnt = queue_cnt(queue); qcnt = queue_cnt(queue);
if (qcnt) { if (qcnt) {
...@@ -104,7 +104,6 @@ static ssize_t extts_fifo_show(struct device *dev, ...@@ -104,7 +104,6 @@ static ssize_t extts_fifo_show(struct device *dev,
cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n", cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
event.index, event.t.sec, event.t.nsec); event.index, event.t.sec, event.t.nsec);
out: out:
mutex_unlock(&ptp->tsevq_mux);
return cnt; return cnt;
} }
static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL); static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment