netlink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
  6. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  24. #include <net/genetlink.h>
  25. #include <linux/nfc.h>
  26. #include <linux/slab.h>
  27. #include "nfc.h"
  28. static struct genl_multicast_group nfc_genl_event_mcgrp = {
  29. .name = NFC_GENL_MCAST_EVENT_NAME,
  30. };
  31. struct genl_family nfc_genl_family = {
  32. .id = GENL_ID_GENERATE,
  33. .hdrsize = 0,
  34. .name = NFC_GENL_NAME,
  35. .version = NFC_GENL_VERSION,
  36. .maxattr = NFC_ATTR_MAX,
  37. };
  38. static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
  39. [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
  40. [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
  41. .len = NFC_DEVICE_NAME_MAXSIZE },
  42. [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
  43. [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
  44. [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
  45. };
  46. static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
  47. struct netlink_callback *cb, int flags)
  48. {
  49. void *hdr;
  50. hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  51. &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
  52. if (!hdr)
  53. return -EMSGSIZE;
  54. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  55. NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx);
  56. NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS,
  57. target->supported_protocols);
  58. NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res);
  59. NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res);
  60. if (target->nfcid1_len > 0)
  61. NLA_PUT(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
  62. target->nfcid1);
  63. return genlmsg_end(msg, hdr);
  64. nla_put_failure:
  65. genlmsg_cancel(msg, hdr);
  66. return -EMSGSIZE;
  67. }
  68. static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
  69. {
  70. struct nfc_dev *dev;
  71. int rc;
  72. u32 idx;
  73. rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
  74. nfc_genl_family.attrbuf,
  75. nfc_genl_family.maxattr,
  76. nfc_genl_policy);
  77. if (rc < 0)
  78. return ERR_PTR(rc);
  79. if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
  80. return ERR_PTR(-EINVAL);
  81. idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
  82. dev = nfc_get_device(idx);
  83. if (!dev)
  84. return ERR_PTR(-ENODEV);
  85. return dev;
  86. }
  87. static int nfc_genl_dump_targets(struct sk_buff *skb,
  88. struct netlink_callback *cb)
  89. {
  90. int i = cb->args[0];
  91. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  92. int rc;
  93. if (!dev) {
  94. dev = __get_device_from_cb(cb);
  95. if (IS_ERR(dev))
  96. return PTR_ERR(dev);
  97. cb->args[1] = (long) dev;
  98. }
  99. spin_lock_bh(&dev->targets_lock);
  100. cb->seq = dev->targets_generation;
  101. while (i < dev->n_targets) {
  102. rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
  103. NLM_F_MULTI);
  104. if (rc < 0)
  105. break;
  106. i++;
  107. }
  108. spin_unlock_bh(&dev->targets_lock);
  109. cb->args[0] = i;
  110. return skb->len;
  111. }
  112. static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
  113. {
  114. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  115. if (dev)
  116. nfc_put_device(dev);
  117. return 0;
  118. }
  119. int nfc_genl_targets_found(struct nfc_dev *dev)
  120. {
  121. struct sk_buff *msg;
  122. void *hdr;
  123. dev->genl_data.poll_req_pid = 0;
  124. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  125. if (!msg)
  126. return -ENOMEM;
  127. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  128. NFC_EVENT_TARGETS_FOUND);
  129. if (!hdr)
  130. goto free_msg;
  131. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  132. genlmsg_end(msg, hdr);
  133. return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  134. nla_put_failure:
  135. genlmsg_cancel(msg, hdr);
  136. free_msg:
  137. nlmsg_free(msg);
  138. return -EMSGSIZE;
  139. }
  140. int nfc_genl_device_added(struct nfc_dev *dev)
  141. {
  142. struct sk_buff *msg;
  143. void *hdr;
  144. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  145. if (!msg)
  146. return -ENOMEM;
  147. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  148. NFC_EVENT_DEVICE_ADDED);
  149. if (!hdr)
  150. goto free_msg;
  151. NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
  152. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  153. NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
  154. genlmsg_end(msg, hdr);
  155. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  156. return 0;
  157. nla_put_failure:
  158. genlmsg_cancel(msg, hdr);
  159. free_msg:
  160. nlmsg_free(msg);
  161. return -EMSGSIZE;
  162. }
  163. int nfc_genl_device_removed(struct nfc_dev *dev)
  164. {
  165. struct sk_buff *msg;
  166. void *hdr;
  167. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  168. if (!msg)
  169. return -ENOMEM;
  170. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  171. NFC_EVENT_DEVICE_REMOVED);
  172. if (!hdr)
  173. goto free_msg;
  174. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  175. genlmsg_end(msg, hdr);
  176. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  177. return 0;
  178. nla_put_failure:
  179. genlmsg_cancel(msg, hdr);
  180. free_msg:
  181. nlmsg_free(msg);
  182. return -EMSGSIZE;
  183. }
  184. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  185. u32 pid, u32 seq,
  186. struct netlink_callback *cb,
  187. int flags)
  188. {
  189. void *hdr;
  190. hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
  191. NFC_CMD_GET_DEVICE);
  192. if (!hdr)
  193. return -EMSGSIZE;
  194. if (cb)
  195. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  196. NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
  197. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  198. NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
  199. return genlmsg_end(msg, hdr);
  200. nla_put_failure:
  201. genlmsg_cancel(msg, hdr);
  202. return -EMSGSIZE;
  203. }
  204. static int nfc_genl_dump_devices(struct sk_buff *skb,
  205. struct netlink_callback *cb)
  206. {
  207. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  208. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  209. bool first_call = false;
  210. if (!iter) {
  211. first_call = true;
  212. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  213. if (!iter)
  214. return -ENOMEM;
  215. cb->args[0] = (long) iter;
  216. }
  217. mutex_lock(&nfc_devlist_mutex);
  218. cb->seq = nfc_devlist_generation;
  219. if (first_call) {
  220. nfc_device_iter_init(iter);
  221. dev = nfc_device_iter_next(iter);
  222. }
  223. while (dev) {
  224. int rc;
  225. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
  226. cb->nlh->nlmsg_seq,
  227. cb, NLM_F_MULTI);
  228. if (rc < 0)
  229. break;
  230. dev = nfc_device_iter_next(iter);
  231. }
  232. mutex_unlock(&nfc_devlist_mutex);
  233. cb->args[1] = (long) dev;
  234. return skb->len;
  235. }
  236. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  237. {
  238. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  239. nfc_device_iter_exit(iter);
  240. kfree(iter);
  241. return 0;
  242. }
  243. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  244. u8 comm_mode, u8 rf_mode)
  245. {
  246. struct sk_buff *msg;
  247. void *hdr;
  248. pr_debug("DEP link is up\n");
  249. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  250. if (!msg)
  251. return -ENOMEM;
  252. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  253. NFC_CMD_DEP_LINK_UP);
  254. if (!hdr)
  255. goto free_msg;
  256. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  257. if (rf_mode == NFC_RF_INITIATOR)
  258. NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
  259. NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
  260. NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
  261. genlmsg_end(msg, hdr);
  262. dev->dep_link_up = true;
  263. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  264. return 0;
  265. nla_put_failure:
  266. genlmsg_cancel(msg, hdr);
  267. free_msg:
  268. nlmsg_free(msg);
  269. return -EMSGSIZE;
  270. }
  271. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  272. {
  273. struct sk_buff *msg;
  274. void *hdr;
  275. pr_debug("DEP link is down\n");
  276. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  277. if (!msg)
  278. return -ENOMEM;
  279. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  280. NFC_CMD_DEP_LINK_DOWN);
  281. if (!hdr)
  282. goto free_msg;
  283. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  284. genlmsg_end(msg, hdr);
  285. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  286. return 0;
  287. nla_put_failure:
  288. genlmsg_cancel(msg, hdr);
  289. free_msg:
  290. nlmsg_free(msg);
  291. return -EMSGSIZE;
  292. }
  293. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  294. {
  295. struct sk_buff *msg;
  296. struct nfc_dev *dev;
  297. u32 idx;
  298. int rc = -ENOBUFS;
  299. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  300. return -EINVAL;
  301. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  302. dev = nfc_get_device(idx);
  303. if (!dev)
  304. return -ENODEV;
  305. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  306. if (!msg) {
  307. rc = -ENOMEM;
  308. goto out_putdev;
  309. }
  310. rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
  311. NULL, 0);
  312. if (rc < 0)
  313. goto out_free;
  314. nfc_put_device(dev);
  315. return genlmsg_reply(msg, info);
  316. out_free:
  317. nlmsg_free(msg);
  318. out_putdev:
  319. nfc_put_device(dev);
  320. return rc;
  321. }
  322. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  323. {
  324. struct nfc_dev *dev;
  325. int rc;
  326. u32 idx;
  327. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  328. return -EINVAL;
  329. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  330. dev = nfc_get_device(idx);
  331. if (!dev)
  332. return -ENODEV;
  333. rc = nfc_dev_up(dev);
  334. nfc_put_device(dev);
  335. return rc;
  336. }
  337. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  338. {
  339. struct nfc_dev *dev;
  340. int rc;
  341. u32 idx;
  342. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  343. return -EINVAL;
  344. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  345. dev = nfc_get_device(idx);
  346. if (!dev)
  347. return -ENODEV;
  348. rc = nfc_dev_down(dev);
  349. nfc_put_device(dev);
  350. return rc;
  351. }
  352. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  353. {
  354. struct nfc_dev *dev;
  355. int rc;
  356. u32 idx;
  357. u32 protocols;
  358. pr_debug("Poll start\n");
  359. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  360. !info->attrs[NFC_ATTR_PROTOCOLS])
  361. return -EINVAL;
  362. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  363. protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  364. dev = nfc_get_device(idx);
  365. if (!dev)
  366. return -ENODEV;
  367. mutex_lock(&dev->genl_data.genl_data_mutex);
  368. rc = nfc_start_poll(dev, protocols);
  369. if (!rc)
  370. dev->genl_data.poll_req_pid = info->snd_pid;
  371. mutex_unlock(&dev->genl_data.genl_data_mutex);
  372. nfc_put_device(dev);
  373. return rc;
  374. }
  375. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  376. {
  377. struct nfc_dev *dev;
  378. int rc;
  379. u32 idx;
  380. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  381. return -EINVAL;
  382. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  383. dev = nfc_get_device(idx);
  384. if (!dev)
  385. return -ENODEV;
  386. mutex_lock(&dev->genl_data.genl_data_mutex);
  387. if (dev->genl_data.poll_req_pid != info->snd_pid) {
  388. rc = -EBUSY;
  389. goto out;
  390. }
  391. rc = nfc_stop_poll(dev);
  392. dev->genl_data.poll_req_pid = 0;
  393. out:
  394. mutex_unlock(&dev->genl_data.genl_data_mutex);
  395. nfc_put_device(dev);
  396. return rc;
  397. }
  398. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  399. {
  400. struct nfc_dev *dev;
  401. int rc, tgt_idx;
  402. u32 idx;
  403. u8 comm, rf;
  404. pr_debug("DEP link up\n");
  405. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  406. !info->attrs[NFC_ATTR_COMM_MODE] ||
  407. !info->attrs[NFC_ATTR_RF_MODE])
  408. return -EINVAL;
  409. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  410. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  411. tgt_idx = NFC_TARGET_IDX_ANY;
  412. else
  413. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  414. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  415. rf = nla_get_u8(info->attrs[NFC_ATTR_RF_MODE]);
  416. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  417. return -EINVAL;
  418. if (rf != NFC_RF_INITIATOR && comm != NFC_RF_TARGET)
  419. return -EINVAL;
  420. dev = nfc_get_device(idx);
  421. if (!dev)
  422. return -ENODEV;
  423. rc = nfc_dep_link_up(dev, tgt_idx, comm, rf);
  424. nfc_put_device(dev);
  425. return rc;
  426. }
  427. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  428. {
  429. struct nfc_dev *dev;
  430. int rc;
  431. u32 idx;
  432. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  433. return -EINVAL;
  434. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  435. dev = nfc_get_device(idx);
  436. if (!dev)
  437. return -ENODEV;
  438. rc = nfc_dep_link_down(dev);
  439. nfc_put_device(dev);
  440. return rc;
  441. }
  442. static struct genl_ops nfc_genl_ops[] = {
  443. {
  444. .cmd = NFC_CMD_GET_DEVICE,
  445. .doit = nfc_genl_get_device,
  446. .dumpit = nfc_genl_dump_devices,
  447. .done = nfc_genl_dump_devices_done,
  448. .policy = nfc_genl_policy,
  449. },
  450. {
  451. .cmd = NFC_CMD_DEV_UP,
  452. .doit = nfc_genl_dev_up,
  453. .policy = nfc_genl_policy,
  454. },
  455. {
  456. .cmd = NFC_CMD_DEV_DOWN,
  457. .doit = nfc_genl_dev_down,
  458. .policy = nfc_genl_policy,
  459. },
  460. {
  461. .cmd = NFC_CMD_START_POLL,
  462. .doit = nfc_genl_start_poll,
  463. .policy = nfc_genl_policy,
  464. },
  465. {
  466. .cmd = NFC_CMD_STOP_POLL,
  467. .doit = nfc_genl_stop_poll,
  468. .policy = nfc_genl_policy,
  469. },
  470. {
  471. .cmd = NFC_CMD_DEP_LINK_UP,
  472. .doit = nfc_genl_dep_link_up,
  473. .policy = nfc_genl_policy,
  474. },
  475. {
  476. .cmd = NFC_CMD_DEP_LINK_DOWN,
  477. .doit = nfc_genl_dep_link_down,
  478. .policy = nfc_genl_policy,
  479. },
  480. {
  481. .cmd = NFC_CMD_GET_TARGET,
  482. .dumpit = nfc_genl_dump_targets,
  483. .done = nfc_genl_dump_targets_done,
  484. .policy = nfc_genl_policy,
  485. },
  486. };
  487. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  488. unsigned long event, void *ptr)
  489. {
  490. struct netlink_notify *n = ptr;
  491. struct class_dev_iter iter;
  492. struct nfc_dev *dev;
  493. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  494. goto out;
  495. pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
  496. nfc_device_iter_init(&iter);
  497. dev = nfc_device_iter_next(&iter);
  498. while (dev) {
  499. if (dev->genl_data.poll_req_pid == n->pid) {
  500. nfc_stop_poll(dev);
  501. dev->genl_data.poll_req_pid = 0;
  502. }
  503. dev = nfc_device_iter_next(&iter);
  504. }
  505. nfc_device_iter_exit(&iter);
  506. out:
  507. return NOTIFY_DONE;
  508. }
  509. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  510. {
  511. genl_data->poll_req_pid = 0;
  512. mutex_init(&genl_data->genl_data_mutex);
  513. }
  514. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  515. {
  516. mutex_destroy(&genl_data->genl_data_mutex);
  517. }
  518. static struct notifier_block nl_notifier = {
  519. .notifier_call = nfc_genl_rcv_nl_event,
  520. };
  521. /**
  522. * nfc_genl_init() - Initialize netlink interface
  523. *
  524. * This initialization function registers the nfc netlink family.
  525. */
  526. int __init nfc_genl_init(void)
  527. {
  528. int rc;
  529. rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
  530. ARRAY_SIZE(nfc_genl_ops));
  531. if (rc)
  532. return rc;
  533. rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
  534. netlink_register_notifier(&nl_notifier);
  535. return rc;
  536. }
  537. /**
  538. * nfc_genl_exit() - Deinitialize netlink interface
  539. *
  540. * This exit function unregisters the nfc netlink family.
  541. */
  542. void nfc_genl_exit(void)
  543. {
  544. netlink_unregister_notifier(&nl_notifier);
  545. genl_unregister_family(&nfc_genl_family);
  546. }