-
Notifications
You must be signed in to change notification settings - Fork 5
/
em2874-core.cpp
408 lines (343 loc) · 9.22 KB
/
em2874-core.cpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <unistd.h>
#include <string.h>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include "usbops.hpp"
#include "em2874-core.hpp"
#define USBREQ_TIMEOUT 100
#define I2C_TIMEOUT 200
#define EM28XX_REG_I2C_RET 0x05
#define EM28XX_REG_CHIPID 0x0A
#define EM2874_REG_CAS_STATUS 0x70
#define EM2874_REG_CAS_DATALEN 0x71
#define EM2874_REG_CAS_MODE1 0x72
#define EM2874_REG_CAS_RESET 0x73
#define EM2874_REG_CAS_MODE2 0x75
#define TARGET_ID_VENDOR 0x0511
#define TARGET_ID_PRODUCT 0x0029
#define TARGET_ID_PRODUCT2 0x003b
const char BASE_DIR_UDEV[] = "/dev/bus/usb"; // udev_USB
const char BASE_DIR_USBFS[] = "/proc/bus/usb"; // usbfs
inline uint8_t ICC_checkSum (const uint8_t* data, int len)
{
uint8_t sum = 0;
for ( ; len > 0; len-- ) {
sum ^= *data++;
}
return sum;
}
inline void miliWait( int s ) { usleep(s*1000); }
std::ostream * EM2874Device::log = NULL;
EM2874Device::EM2874Device ()
: isCardReady(false), fd(-1), isStream(false)
{
}
EM2874Device::~EM2874Device ()
{
if(fd >= 0)
{
writeReg(EM2874_REG_TS_ENABLE, 0);
writeReg(EM28XX_REG_GPIO, 0xFF);
writeReg(EM2874_REG_CAS_MODE1, 0x0);
writeReg(0x0C, 0x0);
usb_release(fd, 0);
close(fd);
}
}
bool EM2874Device::openDevice (const char *devfile)
{
usb_device_descriptor usb_desc;
usb_getdesc(devfile, &usb_desc);
if(usb_desc.idVendor != TARGET_ID_VENDOR ||
(usb_desc.idProduct != TARGET_ID_PRODUCT && usb_desc.idProduct != TARGET_ID_PRODUCT2))
return false;
fd = usb_open(devfile);
if(fd == -1)
return false;
usb_claim(fd, 0);
uint8_t val;
if(readReg(0x0A, &val) && readReg(0x0C, &val) && readReg(0x0B, &val)
){
writeReg(EM28XX_REG_GPIO, 0xFF);
}else{
return false;
}
if(writeReg(0x0C, 0x10)
&& writeReg(0x12, 0x27)
&& writeReg(EM28XX_REG_GPIO, 0xFE)
&& writeReg(EM2874_REG_CAS_MODE1, 0x0))
{
writeReg(0x13, 0x10);
writeReg(0x10, 0);
writeReg(0x11, 0x11);
writeReg(0x28, 0x01);
writeReg(0x29, 0xff);
writeReg(0x2a, 0x01);
writeReg(0x2b, 0xff);
writeReg(0x1c, 0);
writeReg(0x1d, 0);
writeReg(0x1e, 0);
writeReg(0x1f, 0);
writeReg(0x1b, 0);
writeReg(0x5e, 128);
writeReg( EM2874_REG_TS_ENABLE, 0 );
return true;
}
return false;
}
EM2874Device* EM2874Device::AllocDevice()
{
EM2874Device *pDev = new EM2874Device();
boost::filesystem::path base_dir = boost::filesystem::path(BASE_DIR_UDEV);
if (!boost::filesystem::exists(base_dir) || !boost::filesystem::is_directory(base_dir)) {
base_dir = boost::filesystem::path(BASE_DIR_USBFS);
}
bool isFound (false);
// base_dirからデバイスファイルを探す
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator bus_iter(base_dir); bus_iter != end && !isFound; ++bus_iter) {
// バスでループ
if (boost::filesystem::is_directory(*bus_iter)) {
// ディレクトリのみ
for (boost::filesystem::directory_iterator dev_iter(*bus_iter); dev_iter != end && !isFound; ++dev_iter) {
// バスに繋がっているデバイスでループ
if (!boost::filesystem::is_directory(*dev_iter)) {
// 開く
if (pDev->openDevice(dev_iter->path().string().c_str())) {
isFound = true;
if(log) *log << "device: " << dev_iter->path().string() << std::endl;
break;
}
}
}
}
}
if(!isFound) {
delete pDev;
if (log) *log << "no devices can be used." << std::endl;
return NULL;
}
return pDev;
}
bool EM2874Device::initDevice2 ()
{
bool ICCflg;
writeReg( EM28XX_REG_GPIO, 0x3e );
ICCflg = resetICC_1();
miliWait(100);
writeReg( EM28XX_REG_GPIO, 0x7e );
miliWait(50);
if(ICCflg) {
if(waitICC() < 0) {
goto CAS_off;
}
ICCflg = resetICC_2();
}else{
goto CAS_off;
}
return true;
CAS_off:
miliWait(60);
writeReg(EM2874_REG_CAS_MODE1, 0x0);
return false;
}
uint8_t EM2874Device::readReg (const uint8_t idx)
{
uint8_t val;
if(!readReg (idx, &val)) {
#ifdef DEBUG
*log << "readReg(" << (unsigned)idx << ") failed.";
#endif
}
return val;
}
int EM2874Device::readReg (const uint8_t idx, uint8_t *val)
{
usbdevfs_ctrltransfer ctrl1 = {USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE, 0, 0, idx, 1, USBREQ_TIMEOUT, val};
return usb_ctrl(fd, &ctrl1);
}
int EM2874Device::writeReg (const uint8_t idx, const uint8_t val)
{
uint8_t sbuf[1] = {val};
usbdevfs_ctrltransfer ctrl1 = {USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE, 0, 0, idx, 1, USBREQ_TIMEOUT, sbuf};
return usb_ctrl(fd, &ctrl1);
}
bool EM2874Device::readI2C (const uint8_t addr, const uint16_t size, uint8_t *data, const bool isStop)
{
uint8_t req = isStop ? 0x02 : 0x03;
usbdevfs_ctrltransfer ctrl1 = {USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE, req, 0, addr, size, USBREQ_TIMEOUT, data};
if(usb_ctrl(fd, &ctrl1) < 0) return false;
uint8_t ret = readReg(EM28XX_REG_I2C_RET);
return ret == 0 ? true : false;
}
bool EM2874Device::writeI2C (const uint8_t addr, const uint16_t size, uint8_t *data, const bool isStop)
{
uint8_t req = isStop ? 0x02 : 0x03;
usbdevfs_ctrltransfer ctrl1 = {USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE, req, 0, addr, size, USBREQ_TIMEOUT, data};
if(usb_ctrl(fd, &ctrl1) < 0) return false;
uint8_t ret = readReg(EM28XX_REG_I2C_RET);
return ret == 0 ? true : false;
}
bool EM2874Device::resetICC_1 ()
{
if(getCardStatus() == 0x5 &&
writeReg( EM2874_REG_CAS_MODE1, 0x1 ) &&
writeReg( EM2874_REG_CAS_RESET, 0x1 )
) {
return true;
}
return false;
}
bool EM2874Device::resetICC_2 ()
{
uint8_t rbuff[32];
size_t rlen = sizeof(rbuff);
readICC( &rlen, rbuff );
miliWait(1);
static uint8_t cmd[] = { 0x00, 0xc1, 0x01, 0xfe, 0x3e };
usbdevfs_ctrltransfer ctrl1 = {USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE, 0x14, 0, 0x200, sizeof(cmd), USBREQ_TIMEOUT, cmd};
if(usb_ctrl(fd, &ctrl1) < 0) return false;
writeReg( EM2874_REG_CAS_MODE2, 0);
writeReg( EM2874_REG_CAS_STATUS, 0x80 );
miliWait(30);
writeReg( EM2874_REG_CAS_DATALEN, 5 );
if(waitICC() < 0) return false;
ctrl1.bRequestType = USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE;
ctrl1.wIndex = 0;
ctrl1.wLength = 4;
ctrl1.data = rbuff;
if( usb_ctrl(fd, &ctrl1) < 0 || rbuff[1] != 0xe1 || rbuff[3] != 0xfe )
return false;
cardPCB = 0;
isCardReady = true;
return true;
}
bool EM2874Device::resetICC ()
{
if(!resetICC_1())
return false;
if(waitICC() < 0) {
writeReg(EM2874_REG_CAS_MODE1, 0x0);
return false;
}
return resetICC_2();
}
bool EM2874Device::writeICC ( const size_t size, const void *data )
{
uint8_t val;
if( getCardStatus() != 0x5 )
return false;
writeReg( EM2874_REG_CAS_MODE1, 0x01 );
usbdevfs_ctrltransfer ctrl1 = {USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE, 0x14, 0, 0, 0, USBREQ_TIMEOUT, NULL};
uint8_t cardBuf[256];
cardBuf[0] = 0;
cardBuf[1] = cardPCB;
cardBuf[2] = size;
memcpy( cardBuf+3, data, size );
cardBuf[size+3] = ICC_checkSum( cardBuf+1, size+2 );
val = size + 4;
for(int i = 0; i < val; i+=64 ) {
ctrl1.wIndex = 0x200 + i;
ctrl1.wLength = (val - i) > 64 ? 64 : (val - i);
ctrl1.data = cardBuf+i;
if(usb_ctrl(fd, &ctrl1) < 0) {
return false;
}
}
cardPCB ^= 0x40;
writeReg( EM2874_REG_CAS_MODE2, 0);
readReg( EM2874_REG_CAS_STATUS, &val );
writeReg( EM2874_REG_CAS_DATALEN, size + 4 );
miliWait(1);
return true;
}
bool EM2874Device::readICC ( size_t *size, void *data )
{
uint8_t val;
val = readReg( EM2874_REG_CAS_DATALEN );
if( val > *size + 4 || val < 5 )
return false;
*size = val - 4;
usbdevfs_ctrltransfer ctrl1 = {USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE, 0x14, 0, 0, 0, USBREQ_TIMEOUT, NULL};
uint8_t cardBuf[256];
for(int i = 0; i < val; i+=64 ) {
ctrl1.wIndex = i;
ctrl1.wLength = (val - i) > 64 ? 64 : (val - i);
ctrl1.data = cardBuf+i;
if(usb_ctrl(fd, &ctrl1) < 0) {
return false;
}
}
memcpy( data, cardBuf+3, val-4 );
return true;
}
int EM2874Device::waitICC ()
{
uint8_t val;
int i;
for(i = 0; i < 40; i++) {
miliWait(8);
if(!readReg(EM2874_REG_CAS_STATUS, &val))
continue;
if( val == 5 ) {
return i;
}
if( val == 0 ) return -1; // card error
}
return -2; // timeout
}
int EM2874Device::getCardStatus()
{
uint8_t val;
if(readReg(EM2874_REG_CAS_STATUS, &val)) {
return val;
}
return -1;
}
int EM2874Device::getDeviceID()
{
uint8_t buf[2], tuner_reg;
// ROMで判断
writeReg(EM28XX_REG_I2C_CLK, 0x42);
buf[0] = 0; buf[1] = 0x6a; writeI2C(EEPROM_ADDR, 2, buf, false);
if(!readI2C (EEPROM_ADDR, 2, buf, true))
return -1;
if(buf[0] == 0x3b && buf[1] == 0x00)
return 2;
// Tuner Regで判断
writeReg(EM28XX_REG_I2C_CLK, 0x44);
buf[0] = 0xfe; buf[1] = 0; writeI2C(DEMOD_ADDR, 2, buf, true);
tuner_reg = 0x0;
writeI2C(TUNER_ADDR, 1, &tuner_reg, false);
readI2C (TUNER_ADDR, 1, &tuner_reg, true);
buf[0] = 0xfe; buf[1] = 1; writeI2C(DEMOD_ADDR, 2, buf, true);
if(tuner_reg == 0x84) // TDA18271HD
return 1;
return 2;
}
/* */
void EM2874Device::startStream()
{
if (isStream) return;
writeReg(EM2874_REG_TS_ENABLE, EM2874_TS1_CAPTURE_ENABLE | EM2874_TS1_NULL_DISCARD);
usb_setinterface(fd, 0, 1);
ts_func = new TsIoThread (fd, EM2874_EP_TS1);
ts_thread = new boost::thread(boost::ref(*ts_func));
isStream = true;
}
void EM2874Device::stopStream()
{
if (!isStream) return;
writeReg(EM2874_REG_TS_ENABLE, 0);
ts_func->cancel();
ts_thread->join();
delete ts_thread;
delete ts_func;
isStream = false;
}
int EM2874Device::getStream(const void **ptr)
{
if (!isStream) return 0;
return ts_func->readBuffer(ptr);
}
/* */