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