root/branches/libffado-2.0/src/fireworks/fireworks_session_block.cpp

Revision 1284, 9.8 kB (checked in by ppalmers, 16 years ago)

rewrite the audiofire mixer, is now generic for all audiofire devices

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "fireworks_session_block.h"
25 #include "fireworks_device.h"
26
27 #include "libutil/ByteSwap.h"
28
29 #include <fstream>
30 // These classes provide support for reading/writing session blocks on
31 // echo fireworks based devices
32
33 // does not support legacy (v1) sessions
34
35 using namespace std;
36
37 namespace FireWorks {
38
39 IMPL_DEBUG_MODULE( Session, Session, DEBUG_LEVEL_NORMAL );
40
41 Session::Session()
42 {
43 }
44
45 Session::~Session()
46 {
47 }
48
49 bool
50 Session::loadFromDevice(Device &d)
51 {
52     size_t len = sizeof(SessionHeader) + sizeof(SubSession);
53     size_t start = d.getSessionBase();
54     if (start == 0) {
55         debugError("Invalid session base\n");
56         return false;
57     }
58
59     uint32_t data[len/4];
60     if(!d.readFlash(start, len/4, data)) {
61         debugError("Flash read failed\n");
62         return false;
63     }
64
65     if(!loadFromMemory(data, len)) {
66         debugError("Could not load session block from device memory dump\n");
67         return false;
68     }
69     return true;
70 }
71
72 bool
73 Session::saveToDevice(Device &d)
74 {
75     size_t len = sizeof(SessionHeader) + sizeof(SubSession);
76     size_t start = d.getSessionBase();
77     if (start == 0) {
78         debugError("Invalid session base\n");
79         return false;
80     }
81
82     debugWarning("CRC update not implemented, will fail\n");
83
84     uint32_t data[len/4];
85     if(!saveToMemory(data, len)) {
86         debugError("Could not save session to memory block\n");
87         return false;
88     }
89
90     if (!d.lockFlash(true)) {
91         debugError("  Could not lock flash\n");
92         return false;
93     }
94
95     if (!d.eraseFlashBlocks(start, len/4)) {
96         debugError("  Could not erase memory\n");
97         return false;
98     }
99
100     if(!d.writeFlash(start, len/4, data)) {
101         debugError("Writing to flash failed.\n");
102         return false;
103     }
104
105     if (!d.lockFlash(false)) {
106         debugError("  Could not unlock flash\n");
107         return false;
108     }
109
110     return true;
111 }
112
113 bool
114 Session::loadFromFile(std::string filename)
115 {
116     debugOutput(DEBUG_LEVEL_VERBOSE, "Loading session from file %s\n", filename.c_str());
117     fstream sessfile;
118    
119     debugOutput(DEBUG_LEVEL_VERBOSE, " Loading file...\n");
120     sessfile.open( filename.c_str(), ios::in | ios::ate | ios::binary);
121     if ( !sessfile.is_open() ) {
122         debugError("Could not open file.\n");
123         return false;
124     }
125     // get file size
126     int size;
127     size = (int)sessfile.tellg() - ECHO_SESSION_FILE_START_OFFSET;
128
129     sessfile.seekg(ECHO_SESSION_FILE_START_OFFSET, ios_base::beg);
130     debugOutput(DEBUG_LEVEL_VERBOSE, " Reading data, size = %d bytes, %d quads...\n", size, size/4);
131     char data[size];
132     sessfile.read(data, size);
133     sessfile.close();
134     if (sessfile.eof()) {
135         debugError("EOF while reading file\n");
136         return false;
137     }
138    
139     if(!loadFromMemory(data, size)) {
140         debugError("Could not load session block from file\n");
141         return false;
142     }
143     return true;
144 }
145
146 bool
147 Session::saveToFile(std::string filename)
148 {
149     debugOutput(DEBUG_LEVEL_VERBOSE, "Saving session to file %s\n", filename.c_str());
150     fstream sessfile;
151
152     debugOutput(DEBUG_LEVEL_VERBOSE, " Loading file...\n");
153     sessfile.open( filename.c_str(), ios::out | ios::trunc | ios::binary);
154     if ( !sessfile.is_open() ) {
155         debugError("Could not open file.\n");
156         return false;
157     }
158
159     // FIXME: figure out what the file header means
160     debugOutput(DEBUG_LEVEL_VERBOSE, " Writing file header...\n");
161     char header[ECHO_SESSION_FILE_START_OFFSET];
162     sessfile.write(header, ECHO_SESSION_FILE_START_OFFSET);
163
164     debugOutput(DEBUG_LEVEL_VERBOSE, " Writing session data...\n");
165     int size = sizeof(SessionHeader) + sizeof(SubSession);
166     char data[size];
167     if(!saveToMemory(data, size)) {
168         debugError("Could not save session to memory block\n");
169         return false;
170     }
171     sessfile.write(data, size);
172     sessfile.close();
173     return true;
174 }
175
176 bool
177 Session::loadFromMemory(void *buff, size_t len)
178 {
179     if (len != sizeof(SessionHeader) + sizeof(SubSession)) {
180         debugError("Invalid session length\n");
181         return false;
182     }
183     char *raw = (char *)buff;
184     memcpy(&h, raw, sizeof(SessionHeader));
185     memcpy(&s, raw+sizeof(SessionHeader), sizeof(SubSession));
186
187     if (len != h.size_quads*4) {
188         debugWarning("size not correct: got %d, should be %d according to data\n", len, h.size_quads*4);
189     }
190
191 #if __BYTE_ORDER == __BIG_ENDIAN
192     unsigned int i=0;
193     uint32_t *data = (uint32_t *)(&s);
194     for(; i < sizeof(SubSession)/4; i++) {
195         *data = ByteSwap32(*data);
196         data++;
197     }
198 #endif
199
200     return true;
201 }
202
203 bool
204 Session::saveToMemory(void *buff, size_t max_len)
205 {
206     if (max_len < sizeof(SessionHeader) + sizeof(SubSession)) {
207         debugError("Max length too small\n");
208         return false;
209     }
210     char *raw = (char *)buff;
211     memcpy(raw, &h, sizeof(SessionHeader));
212     memcpy(raw+sizeof(SessionHeader), &s, sizeof(SubSession));
213
214 #if __BYTE_ORDER == __BIG_ENDIAN
215     unsigned int i=0;
216     uint32_t *data = (uint32_t *)(raw+sizeof(SessionHeader));
217     for(; i < sizeof(SubSession)/4; i++) {
218         *data = ByteSwap32(*data);
219         data++;
220     }
221 #endif
222
223     return true;
224 }
225
226 uint32_t
227 Session::calculateCRC()
228 {
229     debugWarning("unimplemented\n");
230     return 0;
231 }
232
233 void
234 Session::show()
235 {
236     debugOutput(DEBUG_LEVEL_NORMAL, "Session Block\n");
237     debugOutput(DEBUG_LEVEL_NORMAL, " Size.............: %u (%08X)\n", h.size_quads, h.size_quads);
238     debugOutput(DEBUG_LEVEL_NORMAL, " CRC..............: %u (%08X)\n", h.crc, h.crc);
239     debugOutput(DEBUG_LEVEL_NORMAL, " Version..........: %u (%08X)\n", h.version, h.version);
240     debugOutput(DEBUG_LEVEL_NORMAL, " Flags............: %u (%08X)\n", h.flags, h.flags);
241     debugOutput(DEBUG_LEVEL_NORMAL, " Mirror Channel...: %d (%08X)\n", h.mirror_channel, h.mirror_channel);
242     debugOutput(DEBUG_LEVEL_NORMAL, " Digital Mode.....: %d (%08X)\n", h.digital_mode, h.digital_mode);
243     debugOutput(DEBUG_LEVEL_NORMAL, " Clock............: %d (%08X)\n", h.clock, h.clock);
244     debugOutput(DEBUG_LEVEL_NORMAL, " Rate.............: %d (%08X)\n", h.rate, h.rate);
245
246     debugOutput(DEBUG_LEVEL_NORMAL, " Gains:\n");
247     for(unsigned int in = 0; in < ECHO_SESSION_MAX_PHY_AUDIO_IN; in++) {
248         debugOutput(DEBUG_LEVEL_NORMAL, "  MON %02u: ", in);
249         for(unsigned int out = 0; out < ECHO_SESSION_MAX_PHY_AUDIO_OUT; out++) {
250             debugOutputShort(DEBUG_LEVEL_NORMAL, "%08X ", h.monitorgains[in][out]);
251             flushDebugOutput();
252         }
253         debugOutputShort(DEBUG_LEVEL_NORMAL, "\n");
254     }
255
256     debugOutput(DEBUG_LEVEL_NORMAL, "  PGAIN : ");
257     for(unsigned int out = 0; out < ECHO_SESSION_MAX_PHY_AUDIO_OUT; out++) {
258         debugOutputShort(DEBUG_LEVEL_NORMAL, "%08X ", h.playbackgains[out]);
259         flushDebugOutput();
260     }
261     debugOutputShort(DEBUG_LEVEL_NORMAL, "\n");
262
263     debugOutput(DEBUG_LEVEL_NORMAL, "  OGAIN : ");
264     for(unsigned int out = 0; out < ECHO_SESSION_MAX_PHY_AUDIO_OUT; out++) {
265         debugOutputShort(DEBUG_LEVEL_NORMAL, "%08X ", h.outputgains[out]);
266         flushDebugOutput();
267     }
268     debugOutputShort(DEBUG_LEVEL_NORMAL, "\n");
269
270     debugOutput(DEBUG_LEVEL_NORMAL, " Input settings:\n");
271     for(unsigned int in = 0; in < ECHO_SESSION_MAX_PHY_AUDIO_IN; in++) {
272         debugOutput(DEBUG_LEVEL_NORMAL,
273                     "  IN %02u: shift: %02X, pad: %02X, label: %s\n",
274                     in, s.inputs[in].shift, s.inputs[in].pad, s.inputs[in].label);
275         flushDebugOutput();
276     }
277
278     debugOutput(DEBUG_LEVEL_NORMAL, " Pans:\n");
279     for(unsigned int in = 0; in < ECHO_SESSION_MAX_PHY_AUDIO_IN; in++) {
280         debugOutput(DEBUG_LEVEL_NORMAL, "  IN %02u: ", in);
281         for(unsigned int out = 0; out < ECHO_SESSION_MAX_PHY_AUDIO_OUT; out++) {
282             debugOutputShort(DEBUG_LEVEL_NORMAL, "%03u ", s.monitorpans[in][out]);
283             flushDebugOutput();
284         }
285         debugOutputShort(DEBUG_LEVEL_NORMAL, "\n");
286     }
287     debugOutput(DEBUG_LEVEL_NORMAL, " Flags:\n");
288     for(unsigned int in = 0; in < ECHO_SESSION_MAX_PHY_AUDIO_IN; in++) {
289         debugOutput(DEBUG_LEVEL_NORMAL, "  IN %02u: ", in);
290         for(unsigned int out = 0; out < ECHO_SESSION_MAX_PHY_AUDIO_OUT; out++) {
291             debugOutputShort(DEBUG_LEVEL_NORMAL, "%02X ", s.monitorflags[in][out]);
292             flushDebugOutput();
293         }
294         debugOutputShort(DEBUG_LEVEL_NORMAL, "\n");
295     }
296
297     debugOutput(DEBUG_LEVEL_NORMAL, " Playback settings:\n");
298     for(unsigned int out = 0; out < ECHO_SESSION_MAX_PHY_AUDIO_OUT; out++) {
299         debugOutput(DEBUG_LEVEL_NORMAL,
300                     "  PBK %02u: mute: %02X, solo: %02X, label: %s\n",
301                     out, s.playbacks[out].mute, s.playbacks[out].solo, s.playbacks[out].label);
302     }
303     debugOutput(DEBUG_LEVEL_NORMAL, " Output settings:\n");
304     for(unsigned int out = 0; out < ECHO_SESSION_MAX_PHY_AUDIO_OUT; out++) {
305         debugOutput(DEBUG_LEVEL_NORMAL,
306                     "  OUT %02u: mute: %02X, shift: %02X, label: %s\n",
307                     out, s.outputs[out].mute, s.outputs[out].shift, s.outputs[out].label);
308         flushDebugOutput();
309     }
310
311 }
312
313 void
314 Session::dumpData()
315 {
316
317 }
318
319 } // FireWorks
Note: See TracBrowser for help on using the browser.