^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * HMC Drive CD/DVD Device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright IBM Corp. 2013
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This file provides a Linux "misc" character device for access to an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * assigned HMC drive CD/DVD-ROM. It works as follows: First create the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * device by calling hmcdrv_dev_init(). After open() a lseek(fd, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * SEEK_END) indicates that a new FTP command follows (not needed on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * first command after open). Then write() the FTP command ASCII string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * to it, e.g. "dir /" or "nls <directory>" or "get <filename>". At the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * end read() the response.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define KMSG_COMPONENT "hmcdrv"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "hmcdrv_dev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "hmcdrv_ftp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* If the following macro is defined, then the HMC device creates it's own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * separated device class (and dynamically assigns a major number). If not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * defined then the HMC device is assigned to the "misc" class devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define HMCDRV_DEV_CLASS "hmcftp"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define HMCDRV_DEV_NAME "hmcdrv"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define HMCDRV_DEV_BUSY_DELAY 500 /* delay between -EBUSY trials in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define HMCDRV_DEV_BUSY_RETRIES 3 /* number of retries on -EBUSY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct hmcdrv_dev_node {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #ifdef HMCDRV_DEV_CLASS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct cdev dev; /* character device structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) umode_t mode; /* mode of device node (unused, zero) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct miscdevice dev; /* "misc" device structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int hmcdrv_dev_open(struct inode *inode, struct file *fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int hmcdrv_dev_release(struct inode *inode, struct file *fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) size_t len, loff_t *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) size_t len, loff_t *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) char __user *buf, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * device operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static const struct file_operations hmcdrv_dev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .open = hmcdrv_dev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .llseek = hmcdrv_dev_seek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .release = hmcdrv_dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .read = hmcdrv_dev_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .write = hmcdrv_dev_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static struct hmcdrv_dev_node hmcdrv_dev; /* HMC device struct (static) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #ifdef HMCDRV_DEV_CLASS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static struct class *hmcdrv_dev_class; /* device class pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static dev_t hmcdrv_dev_no; /* device number (major/minor) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * hmcdrv_dev_name() - provides a naming hint for a device node in /dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * @dev: device for which the naming/mode hint is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @mode: file mode for device node created in /dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * See: devtmpfs.c, function devtmpfs_create_node()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Return: recommended device file name in /dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static char *hmcdrv_dev_name(struct device *dev, umode_t *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) char *nodename = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) const char *devname = dev_name(dev); /* kernel device name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) nodename = kasprintf(GFP_KERNEL, "%s", devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* on device destroy (rmmod) the mode pointer may be NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) *mode = hmcdrv_dev.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return nodename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #endif /* HMCDRV_DEV_CLASS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * open()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int hmcdrv_dev_open(struct inode *inode, struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* check for non-blocking access, which is really unsupported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (fp->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Because it makes no sense to open this device read-only (then a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * FTP command cannot be emitted), we respond with an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if ((fp->f_flags & O_ACCMODE) == O_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* prevent unloading this module as long as anyone holds the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * device file open - so increment the reference count here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!try_module_get(THIS_MODULE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) fp->private_data = NULL; /* no command yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) rc = hmcdrv_ftp_startup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pr_debug("open file '/dev/%pD' with return code %d\n", fp, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * release()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int hmcdrv_dev_release(struct inode *inode, struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) pr_debug("closing file '/dev/%pD'\n", fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) kfree(fp->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) fp->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) hmcdrv_ftp_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * lseek()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) switch (whence) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) case SEEK_CUR: /* relative to current file position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pos += fp->f_pos; /* new position stored in 'pos' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) case SEEK_SET: /* absolute (relative to beginning of file) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) break; /* SEEK_SET */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* We use SEEK_END as a special indicator for a SEEK_SET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * (set absolute position), combined with a FTP command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * clear.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) case SEEK_END:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (fp->private_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) kfree(fp->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) fp->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) break; /* SEEK_END */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) default: /* SEEK_DATA, SEEK_HOLE: unsupported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (pos < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (fp->f_pos != pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ++fp->f_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) fp->f_pos = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * transfer (helper function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) char __user *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) ssize_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned trials = HMCDRV_DEV_BUSY_RETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) retlen = hmcdrv_ftp_cmd(cmd, offset, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (retlen != -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) msleep(HMCDRV_DEV_BUSY_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) } while (--trials > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * read()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) size_t len, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ssize_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (((fp->f_flags & O_ACCMODE) == O_WRONLY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) (fp->private_data == NULL)) { /* no FTP cmd defined ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) retlen = hmcdrv_dev_transfer((char *) fp->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) *pos, ubuf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) pr_debug("read from file '/dev/%pD' at %lld returns %zd/%zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) fp, (long long) *pos, retlen, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (retlen > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) *pos += retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * write()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) size_t len, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ssize_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) pr_debug("writing file '/dev/%pD' at pos. %lld with length %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) fp, (long long) *pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (!fp->private_data) { /* first expect a cmd write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) fp->private_data = kmalloc(len + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!fp->private_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!copy_from_user(fp->private_data, ubuf, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ((char *)fp->private_data)[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) kfree(fp->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) fp->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) retlen = hmcdrv_dev_transfer((char *) fp->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) *pos, (char __user *) ubuf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (retlen > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *pos += retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) pr_debug("write to file '/dev/%pD' returned %zd\n", fp, retlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * hmcdrv_dev_init() - creates a HMC drive CD/DVD device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * This function creates a HMC drive CD/DVD kernel device and an associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * device under /dev, using a dynamically allocated major number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * Return: 0 on success, else an error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int hmcdrv_dev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #ifdef HMCDRV_DEV_CLASS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) rc = alloc_chrdev_region(&hmcdrv_dev_no, 0, 1, HMCDRV_DEV_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) cdev_init(&hmcdrv_dev.dev, &hmcdrv_dev_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) hmcdrv_dev.dev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) rc = cdev_add(&hmcdrv_dev.dev, hmcdrv_dev_no, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) goto out_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /* At this point the character device exists in the kernel (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * /proc/devices), but not under /dev nor /sys/devices/virtual. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * we have to create an associated class (see /sys/class).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) hmcdrv_dev_class = class_create(THIS_MODULE, HMCDRV_DEV_CLASS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (IS_ERR(hmcdrv_dev_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) rc = PTR_ERR(hmcdrv_dev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) goto out_devdel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* Finally a device node in /dev has to be established (as 'mkdev'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * does from the command line). Notice that assignment of a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * node name/mode function is optional (only for mode != 0600).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) hmcdrv_dev.mode = 0; /* "unset" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) hmcdrv_dev_class->devnode = hmcdrv_dev_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) dev = device_create(hmcdrv_dev_class, NULL, hmcdrv_dev_no, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) "%s", HMCDRV_DEV_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!IS_ERR(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) rc = PTR_ERR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) class_destroy(hmcdrv_dev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) hmcdrv_dev_class = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) out_devdel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) cdev_del(&hmcdrv_dev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) out_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) unregister_chrdev_region(hmcdrv_dev_no, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) #else /* !HMCDRV_DEV_CLASS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) hmcdrv_dev.dev.minor = MISC_DYNAMIC_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) hmcdrv_dev.dev.name = HMCDRV_DEV_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) hmcdrv_dev.dev.fops = &hmcdrv_dev_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) hmcdrv_dev.dev.mode = 0; /* finally produces 0600 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) rc = misc_register(&hmcdrv_dev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #endif /* HMCDRV_DEV_CLASS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * hmcdrv_dev_exit() - destroys a HMC drive CD/DVD device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) void hmcdrv_dev_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) #ifdef HMCDRV_DEV_CLASS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (!IS_ERR_OR_NULL(hmcdrv_dev_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) device_destroy(hmcdrv_dev_class, hmcdrv_dev_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) class_destroy(hmcdrv_dev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) cdev_del(&hmcdrv_dev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) unregister_chrdev_region(hmcdrv_dev_no, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) #else /* !HMCDRV_DEV_CLASS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) misc_deregister(&hmcdrv_dev.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) #endif /* HMCDRV_DEV_CLASS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }