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