drivers.c revision 4e2f002f7a9b316640eb06ef6df767f017e3e7b1
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
109/* do a little post-config cleanup */
110/* called with module refcount incremented, decrements it */
111static int comedi_device_postconfig(struct comedi_device *dev)
112{
113	int ret = postconfig(dev);
114	module_put(dev->driver->module);
115	if (ret < 0) {
116		__comedi_device_detach(dev);
117		return ret;
118	}
119	if (!dev->board_name) {
120		printk(KERN_WARNING "BUG: dev->board_name=<%p>\n",
121		       dev->board_name);
122		dev->board_name = "BUG";
123	}
124	smp_wmb();
125	dev->attached = 1;
126	return 0;
127}
128
129int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
130{
131	struct comedi_driver *driv;
132	int ret;
133
134	if (dev->attached)
135		return -EBUSY;
136
137	for (driv = comedi_drivers; driv; driv = driv->next) {
138		if (!try_module_get(driv->module)) {
139			printk(KERN_INFO "comedi: failed to increment module count, skipping\n");
140			continue;
141		}
142		if (driv->num_names) {
143			dev->board_ptr = comedi_recognize(driv, it->board_name);
144			if (dev->board_ptr)
145				break;
146		} else if (strcmp(driv->driver_name, it->board_name))
147			break;
148		module_put(driv->module);
149	}
150	if (driv == NULL) {
151		/*  recognize has failed if we get here */
152		/*  report valid board names before returning error */
153		for (driv = comedi_drivers; driv; driv = driv->next) {
154			if (!try_module_get(driv->module)) {
155				printk(KERN_INFO
156				       "comedi: failed to increment module count\n");
157				continue;
158			}
159			comedi_report_boards(driv);
160			module_put(driv->module);
161		}
162		return -EIO;
163	}
164	/* initialize dev->driver here so
165	 * comedi_error() can be called from attach */
166	dev->driver = driv;
167	ret = driv->attach(dev, it);
168	if (ret < 0) {
169		module_put(dev->driver->module);
170		__comedi_device_detach(dev);
171		return ret;
172	}
173	return comedi_device_postconfig(dev);
174}
175
176int comedi_driver_register(struct comedi_driver *driver)
177{
178	driver->next = comedi_drivers;
179	comedi_drivers = driver;
180
181	return 0;
182}
183EXPORT_SYMBOL(comedi_driver_register);
184
185int comedi_driver_unregister(struct comedi_driver *driver)
186{
187	struct comedi_driver *prev;
188	int i;
189
190	/* check for devices using this driver */
191	for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
192		struct comedi_device_file_info *dev_file_info =
193		    comedi_get_device_file_info(i);
194		struct comedi_device *dev;
195
196		if (dev_file_info == NULL)
197			continue;
198		dev = dev_file_info->device;
199
200		mutex_lock(&dev->mutex);
201		if (dev->attached && dev->driver == driver) {
202			if (dev->use_count)
203				printk(KERN_WARNING "BUG! detaching device with use_count=%d\n",
204						dev->use_count);
205			comedi_device_detach(dev);
206		}
207		mutex_unlock(&dev->mutex);
208	}
209
210	if (comedi_drivers == driver) {
211		comedi_drivers = driver->next;
212		return 0;
213	}
214
215	for (prev = comedi_drivers; prev->next; prev = prev->next) {
216		if (prev->next == driver) {
217			prev->next = driver->next;
218			return 0;
219		}
220	}
221	return -EINVAL;
222}
223EXPORT_SYMBOL(comedi_driver_unregister);
224
225static int postconfig(struct comedi_device *dev)
226{
227	int i;
228	struct comedi_subdevice *s;
229	struct comedi_async *async = NULL;
230	int ret;
231
232	for (i = 0; i < dev->n_subdevices; i++) {
233		s = dev->subdevices + i;
234
235		if (s->type == COMEDI_SUBD_UNUSED)
236			continue;
237
238		if (s->len_chanlist == 0)
239			s->len_chanlist = 1;
240
241		if (s->do_cmd) {
242			unsigned int buf_size;
243
244			BUG_ON((s->subdev_flags & (SDF_CMD_READ |
245						   SDF_CMD_WRITE)) == 0);
246			BUG_ON(!s->do_cmdtest);
247
248			async =
249			    kzalloc(sizeof(struct comedi_async), GFP_KERNEL);
250			if (async == NULL) {
251				printk(KERN_INFO
252				       "failed to allocate async struct\n");
253				return -ENOMEM;
254			}
255			init_waitqueue_head(&async->wait_head);
256			async->subdevice = s;
257			s->async = async;
258
259			async->max_bufsize =
260				comedi_default_buf_maxsize_kb * 1024;
261			buf_size = comedi_default_buf_size_kb * 1024;
262			if (buf_size > async->max_bufsize)
263				buf_size = async->max_bufsize;
264
265			async->prealloc_buf = NULL;
266			async->prealloc_bufsz = 0;
267			if (comedi_buf_alloc(dev, s, 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, 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/*
304 * Generic recognize function for drivers that register their supported
305 * board names.
306 *
307 * 'driv->board_name' points to a 'const char *' member within the
308 * zeroth element of an array of some private board information
309 * structure, say 'struct foo_board' containing a member 'const char
310 * *board_name' that is initialized to point to a board name string that
311 * is one of the candidates matched against this function's 'name'
312 * parameter.
313 *
314 * 'driv->offset' is the size of the private board information
315 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
316 * the length of the array of private board information structures.
317 *
318 * If one of the board names in the array of private board information
319 * structures matches the name supplied to this function, the function
320 * returns a pointer to the pointer to the board name, otherwise it
321 * returns NULL.  The return value ends up in the 'board_ptr' member of
322 * a 'struct comedi_device' that the low-level comedi driver's
323 * 'attach()' hook can convert to a point to a particular element of its
324 * array of private board information structures by subtracting the
325 * offset of the member that points to the board name.  (No subtraction
326 * is required if the board name pointer is the first member of the
327 * private board information structure, which is generally the case.)
328 */
329static void *comedi_recognize(struct comedi_driver *driv, const char *name)
330{
331	char **name_ptr = (char **)driv->board_name;
332	int i;
333
334	for (i = 0; i < driv->num_names; i++) {
335		if (strcmp(*name_ptr, name) == 0)
336			return name_ptr;
337		name_ptr = (void *)name_ptr + driv->offset;
338	}
339
340	return NULL;
341}
342
343static void comedi_report_boards(struct comedi_driver *driv)
344{
345	unsigned int i;
346	const char *const *name_ptr;
347
348	printk(KERN_INFO "comedi: valid board names for %s driver are:\n",
349	       driv->driver_name);
350
351	name_ptr = driv->board_name;
352	for (i = 0; i < driv->num_names; i++) {
353		printk(KERN_INFO " %s\n", *name_ptr);
354		name_ptr = (const char **)((char *)name_ptr + driv->offset);
355	}
356
357	if (driv->num_names == 0)
358		printk(KERN_INFO " %s\n", driv->driver_name);
359}
360
361static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
362{
363	return -EINVAL;
364}
365
366int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
367	       struct comedi_insn *insn, unsigned int *data)
368{
369	return -EINVAL;
370}
371
372static int insn_rw_emulate_bits(struct comedi_device *dev,
373				struct comedi_subdevice *s,
374				struct comedi_insn *insn, unsigned int *data)
375{
376	struct comedi_insn new_insn;
377	int ret;
378	static const unsigned channels_per_bitfield = 32;
379
380	unsigned chan = CR_CHAN(insn->chanspec);
381	const unsigned base_bitfield_channel =
382	    (chan < channels_per_bitfield) ? 0 : chan;
383	unsigned int new_data[2];
384	memset(new_data, 0, sizeof(new_data));
385	memset(&new_insn, 0, sizeof(new_insn));
386	new_insn.insn = INSN_BITS;
387	new_insn.chanspec = base_bitfield_channel;
388	new_insn.n = 2;
389	new_insn.data = new_data;
390	new_insn.subdev = insn->subdev;
391
392	if (insn->insn == INSN_WRITE) {
393		if (!(s->subdev_flags & SDF_WRITABLE))
394			return -EINVAL;
395		new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
396		new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
397			      : 0; /* bits */
398	}
399
400	ret = s->insn_bits(dev, s, &new_insn, new_data);
401	if (ret < 0)
402		return ret;
403
404	if (insn->insn == INSN_READ)
405		data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
406
407	return 1;
408}
409
410int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
411		     unsigned long new_size)
412{
413	struct comedi_async *async = s->async;
414
415	/* Round up new_size to multiple of PAGE_SIZE */
416	new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
417
418	/* if no change is required, do nothing */
419	if (async->prealloc_buf && async->prealloc_bufsz == new_size)
420		return 0;
421
422	/*  deallocate old buffer */
423	if (async->prealloc_buf) {
424		vunmap(async->prealloc_buf);
425		async->prealloc_buf = NULL;
426		async->prealloc_bufsz = 0;
427	}
428	if (async->buf_page_list) {
429		unsigned i;
430		for (i = 0; i < async->n_buf_pages; ++i) {
431			if (async->buf_page_list[i].virt_addr) {
432				clear_bit(PG_reserved,
433					&(virt_to_page(async->buf_page_list[i].
434							virt_addr)->flags));
435				if (s->async_dma_dir != DMA_NONE) {
436					dma_free_coherent(dev->hw_dev,
437							  PAGE_SIZE,
438							  async->
439							  buf_page_list
440							  [i].virt_addr,
441							  async->
442							  buf_page_list
443							  [i].dma_addr);
444				} else {
445					free_page((unsigned long)
446						  async->buf_page_list[i].
447						  virt_addr);
448				}
449			}
450		}
451		vfree(async->buf_page_list);
452		async->buf_page_list = NULL;
453		async->n_buf_pages = 0;
454	}
455	/*  allocate new buffer */
456	if (new_size) {
457		unsigned i = 0;
458		unsigned n_pages = new_size >> PAGE_SHIFT;
459		struct page **pages = NULL;
460
461		async->buf_page_list =
462		    vzalloc(sizeof(struct comedi_buf_page) * n_pages);
463		if (async->buf_page_list)
464			pages = vmalloc(sizeof(struct page *) * n_pages);
465
466		if (pages) {
467			for (i = 0; i < n_pages; i++) {
468				if (s->async_dma_dir != DMA_NONE) {
469					async->buf_page_list[i].virt_addr =
470					    dma_alloc_coherent(dev->hw_dev,
471							       PAGE_SIZE,
472							       &async->
473							       buf_page_list
474							       [i].dma_addr,
475							       GFP_KERNEL |
476							       __GFP_COMP);
477				} else {
478					async->buf_page_list[i].virt_addr =
479					    (void *)
480					    get_zeroed_page(GFP_KERNEL);
481				}
482				if (async->buf_page_list[i].virt_addr == NULL)
483					break;
484
485				set_bit(PG_reserved,
486					&(virt_to_page(async->buf_page_list[i].
487							virt_addr)->flags));
488				pages[i] = virt_to_page(async->buf_page_list[i].
489								virt_addr);
490			}
491		}
492		if (i == n_pages) {
493			async->prealloc_buf =
494#ifdef PAGE_KERNEL_NOCACHE
495			    vmap(pages, n_pages, VM_MAP, PAGE_KERNEL_NOCACHE);
496#else
497			    vmap(pages, n_pages, VM_MAP, PAGE_KERNEL);
498#endif
499		}
500		vfree(pages);
501
502		if (async->prealloc_buf == NULL) {
503			/* Some allocation failed above. */
504			if (async->buf_page_list) {
505				for (i = 0; i < n_pages; i++) {
506					if (async->buf_page_list[i].virt_addr ==
507					    NULL) {
508						break;
509					}
510					clear_bit(PG_reserved,
511						&(virt_to_page(async->
512							buf_page_list[i].
513							virt_addr)->flags));
514					if (s->async_dma_dir != DMA_NONE) {
515						dma_free_coherent(dev->hw_dev,
516								  PAGE_SIZE,
517								  async->
518								  buf_page_list
519								  [i].virt_addr,
520								  async->
521								  buf_page_list
522								  [i].dma_addr);
523					} else {
524						free_page((unsigned long)
525							  async->buf_page_list
526							  [i].virt_addr);
527					}
528				}
529				vfree(async->buf_page_list);
530				async->buf_page_list = NULL;
531			}
532			return -ENOMEM;
533		}
534		async->n_buf_pages = n_pages;
535	}
536	async->prealloc_bufsz = new_size;
537
538	return 0;
539}
540
541/* munging is applied to data by core as it passes between user
542 * and kernel space */
543static unsigned int comedi_buf_munge(struct comedi_async *async,
544				     unsigned int num_bytes)
545{
546	struct comedi_subdevice *s = async->subdevice;
547	unsigned int count = 0;
548	const unsigned num_sample_bytes = bytes_per_sample(s);
549
550	if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
551		async->munge_count += num_bytes;
552		BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
553		return num_bytes;
554	}
555	/* don't munge partial samples */
556	num_bytes -= num_bytes % num_sample_bytes;
557	while (count < num_bytes) {
558		int block_size;
559
560		block_size = num_bytes - count;
561		if (block_size < 0) {
562			printk(KERN_WARNING
563			       "%s: %s: bug! block_size is negative\n",
564			       __FILE__, __func__);
565			break;
566		}
567		if ((int)(async->munge_ptr + block_size -
568			  async->prealloc_bufsz) > 0)
569			block_size = async->prealloc_bufsz - async->munge_ptr;
570
571		s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
572			 block_size, async->munge_chan);
573
574		smp_wmb();	/* barrier insures data is munged in buffer
575				 * before munge_count is incremented */
576
577		async->munge_chan += block_size / num_sample_bytes;
578		async->munge_chan %= async->cmd.chanlist_len;
579		async->munge_count += block_size;
580		async->munge_ptr += block_size;
581		async->munge_ptr %= async->prealloc_bufsz;
582		count += block_size;
583	}
584	BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
585	return count;
586}
587
588unsigned int comedi_buf_write_n_available(struct comedi_async *async)
589{
590	unsigned int free_end;
591	unsigned int nbytes;
592
593	if (async == NULL)
594		return 0;
595
596	free_end = async->buf_read_count + async->prealloc_bufsz;
597	nbytes = free_end - async->buf_write_alloc_count;
598	nbytes -= nbytes % bytes_per_sample(async->subdevice);
599	/* barrier insures the read of buf_read_count in this
600	   query occurs before any following writes to the buffer which
601	   might be based on the return value from this query.
602	 */
603	smp_mb();
604	return nbytes;
605}
606
607/* allocates chunk for the writer from free buffer space */
608unsigned int comedi_buf_write_alloc(struct comedi_async *async,
609				    unsigned int nbytes)
610{
611	unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
612
613	if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
614		nbytes = free_end - async->buf_write_alloc_count;
615
616	async->buf_write_alloc_count += nbytes;
617	/* barrier insures the read of buf_read_count above occurs before
618	   we write data to the write-alloc'ed buffer space */
619	smp_mb();
620	return nbytes;
621}
622EXPORT_SYMBOL(comedi_buf_write_alloc);
623
624/* allocates nothing unless it can completely fulfill the request */
625unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
626					   unsigned int nbytes)
627{
628	unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
629
630	if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
631		nbytes = 0;
632
633	async->buf_write_alloc_count += nbytes;
634	/* barrier insures the read of buf_read_count above occurs before
635	   we write data to the write-alloc'ed buffer space */
636	smp_mb();
637	return nbytes;
638}
639
640/* transfers a chunk from writer to filled buffer space */
641unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes)
642{
643	if ((int)(async->buf_write_count + nbytes -
644		  async->buf_write_alloc_count) > 0) {
645		printk(KERN_INFO "comedi: attempted to write-free more bytes than have been write-allocated.\n");
646		nbytes = async->buf_write_alloc_count - async->buf_write_count;
647	}
648	async->buf_write_count += nbytes;
649	async->buf_write_ptr += nbytes;
650	comedi_buf_munge(async, async->buf_write_count - async->munge_count);
651	if (async->buf_write_ptr >= async->prealloc_bufsz)
652		async->buf_write_ptr %= async->prealloc_bufsz;
653
654	return nbytes;
655}
656EXPORT_SYMBOL(comedi_buf_write_free);
657
658/* allocates a chunk for the reader from filled (and munged) buffer space */
659unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes)
660{
661	if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
662	    0) {
663		nbytes = async->munge_count - async->buf_read_alloc_count;
664	}
665	async->buf_read_alloc_count += nbytes;
666	/* barrier insures read of munge_count occurs before we actually read
667	   data out of buffer */
668	smp_rmb();
669	return nbytes;
670}
671EXPORT_SYMBOL(comedi_buf_read_alloc);
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(KERN_INFO
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}
690EXPORT_SYMBOL(comedi_buf_read_free);
691
692void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
693			  const void *data, unsigned int num_bytes)
694{
695	unsigned int write_ptr = async->buf_write_ptr + offset;
696
697	if (write_ptr >= async->prealloc_bufsz)
698		write_ptr %= async->prealloc_bufsz;
699
700	while (num_bytes) {
701		unsigned int block_size;
702
703		if (write_ptr + num_bytes > async->prealloc_bufsz)
704			block_size = async->prealloc_bufsz - write_ptr;
705		else
706			block_size = num_bytes;
707
708		memcpy(async->prealloc_buf + write_ptr, data, block_size);
709
710		data += block_size;
711		num_bytes -= block_size;
712
713		write_ptr = 0;
714	}
715}
716EXPORT_SYMBOL(comedi_buf_memcpy_to);
717
718void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
719			    void *dest, unsigned int nbytes)
720{
721	void *src;
722	unsigned int read_ptr = async->buf_read_ptr + offset;
723
724	if (read_ptr >= async->prealloc_bufsz)
725		read_ptr %= async->prealloc_bufsz;
726
727	while (nbytes) {
728		unsigned int block_size;
729
730		src = async->prealloc_buf + read_ptr;
731
732		if (nbytes >= async->prealloc_bufsz - read_ptr)
733			block_size = async->prealloc_bufsz - read_ptr;
734		else
735			block_size = nbytes;
736
737		memcpy(dest, src, block_size);
738		nbytes -= block_size;
739		dest += block_size;
740		read_ptr = 0;
741	}
742}
743EXPORT_SYMBOL(comedi_buf_memcpy_from);
744
745unsigned int comedi_buf_read_n_available(struct comedi_async *async)
746{
747	unsigned num_bytes;
748
749	if (async == NULL)
750		return 0;
751	num_bytes = async->munge_count - async->buf_read_count;
752	/* barrier insures the read of munge_count in this
753	   query occurs before any following reads of the buffer which
754	   might be based on the return value from this query.
755	 */
756	smp_rmb();
757	return num_bytes;
758}
759EXPORT_SYMBOL(comedi_buf_read_n_available);
760
761int comedi_buf_get(struct comedi_async *async, short *x)
762{
763	unsigned int n = comedi_buf_read_n_available(async);
764
765	if (n < sizeof(short))
766		return 0;
767	comedi_buf_read_alloc(async, sizeof(short));
768	*x = *(short *)(async->prealloc_buf + async->buf_read_ptr);
769	comedi_buf_read_free(async, sizeof(short));
770	return 1;
771}
772EXPORT_SYMBOL(comedi_buf_get);
773
774int comedi_buf_put(struct comedi_async *async, short x)
775{
776	unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short));
777
778	if (n < sizeof(short)) {
779		async->events |= COMEDI_CB_ERROR;
780		return 0;
781	}
782	*(short *)(async->prealloc_buf + async->buf_write_ptr) = x;
783	comedi_buf_write_free(async, sizeof(short));
784	return 1;
785}
786EXPORT_SYMBOL(comedi_buf_put);
787
788void comedi_reset_async_buf(struct comedi_async *async)
789{
790	async->buf_write_alloc_count = 0;
791	async->buf_write_count = 0;
792	async->buf_read_alloc_count = 0;
793	async->buf_read_count = 0;
794
795	async->buf_write_ptr = 0;
796	async->buf_read_ptr = 0;
797
798	async->cur_chan = 0;
799	async->scan_progress = 0;
800	async->munge_chan = 0;
801	async->munge_count = 0;
802	async->munge_ptr = 0;
803
804	async->events = 0;
805}
806
807static int
808comedi_auto_config_helper(struct device *hardware_device,
809			  struct comedi_driver *driver,
810			  int (*attach_wrapper) (struct comedi_device *,
811						 void *), void *context)
812{
813	int minor;
814	struct comedi_device_file_info *dev_file_info;
815	struct comedi_device *comedi_dev;
816	int ret;
817
818	if (!comedi_autoconfig)
819		return 0;
820
821	minor = comedi_alloc_board_minor(hardware_device);
822	if (minor < 0)
823		return minor;
824
825	dev_file_info = comedi_get_device_file_info(minor);
826	comedi_dev = dev_file_info->device;
827
828	mutex_lock(&comedi_dev->mutex);
829	if (comedi_dev->attached)
830		ret = -EBUSY;
831	else if (!try_module_get(driver->module)) {
832		printk(KERN_INFO "comedi: failed to increment module count\n");
833		ret = -EIO;
834	} else {
835		/* set comedi_dev->driver here for attach wrapper */
836		comedi_dev->driver = driver;
837		ret = (*attach_wrapper)(comedi_dev, context);
838		if (ret < 0) {
839			module_put(driver->module);
840			__comedi_device_detach(comedi_dev);
841		} else {
842			ret = comedi_device_postconfig(comedi_dev);
843		}
844	}
845	mutex_unlock(&comedi_dev->mutex);
846
847	if (ret < 0)
848		comedi_free_board_minor(minor);
849	return ret;
850}
851
852static int comedi_auto_config_wrapper(struct comedi_device *dev, void *context)
853{
854	struct comedi_devconfig *it = context;
855	struct comedi_driver *driv = dev->driver;
856
857	if (driv->num_names) {
858		/* look for generic board entry matching driver name, which
859		 * has already been copied to it->board_name */
860		dev->board_ptr = comedi_recognize(driv, it->board_name);
861		if (dev->board_ptr == NULL) {
862			printk(KERN_WARNING
863			       "comedi: auto config failed to find board entry"
864			       " '%s' for driver '%s'\n", it->board_name,
865			       driv->driver_name);
866			comedi_report_boards(driv);
867			return -EINVAL;
868		}
869	}
870	return driv->attach(dev, it);
871}
872
873static int comedi_auto_config(struct device *hardware_device,
874			      struct comedi_driver *driver, const int *options,
875			      unsigned num_options)
876{
877	struct comedi_devconfig it;
878
879	memset(&it, 0, sizeof(it));
880	strncpy(it.board_name, driver->driver_name, COMEDI_NAMELEN);
881	it.board_name[COMEDI_NAMELEN - 1] = '\0';
882	BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
883	memcpy(it.options, options, num_options * sizeof(int));
884	return comedi_auto_config_helper(hardware_device, driver,
885					 comedi_auto_config_wrapper, &it);
886}
887
888static void comedi_auto_unconfig(struct device *hardware_device)
889{
890	int minor;
891
892	if (hardware_device == NULL)
893		return;
894	minor = comedi_find_board_minor(hardware_device);
895	if (minor < 0)
896		return;
897	BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
898	comedi_free_board_minor(minor);
899}
900
901/**
902 * comedi_pci_enable() - Enable the PCI device and request the regions.
903 * @pdev: pci_dev struct
904 * @res_name: name for the requested reqource
905 */
906int comedi_pci_enable(struct pci_dev *pdev, const char *res_name)
907{
908	int rc;
909
910	rc = pci_enable_device(pdev);
911	if (rc < 0)
912		return rc;
913
914	rc = pci_request_regions(pdev, res_name);
915	if (rc < 0)
916		pci_disable_device(pdev);
917
918	return rc;
919}
920EXPORT_SYMBOL_GPL(comedi_pci_enable);
921
922/**
923 * comedi_pci_disable() - Release the regions and disable the PCI device.
924 * @pdev: pci_dev struct
925 *
926 * This must be matched with a previous successful call to comedi_pci_enable().
927 */
928void comedi_pci_disable(struct pci_dev *pdev)
929{
930	pci_release_regions(pdev);
931	pci_disable_device(pdev);
932}
933EXPORT_SYMBOL_GPL(comedi_pci_disable);
934
935static int comedi_old_pci_auto_config(struct pci_dev *pcidev,
936				      struct comedi_driver *driver)
937{
938	int options[2];
939
940	/*  pci bus */
941	options[0] = pcidev->bus->number;
942	/*  pci slot */
943	options[1] = PCI_SLOT(pcidev->devfn);
944
945	return comedi_auto_config(&pcidev->dev, driver,
946				  options, ARRAY_SIZE(options));
947}
948
949static int comedi_pci_attach_wrapper(struct comedi_device *dev, void *pcidev)
950{
951	return dev->driver->attach_pci(dev, pcidev);
952}
953
954static int comedi_new_pci_auto_config(struct pci_dev *pcidev,
955				      struct comedi_driver *driver)
956{
957	return comedi_auto_config_helper(&pcidev->dev, driver,
958					 comedi_pci_attach_wrapper, pcidev);
959}
960
961int comedi_pci_auto_config(struct pci_dev *pcidev, struct comedi_driver *driver)
962{
963
964	if (driver->attach_pci)
965		return comedi_new_pci_auto_config(pcidev, driver);
966	else
967		return comedi_old_pci_auto_config(pcidev, driver);
968}
969EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
970
971void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
972{
973	comedi_auto_unconfig(&pcidev->dev);
974}
975EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
976
977int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
978		struct pci_driver *pci_driver)
979{
980	int ret;
981
982	ret = comedi_driver_register(comedi_driver);
983	if (ret < 0)
984		return ret;
985
986	/* FIXME: Remove this test after auditing all comedi pci drivers */
987	if (!pci_driver->name)
988		pci_driver->name = comedi_driver->driver_name;
989
990	ret = pci_register_driver(pci_driver);
991	if (ret < 0) {
992		comedi_driver_unregister(comedi_driver);
993		return ret;
994	}
995
996	return 0;
997}
998EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
999
1000void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
1001		struct pci_driver *pci_driver)
1002{
1003	pci_unregister_driver(pci_driver);
1004	comedi_driver_unregister(comedi_driver);
1005}
1006EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);
1007
1008static int comedi_old_usb_auto_config(struct usb_interface *intf,
1009				      struct comedi_driver *driver)
1010{
1011	return comedi_auto_config(&intf->dev, driver, NULL, 0);
1012}
1013
1014static int comedi_usb_attach_wrapper(struct comedi_device *dev, void *intf)
1015{
1016	return dev->driver->attach_usb(dev, intf);
1017}
1018
1019static int comedi_new_usb_auto_config(struct usb_interface *intf,
1020				      struct comedi_driver *driver)
1021{
1022	return comedi_auto_config_helper(&intf->dev, driver,
1023					 comedi_usb_attach_wrapper, intf);
1024}
1025
1026int comedi_usb_auto_config(struct usb_interface *intf,
1027			   struct comedi_driver *driver)
1028{
1029	BUG_ON(intf == NULL);
1030	if (driver->attach_usb)
1031		return comedi_new_usb_auto_config(intf, driver);
1032	else
1033		return comedi_old_usb_auto_config(intf, driver);
1034}
1035EXPORT_SYMBOL_GPL(comedi_usb_auto_config);
1036
1037void comedi_usb_auto_unconfig(struct usb_interface *intf)
1038{
1039	BUG_ON(intf == NULL);
1040	comedi_auto_unconfig(&intf->dev);
1041}
1042EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);
1043
1044int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
1045		struct usb_driver *usb_driver)
1046{
1047	int ret;
1048
1049	ret = comedi_driver_register(comedi_driver);
1050	if (ret < 0)
1051		return ret;
1052
1053	ret = usb_register(usb_driver);
1054	if (ret < 0) {
1055		comedi_driver_unregister(comedi_driver);
1056		return ret;
1057	}
1058
1059	return 0;
1060}
1061EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
1062
1063void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
1064		struct usb_driver *usb_driver)
1065{
1066	usb_deregister(usb_driver);
1067	comedi_driver_unregister(comedi_driver);
1068}
1069EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);
1070