blob: 49e53eaa8e412f12cbf257b95b7c546db18b5c96 [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
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 Porrc28264d2008-12-08 23:30:13 +000031#include <linux/usb.h>
David Schleefed9eccb2008-11-04 20:29:31 -080032#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 Schleefed9eccb2008-11-04 20:29:31 -080040#include <linux/highmem.h> /* for SuSE brokenness */
41#include <linux/vmalloc.h>
42#include <linux/cdev.h>
43#include <linux/dma-mapping.h>
Andrea Gelmini5617f9d2010-02-26 17:37:00 +010044#include <linux/io.h>
David Schleefed9eccb2008-11-04 20:29:31 -080045
Greg Kroah-Hartman242e7ad2010-05-03 15:20:29 -070046#include "comedidev.h"
47#include "internal.h"
48
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040049static int postconfig(struct comedi_device *dev);
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053050static int insn_rw_emulate_bits(struct comedi_device *dev,
51 struct comedi_subdevice *s,
52 struct comedi_insn *insn, unsigned int *data);
53static void *comedi_recognize(struct comedi_driver *driv, const char *name);
Bill Pemberton139dfbd2009-03-16 22:05:25 -040054static void comedi_report_boards(struct comedi_driver *driv);
Bill Pemberton34c43922009-03-16 22:05:14 -040055static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s);
David Schleefed9eccb2008-11-04 20:29:31 -080056
Bill Pemberton139dfbd2009-03-16 22:05:25 -040057struct comedi_driver *comedi_drivers;
David Schleefed9eccb2008-11-04 20:29:31 -080058
Bill Pemberton71b5f4f2009-03-16 22:05:08 -040059static void cleanup_device(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -080060{
61 int i;
Bill Pemberton34c43922009-03-16 22:05:14 -040062 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -080063
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 Pembertondedd1322009-03-16 22:04:18 -040077 kfree(dev->private);
78 dev->private = NULL;
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -070079 dev->driver = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -080080 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 Pemberton71b5f4f2009-03-16 22:05:08 -040091static void __comedi_device_detach(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -080092{
93 dev->attached = 0;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +010094 if (dev->driver)
David Schleefed9eccb2008-11-04 20:29:31 -080095 dev->driver->detach(dev);
Andrea Gelmini5617f9d2010-02-26 17:37:00 +010096 else
Mark Rankilor67b0e642010-05-06 17:36:38 +080097 printk(KERN_WARNING
98 "BUG: dev->driver=NULL in comedi_device_detach()\n");
David Schleefed9eccb2008-11-04 20:29:31 -080099 cleanup_device(dev);
100}
101
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400102void comedi_device_detach(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800103{
104 if (!dev->attached)
105 return;
106 __comedi_device_detach(dev);
107}
108
Ian Abbott3902a372012-03-30 17:15:00 +0100109/* do a little post-config cleanup */
110/* called with module refcount incremented, decrements it */
111static int comedi_device_postconfig(struct comedi_device *dev)
112{
113 int ret = postconfig(dev);
114 module_put(dev->driver->module);
115 if (ret < 0) {
116 __comedi_device_detach(dev);
117 return ret;
118 }
119 if (!dev->board_name) {
120 printk(KERN_WARNING "BUG: dev->board_name=<%p>\n",
121 dev->board_name);
122 dev->board_name = "BUG";
123 }
124 smp_wmb();
125 dev->attached = 1;
126 return 0;
127}
128
Bill Pemberton0707bb02009-03-16 22:06:20 -0400129int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
David Schleefed9eccb2008-11-04 20:29:31 -0800130{
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400131 struct comedi_driver *driv;
David Schleefed9eccb2008-11-04 20:29:31 -0800132 int ret;
133
134 if (dev->attached)
135 return -EBUSY;
136
137 for (driv = comedi_drivers; driv; driv = driv->next) {
138 if (!try_module_get(driv->module)) {
Xenofon Foukas88ab8a82010-12-04 00:17:55 +0200139 printk(KERN_INFO "comedi: failed to increment module count, skipping\n");
David Schleefed9eccb2008-11-04 20:29:31 -0800140 continue;
141 }
142 if (driv->num_names) {
143 dev->board_ptr = comedi_recognize(driv, it->board_name);
Ian Abbott3902a372012-03-30 17:15:00 +0100144 if (dev->board_ptr)
145 break;
146 } else if (strcmp(driv->driver_name, it->board_name))
147 break;
David Schleefed9eccb2008-11-04 20:29:31 -0800148 module_put(driv->module);
149 }
Ian Abbott3902a372012-03-30 17:15:00 +0100150 if (driv == NULL) {
151 /* recognize has failed if we get here */
152 /* report valid board names before returning error */
153 for (driv = comedi_drivers; driv; driv = driv->next) {
154 if (!try_module_get(driv->module)) {
155 printk(KERN_INFO
156 "comedi: failed to increment module count\n");
157 continue;
158 }
159 comedi_report_boards(driv);
160 module_put(driv->module);
161 }
162 return -EIO;
163 }
164 /* initialize dev->driver here so
165 * comedi_error() can be called from attach */
166 dev->driver = driv;
167 ret = driv->attach(dev, it);
David Schleefed9eccb2008-11-04 20:29:31 -0800168 if (ret < 0) {
Ian Abbott3902a372012-03-30 17:15:00 +0100169 module_put(dev->driver->module);
David Schleefed9eccb2008-11-04 20:29:31 -0800170 __comedi_device_detach(dev);
171 return ret;
172 }
Ian Abbott3902a372012-03-30 17:15:00 +0100173 return comedi_device_postconfig(dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800174}
175
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400176int comedi_driver_register(struct comedi_driver *driver)
David Schleefed9eccb2008-11-04 20:29:31 -0800177{
178 driver->next = comedi_drivers;
179 comedi_drivers = driver;
180
181 return 0;
182}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700183EXPORT_SYMBOL(comedi_driver_register);
David Schleefed9eccb2008-11-04 20:29:31 -0800184
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400185int comedi_driver_unregister(struct comedi_driver *driver)
David Schleefed9eccb2008-11-04 20:29:31 -0800186{
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400187 struct comedi_driver *prev;
David Schleefed9eccb2008-11-04 20:29:31 -0800188 int i;
189
190 /* check for devices using this driver */
191 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530192 struct comedi_device_file_info *dev_file_info =
193 comedi_get_device_file_info(i);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400194 struct comedi_device *dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800195
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530196 if (dev_file_info == NULL)
197 continue;
David Schleefed9eccb2008-11-04 20:29:31 -0800198 dev = dev_file_info->device;
199
200 mutex_lock(&dev->mutex);
201 if (dev->attached && dev->driver == driver) {
202 if (dev->use_count)
Xenofon Foukas88ab8a82010-12-04 00:17:55 +0200203 printk(KERN_WARNING "BUG! detaching device with use_count=%d\n",
204 dev->use_count);
David Schleefed9eccb2008-11-04 20:29:31 -0800205 comedi_device_detach(dev);
206 }
207 mutex_unlock(&dev->mutex);
208 }
209
210 if (comedi_drivers == driver) {
211 comedi_drivers = driver->next;
212 return 0;
213 }
214
215 for (prev = comedi_drivers; prev->next; prev = prev->next) {
216 if (prev->next == driver) {
217 prev->next = driver->next;
218 return 0;
219 }
220 }
221 return -EINVAL;
222}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700223EXPORT_SYMBOL(comedi_driver_unregister);
David Schleefed9eccb2008-11-04 20:29:31 -0800224
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400225static int postconfig(struct comedi_device *dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800226{
227 int i;
Bill Pemberton34c43922009-03-16 22:05:14 -0400228 struct comedi_subdevice *s;
Bill Pembertond1636792009-03-16 22:05:20 -0400229 struct comedi_async *async = NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800230 int ret;
231
232 for (i = 0; i < dev->n_subdevices; i++) {
233 s = dev->subdevices + i;
234
235 if (s->type == COMEDI_SUBD_UNUSED)
236 continue;
237
238 if (s->len_chanlist == 0)
239 s->len_chanlist = 1;
240
241 if (s->do_cmd) {
Ian Abbott4d7df822012-04-13 14:12:53 +0100242 unsigned int buf_size;
243
David Schleefed9eccb2008-11-04 20:29:31 -0800244 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530245 SDF_CMD_WRITE)) == 0);
David Schleefed9eccb2008-11-04 20:29:31 -0800246 BUG_ON(!s->do_cmdtest);
247
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530248 async =
249 kzalloc(sizeof(struct comedi_async), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800250 if (async == NULL) {
Mark Rankilor67b0e642010-05-06 17:36:38 +0800251 printk(KERN_INFO
252 "failed to allocate async struct\n");
David Schleefed9eccb2008-11-04 20:29:31 -0800253 return -ENOMEM;
254 }
255 init_waitqueue_head(&async->wait_head);
256 async->subdevice = s;
257 s->async = async;
258
Ian Abbott4d7df822012-04-13 14:12:53 +0100259 async->max_bufsize =
260 comedi_default_buf_maxsize_kb * 1024;
261 buf_size = comedi_default_buf_size_kb * 1024;
262 if (buf_size > async->max_bufsize)
263 buf_size = async->max_bufsize;
David Schleefed9eccb2008-11-04 20:29:31 -0800264
265 async->prealloc_buf = NULL;
266 async->prealloc_bufsz = 0;
Ian Abbott4d7df822012-04-13 14:12:53 +0100267 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
Zachary Richeyac4898a2010-04-26 07:49:37 -0400268 printk(KERN_INFO "Buffer allocation failed\n");
David Schleefed9eccb2008-11-04 20:29:31 -0800269 return -ENOMEM;
270 }
271 if (s->buf_change) {
Ian Abbott4d7df822012-04-13 14:12:53 +0100272 ret = s->buf_change(dev, s, buf_size);
David Schleefed9eccb2008-11-04 20:29:31 -0800273 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
Ian Abbott4e2f0022012-06-06 15:17:02 +0100303/*
304 * Generic recognize function for drivers that register their supported
305 * board names.
306 *
307 * 'driv->board_name' points to a 'const char *' member within the
308 * zeroth element of an array of some private board information
309 * structure, say 'struct foo_board' containing a member 'const char
310 * *board_name' that is initialized to point to a board name string that
311 * is one of the candidates matched against this function's 'name'
312 * parameter.
313 *
314 * 'driv->offset' is the size of the private board information
315 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
316 * the length of the array of private board information structures.
317 *
318 * If one of the board names in the array of private board information
319 * structures matches the name supplied to this function, the function
320 * returns a pointer to the pointer to the board name, otherwise it
321 * returns NULL. The return value ends up in the 'board_ptr' member of
322 * a 'struct comedi_device' that the low-level comedi driver's
323 * 'attach()' hook can convert to a point to a particular element of its
324 * array of private board information structures by subtracting the
325 * offset of the member that points to the board name. (No subtraction
326 * is required if the board name pointer is the first member of the
327 * private board information structure, which is generally the case.)
328 */
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700329static void *comedi_recognize(struct comedi_driver *driv, const char *name)
David Schleefed9eccb2008-11-04 20:29:31 -0800330{
Dan Carpenter1c9de582012-05-26 15:14:45 +0300331 char **name_ptr = (char **)driv->board_name;
332 int i;
333
David Schleefed9eccb2008-11-04 20:29:31 -0800334 for (i = 0; i < driv->num_names; i++) {
335 if (strcmp(*name_ptr, name) == 0)
Dan Carpenter1c9de582012-05-26 15:14:45 +0300336 return name_ptr;
337 name_ptr = (void *)name_ptr + driv->offset;
David Schleefed9eccb2008-11-04 20:29:31 -0800338 }
339
340 return NULL;
341}
342
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700343static void comedi_report_boards(struct comedi_driver *driv)
David Schleefed9eccb2008-11-04 20:29:31 -0800344{
345 unsigned int i;
346 const char *const *name_ptr;
347
Zachary Richeyac4898a2010-04-26 07:49:37 -0400348 printk(KERN_INFO "comedi: valid board names for %s driver are:\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530349 driv->driver_name);
David Schleefed9eccb2008-11-04 20:29:31 -0800350
351 name_ptr = driv->board_name;
352 for (i = 0; i < driv->num_names; i++) {
Zachary Richeyac4898a2010-04-26 07:49:37 -0400353 printk(KERN_INFO " %s\n", *name_ptr);
David Schleefed9eccb2008-11-04 20:29:31 -0800354 name_ptr = (const char **)((char *)name_ptr + driv->offset);
355 }
356
357 if (driv->num_names == 0)
Zachary Richeyac4898a2010-04-26 07:49:37 -0400358 printk(KERN_INFO " %s\n", driv->driver_name);
David Schleefed9eccb2008-11-04 20:29:31 -0800359}
360
Bill Pemberton34c43922009-03-16 22:05:14 -0400361static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
David Schleefed9eccb2008-11-04 20:29:31 -0800362{
363 return -EINVAL;
364}
365
Bill Pemberton34c43922009-03-16 22:05:14 -0400366int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530367 struct comedi_insn *insn, unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -0800368{
369 return -EINVAL;
370}
371
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530372static int insn_rw_emulate_bits(struct comedi_device *dev,
373 struct comedi_subdevice *s,
374 struct comedi_insn *insn, unsigned int *data)
David Schleefed9eccb2008-11-04 20:29:31 -0800375{
Bill Pemberton90035c02009-03-16 22:05:53 -0400376 struct comedi_insn new_insn;
David Schleefed9eccb2008-11-04 20:29:31 -0800377 int ret;
378 static const unsigned channels_per_bitfield = 32;
379
380 unsigned chan = CR_CHAN(insn->chanspec);
381 const unsigned base_bitfield_channel =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530382 (chan < channels_per_bitfield) ? 0 : chan;
Bill Pemberton790c5542009-03-16 22:05:02 -0400383 unsigned int new_data[2];
David Schleefed9eccb2008-11-04 20:29:31 -0800384 memset(new_data, 0, sizeof(new_data));
385 memset(&new_insn, 0, sizeof(new_insn));
386 new_insn.insn = INSN_BITS;
387 new_insn.chanspec = base_bitfield_channel;
388 new_insn.n = 2;
389 new_insn.data = new_data;
390 new_insn.subdev = insn->subdev;
391
392 if (insn->insn == INSN_WRITE) {
393 if (!(s->subdev_flags & SDF_WRITABLE))
394 return -EINVAL;
Mark Rankiloraad40292010-05-03 18:07:36 +0800395 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
396 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
397 : 0; /* bits */
David Schleefed9eccb2008-11-04 20:29:31 -0800398 }
399
400 ret = s->insn_bits(dev, s, &new_insn, new_data);
401 if (ret < 0)
402 return ret;
403
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100404 if (insn->insn == INSN_READ)
David Schleefed9eccb2008-11-04 20:29:31 -0800405 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
David Schleefed9eccb2008-11-04 20:29:31 -0800406
407 return 1;
408}
409
Bill Pemberton34c43922009-03-16 22:05:14 -0400410int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530411 unsigned long new_size)
David Schleefed9eccb2008-11-04 20:29:31 -0800412{
Bill Pembertond1636792009-03-16 22:05:20 -0400413 struct comedi_async *async = s->async;
David Schleefed9eccb2008-11-04 20:29:31 -0800414
415 /* Round up new_size to multiple of PAGE_SIZE */
416 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
417
418 /* if no change is required, do nothing */
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100419 if (async->prealloc_buf && async->prealloc_bufsz == new_size)
David Schleefed9eccb2008-11-04 20:29:31 -0800420 return 0;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100421
Bill Pembertonb6c77752009-03-16 22:03:24 -0400422 /* deallocate old buffer */
David Schleefed9eccb2008-11-04 20:29:31 -0800423 if (async->prealloc_buf) {
424 vunmap(async->prealloc_buf);
425 async->prealloc_buf = NULL;
426 async->prealloc_bufsz = 0;
427 }
428 if (async->buf_page_list) {
429 unsigned i;
430 for (i = 0; i < async->n_buf_pages; ++i) {
431 if (async->buf_page_list[i].virt_addr) {
Xenofon Foukas88ab8a82010-12-04 00:17:55 +0200432 clear_bit(PG_reserved,
433 &(virt_to_page(async->buf_page_list[i].
434 virt_addr)->flags));
David Schleefed9eccb2008-11-04 20:29:31 -0800435 if (s->async_dma_dir != DMA_NONE) {
436 dma_free_coherent(dev->hw_dev,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530437 PAGE_SIZE,
438 async->
439 buf_page_list
440 [i].virt_addr,
441 async->
442 buf_page_list
443 [i].dma_addr);
David Schleefed9eccb2008-11-04 20:29:31 -0800444 } else {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530445 free_page((unsigned long)
446 async->buf_page_list[i].
447 virt_addr);
David Schleefed9eccb2008-11-04 20:29:31 -0800448 }
449 }
450 }
451 vfree(async->buf_page_list);
452 async->buf_page_list = NULL;
453 async->n_buf_pages = 0;
454 }
Bill Pembertonb6c77752009-03-16 22:03:24 -0400455 /* allocate new buffer */
David Schleefed9eccb2008-11-04 20:29:31 -0800456 if (new_size) {
457 unsigned i = 0;
458 unsigned n_pages = new_size >> PAGE_SHIFT;
459 struct page **pages = NULL;
460
461 async->buf_page_list =
Joe Perches5b84cc72010-11-04 20:07:59 -0700462 vzalloc(sizeof(struct comedi_buf_page) * n_pages);
Micha Hergarden3ad4e212011-02-02 21:25:07 +0100463 if (async->buf_page_list)
David Schleefed9eccb2008-11-04 20:29:31 -0800464 pages = vmalloc(sizeof(struct page *) * n_pages);
Micha Hergarden3ad4e212011-02-02 21:25:07 +0100465
David Schleefed9eccb2008-11-04 20:29:31 -0800466 if (pages) {
467 for (i = 0; i < n_pages; i++) {
468 if (s->async_dma_dir != DMA_NONE) {
469 async->buf_page_list[i].virt_addr =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530470 dma_alloc_coherent(dev->hw_dev,
471 PAGE_SIZE,
472 &async->
473 buf_page_list
474 [i].dma_addr,
475 GFP_KERNEL |
476 __GFP_COMP);
David Schleefed9eccb2008-11-04 20:29:31 -0800477 } else {
478 async->buf_page_list[i].virt_addr =
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530479 (void *)
480 get_zeroed_page(GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800481 }
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100482 if (async->buf_page_list[i].virt_addr == NULL)
David Schleefed9eccb2008-11-04 20:29:31 -0800483 break;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100484
Greg Kroah-Hartmanbe29eac2010-04-30 15:55:39 -0700485 set_bit(PG_reserved,
Xenofon Foukas88ab8a82010-12-04 00:17:55 +0200486 &(virt_to_page(async->buf_page_list[i].
487 virt_addr)->flags));
488 pages[i] = virt_to_page(async->buf_page_list[i].
489 virt_addr);
David Schleefed9eccb2008-11-04 20:29:31 -0800490 }
491 }
492 if (i == n_pages) {
493 async->prealloc_buf =
Greg Kroah-Hartman408093d2011-06-09 12:13:08 -0700494#ifdef PAGE_KERNEL_NOCACHE
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530495 vmap(pages, n_pages, VM_MAP, PAGE_KERNEL_NOCACHE);
Greg Kroah-Hartman408093d2011-06-09 12:13:08 -0700496#else
497 vmap(pages, n_pages, VM_MAP, PAGE_KERNEL);
498#endif
David Schleefed9eccb2008-11-04 20:29:31 -0800499 }
Figo.zhangb4550732009-06-06 19:11:31 +0800500 vfree(pages);
501
David Schleefed9eccb2008-11-04 20:29:31 -0800502 if (async->prealloc_buf == NULL) {
503 /* Some allocation failed above. */
504 if (async->buf_page_list) {
505 for (i = 0; i < n_pages; i++) {
506 if (async->buf_page_list[i].virt_addr ==
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530507 NULL) {
David Schleefed9eccb2008-11-04 20:29:31 -0800508 break;
509 }
Xenofon Foukas88ab8a82010-12-04 00:17:55 +0200510 clear_bit(PG_reserved,
511 &(virt_to_page(async->
512 buf_page_list[i].
513 virt_addr)->flags));
David Schleefed9eccb2008-11-04 20:29:31 -0800514 if (s->async_dma_dir != DMA_NONE) {
515 dma_free_coherent(dev->hw_dev,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530516 PAGE_SIZE,
517 async->
518 buf_page_list
519 [i].virt_addr,
520 async->
521 buf_page_list
522 [i].dma_addr);
David Schleefed9eccb2008-11-04 20:29:31 -0800523 } else {
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530524 free_page((unsigned long)
525 async->buf_page_list
526 [i].virt_addr);
David Schleefed9eccb2008-11-04 20:29:31 -0800527 }
528 }
529 vfree(async->buf_page_list);
530 async->buf_page_list = NULL;
531 }
532 return -ENOMEM;
533 }
534 async->n_buf_pages = n_pages;
535 }
536 async->prealloc_bufsz = new_size;
537
538 return 0;
539}
540
541/* munging is applied to data by core as it passes between user
542 * and kernel space */
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700543static unsigned int comedi_buf_munge(struct comedi_async *async,
544 unsigned int num_bytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800545{
Bill Pemberton34c43922009-03-16 22:05:14 -0400546 struct comedi_subdevice *s = async->subdevice;
David Schleefed9eccb2008-11-04 20:29:31 -0800547 unsigned int count = 0;
548 const unsigned num_sample_bytes = bytes_per_sample(s);
549
550 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
551 async->munge_count += num_bytes;
Stoyan Gaydarov2961f242009-03-10 00:10:27 -0500552 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
David Schleefed9eccb2008-11-04 20:29:31 -0800553 return num_bytes;
554 }
555 /* don't munge partial samples */
556 num_bytes -= num_bytes % num_sample_bytes;
557 while (count < num_bytes) {
558 int block_size;
559
560 block_size = num_bytes - count;
561 if (block_size < 0) {
Mark Rankilor67b0e642010-05-06 17:36:38 +0800562 printk(KERN_WARNING
563 "%s: %s: bug! block_size is negative\n",
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530564 __FILE__, __func__);
David Schleefed9eccb2008-11-04 20:29:31 -0800565 break;
566 }
567 if ((int)(async->munge_ptr + block_size -
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530568 async->prealloc_bufsz) > 0)
David Schleefed9eccb2008-11-04 20:29:31 -0800569 block_size = async->prealloc_bufsz - async->munge_ptr;
570
571 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530572 block_size, async->munge_chan);
David Schleefed9eccb2008-11-04 20:29:31 -0800573
Zachary Richeyac4898a2010-04-26 07:49:37 -0400574 smp_wmb(); /* barrier insures data is munged in buffer
575 * before munge_count is incremented */
David Schleefed9eccb2008-11-04 20:29:31 -0800576
577 async->munge_chan += block_size / num_sample_bytes;
578 async->munge_chan %= async->cmd.chanlist_len;
579 async->munge_count += block_size;
580 async->munge_ptr += block_size;
581 async->munge_ptr %= async->prealloc_bufsz;
582 count += block_size;
583 }
Stoyan Gaydarov2961f242009-03-10 00:10:27 -0500584 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
David Schleefed9eccb2008-11-04 20:29:31 -0800585 return count;
586}
587
Bill Pembertond1636792009-03-16 22:05:20 -0400588unsigned int comedi_buf_write_n_available(struct comedi_async *async)
David Schleefed9eccb2008-11-04 20:29:31 -0800589{
590 unsigned int free_end;
591 unsigned int nbytes;
592
593 if (async == NULL)
594 return 0;
595
596 free_end = async->buf_read_count + async->prealloc_bufsz;
597 nbytes = free_end - async->buf_write_alloc_count;
598 nbytes -= nbytes % bytes_per_sample(async->subdevice);
599 /* barrier insures the read of buf_read_count in this
600 query occurs before any following writes to the buffer which
601 might be based on the return value from this query.
602 */
603 smp_mb();
604 return nbytes;
605}
606
607/* allocates chunk for the writer from free buffer space */
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530608unsigned int comedi_buf_write_alloc(struct comedi_async *async,
609 unsigned int nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800610{
611 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
612
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100613 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
David Schleefed9eccb2008-11-04 20:29:31 -0800614 nbytes = free_end - async->buf_write_alloc_count;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100615
David Schleefed9eccb2008-11-04 20:29:31 -0800616 async->buf_write_alloc_count += nbytes;
617 /* barrier insures the read of buf_read_count above occurs before
618 we write data to the write-alloc'ed buffer space */
619 smp_mb();
620 return nbytes;
621}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700622EXPORT_SYMBOL(comedi_buf_write_alloc);
David Schleefed9eccb2008-11-04 20:29:31 -0800623
624/* allocates nothing unless it can completely fulfill the request */
Bill Pembertond1636792009-03-16 22:05:20 -0400625unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530626 unsigned int nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800627{
628 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
629
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100630 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
David Schleefed9eccb2008-11-04 20:29:31 -0800631 nbytes = 0;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100632
David Schleefed9eccb2008-11-04 20:29:31 -0800633 async->buf_write_alloc_count += nbytes;
634 /* barrier insures the read of buf_read_count above occurs before
635 we write data to the write-alloc'ed buffer space */
636 smp_mb();
637 return nbytes;
638}
639
640/* transfers a chunk from writer to filled buffer space */
Bill Pembertond1636792009-03-16 22:05:20 -0400641unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800642{
643 if ((int)(async->buf_write_count + nbytes -
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530644 async->buf_write_alloc_count) > 0) {
Xenofon Foukas88ab8a82010-12-04 00:17:55 +0200645 printk(KERN_INFO "comedi: attempted to write-free more bytes than have been write-allocated.\n");
David Schleefed9eccb2008-11-04 20:29:31 -0800646 nbytes = async->buf_write_alloc_count - async->buf_write_count;
647 }
648 async->buf_write_count += nbytes;
649 async->buf_write_ptr += nbytes;
650 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100651 if (async->buf_write_ptr >= async->prealloc_bufsz)
David Schleefed9eccb2008-11-04 20:29:31 -0800652 async->buf_write_ptr %= async->prealloc_bufsz;
Andrea Gelmini5617f9d2010-02-26 17:37:00 +0100653
David Schleefed9eccb2008-11-04 20:29:31 -0800654 return nbytes;
655}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700656EXPORT_SYMBOL(comedi_buf_write_free);
David Schleefed9eccb2008-11-04 20:29:31 -0800657
658/* allocates a chunk for the reader from filled (and munged) buffer space */
Bill Pembertond1636792009-03-16 22:05:20 -0400659unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800660{
661 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530662 0) {
David Schleefed9eccb2008-11-04 20:29:31 -0800663 nbytes = async->munge_count - async->buf_read_alloc_count;
664 }
665 async->buf_read_alloc_count += nbytes;
666 /* barrier insures read of munge_count occurs before we actually read
667 data out of buffer */
668 smp_rmb();
669 return nbytes;
670}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700671EXPORT_SYMBOL(comedi_buf_read_alloc);
David Schleefed9eccb2008-11-04 20:29:31 -0800672
673/* transfers control of a chunk from reader to free buffer space */
Bill Pembertond1636792009-03-16 22:05:20 -0400674unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800675{
Zachary Richeyac4898a2010-04-26 07:49:37 -0400676 /* barrier insures data has been read out of
677 * buffer before read count is incremented */
David Schleefed9eccb2008-11-04 20:29:31 -0800678 smp_mb();
679 if ((int)(async->buf_read_count + nbytes -
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530680 async->buf_read_alloc_count) > 0) {
Mark Rankilor67b0e642010-05-06 17:36:38 +0800681 printk(KERN_INFO
682 "comedi: attempted to read-free more bytes than have been read-allocated.\n");
David Schleefed9eccb2008-11-04 20:29:31 -0800683 nbytes = async->buf_read_alloc_count - async->buf_read_count;
684 }
685 async->buf_read_count += nbytes;
686 async->buf_read_ptr += nbytes;
687 async->buf_read_ptr %= async->prealloc_bufsz;
688 return nbytes;
689}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700690EXPORT_SYMBOL(comedi_buf_read_free);
David Schleefed9eccb2008-11-04 20:29:31 -0800691
Bill Pembertond1636792009-03-16 22:05:20 -0400692void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530693 const void *data, unsigned int num_bytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800694{
695 unsigned int write_ptr = async->buf_write_ptr + offset;
696
697 if (write_ptr >= async->prealloc_bufsz)
698 write_ptr %= async->prealloc_bufsz;
699
700 while (num_bytes) {
701 unsigned int block_size;
702
703 if (write_ptr + num_bytes > async->prealloc_bufsz)
704 block_size = async->prealloc_bufsz - write_ptr;
705 else
706 block_size = num_bytes;
707
708 memcpy(async->prealloc_buf + write_ptr, data, block_size);
709
710 data += block_size;
711 num_bytes -= block_size;
712
713 write_ptr = 0;
714 }
715}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700716EXPORT_SYMBOL(comedi_buf_memcpy_to);
David Schleefed9eccb2008-11-04 20:29:31 -0800717
Bill Pembertond1636792009-03-16 22:05:20 -0400718void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530719 void *dest, unsigned int nbytes)
David Schleefed9eccb2008-11-04 20:29:31 -0800720{
721 void *src;
722 unsigned int read_ptr = async->buf_read_ptr + offset;
723
724 if (read_ptr >= async->prealloc_bufsz)
725 read_ptr %= async->prealloc_bufsz;
726
727 while (nbytes) {
728 unsigned int block_size;
729
730 src = async->prealloc_buf + read_ptr;
731
732 if (nbytes >= async->prealloc_bufsz - read_ptr)
733 block_size = async->prealloc_bufsz - read_ptr;
734 else
735 block_size = nbytes;
736
737 memcpy(dest, src, block_size);
738 nbytes -= block_size;
739 dest += block_size;
740 read_ptr = 0;
741 }
742}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700743EXPORT_SYMBOL(comedi_buf_memcpy_from);
David Schleefed9eccb2008-11-04 20:29:31 -0800744
Bill Pembertond1636792009-03-16 22:05:20 -0400745unsigned int comedi_buf_read_n_available(struct comedi_async *async)
David Schleefed9eccb2008-11-04 20:29:31 -0800746{
747 unsigned num_bytes;
748
749 if (async == NULL)
750 return 0;
751 num_bytes = async->munge_count - async->buf_read_count;
752 /* barrier insures the read of munge_count in this
753 query occurs before any following reads of the buffer which
754 might be based on the return value from this query.
755 */
756 smp_rmb();
757 return num_bytes;
758}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700759EXPORT_SYMBOL(comedi_buf_read_n_available);
David Schleefed9eccb2008-11-04 20:29:31 -0800760
Bill Pembertond1636792009-03-16 22:05:20 -0400761int comedi_buf_get(struct comedi_async *async, short *x)
David Schleefed9eccb2008-11-04 20:29:31 -0800762{
763 unsigned int n = comedi_buf_read_n_available(async);
764
Bill Pemberton790c5542009-03-16 22:05:02 -0400765 if (n < sizeof(short))
David Schleefed9eccb2008-11-04 20:29:31 -0800766 return 0;
Bill Pemberton790c5542009-03-16 22:05:02 -0400767 comedi_buf_read_alloc(async, sizeof(short));
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530768 *x = *(short *)(async->prealloc_buf + async->buf_read_ptr);
Bill Pemberton790c5542009-03-16 22:05:02 -0400769 comedi_buf_read_free(async, sizeof(short));
David Schleefed9eccb2008-11-04 20:29:31 -0800770 return 1;
771}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700772EXPORT_SYMBOL(comedi_buf_get);
David Schleefed9eccb2008-11-04 20:29:31 -0800773
Bill Pembertond1636792009-03-16 22:05:20 -0400774int comedi_buf_put(struct comedi_async *async, short x)
David Schleefed9eccb2008-11-04 20:29:31 -0800775{
Bill Pemberton790c5542009-03-16 22:05:02 -0400776 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short));
David Schleefed9eccb2008-11-04 20:29:31 -0800777
Bill Pemberton790c5542009-03-16 22:05:02 -0400778 if (n < sizeof(short)) {
David Schleefed9eccb2008-11-04 20:29:31 -0800779 async->events |= COMEDI_CB_ERROR;
780 return 0;
781 }
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530782 *(short *)(async->prealloc_buf + async->buf_write_ptr) = x;
Bill Pemberton790c5542009-03-16 22:05:02 -0400783 comedi_buf_write_free(async, sizeof(short));
David Schleefed9eccb2008-11-04 20:29:31 -0800784 return 1;
785}
Greg Kroah-Hartmand58214b2010-05-01 11:54:07 -0700786EXPORT_SYMBOL(comedi_buf_put);
David Schleefed9eccb2008-11-04 20:29:31 -0800787
Bill Pembertond1636792009-03-16 22:05:20 -0400788void comedi_reset_async_buf(struct comedi_async *async)
David Schleefed9eccb2008-11-04 20:29:31 -0800789{
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
Ian Abbottf40116702012-03-30 17:15:01 +0100807static int
808comedi_auto_config_helper(struct device *hardware_device,
809 struct comedi_driver *driver,
810 int (*attach_wrapper) (struct comedi_device *,
811 void *), void *context)
812{
813 int minor;
814 struct comedi_device_file_info *dev_file_info;
815 struct comedi_device *comedi_dev;
816 int ret;
817
818 if (!comedi_autoconfig)
819 return 0;
820
821 minor = comedi_alloc_board_minor(hardware_device);
822 if (minor < 0)
823 return minor;
824
825 dev_file_info = comedi_get_device_file_info(minor);
826 comedi_dev = dev_file_info->device;
827
828 mutex_lock(&comedi_dev->mutex);
829 if (comedi_dev->attached)
830 ret = -EBUSY;
831 else if (!try_module_get(driver->module)) {
832 printk(KERN_INFO "comedi: failed to increment module count\n");
833 ret = -EIO;
834 } else {
835 /* set comedi_dev->driver here for attach wrapper */
836 comedi_dev->driver = driver;
837 ret = (*attach_wrapper)(comedi_dev, context);
838 if (ret < 0) {
839 module_put(driver->module);
840 __comedi_device_detach(comedi_dev);
841 } else {
842 ret = comedi_device_postconfig(comedi_dev);
843 }
844 }
845 mutex_unlock(&comedi_dev->mutex);
846
847 if (ret < 0)
848 comedi_free_board_minor(minor);
849 return ret;
850}
851
Ian Abbottcf938c22012-03-30 17:15:03 +0100852static int comedi_auto_config_wrapper(struct comedi_device *dev, void *context)
853{
854 struct comedi_devconfig *it = context;
855 struct comedi_driver *driv = dev->driver;
856
857 if (driv->num_names) {
858 /* look for generic board entry matching driver name, which
859 * has already been copied to it->board_name */
860 dev->board_ptr = comedi_recognize(driv, it->board_name);
861 if (dev->board_ptr == NULL) {
862 printk(KERN_WARNING
863 "comedi: auto config failed to find board entry"
864 " '%s' for driver '%s'\n", it->board_name,
865 driv->driver_name);
866 comedi_report_boards(driv);
867 return -EINVAL;
868 }
869 }
870 return driv->attach(dev, it);
871}
872
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700873static int comedi_auto_config(struct device *hardware_device,
Ian Abbott63bf3d12012-03-30 17:15:02 +0100874 struct comedi_driver *driver, const int *options,
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700875 unsigned num_options)
David Schleefed9eccb2008-11-04 20:29:31 -0800876{
Bill Pemberton0707bb02009-03-16 22:06:20 -0400877 struct comedi_devconfig it;
David Schleefed9eccb2008-11-04 20:29:31 -0800878
879 memset(&it, 0, sizeof(it));
Ian Abbott63bf3d12012-03-30 17:15:02 +0100880 strncpy(it.board_name, driver->driver_name, COMEDI_NAMELEN);
David Schleefed9eccb2008-11-04 20:29:31 -0800881 it.board_name[COMEDI_NAMELEN - 1] = '\0';
882 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
883 memcpy(it.options, options, num_options * sizeof(int));
Ian Abbottcf938c22012-03-30 17:15:03 +0100884 return comedi_auto_config_helper(hardware_device, driver,
885 comedi_auto_config_wrapper, &it);
David Schleefed9eccb2008-11-04 20:29:31 -0800886}
887
Greg Kroah-Hartman7029a872010-05-03 15:55:45 -0700888static void comedi_auto_unconfig(struct device *hardware_device)
David Schleefed9eccb2008-11-04 20:29:31 -0800889{
Ian Abbottc43435d2012-03-30 17:14:58 +0100890 int minor;
891
892 if (hardware_device == NULL)
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530893 return;
Ian Abbottc43435d2012-03-30 17:14:58 +0100894 minor = comedi_find_board_minor(hardware_device);
895 if (minor < 0)
896 return;
897 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
898 comedi_free_board_minor(minor);
David Schleefed9eccb2008-11-04 20:29:31 -0800899}
900
H Hartley Sweeten55c03cf2012-05-21 17:12:12 -0700901/**
902 * comedi_pci_enable() - Enable the PCI device and request the regions.
903 * @pdev: pci_dev struct
904 * @res_name: name for the requested reqource
905 */
906int comedi_pci_enable(struct pci_dev *pdev, const char *res_name)
907{
908 int rc;
909
910 rc = pci_enable_device(pdev);
911 if (rc < 0)
912 return rc;
913
914 rc = pci_request_regions(pdev, res_name);
915 if (rc < 0)
916 pci_disable_device(pdev);
917
918 return rc;
919}
920EXPORT_SYMBOL_GPL(comedi_pci_enable);
921
922/**
923 * comedi_pci_disable() - Release the regions and disable the PCI device.
924 * @pdev: pci_dev struct
925 *
926 * This must be matched with a previous successful call to comedi_pci_enable().
927 */
928void comedi_pci_disable(struct pci_dev *pdev)
929{
930 pci_release_regions(pdev);
931 pci_disable_device(pdev);
932}
933EXPORT_SYMBOL_GPL(comedi_pci_disable);
934
Ian Abbottf40116702012-03-30 17:15:01 +0100935static int comedi_old_pci_auto_config(struct pci_dev *pcidev,
936 struct comedi_driver *driver)
David Schleefed9eccb2008-11-04 20:29:31 -0800937{
938 int options[2];
939
Bill Pembertonb6c77752009-03-16 22:03:24 -0400940 /* pci bus */
David Schleefed9eccb2008-11-04 20:29:31 -0800941 options[0] = pcidev->bus->number;
Bill Pembertonb6c77752009-03-16 22:03:24 -0400942 /* pci slot */
David Schleefed9eccb2008-11-04 20:29:31 -0800943 options[1] = PCI_SLOT(pcidev->devfn);
944
Ian Abbott63bf3d12012-03-30 17:15:02 +0100945 return comedi_auto_config(&pcidev->dev, driver,
Bill Pemberton8629efa2009-04-23 15:54:56 -0400946 options, ARRAY_SIZE(options));
David Schleefed9eccb2008-11-04 20:29:31 -0800947}
Ian Abbottf40116702012-03-30 17:15:01 +0100948
949static int comedi_pci_attach_wrapper(struct comedi_device *dev, void *pcidev)
950{
951 return dev->driver->attach_pci(dev, pcidev);
952}
953
954static int comedi_new_pci_auto_config(struct pci_dev *pcidev,
955 struct comedi_driver *driver)
956{
957 return comedi_auto_config_helper(&pcidev->dev, driver,
958 comedi_pci_attach_wrapper, pcidev);
959}
960
961int comedi_pci_auto_config(struct pci_dev *pcidev, struct comedi_driver *driver)
962{
963
964 if (driver->attach_pci)
965 return comedi_new_pci_auto_config(pcidev, driver);
966 else
967 return comedi_old_pci_auto_config(pcidev, driver);
968}
Greg Kroah-Hartman4bf93552010-05-01 12:38:02 -0700969EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
David Schleefed9eccb2008-11-04 20:29:31 -0800970
971void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
972{
973 comedi_auto_unconfig(&pcidev->dev);
974}
Greg Kroah-Hartman4bf93552010-05-01 12:38:02 -0700975EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
Bernd Porrc28264d2008-12-08 23:30:13 +0000976
H Hartley Sweetend4899c62012-05-11 16:15:39 -0700977int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
978 struct pci_driver *pci_driver)
979{
980 int ret;
981
982 ret = comedi_driver_register(comedi_driver);
983 if (ret < 0)
984 return ret;
985
986 /* FIXME: Remove this test after auditing all comedi pci drivers */
987 if (!pci_driver->name)
988 pci_driver->name = comedi_driver->driver_name;
989
990 ret = pci_register_driver(pci_driver);
991 if (ret < 0) {
992 comedi_driver_unregister(comedi_driver);
993 return ret;
994 }
995
996 return 0;
997}
998EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
999
1000void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
1001 struct pci_driver *pci_driver)
1002{
1003 pci_unregister_driver(pci_driver);
1004 comedi_driver_unregister(comedi_driver);
1005}
1006EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);
1007
Ian Abbottf40116702012-03-30 17:15:01 +01001008static int comedi_old_usb_auto_config(struct usb_interface *intf,
1009 struct comedi_driver *driver)
1010{
Ian Abbott63bf3d12012-03-30 17:15:02 +01001011 return comedi_auto_config(&intf->dev, driver, NULL, 0);
Ian Abbottf40116702012-03-30 17:15:01 +01001012}
1013
1014static int comedi_usb_attach_wrapper(struct comedi_device *dev, void *intf)
1015{
1016 return dev->driver->attach_usb(dev, intf);
1017}
1018
1019static int comedi_new_usb_auto_config(struct usb_interface *intf,
1020 struct comedi_driver *driver)
1021{
1022 return comedi_auto_config_helper(&intf->dev, driver,
1023 comedi_usb_attach_wrapper, intf);
1024}
1025
Ian Abbottd8b6ca02012-03-30 17:14:59 +01001026int comedi_usb_auto_config(struct usb_interface *intf,
Ian Abbott4c093a62012-03-30 17:14:56 +01001027 struct comedi_driver *driver)
Bernd Porrc28264d2008-12-08 23:30:13 +00001028{
Ian Abbottd8b6ca02012-03-30 17:14:59 +01001029 BUG_ON(intf == NULL);
Ian Abbottf40116702012-03-30 17:15:01 +01001030 if (driver->attach_usb)
1031 return comedi_new_usb_auto_config(intf, driver);
1032 else
1033 return comedi_old_usb_auto_config(intf, driver);
Bernd Porrc28264d2008-12-08 23:30:13 +00001034}
Greg Kroah-Hartman4bf93552010-05-01 12:38:02 -07001035EXPORT_SYMBOL_GPL(comedi_usb_auto_config);
Bernd Porrc28264d2008-12-08 23:30:13 +00001036
Ian Abbottd8b6ca02012-03-30 17:14:59 +01001037void comedi_usb_auto_unconfig(struct usb_interface *intf)
Bernd Porrc28264d2008-12-08 23:30:13 +00001038{
Ian Abbottd8b6ca02012-03-30 17:14:59 +01001039 BUG_ON(intf == NULL);
1040 comedi_auto_unconfig(&intf->dev);
Bernd Porrc28264d2008-12-08 23:30:13 +00001041}
Greg Kroah-Hartman4bf93552010-05-01 12:38:02 -07001042EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);
H Hartley Sweeten64255032012-05-16 18:18:52 -07001043
1044int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
1045 struct usb_driver *usb_driver)
1046{
1047 int ret;
1048
1049 ret = comedi_driver_register(comedi_driver);
1050 if (ret < 0)
1051 return ret;
1052
1053 ret = usb_register(usb_driver);
1054 if (ret < 0) {
1055 comedi_driver_unregister(comedi_driver);
1056 return ret;
1057 }
1058
1059 return 0;
1060}
1061EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
1062
1063void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
1064 struct usb_driver *usb_driver)
1065{
1066 usb_deregister(usb_driver);
1067 comedi_driver_unregister(comedi_driver);
1068}
1069EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);