core.c 24 KB

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