]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/comedi/drivers.c
staging: comedi: change type of num_subdevices parameter to comedi_alloc_subdevices
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / comedi / drivers.c
CommitLineData
ed9eccbe
DS
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>
c28264da 31#include <linux/usb.h>
ed9eccbe
DS
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>
ed9eccbe
DS
40#include <linux/highmem.h> /* for SuSE brokenness */
41#include <linux/vmalloc.h>
42#include <linux/cdev.h>
43#include <linux/dma-mapping.h>
5617f9da 44#include <linux/io.h>
ed9eccbe 45
242e7ad9
GKH
46#include "comedidev.h"
47#include "internal.h"
48
71b5f4f1 49static int postconfig(struct comedi_device *dev);
0a85b6f0
MT
50static 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);
139dfbdf 54static void comedi_report_boards(struct comedi_driver *driv);
34c43922 55static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s);
ed9eccbe 56
139dfbdf 57struct comedi_driver *comedi_drivers;
ed9eccbe 58
8b9ba6e5 59int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
2f0b9d08 60{
8b9ba6e5 61 int i;
2f0b9d08
HS
62
63 dev->n_subdevices = num_subdevices;
64 dev->subdevices =
65 kcalloc(num_subdevices, sizeof(struct comedi_subdevice),
66 GFP_KERNEL);
67 if (!dev->subdevices)
68 return -ENOMEM;
69 for (i = 0; i < num_subdevices; ++i) {
70 dev->subdevices[i].device = dev;
71 dev->subdevices[i].async_dma_dir = DMA_NONE;
72 spin_lock_init(&dev->subdevices[i].spin_lock);
73 dev->subdevices[i].minor = -1;
74 }
75 return 0;
76}
77EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
78
71b5f4f1 79static void cleanup_device(struct comedi_device *dev)
ed9eccbe
DS
80{
81 int i;
34c43922 82 struct comedi_subdevice *s;
ed9eccbe
DS
83
84 if (dev->subdevices) {
85 for (i = 0; i < dev->n_subdevices; i++) {
86 s = dev->subdevices + i;
87 comedi_free_subdevice_minor(s);
88 if (s->async) {
89 comedi_buf_alloc(dev, s, 0);
90 kfree(s->async);
91 }
92 }
93 kfree(dev->subdevices);
94 dev->subdevices = NULL;
95 dev->n_subdevices = 0;
96 }
dedd1325
BP
97 kfree(dev->private);
98 dev->private = NULL;
7029a874 99 dev->driver = NULL;
ed9eccbe
DS
100 dev->board_name = NULL;
101 dev->board_ptr = NULL;
102 dev->iobase = 0;
103 dev->irq = 0;
104 dev->read_subdev = NULL;
105 dev->write_subdev = NULL;
106 dev->open = NULL;
107 dev->close = NULL;
108 comedi_set_hw_dev(dev, NULL);
109}
110
71b5f4f1 111static void __comedi_device_detach(struct comedi_device *dev)
ed9eccbe
DS
112{
113 dev->attached = 0;
5617f9da 114 if (dev->driver)
ed9eccbe 115 dev->driver->detach(dev);
5617f9da 116 else
67b0e64a
MR
117 printk(KERN_WARNING
118 "BUG: dev->driver=NULL in comedi_device_detach()\n");
ed9eccbe
DS
119 cleanup_device(dev);
120}
121
71b5f4f1 122void comedi_device_detach(struct comedi_device *dev)
ed9eccbe
DS
123{
124 if (!dev->attached)
125 return;
126 __comedi_device_detach(dev);
127}
128
3902a370
IA
129/* do a little post-config cleanup */
130/* called with module refcount incremented, decrements it */
131static int comedi_device_postconfig(struct comedi_device *dev)
132{
133 int ret = postconfig(dev);
134 module_put(dev->driver->module);
135 if (ret < 0) {
136 __comedi_device_detach(dev);
137 return ret;
138 }
139 if (!dev->board_name) {
140 printk(KERN_WARNING "BUG: dev->board_name=<%p>\n",
141 dev->board_name);
142 dev->board_name = "BUG";
143 }
144 smp_wmb();
145 dev->attached = 1;
146 return 0;
147}
148
0707bb04 149int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
ed9eccbe 150{
139dfbdf 151 struct comedi_driver *driv;
ed9eccbe
DS
152 int ret;
153
154 if (dev->attached)
155 return -EBUSY;
156
157 for (driv = comedi_drivers; driv; driv = driv->next) {
158 if (!try_module_get(driv->module)) {
88ab8a84 159 printk(KERN_INFO "comedi: failed to increment module count, skipping\n");
ed9eccbe
DS
160 continue;
161 }
162 if (driv->num_names) {
163 dev->board_ptr = comedi_recognize(driv, it->board_name);
3902a370
IA
164 if (dev->board_ptr)
165 break;
166 } else if (strcmp(driv->driver_name, it->board_name))
167 break;
168 module_put(driv->module);
169 }
170 if (driv == NULL) {
171 /* recognize has failed if we get here */
172 /* report valid board names before returning error */
173 for (driv = comedi_drivers; driv; driv = driv->next) {
174 if (!try_module_get(driv->module)) {
175 printk(KERN_INFO
176 "comedi: failed to increment module count\n");
ed9eccbe
DS
177 continue;
178 }
3902a370
IA
179 comedi_report_boards(driv);
180 module_put(driv->module);
ed9eccbe 181 }
3902a370 182 return -EIO;
ed9eccbe 183 }
3902a370
IA
184 /* initialize dev->driver here so
185 * comedi_error() can be called from attach */
186 dev->driver = driv;
187 ret = driv->attach(dev, it);
ed9eccbe 188 if (ret < 0) {
3902a370 189 module_put(dev->driver->module);
ed9eccbe
DS
190 __comedi_device_detach(dev);
191 return ret;
192 }
3902a370 193 return comedi_device_postconfig(dev);
ed9eccbe
DS
194}
195
139dfbdf 196int comedi_driver_register(struct comedi_driver *driver)
ed9eccbe
DS
197{
198 driver->next = comedi_drivers;
199 comedi_drivers = driver;
200
201 return 0;
202}
d58214b0 203EXPORT_SYMBOL(comedi_driver_register);
ed9eccbe 204
139dfbdf 205int comedi_driver_unregister(struct comedi_driver *driver)
ed9eccbe 206{
139dfbdf 207 struct comedi_driver *prev;
ed9eccbe
DS
208 int i;
209
210 /* check for devices using this driver */
211 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
0a85b6f0
MT
212 struct comedi_device_file_info *dev_file_info =
213 comedi_get_device_file_info(i);
71b5f4f1 214 struct comedi_device *dev;
ed9eccbe 215
0a85b6f0
MT
216 if (dev_file_info == NULL)
217 continue;
ed9eccbe
DS
218 dev = dev_file_info->device;
219
220 mutex_lock(&dev->mutex);
221 if (dev->attached && dev->driver == driver) {
222 if (dev->use_count)
88ab8a84
XF
223 printk(KERN_WARNING "BUG! detaching device with use_count=%d\n",
224 dev->use_count);
ed9eccbe
DS
225 comedi_device_detach(dev);
226 }
227 mutex_unlock(&dev->mutex);
228 }
229
230 if (comedi_drivers == driver) {
231 comedi_drivers = driver->next;
232 return 0;
233 }
234
235 for (prev = comedi_drivers; prev->next; prev = prev->next) {
236 if (prev->next == driver) {
237 prev->next = driver->next;
238 return 0;
239 }
240 }
241 return -EINVAL;
242}
d58214b0 243EXPORT_SYMBOL(comedi_driver_unregister);
ed9eccbe 244
71b5f4f1 245static int postconfig(struct comedi_device *dev)
ed9eccbe
DS
246{
247 int i;
34c43922 248 struct comedi_subdevice *s;
d163679c 249 struct comedi_async *async = NULL;
ed9eccbe
DS
250 int ret;
251
252 for (i = 0; i < dev->n_subdevices; i++) {
253 s = dev->subdevices + i;
254
255 if (s->type == COMEDI_SUBD_UNUSED)
256 continue;
257
258 if (s->len_chanlist == 0)
259 s->len_chanlist = 1;
260
261 if (s->do_cmd) {
4d7df821
IA
262 unsigned int buf_size;
263
ed9eccbe 264 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
0a85b6f0 265 SDF_CMD_WRITE)) == 0);
ed9eccbe
DS
266 BUG_ON(!s->do_cmdtest);
267
0a85b6f0
MT
268 async =
269 kzalloc(sizeof(struct comedi_async), GFP_KERNEL);
ed9eccbe 270 if (async == NULL) {
67b0e64a
MR
271 printk(KERN_INFO
272 "failed to allocate async struct\n");
ed9eccbe
DS
273 return -ENOMEM;
274 }
275 init_waitqueue_head(&async->wait_head);
276 async->subdevice = s;
277 s->async = async;
278
4d7df821
IA
279 async->max_bufsize =
280 comedi_default_buf_maxsize_kb * 1024;
281 buf_size = comedi_default_buf_size_kb * 1024;
282 if (buf_size > async->max_bufsize)
283 buf_size = async->max_bufsize;
ed9eccbe
DS
284
285 async->prealloc_buf = NULL;
286 async->prealloc_bufsz = 0;
4d7df821 287 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
ac4898a0 288 printk(KERN_INFO "Buffer allocation failed\n");
ed9eccbe
DS
289 return -ENOMEM;
290 }
291 if (s->buf_change) {
4d7df821 292 ret = s->buf_change(dev, s, buf_size);
ed9eccbe
DS
293 if (ret < 0)
294 return ret;
295 }
296 comedi_alloc_subdevice_minor(dev, s);
297 }
298
299 if (!s->range_table && !s->range_table_list)
300 s->range_table = &range_unknown;
301
302 if (!s->insn_read && s->insn_bits)
303 s->insn_read = insn_rw_emulate_bits;
304 if (!s->insn_write && s->insn_bits)
305 s->insn_write = insn_rw_emulate_bits;
306
307 if (!s->insn_read)
308 s->insn_read = insn_inval;
309 if (!s->insn_write)
310 s->insn_write = insn_inval;
311 if (!s->insn_bits)
312 s->insn_bits = insn_inval;
313 if (!s->insn_config)
314 s->insn_config = insn_inval;
315
316 if (!s->poll)
317 s->poll = poll_invalid;
318 }
319
320 return 0;
321}
322
4e2f002f
IA
323/*
324 * Generic recognize function for drivers that register their supported
325 * board names.
326 *
327 * 'driv->board_name' points to a 'const char *' member within the
328 * zeroth element of an array of some private board information
329 * structure, say 'struct foo_board' containing a member 'const char
330 * *board_name' that is initialized to point to a board name string that
331 * is one of the candidates matched against this function's 'name'
332 * parameter.
333 *
334 * 'driv->offset' is the size of the private board information
335 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
336 * the length of the array of private board information structures.
337 *
338 * If one of the board names in the array of private board information
339 * structures matches the name supplied to this function, the function
340 * returns a pointer to the pointer to the board name, otherwise it
341 * returns NULL. The return value ends up in the 'board_ptr' member of
342 * a 'struct comedi_device' that the low-level comedi driver's
343 * 'attach()' hook can convert to a point to a particular element of its
344 * array of private board information structures by subtracting the
345 * offset of the member that points to the board name. (No subtraction
346 * is required if the board name pointer is the first member of the
347 * private board information structure, which is generally the case.)
348 */
7029a874 349static void *comedi_recognize(struct comedi_driver *driv, const char *name)
ed9eccbe 350{
1c9de58a
DC
351 char **name_ptr = (char **)driv->board_name;
352 int i;
353
ed9eccbe
DS
354 for (i = 0; i < driv->num_names; i++) {
355 if (strcmp(*name_ptr, name) == 0)
1c9de58a
DC
356 return name_ptr;
357 name_ptr = (void *)name_ptr + driv->offset;
ed9eccbe
DS
358 }
359
360 return NULL;
361}
362
7029a874 363static void comedi_report_boards(struct comedi_driver *driv)
ed9eccbe
DS
364{
365 unsigned int i;
366 const char *const *name_ptr;
367
ac4898a0 368 printk(KERN_INFO "comedi: valid board names for %s driver are:\n",
0a85b6f0 369 driv->driver_name);
ed9eccbe
DS
370
371 name_ptr = driv->board_name;
372 for (i = 0; i < driv->num_names; i++) {
ac4898a0 373 printk(KERN_INFO " %s\n", *name_ptr);
ed9eccbe
DS
374 name_ptr = (const char **)((char *)name_ptr + driv->offset);
375 }
376
377 if (driv->num_names == 0)
ac4898a0 378 printk(KERN_INFO " %s\n", driv->driver_name);
ed9eccbe
DS
379}
380
34c43922 381static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
ed9eccbe
DS
382{
383 return -EINVAL;
384}
385
34c43922 386int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
0a85b6f0 387 struct comedi_insn *insn, unsigned int *data)
ed9eccbe
DS
388{
389 return -EINVAL;
390}
391
0a85b6f0
MT
392static int insn_rw_emulate_bits(struct comedi_device *dev,
393 struct comedi_subdevice *s,
394 struct comedi_insn *insn, unsigned int *data)
ed9eccbe 395{
90035c08 396 struct comedi_insn new_insn;
ed9eccbe
DS
397 int ret;
398 static const unsigned channels_per_bitfield = 32;
399
400 unsigned chan = CR_CHAN(insn->chanspec);
401 const unsigned base_bitfield_channel =
0a85b6f0 402 (chan < channels_per_bitfield) ? 0 : chan;
790c5541 403 unsigned int new_data[2];
ed9eccbe
DS
404 memset(new_data, 0, sizeof(new_data));
405 memset(&new_insn, 0, sizeof(new_insn));
406 new_insn.insn = INSN_BITS;
407 new_insn.chanspec = base_bitfield_channel;
408 new_insn.n = 2;
409 new_insn.data = new_data;
410 new_insn.subdev = insn->subdev;
411
412 if (insn->insn == INSN_WRITE) {
413 if (!(s->subdev_flags & SDF_WRITABLE))
414 return -EINVAL;
aad4029a
MR
415 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
416 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
417 : 0; /* bits */
ed9eccbe
DS
418 }
419
420 ret = s->insn_bits(dev, s, &new_insn, new_data);
421 if (ret < 0)
422 return ret;
423
5617f9da 424 if (insn->insn == INSN_READ)
ed9eccbe 425 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
ed9eccbe
DS
426
427 return 1;
428}
429
34c43922 430int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
0a85b6f0 431 unsigned long new_size)
ed9eccbe 432{
d163679c 433 struct comedi_async *async = s->async;
ed9eccbe
DS
434
435 /* Round up new_size to multiple of PAGE_SIZE */
436 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
437
438 /* if no change is required, do nothing */
5617f9da 439 if (async->prealloc_buf && async->prealloc_bufsz == new_size)
ed9eccbe 440 return 0;
5617f9da 441
b6c77757 442 /* deallocate old buffer */
ed9eccbe
DS
443 if (async->prealloc_buf) {
444 vunmap(async->prealloc_buf);
445 async->prealloc_buf = NULL;
446 async->prealloc_bufsz = 0;
447 }
448 if (async->buf_page_list) {
449 unsigned i;
450 for (i = 0; i < async->n_buf_pages; ++i) {
451 if (async->buf_page_list[i].virt_addr) {
88ab8a84
XF
452 clear_bit(PG_reserved,
453 &(virt_to_page(async->buf_page_list[i].
454 virt_addr)->flags));
ed9eccbe
DS
455 if (s->async_dma_dir != DMA_NONE) {
456 dma_free_coherent(dev->hw_dev,
0a85b6f0
MT
457 PAGE_SIZE,
458 async->
459 buf_page_list
460 [i].virt_addr,
461 async->
462 buf_page_list
463 [i].dma_addr);
ed9eccbe 464 } else {
0a85b6f0
MT
465 free_page((unsigned long)
466 async->buf_page_list[i].
467 virt_addr);
ed9eccbe
DS
468 }
469 }
470 }
471 vfree(async->buf_page_list);
472 async->buf_page_list = NULL;
473 async->n_buf_pages = 0;
474 }
b6c77757 475 /* allocate new buffer */
ed9eccbe
DS
476 if (new_size) {
477 unsigned i = 0;
478 unsigned n_pages = new_size >> PAGE_SHIFT;
479 struct page **pages = NULL;
480
481 async->buf_page_list =
5b84cc78 482 vzalloc(sizeof(struct comedi_buf_page) * n_pages);
3ad4e219 483 if (async->buf_page_list)
ed9eccbe 484 pages = vmalloc(sizeof(struct page *) * n_pages);
3ad4e219 485
ed9eccbe
DS
486 if (pages) {
487 for (i = 0; i < n_pages; i++) {
488 if (s->async_dma_dir != DMA_NONE) {
489 async->buf_page_list[i].virt_addr =
0a85b6f0
MT
490 dma_alloc_coherent(dev->hw_dev,
491 PAGE_SIZE,
492 &async->
493 buf_page_list
494 [i].dma_addr,
495 GFP_KERNEL |
496 __GFP_COMP);
ed9eccbe
DS
497 } else {
498 async->buf_page_list[i].virt_addr =
0a85b6f0
MT
499 (void *)
500 get_zeroed_page(GFP_KERNEL);
ed9eccbe 501 }
5617f9da 502 if (async->buf_page_list[i].virt_addr == NULL)
ed9eccbe 503 break;
5617f9da 504
be29eac8 505 set_bit(PG_reserved,
88ab8a84
XF
506 &(virt_to_page(async->buf_page_list[i].
507 virt_addr)->flags));
508 pages[i] = virt_to_page(async->buf_page_list[i].
509 virt_addr);
ed9eccbe
DS
510 }
511 }
512 if (i == n_pages) {
513 async->prealloc_buf =
408093d2 514#ifdef PAGE_KERNEL_NOCACHE
0a85b6f0 515 vmap(pages, n_pages, VM_MAP, PAGE_KERNEL_NOCACHE);
408093d2
GKH
516#else
517 vmap(pages, n_pages, VM_MAP, PAGE_KERNEL);
518#endif
ed9eccbe 519 }
b455073c
F
520 vfree(pages);
521
ed9eccbe
DS
522 if (async->prealloc_buf == NULL) {
523 /* Some allocation failed above. */
524 if (async->buf_page_list) {
525 for (i = 0; i < n_pages; i++) {
526 if (async->buf_page_list[i].virt_addr ==
0a85b6f0 527 NULL) {
ed9eccbe
DS
528 break;
529 }
88ab8a84
XF
530 clear_bit(PG_reserved,
531 &(virt_to_page(async->
532 buf_page_list[i].
533 virt_addr)->flags));
ed9eccbe
DS
534 if (s->async_dma_dir != DMA_NONE) {
535 dma_free_coherent(dev->hw_dev,
0a85b6f0
MT
536 PAGE_SIZE,
537 async->
538 buf_page_list
539 [i].virt_addr,
540 async->
541 buf_page_list
542 [i].dma_addr);
ed9eccbe 543 } else {
0a85b6f0
MT
544 free_page((unsigned long)
545 async->buf_page_list
546 [i].virt_addr);
ed9eccbe
DS
547 }
548 }
549 vfree(async->buf_page_list);
550 async->buf_page_list = NULL;
551 }
552 return -ENOMEM;
553 }
554 async->n_buf_pages = n_pages;
555 }
556 async->prealloc_bufsz = new_size;
557
558 return 0;
559}
560
561/* munging is applied to data by core as it passes between user
562 * and kernel space */
7029a874
GKH
563static unsigned int comedi_buf_munge(struct comedi_async *async,
564 unsigned int num_bytes)
ed9eccbe 565{
34c43922 566 struct comedi_subdevice *s = async->subdevice;
ed9eccbe
DS
567 unsigned int count = 0;
568 const unsigned num_sample_bytes = bytes_per_sample(s);
569
570 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
571 async->munge_count += num_bytes;
2961f24f 572 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
ed9eccbe
DS
573 return num_bytes;
574 }
575 /* don't munge partial samples */
576 num_bytes -= num_bytes % num_sample_bytes;
577 while (count < num_bytes) {
578 int block_size;
579
580 block_size = num_bytes - count;
581 if (block_size < 0) {
67b0e64a
MR
582 printk(KERN_WARNING
583 "%s: %s: bug! block_size is negative\n",
0a85b6f0 584 __FILE__, __func__);
ed9eccbe
DS
585 break;
586 }
587 if ((int)(async->munge_ptr + block_size -
0a85b6f0 588 async->prealloc_bufsz) > 0)
ed9eccbe
DS
589 block_size = async->prealloc_bufsz - async->munge_ptr;
590
591 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
0a85b6f0 592 block_size, async->munge_chan);
ed9eccbe 593
ac4898a0
ZR
594 smp_wmb(); /* barrier insures data is munged in buffer
595 * before munge_count is incremented */
ed9eccbe
DS
596
597 async->munge_chan += block_size / num_sample_bytes;
598 async->munge_chan %= async->cmd.chanlist_len;
599 async->munge_count += block_size;
600 async->munge_ptr += block_size;
601 async->munge_ptr %= async->prealloc_bufsz;
602 count += block_size;
603 }
2961f24f 604 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
ed9eccbe
DS
605 return count;
606}
607
d163679c 608unsigned int comedi_buf_write_n_available(struct comedi_async *async)
ed9eccbe
DS
609{
610 unsigned int free_end;
611 unsigned int nbytes;
612
613 if (async == NULL)
614 return 0;
615
616 free_end = async->buf_read_count + async->prealloc_bufsz;
617 nbytes = free_end - async->buf_write_alloc_count;
618 nbytes -= nbytes % bytes_per_sample(async->subdevice);
619 /* barrier insures the read of buf_read_count in this
620 query occurs before any following writes to the buffer which
621 might be based on the return value from this query.
622 */
623 smp_mb();
624 return nbytes;
625}
626
627/* allocates chunk for the writer from free buffer space */
0a85b6f0
MT
628unsigned int comedi_buf_write_alloc(struct comedi_async *async,
629 unsigned int nbytes)
ed9eccbe
DS
630{
631 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
632
5617f9da 633 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
ed9eccbe 634 nbytes = free_end - async->buf_write_alloc_count;
5617f9da 635
ed9eccbe
DS
636 async->buf_write_alloc_count += nbytes;
637 /* barrier insures the read of buf_read_count above occurs before
638 we write data to the write-alloc'ed buffer space */
639 smp_mb();
640 return nbytes;
641}
d58214b0 642EXPORT_SYMBOL(comedi_buf_write_alloc);
ed9eccbe
DS
643
644/* allocates nothing unless it can completely fulfill the request */
d163679c 645unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
0a85b6f0 646 unsigned int nbytes)
ed9eccbe
DS
647{
648 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
649
5617f9da 650 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
ed9eccbe 651 nbytes = 0;
5617f9da 652
ed9eccbe
DS
653 async->buf_write_alloc_count += nbytes;
654 /* barrier insures the read of buf_read_count above occurs before
655 we write data to the write-alloc'ed buffer space */
656 smp_mb();
657 return nbytes;
658}
659
660/* transfers a chunk from writer to filled buffer space */
d163679c 661unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes)
ed9eccbe
DS
662{
663 if ((int)(async->buf_write_count + nbytes -
0a85b6f0 664 async->buf_write_alloc_count) > 0) {
88ab8a84 665 printk(KERN_INFO "comedi: attempted to write-free more bytes than have been write-allocated.\n");
ed9eccbe
DS
666 nbytes = async->buf_write_alloc_count - async->buf_write_count;
667 }
668 async->buf_write_count += nbytes;
669 async->buf_write_ptr += nbytes;
670 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
5617f9da 671 if (async->buf_write_ptr >= async->prealloc_bufsz)
ed9eccbe 672 async->buf_write_ptr %= async->prealloc_bufsz;
5617f9da 673
ed9eccbe
DS
674 return nbytes;
675}
d58214b0 676EXPORT_SYMBOL(comedi_buf_write_free);
ed9eccbe
DS
677
678/* allocates a chunk for the reader from filled (and munged) buffer space */
d163679c 679unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes)
ed9eccbe
DS
680{
681 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
0a85b6f0 682 0) {
ed9eccbe
DS
683 nbytes = async->munge_count - async->buf_read_alloc_count;
684 }
685 async->buf_read_alloc_count += nbytes;
686 /* barrier insures read of munge_count occurs before we actually read
687 data out of buffer */
688 smp_rmb();
689 return nbytes;
690}
d58214b0 691EXPORT_SYMBOL(comedi_buf_read_alloc);
ed9eccbe
DS
692
693/* transfers control of a chunk from reader to free buffer space */
d163679c 694unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes)
ed9eccbe 695{
ac4898a0
ZR
696 /* barrier insures data has been read out of
697 * buffer before read count is incremented */
ed9eccbe
DS
698 smp_mb();
699 if ((int)(async->buf_read_count + nbytes -
0a85b6f0 700 async->buf_read_alloc_count) > 0) {
67b0e64a
MR
701 printk(KERN_INFO
702 "comedi: attempted to read-free more bytes than have been read-allocated.\n");
ed9eccbe
DS
703 nbytes = async->buf_read_alloc_count - async->buf_read_count;
704 }
705 async->buf_read_count += nbytes;
706 async->buf_read_ptr += nbytes;
707 async->buf_read_ptr %= async->prealloc_bufsz;
708 return nbytes;
709}
d58214b0 710EXPORT_SYMBOL(comedi_buf_read_free);
ed9eccbe 711
d163679c 712void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
0a85b6f0 713 const void *data, unsigned int num_bytes)
ed9eccbe
DS
714{
715 unsigned int write_ptr = async->buf_write_ptr + offset;
716
717 if (write_ptr >= async->prealloc_bufsz)
718 write_ptr %= async->prealloc_bufsz;
719
720 while (num_bytes) {
721 unsigned int block_size;
722
723 if (write_ptr + num_bytes > async->prealloc_bufsz)
724 block_size = async->prealloc_bufsz - write_ptr;
725 else
726 block_size = num_bytes;
727
728 memcpy(async->prealloc_buf + write_ptr, data, block_size);
729
730 data += block_size;
731 num_bytes -= block_size;
732
733 write_ptr = 0;
734 }
735}
d58214b0 736EXPORT_SYMBOL(comedi_buf_memcpy_to);
ed9eccbe 737
d163679c 738void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
0a85b6f0 739 void *dest, unsigned int nbytes)
ed9eccbe
DS
740{
741 void *src;
742 unsigned int read_ptr = async->buf_read_ptr + offset;
743
744 if (read_ptr >= async->prealloc_bufsz)
745 read_ptr %= async->prealloc_bufsz;
746
747 while (nbytes) {
748 unsigned int block_size;
749
750 src = async->prealloc_buf + read_ptr;
751
752 if (nbytes >= async->prealloc_bufsz - read_ptr)
753 block_size = async->prealloc_bufsz - read_ptr;
754 else
755 block_size = nbytes;
756
757 memcpy(dest, src, block_size);
758 nbytes -= block_size;
759 dest += block_size;
760 read_ptr = 0;
761 }
762}
d58214b0 763EXPORT_SYMBOL(comedi_buf_memcpy_from);
ed9eccbe 764
d163679c 765unsigned int comedi_buf_read_n_available(struct comedi_async *async)
ed9eccbe
DS
766{
767 unsigned num_bytes;
768
769 if (async == NULL)
770 return 0;
771 num_bytes = async->munge_count - async->buf_read_count;
772 /* barrier insures the read of munge_count in this
773 query occurs before any following reads of the buffer which
774 might be based on the return value from this query.
775 */
776 smp_rmb();
777 return num_bytes;
778}
d58214b0 779EXPORT_SYMBOL(comedi_buf_read_n_available);
ed9eccbe 780
d163679c 781int comedi_buf_get(struct comedi_async *async, short *x)
ed9eccbe
DS
782{
783 unsigned int n = comedi_buf_read_n_available(async);
784
790c5541 785 if (n < sizeof(short))
ed9eccbe 786 return 0;
790c5541 787 comedi_buf_read_alloc(async, sizeof(short));
0a85b6f0 788 *x = *(short *)(async->prealloc_buf + async->buf_read_ptr);
790c5541 789 comedi_buf_read_free(async, sizeof(short));
ed9eccbe
DS
790 return 1;
791}
d58214b0 792EXPORT_SYMBOL(comedi_buf_get);
ed9eccbe 793
d163679c 794int comedi_buf_put(struct comedi_async *async, short x)
ed9eccbe 795{
790c5541 796 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short));
ed9eccbe 797
790c5541 798 if (n < sizeof(short)) {
ed9eccbe
DS
799 async->events |= COMEDI_CB_ERROR;
800 return 0;
801 }
0a85b6f0 802 *(short *)(async->prealloc_buf + async->buf_write_ptr) = x;
790c5541 803 comedi_buf_write_free(async, sizeof(short));
ed9eccbe
DS
804 return 1;
805}
d58214b0 806EXPORT_SYMBOL(comedi_buf_put);
ed9eccbe 807
d163679c 808void comedi_reset_async_buf(struct comedi_async *async)
ed9eccbe
DS
809{
810 async->buf_write_alloc_count = 0;
811 async->buf_write_count = 0;
812 async->buf_read_alloc_count = 0;
813 async->buf_read_count = 0;
814
815 async->buf_write_ptr = 0;
816 async->buf_read_ptr = 0;
817
818 async->cur_chan = 0;
819 async->scan_progress = 0;
820 async->munge_chan = 0;
821 async->munge_count = 0;
822 async->munge_ptr = 0;
823
824 async->events = 0;
825}
826
f4011670
IA
827static int
828comedi_auto_config_helper(struct device *hardware_device,
829 struct comedi_driver *driver,
830 int (*attach_wrapper) (struct comedi_device *,
831 void *), void *context)
832{
833 int minor;
834 struct comedi_device_file_info *dev_file_info;
835 struct comedi_device *comedi_dev;
836 int ret;
837
838 if (!comedi_autoconfig)
839 return 0;
840
841 minor = comedi_alloc_board_minor(hardware_device);
842 if (minor < 0)
843 return minor;
844
845 dev_file_info = comedi_get_device_file_info(minor);
846 comedi_dev = dev_file_info->device;
847
848 mutex_lock(&comedi_dev->mutex);
849 if (comedi_dev->attached)
850 ret = -EBUSY;
851 else if (!try_module_get(driver->module)) {
852 printk(KERN_INFO "comedi: failed to increment module count\n");
853 ret = -EIO;
854 } else {
855 /* set comedi_dev->driver here for attach wrapper */
856 comedi_dev->driver = driver;
857 ret = (*attach_wrapper)(comedi_dev, context);
858 if (ret < 0) {
859 module_put(driver->module);
860 __comedi_device_detach(comedi_dev);
861 } else {
862 ret = comedi_device_postconfig(comedi_dev);
863 }
864 }
865 mutex_unlock(&comedi_dev->mutex);
866
867 if (ret < 0)
868 comedi_free_board_minor(minor);
869 return ret;
870}
871
cf938c24
IA
872static int comedi_auto_config_wrapper(struct comedi_device *dev, void *context)
873{
874 struct comedi_devconfig *it = context;
875 struct comedi_driver *driv = dev->driver;
876
877 if (driv->num_names) {
878 /* look for generic board entry matching driver name, which
879 * has already been copied to it->board_name */
880 dev->board_ptr = comedi_recognize(driv, it->board_name);
881 if (dev->board_ptr == NULL) {
882 printk(KERN_WARNING
883 "comedi: auto config failed to find board entry"
884 " '%s' for driver '%s'\n", it->board_name,
885 driv->driver_name);
886 comedi_report_boards(driv);
887 return -EINVAL;
888 }
889 }
890 return driv->attach(dev, it);
891}
892
7029a874 893static int comedi_auto_config(struct device *hardware_device,
63bf3d11 894 struct comedi_driver *driver, const int *options,
7029a874 895 unsigned num_options)
ed9eccbe 896{
0707bb04 897 struct comedi_devconfig it;
ed9eccbe
DS
898
899 memset(&it, 0, sizeof(it));
63bf3d11 900 strncpy(it.board_name, driver->driver_name, COMEDI_NAMELEN);
ed9eccbe
DS
901 it.board_name[COMEDI_NAMELEN - 1] = '\0';
902 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
903 memcpy(it.options, options, num_options * sizeof(int));
cf938c24
IA
904 return comedi_auto_config_helper(hardware_device, driver,
905 comedi_auto_config_wrapper, &it);
ed9eccbe
DS
906}
907
7029a874 908static void comedi_auto_unconfig(struct device *hardware_device)
ed9eccbe 909{
c43435d7 910 int minor;
ed9eccbe 911
c43435d7
IA
912 if (hardware_device == NULL)
913 return;
914 minor = comedi_find_board_minor(hardware_device);
915 if (minor < 0)
916 return;
917 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
918 comedi_free_board_minor(minor);
ed9eccbe
DS
919}
920
55c03cff
HS
921/**
922 * comedi_pci_enable() - Enable the PCI device and request the regions.
923 * @pdev: pci_dev struct
924 * @res_name: name for the requested reqource
925 */
926int comedi_pci_enable(struct pci_dev *pdev, const char *res_name)
927{
928 int rc;
929
930 rc = pci_enable_device(pdev);
931 if (rc < 0)
932 return rc;
933
934 rc = pci_request_regions(pdev, res_name);
935 if (rc < 0)
936 pci_disable_device(pdev);
937
938 return rc;
939}
940EXPORT_SYMBOL_GPL(comedi_pci_enable);
941
942/**
943 * comedi_pci_disable() - Release the regions and disable the PCI device.
944 * @pdev: pci_dev struct
945 *
946 * This must be matched with a previous successful call to comedi_pci_enable().
947 */
948void comedi_pci_disable(struct pci_dev *pdev)
949{
950 pci_release_regions(pdev);
951 pci_disable_device(pdev);
952}
953EXPORT_SYMBOL_GPL(comedi_pci_disable);
954
f4011670
IA
955static int comedi_old_pci_auto_config(struct pci_dev *pcidev,
956 struct comedi_driver *driver)
ed9eccbe
DS
957{
958 int options[2];
959
b6c77757 960 /* pci bus */
ed9eccbe 961 options[0] = pcidev->bus->number;
b6c77757 962 /* pci slot */
ed9eccbe
DS
963 options[1] = PCI_SLOT(pcidev->devfn);
964
63bf3d11 965 return comedi_auto_config(&pcidev->dev, driver,
8629efa4 966 options, ARRAY_SIZE(options));
ed9eccbe 967}
f4011670
IA
968
969static int comedi_pci_attach_wrapper(struct comedi_device *dev, void *pcidev)
970{
971 return dev->driver->attach_pci(dev, pcidev);
972}
973
974static int comedi_new_pci_auto_config(struct pci_dev *pcidev,
975 struct comedi_driver *driver)
976{
977 return comedi_auto_config_helper(&pcidev->dev, driver,
978 comedi_pci_attach_wrapper, pcidev);
979}
980
981int comedi_pci_auto_config(struct pci_dev *pcidev, struct comedi_driver *driver)
982{
983
984 if (driver->attach_pci)
985 return comedi_new_pci_auto_config(pcidev, driver);
986 else
987 return comedi_old_pci_auto_config(pcidev, driver);
988}
4bf93559 989EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
ed9eccbe
DS
990
991void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
992{
993 comedi_auto_unconfig(&pcidev->dev);
994}
4bf93559 995EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
c28264da 996
d4899c6f
HS
997int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
998 struct pci_driver *pci_driver)
999{
1000 int ret;
1001
1002 ret = comedi_driver_register(comedi_driver);
1003 if (ret < 0)
1004 return ret;
1005
1006 /* FIXME: Remove this test after auditing all comedi pci drivers */
1007 if (!pci_driver->name)
1008 pci_driver->name = comedi_driver->driver_name;
1009
1010 ret = pci_register_driver(pci_driver);
1011 if (ret < 0) {
1012 comedi_driver_unregister(comedi_driver);
1013 return ret;
1014 }
1015
1016 return 0;
1017}
1018EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
1019
1020void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
1021 struct pci_driver *pci_driver)
1022{
1023 pci_unregister_driver(pci_driver);
1024 comedi_driver_unregister(comedi_driver);
1025}
1026EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);
1027
f4011670
IA
1028static int comedi_old_usb_auto_config(struct usb_interface *intf,
1029 struct comedi_driver *driver)
1030{
63bf3d11 1031 return comedi_auto_config(&intf->dev, driver, NULL, 0);
f4011670
IA
1032}
1033
1034static int comedi_usb_attach_wrapper(struct comedi_device *dev, void *intf)
1035{
1036 return dev->driver->attach_usb(dev, intf);
1037}
1038
1039static int comedi_new_usb_auto_config(struct usb_interface *intf,
1040 struct comedi_driver *driver)
1041{
1042 return comedi_auto_config_helper(&intf->dev, driver,
1043 comedi_usb_attach_wrapper, intf);
1044}
1045
d8b6ca08 1046int comedi_usb_auto_config(struct usb_interface *intf,
4c093a6d 1047 struct comedi_driver *driver)
c28264da 1048{
d8b6ca08 1049 BUG_ON(intf == NULL);
f4011670
IA
1050 if (driver->attach_usb)
1051 return comedi_new_usb_auto_config(intf, driver);
1052 else
1053 return comedi_old_usb_auto_config(intf, driver);
c28264da 1054}
4bf93559 1055EXPORT_SYMBOL_GPL(comedi_usb_auto_config);
c28264da 1056
d8b6ca08 1057void comedi_usb_auto_unconfig(struct usb_interface *intf)
c28264da 1058{
d8b6ca08
IA
1059 BUG_ON(intf == NULL);
1060 comedi_auto_unconfig(&intf->dev);
c28264da 1061}
4bf93559 1062EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);
64255031
HS
1063
1064int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
1065 struct usb_driver *usb_driver)
1066{
1067 int ret;
1068
1069 ret = comedi_driver_register(comedi_driver);
1070 if (ret < 0)
1071 return ret;
1072
1073 ret = usb_register(usb_driver);
1074 if (ret < 0) {
1075 comedi_driver_unregister(comedi_driver);
1076 return ret;
1077 }
1078
1079 return 0;
1080}
1081EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
1082
1083void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
1084 struct usb_driver *usb_driver)
1085{
1086 usb_deregister(usb_driver);
1087 comedi_driver_unregister(comedi_driver);
1088}
1089EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);