core.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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_download(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_download) {
  52. rc = -EOPNOTSUPP;
  53. goto error;
  54. }
  55. dev->fw_download_in_progress = true;
  56. rc = dev->ops->fw_download(dev, firmware_name);
  57. if (rc)
  58. dev->fw_download_in_progress = false;
  59. error:
  60. device_unlock(&dev->dev);
  61. return rc;
  62. }
  63. int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name)
  64. {
  65. dev->fw_download_in_progress = false;
  66. return nfc_genl_fw_download_done(dev, firmware_name);
  67. }
  68. EXPORT_SYMBOL(nfc_fw_download_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_download_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. static struct nfc_se *find_se(struct nfc_dev *dev, u32 se_idx)
  417. {
  418. struct nfc_se *se, *n;
  419. list_for_each_entry_safe(se, n, &dev->secure_elements, list)
  420. if (se->idx == se_idx)
  421. return se;
  422. return NULL;
  423. }
  424. int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
  425. {
  426. struct nfc_se *se;
  427. int rc;
  428. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  429. device_lock(&dev->dev);
  430. if (!device_is_registered(&dev->dev)) {
  431. rc = -ENODEV;
  432. goto error;
  433. }
  434. if (!dev->dev_up) {
  435. rc = -ENODEV;
  436. goto error;
  437. }
  438. if (dev->polling) {
  439. rc = -EBUSY;
  440. goto error;
  441. }
  442. if (!dev->ops->enable_se || !dev->ops->disable_se) {
  443. rc = -EOPNOTSUPP;
  444. goto error;
  445. }
  446. se = find_se(dev, se_idx);
  447. if (!se) {
  448. rc = -EINVAL;
  449. goto error;
  450. }
  451. if (se->type == NFC_SE_ENABLED) {
  452. rc = -EALREADY;
  453. goto error;
  454. }
  455. rc = dev->ops->enable_se(dev, se_idx);
  456. error:
  457. device_unlock(&dev->dev);
  458. return rc;
  459. }
  460. int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
  461. {
  462. struct nfc_se *se;
  463. int rc;
  464. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  465. device_lock(&dev->dev);
  466. if (!device_is_registered(&dev->dev)) {
  467. rc = -ENODEV;
  468. goto error;
  469. }
  470. if (!dev->dev_up) {
  471. rc = -ENODEV;
  472. goto error;
  473. }
  474. if (!dev->ops->enable_se || !dev->ops->disable_se) {
  475. rc = -EOPNOTSUPP;
  476. goto error;
  477. }
  478. se = find_se(dev, se_idx);
  479. if (!se) {
  480. rc = -EINVAL;
  481. goto error;
  482. }
  483. if (se->type == NFC_SE_DISABLED) {
  484. rc = -EALREADY;
  485. goto error;
  486. }
  487. rc = dev->ops->disable_se(dev, se_idx);
  488. error:
  489. device_unlock(&dev->dev);
  490. return rc;
  491. }
  492. int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
  493. {
  494. pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
  495. if (gb_len > NFC_MAX_GT_LEN)
  496. return -EINVAL;
  497. return nfc_llcp_set_remote_gb(dev, gb, gb_len);
  498. }
  499. EXPORT_SYMBOL(nfc_set_remote_general_bytes);
  500. u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
  501. {
  502. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  503. return nfc_llcp_general_bytes(dev, gb_len);
  504. }
  505. EXPORT_SYMBOL(nfc_get_local_general_bytes);
  506. int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
  507. {
  508. /* Only LLCP target mode for now */
  509. if (dev->dep_link_up == false) {
  510. kfree_skb(skb);
  511. return -ENOLINK;
  512. }
  513. return nfc_llcp_data_received(dev, skb);
  514. }
  515. EXPORT_SYMBOL(nfc_tm_data_received);
  516. int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
  517. u8 *gb, size_t gb_len)
  518. {
  519. int rc;
  520. device_lock(&dev->dev);
  521. dev->polling = false;
  522. if (gb != NULL) {
  523. rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
  524. if (rc < 0)
  525. goto out;
  526. }
  527. dev->rf_mode = NFC_RF_TARGET;
  528. if (protocol == NFC_PROTO_NFC_DEP_MASK)
  529. nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
  530. rc = nfc_genl_tm_activated(dev, protocol);
  531. out:
  532. device_unlock(&dev->dev);
  533. return rc;
  534. }
  535. EXPORT_SYMBOL(nfc_tm_activated);
  536. int nfc_tm_deactivated(struct nfc_dev *dev)
  537. {
  538. dev->dep_link_up = false;
  539. dev->rf_mode = NFC_RF_NONE;
  540. return nfc_genl_tm_deactivated(dev);
  541. }
  542. EXPORT_SYMBOL(nfc_tm_deactivated);
  543. /**
  544. * nfc_alloc_send_skb - allocate a skb for data exchange responses
  545. *
  546. * @size: size to allocate
  547. * @gfp: gfp flags
  548. */
  549. struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
  550. unsigned int flags, unsigned int size,
  551. unsigned int *err)
  552. {
  553. struct sk_buff *skb;
  554. unsigned int total_size;
  555. total_size = size +
  556. dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  557. skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
  558. if (skb)
  559. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  560. return skb;
  561. }
  562. /**
  563. * nfc_alloc_recv_skb - allocate a skb for data exchange responses
  564. *
  565. * @size: size to allocate
  566. * @gfp: gfp flags
  567. */
  568. struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
  569. {
  570. struct sk_buff *skb;
  571. unsigned int total_size;
  572. total_size = size + 1;
  573. skb = alloc_skb(total_size, gfp);
  574. if (skb)
  575. skb_reserve(skb, 1);
  576. return skb;
  577. }
  578. EXPORT_SYMBOL(nfc_alloc_recv_skb);
  579. /**
  580. * nfc_targets_found - inform that targets were found
  581. *
  582. * @dev: The nfc device that found the targets
  583. * @targets: array of nfc targets found
  584. * @ntargets: targets array size
  585. *
  586. * The device driver must call this function when one or many nfc targets
  587. * are found. After calling this function, the device driver must stop
  588. * polling for targets.
  589. * NOTE: This function can be called with targets=NULL and n_targets=0 to
  590. * notify a driver error, meaning that the polling operation cannot complete.
  591. * IMPORTANT: this function must not be called from an atomic context.
  592. * In addition, it must also not be called from a context that would prevent
  593. * the NFC Core to call other nfc ops entry point concurrently.
  594. */
  595. int nfc_targets_found(struct nfc_dev *dev,
  596. struct nfc_target *targets, int n_targets)
  597. {
  598. int i;
  599. pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
  600. for (i = 0; i < n_targets; i++)
  601. targets[i].idx = dev->target_next_idx++;
  602. device_lock(&dev->dev);
  603. if (dev->polling == false) {
  604. device_unlock(&dev->dev);
  605. return 0;
  606. }
  607. dev->polling = false;
  608. dev->targets_generation++;
  609. kfree(dev->targets);
  610. dev->targets = NULL;
  611. if (targets) {
  612. dev->targets = kmemdup(targets,
  613. n_targets * sizeof(struct nfc_target),
  614. GFP_ATOMIC);
  615. if (!dev->targets) {
  616. dev->n_targets = 0;
  617. device_unlock(&dev->dev);
  618. return -ENOMEM;
  619. }
  620. }
  621. dev->n_targets = n_targets;
  622. device_unlock(&dev->dev);
  623. nfc_genl_targets_found(dev);
  624. return 0;
  625. }
  626. EXPORT_SYMBOL(nfc_targets_found);
  627. /**
  628. * nfc_target_lost - inform that an activated target went out of field
  629. *
  630. * @dev: The nfc device that had the activated target in field
  631. * @target_idx: the nfc index of the target
  632. *
  633. * The device driver must call this function when the activated target
  634. * goes out of the field.
  635. * IMPORTANT: this function must not be called from an atomic context.
  636. * In addition, it must also not be called from a context that would prevent
  637. * the NFC Core to call other nfc ops entry point concurrently.
  638. */
  639. int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
  640. {
  641. struct nfc_target *tg;
  642. int i;
  643. pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
  644. device_lock(&dev->dev);
  645. for (i = 0; i < dev->n_targets; i++) {
  646. tg = &dev->targets[i];
  647. if (tg->idx == target_idx)
  648. break;
  649. }
  650. if (i == dev->n_targets) {
  651. device_unlock(&dev->dev);
  652. return -EINVAL;
  653. }
  654. dev->targets_generation++;
  655. dev->n_targets--;
  656. dev->active_target = NULL;
  657. if (dev->n_targets) {
  658. memcpy(&dev->targets[i], &dev->targets[i + 1],
  659. (dev->n_targets - i) * sizeof(struct nfc_target));
  660. } else {
  661. kfree(dev->targets);
  662. dev->targets = NULL;
  663. }
  664. device_unlock(&dev->dev);
  665. nfc_genl_target_lost(dev, target_idx);
  666. return 0;
  667. }
  668. EXPORT_SYMBOL(nfc_target_lost);
  669. inline void nfc_driver_failure(struct nfc_dev *dev, int err)
  670. {
  671. nfc_targets_found(dev, NULL, 0);
  672. }
  673. EXPORT_SYMBOL(nfc_driver_failure);
  674. int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
  675. {
  676. struct nfc_se *se;
  677. int rc;
  678. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  679. se = find_se(dev, se_idx);
  680. if (se)
  681. return -EALREADY;
  682. se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
  683. if (!se)
  684. return -ENOMEM;
  685. se->idx = se_idx;
  686. se->type = type;
  687. se->state = NFC_SE_DISABLED;
  688. INIT_LIST_HEAD(&se->list);
  689. list_add(&se->list, &dev->secure_elements);
  690. rc = nfc_genl_se_added(dev, se_idx, type);
  691. if (rc < 0) {
  692. list_del(&se->list);
  693. kfree(se);
  694. return rc;
  695. }
  696. return 0;
  697. }
  698. EXPORT_SYMBOL(nfc_add_se);
  699. int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
  700. {
  701. struct nfc_se *se, *n;
  702. int rc;
  703. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  704. list_for_each_entry_safe(se, n, &dev->secure_elements, list)
  705. if (se->idx == se_idx) {
  706. rc = nfc_genl_se_removed(dev, se_idx);
  707. if (rc < 0)
  708. return rc;
  709. list_del(&se->list);
  710. kfree(se);
  711. return 0;
  712. }
  713. return -EINVAL;
  714. }
  715. EXPORT_SYMBOL(nfc_remove_se);
  716. static void nfc_release(struct device *d)
  717. {
  718. struct nfc_dev *dev = to_nfc_dev(d);
  719. struct nfc_se *se, *n;
  720. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  721. nfc_genl_data_exit(&dev->genl_data);
  722. kfree(dev->targets);
  723. list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
  724. nfc_genl_se_removed(dev, se->idx);
  725. list_del(&se->list);
  726. kfree(se);
  727. }
  728. kfree(dev);
  729. }
  730. static void nfc_check_pres_work(struct work_struct *work)
  731. {
  732. struct nfc_dev *dev = container_of(work, struct nfc_dev,
  733. check_pres_work);
  734. int rc;
  735. device_lock(&dev->dev);
  736. if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
  737. rc = dev->ops->check_presence(dev, dev->active_target);
  738. if (rc == -EOPNOTSUPP)
  739. goto exit;
  740. if (rc) {
  741. u32 active_target_idx = dev->active_target->idx;
  742. device_unlock(&dev->dev);
  743. nfc_target_lost(dev, active_target_idx);
  744. return;
  745. }
  746. if (!dev->shutting_down)
  747. mod_timer(&dev->check_pres_timer, jiffies +
  748. msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
  749. }
  750. exit:
  751. device_unlock(&dev->dev);
  752. }
  753. static void nfc_check_pres_timeout(unsigned long data)
  754. {
  755. struct nfc_dev *dev = (struct nfc_dev *)data;
  756. schedule_work(&dev->check_pres_work);
  757. }
  758. struct class nfc_class = {
  759. .name = "nfc",
  760. .dev_release = nfc_release,
  761. };
  762. EXPORT_SYMBOL(nfc_class);
  763. static int match_idx(struct device *d, const void *data)
  764. {
  765. struct nfc_dev *dev = to_nfc_dev(d);
  766. const unsigned int *idx = data;
  767. return dev->idx == *idx;
  768. }
  769. struct nfc_dev *nfc_get_device(unsigned int idx)
  770. {
  771. struct device *d;
  772. d = class_find_device(&nfc_class, NULL, &idx, match_idx);
  773. if (!d)
  774. return NULL;
  775. return to_nfc_dev(d);
  776. }
  777. /**
  778. * nfc_allocate_device - allocate a new nfc device
  779. *
  780. * @ops: device operations
  781. * @supported_protocols: NFC protocols supported by the device
  782. */
  783. struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
  784. u32 supported_protocols,
  785. int tx_headroom, int tx_tailroom)
  786. {
  787. struct nfc_dev *dev;
  788. if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
  789. !ops->deactivate_target || !ops->im_transceive)
  790. return NULL;
  791. if (!supported_protocols)
  792. return NULL;
  793. dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  794. if (!dev)
  795. return NULL;
  796. dev->ops = ops;
  797. dev->supported_protocols = supported_protocols;
  798. dev->tx_headroom = tx_headroom;
  799. dev->tx_tailroom = tx_tailroom;
  800. INIT_LIST_HEAD(&dev->secure_elements);
  801. nfc_genl_data_init(&dev->genl_data);
  802. dev->rf_mode = NFC_RF_NONE;
  803. /* first generation must not be 0 */
  804. dev->targets_generation = 1;
  805. if (ops->check_presence) {
  806. init_timer(&dev->check_pres_timer);
  807. dev->check_pres_timer.data = (unsigned long)dev;
  808. dev->check_pres_timer.function = nfc_check_pres_timeout;
  809. INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
  810. }
  811. return dev;
  812. }
  813. EXPORT_SYMBOL(nfc_allocate_device);
  814. /**
  815. * nfc_register_device - register a nfc device in the nfc subsystem
  816. *
  817. * @dev: The nfc device to register
  818. */
  819. int nfc_register_device(struct nfc_dev *dev)
  820. {
  821. int rc;
  822. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  823. dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
  824. if (dev->idx < 0)
  825. return dev->idx;
  826. dev->dev.class = &nfc_class;
  827. dev_set_name(&dev->dev, "nfc%d", dev->idx);
  828. device_initialize(&dev->dev);
  829. mutex_lock(&nfc_devlist_mutex);
  830. nfc_devlist_generation++;
  831. rc = device_add(&dev->dev);
  832. mutex_unlock(&nfc_devlist_mutex);
  833. if (rc < 0)
  834. return rc;
  835. rc = nfc_llcp_register_device(dev);
  836. if (rc)
  837. pr_err("Could not register llcp device\n");
  838. rc = nfc_genl_device_added(dev);
  839. if (rc)
  840. pr_debug("The userspace won't be notified that the device %s was added\n",
  841. dev_name(&dev->dev));
  842. dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
  843. RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
  844. if (dev->rfkill) {
  845. if (rfkill_register(dev->rfkill) < 0) {
  846. rfkill_destroy(dev->rfkill);
  847. dev->rfkill = NULL;
  848. }
  849. }
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(nfc_register_device);
  853. /**
  854. * nfc_unregister_device - unregister a nfc device in the nfc subsystem
  855. *
  856. * @dev: The nfc device to unregister
  857. */
  858. void nfc_unregister_device(struct nfc_dev *dev)
  859. {
  860. int rc, id;
  861. pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  862. id = dev->idx;
  863. if (dev->rfkill) {
  864. rfkill_unregister(dev->rfkill);
  865. rfkill_destroy(dev->rfkill);
  866. }
  867. if (dev->ops->check_presence) {
  868. device_lock(&dev->dev);
  869. dev->shutting_down = true;
  870. device_unlock(&dev->dev);
  871. del_timer_sync(&dev->check_pres_timer);
  872. cancel_work_sync(&dev->check_pres_work);
  873. }
  874. rc = nfc_genl_device_removed(dev);
  875. if (rc)
  876. pr_debug("The userspace won't be notified that the device %s "
  877. "was removed\n", dev_name(&dev->dev));
  878. nfc_llcp_unregister_device(dev);
  879. mutex_lock(&nfc_devlist_mutex);
  880. nfc_devlist_generation++;
  881. device_del(&dev->dev);
  882. mutex_unlock(&nfc_devlist_mutex);
  883. ida_simple_remove(&nfc_index_ida, id);
  884. }
  885. EXPORT_SYMBOL(nfc_unregister_device);
  886. static int __init nfc_init(void)
  887. {
  888. int rc;
  889. pr_info("NFC Core ver %s\n", VERSION);
  890. rc = class_register(&nfc_class);
  891. if (rc)
  892. return rc;
  893. rc = nfc_genl_init();
  894. if (rc)
  895. goto err_genl;
  896. /* the first generation must not be 0 */
  897. nfc_devlist_generation = 1;
  898. rc = rawsock_init();
  899. if (rc)
  900. goto err_rawsock;
  901. rc = nfc_llcp_init();
  902. if (rc)
  903. goto err_llcp_sock;
  904. rc = af_nfc_init();
  905. if (rc)
  906. goto err_af_nfc;
  907. return 0;
  908. err_af_nfc:
  909. nfc_llcp_exit();
  910. err_llcp_sock:
  911. rawsock_exit();
  912. err_rawsock:
  913. nfc_genl_exit();
  914. err_genl:
  915. class_unregister(&nfc_class);
  916. return rc;
  917. }
  918. static void __exit nfc_exit(void)
  919. {
  920. af_nfc_exit();
  921. nfc_llcp_exit();
  922. rawsock_exit();
  923. nfc_genl_exit();
  924. class_unregister(&nfc_class);
  925. }
  926. subsys_initcall(nfc_init);
  927. module_exit(nfc_exit);
  928. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  929. MODULE_DESCRIPTION("NFC Core ver " VERSION);
  930. MODULE_VERSION(VERSION);
  931. MODULE_LICENSE("GPL");
  932. MODULE_ALIAS_NETPROTO(PF_NFC);
  933. MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);