blob: 76776571ed91727cd09a35cff776de921d53316e [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
2 comedi/comedi_fops.c
3 comedi kernel module
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23
24#undef DEBUG
25
26#define __NO_VERSION__
27#include "comedi_fops.h"
28#include "comedi_compat32.h"
29
30#include <linux/module.h>
31#include <linux/errno.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/fcntl.h>
35#include <linux/delay.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
38#include <linux/slab.h>
39#include <linux/kmod.h>
40#include <linux/poll.h>
41#include <linux/init.h>
42#include <linux/device.h>
43#include <linux/vmalloc.h>
44#include <linux/fs.h>
45#include "comedidev.h"
46#include <linux/cdev.h>
Frank Mori Hess883db3d2009-04-14 11:21:41 -040047#include <linux/stat.h>
David Schleefed9eccb2008-11-04 20:29:31 -080048
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080049#include <linux/io.h>
50#include <linux/uaccess.h>
David Schleefed9eccb2008-11-04 20:29:31 -080051
Greg Kroah-Hartman242e7ad2010-05-03 15:20:29 -070052#include "internal.h"
David Schleefed9eccb2008-11-04 20:29:31 -080053
54MODULE_AUTHOR("http://www.comedi.org");
55MODULE_DESCRIPTION("Comedi core module");
56MODULE_LICENSE("GPL");
57
58#ifdef CONFIG_COMEDI_DEBUG
59int comedi_debug;
Greg Kroah-Hartman18736432010-05-01 12:02:23 -070060EXPORT_SYMBOL(comedi_debug);
Ian Abbott4d7df822012-04-13 14:12:53 +010061module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
62MODULE_PARM_DESC(comedi_debug,
63 "enable comedi core and driver debugging if non-zero (default 0)"
64 );
David Schleefed9eccb2008-11-04 20:29:31 -080065#endif
66
Rusty Russell90ab5ee2012-01-13 09:32:20 +103067bool comedi_autoconfig = 1;
Ian Abbott4d7df822012-04-13 14:12:53 +010068module_param(comedi_autoconfig, bool, S_IRUGO);
69MODULE_PARM_DESC(comedi_autoconfig,
70 "enable drivers to auto-configure comedi devices (default 1)");
Ian Abbott6a9d7a22008-12-08 17:05:50 +000071
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070072static int comedi_num_legacy_minors;
Ian Abbott4d7df822012-04-13 14:12:53 +010073module_param(comedi_num_legacy_minors, int, S_IRUGO);
74MODULE_PARM_DESC(comedi_num_legacy_minors,
75 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
76 );
77
Ian Abbott234bb3c2012-04-13 14:12:54 +010078unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
Ian Abbott4d7df822012-04-13 14:12:53 +010079module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
80MODULE_PARM_DESC(comedi_default_buf_size_kb,
81 "default asynchronous buffer size in KiB (default "
Ian Abbott234bb3c2012-04-13 14:12:54 +010082 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
Ian Abbott4d7df822012-04-13 14:12:53 +010083
Ian Abbott234bb3c2012-04-13 14:12:54 +010084unsigned int comedi_default_buf_maxsize_kb
85 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
Ian Abbott4d7df822012-04-13 14:12:53 +010086module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
87MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
88 "default maximum size of asynchronous buffer in KiB (default "
Ian Abbott234bb3c2012-04-13 14:12:54 +010089 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
Bernd Porr1dd33ab2008-12-08 23:30:13 +000090
David Schleefed9eccb2008-11-04 20:29:31 -080091static DEFINE_SPINLOCK(comedi_file_info_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -080092static struct comedi_device_file_info
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053093*comedi_file_info_table[COMEDI_NUM_MINORS];
David Schleefed9eccb2008-11-04 20:29:31 -080094
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053095static int do_devconfig_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -070096 struct comedi_devconfig __user *arg);
97static int do_bufconfig_ioctl(struct comedi_device *dev,
98 struct comedi_bufconfig __user *arg);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053099static int do_devinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700100 struct comedi_devinfo __user *arg,
101 struct file *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530102static int do_subdinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700103 struct comedi_subdinfo __user *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530104static int do_chaninfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700105 struct comedi_chaninfo __user *arg);
106static int do_bufinfo_ioctl(struct comedi_device *dev,
Ian Abbott53fa8272010-05-19 18:09:50 +0100107 struct comedi_bufinfo __user *arg, void *file);
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700108static int do_cmd_ioctl(struct comedi_device *dev,
109 struct comedi_cmd __user *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530110static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
111 void *file);
112static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
113 void *file);
114static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
115 void *file);
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700116static int do_cmdtest_ioctl(struct comedi_device *dev,
117 struct comedi_cmd __user *arg, void *file);
118static int do_insnlist_ioctl(struct comedi_device *dev,
119 struct comedi_insnlist __user *arg, void *file);
120static int do_insn_ioctl(struct comedi_device *dev,
121 struct comedi_insn __user *arg, void *file);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530122static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
123 void *file);
David Schleefed9eccb2008-11-04 20:29:31 -0800124
Mark Pearsonf5283a42011-07-10 21:16:38 +0200125static void do_become_nonbusy(struct comedi_device *dev,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530126 struct comedi_subdevice *s);
Bill Pemberton34c43922009-03-16 22:05:14 -0400127static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
David Schleefed9eccb2008-11-04 20:29:31 -0800128
129static int comedi_fasync(int fd, struct file *file, int on);
130
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400131static int is_device_busy(struct comedi_device *dev);
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700132
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400133static int resize_async_buffer(struct comedi_device *dev,
134 struct comedi_subdevice *s,
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700135 struct comedi_async *async, unsigned new_size)
136{
137 int retval;
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400138
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700139 if (new_size > async->max_bufsize)
140 return -EPERM;
141
142 if (s->busy) {
143 DPRINTK("subdevice is busy, cannot resize buffer\n");
144 return -EBUSY;
145 }
146 if (async->mmap_count) {
147 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
148 return -EBUSY;
149 }
150
151 if (!async->prealloc_buf)
152 return -EINVAL;
153
154 /* make sure buffer is an integral number of pages
155 * (we round up) */
156 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
157
158 retval = comedi_buf_alloc(dev, s, new_size);
159 if (retval < 0)
160 return retval;
161
162 if (s->buf_change) {
163 retval = s->buf_change(dev, s, new_size);
164 if (retval < 0)
165 return retval;
166 }
167
168 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
169 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
170 return 0;
171}
172
173/* sysfs attribute files */
174
175static const unsigned bytes_per_kibi = 1024;
176
177static ssize_t show_max_read_buffer_kb(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
180 ssize_t retval;
181 struct comedi_device_file_info *info = dev_get_drvdata(dev);
182 unsigned max_buffer_size_kb = 0;
183 struct comedi_subdevice *const read_subdevice =
184 comedi_get_read_subdevice(info);
185
186 mutex_lock(&info->device->mutex);
187 if (read_subdevice &&
188 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
189 read_subdevice->async) {
190 max_buffer_size_kb = read_subdevice->async->max_bufsize /
191 bytes_per_kibi;
192 }
193 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
194 mutex_unlock(&info->device->mutex);
195
196 return retval;
197}
198
199static ssize_t store_max_read_buffer_kb(struct device *dev,
200 struct device_attribute *attr,
201 const char *buf, size_t count)
202{
203 struct comedi_device_file_info *info = dev_get_drvdata(dev);
204 unsigned int new_max_size_kb;
205 unsigned int new_max_size;
206 int ret;
207 struct comedi_subdevice *const read_subdevice =
208 comedi_get_read_subdevice(info);
209
210 ret = kstrtouint(buf, 10, &new_max_size_kb);
211 if (ret)
212 return ret;
213 if (new_max_size_kb > (UINT_MAX / bytes_per_kibi))
214 return -EINVAL;
215 new_max_size = new_max_size_kb * bytes_per_kibi;
216
217 mutex_lock(&info->device->mutex);
218 if (read_subdevice == NULL ||
219 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
220 read_subdevice->async == NULL) {
221 mutex_unlock(&info->device->mutex);
222 return -EINVAL;
223 }
224 read_subdevice->async->max_bufsize = new_max_size;
225 mutex_unlock(&info->device->mutex);
226
227 return count;
228}
229
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700230static ssize_t show_read_buffer_kb(struct device *dev,
231 struct device_attribute *attr, char *buf)
232{
233 ssize_t retval;
234 struct comedi_device_file_info *info = dev_get_drvdata(dev);
235 unsigned buffer_size_kb = 0;
236 struct comedi_subdevice *const read_subdevice =
237 comedi_get_read_subdevice(info);
238
239 mutex_lock(&info->device->mutex);
240 if (read_subdevice &&
241 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
242 read_subdevice->async) {
243 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
244 bytes_per_kibi;
245 }
246 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
247 mutex_unlock(&info->device->mutex);
248
249 return retval;
250}
251
252static ssize_t store_read_buffer_kb(struct device *dev,
253 struct device_attribute *attr,
254 const char *buf, size_t count)
255{
256 struct comedi_device_file_info *info = dev_get_drvdata(dev);
257 unsigned int new_size_kb;
258 unsigned int new_size;
259 int retval;
260 int ret;
261 struct comedi_subdevice *const read_subdevice =
262 comedi_get_read_subdevice(info);
263
264 ret = kstrtouint(buf, 10, &new_size_kb);
265 if (ret)
266 return ret;
267 if (new_size_kb > (UINT_MAX / bytes_per_kibi))
268 return -EINVAL;
269 new_size = new_size_kb * bytes_per_kibi;
270
271 mutex_lock(&info->device->mutex);
272 if (read_subdevice == NULL ||
273 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
274 read_subdevice->async == NULL) {
275 mutex_unlock(&info->device->mutex);
276 return -EINVAL;
277 }
278 retval = resize_async_buffer(info->device, read_subdevice,
279 read_subdevice->async, new_size);
280 mutex_unlock(&info->device->mutex);
281
282 if (retval < 0)
283 return retval;
284 return count;
285}
286
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700287static ssize_t show_max_write_buffer_kb(struct device *dev,
288 struct device_attribute *attr,
289 char *buf)
290{
291 ssize_t retval;
292 struct comedi_device_file_info *info = dev_get_drvdata(dev);
293 unsigned max_buffer_size_kb = 0;
294 struct comedi_subdevice *const write_subdevice =
295 comedi_get_write_subdevice(info);
296
297 mutex_lock(&info->device->mutex);
298 if (write_subdevice &&
299 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
300 write_subdevice->async) {
301 max_buffer_size_kb = write_subdevice->async->max_bufsize /
302 bytes_per_kibi;
303 }
304 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
305 mutex_unlock(&info->device->mutex);
306
307 return retval;
308}
309
310static ssize_t store_max_write_buffer_kb(struct device *dev,
311 struct device_attribute *attr,
312 const char *buf, size_t count)
313{
314 struct comedi_device_file_info *info = dev_get_drvdata(dev);
315 unsigned int new_max_size_kb;
316 unsigned int new_max_size;
317 int ret;
318 struct comedi_subdevice *const write_subdevice =
319 comedi_get_write_subdevice(info);
320
321 ret = kstrtouint(buf, 10, &new_max_size_kb);
322 if (ret)
323 return ret;
324 if (new_max_size_kb > (UINT_MAX / bytes_per_kibi))
325 return -EINVAL;
326 new_max_size = new_max_size_kb * bytes_per_kibi;
327
328 mutex_lock(&info->device->mutex);
329 if (write_subdevice == NULL ||
330 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
331 write_subdevice->async == NULL) {
332 mutex_unlock(&info->device->mutex);
333 return -EINVAL;
334 }
335 write_subdevice->async->max_bufsize = new_max_size;
336 mutex_unlock(&info->device->mutex);
337
338 return count;
339}
340
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700341static ssize_t show_write_buffer_kb(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
344 ssize_t retval;
345 struct comedi_device_file_info *info = dev_get_drvdata(dev);
346 unsigned buffer_size_kb = 0;
347 struct comedi_subdevice *const write_subdevice =
348 comedi_get_write_subdevice(info);
349
350 mutex_lock(&info->device->mutex);
351 if (write_subdevice &&
352 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
353 write_subdevice->async) {
354 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
355 bytes_per_kibi;
356 }
357 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
358 mutex_unlock(&info->device->mutex);
359
360 return retval;
361}
362
363static ssize_t store_write_buffer_kb(struct device *dev,
364 struct device_attribute *attr,
365 const char *buf, size_t count)
366{
367 struct comedi_device_file_info *info = dev_get_drvdata(dev);
368 unsigned int new_size_kb;
369 unsigned int new_size;
370 int retval;
371 int ret;
372 struct comedi_subdevice *const write_subdevice =
373 comedi_get_write_subdevice(info);
374
375 ret = kstrtouint(buf, 10, &new_size_kb);
376 if (ret)
377 return ret;
378 if (new_size_kb > (UINT_MAX / bytes_per_kibi))
379 return -EINVAL;
380 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
381
382 mutex_lock(&info->device->mutex);
383 if (write_subdevice == NULL ||
384 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
385 write_subdevice->async == NULL) {
386 mutex_unlock(&info->device->mutex);
387 return -EINVAL;
388 }
389 retval = resize_async_buffer(info->device, write_subdevice,
390 write_subdevice->async, new_size);
391 mutex_unlock(&info->device->mutex);
392
393 if (retval < 0)
394 return retval;
395 return count;
396}
397
H Hartley Sweetenfb603672012-05-10 18:05:28 -0700398static struct device_attribute comedi_dev_attrs[] = {
399 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
400 show_max_read_buffer_kb, store_max_read_buffer_kb),
401 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
402 show_read_buffer_kb, store_read_buffer_kb),
403 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
404 show_max_write_buffer_kb, store_max_write_buffer_kb),
405 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
406 show_write_buffer_kb, store_write_buffer_kb),
407 __ATTR_NULL
H Hartley Sweetena5011a22012-05-09 09:20:08 -0700408};
David Schleefed9eccb2008-11-04 20:29:31 -0800409
David Schleefed9eccb2008-11-04 20:29:31 -0800410static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800411 unsigned long arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800412{
413 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800414 struct comedi_device_file_info *dev_file_info =
415 comedi_get_device_file_info(minor);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400416 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800417 int rc;
418
Frank Mori Hess53b670a2008-12-15 13:48:47 +0000419 if (dev_file_info == NULL || dev_file_info->device == NULL)
420 return -ENODEV;
421 dev = dev_file_info->device;
422
David Schleefed9eccb2008-11-04 20:29:31 -0800423 mutex_lock(&dev->mutex);
424
425 /* Device config is special, because it must work on
426 * an unconfigured device. */
427 if (cmd == COMEDI_DEVCONFIG) {
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700428 rc = do_devconfig_ioctl(dev,
429 (struct comedi_devconfig __user *)arg);
David Schleefed9eccb2008-11-04 20:29:31 -0800430 goto done;
431 }
432
433 if (!dev->attached) {
434 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
435 rc = -ENODEV;
436 goto done;
437 }
438
439 switch (cmd) {
440 case COMEDI_BUFCONFIG:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700441 rc = do_bufconfig_ioctl(dev,
442 (struct comedi_bufconfig __user *)arg);
David Schleefed9eccb2008-11-04 20:29:31 -0800443 break;
444 case COMEDI_DEVINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700445 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
446 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800447 break;
448 case COMEDI_SUBDINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700449 rc = do_subdinfo_ioctl(dev,
450 (struct comedi_subdinfo __user *)arg,
451 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800452 break;
453 case COMEDI_CHANINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700454 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
David Schleefed9eccb2008-11-04 20:29:31 -0800455 break;
456 case COMEDI_RANGEINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700457 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
David Schleefed9eccb2008-11-04 20:29:31 -0800458 break;
459 case COMEDI_BUFINFO:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700460 rc = do_bufinfo_ioctl(dev,
Ian Abbott53fa8272010-05-19 18:09:50 +0100461 (struct comedi_bufinfo __user *)arg,
462 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800463 break;
464 case COMEDI_LOCK:
465 rc = do_lock_ioctl(dev, arg, file);
466 break;
467 case COMEDI_UNLOCK:
468 rc = do_unlock_ioctl(dev, arg, file);
469 break;
470 case COMEDI_CANCEL:
471 rc = do_cancel_ioctl(dev, arg, file);
472 break;
473 case COMEDI_CMD:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700474 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
David Schleefed9eccb2008-11-04 20:29:31 -0800475 break;
476 case COMEDI_CMDTEST:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700477 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
478 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800479 break;
480 case COMEDI_INSNLIST:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700481 rc = do_insnlist_ioctl(dev,
482 (struct comedi_insnlist __user *)arg,
483 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800484 break;
485 case COMEDI_INSN:
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700486 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
487 file);
David Schleefed9eccb2008-11-04 20:29:31 -0800488 break;
489 case COMEDI_POLL:
490 rc = do_poll_ioctl(dev, arg, file);
491 break;
492 default:
493 rc = -ENOTTY;
494 break;
495 }
496
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800497done:
David Schleefed9eccb2008-11-04 20:29:31 -0800498 mutex_unlock(&dev->mutex);
499 return rc;
500}
501
502/*
503 COMEDI_DEVCONFIG
504 device config ioctl
505
506 arg:
507 pointer to devconfig structure
508
509 reads:
510 devconfig structure at arg
511
512 writes:
513 none
514*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530515static int do_devconfig_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700516 struct comedi_devconfig __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800517{
Bill Pemberton0707bb02009-03-16 22:06:20 -0400518 struct comedi_devconfig it;
David Schleefed9eccb2008-11-04 20:29:31 -0800519 int ret;
520 unsigned char *aux_data = NULL;
521 int aux_len;
522
523 if (!capable(CAP_SYS_ADMIN))
524 return -EPERM;
525
526 if (arg == NULL) {
527 if (is_device_busy(dev))
528 return -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800529 if (dev->attached) {
David Schleefed9eccb2008-11-04 20:29:31 -0800530 struct module *driver_module = dev->driver->module;
531 comedi_device_detach(dev);
532 module_put(driver_module);
533 }
534 return 0;
535 }
536
Bill Pemberton0707bb02009-03-16 22:06:20 -0400537 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800538 return -EFAULT;
539
540 it.board_name[COMEDI_NAMELEN - 1] = 0;
541
542 if (comedi_aux_data(it.options, 0) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800543 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
David Schleefed9eccb2008-11-04 20:29:31 -0800544 int bit_shift;
545 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
546 if (aux_len < 0)
547 return -EFAULT;
548
549 aux_data = vmalloc(aux_len);
550 if (!aux_data)
551 return -ENOMEM;
552
553 if (copy_from_user(aux_data,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800554 comedi_aux_data(it.options, 0), aux_len)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800555 vfree(aux_data);
556 return -EFAULT;
557 }
558 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800559 (unsigned long)aux_data;
David Schleefed9eccb2008-11-04 20:29:31 -0800560 if (sizeof(void *) > sizeof(int)) {
561 bit_shift = sizeof(int) * 8;
562 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800563 ((unsigned long)aux_data) >> bit_shift;
David Schleefed9eccb2008-11-04 20:29:31 -0800564 } else
565 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
566 }
567
568 ret = comedi_device_attach(dev, &it);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800569 if (ret == 0) {
570 if (!try_module_get(dev->driver->module)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800571 comedi_device_detach(dev);
Julia Lawallabae41e2012-04-22 13:37:09 +0200572 ret = -ENOSYS;
David Schleefed9eccb2008-11-04 20:29:31 -0800573 }
574 }
575
576 if (aux_data)
577 vfree(aux_data);
578
579 return ret;
580}
581
582/*
583 COMEDI_BUFCONFIG
584 buffer configuration ioctl
585
586 arg:
587 pointer to bufconfig structure
588
589 reads:
590 bufconfig at arg
591
592 writes:
593 modified bufconfig at arg
594
595*/
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700596static int do_bufconfig_ioctl(struct comedi_device *dev,
597 struct comedi_bufconfig __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800598{
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400599 struct comedi_bufconfig bc;
Bill Pembertond1636792009-03-16 22:05:20 -0400600 struct comedi_async *async;
Bill Pemberton34c43922009-03-16 22:05:14 -0400601 struct comedi_subdevice *s;
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400602 int retval = 0;
David Schleefed9eccb2008-11-04 20:29:31 -0800603
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400604 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800605 return -EFAULT;
606
607 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
608 return -EINVAL;
609
610 s = dev->subdevices + bc.subdevice;
611 async = s->async;
612
613 if (!async) {
614 DPRINTK("subdevice does not have async capability\n");
615 bc.size = 0;
616 bc.maximum_size = 0;
617 goto copyback;
618 }
619
620 if (bc.maximum_size) {
621 if (!capable(CAP_SYS_ADMIN))
622 return -EPERM;
623
624 async->max_bufsize = bc.maximum_size;
625 }
626
627 if (bc.size) {
Frank Mori Hess883db3d2009-04-14 11:21:41 -0400628 retval = resize_async_buffer(dev, s, async, bc.size);
629 if (retval < 0)
630 return retval;
David Schleefed9eccb2008-11-04 20:29:31 -0800631 }
632
633 bc.size = async->prealloc_bufsz;
634 bc.maximum_size = async->max_bufsize;
635
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800636copyback:
Bill Pembertonbe6aba42009-03-16 22:06:37 -0400637 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
David Schleefed9eccb2008-11-04 20:29:31 -0800638 return -EFAULT;
639
640 return 0;
641}
642
643/*
644 COMEDI_DEVINFO
645 device info ioctl
646
647 arg:
648 pointer to devinfo structure
649
650 reads:
651 none
652
653 writes:
654 devinfo structure
655
656*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530657static int do_devinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700658 struct comedi_devinfo __user *arg,
659 struct file *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800660{
Bill Pemberton063db042009-03-16 22:06:15 -0400661 struct comedi_devinfo devinfo;
David Schleefed9eccb2008-11-04 20:29:31 -0800662 const unsigned minor = iminor(file->f_dentry->d_inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800663 struct comedi_device_file_info *dev_file_info =
664 comedi_get_device_file_info(minor);
Bill Pemberton34c43922009-03-16 22:05:14 -0400665 struct comedi_subdevice *read_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800666 comedi_get_read_subdevice(dev_file_info);
Bill Pemberton34c43922009-03-16 22:05:14 -0400667 struct comedi_subdevice *write_subdev =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800668 comedi_get_write_subdevice(dev_file_info);
David Schleefed9eccb2008-11-04 20:29:31 -0800669
670 memset(&devinfo, 0, sizeof(devinfo));
671
672 /* fill devinfo structure */
673 devinfo.version_code = COMEDI_VERSION_CODE;
674 devinfo.n_subdevs = dev->n_subdevices;
Vasiliy Kulikov819cbb12011-06-26 12:56:22 +0400675 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
676 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
David Schleefed9eccb2008-11-04 20:29:31 -0800677
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800678 if (read_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800679 devinfo.read_subdevice = read_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800680 else
David Schleefed9eccb2008-11-04 20:29:31 -0800681 devinfo.read_subdevice = -1;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800682
683 if (write_subdev)
David Schleefed9eccb2008-11-04 20:29:31 -0800684 devinfo.write_subdevice = write_subdev - dev->subdevices;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800685 else
David Schleefed9eccb2008-11-04 20:29:31 -0800686 devinfo.write_subdevice = -1;
David Schleefed9eccb2008-11-04 20:29:31 -0800687
Bill Pemberton063db042009-03-16 22:06:15 -0400688 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800689 return -EFAULT;
690
691 return 0;
692}
693
694/*
695 COMEDI_SUBDINFO
696 subdevice info ioctl
697
698 arg:
699 pointer to array of subdevice info structures
700
701 reads:
702 none
703
704 writes:
705 array of subdevice info structures at arg
706
707*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530708static int do_subdinfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700709 struct comedi_subdinfo __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800710{
711 int ret, i;
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400712 struct comedi_subdinfo *tmp, *us;
Bill Pemberton34c43922009-03-16 22:05:14 -0400713 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -0800714
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530715 tmp =
716 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
717 GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800718 if (!tmp)
719 return -ENOMEM;
720
721 /* fill subdinfo structs */
722 for (i = 0; i < dev->n_subdevices; i++) {
723 s = dev->subdevices + i;
724 us = tmp + i;
725
726 us->type = s->type;
727 us->n_chan = s->n_chan;
728 us->subd_flags = s->subdev_flags;
729 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
730 us->subd_flags |= SDF_RUNNING;
731#define TIMER_nanosec 5 /* backwards compatibility */
732 us->timer_type = TIMER_nanosec;
733 us->len_chanlist = s->len_chanlist;
734 us->maxdata = s->maxdata;
735 if (s->range_table) {
736 us->range_type =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800737 (i << 24) | (0 << 16) | (s->range_table->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800738 } else {
739 us->range_type = 0; /* XXX */
740 }
741 us->flags = s->flags;
742
743 if (s->busy)
744 us->subd_flags |= SDF_BUSY;
745 if (s->busy == file)
746 us->subd_flags |= SDF_BUSY_OWNER;
747 if (s->lock)
748 us->subd_flags |= SDF_LOCKED;
749 if (s->lock == file)
750 us->subd_flags |= SDF_LOCK_OWNER;
751 if (!s->maxdata && s->maxdata_list)
752 us->subd_flags |= SDF_MAXDATA;
753 if (s->flaglist)
754 us->subd_flags |= SDF_FLAGS;
755 if (s->range_table_list)
756 us->subd_flags |= SDF_RANGETYPE;
757 if (s->do_cmd)
758 us->subd_flags |= SDF_CMD;
759
760 if (s->insn_bits != &insn_inval)
761 us->insn_bits_support = COMEDI_SUPPORTED;
762 else
763 us->insn_bits_support = COMEDI_UNSUPPORTED;
764
765 us->settling_time_0 = s->settling_time_0;
766 }
767
768 ret = copy_to_user(arg, tmp,
Bill Pembertonbd52efb2009-03-16 22:06:09 -0400769 dev->n_subdevices * sizeof(struct comedi_subdinfo));
David Schleefed9eccb2008-11-04 20:29:31 -0800770
771 kfree(tmp);
772
773 return ret ? -EFAULT : 0;
774}
775
776/*
777 COMEDI_CHANINFO
778 subdevice info ioctl
779
780 arg:
781 pointer to chaninfo structure
782
783 reads:
784 chaninfo structure at arg
785
786 writes:
787 arrays at elements of chaninfo structure
788
789*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530790static int do_chaninfo_ioctl(struct comedi_device *dev,
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700791 struct comedi_chaninfo __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -0800792{
Bill Pemberton34c43922009-03-16 22:05:14 -0400793 struct comedi_subdevice *s;
Bill Pembertona18b4162009-03-16 22:06:04 -0400794 struct comedi_chaninfo it;
David Schleefed9eccb2008-11-04 20:29:31 -0800795
Bill Pembertona18b4162009-03-16 22:06:04 -0400796 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800797 return -EFAULT;
798
799 if (it.subdev >= dev->n_subdevices)
800 return -EINVAL;
801 s = dev->subdevices + it.subdev;
802
803 if (it.maxdata_list) {
804 if (s->maxdata || !s->maxdata_list)
805 return -EINVAL;
806 if (copy_to_user(it.maxdata_list, s->maxdata_list,
Bill Pemberton790c5542009-03-16 22:05:02 -0400807 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800808 return -EFAULT;
809 }
810
811 if (it.flaglist) {
812 if (!s->flaglist)
813 return -EINVAL;
814 if (copy_to_user(it.flaglist, s->flaglist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800815 s->n_chan * sizeof(unsigned int)))
David Schleefed9eccb2008-11-04 20:29:31 -0800816 return -EFAULT;
817 }
818
819 if (it.rangelist) {
820 int i;
821
822 if (!s->range_table_list)
823 return -EINVAL;
824 for (i = 0; i < s->n_chan; i++) {
825 int x;
826
827 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800828 (s->range_table_list[i]->length);
Vasiliy Kulikov81604d42010-09-05 22:32:33 +0400829 if (put_user(x, it.rangelist + i))
830 return -EFAULT;
David Schleefed9eccb2008-11-04 20:29:31 -0800831 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800832#if 0
833 if (copy_to_user(it.rangelist, s->range_type_list,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530834 s->n_chan * sizeof(unsigned int)))
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800835 return -EFAULT;
836#endif
David Schleefed9eccb2008-11-04 20:29:31 -0800837 }
838
839 return 0;
840}
841
842 /*
843 COMEDI_BUFINFO
844 buffer information ioctl
845
846 arg:
847 pointer to bufinfo structure
848
849 reads:
850 bufinfo at arg
851
852 writes:
853 modified bufinfo at arg
854
855 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700856static int do_bufinfo_ioctl(struct comedi_device *dev,
Ian Abbott53fa8272010-05-19 18:09:50 +0100857 struct comedi_bufinfo __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800858{
Bill Pemberton9aa53392009-03-16 22:06:42 -0400859 struct comedi_bufinfo bi;
Bill Pemberton34c43922009-03-16 22:05:14 -0400860 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400861 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800862
Bill Pemberton9aa53392009-03-16 22:06:42 -0400863 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800864 return -EFAULT;
865
866 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
867 return -EINVAL;
868
869 s = dev->subdevices + bi.subdevice;
Ian Abbott53fa8272010-05-19 18:09:50 +0100870
871 if (s->lock && s->lock != file)
872 return -EACCES;
873
David Schleefed9eccb2008-11-04 20:29:31 -0800874 async = s->async;
875
876 if (!async) {
877 DPRINTK("subdevice does not have async capability\n");
878 bi.buf_write_ptr = 0;
879 bi.buf_read_ptr = 0;
880 bi.buf_write_count = 0;
881 bi.buf_read_count = 0;
Ian Abbott4772c012010-05-19 18:09:49 +0100882 bi.bytes_read = 0;
883 bi.bytes_written = 0;
David Schleefed9eccb2008-11-04 20:29:31 -0800884 goto copyback;
885 }
Ian Abbott53fa8272010-05-19 18:09:50 +0100886 if (!s->busy) {
887 bi.bytes_read = 0;
888 bi.bytes_written = 0;
889 goto copyback_position;
890 }
891 if (s->busy != file)
892 return -EACCES;
David Schleefed9eccb2008-11-04 20:29:31 -0800893
894 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
895 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
896 comedi_buf_read_free(async, bi.bytes_read);
897
898 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800899 SRF_RUNNING))
900 && async->buf_write_count == async->buf_read_count) {
David Schleefed9eccb2008-11-04 20:29:31 -0800901 do_become_nonbusy(dev, s);
902 }
903 }
904
905 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
906 bi.bytes_written =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800907 comedi_buf_write_alloc(async, bi.bytes_written);
David Schleefed9eccb2008-11-04 20:29:31 -0800908 comedi_buf_write_free(async, bi.bytes_written);
909 }
910
Ian Abbott53fa8272010-05-19 18:09:50 +0100911copyback_position:
David Schleefed9eccb2008-11-04 20:29:31 -0800912 bi.buf_write_count = async->buf_write_count;
913 bi.buf_write_ptr = async->buf_write_ptr;
914 bi.buf_read_count = async->buf_read_count;
915 bi.buf_read_ptr = async->buf_read_ptr;
916
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -0800917copyback:
Bill Pemberton9aa53392009-03-16 22:06:42 -0400918 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -0800919 return -EFAULT;
920
921 return 0;
922}
923
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530924static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
925 unsigned int *data, void *file);
David Schleefed9eccb2008-11-04 20:29:31 -0800926/*
Pieter De Praetere20617f22010-03-10 09:47:44 +0100927 * COMEDI_INSNLIST
928 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -0800929 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100930 * arg:
931 * pointer to sync cmd structure
David Schleefed9eccb2008-11-04 20:29:31 -0800932 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100933 * reads:
934 * sync cmd struct at arg
935 * instruction list
936 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -0800937 *
Pieter De Praetere20617f22010-03-10 09:47:44 +0100938 * writes:
939 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -0800940 */
941/* arbitrary limits */
942#define MAX_SAMPLES 256
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -0700943static int do_insnlist_ioctl(struct comedi_device *dev,
944 struct comedi_insnlist __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -0800945{
Bill Pembertonda613f42009-03-16 22:05:59 -0400946 struct comedi_insnlist insnlist;
Bill Pemberton90035c02009-03-16 22:05:53 -0400947 struct comedi_insn *insns = NULL;
Bill Pemberton790c5542009-03-16 22:05:02 -0400948 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800949 int i = 0;
950 int ret = 0;
951
Bill Pembertonda613f42009-03-16 22:05:59 -0400952 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
David Schleefed9eccb2008-11-04 20:29:31 -0800953 return -EFAULT;
954
Bill Pemberton790c5542009-03-16 22:05:02 -0400955 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800956 if (!data) {
957 DPRINTK("kmalloc failed\n");
958 ret = -ENOMEM;
959 goto error;
960 }
961
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530962 insns =
Xi Wangdfd8ee92011-11-25 16:46:51 -0500963 kcalloc(insnlist.n_insns, sizeof(struct comedi_insn), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800964 if (!insns) {
965 DPRINTK("kmalloc failed\n");
966 ret = -ENOMEM;
967 goto error;
968 }
969
970 if (copy_from_user(insns, insnlist.insns,
Bill Pemberton90035c02009-03-16 22:05:53 -0400971 sizeof(struct comedi_insn) * insnlist.n_insns)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800972 DPRINTK("copy_from_user failed\n");
973 ret = -EFAULT;
974 goto error;
975 }
976
977 for (i = 0; i < insnlist.n_insns; i++) {
978 if (insns[i].n > MAX_SAMPLES) {
979 DPRINTK("number of samples too large\n");
980 ret = -EINVAL;
981 goto error;
982 }
983 if (insns[i].insn & INSN_MASK_WRITE) {
984 if (copy_from_user(data, insns[i].data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400985 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800986 DPRINTK("copy_from_user failed\n");
987 ret = -EFAULT;
988 goto error;
989 }
990 }
991 ret = parse_insn(dev, insns + i, data, file);
992 if (ret < 0)
993 goto error;
994 if (insns[i].insn & INSN_MASK_READ) {
995 if (copy_to_user(insns[i].data, data,
Bill Pemberton790c5542009-03-16 22:05:02 -0400996 insns[i].n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -0800997 DPRINTK("copy_to_user failed\n");
998 ret = -EFAULT;
999 goto error;
1000 }
1001 }
1002 if (need_resched())
1003 schedule();
1004 }
1005
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001006error:
1007 kfree(insns);
1008 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -08001009
1010 if (ret < 0)
1011 return ret;
1012 return i;
1013}
1014
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301015static int check_insn_config_length(struct comedi_insn *insn,
1016 unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -08001017{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001018 if (insn->n < 1)
1019 return -EINVAL;
David Schleefed9eccb2008-11-04 20:29:31 -08001020
1021 switch (data[0]) {
1022 case INSN_CONFIG_DIO_OUTPUT:
1023 case INSN_CONFIG_DIO_INPUT:
1024 case INSN_CONFIG_DISARM:
1025 case INSN_CONFIG_RESET:
1026 if (insn->n == 1)
1027 return 0;
1028 break;
1029 case INSN_CONFIG_ARM:
1030 case INSN_CONFIG_DIO_QUERY:
1031 case INSN_CONFIG_BLOCK_SIZE:
1032 case INSN_CONFIG_FILTER:
1033 case INSN_CONFIG_SERIAL_CLOCK:
1034 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1035 case INSN_CONFIG_ALT_SOURCE:
1036 case INSN_CONFIG_SET_COUNTER_MODE:
1037 case INSN_CONFIG_8254_READ_STATUS:
1038 case INSN_CONFIG_SET_ROUTING:
1039 case INSN_CONFIG_GET_ROUTING:
1040 case INSN_CONFIG_GET_PWM_STATUS:
1041 case INSN_CONFIG_PWM_SET_PERIOD:
1042 case INSN_CONFIG_PWM_GET_PERIOD:
1043 if (insn->n == 2)
1044 return 0;
1045 break;
1046 case INSN_CONFIG_SET_GATE_SRC:
1047 case INSN_CONFIG_GET_GATE_SRC:
1048 case INSN_CONFIG_SET_CLOCK_SRC:
1049 case INSN_CONFIG_GET_CLOCK_SRC:
1050 case INSN_CONFIG_SET_OTHER_SRC:
1051 case INSN_CONFIG_GET_COUNTER_STATUS:
1052 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1053 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1054 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1055 if (insn->n == 3)
1056 return 0;
1057 break;
1058 case INSN_CONFIG_PWM_OUTPUT:
1059 case INSN_CONFIG_ANALOG_TRIG:
1060 if (insn->n == 5)
1061 return 0;
1062 break;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301063 /* by default we allow the insn since we don't have checks for
1064 * all possible cases yet */
David Schleefed9eccb2008-11-04 20:29:31 -08001065 default:
Mark Rankilor3fffdf22010-04-29 18:17:16 +08001066 printk(KERN_WARNING
1067 "comedi: no check for data length of config insn id "
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301068 "%i is implemented.\n"
1069 " Add a check to %s in %s.\n"
1070 " Assuming n=%i is correct.\n", data[0], __func__,
1071 __FILE__, insn->n);
David Schleefed9eccb2008-11-04 20:29:31 -08001072 return 0;
1073 break;
1074 }
1075 return -EINVAL;
1076}
1077
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301078static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1079 unsigned int *data, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001080{
Bill Pemberton34c43922009-03-16 22:05:14 -04001081 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001082 int ret = 0;
1083 int i;
1084
1085 if (insn->insn & INSN_MASK_SPECIAL) {
1086 /* a non-subdevice instruction */
1087
1088 switch (insn->insn) {
1089 case INSN_GTOD:
1090 {
1091 struct timeval tv;
1092
1093 if (insn->n != 2) {
1094 ret = -EINVAL;
1095 break;
1096 }
1097
1098 do_gettimeofday(&tv);
1099 data[0] = tv.tv_sec;
1100 data[1] = tv.tv_usec;
1101 ret = 2;
1102
1103 break;
1104 }
1105 case INSN_WAIT:
1106 if (insn->n != 1 || data[0] >= 100000) {
1107 ret = -EINVAL;
1108 break;
1109 }
1110 udelay(data[0] / 1000);
1111 ret = 1;
1112 break;
1113 case INSN_INTTRIG:
1114 if (insn->n != 1) {
1115 ret = -EINVAL;
1116 break;
1117 }
1118 if (insn->subdev >= dev->n_subdevices) {
1119 DPRINTK("%d not usable subdevice\n",
1120 insn->subdev);
1121 ret = -EINVAL;
1122 break;
1123 }
1124 s = dev->subdevices + insn->subdev;
1125 if (!s->async) {
1126 DPRINTK("no async\n");
1127 ret = -EINVAL;
1128 break;
1129 }
1130 if (!s->async->inttrig) {
1131 DPRINTK("no inttrig\n");
1132 ret = -EAGAIN;
1133 break;
1134 }
1135 ret = s->async->inttrig(dev, s, insn->data[0]);
1136 if (ret >= 0)
1137 ret = 1;
1138 break;
1139 default:
1140 DPRINTK("invalid insn\n");
1141 ret = -EINVAL;
1142 break;
1143 }
1144 } else {
1145 /* a subdevice instruction */
Bill Pemberton790c5542009-03-16 22:05:02 -04001146 unsigned int maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -08001147
1148 if (insn->subdev >= dev->n_subdevices) {
1149 DPRINTK("subdevice %d out of range\n", insn->subdev);
1150 ret = -EINVAL;
1151 goto out;
1152 }
1153 s = dev->subdevices + insn->subdev;
1154
1155 if (s->type == COMEDI_SUBD_UNUSED) {
1156 DPRINTK("%d not usable subdevice\n", insn->subdev);
1157 ret = -EIO;
1158 goto out;
1159 }
1160
1161 /* are we locked? (ioctl lock) */
1162 if (s->lock && s->lock != file) {
1163 DPRINTK("device locked\n");
1164 ret = -EACCES;
1165 goto out;
1166 }
1167
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -07001168 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001169 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001170 ret = -EINVAL;
1171 DPRINTK("bad chanspec\n");
1172 goto out;
1173 }
1174
1175 if (s->busy) {
1176 ret = -EBUSY;
1177 goto out;
1178 }
1179 /* This looks arbitrary. It is. */
1180 s->busy = &parse_insn;
1181 switch (insn->insn) {
1182 case INSN_READ:
1183 ret = s->insn_read(dev, s, insn, data);
1184 break;
1185 case INSN_WRITE:
1186 maxdata = s->maxdata_list
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001187 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1188 : s->maxdata;
David Schleefed9eccb2008-11-04 20:29:31 -08001189 for (i = 0; i < insn->n; ++i) {
1190 if (data[i] > maxdata) {
1191 ret = -EINVAL;
1192 DPRINTK("bad data value(s)\n");
1193 break;
1194 }
1195 }
1196 if (ret == 0)
1197 ret = s->insn_write(dev, s, insn, data);
1198 break;
1199 case INSN_BITS:
1200 if (insn->n != 2) {
1201 ret = -EINVAL;
Ian Abbott2f644cc2011-01-18 17:44:33 +00001202 } else {
1203 /* Most drivers ignore the base channel in
1204 * insn->chanspec. Fix this here if
1205 * the subdevice has <= 32 channels. */
1206 unsigned int shift;
1207 unsigned int orig_mask;
1208
1209 orig_mask = data[0];
1210 if (s->n_chan <= 32) {
1211 shift = CR_CHAN(insn->chanspec);
1212 if (shift > 0) {
1213 insn->chanspec = 0;
1214 data[0] <<= shift;
1215 data[1] <<= shift;
1216 }
1217 } else
1218 shift = 0;
1219 ret = s->insn_bits(dev, s, insn, data);
1220 data[0] = orig_mask;
1221 if (shift > 0)
1222 data[1] >>= shift;
David Schleefed9eccb2008-11-04 20:29:31 -08001223 }
David Schleefed9eccb2008-11-04 20:29:31 -08001224 break;
1225 case INSN_CONFIG:
1226 ret = check_insn_config_length(insn, data);
1227 if (ret)
1228 break;
1229 ret = s->insn_config(dev, s, insn, data);
1230 break;
1231 default:
1232 ret = -EINVAL;
1233 break;
1234 }
1235
1236 s->busy = NULL;
1237 }
1238
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001239out:
David Schleefed9eccb2008-11-04 20:29:31 -08001240 return ret;
1241}
1242
1243/*
Pieter De Praetere20617f22010-03-10 09:47:44 +01001244 * COMEDI_INSN
1245 * synchronous instructions
David Schleefed9eccb2008-11-04 20:29:31 -08001246 *
Pieter De Praetere20617f22010-03-10 09:47:44 +01001247 * arg:
1248 * pointer to insn
David Schleefed9eccb2008-11-04 20:29:31 -08001249 *
Pieter De Praetere20617f22010-03-10 09:47:44 +01001250 * reads:
1251 * struct comedi_insn struct at arg
1252 * data (for writes)
David Schleefed9eccb2008-11-04 20:29:31 -08001253 *
Pieter De Praetere20617f22010-03-10 09:47:44 +01001254 * writes:
1255 * data (for reads)
David Schleefed9eccb2008-11-04 20:29:31 -08001256 */
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001257static int do_insn_ioctl(struct comedi_device *dev,
1258 struct comedi_insn __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001259{
Bill Pemberton90035c02009-03-16 22:05:53 -04001260 struct comedi_insn insn;
Bill Pemberton790c5542009-03-16 22:05:02 -04001261 unsigned int *data = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001262 int ret = 0;
1263
Bill Pemberton790c5542009-03-16 22:05:02 -04001264 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001265 if (!data) {
1266 ret = -ENOMEM;
1267 goto error;
1268 }
1269
Bill Pemberton90035c02009-03-16 22:05:53 -04001270 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001271 ret = -EFAULT;
1272 goto error;
1273 }
1274
1275 /* This is where the behavior of insn and insnlist deviate. */
1276 if (insn.n > MAX_SAMPLES)
1277 insn.n = MAX_SAMPLES;
1278 if (insn.insn & INSN_MASK_WRITE) {
Mark21fe2ee2010-05-13 17:44:39 +08001279 if (copy_from_user(data,
1280 insn.data,
1281 insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001282 ret = -EFAULT;
1283 goto error;
1284 }
1285 }
1286 ret = parse_insn(dev, &insn, data, file);
1287 if (ret < 0)
1288 goto error;
1289 if (insn.insn & INSN_MASK_READ) {
Mark21fe2ee2010-05-13 17:44:39 +08001290 if (copy_to_user(insn.data,
1291 data,
1292 insn.n * sizeof(unsigned int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001293 ret = -EFAULT;
1294 goto error;
1295 }
1296 }
1297 ret = insn.n;
1298
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001299error:
1300 kfree(data);
David Schleefed9eccb2008-11-04 20:29:31 -08001301
1302 return ret;
1303}
1304
Greg Kroah-Hartman181bd672010-05-03 15:15:06 -07001305static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
1306 unsigned mask, unsigned bits)
1307{
1308 unsigned long flags;
1309
1310 spin_lock_irqsave(&s->spin_lock, flags);
1311 s->runflags &= ~mask;
1312 s->runflags |= (bits & mask);
1313 spin_unlock_irqrestore(&s->spin_lock, flags);
1314}
1315
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001316static int do_cmd_ioctl(struct comedi_device *dev,
1317 struct comedi_cmd __user *cmd, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001318{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001319 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001320 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001321 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001322 int ret = 0;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001323 unsigned int __user *chanlist_saver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001324
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001325 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001326 DPRINTK("bad cmd address\n");
1327 return -EFAULT;
1328 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001329 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001330 chanlist_saver = user_cmd.chanlist;
1331
1332 if (user_cmd.subdev >= dev->n_subdevices) {
1333 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1334 return -ENODEV;
1335 }
1336
1337 s = dev->subdevices + user_cmd.subdev;
1338 async = s->async;
1339
1340 if (s->type == COMEDI_SUBD_UNUSED) {
1341 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1342 return -EIO;
1343 }
1344
1345 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1346 DPRINTK("subdevice %i does not support commands\n",
1347 user_cmd.subdev);
1348 return -EIO;
1349 }
1350
1351 /* are we locked? (ioctl lock) */
1352 if (s->lock && s->lock != file) {
1353 DPRINTK("subdevice locked\n");
1354 return -EACCES;
1355 }
1356
1357 /* are we busy? */
1358 if (s->busy) {
1359 DPRINTK("subdevice busy\n");
1360 return -EBUSY;
1361 }
1362 s->busy = file;
1363
1364 /* make sure channel/gain list isn't too long */
1365 if (user_cmd.chanlist_len > s->len_chanlist) {
1366 DPRINTK("channel/gain list too long %u > %d\n",
1367 user_cmd.chanlist_len, s->len_chanlist);
1368 ret = -EINVAL;
1369 goto cleanup;
1370 }
1371
1372 /* make sure channel/gain list isn't too short */
1373 if (user_cmd.chanlist_len < 1) {
1374 DPRINTK("channel/gain list too short %u < 1\n",
1375 user_cmd.chanlist_len);
1376 ret = -EINVAL;
1377 goto cleanup;
1378 }
1379
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001380 kfree(async->cmd.chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001381 async->cmd = user_cmd;
1382 async->cmd.data = NULL;
1383 /* load channel/gain list */
1384 async->cmd.chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001385 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001386 if (!async->cmd.chanlist) {
1387 DPRINTK("allocation failed\n");
1388 ret = -ENOMEM;
1389 goto cleanup;
1390 }
1391
1392 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001393 async->cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001394 DPRINTK("fault reading chanlist\n");
1395 ret = -EFAULT;
1396 goto cleanup;
1397 }
1398
1399 /* make sure each element in channel/gain list is valid */
Mark21fe2ee2010-05-13 17:44:39 +08001400 ret = comedi_check_chanlist(s,
1401 async->cmd.chanlist_len,
1402 async->cmd.chanlist);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001403 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001404 DPRINTK("bad chanlist\n");
1405 goto cleanup;
1406 }
1407
1408 ret = s->do_cmdtest(dev, s, &async->cmd);
1409
1410 if (async->cmd.flags & TRIG_BOGUS || ret) {
1411 DPRINTK("test returned %d\n", ret);
1412 user_cmd = async->cmd;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001413 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001414 user_cmd.chanlist = chanlist_saver;
1415 user_cmd.data = NULL;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001416 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001417 DPRINTK("fault writing cmd\n");
1418 ret = -EFAULT;
1419 goto cleanup;
1420 }
1421 ret = -EAGAIN;
1422 goto cleanup;
1423 }
1424
1425 if (!async->prealloc_bufsz) {
1426 ret = -ENOMEM;
1427 DPRINTK("no buffer (?)\n");
1428 goto cleanup;
1429 }
1430
1431 comedi_reset_async_buf(async);
1432
1433 async->cb_mask =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001434 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1435 COMEDI_CB_OVERFLOW;
1436 if (async->cmd.flags & TRIG_WAKE_EOS)
David Schleefed9eccb2008-11-04 20:29:31 -08001437 async->cb_mask |= COMEDI_CB_EOS;
David Schleefed9eccb2008-11-04 20:29:31 -08001438
1439 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1440
David Schleefed9eccb2008-11-04 20:29:31 -08001441 ret = s->do_cmd(dev, s);
1442 if (ret == 0)
1443 return 0;
1444
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001445cleanup:
David Schleefed9eccb2008-11-04 20:29:31 -08001446 do_become_nonbusy(dev, s);
1447
1448 return ret;
1449}
1450
1451/*
1452 COMEDI_CMDTEST
1453 command testing ioctl
1454
1455 arg:
1456 pointer to cmd structure
1457
1458 reads:
1459 cmd structure at arg
1460 channel/range list
1461
1462 writes:
1463 modified cmd structure at arg
1464
1465*/
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001466static int do_cmdtest_ioctl(struct comedi_device *dev,
1467 struct comedi_cmd __user *arg, void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001468{
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001469 struct comedi_cmd user_cmd;
Bill Pemberton34c43922009-03-16 22:05:14 -04001470 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001471 int ret = 0;
1472 unsigned int *chanlist = NULL;
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001473 unsigned int __user *chanlist_saver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001474
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001475 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001476 DPRINTK("bad cmd address\n");
1477 return -EFAULT;
1478 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001479 /* save user's chanlist pointer so it can be restored later */
David Schleefed9eccb2008-11-04 20:29:31 -08001480 chanlist_saver = user_cmd.chanlist;
1481
1482 if (user_cmd.subdev >= dev->n_subdevices) {
1483 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1484 return -ENODEV;
1485 }
1486
1487 s = dev->subdevices + user_cmd.subdev;
1488 if (s->type == COMEDI_SUBD_UNUSED) {
1489 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1490 return -EIO;
1491 }
1492
1493 if (!s->do_cmd || !s->do_cmdtest) {
1494 DPRINTK("subdevice %i does not support commands\n",
1495 user_cmd.subdev);
1496 return -EIO;
1497 }
1498
1499 /* make sure channel/gain list isn't too long */
1500 if (user_cmd.chanlist_len > s->len_chanlist) {
1501 DPRINTK("channel/gain list too long %d > %d\n",
1502 user_cmd.chanlist_len, s->len_chanlist);
1503 ret = -EINVAL;
1504 goto cleanup;
1505 }
1506
1507 /* load channel/gain list */
1508 if (user_cmd.chanlist) {
1509 chanlist =
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001510 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -08001511 if (!chanlist) {
1512 DPRINTK("allocation failed\n");
1513 ret = -ENOMEM;
1514 goto cleanup;
1515 }
1516
1517 if (copy_from_user(chanlist, user_cmd.chanlist,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001518 user_cmd.chanlist_len * sizeof(int))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001519 DPRINTK("fault reading chanlist\n");
1520 ret = -EFAULT;
1521 goto cleanup;
1522 }
1523
1524 /* make sure each element in channel/gain list is valid */
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -07001525 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001526 if (ret < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001527 DPRINTK("bad chanlist\n");
1528 goto cleanup;
1529 }
1530
1531 user_cmd.chanlist = chanlist;
1532 }
1533
1534 ret = s->do_cmdtest(dev, s, &user_cmd);
1535
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001536 /* restore chanlist pointer before copying back */
David Schleefed9eccb2008-11-04 20:29:31 -08001537 user_cmd.chanlist = chanlist_saver;
1538
Bill Pembertonea6d0d42009-03-16 22:05:47 -04001539 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
David Schleefed9eccb2008-11-04 20:29:31 -08001540 DPRINTK("bad cmd address\n");
1541 ret = -EFAULT;
1542 goto cleanup;
1543 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001544cleanup:
1545 kfree(chanlist);
David Schleefed9eccb2008-11-04 20:29:31 -08001546
1547 return ret;
1548}
1549
1550/*
1551 COMEDI_LOCK
1552 lock subdevice
1553
1554 arg:
1555 subdevice number
1556
1557 reads:
1558 none
1559
1560 writes:
1561 none
1562
1563*/
1564
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301565static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1566 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001567{
1568 int ret = 0;
1569 unsigned long flags;
Bill Pemberton34c43922009-03-16 22:05:14 -04001570 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001571
1572 if (arg >= dev->n_subdevices)
1573 return -EINVAL;
1574 s = dev->subdevices + arg;
1575
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001576 spin_lock_irqsave(&s->spin_lock, flags);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001577 if (s->busy || s->lock)
David Schleefed9eccb2008-11-04 20:29:31 -08001578 ret = -EBUSY;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001579 else
David Schleefed9eccb2008-11-04 20:29:31 -08001580 s->lock = file;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07001581 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08001582
Greg Dietschec5274ab2011-06-13 13:11:47 -05001583#if 0
David Schleefed9eccb2008-11-04 20:29:31 -08001584 if (ret < 0)
1585 return ret;
1586
David Schleefed9eccb2008-11-04 20:29:31 -08001587 if (s->lock_f)
1588 ret = s->lock_f(dev, s);
1589#endif
1590
1591 return ret;
1592}
1593
1594/*
1595 COMEDI_UNLOCK
1596 unlock subdevice
1597
1598 arg:
1599 subdevice number
1600
1601 reads:
1602 none
1603
1604 writes:
1605 none
1606
1607 This function isn't protected by the semaphore, since
1608 we already own the lock.
1609*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301610static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1611 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001612{
Bill Pemberton34c43922009-03-16 22:05:14 -04001613 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001614
1615 if (arg >= dev->n_subdevices)
1616 return -EINVAL;
1617 s = dev->subdevices + arg;
1618
1619 if (s->busy)
1620 return -EBUSY;
1621
1622 if (s->lock && s->lock != file)
1623 return -EACCES;
1624
1625 if (s->lock == file) {
1626#if 0
1627 if (s->unlock)
1628 s->unlock(dev, s);
1629#endif
1630
1631 s->lock = NULL;
1632 }
1633
1634 return 0;
1635}
1636
1637/*
1638 COMEDI_CANCEL
1639 cancel acquisition ioctl
1640
1641 arg:
1642 subdevice number
1643
1644 reads:
1645 nothing
1646
1647 writes:
1648 nothing
1649
1650*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301651static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1652 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001653{
Bill Pemberton34c43922009-03-16 22:05:14 -04001654 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001655
1656 if (arg >= dev->n_subdevices)
1657 return -EINVAL;
1658 s = dev->subdevices + arg;
1659 if (s->async == NULL)
1660 return -EINVAL;
1661
1662 if (s->lock && s->lock != file)
1663 return -EACCES;
1664
1665 if (!s->busy)
1666 return 0;
1667
1668 if (s->busy != file)
1669 return -EBUSY;
1670
1671 return do_cancel(dev, s);
1672}
1673
1674/*
1675 COMEDI_POLL ioctl
1676 instructs driver to synchronize buffers
1677
1678 arg:
1679 subdevice number
1680
1681 reads:
1682 nothing
1683
1684 writes:
1685 nothing
1686
1687*/
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301688static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1689 void *file)
David Schleefed9eccb2008-11-04 20:29:31 -08001690{
Bill Pemberton34c43922009-03-16 22:05:14 -04001691 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08001692
1693 if (arg >= dev->n_subdevices)
1694 return -EINVAL;
1695 s = dev->subdevices + arg;
1696
1697 if (s->lock && s->lock != file)
1698 return -EACCES;
1699
1700 if (!s->busy)
1701 return 0;
1702
1703 if (s->busy != file)
1704 return -EBUSY;
1705
1706 if (s->poll)
1707 return s->poll(dev, s);
1708
1709 return -EINVAL;
1710}
1711
Bill Pemberton34c43922009-03-16 22:05:14 -04001712static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08001713{
1714 int ret = 0;
1715
1716 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1717 ret = s->cancel(dev, s);
1718
1719 do_become_nonbusy(dev, s);
1720
1721 return ret;
1722}
1723
Federico Vagadf30b212011-10-29 09:45:39 +02001724
1725static void comedi_vm_open(struct vm_area_struct *area)
1726{
1727 struct comedi_async *async;
1728 struct comedi_device *dev;
1729
1730 async = area->vm_private_data;
1731 dev = async->subdevice->device;
1732
1733 mutex_lock(&dev->mutex);
1734 async->mmap_count++;
1735 mutex_unlock(&dev->mutex);
1736}
1737
1738static void comedi_vm_close(struct vm_area_struct *area)
David Schleefed9eccb2008-11-04 20:29:31 -08001739{
Bill Pembertond1636792009-03-16 22:05:20 -04001740 struct comedi_async *async;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04001741 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -08001742
1743 async = area->vm_private_data;
1744 dev = async->subdevice->device;
1745
1746 mutex_lock(&dev->mutex);
1747 async->mmap_count--;
1748 mutex_unlock(&dev->mutex);
1749}
1750
1751static struct vm_operations_struct comedi_vm_ops = {
Federico Vagadf30b212011-10-29 09:45:39 +02001752 .open = comedi_vm_open,
1753 .close = comedi_vm_close,
David Schleefed9eccb2008-11-04 20:29:31 -08001754};
1755
1756static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1757{
1758 const unsigned minor = iminor(file->f_dentry->d_inode);
Bill Pembertond1636792009-03-16 22:05:20 -04001759 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08001760 unsigned long start = vma->vm_start;
1761 unsigned long size;
1762 int n_pages;
1763 int i;
1764 int retval;
Bill Pemberton34c43922009-03-16 22:05:14 -04001765 struct comedi_subdevice *s;
Bernd Porr3ffab422011-11-08 21:23:03 +00001766 struct comedi_device_file_info *dev_file_info;
1767 struct comedi_device *dev;
1768
1769 dev_file_info = comedi_get_device_file_info(minor);
1770 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001771 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001772 dev = dev_file_info->device;
1773 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001774 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001775
1776 mutex_lock(&dev->mutex);
1777 if (!dev->attached) {
1778 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1779 retval = -ENODEV;
1780 goto done;
1781 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001782 if (vma->vm_flags & VM_WRITE)
David Schleefed9eccb2008-11-04 20:29:31 -08001783 s = comedi_get_write_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001784 else
David Schleefed9eccb2008-11-04 20:29:31 -08001785 s = comedi_get_read_subdevice(dev_file_info);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001786
David Schleefed9eccb2008-11-04 20:29:31 -08001787 if (s == NULL) {
1788 retval = -EINVAL;
1789 goto done;
1790 }
1791 async = s->async;
1792 if (async == NULL) {
1793 retval = -EINVAL;
1794 goto done;
1795 }
1796
1797 if (vma->vm_pgoff != 0) {
1798 DPRINTK("comedi: mmap() offset must be 0.\n");
1799 retval = -EINVAL;
1800 goto done;
1801 }
1802
1803 size = vma->vm_end - vma->vm_start;
1804 if (size > async->prealloc_bufsz) {
1805 retval = -EFAULT;
1806 goto done;
1807 }
1808 if (size & (~PAGE_MASK)) {
1809 retval = -EFAULT;
1810 goto done;
1811 }
1812
1813 n_pages = size >> PAGE_SHIFT;
1814 for (i = 0; i < n_pages; ++i) {
1815 if (remap_pfn_range(vma, start,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05301816 page_to_pfn(virt_to_page
1817 (async->buf_page_list
1818 [i].virt_addr)), PAGE_SIZE,
1819 PAGE_SHARED)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001820 retval = -EAGAIN;
1821 goto done;
1822 }
1823 start += PAGE_SIZE;
1824 }
1825
1826 vma->vm_ops = &comedi_vm_ops;
1827 vma->vm_private_data = async;
1828
1829 async->mmap_count++;
1830
1831 retval = 0;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001832done:
David Schleefed9eccb2008-11-04 20:29:31 -08001833 mutex_unlock(&dev->mutex);
1834 return retval;
1835}
1836
Benedikt Bergenthal1ae50622012-04-16 12:40:22 +02001837static unsigned int comedi_poll(struct file *file, poll_table *wait)
David Schleefed9eccb2008-11-04 20:29:31 -08001838{
1839 unsigned int mask = 0;
1840 const unsigned minor = iminor(file->f_dentry->d_inode);
Bill Pemberton34c43922009-03-16 22:05:14 -04001841 struct comedi_subdevice *read_subdev;
1842 struct comedi_subdevice *write_subdev;
Bernd Porr3ffab422011-11-08 21:23:03 +00001843 struct comedi_device_file_info *dev_file_info;
1844 struct comedi_device *dev;
1845 dev_file_info = comedi_get_device_file_info(minor);
1846
1847 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001848 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001849 dev = dev_file_info->device;
1850 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001851 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001852
1853 mutex_lock(&dev->mutex);
1854 if (!dev->attached) {
1855 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1856 mutex_unlock(&dev->mutex);
1857 return 0;
1858 }
1859
1860 mask = 0;
1861 read_subdev = comedi_get_read_subdevice(dev_file_info);
1862 if (read_subdev) {
1863 poll_wait(file, &read_subdev->async->wait_head, wait);
1864 if (!read_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001865 || comedi_buf_read_n_available(read_subdev->async) > 0
1866 || !(comedi_get_subdevice_runflags(read_subdev) &
1867 SRF_RUNNING)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001868 mask |= POLLIN | POLLRDNORM;
1869 }
1870 }
1871 write_subdev = comedi_get_write_subdevice(dev_file_info);
1872 if (write_subdev) {
1873 poll_wait(file, &write_subdev->async->wait_head, wait);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001874 comedi_buf_write_alloc(write_subdev->async,
1875 write_subdev->async->prealloc_bufsz);
David Schleefed9eccb2008-11-04 20:29:31 -08001876 if (!write_subdev->busy
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001877 || !(comedi_get_subdevice_runflags(write_subdev) &
1878 SRF_RUNNING)
1879 || comedi_buf_write_n_allocated(write_subdev->async) >=
1880 bytes_per_sample(write_subdev->async->subdevice)) {
David Schleefed9eccb2008-11-04 20:29:31 -08001881 mask |= POLLOUT | POLLWRNORM;
1882 }
1883 }
1884
1885 mutex_unlock(&dev->mutex);
1886 return mask;
1887}
1888
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07001889static ssize_t comedi_write(struct file *file, const char __user *buf,
1890 size_t nbytes, loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08001891{
Bill Pemberton34c43922009-03-16 22:05:14 -04001892 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04001893 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08001894 int n, m, count = 0, retval = 0;
1895 DECLARE_WAITQUEUE(wait, current);
1896 const unsigned minor = iminor(file->f_dentry->d_inode);
Bernd Porr3ffab422011-11-08 21:23:03 +00001897 struct comedi_device_file_info *dev_file_info;
1898 struct comedi_device *dev;
1899 dev_file_info = comedi_get_device_file_info(minor);
1900
1901 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001902 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00001903 dev = dev_file_info->device;
1904 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01001905 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08001906
1907 if (!dev->attached) {
1908 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1909 retval = -ENODEV;
1910 goto done;
1911 }
1912
1913 s = comedi_get_write_subdevice(dev_file_info);
1914 if (s == NULL) {
1915 retval = -EIO;
1916 goto done;
1917 }
1918 async = s->async;
1919
1920 if (!nbytes) {
1921 retval = 0;
1922 goto done;
1923 }
1924 if (!s->busy) {
1925 retval = 0;
1926 goto done;
1927 }
1928 if (s->busy != file) {
1929 retval = -EACCES;
1930 goto done;
1931 }
1932 add_wait_queue(&async->wait_head, &wait);
1933 while (nbytes > 0 && !retval) {
1934 set_current_state(TASK_INTERRUPTIBLE);
1935
Ian Abbottd2611542010-05-19 17:22:41 +01001936 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1937 if (count == 0) {
1938 if (comedi_get_subdevice_runflags(s) &
1939 SRF_ERROR) {
1940 retval = -EPIPE;
1941 } else {
1942 retval = 0;
1943 }
1944 do_become_nonbusy(dev, s);
1945 }
1946 break;
1947 }
1948
David Schleefed9eccb2008-11-04 20:29:31 -08001949 n = nbytes;
1950
1951 m = n;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001952 if (async->buf_write_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08001953 m = async->prealloc_bufsz - async->buf_write_ptr;
David Schleefed9eccb2008-11-04 20:29:31 -08001954 comedi_buf_write_alloc(async, async->prealloc_bufsz);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001955 if (m > comedi_buf_write_n_allocated(async))
David Schleefed9eccb2008-11-04 20:29:31 -08001956 m = comedi_buf_write_n_allocated(async);
David Schleefed9eccb2008-11-04 20:29:31 -08001957 if (m < n)
1958 n = m;
1959
1960 if (n == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08001961 if (file->f_flags & O_NONBLOCK) {
1962 retval = -EAGAIN;
1963 break;
1964 }
Federico Vaga6a9ce6b2011-10-29 09:47:39 +02001965 schedule();
David Schleefed9eccb2008-11-04 20:29:31 -08001966 if (signal_pending(current)) {
1967 retval = -ERESTARTSYS;
1968 break;
1969 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001970 if (!s->busy)
David Schleefed9eccb2008-11-04 20:29:31 -08001971 break;
David Schleefed9eccb2008-11-04 20:29:31 -08001972 if (s->busy != file) {
1973 retval = -EACCES;
1974 break;
1975 }
1976 continue;
1977 }
1978
1979 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001980 buf, n);
David Schleefed9eccb2008-11-04 20:29:31 -08001981 if (m) {
1982 n -= m;
1983 retval = -EFAULT;
1984 }
1985 comedi_buf_write_free(async, n);
1986
1987 count += n;
1988 nbytes -= n;
1989
1990 buf += n;
1991 break; /* makes device work like a pipe */
1992 }
1993 set_current_state(TASK_RUNNING);
1994 remove_wait_queue(&async->wait_head, &wait);
1995
1996done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08001997 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08001998}
1999
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002000static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
Andrea Gelmini6705b682010-02-26 10:14:55 +01002001 loff_t *offset)
David Schleefed9eccb2008-11-04 20:29:31 -08002002{
Bill Pemberton34c43922009-03-16 22:05:14 -04002003 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -04002004 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -08002005 int n, m, count = 0, retval = 0;
2006 DECLARE_WAITQUEUE(wait, current);
2007 const unsigned minor = iminor(file->f_dentry->d_inode);
Bernd Porr3ffab422011-11-08 21:23:03 +00002008 struct comedi_device_file_info *dev_file_info;
2009 struct comedi_device *dev;
2010 dev_file_info = comedi_get_device_file_info(minor);
2011
2012 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01002013 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00002014 dev = dev_file_info->device;
2015 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01002016 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08002017
2018 if (!dev->attached) {
2019 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2020 retval = -ENODEV;
2021 goto done;
2022 }
2023
2024 s = comedi_get_read_subdevice(dev_file_info);
2025 if (s == NULL) {
2026 retval = -EIO;
2027 goto done;
2028 }
2029 async = s->async;
2030 if (!nbytes) {
2031 retval = 0;
2032 goto done;
2033 }
2034 if (!s->busy) {
2035 retval = 0;
2036 goto done;
2037 }
2038 if (s->busy != file) {
2039 retval = -EACCES;
2040 goto done;
2041 }
2042
2043 add_wait_queue(&async->wait_head, &wait);
2044 while (nbytes > 0 && !retval) {
2045 set_current_state(TASK_INTERRUPTIBLE);
2046
2047 n = nbytes;
2048
2049 m = comedi_buf_read_n_available(async);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002050 /* printk("%d available\n",m); */
2051 if (async->buf_read_ptr + m > async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -08002052 m = async->prealloc_bufsz - async->buf_read_ptr;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002053 /* printk("%d contiguous\n",m); */
David Schleefed9eccb2008-11-04 20:29:31 -08002054 if (m < n)
2055 n = m;
2056
2057 if (n == 0) {
2058 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
2059 do_become_nonbusy(dev, s);
2060 if (comedi_get_subdevice_runflags(s) &
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002061 SRF_ERROR) {
David Schleefed9eccb2008-11-04 20:29:31 -08002062 retval = -EPIPE;
2063 } else {
2064 retval = 0;
2065 }
2066 break;
2067 }
2068 if (file->f_flags & O_NONBLOCK) {
2069 retval = -EAGAIN;
2070 break;
2071 }
Federico Vaga6a9ce6b2011-10-29 09:47:39 +02002072 schedule();
David Schleefed9eccb2008-11-04 20:29:31 -08002073 if (signal_pending(current)) {
2074 retval = -ERESTARTSYS;
2075 break;
2076 }
David Schleefed9eccb2008-11-04 20:29:31 -08002077 if (!s->busy) {
2078 retval = 0;
2079 break;
2080 }
2081 if (s->busy != file) {
2082 retval = -EACCES;
2083 break;
2084 }
2085 continue;
2086 }
2087 m = copy_to_user(buf, async->prealloc_buf +
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002088 async->buf_read_ptr, n);
David Schleefed9eccb2008-11-04 20:29:31 -08002089 if (m) {
2090 n -= m;
2091 retval = -EFAULT;
2092 }
2093
2094 comedi_buf_read_alloc(async, n);
2095 comedi_buf_read_free(async, n);
2096
2097 count += n;
2098 nbytes -= n;
2099
2100 buf += n;
2101 break; /* makes device work like a pipe */
2102 }
2103 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002104 async->buf_read_count - async->buf_write_count == 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08002105 do_become_nonbusy(dev, s);
2106 }
2107 set_current_state(TASK_RUNNING);
2108 remove_wait_queue(&async->wait_head, &wait);
2109
2110done:
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002111 return count ? count : retval;
David Schleefed9eccb2008-11-04 20:29:31 -08002112}
2113
2114/*
2115 This function restores a subdevice to an idle state.
2116 */
Bill Pemberton34c43922009-03-16 22:05:14 -04002117void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002118{
Bill Pembertond1636792009-03-16 22:05:20 -04002119 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08002120
2121 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08002122 if (async) {
2123 comedi_reset_async_buf(async);
2124 async->inttrig = NULL;
2125 } else {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002126 printk(KERN_ERR
2127 "BUG: (?) do_become_nonbusy called with async=0\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002128 }
2129
2130 s->busy = NULL;
2131}
2132
2133static int comedi_open(struct inode *inode, struct file *file)
2134{
David Schleefed9eccb2008-11-04 20:29:31 -08002135 const unsigned minor = iminor(inode);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002136 struct comedi_device_file_info *dev_file_info =
2137 comedi_get_device_file_info(minor);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302138 struct comedi_device *dev =
2139 dev_file_info ? dev_file_info->device : NULL;
Ian Abbott97920072009-02-09 16:51:38 +00002140
David Schleefed9eccb2008-11-04 20:29:31 -08002141 if (dev == NULL) {
2142 DPRINTK("invalid minor number\n");
2143 return -ENODEV;
2144 }
2145
2146 /* This is slightly hacky, but we want module autoloading
2147 * to work for root.
2148 * case: user opens device, attached -> ok
2149 * case: user opens device, unattached, in_request_module=0 -> autoload
2150 * case: user opens device, unattached, in_request_module=1 -> fail
2151 * case: root opens device, attached -> ok
2152 * case: root opens device, unattached, in_request_module=1 -> ok
2153 * (typically called from modprobe)
2154 * case: root opens device, unattached, in_request_module=0 -> autoload
2155 *
2156 * The last could be changed to "-> ok", which would deny root
2157 * autoloading.
2158 */
2159 mutex_lock(&dev->mutex);
2160 if (dev->attached)
2161 goto ok;
Eric Parisa8f80e82009-08-13 09:44:51 -04002162 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
David Schleefed9eccb2008-11-04 20:29:31 -08002163 DPRINTK("in request module\n");
2164 mutex_unlock(&dev->mutex);
2165 return -ENODEV;
2166 }
Eric Parisa8f80e82009-08-13 09:44:51 -04002167 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
David Schleefed9eccb2008-11-04 20:29:31 -08002168 goto ok;
2169
2170 dev->in_request_module = 1;
2171
David Schleefed9eccb2008-11-04 20:29:31 -08002172#ifdef CONFIG_KMOD
2173 mutex_unlock(&dev->mutex);
Ian Abbott56d92c62009-02-09 16:32:12 +00002174 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
David Schleefed9eccb2008-11-04 20:29:31 -08002175 mutex_lock(&dev->mutex);
2176#endif
2177
2178 dev->in_request_module = 0;
2179
Eric Parisa8f80e82009-08-13 09:44:51 -04002180 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2181 DPRINTK("not attached and not CAP_NET_ADMIN\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002182 mutex_unlock(&dev->mutex);
2183 return -ENODEV;
2184 }
2185ok:
2186 __module_get(THIS_MODULE);
2187
2188 if (dev->attached) {
2189 if (!try_module_get(dev->driver->module)) {
2190 module_put(THIS_MODULE);
2191 mutex_unlock(&dev->mutex);
2192 return -ENOSYS;
2193 }
2194 }
2195
Ian Abbott3c17ba072010-05-19 14:10:00 +01002196 if (dev->attached && dev->use_count == 0 && dev->open) {
2197 int rc = dev->open(dev);
2198 if (rc < 0) {
2199 module_put(dev->driver->module);
2200 module_put(THIS_MODULE);
2201 mutex_unlock(&dev->mutex);
2202 return rc;
2203 }
2204 }
David Schleefed9eccb2008-11-04 20:29:31 -08002205
2206 dev->use_count++;
2207
2208 mutex_unlock(&dev->mutex);
2209
2210 return 0;
2211}
2212
2213static int comedi_close(struct inode *inode, struct file *file)
2214{
2215 const unsigned minor = iminor(inode);
Bill Pemberton34c43922009-03-16 22:05:14 -04002216 struct comedi_subdevice *s = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08002217 int i;
Bernd Porr3ffab422011-11-08 21:23:03 +00002218 struct comedi_device_file_info *dev_file_info;
2219 struct comedi_device *dev;
2220 dev_file_info = comedi_get_device_file_info(minor);
2221
2222 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01002223 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00002224 dev = dev_file_info->device;
2225 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01002226 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08002227
2228 mutex_lock(&dev->mutex);
2229
2230 if (dev->subdevices) {
2231 for (i = 0; i < dev->n_subdevices; i++) {
2232 s = dev->subdevices + i;
2233
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002234 if (s->busy == file)
David Schleefed9eccb2008-11-04 20:29:31 -08002235 do_cancel(dev, s);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002236 if (s->lock == file)
David Schleefed9eccb2008-11-04 20:29:31 -08002237 s->lock = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -08002238 }
2239 }
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002240 if (dev->attached && dev->use_count == 1 && dev->close)
David Schleefed9eccb2008-11-04 20:29:31 -08002241 dev->close(dev);
David Schleefed9eccb2008-11-04 20:29:31 -08002242
2243 module_put(THIS_MODULE);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002244 if (dev->attached)
David Schleefed9eccb2008-11-04 20:29:31 -08002245 module_put(dev->driver->module);
David Schleefed9eccb2008-11-04 20:29:31 -08002246
2247 dev->use_count--;
2248
2249 mutex_unlock(&dev->mutex);
2250
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002251 if (file->f_flags & FASYNC)
David Schleefed9eccb2008-11-04 20:29:31 -08002252 comedi_fasync(-1, file, 0);
David Schleefed9eccb2008-11-04 20:29:31 -08002253
2254 return 0;
2255}
2256
2257static int comedi_fasync(int fd, struct file *file, int on)
2258{
2259 const unsigned minor = iminor(file->f_dentry->d_inode);
Bernd Porr3ffab422011-11-08 21:23:03 +00002260 struct comedi_device_file_info *dev_file_info;
2261 struct comedi_device *dev;
2262 dev_file_info = comedi_get_device_file_info(minor);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002263
Bernd Porr3ffab422011-11-08 21:23:03 +00002264 if (dev_file_info == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01002265 return -ENODEV;
Bernd Porr3ffab422011-11-08 21:23:03 +00002266 dev = dev_file_info->device;
2267 if (dev == NULL)
Florian Schmaus70fe7422011-12-14 14:23:24 +01002268 return -ENODEV;
David Schleefed9eccb2008-11-04 20:29:31 -08002269
2270 return fasync_helper(fd, file, on, &dev->async_queue);
2271}
2272
2273const struct file_operations comedi_fops = {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302274 .owner = THIS_MODULE,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302275 .unlocked_ioctl = comedi_unlocked_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302276 .compat_ioctl = comedi_compat_ioctl,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302277 .open = comedi_open,
2278 .release = comedi_close,
2279 .read = comedi_read,
2280 .write = comedi_write,
2281 .mmap = comedi_mmap,
2282 .poll = comedi_poll,
2283 .fasync = comedi_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002284 .llseek = noop_llseek,
David Schleefed9eccb2008-11-04 20:29:31 -08002285};
2286
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002287struct class *comedi_class;
David Schleefed9eccb2008-11-04 20:29:31 -08002288static struct cdev comedi_cdev;
2289
2290static void comedi_cleanup_legacy_minors(void)
2291{
2292 unsigned i;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002293
Bernd Porr1dd33ab2008-12-08 23:30:13 +00002294 for (i = 0; i < comedi_num_legacy_minors; i++)
David Schleefed9eccb2008-11-04 20:29:31 -08002295 comedi_free_board_minor(i);
David Schleefed9eccb2008-11-04 20:29:31 -08002296}
2297
2298static int __init comedi_init(void)
2299{
2300 int i;
2301 int retval;
2302
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002303 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
2304 " - http://www.comedi.org\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002305
Frank Mori Hessa3cb729e2008-12-15 13:44:45 +00002306 if (comedi_num_legacy_minors < 0 ||
2307 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2308 printk(KERN_ERR "comedi: error: invalid value for module "
2309 "parameter \"comedi_num_legacy_minors\". Valid values "
2310 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2311 return -EINVAL;
2312 }
2313
2314 /*
2315 * comedi is unusable if both comedi_autoconfig and
2316 * comedi_num_legacy_minors are zero, so we might as well adjust the
2317 * defaults in that case
2318 */
2319 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2320 comedi_num_legacy_minors = 16;
2321
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002322 memset(comedi_file_info_table, 0,
2323 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08002324
2325 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002326 COMEDI_NUM_MINORS, "comedi");
David Schleefed9eccb2008-11-04 20:29:31 -08002327 if (retval)
2328 return -EIO;
2329 cdev_init(&comedi_cdev, &comedi_fops);
2330 comedi_cdev.owner = THIS_MODULE;
2331 kobject_set_name(&comedi_cdev.kobj, "comedi");
2332 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2333 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002334 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08002335 return -EIO;
2336 }
2337 comedi_class = class_create(THIS_MODULE, "comedi");
2338 if (IS_ERR(comedi_class)) {
Mark Rankilor3fffdf22010-04-29 18:17:16 +08002339 printk(KERN_ERR "comedi: failed to create class");
David Schleefed9eccb2008-11-04 20:29:31 -08002340 cdev_del(&comedi_cdev);
2341 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002342 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08002343 return PTR_ERR(comedi_class);
2344 }
2345
H Hartley Sweetenfb603672012-05-10 18:05:28 -07002346 comedi_class->dev_attrs = comedi_dev_attrs;
2347
David Schleefed9eccb2008-11-04 20:29:31 -08002348 /* XXX requires /proc interface */
2349 comedi_proc_init();
2350
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002351 /* create devices files for legacy/manual use */
Bernd Porr1dd33ab2008-12-08 23:30:13 +00002352 for (i = 0; i < comedi_num_legacy_minors; i++) {
David Schleefed9eccb2008-11-04 20:29:31 -08002353 int minor;
2354 minor = comedi_alloc_board_minor(NULL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002355 if (minor < 0) {
David Schleefed9eccb2008-11-04 20:29:31 -08002356 comedi_cleanup_legacy_minors();
2357 cdev_del(&comedi_cdev);
2358 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002359 COMEDI_NUM_MINORS);
David Schleefed9eccb2008-11-04 20:29:31 -08002360 return minor;
2361 }
2362 }
2363
David Schleefed9eccb2008-11-04 20:29:31 -08002364 return 0;
2365}
2366
2367static void __exit comedi_cleanup(void)
2368{
2369 int i;
2370
2371 comedi_cleanup_legacy_minors();
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002372 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
David Schleefed9eccb2008-11-04 20:29:31 -08002373 BUG_ON(comedi_file_info_table[i]);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002374
David Schleefed9eccb2008-11-04 20:29:31 -08002375 class_destroy(comedi_class);
2376 cdev_del(&comedi_cdev);
2377 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2378
2379 comedi_proc_cleanup();
David Schleefed9eccb2008-11-04 20:29:31 -08002380}
2381
2382module_init(comedi_init);
2383module_exit(comedi_cleanup);
2384
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002385void comedi_error(const struct comedi_device *dev, const char *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002386{
Mark Rankilor3fffdf22010-04-29 18:17:16 +08002387 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2388 dev->driver->driver_name, s);
David Schleefed9eccb2008-11-04 20:29:31 -08002389}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002390EXPORT_SYMBOL(comedi_error);
David Schleefed9eccb2008-11-04 20:29:31 -08002391
Bill Pemberton34c43922009-03-16 22:05:14 -04002392void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002393{
Bill Pembertond1636792009-03-16 22:05:20 -04002394 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -08002395 unsigned runflags = 0;
2396 unsigned runflags_mask = 0;
2397
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002398 /* DPRINTK("comedi_event 0x%x\n",mask); */
David Schleefed9eccb2008-11-04 20:29:31 -08002399
2400 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2401 return;
2402
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302403 if (s->
2404 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2405 COMEDI_CB_OVERFLOW)) {
David Schleefed9eccb2008-11-04 20:29:31 -08002406 runflags_mask |= SRF_RUNNING;
2407 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002408 /* remember if an error event has occurred, so an error
David Schleefed9eccb2008-11-04 20:29:31 -08002409 * can be returned the next time the user does a read() */
2410 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2411 runflags_mask |= SRF_ERROR;
2412 runflags |= SRF_ERROR;
2413 }
2414 if (runflags_mask) {
2415 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2416 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2417 }
2418
2419 if (async->cb_mask & s->async->events) {
2420 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
Greg Kroah-Hartmanfcea1152009-04-27 15:15:30 -07002421 wake_up_interruptible(&async->wait_head);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002422 if (s->subdev_flags & SDF_CMD_READ)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302423 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
Andrea Gelmini6705b682010-02-26 10:14:55 +01002424 if (s->subdev_flags & SDF_CMD_WRITE)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302425 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
David Schleefed9eccb2008-11-04 20:29:31 -08002426 } else {
2427 if (async->cb_func)
2428 async->cb_func(s->async->events, async->cb_arg);
David Schleefed9eccb2008-11-04 20:29:31 -08002429 }
2430 }
2431 s->async->events = 0;
2432}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002433EXPORT_SYMBOL(comedi_event);
David Schleefed9eccb2008-11-04 20:29:31 -08002434
Bill Pemberton34c43922009-03-16 22:05:14 -04002435unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002436{
2437 unsigned long flags;
2438 unsigned runflags;
2439
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002440 spin_lock_irqsave(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002441 runflags = s->runflags;
Greg Kroah-Hartman5f74ea12009-04-27 14:44:31 -07002442 spin_unlock_irqrestore(&s->spin_lock, flags);
David Schleefed9eccb2008-11-04 20:29:31 -08002443 return runflags;
2444}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002445EXPORT_SYMBOL(comedi_get_subdevice_runflags);
David Schleefed9eccb2008-11-04 20:29:31 -08002446
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002447static int is_device_busy(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002448{
Bill Pemberton34c43922009-03-16 22:05:14 -04002449 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -08002450 int i;
2451
2452 if (!dev->attached)
2453 return 0;
2454
2455 for (i = 0; i < dev->n_subdevices; i++) {
2456 s = dev->subdevices + i;
2457 if (s->busy)
2458 return 1;
2459 if (s->async && s->async->mmap_count)
2460 return 1;
2461 }
2462
2463 return 0;
2464}
2465
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002466static void comedi_device_init(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002467{
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002468 memset(dev, 0, sizeof(struct comedi_device));
David Schleefed9eccb2008-11-04 20:29:31 -08002469 spin_lock_init(&dev->spinlock);
2470 mutex_init(&dev->mutex);
2471 dev->minor = -1;
2472}
2473
Greg Kroah-Hartman92d01272010-05-03 16:32:28 -07002474static void comedi_device_cleanup(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -08002475{
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002476 if (dev == NULL)
2477 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002478 mutex_lock(&dev->mutex);
2479 comedi_device_detach(dev);
2480 mutex_unlock(&dev->mutex);
2481 mutex_destroy(&dev->mutex);
2482}
2483
2484int comedi_alloc_board_minor(struct device *hardware_device)
2485{
David Schleefed9eccb2008-11-04 20:29:31 -08002486 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002487 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002488 unsigned i;
2489
2490 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002491 if (info == NULL)
2492 return -ENOMEM;
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002493 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002494 if (info->device == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002495 kfree(info);
2496 return -ENOMEM;
2497 }
Ian Abbottc43435d2012-03-30 17:14:58 +01002498 info->hardware_device = hardware_device;
David Schleefed9eccb2008-11-04 20:29:31 -08002499 comedi_device_init(info->device);
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002500 spin_lock(&comedi_file_info_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002501 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2502 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002503 comedi_file_info_table[i] = info;
2504 break;
2505 }
2506 }
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002507 spin_unlock(&comedi_file_info_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002508 if (i == COMEDI_NUM_BOARD_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002509 comedi_device_cleanup(info->device);
2510 kfree(info->device);
2511 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302512 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002513 "comedi: error: "
2514 "ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002515 return -EBUSY;
2516 }
2517 info->device->minor = i;
Pavel Roskin0435f932011-07-06 10:15:44 -04002518 csdev = device_create(comedi_class, hardware_device,
2519 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002520 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002521 info->device->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002522 dev_set_drvdata(csdev, info);
H Hartley Sweetena5011a22012-05-09 09:20:08 -07002523
David Schleefed9eccb2008-11-04 20:29:31 -08002524 return i;
2525}
2526
2527void comedi_free_board_minor(unsigned minor)
2528{
David Schleefed9eccb2008-11-04 20:29:31 -08002529 struct comedi_device_file_info *info;
2530
2531 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002532 spin_lock(&comedi_file_info_table_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002533 info = comedi_file_info_table[minor];
2534 comedi_file_info_table[minor] = NULL;
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002535 spin_unlock(&comedi_file_info_table_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002536
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002537 if (info) {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -04002538 struct comedi_device *dev = info->device;
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002539 if (dev) {
2540 if (dev->class_dev) {
2541 device_destroy(comedi_class,
2542 MKDEV(COMEDI_MAJOR, dev->minor));
David Schleefed9eccb2008-11-04 20:29:31 -08002543 }
2544 comedi_device_cleanup(dev);
2545 kfree(dev);
2546 }
2547 kfree(info);
2548 }
2549}
2550
Ian Abbottc43435d2012-03-30 17:14:58 +01002551int comedi_find_board_minor(struct device *hardware_device)
2552{
2553 int minor;
2554 struct comedi_device_file_info *info;
2555
2556 for (minor = 0; minor < COMEDI_NUM_BOARD_MINORS; minor++) {
2557 spin_lock(&comedi_file_info_table_lock);
2558 info = comedi_file_info_table[minor];
2559 if (info && info->hardware_device == hardware_device) {
2560 spin_unlock(&comedi_file_info_table_lock);
2561 return minor;
2562 }
2563 spin_unlock(&comedi_file_info_table_lock);
2564 }
2565 return -ENODEV;
2566}
2567
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002568int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2569 struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002570{
David Schleefed9eccb2008-11-04 20:29:31 -08002571 struct comedi_device_file_info *info;
Bill Pemberton0bfbbe82009-03-16 22:05:36 -04002572 struct device *csdev;
David Schleefed9eccb2008-11-04 20:29:31 -08002573 unsigned i;
2574
2575 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002576 if (info == NULL)
2577 return -ENOMEM;
David Schleefed9eccb2008-11-04 20:29:31 -08002578 info->device = dev;
2579 info->read_subdevice = s;
2580 info->write_subdevice = s;
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002581 spin_lock(&comedi_file_info_table_lock);
Frank Mori Hess4c41f3a2008-12-09 14:47:22 +00002582 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002583 if (comedi_file_info_table[i] == NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -08002584 comedi_file_info_table[i] = info;
2585 break;
2586 }
2587 }
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002588 spin_unlock(&comedi_file_info_table_lock);
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002589 if (i == COMEDI_NUM_MINORS) {
David Schleefed9eccb2008-11-04 20:29:31 -08002590 kfree(info);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +05302591 printk(KERN_ERR
Mark21fe2ee2010-05-13 17:44:39 +08002592 "comedi: error: "
2593 "ran out of minor numbers for board device files.\n");
David Schleefed9eccb2008-11-04 20:29:31 -08002594 return -EBUSY;
2595 }
2596 s->minor = i;
Pavel Roskin0435f932011-07-06 10:15:44 -04002597 csdev = device_create(comedi_class, dev->class_dev,
2598 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2599 dev->minor, (int)(s - dev->subdevices));
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002600 if (!IS_ERR(csdev))
David Schleefed9eccb2008-11-04 20:29:31 -08002601 s->class_dev = csdev;
Frank Mori Hess883db3d2009-04-14 11:21:41 -04002602 dev_set_drvdata(csdev, info);
H Hartley Sweetena5011a22012-05-09 09:20:08 -07002603
David Schleefed9eccb2008-11-04 20:29:31 -08002604 return i;
2605}
2606
Bill Pemberton34c43922009-03-16 22:05:14 -04002607void comedi_free_subdevice_minor(struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -08002608{
David Schleefed9eccb2008-11-04 20:29:31 -08002609 struct comedi_device_file_info *info;
2610
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002611 if (s == NULL)
2612 return;
2613 if (s->minor < 0)
2614 return;
David Schleefed9eccb2008-11-04 20:29:31 -08002615
2616 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2617 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2618
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002619 spin_lock(&comedi_file_info_table_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002620 info = comedi_file_info_table[s->minor];
2621 comedi_file_info_table[s->minor] = NULL;
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002622 spin_unlock(&comedi_file_info_table_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002623
Greg Kroah-Hartman476b8472008-11-13 17:05:58 -08002624 if (s->class_dev) {
David Schleefed9eccb2008-11-04 20:29:31 -08002625 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2626 s->class_dev = NULL;
2627 }
2628 kfree(info);
2629}
2630
2631struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2632{
David Schleefed9eccb2008-11-04 20:29:31 -08002633 struct comedi_device_file_info *info;
2634
2635 BUG_ON(minor >= COMEDI_NUM_MINORS);
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002636 spin_lock(&comedi_file_info_table_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002637 info = comedi_file_info_table[minor];
Ian Abbott7cbd8f32012-03-30 17:14:57 +01002638 spin_unlock(&comedi_file_info_table_lock);
David Schleefed9eccb2008-11-04 20:29:31 -08002639 return info;
2640}
Greg Kroah-Hartman18736432010-05-01 12:02:23 -07002641EXPORT_SYMBOL_GPL(comedi_get_device_file_info);