xref: /haiku/src/add-ons/kernel/bus_managers/usb/Transfer.cpp (revision 3e216965baa8d58a67bf7372e2bfa13d999f5a9d)
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 	if (fVector && fVector != &fData)
31 		delete[] fVector;
32 }
33 
34 
35 void
36 Transfer::SetRequestData(usb_request_data *data)
37 {
38 	fRequestData = data;
39 }
40 
41 
42 void
43 Transfer::SetData(uint8 *data, size_t dataLength)
44 {
45 	fData.iov_base = data;
46 	fData.iov_len = dataLength;
47 
48 	if (data && dataLength > 0)
49 		fVectorCount = 1;
50 }
51 
52 
53 void
54 Transfer::SetVector(iovec *vector, size_t vectorCount)
55 {
56 	fVector = new(std::nothrow) iovec[vectorCount];
57 	memcpy(fVector, vector, vectorCount * sizeof(iovec));
58 	fVectorCount = vectorCount;
59 }
60 
61 
62 size_t
63 Transfer::VectorLength()
64 {
65 	size_t length = 0;
66 	for (size_t i = 0; i < fVectorCount; i++)
67 		length += fVector[i].iov_len;
68 	return length;
69 }
70 
71 
72 void
73 Transfer::SetCallback(usb_callback_func callback, void *cookie)
74 {
75 	fCallback = callback;
76 	fCallbackCookie = cookie;
77 }
78 
79 
80 void
81 Transfer::Finished(uint32 status, size_t actualLength)
82 {
83 	if (fCallback)
84 		fCallback(fCallbackCookie, status, fVector[0].iov_base, actualLength);
85 }
86