ignore unpaired noteoff's when writing part of a MidiModel to a new source. in realit...
[ardour2.git] / libs / surfaces / tranzport / io_kernel.cc
blob5e29e9ac7bd0e4b381e68c316ee9ce634f47e471
1 /*
2 * Copyright (C) 2006 Paul Davis
3 * Copyright (C) 2007 Michael Taht
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * */
21 #if HAVE_TRANZPORT_KERNEL_DRIVER
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <poll.h>
25 #include "tranzport_control_protocol.h"
27 // Something like open(/dev/surface/tranzport/event) for reading and raw for writing) would be better in the long run
28 // Also support for multiple tranzports needs to be figured out
29 // And bulk reads/writes in general
31 bool
32 TranzportControlProtocol::probe ()
34 if((udev = ::open(TRANZPORT_DEVICE,O_RDWR))> 0) {
35 ::close(udev);
36 return true;
38 error << _("Tranzport: Can't open device for Read/Write: ") << endmsg;
39 return false;
42 int
43 TranzportControlProtocol::open ()
45 if((udev=::open(TRANZPORT_DEVICE,O_RDWR))> 0) {
46 return(udev);
48 error << _("Tranzport: no device detected") << endmsg;
49 return udev;
52 int
53 TranzportControlProtocol::close ()
55 int ret = 0;
57 if (udev < 1) {
58 return 0;
61 if ((ret = ::close (udev)) != 0) {
62 error << _("Tranzport: cannot close device") << endmsg;
65 return ret;
68 // someday do buffered reads, presently this does blocking reads, which is bad...
70 int TranzportControlProtocol::read(uint8_t *buf, uint32_t timeout_override)
72 last_read_error = ::read (udev, (char *) buf, 8);
73 switch(errno) {
74 case -ENOENT:
75 case -ENXIO:
76 case -ECONNRESET:
77 case -ESHUTDOWN:
78 case -ENODEV:
79 cerr << "Tranzport disconnected, errno: " << last_read_error;
80 set_active(false);
81 break;
82 case -ETIMEDOUT: // This is not normal, but lets see what happened
83 cerr << "Tranzport read timed out, errno: " << last_read_error;
84 break;
85 default:
86 #if DEBUG_TRANZPORT
87 cerr << "Got an unknown error on read:" << last_read_error "\n";
88 #endif
89 break;
92 return last_read_error;
96 int
97 TranzportControlProtocol::write_noretry (uint8_t* cmd, uint32_t timeout_override)
99 // inflight is now taken care of by the driver, but...
100 if(inflight > MAX_TRANZPORT_INFLIGHT) { return (-1); }
101 int val = ::write (udev, (char*) cmd, 8);
103 if (val < 0 && val !=8) {
104 #if DEBUG_TRANZPORT
105 printf("write failed: %d\n", val);
106 #endif
107 last_write_error = errno;
108 switch(last_write_error) {
109 case -ENOENT:
110 case -ENXIO:
111 case -ECONNRESET:
112 case -ESHUTDOWN:
113 case -ENODEV:
114 cerr << "Tranzport disconnected, errno: " << last_write_error;
115 set_active(false);
116 break;
117 case -ETIMEDOUT: // This is not normal but
118 cerr << "Tranzport disconnected, errno: " << last_write_error;
119 break;
120 default:
121 #if DEBUG_TRANZPORT
122 cerr << "Got an unknown error on read:" << last_write_error "\n";
123 #endif
124 break;
126 return last_write_error;
129 last_write_error = 0;
130 ++inflight;
132 return 0;
137 TranzportControlProtocol::write (uint8_t* cmd, uint32_t timeout_override)
139 return (write_noretry(cmd,timeout_override));
142 // FIXME - install poll semantics
143 #endif /* HAVE_TRANZPORT_KERNEL_DRIVER */