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> |
97718540 | 17 | #include <linux/compat.h> |
cd874237 | 18 | #include <linux/mount.h> |
15fdc52f | 19 | |
1da177e4 LT |
20 | #include <linux/mtd/mtd.h> |
21 | #include <linux/mtd/compatmac.h> | |
1da177e4 | 22 | |
15fdc52f | 23 | #include <asm/uaccess.h> |
9bc7b387 | 24 | |
cd874237 KS |
25 | #define MTD_INODE_FS_MAGIC 0x11307854 |
26 | static struct vfsmount *mtd_inode_mnt __read_mostly; | |
1da177e4 | 27 | |
045e9a5d | 28 | /* |
f1a28c02 TG |
29 | * Data structure to hold the pointer to the mtd device as well |
30 | * as mode information ofr various use cases. | |
045e9a5d | 31 | */ |
f1a28c02 TG |
32 | struct mtd_file_info { |
33 | struct mtd_info *mtd; | |
cd874237 | 34 | struct inode *ino; |
f1a28c02 TG |
35 | enum mtd_file_modes mode; |
36 | }; | |
31f4233b | 37 | |
1da177e4 LT |
38 | static loff_t mtd_lseek (struct file *file, loff_t offset, int orig) |
39 | { | |
f1a28c02 TG |
40 | struct mtd_file_info *mfi = file->private_data; |
41 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
42 | |
43 | switch (orig) { | |
ea59830d | 44 | case SEEK_SET: |
1da177e4 | 45 | break; |
ea59830d | 46 | case SEEK_CUR: |
8b491d75 | 47 | offset += file->f_pos; |
1da177e4 | 48 | break; |
ea59830d | 49 | case SEEK_END: |
8b491d75 | 50 | offset += mtd->size; |
1da177e4 LT |
51 | break; |
52 | default: | |
53 | return -EINVAL; | |
54 | } | |
55 | ||
1887f517 | 56 | if (offset >= 0 && offset <= mtd->size) |
8b491d75 | 57 | return file->f_pos = offset; |
1da177e4 | 58 | |
8b491d75 | 59 | return -EINVAL; |
1da177e4 LT |
60 | } |
61 | ||
62 | ||
63 | ||
64 | static int mtd_open(struct inode *inode, struct file *file) | |
65 | { | |
66 | int minor = iminor(inode); | |
67 | int devnum = minor >> 1; | |
6071239e | 68 | int ret = 0; |
1da177e4 | 69 | struct mtd_info *mtd; |
f1a28c02 | 70 | struct mtd_file_info *mfi; |
cd874237 | 71 | struct inode *mtd_ino; |
1da177e4 LT |
72 | |
73 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n"); | |
74 | ||
1da177e4 | 75 | /* You can't open the RO devices RW */ |
aeb5d727 | 76 | if ((file->f_mode & FMODE_WRITE) && (minor & 1)) |
1da177e4 LT |
77 | return -EACCES; |
78 | ||
6071239e | 79 | lock_kernel(); |
1da177e4 | 80 | mtd = get_mtd_device(NULL, devnum); |
97894cda | 81 | |
6071239e JC |
82 | if (IS_ERR(mtd)) { |
83 | ret = PTR_ERR(mtd); | |
84 | goto out; | |
85 | } | |
97894cda | 86 | |
402d3265 | 87 | if (mtd->type == MTD_ABSENT) { |
1da177e4 | 88 | put_mtd_device(mtd); |
6071239e JC |
89 | ret = -ENODEV; |
90 | goto out; | |
1da177e4 LT |
91 | } |
92 | ||
cd874237 KS |
93 | mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum); |
94 | if (!mtd_ino) { | |
95 | put_mtd_device(mtd); | |
96 | ret = -ENOMEM; | |
97 | goto out; | |
98 | } | |
99 | if (mtd_ino->i_state & I_NEW) { | |
100 | mtd_ino->i_private = mtd; | |
101 | mtd_ino->i_mode = S_IFCHR; | |
102 | mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info; | |
103 | unlock_new_inode(mtd_ino); | |
104 | } | |
105 | file->f_mapping = mtd_ino->i_mapping; | |
402d3265 | 106 | |
1da177e4 | 107 | /* You can't open it RW if it's not a writeable device */ |
aeb5d727 | 108 | if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) { |
cd874237 | 109 | iput(mtd_ino); |
1da177e4 | 110 | put_mtd_device(mtd); |
6071239e JC |
111 | ret = -EACCES; |
112 | goto out; | |
1da177e4 | 113 | } |
97894cda | 114 | |
f1a28c02 TG |
115 | mfi = kzalloc(sizeof(*mfi), GFP_KERNEL); |
116 | if (!mfi) { | |
cd874237 | 117 | iput(mtd_ino); |
f1a28c02 | 118 | put_mtd_device(mtd); |
6071239e JC |
119 | ret = -ENOMEM; |
120 | goto out; | |
f1a28c02 | 121 | } |
cd874237 | 122 | mfi->ino = mtd_ino; |
f1a28c02 TG |
123 | mfi->mtd = mtd; |
124 | file->private_data = mfi; | |
125 | ||
6071239e JC |
126 | out: |
127 | unlock_kernel(); | |
128 | return ret; | |
1da177e4 LT |
129 | } /* mtd_open */ |
130 | ||
131 | /*====================================================================*/ | |
132 | ||
133 | static int mtd_close(struct inode *inode, struct file *file) | |
134 | { | |
f1a28c02 TG |
135 | struct mtd_file_info *mfi = file->private_data; |
136 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
137 | |
138 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n"); | |
139 | ||
7eafaed5 | 140 | /* Only sync if opened RW */ |
aeb5d727 | 141 | if ((file->f_mode & FMODE_WRITE) && mtd->sync) |
1da177e4 | 142 | mtd->sync(mtd); |
97894cda | 143 | |
cd874237 KS |
144 | iput(mfi->ino); |
145 | ||
1da177e4 | 146 | put_mtd_device(mtd); |
f1a28c02 TG |
147 | file->private_data = NULL; |
148 | kfree(mfi); | |
1da177e4 LT |
149 | |
150 | return 0; | |
151 | } /* mtd_close */ | |
152 | ||
153 | /* FIXME: This _really_ needs to die. In 2.5, we should lock the | |
154 | userspace buffer down and use it directly with readv/writev. | |
155 | */ | |
156 | #define MAX_KMALLOC_SIZE 0x20000 | |
157 | ||
158 | static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos) | |
159 | { | |
f1a28c02 TG |
160 | struct mtd_file_info *mfi = file->private_data; |
161 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
162 | size_t retlen=0; |
163 | size_t total_retlen=0; | |
164 | int ret=0; | |
165 | int len; | |
166 | char *kbuf; | |
97894cda | 167 | |
1da177e4 LT |
168 | DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n"); |
169 | ||
170 | if (*ppos + count > mtd->size) | |
171 | count = mtd->size - *ppos; | |
172 | ||
173 | if (!count) | |
174 | return 0; | |
97894cda | 175 | |
1da177e4 LT |
176 | /* FIXME: Use kiovec in 2.5 to lock down the user's buffers |
177 | and pass them directly to the MTD functions */ | |
b802c074 TG |
178 | |
179 | if (count > MAX_KMALLOC_SIZE) | |
180 | kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL); | |
181 | else | |
182 | kbuf=kmalloc(count, GFP_KERNEL); | |
183 | ||
184 | if (!kbuf) | |
185 | return -ENOMEM; | |
186 | ||
1da177e4 | 187 | while (count) { |
b802c074 | 188 | |
97894cda | 189 | if (count > MAX_KMALLOC_SIZE) |
1da177e4 LT |
190 | len = MAX_KMALLOC_SIZE; |
191 | else | |
192 | len = count; | |
193 | ||
f1a28c02 TG |
194 | switch (mfi->mode) { |
195 | case MTD_MODE_OTP_FACTORY: | |
31f4233b NP |
196 | ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf); |
197 | break; | |
198 | case MTD_MODE_OTP_USER: | |
199 | ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf); | |
200 | break; | |
f1a28c02 TG |
201 | case MTD_MODE_RAW: |
202 | { | |
203 | struct mtd_oob_ops ops; | |
204 | ||
205 | ops.mode = MTD_OOB_RAW; | |
206 | ops.datbuf = kbuf; | |
207 | ops.oobbuf = NULL; | |
208 | ops.len = len; | |
209 | ||
210 | ret = mtd->read_oob(mtd, *ppos, &ops); | |
211 | retlen = ops.retlen; | |
212 | break; | |
213 | } | |
31f4233b | 214 | default: |
f4a43cfc | 215 | ret = mtd->read(mtd, *ppos, len, &retlen, kbuf); |
31f4233b | 216 | } |
1da177e4 LT |
217 | /* Nand returns -EBADMSG on ecc errors, but it returns |
218 | * the data. For our userspace tools it is important | |
97894cda | 219 | * to dump areas with ecc errors ! |
9a1fcdfd TG |
220 | * For kernel internal usage it also might return -EUCLEAN |
221 | * to signal the caller that a bitflip has occured and has | |
222 | * been corrected by the ECC algorithm. | |
1da177e4 LT |
223 | * Userspace software which accesses NAND this way |
224 | * must be aware of the fact that it deals with NAND | |
225 | */ | |
9a1fcdfd | 226 | if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) { |
1da177e4 LT |
227 | *ppos += retlen; |
228 | if (copy_to_user(buf, kbuf, retlen)) { | |
f4a43cfc | 229 | kfree(kbuf); |
1da177e4 LT |
230 | return -EFAULT; |
231 | } | |
232 | else | |
233 | total_retlen += retlen; | |
234 | ||
235 | count -= retlen; | |
236 | buf += retlen; | |
31f4233b NP |
237 | if (retlen == 0) |
238 | count = 0; | |
1da177e4 LT |
239 | } |
240 | else { | |
241 | kfree(kbuf); | |
242 | return ret; | |
243 | } | |
97894cda | 244 | |
1da177e4 LT |
245 | } |
246 | ||
b802c074 | 247 | kfree(kbuf); |
1da177e4 LT |
248 | return total_retlen; |
249 | } /* mtd_read */ | |
250 | ||
251 | static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos) | |
252 | { | |
f1a28c02 TG |
253 | struct mtd_file_info *mfi = file->private_data; |
254 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
255 | char *kbuf; |
256 | size_t retlen; | |
257 | size_t total_retlen=0; | |
258 | int ret=0; | |
259 | int len; | |
260 | ||
261 | DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n"); | |
97894cda | 262 | |
1da177e4 LT |
263 | if (*ppos == mtd->size) |
264 | return -ENOSPC; | |
97894cda | 265 | |
1da177e4 LT |
266 | if (*ppos + count > mtd->size) |
267 | count = mtd->size - *ppos; | |
268 | ||
269 | if (!count) | |
270 | return 0; | |
271 | ||
b802c074 TG |
272 | if (count > MAX_KMALLOC_SIZE) |
273 | kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL); | |
274 | else | |
275 | kbuf=kmalloc(count, GFP_KERNEL); | |
276 | ||
277 | if (!kbuf) | |
278 | return -ENOMEM; | |
279 | ||
1da177e4 | 280 | while (count) { |
b802c074 | 281 | |
97894cda | 282 | if (count > MAX_KMALLOC_SIZE) |
1da177e4 LT |
283 | len = MAX_KMALLOC_SIZE; |
284 | else | |
285 | len = count; | |
286 | ||
1da177e4 LT |
287 | if (copy_from_user(kbuf, buf, len)) { |
288 | kfree(kbuf); | |
289 | return -EFAULT; | |
290 | } | |
97894cda | 291 | |
f1a28c02 TG |
292 | switch (mfi->mode) { |
293 | case MTD_MODE_OTP_FACTORY: | |
31f4233b NP |
294 | ret = -EROFS; |
295 | break; | |
296 | case MTD_MODE_OTP_USER: | |
297 | if (!mtd->write_user_prot_reg) { | |
298 | ret = -EOPNOTSUPP; | |
299 | break; | |
300 | } | |
301 | ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf); | |
302 | break; | |
f1a28c02 TG |
303 | |
304 | case MTD_MODE_RAW: | |
305 | { | |
306 | struct mtd_oob_ops ops; | |
307 | ||
308 | ops.mode = MTD_OOB_RAW; | |
309 | ops.datbuf = kbuf; | |
310 | ops.oobbuf = NULL; | |
311 | ops.len = len; | |
312 | ||
313 | ret = mtd->write_oob(mtd, *ppos, &ops); | |
314 | retlen = ops.retlen; | |
315 | break; | |
316 | } | |
317 | ||
31f4233b NP |
318 | default: |
319 | ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf); | |
320 | } | |
1da177e4 LT |
321 | if (!ret) { |
322 | *ppos += retlen; | |
323 | total_retlen += retlen; | |
324 | count -= retlen; | |
325 | buf += retlen; | |
326 | } | |
327 | else { | |
328 | kfree(kbuf); | |
329 | return ret; | |
330 | } | |
1da177e4 LT |
331 | } |
332 | ||
b802c074 | 333 | kfree(kbuf); |
1da177e4 LT |
334 | return total_retlen; |
335 | } /* mtd_write */ | |
336 | ||
337 | /*====================================================================== | |
338 | ||
339 | IOCTL calls for getting device parameters. | |
340 | ||
341 | ======================================================================*/ | |
342 | static void mtdchar_erase_callback (struct erase_info *instr) | |
343 | { | |
344 | wake_up((wait_queue_head_t *)instr->priv); | |
345 | } | |
346 | ||
34a82443 | 347 | #ifdef CONFIG_HAVE_MTD_OTP |
f1a28c02 TG |
348 | static int otp_select_filemode(struct mtd_file_info *mfi, int mode) |
349 | { | |
350 | struct mtd_info *mtd = mfi->mtd; | |
351 | int ret = 0; | |
352 | ||
353 | switch (mode) { | |
354 | case MTD_OTP_FACTORY: | |
355 | if (!mtd->read_fact_prot_reg) | |
356 | ret = -EOPNOTSUPP; | |
357 | else | |
358 | mfi->mode = MTD_MODE_OTP_FACTORY; | |
359 | break; | |
360 | case MTD_OTP_USER: | |
361 | if (!mtd->read_fact_prot_reg) | |
362 | ret = -EOPNOTSUPP; | |
363 | else | |
364 | mfi->mode = MTD_MODE_OTP_USER; | |
365 | break; | |
366 | default: | |
367 | ret = -EINVAL; | |
368 | case MTD_OTP_OFF: | |
369 | break; | |
370 | } | |
371 | return ret; | |
372 | } | |
373 | #else | |
374 | # define otp_select_filemode(f,m) -EOPNOTSUPP | |
375 | #endif | |
376 | ||
97718540 KC |
377 | static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd, |
378 | uint64_t start, uint32_t length, void __user *ptr, | |
379 | uint32_t __user *retp) | |
380 | { | |
381 | struct mtd_oob_ops ops; | |
382 | uint32_t retlen; | |
383 | int ret = 0; | |
384 | ||
385 | if (!(file->f_mode & FMODE_WRITE)) | |
386 | return -EPERM; | |
387 | ||
388 | if (length > 4096) | |
389 | return -EINVAL; | |
390 | ||
391 | if (!mtd->write_oob) | |
392 | ret = -EOPNOTSUPP; | |
393 | else | |
0040476b | 394 | ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT; |
97718540 KC |
395 | |
396 | if (ret) | |
397 | return ret; | |
398 | ||
399 | ops.ooblen = length; | |
400 | ops.ooboffs = start & (mtd->oobsize - 1); | |
401 | ops.datbuf = NULL; | |
402 | ops.mode = MTD_OOB_PLACE; | |
403 | ||
404 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) | |
405 | return -EINVAL; | |
406 | ||
df1f1d1c JL |
407 | ops.oobbuf = memdup_user(ptr, length); |
408 | if (IS_ERR(ops.oobbuf)) | |
409 | return PTR_ERR(ops.oobbuf); | |
97718540 KC |
410 | |
411 | start &= ~((uint64_t)mtd->oobsize - 1); | |
412 | ret = mtd->write_oob(mtd, start, &ops); | |
413 | ||
414 | if (ops.oobretlen > 0xFFFFFFFFU) | |
415 | ret = -EOVERFLOW; | |
416 | retlen = ops.oobretlen; | |
417 | if (copy_to_user(retp, &retlen, sizeof(length))) | |
418 | ret = -EFAULT; | |
419 | ||
420 | kfree(ops.oobbuf); | |
421 | return ret; | |
422 | } | |
423 | ||
424 | static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start, | |
425 | uint32_t length, void __user *ptr, uint32_t __user *retp) | |
426 | { | |
427 | struct mtd_oob_ops ops; | |
428 | int ret = 0; | |
429 | ||
430 | if (length > 4096) | |
431 | return -EINVAL; | |
432 | ||
433 | if (!mtd->read_oob) | |
434 | ret = -EOPNOTSUPP; | |
435 | else | |
436 | ret = access_ok(VERIFY_WRITE, ptr, | |
437 | length) ? 0 : -EFAULT; | |
438 | if (ret) | |
439 | return ret; | |
440 | ||
441 | ops.ooblen = length; | |
442 | ops.ooboffs = start & (mtd->oobsize - 1); | |
443 | ops.datbuf = NULL; | |
444 | ops.mode = MTD_OOB_PLACE; | |
445 | ||
446 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) | |
447 | return -EINVAL; | |
448 | ||
449 | ops.oobbuf = kmalloc(length, GFP_KERNEL); | |
450 | if (!ops.oobbuf) | |
451 | return -ENOMEM; | |
452 | ||
453 | start &= ~((uint64_t)mtd->oobsize - 1); | |
454 | ret = mtd->read_oob(mtd, start, &ops); | |
455 | ||
456 | if (put_user(ops.oobretlen, retp)) | |
457 | ret = -EFAULT; | |
458 | else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf, | |
459 | ops.oobretlen)) | |
460 | ret = -EFAULT; | |
461 | ||
462 | kfree(ops.oobbuf); | |
463 | return ret; | |
464 | } | |
465 | ||
1da177e4 LT |
466 | static int mtd_ioctl(struct inode *inode, struct file *file, |
467 | u_int cmd, u_long arg) | |
468 | { | |
f1a28c02 TG |
469 | struct mtd_file_info *mfi = file->private_data; |
470 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
471 | void __user *argp = (void __user *)arg; |
472 | int ret = 0; | |
473 | u_long size; | |
73c619ea | 474 | struct mtd_info_user info; |
97894cda | 475 | |
1da177e4 LT |
476 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n"); |
477 | ||
478 | size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT; | |
479 | if (cmd & IOC_IN) { | |
480 | if (!access_ok(VERIFY_READ, argp, size)) | |
481 | return -EFAULT; | |
482 | } | |
483 | if (cmd & IOC_OUT) { | |
484 | if (!access_ok(VERIFY_WRITE, argp, size)) | |
485 | return -EFAULT; | |
486 | } | |
97894cda | 487 | |
1da177e4 LT |
488 | switch (cmd) { |
489 | case MEMGETREGIONCOUNT: | |
490 | if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int))) | |
491 | return -EFAULT; | |
492 | break; | |
493 | ||
494 | case MEMGETREGIONINFO: | |
495 | { | |
b67c5f87 ZW |
496 | uint32_t ur_idx; |
497 | struct mtd_erase_region_info *kr; | |
bcc98a46 | 498 | struct region_info_user __user *ur = argp; |
1da177e4 | 499 | |
b67c5f87 | 500 | if (get_user(ur_idx, &(ur->regionindex))) |
1da177e4 LT |
501 | return -EFAULT; |
502 | ||
b67c5f87 ZW |
503 | kr = &(mtd->eraseregions[ur_idx]); |
504 | ||
505 | if (put_user(kr->offset, &(ur->offset)) | |
506 | || put_user(kr->erasesize, &(ur->erasesize)) | |
507 | || put_user(kr->numblocks, &(ur->numblocks))) | |
1da177e4 | 508 | return -EFAULT; |
b67c5f87 | 509 | |
1da177e4 LT |
510 | break; |
511 | } | |
512 | ||
513 | case MEMGETINFO: | |
73c619ea JE |
514 | info.type = mtd->type; |
515 | info.flags = mtd->flags; | |
516 | info.size = mtd->size; | |
517 | info.erasesize = mtd->erasesize; | |
518 | info.writesize = mtd->writesize; | |
519 | info.oobsize = mtd->oobsize; | |
64f60710 AB |
520 | /* The below fields are obsolete */ |
521 | info.ecctype = -1; | |
522 | info.eccsize = 0; | |
73c619ea | 523 | if (copy_to_user(argp, &info, sizeof(struct mtd_info_user))) |
1da177e4 LT |
524 | return -EFAULT; |
525 | break; | |
526 | ||
527 | case MEMERASE: | |
0dc54e9f | 528 | case MEMERASE64: |
1da177e4 LT |
529 | { |
530 | struct erase_info *erase; | |
531 | ||
aeb5d727 | 532 | if(!(file->f_mode & FMODE_WRITE)) |
1da177e4 LT |
533 | return -EPERM; |
534 | ||
95b93a0c | 535 | erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL); |
1da177e4 LT |
536 | if (!erase) |
537 | ret = -ENOMEM; | |
538 | else { | |
539 | wait_queue_head_t waitq; | |
540 | DECLARE_WAITQUEUE(wait, current); | |
541 | ||
542 | init_waitqueue_head(&waitq); | |
543 | ||
0dc54e9f KC |
544 | if (cmd == MEMERASE64) { |
545 | struct erase_info_user64 einfo64; | |
546 | ||
547 | if (copy_from_user(&einfo64, argp, | |
548 | sizeof(struct erase_info_user64))) { | |
549 | kfree(erase); | |
550 | return -EFAULT; | |
551 | } | |
552 | erase->addr = einfo64.start; | |
553 | erase->len = einfo64.length; | |
554 | } else { | |
555 | struct erase_info_user einfo32; | |
556 | ||
557 | if (copy_from_user(&einfo32, argp, | |
558 | sizeof(struct erase_info_user))) { | |
559 | kfree(erase); | |
560 | return -EFAULT; | |
561 | } | |
562 | erase->addr = einfo32.start; | |
563 | erase->len = einfo32.length; | |
1da177e4 LT |
564 | } |
565 | erase->mtd = mtd; | |
566 | erase->callback = mtdchar_erase_callback; | |
567 | erase->priv = (unsigned long)&waitq; | |
97894cda | 568 | |
1da177e4 LT |
569 | /* |
570 | FIXME: Allow INTERRUPTIBLE. Which means | |
571 | not having the wait_queue head on the stack. | |
97894cda | 572 | |
1da177e4 LT |
573 | If the wq_head is on the stack, and we |
574 | leave because we got interrupted, then the | |
575 | wq_head is no longer there when the | |
576 | callback routine tries to wake us up. | |
577 | */ | |
578 | ret = mtd->erase(mtd, erase); | |
579 | if (!ret) { | |
580 | set_current_state(TASK_UNINTERRUPTIBLE); | |
581 | add_wait_queue(&waitq, &wait); | |
582 | if (erase->state != MTD_ERASE_DONE && | |
583 | erase->state != MTD_ERASE_FAILED) | |
584 | schedule(); | |
585 | remove_wait_queue(&waitq, &wait); | |
586 | set_current_state(TASK_RUNNING); | |
587 | ||
588 | ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0; | |
589 | } | |
590 | kfree(erase); | |
591 | } | |
592 | break; | |
593 | } | |
594 | ||
595 | case MEMWRITEOOB: | |
596 | { | |
597 | struct mtd_oob_buf buf; | |
97718540 | 598 | struct mtd_oob_buf __user *buf_user = argp; |
1da177e4 | 599 | |
97718540 KC |
600 | /* NOTE: writes return length to buf_user->length */ |
601 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
1da177e4 | 602 | ret = -EFAULT; |
97718540 KC |
603 | else |
604 | ret = mtd_do_writeoob(file, mtd, buf.start, buf.length, | |
605 | buf.ptr, &buf_user->length); | |
1da177e4 | 606 | break; |
1da177e4 LT |
607 | } |
608 | ||
609 | case MEMREADOOB: | |
610 | { | |
611 | struct mtd_oob_buf buf; | |
97718540 | 612 | struct mtd_oob_buf __user *buf_user = argp; |
8593fbc6 | 613 | |
97718540 KC |
614 | /* NOTE: writes return length to buf_user->start */ |
615 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
1da177e4 | 616 | ret = -EFAULT; |
97718540 KC |
617 | else |
618 | ret = mtd_do_readoob(mtd, buf.start, buf.length, | |
619 | buf.ptr, &buf_user->start); | |
1da177e4 LT |
620 | break; |
621 | } | |
622 | ||
aea7cea9 KC |
623 | case MEMWRITEOOB64: |
624 | { | |
625 | struct mtd_oob_buf64 buf; | |
626 | struct mtd_oob_buf64 __user *buf_user = argp; | |
627 | ||
628 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
629 | ret = -EFAULT; | |
630 | else | |
631 | ret = mtd_do_writeoob(file, mtd, buf.start, buf.length, | |
632 | (void __user *)(uintptr_t)buf.usr_ptr, | |
633 | &buf_user->length); | |
634 | break; | |
635 | } | |
636 | ||
637 | case MEMREADOOB64: | |
638 | { | |
639 | struct mtd_oob_buf64 buf; | |
640 | struct mtd_oob_buf64 __user *buf_user = argp; | |
641 | ||
642 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
643 | ret = -EFAULT; | |
644 | else | |
645 | ret = mtd_do_readoob(mtd, buf.start, buf.length, | |
646 | (void __user *)(uintptr_t)buf.usr_ptr, | |
647 | &buf_user->length); | |
648 | break; | |
649 | } | |
650 | ||
1da177e4 LT |
651 | case MEMLOCK: |
652 | { | |
175428b2 | 653 | struct erase_info_user einfo; |
1da177e4 | 654 | |
175428b2 | 655 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
1da177e4 LT |
656 | return -EFAULT; |
657 | ||
658 | if (!mtd->lock) | |
659 | ret = -EOPNOTSUPP; | |
660 | else | |
175428b2 | 661 | ret = mtd->lock(mtd, einfo.start, einfo.length); |
1da177e4 LT |
662 | break; |
663 | } | |
664 | ||
665 | case MEMUNLOCK: | |
666 | { | |
175428b2 | 667 | struct erase_info_user einfo; |
1da177e4 | 668 | |
175428b2 | 669 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
1da177e4 LT |
670 | return -EFAULT; |
671 | ||
672 | if (!mtd->unlock) | |
673 | ret = -EOPNOTSUPP; | |
674 | else | |
175428b2 | 675 | ret = mtd->unlock(mtd, einfo.start, einfo.length); |
1da177e4 LT |
676 | break; |
677 | } | |
678 | ||
9938424f RC |
679 | case MEMISLOCKED: |
680 | { | |
681 | struct erase_info_user einfo; | |
682 | ||
683 | if (copy_from_user(&einfo, argp, sizeof(einfo))) | |
684 | return -EFAULT; | |
685 | ||
686 | if (!mtd->is_locked) | |
687 | ret = -EOPNOTSUPP; | |
688 | else | |
689 | ret = mtd->is_locked(mtd, einfo.start, einfo.length); | |
690 | break; | |
691 | } | |
692 | ||
5bd34c09 | 693 | /* Legacy interface */ |
1da177e4 LT |
694 | case MEMGETOOBSEL: |
695 | { | |
5bd34c09 TG |
696 | struct nand_oobinfo oi; |
697 | ||
698 | if (!mtd->ecclayout) | |
699 | return -EOPNOTSUPP; | |
700 | if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos)) | |
701 | return -EINVAL; | |
702 | ||
703 | oi.useecc = MTD_NANDECC_AUTOPLACE; | |
704 | memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos)); | |
705 | memcpy(&oi.oobfree, mtd->ecclayout->oobfree, | |
706 | sizeof(oi.oobfree)); | |
d25ade71 | 707 | oi.eccbytes = mtd->ecclayout->eccbytes; |
5bd34c09 TG |
708 | |
709 | if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo))) | |
1da177e4 LT |
710 | return -EFAULT; |
711 | break; | |
712 | } | |
713 | ||
714 | case MEMGETBADBLOCK: | |
715 | { | |
716 | loff_t offs; | |
97894cda | 717 | |
1da177e4 LT |
718 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
719 | return -EFAULT; | |
720 | if (!mtd->block_isbad) | |
721 | ret = -EOPNOTSUPP; | |
722 | else | |
723 | return mtd->block_isbad(mtd, offs); | |
724 | break; | |
725 | } | |
726 | ||
727 | case MEMSETBADBLOCK: | |
728 | { | |
729 | loff_t offs; | |
730 | ||
731 | if (copy_from_user(&offs, argp, sizeof(loff_t))) | |
732 | return -EFAULT; | |
733 | if (!mtd->block_markbad) | |
734 | ret = -EOPNOTSUPP; | |
735 | else | |
736 | return mtd->block_markbad(mtd, offs); | |
737 | break; | |
738 | } | |
739 | ||
34a82443 | 740 | #ifdef CONFIG_HAVE_MTD_OTP |
31f4233b NP |
741 | case OTPSELECT: |
742 | { | |
743 | int mode; | |
744 | if (copy_from_user(&mode, argp, sizeof(int))) | |
745 | return -EFAULT; | |
f1a28c02 TG |
746 | |
747 | mfi->mode = MTD_MODE_NORMAL; | |
748 | ||
749 | ret = otp_select_filemode(mfi, mode); | |
750 | ||
81dba488 | 751 | file->f_pos = 0; |
31f4233b NP |
752 | break; |
753 | } | |
754 | ||
755 | case OTPGETREGIONCOUNT: | |
756 | case OTPGETREGIONINFO: | |
757 | { | |
758 | struct otp_info *buf = kmalloc(4096, GFP_KERNEL); | |
759 | if (!buf) | |
760 | return -ENOMEM; | |
761 | ret = -EOPNOTSUPP; | |
f1a28c02 TG |
762 | switch (mfi->mode) { |
763 | case MTD_MODE_OTP_FACTORY: | |
31f4233b NP |
764 | if (mtd->get_fact_prot_info) |
765 | ret = mtd->get_fact_prot_info(mtd, buf, 4096); | |
766 | break; | |
767 | case MTD_MODE_OTP_USER: | |
768 | if (mtd->get_user_prot_info) | |
769 | ret = mtd->get_user_prot_info(mtd, buf, 4096); | |
770 | break; | |
f1a28c02 TG |
771 | default: |
772 | break; | |
31f4233b NP |
773 | } |
774 | if (ret >= 0) { | |
775 | if (cmd == OTPGETREGIONCOUNT) { | |
776 | int nbr = ret / sizeof(struct otp_info); | |
777 | ret = copy_to_user(argp, &nbr, sizeof(int)); | |
778 | } else | |
779 | ret = copy_to_user(argp, buf, ret); | |
780 | if (ret) | |
781 | ret = -EFAULT; | |
782 | } | |
783 | kfree(buf); | |
784 | break; | |
785 | } | |
786 | ||
787 | case OTPLOCK: | |
788 | { | |
175428b2 | 789 | struct otp_info oinfo; |
31f4233b | 790 | |
f1a28c02 | 791 | if (mfi->mode != MTD_MODE_OTP_USER) |
31f4233b | 792 | return -EINVAL; |
175428b2 | 793 | if (copy_from_user(&oinfo, argp, sizeof(oinfo))) |
31f4233b NP |
794 | return -EFAULT; |
795 | if (!mtd->lock_user_prot_reg) | |
796 | return -EOPNOTSUPP; | |
175428b2 | 797 | ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length); |
31f4233b NP |
798 | break; |
799 | } | |
800 | #endif | |
801 | ||
f1a28c02 TG |
802 | case ECCGETLAYOUT: |
803 | { | |
804 | if (!mtd->ecclayout) | |
805 | return -EOPNOTSUPP; | |
806 | ||
d25ade71 | 807 | if (copy_to_user(argp, mtd->ecclayout, |
f1a28c02 TG |
808 | sizeof(struct nand_ecclayout))) |
809 | return -EFAULT; | |
810 | break; | |
811 | } | |
812 | ||
813 | case ECCGETSTATS: | |
814 | { | |
815 | if (copy_to_user(argp, &mtd->ecc_stats, | |
816 | sizeof(struct mtd_ecc_stats))) | |
817 | return -EFAULT; | |
818 | break; | |
819 | } | |
820 | ||
821 | case MTDFILEMODE: | |
822 | { | |
823 | mfi->mode = 0; | |
824 | ||
825 | switch(arg) { | |
826 | case MTD_MODE_OTP_FACTORY: | |
827 | case MTD_MODE_OTP_USER: | |
828 | ret = otp_select_filemode(mfi, arg); | |
829 | break; | |
830 | ||
831 | case MTD_MODE_RAW: | |
832 | if (!mtd->read_oob || !mtd->write_oob) | |
833 | return -EOPNOTSUPP; | |
834 | mfi->mode = arg; | |
835 | ||
836 | case MTD_MODE_NORMAL: | |
837 | break; | |
838 | default: | |
839 | ret = -EINVAL; | |
840 | } | |
841 | file->f_pos = 0; | |
842 | break; | |
843 | } | |
844 | ||
1da177e4 LT |
845 | default: |
846 | ret = -ENOTTY; | |
847 | } | |
848 | ||
849 | return ret; | |
850 | } /* memory_ioctl */ | |
851 | ||
97718540 KC |
852 | #ifdef CONFIG_COMPAT |
853 | ||
854 | struct mtd_oob_buf32 { | |
855 | u_int32_t start; | |
856 | u_int32_t length; | |
857 | compat_caddr_t ptr; /* unsigned char* */ | |
858 | }; | |
859 | ||
860 | #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32) | |
861 | #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32) | |
862 | ||
863 | static long mtd_compat_ioctl(struct file *file, unsigned int cmd, | |
864 | unsigned long arg) | |
865 | { | |
668ff9ab | 866 | struct inode *inode = file->f_path.dentry->d_inode; |
97718540 KC |
867 | struct mtd_file_info *mfi = file->private_data; |
868 | struct mtd_info *mtd = mfi->mtd; | |
0b6585ce | 869 | void __user *argp = compat_ptr(arg); |
97718540 KC |
870 | int ret = 0; |
871 | ||
872 | lock_kernel(); | |
873 | ||
874 | switch (cmd) { | |
875 | case MEMWRITEOOB32: | |
876 | { | |
877 | struct mtd_oob_buf32 buf; | |
878 | struct mtd_oob_buf32 __user *buf_user = argp; | |
879 | ||
880 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
881 | ret = -EFAULT; | |
882 | else | |
883 | ret = mtd_do_writeoob(file, mtd, buf.start, | |
884 | buf.length, compat_ptr(buf.ptr), | |
885 | &buf_user->length); | |
886 | break; | |
887 | } | |
888 | ||
889 | case MEMREADOOB32: | |
890 | { | |
891 | struct mtd_oob_buf32 buf; | |
892 | struct mtd_oob_buf32 __user *buf_user = argp; | |
893 | ||
894 | /* NOTE: writes return length to buf->start */ | |
895 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
896 | ret = -EFAULT; | |
897 | else | |
898 | ret = mtd_do_readoob(mtd, buf.start, | |
899 | buf.length, compat_ptr(buf.ptr), | |
900 | &buf_user->start); | |
901 | break; | |
902 | } | |
903 | default: | |
0b6585ce | 904 | ret = mtd_ioctl(inode, file, cmd, (unsigned long)argp); |
97718540 KC |
905 | } |
906 | ||
907 | unlock_kernel(); | |
908 | ||
909 | return ret; | |
910 | } | |
911 | ||
912 | #endif /* CONFIG_COMPAT */ | |
913 | ||
402d3265 DH |
914 | /* |
915 | * try to determine where a shared mapping can be made | |
916 | * - only supported for NOMMU at the moment (MMU can't doesn't copy private | |
917 | * mappings) | |
918 | */ | |
919 | #ifndef CONFIG_MMU | |
920 | static unsigned long mtd_get_unmapped_area(struct file *file, | |
921 | unsigned long addr, | |
922 | unsigned long len, | |
923 | unsigned long pgoff, | |
924 | unsigned long flags) | |
925 | { | |
926 | struct mtd_file_info *mfi = file->private_data; | |
927 | struct mtd_info *mtd = mfi->mtd; | |
928 | ||
929 | if (mtd->get_unmapped_area) { | |
930 | unsigned long offset; | |
931 | ||
932 | if (addr != 0) | |
933 | return (unsigned long) -EINVAL; | |
934 | ||
935 | if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT)) | |
936 | return (unsigned long) -EINVAL; | |
937 | ||
938 | offset = pgoff << PAGE_SHIFT; | |
939 | if (offset > mtd->size - len) | |
940 | return (unsigned long) -EINVAL; | |
941 | ||
942 | return mtd->get_unmapped_area(mtd, len, offset, flags); | |
943 | } | |
944 | ||
945 | /* can't map directly */ | |
946 | return (unsigned long) -ENOSYS; | |
947 | } | |
948 | #endif | |
949 | ||
950 | /* | |
951 | * set up a mapping for shared memory segments | |
952 | */ | |
953 | static int mtd_mmap(struct file *file, struct vm_area_struct *vma) | |
954 | { | |
955 | #ifdef CONFIG_MMU | |
956 | struct mtd_file_info *mfi = file->private_data; | |
957 | struct mtd_info *mtd = mfi->mtd; | |
958 | ||
959 | if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) | |
960 | return 0; | |
961 | return -ENOSYS; | |
962 | #else | |
963 | return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS; | |
964 | #endif | |
965 | } | |
966 | ||
d54b1fdb | 967 | static const struct file_operations mtd_fops = { |
1da177e4 LT |
968 | .owner = THIS_MODULE, |
969 | .llseek = mtd_lseek, | |
970 | .read = mtd_read, | |
971 | .write = mtd_write, | |
972 | .ioctl = mtd_ioctl, | |
97718540 KC |
973 | #ifdef CONFIG_COMPAT |
974 | .compat_ioctl = mtd_compat_ioctl, | |
975 | #endif | |
1da177e4 LT |
976 | .open = mtd_open, |
977 | .release = mtd_close, | |
402d3265 DH |
978 | .mmap = mtd_mmap, |
979 | #ifndef CONFIG_MMU | |
980 | .get_unmapped_area = mtd_get_unmapped_area, | |
981 | #endif | |
1da177e4 LT |
982 | }; |
983 | ||
cd874237 KS |
984 | static int mtd_inodefs_get_sb(struct file_system_type *fs_type, int flags, |
985 | const char *dev_name, void *data, | |
986 | struct vfsmount *mnt) | |
987 | { | |
988 | return get_sb_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC, | |
989 | mnt); | |
990 | } | |
991 | ||
992 | static struct file_system_type mtd_inodefs_type = { | |
993 | .name = "mtd_inodefs", | |
994 | .get_sb = mtd_inodefs_get_sb, | |
995 | .kill_sb = kill_anon_super, | |
996 | }; | |
997 | ||
998 | static void mtdchar_notify_add(struct mtd_info *mtd) | |
999 | { | |
1000 | } | |
1001 | ||
1002 | static void mtdchar_notify_remove(struct mtd_info *mtd) | |
1003 | { | |
1004 | struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index); | |
1005 | ||
1006 | if (mtd_ino) { | |
1007 | /* Destroy the inode if it exists */ | |
1008 | mtd_ino->i_nlink = 0; | |
1009 | iput(mtd_ino); | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | static struct mtd_notifier mtdchar_notifier = { | |
1014 | .add = mtdchar_notify_add, | |
1015 | .remove = mtdchar_notify_remove, | |
1016 | }; | |
1017 | ||
1da177e4 LT |
1018 | static int __init init_mtdchar(void) |
1019 | { | |
cd874237 | 1020 | int ret; |
1f24b5a8 | 1021 | |
cd874237 | 1022 | ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, |
dad0db31 | 1023 | "mtd", &mtd_fops); |
cd874237 KS |
1024 | if (ret < 0) { |
1025 | pr_notice("Can't allocate major number %d for " | |
1026 | "Memory Technology Devices.\n", MTD_CHAR_MAJOR); | |
1027 | return ret; | |
9bc7b387 TP |
1028 | } |
1029 | ||
cd874237 KS |
1030 | ret = register_filesystem(&mtd_inodefs_type); |
1031 | if (ret) { | |
1032 | pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret); | |
1033 | goto err_unregister_chdev; | |
1034 | } | |
1035 | ||
1036 | mtd_inode_mnt = kern_mount(&mtd_inodefs_type); | |
1037 | if (IS_ERR(mtd_inode_mnt)) { | |
1038 | ret = PTR_ERR(mtd_inode_mnt); | |
1039 | pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret); | |
1040 | goto err_unregister_filesystem; | |
1041 | } | |
1042 | register_mtd_user(&mtdchar_notifier); | |
1043 | ||
1044 | return ret; | |
1045 | ||
1046 | err_unregister_filesystem: | |
1047 | unregister_filesystem(&mtd_inodefs_type); | |
1048 | err_unregister_chdev: | |
1049 | __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd"); | |
1050 | return ret; | |
1da177e4 LT |
1051 | } |
1052 | ||
1053 | static void __exit cleanup_mtdchar(void) | |
1054 | { | |
cd874237 KS |
1055 | unregister_mtd_user(&mtdchar_notifier); |
1056 | mntput(mtd_inode_mnt); | |
1057 | unregister_filesystem(&mtd_inodefs_type); | |
dad0db31 | 1058 | __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd"); |
1da177e4 LT |
1059 | } |
1060 | ||
1061 | module_init(init_mtdchar); | |
1062 | module_exit(cleanup_mtdchar); | |
1063 | ||
1f24b5a8 | 1064 | MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR); |
1da177e4 LT |
1065 | |
1066 | MODULE_LICENSE("GPL"); | |
1067 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
1068 | MODULE_DESCRIPTION("Direct character-device access to MTD devices"); | |
90160e13 | 1069 | MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR); |