// SPDX-License-Identifier: GPL-2.0 /* * HMC Drive CD/DVD Device * * Copyright IBM Corp. 2013 * Author(s): Ralf Hoppe (rhoppe@de.ibm.com) * * This file provides a Linux "misc" character device for access to an * assigned HMC drive CD/DVD-ROM. It works as follows: First create the * device by calling hmcdrv_dev_init(). After open() a lseek(fd, 0, * SEEK_END) indicates that a new FTP command follows (not needed on the * first command after open). Then write() the FTP command ASCII string * to it, e.g. "dir /" or "nls <directory>" or "get <filename>". At the * end read() the response.
*/
/* If the following macro is defined, then the HMC device creates it's own * separated device class (and dynamically assigns a major number). If not * defined then the HMC device is assigned to the "misc" class devices. * #define HMCDRV_DEV_CLASS "hmcftp"
*/
#define HMCDRV_DEV_NAME "hmcdrv" #define HMCDRV_DEV_BUSY_DELAY 500 /* delay between -EBUSY trials in ms */ #define HMCDRV_DEV_BUSY_RETRIES 3 /* number of retries on -EBUSY */
staticstructclass *hmcdrv_dev_class; /* device class pointer */ static dev_t hmcdrv_dev_no; /* device number (major/minor) */
/** * hmcdrv_dev_name() - provides a naming hint for a device node in /dev * @dev: device for which the naming/mode hint is * @mode: file mode for device node created in /dev * * See: devtmpfs.c, function devtmpfs_create_node() * * Return: recommended device file name in /dev
*/ staticchar *hmcdrv_dev_name(conststruct device *dev, umode_t *mode)
{ char *nodename = NULL; constchar *devname = dev_name(dev); /* kernel device name */
if (devname)
nodename = kasprintf(GFP_KERNEL, "%s", devname);
/* on device destroy (rmmod) the mode pointer may be NULL
*/ if (mode)
*mode = hmcdrv_dev.mode;
/* check for non-blocking access, which is really unsupported
*/ if (fp->f_flags & O_NONBLOCK) return -EINVAL;
/* Because it makes no sense to open this device read-only (then a * FTP command cannot be emitted), we respond with an error.
*/ if ((fp->f_flags & O_ACCMODE) == O_RDONLY) return -EINVAL;
/* prevent unloading this module as long as anyone holds the * device file open - so increment the reference count here
*/ if (!try_module_get(THIS_MODULE)) return -ENODEV;
fp->private_data = NULL; /* no command yet */
rc = hmcdrv_ftp_startup(); if (rc)
module_put(THIS_MODULE);
/* * lseek()
*/ static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence)
{ switch (whence) { case SEEK_CUR: /* relative to current file position */
pos += fp->f_pos; /* new position stored in 'pos' */ break;
case SEEK_SET: /* absolute (relative to beginning of file) */ break; /* SEEK_SET */
/* We use SEEK_END as a special indicator for a SEEK_SET * (set absolute position), combined with a FTP command * clear.
*/ case SEEK_END: if (fp->private_data) {
kfree(fp->private_data);
fp->private_data = NULL;
}
pr_debug("write to file '/dev/%pD' returned %zd\n", fp, retlen);
return retlen;
}
/** * hmcdrv_dev_init() - creates a HMC drive CD/DVD device * * This function creates a HMC drive CD/DVD kernel device and an associated * device under /dev, using a dynamically allocated major number. * * Return: 0 on success, else an error code.
*/ int hmcdrv_dev_init(void)
{ int rc;
/* At this point the character device exists in the kernel (see * /proc/devices), but not under /dev nor /sys/devices/virtual. So * we have to create an associated class (see /sys/class).
*/
hmcdrv_dev_class = class_create(HMCDRV_DEV_CLASS);
if (IS_ERR(hmcdrv_dev_class)) {
rc = PTR_ERR(hmcdrv_dev_class); goto out_devdel;
}
/* Finally a device node in /dev has to be established (as 'mkdev' * does from the command line). Notice that assignment of a device * node name/mode function is optional (only for mode != 0600).
*/
hmcdrv_dev.mode = 0; /* "unset" */
hmcdrv_dev_class->devnode = hmcdrv_dev_name;
dev = device_create(hmcdrv_dev_class, NULL, hmcdrv_dev_no, NULL, "%s", HMCDRV_DEV_NAME); if (!IS_ERR(dev)) return 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.