/**************************************************************************1*2* Copyright 2010 Pauli Nieminen.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR18* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL20* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,21* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR22* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE23* USE OR OTHER DEALINGS IN THE SOFTWARE.24*25*26**************************************************************************/27/*28* Multipart buffer for coping data which is larger than the page size.29*30* Authors:31* Pauli Nieminen <suokkos-at-gmail-dot-com>32*/3334#ifndef _DRM_BUFFER_H_35#define _DRM_BUFFER_H_3637#include "drmP.h"3839struct drm_buffer {40int iterator;41int size;42char *data[];43};444546/**47* Return the index of page that buffer is currently pointing at.48*/49static inline int drm_buffer_page(struct drm_buffer *buf)50{51return buf->iterator / PAGE_SIZE;52}53/**54* Return the index of the current byte in the page55*/56static inline int drm_buffer_index(struct drm_buffer *buf)57{58return buf->iterator & (PAGE_SIZE - 1);59}60/**61* Return number of bytes that is left to process62*/63static inline int drm_buffer_unprocessed(struct drm_buffer *buf)64{65return buf->size - buf->iterator;66}6768/**69* Advance the buffer iterator number of bytes that is given.70*/71static inline void drm_buffer_advance(struct drm_buffer *buf, int bytes)72{73buf->iterator += bytes;74}7576/**77* Allocate the drm buffer object.78*79* buf: A pointer to a pointer where the object is stored.80* size: The number of bytes to allocate.81*/82extern int drm_buffer_alloc(struct drm_buffer **buf, int size);8384/**85* Copy the user data to the begin of the buffer and reset the processing86* iterator.87*88* user_data: A pointer the data that is copied to the buffer.89* size: The Number of bytes to copy.90*/91extern int drm_buffer_copy_from_user(struct drm_buffer *buf,92void __user *user_data, int size);9394/**95* Free the drm buffer object96*/97extern void drm_buffer_free(struct drm_buffer *buf);9899/**100* Read an object from buffer that may be split to multiple parts. If object101* is not split function just returns the pointer to object in buffer. But in102* case of split object data is copied to given stack object that is suplied103* by caller.104*105* The processing location of the buffer is also advanced to the next byte106* after the object.107*108* objsize: The size of the objet in bytes.109* stack_obj: A pointer to a memory location where object can be copied.110*/111extern void *drm_buffer_read_object(struct drm_buffer *buf,112int objsize, void *stack_obj);113114/**115* Returns the pointer to the dword which is offset number of elements from the116* current processing location.117*118* Caller must make sure that dword is not split in the buffer. This119* requirement is easily met if all the sizes of objects in buffer are120* multiples of dword and PAGE_SIZE is multiple dword.121*122* Call to this function doesn't change the processing location.123*124* offset: The index of the dword relative to the internat iterator.125*/126static inline void *drm_buffer_pointer_to_dword(struct drm_buffer *buffer,127int offset)128{129int iter = buffer->iterator + offset * 4;130return &buffer->data[iter / PAGE_SIZE][iter & (PAGE_SIZE - 1)];131}132/**133* Returns the pointer to the dword which is offset number of elements from134* the current processing location.135*136* Call to this function doesn't change the processing location.137*138* offset: The index of the byte relative to the internat iterator.139*/140static inline void *drm_buffer_pointer_to_byte(struct drm_buffer *buffer,141int offset)142{143int iter = buffer->iterator + offset;144return &buffer->data[iter / PAGE_SIZE][iter & (PAGE_SIZE - 1)];145}146147#endif148149150