blob: 55dc7b786ac8e6872b402cbd22d214bb076d0617 [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
34greybus_function_attr(number);
35greybus_function_attr(cport);
36greybus_function_attr(class);
37greybus_function_attr(subclass);
38greybus_function_attr(protocol);
39
40static 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
49static umode_t function_attrs_are_visible(struct kobject *kobj,
50 struct attribute *a, int n)
51{
Alex Elder778c69c2014-09-22 19:19:03 -050052 struct greybus_module *gmod = to_greybus_module(kobj_to_dev(kobj));
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070053
54 // FIXME - make this a dynamic structure to "know" if it really is here
55 // or not easier?
Alex Elder778c69c2014-09-22 19:19:03 -050056 if (gmod->function.number ||
57 gmod->function.cport ||
58 gmod->function.class ||
59 gmod->function.subclass ||
60 gmod->function.protocol)
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070061 return a->mode;
62 return 0;
63}
64
65static 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) \
72static ssize_t module_##field##_show(struct device *dev, \
73 struct device_attribute *attr, \
74 char *buf) \
75{ \
Alex Elder778c69c2014-09-22 19:19:03 -050076 struct greybus_module *gmod = to_greybus_module(dev); \
Matt Porter6d63ff72014-09-26 20:49:48 -050077 return sprintf(buf, "%x\n", gmod->module.field); \
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070078} \
79static DEVICE_ATTR_RO(module_##field)
80
81greybus_module_attr(vendor);
82greybus_module_attr(product);
83greybus_module_attr(version);
84
Matt Porter4fc64542014-09-26 20:49:49 -050085static 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}
94static DEVICE_ATTR_RO(module_serial_number);
95
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070096static ssize_t module_vendor_string_show(struct device *dev,
97 struct device_attribute *attr,
98 char *buf)
99{
Alex Elder778c69c2014-09-22 19:19:03 -0500100 struct greybus_module *gmod = to_greybus_module(dev);
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -0700101
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700102 return sprintf(buf, "%s",
Matt Porter6d63ff72014-09-26 20:49:48 -0500103 greybus_string(gmod, gmod->module.vendor_stringid));
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700104}
105static DEVICE_ATTR_RO(module_vendor_string);
106
107static ssize_t module_product_string_show(struct device *dev,
108 struct device_attribute *attr,
109 char *buf)
110{
Alex Elder778c69c2014-09-22 19:19:03 -0500111 struct greybus_module *gmod = to_greybus_module(dev);
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -0700112
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700113 return sprintf(buf, "%s",
Matt Porter6d63ff72014-09-26 20:49:48 -0500114 greybus_string(gmod, gmod->module.product_stringid));
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700115}
116static DEVICE_ATTR_RO(module_product_string);
117
118static struct attribute *module_attrs[] = {
119 &dev_attr_module_vendor.attr,
120 &dev_attr_module_product.attr,
121 &dev_attr_module_version.attr,
Matt Porter4fc64542014-09-26 20:49:49 -0500122 &dev_attr_module_serial_number.attr,
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700123 &dev_attr_module_vendor_string.attr,
124 &dev_attr_module_product_string.attr,
125 NULL,
126};
127
128static umode_t module_attrs_are_visible(struct kobject *kobj,
129 struct attribute *a, int n)
130{
Alex Elder778c69c2014-09-22 19:19:03 -0500131 struct greybus_module *gmod = to_greybus_module(kobj_to_dev(kobj));
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700132
133 if ((a == &dev_attr_module_vendor_string.attr) &&
Matt Porter6d63ff72014-09-26 20:49:48 -0500134 (gmod->module.vendor_stringid))
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700135 return a->mode;
136 if ((a == &dev_attr_module_product_string.attr) &&
Matt Porter6d63ff72014-09-26 20:49:48 -0500137 (gmod->module.product_stringid))
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700138 return a->mode;
139
140 // FIXME - make this a dynamic structure to "know" if it really is here
141 // or not easier?
Matt Porter6d63ff72014-09-26 20:49:48 -0500142 if (gmod->module.vendor ||
143 gmod->module.product ||
Matt Porter4fc64542014-09-26 20:49:49 -0500144 gmod->module.version ||
145 gmod->module.serial_number)
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700146 return a->mode;
147 return 0;
148}
149
150static struct attribute_group module_attr_grp = {
151 .attrs = module_attrs,
152 .is_visible = module_attrs_are_visible,
153};
154
155
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700156
157
158const struct attribute_group *greybus_module_groups[] = {
159 &function_attr_grp,
160 &module_attr_grp,
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700161 NULL,
162};
163