1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#define KMSG_COMPONENT "hmcdrv"
18#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/fs.h>
24#include <linux/cdev.h>
25#include <linux/miscdevice.h>
26#include <linux/device.h>
27#include <linux/capability.h>
28#include <linux/delay.h>
29#include <linux/uaccess.h>
30
31#include "hmcdrv_dev.h"
32#include "hmcdrv_ftp.h"
33
34
35
36
37
38
39
40
41#define HMCDRV_DEV_NAME "hmcdrv"
42#define HMCDRV_DEV_BUSY_DELAY 500
43#define HMCDRV_DEV_BUSY_RETRIES 3
44
45struct hmcdrv_dev_node {
46
47#ifdef HMCDRV_DEV_CLASS
48 struct cdev dev;
49 umode_t mode;
50#else
51 struct miscdevice dev;
52#endif
53
54};
55
56static int hmcdrv_dev_open(struct inode *inode, struct file *fp);
57static int hmcdrv_dev_release(struct inode *inode, struct file *fp);
58static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence);
59static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
60 size_t len, loff_t *pos);
61static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
62 size_t len, loff_t *pos);
63static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
64 char __user *buf, size_t len);
65
66
67
68
69static const struct file_operations hmcdrv_dev_fops = {
70 .open = hmcdrv_dev_open,
71 .llseek = hmcdrv_dev_seek,
72 .release = hmcdrv_dev_release,
73 .read = hmcdrv_dev_read,
74 .write = hmcdrv_dev_write,
75};
76
77static struct hmcdrv_dev_node hmcdrv_dev;
78
79#ifdef HMCDRV_DEV_CLASS
80
81static struct class *hmcdrv_dev_class;
82static dev_t hmcdrv_dev_no;
83
84
85
86
87
88
89
90
91
92
93static char *hmcdrv_dev_name(struct device *dev, umode_t *mode)
94{
95 char *nodename = NULL;
96 const char *devname = dev_name(dev);
97
98 if (devname)
99 nodename = kasprintf(GFP_KERNEL, "%s", devname);
100
101
102
103 if (mode)
104 *mode = hmcdrv_dev.mode;
105
106 return nodename;
107}
108
109#endif
110
111
112
113
114static int hmcdrv_dev_open(struct inode *inode, struct file *fp)
115{
116 int rc;
117
118
119
120 if (fp->f_flags & O_NONBLOCK)
121 return -EINVAL;
122
123
124
125
126 if ((fp->f_flags & O_ACCMODE) == O_RDONLY)
127 return -EINVAL;
128
129
130
131
132 if (!try_module_get(THIS_MODULE))
133 return -ENODEV;
134
135 fp->private_data = NULL;
136 rc = hmcdrv_ftp_startup();
137 if (rc)
138 module_put(THIS_MODULE);
139
140 pr_debug("open file '/dev/%pD' with return code %d\n", fp, rc);
141 return rc;
142}
143
144
145
146
147static int hmcdrv_dev_release(struct inode *inode, struct file *fp)
148{
149 pr_debug("closing file '/dev/%pD'\n", fp);
150 kfree(fp->private_data);
151 fp->private_data = NULL;
152 hmcdrv_ftp_shutdown();
153 module_put(THIS_MODULE);
154 return 0;
155}
156
157
158
159
160static loff_t hmcdrv_dev_seek(struct file *fp, loff_t pos, int whence)
161{
162 switch (whence) {
163 case SEEK_CUR:
164 pos += fp->f_pos;
165 break;
166
167 case SEEK_SET:
168 break;
169
170
171
172
173
174 case SEEK_END:
175 if (fp->private_data) {
176 kfree(fp->private_data);
177 fp->private_data = NULL;
178 }
179
180 break;
181
182 default:
183 return -EINVAL;
184 }
185
186 if (pos < 0)
187 return -EINVAL;
188
189 if (fp->f_pos != pos)
190 ++fp->f_version;
191
192 fp->f_pos = pos;
193 return pos;
194}
195
196
197
198
199static ssize_t hmcdrv_dev_transfer(char __kernel *cmd, loff_t offset,
200 char __user *buf, size_t len)
201{
202 ssize_t retlen;
203 unsigned trials = HMCDRV_DEV_BUSY_RETRIES;
204
205 do {
206 retlen = hmcdrv_ftp_cmd(cmd, offset, buf, len);
207
208 if (retlen != -EBUSY)
209 break;
210
211 msleep(HMCDRV_DEV_BUSY_DELAY);
212
213 } while (--trials > 0);
214
215 return retlen;
216}
217
218
219
220
221static ssize_t hmcdrv_dev_read(struct file *fp, char __user *ubuf,
222 size_t len, loff_t *pos)
223{
224 ssize_t retlen;
225
226 if (((fp->f_flags & O_ACCMODE) == O_WRONLY) ||
227 (fp->private_data == NULL)) {
228 return -EBADF;
229 }
230
231 retlen = hmcdrv_dev_transfer((char *) fp->private_data,
232 *pos, ubuf, len);
233
234 pr_debug("read from file '/dev/%pD' at %lld returns %zd/%zu\n",
235 fp, (long long) *pos, retlen, len);
236
237 if (retlen > 0)
238 *pos += retlen;
239
240 return retlen;
241}
242
243
244
245
246static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
247 size_t len, loff_t *pos)
248{
249 ssize_t retlen;
250
251 pr_debug("writing file '/dev/%pD' at pos. %lld with length %zd\n",
252 fp, (long long) *pos, len);
253
254 if (!fp->private_data) {
255 fp->private_data = kmalloc(len + 1, GFP_KERNEL);
256
257 if (!fp->private_data)
258 return -ENOMEM;
259
260 if (!copy_from_user(fp->private_data, ubuf, len)) {
261 ((char *)fp->private_data)[len] = '\0';
262 return len;
263 }
264
265 kfree(fp->private_data);
266 fp->private_data = NULL;
267 return -EFAULT;
268 }
269
270 retlen = hmcdrv_dev_transfer((char *) fp->private_data,
271 *pos, (char __user *) ubuf, len);
272 if (retlen > 0)
273 *pos += retlen;
274
275 pr_debug("write to file '/dev/%pD' returned %zd\n", fp, retlen);
276
277 return retlen;
278}
279
280
281
282
283
284
285
286
287
288int hmcdrv_dev_init(void)
289{
290 int rc;
291
292#ifdef HMCDRV_DEV_CLASS
293 struct device *dev;
294
295 rc = alloc_chrdev_region(&hmcdrv_dev_no, 0, 1, HMCDRV_DEV_NAME);
296
297 if (rc)
298 goto out_err;
299
300 cdev_init(&hmcdrv_dev.dev, &hmcdrv_dev_fops);
301 hmcdrv_dev.dev.owner = THIS_MODULE;
302 rc = cdev_add(&hmcdrv_dev.dev, hmcdrv_dev_no, 1);
303
304 if (rc)
305 goto out_unreg;
306
307
308
309
310
311 hmcdrv_dev_class = class_create(THIS_MODULE, HMCDRV_DEV_CLASS);
312
313 if (IS_ERR(hmcdrv_dev_class)) {
314 rc = PTR_ERR(hmcdrv_dev_class);
315 goto out_devdel;
316 }
317
318
319
320
321
322 hmcdrv_dev.mode = 0;
323 hmcdrv_dev_class->devnode = hmcdrv_dev_name;
324
325 dev = device_create(hmcdrv_dev_class, NULL, hmcdrv_dev_no, NULL,
326 "%s", HMCDRV_DEV_NAME);
327 if (!IS_ERR(dev))
328 return 0;
329
330 rc = PTR_ERR(dev);
331 class_destroy(hmcdrv_dev_class);
332 hmcdrv_dev_class = NULL;
333
334out_devdel:
335 cdev_del(&hmcdrv_dev.dev);
336
337out_unreg:
338 unregister_chrdev_region(hmcdrv_dev_no, 1);
339
340out_err:
341
342#else
343 hmcdrv_dev.dev.minor = MISC_DYNAMIC_MINOR;
344 hmcdrv_dev.dev.name = HMCDRV_DEV_NAME;
345 hmcdrv_dev.dev.fops = &hmcdrv_dev_fops;
346 hmcdrv_dev.dev.mode = 0;
347 rc = misc_register(&hmcdrv_dev.dev);
348#endif
349
350 return rc;
351}
352
353
354
355
356void hmcdrv_dev_exit(void)
357{
358#ifdef HMCDRV_DEV_CLASS
359 if (!IS_ERR_OR_NULL(hmcdrv_dev_class)) {
360 device_destroy(hmcdrv_dev_class, hmcdrv_dev_no);
361 class_destroy(hmcdrv_dev_class);
362 }
363
364 cdev_del(&hmcdrv_dev.dev);
365 unregister_chrdev_region(hmcdrv_dev_no, 1);
366#else
367 misc_deregister(&hmcdrv_dev.dev);
368#endif
369}
370