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