rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / surfaces / tranzport / io_kernel.cc
1 /*
2  *   Copyright (C) 2006 Paul Davis 
3  *   Copyright (C) 2007 Michael Taht
4  *
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.
9  *
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.
14  *
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.
18  *  
19  *   */
20
21 #if HAVE_TRANZPORT_KERNEL_DRIVER
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <poll.h>
25 #include "tranzport_control_protocol.h"
26
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
30
31 bool
32 TranzportControlProtocol::probe ()
33 {
34         if((udev = ::open(TRANZPORT_DEVICE,O_RDWR))> 0) {
35                 ::close(udev);
36                 return true; 
37         } 
38         error << _("Tranzport: Can't open device for Read/Write: ") << endmsg;
39         return false;
40 }
41
42 int
43 TranzportControlProtocol::open ()
44 {
45         if((udev=::open(TRANZPORT_DEVICE,O_RDWR))> 0) {
46                 return(udev);
47         } 
48         error << _("Tranzport: no device detected") << endmsg;
49         return udev;
50 }
51
52 int
53 TranzportControlProtocol::close ()
54 {
55         int ret = 0;
56
57         if (udev < 1) {
58                 return 0;
59         }
60
61         if ((ret = ::close (udev)) != 0) {
62                 error << _("Tranzport: cannot close device") << endmsg;
63         }
64
65         return ret;
66 }
67
68 // someday do buffered reads, presently this does blocking reads, which is bad...
69
70 int TranzportControlProtocol::read(uint8_t *buf, uint32_t timeout_override) 
71 {
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;
90         }
91
92         return last_read_error;
93
94
95         
96 int
97 TranzportControlProtocol::write_noretry (uint8_t* cmd, uint32_t timeout_override)
98 {
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);
102
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;
125                 }
126                 return last_write_error;
127         }
128
129         last_write_error = 0;
130         ++inflight;
131
132         return 0;
133
134 }       
135
136 int
137 TranzportControlProtocol::write (uint8_t* cmd, uint32_t timeout_override)
138 {
139         return (write_noretry(cmd,timeout_override));
140 }       
141
142 // FIXME - install poll semantics
143 #endif /* HAVE_TRANZPORT_KERNEL_DRIVER */
144