kcomedilib_main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * kcomedilib/kcomedilib.c
  3. * a comedlib interface for kernel modules
  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. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/mm.h>
  24. #include <linux/io.h>
  25. #include "../comedi.h"
  26. #include "../comedilib.h"
  27. #include "../comedidev.h"
  28. MODULE_AUTHOR("David Schleef <ds@schleef.org>");
  29. MODULE_DESCRIPTION("Comedi kernel library");
  30. MODULE_LICENSE("GPL");
  31. struct comedi_device *comedi_open(const char *filename)
  32. {
  33. struct comedi_device *dev, *retval = NULL;
  34. unsigned int minor;
  35. if (strncmp(filename, "/dev/comedi", 11) != 0)
  36. return NULL;
  37. if (kstrtouint(filename + 11, 0, &minor))
  38. return NULL;
  39. if (minor >= COMEDI_NUM_BOARD_MINORS)
  40. return NULL;
  41. dev = comedi_dev_get_from_minor(minor);
  42. if (!dev)
  43. return NULL;
  44. down_read(&dev->attach_lock);
  45. if (dev->attached)
  46. retval = dev;
  47. else
  48. retval = NULL;
  49. up_read(&dev->attach_lock);
  50. if (!retval)
  51. comedi_dev_put(dev);
  52. return retval;
  53. }
  54. EXPORT_SYMBOL_GPL(comedi_open);
  55. int comedi_close(struct comedi_device *dev)
  56. {
  57. comedi_dev_put(dev);
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(comedi_close);
  61. static int comedi_do_insn(struct comedi_device *dev,
  62. struct comedi_insn *insn,
  63. unsigned int *data)
  64. {
  65. struct comedi_subdevice *s;
  66. int ret;
  67. mutex_lock(&dev->mutex);
  68. if (!dev->attached) {
  69. ret = -EINVAL;
  70. goto error;
  71. }
  72. /* a subdevice instruction */
  73. if (insn->subdev >= dev->n_subdevices) {
  74. ret = -EINVAL;
  75. goto error;
  76. }
  77. s = &dev->subdevices[insn->subdev];
  78. if (s->type == COMEDI_SUBD_UNUSED) {
  79. dev_err(dev->class_dev,
  80. "%d not usable subdevice\n", insn->subdev);
  81. ret = -EIO;
  82. goto error;
  83. }
  84. /* XXX check lock */
  85. ret = comedi_check_chanlist(s, 1, &insn->chanspec);
  86. if (ret < 0) {
  87. dev_err(dev->class_dev, "bad chanspec\n");
  88. ret = -EINVAL;
  89. goto error;
  90. }
  91. if (s->busy) {
  92. ret = -EBUSY;
  93. goto error;
  94. }
  95. s->busy = dev;
  96. switch (insn->insn) {
  97. case INSN_BITS:
  98. ret = s->insn_bits(dev, s, insn, data);
  99. break;
  100. case INSN_CONFIG:
  101. /* XXX should check instruction length */
  102. ret = s->insn_config(dev, s, insn, data);
  103. break;
  104. default:
  105. ret = -EINVAL;
  106. break;
  107. }
  108. s->busy = NULL;
  109. error:
  110. mutex_unlock(&dev->mutex);
  111. return ret;
  112. }
  113. int comedi_dio_get_config(struct comedi_device *dev, unsigned int subdev,
  114. unsigned int chan, unsigned int *io)
  115. {
  116. struct comedi_insn insn;
  117. unsigned int data[2];
  118. int ret;
  119. memset(&insn, 0, sizeof(insn));
  120. insn.insn = INSN_CONFIG;
  121. insn.n = 2;
  122. insn.subdev = subdev;
  123. insn.chanspec = CR_PACK(chan, 0, 0);
  124. data[0] = INSN_CONFIG_DIO_QUERY;
  125. data[1] = 0;
  126. ret = comedi_do_insn(dev, &insn, data);
  127. if (ret >= 0)
  128. *io = data[1];
  129. return ret;
  130. }
  131. EXPORT_SYMBOL_GPL(comedi_dio_get_config);
  132. int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,
  133. unsigned int chan, unsigned int io)
  134. {
  135. struct comedi_insn insn;
  136. memset(&insn, 0, sizeof(insn));
  137. insn.insn = INSN_CONFIG;
  138. insn.n = 1;
  139. insn.subdev = subdev;
  140. insn.chanspec = CR_PACK(chan, 0, 0);
  141. return comedi_do_insn(dev, &insn, &io);
  142. }
  143. EXPORT_SYMBOL_GPL(comedi_dio_config);
  144. int comedi_dio_bitfield2(struct comedi_device *dev, unsigned int subdev,
  145. unsigned int mask, unsigned int *bits,
  146. unsigned int base_channel)
  147. {
  148. struct comedi_insn insn;
  149. unsigned int data[2];
  150. unsigned int n_chan;
  151. unsigned int shift;
  152. int ret;
  153. base_channel = CR_CHAN(base_channel);
  154. n_chan = comedi_get_n_channels(dev, subdev);
  155. if (base_channel >= n_chan)
  156. return -EINVAL;
  157. memset(&insn, 0, sizeof(insn));
  158. insn.insn = INSN_BITS;
  159. insn.chanspec = base_channel;
  160. insn.n = 2;
  161. insn.subdev = subdev;
  162. data[0] = mask;
  163. data[1] = *bits;
  164. /*
  165. * Most drivers ignore the base channel in insn->chanspec.
  166. * Fix this here if the subdevice has <= 32 channels.
  167. */
  168. if (n_chan <= 32) {
  169. shift = base_channel;
  170. if (shift) {
  171. insn.chanspec = 0;
  172. data[0] <<= shift;
  173. data[1] <<= shift;
  174. }
  175. } else {
  176. shift = 0;
  177. }
  178. ret = comedi_do_insn(dev, &insn, data);
  179. *bits = data[1] >> shift;
  180. return ret;
  181. }
  182. EXPORT_SYMBOL_GPL(comedi_dio_bitfield2);
  183. int comedi_find_subdevice_by_type(struct comedi_device *dev, int type,
  184. unsigned int subd)
  185. {
  186. struct comedi_subdevice *s;
  187. int ret = -ENODEV;
  188. down_read(&dev->attach_lock);
  189. if (dev->attached)
  190. for (; subd < dev->n_subdevices; subd++) {
  191. s = &dev->subdevices[subd];
  192. if (s->type == type) {
  193. ret = subd;
  194. break;
  195. }
  196. }
  197. up_read(&dev->attach_lock);
  198. return ret;
  199. }
  200. EXPORT_SYMBOL_GPL(comedi_find_subdevice_by_type);
  201. int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
  202. {
  203. int n;
  204. down_read(&dev->attach_lock);
  205. if (!dev->attached || subdevice >= dev->n_subdevices)
  206. n = 0;
  207. else
  208. n = dev->subdevices[subdevice].n_chan;
  209. up_read(&dev->attach_lock);
  210. return n;
  211. }
  212. EXPORT_SYMBOL_GPL(comedi_get_n_channels);