hmcdrv_dev.c 8.9 KB

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