xref: /haiku/src/add-ons/kernel/bus_managers/usb/Transfer.cpp (revision 9ecf9d1c1d4888d341a6eac72112c72d1ae3a4cb)
1 /*
2  * Copyright 2003-2006, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Lotz <mmlr@mlotz.ch>
7  *		Niels S. Reedijk
8  */
9 
10 #include "usb_p.h"
11 
12 
13 Transfer::Transfer(Pipe *pipe)
14 	:	fPipe(pipe),
15 		fVector(&fData),
16 		fVectorCount(0),
17 		fCallback(NULL),
18 		fCallbackCookie(NULL),
19 		fRequestData(NULL)
20 {
21 }
22 
23 
24 Transfer::~Transfer()
25 {
26 	// we take ownership of the request data
27 	if (fRequestData)
28 		delete fRequestData;
29 }
30 
31 
32 void
33 Transfer::SetRequestData(usb_request_data *data)
34 {
35 	fRequestData = data;
36 }
37 
38 
39 void
40 Transfer::SetData(uint8 *data, size_t dataLength)
41 {
42 	fData.iov_base = data;
43 	fData.iov_len = dataLength;
44 
45 	if (data && dataLength > 0)
46 		fVectorCount = 1;
47 }
48 
49 
50 void
51 Transfer::SetVector(iovec *vector, size_t vectorCount)
52 {
53 	fVector = vector;
54 	fVectorCount = vectorCount;
55 }
56 
57 
58 size_t
59 Transfer::VectorLength()
60 {
61 	size_t length = 0;
62 	for (size_t i = 0; i < fVectorCount; i++)
63 		length += fVector[i].iov_len;
64 	return length;
65 }
66 
67 
68 void
69 Transfer::SetCallback(usb_callback_func callback, void *cookie)
70 {
71 	fCallback = callback;
72 	fCallbackCookie = cookie;
73 }
74 
75 
76 void
77 Transfer::Finished(uint32 status, size_t actualLength)
78 {
79 	if (fCallback)
80 		fCallback(fCallbackCookie, status, fVector[0].iov_base, actualLength);
81 }
82