summaryrefslogtreecommitdiff
path: root/lib/msp430.c
blob: b2afd2516cb26a15aac0f6e3c2f469db6350c1fe (plain)
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
/*
 * Copyright
 *
 * Copyright (C) 2009-2010 Christian König (deathsimple@vodafone.de)
 *
 * License
 *
 * This program is free software; you can redistribute and/or modify
 * program under the terms of GNU General Public license either version 3
 * of the License, or (at your option) any later version. 
 *
 */

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>

#include <linux/i2c-dev.h>
#ifndef I2C_M_RD
#include <linux/i2c.h>
#endif

#include "cec.h"
#include "../msp430/cec_state.h"

#define BUFFER_SIZE	32
#define I2C_ADDR	0x60

#define RX_DELAY	10000
#define TX_BASE_DELAY	4500
#define TX_BIT_DELAY	2400
#define TX_RETRY	10

#define EDID_BLOCK_SEL	0x30
#define EDID_BYTE_SEL	0x50

struct MSP430_Hardware
{
	struct CEC_Hardware	cec;
	int			i2c;

	int			index;
	uint8_t			buffer[BUFFER_SIZE];
};

static ssize_t Fill_Buffer(struct MSP430_Hardware* hw)
{
	hw->index = 0;
	memset(hw->buffer, 0, BUFFER_SIZE);
	return read(hw->i2c, hw->buffer, BUFFER_SIZE);
}

static int Buffer_Empty(struct MSP430_Hardware* hw)
{
	return	hw->index < BUFFER_SIZE && (
		hw->buffer[hw->index] == 0 ||
		hw->buffer[hw->index] == 0xff);
}

static struct CEC_Packet* Receive(struct CEC_Device* device)
{
	struct MSP430_Hardware* hw = (struct MSP430_Hardware*)device->hardware;

	if(Buffer_Empty(hw)) {
		usleep(RX_DELAY);
		if(Fill_Buffer(hw) < 0) return NULL;
	}

	while(!Buffer_Empty(hw) && hw->buffer[hw->index] > CEC_Bytes_Max)
		hw->index++;

	if(!Buffer_Empty(hw)) {
		hw->buffer[hw->index] >>= 3;

		void* packet = &(hw->buffer[hw->index]);
		hw->index += hw->buffer[hw->index];
		if(hw->index > BUFFER_SIZE) return NULL;
		return (struct CEC_Packet*)packet;
	}
	return NULL;
}

static int Transmit(struct CEC_Device* device, struct CEC_Packet* packet)
{
	struct MSP430_Hardware* hw = (struct MSP430_Hardware*)device->hardware;

	do {
		CEC_Receive(device);
		CEC_Receive(device);
	} while(!Buffer_Empty(hw));

	if(write(hw->i2c, ((void*)packet)+1, packet->length) != packet->length)
		return 0;


	int retry;
	for(retry=0; retry<TX_RETRY; retry++)
	{
		usleep(TX_BASE_DELAY + packet->length * TX_BIT_DELAY * 10);

		int result = Fill_Buffer(hw);
		if(result < 0) continue;

		result = -1;

		int i;
		for(i=0;i<BUFFER_SIZE;i++) {
			if(hw->buffer[i] == CEC_Transmitted)
				result = 1;
			else if(hw->buffer[i] > CEC_Bytes_Max)
				result = 0;
			else if(hw->buffer[i] == 0)
				break;

			i += hw->buffer[i];
		}
		if(result != -1) return result;
		usleep(RX_DELAY);
	}

	return 0;
}

static uint8_t Read_DDC(struct MSP430_Hardware* hw, uint16_t addr)
{
	struct i2c_rdwr_ioctl_data	rdwr;
	struct i2c_msg			msgs[3];
	
	uint8_t block_buf = addr >> 8;
	uint8_t addr_buf = addr & 0xFF;
	uint8_t result_buf = 0;

	rdwr.msgs = msgs;
	rdwr.nmsgs = 0;

	if(block_buf) {
		msgs[0].addr = EDID_BLOCK_SEL;
		msgs[0].flags = 0;
		msgs[0].len = 1;
		msgs[0].buf = (void*)&block_buf;
		rdwr.nmsgs++;
	}

	msgs[rdwr.nmsgs].addr = EDID_BYTE_SEL;
	msgs[rdwr.nmsgs].flags = 0;
	msgs[rdwr.nmsgs].len = 1;
	msgs[rdwr.nmsgs].buf = (void*)&addr_buf;
	rdwr.nmsgs++;

	msgs[rdwr.nmsgs].addr = EDID_BYTE_SEL;
	msgs[rdwr.nmsgs].flags = I2C_M_RD;
	msgs[rdwr.nmsgs].len = 1;
	msgs[rdwr.nmsgs].buf = (void*)&result_buf;
	rdwr.nmsgs++;

	ioctl(hw->i2c, I2C_RDWR, &rdwr);
	return result_buf;
}

static struct CEC_Physical_Address Get_Addr(struct CEC_Device* device)
{
	struct MSP430_Hardware* hw = (struct MSP430_Hardware*)device->hardware;
	uint8_t nr_of_extensions = Read_DDC(hw, 126);

	int i;

	for(i=1; i <= nr_of_extensions; i++)
	{
		uint8_t tag = Read_DDC(hw, 128*i);
		if(tag != 0x02) continue;

		uint8_t data;
		uint8_t end = Read_DDC(hw, 128*i + 0x02);

		for(data = 0x04; data < end; data++)
		{
			uint8_t type = Read_DDC(hw, 128*i + data);
			if((type >> 5) == 3) {
				uint8_t byte[2];
				byte[0] = Read_DDC(hw, 128*i + data + 4);
				byte[1] = Read_DDC(hw, 128*i + data + 5);

				struct CEC_Physical_Address result;
				result.a = byte[0] & 0xF;
				result.b = byte[0] >> 4;
				result.c = byte[1] & 0xF;
				result.d = byte[1] >> 4;
				return result;
			}
			data += type & 0x1F;
		}
	}

	struct CEC_Physical_Address result;
	result.a = 0xF;
	result.b = 0xF;
	result.c = 0xF;
	result.d = 0xF;
	return result;
}

struct CEC_Hardware* MSP430_Open_Hardware(const char* device)
{
	struct MSP430_Hardware* result = malloc(sizeof(struct MSP430_Hardware));

	if(result == NULL)
		return NULL;

	result->cec.func_receive = Receive;
        result->cec.func_transmit = Transmit;
	result->cec.func_get_addr = Get_Addr;

	result->i2c = open(device, O_RDWR);

	if(result->i2c < 0) 
		goto error1;

	if(ioctl(result->i2c, I2C_SLAVE, I2C_ADDR) < 0)
		goto error2;

	return &(result->cec);

error2:
	close(result->i2c);

error1:
	free(result);
	return NULL;
}

void MSP430_Close_Hardware(struct CEC_Hardware* hardware)
{
	struct MSP430_Hardware* hw = (struct MSP430_Hardware*)hardware;
	close(hw->i2c);
	free(hw);
}