Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 2 | /* |
Mika Westerberg | 15c6784 | 2018-10-01 12:31:22 +0300 | [diff] [blame] | 3 | * Thunderbolt driver - capabilities lookup |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
Mika Westerberg | 15c6784 | 2018-10-01 12:31:22 +0300 | [diff] [blame] | 6 | * Copyright (C) 2018, Intel Corporation |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/errno.h> |
| 11 | |
| 12 | #include "tb.h" |
| 13 | |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 14 | #define CAP_OFFSET_MAX 0xff |
| 15 | #define VSE_CAP_OFFSET_MAX 0xffff |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 16 | |
| 17 | struct tb_cap_any { |
| 18 | union { |
| 19 | struct tb_cap_basic basic; |
| 20 | struct tb_cap_extended_short extended_short; |
| 21 | struct tb_cap_extended_long extended_long; |
| 22 | }; |
| 23 | } __packed; |
| 24 | |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 25 | /** |
| 26 | * tb_port_find_cap() - Find port capability |
| 27 | * @port: Port to find the capability for |
| 28 | * @cap: Capability to look |
| 29 | * |
| 30 | * Returns offset to start of capability or %-ENOENT if no such |
| 31 | * capability was found. Negative errno is returned if there was an |
| 32 | * error. |
| 33 | */ |
| 34 | int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap) |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 35 | { |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 36 | u32 offset; |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 37 | |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 38 | /* |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 39 | * DP out adapters claim to implement TMU capability but in |
| 40 | * reality they do not so we hard code the adapter specific |
| 41 | * capability offset here. |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 42 | */ |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 43 | if (port->config.type == TB_TYPE_DP_HDMI_OUT) |
| 44 | offset = 0x39; |
| 45 | else |
| 46 | offset = 0x1; |
| 47 | |
| 48 | do { |
| 49 | struct tb_cap_any header; |
| 50 | int ret; |
| 51 | |
| 52 | ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1); |
| 53 | if (ret) |
| 54 | return ret; |
| 55 | |
| 56 | if (header.basic.cap == cap) |
| 57 | return offset; |
| 58 | |
| 59 | offset = header.basic.next; |
| 60 | } while (offset); |
| 61 | |
| 62 | return -ENOENT; |
| 63 | } |
| 64 | |
| 65 | static int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap) |
| 66 | { |
| 67 | int offset = sw->config.first_cap_offset; |
| 68 | |
| 69 | while (offset > 0 && offset < CAP_OFFSET_MAX) { |
| 70 | struct tb_cap_any header; |
| 71 | int ret; |
| 72 | |
| 73 | ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1); |
| 74 | if (ret) |
| 75 | return ret; |
| 76 | |
| 77 | if (header.basic.cap == cap) |
| 78 | return offset; |
| 79 | |
| 80 | offset = header.basic.next; |
| 81 | } |
| 82 | |
| 83 | return -ENOENT; |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 87 | * tb_switch_find_vse_cap() - Find switch vendor specific capability |
| 88 | * @sw: Switch to find the capability for |
| 89 | * @vsec: Vendor specific capability to look |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 90 | * |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 91 | * Functions enumerates vendor specific capabilities (VSEC) of a switch |
| 92 | * and returns offset when capability matching @vsec is found. If no |
| 93 | * such capability is found returns %-ENOENT. In case of error returns |
| 94 | * negative errno. |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 95 | */ |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 96 | int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec) |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 97 | { |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 98 | struct tb_cap_any header; |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 99 | int offset; |
| 100 | |
| 101 | offset = tb_switch_find_cap(sw, TB_SWITCH_CAP_VSE); |
| 102 | if (offset < 0) |
| 103 | return offset; |
| 104 | |
| 105 | while (offset > 0 && offset < VSE_CAP_OFFSET_MAX) { |
| 106 | int ret; |
| 107 | |
| 108 | ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
| 112 | /* |
| 113 | * Extended vendor specific capabilities come in two |
| 114 | * flavors: short and long. The latter is used when |
| 115 | * offset is over 0xff. |
| 116 | */ |
| 117 | if (offset >= CAP_OFFSET_MAX) { |
| 118 | if (header.extended_long.vsec_id == vsec) |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 119 | return offset; |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 120 | offset = header.extended_long.next; |
| 121 | } else { |
| 122 | if (header.extended_short.vsec_id == vsec) |
| 123 | return offset; |
| 124 | if (!header.extended_short.length) |
| 125 | return -ENOENT; |
| 126 | offset = header.extended_short.next; |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 127 | } |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 128 | } |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 129 | |
| 130 | return -ENOENT; |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 131 | } |