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