Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Core registration and callback routines for MTD |
3 | * drivers and users. | |
4 | * | |
5 | */ | |
6 | ||
1da177e4 LT |
7 | #include <linux/module.h> |
8 | #include <linux/kernel.h> | |
1da177e4 LT |
9 | #include <linux/ptrace.h> |
10 | #include <linux/slab.h> | |
11 | #include <linux/string.h> | |
12 | #include <linux/timer.h> | |
13 | #include <linux/major.h> | |
14 | #include <linux/fs.h> | |
7799308f | 15 | #include <linux/err.h> |
1da177e4 LT |
16 | #include <linux/ioctl.h> |
17 | #include <linux/init.h> | |
18 | #include <linux/mtd/compatmac.h> | |
1da177e4 | 19 | #include <linux/proc_fs.h> |
1da177e4 LT |
20 | |
21 | #include <linux/mtd/mtd.h> | |
402d3265 | 22 | #include "internal.h" |
1da177e4 | 23 | |
356d70f1 BD |
24 | #include "mtdcore.h" |
25 | ||
15bce40c DW |
26 | static int mtd_cls_suspend(struct device *dev, pm_message_t state); |
27 | static int mtd_cls_resume(struct device *dev); | |
28 | ||
29 | static struct class mtd_class = { | |
30 | .name = "mtd", | |
31 | .owner = THIS_MODULE, | |
32 | .suspend = mtd_cls_suspend, | |
33 | .resume = mtd_cls_resume, | |
34 | }; | |
1f24b5a8 | 35 | |
97894cda | 36 | /* These are exported solely for the purpose of mtd_blkdevs.c. You |
1da177e4 | 37 | should not use them for _anything_ else */ |
48b19268 | 38 | DEFINE_MUTEX(mtd_table_mutex); |
1da177e4 LT |
39 | struct mtd_info *mtd_table[MAX_MTD_DEVICES]; |
40 | ||
41 | EXPORT_SYMBOL_GPL(mtd_table_mutex); | |
42 | EXPORT_SYMBOL_GPL(mtd_table); | |
43 | ||
44 | static LIST_HEAD(mtd_notifiers); | |
45 | ||
1f24b5a8 DB |
46 | |
47 | #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE) | |
48 | #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) | |
49 | #else | |
50 | #define MTD_DEVT(index) 0 | |
51 | #endif | |
52 | ||
53 | /* REVISIT once MTD uses the driver model better, whoever allocates | |
54 | * the mtd_info will probably want to use the release() hook... | |
55 | */ | |
56 | static void mtd_release(struct device *dev) | |
57 | { | |
2fdb1144 | 58 | dev_t index = MTD_DEVT(dev_to_mtd(dev)->index); |
1f24b5a8 DB |
59 | |
60 | /* remove /dev/mtdXro node if needed */ | |
2fdb1144 | 61 | if (index) |
15bce40c DW |
62 | device_destroy(&mtd_class, index + 1); |
63 | } | |
64 | ||
65 | static int mtd_cls_suspend(struct device *dev, pm_message_t state) | |
66 | { | |
67 | struct mtd_info *mtd = dev_to_mtd(dev); | |
6afc4fdb SB |
68 | |
69 | if (mtd && mtd->suspend) | |
15bce40c DW |
70 | return mtd->suspend(mtd); |
71 | else | |
72 | return 0; | |
73 | } | |
74 | ||
75 | static int mtd_cls_resume(struct device *dev) | |
76 | { | |
77 | struct mtd_info *mtd = dev_to_mtd(dev); | |
78 | ||
6afc4fdb | 79 | if (mtd && mtd->resume) |
15bce40c DW |
80 | mtd->resume(mtd); |
81 | return 0; | |
1f24b5a8 DB |
82 | } |
83 | ||
84 | static ssize_t mtd_type_show(struct device *dev, | |
85 | struct device_attribute *attr, char *buf) | |
86 | { | |
87 | struct mtd_info *mtd = dev_to_mtd(dev); | |
88 | char *type; | |
89 | ||
90 | switch (mtd->type) { | |
91 | case MTD_ABSENT: | |
92 | type = "absent"; | |
93 | break; | |
94 | case MTD_RAM: | |
95 | type = "ram"; | |
96 | break; | |
97 | case MTD_ROM: | |
98 | type = "rom"; | |
99 | break; | |
100 | case MTD_NORFLASH: | |
101 | type = "nor"; | |
102 | break; | |
103 | case MTD_NANDFLASH: | |
104 | type = "nand"; | |
105 | break; | |
106 | case MTD_DATAFLASH: | |
107 | type = "dataflash"; | |
108 | break; | |
109 | case MTD_UBIVOLUME: | |
110 | type = "ubi"; | |
111 | break; | |
112 | default: | |
113 | type = "unknown"; | |
114 | } | |
115 | ||
116 | return snprintf(buf, PAGE_SIZE, "%s\n", type); | |
117 | } | |
694bb7fc KC |
118 | static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); |
119 | ||
120 | static ssize_t mtd_flags_show(struct device *dev, | |
121 | struct device_attribute *attr, char *buf) | |
122 | { | |
123 | struct mtd_info *mtd = dev_to_mtd(dev); | |
124 | ||
125 | return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); | |
126 | ||
127 | } | |
128 | static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); | |
129 | ||
130 | static ssize_t mtd_size_show(struct device *dev, | |
131 | struct device_attribute *attr, char *buf) | |
132 | { | |
133 | struct mtd_info *mtd = dev_to_mtd(dev); | |
134 | ||
135 | return snprintf(buf, PAGE_SIZE, "%llu\n", | |
136 | (unsigned long long)mtd->size); | |
137 | ||
138 | } | |
139 | static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); | |
140 | ||
141 | static ssize_t mtd_erasesize_show(struct device *dev, | |
142 | struct device_attribute *attr, char *buf) | |
143 | { | |
144 | struct mtd_info *mtd = dev_to_mtd(dev); | |
145 | ||
146 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); | |
147 | ||
148 | } | |
149 | static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); | |
150 | ||
151 | static ssize_t mtd_writesize_show(struct device *dev, | |
152 | struct device_attribute *attr, char *buf) | |
153 | { | |
154 | struct mtd_info *mtd = dev_to_mtd(dev); | |
155 | ||
156 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); | |
157 | ||
158 | } | |
159 | static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); | |
160 | ||
e7693548 AB |
161 | static ssize_t mtd_subpagesize_show(struct device *dev, |
162 | struct device_attribute *attr, char *buf) | |
163 | { | |
164 | struct mtd_info *mtd = dev_to_mtd(dev); | |
165 | unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; | |
166 | ||
167 | return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); | |
168 | ||
169 | } | |
170 | static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); | |
171 | ||
694bb7fc KC |
172 | static ssize_t mtd_oobsize_show(struct device *dev, |
173 | struct device_attribute *attr, char *buf) | |
174 | { | |
175 | struct mtd_info *mtd = dev_to_mtd(dev); | |
176 | ||
177 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); | |
178 | ||
179 | } | |
180 | static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); | |
181 | ||
182 | static ssize_t mtd_numeraseregions_show(struct device *dev, | |
183 | struct device_attribute *attr, char *buf) | |
184 | { | |
185 | struct mtd_info *mtd = dev_to_mtd(dev); | |
186 | ||
187 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); | |
188 | ||
189 | } | |
190 | static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, | |
191 | NULL); | |
192 | ||
193 | static ssize_t mtd_name_show(struct device *dev, | |
194 | struct device_attribute *attr, char *buf) | |
195 | { | |
196 | struct mtd_info *mtd = dev_to_mtd(dev); | |
197 | ||
198 | return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); | |
199 | ||
200 | } | |
201 | static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); | |
1f24b5a8 DB |
202 | |
203 | static struct attribute *mtd_attrs[] = { | |
694bb7fc KC |
204 | &dev_attr_type.attr, |
205 | &dev_attr_flags.attr, | |
206 | &dev_attr_size.attr, | |
207 | &dev_attr_erasesize.attr, | |
208 | &dev_attr_writesize.attr, | |
e7693548 | 209 | &dev_attr_subpagesize.attr, |
694bb7fc KC |
210 | &dev_attr_oobsize.attr, |
211 | &dev_attr_numeraseregions.attr, | |
212 | &dev_attr_name.attr, | |
1f24b5a8 DB |
213 | NULL, |
214 | }; | |
215 | ||
fca91088 | 216 | static struct attribute_group mtd_group = { |
1f24b5a8 DB |
217 | .attrs = mtd_attrs, |
218 | }; | |
219 | ||
6469f540 | 220 | static const struct attribute_group *mtd_groups[] = { |
1f24b5a8 DB |
221 | &mtd_group, |
222 | NULL, | |
223 | }; | |
224 | ||
225 | static struct device_type mtd_devtype = { | |
226 | .name = "mtd", | |
227 | .groups = mtd_groups, | |
228 | .release = mtd_release, | |
229 | }; | |
230 | ||
1da177e4 LT |
231 | /** |
232 | * add_mtd_device - register an MTD device | |
233 | * @mtd: pointer to new MTD device info structure | |
234 | * | |
235 | * Add a device to the list of MTD devices present in the system, and | |
236 | * notify each currently active MTD 'user' of its arrival. Returns | |
237 | * zero on success or 1 on failure, which currently will only happen | |
238 | * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16) | |
1f24b5a8 | 239 | * or there's a sysfs error. |
1da177e4 LT |
240 | */ |
241 | ||
242 | int add_mtd_device(struct mtd_info *mtd) | |
243 | { | |
244 | int i; | |
245 | ||
402d3265 DH |
246 | if (!mtd->backing_dev_info) { |
247 | switch (mtd->type) { | |
248 | case MTD_RAM: | |
249 | mtd->backing_dev_info = &mtd_bdi_rw_mappable; | |
250 | break; | |
251 | case MTD_ROM: | |
252 | mtd->backing_dev_info = &mtd_bdi_ro_mappable; | |
253 | break; | |
254 | default: | |
255 | mtd->backing_dev_info = &mtd_bdi_unmappable; | |
256 | break; | |
257 | } | |
258 | } | |
259 | ||
783ed81f | 260 | BUG_ON(mtd->writesize == 0); |
48b19268 | 261 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
262 | |
263 | for (i=0; i < MAX_MTD_DEVICES; i++) | |
264 | if (!mtd_table[i]) { | |
2606c797 | 265 | struct mtd_notifier *not; |
1da177e4 LT |
266 | |
267 | mtd_table[i] = mtd; | |
268 | mtd->index = i; | |
269 | mtd->usecount = 0; | |
270 | ||
69423d99 AH |
271 | if (is_power_of_2(mtd->erasesize)) |
272 | mtd->erasesize_shift = ffs(mtd->erasesize) - 1; | |
273 | else | |
274 | mtd->erasesize_shift = 0; | |
275 | ||
276 | if (is_power_of_2(mtd->writesize)) | |
277 | mtd->writesize_shift = ffs(mtd->writesize) - 1; | |
278 | else | |
279 | mtd->writesize_shift = 0; | |
280 | ||
281 | mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; | |
282 | mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; | |
283 | ||
187ef152 HS |
284 | /* Some chips always power up locked. Unlock them now */ |
285 | if ((mtd->flags & MTD_WRITEABLE) | |
e619a75f | 286 | && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) { |
187ef152 HS |
287 | if (mtd->unlock(mtd, 0, mtd->size)) |
288 | printk(KERN_WARNING | |
289 | "%s: unlock failed, " | |
290 | "writes may not work\n", | |
291 | mtd->name); | |
292 | } | |
293 | ||
1f24b5a8 DB |
294 | /* Caller should have set dev.parent to match the |
295 | * physical device. | |
296 | */ | |
297 | mtd->dev.type = &mtd_devtype; | |
15bce40c | 298 | mtd->dev.class = &mtd_class; |
1f24b5a8 DB |
299 | mtd->dev.devt = MTD_DEVT(i); |
300 | dev_set_name(&mtd->dev, "mtd%d", i); | |
6afc4fdb | 301 | dev_set_drvdata(&mtd->dev, mtd); |
1f24b5a8 DB |
302 | if (device_register(&mtd->dev) != 0) { |
303 | mtd_table[i] = NULL; | |
304 | break; | |
305 | } | |
306 | ||
307 | if (MTD_DEVT(i)) | |
15bce40c | 308 | device_create(&mtd_class, mtd->dev.parent, |
1f24b5a8 DB |
309 | MTD_DEVT(i) + 1, |
310 | NULL, "mtd%dro", i); | |
311 | ||
1da177e4 LT |
312 | DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name); |
313 | /* No need to get a refcount on the module containing | |
314 | the notifier, since we hold the mtd_table_mutex */ | |
71a928c0 | 315 | list_for_each_entry(not, &mtd_notifiers, list) |
1da177e4 | 316 | not->add(mtd); |
97894cda | 317 | |
48b19268 | 318 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
319 | /* We _know_ we aren't being removed, because |
320 | our caller is still holding us here. So none | |
321 | of this try_ nonsense, and no bitching about it | |
322 | either. :) */ | |
323 | __module_get(THIS_MODULE); | |
324 | return 0; | |
325 | } | |
97894cda | 326 | |
48b19268 | 327 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
328 | return 1; |
329 | } | |
330 | ||
331 | /** | |
332 | * del_mtd_device - unregister an MTD device | |
333 | * @mtd: pointer to MTD device info structure | |
334 | * | |
335 | * Remove a device from the list of MTD devices present in the system, | |
336 | * and notify each currently active MTD 'user' of its departure. | |
337 | * Returns zero on success or 1 on failure, which currently will happen | |
338 | * if the requested device does not appear to be present in the list. | |
339 | */ | |
340 | ||
341 | int del_mtd_device (struct mtd_info *mtd) | |
342 | { | |
343 | int ret; | |
97894cda | 344 | |
48b19268 | 345 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
346 | |
347 | if (mtd_table[mtd->index] != mtd) { | |
348 | ret = -ENODEV; | |
349 | } else if (mtd->usecount) { | |
97894cda | 350 | printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", |
1da177e4 LT |
351 | mtd->index, mtd->name, mtd->usecount); |
352 | ret = -EBUSY; | |
353 | } else { | |
856613c9 | 354 | struct mtd_notifier *not; |
1da177e4 | 355 | |
694bb7fc KC |
356 | device_unregister(&mtd->dev); |
357 | ||
1da177e4 LT |
358 | /* No need to get a refcount on the module containing |
359 | the notifier, since we hold the mtd_table_mutex */ | |
71a928c0 | 360 | list_for_each_entry(not, &mtd_notifiers, list) |
1da177e4 | 361 | not->remove(mtd); |
1da177e4 LT |
362 | |
363 | mtd_table[mtd->index] = NULL; | |
364 | ||
365 | module_put(THIS_MODULE); | |
366 | ret = 0; | |
367 | } | |
368 | ||
48b19268 | 369 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
370 | return ret; |
371 | } | |
372 | ||
373 | /** | |
374 | * register_mtd_user - register a 'user' of MTD devices. | |
375 | * @new: pointer to notifier info structure | |
376 | * | |
377 | * Registers a pair of callbacks function to be called upon addition | |
378 | * or removal of MTD devices. Causes the 'add' callback to be immediately | |
379 | * invoked for each MTD device currently present in the system. | |
380 | */ | |
381 | ||
382 | void register_mtd_user (struct mtd_notifier *new) | |
383 | { | |
384 | int i; | |
385 | ||
48b19268 | 386 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
387 | |
388 | list_add(&new->list, &mtd_notifiers); | |
389 | ||
390 | __module_get(THIS_MODULE); | |
97894cda | 391 | |
1da177e4 LT |
392 | for (i=0; i< MAX_MTD_DEVICES; i++) |
393 | if (mtd_table[i]) | |
394 | new->add(mtd_table[i]); | |
395 | ||
48b19268 | 396 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
397 | } |
398 | ||
399 | /** | |
49450795 AB |
400 | * unregister_mtd_user - unregister a 'user' of MTD devices. |
401 | * @old: pointer to notifier info structure | |
1da177e4 LT |
402 | * |
403 | * Removes a callback function pair from the list of 'users' to be | |
404 | * notified upon addition or removal of MTD devices. Causes the | |
405 | * 'remove' callback to be immediately invoked for each MTD device | |
406 | * currently present in the system. | |
407 | */ | |
408 | ||
409 | int unregister_mtd_user (struct mtd_notifier *old) | |
410 | { | |
411 | int i; | |
412 | ||
48b19268 | 413 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
414 | |
415 | module_put(THIS_MODULE); | |
416 | ||
417 | for (i=0; i< MAX_MTD_DEVICES; i++) | |
418 | if (mtd_table[i]) | |
419 | old->remove(mtd_table[i]); | |
97894cda | 420 | |
1da177e4 | 421 | list_del(&old->list); |
48b19268 | 422 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
423 | return 0; |
424 | } | |
425 | ||
426 | ||
427 | /** | |
428 | * get_mtd_device - obtain a validated handle for an MTD device | |
429 | * @mtd: last known address of the required MTD device | |
430 | * @num: internal device number of the required MTD device | |
431 | * | |
432 | * Given a number and NULL address, return the num'th entry in the device | |
433 | * table, if any. Given an address and num == -1, search the device table | |
434 | * for a device with that address and return if it's still present. Given | |
9c74034f AB |
435 | * both, return the num'th driver only if its address matches. Return |
436 | * error code if not. | |
1da177e4 | 437 | */ |
97894cda | 438 | |
1da177e4 LT |
439 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
440 | { | |
441 | struct mtd_info *ret = NULL; | |
9c74034f | 442 | int i, err = -ENODEV; |
1da177e4 | 443 | |
48b19268 | 444 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
445 | |
446 | if (num == -1) { | |
447 | for (i=0; i< MAX_MTD_DEVICES; i++) | |
448 | if (mtd_table[i] == mtd) | |
449 | ret = mtd_table[i]; | |
450 | } else if (num < MAX_MTD_DEVICES) { | |
451 | ret = mtd_table[num]; | |
452 | if (mtd && mtd != ret) | |
453 | ret = NULL; | |
454 | } | |
455 | ||
9fe912ce AB |
456 | if (!ret) |
457 | goto out_unlock; | |
458 | ||
9c74034f | 459 | if (!try_module_get(ret->owner)) |
9fe912ce | 460 | goto out_unlock; |
9fe912ce | 461 | |
9c74034f AB |
462 | if (ret->get_device) { |
463 | err = ret->get_device(ret); | |
464 | if (err) | |
465 | goto out_put; | |
9fe912ce | 466 | } |
1da177e4 | 467 | |
9fe912ce | 468 | ret->usecount++; |
9c74034f AB |
469 | mutex_unlock(&mtd_table_mutex); |
470 | return ret; | |
1da177e4 | 471 | |
9c74034f AB |
472 | out_put: |
473 | module_put(ret->owner); | |
9fe912ce | 474 | out_unlock: |
48b19268 | 475 | mutex_unlock(&mtd_table_mutex); |
9c74034f | 476 | return ERR_PTR(err); |
1da177e4 LT |
477 | } |
478 | ||
7799308f AB |
479 | /** |
480 | * get_mtd_device_nm - obtain a validated handle for an MTD device by | |
481 | * device name | |
482 | * @name: MTD device name to open | |
483 | * | |
484 | * This function returns MTD device description structure in case of | |
485 | * success and an error code in case of failure. | |
486 | */ | |
487 | ||
488 | struct mtd_info *get_mtd_device_nm(const char *name) | |
489 | { | |
9fe912ce AB |
490 | int i, err = -ENODEV; |
491 | struct mtd_info *mtd = NULL; | |
7799308f AB |
492 | |
493 | mutex_lock(&mtd_table_mutex); | |
494 | ||
495 | for (i = 0; i < MAX_MTD_DEVICES; i++) { | |
496 | if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) { | |
497 | mtd = mtd_table[i]; | |
498 | break; | |
499 | } | |
500 | } | |
501 | ||
9fe912ce | 502 | if (!mtd) |
7799308f AB |
503 | goto out_unlock; |
504 | ||
505 | if (!try_module_get(mtd->owner)) | |
506 | goto out_unlock; | |
507 | ||
9fe912ce AB |
508 | if (mtd->get_device) { |
509 | err = mtd->get_device(mtd); | |
510 | if (err) | |
511 | goto out_put; | |
512 | } | |
513 | ||
7799308f | 514 | mtd->usecount++; |
9fe912ce AB |
515 | mutex_unlock(&mtd_table_mutex); |
516 | return mtd; | |
7799308f | 517 | |
9fe912ce AB |
518 | out_put: |
519 | module_put(mtd->owner); | |
7799308f AB |
520 | out_unlock: |
521 | mutex_unlock(&mtd_table_mutex); | |
9fe912ce | 522 | return ERR_PTR(err); |
7799308f AB |
523 | } |
524 | ||
1da177e4 LT |
525 | void put_mtd_device(struct mtd_info *mtd) |
526 | { | |
527 | int c; | |
528 | ||
48b19268 | 529 | mutex_lock(&mtd_table_mutex); |
1da177e4 | 530 | c = --mtd->usecount; |
9fe912ce AB |
531 | if (mtd->put_device) |
532 | mtd->put_device(mtd); | |
48b19268 | 533 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
534 | BUG_ON(c < 0); |
535 | ||
536 | module_put(mtd->owner); | |
537 | } | |
538 | ||
539 | /* default_mtd_writev - default mtd writev method for MTD devices that | |
dd36f267 | 540 | * don't implement their own |
1da177e4 LT |
541 | */ |
542 | ||
543 | int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, | |
544 | unsigned long count, loff_t to, size_t *retlen) | |
545 | { | |
546 | unsigned long i; | |
547 | size_t totlen = 0, thislen; | |
548 | int ret = 0; | |
549 | ||
550 | if(!mtd->write) { | |
551 | ret = -EROFS; | |
552 | } else { | |
553 | for (i=0; i<count; i++) { | |
554 | if (!vecs[i].iov_len) | |
555 | continue; | |
556 | ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base); | |
557 | totlen += thislen; | |
558 | if (ret || thislen != vecs[i].iov_len) | |
559 | break; | |
560 | to += vecs[i].iov_len; | |
561 | } | |
562 | } | |
563 | if (retlen) | |
564 | *retlen = totlen; | |
565 | return ret; | |
566 | } | |
567 | ||
dd36f267 DW |
568 | EXPORT_SYMBOL_GPL(add_mtd_device); |
569 | EXPORT_SYMBOL_GPL(del_mtd_device); | |
570 | EXPORT_SYMBOL_GPL(get_mtd_device); | |
571 | EXPORT_SYMBOL_GPL(get_mtd_device_nm); | |
572 | EXPORT_SYMBOL_GPL(put_mtd_device); | |
573 | EXPORT_SYMBOL_GPL(register_mtd_user); | |
574 | EXPORT_SYMBOL_GPL(unregister_mtd_user); | |
575 | EXPORT_SYMBOL_GPL(default_mtd_writev); | |
1da177e4 | 576 | |
2d2dce0e PM |
577 | #ifdef CONFIG_PROC_FS |
578 | ||
1da177e4 LT |
579 | /*====================================================================*/ |
580 | /* Support for /proc/mtd */ | |
581 | ||
1da177e4 LT |
582 | static struct proc_dir_entry *proc_mtd; |
583 | ||
584 | static inline int mtd_proc_info (char *buf, int i) | |
585 | { | |
586 | struct mtd_info *this = mtd_table[i]; | |
587 | ||
588 | if (!this) | |
589 | return 0; | |
590 | ||
69423d99 AH |
591 | return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i, |
592 | (unsigned long long)this->size, | |
1da177e4 LT |
593 | this->erasesize, this->name); |
594 | } | |
595 | ||
596 | static int mtd_read_proc (char *page, char **start, off_t off, int count, | |
597 | int *eof, void *data_unused) | |
598 | { | |
599 | int len, l, i; | |
600 | off_t begin = 0; | |
601 | ||
48b19268 | 602 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
603 | |
604 | len = sprintf(page, "dev: size erasesize name\n"); | |
605 | for (i=0; i< MAX_MTD_DEVICES; i++) { | |
606 | ||
607 | l = mtd_proc_info(page + len, i); | |
608 | len += l; | |
609 | if (len+begin > off+count) | |
610 | goto done; | |
611 | if (len+begin < off) { | |
612 | begin += len; | |
613 | len = 0; | |
614 | } | |
615 | } | |
616 | ||
617 | *eof = 1; | |
618 | ||
619 | done: | |
48b19268 | 620 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
621 | if (off >= len+begin) |
622 | return 0; | |
623 | *start = page + (off-begin); | |
624 | return ((count < begin+len-off) ? count : begin+len-off); | |
625 | } | |
626 | ||
45b09076 KC |
627 | #endif /* CONFIG_PROC_FS */ |
628 | ||
1da177e4 LT |
629 | /*====================================================================*/ |
630 | /* Init code */ | |
631 | ||
632 | static int __init init_mtd(void) | |
633 | { | |
15bce40c DW |
634 | int ret; |
635 | ret = class_register(&mtd_class); | |
694bb7fc | 636 | |
15bce40c DW |
637 | if (ret) { |
638 | pr_err("Error registering mtd class: %d\n", ret); | |
639 | return ret; | |
694bb7fc | 640 | } |
45b09076 | 641 | #ifdef CONFIG_PROC_FS |
1da177e4 LT |
642 | if ((proc_mtd = create_proc_entry( "mtd", 0, NULL ))) |
643 | proc_mtd->read_proc = mtd_read_proc; | |
45b09076 | 644 | #endif /* CONFIG_PROC_FS */ |
1da177e4 LT |
645 | return 0; |
646 | } | |
647 | ||
648 | static void __exit cleanup_mtd(void) | |
649 | { | |
45b09076 | 650 | #ifdef CONFIG_PROC_FS |
1da177e4 LT |
651 | if (proc_mtd) |
652 | remove_proc_entry( "mtd", NULL); | |
45b09076 | 653 | #endif /* CONFIG_PROC_FS */ |
15bce40c | 654 | class_unregister(&mtd_class); |
1da177e4 LT |
655 | } |
656 | ||
657 | module_init(init_mtd); | |
658 | module_exit(cleanup_mtd); | |
659 | ||
1da177e4 LT |
660 | MODULE_LICENSE("GPL"); |
661 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
662 | MODULE_DESCRIPTION("Core MTD registration and access routines"); |