comedidev.h revision 3ca88dd5c3c6739f685793539a679ab5ac85aca3
1/* 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> 29#include <linux/kdev_t.h> 30#include <linux/slab.h> 31#include <linux/delay.h> 32#include <linux/errno.h> 33#include <linux/spinlock.h> 34#include <linux/mutex.h> 35#include <linux/wait.h> 36#include <linux/mm.h> 37#include <linux/init.h> 38#include <linux/vmalloc.h> 39#include <linux/dma-mapping.h> 40#include <linux/uaccess.h> 41#include <linux/io.h> 42#include <linux/timer.h> 43 44#include "comedi.h" 45 46#define DPRINTK(format, args...) do { \ 47 if (comedi_debug) \ 48 printk(KERN_DEBUG "comedi: " format , ## args); \ 49} while (0) 50 51#define COMEDI_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c)) 52#define COMEDI_VERSION_CODE COMEDI_VERSION(COMEDI_MAJORVERSION, \ 53 COMEDI_MINORVERSION, COMEDI_MICROVERSION) 54#define COMEDI_RELEASE VERSION 55 56#define COMEDI_INITCLEANUP_NOMODULE(x) \ 57 static int __init x ## _init_module(void) \ 58 {return comedi_driver_register(&(x)); } \ 59 static void __exit x ## _cleanup_module(void) \ 60 {comedi_driver_unregister(&(x)); } \ 61 module_init(x ## _init_module); \ 62 module_exit(x ## _cleanup_module); 63 64#define COMEDI_MODULE_MACROS \ 65 MODULE_AUTHOR("Comedi http://www.comedi.org"); \ 66 MODULE_DESCRIPTION("Comedi low-level driver"); \ 67 MODULE_LICENSE("GPL"); 68 69#define COMEDI_INITCLEANUP(x) \ 70 COMEDI_MODULE_MACROS \ 71 COMEDI_INITCLEANUP_NOMODULE(x) 72 73#define COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table) \ 74 static int __devinit comedi_driver ## _pci_probe(struct pci_dev *dev, \ 75 const struct pci_device_id *ent) \ 76 { \ 77 return comedi_pci_auto_config(dev, comedi_driver.driver_name); \ 78 } \ 79 static void __devexit comedi_driver ## _pci_remove(\ 80 struct pci_dev *dev) \ 81 { \ 82 comedi_pci_auto_unconfig(dev); \ 83 } \ 84 static struct pci_driver comedi_driver ## _pci_driver = \ 85 { \ 86 .id_table = pci_id_table, \ 87 .probe = &comedi_driver ## _pci_probe, \ 88 .remove = __devexit_p(&comedi_driver ## _pci_remove) \ 89 }; \ 90 static int __init comedi_driver ## _init_module(void) \ 91 { \ 92 int retval; \ 93 retval = comedi_driver_register(&comedi_driver); \ 94 if (retval < 0) \ 95 return retval; \ 96 comedi_driver ## _pci_driver.name = \ 97 (char *)comedi_driver.driver_name; \ 98 return pci_register_driver(&comedi_driver ## _pci_driver); \ 99 } \ 100 static void __exit comedi_driver ## _cleanup_module(void) \ 101 { \ 102 pci_unregister_driver(&comedi_driver ## _pci_driver); \ 103 comedi_driver_unregister(&comedi_driver); \ 104 } \ 105 module_init(comedi_driver ## _init_module); \ 106 module_exit(comedi_driver ## _cleanup_module); 107 108#define COMEDI_PCI_INITCLEANUP(comedi_driver, pci_id_table) \ 109 COMEDI_MODULE_MACROS \ 110 COMEDI_PCI_INITCLEANUP_NOMODULE(comedi_driver, pci_id_table) 111 112#define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307 113#define PCI_VENDOR_ID_ADVANTECH 0x13fe 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 119#define COMEDI_NUM_MINORS 0x100 120#define COMEDI_NUM_BOARD_MINORS 0x30 121#define COMEDI_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS 122 123#define COMEDI_DEVICE_CREATE(cs, parent, devt, drvdata, device, fmt...) \ 124 device_create(cs, ((parent) ? (parent) : (device)), devt, drvdata, fmt) 125 126struct comedi_subdevice { 127 struct comedi_device *device; 128 int type; 129 int n_chan; 130 int subdev_flags; 131 int len_chanlist; /* maximum length of channel/gain list */ 132 133 void *private; 134 135 struct comedi_async *async; 136 137 void *lock; 138 void *busy; 139 unsigned runflags; 140 spinlock_t spin_lock; 141 142 int io_bits; 143 144 unsigned int maxdata; /* if maxdata==0, use list */ 145 const unsigned int *maxdata_list; /* list is channel specific */ 146 147 unsigned int flags; 148 const unsigned int *flaglist; 149 150 unsigned int settling_time_0; 151 152 const struct comedi_lrange *range_table; 153 const struct comedi_lrange *const *range_table_list; 154 155 unsigned int *chanlist; /* driver-owned chanlist (not used) */ 156 157 int (*insn_read) (struct comedi_device *, struct comedi_subdevice *, 158 struct comedi_insn *, unsigned int *); 159 int (*insn_write) (struct comedi_device *, struct comedi_subdevice *, 160 struct comedi_insn *, unsigned int *); 161 int (*insn_bits) (struct comedi_device *, struct comedi_subdevice *, 162 struct comedi_insn *, unsigned int *); 163 int (*insn_config) (struct comedi_device *, struct comedi_subdevice *, 164 struct comedi_insn *, unsigned int *); 165 166 int (*do_cmd) (struct comedi_device *, struct comedi_subdevice *); 167 int (*do_cmdtest) (struct comedi_device *, struct comedi_subdevice *, 168 struct comedi_cmd *); 169 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 *, \ 173 struct comedi_subdevice *); */ 174 175 /* called when the buffer changes */ 176 int (*buf_change) (struct comedi_device *dev, 177 struct comedi_subdevice *s, unsigned long new_size); 178 179 void (*munge) (struct comedi_device *dev, struct comedi_subdevice *s, 180 void *data, unsigned int num_bytes, 181 unsigned int start_chan_index); 182 enum dma_data_direction async_dma_dir; 183 184 unsigned int state; 185 186 struct device *class_dev; 187 int minor; 188}; 189 190struct comedi_buf_page { 191 void *virt_addr; 192 dma_addr_t dma_addr; 193}; 194 195struct comedi_async { 196 struct comedi_subdevice *subdevice; 197 198 void *prealloc_buf; /* pre-allocated buffer */ 199 unsigned int prealloc_bufsz; /* buffer size, in bytes */ 200 /* virtual and dma address of each page */ 201 struct comedi_buf_page *buf_page_list; 202 unsigned n_buf_pages; /* num elements in buf_page_list */ 203 204 unsigned int max_bufsize; /* maximum buffer size, bytes */ 205 /* current number of mmaps of prealloc_buf */ 206 unsigned int mmap_count; 207 208 /* byte count for writer (write completed) */ 209 unsigned int buf_write_count; 210 /* byte count for writer (allocated for writing) */ 211 unsigned int buf_write_alloc_count; 212 /* byte count for reader (read completed) */ 213 unsigned int buf_read_count; 214 /* byte count for reader (allocated for reading) */ 215 unsigned int buf_read_alloc_count; 216 217 unsigned int buf_write_ptr; /* buffer marker for writer */ 218 unsigned int buf_read_ptr; /* buffer marker for reader */ 219 220 unsigned int cur_chan; /* useless channel marker for interrupt */ 221 /* number of bytes that have been received for current scan */ 222 unsigned int scan_progress; 223 /* keeps track of where we are in chanlist as for munging */ 224 unsigned int munge_chan; 225 /* number of bytes that have been munged */ 226 unsigned int munge_count; 227 /* buffer marker for munging */ 228 unsigned int munge_ptr; 229 230 unsigned int events; /* events that have occurred */ 231 232 struct comedi_cmd cmd; 233 234 wait_queue_head_t wait_head; 235 236 /* callback stuff */ 237 unsigned int cb_mask; 238 int (*cb_func) (unsigned int flags, void *); 239 void *cb_arg; 240 241 int (*inttrig) (struct comedi_device *dev, struct comedi_subdevice *s, 242 unsigned int x); 243}; 244 245struct comedi_driver { 246 struct comedi_driver *next; 247 248 const char *driver_name; 249 struct module *module; 250 int (*attach) (struct comedi_device *, struct comedi_devconfig *); 251 int (*detach) (struct comedi_device *); 252 253 /* number of elements in board_name and board_id arrays */ 254 unsigned int num_names; 255 const char *const *board_name; 256 /* offset in bytes from one board name pointer to the next */ 257 int offset; 258}; 259 260struct comedi_device { 261 int use_count; 262 struct comedi_driver *driver; 263 void *private; 264 265 struct device *class_dev; 266 int minor; 267 /* hw_dev is passed to dma_alloc_coherent when allocating async buffers 268 * for subdevices that have async_dma_dir set to something other than 269 * DMA_NONE */ 270 struct device *hw_dev; 271 272 const char *board_name; 273 const void *board_ptr; 274 int attached; 275 spinlock_t spinlock; 276 struct mutex mutex; 277 int in_request_module; 278 279 int n_subdevices; 280 struct comedi_subdevice *subdevices; 281 282 /* dumb */ 283 unsigned long iobase; 284 unsigned int irq; 285 286 struct comedi_subdevice *read_subdev; 287 struct comedi_subdevice *write_subdev; 288 289 struct fasync_struct *async_queue; 290 291 void (*open) (struct comedi_device *dev); 292 void (*close) (struct comedi_device *dev); 293}; 294 295struct comedi_device_file_info { 296 struct comedi_device *device; 297 struct comedi_subdevice *read_subdevice; 298 struct comedi_subdevice *write_subdevice; 299}; 300 301#ifdef CONFIG_COMEDI_DEBUG 302extern int comedi_debug; 303#else 304static const int comedi_debug; 305#endif 306 307/* 308 * function prototypes 309 */ 310 311void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s); 312void comedi_error(const struct comedi_device *dev, const char *s); 313 314/* we can expand the number of bits used to encode devices/subdevices into 315 the minor number soon, after more distros support > 8 bit minor numbers 316 (like after Debian Etch gets released) */ 317enum comedi_minor_bits { 318 COMEDI_DEVICE_MINOR_MASK = 0xf, 319 COMEDI_SUBDEVICE_MINOR_MASK = 0xf0 320}; 321static const unsigned COMEDI_SUBDEVICE_MINOR_SHIFT = 4; 322static const unsigned COMEDI_SUBDEVICE_MINOR_OFFSET = 1; 323 324struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor); 325 326static inline struct comedi_subdevice *comedi_get_read_subdevice( 327 const struct comedi_device_file_info *info) 328{ 329 if (info->read_subdevice) 330 return info->read_subdevice; 331 if (info->device == NULL) 332 return NULL; 333 return info->device->read_subdev; 334} 335 336static inline struct comedi_subdevice *comedi_get_write_subdevice( 337 const struct comedi_device_file_info *info) 338{ 339 if (info->write_subdevice) 340 return info->write_subdevice; 341 if (info->device == NULL) 342 return NULL; 343 return info->device->write_subdev; 344} 345 346void comedi_device_detach(struct comedi_device *dev); 347int comedi_device_attach(struct comedi_device *dev, 348 struct comedi_devconfig *it); 349int comedi_driver_register(struct comedi_driver *); 350int comedi_driver_unregister(struct comedi_driver *); 351 352void init_polling(void); 353void cleanup_polling(void); 354void start_polling(struct comedi_device *); 355void stop_polling(struct comedi_device *); 356 357int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s, 358 unsigned long new_size); 359 360#ifdef CONFIG_PROC_FS 361void comedi_proc_init(void); 362void comedi_proc_cleanup(void); 363#else 364static inline void comedi_proc_init(void) 365{ 366} 367 368static inline void comedi_proc_cleanup(void) 369{ 370} 371#endif 372 373/* subdevice runflags */ 374enum subdevice_runflags { 375 SRF_USER = 0x00000001, 376 SRF_RT = 0x00000002, 377 /* indicates an COMEDI_CB_ERROR event has occurred since the last 378 * command was started */ 379 SRF_ERROR = 0x00000004, 380 SRF_RUNNING = 0x08000000 381}; 382 383/* 384 various internal comedi functions 385 */ 386 387int do_rangeinfo_ioctl(struct comedi_device *dev, struct comedi_rangeinfo *arg); 388int check_chanlist(struct comedi_subdevice *s, int n, unsigned int *chanlist); 389void comedi_set_subdevice_runflags(struct comedi_subdevice *s, unsigned mask, 390 unsigned bits); 391unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s); 392int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s, 393 struct comedi_insn *insn, unsigned int *data); 394 395/* range stuff */ 396 397#define RANGE(a, b) {(a)*1e6, (b)*1e6, 0} 398#define RANGE_ext(a, b) {(a)*1e6, (b)*1e6, RF_EXTERNAL} 399#define RANGE_mA(a, b) {(a)*1e6, (b)*1e6, UNIT_mA} 400#define RANGE_unitless(a, b) {(a)*1e6, (b)*1e6, 0} 401#define BIP_RANGE(a) {-(a)*1e6, (a)*1e6, 0} 402#define UNI_RANGE(a) {0, (a)*1e6, 0} 403 404extern const struct comedi_lrange range_bipolar10; 405extern const struct comedi_lrange range_bipolar5; 406extern const struct comedi_lrange range_bipolar2_5; 407extern const struct comedi_lrange range_unipolar10; 408extern const struct comedi_lrange range_unipolar5; 409extern const struct comedi_lrange range_unknown; 410 411#define range_digital range_unipolar5 412 413#if __GNUC__ >= 3 414#define GCC_ZERO_LENGTH_ARRAY 415#else 416#define GCC_ZERO_LENGTH_ARRAY 0 417#endif 418 419struct comedi_lrange { 420 int length; 421 struct comedi_krange range[GCC_ZERO_LENGTH_ARRAY]; 422}; 423 424/* some silly little inline functions */ 425 426static inline int alloc_subdevices(struct comedi_device *dev, 427 unsigned int num_subdevices) 428{ 429 unsigned i; 430 431 dev->n_subdevices = num_subdevices; 432 dev->subdevices = 433 kcalloc(num_subdevices, sizeof(struct comedi_subdevice), 434 GFP_KERNEL); 435 if (!dev->subdevices) 436 return -ENOMEM; 437 for (i = 0; i < num_subdevices; ++i) { 438 dev->subdevices[i].device = dev; 439 dev->subdevices[i].async_dma_dir = DMA_NONE; 440 spin_lock_init(&dev->subdevices[i].spin_lock); 441 dev->subdevices[i].minor = -1; 442 } 443 return 0; 444} 445 446static inline int alloc_private(struct comedi_device *dev, int size) 447{ 448 dev->private = kzalloc(size, GFP_KERNEL); 449 if (!dev->private) 450 return -ENOMEM; 451 return 0; 452} 453 454static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd) 455{ 456 if (subd->subdev_flags & SDF_LSAMPL) 457 return sizeof(unsigned int); 458 else 459 return sizeof(short); 460} 461 462/* must be used in attach to set dev->hw_dev if you wish to dma directly 463into comedi's buffer */ 464static inline void comedi_set_hw_dev(struct comedi_device *dev, 465 struct device *hw_dev) 466{ 467 if (dev->hw_dev) 468 put_device(dev->hw_dev); 469 470 dev->hw_dev = hw_dev; 471 if (dev->hw_dev) { 472 dev->hw_dev = get_device(dev->hw_dev); 473 BUG_ON(dev->hw_dev == NULL); 474 } 475} 476 477int comedi_buf_put(struct comedi_async *async, short x); 478int comedi_buf_get(struct comedi_async *async, short *x); 479 480unsigned int comedi_buf_write_n_available(struct comedi_async *async); 481unsigned int comedi_buf_write_alloc(struct comedi_async *async, 482 unsigned int nbytes); 483unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async, 484 unsigned int nbytes); 485unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes); 486unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes); 487unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes); 488unsigned int comedi_buf_read_n_available(struct comedi_async *async); 489void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset, 490 const void *source, unsigned int num_bytes); 491void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset, 492 void *destination, unsigned int num_bytes); 493static inline unsigned comedi_buf_write_n_allocated(struct comedi_async *async) 494{ 495 return async->buf_write_alloc_count - async->buf_write_count; 496} 497 498static inline unsigned comedi_buf_read_n_allocated(struct comedi_async *async) 499{ 500 return async->buf_read_alloc_count - async->buf_read_count; 501} 502 503void comedi_reset_async_buf(struct comedi_async *async); 504 505static inline void *comedi_aux_data(int options[], int n) 506{ 507 unsigned long address; 508 unsigned long addressLow; 509 int bit_shift; 510 if (sizeof(int) >= sizeof(void *)) 511 address = options[COMEDI_DEVCONF_AUX_DATA_LO]; 512 else { 513 address = options[COMEDI_DEVCONF_AUX_DATA_HI]; 514 bit_shift = sizeof(int) * 8; 515 address <<= bit_shift; 516 addressLow = options[COMEDI_DEVCONF_AUX_DATA_LO]; 517 addressLow &= (1UL << bit_shift) - 1; 518 address |= addressLow; 519 } 520 if (n >= 1) 521 address += options[COMEDI_DEVCONF_AUX_DATA0_LENGTH]; 522 if (n >= 2) 523 address += options[COMEDI_DEVCONF_AUX_DATA1_LENGTH]; 524 if (n >= 3) 525 address += options[COMEDI_DEVCONF_AUX_DATA2_LENGTH]; 526 BUG_ON(n > 3); 527 return (void *)address; 528} 529 530int comedi_alloc_board_minor(struct device *hardware_device); 531void comedi_free_board_minor(unsigned minor); 532int comedi_alloc_subdevice_minor(struct comedi_device *dev, 533 struct comedi_subdevice *s); 534void comedi_free_subdevice_minor(struct comedi_subdevice *s); 535int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name); 536void comedi_pci_auto_unconfig(struct pci_dev *pcidev); 537struct usb_device; /* forward declaration */ 538int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name); 539void comedi_usb_auto_unconfig(struct usb_device *usbdev); 540 541#ifdef CONFIG_COMEDI_PCI_DRIVERS 542#define CONFIG_COMEDI_PCI 543#endif 544#ifdef CONFIG_COMEDI_PCI_DRIVERS_MODULE 545#define CONFIG_COMEDI_PCI 546#endif 547#ifdef CONFIG_COMEDI_PCMCIA_DRIVERS 548#define CONFIG_COMEDI_PCMCIA 549#endif 550#ifdef CONFIG_COMEDI_PCMCIA_DRIVERS_MODULE 551#define CONFIG_COMEDI_PCMCIA 552#endif 553 554#endif /* _COMEDIDEV_H */ 555