Xenomai  3.0-rc7
driver.h
Go to the documentation of this file.
1 
26 #ifndef _COBALT_RTDM_DRIVER_H
27 #define _COBALT_RTDM_DRIVER_H
28 
29 #include <asm/atomic.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/cdev.h>
33 #include <linux/wait.h>
34 #include <linux/notifier.h>
35 #include <xenomai/version.h>
36 #include <cobalt/kernel/heap.h>
37 #include <cobalt/kernel/sched.h>
38 #include <cobalt/kernel/intr.h>
39 #include <cobalt/kernel/synch.h>
40 #include <cobalt/kernel/select.h>
41 #include <cobalt/kernel/clock.h>
42 #include <cobalt/kernel/apc.h>
43 #include <cobalt/kernel/init.h>
44 #include <cobalt/kernel/ancillaries.h>
45 #include <cobalt/kernel/tree.h>
46 #include <rtdm/fd.h>
47 #include <rtdm/rtdm.h>
48 
49 /* debug support */
50 #include <cobalt/kernel/assert.h>
51 #include <trace/events/cobalt-rtdm.h>
52 #ifdef CONFIG_PCI
53 #include <asm-generic/xenomai/pci_ids.h>
54 #endif /* CONFIG_PCI */
55 #include <asm/xenomai/syscall.h>
56 
57 struct class;
58 typedef struct xnselector rtdm_selector_t;
59 enum rtdm_selecttype;
60 
73 #define RTDM_EXCLUSIVE 0x0001
74 
80 #define RTDM_FIXED_MINOR 0x0002
81 
83 #define RTDM_NAMED_DEVICE 0x0010
84 
87 #define RTDM_PROTOCOL_DEVICE 0x0020
88 
90 #define RTDM_DEVICE_TYPE_MASK 0x00F0
91 
93 #define RTDM_SECURE_DEVICE 0x80000000
94 
110  RTDM_SELECTTYPE_READ = XNSELECT_READ,
111 
113  RTDM_SELECTTYPE_WRITE = XNSELECT_WRITE,
114 
116  RTDM_SELECTTYPE_EXCEPT = XNSELECT_EXCEPT
117 };
134  struct rtdm_fd fd;
135 
139 
141  char dev_private[0];
142 };
143 
144 static inline struct rtdm_dev_context *rtdm_fd_to_context(struct rtdm_fd *fd)
145 {
146  return container_of(fd, struct rtdm_dev_context, fd);
147 }
148 
158 static inline void *rtdm_fd_to_private(struct rtdm_fd *fd)
159 {
160  return &rtdm_fd_to_context(fd)->dev_private[0];
161 }
162 
171 static inline struct rtdm_fd *rtdm_private_to_fd(void *dev_private)
172 {
173  struct rtdm_dev_context *ctx;
174  ctx = container_of(dev_private, struct rtdm_dev_context, dev_private);
175  return &ctx->fd;
176 }
177 
186 static inline bool rtdm_fd_is_user(struct rtdm_fd *fd)
187 {
188  return rtdm_fd_owner(fd) != &cobalt_kernel_ppd;
189 }
190 
199 static inline struct rtdm_device *rtdm_fd_device(struct rtdm_fd *fd)
200 {
201  return rtdm_fd_to_context(fd)->device;
202 }
203 
212 struct rtdm_profile_info {
214  const char *name;
216  int class_id;
219  int subclass_id;
221  int version;
223  unsigned int magic;
224  struct module *owner;
225 };
226 
227 struct rtdm_driver;
228 
232 struct rtdm_sm_ops {
234  int (*start)(struct rtdm_driver *drv);
236  int (*stop)(struct rtdm_driver *drv);
237 };
238 
245 struct rtdm_driver {
251  struct rtdm_profile_info profile_info;
258  size_t context_size;
264  struct rtdm_fd_ops ops;
273  struct {
274  union {
275  struct {
276  struct cdev cdev;
277  int major;
278  } named;
279  };
280  atomic_t refcount;
281  struct notifier_block nb_statechange;
282  };
283 };
284 
285 #define RTDM_CLASS_MAGIC 0x8284636c
286 
305 #define RTDM_PROFILE_INFO(__name, __id, __subid, __version) \
306 { \
307  .name = ( # __name ), \
308  .class_id = (__id), \
309  .subclass_id = (__subid), \
310  .version = (__version), \
311  .magic = ~RTDM_CLASS_MAGIC, \
312  .owner = THIS_MODULE, \
313 }
314 
321 struct rtdm_device {
325  void *device_data;
337  const char *label;
352  int minor;
354  struct {
355  unsigned int magic;
356  char *name;
357  union {
358  struct {
359  xnhandle_t handle;
360  } named;
361  struct {
362  struct xnid id;
363  } proto;
364  };
365  dev_t rdev;
366  struct device *kdev;
367  atomic_t refcount;
368  struct rtdm_fd_ops ops;
369  wait_queue_head_t putwq;
370  };
371 };
372 
375 /* --- device registration --- */
376 
377 int rtdm_dev_register(struct rtdm_device *device);
378 
379 void rtdm_dev_unregister(struct rtdm_device *device);
380 
381 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
382 
383 /* --- clock services --- */
384 static inline nanosecs_abs_t rtdm_clock_read(void)
385 {
386  return xnclock_read_realtime(&nkclock);
387 }
388 
389 static inline nanosecs_abs_t rtdm_clock_read_monotonic(void)
390 {
391  return xnclock_read_monotonic(&nkclock);
392 }
393 #endif /* !DOXYGEN_CPP */
394 
395 /* --- timeout sequences */
396 
397 typedef nanosecs_abs_t rtdm_toseq_t;
398 
399 void rtdm_toseq_init(rtdm_toseq_t *timeout_seq, nanosecs_rel_t timeout);
400 
436 #define cobalt_atomic_enter(context) \
437  do { \
438  xnlock_get_irqsave(&nklock, (context)); \
439  xnsched_lock(); \
440  } while (0)
441 
454 #define cobalt_atomic_leave(context) \
455  do { \
456  xnsched_unlock(); \
457  xnlock_put_irqrestore(&nklock, (context)); \
458  } while (0)
459 
488 #ifdef DOXYGEN_CPP /* Beautify doxygen output */
489 #define RTDM_EXECUTE_ATOMICALLY(code_block) \
490 { \
491  <ENTER_ATOMIC_SECTION> \
492  code_block; \
493  <LEAVE_ATOMIC_SECTION> \
494 }
495 #else /* This is how it really works */
496 static inline __attribute__((deprecated)) void
497 rtdm_execute_atomically(void) { }
498 
499 #define RTDM_EXECUTE_ATOMICALLY(code_block) \
500 { \
501  spl_t __rtdm_s; \
502  \
503  rtdm_execute_atomically(); \
504  xnlock_get_irqsave(&nklock, __rtdm_s); \
505  xnsched_lock(); \
506  code_block; \
507  xnsched_unlock(); \
508  xnlock_put_irqrestore(&nklock, __rtdm_s); \
509 }
510 #endif
511 
522 #define RTDM_LOCK_UNLOCKED(__name) IPIPE_SPIN_LOCK_UNLOCKED
523 
524 #define DEFINE_RTDM_LOCK(__name) \
525  rtdm_lock_t __name = RTDM_LOCK_UNLOCKED(__name)
526 
528 typedef ipipe_spinlock_t rtdm_lock_t;
529 
531 typedef unsigned long rtdm_lockctx_t;
532 
540 static inline void rtdm_lock_init(rtdm_lock_t *lock)
541 {
542  spin_lock_init(lock);
543 }
544 
552 static inline void rtdm_lock_get(rtdm_lock_t *lock)
553 {
554  XENO_BUG_ON(COBALT, !spltest());
555  spin_lock(lock);
556  xnsched_lock();
557 }
558 
566 static inline void rtdm_lock_put(rtdm_lock_t *lock)
567 {
568  spin_unlock(lock);
569  xnsched_unlock();
570 }
571 
581 static inline rtdm_lockctx_t __rtdm_lock_get_irqsave(rtdm_lock_t *lock)
582 {
583  rtdm_lockctx_t context;
584 
585  context = ipipe_test_and_stall_head();
586  spin_lock(lock);
587  xnsched_lock();
588 
589  return context;
590 }
591 #define rtdm_lock_get_irqsave(__lock, __context) \
592  ((__context) = __rtdm_lock_get_irqsave(__lock))
593 
602 static inline
603 void rtdm_lock_put_irqrestore(rtdm_lock_t *lock, rtdm_lockctx_t context)
604 {
605  spin_unlock(lock);
606  xnsched_unlock();
607  ipipe_restore_head(context);
608 }
609 
617 #define rtdm_lock_irqsave(context) \
618  splhigh(context)
619 
627 #define rtdm_lock_irqrestore(context) \
628  splexit(context)
629 
632 #ifndef DOXYGEN_CPP
633 
634 struct rtdm_waitqueue {
635  struct xnsynch wait;
636 };
637 typedef struct rtdm_waitqueue rtdm_waitqueue_t;
638 
639 #define RTDM_WAITQUEUE_INITIALIZER(__name) { \
640  .wait = XNSYNCH_WAITQUEUE_INITIALIZER((__name).wait), \
641  }
642 
643 #define DEFINE_RTDM_WAITQUEUE(__name) \
644  struct rtdm_waitqueue __name = RTDM_WAITQUEUE_INITIALIZER(__name)
645 
646 #define DEFINE_RTDM_WAITQUEUE_ONSTACK(__name) \
647  DEFINE_RTDM_WAITQUEUE(__name)
648 
649 static inline void rtdm_waitqueue_init(struct rtdm_waitqueue *wq)
650 {
651  *wq = (struct rtdm_waitqueue)RTDM_WAITQUEUE_INITIALIZER(*wq);
652 }
653 
654 static inline void rtdm_waitqueue_destroy(struct rtdm_waitqueue *wq)
655 {
656  xnsynch_destroy(&wq->wait);
657 }
658 
659 static inline int __rtdm_dowait(struct rtdm_waitqueue *wq,
660  nanosecs_rel_t timeout, xntmode_t timeout_mode)
661 {
662  int ret;
663 
664  ret = xnsynch_sleep_on(&wq->wait, timeout, timeout_mode);
665  if (ret & XNBREAK)
666  return -EINTR;
667  if (ret & XNTIMEO)
668  return -ETIMEDOUT;
669  if (ret & XNRMID)
670  return -EIDRM;
671  return 0;
672 }
673 
674 static inline int __rtdm_timedwait(struct rtdm_waitqueue *wq,
675  nanosecs_rel_t timeout, rtdm_toseq_t *toseq)
676 {
677  if (toseq && timeout > 0)
678  return __rtdm_dowait(wq, *toseq, XN_ABSOLUTE);
679 
680  return __rtdm_dowait(wq, timeout, XN_RELATIVE);
681 }
682 
683 #define rtdm_timedwait_condition_locked(__wq, __cond, __timeout, __toseq) \
684  ({ \
685  int __ret = 0; \
686  while (__ret == 0 && !(__cond)) \
687  __ret = __rtdm_timedwait(__wq, __timeout, __toseq); \
688  __ret; \
689  })
690 
691 #define rtdm_wait_condition_locked(__wq, __cond) \
692  ({ \
693  int __ret = 0; \
694  while (__ret == 0 && !(__cond)) \
695  __ret = __rtdm_dowait(__wq, \
696  XN_INFINITE, XN_RELATIVE); \
697  __ret; \
698  })
699 
700 #define rtdm_timedwait_condition(__wq, __cond, __timeout, __toseq) \
701  ({ \
702  spl_t __s; \
703  int __ret; \
704  xnlock_get_irqsave(&nklock, __s); \
705  __ret = rtdm_timedwait_condition_locked(__wq, __cond, \
706  __timeout, __toseq); \
707  xnlock_put_irqrestore(&nklock, __s); \
708  __ret; \
709  })
710 
711 #define rtdm_timedwait(__wq, __timeout, __toseq) \
712  __rtdm_timedwait(__wq, __timeout, __toseq)
713 
714 #define rtdm_timedwait_locked(__wq, __timeout, __toseq) \
715  rtdm_timedwait(__wq, __timeout, __toseq)
716 
717 #define rtdm_wait_condition(__wq, __cond) \
718  ({ \
719  spl_t __s; \
720  int __ret; \
721  xnlock_get_irqsave(&nklock, __s); \
722  __ret = rtdm_wait_condition_locked(__wq, __cond); \
723  xnlock_put_irqrestore(&nklock, __s); \
724  __ret; \
725  })
726 
727 #define rtdm_wait(__wq) \
728  __rtdm_dowait(__wq, XN_INFINITE, XN_RELATIVE)
729 
730 #define rtdm_wait_locked(__wq) rtdm_wait(__wq)
731 
732 #define rtdm_waitqueue_lock(__wq, __context) cobalt_atomic_enter(__context)
733 
734 #define rtdm_waitqueue_unlock(__wq, __context) cobalt_atomic_leave(__context)
735 
736 #define rtdm_waitqueue_signal(__wq) \
737  ({ \
738  struct xnthread *__waiter; \
739  __waiter = xnsynch_wakeup_one_sleeper(&(__wq)->wait); \
740  xnsched_run(); \
741  __waiter != NULL; \
742  })
743 
744 #define __rtdm_waitqueue_flush(__wq, __reason) \
745  ({ \
746  int __ret; \
747  __ret = xnsynch_flush(&(__wq)->wait, __reason); \
748  xnsched_run(); \
749  __ret == XNSYNCH_RESCHED; \
750  })
751 
752 #define rtdm_waitqueue_broadcast(__wq) \
753  __rtdm_waitqueue_flush(__wq, 0)
754 
755 #define rtdm_waitqueue_flush(__wq) \
756  __rtdm_waitqueue_flush(__wq, XNBREAK)
757 
758 #define rtdm_waitqueue_wakeup(__wq, __waiter) \
759  do { \
760  xnsynch_wakeup_this_sleeper(&(__wq)->wait, __waiter); \
761  xnsched_run(); \
762  } while (0)
763 
764 #define rtdm_for_each_waiter(__pos, __wq) \
765  xnsynch_for_each_sleeper(__pos, &(__wq)->wait)
766 
767 #define rtdm_for_each_waiter_safe(__pos, __tmp, __wq) \
768  xnsynch_for_each_sleeper_safe(__pos, __tmp, &(__wq)->wait)
769 
770 #endif /* !DOXYGEN_CPP */
771 
774 /* --- Interrupt management services --- */
780 typedef struct xnintr rtdm_irq_t;
781 
788 #define RTDM_IRQTYPE_SHARED XN_IRQTYPE_SHARED
789 
791 #define RTDM_IRQTYPE_EDGE XN_IRQTYPE_EDGE
792 
801 typedef int (*rtdm_irq_handler_t)(rtdm_irq_t *irq_handle);
802 
809 #define RTDM_IRQ_NONE XN_IRQ_NONE
810 
811 #define RTDM_IRQ_HANDLED XN_IRQ_HANDLED
812 
813 #define RTDM_IRQ_DISABLE XN_IRQ_DISABLE
814 
827 #define rtdm_irq_get_arg(irq_handle, type) ((type *)irq_handle->cookie)
828 
830 int rtdm_irq_request(rtdm_irq_t *irq_handle, unsigned int irq_no,
831  rtdm_irq_handler_t handler, unsigned long flags,
832  const char *device_name, void *arg);
833 
834 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
835 static inline int rtdm_irq_free(rtdm_irq_t *irq_handle)
836 {
837  if (!XENO_ASSERT(COBALT, xnsched_root_p()))
838  return -EPERM;
839  xnintr_detach(irq_handle);
840  return 0;
841 }
842 
843 static inline int rtdm_irq_enable(rtdm_irq_t *irq_handle)
844 {
845  xnintr_enable(irq_handle);
846  return 0;
847 }
848 
849 static inline int rtdm_irq_disable(rtdm_irq_t *irq_handle)
850 {
851  xnintr_disable(irq_handle);
852  return 0;
853 }
854 #endif /* !DOXYGEN_CPP */
855 
856 /* --- non-real-time signalling services --- */
857 
863 typedef struct rtdm_nrtsig rtdm_nrtsig_t;
874 typedef void (*rtdm_nrtsig_handler_t)(rtdm_nrtsig_t *nrt_sig, void *arg);
875 
876 struct rtdm_nrtsig {
877  rtdm_nrtsig_handler_t handler;
878  void *arg;
879 };
880 
881 void rtdm_schedule_nrt_work(struct work_struct *lostage_work);
884 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
885 static inline void rtdm_nrtsig_init(rtdm_nrtsig_t *nrt_sig,
886  rtdm_nrtsig_handler_t handler, void *arg)
887 {
888  nrt_sig->handler = handler;
889  nrt_sig->arg = arg;
890 }
891 
892 static inline void rtdm_nrtsig_destroy(rtdm_nrtsig_t *nrt_sig)
893 {
894  nrt_sig->handler = NULL;
895  nrt_sig->arg = NULL;
896 }
897 
898 void rtdm_nrtsig_pend(rtdm_nrtsig_t *nrt_sig);
899 #endif /* !DOXYGEN_CPP */
900 
901 /* --- timer services --- */
902 
908 typedef struct xntimer rtdm_timer_t;
909 
915 typedef void (*rtdm_timer_handler_t)(rtdm_timer_t *timer);
916 
924  RTDM_TIMERMODE_RELATIVE = XN_RELATIVE,
925 
927  RTDM_TIMERMODE_ABSOLUTE = XN_ABSOLUTE,
928 
931 };
936 #ifndef DOXYGEN_CPP /* Avoid broken doxygen output */
937 #define rtdm_timer_init(timer, handler, name) \
938 ({ \
939  xntimer_init((timer), &nkclock, handler, \
940  NULL, XNTIMER_IGRAVITY); \
941  xntimer_set_name((timer), (name)); \
942  0; \
943 })
944 
945 #endif /* !DOXYGEN_CPP */
946 
947 void rtdm_timer_destroy(rtdm_timer_t *timer);
948 
949 int rtdm_timer_start(rtdm_timer_t *timer, nanosecs_abs_t expiry,
950  nanosecs_rel_t interval, enum rtdm_timer_mode mode);
951 
952 void rtdm_timer_stop(rtdm_timer_t *timer);
953 
954 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
955 static inline int rtdm_timer_start_in_handler(rtdm_timer_t *timer,
956  nanosecs_abs_t expiry,
957  nanosecs_rel_t interval,
958  enum rtdm_timer_mode mode)
959 {
960  return xntimer_start(timer, expiry, interval, (xntmode_t)mode);
961 }
962 
963 static inline void rtdm_timer_stop_in_handler(rtdm_timer_t *timer)
964 {
965  xntimer_stop(timer);
966 }
967 #endif /* !DOXYGEN_CPP */
968 
969 /* --- task services --- */
975 typedef struct xnthread rtdm_task_t;
976 
982 typedef void (*rtdm_task_proc_t)(void *arg);
983 
988 #define RTDM_TASK_LOWEST_PRIORITY 0
989 #define RTDM_TASK_HIGHEST_PRIORITY 99
990 
996 #define RTDM_TASK_RAISE_PRIORITY (+1)
997 #define RTDM_TASK_LOWER_PRIORITY (-1)
998 
1002 int rtdm_task_init(rtdm_task_t *task, const char *name,
1003  rtdm_task_proc_t task_proc, void *arg,
1004  int priority, nanosecs_rel_t period);
1005 int __rtdm_task_sleep(xnticks_t timeout, xntmode_t mode);
1007 
1008 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
1009 static inline void rtdm_task_destroy(rtdm_task_t *task)
1010 {
1011  xnthread_cancel(task);
1012  xnthread_join(task, true);
1013 }
1014 
1015 static inline int rtdm_task_should_stop(void)
1016 {
1017  return xnthread_test_info(xnthread_current(), XNCANCELD);
1018 }
1019 
1020 void rtdm_task_join(rtdm_task_t *task);
1021 
1022 static inline void __deprecated rtdm_task_join_nrt(rtdm_task_t *task,
1023  unsigned int poll_delay)
1024 {
1025  rtdm_task_join(task);
1026 }
1027 
1028 static inline void rtdm_task_set_priority(rtdm_task_t *task, int priority)
1029 {
1030  union xnsched_policy_param param = { .rt = { .prio = priority } };
1031  xnthread_set_schedparam(task, &xnsched_class_rt, &param);
1032  xnsched_run();
1033 }
1034 
1035 static inline int rtdm_task_set_period(rtdm_task_t *task,
1036  nanosecs_rel_t period)
1037 {
1038  if (period < 0)
1039  period = 0;
1040 
1041  return xnthread_set_periodic(task, XN_INFINITE, XN_RELATIVE, period);
1042 }
1043 
1044 static inline int rtdm_task_unblock(rtdm_task_t *task)
1045 {
1046  int res = xnthread_unblock(task);
1047 
1048  xnsched_run();
1049  return res;
1050 }
1051 
1052 static inline rtdm_task_t *rtdm_task_current(void)
1053 {
1054  return xnthread_current();
1055 }
1056 
1057 static inline int rtdm_task_wait_period(void)
1058 {
1059  if (!XENO_ASSERT(COBALT, !xnsched_unblockable_p()))
1060  return -EPERM;
1061  return xnthread_wait_period(NULL);
1062 }
1063 
1064 static inline int rtdm_task_sleep(nanosecs_rel_t delay)
1065 {
1066  return __rtdm_task_sleep(delay, XN_RELATIVE);
1067 }
1068 
1069 static inline int
1070 rtdm_task_sleep_abs(nanosecs_abs_t wakeup_date, enum rtdm_timer_mode mode)
1071 {
1072  /* For the sake of a consistent API usage... */
1073  if (mode != RTDM_TIMERMODE_ABSOLUTE && mode != RTDM_TIMERMODE_REALTIME)
1074  return -EINVAL;
1075  return __rtdm_task_sleep(wakeup_date, (xntmode_t)mode);
1076 }
1077 
1078 /* rtdm_task_sleep_abs shall be used instead */
1079 static inline int __deprecated rtdm_task_sleep_until(nanosecs_abs_t wakeup_time)
1080 {
1081  return __rtdm_task_sleep(wakeup_time, XN_REALTIME);
1082 }
1083 
1084 #define rtdm_task_busy_wait(__condition, __spin_ns, __sleep_ns) \
1085  ({ \
1086  __label__ done; \
1087  nanosecs_abs_t __end; \
1088  int __ret = 0; \
1089  for (;;) { \
1090  __end = rtdm_clock_read_monotonic() + __spin_ns; \
1091  for (;;) { \
1092  if (__condition) \
1093  goto done; \
1094  if (rtdm_clock_read_monotonic() >= __end) \
1095  break; \
1096  } \
1097  __ret = rtdm_task_sleep(__sleep_ns); \
1098  if (__ret) \
1099  break; \
1100  } \
1101  done: \
1102  __ret; \
1103  })
1104 
1105 #endif /* !DOXYGEN_CPP */
1106 
1107 /* --- event services --- */
1108 
1109 typedef struct rtdm_event {
1110  struct xnsynch synch_base;
1111  DECLARE_XNSELECT(select_block);
1112 } rtdm_event_t;
1113 
1114 #define RTDM_EVENT_PENDING XNSYNCH_SPARE1
1115 
1116 void rtdm_event_init(rtdm_event_t *event, unsigned long pending);
1117 int rtdm_event_select(rtdm_event_t *event, rtdm_selector_t *selector,
1118  enum rtdm_selecttype type, unsigned fd_index);
1119 int rtdm_event_wait(rtdm_event_t *event);
1120 int rtdm_event_timedwait(rtdm_event_t *event, nanosecs_rel_t timeout,
1121  rtdm_toseq_t *timeout_seq);
1122 void rtdm_event_signal(rtdm_event_t *event);
1123 
1124 void rtdm_event_clear(rtdm_event_t *event);
1125 
1126 void rtdm_event_pulse(rtdm_event_t *event);
1127 
1128 void rtdm_event_destroy(rtdm_event_t *event);
1129 
1130 /* --- semaphore services --- */
1131 
1132 typedef struct rtdm_sem {
1133  unsigned long value;
1134  struct xnsynch synch_base;
1135  DECLARE_XNSELECT(select_block);
1136 } rtdm_sem_t;
1137 
1138 void rtdm_sem_init(rtdm_sem_t *sem, unsigned long value);
1139 int rtdm_sem_select(rtdm_sem_t *sem, rtdm_selector_t *selector,
1140  enum rtdm_selecttype type, unsigned fd_index);
1141 int rtdm_sem_down(rtdm_sem_t *sem);
1142 int rtdm_sem_timeddown(rtdm_sem_t *sem, nanosecs_rel_t timeout,
1143  rtdm_toseq_t *timeout_seq);
1144 void rtdm_sem_up(rtdm_sem_t *sem);
1145 
1146 void rtdm_sem_destroy(rtdm_sem_t *sem);
1147 
1148 /* --- mutex services --- */
1149 
1150 typedef struct rtdm_mutex {
1151  struct xnsynch synch_base;
1152  atomic_t fastlock;
1153 } rtdm_mutex_t;
1154 
1155 void rtdm_mutex_init(rtdm_mutex_t *mutex);
1156 int rtdm_mutex_lock(rtdm_mutex_t *mutex);
1157 int rtdm_mutex_timedlock(rtdm_mutex_t *mutex, nanosecs_rel_t timeout,
1158  rtdm_toseq_t *timeout_seq);
1159 void rtdm_mutex_unlock(rtdm_mutex_t *mutex);
1160 void rtdm_mutex_destroy(rtdm_mutex_t *mutex);
1161 
1162 /* --- utility functions --- */
1163 
1164 #define rtdm_printk(format, ...) printk(format, ##__VA_ARGS__)
1165 
1166 struct rtdm_ratelimit_state {
1167  rtdm_lock_t lock; /* protect the state */
1168  nanosecs_abs_t interval;
1169  int burst;
1170  int printed;
1171  int missed;
1172  nanosecs_abs_t begin;
1173 };
1174 
1175 int rtdm_ratelimit(struct rtdm_ratelimit_state *rs, const char *func);
1176 
1177 #define DEFINE_RTDM_RATELIMIT_STATE(name, interval_init, burst_init) \
1178  struct rtdm_ratelimit_state name = { \
1179  .lock = RTDM_LOCK_UNLOCKED((name).lock), \
1180  .interval = interval_init, \
1181  .burst = burst_init, \
1182  }
1183 
1184 /* We use the Linux defaults */
1185 #define DEF_RTDM_RATELIMIT_INTERVAL 5000000000LL
1186 #define DEF_RTDM_RATELIMIT_BURST 10
1187 
1188 #define rtdm_printk_ratelimited(fmt, ...) ({ \
1189  static DEFINE_RTDM_RATELIMIT_STATE(_rs, \
1190  DEF_RTDM_RATELIMIT_INTERVAL, \
1191  DEF_RTDM_RATELIMIT_BURST); \
1192  \
1193  if (rtdm_ratelimit(&_rs, __func__)) \
1194  printk(fmt, ##__VA_ARGS__); \
1195 })
1196 
1197 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
1198 static inline void *rtdm_malloc(size_t size)
1199 {
1200  return xnmalloc(size);
1201 }
1202 
1203 static inline void rtdm_free(void *ptr)
1204 {
1205  xnfree(ptr);
1206 }
1207 
1208 int rtdm_mmap_to_user(struct rtdm_fd *fd,
1209  void *src_addr, size_t len,
1210  int prot, void **pptr,
1211  struct vm_operations_struct *vm_ops,
1212  void *vm_private_data);
1213 
1214 int rtdm_iomap_to_user(struct rtdm_fd *fd,
1215  phys_addr_t src_addr, size_t len,
1216  int prot, void **pptr,
1217  struct vm_operations_struct *vm_ops,
1218  void *vm_private_data);
1219 
1220 int rtdm_mmap_kmem(struct vm_area_struct *vma, void *va);
1221 
1222 int rtdm_mmap_vmem(struct vm_area_struct *vma, void *va);
1223 
1224 int rtdm_mmap_iomem(struct vm_area_struct *vma, phys_addr_t pa);
1225 
1226 int rtdm_munmap(void *ptr, size_t len);
1227 
1228 static inline int rtdm_read_user_ok(struct rtdm_fd *fd,
1229  const void __user *ptr, size_t size)
1230 {
1231  return access_rok(ptr, size);
1232 }
1233 
1234 static inline int rtdm_rw_user_ok(struct rtdm_fd *fd,
1235  const void __user *ptr, size_t size)
1236 {
1237  return access_wok(ptr, size);
1238 }
1239 
1240 static inline int rtdm_copy_from_user(struct rtdm_fd *fd,
1241  void *dst, const void __user *src,
1242  size_t size)
1243 {
1244  return __xn_copy_from_user(dst, src, size) ? -EFAULT : 0;
1245 }
1246 
1247 static inline int rtdm_safe_copy_from_user(struct rtdm_fd *fd,
1248  void *dst, const void __user *src,
1249  size_t size)
1250 {
1251  return cobalt_copy_from_user(dst, src, size);
1252 }
1253 
1254 static inline int rtdm_copy_to_user(struct rtdm_fd *fd,
1255  void __user *dst, const void *src,
1256  size_t size)
1257 {
1258  return __xn_copy_to_user(dst, src, size) ? -EFAULT : 0;
1259 }
1260 
1261 static inline int rtdm_safe_copy_to_user(struct rtdm_fd *fd,
1262  void __user *dst, const void *src,
1263  size_t size)
1264 {
1265  return cobalt_copy_to_user(dst, src, size);
1266 }
1267 
1268 static inline int rtdm_strncpy_from_user(struct rtdm_fd *fd,
1269  char *dst,
1270  const char __user *src, size_t count)
1271 {
1272  return cobalt_strncpy_from_user(dst, src, count);
1273 }
1274 
1275 static inline int rtdm_rt_capable(struct rtdm_fd *fd)
1276 {
1277  if (!XENO_ASSERT(COBALT, !xnsched_interrupt_p()))
1278  return 0;
1279 
1280  if (!rtdm_fd_is_user(fd))
1281  return !xnsched_root_p();
1282 
1283  return xnthread_current() != NULL;
1284 }
1285 
1286 static inline int rtdm_in_rt_context(void)
1287 {
1288  return (ipipe_current_domain != ipipe_root_domain);
1289 }
1290 
1291 #endif /* !DOXYGEN_CPP */
1292 
1293 #endif /* _COBALT_RTDM_DRIVER_H */
void(* rtdm_task_proc_t)(void *arg)
Real-time task procedure.
Definition: driver.h:982
static void rtdm_lock_put_irqrestore(rtdm_lock_t *lock, rtdm_lockctx_t context)
Release lock and restore preemption state.
Definition: driver.h:603
void rtdm_mutex_destroy(rtdm_mutex_t *mutex)
Destroy a mutex.
Definition: drvlib.c:1164
struct rtdm_fd_ops ops
I/O operation handlers.
Definition: driver.h:264
void rtdm_task_busy_sleep(nanosecs_rel_t delay)
Busy-wait a specified amount of time.
Definition: drvlib.c:387
int rtdm_nrtsig_init(rtdm_nrtsig_t *nrt_sig, rtdm_nrtsig_handler_t handler, void *arg)
Register a non-real-time signal handler.
int socket_type
Protocol device identification: socket type (SOCK_xxx)
Definition: driver.h:262
void xnintr_enable(struct xnintr *intr)
Enable an interrupt line.
Definition: intr.c:892
void rtdm_mutex_unlock(rtdm_mutex_t *mutex)
Release a mutex.
Definition: drvlib.c:1182
void(* rtdm_nrtsig_handler_t)(rtdm_nrtsig_t *nrt_sig, void *arg)
Non-real-time signal handler.
Definition: driver.h:874
void xnintr_detach(struct xnintr *intr)
Detach an interrupt descriptor.
Definition: intr.c:866
Select ouput buffer availability events.
Definition: driver.h:113
ipipe_spinlock_t rtdm_lock_t
Lock variable.
Definition: driver.h:528
void xnthread_cancel(struct xnthread *thread)
Cancel a thread.
Definition: thread.c:1479
#define XNCANCELD
Cancellation request is pending.
Definition: thread.h:72
int rtdm_rw_user_ok(struct rtdm_fd *fd, const void __user *ptr, size_t size)
Check if read/write access to user-space memory block is safe.
int rtdm_munmap(void *ptr, size_t len)
Unmap a user memory range.
Definition: drvlib.c:2000
int rtdm_task_sleep_abs(nanosecs_abs_t wakeup_time, enum rtdm_timer_mode mode)
Sleep until a specified absolute time.
static void * rtdm_fd_to_private(struct rtdm_fd *fd)
Locate the driver private area associated to a device context structure.
Definition: driver.h:158
static void rtdm_lock_put(rtdm_lock_t *lock)
Release lock without preemption restoration.
Definition: driver.h:566
int64_t nanosecs_rel_t
RTDM type for representing relative intervals.
Definition: rtdm.h:49
struct rtdm_profile_info profile_info
Class profile information.
Definition: driver.h:251
Adjustable timer with absolute timeout.
Definition: driver.h:930
void rtdm_timer_stop_in_handler(rtdm_timer_t *timer)
Stop a timer from inside a timer handler.
RTDM file operation descriptor.
Definition: fd.h:243
int(* rtdm_irq_handler_t)(rtdm_irq_t *irq_handle)
Interrupt handler.
Definition: driver.h:801
char dev_private[0]
Begin of driver defined context data structure.
Definition: driver.h:141
int rtdm_ratelimit(struct rtdm_ratelimit_state *rs, const char *func)
Enforces a rate limit.
Definition: drvlib.c:2022
void xnintr_disable(struct xnintr *intr)
Disable an interrupt line.
Definition: intr.c:923
size_t context_size
Size of driver defined appendix to struct rtdm_dev_context.
Definition: driver.h:258
int rtdm_timer_start(rtdm_timer_t *timer, nanosecs_abs_t expiry, nanosecs_rel_t interval, enum rtdm_timer_mode mode)
Start a timer.
Definition: drvlib.c:460
int rtdm_dev_register(struct rtdm_device *device)
Register a RTDM device.
Definition: device.c:357
#define XNTIMEO
Woken up due to a timeout condition.
Definition: thread.h:66
void rtdm_task_join(rtdm_task_t *task)
Wait on a real-time task to terminate.
Definition: drvlib.c:364
int(* start)(struct rtdm_driver *drv)
Handler called upon transition to COBALT_STATE_WARMUP.
Definition: driver.h:234
Select exceptional events.
Definition: driver.h:116
int rtdm_sem_select(rtdm_sem_t *sem, rtdm_selector_t *selector, enum rtdm_selecttype type, unsigned int fd_index)
Bind a selector to a semaphore.
Definition: drvlib.c:1102
void * device_data
Driver definable device data.
Definition: driver.h:325
static struct rtdm_device * rtdm_fd_device(struct rtdm_fd *fd)
Locate a device structure from a file descriptor.
Definition: driver.h:199
int rtdm_strncpy_from_user(struct rtdm_fd *fd, char *dst, const char __user *src, size_t count)
Copy user-space string to specified buffer.
void * rtdm_malloc(size_t size)
Allocate memory block.
int rtdm_task_sleep(nanosecs_rel_t delay)
Sleep a specified amount of time.
int rtdm_irq_request(rtdm_irq_t *irq_handle, unsigned int irq_no, rtdm_irq_handler_t handler, unsigned long flags, const char *device_name, void *arg)
Register an interrupt handler.
Definition: drvlib.c:1327
void rtdm_timer_destroy(rtdm_timer_t *timer)
Destroy a timer.
Definition: drvlib.c:430
int rtdm_event_wait(rtdm_event_t *event)
Wait on event occurrence.
Definition: drvlib.c:730
int rtdm_mutex_lock(rtdm_mutex_t *mutex)
Request a mutex.
Definition: drvlib.c:1212
void rtdm_event_pulse(rtdm_event_t *event)
Signal an event occurrence to currently listening waiters.
Definition: drvlib.c:670
Monotonic timer with absolute timeout.
Definition: driver.h:927
void rtdm_toseq_init(rtdm_toseq_t *timeout_seq, nanosecs_rel_t timeout)
Initialise a timeout sequence.
Definition: drvlib.c:600
int rtdm_sem_timeddown(rtdm_sem_t *sem, nanosecs_rel_t timeout, rtdm_toseq_t *timeout_seq)
Decrement a semaphore with timeout.
Definition: drvlib.c:1001
rtdm_task_t * rtdm_task_current(void)
Get current real-time task.
void rtdm_nrtsig_pend(rtdm_nrtsig_t *nrt_sig)
Trigger non-real-time signal.
Definition: drvlib.c:1475
int xnsynch_destroy(struct xnsynch *synch)
Destroy a synchronization object.
Definition: synch.c:117
static struct xnthread * xnthread_current(void)
Retrieve the current Cobalt core TCB.
Definition: thread.h:373
int rtdm_irq_enable(rtdm_irq_t *irq_handle)
Enable interrupt line.
static bool rtdm_fd_is_user(struct rtdm_fd *fd)
Tell whether the passed file descriptor belongs to an application.
Definition: driver.h:186
#define spltest()
Test hard interrupt state on the local processor.
Definition: lock.h:64
void rtdm_waitqueue_destroy(struct rtdm_waitqueue *wq)
Deletes a RTDM wait queue.
const char * label
Device label template for composing the device name.
Definition: driver.h:337
RTDM state management handlers.
Definition: driver.h:232
int rtdm_event_select(rtdm_event_t *event, rtdm_selector_t *selector, enum rtdm_selecttype type, unsigned int fd_index)
Bind a selector to an event.
Definition: drvlib.c:869
int xnthread_wait_period(unsigned long *overruns_r)
Wait for the next periodic release point.
Definition: thread.c:1346
uint64_t nanosecs_abs_t
RTDM type for representing absolute dates.
Definition: rtdm.h:43
int rtdm_copy_from_user(struct rtdm_fd *fd, void *dst, const void __user *src, size_t size)
Copy user-space memory block to specified buffer.
void rtdm_dev_unregister(struct rtdm_device *device)
Unregister a RTDM device.
Definition: device.c:476
void rtdm_timer_stop(rtdm_timer_t *timer)
Stop a timer.
Definition: drvlib.c:482
int xntimer_start(struct xntimer *timer, xnticks_t value, xnticks_t interval, xntmode_t mode)
Arm a timer.
Definition: timer.c:100
static void rtdm_lock_init(rtdm_lock_t *lock)
Dynamic lock initialisation.
Definition: driver.h:540
int rtdm_irq_disable(rtdm_irq_t *irq_handle)
Disable interrupt line.
rtdm_selecttype
Definition: driver.h:108
int rtdm_copy_to_user(struct rtdm_fd *fd, void __user *dst, const void *src, size_t size)
Copy specified buffer to user-space memory block.
void rtdm_event_destroy(rtdm_event_t *event)
Destroy an event.
Definition: drvlib.c:651
void rtdm_sem_init(rtdm_sem_t *sem, unsigned long value)
Initialise a semaphore.
Definition: drvlib.c:910
Select input data availability events.
Definition: driver.h:110
nanosecs_abs_t rtdm_clock_read(void)
Get system time.
int xnthread_set_periodic(struct xnthread *thread, xnticks_t idate, xntmode_t timeout_mode, xnticks_t period)
Make a thread periodic.
Definition: thread.c:1265
int device_flags
Device flags, see Device Flags for details.
Definition: driver.h:256
struct rtdm_driver * driver
Device driver.
Definition: driver.h:323
int rtdm_task_unblock(rtdm_task_t *task)
Activate a blocked real-time task.
struct rtdm_device * device
Set of active device operation handlers.
Definition: driver.h:138
void rtdm_event_signal(rtdm_event_t *event)
Signal an event occurrence.
Definition: drvlib.c:688
int xnsynch_sleep_on(struct xnsynch *synch, xnticks_t timeout, xntmode_t timeout_mode)
Sleep on an ownerless synchronization object.
Definition: synch.c:162
int rtdm_task_wait_period(void)
Wait on next real-time task period.
void rtdm_mutex_init(rtdm_mutex_t *mutex)
Initialise a mutex.
Definition: drvlib.c:1146
nanosecs_abs_t rtdm_clock_read_monotonic(void)
Get monotonic time.
int rtdm_rt_capable(struct rtdm_fd *fd)
Test if the caller is capable of running in real-time context.
#define XNBREAK
Forcibly awaken from a wait state.
Definition: thread.h:68
Copyright © 2011 Gilles Chanteperdrix gilles.chanteperdrix@xenomai.org.
Definition: atomic.h:24
int rtdm_task_sleep_until(nanosecs_abs_t wakeup_time)
Sleep until a specified absolute time.
int rtdm_safe_copy_to_user(struct rtdm_fd *fd, void __user *dst, const void *src, size_t size)
Check if read/write access to user-space memory block is safe and copy specified buffer to it...
static void xntimer_stop(struct xntimer *timer)
Disarm a timer.
Definition: timer.h:453
int rtdm_irq_free(rtdm_irq_t *irq_handle)
Release an interrupt handler.
int xnthread_unblock(struct xnthread *thread)
Unblock a thread.
Definition: thread.c:1178
int device_count
Count of devices this driver manages.
Definition: driver.h:271
void rtdm_event_init(rtdm_event_t *event, unsigned long pending)
Initialise an event.
Definition: drvlib.c:625
void rtdm_free(void *ptr)
Release real-time memory block.
int rtdm_mmap_vmem(struct vm_area_struct *vma, void *va)
Map a virtual memory range to a virtual user area.
Definition: drvlib.c:1951
int xnthread_join(struct xnthread *thread, bool uninterruptible)
Join with a terminated thread.
Definition: thread.c:1565
int rtdm_in_rt_context(void)
Test if running in a real-time task.
int rtdm_task_should_stop(void)
Check for pending termination request.
rtdm_timer_mode
Definition: driver.h:922
int rtdm_timer_start_in_handler(rtdm_timer_t *timer, nanosecs_abs_t expiry, nanosecs_rel_t interval, enum rtdm_timer_mode mode)
Start a timer from inside a timer handler.
void rtdm_task_set_priority(rtdm_task_t *task, int priority)
Adjust real-time task priority.
void rtdm_schedule_nrt_work(struct work_struct *lostage_work)
Put a work task in Linux non real-time global workqueue from primary mode.
Definition: drvlib.c:1506
void rtdm_waitqueue_init(struct rtdm_waitqueue *wq)
Initialize a RTDM wait queue.
int rtdm_event_timedwait(rtdm_event_t *event, nanosecs_rel_t timeout, rtdm_toseq_t *timeout_seq)
Wait on event occurrence with timeout.
Definition: drvlib.c:768
void rtdm_task_destroy(rtdm_task_t *task)
Destroy a real-time task.
int rtdm_mutex_timedlock(rtdm_mutex_t *mutex, nanosecs_rel_t timeout, rtdm_toseq_t *timeout_seq)
Request a mutex with timeout.
Definition: drvlib.c:1246
#define XNRMID
Pending on a removed resource.
Definition: thread.h:67
int protocol_family
Protocol device identification: protocol family (PF_xxx)
Definition: driver.h:260
int rtdm_mmap_to_user(struct rtdm_fd *fd, void *src_addr, size_t len, int prot, void **pptr, struct vm_operations_struct *vm_ops, void *vm_private_data)
Map a kernel memory range into the address space of the user.
Definition: drvlib.c:1813
Device context.
Definition: driver.h:133
static int xnsched_run(void)
The rescheduling procedure.
Definition: sched.h:291
void rtdm_sem_destroy(rtdm_sem_t *sem)
Destroy a semaphore.
Definition: drvlib.c:935
void rtdm_sem_up(rtdm_sem_t *sem)
Increment a semaphore.
Definition: drvlib.c:1060
Monotonic timer with relative timeout.
Definition: driver.h:924
int rtdm_iomap_to_user(struct rtdm_fd *fd, phys_addr_t src_addr, size_t len, int prot, void **pptr, struct vm_operations_struct *vm_ops, void *vm_private_data)
Map an I/O memory range into the address space of the user.
Definition: drvlib.c:1882
int rtdm_task_set_period(rtdm_task_t *task, nanosecs_rel_t period)
Adjust real-time task period.
struct rtdm_sm_ops smops
State management handlers.
Definition: driver.h:266
RTDM device.
Definition: driver.h:321
void(* rtdm_timer_handler_t)(rtdm_timer_t *timer)
Timer handler.
Definition: driver.h:915
int rtdm_mmap_kmem(struct vm_area_struct *vma, void *va)
Map a kernel logical memory range to a virtual user area.
Definition: drvlib.c:1926
int minor
Minor number of the device.
Definition: driver.h:352
int xnthread_set_schedparam(struct xnthread *thread, struct xnsched_class *sched_class, const union xnsched_policy_param *sched_param)
Change the base scheduling parameters of a thread.
Definition: thread.c:1775
static struct rtdm_fd * rtdm_private_to_fd(void *dev_private)
Locate a device file descriptor structure from its driver private area.
Definition: driver.h:171
int rtdm_sem_down(rtdm_sem_t *sem)
Decrement a semaphore.
Definition: drvlib.c:963
int rtdm_task_init(rtdm_task_t *task, const char *name, rtdm_task_proc_t task_proc, void *arg, int priority, nanosecs_rel_t period)
Initialise and start a real-time task.
Definition: drvlib.c:107
unsigned long rtdm_lockctx_t
Variable to save the context while holding a lock.
Definition: driver.h:531
void rtdm_event_clear(rtdm_event_t *event)
Clear event state.
Definition: drvlib.c:831
int rtdm_read_user_ok(struct rtdm_fd *fd, const void __user *ptr, size_t size)
Check if read access to user-space memory block is safe.
RTDM driver.
Definition: driver.h:245
int rtdm_mmap_iomem(struct vm_area_struct *vma, phys_addr_t pa)
Map an I/O memory range to a virtual user area.
Definition: drvlib.c:1979
int(* stop)(struct rtdm_driver *drv)
Handler called upon transition to COBALT_STATE_TEARDOWN.
Definition: driver.h:236
static void rtdm_lock_get(rtdm_lock_t *lock)
Acquire lock from non-preemptible contexts.
Definition: driver.h:552
void rtdm_nrtsig_destroy(rtdm_nrtsig_t *nrt_sig)
Release a non-realtime signal handler.
int rtdm_safe_copy_from_user(struct rtdm_fd *fd, void *dst, const void __user *src, size_t size)
Check if read access to user-space memory block and copy it to specified buffer.