12ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu/*
22ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
32ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * Copyright (C) 2010-2011 LunarG Inc.
42ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu *
52ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * Permission is hereby granted, free of charge, to any person obtaining a
62ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * copy of this software and associated documentation files (the "Software"),
72ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * to deal in the Software without restriction, including without limitation
82ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * the rights to use, copy, modify, merge, publish, distribute, sublicense,
92ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * and/or sell copies of the Software, and to permit persons to whom the
102ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * Software is furnished to do so, subject to the following conditions:
112ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu *
122ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * The above copyright notice and this permission notice shall be included
132ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * in all copies or substantial portions of the Software.
142ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu *
152ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
182ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
192ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
202ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
212ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu * DEALINGS IN THE SOFTWARE.
222ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu */
232ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
242ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu#define LOG_TAG "GRALLOC-MOD"
252ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
262ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu#include <cutils/log.h>
272ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu#include <stdlib.h>
282ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu#include <stdarg.h>
292ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu#include <pthread.h>
302ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu#include <errno.h>
312ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
322ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu#include "gralloc_drm.h"
3325d22516a6eb0991e1b1ec25d25785daf7100effTapani Pälli#include "gralloc_drm_priv.h"
342ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
352ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu/*
36d81a9375614e30376947013d24cea546d39e93fcSean Paul * Initialize the DRM device object
372ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu */
38d81a9375614e30376947013d24cea546d39e93fcSean Paulstatic int drm_init(struct drm_module_t *dmod)
392ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
402ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	int err = 0;
412ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
422ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	pthread_mutex_lock(&dmod->mutex);
432ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (!dmod->drm) {
442ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		dmod->drm = gralloc_drm_create();
452ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		if (!dmod->drm)
462ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			err = -EINVAL;
472ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	}
482ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	pthread_mutex_unlock(&dmod->mutex);
492ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
502ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	return err;
512ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
522ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
532ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_perform(const struct gralloc_module_t *mod, int op, ...)
542ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
552ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct drm_module_t *dmod = (struct drm_module_t *) mod;
562ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	va_list args;
572ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	int err;
582ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
59d81a9375614e30376947013d24cea546d39e93fcSean Paul	err = drm_init(dmod);
602ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (err)
612ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		return err;
622ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
632ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	va_start(args, op);
642ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	switch (op) {
65cda66b80878c0c0f0799a145b41895c1149da88aStephen Hines	case static_cast<int>(GRALLOC_MODULE_PERFORM_GET_DRM_FD):
662ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		{
672ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			int *fd = va_arg(args, int *);
682ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			*fd = gralloc_drm_get_fd(dmod->drm);
692ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			err = 0;
702ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		}
712ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		break;
722ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	default:
732ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		err = -EINVAL;
742ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		break;
752ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	}
762ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	va_end(args);
772ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
782ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	return err;
792ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
802ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
812ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_register_buffer(const gralloc_module_t *mod,
822ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		buffer_handle_t handle)
832ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
842fc5da4da12c8fa36044acc58856b0e460e70621Chia-I Wu	struct drm_module_t *dmod = (struct drm_module_t *) mod;
851db540381c30f11ecb87995f8bb7e1ee0a3f1b9cChia-I Wu	int err;
861db540381c30f11ecb87995f8bb7e1ee0a3f1b9cChia-I Wu
87d81a9375614e30376947013d24cea546d39e93fcSean Paul	err = drm_init(dmod);
881db540381c30f11ecb87995f8bb7e1ee0a3f1b9cChia-I Wu	if (err)
891db540381c30f11ecb87995f8bb7e1ee0a3f1b9cChia-I Wu		return err;
902fc5da4da12c8fa36044acc58856b0e460e70621Chia-I Wu
918542de3915e6b8a88232a1f0f991a425e9289a96Chia-I Wu	return gralloc_drm_handle_register(handle, dmod->drm);
922ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
932ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
942ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_unregister_buffer(const gralloc_module_t *mod,
952ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		buffer_handle_t handle)
962ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
978542de3915e6b8a88232a1f0f991a425e9289a96Chia-I Wu	return gralloc_drm_handle_unregister(handle);
982ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
992ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1002ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle,
1012ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		int usage, int x, int y, int w, int h, void **ptr)
1022ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
1032ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct gralloc_drm_bo_t *bo;
1042ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	int err;
1052ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1068542de3915e6b8a88232a1f0f991a425e9289a96Chia-I Wu	bo = gralloc_drm_bo_from_handle(handle);
1072ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (!bo)
1082ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		return -EINVAL;
1092ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
11025e041367040b6faadebc38acd44e96b8e29a5abChia-I Wu	return gralloc_drm_bo_lock(bo, usage, x, y, w, h, ptr);
1112ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
1122ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
113a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figastatic int drm_mod_lock_ycbcr(const gralloc_module_t *mod, buffer_handle_t bhandle,
114a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		int usage, int x, int y, int w, int h, struct android_ycbcr *ycbcr)
115a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa{
116a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	struct gralloc_drm_handle_t *handle;
117a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	struct gralloc_drm_bo_t *bo;
118a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	void *ptr;
119a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	int err;
120a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa
121a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	bo = gralloc_drm_bo_from_handle(bhandle);
122a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	if (!bo)
123a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		return -EINVAL;
124a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	handle = bo->handle;
125a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa
126a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	switch(handle->format) {
127a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	case HAL_PIXEL_FORMAT_YCbCr_420_888:
128a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		break;
129a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	default:
130a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		return -EINVAL;
131a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	}
132a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa
133a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	err = gralloc_drm_bo_lock(bo, usage, x, y, w, h, &ptr);
134a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	if (err)
135a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		return err;
136a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa
137a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	switch(handle->format) {
138a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	case HAL_PIXEL_FORMAT_YCbCr_420_888:
139a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		ycbcr->y = ptr;
140a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		ycbcr->cb = (uint8_t *)ptr + handle->stride * handle->height;
141a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		ycbcr->cr = (uint8_t *)ycbcr->cb + 1;
142a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		ycbcr->ystride = handle->stride;
143a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		ycbcr->cstride = handle->stride;
144a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		ycbcr->chroma_step = 2;
145a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		break;
146a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	default:
147a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		break;
148a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	}
149a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa
150a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa	return 0;
151a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa}
152a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa
1532ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_unlock(const gralloc_module_t *mod, buffer_handle_t handle)
1542ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
1552ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct drm_module_t *dmod = (struct drm_module_t *) mod;
1562ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct gralloc_drm_bo_t *bo;
1572ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1588542de3915e6b8a88232a1f0f991a425e9289a96Chia-I Wu	bo = gralloc_drm_bo_from_handle(handle);
1592ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (!bo)
1602ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		return -EINVAL;
1612ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
16225e041367040b6faadebc38acd44e96b8e29a5abChia-I Wu	gralloc_drm_bo_unlock(bo);
1632ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1642ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	return 0;
1652ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
1662ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1672ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_close_gpu0(struct hw_device_t *dev)
1682ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
169879cc4e7c6da5ecf631d65ddd98d12bccd5817f5Sean Paul	struct drm_module_t *dmod = (struct drm_module_t *)dev->module;
1702ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct alloc_device_t *alloc = (struct alloc_device_t *) dev;
1712ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
172879cc4e7c6da5ecf631d65ddd98d12bccd5817f5Sean Paul	gralloc_drm_destroy(dmod->drm);
173d225ab0eaecac84e5fd7199968a433a462af0196Sean Paul	delete alloc;
1742ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1752ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	return 0;
1762ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
1772ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1782ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_free_gpu0(alloc_device_t *dev, buffer_handle_t handle)
1792ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
1802ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
1812ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct gralloc_drm_bo_t *bo;
1822ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1838542de3915e6b8a88232a1f0f991a425e9289a96Chia-I Wu	bo = gralloc_drm_bo_from_handle(handle);
1842ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (!bo)
1852ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		return -EINVAL;
1862ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
187a86ecd9036df3862f7289378609509e50ef38cb3Tapani Pälli	gralloc_drm_bo_decref(bo);
1882ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1892ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	return 0;
1902ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
1912ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
1922ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_alloc_gpu0(alloc_device_t *dev,
1932ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		int w, int h, int format, int usage,
1942ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		buffer_handle_t *handle, int *stride)
1952ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
1962ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
1972ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct gralloc_drm_bo_t *bo;
1982ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	int size, bpp, err;
1992ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2002ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	bpp = gralloc_drm_get_bpp(format);
2012ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (!bpp)
2022ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		return -EINVAL;
2032ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2042ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	bo = gralloc_drm_bo_create(dmod->drm, w, h, format, usage);
2052ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (!bo)
2062ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		return -ENOMEM;
2072ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2082ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	*handle = gralloc_drm_bo_get_handle(bo, stride);
2092ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	/* in pixels */
2102ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	*stride /= bpp;
2112ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2122ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	return 0;
2132ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
2142ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2152ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_open_gpu0(struct drm_module_t *dmod, hw_device_t **dev)
2162ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
2172ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct alloc_device_t *alloc;
2182ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	int err;
2192ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
220d81a9375614e30376947013d24cea546d39e93fcSean Paul	err = drm_init(dmod);
2212ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (err)
2222ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		return err;
2232ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
224d225ab0eaecac84e5fd7199968a433a462af0196Sean Paul	alloc = new alloc_device_t;
2252ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (!alloc)
2262ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		return -EINVAL;
2272ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2282ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	alloc->common.tag = HARDWARE_DEVICE_TAG;
2292ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	alloc->common.version = 0;
2302ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	alloc->common.module = &dmod->base.common;
2312ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	alloc->common.close = drm_mod_close_gpu0;
2322ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2332ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	alloc->alloc = drm_mod_alloc_gpu0;
2342ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	alloc->free = drm_mod_free_gpu0;
2352ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2362ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	*dev = &alloc->common;
2372ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2382ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	return 0;
2392ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
2402ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2412ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic int drm_mod_open(const struct hw_module_t *mod,
2422ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		const char *name, struct hw_device_t **dev)
2432ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu{
2442ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	struct drm_module_t *dmod = (struct drm_module_t *) mod;
2452ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	int err;
2462ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2472ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	if (strcmp(name, GRALLOC_HARDWARE_GPU0) == 0)
2482ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		err = drm_mod_open_gpu0(dmod, dev);
2492ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	else
2502ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		err = -EINVAL;
2512ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2522ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	return err;
2532ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu}
2542ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2552ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustatic struct hw_module_methods_t drm_mod_methods = {
2562ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	.open = drm_mod_open
2572ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu};
2582ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu
2592ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wustruct drm_module_t HAL_MODULE_INFO_SYM = {
2602ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	.base = {
2612ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		.common = {
2622ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			.tag = HARDWARE_MODULE_TAG,
2632ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			.version_major = 1,
2642ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			.version_minor = 0,
2652ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			.id = GRALLOC_HARDWARE_MODULE_ID,
2662ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			.name = "DRM Memory Allocator",
2672ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			.author = "Chia-I Wu",
2682ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu			.methods = &drm_mod_methods
2692ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		},
2702ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		.registerBuffer = drm_mod_register_buffer,
2712ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		.unregisterBuffer = drm_mod_unregister_buffer,
2722ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		.lock = drm_mod_lock,
2732ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu		.unlock = drm_mod_unlock,
274a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		.perform = drm_mod_perform,
275a43d9523c050b3ddb40190bb8de7920b7521b6caTomasz Figa		.lock_ycbcr = drm_mod_lock_ycbcr,
2762ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	},
277b201e98a1cbdf217e52da2cf503beb3b21c2e173Tapani Pälli
2782ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	.mutex = PTHREAD_MUTEX_INITIALIZER,
2792ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu	.drm = NULL
2802ec32d4f949f04d0006fff50065c904626c2e58Chia-I Wu};
281