/* * User space memory access macros * * Copyright (c) 2019 Elvees (support@elvees.com) * Author: Dmitry Evtushenko * */ #ifndef __ua_mem_h #define __ua_mem_h //#include #include // Macroses for work with user memory, accesseble by pointer: #ifdef USE_FREE_USER_MEM_ACCESS #define PUT_USER( cmd, val, pntr, type) \ *((type *)pntr) = val; #define GET_USER( cmd, val, pntr, type) \ val = *((type *)pntr); #define COPY_FROM_USER( cmd, pTo, pFrom, sz) //- fictional macros (used with SET_PNTR) #define COPY_TO_USER( cmd, pTo, pFrom, sz) //- fictional macros (used with SET_PNTR) #define SET_PNTR( pntr, obj) //- fictional macros (used with COPY_FROM_USER & COPY_TO_USER) #define CLEAR_USER( cmd, pntr, sz) \ memset( (void *)pntr, 0, sz); #else //- !USE_FREE_USER_MEM_ACCESS: #define PUT_USER( cmd, val, pntr, type) \ if( (ret = put_user( val, (__user type *)pntr)) ) \ { \ printk( KERN_ERR"%s: put_user ERROR in "#cmd": -> %i\n", drv_name, ret); \ return ret; \ } #define GET_USER( cmd, prm, pntr, type) \ if( (ret = get_user( prm, (__user type *)pntr)) ) \ { \ printk( KERN_ERR"%s: get_user ERROR in "#cmd": -> %i\n", drv_name, ret); \ return ret; \ } #define COPY_FROM_USER( cmd, pTo, pFrom, sz) \ if( (ret=copy_from_user( pTo, (pFrom), sz)) ) \ { \ printk( KERN_ERR"%s: copy_from_user ERROR in "#cmd": -> %i\n", drv_name, ret); \ return ret; \ } #define COPY_TO_USER( cmd, pTo, pFrom, sz) \ if( (ret=copy_to_user( pTo, pFrom, sz)) ) \ { \ printk( KERN_ERR"%s: copy_to_user ERROR in "#cmd": -> %i\n", drv_name, ret); \ return ret; \ } #define SET_PNTR( pntr, obj) pntr = &obj; #define CLEAR_USER( cmd, pntr, sz) \ if( (ret=clear_user( pntr, sz)) ) \ { \ printk( KERN_ERR"%s: clear_user ERROR in "#cmd": -> %i\n", drv_name, ret); \ return ret; \ } #endif //- !USE_FREE_USER_MEM_ACCESS // structure for setting value to register with address: struct s_reg_val { unsigned int addr; //- address unsigned int val; //- value }; // extended structure for setting value (with mask and offset) to register with address: struct s_reg_val_ext { unsigned int addr; //- address unsigned int val; //- value unsigned int msk; //- mask unsigned int ofs; //- ofs }; #endif //__ua_mem_h