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