Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
0004  * Copyright (c) 2017, Linaro Ltd.
0005  */
0006 #ifndef __QMI_HELPERS_H__
0007 #define __QMI_HELPERS_H__
0008 
0009 #include <linux/completion.h>
0010 #include <linux/idr.h>
0011 #include <linux/list.h>
0012 #include <linux/qrtr.h>
0013 #include <linux/types.h>
0014 #include <linux/workqueue.h>
0015 
0016 struct socket;
0017 
0018 /**
0019  * struct qmi_header - wireformat header of QMI messages
0020  * @type:   type of message
0021  * @txn_id: transaction id
0022  * @msg_id: message id
0023  * @msg_len:    length of message payload following header
0024  */
0025 struct qmi_header {
0026     u8 type;
0027     u16 txn_id;
0028     u16 msg_id;
0029     u16 msg_len;
0030 } __packed;
0031 
0032 #define QMI_REQUEST 0
0033 #define QMI_RESPONSE    2
0034 #define QMI_INDICATION  4
0035 
0036 #define QMI_COMMON_TLV_TYPE 0
0037 
0038 enum qmi_elem_type {
0039     QMI_EOTI,
0040     QMI_OPT_FLAG,
0041     QMI_DATA_LEN,
0042     QMI_UNSIGNED_1_BYTE,
0043     QMI_UNSIGNED_2_BYTE,
0044     QMI_UNSIGNED_4_BYTE,
0045     QMI_UNSIGNED_8_BYTE,
0046     QMI_SIGNED_2_BYTE_ENUM,
0047     QMI_SIGNED_4_BYTE_ENUM,
0048     QMI_STRUCT,
0049     QMI_STRING,
0050 };
0051 
0052 enum qmi_array_type {
0053     NO_ARRAY,
0054     STATIC_ARRAY,
0055     VAR_LEN_ARRAY,
0056 };
0057 
0058 /**
0059  * struct qmi_elem_info - describes how to encode a single QMI element
0060  * @data_type:  Data type of this element.
0061  * @elem_len:   Array length of this element, if an array.
0062  * @elem_size:  Size of a single instance of this data type.
0063  * @array_type: Array type of this element.
0064  * @tlv_type:   QMI message specific type to identify which element
0065  *      is present in an incoming message.
0066  * @offset: Specifies the offset of the first instance of this
0067  *      element in the data structure.
0068  * @ei_array:   Null-terminated array of @qmi_elem_info to describe nested
0069  *      structures.
0070  */
0071 struct qmi_elem_info {
0072     enum qmi_elem_type data_type;
0073     u32 elem_len;
0074     u32 elem_size;
0075     enum qmi_array_type array_type;
0076     u8 tlv_type;
0077     u32 offset;
0078     struct qmi_elem_info *ei_array;
0079 };
0080 
0081 #define QMI_RESULT_SUCCESS_V01          0
0082 #define QMI_RESULT_FAILURE_V01          1
0083 
0084 #define QMI_ERR_NONE_V01            0
0085 #define QMI_ERR_MALFORMED_MSG_V01       1
0086 #define QMI_ERR_NO_MEMORY_V01           2
0087 #define QMI_ERR_INTERNAL_V01            3
0088 #define QMI_ERR_CLIENT_IDS_EXHAUSTED_V01    5
0089 #define QMI_ERR_INVALID_ID_V01          41
0090 #define QMI_ERR_ENCODING_V01            58
0091 #define QMI_ERR_DISABLED_V01                    69
0092 #define QMI_ERR_INCOMPATIBLE_STATE_V01      90
0093 #define QMI_ERR_NOT_SUPPORTED_V01       94
0094 
0095 /**
0096  * struct qmi_response_type_v01 - common response header (decoded)
0097  * @result: result of the transaction
0098  * @error:  error value, when @result is QMI_RESULT_FAILURE_V01
0099  */
0100 struct qmi_response_type_v01 {
0101     u16 result;
0102     u16 error;
0103 };
0104 
0105 extern struct qmi_elem_info qmi_response_type_v01_ei[];
0106 
0107 /**
0108  * struct qmi_service - context to track lookup-results
0109  * @service:    service type
0110  * @version:    version of the @service
0111  * @instance:   instance id of the @service
0112  * @node:   node of the service
0113  * @port:   port of the service
0114  * @priv:   handle for client's use
0115  * @list_node:  list_head for house keeping
0116  */
0117 struct qmi_service {
0118     unsigned int service;
0119     unsigned int version;
0120     unsigned int instance;
0121 
0122     unsigned int node;
0123     unsigned int port;
0124 
0125     void *priv;
0126     struct list_head list_node;
0127 };
0128 
0129 struct qmi_handle;
0130 
0131 /**
0132  * struct qmi_ops - callbacks for qmi_handle
0133  * @new_server:     inform client of a new_server lookup-result, returning
0134  *                      successfully from this call causes the library to call
0135  *                      @del_server as the service is removed from the
0136  *                      lookup-result. @priv of the qmi_service can be used by
0137  *                      the client
0138  * @del_server:     inform client of a del_server lookup-result
0139  * @net_reset:      inform client that the name service was restarted and
0140  *                      that and any state needs to be released
0141  * @msg_handler:    invoked for incoming messages, allows a client to
0142  *                      override the usual QMI message handler
0143  * @bye:                inform a client that all clients from a node are gone
0144  * @del_client:         inform a client that a particular client is gone
0145  */
0146 struct qmi_ops {
0147     int (*new_server)(struct qmi_handle *qmi, struct qmi_service *svc);
0148     void (*del_server)(struct qmi_handle *qmi, struct qmi_service *svc);
0149     void (*net_reset)(struct qmi_handle *qmi);
0150     void (*msg_handler)(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
0151                 const void *data, size_t count);
0152     void (*bye)(struct qmi_handle *qmi, unsigned int node);
0153     void (*del_client)(struct qmi_handle *qmi,
0154                unsigned int node, unsigned int port);
0155 };
0156 
0157 /**
0158  * struct qmi_txn - transaction context
0159  * @qmi:    QMI handle this transaction is associated with
0160  * @id:     transaction id
0161  * @lock:   for synchronization between handler and waiter of messages
0162  * @completion: completion object as the transaction receives a response
0163  * @result: result code for the completed transaction
0164  * @ei:     description of the QMI encoded response (optional)
0165  * @dest:   destination buffer to decode message into (optional)
0166  */
0167 struct qmi_txn {
0168     struct qmi_handle *qmi;
0169 
0170     u16 id;
0171 
0172     struct mutex lock;
0173     struct completion completion;
0174     int result;
0175 
0176     struct qmi_elem_info *ei;
0177     void *dest;
0178 };
0179 
0180 /**
0181  * struct qmi_msg_handler - description of QMI message handler
0182  * @type:   type of message
0183  * @msg_id: message id
0184  * @ei:     description of the QMI encoded message
0185  * @decoded_size:   size of the decoded object
0186  * @fn:     function to invoke as the message is decoded
0187  */
0188 struct qmi_msg_handler {
0189     unsigned int type;
0190     unsigned int msg_id;
0191 
0192     struct qmi_elem_info *ei;
0193 
0194     size_t decoded_size;
0195     void (*fn)(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
0196            struct qmi_txn *txn, const void *decoded);
0197 };
0198 
0199 /**
0200  * struct qmi_handle - QMI context
0201  * @sock:   socket handle
0202  * @sock_lock:  synchronization of @sock modifications
0203  * @sq:     sockaddr of @sock
0204  * @work:   work for handling incoming messages
0205  * @wq:     workqueue to post @work on
0206  * @recv_buf:   scratch buffer for handling incoming messages
0207  * @recv_buf_size:  size of @recv_buf
0208  * @lookups:        list of registered lookup requests
0209  * @lookup_results: list of lookup-results advertised to the client
0210  * @services:       list of registered services (by this client)
0211  * @ops:    reference to callbacks
0212  * @txns:   outstanding transactions
0213  * @txn_lock:   lock for modifications of @txns
0214  * @handlers:   list of handlers for incoming messages
0215  */
0216 struct qmi_handle {
0217     struct socket *sock;
0218     struct mutex sock_lock;
0219 
0220     struct sockaddr_qrtr sq;
0221 
0222     struct work_struct work;
0223     struct workqueue_struct *wq;
0224 
0225     void *recv_buf;
0226     size_t recv_buf_size;
0227 
0228     struct list_head lookups;
0229     struct list_head lookup_results;
0230     struct list_head services;
0231 
0232     struct qmi_ops ops;
0233 
0234     struct idr txns;
0235     struct mutex txn_lock;
0236 
0237     const struct qmi_msg_handler *handlers;
0238 };
0239 
0240 int qmi_add_lookup(struct qmi_handle *qmi, unsigned int service,
0241            unsigned int version, unsigned int instance);
0242 int qmi_add_server(struct qmi_handle *qmi, unsigned int service,
0243            unsigned int version, unsigned int instance);
0244 
0245 int qmi_handle_init(struct qmi_handle *qmi, size_t max_msg_len,
0246             const struct qmi_ops *ops,
0247             const struct qmi_msg_handler *handlers);
0248 void qmi_handle_release(struct qmi_handle *qmi);
0249 
0250 ssize_t qmi_send_request(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
0251              struct qmi_txn *txn, int msg_id, size_t len,
0252              struct qmi_elem_info *ei, const void *c_struct);
0253 ssize_t qmi_send_response(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
0254               struct qmi_txn *txn, int msg_id, size_t len,
0255               struct qmi_elem_info *ei, const void *c_struct);
0256 ssize_t qmi_send_indication(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
0257                 int msg_id, size_t len, struct qmi_elem_info *ei,
0258                 const void *c_struct);
0259 
0260 void *qmi_encode_message(int type, unsigned int msg_id, size_t *len,
0261              unsigned int txn_id, struct qmi_elem_info *ei,
0262              const void *c_struct);
0263 
0264 int qmi_decode_message(const void *buf, size_t len,
0265                struct qmi_elem_info *ei, void *c_struct);
0266 
0267 int qmi_txn_init(struct qmi_handle *qmi, struct qmi_txn *txn,
0268          struct qmi_elem_info *ei, void *c_struct);
0269 int qmi_txn_wait(struct qmi_txn *txn, unsigned long timeout);
0270 void qmi_txn_cancel(struct qmi_txn *txn);
0271 
0272 #endif