#include <linux/usb/pd_vdo.h>
#include "bus.h"
#include "class.h"
#include "mux.h"
#include "retimer.h"
static inline int
typec_altmode_set_retimer(struct altmode *alt, unsigned long conf, void *data)
{
struct typec_retimer_state state;
if (!alt->retimer)
return 0;
state.alt = &alt->adev;
state.mode = conf;
state.data = data;
return typec_retimer_set(alt->retimer, &state);
}
static inline int
typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
{
struct typec_mux_state state;
if (!alt->mux)
return 0;
state.alt = &alt->adev;
state.mode = conf;
state.data = data;
return typec_mux_set(alt->mux, &state);
}
static inline int
typec_altmode_set_switches(struct altmode *alt, unsigned long conf, void *data)
{
int ret;
ret = typec_altmode_set_retimer(alt, conf, data);
if (ret)
return ret;
return typec_altmode_set_mux(alt, conf, data);
}
static int typec_altmode_set_state(struct typec_altmode *adev,
unsigned long conf, void *data)
{
bool is_port = is_typec_port(adev->dev.parent);
struct altmode *port_altmode;
port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;
return typec_altmode_set_switches(port_altmode, conf, data);
}
int typec_altmode_notify(struct typec_altmode *adev,
unsigned long conf, void *data)
{
bool is_port;
struct altmode *altmode;
struct altmode *partner;
int ret;
if (!adev)
return 0;
altmode = to_altmode(adev);
if (!altmode->partner)
return -ENODEV;
is_port = is_typec_port(adev->dev.parent);
partner = altmode->partner;
ret = typec_altmode_set_switches(is_port ? altmode : partner, conf, data);
if (ret)
return ret;
if (partner->adev.ops && partner->adev.ops->notify)
return partner->adev.ops->notify(&partner->adev, conf, data);
return 0;
}
EXPORT_SYMBOL_GPL(typec_altmode_notify);
int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)
{
struct altmode *partner = to_altmode(adev)->partner;
struct typec_altmode *pdev = &partner->adev;
int ret;
if (!adev || adev->active)
return 0;
if (!pdev->ops || !pdev->ops->enter)
return -EOPNOTSUPP;
if (is_typec_port(pdev->dev.parent) && !pdev->active)
return -EPERM;
ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL);
if (ret)
return ret;
return pdev->ops->enter(pdev, vdo);
}
EXPORT_SYMBOL_GPL(typec_altmode_enter);
int typec_altmode_exit(struct typec_altmode *adev)
{
struct altmode *partner = to_altmode(adev)->partner;
struct typec_altmode *pdev = &partner->adev;
int ret;
if (!adev || !adev->active)
return 0;
if (!pdev->ops || !pdev->ops->exit)
return -EOPNOTSUPP;
ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL);
if (ret)
return ret;
return pdev->ops->exit(pdev);
}
EXPORT_SYMBOL_GPL(typec_altmode_exit);
int typec_altmode_attention(struct typec_altmode *adev, u32 vdo)
{
struct altmode *partner = to_altmode(adev)->partner;
struct typec_altmode *pdev;
if (!partner)
return -ENODEV;
pdev = &partner->adev;
if (pdev->ops && pdev->ops->attention)
pdev->ops->attention(pdev, vdo);
return 0;
}
EXPORT_SYMBOL_GPL(typec_altmode_attention);
int typec_altmode_vdm(struct typec_altmode *adev,
const u32 header, const u32 *vdo, int count)
{
struct typec_altmode *pdev;
struct altmode *altmode;
if (!adev)
return 0;
altmode = to_altmode(adev);
if (!altmode->partner)
return -ENODEV;
pdev = &altmode->partner->adev;
if (!pdev->ops || !pdev->ops->vdm)
return -EOPNOTSUPP;
return pdev->ops->vdm(pdev, header, vdo, count);
}
EXPORT_SYMBOL_GPL(typec_altmode_vdm);
const struct typec_altmode *
typec_altmode_get_partner(struct typec_altmode *adev)
{
if (!adev || !to_altmode(adev)->partner)
return NULL;
return &to_altmode(adev)->partner->adev;
}
EXPORT_SYMBOL_GPL(typec_altmode_get_partner);
struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
enum typec_plug_index index)
{
struct altmode *port = to_altmode(adev)->partner;
if (port->plug[index]) {
get_device(&port->plug[index]->adev.dev);
return &port->plug[index]->adev;
}
return NULL;
}
EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
void typec_altmode_put_plug(struct typec_altmode *plug)
{
if (plug)
put_device(&plug->dev);
}
EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
struct module *module)
{
if (!drv->probe)
return -EINVAL;
drv->driver.owner = module;
drv->driver.bus = &typec_bus;
return driver_register(&drv->driver);
}
EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
{
driver_unregister(&drv->driver);
}
EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
size_t n, u16 svid, u8 mode)
{
int i;
for (i = 0; i < n; i++) {
if (!altmodes[i])
break;
if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
return altmodes[i];
}
return NULL;
}
EXPORT_SYMBOL_GPL(typec_match_altmode);
static ssize_t
description_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct typec_altmode *alt = to_typec_altmode(dev);
return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
}
static DEVICE_ATTR_RO(description);
static struct attribute *typec_attrs[] = {
&dev_attr_description.attr,
NULL
};
ATTRIBUTE_GROUPS(typec);
static int typec_match(struct device *dev, struct device_driver *driver)
{
struct typec_altmode_driver *drv = to_altmode_driver(driver);
struct typec_altmode *altmode = to_typec_altmode(dev);
const struct typec_device_id *id;
for (id = drv->id_table; id->svid; id++)
if (id->svid == altmode->svid &&
(id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
return 1;
return 0;
}
static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env)
{
const struct typec_altmode *altmode = to_typec_altmode(dev);
if (add_uevent_var(env, "SVID=%04X", altmode->svid))
return -ENOMEM;
if (add_uevent_var(env, "MODE=%u", altmode->mode))
return -ENOMEM;
return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
altmode->svid, altmode->mode);
}
static int typec_altmode_create_links(struct altmode *alt)
{
struct device *port_dev = &alt->partner->adev.dev;
struct device *dev = &alt->adev.dev;
int err;
err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
if (err)
return err;
err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
if (err)
sysfs_remove_link(&dev->kobj, "port");
return err;
}
static void typec_altmode_remove_links(struct altmode *alt)
{
sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
sysfs_remove_link(&alt->adev.dev.kobj, "port");
}
static int typec_probe(struct device *dev)
{
struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
struct typec_altmode *adev = to_typec_altmode(dev);
struct altmode *altmode = to_altmode(adev);
int ret;
if (!altmode->partner)
return -ENODEV;
ret = typec_altmode_create_links(altmode);
if (ret) {
dev_warn(dev, "failed to create symlinks\n");
return ret;
}
ret = drv->probe(adev);
if (ret)
typec_altmode_remove_links(altmode);
return ret;
}
static void typec_remove(struct device *dev)
{
struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
struct typec_altmode *adev = to_typec_altmode(dev);
struct altmode *altmode = to_altmode(adev);
typec_altmode_remove_links(altmode);
if (drv->remove)
drv->remove(to_typec_altmode(dev));
if (adev->active) {
WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL));
typec_altmode_update_active(adev, false);
}
adev->desc = NULL;
adev->ops = NULL;
}
const struct bus_type typec_bus = {
.name = "typec",
.dev_groups = typec_groups,
.match = typec_match,
.uevent = typec_uevent,
.probe = typec_probe,
.remove = typec_remove,
}