core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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/nfc.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. /**
  35. * nfc_dev_up - turn on the NFC device
  36. *
  37. * @dev: The nfc device to be turned on
  38. *
  39. * The device remains up until the nfc_dev_down function is called.
  40. */
  41. int nfc_dev_up(struct nfc_dev *dev)
  42. {
  43. int rc = 0;
  44. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  45. device_lock(&dev->dev);
  46. if (!device_is_registered(&dev->dev)) {
  47. rc = -ENODEV;
  48. goto error;
  49. }
  50. if (dev->dev_up) {
  51. rc = -EALREADY;
  52. goto error;
  53. }
  54. if (dev->ops->dev_up)
  55. rc = dev->ops->dev_up(dev);
  56. if (!rc)
  57. dev->dev_up = true;
  58. error:
  59. device_unlock(&dev->dev);
  60. return rc;
  61. }
  62. /**
  63. * nfc_dev_down - turn off the NFC device
  64. *
  65. * @dev: The nfc device to be turned off
  66. */
  67. int nfc_dev_down(struct nfc_dev *dev)
  68. {
  69. int rc = 0;
  70. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  71. device_lock(&dev->dev);
  72. if (!device_is_registered(&dev->dev)) {
  73. rc = -ENODEV;
  74. goto error;
  75. }
  76. if (!dev->dev_up) {
  77. rc = -EALREADY;
  78. goto error;
  79. }
  80. if (dev->polling || dev->active_target) {
  81. rc = -EBUSY;
  82. goto error;
  83. }
  84. if (dev->ops->dev_down)
  85. dev->ops->dev_down(dev);
  86. dev->dev_up = false;
  87. error:
  88. device_unlock(&dev->dev);
  89. return rc;
  90. }
  91. /**
  92. * nfc_start_poll - start polling for nfc targets
  93. *
  94. * @dev: The nfc device that must start polling
  95. * @protocols: bitset of nfc protocols that must be used for polling
  96. *
  97. * The device remains polling for targets until a target is found or
  98. * the nfc_stop_poll function is called.
  99. */
  100. int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
  101. {
  102. int rc;
  103. pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
  104. dev_name(&dev->dev), im_protocols, tm_protocols);
  105. if (!im_protocols && !tm_protocols)
  106. return -EINVAL;
  107. device_lock(&dev->dev);
  108. if (!device_is_registered(&dev->dev)) {
  109. rc = -ENODEV;
  110. goto error;
  111. }
  112. if (dev->polling) {
  113. rc = -EBUSY;
  114. goto error;
  115. }
  116. rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
  117. if (!rc) {
  118. dev->polling = true;
  119. dev->rf_mode = NFC_RF_NONE;
  120. }
  121. error:
  122. device_unlock(&dev->dev);
  123. return rc;
  124. }
  125. /**
  126. * nfc_stop_poll - stop polling for nfc targets
  127. *
  128. * @dev: The nfc device that must stop polling
  129. */
  130. int nfc_stop_poll(struct nfc_dev *dev)
  131. {
  132. int rc = 0;
  133. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  134. device_lock(&dev->dev);
  135. if (!device_is_registered(&dev->dev)) {
  136. rc = -ENODEV;
  137. goto error;
  138. }
  139. if (!dev->polling) {
  140. rc = -EINVAL;
  141. goto error;
  142. }
  143. dev->ops->stop_poll(dev);
  144. dev->polling = false;
  145. error:
  146. device_unlock(&dev->dev);
  147. return rc;
  148. }
  149. static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
  150. {
  151. int i;
  152. if (dev->n_targets == 0)
  153. return NULL;
  154. for (i = 0; i < dev->n_targets ; i++) {
  155. if (dev->targets[i].idx == target_idx)
  156. return &dev->targets[i];
  157. }
  158. return NULL;
  159. }
  160. int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
  161. {
  162. int rc = 0;
  163. u8 *gb;
  164. size_t gb_len;
  165. struct nfc_target *target;
  166. pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
  167. if (!dev->ops->dep_link_up)
  168. return -EOPNOTSUPP;
  169. device_lock(&dev->dev);
  170. if (!device_is_registered(&dev->dev)) {
  171. rc = -ENODEV;
  172. goto error;
  173. }
  174. if (dev->dep_link_up == true) {
  175. rc = -EALREADY;
  176. goto error;
  177. }
  178. gb = nfc_llcp_general_bytes(dev, &gb_len);
  179. if (gb_len > NFC_MAX_GT_LEN) {
  180. rc = -EINVAL;
  181. goto error;
  182. }
  183. target = nfc_find_target(dev, target_index);
  184. if (target == NULL) {
  185. rc = -ENOTCONN;
  186. goto error;
  187. }
  188. rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
  189. if (!rc) {
  190. dev->active_target = target;
  191. dev->rf_mode = NFC_RF_INITIATOR;
  192. }
  193. error:
  194. device_unlock(&dev->dev);
  195. return rc;
  196. }
  197. int nfc_dep_link_down(struct nfc_dev *dev)
  198. {
  199. int rc = 0;
  200. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  201. if (!dev->ops->dep_link_down)
  202. return -EOPNOTSUPP;
  203. device_lock(&dev->dev);
  204. if (!device_is_registered(&dev->dev)) {
  205. rc = -ENODEV;
  206. goto error;
  207. }
  208. if (dev->dep_link_up == false) {
  209. rc = -EALREADY;
  210. goto error;
  211. }
  212. rc = dev->ops->dep_link_down(dev);
  213. if (!rc) {
  214. dev->dep_link_up = false;
  215. dev->active_target = NULL;
  216. nfc_llcp_mac_is_down(dev);
  217. nfc_genl_dep_link_down_event(dev);
  218. }
  219. error:
  220. device_unlock(&dev->dev);
  221. return rc;
  222. }
  223. int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
  224. u8 comm_mode, u8 rf_mode)
  225. {
  226. dev->dep_link_up = true;
  227. nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
  228. return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
  229. }
  230. EXPORT_SYMBOL(nfc_dep_link_is_up);
  231. /**
  232. * nfc_activate_target - prepare the target for data exchange
  233. *
  234. * @dev: The nfc device that found the target
  235. * @target_idx: index of the target that must be activated
  236. * @protocol: nfc protocol that will be used for data exchange
  237. */
  238. int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
  239. {
  240. int rc;
  241. struct nfc_target *target;
  242. pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
  243. dev_name(&dev->dev), target_idx, protocol);
  244. device_lock(&dev->dev);
  245. if (!device_is_registered(&dev->dev)) {
  246. rc = -ENODEV;
  247. goto error;
  248. }
  249. if (dev->active_target) {
  250. rc = -EBUSY;
  251. goto error;
  252. }
  253. target = nfc_find_target(dev, target_idx);
  254. if (target == NULL) {
  255. rc = -ENOTCONN;
  256. goto error;
  257. }
  258. rc = dev->ops->activate_target(dev, target, protocol);
  259. if (!rc) {
  260. dev->active_target = target;
  261. dev->rf_mode = NFC_RF_INITIATOR;
  262. if (dev->ops->check_presence)
  263. mod_timer(&dev->check_pres_timer, jiffies +
  264. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  265. }
  266. error:
  267. device_unlock(&dev->dev);
  268. return rc;
  269. }
  270. /**
  271. * nfc_deactivate_target - deactivate a nfc target
  272. *
  273. * @dev: The nfc device that found the target
  274. * @target_idx: index of the target that must be deactivated
  275. */
  276. int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
  277. {
  278. int rc = 0;
  279. pr_debug("dev_name=%s target_idx=%u\n",
  280. dev_name(&dev->dev), target_idx);
  281. device_lock(&dev->dev);
  282. if (!device_is_registered(&dev->dev)) {
  283. rc = -ENODEV;
  284. goto error;
  285. }
  286. if (dev->active_target == NULL) {
  287. rc = -ENOTCONN;
  288. goto error;
  289. }
  290. if (dev->active_target->idx != target_idx) {
  291. rc = -ENOTCONN;
  292. goto error;
  293. }
  294. if (dev->ops->check_presence)
  295. del_timer_sync(&dev->check_pres_timer);
  296. dev->ops->deactivate_target(dev, dev->active_target);
  297. dev->active_target = NULL;
  298. error:
  299. device_unlock(&dev->dev);
  300. return rc;
  301. }
  302. /**
  303. * nfc_data_exchange - transceive data
  304. *
  305. * @dev: The nfc device that found the target
  306. * @target_idx: index of the target
  307. * @skb: data to be sent
  308. * @cb: callback called when the response is received
  309. * @cb_context: parameter for the callback function
  310. *
  311. * The user must wait for the callback before calling this function again.
  312. */
  313. int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
  314. data_exchange_cb_t cb, void *cb_context)
  315. {
  316. int rc;
  317. pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
  318. dev_name(&dev->dev), target_idx, skb->len);
  319. device_lock(&dev->dev);
  320. if (!device_is_registered(&dev->dev)) {
  321. rc = -ENODEV;
  322. kfree_skb(skb);
  323. goto error;
  324. }
  325. if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
  326. if (dev->active_target->idx != target_idx) {
  327. rc = -EADDRNOTAVAIL;
  328. kfree_skb(skb);
  329. goto error;
  330. }
  331. if (dev->ops->check_presence)
  332. del_timer_sync(&dev->check_pres_timer);
  333. rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
  334. cb_context);
  335. if (!rc && dev->ops->check_presence)
  336. mod_timer(&dev->check_pres_timer, jiffies +
  337. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  338. } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
  339. rc = dev->ops->tm_send(dev, skb);
  340. } else {
  341. rc = -ENOTCONN;
  342. kfree_skb(skb);
  343. goto error;
  344. }
  345. error:
  346. device_unlock(&dev->dev);
  347. return rc;
  348. }
  349. int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
  350. {
  351. pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
  352. if (gb_len > NFC_MAX_GT_LEN)
  353. return -EINVAL;
  354. return nfc_llcp_set_remote_gb(dev, gb, gb_len);
  355. }
  356. EXPORT_SYMBOL(nfc_set_remote_general_bytes);
  357. u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
  358. {
  359. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  360. return nfc_llcp_general_bytes(dev, gb_len);
  361. }
  362. EXPORT_SYMBOL(nfc_get_local_general_bytes);
  363. int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
  364. {
  365. /* Only LLCP target mode for now */
  366. if (dev->dep_link_up == false) {
  367. kfree_skb(skb);
  368. return -ENOLINK;
  369. }
  370. return nfc_llcp_data_received(dev, skb);
  371. }
  372. EXPORT_SYMBOL(nfc_tm_data_received);
  373. int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
  374. u8 *gb, size_t gb_len)
  375. {
  376. int rc;
  377. device_lock(&dev->dev);
  378. dev->polling = false;
  379. if (gb != NULL) {
  380. rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
  381. if (rc < 0)
  382. goto out;
  383. }
  384. dev->rf_mode = NFC_RF_TARGET;
  385. if (protocol == NFC_PROTO_NFC_DEP_MASK)
  386. nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
  387. rc = nfc_genl_tm_activated(dev, protocol);
  388. out:
  389. device_unlock(&dev->dev);
  390. return rc;
  391. }
  392. EXPORT_SYMBOL(nfc_tm_activated);
  393. int nfc_tm_deactivated(struct nfc_dev *dev)
  394. {
  395. dev->dep_link_up = false;
  396. return nfc_genl_tm_deactivated(dev);
  397. }
  398. EXPORT_SYMBOL(nfc_tm_deactivated);
  399. /**
  400. * nfc_alloc_send_skb - allocate a skb for data exchange responses
  401. *
  402. * @size: size to allocate
  403. * @gfp: gfp flags
  404. */
  405. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  406. unsigned int flags, unsigned int size,
  407. unsigned int *err)
  408. {
  409. struct sk_buff *skb;
  410. unsigned int total_size;
  411. total_size = size +
  412. dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  413. skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
  414. if (skb)
  415. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  416. return skb;
  417. }
  418. /**
  419. * nfc_alloc_recv_skb - allocate a skb for data exchange responses
  420. *
  421. * @size: size to allocate
  422. * @gfp: gfp flags
  423. */
  424. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
  425. {
  426. struct sk_buff *skb;
  427. unsigned int total_size;
  428. total_size = size + 1;
  429. skb = alloc_skb(total_size, gfp);
  430. if (skb)
  431. skb_reserve(skb, 1);
  432. return skb;
  433. }
  434. EXPORT_SYMBOL(nfc_alloc_recv_skb);
  435. /**
  436. * nfc_targets_found - inform that targets were found
  437. *
  438. * @dev: The nfc device that found the targets
  439. * @targets: array of nfc targets found
  440. * @ntargets: targets array size
  441. *
  442. * The device driver must call this function when one or many nfc targets
  443. * are found. After calling this function, the device driver must stop
  444. * polling for targets.
  445. * IMPORTANT: this function must not be called from an atomic context.
  446. * In addition, it must also not be called from a context that would prevent
  447. * the NFC Core to call other nfc ops entry point concurrently.
  448. */
  449. int nfc_targets_found(struct nfc_dev *dev,
  450. struct nfc_target *targets, int n_targets)
  451. {
  452. int i;
  453. pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
  454. dev->polling = false;
  455. for (i = 0; i < n_targets; i++)
  456. targets[i].idx = dev->target_next_idx++;
  457. device_lock(&dev->dev);
  458. dev->targets_generation++;
  459. kfree(dev->targets);
  460. dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target),
  461. GFP_ATOMIC);
  462. if (!dev->targets) {
  463. dev->n_targets = 0;
  464. device_unlock(&dev->dev);
  465. return -ENOMEM;
  466. }
  467. dev->n_targets = n_targets;
  468. device_unlock(&dev->dev);
  469. nfc_genl_targets_found(dev);
  470. return 0;
  471. }
  472. EXPORT_SYMBOL(nfc_targets_found);
  473. /**
  474. * nfc_target_lost - inform that an activated target went out of field
  475. *
  476. * @dev: The nfc device that had the activated target in field
  477. * @target_idx: the nfc index of the target
  478. *
  479. * The device driver must call this function when the activated target
  480. * goes out of the field.
  481. * IMPORTANT: this function must not be called from an atomic context.
  482. * In addition, it must also not be called from a context that would prevent
  483. * the NFC Core to call other nfc ops entry point concurrently.
  484. */
  485. int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
  486. {
  487. struct nfc_target *tg;
  488. int i;
  489. pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
  490. device_lock(&dev->dev);
  491. for (i = 0; i < dev->n_targets; i++) {
  492. tg = &dev->targets[i];
  493. if (tg->idx == target_idx)
  494. break;
  495. }
  496. if (i == dev->n_targets) {
  497. device_unlock(&dev->dev);
  498. return -EINVAL;
  499. }
  500. dev->targets_generation++;
  501. dev->n_targets--;
  502. dev->active_target = NULL;
  503. if (dev->n_targets) {
  504. memcpy(&dev->targets[i], &dev->targets[i + 1],
  505. (dev->n_targets - i) * sizeof(struct nfc_target));
  506. } else {
  507. kfree(dev->targets);
  508. dev->targets = NULL;
  509. }
  510. device_unlock(&dev->dev);
  511. nfc_genl_target_lost(dev, target_idx);
  512. return 0;
  513. }
  514. EXPORT_SYMBOL(nfc_target_lost);
  515. void nfc_driver_failure(struct nfc_dev *dev, int err)
  516. {
  517. /*
  518. * TODO: if polling is active, send empty target_found
  519. * or else do whatever makes sense to let user space
  520. * know this device needs to be closed and reinitialized.
  521. */
  522. }
  523. EXPORT_SYMBOL(nfc_driver_failure);
  524. static void nfc_release(struct device *d)
  525. {
  526. struct nfc_dev *dev = to_nfc_dev(d);
  527. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  528. if (dev->ops->check_presence) {
  529. del_timer_sync(&dev->check_pres_timer);
  530. destroy_workqueue(dev->check_pres_wq);
  531. }
  532. nfc_genl_data_exit(&dev->genl_data);
  533. kfree(dev->targets);
  534. kfree(dev);
  535. }
  536. static void nfc_check_pres_work(struct work_struct *work)
  537. {
  538. struct nfc_dev *dev = container_of(work, struct nfc_dev,
  539. check_pres_work);
  540. int rc;
  541. device_lock(&dev->dev);
  542. if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
  543. rc = dev->ops->check_presence(dev, dev->active_target);
  544. if (!rc) {
  545. mod_timer(&dev->check_pres_timer, jiffies +
  546. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  547. } else {
  548. u32 active_target_idx = dev->active_target->idx;
  549. device_unlock(&dev->dev);
  550. nfc_target_lost(dev, active_target_idx);
  551. return;
  552. }
  553. }
  554. device_unlock(&dev->dev);
  555. }
  556. static void nfc_check_pres_timeout(unsigned long data)
  557. {
  558. struct nfc_dev *dev = (struct nfc_dev *)data;
  559. queue_work(dev->check_pres_wq, &dev->check_pres_work);
  560. }
  561. struct class nfc_class = {
  562. .name = "nfc",
  563. .dev_release = nfc_release,
  564. };
  565. EXPORT_SYMBOL(nfc_class);
  566. static int match_idx(struct device *d, void *data)
  567. {
  568. struct nfc_dev *dev = to_nfc_dev(d);
  569. unsigned int *idx = data;
  570. return dev->idx == *idx;
  571. }
  572. struct nfc_dev *nfc_get_device(unsigned int idx)
  573. {
  574. struct device *d;
  575. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  576. if (!d)
  577. return NULL;
  578. return to_nfc_dev(d);
  579. }
  580. /**
  581. * nfc_allocate_device - allocate a new nfc device
  582. *
  583. * @ops: device operations
  584. * @supported_protocols: NFC protocols supported by the device
  585. */
  586. struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
  587. u32 supported_protocols,
  588. int tx_headroom, int tx_tailroom)
  589. {
  590. static atomic_t dev_no = ATOMIC_INIT(0);
  591. struct nfc_dev *dev;
  592. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  593. !ops->deactivate_target || !ops->im_transceive)
  594. return NULL;
  595. if (!supported_protocols)
  596. return NULL;
  597. dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  598. if (!dev)
  599. return NULL;
  600. dev->dev.class = &nfc_class;
  601. dev->idx = atomic_inc_return(&dev_no) - 1;
  602. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  603. device_initialize(&dev->dev);
  604. dev->ops = ops;
  605. dev->supported_protocols = supported_protocols;
  606. dev->tx_headroom = tx_headroom;
  607. dev->tx_tailroom = tx_tailroom;
  608. nfc_genl_data_init(&dev->genl_data);
  609. /* first generation must not be 0 */
  610. dev->targets_generation = 1;
  611. if (ops->check_presence) {
  612. char name[32];
  613. init_timer(&dev->check_pres_timer);
  614. dev->check_pres_timer.data = (unsigned long)dev;
  615. dev->check_pres_timer.function = nfc_check_pres_timeout;
  616. INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
  617. snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx);
  618. dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT |
  619. WQ_UNBOUND |
  620. WQ_MEM_RECLAIM, 1);
  621. if (dev->check_pres_wq == NULL) {
  622. kfree(dev);
  623. return NULL;
  624. }
  625. }
  626. return dev;
  627. }
  628. EXPORT_SYMBOL(nfc_allocate_device);
  629. /**
  630. * nfc_register_device - register a nfc device in the nfc subsystem
  631. *
  632. * @dev: The nfc device to register
  633. */
  634. int nfc_register_device(struct nfc_dev *dev)
  635. {
  636. int rc;
  637. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  638. mutex_lock(&nfc_devlist_mutex);
  639. nfc_devlist_generation++;
  640. rc = device_add(&dev->dev);
  641. mutex_unlock(&nfc_devlist_mutex);
  642. if (rc < 0)
  643. return rc;
  644. rc = nfc_llcp_register_device(dev);
  645. if (rc)
  646. pr_err("Could not register llcp device\n");
  647. rc = nfc_genl_device_added(dev);
  648. if (rc)
  649. pr_debug("The userspace won't be notified that the device %s was added\n",
  650. dev_name(&dev->dev));
  651. return 0;
  652. }
  653. EXPORT_SYMBOL(nfc_register_device);
  654. /**
  655. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  656. *
  657. * @dev: The nfc device to unregister
  658. */
  659. void nfc_unregister_device(struct nfc_dev *dev)
  660. {
  661. int rc;
  662. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  663. mutex_lock(&nfc_devlist_mutex);
  664. nfc_devlist_generation++;
  665. /* lock to avoid unregistering a device while an operation
  666. is in progress */
  667. device_lock(&dev->dev);
  668. device_del(&dev->dev);
  669. device_unlock(&dev->dev);
  670. mutex_unlock(&nfc_devlist_mutex);
  671. nfc_llcp_unregister_device(dev);
  672. rc = nfc_genl_device_removed(dev);
  673. if (rc)
  674. pr_debug("The userspace won't be notified that the device %s was removed\n",
  675. dev_name(&dev->dev));
  676. }
  677. EXPORT_SYMBOL(nfc_unregister_device);
  678. static int __init nfc_init(void)
  679. {
  680. int rc;
  681. pr_info("NFC Core ver %s\n", VERSION);
  682. rc = class_register(&nfc_class);
  683. if (rc)
  684. return rc;
  685. rc = nfc_genl_init();
  686. if (rc)
  687. goto err_genl;
  688. /* the first generation must not be 0 */
  689. nfc_devlist_generation = 1;
  690. rc = rawsock_init();
  691. if (rc)
  692. goto err_rawsock;
  693. rc = nfc_llcp_init();
  694. if (rc)
  695. goto err_llcp_sock;
  696. rc = af_nfc_init();
  697. if (rc)
  698. goto err_af_nfc;
  699. return 0;
  700. err_af_nfc:
  701. nfc_llcp_exit();
  702. err_llcp_sock:
  703. rawsock_exit();
  704. err_rawsock:
  705. nfc_genl_exit();
  706. err_genl:
  707. class_unregister(&nfc_class);
  708. return rc;
  709. }
  710. static void __exit nfc_exit(void)
  711. {
  712. af_nfc_exit();
  713. nfc_llcp_exit();
  714. rawsock_exit();
  715. nfc_genl_exit();
  716. class_unregister(&nfc_class);
  717. }
  718. subsys_initcall(nfc_init);
  719. module_exit(nfc_exit);
  720. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  721. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  722. MODULE_VERSION(VERSION);
  723. MODULE_LICENSE("GPL");