hmcdrv_dev.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 (rhoppe@de.ibm.com)
  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(const 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. fp->f_pos = pos;
  158. return pos;
  159. }
  160. /*
  161. * transfer (helper function)
  162. */
  163. static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
  164. char __user *buf, size_t len)
  165. {
  166. ssize_t retlen;
  167. unsigned trials = HMCDRV_DEV_BUSY_RETRIES;
  168. do {
  169. retlen = hmcdrv_ftp_cmd(cmd, offset, buf, len);
  170. if (retlen != -EBUSY)
  171. break;
  172. msleep(HMCDRV_DEV_BUSY_DELAY);
  173. } while (--trials > 0);
  174. return retlen;
  175. }
  176. /*
  177. * read()
  178. */
  179. static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
  180. size_t len, loff_t *pos)
  181. {
  182. ssize_t retlen;
  183. if (((fp->f_flags & O_ACCMODE) == O_WRONLY) ||
  184. (fp->private_data == NULL)) { /* no FTP cmd defined ? */
  185. return -EBADF;
  186. }
  187. retlen = hmcdrv_dev_transfer((char *) fp->private_data,
  188. *pos, ubuf, len);
  189. pr_debug("read from file '/dev/%pD' at %lld returns %zd/%zu\n",
  190. fp, (long long) *pos, retlen, len);
  191. if (retlen > 0)
  192. *pos += retlen;
  193. return retlen;
  194. }
  195. /*
  196. * write()
  197. */
  198. static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
  199. size_t len, loff_t *pos)
  200. {
  201. ssize_t retlen;
  202. pr_debug("writing file '/dev/%pD' at pos. %lld with length %zd\n",
  203. fp, (long long) *pos, len);
  204. if (!fp->private_data) { /* first expect a cmd write */
  205. fp->private_data = kmalloc(len + 1, GFP_KERNEL);
  206. if (!fp->private_data)
  207. return -ENOMEM;
  208. if (!copy_from_user(fp->private_data, ubuf, len)) {
  209. ((char *)fp->private_data)[len] = '\0';
  210. return len;
  211. }
  212. kfree(fp->private_data);
  213. fp->private_data = NULL;
  214. return -EFAULT;
  215. }
  216. retlen = hmcdrv_dev_transfer((char *) fp->private_data,
  217. *pos, (char __user *) ubuf, len);
  218. if (retlen > 0)
  219. *pos += retlen;
  220. pr_debug("write to file '/dev/%pD' returned %zd\n", fp, retlen);
  221. return retlen;
  222. }
  223. /**
  224. * hmcdrv_dev_init() - creates a HMC drive CD/DVD device
  225. *
  226. * This function creates a HMC drive CD/DVD kernel device and an associated
  227. * device under /dev, using a dynamically allocated major number.
  228. *
  229. * Return: 0 on success, else an error code.
  230. */
  231. int hmcdrv_dev_init(void)
  232. {
  233. int rc;
  234. #ifdef HMCDRV_DEV_CLASS
  235. struct device *dev;
  236. rc = alloc_chrdev_region(&hmcdrv_dev_no, 0, 1, HMCDRV_DEV_NAME);
  237. if (rc)
  238. goto out_err;
  239. cdev_init(&hmcdrv_dev.dev, &hmcdrv_dev_fops);
  240. hmcdrv_dev.dev.owner = THIS_MODULE;
  241. rc = cdev_add(&hmcdrv_dev.dev, hmcdrv_dev_no, 1);
  242. if (rc)
  243. goto out_unreg;
  244. /* At this point the character device exists in the kernel (see
  245. * /proc/devices), but not under /dev nor /sys/devices/virtual. So
  246. * we have to create an associated class (see /sys/class).
  247. */
  248. hmcdrv_dev_class = class_create(HMCDRV_DEV_CLASS);
  249. if (IS_ERR(hmcdrv_dev_class)) {
  250. rc = PTR_ERR(hmcdrv_dev_class);
  251. goto out_devdel;
  252. }
  253. /* Finally a device node in /dev has to be established (as 'mkdev'
  254. * does from the command line). Notice that assignment of a device
  255. * node name/mode function is optional (only for mode != 0600).
  256. */
  257. hmcdrv_dev.mode = 0; /* "unset" */
  258. hmcdrv_dev_class->devnode = hmcdrv_dev_name;
  259. dev = device_create(hmcdrv_dev_class, NULL, hmcdrv_dev_no, NULL,
  260. "%s", HMCDRV_DEV_NAME);
  261. if (!IS_ERR(dev))
  262. return 0;
  263. rc = PTR_ERR(dev);
  264. class_destroy(hmcdrv_dev_class);
  265. hmcdrv_dev_class = NULL;
  266. out_devdel:
  267. cdev_del(&hmcdrv_dev.dev);
  268. out_unreg:
  269. unregister_chrdev_region(hmcdrv_dev_no, 1);
  270. out_err:
  271. #else /* !HMCDRV_DEV_CLASS */
  272. hmcdrv_dev.dev.minor = MISC_DYNAMIC_MINOR;
  273. hmcdrv_dev.dev.name = HMCDRV_DEV_NAME;
  274. hmcdrv_dev.dev.fops = &hmcdrv_dev_fops;
  275. hmcdrv_dev.dev.mode = 0; /* finally produces 0600 */
  276. rc = misc_register(&hmcdrv_dev.dev);
  277. #endif /* HMCDRV_DEV_CLASS */
  278. return rc;
  279. }
  280. /**
  281. * hmcdrv_dev_exit() - destroys a HMC drive CD/DVD device
  282. */
  283. void hmcdrv_dev_exit(void)
  284. {
  285. #ifdef HMCDRV_DEV_CLASS
  286. if (!IS_ERR_OR_NULL(hmcdrv_dev_class)) {
  287. device_destroy(hmcdrv_dev_class, hmcdrv_dev_no);
  288. class_destroy(hmcdrv_dev_class);
  289. }
  290. cdev_del(&hmcdrv_dev.dev);
  291. unregister_chrdev_region(hmcdrv_dev_no, 1);
  292. #else /* !HMCDRV_DEV_CLASS */
  293. misc_deregister(&hmcdrv_dev.dev);
  294. #endif /* HMCDRV_DEV_CLASS */
  295. }