Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *    HMC Drive CD/DVD Device
0004  *
0005  *    Copyright IBM Corp. 2013
0006  *    Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
0007  *
0008  *    This file provides a Linux "misc" character device for access to an
0009  *    assigned HMC drive CD/DVD-ROM. It works as follows: First create the
0010  *    device by calling hmcdrv_dev_init(). After open() a lseek(fd, 0,
0011  *    SEEK_END) indicates that a new FTP command follows (not needed on the
0012  *    first command after open). Then write() the FTP command ASCII string
0013  *    to it, e.g. "dir /" or "nls <directory>" or "get <filename>". At the
0014  *    end read() the response.
0015  */
0016 
0017 #define KMSG_COMPONENT "hmcdrv"
0018 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0019 
0020 #include <linux/kernel.h>
0021 #include <linux/module.h>
0022 #include <linux/slab.h>
0023 #include <linux/fs.h>
0024 #include <linux/cdev.h>
0025 #include <linux/miscdevice.h>
0026 #include <linux/device.h>
0027 #include <linux/capability.h>
0028 #include <linux/delay.h>
0029 #include <linux/uaccess.h>
0030 
0031 #include "hmcdrv_dev.h"
0032 #include "hmcdrv_ftp.h"
0033 
0034 /* If the following macro is defined, then the HMC device creates it's own
0035  * separated device class (and dynamically assigns a major number). If not
0036  * defined then the HMC device is assigned to the "misc" class devices.
0037  *
0038 #define HMCDRV_DEV_CLASS "hmcftp"
0039  */
0040 
0041 #define HMCDRV_DEV_NAME  "hmcdrv"
0042 #define HMCDRV_DEV_BUSY_DELAY    500 /* delay between -EBUSY trials in ms */
0043 #define HMCDRV_DEV_BUSY_RETRIES  3   /* number of retries on -EBUSY */
0044 
0045 struct hmcdrv_dev_node {
0046 
0047 #ifdef HMCDRV_DEV_CLASS
0048     struct cdev dev; /* character device structure */
0049     umode_t mode;    /* mode of device node (unused, zero) */
0050 #else
0051     struct miscdevice dev; /* "misc" device structure */
0052 #endif
0053 
0054 };
0055 
0056 static int hmcdrv_dev_open(struct inode *inode, struct file *fp);
0057 static int hmcdrv_dev_release(struct inode *inode, struct file *fp);
0058 static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence);
0059 static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
0060                    size_t len, loff_t *pos);
0061 static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
0062                 size_t len, loff_t *pos);
0063 static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
0064                    char __user *buf, size_t len);
0065 
0066 /*
0067  * device operations
0068  */
0069 static const struct file_operations hmcdrv_dev_fops = {
0070     .open = hmcdrv_dev_open,
0071     .llseek = hmcdrv_dev_seek,
0072     .release = hmcdrv_dev_release,
0073     .read = hmcdrv_dev_read,
0074     .write = hmcdrv_dev_write,
0075 };
0076 
0077 static struct hmcdrv_dev_node hmcdrv_dev; /* HMC device struct (static) */
0078 
0079 #ifdef HMCDRV_DEV_CLASS
0080 
0081 static struct class *hmcdrv_dev_class; /* device class pointer */
0082 static dev_t hmcdrv_dev_no; /* device number (major/minor) */
0083 
0084 /**
0085  * hmcdrv_dev_name() - provides a naming hint for a device node in /dev
0086  * @dev: device for which the naming/mode hint is
0087  * @mode: file mode for device node created in /dev
0088  *
0089  * See: devtmpfs.c, function devtmpfs_create_node()
0090  *
0091  * Return: recommended device file name in /dev
0092  */
0093 static char *hmcdrv_dev_name(struct device *dev, umode_t *mode)
0094 {
0095     char *nodename = NULL;
0096     const char *devname = dev_name(dev); /* kernel device name */
0097 
0098     if (devname)
0099         nodename = kasprintf(GFP_KERNEL, "%s", devname);
0100 
0101     /* on device destroy (rmmod) the mode pointer may be NULL
0102      */
0103     if (mode)
0104         *mode = hmcdrv_dev.mode;
0105 
0106     return nodename;
0107 }
0108 
0109 #endif  /* HMCDRV_DEV_CLASS */
0110 
0111 /*
0112  * open()
0113  */
0114 static int hmcdrv_dev_open(struct inode *inode, struct file *fp)
0115 {
0116     int rc;
0117 
0118     /* check for non-blocking access, which is really unsupported
0119      */
0120     if (fp->f_flags & O_NONBLOCK)
0121         return -EINVAL;
0122 
0123     /* Because it makes no sense to open this device read-only (then a
0124      * FTP command cannot be emitted), we respond with an error.
0125      */
0126     if ((fp->f_flags & O_ACCMODE) == O_RDONLY)
0127         return -EINVAL;
0128 
0129     /* prevent unloading this module as long as anyone holds the
0130      * device file open - so increment the reference count here
0131      */
0132     if (!try_module_get(THIS_MODULE))
0133         return -ENODEV;
0134 
0135     fp->private_data = NULL; /* no command yet */
0136     rc = hmcdrv_ftp_startup();
0137     if (rc)
0138         module_put(THIS_MODULE);
0139 
0140     pr_debug("open file '/dev/%pD' with return code %d\n", fp, rc);
0141     return rc;
0142 }
0143 
0144 /*
0145  * release()
0146  */
0147 static int hmcdrv_dev_release(struct inode *inode, struct file *fp)
0148 {
0149     pr_debug("closing file '/dev/%pD'\n", fp);
0150     kfree(fp->private_data);
0151     fp->private_data = NULL;
0152     hmcdrv_ftp_shutdown();
0153     module_put(THIS_MODULE);
0154     return 0;
0155 }
0156 
0157 /*
0158  * lseek()
0159  */
0160 static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence)
0161 {
0162     switch (whence) {
0163     case SEEK_CUR: /* relative to current file position */
0164         pos += fp->f_pos; /* new position stored in 'pos' */
0165         break;
0166 
0167     case SEEK_SET: /* absolute (relative to beginning of file) */
0168         break; /* SEEK_SET */
0169 
0170         /* We use SEEK_END as a special indicator for a SEEK_SET
0171          * (set absolute position), combined with a FTP command
0172          * clear.
0173          */
0174     case SEEK_END:
0175         if (fp->private_data) {
0176             kfree(fp->private_data);
0177             fp->private_data = NULL;
0178         }
0179 
0180         break; /* SEEK_END */
0181 
0182     default: /* SEEK_DATA, SEEK_HOLE: unsupported */
0183         return -EINVAL;
0184     }
0185 
0186     if (pos < 0)
0187         return -EINVAL;
0188 
0189     if (fp->f_pos != pos)
0190         ++fp->f_version;
0191 
0192     fp->f_pos = pos;
0193     return pos;
0194 }
0195 
0196 /*
0197  * transfer (helper function)
0198  */
0199 static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
0200                    char __user *buf, size_t len)
0201 {
0202     ssize_t retlen;
0203     unsigned trials = HMCDRV_DEV_BUSY_RETRIES;
0204 
0205     do {
0206         retlen = hmcdrv_ftp_cmd(cmd, offset, buf, len);
0207 
0208         if (retlen != -EBUSY)
0209             break;
0210 
0211         msleep(HMCDRV_DEV_BUSY_DELAY);
0212 
0213     } while (--trials > 0);
0214 
0215     return retlen;
0216 }
0217 
0218 /*
0219  * read()
0220  */
0221 static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
0222                    size_t len, loff_t *pos)
0223 {
0224     ssize_t retlen;
0225 
0226     if (((fp->f_flags & O_ACCMODE) == O_WRONLY) ||
0227         (fp->private_data == NULL)) { /* no FTP cmd defined ? */
0228         return -EBADF;
0229     }
0230 
0231     retlen = hmcdrv_dev_transfer((char *) fp->private_data,
0232                      *pos, ubuf, len);
0233 
0234     pr_debug("read from file '/dev/%pD' at %lld returns %zd/%zu\n",
0235          fp, (long long) *pos, retlen, len);
0236 
0237     if (retlen > 0)
0238         *pos += retlen;
0239 
0240     return retlen;
0241 }
0242 
0243 /*
0244  * write()
0245  */
0246 static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
0247                 size_t len, loff_t *pos)
0248 {
0249     ssize_t retlen;
0250 
0251     pr_debug("writing file '/dev/%pD' at pos. %lld with length %zd\n",
0252          fp, (long long) *pos, len);
0253 
0254     if (!fp->private_data) { /* first expect a cmd write */
0255         fp->private_data = kmalloc(len + 1, GFP_KERNEL);
0256 
0257         if (!fp->private_data)
0258             return -ENOMEM;
0259 
0260         if (!copy_from_user(fp->private_data, ubuf, len)) {
0261             ((char *)fp->private_data)[len] = '\0';
0262             return len;
0263         }
0264 
0265         kfree(fp->private_data);
0266         fp->private_data = NULL;
0267         return -EFAULT;
0268     }
0269 
0270     retlen = hmcdrv_dev_transfer((char *) fp->private_data,
0271                      *pos, (char __user *) ubuf, len);
0272     if (retlen > 0)
0273         *pos += retlen;
0274 
0275     pr_debug("write to file '/dev/%pD' returned %zd\n", fp, retlen);
0276 
0277     return retlen;
0278 }
0279 
0280 /**
0281  * hmcdrv_dev_init() - creates a HMC drive CD/DVD device
0282  *
0283  * This function creates a HMC drive CD/DVD kernel device and an associated
0284  * device under /dev, using a dynamically allocated major number.
0285  *
0286  * Return: 0 on success, else an error code.
0287  */
0288 int hmcdrv_dev_init(void)
0289 {
0290     int rc;
0291 
0292 #ifdef HMCDRV_DEV_CLASS
0293     struct device *dev;
0294 
0295     rc = alloc_chrdev_region(&hmcdrv_dev_no, 0, 1, HMCDRV_DEV_NAME);
0296 
0297     if (rc)
0298         goto out_err;
0299 
0300     cdev_init(&hmcdrv_dev.dev, &hmcdrv_dev_fops);
0301     hmcdrv_dev.dev.owner = THIS_MODULE;
0302     rc = cdev_add(&hmcdrv_dev.dev, hmcdrv_dev_no, 1);
0303 
0304     if (rc)
0305         goto out_unreg;
0306 
0307     /* At this point the character device exists in the kernel (see
0308      * /proc/devices), but not under /dev nor /sys/devices/virtual. So
0309      * we have to create an associated class (see /sys/class).
0310      */
0311     hmcdrv_dev_class = class_create(THIS_MODULE, HMCDRV_DEV_CLASS);
0312 
0313     if (IS_ERR(hmcdrv_dev_class)) {
0314         rc = PTR_ERR(hmcdrv_dev_class);
0315         goto out_devdel;
0316     }
0317 
0318     /* Finally a device node in /dev has to be established (as 'mkdev'
0319      * does from the command line). Notice that assignment of a device
0320      * node name/mode function is optional (only for mode != 0600).
0321      */
0322     hmcdrv_dev.mode = 0; /* "unset" */
0323     hmcdrv_dev_class->devnode = hmcdrv_dev_name;
0324 
0325     dev = device_create(hmcdrv_dev_class, NULL, hmcdrv_dev_no, NULL,
0326                 "%s", HMCDRV_DEV_NAME);
0327     if (!IS_ERR(dev))
0328         return 0;
0329 
0330     rc = PTR_ERR(dev);
0331     class_destroy(hmcdrv_dev_class);
0332     hmcdrv_dev_class = NULL;
0333 
0334 out_devdel:
0335     cdev_del(&hmcdrv_dev.dev);
0336 
0337 out_unreg:
0338     unregister_chrdev_region(hmcdrv_dev_no, 1);
0339 
0340 out_err:
0341 
0342 #else  /* !HMCDRV_DEV_CLASS */
0343     hmcdrv_dev.dev.minor = MISC_DYNAMIC_MINOR;
0344     hmcdrv_dev.dev.name = HMCDRV_DEV_NAME;
0345     hmcdrv_dev.dev.fops = &hmcdrv_dev_fops;
0346     hmcdrv_dev.dev.mode = 0; /* finally produces 0600 */
0347     rc = misc_register(&hmcdrv_dev.dev);
0348 #endif  /* HMCDRV_DEV_CLASS */
0349 
0350     return rc;
0351 }
0352 
0353 /**
0354  * hmcdrv_dev_exit() - destroys a HMC drive CD/DVD device
0355  */
0356 void hmcdrv_dev_exit(void)
0357 {
0358 #ifdef HMCDRV_DEV_CLASS
0359     if (!IS_ERR_OR_NULL(hmcdrv_dev_class)) {
0360         device_destroy(hmcdrv_dev_class, hmcdrv_dev_no);
0361         class_destroy(hmcdrv_dev_class);
0362     }
0363 
0364     cdev_del(&hmcdrv_dev.dev);
0365     unregister_chrdev_region(hmcdrv_dev_no, 1);
0366 #else  /* !HMCDRV_DEV_CLASS */
0367     misc_deregister(&hmcdrv_dev.dev);
0368 #endif  /* HMCDRV_DEV_CLASS */
0369 }