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 | |
| 34 | greybus_function_attr(number); |
| 35 | greybus_function_attr(cport); |
| 36 | greybus_function_attr(class); |
| 37 | greybus_function_attr(subclass); |
| 38 | greybus_function_attr(protocol); |
| 39 | |
| 40 | static struct attribute *function_attrs[] = { |
| 41 | &dev_attr_function_number.attr, |
| 42 | &dev_attr_function_cport.attr, |
| 43 | &dev_attr_function_class.attr, |
| 44 | &dev_attr_function_subclass.attr, |
| 45 | &dev_attr_function_protocol.attr, |
| 46 | NULL, |
| 47 | }; |
| 48 | |
| 49 | static umode_t function_attrs_are_visible(struct kobject *kobj, |
| 50 | struct attribute *a, int n) |
| 51 | { |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 52 | struct greybus_module *gmod = to_greybus_module(kobj_to_dev(kobj)); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 53 | |
| 54 | // FIXME - make this a dynamic structure to "know" if it really is here |
| 55 | // or not easier? |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 56 | if (gmod->function.number || |
| 57 | gmod->function.cport || |
| 58 | gmod->function.class || |
| 59 | gmod->function.subclass || |
| 60 | gmod->function.protocol) |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 61 | return a->mode; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static struct attribute_group function_attr_grp = { |
| 66 | .attrs = function_attrs, |
| 67 | .is_visible = function_attrs_are_visible, |
| 68 | }; |
| 69 | |
| 70 | /* Module fields */ |
| 71 | #define greybus_module_attr(field) \ |
| 72 | static ssize_t module_##field##_show(struct device *dev, \ |
| 73 | struct device_attribute *attr, \ |
| 74 | char *buf) \ |
| 75 | { \ |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 76 | struct greybus_module *gmod = to_greybus_module(dev); \ |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 77 | return sprintf(buf, "%x\n", gmod->module.field); \ |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 78 | } \ |
| 79 | static DEVICE_ATTR_RO(module_##field) |
| 80 | |
| 81 | greybus_module_attr(vendor); |
| 82 | greybus_module_attr(product); |
| 83 | greybus_module_attr(version); |
| 84 | |
Matt Porter | 4fc6454 | 2014-09-26 20:49:49 -0500 | [diff] [blame^] | 85 | static ssize_t module_serial_number_show(struct device *dev, |
| 86 | struct device_attribute *attr, |
| 87 | char *buf) |
| 88 | { |
| 89 | struct greybus_module *gmod = to_greybus_module(dev); |
| 90 | |
| 91 | return sprintf(buf, "%llX\n", |
| 92 | (unsigned long long)le64_to_cpu(gmod->module.serial_number)); |
| 93 | } |
| 94 | static DEVICE_ATTR_RO(module_serial_number); |
| 95 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 96 | static ssize_t module_vendor_string_show(struct device *dev, |
| 97 | struct device_attribute *attr, |
| 98 | char *buf) |
| 99 | { |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 100 | struct greybus_module *gmod = to_greybus_module(dev); |
Greg Kroah-Hartman | 3be03d4 | 2014-09-01 19:10:06 -0700 | [diff] [blame] | 101 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 102 | return sprintf(buf, "%s", |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 103 | greybus_string(gmod, gmod->module.vendor_stringid)); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 104 | } |
| 105 | static DEVICE_ATTR_RO(module_vendor_string); |
| 106 | |
| 107 | static ssize_t module_product_string_show(struct device *dev, |
| 108 | struct device_attribute *attr, |
| 109 | char *buf) |
| 110 | { |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 111 | struct greybus_module *gmod = to_greybus_module(dev); |
Greg Kroah-Hartman | 3be03d4 | 2014-09-01 19:10:06 -0700 | [diff] [blame] | 112 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 113 | return sprintf(buf, "%s", |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 114 | greybus_string(gmod, gmod->module.product_stringid)); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 115 | } |
| 116 | static DEVICE_ATTR_RO(module_product_string); |
| 117 | |
| 118 | static struct attribute *module_attrs[] = { |
| 119 | &dev_attr_module_vendor.attr, |
| 120 | &dev_attr_module_product.attr, |
| 121 | &dev_attr_module_version.attr, |
Matt Porter | 4fc6454 | 2014-09-26 20:49:49 -0500 | [diff] [blame^] | 122 | &dev_attr_module_serial_number.attr, |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 123 | &dev_attr_module_vendor_string.attr, |
| 124 | &dev_attr_module_product_string.attr, |
| 125 | NULL, |
| 126 | }; |
| 127 | |
| 128 | static umode_t module_attrs_are_visible(struct kobject *kobj, |
| 129 | struct attribute *a, int n) |
| 130 | { |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 131 | struct greybus_module *gmod = to_greybus_module(kobj_to_dev(kobj)); |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 132 | |
| 133 | if ((a == &dev_attr_module_vendor_string.attr) && |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 134 | (gmod->module.vendor_stringid)) |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 135 | return a->mode; |
| 136 | if ((a == &dev_attr_module_product_string.attr) && |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 137 | (gmod->module.product_stringid)) |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 138 | return a->mode; |
| 139 | |
| 140 | // FIXME - make this a dynamic structure to "know" if it really is here |
| 141 | // or not easier? |
Matt Porter | 6d63ff7 | 2014-09-26 20:49:48 -0500 | [diff] [blame] | 142 | if (gmod->module.vendor || |
| 143 | gmod->module.product || |
Matt Porter | 4fc6454 | 2014-09-26 20:49:49 -0500 | [diff] [blame^] | 144 | gmod->module.version || |
| 145 | gmod->module.serial_number) |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 146 | return a->mode; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static struct attribute_group module_attr_grp = { |
| 151 | .attrs = module_attrs, |
| 152 | .is_visible = module_attrs_are_visible, |
| 153 | }; |
| 154 | |
| 155 | |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 156 | |
| 157 | |
| 158 | const struct attribute_group *greybus_module_groups[] = { |
| 159 | &function_attr_grp, |
| 160 | &module_attr_grp, |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 161 | NULL, |
| 162 | }; |
| 163 | |