core.c 25 KB

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