summaryrefslogtreecommitdiff
path: root/camlibs/digita/serial.c
blob: abd96721a24e10147a75cbd953ee800a2a0d6b2e (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
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
/*
 * serial.c
 *
 *  Serial digita support
 *
 * Copyright 1999-2001 Johannes Erdfelt
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#ifdef OS2
#include <db.h>
#endif
#include <netinet/in.h>

#include "digita.h"

#define GP_MODULE "digita"

#include <gphoto2/gphoto2-port.h>

#define MAX_BEACON_RETRIES	5
#define MAX_BEACON_TIMEOUTS	2

/*
 * u8  0xA5
 * u8  0x5A
 * u16 vendorid
 * u16 deviceid
 * u8  checksum
 */
#define BEACON_LEN		7

/*
 * u8  0x5A
 * u8  0xA5
 * u8  0x55 (iftype)
 * u8  commflag
 *  u4 reserved
 *  u2 pod_receive_mode
 *  u2 host_receive_mode
 * u32 dataspeed
 * u16 deviceframesize
 * u16 hostframesize
 * u8  checksum
 */
#define BEACON_ACK_LENGTH	13

/*
 * s8  result
 * u8  commflag
 *  u4 reserved
 *  u2 pod_receive_mode
 *  u2 host_receive_mode
 * u32 dataspeed
 * u16 deviceframesize
 * u16 hostframesize
 */
#define BEACON_COMP_LENGTH	10

#define POLL_LENGTH_MASK        0x03FF
#define POLL_BOB                0x0400
#define POLL_EOB                0x0800
#define POLL_CMD                0x1000
#define POLL_POLL_MASK          0xE000
#define POLL_POLL		(1 << 13)

#define POLL_ACK                0x01
#define POLL_NAK                0x02

static unsigned int checksum(const unsigned char *buffer, int len)
{
	int i;
	int limit = len - 1;
	unsigned int sum = 0;

	for (i = 0; i < limit; i++)
		sum += *(buffer++);

	return sum & 0xff;
}

static int poll_and_wait(gp_port *dev, int length, int bob, int eob)
{
	unsigned short s, poll, poll_reply;

	poll = POLL_POLL | POLL_CMD | (length & POLL_LENGTH_MASK) |
		(bob ? POLL_BOB : 0) | (eob ? POLL_EOB : 0);

	do {
		s = htons(poll);
		if (gp_port_write(dev, (void *)&s, sizeof(s)) < 0)
			return -1;
		if (gp_port_read(dev, (void *)&s, sizeof(s)) < 0)
			return -1;
		poll_reply = ntohs(s);
	} while (poll_reply & POLL_NAK);

	return 0;
}

static int digita_serial_send(CameraPrivateLibrary *dev, void *_buffer, int len)
{
	char *buffer = _buffer;
	unsigned short s;
	int sent = 0, size;

	while (sent < len) {
		if ((len - sent) > dev->deviceframesize)
			size = dev->deviceframesize;
		else
			size = len - sent;

		if (poll_and_wait(dev->gpdev, size, sent == 0, (size + sent) == len) < 0)
			return -1;

		if (gp_port_write(dev->gpdev, buffer + sent, size) < 0)
			return -1;

		sent += size;
	}

	s = 0;
	if (gp_port_write(dev->gpdev, (void *)&s, sizeof(s)) < 0)
		return -1;

	return len;
}

static int poll_and_reply(gp_port *dev, int *length, int *eob, int nak)
{
	unsigned short s, poll;

	if (gp_port_read(dev, (void *)&s, sizeof(s)) < 0)
		return -1;

	poll = ntohs(s);
	if (length)
		*length = poll & POLL_LENGTH_MASK;
	if (eob)
		*eob = poll & POLL_EOB;

	s = htons(nak ? POLL_NAK : POLL_ACK);
	if (gp_port_write(dev, (void *)&s, sizeof(s)) < 0)
		return -1;

	return 0;
}

static int digita_serial_read(CameraPrivateLibrary *dev, void *_buffer, int len)
{
	char *buffer = _buffer;
	unsigned short s;
	int received = 0, size, eob;

	while (received < len) {
		if (poll_and_reply(dev->gpdev, &size, &eob, 0) < 0)
			return -1;

		if (gp_port_read(dev->gpdev, buffer + received, size) < 0)
			return -1;

		received += size;
		if (eob)
			break;
	}

	if (gp_port_read(dev->gpdev, (void *)&s, sizeof(s)) < 0)
		return -1;

	return received;
}

int digita_serial_open(CameraPrivateLibrary *dev, Camera *camera)
{
	GPPortSettings settings;
	int selected_speed;
	int ret, retries, negotiated = 0;
	unsigned char buffer[20];
	unsigned short s;
	unsigned int l;

	/* Get the settings */
	ret = gp_port_get_settings(camera->port, &settings);
	if (ret < 0)
		return ret;

	/* Remember the selected speed */
	selected_speed = settings.serial.speed;
	if (!selected_speed)
		selected_speed = 115200;	/* Try the maximum speed */

	/* Set the settings */
	settings.serial.speed = 9600;
	settings.serial.bits = 8;
	settings.serial.parity = 0;
	settings.serial.stopbits = 1;
	ret = gp_port_set_settings(camera->port, settings);
	if (ret < 0)
		return ret;

	dev->send = digita_serial_send;
	dev->read = digita_serial_read;

	gp_port_send_break(dev->gpdev, 4);

	usleep(10000);

	/* FIXME: In some situations we may want to try a slower speed */
	for (retries = 0; !negotiated && retries < MAX_BEACON_RETRIES; retries++) {
		unsigned char csum;
		int i, timeouts = 0;

		/*
		 * Read the beacon
		 */
		memset(buffer, 0, sizeof(buffer));

		for (i = 0; (i < BEACON_LEN * 2) && (timeouts < MAX_BEACON_TIMEOUTS); i++) {
			/* We specifically eat as much as we can to catch any */
			/* garbage before or after */
			ret = gp_port_read(dev->gpdev, (void *)buffer, 1);
			if (ret < 0) {
				GP_DEBUG("couldn't read beacon (ret = %d)", ret);
				timeouts++;
				continue;
			}

			if (buffer[0] == 0xA5)
				break;
		}

		if (timeouts >= MAX_BEACON_TIMEOUTS)
			continue;

		ret = gp_port_read(dev->gpdev, (void *)(buffer + 1), BEACON_LEN - 1);
		if (ret < 0) {
			GP_DEBUG("couldn't read beacon (ret = %d)", ret);
			continue;
		}

		if (buffer[0] != 0xA5 || buffer[1] != 0x5A) {
			GP_DEBUG("Invalid header for beacon 0x%02x 0x%02x", buffer[0], buffer[1]);
			continue;
		}

		csum = buffer[6];
		buffer[6] = 0;
		if (checksum(buffer, BEACON_LEN) != csum) {
			GP_DEBUG("Beacon checksum failed (calculated 0x%02x, received 0x%02x)",
				checksum(buffer, BEACON_LEN), csum);
			continue;
		}

		memcpy((void *)&s, &buffer[2], 2);
		GP_DEBUG("Vendor: 0x%04x", ntohs(s));
		memcpy((void *)&s, &buffer[4], 2);
		GP_DEBUG("Product: 0x%04x", ntohs(s));

		/*
		 * Send the beacon acknowledgement
		 */
		buffer[0] = 0x5A;	/* Magic */
		buffer[1] = 0xA5;
		buffer[2] = 0x55;	/* I/F Type */
		buffer[3] = 0;		/* Comm Flag */
		l = htonl(selected_speed);
		memcpy(&buffer[4], (void *)&l, 4);	/* Data speed */
		s = htons(1023);
		memcpy(&buffer[8], (void *)&s, 2);	/* Device Frame Size */
		s = htons(1023);
		memcpy(&buffer[10], (void *)&s, 2);	/* Host Frame Size */
		buffer[12] = 0;
		buffer[12] = checksum(buffer, BEACON_ACK_LENGTH);

		ret = gp_port_write(dev->gpdev, (void *)buffer, BEACON_ACK_LENGTH);
		if (ret < 0) {
			GP_DEBUG("couldn't write beacon (ret = %d)", ret);
			return -1;
		}

		/*
		 * Read the beacon completion
		 */
		ret = gp_port_read(dev->gpdev, (void *)buffer, BEACON_COMP_LENGTH);
		if (ret < 0) {
			GP_DEBUG("couldn't read beacon_comp (ret = %d)", ret);
			continue;
		}

		if ((signed char)buffer[0] < 0) {
			GP_DEBUG("Bad status %d during beacon completion", (signed int)buffer[0]);
			continue;
		}

		memcpy((void *)&s, &buffer[6], sizeof(s));
		dev->deviceframesize = ntohs(s);

		memcpy((void *)&l, &buffer[2], sizeof(l));
		GP_DEBUG("negotiated %d", ntohl(l));
		settings.serial.speed = ntohl(l);

		usleep(100000);	/* Wait before */

		ret = gp_port_set_settings(dev->gpdev, settings);
		if (ret < 0)
			return ret;

		usleep(100000);	/* Wait after */

		/*
		 * The host interface spec mentions kTestSerialData, but
		 * doesn't elaborate anywhere else on it. So, I guess we
		 * assume everything is working here
		 */
		negotiated = 1;
	}

	return negotiated ? 0 : -1;
}