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