summaryrefslogtreecommitdiffstats
path: root/src/readwrite.c
blob: a0608b7f05e218889ed85c1c3a64220b48b68e4d (plain) (blame)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * libraw1394 - library for raw access to the 1394 bus with the Linux subsystem.
 *
 * Copyright (C) 1999,2000,2001,2002 Andreas Bombe
 *                     2002 Christian Toegel <christian.toegel@gmx.at>
 *                     2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
 *
 * This library is licensed under the GNU Lesser General Public License (LGPL),
 * version 2.1 or later. See the file COPYING.LIB in the distribution for
 * details.
 *
 *
 * Contributions:
 *
 * Christian Toegel <christian.toegel@gmx.at>
 *        lock64 request
 *        transmit physical packet
 *        raw1394_echo_request, raw1394_wake_up (interrupt blocking wait)
 *
 * Manfred Weihs <weihs@ict.tuwien.ac.at>
 *        raw1394_start_async_send, raw1394_async_send
 */

#include <config.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "raw1394.h"
#include "kernel-raw1394.h"
#include "raw1394_private.h"


/**
 * raw1394_start_read - initiate a read transaction
 * @node: target node
 * @addr: address to read from
 * @length: amount of data to read
 * @buffer: pointer to buffer where data will be saved
 * @tag: data to identify the request to completion handler
 *
 * This function starts the specified read request and returns %0 for success
 * and a negative number for an error, in which case errno will be set.  If
 * @length is %4 a quadlet read is initiated and a block read otherwise.
 *
 * The transaction is only started, no success of the transaction is implied
 * with a successful return of this function.  When the transaction completes, a
 * raw1394_loop_iterate() will call the tag handler and pass it the tag and
 * error code of the transaction.  @tag should therefore be set to something
 * that uniquely identifies this transaction (e.g. a struct pointer casted to
 * unsigned long).
 **/
int raw1394_start_read(struct raw1394_handle *handle, nodeid_t node,
                       nodeaddr_t addr, size_t length, quadlet_t *buffer,
                       unsigned long tag)
{
        struct raw1394_request *req = &handle->req;

        CLEAR_REQ(req);

        req->type = RAW1394_REQ_ASYNC_READ;
        req->generation = handle->generation;
        req->tag = tag;

        req->address = ((__u64)node << 48) | addr;
        req->length = length;
        req->recvb = ptr2int(buffer);

        return (int)write(handle->fd, req, sizeof(*req));
}


/**
 * raw1394_start_write - initiate a write transaction
 * @node: target node
 * @addr: address to write to
 * @length: amount of data to write
 * @data: pointer to data to be sent
 * @tag: data to identify the request to completion handler
 *
 * This function starts the specified write request and returns %0 for success
 * and a negative number for an error, in which case errno will be set.  If
 * @length is %4 a quadlet write is initiated and a block write otherwise.
 *
 * The transaction is only started, no success of the transaction is implied
 * with a successful return of this function.  When the transaction completes, a
 * raw1394_loop_iterate() will call the tag handler and pass it the tag and
 * error code of the transaction.  @tag should therefore be set to something
 * that uniquely identifies this transaction (e.g. a struct pointer casted to
 * unsigned long).
 **/
int raw1394_start_write(struct raw1394_handle *handle, nodeid_t node,
                        nodeaddr_t addr, size_t length, quadlet_t *data,
                        unsigned long tag)
{
        struct raw1394_request *req = &handle->req;

        CLEAR_REQ(req);

        req->type = RAW1394_REQ_ASYNC_WRITE;
        req->generation = handle->generation;
        req->tag = tag;

        req->address = ((__u64)node << 48) | addr;
        req->length = length;
        req->sendb = ptr2int(data);

        return (int)write(handle->fd, req, sizeof(*req));
}


/**
 * raw1394_start_lock - initiate a lock transaction
 * @node: target node
 * @addr: address to read from
 * @extcode: extended transaction code determining the lock operation
 * @data: data part of lock parameters
 * @arg: arg part of lock parameters
 * @result: address where return value will be written
 * @tag: data to identify the request to completion handler
 *
 * This function starts the specified lock request and returns %0 for success
 * and a negative number for an error, in which case errno will be set.
 *
 * The transaction is only started, no success of the transaction is implied
 * with a successful return of this function.  When the transaction completes, a
 * raw1394_loop_iterate() will call the tag handler and pass it the tag and
 * error code of the transaction.  @tag should therefore be set to something
 * that uniquely identifies this transaction (e.g. a struct pointer casted to
 * unsigned long).
 **/
int raw1394_start_lock(struct raw1394_handle *handle, nodeid_t node,
                       nodeaddr_t addr, unsigned int extcode, quadlet_t data,
                       quadlet_t arg, quadlet_t *result, unsigned long tag)
{
        struct raw1394_request *req = &handle->req;
        quadlet_t sendbuf[2];

        if ((extcode > 7) || (extcode == 0)) {
                errno = EINVAL;
                return -1;
        }

        CLEAR_REQ(req);

        req->type = RAW1394_REQ_LOCK;
        req->generation = handle->generation;
        req->tag = tag;

        req->address = ((__u64)node << 48) | addr;
        req->sendb = ptr2int(sendbuf);
        req->recvb = ptr2int(result);
        req->misc = extcode;

        switch (extcode) {
        case 3: /* EXTCODE_FETCH_ADD */
        case 4: /* EXTCODE_LITTLE_ADD */
                sendbuf[0] = data;
                req->length = 4;
                break;
        default:
                sendbuf[0] = arg;
                sendbuf[1] = data;
                req->length = 8;
                break;
        }
        return (int)write(handle->fd, req, sizeof(*req));
}

int raw1394_start_lock64(struct raw1394_handle *handle, nodeid_t node,
                         nodeaddr_t addr, unsigned int extcode, octlet_t data,
                         octlet_t arg, octlet_t *result, unsigned long tag)
{
        struct raw1394_request *req = &handle->req;
        octlet_t sendbuf[2];

        if ((extcode > 7) || (extcode == 0)) {
                errno = EINVAL;
                return -1;
        }

        CLEAR_REQ(req);

        req->type = RAW1394_REQ_LOCK64;
        req->generation = handle->generation;
        req->tag = tag;

        req->address = ((__u64)node << 48) | addr;
        req->sendb = ptr2int(sendbuf);
        req->recvb = ptr2int(result);
        req->misc = extcode;

        switch (extcode) {
        case 3: /* EXTCODE_FETCH_ADD */
        case 4: /* EXTCODE_LITTLE_ADD */
                sendbuf[0] = data;
                req->length = 8;
                break;
        default:
                sendbuf[0] = arg;
                sendbuf[1] = data;
                req->length = 16;
                break;
        }

        return (int)write(handle->fd, req, sizeof(*req));
}


/**
 * raw1394_start_iso_write - initiate an isochronous packet write
 * @channel: channel number on which to send on
 * @tag: data to be put into packet's tag field
 * @sy: data to be put into packet's sy field
 * @speed: speed at which to send
 * @length: amount of data to send
 * @data: pointer to data to send
 * @rawtag: data to identify the request to completion handler
 *
 * This function starts the specified isochronous packet transmission and
 * returns %0 for success and a negative number for an error, in which case
 * errno will be set.
 *
 * When the send completes, a raw1394_loop_iterate() will call the tag handler
 * and pass it the tag and error code of the transaction.  @tag should therefore
 * be set to something that uniquely identifies this transaction (e.g. a struct
 * pointer casted to unsigned long).
 *
 * Isochronous packets are automatically
 **/
int raw1394_start_iso_write(struct raw1394_handle *handle, unsigned int channel,
                            unsigned int tag, unsigned int sy,
                            unsigned int speed, size_t length, quadlet_t *data,
                            unsigned long rawtag)
{
        struct raw1394_request *req = &handle->req;

        CLEAR_REQ(req);

        req->type = RAW1394_REQ_ISO_SEND;
        req->generation = handle->generation;
        req->tag = rawtag;

        req->address = ((__u64)channel << 48) | speed;
        req->misc = (tag << 16) | sy;
        req->length = length;
        req->sendb = ptr2int(data);

        return (int)write(handle->fd, req, sizeof(*req));
}

int raw1394_start_async_send(struct raw1394_handle *handle,
                             size_t length, size_t header_length, unsigned int expect_response,
                             quadlet_t *data, unsigned long rawtag)
{
        struct raw1394_request *req = &handle->req;

        CLEAR_REQ(req);

        req->type = RAW1394_REQ_ASYNC_SEND;
        req->generation = handle->generation;
        req->tag = rawtag;

        req->length = length;
        req->misc = (expect_response << 16) | (header_length & 0xffff);
        req->sendb = ptr2int(data);

        return (int)write(handle->fd, req, sizeof(*req));
}


#define SYNCFUNC_VARS                                                     \
        struct sync_cb_data sd = { 0, 0 };                                \
        struct raw1394_reqhandle rh = { (req_callback_t)_raw1394_sync_cb, \
                                        &sd };                            \
        int err = 0

#define SYNCFUNC_BODY                                 \
        while (!sd.done) {                            \
                if (err < 0) return err;              \
                err = raw1394_loop_iterate(handle);   \
        }                                             \
        handle->err = sd.errcode;                     \
        errno = raw1394_errcode_to_errno(sd.errcode); \
        return (errno ? -1 : 0)

int raw1394_read(struct raw1394_handle *handle, nodeid_t node, nodeaddr_t addr,
                 size_t length, quadlet_t *buffer)
{
        SYNCFUNC_VARS;

        err = raw1394_start_read(handle, node, addr, length, buffer, 
                                 (unsigned long)&rh);

        SYNCFUNC_BODY;
}

int raw1394_write(struct raw1394_handle *handle, nodeid_t node, nodeaddr_t addr,
                  size_t length, quadlet_t *data)
{
        SYNCFUNC_VARS;

        err = raw1394_start_write(handle, node, addr, length, data, 
                                  (unsigned long)&rh);

        SYNCFUNC_BODY;
}

int raw1394_lock(struct raw1394_handle *handle, nodeid_t node, nodeaddr_t addr,
                 unsigned int extcode, quadlet_t data, quadlet_t arg,
                 quadlet_t *result)
{
        SYNCFUNC_VARS;

        err = raw1394_start_lock(handle, node, addr, extcode, data, arg, result,
                                 (unsigned long)&rh);

        SYNCFUNC_BODY;
}

int raw1394_lock64(struct raw1394_handle *handle, nodeid_t node, nodeaddr_t addr,
                 unsigned int extcode, octlet_t data, octlet_t arg,
                 octlet_t *result)
{
        SYNCFUNC_VARS;

        err = raw1394_start_lock64(handle, node, addr, extcode, data, arg, result,
                                 (unsigned long)&rh);

        SYNCFUNC_BODY;
}


int raw1394_iso_write(struct raw1394_handle *handle, unsigned int channel,
                      unsigned int tag, unsigned int sy, unsigned int speed,
                      size_t length, quadlet_t *data)
{
        SYNCFUNC_VARS;

        err = raw1394_start_iso_write(handle, channel, tag, sy, speed, length,
                                      data, (unsigned long)&rh);

        SYNCFUNC_BODY;
}

int raw1394_async_send(struct raw1394_handle *handle               ,
                             size_t length, size_t header_length, unsigned int expect_response,
                             quadlet_t *data)
{
        SYNCFUNC_VARS;

        err = raw1394_start_async_send(handle, length, header_length, expect_response,
                                      data, (unsigned long)&rh);

        SYNCFUNC_BODY;
}



int raw1394_start_phy_packet_write(struct raw1394_handle *handle, 
	quadlet_t data, unsigned long tag)
{
        struct raw1394_request *req = &handle->req;

        CLEAR_REQ(req);

        req->type = RAW1394_REQ_PHYPACKET;
        req->generation = handle->generation;
        req->tag = tag;

        req->sendb = data;

        return (int)write(handle->fd, req, sizeof(*req));
}

int raw1394_phy_packet_write (struct raw1394_handle *handle, quadlet_t data)
{
        SYNCFUNC_VARS;

        err = raw1394_start_phy_packet_write(handle, data, (unsigned long)&rh);

        SYNCFUNC_BODY; /* return 0 on success */
}

int raw1394_echo_request(struct raw1394_handle *handle, quadlet_t data)
{
        struct raw1394_request *req = &handle->req;
        int retval=0;

        CLEAR_REQ(req);

        req->type = RAW1394_REQ_ECHO;
        req->misc = data;

        retval = (int)write(handle->fd, req, sizeof(*req));
        if (retval == sizeof(*req)) {
                return 0; /* succcess */
	}
	return -1;
}	

int raw1394_wake_up(raw1394handle_t handle)
{
	return raw1394_echo_request(handle, 0);
}

#undef SYNCFUNC_VARS
#undef SYNCFUNC_BODY