4 * Serial digita support
6 * Copyright 1999-2001 Johannes Erdfelt
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
36 #include "gphoto2-endian.h"
39 #define GP_MODULE "digita"
41 #include <gphoto2/gphoto2-port.h>
43 #define MAX_BEACON_RETRIES 5
44 #define MAX_BEACON_TIMEOUTS 2
62 * u2 host_receive_mode
68 #define BEACON_ACK_LENGTH 13
75 * u2 host_receive_mode
80 #define BEACON_COMP_LENGTH 10
82 #define POLL_LENGTH_MASK 0x03FF
83 #define POLL_BOB 0x0400
84 #define POLL_EOB 0x0800
85 #define POLL_CMD 0x1000
86 #define POLL_POLL_MASK 0xE000
87 #define POLL_POLL (1 << 13)
92 static unsigned int checksum(const unsigned char *buffer, int len)
98 for (i = 0; i < limit; i++)
104 static int poll_and_wait(gp_port *dev, int length, int bob, int eob)
106 unsigned short s, poll, poll_reply;
108 poll = POLL_POLL | POLL_CMD | (length & POLL_LENGTH_MASK) |
109 (bob ? POLL_BOB : 0) | (eob ? POLL_EOB : 0);
113 if (gp_port_write(dev, (void *)&s, sizeof(s)) < 0)
115 if (gp_port_read(dev, (void *)&s, sizeof(s)) < 0)
117 poll_reply = ntohs(s);
118 } while (poll_reply & POLL_NAK);
123 static int digita_serial_send(CameraPrivateLibrary *dev, void *_buffer, int len)
125 char *buffer = _buffer;
130 if ((len - sent) > dev->deviceframesize)
131 size = dev->deviceframesize;
135 if (poll_and_wait(dev->gpdev, size, sent == 0, (size + sent) == len) < 0)
138 if (gp_port_write(dev->gpdev, buffer + sent, size) < 0)
145 if (gp_port_write(dev->gpdev, (void *)&s, sizeof(s)) < 0)
151 static int poll_and_reply(gp_port *dev, int *length, int *eob, int nak)
153 unsigned short s, poll;
155 if (gp_port_read(dev, (void *)&s, sizeof(s)) < 0)
160 *length = poll & POLL_LENGTH_MASK;
162 *eob = poll & POLL_EOB;
164 s = htons(nak ? POLL_NAK : POLL_ACK);
165 if (gp_port_write(dev, (void *)&s, sizeof(s)) < 0)
171 static int digita_serial_read(CameraPrivateLibrary *dev, void *_buffer, int len)
173 char *buffer = _buffer;
175 int received = 0, size, eob;
177 while (received < len) {
178 if (poll_and_reply(dev->gpdev, &size, &eob, 0) < 0)
181 if (gp_port_read(dev->gpdev, buffer + received, size) < 0)
189 if (gp_port_read(dev->gpdev, (void *)&s, sizeof(s)) < 0)
195 int digita_serial_open(CameraPrivateLibrary *dev, Camera *camera)
197 GPPortSettings settings;
199 int ret, retries, negotiated = 0;
200 unsigned char buffer[20];
204 /* Get the settings */
205 ret = gp_port_get_settings(camera->port, &settings);
209 /* Remember the selected speed */
210 selected_speed = settings.serial.speed;
212 selected_speed = 115200; /* Try the maximum speed */
214 /* Set the settings */
215 settings.serial.speed = 9600;
216 settings.serial.bits = 8;
217 settings.serial.parity = 0;
218 settings.serial.stopbits = 1;
219 ret = gp_port_set_settings(camera->port, settings);
223 dev->send = digita_serial_send;
224 dev->read = digita_serial_read;
226 gp_port_send_break(dev->gpdev, 4);
230 /* FIXME: In some situations we may want to try a slower speed */
231 for (retries = 0; !negotiated && retries < MAX_BEACON_RETRIES; retries++) {
238 memset(buffer, 0, sizeof(buffer));
240 for (i = 0; (i < BEACON_LEN * 2) && (timeouts < MAX_BEACON_TIMEOUTS); i++) {
241 /* We specifically eat as much as we can to catch any */
242 /* garbage before or after */
243 ret = gp_port_read(dev->gpdev, (void *)buffer, 1);
245 GP_DEBUG("couldn't read beacon (ret = %d)", ret);
250 if (buffer[0] == 0xA5)
254 if (timeouts >= MAX_BEACON_TIMEOUTS)
257 ret = gp_port_read(dev->gpdev, (void *)(buffer + 1), BEACON_LEN - 1);
259 GP_DEBUG("couldn't read beacon (ret = %d)", ret);
263 if (buffer[0] != 0xA5 || buffer[1] != 0x5A) {
264 GP_DEBUG("Invalid header for beacon 0x%02x 0x%02x", buffer[0], buffer[1]);
270 if (checksum(buffer, BEACON_LEN) != csum) {
271 GP_DEBUG("Beacon checksum failed (calculated 0x%02x, received 0x%02x)",
272 checksum(buffer, BEACON_LEN), csum);
276 memcpy((void *)&s, &buffer[2], 2);
277 GP_DEBUG("Vendor: 0x%04x", ntohs(s));
278 memcpy((void *)&s, &buffer[4], 2);
279 GP_DEBUG("Product: 0x%04x", ntohs(s));
282 * Send the beacon acknowledgement
284 buffer[0] = 0x5A; /* Magic */
286 buffer[2] = 0x55; /* I/F Type */
287 buffer[3] = 0; /* Comm Flag */
288 l = htonl(selected_speed);
289 memcpy(&buffer[4], (void *)&l, 4); /* Data speed */
291 memcpy(&buffer[8], (void *)&s, 2); /* Device Frame Size */
293 memcpy(&buffer[10], (void *)&s, 2); /* Host Frame Size */
295 buffer[12] = checksum(buffer, BEACON_ACK_LENGTH);
297 ret = gp_port_write(dev->gpdev, (void *)buffer, BEACON_ACK_LENGTH);
299 GP_DEBUG("couldn't write beacon (ret = %d)", ret);
304 * Read the beacon completion
306 ret = gp_port_read(dev->gpdev, (void *)buffer, BEACON_COMP_LENGTH);
308 GP_DEBUG("couldn't read beacon_comp (ret = %d)", ret);
312 if ((signed char)buffer[0] < 0) {
313 GP_DEBUG("Bad status %d during beacon completion", (signed int)buffer[0]);
317 memcpy((void *)&s, &buffer[6], sizeof(s));
318 dev->deviceframesize = ntohs(s);
320 memcpy((void *)&l, &buffer[2], sizeof(l));
321 GP_DEBUG("negotiated %d", ntohl(l));
322 settings.serial.speed = ntohl(l);
324 usleep(100000); /* Wait before */
326 ret = gp_port_set_settings(dev->gpdev, settings);
330 usleep(100000); /* Wait after */
333 * The host interface spec mentions kTestSerialData, but
334 * doesn't elaborate anywhere else on it. So, I guess we
335 * assume everything is working here
340 return negotiated ? 0 : -1;