netlink.c 15 KB

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