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