forked from linux-msm/cdba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftdi-gpio.c
253 lines (207 loc) · 6.86 KB
/
ftdi-gpio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright (c) 2023, Linaro Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "cdba-server.h"
#include "ftdi-gpio.h"
#include <libftdi1/ftdi.h>
enum {
GPIO_POWER = 0, // Power input enable
GPIO_FASTBOOT_KEY, // Usually volume key
GPIO_POWER_KEY, // Key to power the device
GPIO_USB_DISCONNECT, // Simulate main USB connection
GPIO_COUNT
};
enum {
GPIO_ACTIVE_HIGH = 0,
GPIO_ACTIVE_LOW,
};
struct ftdi_gpio {
struct ftdi_context *gpio;
char *ftdi_device;
unsigned int ftdi_interface;
unsigned int gpio_present[GPIO_COUNT];
unsigned int gpio_offset[GPIO_COUNT];
unsigned int gpio_polarity[GPIO_COUNT];
unsigned char gpio_lines;
};
static int ftdi_gpio_device_power(struct ftdi_gpio *ftdi_gpio, bool on);
static void ftdi_gpio_device_usb(struct ftdi_gpio *ftdi_gpio, bool on);
/*
* fdio_gpio parameter: <libftdi description>;[<interface>[;<gpios>...]]
* - libftdi description: "s:0xVEND:0xPROD:SERIAL"
* - interface: A, B, C or D (default A)
* - gpios: type,id,polarity
* - type: POWER, FASTBOOT_KEY, POWER_KEY or USB_DISCONNECT
* - id: 0, 1, 2, 3, 4, 5, 6 or 7
* - polarity: ACTIVE_HIGH or ACTIVE_LOW
*
* Example: s:0xVEND:0xPROD:SERIAL;D;POWER,0,ACTIVE_LOW;FASTBOOT_KEY,1,ACTIVE_HIGH;POWER_KEY,2,ACTIVE_HIGH;USB_DISCONNECT,3,ACTIVE_LOW
*/
static void ftdi_gpio_parse_config(struct ftdi_gpio *ftdi_gpio, char *control_dev)
{
char *c, *interface;
size_t device_len;
// First libftdi description
c = strchr(control_dev, ';');
if (!c)
device_len = strlen(control_dev);
else
device_len = c - control_dev;
ftdi_gpio->ftdi_device = strndup(control_dev, device_len);
if (!c)
return;
// Interface
interface = c + 1;
if (*interface != 'A' &&
*interface != 'A' &&
*interface != 'C' &&
*interface != 'D') {
errx(1, "Invalid interface '%c'", *interface);
}
ftdi_gpio->ftdi_interface = *interface - 'A';
c = strchr(interface, ';');
// GPIOs
while(c) {
char *name, *off, *pol;
unsigned gpio_type;
unsigned gpio_offset;
unsigned gpio_polarity;
name = c + 1;
off = strchr(name, ',');
if (!off)
errx(1, "GPIOs config invalid");
off += 1;
pol = strchr(off, ',');
if (!pol)
errx(1, "GPIOs config invalid");
pol += 1;
c = strchr(pol, ';');
if (strncmp("POWER", name, off - name - 1) == 0)
gpio_type = GPIO_POWER;
else if (strncmp("FASTBOOT_KEY", name, off - name - 1) == 0)
gpio_type = GPIO_FASTBOOT_KEY;
else if (strncmp("POWER_KEY", name, off - name - 1) == 0)
gpio_type = GPIO_POWER_KEY;
else if (strncmp("USB_DISCONNECT", name, off - name - 1) == 0)
gpio_type = GPIO_USB_DISCONNECT;
else
errx(1, "GPIOs type invalid: '%s'", name);
gpio_offset = strtoul(off, NULL, 0);
if (gpio_offset > 7)
errx(1, "GPIOs offset invalid: '%d'", gpio_offset);
if (strncmp("ACTIVE_HIGH", pol, c - pol - 1) == 0)
gpio_polarity = GPIO_ACTIVE_HIGH;
else if (strncmp("ACTIVE_LOW", pol, c - pol - 1) == 0)
gpio_polarity = GPIO_ACTIVE_LOW;
else
errx(1, "GPIOs polarity invalid: '%s'", pol);
ftdi_gpio->gpio_present[gpio_type] = 1;
ftdi_gpio->gpio_offset[gpio_type] = gpio_offset;
ftdi_gpio->gpio_polarity[gpio_type] = gpio_polarity;
}
}
void *ftdi_gpio_open(struct device *dev)
{
struct ftdi_gpio *ftdi_gpio;
int ret;
ftdi_gpio = calloc(1, sizeof(*ftdi_gpio));
ftdi_gpio_parse_config(ftdi_gpio, dev->control_dev);
if ((ftdi_gpio->gpio = ftdi_new()) == 0)
errx(1, "failed to allocate ftdi gpio struct");
ftdi_set_interface(ftdi_gpio->gpio, INTERFACE_A + ftdi_gpio->ftdi_interface);
ret = ftdi_usb_open_string(ftdi_gpio->gpio, ftdi_gpio->ftdi_device);
if (ret < 0)
errx(1, "failed to open ftdi gpio device '%s' (%d)", ftdi_gpio->ftdi_device, ret);
ftdi_set_bitmode(ftdi_gpio->gpio, 0xFF, BITMODE_BITBANG);
if (ftdi_gpio->gpio_present[GPIO_POWER_KEY])
dev->has_power_key = true;
ftdi_gpio_device_power(ftdi_gpio, 0);
if (dev->usb_always_on)
ftdi_gpio_device_usb(ftdi_gpio, 1);
else
ftdi_gpio_device_usb(ftdi_gpio, 0);
sleep(0.5);
return ftdi_gpio;
}
static int ftdi_gpio_toggle_io(struct ftdi_gpio *ftdi_gpio, unsigned int gpio, bool on)
{
unsigned int bit;
if (!ftdi_gpio->gpio_present[gpio])
return -EINVAL;
bit = ftdi_gpio->gpio_offset[gpio];
if (ftdi_gpio->gpio_polarity[gpio])
on = !on;
if (on)
ftdi_gpio->gpio_lines |= (1 << bit);
else
ftdi_gpio->gpio_lines &= ~(1 << bit);
return ftdi_write_data(ftdi_gpio->gpio, &ftdi_gpio->gpio_lines, 1);
}
static int ftdi_gpio_device_power(struct ftdi_gpio *ftdi_gpio, bool on)
{
return ftdi_gpio_toggle_io(ftdi_gpio, GPIO_POWER, on);
}
static void ftdi_gpio_device_usb(struct ftdi_gpio *ftdi_gpio, bool on)
{
ftdi_gpio_toggle_io(ftdi_gpio, GPIO_USB_DISCONNECT, on);
}
int ftdi_gpio_power(struct device *dev, bool on)
{
struct ftdi_gpio *ftdi_gpio = dev->cdb;
return ftdi_gpio_device_power(ftdi_gpio, on);
}
void ftdi_gpio_usb(struct device *dev, bool on)
{
struct ftdi_gpio *ftdi_gpio = dev->cdb;
ftdi_gpio_device_usb(ftdi_gpio, on);
}
void ftdi_gpio_key(struct device *dev, int key, bool asserted)
{
struct ftdi_gpio *ftdi_gpio = dev->cdb;
switch (key) {
case DEVICE_KEY_FASTBOOT:
ftdi_gpio_toggle_io(ftdi_gpio, GPIO_FASTBOOT_KEY, asserted);
break;
case DEVICE_KEY_POWER:
ftdi_gpio_toggle_io(ftdi_gpio, GPIO_POWER_KEY, asserted);
break;
}
}