drivers.c revision be29eac8ed3fd21d86f79d2e84dff49b8a13cd2c
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 <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#define _GNU_SOURCE
25
26#define __NO_VERSION__
27#include "comedi_fops.h"
28#include <linux/device.h>
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/usb.h>
32#include <linux/errno.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 "comedidev.h"
41#include <linux/highmem.h>	/* for SuSE brokenness */
42#include <linux/vmalloc.h>
43#include <linux/cdev.h>
44#include <linux/dma-mapping.h>
45
46#include <linux/io.h>
47#include <asm/system.h>
48
49static int postconfig(struct comedi_device *dev);
50static int insn_rw_emulate_bits(struct comedi_device *dev,
51				struct comedi_subdevice *s,
52				struct comedi_insn *insn, unsigned int *data);
53static void *comedi_recognize(struct comedi_driver *driv, const char *name);
54static void comedi_report_boards(struct comedi_driver *driv);
55static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s);
56int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
57		     unsigned long new_size);
58
59struct comedi_driver *comedi_drivers;
60
61int comedi_modprobe(int minor)
62{
63	return -EINVAL;
64}
65
66static void cleanup_device(struct comedi_device *dev)
67{
68	int i;
69	struct comedi_subdevice *s;
70
71	if (dev->subdevices) {
72		for (i = 0; i < dev->n_subdevices; i++) {
73			s = dev->subdevices + i;
74			comedi_free_subdevice_minor(s);
75			if (s->async) {
76				comedi_buf_alloc(dev, s, 0);
77				kfree(s->async);
78			}
79		}
80		kfree(dev->subdevices);
81		dev->subdevices = NULL;
82		dev->n_subdevices = 0;
83	}
84	kfree(dev->private);
85	dev->private = NULL;
86	dev->driver = 0;
87	dev->board_name = NULL;
88	dev->board_ptr = NULL;
89	dev->iobase = 0;
90	dev->irq = 0;
91	dev->read_subdev = NULL;
92	dev->write_subdev = NULL;
93	dev->open = NULL;
94	dev->close = NULL;
95	comedi_set_hw_dev(dev, NULL);
96}
97
98static void __comedi_device_detach(struct comedi_device *dev)
99{
100	dev->attached = 0;
101	if (dev->driver)
102		dev->driver->detach(dev);
103	else
104		printk(KERN_WARNING "BUG: dev->driver=NULL in comedi_device_detach()\n");
105	cleanup_device(dev);
106}
107
108void comedi_device_detach(struct comedi_device *dev)
109{
110	if (!dev->attached)
111		return;
112	__comedi_device_detach(dev);
113}
114
115int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
116{
117	struct comedi_driver *driv;
118	int ret;
119
120	if (dev->attached)
121		return -EBUSY;
122
123	for (driv = comedi_drivers; driv; driv = driv->next) {
124		if (!try_module_get(driv->module)) {
125			printk
126			    (KERN_INFO "comedi: failed to increment module count, skipping\n");
127			continue;
128		}
129		if (driv->num_names) {
130			dev->board_ptr = comedi_recognize(driv, it->board_name);
131			if (dev->board_ptr == NULL) {
132				module_put(driv->module);
133				continue;
134			}
135		} else {
136			if (strcmp(driv->driver_name, it->board_name)) {
137				module_put(driv->module);
138				continue;
139			}
140		}
141		/* initialize dev->driver here so
142		 * comedi_error() can be called from attach */
143		dev->driver = driv;
144		ret = driv->attach(dev, it);
145		if (ret < 0) {
146			module_put(dev->driver->module);
147			__comedi_device_detach(dev);
148			return ret;
149		}
150		goto attached;
151	}
152
153	/*  recognize has failed if we get here */
154	/*  report valid board names before returning error */
155	for (driv = comedi_drivers; driv; driv = driv->next) {
156		if (!try_module_get(driv->module)) {
157			printk(KERN_INFO "comedi: failed to increment module count\n");
158			continue;
159		}
160		comedi_report_boards(driv);
161		module_put(driv->module);
162	}
163	return -EIO;
164
165attached:
166	/* do a little post-config cleanup */
167	ret = postconfig(dev);
168	module_put(dev->driver->module);
169	if (ret < 0) {
170		__comedi_device_detach(dev);
171		return ret;
172	}
173
174	if (!dev->board_name) {
175		printk(KERN_WARNING "BUG: dev->board_name=<%p>\n", dev->board_name);
176		dev->board_name = "BUG";
177	}
178	smp_wmb();
179	dev->attached = 1;
180
181	return 0;
182}
183
184int comedi_driver_register(struct comedi_driver *driver)
185{
186	driver->next = comedi_drivers;
187	comedi_drivers = driver;
188
189	return 0;
190}
191
192int comedi_driver_unregister(struct comedi_driver *driver)
193{
194	struct comedi_driver *prev;
195	int i;
196
197	/* check for devices using this driver */
198	for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
199		struct comedi_device_file_info *dev_file_info =
200		    comedi_get_device_file_info(i);
201		struct comedi_device *dev;
202
203		if (dev_file_info == NULL)
204			continue;
205		dev = dev_file_info->device;
206
207		mutex_lock(&dev->mutex);
208		if (dev->attached && dev->driver == driver) {
209			if (dev->use_count)
210				printk
211				    (KERN_WARNING "BUG! detaching device with use_count=%d\n",
212				     dev->use_count);
213			comedi_device_detach(dev);
214		}
215		mutex_unlock(&dev->mutex);
216	}
217
218	if (comedi_drivers == driver) {
219		comedi_drivers = driver->next;
220		return 0;
221	}
222
223	for (prev = comedi_drivers; prev->next; prev = prev->next) {
224		if (prev->next == driver) {
225			prev->next = driver->next;
226			return 0;
227		}
228	}
229	return -EINVAL;
230}
231
232static int postconfig(struct comedi_device *dev)
233{
234	int i;
235	struct comedi_subdevice *s;
236	struct comedi_async *async = NULL;
237	int ret;
238
239	for (i = 0; i < dev->n_subdevices; i++) {
240		s = dev->subdevices + i;
241
242		if (s->type == COMEDI_SUBD_UNUSED)
243			continue;
244
245		if (s->len_chanlist == 0)
246			s->len_chanlist = 1;
247
248		if (s->do_cmd) {
249			BUG_ON((s->subdev_flags & (SDF_CMD_READ |
250						   SDF_CMD_WRITE)) == 0);
251			BUG_ON(!s->do_cmdtest);
252
253			async =
254			    kzalloc(sizeof(struct comedi_async), GFP_KERNEL);
255			if (async == NULL) {
256				printk(KERN_INFO "failed to allocate async struct\n");
257				return -ENOMEM;
258			}
259			init_waitqueue_head(&async->wait_head);
260			async->subdevice = s;
261			s->async = async;
262
263#define DEFAULT_BUF_MAXSIZE (64*1024)
264#define DEFAULT_BUF_SIZE (64*1024)
265
266			async->max_bufsize = DEFAULT_BUF_MAXSIZE;
267
268			async->prealloc_buf = NULL;
269			async->prealloc_bufsz = 0;
270			if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
271				printk(KERN_INFO "Buffer allocation failed\n");
272				return -ENOMEM;
273			}
274			if (s->buf_change) {
275				ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
276				if (ret < 0)
277					return ret;
278			}
279			comedi_alloc_subdevice_minor(dev, s);
280		}
281
282		if (!s->range_table && !s->range_table_list)
283			s->range_table = &range_unknown;
284
285		if (!s->insn_read && s->insn_bits)
286			s->insn_read = insn_rw_emulate_bits;
287		if (!s->insn_write && s->insn_bits)
288			s->insn_write = insn_rw_emulate_bits;
289
290		if (!s->insn_read)
291			s->insn_read = insn_inval;
292		if (!s->insn_write)
293			s->insn_write = insn_inval;
294		if (!s->insn_bits)
295			s->insn_bits = insn_inval;
296		if (!s->insn_config)
297			s->insn_config = insn_inval;
298
299		if (!s->poll)
300			s->poll = poll_invalid;
301	}
302
303	return 0;
304}
305
306/* generic recognize function for drivers
307 * that register their supported board names */
308void *comedi_recognize(struct comedi_driver *driv, const char *name)
309{
310	unsigned i;
311	const char *const *name_ptr = driv->board_name;
312	for (i = 0; i < driv->num_names; i++) {
313		if (strcmp(*name_ptr, name) == 0)
314			return (void *)name_ptr;
315		name_ptr =
316		    (const char *const *)((const char *)name_ptr +
317					  driv->offset);
318	}
319
320	return NULL;
321}
322
323void comedi_report_boards(struct comedi_driver *driv)
324{
325	unsigned int i;
326	const char *const *name_ptr;
327
328	printk(KERN_INFO "comedi: valid board names for %s driver are:\n",
329	       driv->driver_name);
330
331	name_ptr = driv->board_name;
332	for (i = 0; i < driv->num_names; i++) {
333		printk(KERN_INFO " %s\n", *name_ptr);
334		name_ptr = (const char **)((char *)name_ptr + driv->offset);
335	}
336
337	if (driv->num_names == 0)
338		printk(KERN_INFO " %s\n", driv->driver_name);
339}
340
341static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
342{
343	return -EINVAL;
344}
345
346int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
347	       struct comedi_insn *insn, unsigned int *data)
348{
349	return -EINVAL;
350}
351
352static int insn_rw_emulate_bits(struct comedi_device *dev,
353				struct comedi_subdevice *s,
354				struct comedi_insn *insn, unsigned int *data)
355{
356	struct comedi_insn new_insn;
357	int ret;
358	static const unsigned channels_per_bitfield = 32;
359
360	unsigned chan = CR_CHAN(insn->chanspec);
361	const unsigned base_bitfield_channel =
362	    (chan < channels_per_bitfield) ? 0 : chan;
363	unsigned int new_data[2];
364	memset(new_data, 0, sizeof(new_data));
365	memset(&new_insn, 0, sizeof(new_insn));
366	new_insn.insn = INSN_BITS;
367	new_insn.chanspec = base_bitfield_channel;
368	new_insn.n = 2;
369	new_insn.data = new_data;
370	new_insn.subdev = insn->subdev;
371
372	if (insn->insn == INSN_WRITE) {
373		if (!(s->subdev_flags & SDF_WRITABLE))
374			return -EINVAL;
375		new_data[0] = 1 << (chan - base_bitfield_channel);	/* mask */
376		new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) : 0;	/* bits */
377	}
378
379	ret = s->insn_bits(dev, s, &new_insn, new_data);
380	if (ret < 0)
381		return ret;
382
383	if (insn->insn == INSN_READ)
384		data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
385
386	return 1;
387}
388
389static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
390{
391	unsigned long ret = 0UL;
392	pmd_t *pmd;
393	pte_t *ptep, pte;
394	pud_t *pud;
395
396	if (!pgd_none(*pgd)) {
397		pud = pud_offset(pgd, adr);
398		pmd = pmd_offset(pud, adr);
399		if (!pmd_none(*pmd)) {
400			ptep = pte_offset_kernel(pmd, adr);
401			pte = *ptep;
402			if (pte_present(pte)) {
403				ret = (unsigned long)
404				    page_address(pte_page(pte));
405				ret |= (adr & (PAGE_SIZE - 1));
406			}
407		}
408	}
409	return ret;
410}
411
412static inline unsigned long kvirt_to_kva(unsigned long adr)
413{
414	unsigned long va, kva;
415
416	va = adr;
417	kva = uvirt_to_kva(pgd_offset_k(va), va);
418
419	return kva;
420}
421
422int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
423		     unsigned long new_size)
424{
425	struct comedi_async *async = s->async;
426
427	/* Round up new_size to multiple of PAGE_SIZE */
428	new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
429
430	/* if no change is required, do nothing */
431	if (async->prealloc_buf && async->prealloc_bufsz == new_size)
432		return 0;
433
434	/*  deallocate old buffer */
435	if (async->prealloc_buf) {
436		vunmap(async->prealloc_buf);
437		async->prealloc_buf = NULL;
438		async->prealloc_bufsz = 0;
439	}
440	if (async->buf_page_list) {
441		unsigned i;
442		for (i = 0; i < async->n_buf_pages; ++i) {
443			if (async->buf_page_list[i].virt_addr) {
444				clear_bit(PG_reserved, &(virt_to_page(async->buf_page_list[i].virt_addr)->flags));
445				if (s->async_dma_dir != DMA_NONE) {
446					dma_free_coherent(dev->hw_dev,
447							  PAGE_SIZE,
448							  async->
449							  buf_page_list
450							  [i].virt_addr,
451							  async->
452							  buf_page_list
453							  [i].dma_addr);
454				} else {
455					free_page((unsigned long)
456						  async->buf_page_list[i].
457						  virt_addr);
458				}
459			}
460		}
461		vfree(async->buf_page_list);
462		async->buf_page_list = NULL;
463		async->n_buf_pages = 0;
464	}
465	/*  allocate new buffer */
466	if (new_size) {
467		unsigned i = 0;
468		unsigned n_pages = new_size >> PAGE_SHIFT;
469		struct page **pages = NULL;
470
471		async->buf_page_list =
472		    vmalloc(sizeof(struct comedi_buf_page) * n_pages);
473		if (async->buf_page_list) {
474			memset(async->buf_page_list, 0,
475			       sizeof(struct comedi_buf_page) * n_pages);
476			pages = vmalloc(sizeof(struct page *) * n_pages);
477		}
478		if (pages) {
479			for (i = 0; i < n_pages; i++) {
480				if (s->async_dma_dir != DMA_NONE) {
481					async->buf_page_list[i].virt_addr =
482					    dma_alloc_coherent(dev->hw_dev,
483							       PAGE_SIZE,
484							       &async->
485							       buf_page_list
486							       [i].dma_addr,
487							       GFP_KERNEL |
488							       __GFP_COMP);
489				} else {
490					async->buf_page_list[i].virt_addr =
491					    (void *)
492					    get_zeroed_page(GFP_KERNEL);
493				}
494				if (async->buf_page_list[i].virt_addr == NULL)
495					break;
496
497				set_bit(PG_reserved,
498					&(virt_to_page(async->buf_page_list[i].virt_addr)->flags));
499				pages[i] = virt_to_page(async->buf_page_list[i].virt_addr);
500			}
501		}
502		if (i == n_pages) {
503			async->prealloc_buf =
504			    vmap(pages, n_pages, VM_MAP, PAGE_KERNEL_NOCACHE);
505		}
506		vfree(pages);
507
508		if (async->prealloc_buf == NULL) {
509			/* Some allocation failed above. */
510			if (async->buf_page_list) {
511				for (i = 0; i < n_pages; i++) {
512					if (async->buf_page_list[i].virt_addr ==
513					    NULL) {
514						break;
515					}
516					clear_bit(PG_reserved, &(virt_to_page(async->buf_page_list[i].virt_addr)->flags));
517					if (s->async_dma_dir != DMA_NONE) {
518						dma_free_coherent(dev->hw_dev,
519								  PAGE_SIZE,
520								  async->
521								  buf_page_list
522								  [i].virt_addr,
523								  async->
524								  buf_page_list
525								  [i].dma_addr);
526					} else {
527						free_page((unsigned long)
528							  async->buf_page_list
529							  [i].virt_addr);
530					}
531				}
532				vfree(async->buf_page_list);
533				async->buf_page_list = NULL;
534			}
535			return -ENOMEM;
536		}
537		async->n_buf_pages = n_pages;
538	}
539	async->prealloc_bufsz = new_size;
540
541	return 0;
542}
543
544/* munging is applied to data by core as it passes between user
545 * and kernel space */
546unsigned int comedi_buf_munge(struct comedi_async *async,
547			      unsigned int num_bytes)
548{
549	struct comedi_subdevice *s = async->subdevice;
550	unsigned int count = 0;
551	const unsigned num_sample_bytes = bytes_per_sample(s);
552
553	if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
554		async->munge_count += num_bytes;
555		BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
556		return num_bytes;
557	}
558	/* don't munge partial samples */
559	num_bytes -= num_bytes % num_sample_bytes;
560	while (count < num_bytes) {
561		int block_size;
562
563		block_size = num_bytes - count;
564		if (block_size < 0) {
565			printk(KERN_WARNING "%s: %s: bug! block_size is negative\n",
566			       __FILE__, __func__);
567			break;
568		}
569		if ((int)(async->munge_ptr + block_size -
570			  async->prealloc_bufsz) > 0)
571			block_size = async->prealloc_bufsz - async->munge_ptr;
572
573		s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
574			 block_size, async->munge_chan);
575
576		smp_wmb();	/* barrier insures data is munged in buffer
577				 * before munge_count is incremented */
578
579		async->munge_chan += block_size / num_sample_bytes;
580		async->munge_chan %= async->cmd.chanlist_len;
581		async->munge_count += block_size;
582		async->munge_ptr += block_size;
583		async->munge_ptr %= async->prealloc_bufsz;
584		count += block_size;
585	}
586	BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
587	return count;
588}
589
590unsigned int comedi_buf_write_n_available(struct comedi_async *async)
591{
592	unsigned int free_end;
593	unsigned int nbytes;
594
595	if (async == NULL)
596		return 0;
597
598	free_end = async->buf_read_count + async->prealloc_bufsz;
599	nbytes = free_end - async->buf_write_alloc_count;
600	nbytes -= nbytes % bytes_per_sample(async->subdevice);
601	/* barrier insures the read of buf_read_count in this
602	   query occurs before any following writes to the buffer which
603	   might be based on the return value from this query.
604	 */
605	smp_mb();
606	return nbytes;
607}
608
609/* allocates chunk for the writer from free buffer space */
610unsigned int comedi_buf_write_alloc(struct comedi_async *async,
611				    unsigned int nbytes)
612{
613	unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
614
615	if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
616		nbytes = free_end - async->buf_write_alloc_count;
617
618	async->buf_write_alloc_count += nbytes;
619	/* barrier insures the read of buf_read_count above occurs before
620	   we write data to the write-alloc'ed buffer space */
621	smp_mb();
622	return nbytes;
623}
624
625/* allocates nothing unless it can completely fulfill the request */
626unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
627					   unsigned int nbytes)
628{
629	unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
630
631	if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
632		nbytes = 0;
633
634	async->buf_write_alloc_count += nbytes;
635	/* barrier insures the read of buf_read_count above occurs before
636	   we write data to the write-alloc'ed buffer space */
637	smp_mb();
638	return nbytes;
639}
640
641/* transfers a chunk from writer to filled buffer space */
642unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes)
643{
644	if ((int)(async->buf_write_count + nbytes -
645		  async->buf_write_alloc_count) > 0) {
646		printk
647		    (KERN_INFO "comedi: attempted to write-free more bytes than have been write-allocated.\n");
648		nbytes = async->buf_write_alloc_count - async->buf_write_count;
649	}
650	async->buf_write_count += nbytes;
651	async->buf_write_ptr += nbytes;
652	comedi_buf_munge(async, async->buf_write_count - async->munge_count);
653	if (async->buf_write_ptr >= async->prealloc_bufsz)
654		async->buf_write_ptr %= async->prealloc_bufsz;
655
656	return nbytes;
657}
658
659/* allocates a chunk for the reader from filled (and munged) buffer space */
660unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes)
661{
662	if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
663	    0) {
664		nbytes = async->munge_count - async->buf_read_alloc_count;
665	}
666	async->buf_read_alloc_count += nbytes;
667	/* barrier insures read of munge_count occurs before we actually read
668	   data out of buffer */
669	smp_rmb();
670	return nbytes;
671}
672
673/* transfers control of a chunk from reader to free buffer space */
674unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes)
675{
676	/* barrier insures data has been read out of
677	 * buffer before read count is incremented */
678	smp_mb();
679	if ((int)(async->buf_read_count + nbytes -
680		  async->buf_read_alloc_count) > 0) {
681		printk
682		    ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
683		nbytes = async->buf_read_alloc_count - async->buf_read_count;
684	}
685	async->buf_read_count += nbytes;
686	async->buf_read_ptr += nbytes;
687	async->buf_read_ptr %= async->prealloc_bufsz;
688	return nbytes;
689}
690
691void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
692			  const void *data, unsigned int num_bytes)
693{
694	unsigned int write_ptr = async->buf_write_ptr + offset;
695
696	if (write_ptr >= async->prealloc_bufsz)
697		write_ptr %= async->prealloc_bufsz;
698
699	while (num_bytes) {
700		unsigned int block_size;
701
702		if (write_ptr + num_bytes > async->prealloc_bufsz)
703			block_size = async->prealloc_bufsz - write_ptr;
704		else
705			block_size = num_bytes;
706
707		memcpy(async->prealloc_buf + write_ptr, data, block_size);
708
709		data += block_size;
710		num_bytes -= block_size;
711
712		write_ptr = 0;
713	}
714}
715
716void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
717			    void *dest, unsigned int nbytes)
718{
719	void *src;
720	unsigned int read_ptr = async->buf_read_ptr + offset;
721
722	if (read_ptr >= async->prealloc_bufsz)
723		read_ptr %= async->prealloc_bufsz;
724
725	while (nbytes) {
726		unsigned int block_size;
727
728		src = async->prealloc_buf + read_ptr;
729
730		if (nbytes >= async->prealloc_bufsz - read_ptr)
731			block_size = async->prealloc_bufsz - read_ptr;
732		else
733			block_size = nbytes;
734
735		memcpy(dest, src, block_size);
736		nbytes -= block_size;
737		dest += block_size;
738		read_ptr = 0;
739	}
740}
741
742unsigned int comedi_buf_read_n_available(struct comedi_async *async)
743{
744	unsigned num_bytes;
745
746	if (async == NULL)
747		return 0;
748	num_bytes = async->munge_count - async->buf_read_count;
749	/* barrier insures the read of munge_count in this
750	   query occurs before any following reads of the buffer which
751	   might be based on the return value from this query.
752	 */
753	smp_rmb();
754	return num_bytes;
755}
756
757int comedi_buf_get(struct comedi_async *async, short *x)
758{
759	unsigned int n = comedi_buf_read_n_available(async);
760
761	if (n < sizeof(short))
762		return 0;
763	comedi_buf_read_alloc(async, sizeof(short));
764	*x = *(short *)(async->prealloc_buf + async->buf_read_ptr);
765	comedi_buf_read_free(async, sizeof(short));
766	return 1;
767}
768
769int comedi_buf_put(struct comedi_async *async, short x)
770{
771	unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short));
772
773	if (n < sizeof(short)) {
774		async->events |= COMEDI_CB_ERROR;
775		return 0;
776	}
777	*(short *)(async->prealloc_buf + async->buf_write_ptr) = x;
778	comedi_buf_write_free(async, sizeof(short));
779	return 1;
780}
781
782void comedi_reset_async_buf(struct comedi_async *async)
783{
784	async->buf_write_alloc_count = 0;
785	async->buf_write_count = 0;
786	async->buf_read_alloc_count = 0;
787	async->buf_read_count = 0;
788
789	async->buf_write_ptr = 0;
790	async->buf_read_ptr = 0;
791
792	async->cur_chan = 0;
793	async->scan_progress = 0;
794	async->munge_chan = 0;
795	async->munge_count = 0;
796	async->munge_ptr = 0;
797
798	async->events = 0;
799}
800
801int comedi_auto_config(struct device *hardware_device, const char *board_name,
802		       const int *options, unsigned num_options)
803{
804	struct comedi_devconfig it;
805	int minor;
806	struct comedi_device_file_info *dev_file_info;
807	int retval;
808	unsigned *private_data = NULL;
809
810	if (!comedi_autoconfig) {
811		dev_set_drvdata(hardware_device, NULL);
812		return 0;
813	}
814
815	minor = comedi_alloc_board_minor(hardware_device);
816	if (minor < 0)
817		return minor;
818
819	private_data = kmalloc(sizeof(unsigned), GFP_KERNEL);
820	if (private_data == NULL) {
821		retval = -ENOMEM;
822		goto cleanup;
823	}
824	*private_data = minor;
825	dev_set_drvdata(hardware_device, private_data);
826
827	dev_file_info = comedi_get_device_file_info(minor);
828
829	memset(&it, 0, sizeof(it));
830	strncpy(it.board_name, board_name, COMEDI_NAMELEN);
831	it.board_name[COMEDI_NAMELEN - 1] = '\0';
832	BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
833	memcpy(it.options, options, num_options * sizeof(int));
834
835	mutex_lock(&dev_file_info->device->mutex);
836	retval = comedi_device_attach(dev_file_info->device, &it);
837	mutex_unlock(&dev_file_info->device->mutex);
838
839cleanup:
840	if (retval < 0) {
841		kfree(private_data);
842		comedi_free_board_minor(minor);
843	}
844	return retval;
845}
846
847void comedi_auto_unconfig(struct device *hardware_device)
848{
849	unsigned *minor = (unsigned *)dev_get_drvdata(hardware_device);
850	if (minor == NULL)
851		return;
852
853	BUG_ON(*minor >= COMEDI_NUM_BOARD_MINORS);
854
855	comedi_free_board_minor(*minor);
856	dev_set_drvdata(hardware_device, NULL);
857	kfree(minor);
858}
859
860int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
861{
862	int options[2];
863
864	/*  pci bus */
865	options[0] = pcidev->bus->number;
866	/*  pci slot */
867	options[1] = PCI_SLOT(pcidev->devfn);
868
869	return comedi_auto_config(&pcidev->dev, board_name,
870				  options, ARRAY_SIZE(options));
871}
872
873void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
874{
875	comedi_auto_unconfig(&pcidev->dev);
876}
877
878int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name)
879{
880	BUG_ON(usbdev == NULL);
881	return comedi_auto_config(&usbdev->dev, board_name, NULL, 0);
882}
883
884void comedi_usb_auto_unconfig(struct usb_device *usbdev)
885{
886	BUG_ON(usbdev == NULL);
887	comedi_auto_unconfig(&usbdev->dev);
888}
889