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