blob: 1f17388445ad87524b6ebb4b857705d41905ec81 [file] [log] [blame]
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -07001/*
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-Hartmanf91121b2014-09-11 08:22:06 -070021#include "kernel_ver.h"
22
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070023/* Function fields */
24#define greybus_function_attr(field) \
25static ssize_t function_##field##_show(struct device *dev, \
26 struct device_attribute *attr, \
27 char *buf) \
28{ \
Alex Elder778c69c2014-09-22 19:19:03 -050029 struct greybus_module *gmod = to_greybus_module(dev); \
30 return sprintf(buf, "%d\n", gmod->function.field); \
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070031} \
32static DEVICE_ATTR_RO(function_##field)
33
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070034greybus_function_attr(cport);
Matt Porter097724c2014-09-26 20:49:50 -050035greybus_function_attr(function_type);
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070036
37static struct attribute *function_attrs[] = {
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070038 &dev_attr_function_cport.attr,
Matt Porter097724c2014-09-26 20:49:50 -050039 &dev_attr_function_function_type.attr,
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070040 NULL,
41};
42
43static umode_t function_attrs_are_visible(struct kobject *kobj,
44 struct attribute *a, int n)
45{
Alex Elder778c69c2014-09-22 19:19:03 -050046 struct greybus_module *gmod = to_greybus_module(kobj_to_dev(kobj));
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070047
48 // FIXME - make this a dynamic structure to "know" if it really is here
49 // or not easier?
Matt Porter097724c2014-09-26 20:49:50 -050050 if (gmod->function.cport ||
51 gmod->function.function_type)
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070052 return a->mode;
53 return 0;
54}
55
56static 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) \
63static ssize_t module_##field##_show(struct device *dev, \
64 struct device_attribute *attr, \
65 char *buf) \
66{ \
Alex Elder778c69c2014-09-22 19:19:03 -050067 struct greybus_module *gmod = to_greybus_module(dev); \
Matt Porter6d63ff72014-09-26 20:49:48 -050068 return sprintf(buf, "%x\n", gmod->module.field); \
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070069} \
70static DEVICE_ATTR_RO(module_##field)
71
72greybus_module_attr(vendor);
73greybus_module_attr(product);
74greybus_module_attr(version);
75
Matt Porter4fc64542014-09-26 20:49:49 -050076static 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}
85static DEVICE_ATTR_RO(module_serial_number);
86
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070087static ssize_t module_vendor_string_show(struct device *dev,
88 struct device_attribute *attr,
89 char *buf)
90{
Alex Elder778c69c2014-09-22 19:19:03 -050091 struct greybus_module *gmod = to_greybus_module(dev);
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -070092
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070093 return sprintf(buf, "%s",
Matt Porter6d63ff72014-09-26 20:49:48 -050094 greybus_string(gmod, gmod->module.vendor_stringid));
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070095}
96static DEVICE_ATTR_RO(module_vendor_string);
97
98static ssize_t module_product_string_show(struct device *dev,
99 struct device_attribute *attr,
100 char *buf)
101{
Alex Elder778c69c2014-09-22 19:19:03 -0500102 struct greybus_module *gmod = to_greybus_module(dev);
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -0700103
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700104 return sprintf(buf, "%s",
Matt Porter6d63ff72014-09-26 20:49:48 -0500105 greybus_string(gmod, gmod->module.product_stringid));
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700106}
107static DEVICE_ATTR_RO(module_product_string);
108
109static struct attribute *module_attrs[] = {
110 &dev_attr_module_vendor.attr,
111 &dev_attr_module_product.attr,
112 &dev_attr_module_version.attr,
Matt Porter4fc64542014-09-26 20:49:49 -0500113 &dev_attr_module_serial_number.attr,
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700114 &dev_attr_module_vendor_string.attr,
115 &dev_attr_module_product_string.attr,
116 NULL,
117};
118
119static umode_t module_attrs_are_visible(struct kobject *kobj,
120 struct attribute *a, int n)
121{
Alex Elder778c69c2014-09-22 19:19:03 -0500122 struct greybus_module *gmod = to_greybus_module(kobj_to_dev(kobj));
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700123
124 if ((a == &dev_attr_module_vendor_string.attr) &&
Matt Porter6d63ff72014-09-26 20:49:48 -0500125 (gmod->module.vendor_stringid))
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700126 return a->mode;
127 if ((a == &dev_attr_module_product_string.attr) &&
Matt Porter6d63ff72014-09-26 20:49:48 -0500128 (gmod->module.product_stringid))
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700129 return a->mode;
130
131 // FIXME - make this a dynamic structure to "know" if it really is here
132 // or not easier?
Matt Porter6d63ff72014-09-26 20:49:48 -0500133 if (gmod->module.vendor ||
134 gmod->module.product ||
Matt Porter4fc64542014-09-26 20:49:49 -0500135 gmod->module.version ||
136 gmod->module.serial_number)
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700137 return a->mode;
138 return 0;
139}
140
141static struct attribute_group module_attr_grp = {
142 .attrs = module_attrs,
143 .is_visible = module_attrs_are_visible,
144};
145
146
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700147
148
149const struct attribute_group *greybus_module_groups[] = {
150 &function_attr_grp,
151 &module_attr_grp,
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700152 NULL,
153};
154