0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #define KMSG_COMPONENT "hmcdrv"
0018 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0019
0020 #include <linux/kernel.h>
0021 #include <linux/module.h>
0022 #include <linux/slab.h>
0023 #include <linux/fs.h>
0024 #include <linux/cdev.h>
0025 #include <linux/miscdevice.h>
0026 #include <linux/device.h>
0027 #include <linux/capability.h>
0028 #include <linux/delay.h>
0029 #include <linux/uaccess.h>
0030
0031 #include "hmcdrv_dev.h"
0032 #include "hmcdrv_ftp.h"
0033
0034
0035
0036
0037
0038
0039
0040
0041 #define HMCDRV_DEV_NAME "hmcdrv"
0042 #define HMCDRV_DEV_BUSY_DELAY 500
0043 #define HMCDRV_DEV_BUSY_RETRIES 3
0044
0045 struct hmcdrv_dev_node {
0046
0047 #ifdef HMCDRV_DEV_CLASS
0048 struct cdev dev;
0049 umode_t mode;
0050 #else
0051 struct miscdevice dev;
0052 #endif
0053
0054 };
0055
0056 static int hmcdrv_dev_open(struct inode *inode, struct file *fp);
0057 static int hmcdrv_dev_release(struct inode *inode, struct file *fp);
0058 static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence);
0059 static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
0060 size_t len, loff_t *pos);
0061 static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
0062 size_t len, loff_t *pos);
0063 static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
0064 char __user *buf, size_t len);
0065
0066
0067
0068
0069 static const struct file_operations hmcdrv_dev_fops = {
0070 .open = hmcdrv_dev_open,
0071 .llseek = hmcdrv_dev_seek,
0072 .release = hmcdrv_dev_release,
0073 .read = hmcdrv_dev_read,
0074 .write = hmcdrv_dev_write,
0075 };
0076
0077 static struct hmcdrv_dev_node hmcdrv_dev;
0078
0079 #ifdef HMCDRV_DEV_CLASS
0080
0081 static struct class *hmcdrv_dev_class;
0082 static dev_t hmcdrv_dev_no;
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093 static char *hmcdrv_dev_name(struct device *dev, umode_t *mode)
0094 {
0095 char *nodename = NULL;
0096 const char *devname = dev_name(dev);
0097
0098 if (devname)
0099 nodename = kasprintf(GFP_KERNEL, "%s", devname);
0100
0101
0102
0103 if (mode)
0104 *mode = hmcdrv_dev.mode;
0105
0106 return nodename;
0107 }
0108
0109 #endif
0110
0111
0112
0113
0114 static int hmcdrv_dev_open(struct inode *inode, struct file *fp)
0115 {
0116 int rc;
0117
0118
0119
0120 if (fp->f_flags & O_NONBLOCK)
0121 return -EINVAL;
0122
0123
0124
0125
0126 if ((fp->f_flags & O_ACCMODE) == O_RDONLY)
0127 return -EINVAL;
0128
0129
0130
0131
0132 if (!try_module_get(THIS_MODULE))
0133 return -ENODEV;
0134
0135 fp->private_data = NULL;
0136 rc = hmcdrv_ftp_startup();
0137 if (rc)
0138 module_put(THIS_MODULE);
0139
0140 pr_debug("open file '/dev/%pD' with return code %d\n", fp, rc);
0141 return rc;
0142 }
0143
0144
0145
0146
0147 static int hmcdrv_dev_release(struct inode *inode, struct file *fp)
0148 {
0149 pr_debug("closing file '/dev/%pD'\n", fp);
0150 kfree(fp->private_data);
0151 fp->private_data = NULL;
0152 hmcdrv_ftp_shutdown();
0153 module_put(THIS_MODULE);
0154 return 0;
0155 }
0156
0157
0158
0159
0160 static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence)
0161 {
0162 switch (whence) {
0163 case SEEK_CUR:
0164 pos += fp->f_pos;
0165 break;
0166
0167 case SEEK_SET:
0168 break;
0169
0170
0171
0172
0173
0174 case SEEK_END:
0175 if (fp->private_data) {
0176 kfree(fp->private_data);
0177 fp->private_data = NULL;
0178 }
0179
0180 break;
0181
0182 default:
0183 return -EINVAL;
0184 }
0185
0186 if (pos < 0)
0187 return -EINVAL;
0188
0189 if (fp->f_pos != pos)
0190 ++fp->f_version;
0191
0192 fp->f_pos = pos;
0193 return pos;
0194 }
0195
0196
0197
0198
0199 static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
0200 char __user *buf, size_t len)
0201 {
0202 ssize_t retlen;
0203 unsigned trials = HMCDRV_DEV_BUSY_RETRIES;
0204
0205 do {
0206 retlen = hmcdrv_ftp_cmd(cmd, offset, buf, len);
0207
0208 if (retlen != -EBUSY)
0209 break;
0210
0211 msleep(HMCDRV_DEV_BUSY_DELAY);
0212
0213 } while (--trials > 0);
0214
0215 return retlen;
0216 }
0217
0218
0219
0220
0221 static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
0222 size_t len, loff_t *pos)
0223 {
0224 ssize_t retlen;
0225
0226 if (((fp->f_flags & O_ACCMODE) == O_WRONLY) ||
0227 (fp->private_data == NULL)) {
0228 return -EBADF;
0229 }
0230
0231 retlen = hmcdrv_dev_transfer((char *) fp->private_data,
0232 *pos, ubuf, len);
0233
0234 pr_debug("read from file '/dev/%pD' at %lld returns %zd/%zu\n",
0235 fp, (long long) *pos, retlen, len);
0236
0237 if (retlen > 0)
0238 *pos += retlen;
0239
0240 return retlen;
0241 }
0242
0243
0244
0245
0246 static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
0247 size_t len, loff_t *pos)
0248 {
0249 ssize_t retlen;
0250
0251 pr_debug("writing file '/dev/%pD' at pos. %lld with length %zd\n",
0252 fp, (long long) *pos, len);
0253
0254 if (!fp->private_data) {
0255 fp->private_data = kmalloc(len + 1, GFP_KERNEL);
0256
0257 if (!fp->private_data)
0258 return -ENOMEM;
0259
0260 if (!copy_from_user(fp->private_data, ubuf, len)) {
0261 ((char *)fp->private_data)[len] = '\0';
0262 return len;
0263 }
0264
0265 kfree(fp->private_data);
0266 fp->private_data = NULL;
0267 return -EFAULT;
0268 }
0269
0270 retlen = hmcdrv_dev_transfer((char *) fp->private_data,
0271 *pos, (char __user *) ubuf, len);
0272 if (retlen > 0)
0273 *pos += retlen;
0274
0275 pr_debug("write to file '/dev/%pD' returned %zd\n", fp, retlen);
0276
0277 return retlen;
0278 }
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 int hmcdrv_dev_init(void)
0289 {
0290 int rc;
0291
0292 #ifdef HMCDRV_DEV_CLASS
0293 struct device *dev;
0294
0295 rc = alloc_chrdev_region(&hmcdrv_dev_no, 0, 1, HMCDRV_DEV_NAME);
0296
0297 if (rc)
0298 goto out_err;
0299
0300 cdev_init(&hmcdrv_dev.dev, &hmcdrv_dev_fops);
0301 hmcdrv_dev.dev.owner = THIS_MODULE;
0302 rc = cdev_add(&hmcdrv_dev.dev, hmcdrv_dev_no, 1);
0303
0304 if (rc)
0305 goto out_unreg;
0306
0307
0308
0309
0310
0311 hmcdrv_dev_class = class_create(THIS_MODULE, HMCDRV_DEV_CLASS);
0312
0313 if (IS_ERR(hmcdrv_dev_class)) {
0314 rc = PTR_ERR(hmcdrv_dev_class);
0315 goto out_devdel;
0316 }
0317
0318
0319
0320
0321
0322 hmcdrv_dev.mode = 0;
0323 hmcdrv_dev_class->devnode = hmcdrv_dev_name;
0324
0325 dev = device_create(hmcdrv_dev_class, NULL, hmcdrv_dev_no, NULL,
0326 "%s", HMCDRV_DEV_NAME);
0327 if (!IS_ERR(dev))
0328 return 0;
0329
0330 rc = PTR_ERR(dev);
0331 class_destroy(hmcdrv_dev_class);
0332 hmcdrv_dev_class = NULL;
0333
0334 out_devdel:
0335 cdev_del(&hmcdrv_dev.dev);
0336
0337 out_unreg:
0338 unregister_chrdev_region(hmcdrv_dev_no, 1);
0339
0340 out_err:
0341
0342 #else
0343 hmcdrv_dev.dev.minor = MISC_DYNAMIC_MINOR;
0344 hmcdrv_dev.dev.name = HMCDRV_DEV_NAME;
0345 hmcdrv_dev.dev.fops = &hmcdrv_dev_fops;
0346 hmcdrv_dev.dev.mode = 0;
0347 rc = misc_register(&hmcdrv_dev.dev);
0348 #endif
0349
0350 return rc;
0351 }
0352
0353
0354
0355
0356 void hmcdrv_dev_exit(void)
0357 {
0358 #ifdef HMCDRV_DEV_CLASS
0359 if (!IS_ERR_OR_NULL(hmcdrv_dev_class)) {
0360 device_destroy(hmcdrv_dev_class, hmcdrv_dev_no);
0361 class_destroy(hmcdrv_dev_class);
0362 }
0363
0364 cdev_del(&hmcdrv_dev.dev);
0365 unregister_chrdev_region(hmcdrv_dev_no, 1);
0366 #else
0367 misc_deregister(&hmcdrv_dev.dev);
0368 #endif
0369 }