Xenomai  3.1
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 
97 #define RTDM_MAX_MINOR 4096
98 
113  RTDM_SELECTTYPE_READ = XNSELECT_READ,
114 
116  RTDM_SELECTTYPE_WRITE = XNSELECT_WRITE,
117 
119  RTDM_SELECTTYPE_EXCEPT = XNSELECT_EXCEPT
120 };
137  struct rtdm_fd fd;
138 
142 
144  char dev_private[0];
145 };
146 
147 static inline struct rtdm_dev_context *rtdm_fd_to_context(struct rtdm_fd *fd)
148 {
149  return container_of(fd, struct rtdm_dev_context, fd);
150 }
151 
161 static inline void *rtdm_fd_to_private(struct rtdm_fd *fd)
162 {
163  return &rtdm_fd_to_context(fd)->dev_private[0];
164 }
165 
174 static inline struct rtdm_fd *rtdm_private_to_fd(void *dev_private)
175 {
176  struct rtdm_dev_context *ctx;
177  ctx = container_of(dev_private, struct rtdm_dev_context, dev_private);
178  return &ctx->fd;
179 }
180 
189 static inline bool rtdm_fd_is_user(struct rtdm_fd *fd)
190 {
191  return rtdm_fd_owner(fd) != &cobalt_kernel_ppd;
192 }
193 
202 static inline struct rtdm_device *rtdm_fd_device(struct rtdm_fd *fd)
203 {
204  return rtdm_fd_to_context(fd)->device;
205 }
206 
217  const char *name;
219  int class_id;
224  int version;
226  unsigned int magic;
227  struct module *owner;
228  struct class *kdev_class;
229 };
230 
231 struct rtdm_driver;
232 
236 struct rtdm_sm_ops {
238  int (*start)(struct rtdm_driver *drv);
240  int (*stop)(struct rtdm_driver *drv);
241 };
242 
249 struct rtdm_driver {
269  size_t context_size;
275  struct rtdm_fd_ops ops;
286  struct {
287  union {
288  struct {
289  struct cdev cdev;
290  int major;
291  } named;
292  };
293  atomic_t refcount;
294  struct notifier_block nb_statechange;
295  DECLARE_BITMAP(minor_map, RTDM_MAX_MINOR);
296  };
297 };
298 
299 #define RTDM_CLASS_MAGIC 0x8284636c
300 
319 #define RTDM_PROFILE_INFO(__name, __id, __subid, __version) \
320 { \
321  .name = ( # __name ), \
322  .class_id = (__id), \
323  .subclass_id = (__subid), \
324  .version = (__version), \
325  .magic = ~RTDM_CLASS_MAGIC, \
326  .owner = THIS_MODULE, \
327  .kdev_class = NULL, \
328 }
329 
330 int rtdm_drv_set_sysclass(struct rtdm_driver *drv, struct class *cls);
331 
338 struct rtdm_device {
342  void *device_data;
354  const char *label;
369  int minor;
371  struct {
372  unsigned int magic;
373  char *name;
374  union {
375  struct {
376  xnhandle_t handle;
377  } named;
378  struct {
379  struct xnid id;
380  } proto;
381  };
382  dev_t rdev;
383  struct device *kdev;
384  struct class *kdev_class;
385  atomic_t refcount;
386  struct rtdm_fd_ops ops;
387  wait_queue_head_t putwq;
388  struct list_head openfd_list;
389  };
390 };
391 
392 /* --- device registration --- */
393 
394 int rtdm_dev_register(struct rtdm_device *device);
395 
396 void rtdm_dev_unregister(struct rtdm_device *device);
397 
398 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
399 
400 static inline struct device *rtdm_dev_to_kdev(struct rtdm_device *device)
401 {
402  return device->kdev;
403 }
404 
405 /* --- clock services --- */
406 static inline nanosecs_abs_t rtdm_clock_read(void)
407 {
408  return xnclock_read_realtime(&nkclock);
409 }
410 
411 static inline nanosecs_abs_t rtdm_clock_read_monotonic(void)
412 {
413  return xnclock_read_monotonic(&nkclock);
414 }
415 #endif /* !DOXYGEN_CPP */
416 
417 /* --- timeout sequences */
418 
419 typedef nanosecs_abs_t rtdm_toseq_t;
420 
421 void rtdm_toseq_init(rtdm_toseq_t *timeout_seq, nanosecs_rel_t timeout);
422 
458 #define cobalt_atomic_enter(__context) \
459  do { \
460  xnlock_get_irqsave(&nklock, (__context)); \
461  xnsched_lock(); \
462  } while (0)
463 
476 #define cobalt_atomic_leave(__context) \
477  do { \
478  xnsched_unlock(); \
479  xnlock_put_irqrestore(&nklock, (__context)); \
480  } while (0)
481 
510 #ifdef DOXYGEN_CPP /* Beautify doxygen output */
511 #define RTDM_EXECUTE_ATOMICALLY(code_block) \
512 { \
513  <ENTER_ATOMIC_SECTION> \
514  code_block; \
515  <LEAVE_ATOMIC_SECTION> \
516 }
517 #else /* This is how it really works */
518 static inline __attribute__((deprecated)) void
519 rtdm_execute_atomically(void) { }
520 
521 #define RTDM_EXECUTE_ATOMICALLY(code_block) \
522 { \
523  spl_t __rtdm_s; \
524  \
525  rtdm_execute_atomically(); \
526  xnlock_get_irqsave(&nklock, __rtdm_s); \
527  xnsched_lock(); \
528  code_block; \
529  xnsched_unlock(); \
530  xnlock_put_irqrestore(&nklock, __rtdm_s); \
531 }
532 #endif
533 
544 #define RTDM_LOCK_UNLOCKED(__name) IPIPE_SPIN_LOCK_UNLOCKED
546 #define DEFINE_RTDM_LOCK(__name) \
547  rtdm_lock_t __name = RTDM_LOCK_UNLOCKED(__name)
548 
550 typedef ipipe_spinlock_t rtdm_lock_t;
553 typedef unsigned long rtdm_lockctx_t;
562 static inline void rtdm_lock_init(rtdm_lock_t *lock)
563 {
564  raw_spin_lock_init(lock);
565 }
566 
574 static inline void rtdm_lock_get(rtdm_lock_t *lock)
575 {
576  XENO_BUG_ON(COBALT, !spltest());
577  raw_spin_lock(lock);
578  xnsched_lock();
579 }
580 
588 static inline void rtdm_lock_put(rtdm_lock_t *lock)
589 {
590  raw_spin_unlock(lock);
591  xnsched_unlock();
592 }
593 
602 #define rtdm_lock_get_irqsave(__lock, __context) \
603  ((__context) = __rtdm_lock_get_irqsave(__lock))
604 
605 static inline rtdm_lockctx_t __rtdm_lock_get_irqsave(rtdm_lock_t *lock)
606 {
607  rtdm_lockctx_t context;
608 
609  context = ipipe_test_and_stall_head();
610  raw_spin_lock(lock);
611  xnsched_lock();
612 
613  return context;
614 }
615 
624 static inline
626 {
627  raw_spin_unlock(lock);
628  xnsched_unlock();
629  ipipe_restore_head(context);
630 }
639 #define rtdm_lock_irqsave(__context) \
640  splhigh(__context)
641 
649 #define rtdm_lock_irqrestore(__context) \
650  splexit(__context)
651 
654 #ifndef DOXYGEN_CPP
655 
656 struct rtdm_waitqueue {
657  struct xnsynch wait;
658 };
659 typedef struct rtdm_waitqueue rtdm_waitqueue_t;
660 
661 #define RTDM_WAITQUEUE_INITIALIZER(__name) { \
662  .wait = XNSYNCH_WAITQUEUE_INITIALIZER((__name).wait), \
663  }
664 
665 #define DEFINE_RTDM_WAITQUEUE(__name) \
666  struct rtdm_waitqueue __name = RTDM_WAITQUEUE_INITIALIZER(__name)
667 
668 #define DEFINE_RTDM_WAITQUEUE_ONSTACK(__name) \
669  DEFINE_RTDM_WAITQUEUE(__name)
670 
671 static inline void rtdm_waitqueue_init(struct rtdm_waitqueue *wq)
672 {
673  *wq = (struct rtdm_waitqueue)RTDM_WAITQUEUE_INITIALIZER(*wq);
674 }
675 
676 static inline void rtdm_waitqueue_destroy(struct rtdm_waitqueue *wq)
677 {
678  xnsynch_destroy(&wq->wait);
679 }
680 
681 static inline int __rtdm_dowait(struct rtdm_waitqueue *wq,
682  nanosecs_rel_t timeout, xntmode_t timeout_mode)
683 {
684  int ret;
685 
686  ret = xnsynch_sleep_on(&wq->wait, timeout, timeout_mode);
687  if (ret & XNBREAK)
688  return -EINTR;
689  if (ret & XNTIMEO)
690  return -ETIMEDOUT;
691  if (ret & XNRMID)
692  return -EIDRM;
693  return 0;
694 }
695 
696 static inline int __rtdm_timedwait(struct rtdm_waitqueue *wq,
697  nanosecs_rel_t timeout, rtdm_toseq_t *toseq)
698 {
699  if (toseq && timeout > 0)
700  return __rtdm_dowait(wq, *toseq, XN_ABSOLUTE);
701 
702  return __rtdm_dowait(wq, timeout, XN_RELATIVE);
703 }
704 
705 #define rtdm_timedwait_condition_locked(__wq, __cond, __timeout, __toseq) \
706  ({ \
707  int __ret = 0; \
708  while (__ret == 0 && !(__cond)) \
709  __ret = __rtdm_timedwait(__wq, __timeout, __toseq); \
710  __ret; \
711  })
712 
713 #define rtdm_wait_condition_locked(__wq, __cond) \
714  ({ \
715  int __ret = 0; \
716  while (__ret == 0 && !(__cond)) \
717  __ret = __rtdm_dowait(__wq, \
718  XN_INFINITE, XN_RELATIVE); \
719  __ret; \
720  })
721 
722 #define rtdm_timedwait_condition(__wq, __cond, __timeout, __toseq) \
723  ({ \
724  spl_t __s; \
725  int __ret; \
726  xnlock_get_irqsave(&nklock, __s); \
727  __ret = rtdm_timedwait_condition_locked(__wq, __cond, \
728  __timeout, __toseq); \
729  xnlock_put_irqrestore(&nklock, __s); \
730  __ret; \
731  })
732 
733 #define rtdm_timedwait(__wq, __timeout, __toseq) \
734  __rtdm_timedwait(__wq, __timeout, __toseq)
735 
736 #define rtdm_timedwait_locked(__wq, __timeout, __toseq) \
737  rtdm_timedwait(__wq, __timeout, __toseq)
738 
739 #define rtdm_wait_condition(__wq, __cond) \
740  ({ \
741  spl_t __s; \
742  int __ret; \
743  xnlock_get_irqsave(&nklock, __s); \
744  __ret = rtdm_wait_condition_locked(__wq, __cond); \
745  xnlock_put_irqrestore(&nklock, __s); \
746  __ret; \
747  })
748 
749 #define rtdm_wait(__wq) \
750  __rtdm_dowait(__wq, XN_INFINITE, XN_RELATIVE)
751 
752 #define rtdm_wait_locked(__wq) rtdm_wait(__wq)
753 
754 #define rtdm_waitqueue_lock(__wq, __context) cobalt_atomic_enter(__context)
755 
756 #define rtdm_waitqueue_unlock(__wq, __context) cobalt_atomic_leave(__context)
757 
758 #define rtdm_waitqueue_signal(__wq) \
759  ({ \
760  struct xnthread *__waiter; \
761  __waiter = xnsynch_wakeup_one_sleeper(&(__wq)->wait); \
762  xnsched_run(); \
763  __waiter != NULL; \
764  })
765 
766 #define __rtdm_waitqueue_flush(__wq, __reason) \
767  ({ \
768  int __ret; \
769  __ret = xnsynch_flush(&(__wq)->wait, __reason); \
770  xnsched_run(); \
771  __ret == XNSYNCH_RESCHED; \
772  })
773 
774 #define rtdm_waitqueue_broadcast(__wq) \
775  __rtdm_waitqueue_flush(__wq, 0)
776 
777 #define rtdm_waitqueue_flush(__wq) \
778  __rtdm_waitqueue_flush(__wq, XNBREAK)
779 
780 #define rtdm_waitqueue_wakeup(__wq, __waiter) \
781  do { \
782  xnsynch_wakeup_this_sleeper(&(__wq)->wait, __waiter); \
783  xnsched_run(); \
784  } while (0)
785 
786 #define rtdm_for_each_waiter(__pos, __wq) \
787  xnsynch_for_each_sleeper(__pos, &(__wq)->wait)
788 
789 #define rtdm_for_each_waiter_safe(__pos, __tmp, __wq) \
790  xnsynch_for_each_sleeper_safe(__pos, __tmp, &(__wq)->wait)
791 
792 #endif /* !DOXYGEN_CPP */
793 
796 /* --- Interrupt management services --- */
802 typedef struct xnintr rtdm_irq_t;
803 
810 #define RTDM_IRQTYPE_SHARED XN_IRQTYPE_SHARED
811 
813 #define RTDM_IRQTYPE_EDGE XN_IRQTYPE_EDGE
814 
823 typedef int (*rtdm_irq_handler_t)(rtdm_irq_t *irq_handle);
824 
831 #define RTDM_IRQ_NONE XN_IRQ_NONE
832 
833 #define RTDM_IRQ_HANDLED XN_IRQ_HANDLED
834 
835 #define RTDM_IRQ_DISABLE XN_IRQ_DISABLE
836 
849 #define rtdm_irq_get_arg(irq_handle, type) ((type *)irq_handle->cookie)
850 
852 int rtdm_irq_request(rtdm_irq_t *irq_handle, unsigned int irq_no,
853  rtdm_irq_handler_t handler, unsigned long flags,
854  const char *device_name, void *arg);
855 
856 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
857 static inline int rtdm_irq_free(rtdm_irq_t *irq_handle)
858 {
859  if (!XENO_ASSERT(COBALT, xnsched_root_p()))
860  return -EPERM;
861  xnintr_detach(irq_handle);
862  return 0;
863 }
864 
865 static inline int rtdm_irq_enable(rtdm_irq_t *irq_handle)
866 {
867  xnintr_enable(irq_handle);
868  return 0;
869 }
870 
871 static inline int rtdm_irq_disable(rtdm_irq_t *irq_handle)
872 {
873  xnintr_disable(irq_handle);
874  return 0;
875 }
876 #endif /* !DOXYGEN_CPP */
877 
878 /* --- non-real-time signalling services --- */
879 
885 typedef struct rtdm_nrtsig rtdm_nrtsig_t;
896 typedef void (*rtdm_nrtsig_handler_t)(rtdm_nrtsig_t *nrt_sig, void *arg);
897 
898 struct rtdm_nrtsig {
899  rtdm_nrtsig_handler_t handler;
900  void *arg;
901 };
902 
903 void rtdm_schedule_nrt_work(struct work_struct *lostage_work);
906 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
907 static inline void rtdm_nrtsig_init(rtdm_nrtsig_t *nrt_sig,
908  rtdm_nrtsig_handler_t handler, void *arg)
909 {
910  nrt_sig->handler = handler;
911  nrt_sig->arg = arg;
912 }
913 
914 static inline void rtdm_nrtsig_destroy(rtdm_nrtsig_t *nrt_sig)
915 {
916  nrt_sig->handler = NULL;
917  nrt_sig->arg = NULL;
918 }
919 
920 void rtdm_nrtsig_pend(rtdm_nrtsig_t *nrt_sig);
921 #endif /* !DOXYGEN_CPP */
922 
923 /* --- timer services --- */
924 
930 typedef struct xntimer rtdm_timer_t;
931 
937 typedef void (*rtdm_timer_handler_t)(rtdm_timer_t *timer);
938 
944 enum rtdm_timer_mode {
946  RTDM_TIMERMODE_RELATIVE = XN_RELATIVE,
947 
949  RTDM_TIMERMODE_ABSOLUTE = XN_ABSOLUTE,
950 
952  RTDM_TIMERMODE_REALTIME = XN_REALTIME
953 };
958 int rtdm_timer_init(rtdm_timer_t *timer, rtdm_timer_handler_t handler,
959  const char *name);
960 
961 void rtdm_timer_destroy(rtdm_timer_t *timer);
962 
963 int rtdm_timer_start(rtdm_timer_t *timer, nanosecs_abs_t expiry,
964  nanosecs_rel_t interval, enum rtdm_timer_mode mode);
965 
966 void rtdm_timer_stop(rtdm_timer_t *timer);
967 
968 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
969 static inline int rtdm_timer_start_in_handler(rtdm_timer_t *timer,
970  nanosecs_abs_t expiry,
971  nanosecs_rel_t interval,
972  enum rtdm_timer_mode mode)
973 {
974  return xntimer_start(timer, expiry, interval, (xntmode_t)mode);
975 }
976 
977 static inline void rtdm_timer_stop_in_handler(rtdm_timer_t *timer)
978 {
979  xntimer_stop(timer);
980 }
981 #endif /* !DOXYGEN_CPP */
982 
983 /* --- task services --- */
989 typedef struct xnthread rtdm_task_t;
990 
996 typedef void (*rtdm_task_proc_t)(void *arg);
997 
1002 #define RTDM_TASK_LOWEST_PRIORITY 0
1003 #define RTDM_TASK_HIGHEST_PRIORITY 99
1004 
1010 #define RTDM_TASK_RAISE_PRIORITY (+1)
1011 #define RTDM_TASK_LOWER_PRIORITY (-1)
1012 
1016 int rtdm_task_init(rtdm_task_t *task, const char *name,
1017  rtdm_task_proc_t task_proc, void *arg,
1018  int priority, nanosecs_rel_t period);
1019 int __rtdm_task_sleep(xnticks_t timeout, xntmode_t mode);
1021 
1022 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
1023 static inline void rtdm_task_destroy(rtdm_task_t *task)
1024 {
1025  xnthread_cancel(task);
1026  xnthread_join(task, true);
1027 }
1028 
1029 static inline int rtdm_task_should_stop(void)
1030 {
1031  return xnthread_test_info(xnthread_current(), XNCANCELD);
1032 }
1033 
1034 void rtdm_task_join(rtdm_task_t *task);
1035 
1036 static inline void __deprecated rtdm_task_join_nrt(rtdm_task_t *task,
1037  unsigned int poll_delay)
1038 {
1039  rtdm_task_join(task);
1040 }
1041 
1042 static inline void rtdm_task_set_priority(rtdm_task_t *task, int priority)
1043 {
1044  union xnsched_policy_param param = { .rt = { .prio = priority } };
1045  spl_t s;
1046 
1047  splhigh(s);
1048  xnthread_set_schedparam(task, &xnsched_class_rt, &param);
1049  xnsched_run();
1050  splexit(s);
1051 }
1052 
1053 static inline int rtdm_task_set_period(rtdm_task_t *task,
1054  nanosecs_abs_t start_date,
1055  nanosecs_rel_t period)
1056 {
1057  if (period < 0)
1058  period = 0;
1059  if (start_date == 0)
1060  start_date = XN_INFINITE;
1061 
1062  return xnthread_set_periodic(task, start_date, XN_ABSOLUTE, period);
1063 }
1064 
1065 static inline int rtdm_task_unblock(rtdm_task_t *task)
1066 {
1067  spl_t s;
1068  int res;
1069 
1070  splhigh(s);
1071  res = xnthread_unblock(task);
1072  xnsched_run();
1073  splexit(s);
1074 
1075  return res;
1076 }
1077 
1078 static inline rtdm_task_t *rtdm_task_current(void)
1079 {
1080  return xnthread_current();
1081 }
1082 
1083 static inline int rtdm_task_wait_period(unsigned long *overruns_r)
1084 {
1085  if (!XENO_ASSERT(COBALT, !xnsched_unblockable_p()))
1086  return -EPERM;
1087  return xnthread_wait_period(overruns_r);
1088 }
1089 
1090 static inline int rtdm_task_sleep(nanosecs_rel_t delay)
1091 {
1092  return __rtdm_task_sleep(delay, XN_RELATIVE);
1093 }
1094 
1095 static inline int
1096 rtdm_task_sleep_abs(nanosecs_abs_t wakeup_date, enum rtdm_timer_mode mode)
1097 {
1098  /* For the sake of a consistent API usage... */
1099  if (mode != RTDM_TIMERMODE_ABSOLUTE && mode != RTDM_TIMERMODE_REALTIME)
1100  return -EINVAL;
1101  return __rtdm_task_sleep(wakeup_date, (xntmode_t)mode);
1102 }
1103 
1104 /* rtdm_task_sleep_abs shall be used instead */
1105 static inline int __deprecated rtdm_task_sleep_until(nanosecs_abs_t wakeup_time)
1106 {
1107  return __rtdm_task_sleep(wakeup_time, XN_REALTIME);
1108 }
1109 
1110 #define rtdm_task_busy_wait(__condition, __spin_ns, __sleep_ns) \
1111  ({ \
1112  __label__ done; \
1113  nanosecs_abs_t __end; \
1114  int __ret = 0; \
1115  for (;;) { \
1116  __end = rtdm_clock_read_monotonic() + __spin_ns; \
1117  for (;;) { \
1118  if (__condition) \
1119  goto done; \
1120  if (rtdm_clock_read_monotonic() >= __end) \
1121  break; \
1122  } \
1123  __ret = rtdm_task_sleep(__sleep_ns); \
1124  if (__ret) \
1125  break; \
1126  } \
1127  done: \
1128  __ret; \
1129  })
1130 
1131 #define rtdm_wait_context xnthread_wait_context
1132 
1133 static inline
1134 void rtdm_wait_complete(struct rtdm_wait_context *wc)
1135 {
1136  xnthread_complete_wait(wc);
1137 }
1138 
1139 static inline
1140 int rtdm_wait_is_completed(struct rtdm_wait_context *wc)
1141 {
1142  return xnthread_wait_complete_p(wc);
1143 }
1144 
1145 static inline void rtdm_wait_prepare(struct rtdm_wait_context *wc)
1146 {
1147  xnthread_prepare_wait(wc);
1148 }
1149 
1150 static inline
1151 struct rtdm_wait_context *rtdm_wait_get_context(rtdm_task_t *task)
1152 {
1153  return xnthread_get_wait_context(task);
1154 }
1155 
1156 #endif /* !DOXYGEN_CPP */
1157 
1158 /* --- event services --- */
1159 
1160 typedef struct rtdm_event {
1161  struct xnsynch synch_base;
1162  DECLARE_XNSELECT(select_block);
1163 } rtdm_event_t;
1164 
1165 #define RTDM_EVENT_PENDING XNSYNCH_SPARE1
1166 
1167 void rtdm_event_init(rtdm_event_t *event, unsigned long pending);
1168 int rtdm_event_select(rtdm_event_t *event, rtdm_selector_t *selector,
1169  enum rtdm_selecttype type, unsigned fd_index);
1170 int rtdm_event_wait(rtdm_event_t *event);
1171 int rtdm_event_timedwait(rtdm_event_t *event, nanosecs_rel_t timeout,
1172  rtdm_toseq_t *timeout_seq);
1173 void rtdm_event_signal(rtdm_event_t *event);
1174 
1175 void rtdm_event_clear(rtdm_event_t *event);
1176 
1177 void rtdm_event_pulse(rtdm_event_t *event);
1178 
1179 void rtdm_event_destroy(rtdm_event_t *event);
1180 
1181 /* --- semaphore services --- */
1182 
1183 typedef struct rtdm_sem {
1184  unsigned long value;
1185  struct xnsynch synch_base;
1186  DECLARE_XNSELECT(select_block);
1187 } rtdm_sem_t;
1188 
1189 void rtdm_sem_init(rtdm_sem_t *sem, unsigned long value);
1190 int rtdm_sem_select(rtdm_sem_t *sem, rtdm_selector_t *selector,
1191  enum rtdm_selecttype type, unsigned fd_index);
1192 int rtdm_sem_down(rtdm_sem_t *sem);
1193 int rtdm_sem_timeddown(rtdm_sem_t *sem, nanosecs_rel_t timeout,
1194  rtdm_toseq_t *timeout_seq);
1195 void rtdm_sem_up(rtdm_sem_t *sem);
1196 
1197 void rtdm_sem_destroy(rtdm_sem_t *sem);
1198 
1199 /* --- mutex services --- */
1200 
1201 typedef struct rtdm_mutex {
1202  struct xnsynch synch_base;
1203  atomic_t fastlock;
1204 } rtdm_mutex_t;
1205 
1206 void rtdm_mutex_init(rtdm_mutex_t *mutex);
1207 int rtdm_mutex_lock(rtdm_mutex_t *mutex);
1208 int rtdm_mutex_timedlock(rtdm_mutex_t *mutex, nanosecs_rel_t timeout,
1209  rtdm_toseq_t *timeout_seq);
1210 void rtdm_mutex_unlock(rtdm_mutex_t *mutex);
1211 void rtdm_mutex_destroy(rtdm_mutex_t *mutex);
1212 
1213 /* --- utility functions --- */
1214 
1215 #define rtdm_printk(format, ...) printk(format, ##__VA_ARGS__)
1216 
1217 #define rtdm_printk_ratelimited(fmt, ...) do { \
1218  if (xnclock_ratelimit()) \
1219  printk(fmt, ##__VA_ARGS__); \
1220 } while (0)
1221 
1222 #ifndef DOXYGEN_CPP /* Avoid static inline tags for RTDM in doxygen */
1223 static inline void *rtdm_malloc(size_t size)
1224 {
1225  return xnmalloc(size);
1226 }
1227 
1228 static inline void rtdm_free(void *ptr)
1229 {
1230  xnfree(ptr);
1231 }
1232 
1233 int rtdm_mmap_to_user(struct rtdm_fd *fd,
1234  void *src_addr, size_t len,
1235  int prot, void **pptr,
1236  struct vm_operations_struct *vm_ops,
1237  void *vm_private_data);
1238 
1239 int rtdm_iomap_to_user(struct rtdm_fd *fd,
1240  phys_addr_t src_addr, size_t len,
1241  int prot, void **pptr,
1242  struct vm_operations_struct *vm_ops,
1243  void *vm_private_data);
1244 
1245 int rtdm_mmap_kmem(struct vm_area_struct *vma, void *va);
1246 
1247 int rtdm_mmap_vmem(struct vm_area_struct *vma, void *va);
1248 
1249 int rtdm_mmap_iomem(struct vm_area_struct *vma, phys_addr_t pa);
1250 
1251 int rtdm_munmap(void *ptr, size_t len);
1252 
1253 static inline int rtdm_read_user_ok(struct rtdm_fd *fd,
1254  const void __user *ptr, size_t size)
1255 {
1256  return access_rok(ptr, size);
1257 }
1258 
1259 static inline int rtdm_rw_user_ok(struct rtdm_fd *fd,
1260  const void __user *ptr, size_t size)
1261 {
1262  return access_wok(ptr, size);
1263 }
1264 
1265 static inline int rtdm_copy_from_user(struct rtdm_fd *fd,
1266  void *dst, const void __user *src,
1267  size_t size)
1268 {
1269  return __xn_copy_from_user(dst, src, size) ? -EFAULT : 0;
1270 }
1271 
1272 static inline int rtdm_safe_copy_from_user(struct rtdm_fd *fd,
1273  void *dst, const void __user *src,
1274  size_t size)
1275 {
1276  return cobalt_copy_from_user(dst, src, size);
1277 }
1278 
1279 static inline int rtdm_copy_to_user(struct rtdm_fd *fd,
1280  void __user *dst, const void *src,
1281  size_t size)
1282 {
1283  return __xn_copy_to_user(dst, src, size) ? -EFAULT : 0;
1284 }
1285 
1286 static inline int rtdm_safe_copy_to_user(struct rtdm_fd *fd,
1287  void __user *dst, const void *src,
1288  size_t size)
1289 {
1290  return cobalt_copy_to_user(dst, src, size);
1291 }
1292 
1293 static inline int rtdm_strncpy_from_user(struct rtdm_fd *fd,
1294  char *dst,
1295  const char __user *src, size_t count)
1296 {
1297  return cobalt_strncpy_from_user(dst, src, count);
1298 }
1299 
1300 static inline bool rtdm_available(void)
1301 {
1302  return realtime_core_enabled();
1303 }
1304 
1305 static inline int rtdm_rt_capable(struct rtdm_fd *fd)
1306 {
1307  if (!XENO_ASSERT(COBALT, !xnsched_interrupt_p()))
1308  return 0;
1309 
1310  if (!rtdm_fd_is_user(fd))
1311  return !xnsched_root_p();
1312 
1313  return xnthread_current() != NULL;
1314 }
1315 
1316 static inline int rtdm_in_rt_context(void)
1317 {
1318  return (ipipe_current_domain != ipipe_root_domain);
1319 }
1320 
1321 #define RTDM_IOV_FASTMAX 16
1322 
1323 int rtdm_get_iovec(struct rtdm_fd *fd, struct iovec **iov,
1324  const struct user_msghdr *msg,
1325  struct iovec *iov_fast);
1326 
1327 int rtdm_put_iovec(struct rtdm_fd *fd, struct iovec *iov,
1328  const struct user_msghdr *msg,
1329  struct iovec *iov_fast);
1330 
1331 static inline
1332 void rtdm_drop_iovec(struct iovec *iov, struct iovec *iov_fast)
1333 {
1334  if (iov != iov_fast)
1335  xnfree(iov);
1336 }
1337 
1338 ssize_t rtdm_get_iov_flatlen(struct iovec *iov, int iovlen);
1339 
1340 #endif /* !DOXYGEN_CPP */
1341 
1342 #endif /* _COBALT_RTDM_DRIVER_H */
void(* rtdm_task_proc_t)(void *arg)
Real-time task procedure.
Definition: driver.h:1005
static void rtdm_lock_put_irqrestore(rtdm_lock_t *lock, rtdm_lockctx_t context)
Release lock and restore preemption state
Definition: driver.h:631
void rtdm_mutex_destroy(rtdm_mutex_t *mutex)
Destroy a mutex
Definition: drvlib.c:1314
struct rtdm_fd_ops ops
I/O operation handlers.
Definition: driver.h:275
void rtdm_task_busy_sleep(nanosecs_rel_t delay)
Busy-wait a specified amount of time
Definition: drvlib.c:502
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:273
void xnintr_enable(struct xnintr *intr)
Enable an interrupt line.
Definition: intr.c:929
void rtdm_mutex_unlock(rtdm_mutex_t *mutex)
Release a mutex
Definition: drvlib.c:1334
void(* rtdm_nrtsig_handler_t)(rtdm_nrtsig_t *nrt_sig, void *arg)
Non-real-time signal handler.
Definition: driver.h:905
void xnintr_detach(struct xnintr *intr)
Detach an interrupt descriptor.
Definition: intr.c:902
#define splexit(x)
Restore the saved hard interrupt state on the local processor.
Definition: lock.h:46
Select ouput buffer availability events.
Definition: driver.h:116
ipipe_spinlock_t rtdm_lock_t
Lock variable.
Definition: driver.h:551
void xnthread_cancel(struct xnthread *thread)
Cancel a thread.
Definition: thread.c:1541
#define XNCANCELD
Cancellation request is pending.
Definition: thread.h:73
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:2176
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:161
static void rtdm_lock_put(rtdm_lock_t *lock)
Release lock without preemption restoration
Definition: driver.h:592
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:255
int rtdm_drv_set_sysclass(struct rtdm_driver *drv, struct class *cls)
Set the kernel device class of a RTDM driver.
Definition: device.c:612
Adjustable timer with absolute timeout.
Definition: driver.h:961
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:244
int(* rtdm_irq_handler_t)(rtdm_irq_t *irq_handle)
Interrupt handler.
Definition: driver.h:831
char dev_private[0]
Begin of driver defined context data structure.
Definition: driver.h:144
RTDM profile information.
Definition: driver.h:215
void xnintr_disable(struct xnintr *intr)
Disable an interrupt line.
Definition: intr.c:961
size_t context_size
Size of the private memory area the core should automatically allocate for each open file descriptor...
Definition: driver.h:269
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:586
int rtdm_dev_register(struct rtdm_device *device)
Register a RTDM device
Definition: device.c:390
#define XNTIMEO
Woken up due to a timeout condition.
Definition: thread.h:67
void rtdm_task_join(rtdm_task_t *task)
Wait on a real-time task to terminate
Definition: drvlib.c:478
int(* start)(struct rtdm_driver *drv)
Handler called upon transition to COBALT_STATE_WARMUP.
Definition: driver.h:238
Select exceptional events.
Definition: driver.h:119
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:1250
void * device_data
Driver definable device data.
Definition: driver.h:342
static struct rtdm_device * rtdm_fd_device(struct rtdm_fd *fd)
Locate a device structure from a file descriptor.
Definition: driver.h:202
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:1484
void rtdm_timer_destroy(rtdm_timer_t *timer)
Destroy a timer
Definition: drvlib.c:555
int rtdm_event_wait(rtdm_event_t *event)
Wait on event occurrence
Definition: drvlib.c:867
int rtdm_mutex_lock(rtdm_mutex_t *mutex)
Request a mutex
Definition: drvlib.c:1365
void rtdm_event_pulse(rtdm_event_t *event)
Signal an event occurrence to currently listening waiters
Definition: drvlib.c:805
Monotonic timer with absolute timeout.
Definition: driver.h:958
void rtdm_wait_prepare(struct rtdm_wait_context *wc)
Register wait context.
void rtdm_toseq_init(rtdm_toseq_t *timeout_seq, nanosecs_rel_t timeout)
Initialise a timeout sequence
Definition: drvlib.c:730
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:1147
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:1641
int xnsynch_destroy(struct xnsynch *synch)
Destroy a synchronization object.
Definition: synch.c:160
static struct xnthread * xnthread_current(void)
Retrieve the current Cobalt core TCB.
Definition: thread.h:374
int rtdm_irq_enable(rtdm_irq_t *irq_handle)
Enable interrupt line
bool rtdm_available(void)
Test if the real-time core is available
static bool rtdm_fd_is_user(struct rtdm_fd *fd)
Tell whether the passed file descriptor belongs to an application.
Definition: driver.h:189
#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:354
RTDM state management handlers.
Definition: driver.h:236
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:1009
int xnthread_wait_period(unsigned long *overruns_r)
Wait for the next periodic release point.
Definition: thread.c:1406
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:538
#define splhigh(x)
Hard disable interrupts on the local processor, saving previous state.
Definition: lock.h:39
const char * name
Device class name.
Definition: driver.h:217
void rtdm_timer_stop(rtdm_timer_t *timer)
Stop a timer
Definition: drvlib.c:609
int xntimer_start(struct xntimer *timer, xnticks_t value, xnticks_t interval, xntmode_t mode)
Arm a timer.
Definition: timer.c:114
static void rtdm_lock_init(rtdm_lock_t *lock)
Dynamic lock initialisation
Definition: driver.h:564
int rtdm_irq_disable(rtdm_irq_t *irq_handle)
Disable interrupt line
rtdm_selecttype
Definition: driver.h:111
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:783
int rtdm_task_wait_period(unsigned long *overruns_r)
Wait on next real-time task period
void rtdm_sem_init(rtdm_sem_t *sem, unsigned long value)
Initialise a semaphore
Definition: drvlib.c:1051
Select input data availability events.
Definition: driver.h:113
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:1320
int device_flags
Device flags, see Device Flags for details.
Definition: driver.h:260
struct rtdm_driver * driver
Device driver.
Definition: driver.h:340
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:141
void rtdm_event_signal(rtdm_event_t *event)
Signal an event occurrence
Definition: drvlib.c:824
int __must_check xnsynch_sleep_on(struct xnsynch *synch, xnticks_t timeout, xntmode_t timeout_mode)
Sleep on an ownerless synchronization object.
Definition: synch.c:206
void rtdm_mutex_init(rtdm_mutex_t *mutex)
Initialise a mutex
Definition: drvlib.c:1295
unsigned int magic
Reserved.
Definition: driver.h:226
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:69
int class_id
Device class ID, see RTDM_CLASS_xxx.
Definition: driver.h:219
int version
Supported device profile version.
Definition: driver.h:224
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:472
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:1228
int rtdm_timer_init(rtdm_timer_t *timer, rtdm_timer_handler_t handler, const char *name)
Initialise a timer
Definition: drvlib.c:534
int base_minor
Base minor for named devices.
Definition: driver.h:284
int device_count
Count of devices this driver manages.
Definition: driver.h:282
void rtdm_event_init(rtdm_event_t *event, unsigned long pending)
Initialise an event
Definition: drvlib.c:756
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:2125
int rtdm_wait_is_completed(struct rtdm_wait_context *wc)
Test completion of a wait context.
int xnthread_join(struct xnthread *thread, bool uninterruptible)
Join with a terminated thread.
Definition: thread.c:1671
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:953
int rtdm_task_set_period(rtdm_task_t *task, nanosecs_abs_t start_date, nanosecs_rel_t period)
Adjust real-time task period
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:1672
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:906
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:1400
static int __attribute__((cold))
Test if a mutex structure contains a valid autoinitializer.
Definition: mutex.c:177
#define XNRMID
Pending on a removed resource.
Definition: thread.h:68
int protocol_family
Protocol device identification: protocol family (PF_xxx)
Definition: driver.h:271
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:1984
Device context.
Definition: driver.h:136
static int xnsched_run(void)
The rescheduling procedure.
Definition: sched.h:311
void rtdm_sem_destroy(rtdm_sem_t *sem)
Destroy a semaphore
Definition: drvlib.c:1077
void rtdm_sem_up(rtdm_sem_t *sem)
Increment a semaphore
Definition: drvlib.c:1207
Monotonic timer with relative timeout.
Definition: driver.h:955
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:2054
void rtdm_wait_complete(struct rtdm_wait_context *wc)
Mark completion for a wait context.
#define RTDM_MAX_MINOR
Maximum number of named devices per driver.
Definition: driver.h:97
struct rtdm_sm_ops smops
State management handlers.
Definition: driver.h:277
RTDM device.
Definition: driver.h:338
void(* rtdm_timer_handler_t)(rtdm_timer_t *timer)
Timer handler.
Definition: driver.h:946
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:2099
int minor
Minor number of the device.
Definition: driver.h:369
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:1847
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:174
int rtdm_sem_down(rtdm_sem_t *sem)
Decrement a semaphore
Definition: drvlib.c:1108
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:111
unsigned long rtdm_lockctx_t
Variable to save the context while holding a lock.
Definition: driver.h:554
void rtdm_event_clear(rtdm_event_t *event)
Clear event state
Definition: drvlib.c:970
int subclass_id
Device sub-class, see RTDM_SUBCLASS_xxx definition in the Device Profiles.
Definition: driver.h:222
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:249
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:2154
int(* stop)(struct rtdm_driver *drv)
Handler called upon transition to COBALT_STATE_TEARDOWN.
Definition: driver.h:240
static void rtdm_lock_get(rtdm_lock_t *lock)
Acquire lock from non-preemptible contexts
Definition: driver.h:577
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 ...