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