core.c 24 KB

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