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