blob: ea319360ae6839ddbfaca49d8d556eedf2a6a55d [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
2 include/linux/comedidev.h
3 header file for kernel-only structures, variables, and constants
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#ifndef _COMEDIDEV_H
25#define _COMEDIDEV_H
26
27#include <linux/kernel.h>
28#include <linux/module.h>
David Schleefed9eccb2008-11-04 20:29:31 -080029#include <linux/kdev_t.h>
30#include <linux/slab.h>
31#include <linux/errno.h>
32#include <linux/spinlock.h>
33#include <linux/mutex.h>
34#include <linux/wait.h>
35#include <linux/mm.h>
36#include <linux/init.h>
37#include <linux/vmalloc.h>
David Schleef74b847f2008-11-14 14:20:56 -080038#include "interrupt.h"
David Schleefed9eccb2008-11-04 20:29:31 -080039#include <linux/dma-mapping.h>
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -080040#include <linux/uaccess.h>
41#include <linux/io.h>
David Schleefed9eccb2008-11-04 20:29:31 -080042
43#include "comedi.h"
44
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -080045#define DPRINTK(format, args...) do { \
46 if (comedi_debug) \
47 printk(KERN_DEBUG "comedi: " format , ## args); \
48} while (0)
David Schleefed9eccb2008-11-04 20:29:31 -080049
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -080050#define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
51#define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, COMEDI_MINORVERSION, COMEDI_MICROVERSION)
David Schleefed9eccb2008-11-04 20:29:31 -080052#define COMEDI_RELEASE VERSION
53
54#define COMEDI_INITCLEANUP_NOMODULE(x) \
55 static int __init x ## _init_module(void) \
Bill Pemberton51b713a2009-03-16 22:04:12 -040056 {return comedi_driver_register(&(x)); } \
David Schleefed9eccb2008-11-04 20:29:31 -080057 static void __exit x ## _cleanup_module(void) \
Bill Pemberton51b713a2009-03-16 22:04:12 -040058 {comedi_driver_unregister(&(x)); } \
David Schleefed9eccb2008-11-04 20:29:31 -080059 module_init(x ## _init_module); \
60 module_exit(x ## _cleanup_module); \
61
62#define COMEDI_MODULE_MACROS \
63 MODULE_AUTHOR("Comedi http://www.comedi.org"); \
64 MODULE_DESCRIPTION("Comedi low-level driver"); \
65 MODULE_LICENSE("GPL"); \
66
67#define COMEDI_INITCLEANUP(x) \
68 COMEDI_MODULE_MACROS \
69 COMEDI_INITCLEANUP_NOMODULE(x)
70
71#define COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table) \
72 static int __devinit comedi_driver ## _pci_probe(struct pci_dev *dev, \
73 const struct pci_device_id *ent) \
74 { \
75 return comedi_pci_auto_config(dev, comedi_driver.driver_name); \
76 } \
77 static void __devexit comedi_driver ## _pci_remove(struct pci_dev *dev) \
78 { \
79 comedi_pci_auto_unconfig(dev); \
80 } \
81 static struct pci_driver comedi_driver ## _pci_driver = \
82 { \
83 .id_table = pci_id_table, \
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -080084 .probe = &comedi_driver ## _pci_probe, \
85 .remove = __devexit_p(&comedi_driver ## _pci_remove) \
David Schleefed9eccb2008-11-04 20:29:31 -080086 }; \
87 static int __init comedi_driver ## _init_module(void) \
88 { \
89 int retval; \
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -080090 retval = comedi_driver_register(&comedi_driver); \
91 if (retval < 0) \
92 return retval; \
93 comedi_driver ## _pci_driver.name = (char *)comedi_driver.driver_name; \
94 return pci_register_driver(&comedi_driver ## _pci_driver); \
David Schleefed9eccb2008-11-04 20:29:31 -080095 } \
96 static void __exit comedi_driver ## _cleanup_module(void) \
97 { \
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -080098 pci_unregister_driver(&comedi_driver ## _pci_driver); \
99 comedi_driver_unregister(&comedi_driver); \
David Schleefed9eccb2008-11-04 20:29:31 -0800100 } \
101 module_init(comedi_driver ## _init_module); \
102 module_exit(comedi_driver ## _cleanup_module);
103
104#define COMEDI_PCI_INITCLEANUP(comedi_driver, pci_id_table) \
105 COMEDI_MODULE_MACROS \
106 COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table)
107
108#define PCI_VENDOR_ID_INOVA 0x104c
109#define PCI_VENDOR_ID_NATINST 0x1093
110#define PCI_VENDOR_ID_DATX 0x1116
111#define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
112#define PCI_VENDOR_ID_ADVANTECH 0x13fe
113#define PCI_VENDOR_ID_RTD 0x1435
114#define PCI_VENDOR_ID_AMPLICON 0x14dc
115#define PCI_VENDOR_ID_ADLINK 0x144a
116#define PCI_VENDOR_ID_ICP 0x104c
117#define PCI_VENDOR_ID_CONTEC 0x1221
118#define PCI_VENDOR_ID_MEILHAUS 0x1402
119
120#define COMEDI_NUM_MINORS 0x100
David Schleefed9eccb2008-11-04 20:29:31 -0800121#define COMEDI_NUM_BOARD_MINORS 0x30
122#define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
123
David Schleefed9eccb2008-11-04 20:29:31 -0800124#define COMEDI_DEVICE_CREATE(cs, parent, devt, drvdata, device, fmt...) \
125 device_create(cs, ((parent) ? (parent) : (device)), devt, drvdata, fmt)
126
Bill Pemberton34c43922009-03-16 22:05:14 -0400127struct comedi_subdevice {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400128 struct comedi_device *device;
David Schleefed9eccb2008-11-04 20:29:31 -0800129 int type;
130 int n_chan;
131 volatile int subdev_flags;
132 int len_chanlist; /* maximum length of channel/gain list */
133
134 void *private;
135
Bill Pembertond1636792009-03-16 22:05:20 -0400136 struct comedi_async *async;
David Schleefed9eccb2008-11-04 20:29:31 -0800137
138 void *lock;
139 void *busy;
140 unsigned runflags;
141 spinlock_t spin_lock;
142
143 int io_bits;
144
Bill Pemberton790c5542009-03-16 22:05:02 -0400145 unsigned int maxdata; /* if maxdata==0, use list */
146 const unsigned int *maxdata_list; /* list is channel specific */
David Schleefed9eccb2008-11-04 20:29:31 -0800147
148 unsigned int flags;
149 const unsigned int *flaglist;
150
151 unsigned int settling_time_0;
152
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400153 const struct comedi_lrange *range_table;
154 const struct comedi_lrange *const *range_table_list;
David Schleefed9eccb2008-11-04 20:29:31 -0800155
156 unsigned int *chanlist; /* driver-owned chanlist (not used) */
157
Bill Pemberton90035c02009-03-16 22:05:53 -0400158 int (*insn_read) (struct comedi_device *, struct comedi_subdevice *, struct comedi_insn *,
Bill Pemberton790c5542009-03-16 22:05:02 -0400159 unsigned int *);
Bill Pemberton90035c02009-03-16 22:05:53 -0400160 int (*insn_write) (struct comedi_device *, struct comedi_subdevice *, struct comedi_insn *,
Bill Pemberton790c5542009-03-16 22:05:02 -0400161 unsigned int *);
Bill Pemberton90035c02009-03-16 22:05:53 -0400162 int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *, struct comedi_insn *,
Bill Pemberton790c5542009-03-16 22:05:02 -0400163 unsigned int *);
Bill Pemberton90035c02009-03-16 22:05:53 -0400164 int (*insn_config) (struct comedi_device *, struct comedi_subdevice *, struct comedi_insn *,
Bill Pemberton790c5542009-03-16 22:05:02 -0400165 unsigned int *);
David Schleefed9eccb2008-11-04 20:29:31 -0800166
Bill Pemberton34c43922009-03-16 22:05:14 -0400167 int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *);
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400168 int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *, struct comedi_cmd *);
Bill Pemberton34c43922009-03-16 22:05:14 -0400169 int (*poll) (struct comedi_device *, struct comedi_subdevice *);
170 int (*cancel) (struct comedi_device *, struct comedi_subdevice *);
171 /* int (*do_lock)(struct comedi_device *,struct comedi_subdevice *); */
172 /* int (*do_unlock)(struct comedi_device *,struct comedi_subdevice *); */
David Schleefed9eccb2008-11-04 20:29:31 -0800173
174 /* called when the buffer changes */
Bill Pemberton34c43922009-03-16 22:05:14 -0400175 int (*buf_change) (struct comedi_device *dev, struct comedi_subdevice *s,
David Schleefed9eccb2008-11-04 20:29:31 -0800176 unsigned long new_size);
177
Bill Pemberton34c43922009-03-16 22:05:14 -0400178 void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s, void *data,
David Schleefed9eccb2008-11-04 20:29:31 -0800179 unsigned int num_bytes, unsigned int start_chan_index);
180 enum dma_data_direction async_dma_dir;
181
182 unsigned int state;
183
Bill Pemberton0bfbbe82009-03-16 22:05:36 -0400184 struct device *class_dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800185 int minor;
186};
187
188struct comedi_buf_page {
189 void *virt_addr;
190 dma_addr_t dma_addr;
191};
192
Bill Pembertond1636792009-03-16 22:05:20 -0400193struct comedi_async {
Bill Pemberton34c43922009-03-16 22:05:14 -0400194 struct comedi_subdevice *subdevice;
David Schleefed9eccb2008-11-04 20:29:31 -0800195
196 void *prealloc_buf; /* pre-allocated buffer */
197 unsigned int prealloc_bufsz; /* buffer size, in bytes */
198 struct comedi_buf_page *buf_page_list; /* virtual and dma address of each page */
199 unsigned n_buf_pages; /* num elements in buf_page_list */
200
201 unsigned int max_bufsize; /* maximum buffer size, bytes */
202 unsigned int mmap_count; /* current number of mmaps of prealloc_buf */
203
204 unsigned int buf_write_count; /* byte count for writer (write completed) */
205 unsigned int buf_write_alloc_count; /* byte count for writer (allocated for writing) */
206 unsigned int buf_read_count; /* byte count for reader (read completed) */
207 unsigned int buf_read_alloc_count; /* byte count for reader (allocated for reading) */
208
209 unsigned int buf_write_ptr; /* buffer marker for writer */
210 unsigned int buf_read_ptr; /* buffer marker for reader */
211
212 unsigned int cur_chan; /* useless channel marker for interrupt */
213 /* number of bytes that have been received for current scan */
214 unsigned int scan_progress;
215 /* keeps track of where we are in chanlist as for munging */
216 unsigned int munge_chan;
217 /* number of bytes that have been munged */
218 unsigned int munge_count;
219 /* buffer marker for munging */
220 unsigned int munge_ptr;
221
222 unsigned int events; /* events that have occurred */
223
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400224 struct comedi_cmd cmd;
David Schleefed9eccb2008-11-04 20:29:31 -0800225
226 wait_queue_head_t wait_head;
227
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800228 /* callback stuff */
David Schleefed9eccb2008-11-04 20:29:31 -0800229 unsigned int cb_mask;
230 int (*cb_func) (unsigned int flags, void *);
231 void *cb_arg;
232
Bill Pemberton34c43922009-03-16 22:05:14 -0400233 int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s,
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800234 unsigned int x);
David Schleefed9eccb2008-11-04 20:29:31 -0800235};
236
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400237struct comedi_driver {
238 struct comedi_driver *next;
David Schleefed9eccb2008-11-04 20:29:31 -0800239
240 const char *driver_name;
241 struct module *module;
Bill Pemberton0707bb02009-03-16 22:06:20 -0400242 int (*attach) (struct comedi_device *, struct comedi_devconfig *);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400243 int (*detach) (struct comedi_device *);
David Schleefed9eccb2008-11-04 20:29:31 -0800244
245 /* number of elements in board_name and board_id arrays */
246 unsigned int num_names;
247 const char *const *board_name;
248 /* offset in bytes from one board name pointer to the next */
249 int offset;
250};
251
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400252struct comedi_device {
David Schleefed9eccb2008-11-04 20:29:31 -0800253 int use_count;
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400254 struct comedi_driver *driver;
David Schleefed9eccb2008-11-04 20:29:31 -0800255 void *private;
256
Bill Pemberton0bfbbe82009-03-16 22:05:36 -0400257 struct device *class_dev;
David Schleefed9eccb2008-11-04 20:29:31 -0800258 int minor;
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800259 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers
260 * for subdevices that have async_dma_dir set to something other than
261 * DMA_NONE */
David Schleefed9eccb2008-11-04 20:29:31 -0800262 struct device *hw_dev;
263
264 const char *board_name;
265 const void *board_ptr;
266 int attached;
267 int rt;
268 spinlock_t spinlock;
269 struct mutex mutex;
270 int in_request_module;
271
272 int n_subdevices;
Bill Pemberton34c43922009-03-16 22:05:14 -0400273 struct comedi_subdevice *subdevices;
David Schleefed9eccb2008-11-04 20:29:31 -0800274
275 /* dumb */
276 unsigned long iobase;
277 unsigned int irq;
278
Bill Pemberton34c43922009-03-16 22:05:14 -0400279 struct comedi_subdevice *read_subdev;
280 struct comedi_subdevice *write_subdev;
David Schleefed9eccb2008-11-04 20:29:31 -0800281
282 struct fasync_struct *async_queue;
283
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400284 void (*open) (struct comedi_device *dev);
285 void (*close) (struct comedi_device *dev);
David Schleefed9eccb2008-11-04 20:29:31 -0800286};
287
288struct comedi_device_file_info {
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400289 struct comedi_device *device;
Bill Pemberton34c43922009-03-16 22:05:14 -0400290 struct comedi_subdevice *read_subdevice;
291 struct comedi_subdevice *write_subdevice;
David Schleefed9eccb2008-11-04 20:29:31 -0800292};
293
294#ifdef CONFIG_COMEDI_DEBUG
295extern int comedi_debug;
296#else
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800297static const int comedi_debug;
David Schleefed9eccb2008-11-04 20:29:31 -0800298#endif
299
300/*
301 * function prototypes
302 */
303
Bill Pemberton34c43922009-03-16 22:05:14 -0400304void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400305void comedi_error(const struct comedi_device *dev, const char *s);
David Schleefed9eccb2008-11-04 20:29:31 -0800306
307/* we can expand the number of bits used to encode devices/subdevices into
308 the minor number soon, after more distros support > 8 bit minor numbers
309 (like after Debian Etch gets released) */
310enum comedi_minor_bits {
311 COMEDI_DEVICE_MINOR_MASK = 0xf,
312 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0
313};
314static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4;
315static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1;
316
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800317struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor);
David Schleefed9eccb2008-11-04 20:29:31 -0800318
Bill Pemberton34c43922009-03-16 22:05:14 -0400319static inline struct comedi_subdevice *comedi_get_read_subdevice(
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800320 const struct comedi_device_file_info *info)
David Schleefed9eccb2008-11-04 20:29:31 -0800321{
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800322 if (info->read_subdevice)
323 return info->read_subdevice;
324 if (info->device == NULL)
325 return NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800326 return info->device->read_subdev;
327}
328
Bill Pemberton34c43922009-03-16 22:05:14 -0400329static inline struct comedi_subdevice *comedi_get_write_subdevice(
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800330 const struct comedi_device_file_info *info)
David Schleefed9eccb2008-11-04 20:29:31 -0800331{
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800332 if (info->write_subdevice)
333 return info->write_subdevice;
334 if (info->device == NULL)
335 return NULL;
David Schleefed9eccb2008-11-04 20:29:31 -0800336 return info->device->write_subdev;
337}
338
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400339void comedi_device_detach(struct comedi_device *dev);
Bill Pemberton0707bb02009-03-16 22:06:20 -0400340int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it);
Bill Pemberton139dfbd2009-03-16 22:05:25 -0400341int comedi_driver_register(struct comedi_driver *);
342int comedi_driver_unregister(struct comedi_driver *);
David Schleefed9eccb2008-11-04 20:29:31 -0800343
344void init_polling(void);
345void cleanup_polling(void);
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400346void start_polling(struct comedi_device *);
347void stop_polling(struct comedi_device *);
David Schleefed9eccb2008-11-04 20:29:31 -0800348
Bill Pemberton34c43922009-03-16 22:05:14 -0400349int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s, unsigned long
David Schleefed9eccb2008-11-04 20:29:31 -0800350 new_size);
351
352#ifdef CONFIG_PROC_FS
353void comedi_proc_init(void);
354void comedi_proc_cleanup(void);
355#else
356static inline void comedi_proc_init(void)
357{
358}
359static inline void comedi_proc_cleanup(void)
360{
361}
362#endif
363
364/* subdevice runflags */
365enum subdevice_runflags {
366 SRF_USER = 0x00000001,
367 SRF_RT = 0x00000002,
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800368 /* indicates an COMEDI_CB_ERROR event has occurred since the last
369 * command was started */
David Schleefed9eccb2008-11-04 20:29:31 -0800370 SRF_ERROR = 0x00000004,
371 SRF_RUNNING = 0x08000000
372};
373
374/*
375 various internal comedi functions
376 */
377
Bill Pembertond0a353f2009-03-16 22:06:26 -0400378int do_rangeinfo_ioctl(struct comedi_device *dev, struct comedi_rangeinfo *arg);
Bill Pemberton34c43922009-03-16 22:05:14 -0400379int check_chanlist(struct comedi_subdevice *s, int n, unsigned int *chanlist);
380void comedi_set_subdevice_runflags(struct comedi_subdevice *s, unsigned mask,
David Schleefed9eccb2008-11-04 20:29:31 -0800381 unsigned bits);
Bill Pemberton34c43922009-03-16 22:05:14 -0400382unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s);
383int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
Bill Pemberton90035c02009-03-16 22:05:53 -0400384 struct comedi_insn *insn, unsigned int *data);
David Schleefed9eccb2008-11-04 20:29:31 -0800385
386/* range stuff */
387
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800388#define RANGE(a, b) {(a)*1e6, (b)*1e6, 0}
389#define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL}
390#define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA}
391#define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0} /* XXX */
392#define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0}
393#define UNI_RANGE(a) {0, (a)*1e6, 0}
David Schleefed9eccb2008-11-04 20:29:31 -0800394
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400395extern const struct comedi_lrange range_bipolar10;
396extern const struct comedi_lrange range_bipolar5;
397extern const struct comedi_lrange range_bipolar2_5;
398extern const struct comedi_lrange range_unipolar10;
399extern const struct comedi_lrange range_unipolar5;
400extern const struct comedi_lrange range_unknown;
David Schleefed9eccb2008-11-04 20:29:31 -0800401
402#define range_digital range_unipolar5
403
404#if __GNUC__ >= 3
405#define GCC_ZERO_LENGTH_ARRAY
406#else
407#define GCC_ZERO_LENGTH_ARRAY 0
408#endif
409
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400410struct comedi_lrange {
David Schleefed9eccb2008-11-04 20:29:31 -0800411 int length;
Bill Pemberton1f6325d2009-03-16 22:06:31 -0400412 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY];
David Schleefed9eccb2008-11-04 20:29:31 -0800413};
414
415/* some silly little inline functions */
416
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400417static inline int alloc_subdevices(struct comedi_device *dev,
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800418 unsigned int num_subdevices)
David Schleefed9eccb2008-11-04 20:29:31 -0800419{
420 unsigned i;
421
422 dev->n_subdevices = num_subdevices;
423 dev->subdevices =
Bill Pemberton34c43922009-03-16 22:05:14 -0400424 kcalloc(num_subdevices, sizeof(struct comedi_subdevice), GFP_KERNEL);
David Schleefed9eccb2008-11-04 20:29:31 -0800425 if (!dev->subdevices)
426 return -ENOMEM;
427 for (i = 0; i < num_subdevices; ++i) {
428 dev->subdevices[i].device = dev;
429 dev->subdevices[i].async_dma_dir = DMA_NONE;
430 spin_lock_init(&dev->subdevices[i].spin_lock);
431 dev->subdevices[i].minor = -1;
432 }
433 return 0;
434}
435
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400436static inline int alloc_private(struct comedi_device *dev, int size)
David Schleefed9eccb2008-11-04 20:29:31 -0800437{
438 dev->private = kzalloc(size, GFP_KERNEL);
439 if (!dev->private)
440 return -ENOMEM;
441 return 0;
442}
443
Bill Pemberton34c43922009-03-16 22:05:14 -0400444static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
David Schleefed9eccb2008-11-04 20:29:31 -0800445{
446 if (subd->subdev_flags & SDF_LSAMPL)
Bill Pemberton790c5542009-03-16 22:05:02 -0400447 return sizeof(unsigned int);
David Schleefed9eccb2008-11-04 20:29:31 -0800448 else
Bill Pemberton790c5542009-03-16 22:05:02 -0400449 return sizeof(short);
David Schleefed9eccb2008-11-04 20:29:31 -0800450}
451
452/* must be used in attach to set dev->hw_dev if you wish to dma directly
453into comedi's buffer */
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400454static inline void comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800455{
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800456 if (dev->hw_dev)
David Schleefed9eccb2008-11-04 20:29:31 -0800457 put_device(dev->hw_dev);
Greg Kroah-Hartmane0dcef72008-11-13 16:36:22 -0800458
David Schleefed9eccb2008-11-04 20:29:31 -0800459 dev->hw_dev = hw_dev;
460 if (dev->hw_dev) {
461 dev->hw_dev = get_device(dev->hw_dev);
462 BUG_ON(dev->hw_dev == NULL);
463 }
464}
465
Bill Pembertond1636792009-03-16 22:05:20 -0400466int comedi_buf_put(struct comedi_async *async, short x);
467int comedi_buf_get(struct comedi_async *async, short *x);
David Schleefed9eccb2008-11-04 20:29:31 -0800468
Bill Pembertond1636792009-03-16 22:05:20 -0400469unsigned int comedi_buf_write_n_available(struct comedi_async *async);
470unsigned int comedi_buf_write_alloc(struct comedi_async *async, unsigned int nbytes);
471unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
David Schleefed9eccb2008-11-04 20:29:31 -0800472 unsigned int nbytes);
Bill Pembertond1636792009-03-16 22:05:20 -0400473unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes);
474unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes);
475unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes);
476unsigned int comedi_buf_read_n_available(struct comedi_async *async);
477void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
David Schleefed9eccb2008-11-04 20:29:31 -0800478 const void *source, unsigned int num_bytes);
Bill Pembertond1636792009-03-16 22:05:20 -0400479void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
David Schleefed9eccb2008-11-04 20:29:31 -0800480 void *destination, unsigned int num_bytes);
Bill Pembertond1636792009-03-16 22:05:20 -0400481static inline unsigned comedi_buf_write_n_allocated(struct comedi_async *async)
David Schleefed9eccb2008-11-04 20:29:31 -0800482{
483 return async->buf_write_alloc_count - async->buf_write_count;
484}
Bill Pembertond1636792009-03-16 22:05:20 -0400485static inline unsigned comedi_buf_read_n_allocated(struct comedi_async *async)
David Schleefed9eccb2008-11-04 20:29:31 -0800486{
487 return async->buf_read_alloc_count - async->buf_read_count;
488}
489
Bill Pembertond1636792009-03-16 22:05:20 -0400490void comedi_reset_async_buf(struct comedi_async *async);
David Schleefed9eccb2008-11-04 20:29:31 -0800491
492static inline void *comedi_aux_data(int options[], int n)
493{
494 unsigned long address;
495 unsigned long addressLow;
496 int bit_shift;
497 if (sizeof(int) >= sizeof(void *))
498 address = options[COMEDI_DEVCONF_AUX_DATA_LO];
499 else {
500 address = options[COMEDI_DEVCONF_AUX_DATA_HI];
501 bit_shift = sizeof(int) * 8;
502 address <<= bit_shift;
503 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO];
504 addressLow &= (1UL << bit_shift) - 1;
505 address |= addressLow;
506 }
507 if (n >= 1)
508 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH];
509 if (n >= 2)
510 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH];
511 if (n >= 3)
512 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH];
513 BUG_ON(n > 3);
514 return (void *)address;
515}
516
517int comedi_alloc_board_minor(struct device *hardware_device);
518void comedi_free_board_minor(unsigned minor);
Bill Pemberton34c43922009-03-16 22:05:14 -0400519int comedi_alloc_subdevice_minor(struct comedi_device *dev, struct comedi_subdevice *s);
520void comedi_free_subdevice_minor(struct comedi_subdevice *s);
David Schleefed9eccb2008-11-04 20:29:31 -0800521int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name);
522void comedi_pci_auto_unconfig(struct pci_dev *pcidev);
Bernd Porrc28264d2008-12-08 23:30:13 +0000523struct usb_device; /* forward declaration */
524int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name);
525void comedi_usb_auto_unconfig(struct usb_device *usbdev);
David Schleefed9eccb2008-11-04 20:29:31 -0800526
David Schleefed9eccb2008-11-04 20:29:31 -0800527#include "comedi_rt.h"
David Schleefed9eccb2008-11-04 20:29:31 -0800528
529#endif /* _COMEDIDEV_H */