| /* * ./kernel_modules/pse/pse-sysfs.c |
| * |
| * This file defines the sysfs interface for the PSE class |
| * |
| * Author: Cradlepoint Technology, Inc. <source@cradlepoint.com> |
| * Kyle Swenson <kswenson@cradlepoint.com> |
| * |
| * Copyright 2017 Cradlepoint Technology, Inc. |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 |
| * as published by the Free Software Foundation. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| */ |
| #include <linux/kernel.h> |
| #include <linux/module.h> |
| #include <linux/device.h> |
| #include <linux/err.h> |
| #include <linux/of.h> |
| #include <linux/slab.h> |
| #include <linux/sysfs.h> |
| #include <linux/idr.h> |
| #include <linux/spinlock.h> |
| #include <linux/list_sort.h> |
| #include <linux/sort.h> |
| |
| #include "pse-port.h" |
| #include "pse-class.h" |
| #include "pse-sysfs.h" |
| |
| char *pse_detect_strings[] = { |
| "0:DETECT_NO_DEVICE", |
| "1:DETECT_NORMAL_DEVICE", /* Short */ |
| "2:DETECT_LEGACY_DEVICE", /* High cap devices */ |
| "3:DETECT_POWERED_DEVICE",/* Signature resistance good */ /* Maybe we want to even break this down into detect Type of device iff that's a detect-time thing */ |
| "4:DETECT_ERROR", |
| NULL |
| }; |
| EXPORT_SYMBOL(pse_detect_strings); |
| char *pse_class_strings[] = { |
| "0:PD_CLASS_UNKNOWN", |
| "1:PD_CLASS_1", |
| "2:PD_CLASS_2", |
| "3:PD_CLASS_3", |
| "4:PD_CLASS_4", |
| "5:PD_CLASS_5", |
| "6:PD_CLASS_6", |
| "7:PD_CLASS_7", |
| "8:PD_CLASS_8", |
| "1D:PD_CLASS_1D", |
| "2D:PD_CLASS_2D", |
| "3D:PD_CLASS_3D", |
| "4D:PD_CLASS_4D", |
| "5D:PD_CLASS_5D", |
| "9:PD_CLASS_INVALID", |
| NULL |
| }; |
| EXPORT_SYMBOL(pse_class_strings); |
| |
| uint16_t pse_port_current_table[] = { |
| 375, /* Class 0 (unknown) */ |
| 112, /* Class 1 */ |
| 206, /* Class 2 */ |
| 375, /* Class 3 */ |
| 638, /* Class 4 (Type 2) */ |
| 938, /* Class 5 */ |
| 1250, /* Class 6 */ |
| 1563, /* Class 7 */ |
| 1875, /* Class 8 */ |
| 2*112, /* Class 1D */ |
| 2*206, /* Class 2D */ |
| 2*375, /* Class 3D */ |
| 2*638, /* Class 4D */ |
| 2*938 /* Class 5D */ |
| }; |
| |
| uint32_t pse_port_power_table[] = { |
| 15400, /* Class 0 (unknown) */ |
| 4000, /* Class 1 */ |
| 7000, /* Class 2 */ |
| 15400, /* Class 3 */ |
| 30000, /* Class 4 */ |
| 45000, /* Class 5 */ |
| 60000, /* Class 6 */ |
| 75000, /* Class 7 */ |
| 90000, /* Class 8 */ |
| 2*4000, /* Class 1D */ |
| 2*7000, /* Class 2D */ |
| 2*15400, /* Class 3D */ |
| 2*30000, /* Class 4D */ |
| 2*45000, /* Class 5D */ |
| }; |
| EXPORT_SYMBOL(pse_port_power_table); |
| /* ============ PSE CLASS ATTRIBUTES ================= |
| * PSE Class level attributes; these appear under /sys/class/pse/ |
| */ |
| static ssize_t |
| pse_version_show(struct class *class, struct class_attribute *attr, char *buf) |
| { |
| return sprintf(buf, "%s\n", PSE_VERSION_STR); |
| } |
| |
| static struct class_attribute pse_version = __ATTR_RO(pse_version); |
| |
| |
| |
| /* -1 for false |
| * 0 for no idea |
| * +1 for true |
| */ |
| static int is_trueish(const char *buf, size_t len) |
| { |
| if (len == 0) |
| return 0; |
| |
| switch (buf[0]) { |
| case '1': |
| /* ASCII char 0x31 => '1' means true*/ |
| case 'y': |
| case 'Y': |
| /* [yY].* */ |
| case 't': |
| case 'T': |
| /* [tT].* */ |
| case 'e': |
| case 'E': |
| /* [eE] for enable */ |
| return 1; |
| case '0': |
| /* ASCII char 0x30 => '0' means false */ |
| case 'n': |
| case 'N': |
| /* [nN].* for No */ |
| case 'F': |
| case 'f': |
| /* [fF].* for false */ |
| case 'd': |
| case 'D': |
| /* [dD] for disable*/ |
| return -1; |
| case 'o': |
| case 'O': |
| /* Could be On, or Off need to look more */ |
| break; |
| default: |
| /* Bail if we don't recognize the first char */ |
| return 0; |
| } |
| /*If we're here we know the first char is [oO] */ |
| /* But if we've only got one char in the buffer, we can't determine so bail*/ |
| if (len == 1) |
| return 0; |
| |
| /* Here, we know len > 1, so at least two */ |
| switch (buf[1]) { |
| case 'n': |
| case 'N': |
| /* for [oO][nN].* */ |
| return 1; |
| case 'f': |
| case 'F': |
| /* for [oO][fF].* */ |
| return -1; |
| default: |
| return 0; |
| } |
| return 0; |
| } |
| |
| static ssize_t |
| port_priority_show(struct class *class, struct class_attribute *attr, char *buf) |
| { |
| struct class_dev_iter iter; |
| struct device *dev; |
| int bput = 0; |
| |
| class_dev_iter_init(&iter, class, NULL, NULL); |
| dev = class_dev_iter_next(&iter); |
| while (dev) { |
| bput += snprintf((char *)(buf+bput), 32, "port%d -> priority %d\n", to_pse_port_device(dev)->id, to_pse_port_device(dev)->priority); |
| dev = class_dev_iter_next(&iter); |
| } |
| class_dev_iter_exit(&iter); |
| return bput; |
| } |
| |
| /* We are implying port and specifying priority: |
| * |Port 0 relative priority 2 |
| * | |Port 1 is priority 3 |
| * | | |Port 2 has priority 1 |
| * | | | |Port 3 is priority 0 (highest) |
| * 2 3 1 0 |
| * |
| */ |
| static ssize_t |
| port_priority_store(struct class *class, struct class_attribute *attr, const char *buff, size_t count) |
| { |
| struct device *dev; |
| struct class_dev_iter iter; |
| char *current_port_priority; |
| char *port_priorities; |
| char *local_buff; |
| int vi = 0; |
| size_t bget = 0; |
| uint8_t port_priority = 0; |
| struct pse_data *pse_data = get_pse_data(class); |
| struct pse_port_device *port; |
| |
| local_buff = kzalloc(sizeof(char) * (count + 1), GFP_KERNEL); |
| if (!local_buff) |
| return -ENOMEM; |
| |
| memcpy(local_buff, buff, count); |
| port_priorities=local_buff; |
| pr_debug("buff=%s, count=%ld\n", port_priorities, count); |
| mutex_lock(&pse_data->pse_lock); |
| while (bget <= count && vi < PSE_MAX_PORTS) { |
| current_port_priority = strsep(&port_priorities, " "); |
| if (current_port_priority == NULL) { |
| break; |
| } |
| pr_debug("input=%s port_priorities=%s Current_port_priority=%s bget=%lu\n", buff, port_priorities, current_port_priority, bget); |
| if (kstrtou8(current_port_priority, 0, &port_priority) < 0) { |
| break; |
| } |
| pse_data->port_priorities[vi++] = port_priority; |
| bget += strlen(current_port_priority) + 1; |
| } |
| class_dev_iter_init(&iter, class, NULL, NULL); |
| dev = class_dev_iter_next(&iter); |
| while (dev) { |
| port = to_pse_port_device(dev); |
| port->priority = pse_data->port_priorities[port->id]; |
| dev_dbg(dev, "Storing port_priority %hhu to port%u", port->priority, port->id); |
| dev = class_dev_iter_next(&iter); |
| } |
| class_dev_iter_exit(&iter); |
| relink_priority_list(class); /* Rebuild the dubly linked list */ |
| redistribute_power(class); /* Force every port off & on again, to get them to repower in correct, priority order */ |
| mutex_unlock(&pse_data->pse_lock); |
| kfree(local_buff); |
| |
| return count; |
| } |
| static struct class_attribute port_priority = __ATTR_RW(port_priority); |
| |
| static ssize_t |
| power_budget_show(struct class *class, struct class_attribute *attr, char *buf) |
| { |
| struct pse_data *pse_data = get_pse_data(class); |
| |
| return sprintf(buf, "%d", pse_data->power_budget); |
| } |
| static ssize_t |
| power_budget_store(struct class *class, struct class_attribute *attr, const char *buff, size_t count) |
| { |
| int new; |
| int res; |
| struct pse_data *pse_data = get_pse_data(class); |
| |
| res = kstrtoint(buff, 0, &new); |
| if (res != 0) |
| return res; |
| mutex_lock(&pse_data->pse_lock); |
| pse_data->power_budget = new; |
| redistribute_power(&pse_data->cls); |
| mutex_unlock(&pse_data->pse_lock); |
| return count; |
| } |
| static struct class_attribute power_budget = __ATTR_RW(power_budget); |
| |
| static ssize_t |
| enable_show(struct class *class, struct class_attribute *attr, char *buf) |
| { |
| struct pse_data *pse_data = get_pse_data(class); |
| |
| return sprintf(buf, pse_data->enabled ? "True" : "False"); |
| } |
| |
| static ssize_t |
| enable_store(struct class *class, struct class_attribute *attr, const char *buf, size_t count) |
| { |
| |
| struct pse_data *pse_data = get_pse_data(class); |
| int t = is_trueish(buf, count); |
| |
| if (t == 0) |
| return -EINVAL; |
| |
| mutex_lock(&pse_data->pse_lock); |
| if (t > 0) |
| pse_data->enabled = 1; |
| else |
| pse_data->enabled = 0; |
| |
| redistribute_power(class); |
| mutex_unlock(&pse_data->pse_lock); |
| return count; |
| } |
| static struct class_attribute enable = __ATTR_RW(enable); |
| |
| static ssize_t |
| power_used_show(struct class *class, struct class_attribute *attr, char *buf) |
| { |
| struct pse_data *pse_data = get_pse_data(class); |
| |
| return sprintf(buf, "%d", pse_data->power_used); |
| } |
| static struct class_attribute power_used = __ATTR_RO(power_used); |
| |
| int pse_class_sysfs_init(struct class *class) |
| { |
| int err; |
| |
| err = class_create_file(class, &pse_version); |
| if (err) { |
| pr_err("couldn't register pse class version file\n"); |
| return err; |
| } |
| err = class_create_file(class, &port_priority); |
| if (err) { |
| pr_err("couldn't register pse class port priority file\n"); |
| return err; |
| } |
| err = class_create_file(class, &power_budget); |
| if (err) { |
| pr_err("couldn't register pse class port priority file\n"); |
| return err; |
| } |
| err = class_create_file(class, &enable); |
| if (err) { |
| pr_err("couldn't register pse class enable file\n"); |
| return err; |
| } |
| err = class_create_file(class, &power_used); |
| if (err) { |
| pr_err("couldn't register pse class enable file\n"); |
| return err; |
| } |
| return 0; |
| } |
| void pse_class_sysfs_close(struct class *class) |
| { |
| class_remove_file(class, &pse_version); |
| class_remove_file(class, &port_priority); |
| class_remove_file(class, &power_budget); |
| class_remove_file(class, &enable); |
| class_remove_file(class, &power_used); |
| } |
| |
| |
| /* ============= PSE PORT DEVICE ATTRIBUTES ================== |
| * These are attributes that appear at /sys/class/pse/portX/<attribute> |
| * These Attributes DO NOT EXIST until a device driver has registered a port device with the pse class! |
| */ |
| |
| /* current_show : Display the current in mA being sourced by the port |
| */ |
| static ssize_t |
| pd_current_show(struct device *dev, struct device_attribute *attr, char *buf) |
| { |
| int pd_current = 0; |
| struct pse_port_device *port = to_pse_port_device(dev); |
| |
| dev_dbg(dev, "%s:%d\n", __func__, __LINE__); |
| if (!port || !port->ops->pd_current || !port->ops->pd_powered) |
| return -EINVAL; |
| |
| if (port->enabled && port->ops->pd_powered(dev)) |
| pd_current = to_pse_port_device(dev)->ops->pd_current(dev); |
| |
| return sprintf(buf, "%d\n", pd_current); |
| } |
| |
| static struct device_attribute port_current = __ATTR_RO(pd_current); |
| |
| /* voltage_show: Show the port's voltage |
| */ |
| static ssize_t |
| port_enable_show(struct device *dev, struct device_attribute *attr, char *buf) |
| { |
| return sprintf(buf, |
| to_pse_port_device(dev)->enabled ? "True" : "False"); |
| } |
| |
| static ssize_t |
| port_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| { |
| int t = is_trueish(buf, count); |
| struct pse_port_device *port = to_pse_port_device(dev); |
| |
| if (t == 0) |
| return -EINVAL; |
| |
| mutex_lock(&port->lock); |
| |
| if (t < 0) { |
| port->enabled = 0; |
| queue_work(port->wq, &port->disconnect); |
| } else { |
| port->enabled = 1; |
| } |
| mutex_unlock(&port->lock); |
| |
| return count; |
| } |
| static struct device_attribute port_enable = __ATTR_RW(port_enable); |
| /* voltage_show: Show the port's voltage |
| */ |
| static ssize_t |
| pd_voltage_show(struct device *dev, struct device_attribute *attr, char *buf) |
| { |
| int pd_voltage = 0; |
| struct pse_port_device *port = to_pse_port_device(dev); |
| |
| if (!port || !port->ops->pd_voltage || !port->ops->pd_powered) |
| return -EINVAL; |
| |
| if (port->enabled && port->ops->pd_powered(dev)) { |
| pd_voltage = port->ops->pd_voltage(dev); |
| } |
| return sprintf(buf, "%d\n", pd_voltage); |
| } |
| |
| static struct device_attribute port_voltage = __ATTR_RO(pd_voltage); |
| |
| /* class_show : Show the port's last classification result |
| */ |
| static ssize_t |
| class_show(struct device *dev, struct device_attribute *attr, char *buf) |
| { |
| uint8_t lc; |
| struct pse_port_device *p = to_pse_port_device(dev); |
| |
| if (!p->enabled) { |
| return sprintf(buf, "%s", pse_class_strings[CLASS_INVALID]); |
| } |
| if (p->ops->update_classification) { |
| p->ops->update_classification(dev); |
| } else { |
| mutex_lock(&p->lock); |
| p->last_class_result = p->ops->last_classification(dev); |
| mutex_unlock(&p->lock); |
| } |
| |
| mutex_lock(&p->lock); |
| lc = p->last_class_result; |
| mutex_unlock(&p->lock); |
| |
| if (lc > CLASS_INVALID) { |
| dev_warn(dev, "PSE Class saw invalid class code 0x%02X\n", lc); |
| lc = CLASS_INVALID; |
| } |
| return sprintf(buf, "%s", pse_class_strings[lc]); |
| } |
| |
| static ssize_t |
| class_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| { |
| int t; |
| struct pse_port_device *port = to_pse_port_device(dev); |
| |
| if (!port || !port->ops->enable_classify) |
| return -EINVAL; |
| |
| t = is_trueish(buf, count); |
| if (t == 0) |
| return -EINVAL; |
| |
| if (t < 0) { |
| dev_dbg(dev, "disabling classification for port %d\n", to_pse_port_device(dev)->port_id); |
| port->ops->enable_classify(dev, 0); |
| } else { |
| dev_dbg(dev, "enabling classification for port %d\n", to_pse_port_device(dev)->port_id); |
| port->ops->enable_classify(dev, 1); |
| } |
| return count; |
| } |
| static struct device_attribute port_class = __ATTR_RW(class); |
| |
| /* detect_show : show the ports' last detection_result |
| */ |
| static ssize_t |
| detect_show(struct device *dev, struct device_attribute *attr, char *buf) |
| { |
| |
| uint8_t detval; |
| struct pse_port_device *p = to_pse_port_device(dev); |
| |
| if (!p->enabled) { |
| return sprintf(buf, "%s", pse_detect_strings[DETECT_NO_DEVICE]); |
| } |
| if (p->ops->update_detection) { |
| p->ops->update_detection(dev); |
| } else { |
| mutex_lock(&p->lock); |
| p->last_detect_result = p->ops->last_detection(dev); |
| mutex_unlock(&p->lock); |
| } |
| mutex_lock(&p->lock); |
| detval = p->last_detect_result; |
| mutex_unlock(&p->lock); |
| |
| if (detval >= PSE_NUM_DETECT_EVENTS) { |
| dev_warn(dev, "PSE class saw an invalid detection code 0x%02x\n", detval); |
| detval = DETECT_ERROR; |
| } |
| return sprintf(buf, "%s", pse_detect_strings[detval]); |
| } |
| static ssize_t |
| detect_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| { |
| int enable; |
| struct pse_port_device *port = to_pse_port_device(dev); |
| |
| if (!port || !port->ops->enable_detect) |
| return -EINVAL; |
| |
| enable = is_trueish(buf, count); |
| if (enable == 0) |
| return -EINVAL; |
| |
| if (enable < 0) { |
| port->ops->enable_detect(dev, 0); |
| dev_dbg(dev, "disabling detection for port %d\n", to_pse_port_device(dev)->port_id); |
| } else { |
| port->ops->enable_detect(dev, 1); |
| dev_dbg(dev, "enabling detection for port %d\n", to_pse_port_device(dev)->port_id); |
| } |
| return count; |
| } |
| static struct device_attribute port_detect = __ATTR_RW(detect); |
| |
| /*port_power_allocation_show: Show the allocation for this port |
| */ |
| static ssize_t |
| port_power_allocation_show(struct device *dev, struct device_attribute *attr, char *buf) |
| { |
| struct pse_port_device *port = to_pse_port_device(dev); |
| |
| if (!port) |
| return -EINVAL; |
| |
| return sprintf(buf, "%d", port->power_allocation); |
| } |
| |
| /*port_power_allocation_store: Request a power allocation change for this port |
| */ |
| static ssize_t |
| port_power_allocation_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| { |
| uint32_t power_request; |
| struct pse_port_device *port = to_pse_port_device(dev); |
| if (!port) |
| return -EINVAL; |
| |
| if (kstrtou32(buf, 0, &power_request) != 0) |
| return -EINVAL; |
| |
| dev_info(dev, "Got %u mW for power request\n", power_request); |
| mutex_lock(&port->lock); |
| port->power_request = power_request; |
| queue_work(port->wq, &port->request_power); |
| mutex_unlock(&port->lock); |
| |
| return count; |
| } |
| static struct device_attribute port_power_allocation = __ATTR_RW(port_power_allocation); |
| |
| |
| /* port_power_store : power "on/off" a port */ |
| static ssize_t |
| port_power_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| { |
| int t; |
| |
| dev_dbg(dev, "%s:%d\n", __func__, __LINE__); |
| if (!to_pse_port_device(dev)->ops->apply_power) |
| return -EINVAL; |
| |
| t = is_trueish(buf, count); |
| if (t == 0) |
| return -EINVAL; |
| |
| if (t > 0) |
| to_pse_port_device(dev)->ops->apply_power(dev, 1); |
| else |
| to_pse_port_device(dev)->ops->apply_power(dev, 0); |
| |
| return count; |
| |
| } |
| static ssize_t |
| port_power_show(struct device *dev, struct device_attribute *attr, char *buf) |
| { |
| if (!to_pse_port_device(dev)->ops->pd_powered) |
| return -EINVAL; |
| |
| return sprintf(buf, to_pse_port_device(dev)->ops->pd_powered(dev) ? "True" : "False"); |
| } |
| static struct device_attribute port_power = __ATTR_RW(port_power); |
| |
| |
| static ssize_t |
| config_show(struct device *dev, struct device_attribute *attr, char *buf) |
| { |
| struct pse_port_config config; |
| |
| if (!to_pse_port_device(dev)->ops->get_config) |
| return -EINVAL; |
| |
| to_pse_port_device(dev)->ops->get_config(dev, &config); |
| |
| return sprintf(buf, "enabled:%s\nmanaged:%s\ntype:%d\n" |
| "disconnect_detection:%s\npd_detection_enabled:%s\n" |
| "pd_classification_enabled:%s\n" |
| "legacy_device_enable:%s\ncurrent_limit:%d\n", |
| (config.enabled ? "True" : "False"), |
| (config.managed ? "True" : "False"), |
| (config.port_type+1), |
| (config.disconnect_event ? "True" : "False"), |
| (config.detect_enable ? "True" : "False"), |
| (config.classify_enable ? "True" : "False"), |
| (config.legacy_enable ? "True" : "False"), |
| config.current_limit); |
| |
| } |
| static struct device_attribute port_config = __ATTR_RO(config); |
| |
| static ssize_t |
| reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| { |
| if (!to_pse_port_device(dev)->ops->reset) |
| return -EINVAL; |
| |
| to_pse_port_device(dev)->ops->reset(dev); |
| return count; |
| } |
| static struct device_attribute port_reset = __ATTR_WO(reset); |
| |
| struct attribute *dev_attrs[] = { |
| &port_current.attr, |
| &port_voltage.attr, |
| &port_class.attr, |
| &port_detect.attr, |
| &port_power.attr, |
| &port_power_allocation.attr, |
| &port_enable.attr, |
| &port_config.attr, |
| &port_reset.attr, |
| NULL, |
| }; |
| |
| struct attribute_group dev_attr_group = { |
| .attrs = dev_attrs, |
| |
| }; |
| |
| const struct attribute_group *pse_port_dev_attr_groups[] = { |
| &dev_attr_group, |
| NULL, |
| }; |