mbgtools-lx  4.2.8
mbgioctl.c
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: mbgioctl.c 1.6 2019/04/03 12:59:43 martin TRASH $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Utility functions to be used with IOCTLs.
10  *
11  * -----------------------------------------------------------------------
12  * $Log: mbgioctl.c $
13  * Revision 1.6 2019/04/03 12:59:43 martin
14  * Fixed preprocessor conditionals using unknown symbols.
15  * Revision 1.5 2018/11/22 14:57:43 martin
16  * Check new preprocessor symbol MBG_TGT_USE_IOCTL to exclude
17  * code from build on targets that don't use IOCTL calls.
18  * Revision 1.4 2018/11/05 13:33:26 martin
19  * Avoid signed/unsigned mismatch by using appropriate function parameter.
20  * Revision 1.3 2018/07/05 08:57:05Z martin
21  * New function mbgioctl_rc_to_mbg_errno().
22  * New function mbgioctl_get_name().
23  * Updated file header comment.
24  * Revision 1.2 2003/03/18 09:30:05 martin
25  * Changed names from ioctlw32 to mbgioctl.
26  * Revision 1.1 2002/02/19 13:51:08Z MARTIN
27  * Initial revision
28  *
29  **************************************************************************/
30 
31 #include <mbg_tgt.h>
32 
33 // The code below is only required for target systems
34 // where IOCTLs are used to access a kernel driver, so
35 // we exclude it from build on systems where the physical
36 // devices are accessed directly from user space.
37 
38 #if MBG_TGT_USE_IOCTL
39 
40 #define _MBGIOCTL
41  #include <mbgioctl.h>
42 #undef _MBGIOCTL
43 
44 #include <mbgerror.h>
45 
46 
47 #if defined( MBG_TGT_WIN32 ) && !defined( MBG_TGT_KERNEL )
48 
49 /*HDR*/
50 int mbgioctl_rc_to_mbg_errno( int sys_errno )
51 {
52  // FIXME: This is a hack: Under Linux the IOCTL_RC_ERR_...
53  // code definitions are all negative, but the sys_errno
54  // number we expect here is positive. So just revert the
55  // sign to be able to uses the original IOCTL_RC_ERR_...
56  // definitions for the switch cases.
57  #if defined( MBG_TGT_LINUX )
58  int cmp_rc_code = -sys_errno;
59  #else
60  int cmp_rc_code = sys_errno;
61  #endif
62 
63  switch ( cmp_rc_code )
64  {
65  case IOCTL_RC_ERR_PERM: return MBG_ERR_PERM;
66  case IOCTL_RC_ERR_UNSUPP_IOCTL: return MBG_ERR_INV_DEV_REQUEST;
67  case IOCTL_RC_ERR_INVAL_PARAM: return MBG_ERR_INV_PARM;
68  case IOCTL_RC_ERR_NOT_SUPP_BY_DEV: return MBG_ERR_NOT_SUPP_BY_DEV;
69  case IOCTL_RC_ERR_NO_MEM: return MBG_ERR_NO_MEM;
70  case IOCTL_RC_ERR_BUSY_IRQ_UNSAFE: return MBG_ERR_IRQ_UNSAFE; // or MBG_ERR_BUSY ?
71  case IOCTL_RC_ERR_DEV_ACCESS: return MBG_ERR_IO;
72 
73  #if defined( MBG_TGT_LINUX ) // Linux-specific codes
74  case IOCTL_RC_ERR_COPY_TO_USER: return MBG_ERR_COPY_TO_USER; // FIXME
75  // case IOCTL_RC_ERR_COPY_FROM_USER: return MBG_ERR_COPY_FROM_USER;
76  #endif // defined( MBG_TGT_LINUX )
77  }
78 
79  // FIXME: This is another hack: Eventually the error code returned
80  // in kernel space is remapped to some other code by the kernel,
81  // so this is is not covered by the switch above, and the error
82  // code used in kernel may not even be defined in user space.
83  // E.g. ENOIOCTL for *BSD is only defined in kernel space.
84  return mbg_posix_errno_to_mbg( sys_errno, NULL );
85 
86 } // mbgioctl_rc_to_mbg_errno
87 
88 #endif // defined( MBG_TGT_WIN32 ) && !defined( MBG_TGT_KERNEL )
89 
90 
91 
92 #if defined( DEBUG ) || defined( MBG_DEBUG )
93 
94 /*HDR*/
95 const char *mbgioctl_get_name( long code )
96 {
97  static const MBG_CODE_NAME_TABLE_ENTRY tbl[] = IOCTL_CODES_TABLE;
98 
99  const MBG_CODE_NAME_TABLE_ENTRY *p = tbl;
100 
101  for ( p = tbl; p->name; p++ )
102  {
103  if ( p->code == code )
104  return p->name;
105  }
106 
107  return "UNKNOWN_IOCTL";
108 
109 } // mbgioctl_get_name
110 
111 #endif // defined( DEBUG ) || defined( MBG_DEBUG )
112 
113 #endif // MBG_TGT_USE_IOCTL
114 
const char * mbgioctl_get_name(long code)
#define MBG_ERR_INV_PARM
Invalid parameter.
Definition: mbgerror.h:329
A table entry which can be used to map codes to names.
Definition: words.h:532
#define MBG_ERR_COPY_TO_USER
Kernel driver failed to copy data from kernel to user space.
Definition: mbgerror.h:302
int mbg_posix_errno_to_mbg(int posix_errno, const char *info)
Translate a POSIX errno error code to one of the MBG_ERROR_CODES.
Definition: mbgerror.c:920
#define MBG_ERR_IRQ_UNSAFE
Enabled IRQ of bus-level device is unsafe with this firmware/ASIC version.
Definition: mbgerror.h:293
#define MBG_ERR_IO
Input/output error.
Definition: mbgerror.h:328
#define MBG_ERR_INV_DEV_REQUEST
IOCTL call not supported by driver.
Definition: mbgerror.h:285
#define MBG_ERR_NO_MEM
Failed to allocate memory.
Definition: mbgerror.h:282
long code
Definition: words.h:534
#define MBG_ERR_PERM
Operation not permitted, e.g. when trying to set the system time without sufficient permissions...
Definition: mbgerror.h:337
#define IOCTL_CODES_TABLE
An initializer for a table of IOCTL codes and associated names.
Definition: mbgioctl.h:753
const char * name
Definition: words.h:535
#define MBG_ERR_NOT_SUPP_BY_DEV
Command or feature not supported by device.
Definition: mbgerror.h:286
int mbgioctl_rc_to_mbg_errno(int sys_errno)