nfc.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  4. * Copyright (C) 2014 Marvell International Ltd.
  5. *
  6. * Authors:
  7. * Lauro Ramos Venancio <[email protected]>
  8. * Aloisio Almeida Jr <[email protected]>
  9. */
  10. #ifndef __NET_NFC_H
  11. #define __NET_NFC_H
  12. #include <linux/nfc.h>
  13. #include <linux/device.h>
  14. #include <linux/skbuff.h>
  15. #define nfc_dbg(dev, fmt, ...) dev_dbg((dev), "NFC: " fmt, ##__VA_ARGS__)
  16. #define nfc_info(dev, fmt, ...) dev_info((dev), "NFC: " fmt, ##__VA_ARGS__)
  17. #define nfc_err(dev, fmt, ...) dev_err((dev), "NFC: " fmt, ##__VA_ARGS__)
  18. struct nfc_phy_ops {
  19. int (*write)(void *dev_id, struct sk_buff *skb);
  20. int (*enable)(void *dev_id);
  21. void (*disable)(void *dev_id);
  22. };
  23. struct nfc_dev;
  24. /**
  25. * data_exchange_cb_t - Definition of nfc_data_exchange callback
  26. *
  27. * @context: nfc_data_exchange cb_context parameter
  28. * @skb: response data
  29. * @err: If an error has occurred during data exchange, it is the
  30. * error number. Zero means no error.
  31. *
  32. * When a rx or tx package is lost or corrupted or the target gets out
  33. * of the operating field, err is -EIO.
  34. */
  35. typedef void (*data_exchange_cb_t)(void *context, struct sk_buff *skb,
  36. int err);
  37. typedef void (*se_io_cb_t)(void *context, u8 *apdu, size_t apdu_len, int err);
  38. struct nfc_target;
  39. struct nfc_ops {
  40. int (*dev_up)(struct nfc_dev *dev);
  41. int (*dev_down)(struct nfc_dev *dev);
  42. int (*start_poll)(struct nfc_dev *dev,
  43. u32 im_protocols, u32 tm_protocols);
  44. void (*stop_poll)(struct nfc_dev *dev);
  45. int (*dep_link_up)(struct nfc_dev *dev, struct nfc_target *target,
  46. u8 comm_mode, u8 *gb, size_t gb_len);
  47. int (*dep_link_down)(struct nfc_dev *dev);
  48. int (*activate_target)(struct nfc_dev *dev, struct nfc_target *target,
  49. u32 protocol);
  50. void (*deactivate_target)(struct nfc_dev *dev,
  51. struct nfc_target *target, u8 mode);
  52. int (*im_transceive)(struct nfc_dev *dev, struct nfc_target *target,
  53. struct sk_buff *skb, data_exchange_cb_t cb,
  54. void *cb_context);
  55. int (*tm_send)(struct nfc_dev *dev, struct sk_buff *skb);
  56. int (*check_presence)(struct nfc_dev *dev, struct nfc_target *target);
  57. int (*fw_download)(struct nfc_dev *dev, const char *firmware_name);
  58. /* Secure Element API */
  59. int (*discover_se)(struct nfc_dev *dev);
  60. int (*enable_se)(struct nfc_dev *dev, u32 se_idx);
  61. int (*disable_se)(struct nfc_dev *dev, u32 se_idx);
  62. int (*se_io) (struct nfc_dev *dev, u32 se_idx,
  63. u8 *apdu, size_t apdu_length,
  64. se_io_cb_t cb, void *cb_context);
  65. };
  66. #define NFC_TARGET_IDX_ANY -1
  67. #define NFC_MAX_GT_LEN 48
  68. #define NFC_ATR_RES_GT_OFFSET 15
  69. #define NFC_ATR_REQ_GT_OFFSET 14
  70. /**
  71. * struct nfc_target - NFC target descriptiom
  72. *
  73. * @sens_res: 2 bytes describing the target SENS_RES response, if the target
  74. * is a type A one. The %sens_res most significant byte must be byte 2
  75. * as described by the NFC Forum digital specification (i.e. the platform
  76. * configuration one) while %sens_res least significant byte is byte 1.
  77. */
  78. struct nfc_target {
  79. u32 idx;
  80. u32 supported_protocols;
  81. u16 sens_res;
  82. u8 sel_res;
  83. u8 nfcid1_len;
  84. u8 nfcid1[NFC_NFCID1_MAXSIZE];
  85. u8 nfcid2_len;
  86. u8 nfcid2[NFC_NFCID2_MAXSIZE];
  87. u8 sensb_res_len;
  88. u8 sensb_res[NFC_SENSB_RES_MAXSIZE];
  89. u8 sensf_res_len;
  90. u8 sensf_res[NFC_SENSF_RES_MAXSIZE];
  91. u8 hci_reader_gate;
  92. u8 logical_idx;
  93. u8 is_iso15693;
  94. u8 iso15693_dsfid;
  95. u8 iso15693_uid[NFC_ISO15693_UID_MAXSIZE];
  96. };
  97. /**
  98. * nfc_se - A structure for NFC accessible secure elements.
  99. *
  100. * @idx: The secure element index. User space will enable or
  101. * disable a secure element by its index.
  102. * @type: The secure element type. It can be SE_UICC or
  103. * SE_EMBEDDED.
  104. * @state: The secure element state, either enabled or disabled.
  105. *
  106. */
  107. struct nfc_se {
  108. struct list_head list;
  109. u32 idx;
  110. u16 type;
  111. u16 state;
  112. };
  113. /**
  114. * nfc_evt_transaction - A struct for NFC secure element event transaction.
  115. *
  116. * @aid: The application identifier triggering the event
  117. *
  118. * @aid_len: The application identifier length [5:16]
  119. *
  120. * @params: The application parameters transmitted during the transaction
  121. *
  122. * @params_len: The applications parameters length [0:255]
  123. *
  124. */
  125. #define NFC_MIN_AID_LENGTH 5
  126. #define NFC_MAX_AID_LENGTH 16
  127. #define NFC_MAX_PARAMS_LENGTH 255
  128. #define NFC_EVT_TRANSACTION_AID_TAG 0x81
  129. #define NFC_EVT_TRANSACTION_PARAMS_TAG 0x82
  130. struct nfc_evt_transaction {
  131. u32 aid_len;
  132. u8 aid[NFC_MAX_AID_LENGTH];
  133. u8 params_len;
  134. u8 params[];
  135. } __packed;
  136. struct nfc_genl_data {
  137. u32 poll_req_portid;
  138. struct mutex genl_data_mutex;
  139. };
  140. struct nfc_vendor_cmd {
  141. __u32 vendor_id;
  142. __u32 subcmd;
  143. int (*doit)(struct nfc_dev *dev, void *data, size_t data_len);
  144. };
  145. struct nfc_dev {
  146. int idx;
  147. u32 target_next_idx;
  148. struct nfc_target *targets;
  149. int n_targets;
  150. int targets_generation;
  151. struct device dev;
  152. bool dev_up;
  153. bool fw_download_in_progress;
  154. u8 rf_mode;
  155. bool polling;
  156. struct nfc_target *active_target;
  157. bool dep_link_up;
  158. struct nfc_genl_data genl_data;
  159. u32 supported_protocols;
  160. struct list_head secure_elements;
  161. int tx_headroom;
  162. int tx_tailroom;
  163. struct timer_list check_pres_timer;
  164. struct work_struct check_pres_work;
  165. bool shutting_down;
  166. struct rfkill *rfkill;
  167. const struct nfc_vendor_cmd *vendor_cmds;
  168. int n_vendor_cmds;
  169. const struct nfc_ops *ops;
  170. struct genl_info *cur_cmd_info;
  171. };
  172. #define to_nfc_dev(_dev) container_of(_dev, struct nfc_dev, dev)
  173. extern struct class nfc_class;
  174. struct nfc_dev *nfc_allocate_device(const struct nfc_ops *ops,
  175. u32 supported_protocols,
  176. int tx_headroom,
  177. int tx_tailroom);
  178. /**
  179. * nfc_free_device - free nfc device
  180. *
  181. * @dev: The nfc device to free
  182. */
  183. static inline void nfc_free_device(struct nfc_dev *dev)
  184. {
  185. put_device(&dev->dev);
  186. }
  187. int nfc_register_device(struct nfc_dev *dev);
  188. void nfc_unregister_device(struct nfc_dev *dev);
  189. /**
  190. * nfc_set_parent_dev - set the parent device
  191. *
  192. * @nfc_dev: The nfc device whose parent is being set
  193. * @dev: The parent device
  194. */
  195. static inline void nfc_set_parent_dev(struct nfc_dev *nfc_dev,
  196. struct device *dev)
  197. {
  198. nfc_dev->dev.parent = dev;
  199. }
  200. /**
  201. * nfc_set_drvdata - set driver specifc data
  202. *
  203. * @dev: The nfc device
  204. * @data: Pointer to driver specifc data
  205. */
  206. static inline void nfc_set_drvdata(struct nfc_dev *dev, void *data)
  207. {
  208. dev_set_drvdata(&dev->dev, data);
  209. }
  210. /**
  211. * nfc_get_drvdata - get driver specifc data
  212. *
  213. * @dev: The nfc device
  214. */
  215. static inline void *nfc_get_drvdata(const struct nfc_dev *dev)
  216. {
  217. return dev_get_drvdata(&dev->dev);
  218. }
  219. /**
  220. * nfc_device_name - get the nfc device name
  221. *
  222. * @dev: The nfc device whose name to return
  223. */
  224. static inline const char *nfc_device_name(const struct nfc_dev *dev)
  225. {
  226. return dev_name(&dev->dev);
  227. }
  228. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  229. unsigned int flags, unsigned int size,
  230. unsigned int *err);
  231. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp);
  232. int nfc_set_remote_general_bytes(struct nfc_dev *dev,
  233. const u8 *gt, u8 gt_len);
  234. u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len);
  235. int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  236. u32 result);
  237. int nfc_targets_found(struct nfc_dev *dev,
  238. struct nfc_target *targets, int ntargets);
  239. int nfc_target_lost(struct nfc_dev *dev, u32 target_idx);
  240. int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
  241. u8 comm_mode, u8 rf_mode);
  242. int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
  243. const u8 *gb, size_t gb_len);
  244. int nfc_tm_deactivated(struct nfc_dev *dev);
  245. int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb);
  246. void nfc_driver_failure(struct nfc_dev *dev, int err);
  247. int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
  248. struct nfc_evt_transaction *evt_transaction);
  249. int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx);
  250. int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type);
  251. int nfc_remove_se(struct nfc_dev *dev, u32 se_idx);
  252. struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx);
  253. void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
  254. u8 payload_type, u8 direction);
  255. static inline int nfc_set_vendor_cmds(struct nfc_dev *dev,
  256. const struct nfc_vendor_cmd *cmds,
  257. int n_cmds)
  258. {
  259. if (dev->vendor_cmds || dev->n_vendor_cmds)
  260. return -EINVAL;
  261. dev->vendor_cmds = cmds;
  262. dev->n_vendor_cmds = n_cmds;
  263. return 0;
  264. }
  265. struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
  266. enum nfc_attrs attr,
  267. u32 oui, u32 subcmd,
  268. int approxlen);
  269. int nfc_vendor_cmd_reply(struct sk_buff *skb);
  270. /**
  271. * nfc_vendor_cmd_alloc_reply_skb - allocate vendor command reply
  272. * @dev: nfc device
  273. * @oui: vendor oui
  274. * @approxlen: an upper bound of the length of the data that will
  275. * be put into the skb
  276. *
  277. * This function allocates and pre-fills an skb for a reply to
  278. * a vendor command. Since it is intended for a reply, calling
  279. * it outside of a vendor command's doit() operation is invalid.
  280. *
  281. * The returned skb is pre-filled with some identifying data in
  282. * a way that any data that is put into the skb (with skb_put(),
  283. * nla_put() or similar) will end up being within the
  284. * %NFC_ATTR_VENDOR_DATA attribute, so all that needs to be done
  285. * with the skb is adding data for the corresponding userspace tool
  286. * which can then read that data out of the vendor data attribute.
  287. * You must not modify the skb in any other way.
  288. *
  289. * When done, call nfc_vendor_cmd_reply() with the skb and return
  290. * its error code as the result of the doit() operation.
  291. *
  292. * Return: An allocated and pre-filled skb. %NULL if any errors happen.
  293. */
  294. static inline struct sk_buff *
  295. nfc_vendor_cmd_alloc_reply_skb(struct nfc_dev *dev,
  296. u32 oui, u32 subcmd, int approxlen)
  297. {
  298. return __nfc_alloc_vendor_cmd_reply_skb(dev,
  299. NFC_ATTR_VENDOR_DATA,
  300. oui,
  301. subcmd, approxlen);
  302. }
  303. #endif /* __NET_NFC_H */