Documentation: configfs examples crash fix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ptp / ptp_chardev.c
bloba8d03aeb40516af8dbba5beb4f18ee8a37af1a71
1 /*
2 * PTP 1588 clock support - character device implementation.
4 * Copyright (C) 2010 OMICRON electronics GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/module.h>
21 #include <linux/posix-clock.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
25 #include "ptp_private.h"
27 int ptp_open(struct posix_clock *pc, fmode_t fmode)
29 return 0;
32 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
34 struct ptp_clock_caps caps;
35 struct ptp_clock_request req;
36 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
37 struct ptp_clock_info *ops = ptp->info;
38 int enable, err = 0;
40 switch (cmd) {
42 case PTP_CLOCK_GETCAPS:
43 memset(&caps, 0, sizeof(caps));
44 caps.max_adj = ptp->info->max_adj;
45 caps.n_alarm = ptp->info->n_alarm;
46 caps.n_ext_ts = ptp->info->n_ext_ts;
47 caps.n_per_out = ptp->info->n_per_out;
48 caps.pps = ptp->info->pps;
49 err = copy_to_user((void __user *)arg, &caps, sizeof(caps));
50 break;
52 case PTP_EXTTS_REQUEST:
53 if (copy_from_user(&req.extts, (void __user *)arg,
54 sizeof(req.extts))) {
55 err = -EFAULT;
56 break;
58 if (req.extts.index >= ops->n_ext_ts) {
59 err = -EINVAL;
60 break;
62 req.type = PTP_CLK_REQ_EXTTS;
63 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
64 err = ops->enable(ops, &req, enable);
65 break;
67 case PTP_PEROUT_REQUEST:
68 if (copy_from_user(&req.perout, (void __user *)arg,
69 sizeof(req.perout))) {
70 err = -EFAULT;
71 break;
73 if (req.perout.index >= ops->n_per_out) {
74 err = -EINVAL;
75 break;
77 req.type = PTP_CLK_REQ_PEROUT;
78 enable = req.perout.period.sec || req.perout.period.nsec;
79 err = ops->enable(ops, &req, enable);
80 break;
82 case PTP_ENABLE_PPS:
83 if (!capable(CAP_SYS_TIME))
84 return -EPERM;
85 req.type = PTP_CLK_REQ_PPS;
86 enable = arg ? 1 : 0;
87 err = ops->enable(ops, &req, enable);
88 break;
90 default:
91 err = -ENOTTY;
92 break;
94 return err;
97 unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
99 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
101 poll_wait(fp, &ptp->tsev_wq, wait);
103 return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
106 ssize_t ptp_read(struct posix_clock *pc,
107 uint rdflags, char __user *buf, size_t cnt)
109 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
110 struct timestamp_event_queue *queue = &ptp->tsevq;
111 struct ptp_extts_event event[PTP_BUF_TIMESTAMPS];
112 unsigned long flags;
113 size_t qcnt, i;
115 if (cnt % sizeof(struct ptp_extts_event) != 0)
116 return -EINVAL;
118 if (cnt > sizeof(event))
119 cnt = sizeof(event);
121 cnt = cnt / sizeof(struct ptp_extts_event);
123 if (mutex_lock_interruptible(&ptp->tsevq_mux))
124 return -ERESTARTSYS;
126 if (wait_event_interruptible(ptp->tsev_wq,
127 ptp->defunct || queue_cnt(queue))) {
128 mutex_unlock(&ptp->tsevq_mux);
129 return -ERESTARTSYS;
132 if (ptp->defunct)
133 return -ENODEV;
135 spin_lock_irqsave(&queue->lock, flags);
137 qcnt = queue_cnt(queue);
139 if (cnt > qcnt)
140 cnt = qcnt;
142 for (i = 0; i < cnt; i++) {
143 event[i] = queue->buf[queue->head];
144 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
147 spin_unlock_irqrestore(&queue->lock, flags);
149 cnt = cnt * sizeof(struct ptp_extts_event);
151 mutex_unlock(&ptp->tsevq_mux);
153 if (copy_to_user(buf, event, cnt)) {
154 mutex_unlock(&ptp->tsevq_mux);
155 return -EFAULT;
158 return cnt;