core.c 22 KB

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