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