416d47d5a2f992b59281bda80e9a8e10857904b4
[linux-flexiantxendom0-3.2.10.git] / arch / i386 / kernel / edd.c
1 /*
2  * linux/arch/i386/kernel/edd.c
3  *  Copyright (C) 2002, 2003 Dell Inc.
4  *  by Matt Domsch <Matt_Domsch@dell.com>
5  *  disk80 signature by Matt Domsch, Andrew Wilks, and Sandeep K. Shandilya
6  *
7  * BIOS Enhanced Disk Drive Services (EDD)
8  * conformant to T13 Committee www.t13.org
9  *   projects 1572D, 1484D, 1386D, 1226DT
10  *
11  * This code takes information provided by BIOS EDD calls
12  * fn41 - Check Extensions Present and
13  * fn48 - Get Device Parametes with EDD extensions
14  * made in setup.S, copied to safe structures in setup.c,
15  * and presents it in sysfs.
16  *
17  * Please see http://domsch.com/linux/edd30/results.html for
18  * the list of BIOSs which have been reported to implement EDD.
19  * If you don't see yours listed, please send a report as described there.
20  *
21  * This program is free software; you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License v2.0 as published by
23  * the Free Software Foundation
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  */
31
32 /*
33  * Known issues:
34  * - refcounting of struct device objects could be improved.
35  *
36  * TODO:
37  * - Add IDE and USB disk device support
38  * - move edd.[ch] to better locations if/when one is decided
39  */
40
41 #include <linux/module.h>
42 #include <linux/string.h>
43 #include <linux/types.h>
44 #include <linux/init.h>
45 #include <linux/stat.h>
46 #include <linux/err.h>
47 #include <linux/ctype.h>
48 #include <linux/slab.h>
49 #include <linux/limits.h>
50 #include <linux/device.h>
51 #include <linux/pci.h>
52 #include <linux/device.h>
53 #include <linux/blkdev.h>
54 #include <asm/edd.h>
55 /* FIXME - this really belongs in include/scsi/scsi.h */
56 #include <../drivers/scsi/scsi.h>
57 #include <../drivers/scsi/hosts.h>
58
59 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
60 MODULE_DESCRIPTION("sysfs interface to BIOS EDD information");
61 MODULE_LICENSE("GPL");
62
63 #define EDD_VERSION "0.12 2004-Jan-26"
64 #define EDD_DEVICE_NAME_SIZE 16
65 #define REPORT_URL "http://linux.dell.com/edd/results.html"
66
67 #define left (PAGE_SIZE - (p - buf) - 1)
68
69 struct edd_device {
70         struct edd_info *info;
71         struct kobject kobj;
72 };
73
74 struct edd_attribute {
75         struct attribute attr;
76         ssize_t(*show) (struct edd_device * edev, char *buf);
77         int (*test) (struct edd_device * edev);
78 };
79
80 /* forward declarations */
81 static int edd_dev_is_type(struct edd_device *edev, const char *type);
82 static struct pci_dev *edd_get_pci_dev(struct edd_device *edev);
83
84 static struct edd_device *edd_devices[EDDMAXNR];
85
86 #define EDD_DEVICE_ATTR(_name,_mode,_show,_test) \
87 struct edd_attribute edd_attr_##_name = {       \
88         .attr = {.name = __stringify(_name), .mode = _mode },   \
89         .show   = _show,                                \
90         .test   = _test,                                \
91 };
92
93 static inline struct edd_info *
94 edd_dev_get_info(struct edd_device *edev)
95 {
96         return edev->info;
97 }
98
99 static inline void
100 edd_dev_set_info(struct edd_device *edev, struct edd_info *info)
101 {
102         edev->info = info;
103 }
104
105 #define to_edd_attr(_attr) container_of(_attr,struct edd_attribute,attr)
106 #define to_edd_device(obj) container_of(obj,struct edd_device,kobj)
107
108 static ssize_t
109 edd_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
110 {
111         struct edd_device *dev = to_edd_device(kobj);
112         struct edd_attribute *edd_attr = to_edd_attr(attr);
113         ssize_t ret = 0;
114
115         if (edd_attr->show)
116                 ret = edd_attr->show(dev, buf);
117         return ret;
118 }
119
120 static struct sysfs_ops edd_attr_ops = {
121         .show = edd_attr_show,
122 };
123
124 static ssize_t
125 edd_show_host_bus(struct edd_device *edev, char *buf)
126 {
127         struct edd_info *info = edd_dev_get_info(edev);
128         char *p = buf;
129         int i;
130
131         if (!edev || !info || !buf) {
132                 return -EINVAL;
133         }
134
135         for (i = 0; i < 4; i++) {
136                 if (isprint(info->params.host_bus_type[i])) {
137                         p += snprintf(p, left, "%c", info->params.host_bus_type[i]);
138                 } else {
139                         p += snprintf(p, left, " ");
140                 }
141         }
142
143         if (!strncmp(info->params.host_bus_type, "ISA", 3)) {
144                 p += snprintf(p, left, "\tbase_address: %x\n",
145                              info->params.interface_path.isa.base_address);
146         } else if (!strncmp(info->params.host_bus_type, "PCIX", 4) ||
147                    !strncmp(info->params.host_bus_type, "PCI", 3)) {
148                 p += snprintf(p, left,
149                              "\t%02x:%02x.%d  channel: %u\n",
150                              info->params.interface_path.pci.bus,
151                              info->params.interface_path.pci.slot,
152                              info->params.interface_path.pci.function,
153                              info->params.interface_path.pci.channel);
154         } else if (!strncmp(info->params.host_bus_type, "IBND", 4) ||
155                    !strncmp(info->params.host_bus_type, "XPRS", 4) ||
156                    !strncmp(info->params.host_bus_type, "HTPT", 4)) {
157                 p += snprintf(p, left,
158                              "\tTBD: %llx\n",
159                              info->params.interface_path.ibnd.reserved);
160
161         } else {
162                 p += snprintf(p, left, "\tunknown: %llx\n",
163                              info->params.interface_path.unknown.reserved);
164         }
165         return (p - buf);
166 }
167
168 static ssize_t
169 edd_show_interface(struct edd_device *edev, char *buf)
170 {
171         struct edd_info *info = edd_dev_get_info(edev);
172         char *p = buf;
173         int i;
174
175         if (!edev || !info || !buf) {
176                 return -EINVAL;
177         }
178
179         for (i = 0; i < 8; i++) {
180                 if (isprint(info->params.interface_type[i])) {
181                         p += snprintf(p, left, "%c", info->params.interface_type[i]);
182                 } else {
183                         p += snprintf(p, left, " ");
184                 }
185         }
186         if (!strncmp(info->params.interface_type, "ATAPI", 5)) {
187                 p += snprintf(p, left, "\tdevice: %u  lun: %u\n",
188                              info->params.device_path.atapi.device,
189                              info->params.device_path.atapi.lun);
190         } else if (!strncmp(info->params.interface_type, "ATA", 3)) {
191                 p += snprintf(p, left, "\tdevice: %u\n",
192                              info->params.device_path.ata.device);
193         } else if (!strncmp(info->params.interface_type, "SCSI", 4)) {
194                 p += snprintf(p, left, "\tid: %u  lun: %llu\n",
195                              info->params.device_path.scsi.id,
196                              info->params.device_path.scsi.lun);
197         } else if (!strncmp(info->params.interface_type, "USB", 3)) {
198                 p += snprintf(p, left, "\tserial_number: %llx\n",
199                              info->params.device_path.usb.serial_number);
200         } else if (!strncmp(info->params.interface_type, "1394", 4)) {
201                 p += snprintf(p, left, "\teui: %llx\n",
202                              info->params.device_path.i1394.eui);
203         } else if (!strncmp(info->params.interface_type, "FIBRE", 5)) {
204                 p += snprintf(p, left, "\twwid: %llx lun: %llx\n",
205                              info->params.device_path.fibre.wwid,
206                              info->params.device_path.fibre.lun);
207         } else if (!strncmp(info->params.interface_type, "I2O", 3)) {
208                 p += snprintf(p, left, "\tidentity_tag: %llx\n",
209                              info->params.device_path.i2o.identity_tag);
210         } else if (!strncmp(info->params.interface_type, "RAID", 4)) {
211                 p += snprintf(p, left, "\tidentity_tag: %x\n",
212                              info->params.device_path.raid.array_number);
213         } else if (!strncmp(info->params.interface_type, "SATA", 4)) {
214                 p += snprintf(p, left, "\tdevice: %u\n",
215                              info->params.device_path.sata.device);
216         } else {
217                 p += snprintf(p, left, "\tunknown: %llx %llx\n",
218                              info->params.device_path.unknown.reserved1,
219                              info->params.device_path.unknown.reserved2);
220         }
221
222         return (p - buf);
223 }
224
225 /**
226  * edd_show_raw_data() - copies raw data to buffer for userspace to parse
227  *
228  * Returns: number of bytes written, or -EINVAL on failure
229  */
230 static ssize_t
231 edd_show_raw_data(struct edd_device *edev, char *buf)
232 {
233         struct edd_info *info = edd_dev_get_info(edev);
234         ssize_t len = sizeof (*info) - 4;
235         if (!edev || !info || !buf) {
236                 return -EINVAL;
237         }
238
239         if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE))
240                 len = info->params.length;
241
242         /* In case of buggy BIOSs */
243         if (len > (sizeof(*info) - 4))
244                 len = sizeof(*info) - 4;
245
246         memcpy(buf, ((char *)info) + 4, len);
247         return len;
248 }
249
250 static ssize_t
251 edd_show_version(struct edd_device *edev, char *buf)
252 {
253         struct edd_info *info = edd_dev_get_info(edev);
254         char *p = buf;
255         if (!edev || !info || !buf) {
256                 return -EINVAL;
257         }
258
259         p += snprintf(p, left, "0x%02x\n", info->version);
260         return (p - buf);
261 }
262
263 static ssize_t
264 edd_show_disk80_sig(struct edd_device *edev, char *buf)
265 {
266         char *p = buf;
267         p += snprintf(p, left, "0x%08x\n", edd_disk80_sig);
268         return (p - buf);
269 }
270
271 static ssize_t
272 edd_show_extensions(struct edd_device *edev, char *buf)
273 {
274         struct edd_info *info = edd_dev_get_info(edev);
275         char *p = buf;
276         if (!edev || !info || !buf) {
277                 return -EINVAL;
278         }
279
280         if (info->interface_support & EDD_EXT_FIXED_DISK_ACCESS) {
281                 p += snprintf(p, left, "Fixed disk access\n");
282         }
283         if (info->interface_support & EDD_EXT_DEVICE_LOCKING_AND_EJECTING) {
284                 p += snprintf(p, left, "Device locking and ejecting\n");
285         }
286         if (info->interface_support & EDD_EXT_ENHANCED_DISK_DRIVE_SUPPORT) {
287                 p += snprintf(p, left, "Enhanced Disk Drive support\n");
288         }
289         if (info->interface_support & EDD_EXT_64BIT_EXTENSIONS) {
290                 p += snprintf(p, left, "64-bit extensions\n");
291         }
292         return (p - buf);
293 }
294
295 static ssize_t
296 edd_show_info_flags(struct edd_device *edev, char *buf)
297 {
298         struct edd_info *info = edd_dev_get_info(edev);
299         char *p = buf;
300         if (!edev || !info || !buf) {
301                 return -EINVAL;
302         }
303
304         if (info->params.info_flags & EDD_INFO_DMA_BOUNDARY_ERROR_TRANSPARENT)
305                 p += snprintf(p, left, "DMA boundary error transparent\n");
306         if (info->params.info_flags & EDD_INFO_GEOMETRY_VALID)
307                 p += snprintf(p, left, "geometry valid\n");
308         if (info->params.info_flags & EDD_INFO_REMOVABLE)
309                 p += snprintf(p, left, "removable\n");
310         if (info->params.info_flags & EDD_INFO_WRITE_VERIFY)
311                 p += snprintf(p, left, "write verify\n");
312         if (info->params.info_flags & EDD_INFO_MEDIA_CHANGE_NOTIFICATION)
313                 p += snprintf(p, left, "media change notification\n");
314         if (info->params.info_flags & EDD_INFO_LOCKABLE)
315                 p += snprintf(p, left, "lockable\n");
316         if (info->params.info_flags & EDD_INFO_NO_MEDIA_PRESENT)
317                 p += snprintf(p, left, "no media present\n");
318         if (info->params.info_flags & EDD_INFO_USE_INT13_FN50)
319                 p += snprintf(p, left, "use int13 fn50\n");
320         return (p - buf);
321 }
322
323 static ssize_t
324 edd_show_default_cylinders(struct edd_device *edev, char *buf)
325 {
326         struct edd_info *info = edd_dev_get_info(edev);
327         char *p = buf;
328         if (!edev || !info || !buf) {
329                 return -EINVAL;
330         }
331
332         p += snprintf(p, left, "0x%x\n", info->params.num_default_cylinders);
333         return (p - buf);
334 }
335
336 static ssize_t
337 edd_show_default_heads(struct edd_device *edev, char *buf)
338 {
339         struct edd_info *info = edd_dev_get_info(edev);
340         char *p = buf;
341         if (!edev || !info || !buf) {
342                 return -EINVAL;
343         }
344
345         p += snprintf(p, left, "0x%x\n", info->params.num_default_heads);
346         return (p - buf);
347 }
348
349 static ssize_t
350 edd_show_default_sectors_per_track(struct edd_device *edev, char *buf)
351 {
352         struct edd_info *info = edd_dev_get_info(edev);
353         char *p = buf;
354         if (!edev || !info || !buf) {
355                 return -EINVAL;
356         }
357
358         p += snprintf(p, left, "0x%x\n", info->params.sectors_per_track);
359         return (p - buf);
360 }
361
362 static ssize_t
363 edd_show_sectors(struct edd_device *edev, char *buf)
364 {
365         struct edd_info *info = edd_dev_get_info(edev);
366         char *p = buf;
367         if (!edev || !info || !buf) {
368                 return -EINVAL;
369         }
370
371         p += snprintf(p, left, "0x%llx\n", info->params.number_of_sectors);
372         return (p - buf);
373 }
374
375
376 /*
377  * Some device instances may not have all the above attributes,
378  * or the attribute values may be meaningless (i.e. if
379  * the device is < EDD 3.0, it won't have host_bus and interface
380  * information), so don't bother making files for them.  Likewise
381  * if the default_{cylinders,heads,sectors_per_track} values
382  * are zero, the BIOS doesn't provide sane values, don't bother
383  * creating files for them either.
384  */
385
386 static int
387 edd_has_default_cylinders(struct edd_device *edev)
388 {
389         struct edd_info *info = edd_dev_get_info(edev);
390         if (!edev || !info)
391                 return -EINVAL;
392         return info->params.num_default_cylinders > 0;
393 }
394
395 static int
396 edd_has_default_heads(struct edd_device *edev)
397 {
398         struct edd_info *info = edd_dev_get_info(edev);
399         if (!edev || !info)
400                 return -EINVAL;
401         return info->params.num_default_heads > 0;
402 }
403
404 static int
405 edd_has_default_sectors_per_track(struct edd_device *edev)
406 {
407         struct edd_info *info = edd_dev_get_info(edev);
408         if (!edev || !info)
409                 return -EINVAL;
410         return info->params.sectors_per_track > 0;
411 }
412
413 static int
414 edd_has_edd30(struct edd_device *edev)
415 {
416         struct edd_info *info = edd_dev_get_info(edev);
417         int i, nonzero_path = 0;
418         char c;
419
420         if (!edev || !info)
421                 return 0;
422
423         if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE)) {
424                 return 0;
425         }
426
427         for (i = 30; i <= 73; i++) {
428                 c = *(((uint8_t *) info) + i + 4);
429                 if (c) {
430                         nonzero_path++;
431                         break;
432                 }
433         }
434         if (!nonzero_path) {
435                 return 0;
436         }
437
438         return 1;
439 }
440
441 static int
442 edd_has_disk80_sig(struct edd_device *edev)
443 {
444         struct edd_info *info = edd_dev_get_info(edev);
445         if (!edev || !info)
446                 return 0;
447         return info->device == 0x80;
448 }
449
450 static EDD_DEVICE_ATTR(raw_data, 0444, edd_show_raw_data, NULL);
451 static EDD_DEVICE_ATTR(version, 0444, edd_show_version, NULL);
452 static EDD_DEVICE_ATTR(extensions, 0444, edd_show_extensions, NULL);
453 static EDD_DEVICE_ATTR(info_flags, 0444, edd_show_info_flags, NULL);
454 static EDD_DEVICE_ATTR(sectors, 0444, edd_show_sectors, NULL);
455 static EDD_DEVICE_ATTR(default_cylinders, 0444, edd_show_default_cylinders,
456                        edd_has_default_cylinders);
457 static EDD_DEVICE_ATTR(default_heads, 0444, edd_show_default_heads,
458                        edd_has_default_heads);
459 static EDD_DEVICE_ATTR(default_sectors_per_track, 0444,
460                        edd_show_default_sectors_per_track,
461                        edd_has_default_sectors_per_track);
462 static EDD_DEVICE_ATTR(interface, 0444, edd_show_interface, edd_has_edd30);
463 static EDD_DEVICE_ATTR(host_bus, 0444, edd_show_host_bus, edd_has_edd30);
464 static EDD_DEVICE_ATTR(mbr_signature, 0444, edd_show_disk80_sig, edd_has_disk80_sig);
465
466
467 /* These are default attributes that are added for every edd
468  * device discovered.
469  */
470 static struct attribute * def_attrs[] = {
471         &edd_attr_raw_data.attr,
472         &edd_attr_version.attr,
473         &edd_attr_extensions.attr,
474         &edd_attr_info_flags.attr,
475         &edd_attr_sectors.attr,
476         NULL,
477 };
478
479 /* These attributes are conditional and only added for some devices. */
480 static struct edd_attribute * edd_attrs[] = {
481         &edd_attr_default_cylinders,
482         &edd_attr_default_heads,
483         &edd_attr_default_sectors_per_track,
484         &edd_attr_interface,
485         &edd_attr_host_bus,
486         &edd_attr_mbr_signature,
487         NULL,
488 };
489
490 /**
491  *      edd_release - free edd structure
492  *      @kobj:  kobject of edd structure
493  *
494  *      This is called when the refcount of the edd structure
495  *      reaches 0. This should happen right after we unregister,
496  *      but just in case, we use the release callback anyway.
497  */
498
499 static void edd_release(struct kobject * kobj)
500 {
501         struct edd_device * dev = to_edd_device(kobj);
502         kfree(dev);
503 }
504
505 static struct kobj_type ktype_edd = {
506         .release        = edd_release,
507         .sysfs_ops      = &edd_attr_ops,
508         .default_attrs  = def_attrs,
509 };
510
511 static decl_subsys(edd,&ktype_edd,NULL);
512
513
514 /**
515  * edd_dev_is_type() - is this EDD device a 'type' device?
516  * @edev
517  * @type - a host bus or interface identifier string per the EDD spec
518  *
519  * Returns 1 (TRUE) if it is a 'type' device, 0 otherwise.
520  */
521 static int
522 edd_dev_is_type(struct edd_device *edev, const char *type)
523 {
524         struct edd_info *info = edd_dev_get_info(edev);
525
526         if (edev && type && info) {
527                 if (!strncmp(info->params.host_bus_type, type, strlen(type)) ||
528                     !strncmp(info->params.interface_type, type, strlen(type)))
529                         return 1;
530         }
531         return 0;
532 }
533
534 /**
535  * edd_get_pci_dev() - finds pci_dev that matches edev
536  * @edev - edd_device
537  *
538  * Returns pci_dev if found, or NULL
539  */
540 static struct pci_dev *
541 edd_get_pci_dev(struct edd_device *edev)
542 {
543         struct edd_info *info = edd_dev_get_info(edev);
544
545         if (edd_dev_is_type(edev, "PCI")) {
546                 return pci_find_slot(info->params.interface_path.pci.bus,
547                                      PCI_DEVFN(info->params.interface_path.pci.slot,
548                                                info->params.interface_path.pci.
549                                                function));
550         }
551         return NULL;
552 }
553
554 static int
555 edd_create_symlink_to_pcidev(struct edd_device *edev)
556 {
557
558         struct pci_dev *pci_dev = edd_get_pci_dev(edev);
559         if (!pci_dev)
560                 return 1;
561         return sysfs_create_link(&edev->kobj,&pci_dev->dev.kobj,"pci_dev");
562 }
563
564 /*
565  * FIXME - as of 15-Jan-2003, there are some non-"scsi_device"s on the
566  * scsi_bus list.  The following functions could possibly mis-access
567  * memory in that case.  This is actually a problem with the SCSI
568  * layer, which is being addressed there.  Until then, don't use the
569  * SCSI functions.
570  */
571
572 #undef CONFIG_SCSI
573 #undef CONFIG_SCSI_MODULE
574 #if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE)
575
576 struct edd_match_data {
577         struct edd_device       * edev;
578         struct scsi_device      * sd;
579 };
580
581 /**
582  * edd_match_scsidev()
583  * @edev - EDD device is a known SCSI device
584  * @sd - scsi_device with host who's parent is a PCI controller
585  * 
586  * returns 1 if a match is found, 0 if not.
587  */
588 static int edd_match_scsidev(struct device * dev, void * d)
589 {
590         struct edd_match_data * data = (struct edd_match_data *)d;
591         struct edd_info *info = edd_dev_get_info(data->edev);
592         struct scsi_device * sd = to_scsi_device(dev);
593
594         if (info) {
595                 if ((sd->channel == info->params.interface_path.pci.channel) &&
596                     (sd->id == info->params.device_path.scsi.id) &&
597                     (sd->lun == info->params.device_path.scsi.lun)) {
598                         data->sd = sd;
599                         return 1;
600                 }
601         }
602         return 0;
603 }
604
605 /**
606  * edd_find_matching_device()
607  * @edev - edd_device to match
608  *
609  * Search the SCSI devices for a drive that matches the EDD 
610  * device descriptor we have. If we find a match, return it,
611  * otherwise, return NULL.
612  */
613
614 static struct scsi_device *
615 edd_find_matching_scsi_device(struct edd_device *edev)
616 {
617         struct edd_match_data data;
618         struct bus_type * scsi_bus = find_bus("scsi");
619
620         if (!scsi_bus) {
621                 return NULL;
622         }
623
624         data.edev = edev;
625
626         if (edd_dev_is_type(edev, "SCSI")) {
627                 if (bus_for_each_dev(scsi_bus,NULL,&data,edd_match_scsidev))
628                         return data.sd;
629         }
630         return NULL;
631 }
632
633 static int
634 edd_create_symlink_to_scsidev(struct edd_device *edev)
635 {
636         struct pci_dev *pci_dev;
637         int rc = -EINVAL;
638
639         pci_dev = edd_get_pci_dev(edev);
640         if (pci_dev) {
641                 struct scsi_device * sdev = edd_find_matching_scsi_device(edev);
642                 if (sdev && get_device(&sdev->sdev_driverfs_dev)) {
643                         rc = sysfs_create_link(&edev->kobj,
644                                                &sdev->sdev_driverfs_dev.kobj,
645                                                "disc");
646                         put_device(&sdev->sdev_driverfs_dev);
647                 }
648         }
649         return rc;
650 }
651
652 #else
653 static int
654 edd_create_symlink_to_scsidev(struct edd_device *edev)
655 {
656         return -ENOSYS;
657 }
658 #endif
659
660
661 static inline void
662 edd_device_unregister(struct edd_device *edev)
663 {
664         kobject_unregister(&edev->kobj);
665 }
666
667 static void edd_populate_dir(struct edd_device * edev)
668 {
669         struct edd_attribute * attr;
670         int error = 0;
671         int i;
672
673         for (i = 0; (attr = edd_attrs[i]) && !error; i++) {
674                 if (!attr->test ||
675                     (attr->test && attr->test(edev)))
676                         error = sysfs_create_file(&edev->kobj,&attr->attr);
677         }
678
679         if (!error) {
680                 edd_create_symlink_to_pcidev(edev);
681                 edd_create_symlink_to_scsidev(edev);
682         }
683 }
684
685 static int
686 edd_device_register(struct edd_device *edev, int i)
687 {
688         int error;
689
690         if (!edev)
691                 return 1;
692         memset(edev, 0, sizeof (*edev));
693         edd_dev_set_info(edev, &edd[i]);
694         snprintf(edev->kobj.name, EDD_DEVICE_NAME_SIZE, "int13_dev%02x",
695                  edd[i].device);
696         kobj_set_kset_s(edev,edd_subsys);
697         error = kobject_register(&edev->kobj);
698         if (!error)
699                 edd_populate_dir(edev);
700         return error;
701 }
702
703 /**
704  * edd_init() - creates sysfs tree of EDD data
705  *
706  * This assumes that eddnr and edd were
707  * assigned in setup.c already.
708  */
709 static int __init
710 edd_init(void)
711 {
712         unsigned int i;
713         int rc=0;
714         struct edd_device *edev;
715
716         printk(KERN_INFO "BIOS EDD facility v%s, %d devices found\n",
717                EDD_VERSION, eddnr);
718         printk(KERN_INFO "Please report your BIOS at %s\n", REPORT_URL);
719
720         if (!eddnr) {
721                 printk(KERN_INFO "EDD information not available.\n");
722                 return 1;
723         }
724
725         rc = firmware_register(&edd_subsys);
726         if (rc)
727                 return rc;
728
729         for (i = 0; i < eddnr && i < EDDMAXNR && !rc; i++) {
730                 edev = kmalloc(sizeof (*edev), GFP_KERNEL);
731                 if (!edev)
732                         return -ENOMEM;
733
734                 rc = edd_device_register(edev, i);
735                 if (rc) {
736                         kfree(edev);
737                         break;
738                 }
739                 edd_devices[i] = edev;
740         }
741
742         if (rc)
743                 firmware_unregister(&edd_subsys);
744         return rc;
745 }
746
747 static void __exit
748 edd_exit(void)
749 {
750         int i;
751         struct edd_device *edev;
752
753         for (i = 0; i < eddnr && i < EDDMAXNR; i++) {
754                 if ((edev = edd_devices[i]))
755                         edd_device_unregister(edev);
756         }
757         firmware_unregister(&edd_subsys);
758 }
759
760 late_initcall(edd_init);
761 module_exit(edd_exit);