summaryrefslogtreecommitdiff
path: root/camlibs/topfield/usb_io.c
blob: 87356d36f9a21c3d34750b1904e7504c02015ed3 (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
/*

  Copyright (C) 2004 Peter Urbanec <toppy at urbanec.net>

  This file is part of puppy.

  puppy is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  puppy 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with puppy; if not, write to the 
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA  02110-1301  USA

*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "usb_io.h"
#include "tf_bytes.h"
#include "crc16.h"

/* The Topfield packet handling is a bit unusual. All data is stored in
 * memory in big endian order, however, just prior to transmission all
 * data is byte swapped.
 *
 * Functions to read and write the memory version of packets are provided
 * in tf_bytes.c
 *
 * Routines here take care of CRC generation, byte swapping and packet
 * transmission.
 */

/* Swap the odd and even bytes in the buffer, up to count bytes.
 * If count is odd, the last byte remains unafected.
 */
static void byte_swap(unsigned char * d, int count)
{
    int i;

    for(i = 0; i < (count & ~1); i += 2)
    {
        unsigned char t = d[i];

        d[i] = d[i + 1];
        d[i + 1] = t;
    }
}

/* Byte swap an incoming packet. */
static void swap_in_packet(struct tf_packet *packet)
{
    int size = (get_u16_raw(packet) + 1) & ~1;

    if(size > MAXIMUM_PACKET_SIZE)
    {
        size = MAXIMUM_PACKET_SIZE;
    };

    byte_swap((unsigned char *) packet, size);
}

/* Byte swap an outgoing packet. */
static void swap_out_packet(struct tf_packet *packet)
{
    int size = (get_u16(packet) + 1) & ~1;

    byte_swap((unsigned char *) packet, size);
}

static const unsigned char cancel_packet[8] = {
    0x08, 0x00, 0x40, 0x01, 0x00, 0x00, 0x03, 0x00
};

static const unsigned char success_packet[8] = {
    0x08, 0x00, 0x81, 0xc1, 0x00, 0x00, 0x02, 0x00
};

/* Optimised packet handling to reduce overhead during bulk file transfers. */
ssize_t send_cancel(Camera *camera,GPContext *context)
{
    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    return gp_port_write (camera->port, (char *)cancel_packet, 8);
}

ssize_t send_success(Camera *camera, GPContext *context)
{
    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    return gp_port_write (camera->port, (char *)success_packet, 8);
}

ssize_t send_cmd_ready(Camera *camera, GPContext *context)
{
    struct tf_packet req;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    put_u16(&req.length, 8);
    put_u32(&req.cmd, CMD_READY);
    return send_tf_packet(camera, &req, context);
}

ssize_t send_cmd_reset(Camera *camera, GPContext *context)
{
    struct tf_packet req;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    put_u16(&req.length, 8);
    put_u32(&req.cmd, CMD_RESET);
    return send_tf_packet(camera, &req, context);
}

ssize_t send_cmd_turbo(Camera *camera, int turbo_on, GPContext *context)
{
    struct tf_packet req;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    put_u16(&req.length, 12);
    put_u32(&req.cmd, CMD_TURBO);
    put_u32(&req.data, turbo_on);
    return send_tf_packet(camera, &req, context);
}

ssize_t send_cmd_hdd_size(Camera *camera, GPContext *context)
{
    struct tf_packet req;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    put_u16(&req.length, 8);
    put_u32(&req.cmd, CMD_HDD_SIZE);
    return send_tf_packet(camera, &req, context);
}

static unsigned short get_crc(struct tf_packet * packet)
{
    return crc16_ansi(&(packet->cmd), get_u16(&packet->length) - 4);
}

ssize_t send_cmd_hdd_dir(Camera *camera, char *path, GPContext *context)
{
    struct tf_packet req;
    unsigned short packetSize;
    int pathLen = strlen(path) + 1;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);

    if((PACKET_HEAD_SIZE + pathLen) >= MAXIMUM_PACKET_SIZE)
    {
        fprintf(stderr, "ERROR: Path is too long.\n");
        return -1;
    }

    packetSize = PACKET_HEAD_SIZE + pathLen;
    packetSize = (packetSize + 1) & ~1;
    put_u16(&req.length, packetSize);
    put_u32(&req.cmd, CMD_HDD_DIR);
    strcpy((char *) req.data, path);
    return send_tf_packet(camera, &req,context);
}

ssize_t send_cmd_hdd_file_send(Camera *camera, unsigned char dir, char *path, GPContext *context)
{
    struct tf_packet req;
    unsigned short packetSize;
    int pathLen = strlen(path) + 1;

    gp_log (GP_LOG_DEBUG, "topfield", "send_cmd_hdd_file_send(dir = %d, path = %s)", dir, path);
    if((PACKET_HEAD_SIZE + 1 + 2 + pathLen) >= MAXIMUM_PACKET_SIZE) {
        fprintf(stderr, "ERROR: Path is too long.\n");
        return -1;
    }

    packetSize = PACKET_HEAD_SIZE + 1 + 2 + pathLen;
    packetSize = (packetSize + 1) & ~1;
    put_u16(&req.length, packetSize);
    put_u32(&req.cmd, CMD_HDD_FILE_SEND);
    req.data[0] = dir;
    put_u16(&req.data[1], pathLen);
    strcpy((char *) &req.data[3], path);
    return send_tf_packet(camera, &req, context);
}

ssize_t send_cmd_hdd_del(Camera *camera, char *path, GPContext *context)
{
    struct tf_packet req;
    unsigned short packetSize;
    int pathLen = strlen(path) + 1;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    if((PACKET_HEAD_SIZE + pathLen) >= MAXIMUM_PACKET_SIZE)
    {
        fprintf(stderr, "ERROR: Path is too long.\n");
        return -1;
    }

    packetSize = PACKET_HEAD_SIZE + pathLen;
    packetSize = (packetSize + 1) & ~1;
    put_u16(&req.length, packetSize);
    put_u32(&req.cmd, CMD_HDD_DEL);
    strcpy((char *) req.data, path);
    return send_tf_packet(camera, &req, context);
}

ssize_t send_cmd_hdd_rename(Camera *camera, char *src, char *dst, GPContext *context)
{
    struct tf_packet req;
    unsigned short packetSize;
    unsigned short srcLen = strlen(src) + 1;
    unsigned short dstLen = strlen(dst) + 1;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    if((PACKET_HEAD_SIZE + 2 + srcLen + 2 + dstLen) >= MAXIMUM_PACKET_SIZE)
    {
        fprintf(stderr,
                "ERROR: Combination of source and destination paths is too long.\n");
        return -1;
    }

    packetSize = PACKET_HEAD_SIZE + 2 + srcLen + 2 + dstLen;
    packetSize = (packetSize + 1) & ~1;
    put_u16(&req.length, packetSize);
    put_u32(&req.cmd, CMD_HDD_RENAME);
    put_u16(&req.data[0], srcLen);
    strcpy((char *) &req.data[2], src);
    put_u16(&req.data[2 + srcLen], dstLen);
    strcpy((char *) &req.data[2 + srcLen + 2], dst);
    return send_tf_packet(camera, &req, context);
}

ssize_t send_cmd_hdd_create_dir(Camera *camera, char *path, GPContext *context)
{
    struct tf_packet req;
    unsigned short packetSize;
    unsigned short pathLen = strlen(path) + 1;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    if((PACKET_HEAD_SIZE + 2 + pathLen) >= MAXIMUM_PACKET_SIZE)
    {
        fprintf(stderr, "ERROR: Path is too long.\n");
        return -1;
    }

    packetSize = PACKET_HEAD_SIZE + 2 + pathLen;
    packetSize = (packetSize + 1) & ~1;
    put_u16(&req.length, packetSize);
    put_u32(&req.cmd, CMD_HDD_CREATE_DIR);
    put_u16(&req.data[0], pathLen);
    strcpy((char *) &req.data[2], path);
    return send_tf_packet(camera, &req, context);
}

/* Given a Topfield protocol packet, this function will calculate the required
 * CRC and send the packet out over a bulk pipe. */
ssize_t send_tf_packet(Camera *camera, struct tf_packet *packet, GPContext *context)
{
    unsigned int pl = get_u16(&packet->length);
    ssize_t byte_count = (pl + 1) & ~1;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    put_u16(&packet->crc, get_crc(packet));
    swap_out_packet(packet);
    return gp_port_write (camera->port, (char *) packet, byte_count);
}

/* Receive a Topfield protocol packet.
 * Returns a negative number if the packet read failed for some reason.
 */
ssize_t get_tf_packet(Camera *camera, struct tf_packet * packet, GPContext *context)
{
    unsigned char *buf = (unsigned char *) packet;
    int r;

    gp_log (GP_LOG_DEBUG, "topfield", __func__);
    r = gp_port_read (camera->port, (char *)buf, MAXIMUM_PACKET_SIZE);
    if(r < 0)
        return r;

    if(r < PACKET_HEAD_SIZE) {
        gp_log (GP_LOG_DEBUG, "topfield", "Short read. %d bytes\n", r);
        return -1;
    }

    /* Send SUCCESS as soon as we see a data transfer packet */
    if(DATA_HDD_FILE_DATA == get_u32_raw(&packet->cmd))
        send_success(camera,context);

    swap_in_packet(packet);

    {
        unsigned short crc;
        unsigned short calc_crc;
        unsigned short len = get_u16(&packet->length);

        if(len < PACKET_HEAD_SIZE) {
            gp_log (GP_LOG_DEBUG, "topfield", "Invalid packet length %04x\n", len);
            return -1;
        }

        crc = get_u16(&packet->crc);
        calc_crc = get_crc(packet);

        /* Complain about CRC mismatch */
        if(crc != calc_crc)
            gp_log (GP_LOG_ERROR, "topfield", "WARNING: Packet CRC %04x, expected %04x\n", crc, calc_crc);
    }
    return r;
}