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