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
1748a00c0fSAugustin Cavalier#include <kernel.h>
188f217450SStefano Ceccherini#include <util/kernel_cpp.h>
198f217450SStefano Ceccherini#include <util/ring_buffer.h>
208f217450SStefano Ceccherini
218a690c49SJérôme Duval#include "ACPIPrivate.h"
228a690c49SJérôme Duval
238a690c49SJérôme Duval
248f217450SStefano Ceccheriniclass RingBuffer {
258f217450SStefano Ceccherinipublic:
268f217450SStefano Ceccherini	RingBuffer(size_t size = 1024);
27cad6c2c5SFredrik Holmqvist	~RingBuffer();
28b44c75c5SFredrik Holmqvist	size_t Read(void *buffer, ssize_t length);
29b44c75c5SFredrik Holmqvist	size_t Write(const void *buffer, ssize_t length);
308f217450SStefano Ceccherini	size_t WritableAmount() const;
318f217450SStefano Ceccherini	size_t ReadableAmount() const;
328f217450SStefano Ceccherini
338f217450SStefano Ceccherini	bool Lock();
348f217450SStefano Ceccherini	void Unlock();
356fde2584SFredrik Holmqvist	void DestroyLock();
368f217450SStefano Ceccheriniprivate:
378f217450SStefano Ceccherini	ring_buffer *fBuffer;
388f217450SStefano Ceccherini	sem_id fLock;
398f217450SStefano Ceccherini};
408f217450SStefano Ceccherini
418f217450SStefano Ceccherini
428f217450SStefano Ceccherinitypedef struct acpi_ns_device_info {
438f217450SStefano Ceccherini	device_node *node;
448f217450SStefano Ceccherini	acpi_root_info	*acpi;
458f217450SStefano Ceccherini	void	*acpi_cookie;
468f217450SStefano Ceccherini	thread_id thread;
478f217450SStefano Ceccherini	sem_id read_sem;
488f217450SStefano Ceccherini	RingBuffer *buffer;
498f217450SStefano Ceccherini} acpi_ns_device_info;
508f217450SStefano Ceccherini
518f217450SStefano Ceccherini
528f217450SStefano Ceccherini
538f217450SStefano Ceccherini// called with the buffer lock held
548f217450SStefano Ceccherinistatic bool
558f217450SStefano Ceccherinimake_space(acpi_ns_device_info *device, size_t space)
568f217450SStefano Ceccherini{
578f217450SStefano Ceccherini	size_t available = device->buffer->WritableAmount();
588f217450SStefano Ceccherini	if (space <= available)
598f217450SStefano Ceccherini		return true;
608f217450SStefano Ceccherini	bool released = false;
618f217450SStefano Ceccherini	do {
628f217450SStefano Ceccherini		device->buffer->Unlock();
638f217450SStefano Ceccherini
648f217450SStefano Ceccherini		if (!released) {
658f217450SStefano Ceccherini			if (release_sem_etc(device->read_sem, 1, B_RELEASE_IF_WAITING_ONLY) == B_OK)
668f217450SStefano Ceccherini				released = true;
678f217450SStefano Ceccherini		}
688f217450SStefano Ceccherini		snooze(10000);
698f217450SStefano Ceccherini
70cad6c2c5SFredrik Holmqvist		if (!device->buffer->Lock())
718f217450SStefano Ceccherini			return false;
72cad6c2c5SFredrik Holmqvist
738f217450SStefano Ceccherini	} while (device->buffer->WritableAmount() < space);
74cad6c2c5SFredrik Holmqvist
758f217450SStefano Ceccherini	return true;
76cad6c2c5SFredrik Holmqvist}
778f217450SStefano Ceccherini
788f217450SStefano Ceccherini
798f217450SStefano Ceccherini
80cad6c2c5SFredrik Holmqviststatic void
81cad6c2c5SFredrik Holmqvistdump_acpi_namespace(acpi_ns_device_info *device, char *root, int indenting)
828f217450SStefano Ceccherini{
838f217450SStefano Ceccherini	char result[255];
848f217450SStefano Ceccherini	char output[320];
85176ecb2fSStefano Ceccherini	char tabs[255] = "";
86176ecb2fSStefano Ceccherini	char hid[16] = "";
878f217450SStefano Ceccherini	int i;
888f217450SStefano Ceccherini	size_t written = 0;
89cad6c2c5SFredrik Holmqvist	for (i = 0; i < indenting; i++)
908f217450SStefano Ceccherini		strlcat(tabs, "|    ", sizeof(tabs));
918f217450SStefano Ceccherini
928f217450SStefano Ceccherini	strlcat(tabs, "|--- ", sizeof(tabs));
938f217450SStefano Ceccherini
948f217450SStefano Ceccherini	int depth = sizeof(char) * 5 * indenting + sizeof(char); // index into result where the device name will be.
95cad6c2c5SFredrik Holmqvist
968f217450SStefano Ceccherini	void *counter = NULL;
978f217450SStefano Ceccherini	while (device->acpi->get_next_entry(ACPI_TYPE_ANY, root, result, 255, &counter) == B_OK) {
988f217450SStefano Ceccherini		uint32 type = device->acpi->get_object_type(result);
998f217450SStefano Ceccherini		snprintf(output, sizeof(output), "%s%s", tabs, result + depth);
1008f217450SStefano Ceccherini		switch(type) {
1018f217450SStefano Ceccherini			case ACPI_TYPE_INTEGER:
10285965abaSAdrien Destugues				strlcat(output, "     INTEGER", sizeof(output));
1038f217450SStefano Ceccherini				break;
1048f217450SStefano Ceccherini			case ACPI_TYPE_STRING:
10585965abaSAdrien Destugues				strlcat(output, "     STRING", sizeof(output));
1068f217450SStefano Ceccherini				break;
1078f217450SStefano Ceccherini			case ACPI_TYPE_BUFFER:
10885965abaSAdrien Destugues				strlcat(output, "     BUFFER", sizeof(output));
1098f217450SStefano Ceccherini				break;
1108f217450SStefano Ceccherini			case ACPI_TYPE_PACKAGE:
11185965abaSAdrien Destugues				strlcat(output, "     PACKAGE", sizeof(output));
1128f217450SStefano Ceccherini				break;
1138f217450SStefano Ceccherini			case ACPI_TYPE_FIELD_UNIT:
11485965abaSAdrien Destugues				strlcat(output, "     FIELD UNIT", sizeof(output));
1158f217450SStefano Ceccherini				break;
1168f217450SStefano Ceccherini			case ACPI_TYPE_DEVICE:
1178acabe5aSIthamar R. Adema				hid[0] = 0; /* zero-terminate string; get_device_hid can (and will) fail! */
118c1f94990SStefano Ceccherini				device->acpi->get_device_hid(result, hid, sizeof(hid));
11985965abaSAdrien Destugues				strlcat(output, "     DEVICE (", sizeof(output));
12085965abaSAdrien Destugues				strlcat(output, hid, sizeof(output));
12185965abaSAdrien Destugues				strlcat(output, ")", sizeof(output));
1228f217450SStefano Ceccherini				break;
1238f217450SStefano Ceccherini			case ACPI_TYPE_EVENT:
12485965abaSAdrien Destugues				strlcat(output, "     EVENT", sizeof(output));
1258f217450SStefano Ceccherini				break;
1268f217450SStefano Ceccherini			case ACPI_TYPE_METHOD:
12785965abaSAdrien Destugues				strlcat(output, "     METHOD", sizeof(output));
1288f217450SStefano Ceccherini				break;
1298f217450SStefano Ceccherini			case ACPI_TYPE_MUTEX:
13085965abaSAdrien Destugues				strlcat(output, "     MUTEX", sizeof(output));
1318f217450SStefano Ceccherini				break;
1328f217450SStefano Ceccherini			case ACPI_TYPE_REGION:
13385965abaSAdrien Destugues				strlcat(output, "     REGION", sizeof(output));
1348f217450SStefano Ceccherini				break;
1358f217450SStefano Ceccherini			case ACPI_TYPE_POWER:
13685965abaSAdrien Destugues				strlcat(output, "     POWER", sizeof(output));
1378f217450SStefano Ceccherini				break;
1388f217450SStefano Ceccherini			case ACPI_TYPE_PROCESSOR:
13985965abaSAdrien Destugues				strlcat(output, "     PROCESSOR", sizeof(output));
1408f217450SStefano Ceccherini				break;
1418f217450SStefano Ceccherini			case ACPI_TYPE_THERMAL:
14285965abaSAdrien Destugues				strlcat(output, "     THERMAL", sizeof(output));
1438f217450SStefano Ceccherini				break;
1448f217450SStefano Ceccherini			case ACPI_TYPE_BUFFER_FIELD:
14585965abaSAdrien Destugues				strlcat(output, "     BUFFER_FIELD", sizeof(output));
1468f217450SStefano Ceccherini				break;
1478f217450SStefano Ceccherini			case ACPI_TYPE_ANY:
1488f217450SStefano Ceccherini			default:
1498f217450SStefano Ceccherini				break;
1508f217450SStefano Ceccherini		}
1518f217450SStefano Ceccherini		written = 0;
1528f217450SStefano Ceccherini		RingBuffer &ringBuffer = *device->buffer;
1538f217450SStefano Ceccherini		size_t toWrite = strlen(output);
154cad6c2c5SFredrik Holmqvist
155cc9a637eSViktor Müntzing		if (toWrite == 0)
1566fde2584SFredrik Holmqvist			break;
1578f217450SStefano Ceccherini
158cc9a637eSViktor Müntzing		toWrite = strlcat(output, "\n", sizeof(output));
159cc9a637eSViktor Müntzing
160cad6c2c5SFredrik Holmqvist		if (!ringBuffer.Lock())
1616fde2584SFredrik Holmqvist			break;
1626fde2584SFredrik Holmqvist
1636fde2584SFredrik Holmqvist		if (ringBuffer.WritableAmount() < toWrite &&
1646fde2584SFredrik Holmqvist			!make_space(device, toWrite))
1656fde2584SFredrik Holmqvist			break;
1666fde2584SFredrik Holmqvist
1676fde2584SFredrik Holmqvist		written = ringBuffer.Write(output, toWrite);
1686fde2584SFredrik Holmqvist		ringBuffer.Unlock();
1696fde2584SFredrik Holmqvist		dump_acpi_namespace(device, result, indenting + 1);
1708f217450SStefano Ceccherini	}
1718f217450SStefano Ceccherini}
1728f217450SStefano Ceccherini
1738f217450SStefano Ceccherini
1748f217450SStefano Ceccherinistatic int32
1758f217450SStefano Ceccheriniacpi_namespace_dump(void *arg)
1768f217450SStefano Ceccherini{
1778f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info*)(arg);
1788a690c49SJérôme Duval	dump_acpi_namespace(device, NULL, 0);
1798f217450SStefano Ceccherini
1808f217450SStefano Ceccherini	delete_sem(device->read_sem);
1818f217450SStefano Ceccherini	device->read_sem = -1;
1828f217450SStefano Ceccherini
1838f217450SStefano Ceccherini	return 0;
1848f217450SStefano Ceccherini}
1858f217450SStefano Ceccherini
1868f217450SStefano Ceccheriniextern "C" {
1878f217450SStefano Ceccherini/* ----------
1888f217450SStefano Ceccherini	acpi_namespace_open - handle open() calls
1898f217450SStefano Ceccherini----- */
1908f217450SStefano Ceccherini
1918f217450SStefano Ceccherinistatic status_t
1928f217450SStefano Ceccheriniacpi_namespace_open(void *_cookie, const char* path, int flags, void** cookie)
1938f217450SStefano Ceccherini{
1948f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info *)_cookie;
1958f217450SStefano Ceccherini
1968f217450SStefano Ceccherini	dprintf("\nacpi_ns_dump: device_open\n");
1978f217450SStefano Ceccherini
1988f217450SStefano Ceccherini	*cookie = device;
1998f217450SStefano Ceccherini
2008f217450SStefano Ceccherini	RingBuffer *ringBuffer = new RingBuffer(1024);
2018f217450SStefano Ceccherini	if (ringBuffer == NULL)
2028f217450SStefano Ceccherini		return B_NO_MEMORY;
2038f217450SStefano Ceccherini
2048f217450SStefano Ceccherini	device->read_sem = create_sem(0, "read_sem");
2058f217450SStefano Ceccherini	if (device->read_sem < B_OK) {
2068f217450SStefano Ceccherini		delete ringBuffer;
2078f217450SStefano Ceccherini		return device->read_sem;
2088f217450SStefano Ceccherini	}
2098f217450SStefano Ceccherini
2108f217450SStefano Ceccherini	device->thread = spawn_kernel_thread(acpi_namespace_dump, "acpi dumper",
2118f217450SStefano Ceccherini		 B_NORMAL_PRIORITY, device);
2128f217450SStefano Ceccherini	if (device->thread < 0) {
2138f217450SStefano Ceccherini		delete ringBuffer;
2148f217450SStefano Ceccherini		delete_sem(device->read_sem);
2158f217450SStefano Ceccherini		return device->thread;
2168f217450SStefano Ceccherini	}
2178f217450SStefano Ceccherini
2188f217450SStefano Ceccherini	device->buffer = ringBuffer;
2198f217450SStefano Ceccherini
2208f217450SStefano Ceccherini	resume_thread(device->thread);
2218f217450SStefano Ceccherini
2228f217450SStefano Ceccherini	return B_OK;
2238f217450SStefano Ceccherini}
2248f217450SStefano Ceccherini
2258f217450SStefano Ceccherini
2268f217450SStefano Ceccherini/* ----------
2278f217450SStefano Ceccherini	acpi_namespace_read - handle read() calls
2288f217450SStefano Ceccherini----- */
2298f217450SStefano Ceccherinistatic status_t
2308f217450SStefano Ceccheriniacpi_namespace_read(void *_cookie, off_t position, void *buf, size_t* num_bytes)
2318f217450SStefano Ceccherini{
2328f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info *)_cookie;
2338f217450SStefano Ceccherini	RingBuffer &ringBuffer = *device->buffer;
2348f217450SStefano Ceccherini
235b44c75c5SFredrik Holmqvist	if (!ringBuffer.Lock()) {
236b44c75c5SFredrik Holmqvist		*num_bytes = 0;
237b44c75c5SFredrik Holmqvist		return B_ERROR;
238b44c75c5SFredrik Holmqvist	}
2398f217450SStefano Ceccherini
240b44c75c5SFredrik Holmqvist	if (ringBuffer.ReadableAmount() == 0) {
2418f217450SStefano Ceccherini		ringBuffer.Unlock();
242b44c75c5SFredrik Holmqvist		status_t status = acquire_sem_etc(device->read_sem, 1, B_CAN_INTERRUPT, 0);
2438a690c49SJérôme Duval		if (status != B_OK && status != B_BAD_SEM_ID) {
244b44c75c5SFredrik Holmqvist			*num_bytes = 0;
245b44c75c5SFredrik Holmqvist			return status;
246b44c75c5SFredrik Holmqvist		}
247b44c75c5SFredrik Holmqvist		if (!ringBuffer.Lock()) {
248b44c75c5SFredrik Holmqvist			*num_bytes = 0;
249b44c75c5SFredrik Holmqvist			return B_ERROR;
250b44c75c5SFredrik Holmqvist		}
2518f217450SStefano Ceccherini	}
2528f217450SStefano Ceccherini
253b44c75c5SFredrik Holmqvist	*num_bytes = ringBuffer.Read(buf, *num_bytes);
254b44c75c5SFredrik Holmqvist	ringBuffer.Unlock();
2558f217450SStefano Ceccherini
2568f217450SStefano Ceccherini	return B_OK;
2578f217450SStefano Ceccherini}
2588f217450SStefano Ceccherini
2598f217450SStefano Ceccherini
2608f217450SStefano Ceccherini/* ----------
2618f217450SStefano Ceccherini	acpi_namespace_write - handle write() calls
2628f217450SStefano Ceccherini----- */
2638f217450SStefano Ceccherini
2648f217450SStefano Ceccherinistatic status_t
2658f217450SStefano Ceccheriniacpi_namespace_write(void* cookie, off_t position, const void* buffer, size_t* num_bytes)
2668f217450SStefano Ceccherini{
2678f217450SStefano Ceccherini	*num_bytes = 0;				/* tell caller nothing was written */
2688f217450SStefano Ceccherini	return B_IO_ERROR;
2698f217450SStefano Ceccherini}
2708f217450SStefano Ceccherini
2718f217450SStefano Ceccherini
2728f217450SStefano Ceccherini/* ----------
2738f217450SStefano Ceccherini	acpi_namespace_control - handle ioctl calls
2748f217450SStefano Ceccherini----- */
2758f217450SStefano Ceccherini
2768f217450SStefano Ceccherinistatic status_t
2778f217450SStefano Ceccheriniacpi_namespace_control(void* cookie, uint32 op, void* arg, size_t len)
2788f217450SStefano Ceccherini{
2798f217450SStefano Ceccherini	dprintf("acpi_ns_dump: device_control\n");
280e523d3cfSJérôme Duval	return B_DEV_INVALID_IOCTL;
2818f217450SStefano Ceccherini}
2828f217450SStefano Ceccherini
2838f217450SStefano Ceccherini
2848f217450SStefano Ceccherini/* ----------
2858f217450SStefano Ceccherini	acpi_namespace_close - handle close() calls
2868f217450SStefano Ceccherini----- */
2878f217450SStefano Ceccherini
2888f217450SStefano Ceccherinistatic status_t
2898f217450SStefano Ceccheriniacpi_namespace_close(void* cookie)
2908f217450SStefano Ceccherini{
2918f217450SStefano Ceccherini	dprintf("acpi_ns_dump: device_close\n");
2928f217450SStefano Ceccherini	return B_OK;
2938f217450SStefano Ceccherini}
2948f217450SStefano Ceccherini
2958f217450SStefano Ceccherini
2968f217450SStefano Ceccherini/* -----
2978f217450SStefano Ceccherini	acpi_namespace_free - called after the last device is closed, and after
2988f217450SStefano Ceccherini	all i/o is complete.
2998f217450SStefano Ceccherini----- */
3008f217450SStefano Ceccherinistatic status_t
3018f217450SStefano Ceccheriniacpi_namespace_free(void* cookie)
3028f217450SStefano Ceccherini{
3038a690c49SJérôme Duval	status_t status;
3048a690c49SJérôme Duval	acpi_ns_device_info *device = (acpi_ns_device_info *)cookie;
3058f217450SStefano Ceccherini	dprintf("acpi_ns_dump: device_free\n");
3066fde2584SFredrik Holmqvist
3078a690c49SJérôme Duval	if (device->read_sem >= 0)
3088a690c49SJérôme Duval		delete_sem(device->read_sem);
3098a690c49SJérôme Duval
3108a690c49SJérôme Duval	device->buffer->DestroyLock();
3118a690c49SJérôme Duval	wait_for_thread(device->thread, &status);
3128a690c49SJérôme Duval	delete device->buffer;
3138a690c49SJérôme Duval
3148f217450SStefano Ceccherini	return B_OK;
3158f217450SStefano Ceccherini}
3168f217450SStefano Ceccherini
3178f217450SStefano Ceccherini
3188f217450SStefano Ceccherini//	#pragma mark - device module API
3198f217450SStefano Ceccherini
3208f217450SStefano Ceccherini
3218f217450SStefano Ceccherinistatic status_t
3228f217450SStefano Ceccheriniacpi_namespace_init_device(void *_cookie, void **cookie)
3238f217450SStefano Ceccherini{
3248f217450SStefano Ceccherini	device_node *node = (device_node *)_cookie;
3258f217450SStefano Ceccherini	status_t err;
3266fde2584SFredrik Holmqvist
3278f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info *)calloc(1, sizeof(*device));
3288f217450SStefano Ceccherini	if (device == NULL)
3298f217450SStefano Ceccherini		return B_NO_MEMORY;
3308f217450SStefano Ceccherini
3318f217450SStefano Ceccherini	device->node = node;
3328f217450SStefano Ceccherini	err = gDeviceManager->get_driver(node, (driver_module_info **)&device->acpi,
3338f217450SStefano Ceccherini		(void **)&device->acpi_cookie);
3348f217450SStefano Ceccherini	if (err != B_OK) {
3358f217450SStefano Ceccherini		free(device);
3368f217450SStefano Ceccherini		return err;
3378f217450SStefano Ceccherini	}
3386fde2584SFredrik Holmqvist
3398f217450SStefano Ceccherini	*cookie = device;
3408f217450SStefano Ceccherini	return B_OK;
3418f217450SStefano Ceccherini}
3428f217450SStefano Ceccherini
3438f217450SStefano Ceccherini
3448f217450SStefano Ceccherinistatic void
3458f217450SStefano Ceccheriniacpi_namespace_uninit_device(void *_cookie)
3468f217450SStefano Ceccherini{
3478f217450SStefano Ceccherini	acpi_ns_device_info *device = (acpi_ns_device_info *)_cookie;
3488f217450SStefano Ceccherini	free(device);
3498f217450SStefano Ceccherini}
3508f217450SStefano Ceccherini
3518f217450SStefano Ceccherini}
3528f217450SStefano Ceccherini
3538f217450SStefano Ceccherinistruct device_module_info acpi_ns_dump_module = {
3548f217450SStefano Ceccherini	{
3558f217450SStefano Ceccherini		ACPI_NS_DUMP_DEVICE_MODULE_NAME,
3568f217450SStefano Ceccherini		0,
3578f217450SStefano Ceccherini		NULL
3588f217450SStefano Ceccherini	},
3598f217450SStefano Ceccherini
3608f217450SStefano Ceccherini	acpi_namespace_init_device,
3618f217450SStefano Ceccherini	acpi_namespace_uninit_device,
3628f217450SStefano Ceccherini	NULL,
3636fde2584SFredrik Holmqvist
3648f217450SStefano Ceccherini	acpi_namespace_open,
3658f217450SStefano Ceccherini	acpi_namespace_close,
3668f217450SStefano Ceccherini	acpi_namespace_free,
3678f217450SStefano Ceccherini	acpi_namespace_read,
3688f217450SStefano Ceccherini	acpi_namespace_write,
3698f217450SStefano Ceccherini	NULL,
3708f217450SStefano Ceccherini	acpi_namespace_control,
3718f217450SStefano Ceccherini
3728f217450SStefano Ceccherini	NULL,
3738f217450SStefano Ceccherini	NULL
3748f217450SStefano Ceccherini};
3758f217450SStefano Ceccherini
3768f217450SStefano Ceccherini
3778f217450SStefano CeccheriniRingBuffer::RingBuffer(size_t size)
3788f217450SStefano Ceccherini{
3798f217450SStefano Ceccherini	fBuffer = create_ring_buffer(size);
3808f217450SStefano Ceccherini	fLock = create_sem(1, "ring buffer lock");
3818f217450SStefano Ceccherini}
3828f217450SStefano Ceccherini
3838f217450SStefano Ceccherini
3848f217450SStefano CeccheriniRingBuffer::~RingBuffer()
3858f217450SStefano Ceccherini{
3868f217450SStefano Ceccherini	delete_ring_buffer(fBuffer);
3878f217450SStefano Ceccherini}
3888f217450SStefano Ceccherini
3898f217450SStefano Ceccherini
390b44c75c5SFredrik Holmqvistsize_t
391b44c75c5SFredrik HolmqvistRingBuffer::Read(void *buffer, ssize_t size)
3928f217450SStefano Ceccherini{
39348a00c0fSAugustin Cavalier	if (IS_USER_ADDRESS(buffer))
39448a00c0fSAugustin Cavalier		return ring_buffer_user_read(fBuffer, (uint8*)buffer, size);
39548a00c0fSAugustin Cavalier	else
39648a00c0fSAugustin Cavalier		return ring_buffer_read(fBuffer, (uint8*)buffer, size);
3978f217450SStefano Ceccherini}
3988f217450SStefano Ceccherini
3998f217450SStefano Ceccherini
400b44c75c5SFredrik Holmqvistsize_t
401b44c75c5SFredrik HolmqvistRingBuffer::Write(const void *buffer, ssize_t size)
4028f217450SStefano Ceccherini{
4038f217450SStefano Ceccherini	return ring_buffer_write(fBuffer, (uint8*)buffer, size);
4048f217450SStefano Ceccherini}
4058f217450SStefano Ceccherini
4068f217450SStefano Ceccherini
4078f217450SStefano Ceccherinisize_t
4088f217450SStefano CeccheriniRingBuffer::ReadableAmount() const
4098f217450SStefano Ceccherini{
4108f217450SStefano Ceccherini	return ring_buffer_readable(fBuffer);
4118f217450SStefano Ceccherini}
4128f217450SStefano Ceccherini
4138f217450SStefano Ceccherini
4148f217450SStefano Ceccherinisize_t
4158f217450SStefano CeccheriniRingBuffer::WritableAmount() const
4168f217450SStefano Ceccherini{
4178f217450SStefano Ceccherini	return ring_buffer_writable(fBuffer);
4188f217450SStefano Ceccherini}
4198f217450SStefano Ceccherini
4208f217450SStefano Ceccherini
4218f217450SStefano Ceccherinibool
4228f217450SStefano CeccheriniRingBuffer::Lock()
4238f217450SStefano Ceccherini{
4248f217450SStefano Ceccherini	//status_t status = acquire_sem_etc(fLock, 1, B_CAN_INTERRUPT, 0);
4258f217450SStefano Ceccherini	status_t status = acquire_sem(fLock);
4268f217450SStefano Ceccherini	return status == B_OK;
4278f217450SStefano Ceccherini}
4288f217450SStefano Ceccherini
4298f217450SStefano Ceccherini
4308f217450SStefano Ceccherinivoid
4318f217450SStefano CeccheriniRingBuffer::Unlock()
4328f217450SStefano Ceccherini{
4338f217450SStefano Ceccherini	release_sem(fLock);
4348f217450SStefano Ceccherini}
4358f217450SStefano Ceccherini
4368f217450SStefano Ceccherini
4376fde2584SFredrik Holmqvistvoid
4386fde2584SFredrik HolmqvistRingBuffer::DestroyLock()
4396fde2584SFredrik Holmqvist{
4406fde2584SFredrik Holmqvist	delete_sem(fLock);
4416fde2584SFredrik Holmqvist}
4428f217450SStefano Ceccherini
443