1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 28f933b10SRalf Hoppe /* 38f933b10SRalf Hoppe * HMC Drive CD/DVD Device 48f933b10SRalf Hoppe * 58f933b10SRalf Hoppe * Copyright IBM Corp. 2013 68f933b10SRalf Hoppe * Author(s): Ralf Hoppe (rhoppe@de.ibm.com) 78f933b10SRalf Hoppe * 88f933b10SRalf Hoppe * This file provides a Linux "misc" character device for access to an 98f933b10SRalf Hoppe * assigned HMC drive CD/DVD-ROM. It works as follows: First create the 108f933b10SRalf Hoppe * device by calling hmcdrv_dev_init(). After open() a lseek(fd, 0, 118f933b10SRalf Hoppe * SEEK_END) indicates that a new FTP command follows (not needed on the 128f933b10SRalf Hoppe * first command after open). Then write() the FTP command ASCII string 138f933b10SRalf Hoppe * to it, e.g. "dir /" or "nls <directory>" or "get <filename>". At the 148f933b10SRalf Hoppe * end read() the response. 158f933b10SRalf Hoppe */ 168f933b10SRalf Hoppe 178f933b10SRalf Hoppe #define KMSG_COMPONENT "hmcdrv" 188f933b10SRalf Hoppe #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 198f933b10SRalf Hoppe 208f933b10SRalf Hoppe #include <linux/kernel.h> 218f933b10SRalf Hoppe #include <linux/module.h> 228f933b10SRalf Hoppe #include <linux/slab.h> 238f933b10SRalf Hoppe #include <linux/fs.h> 248f933b10SRalf Hoppe #include <linux/cdev.h> 258f933b10SRalf Hoppe #include <linux/miscdevice.h> 268f933b10SRalf Hoppe #include <linux/device.h> 278f933b10SRalf Hoppe #include <linux/capability.h> 288f933b10SRalf Hoppe #include <linux/delay.h> 298f933b10SRalf Hoppe #include <linux/uaccess.h> 308f933b10SRalf Hoppe 318f933b10SRalf Hoppe #include "hmcdrv_dev.h" 328f933b10SRalf Hoppe #include "hmcdrv_ftp.h" 338f933b10SRalf Hoppe 348f933b10SRalf Hoppe /* If the following macro is defined, then the HMC device creates it's own 358f933b10SRalf Hoppe * separated device class (and dynamically assigns a major number). If not 368f933b10SRalf Hoppe * defined then the HMC device is assigned to the "misc" class devices. 378f933b10SRalf Hoppe * 388f933b10SRalf Hoppe #define HMCDRV_DEV_CLASS "hmcftp" 398f933b10SRalf Hoppe */ 408f933b10SRalf Hoppe 418f933b10SRalf Hoppe #define HMCDRV_DEV_NAME "hmcdrv" 428f933b10SRalf Hoppe #define HMCDRV_DEV_BUSY_DELAY 500 /* delay between -EBUSY trials in ms */ 438f933b10SRalf Hoppe #define HMCDRV_DEV_BUSY_RETRIES 3 /* number of retries on -EBUSY */ 448f933b10SRalf Hoppe 458f933b10SRalf Hoppe struct hmcdrv_dev_node { 468f933b10SRalf Hoppe 478f933b10SRalf Hoppe #ifdef HMCDRV_DEV_CLASS 488f933b10SRalf Hoppe struct cdev dev; /* character device structure */ 498f933b10SRalf Hoppe umode_t mode; /* mode of device node (unused, zero) */ 508f933b10SRalf Hoppe #else 518f933b10SRalf Hoppe struct miscdevice dev; /* "misc" device structure */ 528f933b10SRalf Hoppe #endif 538f933b10SRalf Hoppe 548f933b10SRalf Hoppe }; 558f933b10SRalf Hoppe 568f933b10SRalf Hoppe static int hmcdrv_dev_open(struct inode *inode, struct file *fp); 578f933b10SRalf Hoppe static int hmcdrv_dev_release(struct inode *inode, struct file *fp); 588f933b10SRalf Hoppe static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence); 598f933b10SRalf Hoppe static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf, 608f933b10SRalf Hoppe size_t len, loff_t *pos); 618f933b10SRalf Hoppe static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf, 628f933b10SRalf Hoppe size_t len, loff_t *pos); 638f933b10SRalf Hoppe static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset, 648f933b10SRalf Hoppe char __user *buf, size_t len); 658f933b10SRalf Hoppe 668f933b10SRalf Hoppe /* 678f933b10SRalf Hoppe * device operations 688f933b10SRalf Hoppe */ 698f933b10SRalf Hoppe static const struct file_operations hmcdrv_dev_fops = { 708f933b10SRalf Hoppe .open = hmcdrv_dev_open, 718f933b10SRalf Hoppe .llseek = hmcdrv_dev_seek, 728f933b10SRalf Hoppe .release = hmcdrv_dev_release, 738f933b10SRalf Hoppe .read = hmcdrv_dev_read, 748f933b10SRalf Hoppe .write = hmcdrv_dev_write, 758f933b10SRalf Hoppe }; 768f933b10SRalf Hoppe 778f933b10SRalf Hoppe static struct hmcdrv_dev_node hmcdrv_dev; /* HMC device struct (static) */ 788f933b10SRalf Hoppe 798f933b10SRalf Hoppe #ifdef HMCDRV_DEV_CLASS 808f933b10SRalf Hoppe 818f933b10SRalf Hoppe static struct class *hmcdrv_dev_class; /* device class pointer */ 828f933b10SRalf Hoppe static dev_t hmcdrv_dev_no; /* device number (major/minor) */ 838f933b10SRalf Hoppe 848f933b10SRalf Hoppe /** 858f933b10SRalf Hoppe * hmcdrv_dev_name() - provides a naming hint for a device node in /dev 868f933b10SRalf Hoppe * @dev: device for which the naming/mode hint is 878f933b10SRalf Hoppe * @mode: file mode for device node created in /dev 888f933b10SRalf Hoppe * 898f933b10SRalf Hoppe * See: devtmpfs.c, function devtmpfs_create_node() 908f933b10SRalf Hoppe * 918f933b10SRalf Hoppe * Return: recommended device file name in /dev 928f933b10SRalf Hoppe */ 938f933b10SRalf Hoppe static char *hmcdrv_dev_name(struct device *dev, umode_t *mode) 948f933b10SRalf Hoppe { 958f933b10SRalf Hoppe char *nodename = NULL; 968f933b10SRalf Hoppe const char *devname = dev_name(dev); /* kernel device name */ 978f933b10SRalf Hoppe 988f933b10SRalf Hoppe if (devname) 998f933b10SRalf Hoppe nodename = kasprintf(GFP_KERNEL, "%s", devname); 1008f933b10SRalf Hoppe 1018f933b10SRalf Hoppe /* on device destroy (rmmod) the mode pointer may be NULL 1028f933b10SRalf Hoppe */ 1038f933b10SRalf Hoppe if (mode) 1048f933b10SRalf Hoppe *mode = hmcdrv_dev.mode; 1058f933b10SRalf Hoppe 1068f933b10SRalf Hoppe return nodename; 1078f933b10SRalf Hoppe } 1088f933b10SRalf Hoppe 1098f933b10SRalf Hoppe #endif /* HMCDRV_DEV_CLASS */ 1108f933b10SRalf Hoppe 1118f933b10SRalf Hoppe /* 1128f933b10SRalf Hoppe * open() 1138f933b10SRalf Hoppe */ 1148f933b10SRalf Hoppe static int hmcdrv_dev_open(struct inode *inode, struct file *fp) 1158f933b10SRalf Hoppe { 1168f933b10SRalf Hoppe int rc; 1178f933b10SRalf Hoppe 1188f933b10SRalf Hoppe /* check for non-blocking access, which is really unsupported 1198f933b10SRalf Hoppe */ 1208f933b10SRalf Hoppe if (fp->f_flags & O_NONBLOCK) 1218f933b10SRalf Hoppe return -EINVAL; 1228f933b10SRalf Hoppe 1238f933b10SRalf Hoppe /* Because it makes no sense to open this device read-only (then a 1248f933b10SRalf Hoppe * FTP command cannot be emitted), we respond with an error. 1258f933b10SRalf Hoppe */ 1268f933b10SRalf Hoppe if ((fp->f_flags & O_ACCMODE) == O_RDONLY) 1278f933b10SRalf Hoppe return -EINVAL; 1288f933b10SRalf Hoppe 1298f933b10SRalf Hoppe /* prevent unloading this module as long as anyone holds the 1308f933b10SRalf Hoppe * device file open - so increment the reference count here 1318f933b10SRalf Hoppe */ 1328f933b10SRalf Hoppe if (!try_module_get(THIS_MODULE)) 1338f933b10SRalf Hoppe return -ENODEV; 1348f933b10SRalf Hoppe 1358f933b10SRalf Hoppe fp->private_data = NULL; /* no command yet */ 1368f933b10SRalf Hoppe rc = hmcdrv_ftp_startup(); 1378f933b10SRalf Hoppe if (rc) 1388f933b10SRalf Hoppe module_put(THIS_MODULE); 1398f933b10SRalf Hoppe 140a455589fSAl Viro pr_debug("open file '/dev/%pD' with return code %d\n", fp, rc); 1418f933b10SRalf Hoppe return rc; 1428f933b10SRalf Hoppe } 1438f933b10SRalf Hoppe 1448f933b10SRalf Hoppe /* 1458f933b10SRalf Hoppe * release() 1468f933b10SRalf Hoppe */ 1478f933b10SRalf Hoppe static int hmcdrv_dev_release(struct inode *inode, struct file *fp) 1488f933b10SRalf Hoppe { 149a455589fSAl Viro pr_debug("closing file '/dev/%pD'\n", fp); 1508f933b10SRalf Hoppe kfree(fp->private_data); 1518f933b10SRalf Hoppe fp->private_data = NULL; 1528f933b10SRalf Hoppe hmcdrv_ftp_shutdown(); 1538f933b10SRalf Hoppe module_put(THIS_MODULE); 1548f933b10SRalf Hoppe return 0; 1558f933b10SRalf Hoppe } 1568f933b10SRalf Hoppe 1578f933b10SRalf Hoppe /* 1588f933b10SRalf Hoppe * lseek() 1598f933b10SRalf Hoppe */ 1608f933b10SRalf Hoppe static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence) 1618f933b10SRalf Hoppe { 1628f933b10SRalf Hoppe switch (whence) { 1638f933b10SRalf Hoppe case SEEK_CUR: /* relative to current file position */ 1648f933b10SRalf Hoppe pos += fp->f_pos; /* new position stored in 'pos' */ 1658f933b10SRalf Hoppe break; 1668f933b10SRalf Hoppe 1678f933b10SRalf Hoppe case SEEK_SET: /* absolute (relative to beginning of file) */ 1688f933b10SRalf Hoppe break; /* SEEK_SET */ 1698f933b10SRalf Hoppe 1708f933b10SRalf Hoppe /* We use SEEK_END as a special indicator for a SEEK_SET 1718f933b10SRalf Hoppe * (set absolute position), combined with a FTP command 1728f933b10SRalf Hoppe * clear. 1738f933b10SRalf Hoppe */ 1748f933b10SRalf Hoppe case SEEK_END: 1758f933b10SRalf Hoppe if (fp->private_data) { 1768f933b10SRalf Hoppe kfree(fp->private_data); 1778f933b10SRalf Hoppe fp->private_data = NULL; 1788f933b10SRalf Hoppe } 1798f933b10SRalf Hoppe 1808f933b10SRalf Hoppe break; /* SEEK_END */ 1818f933b10SRalf Hoppe 1828f933b10SRalf Hoppe default: /* SEEK_DATA, SEEK_HOLE: unsupported */ 1838f933b10SRalf Hoppe return -EINVAL; 1848f933b10SRalf Hoppe } 1858f933b10SRalf Hoppe 1868f933b10SRalf Hoppe if (pos < 0) 1878f933b10SRalf Hoppe return -EINVAL; 1888f933b10SRalf Hoppe 1898f933b10SRalf Hoppe if (fp->f_pos != pos) 1908f933b10SRalf Hoppe ++fp->f_version; 1918f933b10SRalf Hoppe 1928f933b10SRalf Hoppe fp->f_pos = pos; 1938f933b10SRalf Hoppe return pos; 1948f933b10SRalf Hoppe } 1958f933b10SRalf Hoppe 1968f933b10SRalf Hoppe /* 1978f933b10SRalf Hoppe * transfer (helper function) 1988f933b10SRalf Hoppe */ 1998f933b10SRalf Hoppe static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset, 2008f933b10SRalf Hoppe char __user *buf, size_t len) 2018f933b10SRalf Hoppe { 2028f933b10SRalf Hoppe ssize_t retlen; 2038f933b10SRalf Hoppe unsigned trials = HMCDRV_DEV_BUSY_RETRIES; 2048f933b10SRalf Hoppe 2058f933b10SRalf Hoppe do { 2068f933b10SRalf Hoppe retlen = hmcdrv_ftp_cmd(cmd, offset, buf, len); 2078f933b10SRalf Hoppe 2088f933b10SRalf Hoppe if (retlen != -EBUSY) 2098f933b10SRalf Hoppe break; 2108f933b10SRalf Hoppe 2118f933b10SRalf Hoppe msleep(HMCDRV_DEV_BUSY_DELAY); 2128f933b10SRalf Hoppe 2138f933b10SRalf Hoppe } while (--trials > 0); 2148f933b10SRalf Hoppe 2158f933b10SRalf Hoppe return retlen; 2168f933b10SRalf Hoppe } 2178f933b10SRalf Hoppe 2188f933b10SRalf Hoppe /* 2198f933b10SRalf Hoppe * read() 2208f933b10SRalf Hoppe */ 2218f933b10SRalf Hoppe static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf, 2228f933b10SRalf Hoppe size_t len, loff_t *pos) 2238f933b10SRalf Hoppe { 2248f933b10SRalf Hoppe ssize_t retlen; 2258f933b10SRalf Hoppe 2268f933b10SRalf Hoppe if (((fp->f_flags & O_ACCMODE) == O_WRONLY) || 2278f933b10SRalf Hoppe (fp->private_data == NULL)) { /* no FTP cmd defined ? */ 2288f933b10SRalf Hoppe return -EBADF; 2298f933b10SRalf Hoppe } 2308f933b10SRalf Hoppe 2318f933b10SRalf Hoppe retlen = hmcdrv_dev_transfer((char *) fp->private_data, 2328f933b10SRalf Hoppe *pos, ubuf, len); 2338f933b10SRalf Hoppe 234a455589fSAl Viro pr_debug("read from file '/dev/%pD' at %lld returns %zd/%zu\n", 235a455589fSAl Viro fp, (long long) *pos, retlen, len); 2368f933b10SRalf Hoppe 2378f933b10SRalf Hoppe if (retlen > 0) 2388f933b10SRalf Hoppe *pos += retlen; 2398f933b10SRalf Hoppe 2408f933b10SRalf Hoppe return retlen; 2418f933b10SRalf Hoppe } 2428f933b10SRalf Hoppe 2438f933b10SRalf Hoppe /* 2448f933b10SRalf Hoppe * write() 2458f933b10SRalf Hoppe */ 2468f933b10SRalf Hoppe static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf, 2478f933b10SRalf Hoppe size_t len, loff_t *pos) 2488f933b10SRalf Hoppe { 2498f933b10SRalf Hoppe ssize_t retlen; 2508f933b10SRalf Hoppe 251a455589fSAl Viro pr_debug("writing file '/dev/%pD' at pos. %lld with length %zd\n", 252a455589fSAl Viro fp, (long long) *pos, len); 2538f933b10SRalf Hoppe 2548f933b10SRalf Hoppe if (!fp->private_data) { /* first expect a cmd write */ 2558f933b10SRalf Hoppe fp->private_data = kmalloc(len + 1, GFP_KERNEL); 2568f933b10SRalf Hoppe 2578f933b10SRalf Hoppe if (!fp->private_data) 2588f933b10SRalf Hoppe return -ENOMEM; 2598f933b10SRalf Hoppe 2608f933b10SRalf Hoppe if (!copy_from_user(fp->private_data, ubuf, len)) { 2618f933b10SRalf Hoppe ((char *)fp->private_data)[len] = '\0'; 2628f933b10SRalf Hoppe return len; 2638f933b10SRalf Hoppe } 2648f933b10SRalf Hoppe 2658f933b10SRalf Hoppe kfree(fp->private_data); 2668f933b10SRalf Hoppe fp->private_data = NULL; 2678f933b10SRalf Hoppe return -EFAULT; 2688f933b10SRalf Hoppe } 2698f933b10SRalf Hoppe 2708f933b10SRalf Hoppe retlen = hmcdrv_dev_transfer((char *) fp->private_data, 2718f933b10SRalf Hoppe *pos, (char __user *) ubuf, len); 2728f933b10SRalf Hoppe if (retlen > 0) 2738f933b10SRalf Hoppe *pos += retlen; 2748f933b10SRalf Hoppe 275a455589fSAl Viro pr_debug("write to file '/dev/%pD' returned %zd\n", fp, retlen); 2768f933b10SRalf Hoppe 2778f933b10SRalf Hoppe return retlen; 2788f933b10SRalf Hoppe } 2798f933b10SRalf Hoppe 2808f933b10SRalf Hoppe /** 2818f933b10SRalf Hoppe * hmcdrv_dev_init() - creates a HMC drive CD/DVD device 2828f933b10SRalf Hoppe * 2838f933b10SRalf Hoppe * This function creates a HMC drive CD/DVD kernel device and an associated 2848f933b10SRalf Hoppe * device under /dev, using a dynamically allocated major number. 2858f933b10SRalf Hoppe * 2868f933b10SRalf Hoppe * Return: 0 on success, else an error code. 2878f933b10SRalf Hoppe */ 2888f933b10SRalf Hoppe int hmcdrv_dev_init(void) 2898f933b10SRalf Hoppe { 2908f933b10SRalf Hoppe int rc; 2918f933b10SRalf Hoppe 2928f933b10SRalf Hoppe #ifdef HMCDRV_DEV_CLASS 2938f933b10SRalf Hoppe struct device *dev; 2948f933b10SRalf Hoppe 2958f933b10SRalf Hoppe rc = alloc_chrdev_region(&hmcdrv_dev_no, 0, 1, HMCDRV_DEV_NAME); 2968f933b10SRalf Hoppe 2978f933b10SRalf Hoppe if (rc) 2988f933b10SRalf Hoppe goto out_err; 2998f933b10SRalf Hoppe 3008f933b10SRalf Hoppe cdev_init(&hmcdrv_dev.dev, &hmcdrv_dev_fops); 3018f933b10SRalf Hoppe hmcdrv_dev.dev.owner = THIS_MODULE; 3028f933b10SRalf Hoppe rc = cdev_add(&hmcdrv_dev.dev, hmcdrv_dev_no, 1); 3038f933b10SRalf Hoppe 3048f933b10SRalf Hoppe if (rc) 3058f933b10SRalf Hoppe goto out_unreg; 3068f933b10SRalf Hoppe 3078f933b10SRalf Hoppe /* At this point the character device exists in the kernel (see 3088f933b10SRalf Hoppe * /proc/devices), but not under /dev nor /sys/devices/virtual. So 3098f933b10SRalf Hoppe * we have to create an associated class (see /sys/class). 3108f933b10SRalf Hoppe */ 3118f933b10SRalf Hoppe hmcdrv_dev_class = class_create(THIS_MODULE, HMCDRV_DEV_CLASS); 3128f933b10SRalf Hoppe 3138f933b10SRalf Hoppe if (IS_ERR(hmcdrv_dev_class)) { 3148f933b10SRalf Hoppe rc = PTR_ERR(hmcdrv_dev_class); 3158f933b10SRalf Hoppe goto out_devdel; 3168f933b10SRalf Hoppe } 3178f933b10SRalf Hoppe 3188f933b10SRalf Hoppe /* Finally a device node in /dev has to be established (as 'mkdev' 3198f933b10SRalf Hoppe * does from the command line). Notice that assignment of a device 3208f933b10SRalf Hoppe * node name/mode function is optional (only for mode != 0600). 3218f933b10SRalf Hoppe */ 3228f933b10SRalf Hoppe hmcdrv_dev.mode = 0; /* "unset" */ 3238f933b10SRalf Hoppe hmcdrv_dev_class->devnode = hmcdrv_dev_name; 3248f933b10SRalf Hoppe 3258f933b10SRalf Hoppe dev = device_create(hmcdrv_dev_class, NULL, hmcdrv_dev_no, NULL, 3268f933b10SRalf Hoppe "%s", HMCDRV_DEV_NAME); 3278f933b10SRalf Hoppe if (!IS_ERR(dev)) 3288f933b10SRalf Hoppe return 0; 3298f933b10SRalf Hoppe 3308f933b10SRalf Hoppe rc = PTR_ERR(dev); 3318f933b10SRalf Hoppe class_destroy(hmcdrv_dev_class); 3328f933b10SRalf Hoppe hmcdrv_dev_class = NULL; 3338f933b10SRalf Hoppe 3348f933b10SRalf Hoppe out_devdel: 3358f933b10SRalf Hoppe cdev_del(&hmcdrv_dev.dev); 3368f933b10SRalf Hoppe 3378f933b10SRalf Hoppe out_unreg: 3388f933b10SRalf Hoppe unregister_chrdev_region(hmcdrv_dev_no, 1); 3398f933b10SRalf Hoppe 3408f933b10SRalf Hoppe out_err: 3418f933b10SRalf Hoppe 3428f933b10SRalf Hoppe #else /* !HMCDRV_DEV_CLASS */ 3438f933b10SRalf Hoppe hmcdrv_dev.dev.minor = MISC_DYNAMIC_MINOR; 3448f933b10SRalf Hoppe hmcdrv_dev.dev.name = HMCDRV_DEV_NAME; 3458f933b10SRalf Hoppe hmcdrv_dev.dev.fops = &hmcdrv_dev_fops; 3468f933b10SRalf Hoppe hmcdrv_dev.dev.mode = 0; /* finally produces 0600 */ 3478f933b10SRalf Hoppe rc = misc_register(&hmcdrv_dev.dev); 3488f933b10SRalf Hoppe #endif /* HMCDRV_DEV_CLASS */ 3498f933b10SRalf Hoppe 3508f933b10SRalf Hoppe return rc; 3518f933b10SRalf Hoppe } 3528f933b10SRalf Hoppe 3538f933b10SRalf Hoppe /** 3548f933b10SRalf Hoppe * hmcdrv_dev_exit() - destroys a HMC drive CD/DVD device 3558f933b10SRalf Hoppe */ 3568f933b10SRalf Hoppe void hmcdrv_dev_exit(void) 3578f933b10SRalf Hoppe { 3588f933b10SRalf Hoppe #ifdef HMCDRV_DEV_CLASS 3598f933b10SRalf Hoppe if (!IS_ERR_OR_NULL(hmcdrv_dev_class)) { 3608f933b10SRalf Hoppe device_destroy(hmcdrv_dev_class, hmcdrv_dev_no); 3618f933b10SRalf Hoppe class_destroy(hmcdrv_dev_class); 3628f933b10SRalf Hoppe } 3638f933b10SRalf Hoppe 3648f933b10SRalf Hoppe cdev_del(&hmcdrv_dev.dev); 3658f933b10SRalf Hoppe unregister_chrdev_region(hmcdrv_dev_no, 1); 3668f933b10SRalf Hoppe #else /* !HMCDRV_DEV_CLASS */ 3678f933b10SRalf Hoppe misc_deregister(&hmcdrv_dev.dev); 3688f933b10SRalf Hoppe #endif /* HMCDRV_DEV_CLASS */ 3698f933b10SRalf Hoppe } 370