David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | module/drivers.c |
| 3 | functions for manipulating drivers |
| 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 | #define _GNU_SOURCE |
| 25 | |
| 26 | #define __NO_VERSION__ |
| 27 | #include "comedi_fops.h" |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/pci.h> |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 31 | #include <linux/usb.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 32 | #include <linux/errno.h> |
| 33 | #include <linux/kernel.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/fcntl.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/ioport.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/slab.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 40 | #include <linux/highmem.h> /* for SuSE brokenness */ |
| 41 | #include <linux/vmalloc.h> |
| 42 | #include <linux/cdev.h> |
| 43 | #include <linux/dma-mapping.h> |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 44 | #include <linux/io.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 45 | |
Greg Kroah-Hartman | 242e7ad | 2010-05-03 15:20:29 -0700 | [diff] [blame] | 46 | #include "comedidev.h" |
| 47 | #include "internal.h" |
| 48 | |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 49 | static int postconfig(struct comedi_device *dev); |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 50 | static int insn_rw_emulate_bits(struct comedi_device *dev, |
| 51 | struct comedi_subdevice *s, |
| 52 | struct comedi_insn *insn, unsigned int *data); |
| 53 | static void *comedi_recognize(struct comedi_driver *driv, const char *name); |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 54 | static void comedi_report_boards(struct comedi_driver *driv); |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 55 | static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 56 | |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 57 | struct comedi_driver *comedi_drivers; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 58 | |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 59 | static void cleanup_device(struct comedi_device *dev) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 60 | { |
| 61 | int i; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 62 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 63 | |
| 64 | if (dev->subdevices) { |
| 65 | for (i = 0; i < dev->n_subdevices; i++) { |
| 66 | s = dev->subdevices + i; |
| 67 | comedi_free_subdevice_minor(s); |
| 68 | if (s->async) { |
| 69 | comedi_buf_alloc(dev, s, 0); |
| 70 | kfree(s->async); |
| 71 | } |
| 72 | } |
| 73 | kfree(dev->subdevices); |
| 74 | dev->subdevices = NULL; |
| 75 | dev->n_subdevices = 0; |
| 76 | } |
Bill Pemberton | dedd132 | 2009-03-16 22:04:18 -0400 | [diff] [blame] | 77 | kfree(dev->private); |
| 78 | dev->private = NULL; |
Greg Kroah-Hartman | 7029a87 | 2010-05-03 15:55:45 -0700 | [diff] [blame] | 79 | dev->driver = NULL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 80 | dev->board_name = NULL; |
| 81 | dev->board_ptr = NULL; |
| 82 | dev->iobase = 0; |
| 83 | dev->irq = 0; |
| 84 | dev->read_subdev = NULL; |
| 85 | dev->write_subdev = NULL; |
| 86 | dev->open = NULL; |
| 87 | dev->close = NULL; |
| 88 | comedi_set_hw_dev(dev, NULL); |
| 89 | } |
| 90 | |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 91 | static void __comedi_device_detach(struct comedi_device *dev) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 92 | { |
| 93 | dev->attached = 0; |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 94 | if (dev->driver) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 95 | dev->driver->detach(dev); |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 96 | else |
Mark Rankilor | 67b0e64 | 2010-05-06 17:36:38 +0800 | [diff] [blame] | 97 | printk(KERN_WARNING |
| 98 | "BUG: dev->driver=NULL in comedi_device_detach()\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 99 | cleanup_device(dev); |
| 100 | } |
| 101 | |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 102 | void comedi_device_detach(struct comedi_device *dev) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 103 | { |
| 104 | if (!dev->attached) |
| 105 | return; |
| 106 | __comedi_device_detach(dev); |
| 107 | } |
| 108 | |
Bill Pemberton | 0707bb0 | 2009-03-16 22:06:20 -0400 | [diff] [blame] | 109 | int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 110 | { |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 111 | struct comedi_driver *driv; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 112 | int ret; |
| 113 | |
| 114 | if (dev->attached) |
| 115 | return -EBUSY; |
| 116 | |
| 117 | for (driv = comedi_drivers; driv; driv = driv->next) { |
| 118 | if (!try_module_get(driv->module)) { |
Xenofon Foukas | 88ab8a8 | 2010-12-04 00:17:55 +0200 | [diff] [blame] | 119 | printk(KERN_INFO "comedi: failed to increment module count, skipping\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | if (driv->num_names) { |
| 123 | dev->board_ptr = comedi_recognize(driv, it->board_name); |
| 124 | if (dev->board_ptr == NULL) { |
| 125 | module_put(driv->module); |
| 126 | continue; |
| 127 | } |
| 128 | } else { |
| 129 | if (strcmp(driv->driver_name, it->board_name)) { |
| 130 | module_put(driv->module); |
| 131 | continue; |
| 132 | } |
| 133 | } |
Zachary Richey | ac4898a | 2010-04-26 07:49:37 -0400 | [diff] [blame] | 134 | /* initialize dev->driver here so |
| 135 | * comedi_error() can be called from attach */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 136 | dev->driver = driv; |
| 137 | ret = driv->attach(dev, it); |
| 138 | if (ret < 0) { |
| 139 | module_put(dev->driver->module); |
| 140 | __comedi_device_detach(dev); |
| 141 | return ret; |
| 142 | } |
| 143 | goto attached; |
| 144 | } |
| 145 | |
Bill Pemberton | b6c7775 | 2009-03-16 22:03:24 -0400 | [diff] [blame] | 146 | /* recognize has failed if we get here */ |
| 147 | /* report valid board names before returning error */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 148 | for (driv = comedi_drivers; driv; driv = driv->next) { |
| 149 | if (!try_module_get(driv->module)) { |
Mark Rankilor | 67b0e64 | 2010-05-06 17:36:38 +0800 | [diff] [blame] | 150 | printk(KERN_INFO |
| 151 | "comedi: failed to increment module count\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 152 | continue; |
| 153 | } |
| 154 | comedi_report_boards(driv); |
| 155 | module_put(driv->module); |
| 156 | } |
| 157 | return -EIO; |
| 158 | |
| 159 | attached: |
| 160 | /* do a little post-config cleanup */ |
| 161 | ret = postconfig(dev); |
| 162 | module_put(dev->driver->module); |
| 163 | if (ret < 0) { |
| 164 | __comedi_device_detach(dev); |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | if (!dev->board_name) { |
Mark Rankilor | aad4029 | 2010-05-03 18:07:36 +0800 | [diff] [blame] | 169 | printk(KERN_WARNING "BUG: dev->board_name=<%p>\n", |
| 170 | dev->board_name); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 171 | dev->board_name = "BUG"; |
| 172 | } |
| 173 | smp_wmb(); |
| 174 | dev->attached = 1; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 179 | int comedi_driver_register(struct comedi_driver *driver) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 180 | { |
| 181 | driver->next = comedi_drivers; |
| 182 | comedi_drivers = driver; |
| 183 | |
| 184 | return 0; |
| 185 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 186 | EXPORT_SYMBOL(comedi_driver_register); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 187 | |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 188 | int comedi_driver_unregister(struct comedi_driver *driver) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 189 | { |
Bill Pemberton | 139dfbd | 2009-03-16 22:05:25 -0400 | [diff] [blame] | 190 | struct comedi_driver *prev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 191 | int i; |
| 192 | |
| 193 | /* check for devices using this driver */ |
| 194 | for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 195 | struct comedi_device_file_info *dev_file_info = |
| 196 | comedi_get_device_file_info(i); |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 197 | struct comedi_device *dev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 198 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 199 | if (dev_file_info == NULL) |
| 200 | continue; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 201 | dev = dev_file_info->device; |
| 202 | |
| 203 | mutex_lock(&dev->mutex); |
| 204 | if (dev->attached && dev->driver == driver) { |
| 205 | if (dev->use_count) |
Xenofon Foukas | 88ab8a8 | 2010-12-04 00:17:55 +0200 | [diff] [blame] | 206 | printk(KERN_WARNING "BUG! detaching device with use_count=%d\n", |
| 207 | dev->use_count); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 208 | comedi_device_detach(dev); |
| 209 | } |
| 210 | mutex_unlock(&dev->mutex); |
| 211 | } |
| 212 | |
| 213 | if (comedi_drivers == driver) { |
| 214 | comedi_drivers = driver->next; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | for (prev = comedi_drivers; prev->next; prev = prev->next) { |
| 219 | if (prev->next == driver) { |
| 220 | prev->next = driver->next; |
| 221 | return 0; |
| 222 | } |
| 223 | } |
| 224 | return -EINVAL; |
| 225 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 226 | EXPORT_SYMBOL(comedi_driver_unregister); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 227 | |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 228 | static int postconfig(struct comedi_device *dev) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 229 | { |
| 230 | int i; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 231 | struct comedi_subdevice *s; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 232 | struct comedi_async *async = NULL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 233 | int ret; |
| 234 | |
| 235 | for (i = 0; i < dev->n_subdevices; i++) { |
| 236 | s = dev->subdevices + i; |
| 237 | |
| 238 | if (s->type == COMEDI_SUBD_UNUSED) |
| 239 | continue; |
| 240 | |
| 241 | if (s->len_chanlist == 0) |
| 242 | s->len_chanlist = 1; |
| 243 | |
| 244 | if (s->do_cmd) { |
| 245 | BUG_ON((s->subdev_flags & (SDF_CMD_READ | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 246 | SDF_CMD_WRITE)) == 0); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 247 | BUG_ON(!s->do_cmdtest); |
| 248 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 249 | async = |
| 250 | kzalloc(sizeof(struct comedi_async), GFP_KERNEL); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 251 | if (async == NULL) { |
Mark Rankilor | 67b0e64 | 2010-05-06 17:36:38 +0800 | [diff] [blame] | 252 | printk(KERN_INFO |
| 253 | "failed to allocate async struct\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 254 | return -ENOMEM; |
| 255 | } |
| 256 | init_waitqueue_head(&async->wait_head); |
| 257 | async->subdevice = s; |
| 258 | s->async = async; |
| 259 | |
| 260 | #define DEFAULT_BUF_MAXSIZE (64*1024) |
| 261 | #define DEFAULT_BUF_SIZE (64*1024) |
| 262 | |
| 263 | async->max_bufsize = DEFAULT_BUF_MAXSIZE; |
| 264 | |
| 265 | async->prealloc_buf = NULL; |
| 266 | async->prealloc_bufsz = 0; |
| 267 | if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) { |
Zachary Richey | ac4898a | 2010-04-26 07:49:37 -0400 | [diff] [blame] | 268 | printk(KERN_INFO "Buffer allocation failed\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 269 | return -ENOMEM; |
| 270 | } |
| 271 | if (s->buf_change) { |
| 272 | ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE); |
| 273 | if (ret < 0) |
| 274 | return ret; |
| 275 | } |
| 276 | comedi_alloc_subdevice_minor(dev, s); |
| 277 | } |
| 278 | |
| 279 | if (!s->range_table && !s->range_table_list) |
| 280 | s->range_table = &range_unknown; |
| 281 | |
| 282 | if (!s->insn_read && s->insn_bits) |
| 283 | s->insn_read = insn_rw_emulate_bits; |
| 284 | if (!s->insn_write && s->insn_bits) |
| 285 | s->insn_write = insn_rw_emulate_bits; |
| 286 | |
| 287 | if (!s->insn_read) |
| 288 | s->insn_read = insn_inval; |
| 289 | if (!s->insn_write) |
| 290 | s->insn_write = insn_inval; |
| 291 | if (!s->insn_bits) |
| 292 | s->insn_bits = insn_inval; |
| 293 | if (!s->insn_config) |
| 294 | s->insn_config = insn_inval; |
| 295 | |
| 296 | if (!s->poll) |
| 297 | s->poll = poll_invalid; |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
Zachary Richey | ac4898a | 2010-04-26 07:49:37 -0400 | [diff] [blame] | 303 | /* generic recognize function for drivers |
| 304 | * that register their supported board names */ |
Greg Kroah-Hartman | 7029a87 | 2010-05-03 15:55:45 -0700 | [diff] [blame] | 305 | static void *comedi_recognize(struct comedi_driver *driv, const char *name) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 306 | { |
| 307 | unsigned i; |
| 308 | const char *const *name_ptr = driv->board_name; |
| 309 | for (i = 0; i < driv->num_names; i++) { |
| 310 | if (strcmp(*name_ptr, name) == 0) |
| 311 | return (void *)name_ptr; |
| 312 | name_ptr = |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 313 | (const char *const *)((const char *)name_ptr + |
| 314 | driv->offset); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return NULL; |
| 318 | } |
| 319 | |
Greg Kroah-Hartman | 7029a87 | 2010-05-03 15:55:45 -0700 | [diff] [blame] | 320 | static void comedi_report_boards(struct comedi_driver *driv) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 321 | { |
| 322 | unsigned int i; |
| 323 | const char *const *name_ptr; |
| 324 | |
Zachary Richey | ac4898a | 2010-04-26 07:49:37 -0400 | [diff] [blame] | 325 | printk(KERN_INFO "comedi: valid board names for %s driver are:\n", |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 326 | driv->driver_name); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 327 | |
| 328 | name_ptr = driv->board_name; |
| 329 | for (i = 0; i < driv->num_names; i++) { |
Zachary Richey | ac4898a | 2010-04-26 07:49:37 -0400 | [diff] [blame] | 330 | printk(KERN_INFO " %s\n", *name_ptr); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 331 | name_ptr = (const char **)((char *)name_ptr + driv->offset); |
| 332 | } |
| 333 | |
| 334 | if (driv->num_names == 0) |
Zachary Richey | ac4898a | 2010-04-26 07:49:37 -0400 | [diff] [blame] | 335 | printk(KERN_INFO " %s\n", driv->driver_name); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 338 | static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 339 | { |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 343 | int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 344 | struct comedi_insn *insn, unsigned int *data) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 345 | { |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 349 | static int insn_rw_emulate_bits(struct comedi_device *dev, |
| 350 | struct comedi_subdevice *s, |
| 351 | struct comedi_insn *insn, unsigned int *data) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 352 | { |
Bill Pemberton | 90035c0 | 2009-03-16 22:05:53 -0400 | [diff] [blame] | 353 | struct comedi_insn new_insn; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 354 | int ret; |
| 355 | static const unsigned channels_per_bitfield = 32; |
| 356 | |
| 357 | unsigned chan = CR_CHAN(insn->chanspec); |
| 358 | const unsigned base_bitfield_channel = |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 359 | (chan < channels_per_bitfield) ? 0 : chan; |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 360 | unsigned int new_data[2]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 361 | memset(new_data, 0, sizeof(new_data)); |
| 362 | memset(&new_insn, 0, sizeof(new_insn)); |
| 363 | new_insn.insn = INSN_BITS; |
| 364 | new_insn.chanspec = base_bitfield_channel; |
| 365 | new_insn.n = 2; |
| 366 | new_insn.data = new_data; |
| 367 | new_insn.subdev = insn->subdev; |
| 368 | |
| 369 | if (insn->insn == INSN_WRITE) { |
| 370 | if (!(s->subdev_flags & SDF_WRITABLE)) |
| 371 | return -EINVAL; |
Mark Rankilor | aad4029 | 2010-05-03 18:07:36 +0800 | [diff] [blame] | 372 | new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */ |
| 373 | new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) |
| 374 | : 0; /* bits */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | ret = s->insn_bits(dev, s, &new_insn, new_data); |
| 378 | if (ret < 0) |
| 379 | return ret; |
| 380 | |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 381 | if (insn->insn == INSN_READ) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 382 | data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 383 | |
| 384 | return 1; |
| 385 | } |
| 386 | |
Mithlesh Thukral | d43d27a | 2009-09-21 11:53:27 -0700 | [diff] [blame] | 387 | static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 388 | { |
| 389 | unsigned long ret = 0UL; |
| 390 | pmd_t *pmd; |
| 391 | pte_t *ptep, pte; |
| 392 | pud_t *pud; |
| 393 | |
| 394 | if (!pgd_none(*pgd)) { |
| 395 | pud = pud_offset(pgd, adr); |
| 396 | pmd = pmd_offset(pud, adr); |
| 397 | if (!pmd_none(*pmd)) { |
| 398 | ptep = pte_offset_kernel(pmd, adr); |
| 399 | pte = *ptep; |
| 400 | if (pte_present(pte)) { |
| 401 | ret = (unsigned long) |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 402 | page_address(pte_page(pte)); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 403 | ret |= (adr & (PAGE_SIZE - 1)); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | static inline unsigned long kvirt_to_kva(unsigned long adr) |
| 411 | { |
| 412 | unsigned long va, kva; |
| 413 | |
| 414 | va = adr; |
| 415 | kva = uvirt_to_kva(pgd_offset_k(va), va); |
| 416 | |
| 417 | return kva; |
| 418 | } |
| 419 | |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 420 | int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 421 | unsigned long new_size) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 422 | { |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 423 | struct comedi_async *async = s->async; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 424 | |
| 425 | /* Round up new_size to multiple of PAGE_SIZE */ |
| 426 | new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK; |
| 427 | |
| 428 | /* if no change is required, do nothing */ |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 429 | if (async->prealloc_buf && async->prealloc_bufsz == new_size) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 430 | return 0; |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 431 | |
Bill Pemberton | b6c7775 | 2009-03-16 22:03:24 -0400 | [diff] [blame] | 432 | /* deallocate old buffer */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 433 | if (async->prealloc_buf) { |
| 434 | vunmap(async->prealloc_buf); |
| 435 | async->prealloc_buf = NULL; |
| 436 | async->prealloc_bufsz = 0; |
| 437 | } |
| 438 | if (async->buf_page_list) { |
| 439 | unsigned i; |
| 440 | for (i = 0; i < async->n_buf_pages; ++i) { |
| 441 | if (async->buf_page_list[i].virt_addr) { |
Xenofon Foukas | 88ab8a8 | 2010-12-04 00:17:55 +0200 | [diff] [blame] | 442 | clear_bit(PG_reserved, |
| 443 | &(virt_to_page(async->buf_page_list[i]. |
| 444 | virt_addr)->flags)); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 445 | if (s->async_dma_dir != DMA_NONE) { |
| 446 | dma_free_coherent(dev->hw_dev, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 447 | PAGE_SIZE, |
| 448 | async-> |
| 449 | buf_page_list |
| 450 | [i].virt_addr, |
| 451 | async-> |
| 452 | buf_page_list |
| 453 | [i].dma_addr); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 454 | } else { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 455 | free_page((unsigned long) |
| 456 | async->buf_page_list[i]. |
| 457 | virt_addr); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | vfree(async->buf_page_list); |
| 462 | async->buf_page_list = NULL; |
| 463 | async->n_buf_pages = 0; |
| 464 | } |
Bill Pemberton | b6c7775 | 2009-03-16 22:03:24 -0400 | [diff] [blame] | 465 | /* allocate new buffer */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 466 | if (new_size) { |
| 467 | unsigned i = 0; |
| 468 | unsigned n_pages = new_size >> PAGE_SHIFT; |
| 469 | struct page **pages = NULL; |
| 470 | |
| 471 | async->buf_page_list = |
Joe Perches | 5b84cc7 | 2010-11-04 20:07:59 -0700 | [diff] [blame] | 472 | vzalloc(sizeof(struct comedi_buf_page) * n_pages); |
Micha Hergarden | 3ad4e21 | 2011-02-02 21:25:07 +0100 | [diff] [blame] | 473 | if (async->buf_page_list) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 474 | pages = vmalloc(sizeof(struct page *) * n_pages); |
Micha Hergarden | 3ad4e21 | 2011-02-02 21:25:07 +0100 | [diff] [blame] | 475 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 476 | if (pages) { |
| 477 | for (i = 0; i < n_pages; i++) { |
| 478 | if (s->async_dma_dir != DMA_NONE) { |
| 479 | async->buf_page_list[i].virt_addr = |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 480 | dma_alloc_coherent(dev->hw_dev, |
| 481 | PAGE_SIZE, |
| 482 | &async-> |
| 483 | buf_page_list |
| 484 | [i].dma_addr, |
| 485 | GFP_KERNEL | |
| 486 | __GFP_COMP); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 487 | } else { |
| 488 | async->buf_page_list[i].virt_addr = |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 489 | (void *) |
| 490 | get_zeroed_page(GFP_KERNEL); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 491 | } |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 492 | if (async->buf_page_list[i].virt_addr == NULL) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 493 | break; |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 494 | |
Greg Kroah-Hartman | be29eac | 2010-04-30 15:55:39 -0700 | [diff] [blame] | 495 | set_bit(PG_reserved, |
Xenofon Foukas | 88ab8a8 | 2010-12-04 00:17:55 +0200 | [diff] [blame] | 496 | &(virt_to_page(async->buf_page_list[i]. |
| 497 | virt_addr)->flags)); |
| 498 | pages[i] = virt_to_page(async->buf_page_list[i]. |
| 499 | virt_addr); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | if (i == n_pages) { |
| 503 | async->prealloc_buf = |
Greg Kroah-Hartman | 408093d | 2011-06-09 12:13:08 -0700 | [diff] [blame] | 504 | #ifdef PAGE_KERNEL_NOCACHE |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 505 | vmap(pages, n_pages, VM_MAP, PAGE_KERNEL_NOCACHE); |
Greg Kroah-Hartman | 408093d | 2011-06-09 12:13:08 -0700 | [diff] [blame] | 506 | #else |
| 507 | vmap(pages, n_pages, VM_MAP, PAGE_KERNEL); |
| 508 | #endif |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 509 | } |
Figo.zhang | b455073 | 2009-06-06 19:11:31 +0800 | [diff] [blame] | 510 | vfree(pages); |
| 511 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 512 | if (async->prealloc_buf == NULL) { |
| 513 | /* Some allocation failed above. */ |
| 514 | if (async->buf_page_list) { |
| 515 | for (i = 0; i < n_pages; i++) { |
| 516 | if (async->buf_page_list[i].virt_addr == |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 517 | NULL) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 518 | break; |
| 519 | } |
Xenofon Foukas | 88ab8a8 | 2010-12-04 00:17:55 +0200 | [diff] [blame] | 520 | clear_bit(PG_reserved, |
| 521 | &(virt_to_page(async-> |
| 522 | buf_page_list[i]. |
| 523 | virt_addr)->flags)); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 524 | if (s->async_dma_dir != DMA_NONE) { |
| 525 | dma_free_coherent(dev->hw_dev, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 526 | PAGE_SIZE, |
| 527 | async-> |
| 528 | buf_page_list |
| 529 | [i].virt_addr, |
| 530 | async-> |
| 531 | buf_page_list |
| 532 | [i].dma_addr); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 533 | } else { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 534 | free_page((unsigned long) |
| 535 | async->buf_page_list |
| 536 | [i].virt_addr); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | vfree(async->buf_page_list); |
| 540 | async->buf_page_list = NULL; |
| 541 | } |
| 542 | return -ENOMEM; |
| 543 | } |
| 544 | async->n_buf_pages = n_pages; |
| 545 | } |
| 546 | async->prealloc_bufsz = new_size; |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | /* munging is applied to data by core as it passes between user |
| 552 | * and kernel space */ |
Greg Kroah-Hartman | 7029a87 | 2010-05-03 15:55:45 -0700 | [diff] [blame] | 553 | static unsigned int comedi_buf_munge(struct comedi_async *async, |
| 554 | unsigned int num_bytes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 555 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 556 | struct comedi_subdevice *s = async->subdevice; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 557 | unsigned int count = 0; |
| 558 | const unsigned num_sample_bytes = bytes_per_sample(s); |
| 559 | |
| 560 | if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) { |
| 561 | async->munge_count += num_bytes; |
Stoyan Gaydarov | 2961f24 | 2009-03-10 00:10:27 -0500 | [diff] [blame] | 562 | BUG_ON((int)(async->munge_count - async->buf_write_count) > 0); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 563 | return num_bytes; |
| 564 | } |
| 565 | /* don't munge partial samples */ |
| 566 | num_bytes -= num_bytes % num_sample_bytes; |
| 567 | while (count < num_bytes) { |
| 568 | int block_size; |
| 569 | |
| 570 | block_size = num_bytes - count; |
| 571 | if (block_size < 0) { |
Mark Rankilor | 67b0e64 | 2010-05-06 17:36:38 +0800 | [diff] [blame] | 572 | printk(KERN_WARNING |
| 573 | "%s: %s: bug! block_size is negative\n", |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 574 | __FILE__, __func__); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 575 | break; |
| 576 | } |
| 577 | if ((int)(async->munge_ptr + block_size - |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 578 | async->prealloc_bufsz) > 0) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 579 | block_size = async->prealloc_bufsz - async->munge_ptr; |
| 580 | |
| 581 | s->munge(s->device, s, async->prealloc_buf + async->munge_ptr, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 582 | block_size, async->munge_chan); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 583 | |
Zachary Richey | ac4898a | 2010-04-26 07:49:37 -0400 | [diff] [blame] | 584 | smp_wmb(); /* barrier insures data is munged in buffer |
| 585 | * before munge_count is incremented */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 586 | |
| 587 | async->munge_chan += block_size / num_sample_bytes; |
| 588 | async->munge_chan %= async->cmd.chanlist_len; |
| 589 | async->munge_count += block_size; |
| 590 | async->munge_ptr += block_size; |
| 591 | async->munge_ptr %= async->prealloc_bufsz; |
| 592 | count += block_size; |
| 593 | } |
Stoyan Gaydarov | 2961f24 | 2009-03-10 00:10:27 -0500 | [diff] [blame] | 594 | BUG_ON((int)(async->munge_count - async->buf_write_count) > 0); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 595 | return count; |
| 596 | } |
| 597 | |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 598 | unsigned int comedi_buf_write_n_available(struct comedi_async *async) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 599 | { |
| 600 | unsigned int free_end; |
| 601 | unsigned int nbytes; |
| 602 | |
| 603 | if (async == NULL) |
| 604 | return 0; |
| 605 | |
| 606 | free_end = async->buf_read_count + async->prealloc_bufsz; |
| 607 | nbytes = free_end - async->buf_write_alloc_count; |
| 608 | nbytes -= nbytes % bytes_per_sample(async->subdevice); |
| 609 | /* barrier insures the read of buf_read_count in this |
| 610 | query occurs before any following writes to the buffer which |
| 611 | might be based on the return value from this query. |
| 612 | */ |
| 613 | smp_mb(); |
| 614 | return nbytes; |
| 615 | } |
| 616 | |
| 617 | /* allocates chunk for the writer from free buffer space */ |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 618 | unsigned int comedi_buf_write_alloc(struct comedi_async *async, |
| 619 | unsigned int nbytes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 620 | { |
| 621 | unsigned int free_end = async->buf_read_count + async->prealloc_bufsz; |
| 622 | |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 623 | if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 624 | nbytes = free_end - async->buf_write_alloc_count; |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 625 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 626 | async->buf_write_alloc_count += nbytes; |
| 627 | /* barrier insures the read of buf_read_count above occurs before |
| 628 | we write data to the write-alloc'ed buffer space */ |
| 629 | smp_mb(); |
| 630 | return nbytes; |
| 631 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 632 | EXPORT_SYMBOL(comedi_buf_write_alloc); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 633 | |
| 634 | /* allocates nothing unless it can completely fulfill the request */ |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 635 | unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 636 | unsigned int nbytes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 637 | { |
| 638 | unsigned int free_end = async->buf_read_count + async->prealloc_bufsz; |
| 639 | |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 640 | if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 641 | nbytes = 0; |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 642 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 643 | async->buf_write_alloc_count += nbytes; |
| 644 | /* barrier insures the read of buf_read_count above occurs before |
| 645 | we write data to the write-alloc'ed buffer space */ |
| 646 | smp_mb(); |
| 647 | return nbytes; |
| 648 | } |
| 649 | |
| 650 | /* transfers a chunk from writer to filled buffer space */ |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 651 | unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 652 | { |
| 653 | if ((int)(async->buf_write_count + nbytes - |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 654 | async->buf_write_alloc_count) > 0) { |
Xenofon Foukas | 88ab8a8 | 2010-12-04 00:17:55 +0200 | [diff] [blame] | 655 | printk(KERN_INFO "comedi: attempted to write-free more bytes than have been write-allocated.\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 656 | nbytes = async->buf_write_alloc_count - async->buf_write_count; |
| 657 | } |
| 658 | async->buf_write_count += nbytes; |
| 659 | async->buf_write_ptr += nbytes; |
| 660 | comedi_buf_munge(async, async->buf_write_count - async->munge_count); |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 661 | if (async->buf_write_ptr >= async->prealloc_bufsz) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 662 | async->buf_write_ptr %= async->prealloc_bufsz; |
Andrea Gelmini | 5617f9d | 2010-02-26 17:37:00 +0100 | [diff] [blame] | 663 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 664 | return nbytes; |
| 665 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 666 | EXPORT_SYMBOL(comedi_buf_write_free); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 667 | |
| 668 | /* allocates a chunk for the reader from filled (and munged) buffer space */ |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 669 | unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 670 | { |
| 671 | if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) > |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 672 | 0) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 673 | nbytes = async->munge_count - async->buf_read_alloc_count; |
| 674 | } |
| 675 | async->buf_read_alloc_count += nbytes; |
| 676 | /* barrier insures read of munge_count occurs before we actually read |
| 677 | data out of buffer */ |
| 678 | smp_rmb(); |
| 679 | return nbytes; |
| 680 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 681 | EXPORT_SYMBOL(comedi_buf_read_alloc); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 682 | |
| 683 | /* transfers control of a chunk from reader to free buffer space */ |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 684 | unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 685 | { |
Zachary Richey | ac4898a | 2010-04-26 07:49:37 -0400 | [diff] [blame] | 686 | /* barrier insures data has been read out of |
| 687 | * buffer before read count is incremented */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 688 | smp_mb(); |
| 689 | if ((int)(async->buf_read_count + nbytes - |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 690 | async->buf_read_alloc_count) > 0) { |
Mark Rankilor | 67b0e64 | 2010-05-06 17:36:38 +0800 | [diff] [blame] | 691 | printk(KERN_INFO |
| 692 | "comedi: attempted to read-free more bytes than have been read-allocated.\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 693 | nbytes = async->buf_read_alloc_count - async->buf_read_count; |
| 694 | } |
| 695 | async->buf_read_count += nbytes; |
| 696 | async->buf_read_ptr += nbytes; |
| 697 | async->buf_read_ptr %= async->prealloc_bufsz; |
| 698 | return nbytes; |
| 699 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 700 | EXPORT_SYMBOL(comedi_buf_read_free); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 701 | |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 702 | void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 703 | const void *data, unsigned int num_bytes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 704 | { |
| 705 | unsigned int write_ptr = async->buf_write_ptr + offset; |
| 706 | |
| 707 | if (write_ptr >= async->prealloc_bufsz) |
| 708 | write_ptr %= async->prealloc_bufsz; |
| 709 | |
| 710 | while (num_bytes) { |
| 711 | unsigned int block_size; |
| 712 | |
| 713 | if (write_ptr + num_bytes > async->prealloc_bufsz) |
| 714 | block_size = async->prealloc_bufsz - write_ptr; |
| 715 | else |
| 716 | block_size = num_bytes; |
| 717 | |
| 718 | memcpy(async->prealloc_buf + write_ptr, data, block_size); |
| 719 | |
| 720 | data += block_size; |
| 721 | num_bytes -= block_size; |
| 722 | |
| 723 | write_ptr = 0; |
| 724 | } |
| 725 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 726 | EXPORT_SYMBOL(comedi_buf_memcpy_to); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 727 | |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 728 | void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 729 | void *dest, unsigned int nbytes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 730 | { |
| 731 | void *src; |
| 732 | unsigned int read_ptr = async->buf_read_ptr + offset; |
| 733 | |
| 734 | if (read_ptr >= async->prealloc_bufsz) |
| 735 | read_ptr %= async->prealloc_bufsz; |
| 736 | |
| 737 | while (nbytes) { |
| 738 | unsigned int block_size; |
| 739 | |
| 740 | src = async->prealloc_buf + read_ptr; |
| 741 | |
| 742 | if (nbytes >= async->prealloc_bufsz - read_ptr) |
| 743 | block_size = async->prealloc_bufsz - read_ptr; |
| 744 | else |
| 745 | block_size = nbytes; |
| 746 | |
| 747 | memcpy(dest, src, block_size); |
| 748 | nbytes -= block_size; |
| 749 | dest += block_size; |
| 750 | read_ptr = 0; |
| 751 | } |
| 752 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 753 | EXPORT_SYMBOL(comedi_buf_memcpy_from); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 754 | |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 755 | unsigned int comedi_buf_read_n_available(struct comedi_async *async) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 756 | { |
| 757 | unsigned num_bytes; |
| 758 | |
| 759 | if (async == NULL) |
| 760 | return 0; |
| 761 | num_bytes = async->munge_count - async->buf_read_count; |
| 762 | /* barrier insures the read of munge_count in this |
| 763 | query occurs before any following reads of the buffer which |
| 764 | might be based on the return value from this query. |
| 765 | */ |
| 766 | smp_rmb(); |
| 767 | return num_bytes; |
| 768 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 769 | EXPORT_SYMBOL(comedi_buf_read_n_available); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 770 | |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 771 | int comedi_buf_get(struct comedi_async *async, short *x) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 772 | { |
| 773 | unsigned int n = comedi_buf_read_n_available(async); |
| 774 | |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 775 | if (n < sizeof(short)) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 776 | return 0; |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 777 | comedi_buf_read_alloc(async, sizeof(short)); |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 778 | *x = *(short *)(async->prealloc_buf + async->buf_read_ptr); |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 779 | comedi_buf_read_free(async, sizeof(short)); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 780 | return 1; |
| 781 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 782 | EXPORT_SYMBOL(comedi_buf_get); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 783 | |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 784 | int comedi_buf_put(struct comedi_async *async, short x) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 785 | { |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 786 | unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short)); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 787 | |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 788 | if (n < sizeof(short)) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 789 | async->events |= COMEDI_CB_ERROR; |
| 790 | return 0; |
| 791 | } |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 792 | *(short *)(async->prealloc_buf + async->buf_write_ptr) = x; |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 793 | comedi_buf_write_free(async, sizeof(short)); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 794 | return 1; |
| 795 | } |
Greg Kroah-Hartman | d58214b | 2010-05-01 11:54:07 -0700 | [diff] [blame] | 796 | EXPORT_SYMBOL(comedi_buf_put); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 797 | |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 798 | void comedi_reset_async_buf(struct comedi_async *async) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 799 | { |
| 800 | async->buf_write_alloc_count = 0; |
| 801 | async->buf_write_count = 0; |
| 802 | async->buf_read_alloc_count = 0; |
| 803 | async->buf_read_count = 0; |
| 804 | |
| 805 | async->buf_write_ptr = 0; |
| 806 | async->buf_read_ptr = 0; |
| 807 | |
| 808 | async->cur_chan = 0; |
| 809 | async->scan_progress = 0; |
| 810 | async->munge_chan = 0; |
| 811 | async->munge_count = 0; |
| 812 | async->munge_ptr = 0; |
| 813 | |
| 814 | async->events = 0; |
| 815 | } |
| 816 | |
Greg Kroah-Hartman | 7029a87 | 2010-05-03 15:55:45 -0700 | [diff] [blame] | 817 | static int comedi_auto_config(struct device *hardware_device, |
| 818 | const char *board_name, const int *options, |
| 819 | unsigned num_options) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 820 | { |
Bill Pemberton | 0707bb0 | 2009-03-16 22:06:20 -0400 | [diff] [blame] | 821 | struct comedi_devconfig it; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 822 | int minor; |
| 823 | struct comedi_device_file_info *dev_file_info; |
| 824 | int retval; |
| 825 | |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 826 | if (!comedi_autoconfig) |
Ian Abbott | 719548e | 2008-12-09 11:07:22 +0000 | [diff] [blame] | 827 | return 0; |
Ian Abbott | 6a9d7a2 | 2008-12-08 17:05:50 +0000 | [diff] [blame] | 828 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 829 | minor = comedi_alloc_board_minor(hardware_device); |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 830 | if (minor < 0) |
| 831 | return minor; |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 832 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 833 | dev_file_info = comedi_get_device_file_info(minor); |
| 834 | |
| 835 | memset(&it, 0, sizeof(it)); |
| 836 | strncpy(it.board_name, board_name, COMEDI_NAMELEN); |
| 837 | it.board_name[COMEDI_NAMELEN - 1] = '\0'; |
| 838 | BUG_ON(num_options > COMEDI_NDEVCONFOPTS); |
| 839 | memcpy(it.options, options, num_options * sizeof(int)); |
| 840 | |
| 841 | mutex_lock(&dev_file_info->device->mutex); |
| 842 | retval = comedi_device_attach(dev_file_info->device, &it); |
| 843 | mutex_unlock(&dev_file_info->device->mutex); |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 844 | |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 845 | if (retval < 0) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 846 | comedi_free_board_minor(minor); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 847 | return retval; |
| 848 | } |
| 849 | |
Greg Kroah-Hartman | 7029a87 | 2010-05-03 15:55:45 -0700 | [diff] [blame] | 850 | static void comedi_auto_unconfig(struct device *hardware_device) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 851 | { |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 852 | int minor; |
| 853 | |
| 854 | if (hardware_device == NULL) |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 855 | return; |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 856 | minor = comedi_find_board_minor(hardware_device); |
| 857 | if (minor < 0) |
| 858 | return; |
| 859 | BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS); |
| 860 | comedi_free_board_minor(minor); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Ian Abbott | 4c093a6 | 2012-03-30 17:14:56 +0100 | [diff] [blame] | 863 | int comedi_pci_auto_config(struct pci_dev *pcidev, struct comedi_driver *driver) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 864 | { |
| 865 | int options[2]; |
| 866 | |
Bill Pemberton | b6c7775 | 2009-03-16 22:03:24 -0400 | [diff] [blame] | 867 | /* pci bus */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 868 | options[0] = pcidev->bus->number; |
Bill Pemberton | b6c7775 | 2009-03-16 22:03:24 -0400 | [diff] [blame] | 869 | /* pci slot */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 870 | options[1] = PCI_SLOT(pcidev->devfn); |
| 871 | |
Ian Abbott | 4c093a6 | 2012-03-30 17:14:56 +0100 | [diff] [blame] | 872 | return comedi_auto_config(&pcidev->dev, driver->driver_name, |
Bill Pemberton | 8629efa | 2009-04-23 15:54:56 -0400 | [diff] [blame] | 873 | options, ARRAY_SIZE(options)); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 874 | } |
Greg Kroah-Hartman | 4bf9355 | 2010-05-01 12:38:02 -0700 | [diff] [blame] | 875 | EXPORT_SYMBOL_GPL(comedi_pci_auto_config); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 876 | |
| 877 | void comedi_pci_auto_unconfig(struct pci_dev *pcidev) |
| 878 | { |
| 879 | comedi_auto_unconfig(&pcidev->dev); |
| 880 | } |
Greg Kroah-Hartman | 4bf9355 | 2010-05-01 12:38:02 -0700 | [diff] [blame] | 881 | EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig); |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 882 | |
Ian Abbott | d8b6ca0 | 2012-03-30 17:14:59 +0100 | [diff] [blame^] | 883 | int comedi_usb_auto_config(struct usb_interface *intf, |
Ian Abbott | 4c093a6 | 2012-03-30 17:14:56 +0100 | [diff] [blame] | 884 | struct comedi_driver *driver) |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 885 | { |
Ian Abbott | d8b6ca0 | 2012-03-30 17:14:59 +0100 | [diff] [blame^] | 886 | BUG_ON(intf == NULL); |
| 887 | return comedi_auto_config(&intf->dev, driver->driver_name, NULL, 0); |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 888 | } |
Greg Kroah-Hartman | 4bf9355 | 2010-05-01 12:38:02 -0700 | [diff] [blame] | 889 | EXPORT_SYMBOL_GPL(comedi_usb_auto_config); |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 890 | |
Ian Abbott | d8b6ca0 | 2012-03-30 17:14:59 +0100 | [diff] [blame^] | 891 | void comedi_usb_auto_unconfig(struct usb_interface *intf) |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 892 | { |
Ian Abbott | d8b6ca0 | 2012-03-30 17:14:59 +0100 | [diff] [blame^] | 893 | BUG_ON(intf == NULL); |
| 894 | comedi_auto_unconfig(&intf->dev); |
Bernd Porr | c28264d | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 895 | } |
Greg Kroah-Hartman | 4bf9355 | 2010-05-01 12:38:02 -0700 | [diff] [blame] | 896 | EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig); |