1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Bus for USB Type-C Alternate Modes 4 * 5 * Copyright (C) 2018 Intel Corporation 6 * Author: Heikki Krogerus <[email protected]> 7 */ 8 9 #include <linux/usb/pd_vdo.h> 10 11 #include "bus.h" 12 #include "class.h" 13 #include "mux.h" 14 15 static inline int 16 typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data) 17 { 18 struct typec_mux_state state; 19 20 if (!alt->mux) 21 return 0; 22 23 state.alt = &alt->adev; 24 state.mode = conf; 25 state.data = data; 26 27 return typec_mux_set(alt->mux, &state); 28 } 29 30 /* Wrapper to set various Type-C port switches together. */ 31 static inline int 32 typec_altmode_set_switches(struct altmode *alt, unsigned long conf, void *data) 33 { 34 return typec_altmode_set_mux(alt, conf, data); 35 } 36 37 static int typec_altmode_set_state(struct typec_altmode *adev, 38 unsigned long conf, void *data) 39 { 40 bool is_port = is_typec_port(adev->dev.parent); 41 struct altmode *port_altmode; 42 43 port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner; 44 45 return typec_altmode_set_switches(port_altmode, conf, data); 46 } 47 48 /* -------------------------------------------------------------------------- */ 49 /* Common API */ 50 51 /** 52 * typec_altmode_notify - Communication between the OS and alternate mode driver 53 * @adev: Handle to the alternate mode 54 * @conf: Alternate mode specific configuration value 55 * @data: Alternate mode specific data 56 * 57 * The primary purpose for this function is to allow the alternate mode drivers 58 * to tell which pin configuration has been negotiated with the partner. That 59 * information will then be used for example to configure the muxes. 60 * Communication to the other direction is also possible, and low level device 61 * drivers can also send notifications to the alternate mode drivers. The actual 62 * communication will be specific for every SVID. 63 */ 64 int typec_altmode_notify(struct typec_altmode *adev, 65 unsigned long conf, void *data) 66 { 67 bool is_port; 68 struct altmode *altmode; 69 struct altmode *partner; 70 int ret; 71 72 if (!adev) 73 return 0; 74 75 altmode = to_altmode(adev); 76 77 if (!altmode->partner) 78 return -ENODEV; 79 80 is_port = is_typec_port(adev->dev.parent); 81 partner = altmode->partner; 82 83 ret = typec_altmode_set_switches(is_port ? altmode : partner, conf, data); 84 if (ret) 85 return ret; 86 87 if (partner->adev.ops && partner->adev.ops->notify) 88 return partner->adev.ops->notify(&partner->adev, conf, data); 89 90 return 0; 91 } 92 EXPORT_SYMBOL_GPL(typec_altmode_notify); 93 94 /** 95 * typec_altmode_enter - Enter Mode 96 * @adev: The alternate mode 97 * @vdo: VDO for the Enter Mode command 98 * 99 * The alternate mode drivers use this function to enter mode. The port drivers 100 * use this to inform the alternate mode drivers that the partner has initiated 101 * Enter Mode command. If the alternate mode does not require VDO, @vdo must be 102 * NULL. 103 */ 104 int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo) 105 { 106 struct altmode *partner = to_altmode(adev)->partner; 107 struct typec_altmode *pdev = &partner->adev; 108 int ret; 109 110 if (!adev || adev->active) 111 return 0; 112 113 if (!pdev->ops || !pdev->ops->enter) 114 return -EOPNOTSUPP; 115 116 if (is_typec_port(pdev->dev.parent) && !pdev->active) 117 return -EPERM; 118 119 /* Moving to USB Safe State */ 120 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL); 121 if (ret) 122 return ret; 123 124 /* Enter Mode */ 125 return pdev->ops->enter(pdev, vdo); 126 } 127 EXPORT_SYMBOL_GPL(typec_altmode_enter); 128 129 /** 130 * typec_altmode_exit - Exit Mode 131 * @adev: The alternate mode 132 * 133 * The partner of @adev has initiated Exit Mode command. 134 */ 135 int typec_altmode_exit(struct typec_altmode *adev) 136 { 137 struct altmode *partner = to_altmode(adev)->partner; 138 struct typec_altmode *pdev = &partner->adev; 139 int ret; 140 141 if (!adev || !adev->active) 142 return 0; 143 144 if (!pdev->ops || !pdev->ops->exit) 145 return -EOPNOTSUPP; 146 147 /* Moving to USB Safe State */ 148 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL); 149 if (ret) 150 return ret; 151 152 /* Exit Mode command */ 153 return pdev->ops->exit(pdev); 154 } 155 EXPORT_SYMBOL_GPL(typec_altmode_exit); 156 157 /** 158 * typec_altmode_attention - Attention command 159 * @adev: The alternate mode 160 * @vdo: VDO for the Attention command 161 * 162 * Notifies the partner of @adev about Attention command. 163 */ 164 void typec_altmode_attention(struct typec_altmode *adev, u32 vdo) 165 { 166 struct typec_altmode *pdev = &to_altmode(adev)->partner->adev; 167 168 if (pdev->ops && pdev->ops->attention) 169 pdev->ops->attention(pdev, vdo); 170 } 171 EXPORT_SYMBOL_GPL(typec_altmode_attention); 172 173 /** 174 * typec_altmode_vdm - Send Vendor Defined Messages (VDM) to the partner 175 * @adev: Alternate mode handle 176 * @header: VDM Header 177 * @vdo: Array of Vendor Defined Data Objects 178 * @count: Number of Data Objects 179 * 180 * The alternate mode drivers use this function for SVID specific communication 181 * with the partner. The port drivers use it to deliver the Structured VDMs 182 * received from the partners to the alternate mode drivers. 183 */ 184 int typec_altmode_vdm(struct typec_altmode *adev, 185 const u32 header, const u32 *vdo, int count) 186 { 187 struct typec_altmode *pdev; 188 struct altmode *altmode; 189 190 if (!adev) 191 return 0; 192 193 altmode = to_altmode(adev); 194 195 if (!altmode->partner) 196 return -ENODEV; 197 198 pdev = &altmode->partner->adev; 199 200 if (!pdev->ops || !pdev->ops->vdm) 201 return -EOPNOTSUPP; 202 203 return pdev->ops->vdm(pdev, header, vdo, count); 204 } 205 EXPORT_SYMBOL_GPL(typec_altmode_vdm); 206 207 const struct typec_altmode * 208 typec_altmode_get_partner(struct typec_altmode *adev) 209 { 210 if (!adev || !to_altmode(adev)->partner) 211 return NULL; 212 213 return &to_altmode(adev)->partner->adev; 214 } 215 EXPORT_SYMBOL_GPL(typec_altmode_get_partner); 216 217 /* -------------------------------------------------------------------------- */ 218 /* API for the alternate mode drivers */ 219 220 /** 221 * typec_altmode_get_plug - Find cable plug alternate mode 222 * @adev: Handle to partner alternate mode 223 * @index: Cable plug index 224 * 225 * Increment reference count for cable plug alternate mode device. Returns 226 * handle to the cable plug alternate mode, or NULL if none is found. 227 */ 228 struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev, 229 enum typec_plug_index index) 230 { 231 struct altmode *port = to_altmode(adev)->partner; 232 233 if (port->plug[index]) { 234 get_device(&port->plug[index]->adev.dev); 235 return &port->plug[index]->adev; 236 } 237 238 return NULL; 239 } 240 EXPORT_SYMBOL_GPL(typec_altmode_get_plug); 241 242 /** 243 * typec_altmode_put_plug - Decrement cable plug alternate mode reference count 244 * @plug: Handle to the cable plug alternate mode 245 */ 246 void typec_altmode_put_plug(struct typec_altmode *plug) 247 { 248 if (plug) 249 put_device(&plug->dev); 250 } 251 EXPORT_SYMBOL_GPL(typec_altmode_put_plug); 252 253 int __typec_altmode_register_driver(struct typec_altmode_driver *drv, 254 struct module *module) 255 { 256 if (!drv->probe) 257 return -EINVAL; 258 259 drv->driver.owner = module; 260 drv->driver.bus = &typec_bus; 261 262 return driver_register(&drv->driver); 263 } 264 EXPORT_SYMBOL_GPL(__typec_altmode_register_driver); 265 266 void typec_altmode_unregister_driver(struct typec_altmode_driver *drv) 267 { 268 driver_unregister(&drv->driver); 269 } 270 EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver); 271 272 /* -------------------------------------------------------------------------- */ 273 /* API for the port drivers */ 274 275 /** 276 * typec_match_altmode - Match SVID and mode to an array of alternate modes 277 * @altmodes: Array of alternate modes 278 * @n: Number of elements in the array, or -1 for NULL terminated arrays 279 * @svid: Standard or Vendor ID to match with 280 * @mode: Mode to match with 281 * 282 * Return pointer to an alternate mode with SVID matching @svid, or NULL when no 283 * match is found. 284 */ 285 struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes, 286 size_t n, u16 svid, u8 mode) 287 { 288 int i; 289 290 for (i = 0; i < n; i++) { 291 if (!altmodes[i]) 292 break; 293 if (altmodes[i]->svid == svid && altmodes[i]->mode == mode) 294 return altmodes[i]; 295 } 296 297 return NULL; 298 } 299 EXPORT_SYMBOL_GPL(typec_match_altmode); 300 301 /* -------------------------------------------------------------------------- */ 302 303 static ssize_t 304 description_show(struct device *dev, struct device_attribute *attr, char *buf) 305 { 306 struct typec_altmode *alt = to_typec_altmode(dev); 307 308 return sprintf(buf, "%s\n", alt->desc ? alt->desc : ""); 309 } 310 static DEVICE_ATTR_RO(description); 311 312 static struct attribute *typec_attrs[] = { 313 &dev_attr_description.attr, 314 NULL 315 }; 316 ATTRIBUTE_GROUPS(typec); 317 318 static int typec_match(struct device *dev, struct device_driver *driver) 319 { 320 struct typec_altmode_driver *drv = to_altmode_driver(driver); 321 struct typec_altmode *altmode = to_typec_altmode(dev); 322 const struct typec_device_id *id; 323 324 for (id = drv->id_table; id->svid; id++) 325 if (id->svid == altmode->svid && 326 (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode)) 327 return 1; 328 return 0; 329 } 330 331 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env) 332 { 333 struct typec_altmode *altmode = to_typec_altmode(dev); 334 335 if (add_uevent_var(env, "SVID=%04X", altmode->svid)) 336 return -ENOMEM; 337 338 if (add_uevent_var(env, "MODE=%u", altmode->mode)) 339 return -ENOMEM; 340 341 return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X", 342 altmode->svid, altmode->mode); 343 } 344 345 static int typec_altmode_create_links(struct altmode *alt) 346 { 347 struct device *port_dev = &alt->partner->adev.dev; 348 struct device *dev = &alt->adev.dev; 349 int err; 350 351 err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port"); 352 if (err) 353 return err; 354 355 err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner"); 356 if (err) 357 sysfs_remove_link(&dev->kobj, "port"); 358 359 return err; 360 } 361 362 static void typec_altmode_remove_links(struct altmode *alt) 363 { 364 sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner"); 365 sysfs_remove_link(&alt->adev.dev.kobj, "port"); 366 } 367 368 static int typec_probe(struct device *dev) 369 { 370 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver); 371 struct typec_altmode *adev = to_typec_altmode(dev); 372 struct altmode *altmode = to_altmode(adev); 373 int ret; 374 375 /* Fail if the port does not support the alternate mode */ 376 if (!altmode->partner) 377 return -ENODEV; 378 379 ret = typec_altmode_create_links(altmode); 380 if (ret) { 381 dev_warn(dev, "failed to create symlinks\n"); 382 return ret; 383 } 384 385 ret = drv->probe(adev); 386 if (ret) 387 typec_altmode_remove_links(altmode); 388 389 return ret; 390 } 391 392 static void typec_remove(struct device *dev) 393 { 394 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver); 395 struct typec_altmode *adev = to_typec_altmode(dev); 396 struct altmode *altmode = to_altmode(adev); 397 398 typec_altmode_remove_links(altmode); 399 400 if (drv->remove) 401 drv->remove(to_typec_altmode(dev)); 402 403 if (adev->active) { 404 WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL)); 405 typec_altmode_update_active(adev, false); 406 } 407 408 adev->desc = NULL; 409 adev->ops = NULL; 410 } 411 412 struct bus_type typec_bus = { 413 .name = "typec", 414 .dev_groups = typec_groups, 415 .match = typec_match, 416 .uevent = typec_uevent, 417 .probe = typec_probe, 418 .remove = typec_remove, 419 }; 420