18a690c49SJérôme Duval/*
28a690c49SJérôme Duval * Copyright 2006-2013, J��r��me Duval. All rights reserved.
38a690c49SJérôme Duval * Copyright 2011-2012, Fredrik Holmqvis. All rights reserved.
48a690c49SJérôme Duval * Copyright 2008, Stefano Ceccherini. All rights reserved.
58a690c49SJérôme Duval * Copyright 2006, Bryan Varner. All rights reserved.
68a690c49SJérôme Duval * Distributed under the terms of the MIT License.
78a690c49SJérôme Duval */
88a690c49SJérôme Duval
98f217450SStefano Ceccherini
108f217450SStefano Ceccherini#include <stdio.h>
118f217450SStefano Ceccherini#include <stdlib.h>
128f217450SStefano Ceccherini#include <string.h>
138a690c49SJérôme Duval#include <unistd.h>
148f217450SStefano Ceccherini
158a690c49SJérôme Duval#include <Drivers.h>
168f217450SStefano Ceccherini
178f217450SStefano Ceccherini#include <util/kernel_cpp.h>
188f217450SStefano Ceccherini#include <util/ring_buffer.h>
198f217450SStefano Ceccherini
208a690c49SJérôme Duval#include "ACPIPrivate.h"
218a690c49SJérôme Duval
228a690c49SJérôme Duval
238f217450SStefano Ceccheriniclass RingBuffer {
248f217450SStefano Ceccherinipublic:
258f217450SStefano Ceccherini	RingBuffer(size_t size = 1024);
26cad6c2c5SFredrik Holmqvist	~RingBuffer();
27b44c75c5SFredrik Holmqvist	size_t Read(void *buffer, ssize_t length);
28b44c75c5SFredrik Holmqvist	size_t Write(const void *buffer, ssize_t length);
298f217450SStefano Ceccherini	size_t WritableAmount() const;
308f217450SStefano Ceccherini	size_t ReadableAmount() const;
318f217450SStefano Ceccherini
328f217450SStefano Ceccherini	bool Lock();
338f217450SStefano Ceccherini	void Unlock();
346fde2584SFredrik Holmqvist	void DestroyLock();
358f217450SStefano Ceccheriniprivate:
368f217450SStefano Ceccherini	ring_buffer *fBuffer;
378f217450SStefano Ceccherini	sem_id fLock;
388f217450SStefano Ceccherini};
398f217450SStefano Ceccherini
408f217450SStefano Ceccherini
418f217450SStefano Ceccherinitypedef struct acpi_ns_device_info {
428f217450SStefano Ceccherini	device_node *node;
438f217450SStefano Ceccherini	acpi_root_info	*acpi;
448f217450SStefano Ceccherini	void	*acpi_cookie;
458f217450SStefano Ceccherini	thread_id thread;
468f217450SStefano Ceccherini	sem_id read_sem;
478f217450SStefano Ceccherini	RingBuffer *buffer;
488f217450SStefano Ceccherini} acpi_ns_device_info;
498f217450SStefano Ceccherini
508f217450SStefano Ceccherini
518f217450SStefano Ceccherini
528f217450SStefano Ceccherini// called with the buffer lock held
538f217450SStefano Ceccherinistatic bool
548f217450SStefano Ceccherinimake_space(acpi_ns_device_info *device, size_t space)
558f217450SStefano Ceccherini{
568f217450SStefano Ceccherini	size_t available = device->buffer->WritableAmount();
578f217450SStefano Ceccherini	if (space <= available)
588f217450SStefano Ceccherini		return true;
598f217450SStefano Ceccherini	bool released = false;
608f217450SStefano Ceccherini	do {
618f217450SStefano Ceccherini		device->buffer->Unlock();
628f217450SStefano Ceccherini
638f217450SStefano Ceccherini		if (!released) {
648f217450SStefano Ceccherini			if (release_sem_etc(device->read_sem, 1, B_RELEASE_IF_WAITING_ONLY) == B_OK)
658f217450SStefano Ceccherini				released = true;
668f217450SStefano Ceccherini		}
678f217450SStefano Ceccherini		snooze(10000);
688f217450SStefano Ceccherini
69cad6c2c5SFredrik Holmqvist		if (!device->buffer->Lock())
708f217450SStefano Ceccherini			return false;
71cad6c2c5SFredrik Holmqvist
728f217450SStefano Ceccherini	} while (device->buffer->WritableAmount() < space);
73cad6c2c5SFredrik Holmqvist
748f217450SStefano Ceccherini	return true;
75cad6c2c5SFredrik Holmqvist}
768f217450SStefano Ceccherini
778f217450SStefano Ceccherini
788f217450SStefano Ceccherini
79cad6c2c5SFredrik Holmqviststatic void
80cad6c2c5SFredrik Holmqvistdump_acpi_namespace(acpi_ns_device_info *device, char *root, int indenting)
818f217450SStefano Ceccherini{
828f217450SStefano Ceccherini	char result[255];
838f217450SStefano Ceccherini	char output[320];
84176ecb2fSStefano Ceccherini	char tabs[255] = "";
85176ecb2fSStefano Ceccherini	char hid[16] = "";
868f217450SStefano Ceccherini	int i;
878f217450SStefano Ceccherini	size_t written = 0;
88cad6c2c5SFredrik Holmqvist	for (i = 0; i < indenting; i++)
898f217450SStefano Ceccherini		strlcat(tabs, "|    ", sizeof(tabs));
908f217450SStefano Ceccherini
918f217450SStefano Ceccherini	strlcat(tabs, "|--- ", sizeof(tabs));
928f217450SStefano Ceccherini
938f217450SStefano Ceccherini	int depth = sizeof(char) * 5 * indenting + sizeof(char); // index into result where the device name will be.
94cad6c2c5SFredrik Holmqvist
958f217450SStefano Ceccherini	void *counter = NULL;
968f217450SStefano Ceccherini	while (device->acpi->get_next_entry(ACPI_TYPE_ANY, root, result, 255, &counter) == B_OK) {
978f217450SStefano Ceccherini		uint32 type = device->acpi->get_object_type(result);
988f217450SStefano Ceccherini		snprintf(output, sizeof(output), "%s%s", tabs, result + depth);
998f217450SStefano Ceccherini		switch(type) {
1008f217450SStefano Ceccherini			case ACPI_TYPE_INTEGER:
10185965abaSAdrien Destugues				strlcat(output, "     INTEGER", sizeof(output));
1028f217450SStefano Ceccherini				break;
1038f217450SStefano Ceccherini			case ACPI_TYPE_STRING:
10485965abaSAdrien Destugues				strlcat(output, "     STRING", sizeof(output));
1058f217450SStefano Ceccherini				break;
1068f217450SStefano Ceccherini			case ACPI_TYPE_BUFFER:
10785965abaSAdrien Destugues				strlcat(output, "     BUFFER", sizeof(output));
1088f217450SStefano Ceccherini				break;
1098f217450SStefano Ceccherini			case ACPI_TYPE_PACKAGE:
11085965abaSAdrien Destugues				strlcat(output, "     PACKAGE", sizeof(output));
1118f217450SStefano Ceccherini				break;
1128f217450SStefano Ceccherini			case ACPI_TYPE_FIELD_UNIT:
11385965abaSAdrien Destugues				strlcat(output, "     FIELD UNIT", sizeof(output));
1148f217450SStefano Ceccherini				break;
1158f217450SStefano Ceccherini			case ACPI_TYPE_DEVICE:
1168acabe5aSIthamar R. Adema				hid[0] = 0; /* zero-terminate string; get_device_hid can (and will) fail! */
117c1f94990SStefano Ceccherini				device->acpi->get_device_hid(result, hid, sizeof(hid));
11885965abaSAdrien Destugues				strlcat(output, "     DEVICE (", sizeof(output));
11985965abaSAdrien Destugues				strlcat(output, hid, sizeof(output));
12085965abaSAdrien Destugues				strlcat(output, ")", sizeof(output));
1218f217450SStefano Ceccherini				break;
1228f217450SStefano Ceccherini			case ACPI_TYPE_EVENT:
12385965abaSAdrien Destugues				strlcat(output, "     EVENT", sizeof(output));
1248f217450SStefano Ceccherini				break;
1258f217450SStefano Ceccherini			case ACPI_TYPE_METHOD:
12685965abaSAdrien Destugues				strlcat(output, "     METHOD", sizeof(output));
1278f217450SStefano Ceccherini				break;
1288f217450SStefano Ceccherini			case ACPI_TYPE_MUTEX:
12985965abaSAdrien Destugues				strlcat(output, "     MUTEX", sizeof(output));
1308f217450SStefano Ceccherini				break;
1318f217450SStefano Ceccherini			case ACPI_TYPE_REGION:
13285965abaSAdrien Destugues				strlcat(output, "     REGION", sizeof(output));
1338f217450SStefano Ceccherini				break;
1348f217450SStefano Ceccherini			case ACPI_TYPE_POWER:
13585965abaSAdrien Destugues				strlcat(output, "     POWER", sizeof(output));
1368f217450SStefano Ceccherini				break;
1378f217450SStefano Ceccherini			case ACPI_TYPE_PROCESSOR:
13885965abaSAdrien Destugues				strlcat(output, "     PROCESSOR", sizeof(output));
1398f217450SStefano Ceccherini				break;
1408f217450SStefano Ceccherini			case ACPI_TYPE_THERMAL:
14185965abaSAdrien Destugues				strlcat(output, "     THERMAL", sizeof(output));
1428f217450SStefano Ceccherini				break;
1438f217450SStefano Ceccherini			case ACPI_TYPE_BUFFER_FIELD:
14485965abaSAdrien Destugues				strlcat(output, "     BUFFER_FIELD", sizeof(output));
1458f217450SStefano Ceccherini				break;
1468f217450SStefano Ceccherini			case ACPI_TYPE_ANY:
1478f217450SStefano Ceccherini			default:
1488f217450SStefano Ceccherini				break;
1498f217450SStefano Ceccherini		}
1508f217450SStefano Ceccherini		written = 0;
1518f217450SStefano Ceccherini		RingBuffer &ringBuffer = *device->buffer;
1528f217450SStefano Ceccherini		size_t toWrite = strlen(output);
153cad6c2c5SFredrik Holmqvist
154cc9a637eSViktor Müntzing		if (toWrite == 0)
1556fde2584SFredrik Holmqvist			break;
1568f217450SStefano Ceccherini
157cc9a637eSViktor Müntzing		toWrite = strlcat(output, "\n", sizeof(output));
158cc9a637eSViktor Müntzing
159cad6c2c5SFredrik Holmqvist		if (!ringBuffer.Lock())
1606fde2584SFredrik Holmqvist			break;
1616fde2584SFredrik Holmqvist
1626fde2584SFredrik Holmqvist		if (ringBuffer.WritableAmount() < toWrite &&
1636fde2584SFredrik Holmqvist			!make_space(device, toWrite))
1646fde2584SFredrik Holmqvist			break;
1656fde2584SFredrik Holmqvist
1666fde2584SFredrik Holmqvist		written = ringBuffer.Write(output, toWrite);
1676fde2584SFredrik Holmqvist		ringBuffer.Unlock();
1686fde2584SFredrik Holmqvist		dump_acpi_namespace(device, result, indenting + 1);
1698f217450SStefano Ceccherini	}
1708f217450SStefano Ceccherini}
1718f217450SStefano Ceccherini
1728f217450SStefano Ceccherini
1738f217450SStefano Ceccherinistatic int32
1748f217450SStefano Ceccheriniacpi_namespace_dump(void *arg)
1758f217450SStefano Ceccherini{
1768f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info*)(arg);
1778a690c49SJérôme Duval	dump_acpi_namespace(device, NULL, 0);
1788f217450SStefano Ceccherini
1798f217450SStefano Ceccherini	delete_sem(device->read_sem);
1808f217450SStefano Ceccherini	device->read_sem = -1;
1818f217450SStefano Ceccherini
1828f217450SStefano Ceccherini	return 0;
1838f217450SStefano Ceccherini}
1848f217450SStefano Ceccherini
1858f217450SStefano Ceccheriniextern "C" {
1868f217450SStefano Ceccherini/* ----------
1878f217450SStefano Ceccherini	acpi_namespace_open - handle open() calls
1888f217450SStefano Ceccherini----- */
1898f217450SStefano Ceccherini
1908f217450SStefano Ceccherinistatic status_t
1918f217450SStefano Ceccheriniacpi_namespace_open(void *_cookie, const char* path, int flags, void** cookie)
1928f217450SStefano Ceccherini{
1938f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info *)_cookie;
1948f217450SStefano Ceccherini
1958f217450SStefano Ceccherini	dprintf("\nacpi_ns_dump: device_open\n");
1968f217450SStefano Ceccherini
1978f217450SStefano Ceccherini	*cookie = device;
1988f217450SStefano Ceccherini
1998f217450SStefano Ceccherini	RingBuffer *ringBuffer = new RingBuffer(1024);
2008f217450SStefano Ceccherini	if (ringBuffer == NULL)
2018f217450SStefano Ceccherini		return B_NO_MEMORY;
2028f217450SStefano Ceccherini
2038f217450SStefano Ceccherini	device->read_sem = create_sem(0, "read_sem");
2048f217450SStefano Ceccherini	if (device->read_sem < B_OK) {
2058f217450SStefano Ceccherini		delete ringBuffer;
2068f217450SStefano Ceccherini		return device->read_sem;
2078f217450SStefano Ceccherini	}
2088f217450SStefano Ceccherini
2098f217450SStefano Ceccherini	device->thread = spawn_kernel_thread(acpi_namespace_dump, "acpi dumper",
2108f217450SStefano Ceccherini		 B_NORMAL_PRIORITY, device);
2118f217450SStefano Ceccherini	if (device->thread < 0) {
2128f217450SStefano Ceccherini		delete ringBuffer;
2138f217450SStefano Ceccherini		delete_sem(device->read_sem);
2148f217450SStefano Ceccherini		return device->thread;
2158f217450SStefano Ceccherini	}
2168f217450SStefano Ceccherini
2178f217450SStefano Ceccherini	device->buffer = ringBuffer;
2188f217450SStefano Ceccherini
2198f217450SStefano Ceccherini	resume_thread(device->thread);
2208f217450SStefano Ceccherini
2218f217450SStefano Ceccherini	return B_OK;
2228f217450SStefano Ceccherini}
2238f217450SStefano Ceccherini
2248f217450SStefano Ceccherini
2258f217450SStefano Ceccherini/* ----------
2268f217450SStefano Ceccherini	acpi_namespace_read - handle read() calls
2278f217450SStefano Ceccherini----- */
2288f217450SStefano Ceccherinistatic status_t
2298f217450SStefano Ceccheriniacpi_namespace_read(void *_cookie, off_t position, void *buf, size_t* num_bytes)
2308f217450SStefano Ceccherini{
2318f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info *)_cookie;
2328f217450SStefano Ceccherini	RingBuffer &ringBuffer = *device->buffer;
2338f217450SStefano Ceccherini
234b44c75c5SFredrik Holmqvist	if (!ringBuffer.Lock()) {
235b44c75c5SFredrik Holmqvist		*num_bytes = 0;
236b44c75c5SFredrik Holmqvist		return B_ERROR;
237b44c75c5SFredrik Holmqvist	}
2388f217450SStefano Ceccherini
239b44c75c5SFredrik Holmqvist	if (ringBuffer.ReadableAmount() == 0) {
2408f217450SStefano Ceccherini		ringBuffer.Unlock();
241b44c75c5SFredrik Holmqvist		status_t status = acquire_sem_etc(device->read_sem, 1, B_CAN_INTERRUPT, 0);
2428a690c49SJérôme Duval		if (status != B_OK && status != B_BAD_SEM_ID) {
243b44c75c5SFredrik Holmqvist			*num_bytes = 0;
244b44c75c5SFredrik Holmqvist			return status;
245b44c75c5SFredrik Holmqvist		}
246b44c75c5SFredrik Holmqvist		if (!ringBuffer.Lock()) {
247b44c75c5SFredrik Holmqvist			*num_bytes = 0;
248b44c75c5SFredrik Holmqvist			return B_ERROR;
249b44c75c5SFredrik Holmqvist		}
2508f217450SStefano Ceccherini	}
2518f217450SStefano Ceccherini
252b44c75c5SFredrik Holmqvist	*num_bytes = ringBuffer.Read(buf, *num_bytes);
253b44c75c5SFredrik Holmqvist	ringBuffer.Unlock();
2548f217450SStefano Ceccherini
2558f217450SStefano Ceccherini	return B_OK;
2568f217450SStefano Ceccherini}
2578f217450SStefano Ceccherini
2588f217450SStefano Ceccherini
2598f217450SStefano Ceccherini/* ----------
2608f217450SStefano Ceccherini	acpi_namespace_write - handle write() calls
2618f217450SStefano Ceccherini----- */
2628f217450SStefano Ceccherini
2638f217450SStefano Ceccherinistatic status_t
2648f217450SStefano Ceccheriniacpi_namespace_write(void* cookie, off_t position, const void* buffer, size_t* num_bytes)
2658f217450SStefano Ceccherini{
2668f217450SStefano Ceccherini	*num_bytes = 0;				/* tell caller nothing was written */
2678f217450SStefano Ceccherini	return B_IO_ERROR;
2688f217450SStefano Ceccherini}
2698f217450SStefano Ceccherini
2708f217450SStefano Ceccherini
2718f217450SStefano Ceccherini/* ----------
2728f217450SStefano Ceccherini	acpi_namespace_control - handle ioctl calls
2738f217450SStefano Ceccherini----- */
2748f217450SStefano Ceccherini
2758f217450SStefano Ceccherinistatic status_t
2768f217450SStefano Ceccheriniacpi_namespace_control(void* cookie, uint32 op, void* arg, size_t len)
2778f217450SStefano Ceccherini{
2788f217450SStefano Ceccherini	dprintf("acpi_ns_dump: device_control\n");
279e523d3cfSJérôme Duval	return B_DEV_INVALID_IOCTL;
2808f217450SStefano Ceccherini}
2818f217450SStefano Ceccherini
2828f217450SStefano Ceccherini
2838f217450SStefano Ceccherini/* ----------
2848f217450SStefano Ceccherini	acpi_namespace_close - handle close() calls
2858f217450SStefano Ceccherini----- */
2868f217450SStefano Ceccherini
2878f217450SStefano Ceccherinistatic status_t
2888f217450SStefano Ceccheriniacpi_namespace_close(void* cookie)
2898f217450SStefano Ceccherini{
2908f217450SStefano Ceccherini	dprintf("acpi_ns_dump: device_close\n");
2918f217450SStefano Ceccherini	return B_OK;
2928f217450SStefano Ceccherini}
2938f217450SStefano Ceccherini
2948f217450SStefano Ceccherini
2958f217450SStefano Ceccherini/* -----
2968f217450SStefano Ceccherini	acpi_namespace_free - called after the last device is closed, and after
2978f217450SStefano Ceccherini	all i/o is complete.
2988f217450SStefano Ceccherini----- */
2998f217450SStefano Ceccherinistatic status_t
3008f217450SStefano Ceccheriniacpi_namespace_free(void* cookie)
3018f217450SStefano Ceccherini{
3028a690c49SJérôme Duval	status_t status;
3038a690c49SJérôme Duval	acpi_ns_device_info *device = (acpi_ns_device_info *)cookie;
3048f217450SStefano Ceccherini	dprintf("acpi_ns_dump: device_free\n");
3056fde2584SFredrik Holmqvist
3068a690c49SJérôme Duval	if (device->read_sem >= 0)
3078a690c49SJérôme Duval		delete_sem(device->read_sem);
3088a690c49SJérôme Duval
3098a690c49SJérôme Duval	device->buffer->DestroyLock();
3108a690c49SJérôme Duval	wait_for_thread(device->thread, &status);
3118a690c49SJérôme Duval	delete device->buffer;
3128a690c49SJérôme Duval
3138f217450SStefano Ceccherini	return B_OK;
3148f217450SStefano Ceccherini}
3158f217450SStefano Ceccherini
3168f217450SStefano Ceccherini
3178f217450SStefano Ceccherini//	#pragma mark - device module API
3188f217450SStefano Ceccherini
3198f217450SStefano Ceccherini
3208f217450SStefano Ceccherinistatic status_t
3218f217450SStefano Ceccheriniacpi_namespace_init_device(void *_cookie, void **cookie)
3228f217450SStefano Ceccherini{
3238f217450SStefano Ceccherini	device_node *node = (device_node *)_cookie;
3248f217450SStefano Ceccherini	status_t err;
3256fde2584SFredrik Holmqvist
3268f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info *)calloc(1, sizeof(*device));
3278f217450SStefano Ceccherini	if (device == NULL)
3288f217450SStefano Ceccherini		return B_NO_MEMORY;
3298f217450SStefano Ceccherini
3308f217450SStefano Ceccherini	device->node = node;
3318f217450SStefano Ceccherini	err = gDeviceManager->get_driver(node, (driver_module_info **)&device->acpi,
3328f217450SStefano Ceccherini		(void **)&device->acpi_cookie);
3338f217450SStefano Ceccherini	if (err != B_OK) {
3348f217450SStefano Ceccherini		free(device);
3358f217450SStefano Ceccherini		return err;
3368f217450SStefano Ceccherini	}
3376fde2584SFredrik Holmqvist
3388f217450SStefano Ceccherini	*cookie = device;
3398f217450SStefano Ceccherini	return B_OK;
3408f217450SStefano Ceccherini}
3418f217450SStefano Ceccherini
3428f217450SStefano Ceccherini
3438f217450SStefano Ceccherinistatic void
3448f217450SStefano Ceccheriniacpi_namespace_uninit_device(void *_cookie)
3458f217450SStefano Ceccherini{
3468f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info *)_cookie;
3478f217450SStefano Ceccherini	free(device);
3488f217450SStefano Ceccherini}
3498f217450SStefano Ceccherini
3508f217450SStefano Ceccherini}
3518f217450SStefano Ceccherini
3528f217450SStefano Ceccherinistruct device_module_info acpi_ns_dump_module = {
3538f217450SStefano Ceccherini	{
3548f217450SStefano Ceccherini		ACPI_NS_DUMP_DEVICE_MODULE_NAME,
3558f217450SStefano Ceccherini		0,
3568f217450SStefano Ceccherini		NULL
3578f217450SStefano Ceccherini	},
3588f217450SStefano Ceccherini
3598f217450SStefano Ceccherini	acpi_namespace_init_device,
3608f217450SStefano Ceccherini	acpi_namespace_uninit_device,
3618f217450SStefano Ceccherini	NULL,
3626fde2584SFredrik Holmqvist
3638f217450SStefano Ceccherini	acpi_namespace_open,
3648f217450SStefano Ceccherini	acpi_namespace_close,
3658f217450SStefano Ceccherini	acpi_namespace_free,
3668f217450SStefano Ceccherini	acpi_namespace_read,
3678f217450SStefano Ceccherini	acpi_namespace_write,
3688f217450SStefano Ceccherini	NULL,
3698f217450SStefano Ceccherini	acpi_namespace_control,
3708f217450SStefano Ceccherini
3718f217450SStefano Ceccherini	NULL,
3728f217450SStefano Ceccherini	NULL
3738f217450SStefano Ceccherini};
3748f217450SStefano Ceccherini
3758f217450SStefano Ceccherini
3768f217450SStefano CeccheriniRingBuffer::RingBuffer(size_t size)
3778f217450SStefano Ceccherini{
3788f217450SStefano Ceccherini	fBuffer = create_ring_buffer(size);
3798f217450SStefano Ceccherini	fLock = create_sem(1, "ring buffer lock");
3808f217450SStefano Ceccherini}
3818f217450SStefano Ceccherini
3828f217450SStefano Ceccherini
3838f217450SStefano CeccheriniRingBuffer::~RingBuffer()
3848f217450SStefano Ceccherini{
3858f217450SStefano Ceccherini	delete_ring_buffer(fBuffer);
3868f217450SStefano Ceccherini}
3878f217450SStefano Ceccherini
3888f217450SStefano Ceccherini
389b44c75c5SFredrik Holmqvistsize_t
390b44c75c5SFredrik HolmqvistRingBuffer::Read(void *buffer, ssize_t size)
3918f217450SStefano Ceccherini{
3928f217450SStefano Ceccherini	return ring_buffer_read(fBuffer, (uint8*)buffer, size);
3938f217450SStefano Ceccherini}
3948f217450SStefano Ceccherini
3958f217450SStefano Ceccherini
396b44c75c5SFredrik Holmqvistsize_t
397b44c75c5SFredrik HolmqvistRingBuffer::Write(const void *buffer, ssize_t size)
3988f217450SStefano Ceccherini{
3998f217450SStefano Ceccherini	return ring_buffer_write(fBuffer, (uint8*)buffer, size);
4008f217450SStefano Ceccherini}
4018f217450SStefano Ceccherini
4028f217450SStefano Ceccherini
4038f217450SStefano Ceccherinisize_t
4048f217450SStefano CeccheriniRingBuffer::ReadableAmount() const
4058f217450SStefano Ceccherini{
4068f217450SStefano Ceccherini	return ring_buffer_readable(fBuffer);
4078f217450SStefano Ceccherini}
4088f217450SStefano Ceccherini
4098f217450SStefano Ceccherini
4108f217450SStefano Ceccherinisize_t
4118f217450SStefano CeccheriniRingBuffer::WritableAmount() const
4128f217450SStefano Ceccherini{
4138f217450SStefano Ceccherini	return ring_buffer_writable(fBuffer);
4148f217450SStefano Ceccherini}
4158f217450SStefano Ceccherini
4168f217450SStefano Ceccherini
4178f217450SStefano Ceccherinibool
4188f217450SStefano CeccheriniRingBuffer::Lock()
4198f217450SStefano Ceccherini{
4208f217450SStefano Ceccherini	//status_t status = acquire_sem_etc(fLock, 1, B_CAN_INTERRUPT, 0);
4218f217450SStefano Ceccherini	status_t status = acquire_sem(fLock);
4228f217450SStefano Ceccherini	return status == B_OK;
4238f217450SStefano Ceccherini}
4248f217450SStefano Ceccherini
4258f217450SStefano Ceccherini
4268f217450SStefano Ceccherinivoid
4278f217450SStefano CeccheriniRingBuffer::Unlock()
4288f217450SStefano Ceccherini{
4298f217450SStefano Ceccherini	release_sem(fLock);
4308f217450SStefano Ceccherini}
4318f217450SStefano Ceccherini
4328f217450SStefano Ceccherini
4336fde2584SFredrik Holmqvistvoid
4346fde2584SFredrik HolmqvistRingBuffer::DestroyLock()
4356fde2584SFredrik Holmqvist{
4366fde2584SFredrik Holmqvist	delete_sem(fLock);
4376fde2584SFredrik Holmqvist}
4388f217450SStefano Ceccherini
439