0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/pm_runtime.h>
0010 #include <linux/component.h>
0011 #include <linux/property.h>
0012
0013 #include "tb.h"
0014
0015 static int connector_bind(struct device *dev, struct device *connector, void *data)
0016 {
0017 int ret;
0018
0019 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
0020 if (ret)
0021 return ret;
0022
0023 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
0024 if (ret)
0025 sysfs_remove_link(&dev->kobj, "connector");
0026
0027 return ret;
0028 }
0029
0030 static void connector_unbind(struct device *dev, struct device *connector, void *data)
0031 {
0032 sysfs_remove_link(&connector->kobj, dev_name(dev));
0033 sysfs_remove_link(&dev->kobj, "connector");
0034 }
0035
0036 static const struct component_ops connector_ops = {
0037 .bind = connector_bind,
0038 .unbind = connector_unbind,
0039 };
0040
0041 static ssize_t link_show(struct device *dev, struct device_attribute *attr,
0042 char *buf)
0043 {
0044 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
0045 struct tb_port *port = usb4->port;
0046 struct tb *tb = port->sw->tb;
0047 const char *link;
0048
0049 if (mutex_lock_interruptible(&tb->lock))
0050 return -ERESTARTSYS;
0051
0052 if (tb_is_upstream_port(port))
0053 link = port->sw->link_usb4 ? "usb4" : "tbt";
0054 else if (tb_port_has_remote(port))
0055 link = port->remote->sw->link_usb4 ? "usb4" : "tbt";
0056 else
0057 link = "none";
0058
0059 mutex_unlock(&tb->lock);
0060
0061 return sysfs_emit(buf, "%s\n", link);
0062 }
0063 static DEVICE_ATTR_RO(link);
0064
0065 static struct attribute *common_attrs[] = {
0066 &dev_attr_link.attr,
0067 NULL
0068 };
0069
0070 static const struct attribute_group common_group = {
0071 .attrs = common_attrs,
0072 };
0073
0074 static int usb4_port_offline(struct usb4_port *usb4)
0075 {
0076 struct tb_port *port = usb4->port;
0077 int ret;
0078
0079 ret = tb_acpi_power_on_retimers(port);
0080 if (ret)
0081 return ret;
0082
0083 ret = usb4_port_router_offline(port);
0084 if (ret) {
0085 tb_acpi_power_off_retimers(port);
0086 return ret;
0087 }
0088
0089 ret = tb_retimer_scan(port, false);
0090 if (ret) {
0091 usb4_port_router_online(port);
0092 tb_acpi_power_off_retimers(port);
0093 }
0094
0095 return ret;
0096 }
0097
0098 static void usb4_port_online(struct usb4_port *usb4)
0099 {
0100 struct tb_port *port = usb4->port;
0101
0102 usb4_port_router_online(port);
0103 tb_acpi_power_off_retimers(port);
0104 }
0105
0106 static ssize_t offline_show(struct device *dev,
0107 struct device_attribute *attr, char *buf)
0108 {
0109 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
0110
0111 return sysfs_emit(buf, "%d\n", usb4->offline);
0112 }
0113
0114 static ssize_t offline_store(struct device *dev,
0115 struct device_attribute *attr, const char *buf, size_t count)
0116 {
0117 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
0118 struct tb_port *port = usb4->port;
0119 struct tb *tb = port->sw->tb;
0120 bool val;
0121 int ret;
0122
0123 ret = kstrtobool(buf, &val);
0124 if (ret)
0125 return ret;
0126
0127 pm_runtime_get_sync(&usb4->dev);
0128
0129 if (mutex_lock_interruptible(&tb->lock)) {
0130 ret = -ERESTARTSYS;
0131 goto out_rpm;
0132 }
0133
0134 if (val == usb4->offline)
0135 goto out_unlock;
0136
0137
0138 if (tb_port_has_remote(port)) {
0139 ret = -EBUSY;
0140 goto out_unlock;
0141 }
0142
0143 if (val) {
0144 ret = usb4_port_offline(usb4);
0145 if (ret)
0146 goto out_unlock;
0147 } else {
0148 usb4_port_online(usb4);
0149 tb_retimer_remove_all(port);
0150 }
0151
0152 usb4->offline = val;
0153 tb_port_dbg(port, "%s offline mode\n", val ? "enter" : "exit");
0154
0155 out_unlock:
0156 mutex_unlock(&tb->lock);
0157 out_rpm:
0158 pm_runtime_mark_last_busy(&usb4->dev);
0159 pm_runtime_put_autosuspend(&usb4->dev);
0160
0161 return ret ? ret : count;
0162 }
0163 static DEVICE_ATTR_RW(offline);
0164
0165 static ssize_t rescan_store(struct device *dev,
0166 struct device_attribute *attr, const char *buf, size_t count)
0167 {
0168 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
0169 struct tb_port *port = usb4->port;
0170 struct tb *tb = port->sw->tb;
0171 bool val;
0172 int ret;
0173
0174 ret = kstrtobool(buf, &val);
0175 if (ret)
0176 return ret;
0177
0178 if (!val)
0179 return count;
0180
0181 pm_runtime_get_sync(&usb4->dev);
0182
0183 if (mutex_lock_interruptible(&tb->lock)) {
0184 ret = -ERESTARTSYS;
0185 goto out_rpm;
0186 }
0187
0188
0189 if (!usb4->offline) {
0190 ret = -EINVAL;
0191 goto out_unlock;
0192 }
0193
0194 tb_retimer_remove_all(port);
0195 ret = tb_retimer_scan(port, true);
0196
0197 out_unlock:
0198 mutex_unlock(&tb->lock);
0199 out_rpm:
0200 pm_runtime_mark_last_busy(&usb4->dev);
0201 pm_runtime_put_autosuspend(&usb4->dev);
0202
0203 return ret ? ret : count;
0204 }
0205 static DEVICE_ATTR_WO(rescan);
0206
0207 static struct attribute *service_attrs[] = {
0208 &dev_attr_offline.attr,
0209 &dev_attr_rescan.attr,
0210 NULL
0211 };
0212
0213 static umode_t service_attr_is_visible(struct kobject *kobj,
0214 struct attribute *attr, int n)
0215 {
0216 struct device *dev = kobj_to_dev(kobj);
0217 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
0218
0219
0220
0221
0222
0223 return usb4->can_offline ? attr->mode : 0;
0224 }
0225
0226 static const struct attribute_group service_group = {
0227 .attrs = service_attrs,
0228 .is_visible = service_attr_is_visible,
0229 };
0230
0231 static const struct attribute_group *usb4_port_device_groups[] = {
0232 &common_group,
0233 &service_group,
0234 NULL
0235 };
0236
0237 static void usb4_port_device_release(struct device *dev)
0238 {
0239 struct usb4_port *usb4 = container_of(dev, struct usb4_port, dev);
0240
0241 kfree(usb4);
0242 }
0243
0244 struct device_type usb4_port_device_type = {
0245 .name = "usb4_port",
0246 .groups = usb4_port_device_groups,
0247 .release = usb4_port_device_release,
0248 };
0249
0250
0251
0252
0253
0254
0255
0256
0257 struct usb4_port *usb4_port_device_add(struct tb_port *port)
0258 {
0259 struct usb4_port *usb4;
0260 int ret;
0261
0262 usb4 = kzalloc(sizeof(*usb4), GFP_KERNEL);
0263 if (!usb4)
0264 return ERR_PTR(-ENOMEM);
0265
0266 usb4->port = port;
0267 usb4->dev.type = &usb4_port_device_type;
0268 usb4->dev.parent = &port->sw->dev;
0269 dev_set_name(&usb4->dev, "usb4_port%d", port->port);
0270
0271 ret = device_register(&usb4->dev);
0272 if (ret) {
0273 put_device(&usb4->dev);
0274 return ERR_PTR(ret);
0275 }
0276
0277 if (dev_fwnode(&usb4->dev)) {
0278 ret = component_add(&usb4->dev, &connector_ops);
0279 if (ret) {
0280 dev_err(&usb4->dev, "failed to add component\n");
0281 device_unregister(&usb4->dev);
0282 }
0283 }
0284
0285 pm_runtime_no_callbacks(&usb4->dev);
0286 pm_runtime_set_active(&usb4->dev);
0287 pm_runtime_enable(&usb4->dev);
0288 pm_runtime_set_autosuspend_delay(&usb4->dev, TB_AUTOSUSPEND_DELAY);
0289 pm_runtime_mark_last_busy(&usb4->dev);
0290 pm_runtime_use_autosuspend(&usb4->dev);
0291
0292 return usb4;
0293 }
0294
0295
0296
0297
0298
0299
0300
0301
0302 void usb4_port_device_remove(struct usb4_port *usb4)
0303 {
0304 if (dev_fwnode(&usb4->dev))
0305 component_del(&usb4->dev, &connector_ops);
0306 device_unregister(&usb4->dev);
0307 }
0308
0309
0310
0311
0312
0313
0314
0315 int usb4_port_device_resume(struct usb4_port *usb4)
0316 {
0317 return usb4->offline ? usb4_port_offline(usb4) : 0;
0318 }