Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * NFC Digital Protocol stack
0004  * Copyright (c) 2013, Intel Corporation.
0005  */
0006 
0007 #ifndef __NFC_DIGITAL_H
0008 #define __NFC_DIGITAL_H
0009 
0010 #include <linux/skbuff.h>
0011 #include <net/nfc/nfc.h>
0012 
0013 /**
0014  * Configuration types for in_configure_hw and tg_configure_hw.
0015  */
0016 enum {
0017     NFC_DIGITAL_CONFIG_RF_TECH = 0,
0018     NFC_DIGITAL_CONFIG_FRAMING,
0019 };
0020 
0021 /**
0022  * RF technology values passed as param argument to in_configure_hw and
0023  * tg_configure_hw for NFC_DIGITAL_CONFIG_RF_TECH configuration type.
0024  */
0025 enum {
0026     NFC_DIGITAL_RF_TECH_106A = 0,
0027     NFC_DIGITAL_RF_TECH_212F,
0028     NFC_DIGITAL_RF_TECH_424F,
0029     NFC_DIGITAL_RF_TECH_ISO15693,
0030     NFC_DIGITAL_RF_TECH_106B,
0031 
0032     NFC_DIGITAL_RF_TECH_LAST,
0033 };
0034 
0035 /**
0036  * Framing configuration passed as param argument to in_configure_hw and
0037  * tg_configure_hw for NFC_DIGITAL_CONFIG_FRAMING configuration type.
0038  */
0039 enum {
0040     NFC_DIGITAL_FRAMING_NFCA_SHORT = 0,
0041     NFC_DIGITAL_FRAMING_NFCA_STANDARD,
0042     NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A,
0043     NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE,
0044 
0045     NFC_DIGITAL_FRAMING_NFCA_T1T,
0046     NFC_DIGITAL_FRAMING_NFCA_T2T,
0047     NFC_DIGITAL_FRAMING_NFCA_T4T,
0048     NFC_DIGITAL_FRAMING_NFCA_NFC_DEP,
0049 
0050     NFC_DIGITAL_FRAMING_NFCF,
0051     NFC_DIGITAL_FRAMING_NFCF_T3T,
0052     NFC_DIGITAL_FRAMING_NFCF_NFC_DEP,
0053     NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED,
0054 
0055     NFC_DIGITAL_FRAMING_ISO15693_INVENTORY,
0056     NFC_DIGITAL_FRAMING_ISO15693_T5T,
0057 
0058     NFC_DIGITAL_FRAMING_NFCB,
0059     NFC_DIGITAL_FRAMING_NFCB_T4T,
0060 
0061     NFC_DIGITAL_FRAMING_LAST,
0062 };
0063 
0064 #define DIGITAL_MDAA_NFCID1_SIZE 3
0065 
0066 struct digital_tg_mdaa_params {
0067     u16 sens_res;
0068     u8 nfcid1[DIGITAL_MDAA_NFCID1_SIZE];
0069     u8 sel_res;
0070 
0071     u8 nfcid2[NFC_NFCID2_MAXSIZE];
0072     u16 sc;
0073 };
0074 
0075 struct nfc_digital_dev;
0076 
0077 /**
0078  * nfc_digital_cmd_complete_t - Definition of command result callback
0079  *
0080  * @ddev: nfc_digital_device ref
0081  * @arg: user data
0082  * @resp: response data
0083  *
0084  * resp pointer can be an error code and will be checked with IS_ERR() macro.
0085  * The callback is responsible for freeing resp sk_buff.
0086  */
0087 typedef void (*nfc_digital_cmd_complete_t)(struct nfc_digital_dev *ddev,
0088                        void *arg, struct sk_buff *resp);
0089 
0090 /**
0091  * Device side NFC Digital operations
0092  *
0093  * Initiator mode:
0094  * @in_configure_hw: Hardware configuration for RF technology and communication
0095  *  framing in initiator mode. This is a synchronous function.
0096  * @in_send_cmd: Initiator mode data exchange using RF technology and framing
0097  *  previously set with in_configure_hw. The peer response is returned
0098  *  through callback cb. If an io error occurs or the peer didn't reply
0099  *  within the specified timeout (ms), the error code is passed back through
0100  *  the resp pointer. This is an asynchronous function.
0101  *
0102  * Target mode: Only NFC-DEP protocol is supported in target mode.
0103  * @tg_configure_hw: Hardware configuration for RF technology and communication
0104  *  framing in target mode. This is a synchronous function.
0105  * @tg_send_cmd: Target mode data exchange using RF technology and framing
0106  *  previously set with tg_configure_hw. The peer next command is returned
0107  *  through callback cb. If an io error occurs or the peer didn't reply
0108  *  within the specified timeout (ms), the error code is passed back through
0109  *  the resp pointer. This is an asynchronous function.
0110  * @tg_listen: Put the device in listen mode waiting for data from the peer
0111  *  device. This is an asynchronous function.
0112  * @tg_listen_mdaa: If supported, put the device in automatic listen mode with
0113  *  mode detection and automatic anti-collision. In this mode, the device
0114  *  automatically detects the RF technology and executes the anti-collision
0115  *  detection using the command responses specified in mdaa_params. The
0116  *  mdaa_params structure contains SENS_RES, NFCID1, and SEL_RES for 106A RF
0117  *  tech. NFCID2 and system code (sc) for 212F and 424F. The driver returns
0118  *  the NFC-DEP ATR_REQ command through cb. The digital stack deducts the RF
0119  *  tech by analyzing the SoD of the frame containing the ATR_REQ command.
0120  *  This is an asynchronous function.
0121  * @tg_listen_md: If supported, put the device in automatic listen mode with
0122  *  mode detection but without automatic anti-collision. In this mode, the
0123  *  device automatically detects the RF technology.  What the actual
0124  *  RF technology is can be retrieved by calling @tg_get_rf_tech.
0125  *  The digital stack will then perform the appropriate anti-collision
0126  *  sequence.  This is an asynchronous function.
0127  * @tg_get_rf_tech: Required when @tg_listen_md is supported, unused otherwise.
0128  *  Return the RF Technology that was detected by the @tg_listen_md call.
0129  *  This is a synchronous function.
0130  *
0131  * @switch_rf: Turns device radio on or off. The stack does not call explicitly
0132  *  switch_rf to turn the radio on. A call to in|tg_configure_hw must turn
0133  *  the device radio on.
0134  * @abort_cmd: Discard the last sent command.
0135  *
0136  * Notes: Asynchronous functions have a timeout parameter. It is the driver
0137  *  responsibility to call the digital stack back through the
0138  *  nfc_digital_cmd_complete_t callback when no RF respsonse has been
0139  *  received within the specified time (in milliseconds). In that case the
0140  *  driver must set the resp sk_buff to ERR_PTR(-ETIMEDOUT).
0141  *  Since the digital stack serializes commands to be sent, it's mandatory
0142  *  for the driver to handle the timeout correctly. Otherwise the stack
0143  *  would not be able to send new commands, waiting for the reply of the
0144  *  current one.
0145  */
0146 struct nfc_digital_ops {
0147     int (*in_configure_hw)(struct nfc_digital_dev *ddev, int type,
0148                    int param);
0149     int (*in_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
0150                u16 timeout, nfc_digital_cmd_complete_t cb,
0151                void *arg);
0152 
0153     int (*tg_configure_hw)(struct nfc_digital_dev *ddev, int type,
0154                    int param);
0155     int (*tg_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
0156                u16 timeout, nfc_digital_cmd_complete_t cb,
0157                void *arg);
0158     int (*tg_listen)(struct nfc_digital_dev *ddev, u16 timeout,
0159              nfc_digital_cmd_complete_t cb, void *arg);
0160     int (*tg_listen_mdaa)(struct nfc_digital_dev *ddev,
0161                   struct digital_tg_mdaa_params *mdaa_params,
0162                   u16 timeout, nfc_digital_cmd_complete_t cb,
0163                   void *arg);
0164     int (*tg_listen_md)(struct nfc_digital_dev *ddev, u16 timeout,
0165                 nfc_digital_cmd_complete_t cb, void *arg);
0166     int (*tg_get_rf_tech)(struct nfc_digital_dev *ddev, u8 *rf_tech);
0167 
0168     int (*switch_rf)(struct nfc_digital_dev *ddev, bool on);
0169     void (*abort_cmd)(struct nfc_digital_dev *ddev);
0170 };
0171 
0172 #define NFC_DIGITAL_POLL_MODE_COUNT_MAX 6 /* 106A, 212F, and 424F in & tg */
0173 
0174 typedef int (*digital_poll_t)(struct nfc_digital_dev *ddev, u8 rf_tech);
0175 
0176 struct digital_poll_tech {
0177     u8 rf_tech;
0178     digital_poll_t poll_func;
0179 };
0180 
0181 /**
0182  * Driver capabilities - bit mask made of the following values
0183  *
0184  * @NFC_DIGITAL_DRV_CAPS_IN_CRC: The driver handles CRC calculation in initiator
0185  *  mode.
0186  * @NFC_DIGITAL_DRV_CAPS_TG_CRC: The driver handles CRC calculation in target
0187  *  mode.
0188  */
0189 #define NFC_DIGITAL_DRV_CAPS_IN_CRC 0x0001
0190 #define NFC_DIGITAL_DRV_CAPS_TG_CRC 0x0002
0191 
0192 struct nfc_digital_dev {
0193     struct nfc_dev *nfc_dev;
0194     const struct nfc_digital_ops *ops;
0195 
0196     u32 protocols;
0197 
0198     int tx_headroom;
0199     int tx_tailroom;
0200 
0201     u32 driver_capabilities;
0202     void *driver_data;
0203 
0204     struct digital_poll_tech poll_techs[NFC_DIGITAL_POLL_MODE_COUNT_MAX];
0205     u8 poll_tech_count;
0206     u8 poll_tech_index;
0207     struct mutex poll_lock;
0208 
0209     struct work_struct cmd_work;
0210     struct work_struct cmd_complete_work;
0211     struct list_head cmd_queue;
0212     struct mutex cmd_lock;
0213 
0214     struct delayed_work poll_work;
0215 
0216     u8 curr_protocol;
0217     u8 curr_rf_tech;
0218     u8 curr_nfc_dep_pni;
0219     u8 did;
0220     u16 dep_rwt;
0221 
0222     u8 local_payload_max;
0223     u8 remote_payload_max;
0224 
0225     struct sk_buff *chaining_skb;
0226     struct digital_data_exch *data_exch;
0227 
0228     int atn_count;
0229     int nack_count;
0230 
0231     struct sk_buff *saved_skb;
0232 
0233     u16 target_fsc;
0234 
0235     int (*skb_check_crc)(struct sk_buff *skb);
0236     void (*skb_add_crc)(struct sk_buff *skb);
0237 };
0238 
0239 struct nfc_digital_dev *nfc_digital_allocate_device(const struct nfc_digital_ops *ops,
0240                             __u32 supported_protocols,
0241                             __u32 driver_capabilities,
0242                             int tx_headroom,
0243                             int tx_tailroom);
0244 void nfc_digital_free_device(struct nfc_digital_dev *ndev);
0245 int nfc_digital_register_device(struct nfc_digital_dev *ndev);
0246 void nfc_digital_unregister_device(struct nfc_digital_dev *ndev);
0247 
0248 static inline void nfc_digital_set_parent_dev(struct nfc_digital_dev *ndev,
0249                           struct device *dev)
0250 {
0251     nfc_set_parent_dev(ndev->nfc_dev, dev);
0252 }
0253 
0254 static inline void nfc_digital_set_drvdata(struct nfc_digital_dev *dev,
0255                        void *data)
0256 {
0257     dev->driver_data = data;
0258 }
0259 
0260 static inline void *nfc_digital_get_drvdata(struct nfc_digital_dev *dev)
0261 {
0262     return dev->driver_data;
0263 }
0264 
0265 #endif /* __NFC_DIGITAL_H */