Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
97894cda | 2 | * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $ |
1da177e4 LT |
3 | * |
4 | * Character-device access to raw MTD devices. | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <linux/config.h> | |
15fdc52f TG |
9 | #include <linux/device.h> |
10 | #include <linux/fs.h> | |
11 | #include <linux/init.h> | |
1da177e4 LT |
12 | #include <linux/kernel.h> |
13 | #include <linux/module.h> | |
15fdc52f TG |
14 | #include <linux/slab.h> |
15 | #include <linux/sched.h> | |
16 | ||
1da177e4 LT |
17 | #include <linux/mtd/mtd.h> |
18 | #include <linux/mtd/compatmac.h> | |
1da177e4 | 19 | |
15fdc52f | 20 | #include <asm/uaccess.h> |
9bc7b387 TP |
21 | |
22 | static struct class *mtd_class; | |
1da177e4 LT |
23 | |
24 | static void mtd_notify_add(struct mtd_info* mtd) | |
25 | { | |
26 | if (!mtd) | |
27 | return; | |
28 | ||
53f46542 | 29 | class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), |
9bc7b387 | 30 | NULL, "mtd%d", mtd->index); |
97894cda | 31 | |
53f46542 | 32 | class_device_create(mtd_class, NULL, |
9bc7b387 TP |
33 | MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1), |
34 | NULL, "mtd%dro", mtd->index); | |
1da177e4 LT |
35 | } |
36 | ||
37 | static void mtd_notify_remove(struct mtd_info* mtd) | |
38 | { | |
39 | if (!mtd) | |
40 | return; | |
9bc7b387 TP |
41 | |
42 | class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2)); | |
43 | class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1)); | |
1da177e4 LT |
44 | } |
45 | ||
46 | static struct mtd_notifier notifier = { | |
47 | .add = mtd_notify_add, | |
48 | .remove = mtd_notify_remove, | |
49 | }; | |
50 | ||
045e9a5d NP |
51 | /* |
52 | * We use file->private_data to store a pointer to the MTDdevice. | |
53 | * Since alighment is at least 32 bits, we have 2 bits free for OTP | |
54 | * modes as well. | |
55 | */ | |
56 | ||
57 | #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L) | |
31f4233b | 58 | |
045e9a5d NP |
59 | #define MTD_MODE_OTP_FACT 1 |
60 | #define MTD_MODE_OTP_USER 2 | |
61 | #define MTD_MODE(file) ((long)((file)->private_data) & 3) | |
62 | ||
63 | #define SET_MTD_MODE(file, mode) \ | |
64 | do { long __p = (long)((file)->private_data); \ | |
65 | (file)->private_data = (void *)((__p & ~3L) | mode); } while (0) | |
31f4233b | 66 | |
1da177e4 LT |
67 | static loff_t mtd_lseek (struct file *file, loff_t offset, int orig) |
68 | { | |
045e9a5d | 69 | struct mtd_info *mtd = TO_MTD(file); |
1da177e4 LT |
70 | |
71 | switch (orig) { | |
72 | case 0: | |
73 | /* SEEK_SET */ | |
1da177e4 LT |
74 | break; |
75 | case 1: | |
76 | /* SEEK_CUR */ | |
8b491d75 | 77 | offset += file->f_pos; |
1da177e4 LT |
78 | break; |
79 | case 2: | |
80 | /* SEEK_END */ | |
8b491d75 | 81 | offset += mtd->size; |
1da177e4 LT |
82 | break; |
83 | default: | |
84 | return -EINVAL; | |
85 | } | |
86 | ||
8b491d75 TP |
87 | if (offset >= 0 && offset < mtd->size) |
88 | return file->f_pos = offset; | |
1da177e4 | 89 | |
8b491d75 | 90 | return -EINVAL; |
1da177e4 LT |
91 | } |
92 | ||
93 | ||
94 | ||
95 | static int mtd_open(struct inode *inode, struct file *file) | |
96 | { | |
97 | int minor = iminor(inode); | |
98 | int devnum = minor >> 1; | |
99 | struct mtd_info *mtd; | |
100 | ||
101 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n"); | |
102 | ||
103 | if (devnum >= MAX_MTD_DEVICES) | |
104 | return -ENODEV; | |
105 | ||
106 | /* You can't open the RO devices RW */ | |
107 | if ((file->f_mode & 2) && (minor & 1)) | |
108 | return -EACCES; | |
109 | ||
110 | mtd = get_mtd_device(NULL, devnum); | |
97894cda | 111 | |
1da177e4 LT |
112 | if (!mtd) |
113 | return -ENODEV; | |
97894cda | 114 | |
1da177e4 LT |
115 | if (MTD_ABSENT == mtd->type) { |
116 | put_mtd_device(mtd); | |
117 | return -ENODEV; | |
118 | } | |
119 | ||
120 | file->private_data = mtd; | |
97894cda | 121 | |
1da177e4 LT |
122 | /* You can't open it RW if it's not a writeable device */ |
123 | if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) { | |
124 | put_mtd_device(mtd); | |
125 | return -EACCES; | |
126 | } | |
97894cda | 127 | |
1da177e4 LT |
128 | return 0; |
129 | } /* mtd_open */ | |
130 | ||
131 | /*====================================================================*/ | |
132 | ||
133 | static int mtd_close(struct inode *inode, struct file *file) | |
134 | { | |
135 | struct mtd_info *mtd; | |
136 | ||
137 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n"); | |
138 | ||
045e9a5d | 139 | mtd = TO_MTD(file); |
97894cda | 140 | |
1da177e4 LT |
141 | if (mtd->sync) |
142 | mtd->sync(mtd); | |
97894cda | 143 | |
1da177e4 LT |
144 | put_mtd_device(mtd); |
145 | ||
146 | return 0; | |
147 | } /* mtd_close */ | |
148 | ||
149 | /* FIXME: This _really_ needs to die. In 2.5, we should lock the | |
150 | userspace buffer down and use it directly with readv/writev. | |
151 | */ | |
152 | #define MAX_KMALLOC_SIZE 0x20000 | |
153 | ||
154 | static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos) | |
155 | { | |
045e9a5d | 156 | struct mtd_info *mtd = TO_MTD(file); |
1da177e4 LT |
157 | size_t retlen=0; |
158 | size_t total_retlen=0; | |
159 | int ret=0; | |
160 | int len; | |
161 | char *kbuf; | |
97894cda | 162 | |
1da177e4 LT |
163 | DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n"); |
164 | ||
165 | if (*ppos + count > mtd->size) | |
166 | count = mtd->size - *ppos; | |
167 | ||
168 | if (!count) | |
169 | return 0; | |
97894cda | 170 | |
1da177e4 LT |
171 | /* FIXME: Use kiovec in 2.5 to lock down the user's buffers |
172 | and pass them directly to the MTD functions */ | |
b802c074 TG |
173 | |
174 | if (count > MAX_KMALLOC_SIZE) | |
175 | kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL); | |
176 | else | |
177 | kbuf=kmalloc(count, GFP_KERNEL); | |
178 | ||
179 | if (!kbuf) | |
180 | return -ENOMEM; | |
181 | ||
1da177e4 | 182 | while (count) { |
b802c074 | 183 | |
97894cda | 184 | if (count > MAX_KMALLOC_SIZE) |
1da177e4 LT |
185 | len = MAX_KMALLOC_SIZE; |
186 | else | |
187 | len = count; | |
188 | ||
045e9a5d | 189 | switch (MTD_MODE(file)) { |
31f4233b NP |
190 | case MTD_MODE_OTP_FACT: |
191 | ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf); | |
192 | break; | |
193 | case MTD_MODE_OTP_USER: | |
194 | ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf); | |
195 | break; | |
196 | default: | |
f4a43cfc | 197 | ret = mtd->read(mtd, *ppos, len, &retlen, kbuf); |
31f4233b | 198 | } |
1da177e4 LT |
199 | /* Nand returns -EBADMSG on ecc errors, but it returns |
200 | * the data. For our userspace tools it is important | |
97894cda | 201 | * to dump areas with ecc errors ! |
9a1fcdfd TG |
202 | * For kernel internal usage it also might return -EUCLEAN |
203 | * to signal the caller that a bitflip has occured and has | |
204 | * been corrected by the ECC algorithm. | |
1da177e4 LT |
205 | * Userspace software which accesses NAND this way |
206 | * must be aware of the fact that it deals with NAND | |
207 | */ | |
9a1fcdfd | 208 | if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) { |
1da177e4 LT |
209 | *ppos += retlen; |
210 | if (copy_to_user(buf, kbuf, retlen)) { | |
f4a43cfc | 211 | kfree(kbuf); |
1da177e4 LT |
212 | return -EFAULT; |
213 | } | |
214 | else | |
215 | total_retlen += retlen; | |
216 | ||
217 | count -= retlen; | |
218 | buf += retlen; | |
31f4233b NP |
219 | if (retlen == 0) |
220 | count = 0; | |
1da177e4 LT |
221 | } |
222 | else { | |
223 | kfree(kbuf); | |
224 | return ret; | |
225 | } | |
97894cda | 226 | |
1da177e4 LT |
227 | } |
228 | ||
b802c074 | 229 | kfree(kbuf); |
1da177e4 LT |
230 | return total_retlen; |
231 | } /* mtd_read */ | |
232 | ||
233 | static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos) | |
234 | { | |
045e9a5d | 235 | struct mtd_info *mtd = TO_MTD(file); |
1da177e4 LT |
236 | char *kbuf; |
237 | size_t retlen; | |
238 | size_t total_retlen=0; | |
239 | int ret=0; | |
240 | int len; | |
241 | ||
242 | DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n"); | |
97894cda | 243 | |
1da177e4 LT |
244 | if (*ppos == mtd->size) |
245 | return -ENOSPC; | |
97894cda | 246 | |
1da177e4 LT |
247 | if (*ppos + count > mtd->size) |
248 | count = mtd->size - *ppos; | |
249 | ||
250 | if (!count) | |
251 | return 0; | |
252 | ||
b802c074 TG |
253 | if (count > MAX_KMALLOC_SIZE) |
254 | kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL); | |
255 | else | |
256 | kbuf=kmalloc(count, GFP_KERNEL); | |
257 | ||
258 | if (!kbuf) | |
259 | return -ENOMEM; | |
260 | ||
1da177e4 | 261 | while (count) { |
b802c074 | 262 | |
97894cda | 263 | if (count > MAX_KMALLOC_SIZE) |
1da177e4 LT |
264 | len = MAX_KMALLOC_SIZE; |
265 | else | |
266 | len = count; | |
267 | ||
1da177e4 LT |
268 | if (copy_from_user(kbuf, buf, len)) { |
269 | kfree(kbuf); | |
270 | return -EFAULT; | |
271 | } | |
97894cda | 272 | |
045e9a5d | 273 | switch (MTD_MODE(file)) { |
31f4233b NP |
274 | case MTD_MODE_OTP_FACT: |
275 | ret = -EROFS; | |
276 | break; | |
277 | case MTD_MODE_OTP_USER: | |
278 | if (!mtd->write_user_prot_reg) { | |
279 | ret = -EOPNOTSUPP; | |
280 | break; | |
281 | } | |
282 | ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf); | |
283 | break; | |
284 | default: | |
285 | ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf); | |
286 | } | |
1da177e4 LT |
287 | if (!ret) { |
288 | *ppos += retlen; | |
289 | total_retlen += retlen; | |
290 | count -= retlen; | |
291 | buf += retlen; | |
292 | } | |
293 | else { | |
294 | kfree(kbuf); | |
295 | return ret; | |
296 | } | |
1da177e4 LT |
297 | } |
298 | ||
b802c074 | 299 | kfree(kbuf); |
1da177e4 LT |
300 | return total_retlen; |
301 | } /* mtd_write */ | |
302 | ||
303 | /*====================================================================== | |
304 | ||
305 | IOCTL calls for getting device parameters. | |
306 | ||
307 | ======================================================================*/ | |
308 | static void mtdchar_erase_callback (struct erase_info *instr) | |
309 | { | |
310 | wake_up((wait_queue_head_t *)instr->priv); | |
311 | } | |
312 | ||
313 | static int mtd_ioctl(struct inode *inode, struct file *file, | |
314 | u_int cmd, u_long arg) | |
315 | { | |
045e9a5d | 316 | struct mtd_info *mtd = TO_MTD(file); |
1da177e4 LT |
317 | void __user *argp = (void __user *)arg; |
318 | int ret = 0; | |
319 | u_long size; | |
97894cda | 320 | |
1da177e4 LT |
321 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n"); |
322 | ||
323 | size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT; | |
324 | if (cmd & IOC_IN) { | |
325 | if (!access_ok(VERIFY_READ, argp, size)) | |
326 | return -EFAULT; | |
327 | } | |
328 | if (cmd & IOC_OUT) { | |
329 | if (!access_ok(VERIFY_WRITE, argp, size)) | |
330 | return -EFAULT; | |
331 | } | |
97894cda | 332 | |
1da177e4 LT |
333 | switch (cmd) { |
334 | case MEMGETREGIONCOUNT: | |
335 | if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int))) | |
336 | return -EFAULT; | |
337 | break; | |
338 | ||
339 | case MEMGETREGIONINFO: | |
340 | { | |
341 | struct region_info_user ur; | |
342 | ||
343 | if (copy_from_user(&ur, argp, sizeof(struct region_info_user))) | |
344 | return -EFAULT; | |
345 | ||
346 | if (ur.regionindex >= mtd->numeraseregions) | |
347 | return -EINVAL; | |
348 | if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]), | |
349 | sizeof(struct mtd_erase_region_info))) | |
350 | return -EFAULT; | |
351 | break; | |
352 | } | |
353 | ||
354 | case MEMGETINFO: | |
355 | if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user))) | |
356 | return -EFAULT; | |
357 | break; | |
358 | ||
359 | case MEMERASE: | |
360 | { | |
361 | struct erase_info *erase; | |
362 | ||
363 | if(!(file->f_mode & 2)) | |
364 | return -EPERM; | |
365 | ||
366 | erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL); | |
367 | if (!erase) | |
368 | ret = -ENOMEM; | |
369 | else { | |
370 | wait_queue_head_t waitq; | |
371 | DECLARE_WAITQUEUE(wait, current); | |
372 | ||
373 | init_waitqueue_head(&waitq); | |
374 | ||
375 | memset (erase,0,sizeof(struct erase_info)); | |
376 | if (copy_from_user(&erase->addr, argp, | |
377 | sizeof(struct erase_info_user))) { | |
378 | kfree(erase); | |
379 | return -EFAULT; | |
380 | } | |
381 | erase->mtd = mtd; | |
382 | erase->callback = mtdchar_erase_callback; | |
383 | erase->priv = (unsigned long)&waitq; | |
97894cda | 384 | |
1da177e4 LT |
385 | /* |
386 | FIXME: Allow INTERRUPTIBLE. Which means | |
387 | not having the wait_queue head on the stack. | |
97894cda | 388 | |
1da177e4 LT |
389 | If the wq_head is on the stack, and we |
390 | leave because we got interrupted, then the | |
391 | wq_head is no longer there when the | |
392 | callback routine tries to wake us up. | |
393 | */ | |
394 | ret = mtd->erase(mtd, erase); | |
395 | if (!ret) { | |
396 | set_current_state(TASK_UNINTERRUPTIBLE); | |
397 | add_wait_queue(&waitq, &wait); | |
398 | if (erase->state != MTD_ERASE_DONE && | |
399 | erase->state != MTD_ERASE_FAILED) | |
400 | schedule(); | |
401 | remove_wait_queue(&waitq, &wait); | |
402 | set_current_state(TASK_RUNNING); | |
403 | ||
404 | ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0; | |
405 | } | |
406 | kfree(erase); | |
407 | } | |
408 | break; | |
409 | } | |
410 | ||
411 | case MEMWRITEOOB: | |
412 | { | |
413 | struct mtd_oob_buf buf; | |
8593fbc6 | 414 | struct mtd_oob_ops ops; |
97894cda | 415 | |
1da177e4 LT |
416 | if(!(file->f_mode & 2)) |
417 | return -EPERM; | |
418 | ||
419 | if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) | |
420 | return -EFAULT; | |
97894cda | 421 | |
8593fbc6 | 422 | if (buf.length > 4096) |
1da177e4 LT |
423 | return -EINVAL; |
424 | ||
425 | if (!mtd->write_oob) | |
426 | ret = -EOPNOTSUPP; | |
427 | else | |
428 | ret = access_ok(VERIFY_READ, buf.ptr, | |
429 | buf.length) ? 0 : EFAULT; | |
430 | ||
431 | if (ret) | |
432 | return ret; | |
433 | ||
8593fbc6 TG |
434 | ops.len = buf.length; |
435 | ops.ooblen = mtd->oobsize; | |
436 | ops.ooboffs = buf.start & (mtd->oobsize - 1); | |
437 | ops.datbuf = NULL; | |
438 | ops.mode = MTD_OOB_PLACE; | |
439 | ||
440 | if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs)) | |
441 | return -EINVAL; | |
442 | ||
443 | ops.oobbuf = kmalloc(buf.length, GFP_KERNEL); | |
444 | if (!ops.oobbuf) | |
1da177e4 | 445 | return -ENOMEM; |
97894cda | 446 | |
8593fbc6 TG |
447 | if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) { |
448 | kfree(ops.oobbuf); | |
1da177e4 LT |
449 | return -EFAULT; |
450 | } | |
451 | ||
8593fbc6 TG |
452 | buf.start &= ~(mtd->oobsize - 1); |
453 | ret = mtd->write_oob(mtd, buf.start, &ops); | |
1da177e4 | 454 | |
8593fbc6 TG |
455 | if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen, |
456 | sizeof(uint32_t))) | |
1da177e4 LT |
457 | ret = -EFAULT; |
458 | ||
8593fbc6 | 459 | kfree(ops.oobbuf); |
1da177e4 LT |
460 | break; |
461 | ||
462 | } | |
463 | ||
464 | case MEMREADOOB: | |
465 | { | |
466 | struct mtd_oob_buf buf; | |
8593fbc6 | 467 | struct mtd_oob_ops ops; |
1da177e4 LT |
468 | |
469 | if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) | |
470 | return -EFAULT; | |
97894cda | 471 | |
8593fbc6 | 472 | if (buf.length > 4096) |
1da177e4 LT |
473 | return -EINVAL; |
474 | ||
475 | if (!mtd->read_oob) | |
476 | ret = -EOPNOTSUPP; | |
477 | else | |
478 | ret = access_ok(VERIFY_WRITE, buf.ptr, | |
479 | buf.length) ? 0 : -EFAULT; | |
1da177e4 LT |
480 | if (ret) |
481 | return ret; | |
482 | ||
8593fbc6 TG |
483 | ops.len = buf.length; |
484 | ops.ooblen = mtd->oobsize; | |
485 | ops.ooboffs = buf.start & (mtd->oobsize - 1); | |
486 | ops.datbuf = NULL; | |
487 | ops.mode = MTD_OOB_PLACE; | |
488 | ||
489 | if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs)) | |
490 | return -EINVAL; | |
491 | ||
492 | ops.oobbuf = kmalloc(buf.length, GFP_KERNEL); | |
493 | if (!ops.oobbuf) | |
1da177e4 | 494 | return -ENOMEM; |
97894cda | 495 | |
8593fbc6 TG |
496 | buf.start &= ~(mtd->oobsize - 1); |
497 | ret = mtd->read_oob(mtd, buf.start, &ops); | |
1da177e4 | 498 | |
8593fbc6 | 499 | if (put_user(ops.retlen, (uint32_t __user *)argp)) |
1da177e4 | 500 | ret = -EFAULT; |
8593fbc6 TG |
501 | else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf, |
502 | ops.retlen)) | |
1da177e4 | 503 | ret = -EFAULT; |
97894cda | 504 | |
8593fbc6 | 505 | kfree(ops.oobbuf); |
1da177e4 LT |
506 | break; |
507 | } | |
508 | ||
509 | case MEMLOCK: | |
510 | { | |
511 | struct erase_info_user info; | |
512 | ||
513 | if (copy_from_user(&info, argp, sizeof(info))) | |
514 | return -EFAULT; | |
515 | ||
516 | if (!mtd->lock) | |
517 | ret = -EOPNOTSUPP; | |
518 | else | |
519 | ret = mtd->lock(mtd, info.start, info.length); | |
520 | break; | |
521 | } | |
522 | ||
523 | case MEMUNLOCK: | |
524 | { | |
525 | struct erase_info_user info; | |
526 | ||
527 | if (copy_from_user(&info, argp, sizeof(info))) | |
528 | return -EFAULT; | |
529 | ||
530 | if (!mtd->unlock) | |
531 | ret = -EOPNOTSUPP; | |
532 | else | |
533 | ret = mtd->unlock(mtd, info.start, info.length); | |
534 | break; | |
535 | } | |
536 | ||
5bd34c09 | 537 | /* Legacy interface */ |
1da177e4 LT |
538 | case MEMGETOOBSEL: |
539 | { | |
5bd34c09 TG |
540 | struct nand_oobinfo oi; |
541 | ||
542 | if (!mtd->ecclayout) | |
543 | return -EOPNOTSUPP; | |
544 | if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos)) | |
545 | return -EINVAL; | |
546 | ||
547 | oi.useecc = MTD_NANDECC_AUTOPLACE; | |
548 | memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos)); | |
549 | memcpy(&oi.oobfree, mtd->ecclayout->oobfree, | |
550 | sizeof(oi.oobfree)); | |
551 | ||
552 | if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo))) | |
1da177e4 LT |
553 | return -EFAULT; |
554 | break; | |
555 | } | |
556 | ||
5bd34c09 TG |
557 | case ECCGETLAYOUT: |
558 | ||
559 | if (!mtd->ecclayout) | |
560 | return -EOPNOTSUPP; | |
561 | ||
562 | if (copy_to_user(argp, &mtd->ecclayout, | |
563 | sizeof(struct nand_ecclayout))) | |
564 | return -EFAULT; | |
565 | break; | |
566 | ||
1da177e4 LT |
567 | case MEMGETBADBLOCK: |
568 | { | |
569 | loff_t offs; | |
97894cda | 570 | |
1da177e4 LT |
571 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
572 | return -EFAULT; | |
573 | if (!mtd->block_isbad) | |
574 | ret = -EOPNOTSUPP; | |
575 | else | |
576 | return mtd->block_isbad(mtd, offs); | |
577 | break; | |
578 | } | |
579 | ||
580 | case MEMSETBADBLOCK: | |
581 | { | |
582 | loff_t offs; | |
583 | ||
584 | if (copy_from_user(&offs, argp, sizeof(loff_t))) | |
585 | return -EFAULT; | |
586 | if (!mtd->block_markbad) | |
587 | ret = -EOPNOTSUPP; | |
588 | else | |
589 | return mtd->block_markbad(mtd, offs); | |
590 | break; | |
591 | } | |
592 | ||
493c6460 | 593 | #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP) |
31f4233b NP |
594 | case OTPSELECT: |
595 | { | |
596 | int mode; | |
597 | if (copy_from_user(&mode, argp, sizeof(int))) | |
598 | return -EFAULT; | |
045e9a5d | 599 | SET_MTD_MODE(file, 0); |
31f4233b NP |
600 | switch (mode) { |
601 | case MTD_OTP_FACTORY: | |
602 | if (!mtd->read_fact_prot_reg) | |
603 | ret = -EOPNOTSUPP; | |
604 | else | |
045e9a5d | 605 | SET_MTD_MODE(file, MTD_MODE_OTP_FACT); |
31f4233b NP |
606 | break; |
607 | case MTD_OTP_USER: | |
608 | if (!mtd->read_fact_prot_reg) | |
609 | ret = -EOPNOTSUPP; | |
610 | else | |
045e9a5d | 611 | SET_MTD_MODE(file, MTD_MODE_OTP_USER); |
31f4233b NP |
612 | break; |
613 | default: | |
614 | ret = -EINVAL; | |
615 | case MTD_OTP_OFF: | |
616 | break; | |
617 | } | |
81dba488 | 618 | file->f_pos = 0; |
31f4233b NP |
619 | break; |
620 | } | |
621 | ||
622 | case OTPGETREGIONCOUNT: | |
623 | case OTPGETREGIONINFO: | |
624 | { | |
625 | struct otp_info *buf = kmalloc(4096, GFP_KERNEL); | |
626 | if (!buf) | |
627 | return -ENOMEM; | |
628 | ret = -EOPNOTSUPP; | |
045e9a5d | 629 | switch (MTD_MODE(file)) { |
31f4233b NP |
630 | case MTD_MODE_OTP_FACT: |
631 | if (mtd->get_fact_prot_info) | |
632 | ret = mtd->get_fact_prot_info(mtd, buf, 4096); | |
633 | break; | |
634 | case MTD_MODE_OTP_USER: | |
635 | if (mtd->get_user_prot_info) | |
636 | ret = mtd->get_user_prot_info(mtd, buf, 4096); | |
637 | break; | |
638 | } | |
639 | if (ret >= 0) { | |
640 | if (cmd == OTPGETREGIONCOUNT) { | |
641 | int nbr = ret / sizeof(struct otp_info); | |
642 | ret = copy_to_user(argp, &nbr, sizeof(int)); | |
643 | } else | |
644 | ret = copy_to_user(argp, buf, ret); | |
645 | if (ret) | |
646 | ret = -EFAULT; | |
647 | } | |
648 | kfree(buf); | |
649 | break; | |
650 | } | |
651 | ||
652 | case OTPLOCK: | |
653 | { | |
654 | struct otp_info info; | |
655 | ||
045e9a5d | 656 | if (MTD_MODE(file) != MTD_MODE_OTP_USER) |
31f4233b NP |
657 | return -EINVAL; |
658 | if (copy_from_user(&info, argp, sizeof(info))) | |
659 | return -EFAULT; | |
660 | if (!mtd->lock_user_prot_reg) | |
661 | return -EOPNOTSUPP; | |
662 | ret = mtd->lock_user_prot_reg(mtd, info.start, info.length); | |
663 | break; | |
664 | } | |
665 | #endif | |
666 | ||
1da177e4 LT |
667 | default: |
668 | ret = -ENOTTY; | |
669 | } | |
670 | ||
671 | return ret; | |
672 | } /* memory_ioctl */ | |
673 | ||
674 | static struct file_operations mtd_fops = { | |
675 | .owner = THIS_MODULE, | |
676 | .llseek = mtd_lseek, | |
677 | .read = mtd_read, | |
678 | .write = mtd_write, | |
679 | .ioctl = mtd_ioctl, | |
680 | .open = mtd_open, | |
681 | .release = mtd_close, | |
682 | }; | |
683 | ||
684 | static int __init init_mtdchar(void) | |
685 | { | |
686 | if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) { | |
687 | printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n", | |
688 | MTD_CHAR_MAJOR); | |
689 | return -EAGAIN; | |
690 | } | |
691 | ||
9bc7b387 TP |
692 | mtd_class = class_create(THIS_MODULE, "mtd"); |
693 | ||
694 | if (IS_ERR(mtd_class)) { | |
695 | printk(KERN_ERR "Error creating mtd class.\n"); | |
696 | unregister_chrdev(MTD_CHAR_MAJOR, "mtd"); | |
3a7a8824 | 697 | return PTR_ERR(mtd_class); |
9bc7b387 TP |
698 | } |
699 | ||
700 | register_mtd_user(¬ifier); | |
1da177e4 LT |
701 | return 0; |
702 | } | |
703 | ||
704 | static void __exit cleanup_mtdchar(void) | |
705 | { | |
9bc7b387 TP |
706 | unregister_mtd_user(¬ifier); |
707 | class_destroy(mtd_class); | |
1da177e4 LT |
708 | unregister_chrdev(MTD_CHAR_MAJOR, "mtd"); |
709 | } | |
710 | ||
711 | module_init(init_mtdchar); | |
712 | module_exit(cleanup_mtdchar); | |
713 | ||
714 | ||
715 | MODULE_LICENSE("GPL"); | |
716 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
717 | MODULE_DESCRIPTION("Direct character-device access to MTD devices"); |