core.c 24 KB

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