Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Greybus sysfs file functions |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * |
| 6 | * Released under the GPLv2 only. |
| 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/kthread.h> |
| 17 | #include <linux/device.h> |
| 18 | |
| 19 | #include "greybus.h" |
| 20 | |
Greg Kroah-Hartman | f91121b | 2014-09-11 08:22:06 -0700 | [diff] [blame] | 21 | #include "kernel_ver.h" |
| 22 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 23 | /* Function fields */ |
| 24 | #define greybus_function_attr(field) \ |
| 25 | static ssize_t function_##field##_show(struct device *dev, \ |
| 26 | struct device_attribute *attr, \ |
| 27 | char *buf) \ |
| 28 | { \ |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 29 | struct greybus_module *gmod = to_greybus_module(dev); \ |
| 30 | return sprintf(buf, "%d\n", gmod->function.field); \ |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 31 | } \ |
| 32 | static DEVICE_ATTR_RO(function_##field) |
| 33 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 34 | greybus_function_attr(cport); |
Matt Porter | 097724c | 2014-09-26 20:49:50 -0500 | [diff] [blame] | 35 | greybus_function_attr(function_type); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 36 | |
| 37 | static struct attribute *function_attrs[] = { |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 38 | &dev_attr_function_cport.attr, |
Matt Porter | 097724c | 2014-09-26 20:49:50 -0500 | [diff] [blame] | 39 | &dev_attr_function_function_type.attr, |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 40 | NULL, |
| 41 | }; |
| 42 | |
| 43 | static umode_t function_attrs_are_visible(struct kobject *kobj, |
| 44 | struct attribute *a, int n) |
| 45 | { |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 46 | struct greybus_module *gmod = to_greybus_module(kobj_to_dev(kobj)); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 47 | |
| 48 | // FIXME - make this a dynamic structure to "know" if it really is here |
| 49 | // or not easier? |
Matt Porter | 097724c | 2014-09-26 20:49:50 -0500 | [diff] [blame] | 50 | if (gmod->function.cport || |
| 51 | gmod->function.function_type) |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 52 | return a->mode; |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static struct attribute_group function_attr_grp = { |
| 57 | .attrs = function_attrs, |
| 58 | .is_visible = function_attrs_are_visible, |
| 59 | }; |
| 60 | |
| 61 | /* Module fields */ |
| 62 | #define greybus_module_attr(field) \ |
| 63 | static ssize_t module_##field##_show(struct device *dev, \ |
| 64 | struct device_attribute *attr, \ |
| 65 | char *buf) \ |
| 66 | { \ |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 67 | struct greybus_module *gmod = to_greybus_module(dev); \ |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 68 | return sprintf(buf, "%x\n", gmod->module.field); \ |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 69 | } \ |
| 70 | static DEVICE_ATTR_RO(module_##field) |
| 71 | |
| 72 | greybus_module_attr(vendor); |
| 73 | greybus_module_attr(product); |
| 74 | greybus_module_attr(version); |
| 75 | |
Matt Porter | 4fc6454 | 2014-09-26 20:49:49 -0500 | [diff] [blame] | 76 | static ssize_t module_serial_number_show(struct device *dev, |
| 77 | struct device_attribute *attr, |
| 78 | char *buf) |
| 79 | { |
| 80 | struct greybus_module *gmod = to_greybus_module(dev); |
| 81 | |
| 82 | return sprintf(buf, "%llX\n", |
| 83 | (unsigned long long)le64_to_cpu(gmod->module.serial_number)); |
| 84 | } |
| 85 | static DEVICE_ATTR_RO(module_serial_number); |
| 86 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 87 | static ssize_t module_vendor_string_show(struct device *dev, |
| 88 | struct device_attribute *attr, |
| 89 | char *buf) |
| 90 | { |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 91 | struct greybus_module *gmod = to_greybus_module(dev); |
Greg Kroah-Hartman | 3be03d4 | 2014-09-01 19:10:06 -0700 | [diff] [blame] | 92 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 93 | return sprintf(buf, "%s", |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 94 | greybus_string(gmod, gmod->module.vendor_stringid)); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 95 | } |
| 96 | static DEVICE_ATTR_RO(module_vendor_string); |
| 97 | |
| 98 | static ssize_t module_product_string_show(struct device *dev, |
| 99 | struct device_attribute *attr, |
| 100 | char *buf) |
| 101 | { |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 102 | struct greybus_module *gmod = to_greybus_module(dev); |
Greg Kroah-Hartman | 3be03d4 | 2014-09-01 19:10:06 -0700 | [diff] [blame] | 103 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 104 | return sprintf(buf, "%s", |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 105 | greybus_string(gmod, gmod->module.product_stringid)); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 106 | } |
| 107 | static DEVICE_ATTR_RO(module_product_string); |
| 108 | |
| 109 | static struct attribute *module_attrs[] = { |
| 110 | &dev_attr_module_vendor.attr, |
| 111 | &dev_attr_module_product.attr, |
| 112 | &dev_attr_module_version.attr, |
Matt Porter | 4fc6454 | 2014-09-26 20:49:49 -0500 | [diff] [blame] | 113 | &dev_attr_module_serial_number.attr, |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 114 | &dev_attr_module_vendor_string.attr, |
| 115 | &dev_attr_module_product_string.attr, |
| 116 | NULL, |
| 117 | }; |
| 118 | |
| 119 | static umode_t module_attrs_are_visible(struct kobject *kobj, |
| 120 | struct attribute *a, int n) |
| 121 | { |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 122 | struct greybus_module *gmod = to_greybus_module(kobj_to_dev(kobj)); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 123 | |
| 124 | if ((a == &dev_attr_module_vendor_string.attr) && |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 125 | (gmod->module.vendor_stringid)) |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 126 | return a->mode; |
| 127 | if ((a == &dev_attr_module_product_string.attr) && |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 128 | (gmod->module.product_stringid)) |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 129 | return a->mode; |
| 130 | |
| 131 | // FIXME - make this a dynamic structure to "know" if it really is here |
| 132 | // or not easier? |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 133 | if (gmod->module.vendor || |
| 134 | gmod->module.product || |
Matt Porter | 4fc6454 | 2014-09-26 20:49:49 -0500 | [diff] [blame] | 135 | gmod->module.version || |
| 136 | gmod->module.serial_number) |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 137 | return a->mode; |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static struct attribute_group module_attr_grp = { |
| 142 | .attrs = module_attrs, |
| 143 | .is_visible = module_attrs_are_visible, |
| 144 | }; |
| 145 | |
| 146 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 147 | |
| 148 | |
| 149 | const struct attribute_group *greybus_module_groups[] = { |
| 150 | &function_attr_grp, |
| 151 | &module_attr_grp, |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 152 | NULL, |
| 153 | }; |
| 154 | |