core.c 24 KB

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