summaryrefslogtreecommitdiff
path: root/libs/surfaces/tranzport/io_kernel.cc
blob: 0532ee7e6b335bf9cd8adcf3de65c94d358b661f (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
/*
 *   Copyright (C) 2006 Paul Davis
 *   Copyright (C) 2007 Michael Taht
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   */

#if HAVE_TRANZPORT_KERNEL_DRIVER
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include "tranzport_control_protocol.h"

// Something like open(/dev/surface/tranzport/event) for reading and raw for writing) would be better in the long run
// Also support for multiple tranzports needs to be figured out
// And bulk reads/writes in general

bool
TranzportControlProtocol::probe ()
{
	if((udev = ::open(TRANZPORT_DEVICE,O_RDWR))> 0) {
		::close(udev);
		return true;
	}
	error << _("Tranzport: Can't open device for Read/Write: ") << endmsg;
	return false;
}

int
TranzportControlProtocol::open ()
{
	if((udev=::open(TRANZPORT_DEVICE,O_RDWR))> 0) {
		return(udev);
	}
	error << _("Tranzport: no device detected") << endmsg;
	return udev;
}

int
TranzportControlProtocol::close ()
{
	int ret = 0;

	if (udev < 1) {
		return 0;
	}

	if ((ret = ::close (udev)) != 0) {
		error << _("Tranzport: cannot close device") << endmsg;
	}

	return ret;
}

// someday do buffered reads, presently this does blocking reads, which is bad...

int TranzportControlProtocol::read(uint8_t *buf, uint32_t timeout_override)
{
	last_read_error = ::read (udev, (char *) buf, 8);
	switch(errno) {
	case -ENOENT:
	case -ENXIO:
	case -ECONNRESET:
	case -ESHUTDOWN:
	case -ENODEV:
		cerr << "Tranzport disconnected, errno: " << last_read_error;
		set_active(false);
		break;
	case -ETIMEDOUT: // This is not normal, but lets see what happened
		cerr << "Tranzport read timed out, errno: " << last_read_error;
		break;
	default:
#if DEBUG_TRANZPORT
		cerr << "Got an unknown error on read:" << last_read_error "\n";
#endif
		break;
	}

	return last_read_error;
}

	
int
TranzportControlProtocol::write_noretry (uint8_t* cmd, uint32_t timeout_override)
{
	// inflight is now taken care of by the driver, but...
	if(inflight > MAX_TRANZPORT_INFLIGHT) { return (-1); }
	int val = ::write (udev, (char*) cmd, 8);

	if (val < 0 && val !=8) {
#if DEBUG_TRANZPORT
		printf("write failed: %d\n", val);
#endif
		last_write_error = errno;
		switch(last_write_error) {
		case -ENOENT:
		case -ENXIO:
		case -ECONNRESET:
		case -ESHUTDOWN:
		case -ENODEV:
			cerr << "Tranzport disconnected, errno: " << last_write_error;
			set_active(false);
			break;
		case -ETIMEDOUT: // This is not normal but
			cerr << "Tranzport disconnected, errno: " << last_write_error;
			break;
		default:
#if DEBUG_TRANZPORT
			cerr << "Got an unknown error on read:" << last_write_error "\n";
#endif
			break;
		}
		return last_write_error;
	}

	last_write_error = 0;
	++inflight;

	return 0;

}	

int
TranzportControlProtocol::write (uint8_t* cmd, uint32_t timeout_override)
{
	return (write_noretry(cmd,timeout_override));
}	

// FIXME - install poll semantics
#endif /* HAVE_TRANZPORT_KERNEL_DRIVER */