i2c.c 6.8 KB

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