1
2
3
4
5
6
7
8
9
10
11#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/ioport.h>
16#include <linux/slab.h>
17#include <linux/dma-direction.h>
18#include <linux/interrupt.h>
19#include <linux/firmware.h>
20
21#include "comedidev.h"
22#include "comedi_internal.h"
23
24struct comedi_driver *comedi_drivers;
25
26DEFINE_MUTEX(comedi_drivers_list_lock);
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
51{
52 if (hw_dev == dev->hw_dev)
53 return 0;
54 if (dev->hw_dev)
55 return -EEXIST;
56 dev->hw_dev = get_device(hw_dev);
57 return 0;
58}
59EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
60
61static void comedi_clear_hw_dev(struct comedi_device *dev)
62{
63 put_device(dev->hw_dev);
64 dev->hw_dev = NULL;
65}
66
67
68
69
70
71
72
73
74
75
76
77
78void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
79{
80 dev->private = kzalloc(size, GFP_KERNEL);
81 return dev->private;
82}
83EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
84
85
86
87
88
89
90
91
92
93
94
95
96
97int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
98{
99 struct comedi_subdevice *s;
100 int i;
101
102 if (num_subdevices < 1)
103 return -EINVAL;
104
105 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
106 if (!s)
107 return -ENOMEM;
108 dev->subdevices = s;
109 dev->n_subdevices = num_subdevices;
110
111 for (i = 0; i < num_subdevices; ++i) {
112 s = &dev->subdevices[i];
113 s->device = dev;
114 s->index = i;
115 s->async_dma_dir = DMA_NONE;
116 spin_lock_init(&s->spin_lock);
117 s->minor = -1;
118 }
119 return 0;
120}
121EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141int comedi_alloc_subdev_readback(struct comedi_subdevice *s)
142{
143 if (!s->n_chan)
144 return -EINVAL;
145
146 s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL);
147 if (!s->readback)
148 return -ENOMEM;
149
150 if (!s->insn_read)
151 s->insn_read = comedi_readback_insn_read;
152
153 return 0;
154}
155EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
156
157static void comedi_device_detach_cleanup(struct comedi_device *dev)
158{
159 int i;
160 struct comedi_subdevice *s;
161
162 if (dev->subdevices) {
163 for (i = 0; i < dev->n_subdevices; i++) {
164 s = &dev->subdevices[i];
165 if (comedi_can_auto_free_spriv(s))
166 kfree(s->private);
167 comedi_free_subdevice_minor(s);
168 if (s->async) {
169 comedi_buf_alloc(dev, s, 0);
170 kfree(s->async);
171 }
172 kfree(s->readback);
173 }
174 kfree(dev->subdevices);
175 dev->subdevices = NULL;
176 dev->n_subdevices = 0;
177 }
178 kfree(dev->private);
179 kfree(dev->pacer);
180 dev->private = NULL;
181 dev->pacer = NULL;
182 dev->driver = NULL;
183 dev->board_name = NULL;
184 dev->board_ptr = NULL;
185 dev->mmio = NULL;
186 dev->iobase = 0;
187 dev->iolen = 0;
188 dev->ioenabled = false;
189 dev->irq = 0;
190 dev->read_subdev = NULL;
191 dev->write_subdev = NULL;
192 dev->open = NULL;
193 dev->close = NULL;
194 comedi_clear_hw_dev(dev);
195}
196
197void comedi_device_detach(struct comedi_device *dev)
198{
199 comedi_device_cancel_all(dev);
200 down_write(&dev->attach_lock);
201 dev->attached = false;
202 dev->detach_count++;
203 if (dev->driver)
204 dev->driver->detach(dev);
205 comedi_device_detach_cleanup(dev);
206 up_write(&dev->attach_lock);
207}
208
209static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
210{
211 return -EINVAL;
212}
213
214static int insn_device_inval(struct comedi_device *dev,
215 struct comedi_insn *insn, unsigned int *data)
216{
217 return -EINVAL;
218}
219
220static unsigned int get_zero_valid_routes(struct comedi_device *dev,
221 unsigned int n_pairs,
222 unsigned int *pair_data)
223{
224 return 0;
225}
226
227int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
228 struct comedi_insn *insn, unsigned int *data)
229{
230 return -EINVAL;
231}
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250int comedi_readback_insn_read(struct comedi_device *dev,
251 struct comedi_subdevice *s,
252 struct comedi_insn *insn,
253 unsigned int *data)
254{
255 unsigned int chan = CR_CHAN(insn->chanspec);
256 int i;
257
258 if (!s->readback)
259 return -EINVAL;
260
261 for (i = 0; i < insn->n; i++)
262 data[i] = s->readback[chan];
263
264 return insn->n;
265}
266EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285int comedi_timeout(struct comedi_device *dev,
286 struct comedi_subdevice *s,
287 struct comedi_insn *insn,
288 int (*cb)(struct comedi_device *dev,
289 struct comedi_subdevice *s,
290 struct comedi_insn *insn,
291 unsigned long context),
292 unsigned long context)
293{
294 unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS);
295 int ret;
296
297 while (time_before(jiffies, timeout)) {
298 ret = cb(dev, s, insn, context);
299 if (ret != -EBUSY)
300 return ret;
301 cpu_relax();
302 }
303 return -ETIMEDOUT;
304}
305EXPORT_SYMBOL_GPL(comedi_timeout);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333int comedi_dio_insn_config(struct comedi_device *dev,
334 struct comedi_subdevice *s,
335 struct comedi_insn *insn,
336 unsigned int *data,
337 unsigned int mask)
338{
339 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
340
341 if (!mask)
342 mask = chan_mask;
343
344 switch (data[0]) {
345 case INSN_CONFIG_DIO_INPUT:
346 s->io_bits &= ~mask;
347 break;
348
349 case INSN_CONFIG_DIO_OUTPUT:
350 s->io_bits |= mask;
351 break;
352
353 case INSN_CONFIG_DIO_QUERY:
354 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
355 return insn->n;
356
357 default:
358 return -EINVAL;
359 }
360
361 return 0;
362}
363EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
380 unsigned int *data)
381{
382 unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
383 : 0xffffffff;
384 unsigned int mask = data[0] & chanmask;
385 unsigned int bits = data[1];
386
387 if (mask) {
388 s->state &= ~mask;
389 s->state |= (bits & mask);
390 }
391
392 return mask;
393}
394EXPORT_SYMBOL_GPL(comedi_dio_update_state);
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
412{
413 struct comedi_cmd *cmd = &s->async->cmd;
414 unsigned int num_samples;
415 unsigned int bits_per_sample;
416
417 switch (s->type) {
418 case COMEDI_SUBD_DI:
419 case COMEDI_SUBD_DO:
420 case COMEDI_SUBD_DIO:
421 bits_per_sample = 8 * comedi_bytes_per_sample(s);
422 num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample);
423 break;
424 default:
425 num_samples = cmd->scan_end_arg;
426 break;
427 }
428 return comedi_samples_to_bytes(s, num_samples);
429}
430EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
431
432static unsigned int __comedi_nscans_left(struct comedi_subdevice *s,
433 unsigned int nscans)
434{
435 struct comedi_async *async = s->async;
436 struct comedi_cmd *cmd = &async->cmd;
437
438 if (cmd->stop_src == TRIG_COUNT) {
439 unsigned int scans_left = 0;
440
441 if (async->scans_done < cmd->stop_arg)
442 scans_left = cmd->stop_arg - async->scans_done;
443
444 if (nscans > scans_left)
445 nscans = scans_left;
446 }
447 return nscans;
448}
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464unsigned int comedi_nscans_left(struct comedi_subdevice *s,
465 unsigned int nscans)
466{
467 if (nscans == 0) {
468 unsigned int nbytes = comedi_buf_read_n_available(s);
469
470 nscans = nbytes / comedi_bytes_per_scan(s);
471 }
472 return __comedi_nscans_left(s, nscans);
473}
474EXPORT_SYMBOL_GPL(comedi_nscans_left);
475
476
477
478
479
480
481
482
483
484unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
485 unsigned int nsamples)
486{
487 struct comedi_async *async = s->async;
488 struct comedi_cmd *cmd = &async->cmd;
489 unsigned long long scans_left;
490 unsigned long long samples_left;
491
492 if (cmd->stop_src != TRIG_COUNT)
493 return nsamples;
494
495 scans_left = __comedi_nscans_left(s, cmd->stop_arg);
496 if (!scans_left)
497 return 0;
498
499 samples_left = scans_left * cmd->scan_end_arg -
500 comedi_bytes_to_samples(s, async->scan_progress);
501
502 if (samples_left < nsamples)
503 return samples_left;
504 return nsamples;
505}
506EXPORT_SYMBOL_GPL(comedi_nsamples_left);
507
508
509
510
511
512
513
514
515
516
517
518void comedi_inc_scan_progress(struct comedi_subdevice *s,
519 unsigned int num_bytes)
520{
521 struct comedi_async *async = s->async;
522 struct comedi_cmd *cmd = &async->cmd;
523 unsigned int scan_length = comedi_bytes_per_scan(s);
524
525
526 if (!(s->subdev_flags & SDF_PACKED)) {
527 async->cur_chan += comedi_bytes_to_samples(s, num_bytes);
528 async->cur_chan %= cmd->chanlist_len;
529 }
530
531 async->scan_progress += num_bytes;
532 if (async->scan_progress >= scan_length) {
533 unsigned int nscans = async->scan_progress / scan_length;
534
535 if (async->scans_done < (UINT_MAX - nscans))
536 async->scans_done += nscans;
537 else
538 async->scans_done = UINT_MAX;
539
540 async->scan_progress %= scan_length;
541 async->events |= COMEDI_CB_EOS;
542 }
543}
544EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562unsigned int comedi_handle_events(struct comedi_device *dev,
563 struct comedi_subdevice *s)
564{
565 unsigned int events = s->async->events;
566
567 if (events == 0)
568 return events;
569
570 if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel)
571 s->cancel(dev, s);
572
573 comedi_event(dev, s);
574
575 return events;
576}
577EXPORT_SYMBOL_GPL(comedi_handle_events);
578
579static int insn_rw_emulate_bits(struct comedi_device *dev,
580 struct comedi_subdevice *s,
581 struct comedi_insn *insn,
582 unsigned int *data)
583{
584 struct comedi_insn _insn;
585 unsigned int chan = CR_CHAN(insn->chanspec);
586 unsigned int base_chan = (chan < 32) ? 0 : chan;
587 unsigned int _data[2];
588 int ret;
589
590 memset(_data, 0, sizeof(_data));
591 memset(&_insn, 0, sizeof(_insn));
592 _insn.insn = INSN_BITS;
593 _insn.chanspec = base_chan;
594 _insn.n = 2;
595 _insn.subdev = insn->subdev;
596
597 if (insn->insn == INSN_WRITE) {
598 if (!(s->subdev_flags & SDF_WRITABLE))
599 return -EINVAL;
600 _data[0] = 1 << (chan - base_chan);
601 _data[1] = data[0] ? (1 << (chan - base_chan)) : 0;
602 }
603
604 ret = s->insn_bits(dev, s, &_insn, _data);
605 if (ret < 0)
606 return ret;
607
608 if (insn->insn == INSN_READ)
609 data[0] = (_data[1] >> (chan - base_chan)) & 1;
610
611 return 1;
612}
613
614static int __comedi_device_postconfig_async(struct comedi_device *dev,
615 struct comedi_subdevice *s)
616{
617 struct comedi_async *async;
618 unsigned int buf_size;
619 int ret;
620
621 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
622 dev_warn(dev->class_dev,
623 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
624 return -EINVAL;
625 }
626 if (!s->do_cmdtest) {
627 dev_warn(dev->class_dev,
628 "async subdevices must have a do_cmdtest() function\n");
629 return -EINVAL;
630 }
631 if (!s->cancel)
632 dev_warn(dev->class_dev,
633 "async subdevices should have a cancel() function\n");
634
635 async = kzalloc(sizeof(*async), GFP_KERNEL);
636 if (!async)
637 return -ENOMEM;
638
639 init_waitqueue_head(&async->wait_head);
640 s->async = async;
641
642 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
643 buf_size = comedi_default_buf_size_kb * 1024;
644 if (buf_size > async->max_bufsize)
645 buf_size = async->max_bufsize;
646
647 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
648 dev_warn(dev->class_dev, "Buffer allocation failed\n");
649 return -ENOMEM;
650 }
651 if (s->buf_change) {
652 ret = s->buf_change(dev, s);
653 if (ret < 0)
654 return ret;
655 }
656
657 comedi_alloc_subdevice_minor(s);
658
659 return 0;
660}
661
662static int __comedi_device_postconfig(struct comedi_device *dev)
663{
664 struct comedi_subdevice *s;
665 int ret;
666 int i;
667
668 if (!dev->insn_device_config)
669 dev->insn_device_config = insn_device_inval;
670
671 if (!dev->get_valid_routes)
672 dev->get_valid_routes = get_zero_valid_routes;
673
674 for (i = 0; i < dev->n_subdevices; i++) {
675 s = &dev->subdevices[i];
676
677 if (s->type == COMEDI_SUBD_UNUSED)
678 continue;
679
680 if (s->type == COMEDI_SUBD_DO) {
681 if (s->n_chan < 32)
682 s->io_bits = (1 << s->n_chan) - 1;
683 else
684 s->io_bits = 0xffffffff;
685 }
686
687 if (s->len_chanlist == 0)
688 s->len_chanlist = 1;
689
690 if (s->do_cmd) {
691 ret = __comedi_device_postconfig_async(dev, s);
692 if (ret)
693 return ret;
694 }
695
696 if (!s->range_table && !s->range_table_list)
697 s->range_table = &range_unknown;
698
699 if (!s->insn_read && s->insn_bits)
700 s->insn_read = insn_rw_emulate_bits;
701 if (!s->insn_write && s->insn_bits)
702 s->insn_write = insn_rw_emulate_bits;
703
704 if (!s->insn_read)
705 s->insn_read = insn_inval;
706 if (!s->insn_write)
707 s->insn_write = insn_inval;
708 if (!s->insn_bits)
709 s->insn_bits = insn_inval;
710 if (!s->insn_config)
711 s->insn_config = insn_inval;
712
713 if (!s->poll)
714 s->poll = poll_invalid;
715 }
716
717 return 0;
718}
719
720
721static int comedi_device_postconfig(struct comedi_device *dev)
722{
723 int ret;
724
725 ret = __comedi_device_postconfig(dev);
726 if (ret < 0)
727 return ret;
728 down_write(&dev->attach_lock);
729 dev->attached = true;
730 up_write(&dev->attach_lock);
731 return 0;
732}
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760static void *comedi_recognize(struct comedi_driver *driv, const char *name)
761{
762 char **name_ptr = (char **)driv->board_name;
763 int i;
764
765 for (i = 0; i < driv->num_names; i++) {
766 if (strcmp(*name_ptr, name) == 0)
767 return name_ptr;
768 name_ptr = (void *)name_ptr + driv->offset;
769 }
770
771 return NULL;
772}
773
774static void comedi_report_boards(struct comedi_driver *driv)
775{
776 unsigned int i;
777 const char *const *name_ptr;
778
779 pr_info("comedi: valid board names for %s driver are:\n",
780 driv->driver_name);
781
782 name_ptr = driv->board_name;
783 for (i = 0; i < driv->num_names; i++) {
784 pr_info(" %s\n", *name_ptr);
785 name_ptr = (const char **)((char *)name_ptr + driv->offset);
786 }
787
788 if (driv->num_names == 0)
789 pr_info(" %s\n", driv->driver_name);
790}
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807int comedi_load_firmware(struct comedi_device *dev,
808 struct device *device,
809 const char *name,
810 int (*cb)(struct comedi_device *dev,
811 const u8 *data, size_t size,
812 unsigned long context),
813 unsigned long context)
814{
815 const struct firmware *fw;
816 int ret;
817
818 if (!cb)
819 return -EINVAL;
820
821 ret = request_firmware(&fw, name, device);
822 if (ret == 0) {
823 ret = cb(dev, fw->data, fw->size, context);
824 release_firmware(fw);
825 }
826
827 return ret < 0 ? ret : 0;
828}
829EXPORT_SYMBOL_GPL(comedi_load_firmware);
830
831
832
833
834
835
836
837
838
839
840
841
842
843int __comedi_request_region(struct comedi_device *dev,
844 unsigned long start, unsigned long len)
845{
846 if (!start) {
847 dev_warn(dev->class_dev,
848 "%s: a I/O base address must be specified\n",
849 dev->board_name);
850 return -EINVAL;
851 }
852
853 if (!request_region(start, len, dev->board_name)) {
854 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
855 dev->board_name, start, len);
856 return -EIO;
857 }
858
859 return 0;
860}
861EXPORT_SYMBOL_GPL(__comedi_request_region);
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878int comedi_request_region(struct comedi_device *dev,
879 unsigned long start, unsigned long len)
880{
881 int ret;
882
883 ret = __comedi_request_region(dev, start, len);
884 if (ret == 0) {
885 dev->iobase = start;
886 dev->iolen = len;
887 }
888
889 return ret;
890}
891EXPORT_SYMBOL_GPL(comedi_request_region);
892
893
894
895
896
897
898
899
900
901
902
903
904
905void comedi_legacy_detach(struct comedi_device *dev)
906{
907 if (dev->irq) {
908 free_irq(dev->irq, dev);
909 dev->irq = 0;
910 }
911 if (dev->iobase && dev->iolen) {
912 release_region(dev->iobase, dev->iolen);
913 dev->iobase = 0;
914 dev->iolen = 0;
915 }
916}
917EXPORT_SYMBOL_GPL(comedi_legacy_detach);
918
919int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
920{
921 struct comedi_driver *driv;
922 int ret;
923
924 if (dev->attached)
925 return -EBUSY;
926
927 mutex_lock(&comedi_drivers_list_lock);
928 for (driv = comedi_drivers; driv; driv = driv->next) {
929 if (!try_module_get(driv->module))
930 continue;
931 if (driv->num_names) {
932 dev->board_ptr = comedi_recognize(driv, it->board_name);
933 if (dev->board_ptr)
934 break;
935 } else if (strcmp(driv->driver_name, it->board_name) == 0) {
936 break;
937 }
938 module_put(driv->module);
939 }
940 if (!driv) {
941
942
943 for (driv = comedi_drivers; driv; driv = driv->next) {
944 if (!try_module_get(driv->module))
945 continue;
946 comedi_report_boards(driv);
947 module_put(driv->module);
948 }
949 ret = -EIO;
950 goto out;
951 }
952 if (!driv->attach) {
953
954 dev_warn(dev->class_dev,
955 "driver '%s' does not support attach using comedi_config\n",
956 driv->driver_name);
957 module_put(driv->module);
958 ret = -EIO;
959 goto out;
960 }
961 dev->driver = driv;
962 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
963 : dev->driver->driver_name;
964 ret = driv->attach(dev, it);
965 if (ret >= 0)
966 ret = comedi_device_postconfig(dev);
967 if (ret < 0) {
968 comedi_device_detach(dev);
969 module_put(driv->module);
970 }
971
972out:
973 mutex_unlock(&comedi_drivers_list_lock);
974 return ret;
975}
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000int comedi_auto_config(struct device *hardware_device,
1001 struct comedi_driver *driver, unsigned long context)
1002{
1003 struct comedi_device *dev;
1004 int ret;
1005
1006 if (!hardware_device) {
1007 pr_warn("BUG! %s called with NULL hardware_device\n", __func__);
1008 return -EINVAL;
1009 }
1010 if (!driver) {
1011 dev_warn(hardware_device,
1012 "BUG! %s called with NULL comedi driver\n", __func__);
1013 return -EINVAL;
1014 }
1015
1016 if (!driver->auto_attach) {
1017 dev_warn(hardware_device,
1018 "BUG! comedi driver '%s' has no auto_attach handler\n",
1019 driver->driver_name);
1020 return -EINVAL;
1021 }
1022
1023 dev = comedi_alloc_board_minor(hardware_device);
1024 if (IS_ERR(dev)) {
1025 dev_warn(hardware_device,
1026 "driver '%s' could not create device.\n",
1027 driver->driver_name);
1028 return PTR_ERR(dev);
1029 }
1030
1031
1032 dev->driver = driver;
1033 dev->board_name = dev->driver->driver_name;
1034 ret = driver->auto_attach(dev, context);
1035 if (ret >= 0)
1036 ret = comedi_device_postconfig(dev);
1037 mutex_unlock(&dev->mutex);
1038
1039 if (ret < 0) {
1040 dev_warn(hardware_device,
1041 "driver '%s' failed to auto-configure device.\n",
1042 driver->driver_name);
1043 comedi_release_hardware_device(hardware_device);
1044 } else {
1045
1046
1047
1048
1049 dev_info(dev->class_dev,
1050 "driver '%s' has successfully auto-configured '%s'.\n",
1051 driver->driver_name, dev->board_name);
1052 }
1053 return ret;
1054}
1055EXPORT_SYMBOL_GPL(comedi_auto_config);
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073void comedi_auto_unconfig(struct device *hardware_device)
1074{
1075 if (!hardware_device)
1076 return;
1077 comedi_release_hardware_device(hardware_device);
1078}
1079EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092int comedi_driver_register(struct comedi_driver *driver)
1093{
1094 mutex_lock(&comedi_drivers_list_lock);
1095 driver->next = comedi_drivers;
1096 comedi_drivers = driver;
1097 mutex_unlock(&comedi_drivers_list_lock);
1098
1099 return 0;
1100}
1101EXPORT_SYMBOL_GPL(comedi_driver_register);
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112void comedi_driver_unregister(struct comedi_driver *driver)
1113{
1114 struct comedi_driver *prev;
1115 int i;
1116
1117
1118 mutex_lock(&comedi_drivers_list_lock);
1119 if (comedi_drivers == driver) {
1120 comedi_drivers = driver->next;
1121 } else {
1122 for (prev = comedi_drivers; prev->next; prev = prev->next) {
1123 if (prev->next == driver) {
1124 prev->next = driver->next;
1125 break;
1126 }
1127 }
1128 }
1129 mutex_unlock(&comedi_drivers_list_lock);
1130
1131
1132 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
1133 struct comedi_device *dev = comedi_dev_get_from_minor(i);
1134
1135 if (!dev)
1136 continue;
1137
1138 mutex_lock(&dev->mutex);
1139 if (dev->attached && dev->driver == driver) {
1140 if (dev->use_count)
1141 dev_warn(dev->class_dev,
1142 "BUG! detaching device with use_count=%d\n",
1143 dev->use_count);
1144 comedi_device_detach(dev);
1145 }
1146 mutex_unlock(&dev->mutex);
1147 comedi_dev_put(dev);
1148 }
1149}
1150EXPORT_SYMBOL_GPL(comedi_driver_unregister);
1151