hmcdrv_dev.c 8.9 KB

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