core.c 21 KB

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