Continued update of master's rtdm part to xenomai 3 : ioctl context now obtained from rtdm_fd stable-1.5
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Tue, 11 Sep 2018 22:16:10 +0200
branchstable-1.5
changeset 2707 245d8592cfdc
parent 2706 43f8e40a183d
child 2708 742c70a97622
Continued update of master's rtdm part to xenomai 3 : ioctl context now obtained from rtdm_fd
master/rtdm.c
--- a/master/rtdm.c	Tue Sep 11 08:43:17 2018 +0200
+++ b/master/rtdm.c	Tue Sep 11 22:16:10 2018 +0200
@@ -44,20 +44,12 @@
  */
 #define DEBUG 0
 
-/****************************************************************************/
-
-/** Context structure for an open RTDM file handle.
- */
-typedef struct {
-    struct rtdm_fd *fd; /**< RTDM user data. */
-    ec_ioctl_context_t ioctl_ctx; /**< Context structure. */
-} ec_rtdm_context_t;
-
-/****************************************************************************/
-
-int ec_rtdm_open(struct rtdm_dev_context *, struct rtdm_fd *, int);
-int ec_rtdm_close(struct rtdm_dev_context *, struct rtdm_fd *);
-int ec_rtdm_ioctl(struct rtdm_dev_context *, struct rtdm_fd *,
+
+/****************************************************************************/
+
+int ec_rtdm_open(struct rtdm_fd *, int);
+void ec_rtdm_close(struct rtdm_fd *);
+int ec_rtdm_ioctl(struct rtdm_fd *,
         unsigned int, void __user *);
 
 /****************************************************************************/
@@ -69,10 +61,10 @@
 							  1),
 	.device_flags		=	RTDM_NAMED_DEVICE,
 	.device_count		=	1,
-	.context_size		=	sizeof(ec_rtdm_context_t),
+	.context_size		=	sizeof(ec_ioctl_context_t),
 	.ops = {
-        .open      = ec_rtdm_open;
-        .close_nrt = ec_rtdm_close,
+        .open      = ec_rtdm_open,
+        .close     = ec_rtdm_close,
         .ioctl_rt  = ec_rtdm_ioctl,
         .ioctl_nrt = ec_rtdm_ioctl
 	},
@@ -88,8 +80,7 @@
         )
 {
     int ret;
-
-    MODULE_VERSION
+    char* devlabel;
 
     rtdm_dev->master = master;
 
@@ -99,22 +90,22 @@
         return -ENOMEM;
     }
     
-    rtdm_dev->dev->label = kzalloc(RTDM_MAX_DEVNAME_LEN+1, GFP_KERNEL);
-    if (!rtdm_dev->dev->label) {
+    devlabel = kzalloc(RTDM_MAX_DEVNAME_LEN+1, GFP_KERNEL);
+    if (!devlabel) {
         EC_MASTER_ERR(master, "Failed to reserve memory for RTDM device name.\n");
         return -ENOMEM;
         kfree(rtdm_dev->dev);
     }
-    
-
+    snprintf(devlabel, RTDM_MAX_DEVNAME_LEN,
+            "EtherCAT%u", master->index);
+
+    rtdm_dev->dev->label = devlabel;
     rtdm_dev->dev->driver = &ec_rtdm_driver;
-    snprintf(rtdm_dev->dev->label, RTDM_MAX_DEVNAME_LEN,
-            "EtherCAT%u", master->index);
     rtdm_dev->dev->device_data = rtdm_dev; /* pointer to parent */
 
 
     EC_MASTER_INFO(master, "Registering RTDM device %s.\n",
-            rtdm_dev->dev->driver_name);
+            devlabel);
     ret = rtdm_dev_register(rtdm_dev->dev);
     if (ret) {
         EC_MASTER_ERR(master, "Initialization of RTDM interface failed"
@@ -134,15 +125,9 @@
         ec_rtdm_dev_t *rtdm_dev /**< EtherCAT RTDM device. */
         )
 {
-    int ret;
-
     EC_MASTER_INFO(rtdm_dev->master, "Unregistering RTDM device %s.\n",
             rtdm_dev->dev->label);
-    ret = rtdm_dev_unregister(rtdm_dev->dev, 1000 /* poll delay [ms] */);
-    if (ret < 0) {
-        EC_MASTER_WARN(rtdm_dev->master,
-                "Failed to unregister RTDM device (code %i).\n", ret);
-    }
+    rtdm_dev_unregister(rtdm_dev->dev);
 
     kfree(rtdm_dev->dev->label);
     kfree(rtdm_dev->dev);
@@ -155,21 +140,19 @@
  * \return Always zero (success).
  */
 int ec_rtdm_open(
-        struct rtdm_dev_context *context, /**< Context. */
         struct rtdm_fd *fd, /**< User data. */
         int oflags /**< Open flags. */
         )
 {
-    ec_rtdm_context_t *ctx = (ec_rtdm_context_t *) context->dev_private;
-#if DEBUG
-    ec_rtdm_dev_t *rtdm_dev = (ec_rtdm_dev_t *) context->device->device_data;
-#endif
-
-    ctx->fd = fd;
-    ctx->ioctl_ctx.writable = oflags & O_WRONLY || oflags & O_RDWR;
-    ctx->ioctl_ctx.requested = 0;
-    ctx->ioctl_ctx.process_data = NULL;
-    ctx->ioctl_ctx.process_data_size = 0;
+    ec_ioctl_context_t *ctx = (ec_ioctl_context_t *) rtdm_fd_to_private(fd);
+#if DEBUG
+    ec_rtdm_dev_t *rtdm_dev = (ec_rtdm_dev_t *) rtdm_fd_device(fd)->device_data;
+#endif
+
+    ctx->writable = oflags & O_WRONLY || oflags & O_RDWR;
+    ctx->requested = 0;
+    ctx->process_data = NULL;
+    ctx->process_data_size = 0;
 
 #if DEBUG
     EC_MASTER_INFO(rtdm_dev->master, "RTDM device %s opened.\n",
@@ -184,15 +167,14 @@
  *
  * \return Always zero (success).
  */
-int ec_rtdm_close(
-        struct rtdm_dev_context *context, /**< Context. */
+void ec_rtdm_close(
         struct rtdm_fd *fd /**< User data. */
         )
 {
-    ec_rtdm_context_t *ctx = (ec_rtdm_context_t *) context->dev_private;
-    ec_rtdm_dev_t *rtdm_dev = (ec_rtdm_dev_t *) context->device->device_data;
-
-    if (ctx->ioctl_ctx.requested) {
+    ec_ioctl_context_t *ctx = (ec_ioctl_context_t *) rtdm_fd_to_private(fd);
+    ec_rtdm_dev_t *rtdm_dev = (ec_rtdm_dev_t *) rtdm_fd_device(fd)->device_data;
+
+    if (ctx->requested) {
         ecrt_release_master(rtdm_dev->master);
 	}
 
@@ -200,7 +182,6 @@
     EC_MASTER_INFO(rtdm_dev->master, "RTDM device %s closed.\n",
             context->device->device_name);
 #endif
-    return 0;
 }
 
 /****************************************************************************/
@@ -210,21 +191,20 @@
  * \return ioctl() return code.
  */
 int ec_rtdm_ioctl(
-        struct rtdm_dev_context *context, /**< Context. */
         struct rtdm_fd *fd, /**< User data. */
         unsigned int request, /**< Request. */
         void __user *arg /**< Argument. */
         )
 {
-    ec_rtdm_context_t *ctx = (ec_rtdm_context_t *) context->dev_private;
-    ec_rtdm_dev_t *rtdm_dev = (ec_rtdm_dev_t *) context->device->device_data;
+    ec_ioctl_context_t *ctx = (ec_ioctl_context_t *) rtdm_fd_to_private(fd);
+    ec_rtdm_dev_t *rtdm_dev = (ec_rtdm_dev_t *) rtdm_fd_device(fd)->device_data;
 
 #if DEBUG
     EC_MASTER_INFO(rtdm_dev->master, "ioctl(request = %u, ctl = %02x)"
             " on RTDM device %s.\n", request, _IOC_NR(request),
             context->device->device_name);
 #endif
-    return ec_ioctl_rtdm(rtdm_dev->master, &ctx->ioctl_ctx, request, arg);
+    return ec_ioctl_rtdm(rtdm_dev->master, ctx, request, arg);
 }
 
 /****************************************************************************/
@@ -234,16 +214,14 @@
  * \return Zero on success, otherwise a negative error code.
  */
 int ec_rtdm_mmap(
-        ec_ioctl_context_t *ioctl_ctx, /**< Context. */
+        ec_ioctl_context_t *ctx, /**< Context. */
         void **user_address /**< Userspace address. */
         )
 {
-    ec_rtdm_context_t *ctx =
-        container_of(ioctl_ctx, ec_rtdm_context_t, ioctl_ctx);
     int ret;
 
-    ret = rtdm_mmap_to_user(ctx->fd,
-            ioctl_ctx->process_data, ioctl_ctx->process_data_size,
+    ret = rtdm_mmap_to_user(rtdm_private_to_fd(ctx),
+            ctx->process_data, ctx->process_data_size,
             PROT_READ | PROT_WRITE,
             user_address,
             NULL, NULL);