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