core.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
  6. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/rfkill.h>
  27. #include <linux/nfc.h>
  28. #include <net/genetlink.h>
  29. #include "nfc.h"
  30. #define VERSION "0.1"
  31. #define NFC_CHECK_PRES_FREQ_MS 2000
  32. int nfc_devlist_generation;
  33. DEFINE_MUTEX(nfc_devlist_mutex);
  34. /* NFC device ID bitmap */
  35. static DEFINE_IDA(nfc_index_ida);
  36. int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
  37. {
  38. int rc = 0;
  39. pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
  40. device_lock(&dev->dev);
  41. if (!device_is_registered(&dev->dev)) {
  42. rc = -ENODEV;
  43. goto error;
  44. }
  45. if (dev->dev_up) {
  46. rc = -EBUSY;
  47. goto error;
  48. }
  49. if (!dev->ops->fw_download) {
  50. rc = -EOPNOTSUPP;
  51. goto error;
  52. }
  53. dev->fw_download_in_progress = true;
  54. rc = dev->ops->fw_download(dev, firmware_name);
  55. if (rc)
  56. dev->fw_download_in_progress = false;
  57. error:
  58. device_unlock(&dev->dev);
  59. return rc;
  60. }
  61. /**
  62. * nfc_fw_download_done - inform that a firmware download was completed
  63. *
  64. * @dev: The nfc device to which firmware was downloaded
  65. * @firmware_name: The firmware filename
  66. * @result: The positive value of a standard errno value
  67. */
  68. int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  69. u32 result)
  70. {
  71. dev->fw_download_in_progress = false;
  72. return nfc_genl_fw_download_done(dev, firmware_name, result);
  73. }
  74. EXPORT_SYMBOL(nfc_fw_download_done);
  75. /**
  76. * nfc_dev_up - turn on the NFC device
  77. *
  78. * @dev: The nfc device to be turned on
  79. *
  80. * The device remains up until the nfc_dev_down function is called.
  81. */
  82. int nfc_dev_up(struct nfc_dev *dev)
  83. {
  84. int rc = 0;
  85. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  86. device_lock(&dev->dev);
  87. if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
  88. rc = -ERFKILL;
  89. goto error;
  90. }
  91. if (!device_is_registered(&dev->dev)) {
  92. rc = -ENODEV;
  93. goto error;
  94. }
  95. if (dev->fw_download_in_progress) {
  96. rc = -EBUSY;
  97. goto error;
  98. }
  99. if (dev->dev_up) {
  100. rc = -EALREADY;
  101. goto error;
  102. }
  103. if (dev->ops->dev_up)
  104. rc = dev->ops->dev_up(dev);
  105. if (!rc)
  106. dev->dev_up = true;
  107. /* We have to enable the device before discovering SEs */
  108. if (dev->ops->discover_se && dev->ops->discover_se(dev))
  109. pr_err("SE discovery failed\n");
  110. error:
  111. device_unlock(&dev->dev);
  112. return rc;
  113. }
  114. /**
  115. * nfc_dev_down - turn off the NFC device
  116. *
  117. * @dev: The nfc device to be turned off
  118. */
  119. int nfc_dev_down(struct nfc_dev *dev)
  120. {
  121. int rc = 0;
  122. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  123. device_lock(&dev->dev);
  124. if (!device_is_registered(&dev->dev)) {
  125. rc = -ENODEV;
  126. goto error;
  127. }
  128. if (!dev->dev_up) {
  129. rc = -EALREADY;
  130. goto error;
  131. }
  132. if (dev->polling || dev->active_target) {
  133. rc = -EBUSY;
  134. goto error;
  135. }
  136. if (dev->ops->dev_down)
  137. dev->ops->dev_down(dev);
  138. dev->dev_up = false;
  139. error:
  140. device_unlock(&dev->dev);
  141. return rc;
  142. }
  143. static int nfc_rfkill_set_block(void *data, bool blocked)
  144. {
  145. struct nfc_dev *dev = data;
  146. pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
  147. if (!blocked)
  148. return 0;
  149. nfc_dev_down(dev);
  150. return 0;
  151. }
  152. static const struct rfkill_ops nfc_rfkill_ops = {
  153. .set_block = nfc_rfkill_set_block,
  154. };
  155. /**
  156. * nfc_start_poll - start polling for nfc targets
  157. *
  158. * @dev: The nfc device that must start polling
  159. * @protocols: bitset of nfc protocols that must be used for polling
  160. *
  161. * The device remains polling for targets until a target is found or
  162. * the nfc_stop_poll function is called.
  163. */
  164. int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
  165. {
  166. int rc;
  167. pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
  168. dev_name(&dev->dev), im_protocols, tm_protocols);
  169. if (!im_protocols && !tm_protocols)
  170. return -EINVAL;
  171. device_lock(&dev->dev);
  172. if (!device_is_registered(&dev->dev)) {
  173. rc = -ENODEV;
  174. goto error;
  175. }
  176. if (!dev->dev_up) {
  177. rc = -ENODEV;
  178. goto error;
  179. }
  180. if (dev->polling) {
  181. rc = -EBUSY;
  182. goto error;
  183. }
  184. rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
  185. if (!rc) {
  186. dev->polling = true;
  187. dev->rf_mode = NFC_RF_NONE;
  188. }
  189. error:
  190. device_unlock(&dev->dev);
  191. return rc;
  192. }
  193. /**
  194. * nfc_stop_poll - stop polling for nfc targets
  195. *
  196. * @dev: The nfc device that must stop polling
  197. */
  198. int nfc_stop_poll(struct nfc_dev *dev)
  199. {
  200. int rc = 0;
  201. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  202. device_lock(&dev->dev);
  203. if (!device_is_registered(&dev->dev)) {
  204. rc = -ENODEV;
  205. goto error;
  206. }
  207. if (!dev->polling) {
  208. rc = -EINVAL;
  209. goto error;
  210. }
  211. dev->ops->stop_poll(dev);
  212. dev->polling = false;
  213. dev->rf_mode = NFC_RF_NONE;
  214. error:
  215. device_unlock(&dev->dev);
  216. return rc;
  217. }
  218. static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
  219. {
  220. int i;
  221. for (i = 0; i < dev->n_targets; i++) {
  222. if (dev->targets[i].idx == target_idx)
  223. return &dev->targets[i];
  224. }
  225. return NULL;
  226. }
  227. int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
  228. {
  229. int rc = 0;
  230. u8 *gb;
  231. size_t gb_len;
  232. struct nfc_target *target;
  233. pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
  234. if (!dev->ops->dep_link_up)
  235. return -EOPNOTSUPP;
  236. device_lock(&dev->dev);
  237. if (!device_is_registered(&dev->dev)) {
  238. rc = -ENODEV;
  239. goto error;
  240. }
  241. if (dev->dep_link_up == true) {
  242. rc = -EALREADY;
  243. goto error;
  244. }
  245. gb = nfc_llcp_general_bytes(dev, &gb_len);
  246. if (gb_len > NFC_MAX_GT_LEN) {
  247. rc = -EINVAL;
  248. goto error;
  249. }
  250. target = nfc_find_target(dev, target_index);
  251. if (target == NULL) {
  252. rc = -ENOTCONN;
  253. goto error;
  254. }
  255. rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
  256. if (!rc) {
  257. dev->active_target = target;
  258. dev->rf_mode = NFC_RF_INITIATOR;
  259. }
  260. error:
  261. device_unlock(&dev->dev);
  262. return rc;
  263. }
  264. int nfc_dep_link_down(struct nfc_dev *dev)
  265. {
  266. int rc = 0;
  267. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  268. if (!dev->ops->dep_link_down)
  269. return -EOPNOTSUPP;
  270. device_lock(&dev->dev);
  271. if (!device_is_registered(&dev->dev)) {
  272. rc = -ENODEV;
  273. goto error;
  274. }
  275. if (dev->dep_link_up == false) {
  276. rc = -EALREADY;
  277. goto error;
  278. }
  279. rc = dev->ops->dep_link_down(dev);
  280. if (!rc) {
  281. dev->dep_link_up = false;
  282. dev->active_target = NULL;
  283. dev->rf_mode = NFC_RF_NONE;
  284. nfc_llcp_mac_is_down(dev);
  285. nfc_genl_dep_link_down_event(dev);
  286. }
  287. error:
  288. device_unlock(&dev->dev);
  289. return rc;
  290. }
  291. int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
  292. u8 comm_mode, u8 rf_mode)
  293. {
  294. dev->dep_link_up = true;
  295. if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
  296. struct nfc_target *target;
  297. target = nfc_find_target(dev, target_idx);
  298. if (target == NULL)
  299. return -ENOTCONN;
  300. dev->active_target = target;
  301. }
  302. dev->polling = false;
  303. dev->rf_mode = rf_mode;
  304. nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
  305. return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
  306. }
  307. EXPORT_SYMBOL(nfc_dep_link_is_up);
  308. /**
  309. * nfc_activate_target - prepare the target for data exchange
  310. *
  311. * @dev: The nfc device that found the target
  312. * @target_idx: index of the target that must be activated
  313. * @protocol: nfc protocol that will be used for data exchange
  314. */
  315. int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
  316. {
  317. int rc;
  318. struct nfc_target *target;
  319. pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
  320. dev_name(&dev->dev), target_idx, protocol);
  321. device_lock(&dev->dev);
  322. if (!device_is_registered(&dev->dev)) {
  323. rc = -ENODEV;
  324. goto error;
  325. }
  326. if (dev->active_target) {
  327. rc = -EBUSY;
  328. goto error;
  329. }
  330. target = nfc_find_target(dev, target_idx);
  331. if (target == NULL) {
  332. rc = -ENOTCONN;
  333. goto error;
  334. }
  335. rc = dev->ops->activate_target(dev, target, protocol);
  336. if (!rc) {
  337. dev->active_target = target;
  338. dev->rf_mode = NFC_RF_INITIATOR;
  339. if (dev->ops->check_presence && !dev->shutting_down)
  340. mod_timer(&dev->check_pres_timer, jiffies +
  341. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  342. }
  343. error:
  344. device_unlock(&dev->dev);
  345. return rc;
  346. }
  347. /**
  348. * nfc_deactivate_target - deactivate a nfc target
  349. *
  350. * @dev: The nfc device that found the target
  351. * @target_idx: index of the target that must be deactivated
  352. */
  353. int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
  354. {
  355. int rc = 0;
  356. pr_debug("dev_name=%s target_idx=%u\n",
  357. dev_name(&dev->dev), target_idx);
  358. device_lock(&dev->dev);
  359. if (!device_is_registered(&dev->dev)) {
  360. rc = -ENODEV;
  361. goto error;
  362. }
  363. if (dev->active_target == NULL) {
  364. rc = -ENOTCONN;
  365. goto error;
  366. }
  367. if (dev->active_target->idx != target_idx) {
  368. rc = -ENOTCONN;
  369. goto error;
  370. }
  371. if (dev->ops->check_presence)
  372. del_timer_sync(&dev->check_pres_timer);
  373. dev->ops->deactivate_target(dev, dev->active_target, mode);
  374. dev->active_target = NULL;
  375. error:
  376. device_unlock(&dev->dev);
  377. return rc;
  378. }
  379. /**
  380. * nfc_data_exchange - transceive data
  381. *
  382. * @dev: The nfc device that found the target
  383. * @target_idx: index of the target
  384. * @skb: data to be sent
  385. * @cb: callback called when the response is received
  386. * @cb_context: parameter for the callback function
  387. *
  388. * The user must wait for the callback before calling this function again.
  389. */
  390. int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
  391. data_exchange_cb_t cb, void *cb_context)
  392. {
  393. int rc;
  394. pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
  395. dev_name(&dev->dev), target_idx, skb->len);
  396. device_lock(&dev->dev);
  397. if (!device_is_registered(&dev->dev)) {
  398. rc = -ENODEV;
  399. kfree_skb(skb);
  400. goto error;
  401. }
  402. if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
  403. if (dev->active_target->idx != target_idx) {
  404. rc = -EADDRNOTAVAIL;
  405. kfree_skb(skb);
  406. goto error;
  407. }
  408. if (dev->ops->check_presence)
  409. del_timer_sync(&dev->check_pres_timer);
  410. rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
  411. cb_context);
  412. if (!rc && dev->ops->check_presence && !dev->shutting_down)
  413. mod_timer(&dev->check_pres_timer, jiffies +
  414. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  415. } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
  416. rc = dev->ops->tm_send(dev, skb);
  417. } else {
  418. rc = -ENOTCONN;
  419. kfree_skb(skb);
  420. goto error;
  421. }
  422. error:
  423. device_unlock(&dev->dev);
  424. return rc;
  425. }
  426. struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
  427. {
  428. struct nfc_se *se;
  429. list_for_each_entry(se, &dev->secure_elements, list)
  430. if (se->idx == se_idx)
  431. return se;
  432. return NULL;
  433. }
  434. EXPORT_SYMBOL(nfc_find_se);
  435. int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
  436. {
  437. struct nfc_se *se;
  438. int rc;
  439. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  440. device_lock(&dev->dev);
  441. if (!device_is_registered(&dev->dev)) {
  442. rc = -ENODEV;
  443. goto error;
  444. }
  445. if (!dev->dev_up) {
  446. rc = -ENODEV;
  447. goto error;
  448. }
  449. if (dev->polling) {
  450. rc = -EBUSY;
  451. goto error;
  452. }
  453. if (!dev->ops->enable_se || !dev->ops->disable_se) {
  454. rc = -EOPNOTSUPP;
  455. goto error;
  456. }
  457. se = nfc_find_se(dev, se_idx);
  458. if (!se) {
  459. rc = -EINVAL;
  460. goto error;
  461. }
  462. if (se->state == NFC_SE_ENABLED) {
  463. rc = -EALREADY;
  464. goto error;
  465. }
  466. rc = dev->ops->enable_se(dev, se_idx);
  467. if (rc >= 0)
  468. se->state = NFC_SE_ENABLED;
  469. error:
  470. device_unlock(&dev->dev);
  471. return rc;
  472. }
  473. int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
  474. {
  475. struct nfc_se *se;
  476. int rc;
  477. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  478. device_lock(&dev->dev);
  479. if (!device_is_registered(&dev->dev)) {
  480. rc = -ENODEV;
  481. goto error;
  482. }
  483. if (!dev->dev_up) {
  484. rc = -ENODEV;
  485. goto error;
  486. }
  487. if (!dev->ops->enable_se || !dev->ops->disable_se) {
  488. rc = -EOPNOTSUPP;
  489. goto error;
  490. }
  491. se = nfc_find_se(dev, se_idx);
  492. if (!se) {
  493. rc = -EINVAL;
  494. goto error;
  495. }
  496. if (se->state == NFC_SE_DISABLED) {
  497. rc = -EALREADY;
  498. goto error;
  499. }
  500. rc = dev->ops->disable_se(dev, se_idx);
  501. if (rc >= 0)
  502. se->state = NFC_SE_DISABLED;
  503. error:
  504. device_unlock(&dev->dev);
  505. return rc;
  506. }
  507. int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
  508. {
  509. pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
  510. return nfc_llcp_set_remote_gb(dev, gb, gb_len);
  511. }
  512. EXPORT_SYMBOL(nfc_set_remote_general_bytes);
  513. u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
  514. {
  515. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  516. return nfc_llcp_general_bytes(dev, gb_len);
  517. }
  518. EXPORT_SYMBOL(nfc_get_local_general_bytes);
  519. int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
  520. {
  521. /* Only LLCP target mode for now */
  522. if (dev->dep_link_up == false) {
  523. kfree_skb(skb);
  524. return -ENOLINK;
  525. }
  526. return nfc_llcp_data_received(dev, skb);
  527. }
  528. EXPORT_SYMBOL(nfc_tm_data_received);
  529. int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
  530. u8 *gb, size_t gb_len)
  531. {
  532. int rc;
  533. device_lock(&dev->dev);
  534. dev->polling = false;
  535. if (gb != NULL) {
  536. rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
  537. if (rc < 0)
  538. goto out;
  539. }
  540. dev->rf_mode = NFC_RF_TARGET;
  541. if (protocol == NFC_PROTO_NFC_DEP_MASK)
  542. nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
  543. rc = nfc_genl_tm_activated(dev, protocol);
  544. out:
  545. device_unlock(&dev->dev);
  546. return rc;
  547. }
  548. EXPORT_SYMBOL(nfc_tm_activated);
  549. int nfc_tm_deactivated(struct nfc_dev *dev)
  550. {
  551. dev->dep_link_up = false;
  552. dev->rf_mode = NFC_RF_NONE;
  553. return nfc_genl_tm_deactivated(dev);
  554. }
  555. EXPORT_SYMBOL(nfc_tm_deactivated);
  556. /**
  557. * nfc_alloc_send_skb - allocate a skb for data exchange responses
  558. *
  559. * @size: size to allocate
  560. * @gfp: gfp flags
  561. */
  562. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  563. unsigned int flags, unsigned int size,
  564. unsigned int *err)
  565. {
  566. struct sk_buff *skb;
  567. unsigned int total_size;
  568. total_size = size +
  569. dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  570. skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
  571. if (skb)
  572. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  573. return skb;
  574. }
  575. /**
  576. * nfc_alloc_recv_skb - allocate a skb for data exchange responses
  577. *
  578. * @size: size to allocate
  579. * @gfp: gfp flags
  580. */
  581. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
  582. {
  583. struct sk_buff *skb;
  584. unsigned int total_size;
  585. total_size = size + 1;
  586. skb = alloc_skb(total_size, gfp);
  587. if (skb)
  588. skb_reserve(skb, 1);
  589. return skb;
  590. }
  591. EXPORT_SYMBOL(nfc_alloc_recv_skb);
  592. /**
  593. * nfc_targets_found - inform that targets were found
  594. *
  595. * @dev: The nfc device that found the targets
  596. * @targets: array of nfc targets found
  597. * @ntargets: targets array size
  598. *
  599. * The device driver must call this function when one or many nfc targets
  600. * are found. After calling this function, the device driver must stop
  601. * polling for targets.
  602. * NOTE: This function can be called with targets=NULL and n_targets=0 to
  603. * notify a driver error, meaning that the polling operation cannot complete.
  604. * IMPORTANT: this function must not be called from an atomic context.
  605. * In addition, it must also not be called from a context that would prevent
  606. * the NFC Core to call other nfc ops entry point concurrently.
  607. */
  608. int nfc_targets_found(struct nfc_dev *dev,
  609. struct nfc_target *targets, int n_targets)
  610. {
  611. int i;
  612. pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
  613. for (i = 0; i < n_targets; i++)
  614. targets[i].idx = dev->target_next_idx++;
  615. device_lock(&dev->dev);
  616. if (dev->polling == false) {
  617. device_unlock(&dev->dev);
  618. return 0;
  619. }
  620. dev->polling = false;
  621. dev->targets_generation++;
  622. kfree(dev->targets);
  623. dev->targets = NULL;
  624. if (targets) {
  625. dev->targets = kmemdup(targets,
  626. n_targets * sizeof(struct nfc_target),
  627. GFP_ATOMIC);
  628. if (!dev->targets) {
  629. dev->n_targets = 0;
  630. device_unlock(&dev->dev);
  631. return -ENOMEM;
  632. }
  633. }
  634. dev->n_targets = n_targets;
  635. device_unlock(&dev->dev);
  636. nfc_genl_targets_found(dev);
  637. return 0;
  638. }
  639. EXPORT_SYMBOL(nfc_targets_found);
  640. /**
  641. * nfc_target_lost - inform that an activated target went out of field
  642. *
  643. * @dev: The nfc device that had the activated target in field
  644. * @target_idx: the nfc index of the target
  645. *
  646. * The device driver must call this function when the activated target
  647. * goes out of the field.
  648. * IMPORTANT: this function must not be called from an atomic context.
  649. * In addition, it must also not be called from a context that would prevent
  650. * the NFC Core to call other nfc ops entry point concurrently.
  651. */
  652. int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
  653. {
  654. struct nfc_target *tg;
  655. int i;
  656. pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
  657. device_lock(&dev->dev);
  658. for (i = 0; i < dev->n_targets; i++) {
  659. tg = &dev->targets[i];
  660. if (tg->idx == target_idx)
  661. break;
  662. }
  663. if (i == dev->n_targets) {
  664. device_unlock(&dev->dev);
  665. return -EINVAL;
  666. }
  667. dev->targets_generation++;
  668. dev->n_targets--;
  669. dev->active_target = NULL;
  670. if (dev->n_targets) {
  671. memcpy(&dev->targets[i], &dev->targets[i + 1],
  672. (dev->n_targets - i) * sizeof(struct nfc_target));
  673. } else {
  674. kfree(dev->targets);
  675. dev->targets = NULL;
  676. }
  677. device_unlock(&dev->dev);
  678. nfc_genl_target_lost(dev, target_idx);
  679. return 0;
  680. }
  681. EXPORT_SYMBOL(nfc_target_lost);
  682. inline void nfc_driver_failure(struct nfc_dev *dev, int err)
  683. {
  684. nfc_targets_found(dev, NULL, 0);
  685. }
  686. EXPORT_SYMBOL(nfc_driver_failure);
  687. int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
  688. {
  689. struct nfc_se *se;
  690. int rc;
  691. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  692. se = nfc_find_se(dev, se_idx);
  693. if (se)
  694. return -EALREADY;
  695. se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
  696. if (!se)
  697. return -ENOMEM;
  698. se->idx = se_idx;
  699. se->type = type;
  700. se->state = NFC_SE_DISABLED;
  701. INIT_LIST_HEAD(&se->list);
  702. list_add(&se->list, &dev->secure_elements);
  703. rc = nfc_genl_se_added(dev, se_idx, type);
  704. if (rc < 0) {
  705. list_del(&se->list);
  706. kfree(se);
  707. return rc;
  708. }
  709. return 0;
  710. }
  711. EXPORT_SYMBOL(nfc_add_se);
  712. int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
  713. {
  714. struct nfc_se *se, *n;
  715. int rc;
  716. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  717. list_for_each_entry_safe(se, n, &dev->secure_elements, list)
  718. if (se->idx == se_idx) {
  719. rc = nfc_genl_se_removed(dev, se_idx);
  720. if (rc < 0)
  721. return rc;
  722. list_del(&se->list);
  723. kfree(se);
  724. return 0;
  725. }
  726. return -EINVAL;
  727. }
  728. EXPORT_SYMBOL(nfc_remove_se);
  729. int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
  730. struct nfc_evt_transaction *evt_transaction)
  731. {
  732. int rc;
  733. pr_debug("transaction: %x\n", se_idx);
  734. device_lock(&dev->dev);
  735. if (!evt_transaction) {
  736. rc = -EPROTO;
  737. goto out;
  738. }
  739. rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
  740. out:
  741. device_unlock(&dev->dev);
  742. return rc;
  743. }
  744. EXPORT_SYMBOL(nfc_se_transaction);
  745. int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
  746. {
  747. int rc;
  748. pr_debug("connectivity: %x\n", se_idx);
  749. device_lock(&dev->dev);
  750. rc = nfc_genl_se_connectivity(dev, se_idx);
  751. device_unlock(&dev->dev);
  752. return rc;
  753. }
  754. EXPORT_SYMBOL(nfc_se_connectivity);
  755. static void nfc_release(struct device *d)
  756. {
  757. struct nfc_dev *dev = to_nfc_dev(d);
  758. struct nfc_se *se, *n;
  759. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  760. nfc_genl_data_exit(&dev->genl_data);
  761. kfree(dev->targets);
  762. list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
  763. nfc_genl_se_removed(dev, se->idx);
  764. list_del(&se->list);
  765. kfree(se);
  766. }
  767. ida_simple_remove(&nfc_index_ida, dev->idx);
  768. kfree(dev);
  769. }
  770. static void nfc_check_pres_work(struct work_struct *work)
  771. {
  772. struct nfc_dev *dev = container_of(work, struct nfc_dev,
  773. check_pres_work);
  774. int rc;
  775. device_lock(&dev->dev);
  776. if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
  777. rc = dev->ops->check_presence(dev, dev->active_target);
  778. if (rc == -EOPNOTSUPP)
  779. goto exit;
  780. if (rc) {
  781. u32 active_target_idx = dev->active_target->idx;
  782. device_unlock(&dev->dev);
  783. nfc_target_lost(dev, active_target_idx);
  784. return;
  785. }
  786. if (!dev->shutting_down)
  787. mod_timer(&dev->check_pres_timer, jiffies +
  788. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  789. }
  790. exit:
  791. device_unlock(&dev->dev);
  792. }
  793. static void nfc_check_pres_timeout(struct timer_list *t)
  794. {
  795. struct nfc_dev *dev = from_timer(dev, t, check_pres_timer);
  796. schedule_work(&dev->check_pres_work);
  797. }
  798. struct class nfc_class = {
  799. .name = "nfc",
  800. .dev_release = nfc_release,
  801. };
  802. EXPORT_SYMBOL(nfc_class);
  803. static int match_idx(struct device *d, const void *data)
  804. {
  805. struct nfc_dev *dev = to_nfc_dev(d);
  806. const unsigned int *idx = data;
  807. return dev->idx == *idx;
  808. }
  809. struct nfc_dev *nfc_get_device(unsigned int idx)
  810. {
  811. struct device *d;
  812. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  813. if (!d)
  814. return NULL;
  815. return to_nfc_dev(d);
  816. }
  817. /**
  818. * nfc_allocate_device - allocate a new nfc device
  819. *
  820. * @ops: device operations
  821. * @supported_protocols: NFC protocols supported by the device
  822. */
  823. struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
  824. u32 supported_protocols,
  825. int tx_headroom, int tx_tailroom)
  826. {
  827. struct nfc_dev *dev;
  828. int rc;
  829. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  830. !ops->deactivate_target || !ops->im_transceive)
  831. return NULL;
  832. if (!supported_protocols)
  833. return NULL;
  834. dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  835. if (!dev)
  836. return NULL;
  837. rc = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
  838. if (rc < 0)
  839. goto err_free_dev;
  840. dev->idx = rc;
  841. dev->dev.class = &nfc_class;
  842. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  843. device_initialize(&dev->dev);
  844. dev->ops = ops;
  845. dev->supported_protocols = supported_protocols;
  846. dev->tx_headroom = tx_headroom;
  847. dev->tx_tailroom = tx_tailroom;
  848. INIT_LIST_HEAD(&dev->secure_elements);
  849. nfc_genl_data_init(&dev->genl_data);
  850. dev->rf_mode = NFC_RF_NONE;
  851. /* first generation must not be 0 */
  852. dev->targets_generation = 1;
  853. if (ops->check_presence) {
  854. timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0);
  855. INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
  856. }
  857. return dev;
  858. err_free_dev:
  859. kfree(dev);
  860. return NULL;
  861. }
  862. EXPORT_SYMBOL(nfc_allocate_device);
  863. /**
  864. * nfc_register_device - register a nfc device in the nfc subsystem
  865. *
  866. * @dev: The nfc device to register
  867. */
  868. int nfc_register_device(struct nfc_dev *dev)
  869. {
  870. int rc;
  871. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  872. mutex_lock(&nfc_devlist_mutex);
  873. nfc_devlist_generation++;
  874. rc = device_add(&dev->dev);
  875. mutex_unlock(&nfc_devlist_mutex);
  876. if (rc < 0)
  877. return rc;
  878. rc = nfc_llcp_register_device(dev);
  879. if (rc)
  880. pr_err("Could not register llcp device\n");
  881. rc = nfc_genl_device_added(dev);
  882. if (rc)
  883. pr_debug("The userspace won't be notified that the device %s was added\n",
  884. dev_name(&dev->dev));
  885. dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
  886. RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
  887. if (dev->rfkill) {
  888. if (rfkill_register(dev->rfkill) < 0) {
  889. rfkill_destroy(dev->rfkill);
  890. dev->rfkill = NULL;
  891. }
  892. }
  893. return 0;
  894. }
  895. EXPORT_SYMBOL(nfc_register_device);
  896. /**
  897. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  898. *
  899. * @dev: The nfc device to unregister
  900. */
  901. void nfc_unregister_device(struct nfc_dev *dev)
  902. {
  903. int rc;
  904. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  905. if (dev->rfkill) {
  906. rfkill_unregister(dev->rfkill);
  907. rfkill_destroy(dev->rfkill);
  908. }
  909. if (dev->ops->check_presence) {
  910. device_lock(&dev->dev);
  911. dev->shutting_down = true;
  912. device_unlock(&dev->dev);
  913. del_timer_sync(&dev->check_pres_timer);
  914. cancel_work_sync(&dev->check_pres_work);
  915. }
  916. rc = nfc_genl_device_removed(dev);
  917. if (rc)
  918. pr_debug("The userspace won't be notified that the device %s "
  919. "was removed\n", dev_name(&dev->dev));
  920. nfc_llcp_unregister_device(dev);
  921. mutex_lock(&nfc_devlist_mutex);
  922. nfc_devlist_generation++;
  923. device_del(&dev->dev);
  924. mutex_unlock(&nfc_devlist_mutex);
  925. }
  926. EXPORT_SYMBOL(nfc_unregister_device);
  927. static int __init nfc_init(void)
  928. {
  929. int rc;
  930. pr_info("NFC Core ver %s\n", VERSION);
  931. rc = class_register(&nfc_class);
  932. if (rc)
  933. return rc;
  934. rc = nfc_genl_init();
  935. if (rc)
  936. goto err_genl;
  937. /* the first generation must not be 0 */
  938. nfc_devlist_generation = 1;
  939. rc = rawsock_init();
  940. if (rc)
  941. goto err_rawsock;
  942. rc = nfc_llcp_init();
  943. if (rc)
  944. goto err_llcp_sock;
  945. rc = af_nfc_init();
  946. if (rc)
  947. goto err_af_nfc;
  948. return 0;
  949. err_af_nfc:
  950. nfc_llcp_exit();
  951. err_llcp_sock:
  952. rawsock_exit();
  953. err_rawsock:
  954. nfc_genl_exit();
  955. err_genl:
  956. class_unregister(&nfc_class);
  957. return rc;
  958. }
  959. static void __exit nfc_exit(void)
  960. {
  961. af_nfc_exit();
  962. nfc_llcp_exit();
  963. rawsock_exit();
  964. nfc_genl_exit();
  965. class_unregister(&nfc_class);
  966. }
  967. subsys_initcall(nfc_init);
  968. module_exit(nfc_exit);
  969. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  970. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  971. MODULE_VERSION(VERSION);
  972. MODULE_LICENSE("GPL");
  973. MODULE_ALIAS_NETPROTO(PF_NFC);
  974. MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);