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