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