netlink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. return genlmsg_end(msg, hdr);
  61. nla_put_failure:
  62. genlmsg_cancel(msg, hdr);
  63. return -EMSGSIZE;
  64. }
  65. static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
  66. {
  67. struct nfc_dev *dev;
  68. int rc;
  69. u32 idx;
  70. rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
  71. nfc_genl_family.attrbuf,
  72. nfc_genl_family.maxattr,
  73. nfc_genl_policy);
  74. if (rc < 0)
  75. return ERR_PTR(rc);
  76. if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
  77. return ERR_PTR(-EINVAL);
  78. idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
  79. dev = nfc_get_device(idx);
  80. if (!dev)
  81. return ERR_PTR(-ENODEV);
  82. return dev;
  83. }
  84. static int nfc_genl_dump_targets(struct sk_buff *skb,
  85. struct netlink_callback *cb)
  86. {
  87. int i = cb->args[0];
  88. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  89. int rc;
  90. if (!dev) {
  91. dev = __get_device_from_cb(cb);
  92. if (IS_ERR(dev))
  93. return PTR_ERR(dev);
  94. cb->args[1] = (long) dev;
  95. }
  96. spin_lock_bh(&dev->targets_lock);
  97. cb->seq = dev->targets_generation;
  98. while (i < dev->n_targets) {
  99. rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
  100. NLM_F_MULTI);
  101. if (rc < 0)
  102. break;
  103. i++;
  104. }
  105. spin_unlock_bh(&dev->targets_lock);
  106. cb->args[0] = i;
  107. return skb->len;
  108. }
  109. static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
  110. {
  111. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  112. if (dev)
  113. nfc_put_device(dev);
  114. return 0;
  115. }
  116. int nfc_genl_targets_found(struct nfc_dev *dev)
  117. {
  118. struct sk_buff *msg;
  119. void *hdr;
  120. dev->genl_data.poll_req_pid = 0;
  121. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  122. if (!msg)
  123. return -ENOMEM;
  124. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  125. NFC_EVENT_TARGETS_FOUND);
  126. if (!hdr)
  127. goto free_msg;
  128. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  129. genlmsg_end(msg, hdr);
  130. return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  131. nla_put_failure:
  132. genlmsg_cancel(msg, hdr);
  133. free_msg:
  134. nlmsg_free(msg);
  135. return -EMSGSIZE;
  136. }
  137. int nfc_genl_device_added(struct nfc_dev *dev)
  138. {
  139. struct sk_buff *msg;
  140. void *hdr;
  141. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  142. if (!msg)
  143. return -ENOMEM;
  144. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  145. NFC_EVENT_DEVICE_ADDED);
  146. if (!hdr)
  147. goto free_msg;
  148. NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
  149. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  150. NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
  151. genlmsg_end(msg, hdr);
  152. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  153. return 0;
  154. nla_put_failure:
  155. genlmsg_cancel(msg, hdr);
  156. free_msg:
  157. nlmsg_free(msg);
  158. return -EMSGSIZE;
  159. }
  160. int nfc_genl_device_removed(struct nfc_dev *dev)
  161. {
  162. struct sk_buff *msg;
  163. void *hdr;
  164. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  165. if (!msg)
  166. return -ENOMEM;
  167. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  168. NFC_EVENT_DEVICE_REMOVED);
  169. if (!hdr)
  170. goto free_msg;
  171. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  172. genlmsg_end(msg, hdr);
  173. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  174. return 0;
  175. nla_put_failure:
  176. genlmsg_cancel(msg, hdr);
  177. free_msg:
  178. nlmsg_free(msg);
  179. return -EMSGSIZE;
  180. }
  181. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  182. u32 pid, u32 seq,
  183. struct netlink_callback *cb,
  184. int flags)
  185. {
  186. void *hdr;
  187. hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
  188. NFC_CMD_GET_DEVICE);
  189. if (!hdr)
  190. return -EMSGSIZE;
  191. if (cb)
  192. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  193. NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev));
  194. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  195. NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols);
  196. return genlmsg_end(msg, hdr);
  197. nla_put_failure:
  198. genlmsg_cancel(msg, hdr);
  199. return -EMSGSIZE;
  200. }
  201. static int nfc_genl_dump_devices(struct sk_buff *skb,
  202. struct netlink_callback *cb)
  203. {
  204. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  205. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  206. bool first_call = false;
  207. if (!iter) {
  208. first_call = true;
  209. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  210. if (!iter)
  211. return -ENOMEM;
  212. cb->args[0] = (long) iter;
  213. }
  214. mutex_lock(&nfc_devlist_mutex);
  215. cb->seq = nfc_devlist_generation;
  216. if (first_call) {
  217. nfc_device_iter_init(iter);
  218. dev = nfc_device_iter_next(iter);
  219. }
  220. while (dev) {
  221. int rc;
  222. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
  223. cb->nlh->nlmsg_seq,
  224. cb, NLM_F_MULTI);
  225. if (rc < 0)
  226. break;
  227. dev = nfc_device_iter_next(iter);
  228. }
  229. mutex_unlock(&nfc_devlist_mutex);
  230. cb->args[1] = (long) dev;
  231. return skb->len;
  232. }
  233. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  234. {
  235. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  236. nfc_device_iter_exit(iter);
  237. kfree(iter);
  238. return 0;
  239. }
  240. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  241. u8 comm_mode, u8 rf_mode)
  242. {
  243. struct sk_buff *msg;
  244. void *hdr;
  245. pr_debug("DEP link is up\n");
  246. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  247. if (!msg)
  248. return -ENOMEM;
  249. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  250. NFC_CMD_DEP_LINK_UP);
  251. if (!hdr)
  252. goto free_msg;
  253. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  254. if (rf_mode == NFC_RF_INITIATOR)
  255. NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
  256. NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
  257. NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
  258. genlmsg_end(msg, hdr);
  259. dev->dep_link_up = true;
  260. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  261. return 0;
  262. nla_put_failure:
  263. genlmsg_cancel(msg, hdr);
  264. free_msg:
  265. nlmsg_free(msg);
  266. return -EMSGSIZE;
  267. }
  268. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  269. {
  270. struct sk_buff *msg;
  271. void *hdr;
  272. pr_debug("DEP link is down\n");
  273. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  274. if (!msg)
  275. return -ENOMEM;
  276. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  277. NFC_CMD_DEP_LINK_DOWN);
  278. if (!hdr)
  279. goto free_msg;
  280. NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx);
  281. genlmsg_end(msg, hdr);
  282. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  283. return 0;
  284. nla_put_failure:
  285. genlmsg_cancel(msg, hdr);
  286. free_msg:
  287. nlmsg_free(msg);
  288. return -EMSGSIZE;
  289. }
  290. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  291. {
  292. struct sk_buff *msg;
  293. struct nfc_dev *dev;
  294. u32 idx;
  295. int rc = -ENOBUFS;
  296. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  297. return -EINVAL;
  298. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  299. dev = nfc_get_device(idx);
  300. if (!dev)
  301. return -ENODEV;
  302. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  303. if (!msg) {
  304. rc = -ENOMEM;
  305. goto out_putdev;
  306. }
  307. rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
  308. NULL, 0);
  309. if (rc < 0)
  310. goto out_free;
  311. nfc_put_device(dev);
  312. return genlmsg_reply(msg, info);
  313. out_free:
  314. nlmsg_free(msg);
  315. out_putdev:
  316. nfc_put_device(dev);
  317. return rc;
  318. }
  319. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  320. {
  321. struct nfc_dev *dev;
  322. int rc;
  323. u32 idx;
  324. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  325. return -EINVAL;
  326. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  327. dev = nfc_get_device(idx);
  328. if (!dev)
  329. return -ENODEV;
  330. rc = nfc_dev_up(dev);
  331. nfc_put_device(dev);
  332. return rc;
  333. }
  334. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  335. {
  336. struct nfc_dev *dev;
  337. int rc;
  338. u32 idx;
  339. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  340. return -EINVAL;
  341. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  342. dev = nfc_get_device(idx);
  343. if (!dev)
  344. return -ENODEV;
  345. rc = nfc_dev_down(dev);
  346. nfc_put_device(dev);
  347. return rc;
  348. }
  349. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  350. {
  351. struct nfc_dev *dev;
  352. int rc;
  353. u32 idx;
  354. u32 protocols;
  355. pr_debug("Poll start\n");
  356. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  357. !info->attrs[NFC_ATTR_PROTOCOLS])
  358. return -EINVAL;
  359. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  360. protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  361. dev = nfc_get_device(idx);
  362. if (!dev)
  363. return -ENODEV;
  364. mutex_lock(&dev->genl_data.genl_data_mutex);
  365. rc = nfc_start_poll(dev, protocols);
  366. if (!rc)
  367. dev->genl_data.poll_req_pid = info->snd_pid;
  368. mutex_unlock(&dev->genl_data.genl_data_mutex);
  369. nfc_put_device(dev);
  370. return rc;
  371. }
  372. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  373. {
  374. struct nfc_dev *dev;
  375. int rc;
  376. u32 idx;
  377. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  378. return -EINVAL;
  379. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  380. dev = nfc_get_device(idx);
  381. if (!dev)
  382. return -ENODEV;
  383. mutex_lock(&dev->genl_data.genl_data_mutex);
  384. if (dev->genl_data.poll_req_pid != info->snd_pid) {
  385. rc = -EBUSY;
  386. goto out;
  387. }
  388. rc = nfc_stop_poll(dev);
  389. dev->genl_data.poll_req_pid = 0;
  390. out:
  391. mutex_unlock(&dev->genl_data.genl_data_mutex);
  392. nfc_put_device(dev);
  393. return rc;
  394. }
  395. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  396. {
  397. struct nfc_dev *dev;
  398. int rc, tgt_idx;
  399. u32 idx;
  400. u8 comm, rf;
  401. pr_debug("DEP link up\n");
  402. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  403. !info->attrs[NFC_ATTR_COMM_MODE] ||
  404. !info->attrs[NFC_ATTR_RF_MODE])
  405. return -EINVAL;
  406. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  407. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  408. tgt_idx = NFC_TARGET_IDX_ANY;
  409. else
  410. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  411. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  412. rf = nla_get_u8(info->attrs[NFC_ATTR_RF_MODE]);
  413. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  414. return -EINVAL;
  415. if (rf != NFC_RF_INITIATOR && comm != NFC_RF_TARGET)
  416. return -EINVAL;
  417. dev = nfc_get_device(idx);
  418. if (!dev)
  419. return -ENODEV;
  420. rc = nfc_dep_link_up(dev, tgt_idx, comm, rf);
  421. nfc_put_device(dev);
  422. return rc;
  423. }
  424. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  425. {
  426. struct nfc_dev *dev;
  427. int rc;
  428. u32 idx;
  429. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  430. return -EINVAL;
  431. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  432. dev = nfc_get_device(idx);
  433. if (!dev)
  434. return -ENODEV;
  435. rc = nfc_dep_link_down(dev);
  436. nfc_put_device(dev);
  437. return rc;
  438. }
  439. static struct genl_ops nfc_genl_ops[] = {
  440. {
  441. .cmd = NFC_CMD_GET_DEVICE,
  442. .doit = nfc_genl_get_device,
  443. .dumpit = nfc_genl_dump_devices,
  444. .done = nfc_genl_dump_devices_done,
  445. .policy = nfc_genl_policy,
  446. },
  447. {
  448. .cmd = NFC_CMD_DEV_UP,
  449. .doit = nfc_genl_dev_up,
  450. .policy = nfc_genl_policy,
  451. },
  452. {
  453. .cmd = NFC_CMD_DEV_DOWN,
  454. .doit = nfc_genl_dev_down,
  455. .policy = nfc_genl_policy,
  456. },
  457. {
  458. .cmd = NFC_CMD_START_POLL,
  459. .doit = nfc_genl_start_poll,
  460. .policy = nfc_genl_policy,
  461. },
  462. {
  463. .cmd = NFC_CMD_STOP_POLL,
  464. .doit = nfc_genl_stop_poll,
  465. .policy = nfc_genl_policy,
  466. },
  467. {
  468. .cmd = NFC_CMD_DEP_LINK_UP,
  469. .doit = nfc_genl_dep_link_up,
  470. .policy = nfc_genl_policy,
  471. },
  472. {
  473. .cmd = NFC_CMD_DEP_LINK_DOWN,
  474. .doit = nfc_genl_dep_link_down,
  475. .policy = nfc_genl_policy,
  476. },
  477. {
  478. .cmd = NFC_CMD_GET_TARGET,
  479. .dumpit = nfc_genl_dump_targets,
  480. .done = nfc_genl_dump_targets_done,
  481. .policy = nfc_genl_policy,
  482. },
  483. };
  484. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  485. unsigned long event, void *ptr)
  486. {
  487. struct netlink_notify *n = ptr;
  488. struct class_dev_iter iter;
  489. struct nfc_dev *dev;
  490. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  491. goto out;
  492. pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
  493. nfc_device_iter_init(&iter);
  494. dev = nfc_device_iter_next(&iter);
  495. while (dev) {
  496. if (dev->genl_data.poll_req_pid == n->pid) {
  497. nfc_stop_poll(dev);
  498. dev->genl_data.poll_req_pid = 0;
  499. }
  500. dev = nfc_device_iter_next(&iter);
  501. }
  502. nfc_device_iter_exit(&iter);
  503. out:
  504. return NOTIFY_DONE;
  505. }
  506. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  507. {
  508. genl_data->poll_req_pid = 0;
  509. mutex_init(&genl_data->genl_data_mutex);
  510. }
  511. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  512. {
  513. mutex_destroy(&genl_data->genl_data_mutex);
  514. }
  515. static struct notifier_block nl_notifier = {
  516. .notifier_call = nfc_genl_rcv_nl_event,
  517. };
  518. /**
  519. * nfc_genl_init() - Initialize netlink interface
  520. *
  521. * This initialization function registers the nfc netlink family.
  522. */
  523. int __init nfc_genl_init(void)
  524. {
  525. int rc;
  526. rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
  527. ARRAY_SIZE(nfc_genl_ops));
  528. if (rc)
  529. return rc;
  530. rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
  531. netlink_register_notifier(&nl_notifier);
  532. return rc;
  533. }
  534. /**
  535. * nfc_genl_exit() - Deinitialize netlink interface
  536. *
  537. * This exit function unregisters the nfc netlink family.
  538. */
  539. void nfc_genl_exit(void)
  540. {
  541. netlink_unregister_notifier(&nl_notifier);
  542. genl_unregister_family(&nfc_genl_family);
  543. }