qmi_encdec.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #ifndef _QMI_ENCDEC_H_
  13. #define _QMI_ENCDEC_H_
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/mm.h>
  17. #include <linux/list.h>
  18. #include <linux/socket.h>
  19. #include <linux/gfp.h>
  20. #define QMI_REQUEST_CONTROL_FLAG 0x00
  21. #define QMI_RESPONSE_CONTROL_FLAG 0x02
  22. #define QMI_INDICATION_CONTROL_FLAG 0x04
  23. #define QMI_HEADER_SIZE 7
  24. /**
  25. * elem_type - Enum to identify the data type of elements in a data
  26. * structure.
  27. */
  28. enum elem_type {
  29. QMI_OPT_FLAG = 1,
  30. QMI_DATA_LEN,
  31. QMI_UNSIGNED_1_BYTE,
  32. QMI_UNSIGNED_2_BYTE,
  33. QMI_UNSIGNED_4_BYTE,
  34. QMI_UNSIGNED_8_BYTE,
  35. QMI_SIGNED_2_BYTE_ENUM,
  36. QMI_SIGNED_4_BYTE_ENUM,
  37. QMI_STRUCT,
  38. QMI_EOTI,
  39. };
  40. /**
  41. * array_type - Enum to identify if an element in a data structure is
  42. * an array. If so, then is it a static length array or a
  43. * variable length array.
  44. */
  45. enum array_type {
  46. NO_ARRAY = 0,
  47. STATIC_ARRAY = 1,
  48. VAR_LEN_ARRAY = 2,
  49. };
  50. /**
  51. * elem_info - Data structure to specify information about an element
  52. * in a data structure. An array of this data structure
  53. * can be used to specify info about a complex data
  54. * structure to be encoded/decoded.
  55. *
  56. * @data_type: Data type of this element.
  57. * @elem_len: Array length of this element, if an array.
  58. * @elem_size: Size of a single instance of this data type.
  59. * @is_array: Array type of this element.
  60. * @tlv_type: QMI message specific type to identify which element
  61. * is present in an incoming message.
  62. * @offset: To identify the address of the first instance of this
  63. * element in the data structure.
  64. * @ei_array: Array to provide information about the nested structure
  65. * within a data structure to be encoded/decoded.
  66. */
  67. struct elem_info {
  68. enum elem_type data_type;
  69. uint32_t elem_len;
  70. uint32_t elem_size;
  71. enum array_type is_array;
  72. uint8_t tlv_type;
  73. uint32_t offset;
  74. struct elem_info *ei_array;
  75. };
  76. /**
  77. * @msg_desc - Describe about the main/outer structure to be
  78. * encoded/decoded.
  79. *
  80. * @max_msg_len: Maximum possible length of the QMI message.
  81. * @ei_array: Array to provide information about a data structure.
  82. */
  83. struct msg_desc {
  84. uint16_t msg_id;
  85. int max_msg_len;
  86. struct elem_info *ei_array;
  87. };
  88. struct qmi_header {
  89. unsigned char cntl_flag;
  90. uint16_t txn_id;
  91. uint16_t msg_id;
  92. uint16_t msg_len;
  93. } __attribute__((__packed__));
  94. static inline void encode_qmi_header(unsigned char *buf,
  95. unsigned char cntl_flag, uint16_t txn_id,
  96. uint16_t msg_id, uint16_t msg_len)
  97. {
  98. struct qmi_header *hdr = (struct qmi_header *)buf;
  99. hdr->cntl_flag = cntl_flag;
  100. hdr->txn_id = txn_id;
  101. hdr->msg_id = msg_id;
  102. hdr->msg_len = msg_len;
  103. }
  104. static inline void decode_qmi_header(unsigned char *buf,
  105. unsigned char *cntl_flag, uint16_t *txn_id,
  106. uint16_t *msg_id, uint16_t *msg_len)
  107. {
  108. struct qmi_header *hdr = (struct qmi_header *)buf;
  109. *cntl_flag = hdr->cntl_flag;
  110. *txn_id = hdr->txn_id;
  111. *msg_id = hdr->msg_id;
  112. *msg_len = hdr->msg_len;
  113. }
  114. #ifdef CONFIG_QMI_ENCDEC
  115. /**
  116. * qmi_kernel_encode() - Encode to QMI message wire format
  117. * @desc: Pointer to structure descriptor.
  118. * @out_buf: Buffer to hold the encoded QMI message.
  119. * @out_buf_len: Length of the out buffer.
  120. * @in_c_struct: C Structure to be encoded.
  121. *
  122. * @return: size of encoded message on success, < 0 on error.
  123. */
  124. int qmi_kernel_encode(struct msg_desc *desc,
  125. void *out_buf, uint32_t out_buf_len,
  126. void *in_c_struct);
  127. /**
  128. * qmi_kernel_decode() - Decode to C Structure format
  129. * @desc: Pointer to structure descriptor.
  130. * @out_c_struct: Buffer to hold the decoded C structure.
  131. * @in_buf: Buffer containg the QMI message to be decoded.
  132. * @in_buf_len: Length of the incoming QMI message.
  133. *
  134. * @return: 0 on success, < 0 on error.
  135. */
  136. int qmi_kernel_decode(struct msg_desc *desc, void *out_c_struct,
  137. void *in_buf, uint32_t in_buf_len);
  138. /**
  139. * qmi_verify_max_msg_len() - Verify the maximum length of a QMI message
  140. * @desc: Pointer to structure descriptor.
  141. *
  142. * @return: true if the maximum message length embedded in structure
  143. * descriptor matches the calculated value, else false.
  144. */
  145. bool qmi_verify_max_msg_len(struct msg_desc *desc);
  146. #else
  147. static inline int qmi_kernel_encode(struct msg_desc *desc,
  148. void *out_buf, uint32_t out_buf_len,
  149. void *in_c_struct)
  150. {
  151. return -EOPNOTSUPP;
  152. }
  153. static inline int qmi_kernel_decode(struct msg_desc *desc,
  154. void *out_c_struct,
  155. void *in_buf, uint32_t in_buf_len)
  156. {
  157. return -EOPNOTSUPP;
  158. }
  159. static inline bool qmi_verify_max_msg_len(struct msg_desc *desc)
  160. {
  161. return false;
  162. }
  163. #endif
  164. #endif