blob: b9c98a14544b470927f61e81df04417f08eafd75 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Laurie3e7197a2018-05-07 14:18:13 -07002
3#include <stdint.h>
4#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
6#include <acpi/acpi_pld.h>
Duncan Laurie3e7197a2018-05-07 14:18:13 -07007
Duncan Lauriee1eca1d2018-12-01 17:14:35 -08008int acpi_pld_fill_usb(struct acpi_pld *pld, enum acpi_upc_type type,
9 struct acpi_pld_group *group)
Duncan Laurie3e7197a2018-05-07 14:18:13 -070010{
11 if (!pld)
12 return -1;
13
14 memset(pld, 0, sizeof(struct acpi_pld));
15
16 /* Set defaults */
17 pld->ignore_color = 1;
18 pld->panel = PLD_PANEL_UNKNOWN;
19 pld->vertical_position = PLD_VERTICAL_POSITION_CENTER;
20 pld->horizontal_position = PLD_HORIZONTAL_POSITION_CENTER;
21 pld->rotation = PLD_ROTATE_0;
22 pld->visible = 1;
Duncan Lauriee1eca1d2018-12-01 17:14:35 -080023 pld->group.token = group->token;
24 pld->group.position = group->position;
Duncan Laurie3e7197a2018-05-07 14:18:13 -070025
26 /* Set the shape based on port type */
27 switch (type) {
28 case UPC_TYPE_A:
29 case UPC_TYPE_USB3_A:
30 case UPC_TYPE_USB3_POWER_B:
31 pld->shape = PLD_SHAPE_HORIZONTAL_RECTANGLE;
32 break;
33 case UPC_TYPE_MINI_AB:
34 case UPC_TYPE_USB3_B:
35 pld->shape = PLD_SHAPE_CHAMFERED;
36 break;
37 case UPC_TYPE_USB3_MICRO_B:
38 case UPC_TYPE_USB3_MICRO_AB:
39 pld->shape = PLD_SHAPE_HORIZONTAL_TRAPEZOID;
40 break;
41 case UPC_TYPE_C_USB2_ONLY:
42 case UPC_TYPE_C_USB2_SS_SWITCH:
43 case UPC_TYPE_C_USB2_SS:
44 pld->shape = PLD_SHAPE_OVAL;
45 break;
46 case UPC_TYPE_INTERNAL:
47 default:
48 pld->shape = PLD_SHAPE_UNKNOWN;
49 pld->visible = 0;
50 break;
51 }
52
53 return 0;
54}
55
56int acpi_pld_to_buffer(const struct acpi_pld *pld, uint8_t *buf, int buf_len)
57{
58 if (!pld || !buf)
59 return -1;
60
61 memset(buf, 0, buf_len);
62
63 /* [0] Revision (=2) */
64 buf[0] = 0x2;
65
66 if (pld->ignore_color) {
67 /* [1] Ignore Color */
68 buf[0] |= 0x80;
69 } else {
70 /* [15:8] Red Color */
71 buf[1] = pld->color_red;
72 /* [23:16] Green Color */
73 buf[2] = pld->color_green;
74 /* [31:24] Blue Color */
75 buf[3] = pld->color_blue;
76 }
77
78 /* [47:32] Width */
79 buf[4] = pld->width & 0xff;
80 buf[5] = pld->width >> 8;
81
82 /* [63:48] Height */
83 buf[6] = pld->height & 0xff;
84 buf[7] = pld->height >> 8;
85
86 /* [64] User Visible */
87 buf[8] |= (pld->visible & 0x1);
88
89 /* [65] Dock */
90 buf[8] |= (pld->dock & 0x1) << 1;
91
92 /* [66] Lid */
93 buf[8] |= (pld->lid & 0x1) << 2;
94
95 /* [69:67] Panel */
96 buf[8] |= (pld->panel & 0x7) << 3;
97
98 /* [71:70] Vertical Position */
99 buf[8] |= (pld->vertical_position & 0x3) << 6;
100
101 /* [73:72] Horizontal Position */
102 buf[9] |= (pld->horizontal_position & 0x3);
103
104 /* [77:74] Shape */
105 buf[9] |= (pld->shape & 0xf) << 2;
106
Duncan Lauriee1eca1d2018-12-01 17:14:35 -0800107 /* [78] Orientation */
108 buf[9] |= (pld->orientation & 0x1) << 6;
Duncan Laurie3e7197a2018-05-07 14:18:13 -0700109
110 /* [86:79] Group Token (incorrectly defined as 1 bit in ACPI 6.2A) */
Duncan Lauriee1eca1d2018-12-01 17:14:35 -0800111 buf[9] |= (pld->group.token & 0x1) << 7;
112 buf[10] |= (pld->group.token >> 0x1) & 0x7f;
Duncan Laurie3e7197a2018-05-07 14:18:13 -0700113
114 /* [94:87] Group Position */
Duncan Lauriee1eca1d2018-12-01 17:14:35 -0800115 buf[10] |= (pld->group.position & 0x1) << 7;
116 buf[11] |= (pld->group.position >> 0x1) & 0x7f;
Duncan Laurie3e7197a2018-05-07 14:18:13 -0700117
118 /* [95] Bay */
119 buf[11] |= (pld->bay & 0x1) << 7;
120
121 /* [96] Ejectable */
122 buf[12] |= (pld->ejectable & 0x1);
123
124 /* [97] Ejectable with OSPM help */
125 buf[12] |= (pld->ejectable_ospm & 0x1) << 1;
126
127 /* [105:98] Cabinet Number */
128 buf[12] |= (pld->cabinet_number & 0x3f) << 2;
129 buf[13] |= (pld->cabinet_number >> 6) & 0x3;
130
131 /* [113:106] Card Cage Number */
132 buf[13] |= (pld->card_cage_number & 0x3f) << 2;
133 buf[14] |= (pld->card_cage_number >> 6) & 0x3;
134
135 /* [114] PLD is a Reference Shape */
136 buf[14] |= (pld->reference_shape & 0x1) << 2;
137
138 /* [118:115] Rotation */
139 buf[14] |= (pld->rotation & 0xf) << 3;
140
141 /* [123:119] Draw Order */
142 buf[14] |= (pld->draw_order & 0x1) << 7;
143 buf[15] |= (pld->draw_order >> 1) & 0xf;
144
145 /* [127:124] Reserved */
146
147 /* Both 16 byte and 20 byte buffers are supported by the spec */
148 if (buf_len == 20) {
149 /* [143:128] Vertical Offset */
150 buf[16] = pld->vertical_offset & 0xff;
151 buf[17] = pld->vertical_offset >> 8;
152
153 /* [159:144] Horizontal Offset */
154 buf[18] = pld->horizontal_offset & 0xff;
155 buf[19] = pld->horizontal_offset >> 8;
156 }
157
158 return 0;
159}