// SPDX-License-Identifier: GPL-2.0
/*
* USB Type-C Connector Class
*
* Copyright (C) 2017, Intel Corporation
* Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
*/
#include <linux/device.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include "bus.h"
struct typec_plug {
struct device dev;
enum typec_plug_index index;
struct ida mode_ids;
};
struct typec_cable {
struct device dev;
enum typec_plug_type type;
struct usb_pd_identity *identity;
unsigned int active:1;
};
struct typec_partner {
struct device dev;
unsigned int usb_pd:1;
struct usb_pd_identity *identity;
enum typec_accessory accessory;
struct ida mode_ids;
};
struct typec_port {
unsigned int id;
struct device dev;
struct ida mode_ids;
int prefer_role;
enum typec_data_role data_role;
enum typec_role pwr_role;
enum typec_role vconn_role;
enum typec_pwr_opmode pwr_opmode;
enum typec_port_type port_type;
struct mutex port_type_lock;
enum typec_orientation orientation;
struct typec_switch *sw;
struct typec_mux *mux;
const struct typec_capability *cap;
};
#define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
#define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
#define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
#define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
static const struct device_type typec_partner_dev_type;
static const struct device_type typec_cable_dev_type;
static const struct device_type typec_plug_dev_type;
#define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
#define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
#define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
static DEFINE_IDA(typec_index_ida);
static struct class *typec_class;
/* ------------------------------------------------------------------------- */
/* Common attributes */
static const char * const typec_accessory_modes[] = {
[TYPEC_ACCESSORY_NONE] = "none",
[TYPEC_ACCESSORY_AUDIO] = "analog_audio",
[TYPEC_ACCESSORY_DEBUG] = "debug",
};
static struct usb_pd_identity *get_pd_identity(struct device *dev)
{
if (is_typec_partner(dev)) {
struct typec_partner *partner = to_typec_partner(dev);
return partner->identity;
} else if (is_typec_cable(dev)) {
struct typec_cable *cable = to_typec_cable(dev);
return cable->identity;
}
return NULL;
}
static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct usb_pd_identity *id = get_pd_identity(dev);
return sprintf(buf, "0x%08x\n", id->id_header);
}
static DEVICE_ATTR_RO(id_header);
static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct usb_pd_identity *id = g
|