i2c.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * HCI based Driver for Inside Secure microread NFC Chip - i2c layer
  3. *
  4. * Copyright (C) 2013 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/i2c.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/gpio.h>
  25. #include <linux/nfc.h>
  26. #include <net/nfc/hci.h>
  27. #include <net/nfc/llc.h>
  28. #include "microread.h"
  29. #define MICROREAD_I2C_DRIVER_NAME "microread"
  30. #define MICROREAD_I2C_FRAME_HEADROOM 1
  31. #define MICROREAD_I2C_FRAME_TAILROOM 1
  32. /* framing in HCI mode */
  33. #define MICROREAD_I2C_LLC_LEN 1
  34. #define MICROREAD_I2C_LLC_CRC 1
  35. #define MICROREAD_I2C_LLC_LEN_CRC (MICROREAD_I2C_LLC_LEN + \
  36. MICROREAD_I2C_LLC_CRC)
  37. #define MICROREAD_I2C_LLC_MIN_SIZE (1 + MICROREAD_I2C_LLC_LEN_CRC)
  38. #define MICROREAD_I2C_LLC_MAX_PAYLOAD 29
  39. #define MICROREAD_I2C_LLC_MAX_SIZE (MICROREAD_I2C_LLC_LEN_CRC + 1 + \
  40. MICROREAD_I2C_LLC_MAX_PAYLOAD)
  41. struct microread_i2c_phy {
  42. struct i2c_client *i2c_dev;
  43. struct nfc_hci_dev *hdev;
  44. int hard_fault; /*
  45. * < 0 if hardware error occured (e.g. i2c err)
  46. * and prevents normal operation.
  47. */
  48. };
  49. #define I2C_DUMP_SKB(info, skb) \
  50. do { \
  51. pr_debug("%s:\n", info); \
  52. print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
  53. 16, 1, (skb)->data, (skb)->len, 0); \
  54. } while (0)
  55. static void microread_i2c_add_len_crc(struct sk_buff *skb)
  56. {
  57. int i;
  58. u8 crc = 0;
  59. int len;
  60. len = skb->len;
  61. *skb_push(skb, 1) = len;
  62. for (i = 0; i < skb->len; i++)
  63. crc = crc ^ skb->data[i];
  64. *skb_put(skb, 1) = crc;
  65. }
  66. static void microread_i2c_remove_len_crc(struct sk_buff *skb)
  67. {
  68. skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM);
  69. skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM);
  70. }
  71. static int check_crc(struct sk_buff *skb)
  72. {
  73. int i;
  74. u8 crc = 0;
  75. for (i = 0; i < skb->len - 1; i++)
  76. crc = crc ^ skb->data[i];
  77. if (crc != skb->data[skb->len-1]) {
  78. pr_err("CRC error 0x%x != 0x%x\n", crc, skb->data[skb->len-1]);
  79. pr_info("%s: BAD CRC\n", __func__);
  80. return -EPERM;
  81. }
  82. return 0;
  83. }
  84. static int microread_i2c_enable(void *phy_id)
  85. {
  86. return 0;
  87. }
  88. static void microread_i2c_disable(void *phy_id)
  89. {
  90. return;
  91. }
  92. static int microread_i2c_write(void *phy_id, struct sk_buff *skb)
  93. {
  94. int r;
  95. struct microread_i2c_phy *phy = phy_id;
  96. struct i2c_client *client = phy->i2c_dev;
  97. if (phy->hard_fault != 0)
  98. return phy->hard_fault;
  99. usleep_range(3000, 6000);
  100. microread_i2c_add_len_crc(skb);
  101. I2C_DUMP_SKB("i2c frame written", skb);
  102. r = i2c_master_send(client, skb->data, skb->len);
  103. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  104. usleep_range(6000, 10000);
  105. r = i2c_master_send(client, skb->data, skb->len);
  106. }
  107. if (r >= 0) {
  108. if (r != skb->len)
  109. r = -EREMOTEIO;
  110. else
  111. r = 0;
  112. }
  113. microread_i2c_remove_len_crc(skb);
  114. return r;
  115. }
  116. static int microread_i2c_read(struct microread_i2c_phy *phy,
  117. struct sk_buff **skb)
  118. {
  119. int r;
  120. u8 len;
  121. u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1];
  122. struct i2c_client *client = phy->i2c_dev;
  123. r = i2c_master_recv(client, &len, 1);
  124. if (r != 1) {
  125. nfc_err(&client->dev, "cannot read len byte\n");
  126. return -EREMOTEIO;
  127. }
  128. if ((len < MICROREAD_I2C_LLC_MIN_SIZE) ||
  129. (len > MICROREAD_I2C_LLC_MAX_SIZE)) {
  130. nfc_err(&client->dev, "invalid len byte\n");
  131. r = -EBADMSG;
  132. goto flush;
  133. }
  134. *skb = alloc_skb(1 + len, GFP_KERNEL);
  135. if (*skb == NULL) {
  136. r = -ENOMEM;
  137. goto flush;
  138. }
  139. *skb_put(*skb, 1) = len;
  140. r = i2c_master_recv(client, skb_put(*skb, len), len);
  141. if (r != len) {
  142. kfree_skb(*skb);
  143. return -EREMOTEIO;
  144. }
  145. I2C_DUMP_SKB("cc frame read", *skb);
  146. r = check_crc(*skb);
  147. if (r != 0) {
  148. kfree_skb(*skb);
  149. r = -EBADMSG;
  150. goto flush;
  151. }
  152. skb_pull(*skb, 1);
  153. skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM);
  154. usleep_range(3000, 6000);
  155. return 0;
  156. flush:
  157. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  158. r = -EREMOTEIO;
  159. usleep_range(3000, 6000);
  160. return r;
  161. }
  162. static irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id)
  163. {
  164. struct microread_i2c_phy *phy = phy_id;
  165. struct sk_buff *skb = NULL;
  166. int r;
  167. if (!phy || irq != phy->i2c_dev->irq) {
  168. WARN_ON_ONCE(1);
  169. return IRQ_NONE;
  170. }
  171. if (phy->hard_fault != 0)
  172. return IRQ_HANDLED;
  173. r = microread_i2c_read(phy, &skb);
  174. if (r == -EREMOTEIO) {
  175. phy->hard_fault = r;
  176. nfc_hci_recv_frame(phy->hdev, NULL);
  177. return IRQ_HANDLED;
  178. } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
  179. return IRQ_HANDLED;
  180. }
  181. nfc_hci_recv_frame(phy->hdev, skb);
  182. return IRQ_HANDLED;
  183. }
  184. static struct nfc_phy_ops i2c_phy_ops = {
  185. .write = microread_i2c_write,
  186. .enable = microread_i2c_enable,
  187. .disable = microread_i2c_disable,
  188. };
  189. static int microread_i2c_probe(struct i2c_client *client,
  190. const struct i2c_device_id *id)
  191. {
  192. struct microread_i2c_phy *phy;
  193. int r;
  194. dev_dbg(&client->dev, "client %p\n", client);
  195. phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
  196. GFP_KERNEL);
  197. if (!phy)
  198. return -ENOMEM;
  199. i2c_set_clientdata(client, phy);
  200. phy->i2c_dev = client;
  201. r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn,
  202. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  203. MICROREAD_I2C_DRIVER_NAME, phy);
  204. if (r) {
  205. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  206. return r;
  207. }
  208. r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
  209. MICROREAD_I2C_FRAME_HEADROOM,
  210. MICROREAD_I2C_FRAME_TAILROOM,
  211. MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
  212. if (r < 0)
  213. goto err_irq;
  214. nfc_info(&client->dev, "Probed\n");
  215. return 0;
  216. err_irq:
  217. free_irq(client->irq, phy);
  218. return r;
  219. }
  220. static int microread_i2c_remove(struct i2c_client *client)
  221. {
  222. struct microread_i2c_phy *phy = i2c_get_clientdata(client);
  223. microread_remove(phy->hdev);
  224. free_irq(client->irq, phy);
  225. return 0;
  226. }
  227. static struct i2c_device_id microread_i2c_id[] = {
  228. { MICROREAD_I2C_DRIVER_NAME, 0},
  229. { }
  230. };
  231. MODULE_DEVICE_TABLE(i2c, microread_i2c_id);
  232. static struct i2c_driver microread_i2c_driver = {
  233. .driver = {
  234. .name = MICROREAD_I2C_DRIVER_NAME,
  235. },
  236. .probe = microread_i2c_probe,
  237. .remove = microread_i2c_remove,
  238. .id_table = microread_i2c_id,
  239. };
  240. module_i2c_driver(microread_i2c_driver);
  241. MODULE_LICENSE("GPL");
  242. MODULE_DESCRIPTION(DRIVER_DESC);