nfc.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <lauro.venancio@openbossa.org>
  8. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  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 description
  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. * @ats_len: length of Answer To Select in bytes
  78. * @ats: Answer To Select returned by an ISO 14443 Type A target upon activation
  79. */
  80. struct nfc_target {
  81. u32 idx;
  82. u32 supported_protocols;
  83. u16 sens_res;
  84. u8 sel_res;
  85. u8 nfcid1_len;
  86. u8 nfcid1[NFC_NFCID1_MAXSIZE];
  87. u8 nfcid2_len;
  88. u8 nfcid2[NFC_NFCID2_MAXSIZE];
  89. u8 sensb_res_len;
  90. u8 sensb_res[NFC_SENSB_RES_MAXSIZE];
  91. u8 sensf_res_len;
  92. u8 sensf_res[NFC_SENSF_RES_MAXSIZE];
  93. u8 hci_reader_gate;
  94. u8 logical_idx;
  95. u8 is_iso15693;
  96. u8 iso15693_dsfid;
  97. u8 iso15693_uid[NFC_ISO15693_UID_MAXSIZE];
  98. u8 ats_len;
  99. u8 ats[NFC_ATS_MAXSIZE];
  100. };
  101. /**
  102. * nfc_se - A structure for NFC accessible secure elements.
  103. *
  104. * @idx: The secure element index. User space will enable or
  105. * disable a secure element by its index.
  106. * @type: The secure element type. It can be SE_UICC or
  107. * SE_EMBEDDED.
  108. * @state: The secure element state, either enabled or disabled.
  109. *
  110. */
  111. struct nfc_se {
  112. struct list_head list;
  113. u32 idx;
  114. u16 type;
  115. u16 state;
  116. };
  117. /**
  118. * nfc_evt_transaction - A struct for NFC secure element event transaction.
  119. *
  120. * @aid: The application identifier triggering the event
  121. *
  122. * @aid_len: The application identifier length [5:16]
  123. *
  124. * @params: The application parameters transmitted during the transaction
  125. *
  126. * @params_len: The applications parameters length [0:255]
  127. *
  128. */
  129. #define NFC_MIN_AID_LENGTH 5
  130. #define NFC_MAX_AID_LENGTH 16
  131. #define NFC_MAX_PARAMS_LENGTH 255
  132. #define NFC_EVT_TRANSACTION_AID_TAG 0x81
  133. #define NFC_EVT_TRANSACTION_PARAMS_TAG 0x82
  134. struct nfc_evt_transaction {
  135. u32 aid_len;
  136. u8 aid[NFC_MAX_AID_LENGTH];
  137. u8 params_len;
  138. u8 params[];
  139. } __packed;
  140. struct nfc_genl_data {
  141. u32 poll_req_portid;
  142. struct mutex genl_data_mutex;
  143. };
  144. struct nfc_vendor_cmd {
  145. __u32 vendor_id;
  146. __u32 subcmd;
  147. int (*doit)(struct nfc_dev *dev, void *data, size_t data_len);
  148. };
  149. struct nfc_dev {
  150. int idx;
  151. u32 target_next_idx;
  152. struct nfc_target *targets;
  153. int n_targets;
  154. int targets_generation;
  155. struct device dev;
  156. bool dev_up;
  157. bool fw_download_in_progress;
  158. u8 rf_mode;
  159. bool polling;
  160. struct nfc_target *active_target;
  161. bool dep_link_up;
  162. struct nfc_genl_data genl_data;
  163. u32 supported_protocols;
  164. struct list_head secure_elements;
  165. int tx_headroom;
  166. int tx_tailroom;
  167. struct timer_list check_pres_timer;
  168. struct work_struct check_pres_work;
  169. bool shutting_down;
  170. struct rfkill *rfkill;
  171. const struct nfc_vendor_cmd *vendor_cmds;
  172. int n_vendor_cmds;
  173. const struct nfc_ops *ops;
  174. struct genl_info *cur_cmd_info;
  175. };
  176. #define to_nfc_dev(_dev) container_of(_dev, struct nfc_dev, dev)
  177. extern const struct class nfc_class;
  178. struct nfc_dev *nfc_allocate_device(const struct nfc_ops *ops,
  179. u32 supported_protocols,
  180. int tx_headroom,
  181. int tx_tailroom);
  182. /**
  183. * nfc_free_device - free nfc device
  184. *
  185. * @dev: The nfc device to free
  186. */
  187. static inline void nfc_free_device(struct nfc_dev *dev)
  188. {
  189. put_device(&dev->dev);
  190. }
  191. int nfc_register_device(struct nfc_dev *dev);
  192. void nfc_unregister_rfkill(struct nfc_dev *dev);
  193. void nfc_remove_device(struct nfc_dev *dev);
  194. void nfc_unregister_device(struct nfc_dev *dev);
  195. /**
  196. * nfc_set_parent_dev - set the parent device
  197. *
  198. * @nfc_dev: The nfc device whose parent is being set
  199. * @dev: The parent device
  200. */
  201. static inline void nfc_set_parent_dev(struct nfc_dev *nfc_dev,
  202. struct device *dev)
  203. {
  204. nfc_dev->dev.parent = dev;
  205. }
  206. /**
  207. * nfc_set_drvdata - set driver specific data
  208. *
  209. * @dev: The nfc device
  210. * @data: Pointer to driver specific data
  211. */
  212. static inline void nfc_set_drvdata(struct nfc_dev *dev, void *data)
  213. {
  214. dev_set_drvdata(&dev->dev, data);
  215. }
  216. /**
  217. * nfc_get_drvdata - get driver specific data
  218. *
  219. * @dev: The nfc device
  220. */
  221. static inline void *nfc_get_drvdata(const struct nfc_dev *dev)
  222. {
  223. return dev_get_drvdata(&dev->dev);
  224. }
  225. /**
  226. * nfc_device_name - get the nfc device name
  227. *
  228. * @dev: The nfc device whose name to return
  229. */
  230. static inline const char *nfc_device_name(const struct nfc_dev *dev)
  231. {
  232. return dev_name(&dev->dev);
  233. }
  234. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  235. unsigned int flags, unsigned int size,
  236. unsigned int *err);
  237. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp);
  238. int nfc_set_remote_general_bytes(struct nfc_dev *dev,
  239. const u8 *gt, u8 gt_len);
  240. u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len);
  241. int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  242. u32 result);
  243. int nfc_targets_found(struct nfc_dev *dev,
  244. struct nfc_target *targets, int ntargets);
  245. int nfc_target_lost(struct nfc_dev *dev, u32 target_idx);
  246. int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
  247. u8 comm_mode, u8 rf_mode);
  248. int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
  249. const u8 *gb, size_t gb_len);
  250. int nfc_tm_deactivated(struct nfc_dev *dev);
  251. int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb);
  252. void nfc_driver_failure(struct nfc_dev *dev, int err);
  253. int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
  254. struct nfc_evt_transaction *evt_transaction);
  255. int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx);
  256. int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type);
  257. int nfc_remove_se(struct nfc_dev *dev, u32 se_idx);
  258. struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx);
  259. void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
  260. u8 payload_type, u8 direction);
  261. static inline int nfc_set_vendor_cmds(struct nfc_dev *dev,
  262. const struct nfc_vendor_cmd *cmds,
  263. int n_cmds)
  264. {
  265. if (dev->vendor_cmds || dev->n_vendor_cmds)
  266. return -EINVAL;
  267. dev->vendor_cmds = cmds;
  268. dev->n_vendor_cmds = n_cmds;
  269. return 0;
  270. }
  271. struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev,
  272. enum nfc_attrs attr,
  273. u32 oui, u32 subcmd,
  274. int approxlen);
  275. int nfc_vendor_cmd_reply(struct sk_buff *skb);
  276. /**
  277. * nfc_vendor_cmd_alloc_reply_skb - allocate vendor command reply
  278. * @dev: nfc device
  279. * @oui: vendor oui
  280. * @approxlen: an upper bound of the length of the data that will
  281. * be put into the skb
  282. *
  283. * This function allocates and pre-fills an skb for a reply to
  284. * a vendor command. Since it is intended for a reply, calling
  285. * it outside of a vendor command's doit() operation is invalid.
  286. *
  287. * The returned skb is pre-filled with some identifying data in
  288. * a way that any data that is put into the skb (with skb_put(),
  289. * nla_put() or similar) will end up being within the
  290. * %NFC_ATTR_VENDOR_DATA attribute, so all that needs to be done
  291. * with the skb is adding data for the corresponding userspace tool
  292. * which can then read that data out of the vendor data attribute.
  293. * You must not modify the skb in any other way.
  294. *
  295. * When done, call nfc_vendor_cmd_reply() with the skb and return
  296. * its error code as the result of the doit() operation.
  297. *
  298. * Return: An allocated and pre-filled skb. %NULL if any errors happen.
  299. */
  300. static inline struct sk_buff *
  301. nfc_vendor_cmd_alloc_reply_skb(struct nfc_dev *dev,
  302. u32 oui, u32 subcmd, int approxlen)
  303. {
  304. return __nfc_alloc_vendor_cmd_reply_skb(dev,
  305. NFC_ATTR_VENDOR_DATA,
  306. oui,
  307. subcmd, approxlen);
  308. }
  309. #endif /* __NET_NFC_H */