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