range.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * comedi/range.c
  3. * comedi routines for voltage ranges
  4. *
  5. * COMEDI - Linux Control and Measurement Device Interface
  6. * Copyright (C) 1997-8 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/uaccess.h>
  19. #include "comedidev.h"
  20. #include "comedi_internal.h"
  21. const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
  22. EXPORT_SYMBOL_GPL(range_bipolar10);
  23. const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
  24. EXPORT_SYMBOL_GPL(range_bipolar5);
  25. const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
  26. EXPORT_SYMBOL_GPL(range_bipolar2_5);
  27. const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
  28. EXPORT_SYMBOL_GPL(range_unipolar10);
  29. const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
  30. EXPORT_SYMBOL_GPL(range_unipolar5);
  31. const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
  32. EXPORT_SYMBOL_GPL(range_unipolar2_5);
  33. const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
  34. EXPORT_SYMBOL_GPL(range_0_20mA);
  35. const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
  36. EXPORT_SYMBOL_GPL(range_4_20mA);
  37. const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} };
  38. EXPORT_SYMBOL_GPL(range_0_32mA);
  39. const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
  40. EXPORT_SYMBOL_GPL(range_unknown);
  41. /*
  42. * COMEDI_RANGEINFO ioctl
  43. * range information
  44. *
  45. * arg:
  46. * pointer to comedi_rangeinfo structure
  47. *
  48. * reads:
  49. * comedi_rangeinfo structure
  50. *
  51. * writes:
  52. * array of comedi_krange structures to rangeinfo->range_ptr pointer
  53. */
  54. int do_rangeinfo_ioctl(struct comedi_device *dev,
  55. struct comedi_rangeinfo __user *arg)
  56. {
  57. struct comedi_rangeinfo it;
  58. int subd, chan;
  59. const struct comedi_lrange *lr;
  60. struct comedi_subdevice *s;
  61. if (copy_from_user(&it, arg, sizeof(struct comedi_rangeinfo)))
  62. return -EFAULT;
  63. subd = (it.range_type >> 24) & 0xf;
  64. chan = (it.range_type >> 16) & 0xff;
  65. if (!dev->attached)
  66. return -EINVAL;
  67. if (subd >= dev->n_subdevices)
  68. return -EINVAL;
  69. s = &dev->subdevices[subd];
  70. if (s->range_table) {
  71. lr = s->range_table;
  72. } else if (s->range_table_list) {
  73. if (chan >= s->n_chan)
  74. return -EINVAL;
  75. lr = s->range_table_list[chan];
  76. } else {
  77. return -EINVAL;
  78. }
  79. if (RANGE_LENGTH(it.range_type) != lr->length) {
  80. dev_dbg(dev->class_dev,
  81. "wrong length %d should be %d (0x%08x)\n",
  82. RANGE_LENGTH(it.range_type),
  83. lr->length, it.range_type);
  84. return -EINVAL;
  85. }
  86. if (copy_to_user(it.range_ptr, lr->range,
  87. sizeof(struct comedi_krange) * lr->length))
  88. return -EFAULT;
  89. return 0;
  90. }
  91. /**
  92. * comedi_check_chanlist() - Validate each element in a chanlist.
  93. * @s: comedi_subdevice struct
  94. * @n: number of elements in the chanlist
  95. * @chanlist: the chanlist to validate
  96. *
  97. * Each element consists of a channel number, a range index, an analog
  98. * reference type and some flags, all packed into an unsigned int.
  99. *
  100. * This checks that the channel number and range index are supported by
  101. * the comedi subdevice. It does not check whether the analog reference
  102. * type and the flags are supported. Drivers that care should check those
  103. * themselves.
  104. *
  105. * Return: %0 if all @chanlist elements are valid (success),
  106. * %-EINVAL if one or more elements are invalid.
  107. */
  108. int comedi_check_chanlist(struct comedi_subdevice *s, int n,
  109. unsigned int *chanlist)
  110. {
  111. struct comedi_device *dev = s->device;
  112. unsigned int chanspec;
  113. int chan, range_len, i;
  114. for (i = 0; i < n; i++) {
  115. chanspec = chanlist[i];
  116. chan = CR_CHAN(chanspec);
  117. if (s->range_table)
  118. range_len = s->range_table->length;
  119. else if (s->range_table_list && chan < s->n_chan)
  120. range_len = s->range_table_list[chan]->length;
  121. else
  122. range_len = 0;
  123. if (chan >= s->n_chan ||
  124. CR_RANGE(chanspec) >= range_len) {
  125. dev_warn(dev->class_dev,
  126. "bad chanlist[%d]=0x%08x chan=%d range length=%d\n",
  127. i, chanspec, chan, range_len);
  128. return -EINVAL;
  129. }
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(comedi_check_chanlist);