intel_de3815: avoid double free in case of error
[contrib/mraa.git] / src / x86 / intel_de3815.c
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #define _GNU_SOURCE
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 #include "common.h"
33 #include "x86/intel_de3815.h"
34
35 #define PLATFORM_NAME "Intel DE3815"
36 #define MAX_SIZE 64
37 #define SYSFS_CLASS_GPIO "/sys/class/gpio"
38 #define I2CNAME "designware"
39
40 mraa_board_t*
41 mraa_intel_de3815()
42 {
43     mraa_board_t* b = (mraa_board_t*) malloc(sizeof(mraa_board_t));
44     if (b == NULL) {
45         return NULL;
46     }
47
48     b->platform_name = PLATFORM_NAME;
49     b->phy_pin_count = 18;
50     // b->gpio_count = 14;
51     b->aio_count = 0;
52     b->adc_raw = 0;
53     b->adc_supported = 0;
54     b->pwm_default_period = 500;
55     b->pwm_max_period = 2147483;
56     b->pwm_min_period = 1;
57
58     b->pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * MRAA_INTEL_DE3815_PINCOUNT);
59     if (b->pins == NULL) {
60         goto error;
61     }
62
63     strncpy(b->pins[0].name, "1.8v", 8);
64     b->pins[0].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
65
66     strncpy(b->pins[1].name, "GND", 8);
67     b->pins[1].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
68     strncpy(b->pins[2].name, "HDMIcec", 8);
69     b->pins[2].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
70     strncpy(b->pins[3].name, "DMICclk", 8);
71     b->pins[3].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
72     strncpy(b->pins[4].name, "3.3v", 8);
73     b->pins[4].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
74     strncpy(b->pins[5].name, "DMICda", 8);
75     b->pins[5].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
76     strncpy(b->pins[6].name, "Key", 8);
77     b->pins[6].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
78     strncpy(b->pins[7].name, "SMB-A", 8);
79     b->pins[7].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
80     strncpy(b->pins[8].name, "5v", 8);
81     b->pins[8].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
82     strncpy(b->pins[9].name, "SCI", 8);
83     b->pins[9].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
84
85     strncpy(b->pins[10].name, "PWM0", 8);
86     b->pins[10].capabilites = (mraa_pincapabilities_t){ 1, 0, 1, 0, 0, 0, 0, 0 };
87     b->pins[10].pwm.pinmap = 0;
88     b->pins[10].pwm.parent_id = 0;
89     b->pins[10].pwm.mux_total = 0;
90
91     strncpy(b->pins[11].name, "PWM1", 8);
92     b->pins[11].capabilites = (mraa_pincapabilities_t){ 1, 0, 1, 0, 0, 0, 0, 0 };
93     b->pins[11].pwm.pinmap = 0;
94     b->pins[11].pwm.parent_id = 1;
95     b->pins[11].pwm.mux_total = 0;
96
97     strncpy(b->pins[12].name, "I2C0SCL", 8);
98     b->pins[12].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
99     b->pins[12].i2c.pinmap = 1;
100     b->pins[12].i2c.mux_total = 0;
101
102     strncpy(b->pins[13].name, "I2C0SDA", 8);
103     b->pins[13].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
104     b->pins[13].i2c.pinmap = 1;
105     b->pins[13].i2c.mux_total = 0;
106
107     strncpy(b->pins[14].name, "I2C1SCL", 8);
108     b->pins[14].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
109     b->pins[14].i2c.pinmap = 1;
110     b->pins[14].i2c.mux_total = 0;
111
112     strncpy(b->pins[15].name, "I2C1SDA", 8);
113     b->pins[15].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
114     b->pins[15].i2c.pinmap = 1;
115     b->pins[15].i2c.mux_total = 0;
116
117     strncpy(b->pins[16].name, "SMB_CLK", 8);
118     b->pins[16].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
119     strncpy(b->pins[17].name, "SMB_SDA", 8);
120     b->pins[17].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
121
122     // BUS DEFINITIONS
123     int fd;
124     if (mraa_file_exist("/dev/i2c-0")) {
125         b->i2c_bus_count = 2;
126         int i = 0;
127         int suc = 0;
128         for (i = 0; i < 9; i++) {
129             off_t size, err;
130             char path[MAX_SIZE];
131             char value[MAX_SIZE];
132             snprintf(path, MAX_SIZE, "/sys/class/i2c-dev/i2c-%u/name", i);
133             fd = open(path, O_RDONLY);
134             if (fd < 0) {
135                 break;
136             }
137             size = lseek(fd, 0, SEEK_END);
138             if (size < 0) {
139                 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
140                 close(fd);
141                 break;
142             }
143             err = lseek(fd, 0, SEEK_SET);
144             if (err < 0) {
145                 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
146                 close(fd);
147                 break;
148             }
149             if (size >= MAX_SIZE) {
150                 syslog(LOG_NOTICE, "mraa: i2c filename file too big, skipping");
151                 close(fd);
152                 break;
153             }
154             ssize_t r = read(fd, value, size);
155             if (r > 0) {
156                 if (strcasestr(value, I2CNAME) != NULL) {
157                     suc = 1;
158                     b->i2c_bus_count = 2;
159                     b->def_i2c_bus = 0;
160                     b->i2c_bus[0].bus_id = i;
161                     b->i2c_bus[0].sda = 12;
162                     b->i2c_bus[0].scl = 13;
163
164                     b->i2c_bus[1].bus_id = i + 1;
165                     b->i2c_bus[1].sda = 14;
166                     b->i2c_bus[1].scl = 15;
167                     close(fd);
168                     break;
169                 }
170             } else {
171                 syslog(LOG_ERR, "mraa: sysfs i2cdev failed");
172                 close(fd);
173                 break;
174             }
175             close(fd);
176         }
177
178         if (!suc) {
179             syslog(LOG_WARNING, "mraa: no i2c-dev detected, load i2c-dev");
180             b->i2c_bus_count = 0;
181             b->def_i2c_bus = 0;
182         }
183     } else {
184         syslog(LOG_WARNING, "mraa: no i2c-dev detected, load i2c-dev");
185         b->i2c_bus_count = 0;
186         b->def_i2c_bus = 0;
187     }
188
189     b->spi_bus_count = 1;
190     b->def_spi_bus = 0;
191     b->spi_bus[0].bus_id = 1;
192     b->spi_bus[0].slave_s = 0;
193     b->spi_bus[0].cs = 10;
194     b->spi_bus[0].mosi = 11;
195     b->spi_bus[0].miso = 12;
196     b->spi_bus[0].sclk = 13;
197
198     b->uart_dev_count = 0;
199
200     return b;
201 error:
202     syslog(LOG_CRIT, "de3815: Platform failed to initialise");
203     free(b);
204     return NULL;
205 }