xref: /haiku/src/add-ons/kernel/bus_managers/usb/Pipe.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2004-2020, 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_private.h"
11 
12 
13 Pipe::Pipe(Object *parent)
14 	:	Object(parent),
15 		fDataToggle(false),
16 		fControllerCookie(NULL)
17 {
18 	// all other init is to be done in InitCommon()
19 }
20 
21 
22 Pipe::~Pipe()
23 {
24 	PutUSBID();
25 
26 	Pipe::CancelQueuedTransfers(true);
27 	GetBusManager()->NotifyPipeChange(this, USB_CHANGE_DESTROYED);
28 }
29 
30 
31 void
32 Pipe::InitCommon(int8 deviceAddress, uint8 endpointAddress, usb_speed speed,
33 	pipeDirection direction, size_t maxPacketSize, uint8 interval,
34 	int8 hubAddress, uint8 hubPort)
35 {
36 	fDeviceAddress = deviceAddress;
37 	fEndpointAddress = endpointAddress;
38 	fSpeed = speed;
39 	fDirection = direction;
40 	fMaxPacketSize = maxPacketSize;
41 	fInterval = interval;
42 	fHubAddress = hubAddress;
43 	fHubPort = hubPort;
44 
45 	fMaxBurst = 0;
46 	fBytesPerInterval = 0;
47 
48 	GetBusManager()->NotifyPipeChange(this, USB_CHANGE_CREATED);
49 }
50 
51 
52 void
53 Pipe::InitSuperSpeed(uint8 maxBurst, uint16 bytesPerInterval)
54 {
55 	fMaxBurst = maxBurst;
56 	fBytesPerInterval = bytesPerInterval;
57 }
58 
59 
60 void
61 Pipe::SetHubInfo(int8 address, uint8 port)
62 {
63 	fHubAddress = address;
64 	fHubPort = port;
65 }
66 
67 
68 status_t
69 Pipe::SubmitTransfer(Transfer *transfer)
70 {
71 	if (USBID() == UINT32_MAX)
72 		return B_NO_INIT;
73 
74 	// ToDo: keep track of all submited transfers to be able to cancel them
75 	return GetBusManager()->SubmitTransfer(transfer);
76 }
77 
78 
79 status_t
80 Pipe::CancelQueuedTransfers(bool force)
81 {
82 	return GetBusManager()->CancelQueuedTransfers(this, force);
83 }
84 
85 
86 status_t
87 Pipe::SetFeature(uint16 selector)
88 {
89 	TRACE("set feature %u\n", selector);
90 	return ((Device *)Parent())->DefaultPipe()->SendRequest(
91 		USB_REQTYPE_STANDARD | USB_REQTYPE_ENDPOINT_OUT,
92 		USB_REQUEST_SET_FEATURE,
93 		selector,
94 		fEndpointAddress | (fDirection == In ? USB_ENDPOINT_ADDR_DIR_IN
95 			: USB_ENDPOINT_ADDR_DIR_OUT),
96 		0,
97 		NULL,
98 		0,
99 		NULL);
100 }
101 
102 
103 status_t
104 Pipe::ClearFeature(uint16 selector)
105 {
106 	// clearing a stalled condition resets the data toggle
107 	if (selector == USB_FEATURE_ENDPOINT_HALT)
108 		SetDataToggle(false);
109 
110 	TRACE("clear feature %u\n", selector);
111 	return ((Device *)Parent())->DefaultPipe()->SendRequest(
112 		USB_REQTYPE_STANDARD | USB_REQTYPE_ENDPOINT_OUT,
113 		USB_REQUEST_CLEAR_FEATURE,
114 		selector,
115 		fEndpointAddress | (fDirection == In ? USB_ENDPOINT_ADDR_DIR_IN
116 			: USB_ENDPOINT_ADDR_DIR_OUT),
117 		0,
118 		NULL,
119 		0,
120 		NULL);
121 }
122 
123 
124 status_t
125 Pipe::GetStatus(uint16 *status)
126 {
127 	TRACE("get status\n");
128 	return ((Device *)Parent())->DefaultPipe()->SendRequest(
129 		USB_REQTYPE_STANDARD | USB_REQTYPE_ENDPOINT_IN,
130 		USB_REQUEST_GET_STATUS,
131 		0,
132 		fEndpointAddress | (fDirection == In ? USB_ENDPOINT_ADDR_DIR_IN
133 			: USB_ENDPOINT_ADDR_DIR_OUT),
134 		2,
135 		(void *)status,
136 		2,
137 		NULL);
138 }
139 
140 
141 //
142 // #pragma mark -
143 //
144 
145 
146 InterruptPipe::InterruptPipe(Object *parent)
147 	:	Pipe(parent)
148 {
149 }
150 
151 
152 status_t
153 InterruptPipe::QueueInterrupt(void *data, size_t dataLength,
154 	usb_callback_func callback, void *callbackCookie)
155 {
156 	if (dataLength > 0 && data == NULL)
157 		return B_BAD_VALUE;
158 
159 	Transfer *transfer = new(std::nothrow) Transfer(this);
160 	if (!transfer)
161 		return B_NO_MEMORY;
162 
163 	transfer->SetData((uint8 *)data, dataLength);
164 	transfer->SetCallback(callback, callbackCookie);
165 
166 	status_t result = GetBusManager()->SubmitTransfer(transfer);
167 	if (result < B_OK)
168 		delete transfer;
169 	return result;
170 }
171 
172 
173 //
174 // #pragma mark -
175 //
176 
177 
178 BulkPipe::BulkPipe(Object *parent)
179 	:	Pipe(parent)
180 {
181 }
182 
183 
184 void
185 BulkPipe::InitCommon(int8 deviceAddress, uint8 endpointAddress,
186 	usb_speed speed, pipeDirection direction, size_t maxPacketSize,
187 	uint8 interval, int8 hubAddress, uint8 hubPort)
188 {
189 	// See comments in ControlPipe::InitCommon.
190 	switch (speed) {
191 		case USB_SPEED_HIGHSPEED:
192 			maxPacketSize = 512;
193 			break;
194 		case USB_SPEED_SUPERSPEED:
195 			maxPacketSize = 1024;
196 			break;
197 
198 		default:
199 			break;
200 	}
201 
202 	Pipe::InitCommon(deviceAddress, endpointAddress, speed, direction,
203 		maxPacketSize, interval, hubAddress, hubPort);
204 }
205 
206 
207 status_t
208 BulkPipe::QueueBulk(void *data, size_t dataLength, usb_callback_func callback,
209 	void *callbackCookie)
210 {
211 	if (dataLength > 0 && data == NULL)
212 		return B_BAD_VALUE;
213 
214 	Transfer *transfer = new(std::nothrow) Transfer(this);
215 	if (!transfer)
216 		return B_NO_MEMORY;
217 
218 	transfer->SetData((uint8 *)data, dataLength);
219 	transfer->SetCallback(callback, callbackCookie);
220 
221 	status_t result = GetBusManager()->SubmitTransfer(transfer);
222 	if (result < B_OK)
223 		delete transfer;
224 	return result;
225 }
226 
227 
228 status_t
229 BulkPipe::QueueBulkV(iovec *vector, size_t vectorCount,
230 	usb_callback_func callback, void *callbackCookie, bool physical)
231 {
232 	if (vectorCount > 0 && vector == NULL)
233 		return B_BAD_VALUE;
234 
235 	Transfer *transfer = new(std::nothrow) Transfer(this);
236 	if (!transfer)
237 		return B_NO_MEMORY;
238 
239 	transfer->SetPhysical(physical);
240 	transfer->SetVector(vector, vectorCount);
241 	transfer->SetCallback(callback, callbackCookie);
242 
243 	status_t result = GetBusManager()->SubmitTransfer(transfer);
244 	if (result < B_OK)
245 		delete transfer;
246 	return result;
247 }
248 
249 
250 //
251 // #pragma mark -
252 //
253 
254 
255 IsochronousPipe::IsochronousPipe(Object *parent)
256 	:	Pipe(parent),
257 		fMaxQueuedPackets(0),
258 		fMaxBufferDuration(0),
259 		fSampleSize(0)
260 {
261 }
262 
263 
264 status_t
265 IsochronousPipe::QueueIsochronous(void *data, size_t dataLength,
266 	usb_iso_packet_descriptor *packetDesc, uint32 packetCount,
267 	uint32 *startingFrameNumber, uint32 flags, usb_callback_func callback,
268 	void *callbackCookie)
269 {
270 	if ((dataLength > 0 && data == NULL)
271 		|| (packetCount > 0 && packetDesc == NULL))
272 		return B_BAD_VALUE;
273 
274 	usb_isochronous_data *isochronousData
275 		= new(std::nothrow) usb_isochronous_data;
276 
277 	if (!isochronousData)
278 		return B_NO_MEMORY;
279 
280 	isochronousData->packet_descriptors = packetDesc;
281 	isochronousData->packet_count = packetCount;
282 	isochronousData->starting_frame_number = startingFrameNumber;
283 	isochronousData->flags = flags;
284 
285 	Transfer *transfer = new(std::nothrow) Transfer(this);
286 	if (!transfer) {
287 		delete isochronousData;
288 		return B_NO_MEMORY;
289 	}
290 
291 	transfer->SetData((uint8 *)data, dataLength);
292 	transfer->SetCallback(callback, callbackCookie);
293 	transfer->SetIsochronousData(isochronousData);
294 
295 	status_t result = GetBusManager()->SubmitTransfer(transfer);
296 	if (result < B_OK)
297 		delete transfer;
298 	return result;
299 }
300 
301 
302 status_t
303 IsochronousPipe::SetPipePolicy(uint8 maxQueuedPackets,
304 	uint16 maxBufferDurationMS, uint16 sampleSize)
305 {
306 	if (maxQueuedPackets == fMaxQueuedPackets
307 		|| maxBufferDurationMS == fMaxBufferDuration
308 		|| sampleSize == fSampleSize)
309 		return B_OK;
310 
311 	fMaxQueuedPackets = maxQueuedPackets;
312 	fMaxBufferDuration = maxBufferDurationMS;
313 	fSampleSize = sampleSize;
314 
315 	GetBusManager()->NotifyPipeChange(this, USB_CHANGE_PIPE_POLICY_CHANGED);
316 	return B_OK;
317 }
318 
319 
320 status_t
321 IsochronousPipe::GetPipePolicy(uint8 *maxQueuedPackets,
322 	uint16 *maxBufferDurationMS, uint16 *sampleSize)
323 {
324 	if (maxQueuedPackets)
325 		*maxQueuedPackets = fMaxQueuedPackets;
326 	if (maxBufferDurationMS)
327 		*maxBufferDurationMS = fMaxBufferDuration;
328 	if (sampleSize)
329 		*sampleSize = fSampleSize;
330 	return B_OK;
331 }
332 
333 
334 //
335 // #pragma mark -
336 //
337 
338 
339 ControlPipe::ControlPipe(Object *parent)
340 	:	Pipe(parent),
341 		fNotifySem(-1)
342 {
343 	mutex_init(&fSendRequestLock, "control pipe send request");
344 }
345 
346 
347 ControlPipe::~ControlPipe()
348 {
349 	// We do this here in case a submitted request is still running.
350 	PutUSBID(false);
351 	ControlPipe::CancelQueuedTransfers(true);
352 	WaitForUnbusy();
353 
354 	if (fNotifySem >= 0)
355 		delete_sem(fNotifySem);
356 	mutex_lock(&fSendRequestLock);
357 	mutex_destroy(&fSendRequestLock);
358 }
359 
360 
361 void
362 ControlPipe::InitCommon(int8 deviceAddress, uint8 endpointAddress,
363 	usb_speed speed, pipeDirection direction, size_t maxPacketSize,
364 	uint8 interval, int8 hubAddress, uint8 hubPort)
365 {
366 	// The USB 2.0 spec section 5.5.3 gives fixed max packet sizes for the
367 	// different speeds. The USB 3.1 specs defines some fixed max packet sizes,
368 	// including for control endpoints in 9.6.6. Some devices ignore these
369 	// values and use bogus ones, so we restrict them here.
370 	switch (speed) {
371 		case USB_SPEED_LOWSPEED:
372 			maxPacketSize = 8;
373 			break;
374 		case USB_SPEED_HIGHSPEED:
375 			maxPacketSize = 64;
376 			break;
377 		case USB_SPEED_SUPERSPEED:
378 			maxPacketSize = 512;
379 			break;
380 
381 		default:
382 			break;
383 	}
384 
385 	Pipe::InitCommon(deviceAddress, endpointAddress, speed, direction,
386 		maxPacketSize, interval, hubAddress, hubPort);
387 }
388 
389 
390 status_t
391 ControlPipe::SendRequest(uint8 requestType, uint8 request, uint16 value,
392 	uint16 index, uint16 length, void *data, size_t dataLength,
393 	size_t *actualLength)
394 {
395 	status_t result = mutex_lock(&fSendRequestLock);
396 	if (result != B_OK)
397 		return result;
398 
399 	if (fNotifySem < 0) {
400 		fNotifySem = create_sem(0, "usb send request notify");
401 		if (fNotifySem < 0) {
402 			mutex_unlock(&fSendRequestLock);
403 			return B_NO_MORE_SEMS;
404 		}
405 	}
406 
407 	result = QueueRequest(requestType, request, value, index, length, data,
408 		dataLength, SendRequestCallback, this);
409 	if (result < B_OK) {
410 		mutex_unlock(&fSendRequestLock);
411 		return result;
412 	}
413 
414 	// The sem will be released unconditionally in the callback after the
415 	// result data was filled in. Use a 2 seconds timeout for control transfers.
416 	if (acquire_sem_etc(fNotifySem, 1, B_RELATIVE_TIMEOUT, 2000000) < B_OK) {
417 		TRACE_ERROR("timeout waiting for queued request to complete\n");
418 
419 		CancelQueuedTransfers(false);
420 
421 		// After the above cancel returns it is guaranteed that the callback
422 		// has been invoked. Therefore we can simply grab that released
423 		// semaphore again to clean up.
424 		acquire_sem(fNotifySem);
425 
426 		if (actualLength)
427 			*actualLength = 0;
428 
429 		mutex_unlock(&fSendRequestLock);
430 		return B_TIMED_OUT;
431 	}
432 
433 	if (actualLength)
434 		*actualLength = fActualLength;
435 
436 	mutex_unlock(&fSendRequestLock);
437 	return fTransferStatus;
438 }
439 
440 
441 void
442 ControlPipe::SendRequestCallback(void *cookie, status_t status, void *data,
443 	size_t actualLength)
444 {
445 	ControlPipe *pipe = (ControlPipe *)cookie;
446 	pipe->fTransferStatus = status;
447 	pipe->fActualLength = actualLength;
448 	release_sem(pipe->fNotifySem);
449 }
450 
451 
452 status_t
453 ControlPipe::QueueRequest(uint8 requestType, uint8 request, uint16 value,
454 	uint16 index, uint16 length, void *data, size_t dataLength,
455 	usb_callback_func callback, void *callbackCookie)
456 {
457 	if (dataLength > 0 && data == NULL)
458 		return B_BAD_VALUE;
459 
460 	if (USBID() == UINT32_MAX)
461 		return B_NO_INIT;
462 
463 	usb_request_data *requestData = new(std::nothrow) usb_request_data;
464 	if (!requestData)
465 		return B_NO_MEMORY;
466 
467 	requestData->RequestType = requestType;
468 	requestData->Request = request;
469 	requestData->Value = value;
470 	requestData->Index = index;
471 	requestData->Length = length;
472 
473 	Transfer *transfer = new(std::nothrow) Transfer(this);
474 	if (!transfer) {
475 		delete requestData;
476 		return B_NO_MEMORY;
477 	}
478 
479 	transfer->SetRequestData(requestData);
480 	transfer->SetData((uint8 *)data, dataLength);
481 	transfer->SetCallback(callback, callbackCookie);
482 
483 	status_t result = GetBusManager()->SubmitTransfer(transfer);
484 	if (result < B_OK)
485 		delete transfer;
486 	return result;
487 }
488 
489 
490 status_t
491 ControlPipe::CancelQueuedTransfers(bool force)
492 {
493 	if (force && fNotifySem >= 0) {
494 		// There is likely a transfer currently running; we need to cancel it
495 		// manually, as callbacks are not invoked when force-cancelling.
496 		fTransferStatus = B_CANCELED;
497 		fActualLength = 0;
498 		release_sem_etc(fNotifySem, 1, B_RELEASE_IF_WAITING_ONLY);
499 	}
500 
501 	return Pipe::CancelQueuedTransfers(force);
502 }
503