blob: 2777b8cf5b3ac0be6b9d5caf9f9b73442e3259ed [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"
Greg Kroah-Hartmanf91121b2014-09-11 08:22:06 -070020#include "kernel_ver.h"
21
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070022/* Module fields */
Alex Eldere1e9dbd2014-10-01 21:54:11 -050023#define gb_module_attr(field) \
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070024static ssize_t module_##field##_show(struct device *dev, \
25 struct device_attribute *attr, \
26 char *buf) \
27{ \
Alex Eldere1e9dbd2014-10-01 21:54:11 -050028 struct gb_module *gmod = to_gb_module(dev); \
Alex Elderb09c94a2014-10-01 21:54:16 -050029 return sprintf(buf, "%x\n", gmod->field); \
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070030} \
31static DEVICE_ATTR_RO(module_##field)
32
Alex Eldere1e9dbd2014-10-01 21:54:11 -050033gb_module_attr(vendor);
34gb_module_attr(product);
35gb_module_attr(version);
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070036
Matt Porter4fc64542014-09-26 20:49:49 -050037static ssize_t module_serial_number_show(struct device *dev,
38 struct device_attribute *attr,
39 char *buf)
40{
Alex Eldere1e9dbd2014-10-01 21:54:11 -050041 struct gb_module *gmod = to_gb_module(dev);
Matt Porter4fc64542014-09-26 20:49:49 -050042
Alex Elder63cc9322014-10-02 12:30:02 -050043 return sprintf(buf, "%llX\n", (unsigned long long)gmod->unique_id);
Matt Porter4fc64542014-09-26 20:49:49 -050044}
45static DEVICE_ATTR_RO(module_serial_number);
46
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070047static ssize_t module_vendor_string_show(struct device *dev,
48 struct device_attribute *attr,
49 char *buf)
50{
Alex Eldere1e9dbd2014-10-01 21:54:11 -050051 struct gb_module *gmod = to_gb_module(dev);
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -070052
Alex Elderb09c94a2014-10-01 21:54:16 -050053 return sprintf(buf, "%s", gmod->vendor_string);
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070054}
55static DEVICE_ATTR_RO(module_vendor_string);
56
57static ssize_t module_product_string_show(struct device *dev,
58 struct device_attribute *attr,
59 char *buf)
60{
Alex Eldere1e9dbd2014-10-01 21:54:11 -050061 struct gb_module *gmod = to_gb_module(dev);
Greg Kroah-Hartman3be03d42014-09-01 19:10:06 -070062
Alex Elderb09c94a2014-10-01 21:54:16 -050063 return sprintf(buf, "%s", gmod->product_string);
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070064}
65static DEVICE_ATTR_RO(module_product_string);
66
67static struct attribute *module_attrs[] = {
68 &dev_attr_module_vendor.attr,
69 &dev_attr_module_product.attr,
70 &dev_attr_module_version.attr,
Matt Porter4fc64542014-09-26 20:49:49 -050071 &dev_attr_module_serial_number.attr,
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070072 &dev_attr_module_vendor_string.attr,
73 &dev_attr_module_product_string.attr,
74 NULL,
75};
76
77static umode_t module_attrs_are_visible(struct kobject *kobj,
78 struct attribute *a, int n)
79{
Alex Eldere1e9dbd2014-10-01 21:54:11 -050080 struct gb_module *gmod = to_gb_module(kobj_to_dev(kobj));
Alex Elderb09c94a2014-10-01 21:54:16 -050081 umode_t mode = a->mode;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070082
Alex Elderb09c94a2014-10-01 21:54:16 -050083 if (a == &dev_attr_module_vendor_string.attr && gmod->vendor_string)
84 return mode;
85 if (a == &dev_attr_module_product_string.attr && gmod->product_string)
86 return mode;
87 if (gmod->vendor || gmod->product || gmod->version)
88 return mode;
Alex Elder63cc9322014-10-02 12:30:02 -050089 if (gmod->unique_id)
Alex Elderb09c94a2014-10-01 21:54:16 -050090 return mode;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070091
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -070092 return 0;
93}
94
95static struct attribute_group module_attr_grp = {
96 .attrs = module_attrs,
97 .is_visible = module_attrs_are_visible,
98};
99
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700100const struct attribute_group *greybus_module_groups[] = {
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700101 &module_attr_grp,
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700102 NULL,
103};
104