2 * Copyright (C) 2012 CERN (www.cern.ch)
3 * Author: Alessandro Rubini <rubini@gnudd.com>
5 * Released according to the GNU GPL, version 2 or any later version.
7 * This work is part of the White Rabbit project, a research effort led
8 * by CERN, the European Institute for Nuclear Research.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/fmc.h>
17 static int fmc_check_version(unsigned long version, const char *name)
19 if (__FMC_MAJOR(version) != FMC_MAJOR) {
20 pr_err("%s: \"%s\" has wrong major (has %li, expected %i)\n",
21 __func__, name, __FMC_MAJOR(version), FMC_MAJOR);
25 if (__FMC_MINOR(version) != FMC_MINOR)
26 pr_info("%s: \"%s\" has wrong minor (has %li, expected %i)\n",
27 __func__, name, __FMC_MINOR(version), FMC_MINOR);
31 static int fmc_uevent(struct device *dev, struct kobj_uevent_env *env)
33 /* struct fmc_device *fdev = to_fmc_device(dev); */
35 /* FIXME: The MODALIAS */
36 add_uevent_var(env, "MODALIAS=%s", "fmc");
40 static int fmc_probe(struct device *dev)
42 struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
43 struct fmc_device *fdev = to_fmc_device(dev);
45 return fdrv->probe(fdev);
48 static int fmc_remove(struct device *dev)
50 struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
51 struct fmc_device *fdev = to_fmc_device(dev);
53 return fdrv->remove(fdev);
56 static void fmc_shutdown(struct device *dev)
58 /* not implemented but mandatory */
61 static struct bus_type fmc_bus_type = {
67 .shutdown = fmc_shutdown,
70 static void fmc_release(struct device *dev)
72 struct fmc_device *fmc = container_of(dev, struct fmc_device, dev);
78 * The eeprom is exported in sysfs, through a binary attribute
81 static ssize_t fmc_read_eeprom(struct file *file, struct kobject *kobj,
82 struct bin_attribute *bin_attr,
83 char *buf, loff_t off, size_t count)
86 struct fmc_device *fmc;
89 dev = container_of(kobj, struct device, kobj);
90 fmc = container_of(dev, struct fmc_device, dev);
91 eelen = fmc->eeprom_len;
96 if (off + count > eelen)
98 memcpy(buf, fmc->eeprom + off, count);
102 static ssize_t fmc_write_eeprom(struct file *file, struct kobject *kobj,
103 struct bin_attribute *bin_attr,
104 char *buf, loff_t off, size_t count)
107 struct fmc_device *fmc;
109 dev = container_of(kobj, struct device, kobj);
110 fmc = container_of(dev, struct fmc_device, dev);
111 return fmc->op->write_ee(fmc, off, buf, count);
114 static struct bin_attribute fmc_eeprom_attr = {
115 .attr = { .name = "eeprom", .mode = S_IRUGO | S_IWUSR, },
116 .size = 8192, /* more or less standard */
117 .read = fmc_read_eeprom,
118 .write = fmc_write_eeprom,
122 * Functions for client modules follow
125 int fmc_driver_register(struct fmc_driver *drv)
127 if (fmc_check_version(drv->version, drv->driver.name))
129 drv->driver.bus = &fmc_bus_type;
130 return driver_register(&drv->driver);
132 EXPORT_SYMBOL(fmc_driver_register);
134 void fmc_driver_unregister(struct fmc_driver *drv)
136 driver_unregister(&drv->driver);
138 EXPORT_SYMBOL(fmc_driver_unregister);
141 * When a device set is registered, all eeproms must be read
142 * and all FRUs must be parsed
144 int fmc_device_register_n(struct fmc_device **devs, int n)
146 struct fmc_device *fmc, **devarray;
153 /* Check the version of the first data structure (function prints) */
154 if (fmc_check_version(devs[0]->version, devs[0]->carrier_name))
157 devarray = kmemdup(devs, n * sizeof(*devs), GFP_KERNEL);
161 /* Make all other checks before continuing, for all devices */
162 for (i = 0; i < n; i++) {
165 pr_err("%s: device nr. %i has no hwdev pointer\n",
170 if (fmc->flags & FMC_DEVICE_NO_MEZZANINE) {
171 dev_info(fmc->hwdev, "absent mezzanine in slot %d\n",
176 dev_err(fmc->hwdev, "no eeprom provided for slot %i\n",
180 if (!fmc->eeprom_addr) {
181 dev_err(fmc->hwdev, "no eeprom_addr for slot %i\n",
185 if (!fmc->carrier_name || !fmc->carrier_data ||
188 "deivce nr %i: carrier name, "
189 "data or dev_id not set\n", i);
201 /* Validation is ok. Now init and register the devices */
202 for (i = 0; i < n; i++) {
205 fmc->nr_slots = n; /* each slot must know how many are there */
206 fmc->devarray = devarray;
208 device_initialize(&fmc->dev);
209 fmc->dev.release = fmc_release;
210 fmc->dev.parent = fmc->hwdev;
212 /* Fill the identification stuff (may fail) */
213 fmc_fill_id_info(fmc);
215 fmc->dev.bus = &fmc_bus_type;
217 /* Name from mezzanine info or carrier info. Or 0,1,2.. */
218 device_id = fmc->device_id;
219 if (!fmc->mezzanine_name)
220 dev_set_name(&fmc->dev, "fmc-%04x", device_id);
222 dev_set_name(&fmc->dev, "%s-%04x", fmc->mezzanine_name,
224 ret = device_add(&fmc->dev);
226 dev_err(fmc->hwdev, "Slot %i: Failed in registering "
227 "\"%s\"\n", fmc->slot_id, fmc->dev.kobj.name);
230 ret = sysfs_create_bin_file(&fmc->dev.kobj, &fmc_eeprom_attr);
232 dev_err(&fmc->dev, "Failed in registering eeprom\n");
235 /* This device went well, give information to the user */
236 fmc_dump_eeprom(fmc);
242 device_del(&fmc->dev);
244 fmc_free_id_info(fmc);
245 put_device(&fmc->dev);
248 for (i--; i >= 0; i--) {
249 sysfs_remove_bin_file(&devs[i]->dev.kobj, &fmc_eeprom_attr);
250 device_del(&devs[i]->dev);
251 fmc_free_id_info(devs[i]);
252 put_device(&devs[i]->dev);
257 EXPORT_SYMBOL(fmc_device_register_n);
259 int fmc_device_register(struct fmc_device *fmc)
261 return fmc_device_register_n(&fmc, 1);
263 EXPORT_SYMBOL(fmc_device_register);
265 void fmc_device_unregister_n(struct fmc_device **devs, int n)
272 /* Free devarray first, not used by the later loop */
273 kfree(devs[0]->devarray);
275 for (i = 0; i < n; i++) {
276 sysfs_remove_bin_file(&devs[i]->dev.kobj, &fmc_eeprom_attr);
277 device_del(&devs[i]->dev);
278 fmc_free_id_info(devs[i]);
279 put_device(&devs[i]->dev);
282 EXPORT_SYMBOL(fmc_device_unregister_n);
284 void fmc_device_unregister(struct fmc_device *fmc)
286 fmc_device_unregister_n(&fmc, 1);
288 EXPORT_SYMBOL(fmc_device_unregister);
290 /* Init and exit are trivial */
291 static int fmc_init(void)
293 return bus_register(&fmc_bus_type);
296 static void fmc_exit(void)
298 bus_unregister(&fmc_bus_type);
301 module_init(fmc_init);
302 module_exit(fmc_exit);
304 MODULE_LICENSE("GPL");