xref: /openbmc/linux/include/net/nfc/digital.h (revision 7186aac9)
12025cf9eSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
24b10884eSThierry Escande /*
34b10884eSThierry Escande  * NFC Digital Protocol stack
44b10884eSThierry Escande  * Copyright (c) 2013, Intel Corporation.
54b10884eSThierry Escande  */
64b10884eSThierry Escande 
74b10884eSThierry Escande #ifndef __NFC_DIGITAL_H
84b10884eSThierry Escande #define __NFC_DIGITAL_H
94b10884eSThierry Escande 
104b10884eSThierry Escande #include <linux/skbuff.h>
114b10884eSThierry Escande #include <net/nfc/nfc.h>
124b10884eSThierry Escande 
134b10884eSThierry Escande /**
144b10884eSThierry Escande  * Configuration types for in_configure_hw and tg_configure_hw.
154b10884eSThierry Escande  */
164b10884eSThierry Escande enum {
174b10884eSThierry Escande 	NFC_DIGITAL_CONFIG_RF_TECH = 0,
184b10884eSThierry Escande 	NFC_DIGITAL_CONFIG_FRAMING,
194b10884eSThierry Escande };
204b10884eSThierry Escande 
214b10884eSThierry Escande /**
224b10884eSThierry Escande  * RF technology values passed as param argument to in_configure_hw and
234b10884eSThierry Escande  * tg_configure_hw for NFC_DIGITAL_CONFIG_RF_TECH configuration type.
244b10884eSThierry Escande  */
254b10884eSThierry Escande enum {
264b10884eSThierry Escande 	NFC_DIGITAL_RF_TECH_106A = 0,
274b10884eSThierry Escande 	NFC_DIGITAL_RF_TECH_212F,
284b10884eSThierry Escande 	NFC_DIGITAL_RF_TECH_424F,
29e487e4dcSMark A. Greer 	NFC_DIGITAL_RF_TECH_ISO15693,
3051d98fa4SMark A. Greer 	NFC_DIGITAL_RF_TECH_106B,
314b10884eSThierry Escande 
324b10884eSThierry Escande 	NFC_DIGITAL_RF_TECH_LAST,
334b10884eSThierry Escande };
344b10884eSThierry Escande 
354b10884eSThierry Escande /**
364b10884eSThierry Escande  * Framing configuration passed as param argument to in_configure_hw and
374b10884eSThierry Escande  * tg_configure_hw for NFC_DIGITAL_CONFIG_FRAMING configuration type.
384b10884eSThierry Escande  */
394b10884eSThierry Escande enum {
404b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCA_SHORT = 0,
414b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCA_STANDARD,
424b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A,
4355537c7eSMark A. Greer 	NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE,
444b10884eSThierry Escande 
454b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCA_T1T,
464b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCA_T2T,
4712e3d241SThierry Escande 	NFC_DIGITAL_FRAMING_NFCA_T4T,
484b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCA_NFC_DEP,
494b10884eSThierry Escande 
504b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCF,
514b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCF_T3T,
524b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFCF_NFC_DEP,
534b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED,
544b10884eSThierry Escande 
55e487e4dcSMark A. Greer 	NFC_DIGITAL_FRAMING_ISO15693_INVENTORY,
56ceeee42dSMark A. Greer 	NFC_DIGITAL_FRAMING_ISO15693_T5T,
57e487e4dcSMark A. Greer 
5851d98fa4SMark A. Greer 	NFC_DIGITAL_FRAMING_NFCB,
5951d98fa4SMark A. Greer 	NFC_DIGITAL_FRAMING_NFCB_T4T,
6051d98fa4SMark A. Greer 
614b10884eSThierry Escande 	NFC_DIGITAL_FRAMING_LAST,
624b10884eSThierry Escande };
634b10884eSThierry Escande 
644b10884eSThierry Escande #define DIGITAL_MDAA_NFCID1_SIZE 3
654b10884eSThierry Escande 
664b10884eSThierry Escande struct digital_tg_mdaa_params {
674b10884eSThierry Escande 	u16 sens_res;
684b10884eSThierry Escande 	u8 nfcid1[DIGITAL_MDAA_NFCID1_SIZE];
694b10884eSThierry Escande 	u8 sel_res;
704b10884eSThierry Escande 
714b10884eSThierry Escande 	u8 nfcid2[NFC_NFCID2_MAXSIZE];
724b10884eSThierry Escande 	u16 sc;
734b10884eSThierry Escande };
744b10884eSThierry Escande 
754b10884eSThierry Escande struct nfc_digital_dev;
764b10884eSThierry Escande 
774b10884eSThierry Escande /**
784b10884eSThierry Escande  * nfc_digital_cmd_complete_t - Definition of command result callback
794b10884eSThierry Escande  *
804b10884eSThierry Escande  * @ddev: nfc_digital_device ref
814b10884eSThierry Escande  * @arg: user data
824b10884eSThierry Escande  * @resp: response data
834b10884eSThierry Escande  *
844b10884eSThierry Escande  * resp pointer can be an error code and will be checked with IS_ERR() macro.
854b10884eSThierry Escande  * The callback is responsible for freeing resp sk_buff.
864b10884eSThierry Escande  */
874b10884eSThierry Escande typedef void (*nfc_digital_cmd_complete_t)(struct nfc_digital_dev *ddev,
884b10884eSThierry Escande 					   void *arg, struct sk_buff *resp);
894b10884eSThierry Escande 
904b10884eSThierry Escande /**
914b10884eSThierry Escande  * Device side NFC Digital operations
924b10884eSThierry Escande  *
934b10884eSThierry Escande  * Initiator mode:
944b10884eSThierry Escande  * @in_configure_hw: Hardware configuration for RF technology and communication
954b10884eSThierry Escande  *	framing in initiator mode. This is a synchronous function.
964b10884eSThierry Escande  * @in_send_cmd: Initiator mode data exchange using RF technology and framing
974b10884eSThierry Escande  *	previously set with in_configure_hw. The peer response is returned
984b10884eSThierry Escande  *	through callback cb. If an io error occurs or the peer didn't reply
994b10884eSThierry Escande  *	within the specified timeout (ms), the error code is passed back through
1004b10884eSThierry Escande  *	the resp pointer. This is an asynchronous function.
1014b10884eSThierry Escande  *
1024b10884eSThierry Escande  * Target mode: Only NFC-DEP protocol is supported in target mode.
1034b10884eSThierry Escande  * @tg_configure_hw: Hardware configuration for RF technology and communication
1044b10884eSThierry Escande  *	framing in target mode. This is a synchronous function.
1054b10884eSThierry Escande  * @tg_send_cmd: Target mode data exchange using RF technology and framing
1064b10884eSThierry Escande  *	previously set with tg_configure_hw. The peer next command is returned
1074b10884eSThierry Escande  *	through callback cb. If an io error occurs or the peer didn't reply
1084b10884eSThierry Escande  *	within the specified timeout (ms), the error code is passed back through
1094b10884eSThierry Escande  *	the resp pointer. This is an asynchronous function.
1104b10884eSThierry Escande  * @tg_listen: Put the device in listen mode waiting for data from the peer
1114b10884eSThierry Escande  *	device. This is an asynchronous function.
1124b10884eSThierry Escande  * @tg_listen_mdaa: If supported, put the device in automatic listen mode with
1134b10884eSThierry Escande  *	mode detection and automatic anti-collision. In this mode, the device
1144b10884eSThierry Escande  *	automatically detects the RF technology and executes the anti-collision
1154b10884eSThierry Escande  *	detection using the command responses specified in mdaa_params. The
1164b10884eSThierry Escande  *	mdaa_params structure contains SENS_RES, NFCID1, and SEL_RES for 106A RF
1174b10884eSThierry Escande  *	tech. NFCID2 and system code (sc) for 212F and 424F. The driver returns
1184b10884eSThierry Escande  *	the NFC-DEP ATR_REQ command through cb. The digital stack deducts the RF
1194b10884eSThierry Escande  *	tech by analyzing the SoD of the frame containing the ATR_REQ command.
1204b10884eSThierry Escande  *	This is an asynchronous function.
121bf30a67cSMark A. Greer  * @tg_listen_md: If supported, put the device in automatic listen mode with
122bf30a67cSMark A. Greer  *	mode detection but without automatic anti-collision. In this mode, the
123bf30a67cSMark A. Greer  *	device automatically detects the RF technology.  What the actual
124bf30a67cSMark A. Greer  *	RF technology is can be retrieved by calling @tg_get_rf_tech.
125bf30a67cSMark A. Greer  *	The digital stack will then perform the appropriate anti-collision
126bf30a67cSMark A. Greer  *	sequence.  This is an asynchronous function.
127bf30a67cSMark A. Greer  * @tg_get_rf_tech: Required when @tg_listen_md is supported, unused otherwise.
128bf30a67cSMark A. Greer  *	Return the RF Technology that was detected by the @tg_listen_md call.
129bf30a67cSMark A. Greer  *	This is a synchronous function.
1304b10884eSThierry Escande  *
1314b10884eSThierry Escande  * @switch_rf: Turns device radio on or off. The stack does not call explicitly
1324b10884eSThierry Escande  *	switch_rf to turn the radio on. A call to in|tg_configure_hw must turn
1334b10884eSThierry Escande  *	the device radio on.
1344b10884eSThierry Escande  * @abort_cmd: Discard the last sent command.
135444fb98eSThierry Escande  *
136444fb98eSThierry Escande  * Notes: Asynchronous functions have a timeout parameter. It is the driver
137444fb98eSThierry Escande  *	responsibility to call the digital stack back through the
138444fb98eSThierry Escande  *	nfc_digital_cmd_complete_t callback when no RF respsonse has been
139444fb98eSThierry Escande  *	received within the specified time (in milliseconds). In that case the
140444fb98eSThierry Escande  *	driver must set the resp sk_buff to ERR_PTR(-ETIMEDOUT).
141444fb98eSThierry Escande  *	Since the digital stack serializes commands to be sent, it's mandatory
142444fb98eSThierry Escande  *	for the driver to handle the timeout correctly. Otherwise the stack
143444fb98eSThierry Escande  *	would not be able to send new commands, waiting for the reply of the
144444fb98eSThierry Escande  *	current one.
1454b10884eSThierry Escande  */
1464b10884eSThierry Escande struct nfc_digital_ops {
1474b10884eSThierry Escande 	int (*in_configure_hw)(struct nfc_digital_dev *ddev, int type,
1484b10884eSThierry Escande 			       int param);
1494b10884eSThierry Escande 	int (*in_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
1504b10884eSThierry Escande 			   u16 timeout, nfc_digital_cmd_complete_t cb,
1514b10884eSThierry Escande 			   void *arg);
1524b10884eSThierry Escande 
1534b10884eSThierry Escande 	int (*tg_configure_hw)(struct nfc_digital_dev *ddev, int type,
1544b10884eSThierry Escande 			       int param);
1554b10884eSThierry Escande 	int (*tg_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb,
1564b10884eSThierry Escande 			   u16 timeout, nfc_digital_cmd_complete_t cb,
1574b10884eSThierry Escande 			   void *arg);
1584b10884eSThierry Escande 	int (*tg_listen)(struct nfc_digital_dev *ddev, u16 timeout,
1594b10884eSThierry Escande 			 nfc_digital_cmd_complete_t cb, void *arg);
1604b10884eSThierry Escande 	int (*tg_listen_mdaa)(struct nfc_digital_dev *ddev,
1614b10884eSThierry Escande 			      struct digital_tg_mdaa_params *mdaa_params,
1624b10884eSThierry Escande 			      u16 timeout, nfc_digital_cmd_complete_t cb,
1634b10884eSThierry Escande 			      void *arg);
164bf30a67cSMark A. Greer 	int (*tg_listen_md)(struct nfc_digital_dev *ddev, u16 timeout,
165bf30a67cSMark A. Greer 			    nfc_digital_cmd_complete_t cb, void *arg);
166bf30a67cSMark A. Greer 	int (*tg_get_rf_tech)(struct nfc_digital_dev *ddev, u8 *rf_tech);
1674b10884eSThierry Escande 
1684b10884eSThierry Escande 	int (*switch_rf)(struct nfc_digital_dev *ddev, bool on);
1694b10884eSThierry Escande 	void (*abort_cmd)(struct nfc_digital_dev *ddev);
1704b10884eSThierry Escande };
1714b10884eSThierry Escande 
17259ee2361SThierry Escande #define NFC_DIGITAL_POLL_MODE_COUNT_MAX	6 /* 106A, 212F, and 424F in & tg */
17359ee2361SThierry Escande 
17459ee2361SThierry Escande typedef int (*digital_poll_t)(struct nfc_digital_dev *ddev, u8 rf_tech);
17559ee2361SThierry Escande 
17659ee2361SThierry Escande struct digital_poll_tech {
17759ee2361SThierry Escande 	u8 rf_tech;
17859ee2361SThierry Escande 	digital_poll_t poll_func;
17959ee2361SThierry Escande };
18059ee2361SThierry Escande 
1814b10884eSThierry Escande /**
1824b10884eSThierry Escande  * Driver capabilities - bit mask made of the following values
1834b10884eSThierry Escande  *
1844b10884eSThierry Escande  * @NFC_DIGITAL_DRV_CAPS_IN_CRC: The driver handles CRC calculation in initiator
1854b10884eSThierry Escande  *	mode.
1864b10884eSThierry Escande  * @NFC_DIGITAL_DRV_CAPS_TG_CRC: The driver handles CRC calculation in target
1874b10884eSThierry Escande  *	mode.
1884b10884eSThierry Escande  */
1894b10884eSThierry Escande #define NFC_DIGITAL_DRV_CAPS_IN_CRC	0x0001
1904b10884eSThierry Escande #define NFC_DIGITAL_DRV_CAPS_TG_CRC	0x0002
1914b10884eSThierry Escande 
1924b10884eSThierry Escande struct nfc_digital_dev {
1934b10884eSThierry Escande 	struct nfc_dev *nfc_dev;
194*7186aac9SKrzysztof Kozlowski 	const struct nfc_digital_ops *ops;
1954b10884eSThierry Escande 
1964b10884eSThierry Escande 	u32 protocols;
1974b10884eSThierry Escande 
1984b10884eSThierry Escande 	int tx_headroom;
1994b10884eSThierry Escande 	int tx_tailroom;
2004b10884eSThierry Escande 
2014b10884eSThierry Escande 	u32 driver_capabilities;
2024b10884eSThierry Escande 	void *driver_data;
20359ee2361SThierry Escande 
20459ee2361SThierry Escande 	struct digital_poll_tech poll_techs[NFC_DIGITAL_POLL_MODE_COUNT_MAX];
20559ee2361SThierry Escande 	u8 poll_tech_count;
20659ee2361SThierry Escande 	u8 poll_tech_index;
20759ee2361SThierry Escande 	struct mutex poll_lock;
20859ee2361SThierry Escande 
20959ee2361SThierry Escande 	struct work_struct cmd_work;
21059ee2361SThierry Escande 	struct work_struct cmd_complete_work;
21159ee2361SThierry Escande 	struct list_head cmd_queue;
21259ee2361SThierry Escande 	struct mutex cmd_lock;
21359ee2361SThierry Escande 
2147854a445SThierry Escande 	struct delayed_work poll_work;
21559ee2361SThierry Escande 
21659ee2361SThierry Escande 	u8 curr_protocol;
21759ee2361SThierry Escande 	u8 curr_rf_tech;
21859ee2361SThierry Escande 	u8 curr_nfc_dep_pni;
21905afedcbSMark A. Greer 	u8 did;
2201a09c56fSThierry Escande 	u16 dep_rwt;
2212c66daecSThierry Escande 
222b08147cbSMark A. Greer 	u8 local_payload_max;
223b08147cbSMark A. Greer 	u8 remote_payload_max;
224b08147cbSMark A. Greer 
2253bd2a5bcSMark A. Greer 	struct sk_buff *chaining_skb;
2263bd2a5bcSMark A. Greer 	struct digital_data_exch *data_exch;
2273bd2a5bcSMark A. Greer 
228384ab1d1SMark A. Greer 	int atn_count;
229a80509c7SMark A. Greer 	int nack_count;
230a80509c7SMark A. Greer 
23149dbb14eSMark A. Greer 	struct sk_buff *saved_skb;
23249dbb14eSMark A. Greer 
23312e3d241SThierry Escande 	u16 target_fsc;
23412e3d241SThierry Escande 
2352c66daecSThierry Escande 	int (*skb_check_crc)(struct sk_buff *skb);
2362c66daecSThierry Escande 	void (*skb_add_crc)(struct sk_buff *skb);
2374b10884eSThierry Escande };
2384b10884eSThierry Escande 
239*7186aac9SKrzysztof Kozlowski struct nfc_digital_dev *nfc_digital_allocate_device(const struct nfc_digital_ops *ops,
2404b10884eSThierry Escande 						    __u32 supported_protocols,
2414b10884eSThierry Escande 						    __u32 driver_capabilities,
2424b10884eSThierry Escande 						    int tx_headroom,
2434b10884eSThierry Escande 						    int tx_tailroom);
2444b10884eSThierry Escande void nfc_digital_free_device(struct nfc_digital_dev *ndev);
2454b10884eSThierry Escande int nfc_digital_register_device(struct nfc_digital_dev *ndev);
2464b10884eSThierry Escande void nfc_digital_unregister_device(struct nfc_digital_dev *ndev);
2474b10884eSThierry Escande 
nfc_digital_set_parent_dev(struct nfc_digital_dev * ndev,struct device * dev)2484b10884eSThierry Escande static inline void nfc_digital_set_parent_dev(struct nfc_digital_dev *ndev,
2494b10884eSThierry Escande 					      struct device *dev)
2504b10884eSThierry Escande {
2514b10884eSThierry Escande 	nfc_set_parent_dev(ndev->nfc_dev, dev);
2524b10884eSThierry Escande }
2534b10884eSThierry Escande 
nfc_digital_set_drvdata(struct nfc_digital_dev * dev,void * data)2544b10884eSThierry Escande static inline void nfc_digital_set_drvdata(struct nfc_digital_dev *dev,
2554b10884eSThierry Escande 					   void *data)
2564b10884eSThierry Escande {
2574b10884eSThierry Escande 	dev->driver_data = data;
2584b10884eSThierry Escande }
2594b10884eSThierry Escande 
nfc_digital_get_drvdata(struct nfc_digital_dev * dev)2604b10884eSThierry Escande static inline void *nfc_digital_get_drvdata(struct nfc_digital_dev *dev)
2614b10884eSThierry Escande {
2624b10884eSThierry Escande 	return dev->driver_data;
2634b10884eSThierry Escande }
2644b10884eSThierry Escande 
2654b10884eSThierry Escande #endif /* __NFC_DIGITAL_H */
266