i2c.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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, write to the
  17. * Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/i2c.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/gpio.h>
  27. #include <linux/nfc.h>
  28. #include <net/nfc/hci.h>
  29. #include <net/nfc/llc.h>
  30. #include "microread.h"
  31. #define MICROREAD_I2C_DRIVER_NAME "microread"
  32. #define MICROREAD_I2C_FRAME_HEADROOM 1
  33. #define MICROREAD_I2C_FRAME_TAILROOM 1
  34. /* framing in HCI mode */
  35. #define MICROREAD_I2C_LLC_LEN 1
  36. #define MICROREAD_I2C_LLC_CRC 1
  37. #define MICROREAD_I2C_LLC_LEN_CRC (MICROREAD_I2C_LLC_LEN + \
  38. MICROREAD_I2C_LLC_CRC)
  39. #define MICROREAD_I2C_LLC_MIN_SIZE (1 + MICROREAD_I2C_LLC_LEN_CRC)
  40. #define MICROREAD_I2C_LLC_MAX_PAYLOAD 29
  41. #define MICROREAD_I2C_LLC_MAX_SIZE (MICROREAD_I2C_LLC_LEN_CRC + 1 + \
  42. MICROREAD_I2C_LLC_MAX_PAYLOAD)
  43. struct microread_i2c_phy {
  44. struct i2c_client *i2c_dev;
  45. struct nfc_hci_dev *hdev;
  46. int irq;
  47. int hard_fault; /*
  48. * < 0 if hardware error occured (e.g. i2c err)
  49. * and prevents normal operation.
  50. */
  51. };
  52. #define I2C_DUMP_SKB(info, skb) \
  53. do { \
  54. pr_debug("%s:\n", info); \
  55. print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
  56. 16, 1, (skb)->data, (skb)->len, 0); \
  57. } while (0)
  58. static void microread_i2c_add_len_crc(struct sk_buff *skb)
  59. {
  60. int i;
  61. u8 crc = 0;
  62. int len;
  63. len = skb->len;
  64. *skb_push(skb, 1) = len;
  65. for (i = 0; i < skb->len; i++)
  66. crc = crc ^ skb->data[i];
  67. *skb_put(skb, 1) = crc;
  68. }
  69. static void microread_i2c_remove_len_crc(struct sk_buff *skb)
  70. {
  71. skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM);
  72. skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM);
  73. }
  74. static int check_crc(struct sk_buff *skb)
  75. {
  76. int i;
  77. u8 crc = 0;
  78. for (i = 0; i < skb->len - 1; i++)
  79. crc = crc ^ skb->data[i];
  80. if (crc != skb->data[skb->len-1]) {
  81. pr_err("CRC error 0x%x != 0x%x\n", crc, skb->data[skb->len-1]);
  82. pr_info("%s: BAD CRC\n", __func__);
  83. return -EPERM;
  84. }
  85. return 0;
  86. }
  87. static int microread_i2c_enable(void *phy_id)
  88. {
  89. return 0;
  90. }
  91. static void microread_i2c_disable(void *phy_id)
  92. {
  93. return;
  94. }
  95. static int microread_i2c_write(void *phy_id, struct sk_buff *skb)
  96. {
  97. int r;
  98. struct microread_i2c_phy *phy = phy_id;
  99. struct i2c_client *client = phy->i2c_dev;
  100. if (phy->hard_fault != 0)
  101. return phy->hard_fault;
  102. usleep_range(3000, 6000);
  103. microread_i2c_add_len_crc(skb);
  104. I2C_DUMP_SKB("i2c frame written", skb);
  105. r = i2c_master_send(client, skb->data, skb->len);
  106. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  107. usleep_range(6000, 10000);
  108. r = i2c_master_send(client, skb->data, skb->len);
  109. }
  110. if (r >= 0) {
  111. if (r != skb->len)
  112. r = -EREMOTEIO;
  113. else
  114. r = 0;
  115. }
  116. microread_i2c_remove_len_crc(skb);
  117. return r;
  118. }
  119. static int microread_i2c_read(struct microread_i2c_phy *phy,
  120. struct sk_buff **skb)
  121. {
  122. int r;
  123. u8 len;
  124. u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1];
  125. struct i2c_client *client = phy->i2c_dev;
  126. r = i2c_master_recv(client, &len, 1);
  127. if (r != 1) {
  128. nfc_err(&client->dev, "cannot read len byte\n");
  129. return -EREMOTEIO;
  130. }
  131. if ((len < MICROREAD_I2C_LLC_MIN_SIZE) ||
  132. (len > MICROREAD_I2C_LLC_MAX_SIZE)) {
  133. nfc_err(&client->dev, "invalid len byte\n");
  134. r = -EBADMSG;
  135. goto flush;
  136. }
  137. *skb = alloc_skb(1 + len, GFP_KERNEL);
  138. if (*skb == NULL) {
  139. r = -ENOMEM;
  140. goto flush;
  141. }
  142. *skb_put(*skb, 1) = len;
  143. r = i2c_master_recv(client, skb_put(*skb, len), len);
  144. if (r != len) {
  145. kfree_skb(*skb);
  146. return -EREMOTEIO;
  147. }
  148. I2C_DUMP_SKB("cc frame read", *skb);
  149. r = check_crc(*skb);
  150. if (r != 0) {
  151. kfree_skb(*skb);
  152. r = -EBADMSG;
  153. goto flush;
  154. }
  155. skb_pull(*skb, 1);
  156. skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM);
  157. usleep_range(3000, 6000);
  158. return 0;
  159. flush:
  160. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  161. r = -EREMOTEIO;
  162. usleep_range(3000, 6000);
  163. return r;
  164. }
  165. static irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id)
  166. {
  167. struct microread_i2c_phy *phy = phy_id;
  168. struct i2c_client *client;
  169. struct sk_buff *skb = NULL;
  170. int r;
  171. if (!phy || irq != phy->i2c_dev->irq) {
  172. WARN_ON_ONCE(1);
  173. return IRQ_NONE;
  174. }
  175. client = phy->i2c_dev;
  176. if (phy->hard_fault != 0)
  177. return IRQ_HANDLED;
  178. r = microread_i2c_read(phy, &skb);
  179. if (r == -EREMOTEIO) {
  180. phy->hard_fault = r;
  181. nfc_hci_recv_frame(phy->hdev, NULL);
  182. return IRQ_HANDLED;
  183. } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
  184. return IRQ_HANDLED;
  185. }
  186. nfc_hci_recv_frame(phy->hdev, skb);
  187. return IRQ_HANDLED;
  188. }
  189. static struct nfc_phy_ops i2c_phy_ops = {
  190. .write = microread_i2c_write,
  191. .enable = microread_i2c_enable,
  192. .disable = microread_i2c_disable,
  193. };
  194. static int microread_i2c_probe(struct i2c_client *client,
  195. const struct i2c_device_id *id)
  196. {
  197. struct microread_i2c_phy *phy;
  198. struct microread_nfc_platform_data *pdata =
  199. dev_get_platdata(&client->dev);
  200. int r;
  201. dev_dbg(&client->dev, "client %p\n", client);
  202. if (!pdata) {
  203. nfc_err(&client->dev, "client %p: missing platform data\n",
  204. client);
  205. return -EINVAL;
  206. }
  207. phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
  208. GFP_KERNEL);
  209. if (!phy)
  210. return -ENOMEM;
  211. i2c_set_clientdata(client, phy);
  212. phy->i2c_dev = client;
  213. r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn,
  214. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  215. MICROREAD_I2C_DRIVER_NAME, phy);
  216. if (r) {
  217. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  218. return r;
  219. }
  220. r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
  221. MICROREAD_I2C_FRAME_HEADROOM,
  222. MICROREAD_I2C_FRAME_TAILROOM,
  223. MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
  224. if (r < 0)
  225. goto err_irq;
  226. nfc_info(&client->dev, "Probed");
  227. return 0;
  228. err_irq:
  229. free_irq(client->irq, phy);
  230. return r;
  231. }
  232. static int microread_i2c_remove(struct i2c_client *client)
  233. {
  234. struct microread_i2c_phy *phy = i2c_get_clientdata(client);
  235. microread_remove(phy->hdev);
  236. free_irq(client->irq, phy);
  237. return 0;
  238. }
  239. static struct i2c_device_id microread_i2c_id[] = {
  240. { MICROREAD_I2C_DRIVER_NAME, 0},
  241. { }
  242. };
  243. MODULE_DEVICE_TABLE(i2c, microread_i2c_id);
  244. static struct i2c_driver microread_i2c_driver = {
  245. .driver = {
  246. .name = MICROREAD_I2C_DRIVER_NAME,
  247. },
  248. .probe = microread_i2c_probe,
  249. .remove = microread_i2c_remove,
  250. .id_table = microread_i2c_id,
  251. };
  252. module_i2c_driver(microread_i2c_driver);
  253. MODULE_LICENSE("GPL");
  254. MODULE_DESCRIPTION(DRIVER_DESC);