i2c.c 7.2 KB

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