core.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*
  2. * The NFC Controller Interface is the communication protocol between an
  3. * NFC Controller (NFCC) and a Device Host (DH).
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2014 Marvell International Ltd.
  7. *
  8. * Written by Ilan Elias <ilane@ti.com>
  9. *
  10. * Acknowledgements:
  11. * This file is based on hci_core.c, which was written
  12. * by Maxim Krasnyansky.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2
  16. * as published by the Free Software Foundation
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/completion.h>
  33. #include <linux/export.h>
  34. #include <linux/sched.h>
  35. #include <linux/bitops.h>
  36. #include <linux/skbuff.h>
  37. #include "../nfc.h"
  38. #include <net/nfc/nci.h>
  39. #include <net/nfc/nci_core.h>
  40. #include <linux/nfc.h>
  41. struct core_conn_create_data {
  42. int length;
  43. struct nci_core_conn_create_cmd *cmd;
  44. };
  45. static void nci_cmd_work(struct work_struct *work);
  46. static void nci_rx_work(struct work_struct *work);
  47. static void nci_tx_work(struct work_struct *work);
  48. struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
  49. int conn_id)
  50. {
  51. struct nci_conn_info *conn_info;
  52. list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
  53. if (conn_info->conn_id == conn_id)
  54. return conn_info;
  55. }
  56. return NULL;
  57. }
  58. int nci_get_conn_info_by_dest_type_params(struct nci_dev *ndev, u8 dest_type,
  59. struct dest_spec_params *params)
  60. {
  61. struct nci_conn_info *conn_info;
  62. list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
  63. if (conn_info->dest_type == dest_type) {
  64. if (!params)
  65. return conn_info->conn_id;
  66. if (params->id == conn_info->dest_params->id &&
  67. params->protocol == conn_info->dest_params->protocol)
  68. return conn_info->conn_id;
  69. }
  70. }
  71. return -EINVAL;
  72. }
  73. EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params);
  74. /* ---- NCI requests ---- */
  75. void nci_req_complete(struct nci_dev *ndev, int result)
  76. {
  77. if (ndev->req_status == NCI_REQ_PEND) {
  78. ndev->req_result = result;
  79. ndev->req_status = NCI_REQ_DONE;
  80. complete(&ndev->req_completion);
  81. }
  82. }
  83. EXPORT_SYMBOL(nci_req_complete);
  84. static void nci_req_cancel(struct nci_dev *ndev, int err)
  85. {
  86. if (ndev->req_status == NCI_REQ_PEND) {
  87. ndev->req_result = err;
  88. ndev->req_status = NCI_REQ_CANCELED;
  89. complete(&ndev->req_completion);
  90. }
  91. }
  92. /* Execute request and wait for completion. */
  93. static int __nci_request(struct nci_dev *ndev,
  94. void (*req)(struct nci_dev *ndev, unsigned long opt),
  95. unsigned long opt, __u32 timeout)
  96. {
  97. int rc = 0;
  98. long completion_rc;
  99. ndev->req_status = NCI_REQ_PEND;
  100. reinit_completion(&ndev->req_completion);
  101. req(ndev, opt);
  102. completion_rc =
  103. wait_for_completion_interruptible_timeout(&ndev->req_completion,
  104. timeout);
  105. pr_debug("wait_for_completion return %ld\n", completion_rc);
  106. if (completion_rc > 0) {
  107. switch (ndev->req_status) {
  108. case NCI_REQ_DONE:
  109. rc = nci_to_errno(ndev->req_result);
  110. break;
  111. case NCI_REQ_CANCELED:
  112. rc = -ndev->req_result;
  113. break;
  114. default:
  115. rc = -ETIMEDOUT;
  116. break;
  117. }
  118. } else {
  119. pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
  120. completion_rc);
  121. rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
  122. }
  123. ndev->req_status = ndev->req_result = 0;
  124. return rc;
  125. }
  126. inline int nci_request(struct nci_dev *ndev,
  127. void (*req)(struct nci_dev *ndev,
  128. unsigned long opt),
  129. unsigned long opt, __u32 timeout)
  130. {
  131. int rc;
  132. if (!test_bit(NCI_UP, &ndev->flags))
  133. return -ENETDOWN;
  134. /* Serialize all requests */
  135. mutex_lock(&ndev->req_lock);
  136. rc = __nci_request(ndev, req, opt, timeout);
  137. mutex_unlock(&ndev->req_lock);
  138. return rc;
  139. }
  140. static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
  141. {
  142. struct nci_core_reset_cmd cmd;
  143. cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
  144. nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
  145. }
  146. static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
  147. {
  148. nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
  149. }
  150. static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
  151. {
  152. struct nci_rf_disc_map_cmd cmd;
  153. struct disc_map_config *cfg = cmd.mapping_configs;
  154. __u8 *num = &cmd.num_mapping_configs;
  155. int i;
  156. /* set rf mapping configurations */
  157. *num = 0;
  158. /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
  159. for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
  160. if (ndev->supported_rf_interfaces[i] ==
  161. NCI_RF_INTERFACE_ISO_DEP) {
  162. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  163. cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
  164. NCI_DISC_MAP_MODE_LISTEN;
  165. cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
  166. (*num)++;
  167. } else if (ndev->supported_rf_interfaces[i] ==
  168. NCI_RF_INTERFACE_NFC_DEP) {
  169. cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  170. cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
  171. NCI_DISC_MAP_MODE_LISTEN;
  172. cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
  173. (*num)++;
  174. }
  175. if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
  176. break;
  177. }
  178. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
  179. (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
  180. }
  181. struct nci_set_config_param {
  182. __u8 id;
  183. size_t len;
  184. __u8 *val;
  185. };
  186. static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
  187. {
  188. struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
  189. struct nci_core_set_config_cmd cmd;
  190. BUG_ON(param->len > NCI_MAX_PARAM_LEN);
  191. cmd.num_params = 1;
  192. cmd.param.id = param->id;
  193. cmd.param.len = param->len;
  194. memcpy(cmd.param.val, param->val, param->len);
  195. nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
  196. }
  197. struct nci_rf_discover_param {
  198. __u32 im_protocols;
  199. __u32 tm_protocols;
  200. };
  201. static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
  202. {
  203. struct nci_rf_discover_param *param =
  204. (struct nci_rf_discover_param *)opt;
  205. struct nci_rf_disc_cmd cmd;
  206. cmd.num_disc_configs = 0;
  207. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  208. (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
  209. param->im_protocols & NFC_PROTO_MIFARE_MASK ||
  210. param->im_protocols & NFC_PROTO_ISO14443_MASK ||
  211. param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  212. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  213. NCI_NFC_A_PASSIVE_POLL_MODE;
  214. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  215. cmd.num_disc_configs++;
  216. }
  217. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  218. (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
  219. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  220. NCI_NFC_B_PASSIVE_POLL_MODE;
  221. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  222. cmd.num_disc_configs++;
  223. }
  224. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  225. (param->im_protocols & NFC_PROTO_FELICA_MASK ||
  226. param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  227. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  228. NCI_NFC_F_PASSIVE_POLL_MODE;
  229. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  230. cmd.num_disc_configs++;
  231. }
  232. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
  233. (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
  234. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  235. NCI_NFC_V_PASSIVE_POLL_MODE;
  236. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  237. cmd.num_disc_configs++;
  238. }
  239. if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
  240. (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
  241. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  242. NCI_NFC_A_PASSIVE_LISTEN_MODE;
  243. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  244. cmd.num_disc_configs++;
  245. cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
  246. NCI_NFC_F_PASSIVE_LISTEN_MODE;
  247. cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
  248. cmd.num_disc_configs++;
  249. }
  250. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
  251. (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
  252. &cmd);
  253. }
  254. struct nci_rf_discover_select_param {
  255. __u8 rf_discovery_id;
  256. __u8 rf_protocol;
  257. };
  258. static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
  259. {
  260. struct nci_rf_discover_select_param *param =
  261. (struct nci_rf_discover_select_param *)opt;
  262. struct nci_rf_discover_select_cmd cmd;
  263. cmd.rf_discovery_id = param->rf_discovery_id;
  264. cmd.rf_protocol = param->rf_protocol;
  265. switch (cmd.rf_protocol) {
  266. case NCI_RF_PROTOCOL_ISO_DEP:
  267. cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
  268. break;
  269. case NCI_RF_PROTOCOL_NFC_DEP:
  270. cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
  271. break;
  272. default:
  273. cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
  274. break;
  275. }
  276. nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
  277. sizeof(struct nci_rf_discover_select_cmd), &cmd);
  278. }
  279. static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
  280. {
  281. struct nci_rf_deactivate_cmd cmd;
  282. cmd.type = opt;
  283. nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
  284. sizeof(struct nci_rf_deactivate_cmd), &cmd);
  285. }
  286. struct nci_cmd_param {
  287. __u16 opcode;
  288. size_t len;
  289. __u8 *payload;
  290. };
  291. static void nci_generic_req(struct nci_dev *ndev, unsigned long opt)
  292. {
  293. struct nci_cmd_param *param =
  294. (struct nci_cmd_param *)opt;
  295. nci_send_cmd(ndev, param->opcode, param->len, param->payload);
  296. }
  297. int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
  298. {
  299. struct nci_cmd_param param;
  300. param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
  301. param.len = len;
  302. param.payload = payload;
  303. return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
  304. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  305. }
  306. EXPORT_SYMBOL(nci_prop_cmd);
  307. int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, __u8 *payload)
  308. {
  309. struct nci_cmd_param param;
  310. param.opcode = opcode;
  311. param.len = len;
  312. param.payload = payload;
  313. return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
  314. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  315. }
  316. EXPORT_SYMBOL(nci_core_cmd);
  317. int nci_core_reset(struct nci_dev *ndev)
  318. {
  319. return __nci_request(ndev, nci_reset_req, 0,
  320. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  321. }
  322. EXPORT_SYMBOL(nci_core_reset);
  323. int nci_core_init(struct nci_dev *ndev)
  324. {
  325. return __nci_request(ndev, nci_init_req, 0,
  326. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  327. }
  328. EXPORT_SYMBOL(nci_core_init);
  329. struct nci_loopback_data {
  330. u8 conn_id;
  331. struct sk_buff *data;
  332. };
  333. static void nci_send_data_req(struct nci_dev *ndev, unsigned long opt)
  334. {
  335. struct nci_loopback_data *data = (struct nci_loopback_data *)opt;
  336. nci_send_data(ndev, data->conn_id, data->data);
  337. }
  338. static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
  339. {
  340. struct nci_dev *ndev = (struct nci_dev *)context;
  341. struct nci_conn_info *conn_info;
  342. conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
  343. if (!conn_info) {
  344. nci_req_complete(ndev, NCI_STATUS_REJECTED);
  345. return;
  346. }
  347. conn_info->rx_skb = skb;
  348. nci_req_complete(ndev, NCI_STATUS_OK);
  349. }
  350. int nci_nfcc_loopback(struct nci_dev *ndev, void *data, size_t data_len,
  351. struct sk_buff **resp)
  352. {
  353. int r;
  354. struct nci_loopback_data loopback_data;
  355. struct nci_conn_info *conn_info;
  356. struct sk_buff *skb;
  357. int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
  358. NCI_DESTINATION_NFCC_LOOPBACK, NULL);
  359. if (conn_id < 0) {
  360. r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
  361. 0, 0, NULL);
  362. if (r != NCI_STATUS_OK)
  363. return r;
  364. conn_id = nci_get_conn_info_by_dest_type_params(ndev,
  365. NCI_DESTINATION_NFCC_LOOPBACK,
  366. NULL);
  367. }
  368. conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
  369. if (!conn_info)
  370. return -EPROTO;
  371. /* store cb and context to be used on receiving data */
  372. conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
  373. conn_info->data_exchange_cb_context = ndev;
  374. skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
  375. if (!skb)
  376. return -ENOMEM;
  377. skb_reserve(skb, NCI_DATA_HDR_SIZE);
  378. skb_put_data(skb, data, data_len);
  379. loopback_data.conn_id = conn_id;
  380. loopback_data.data = skb;
  381. ndev->cur_conn_id = conn_id;
  382. r = nci_request(ndev, nci_send_data_req, (unsigned long)&loopback_data,
  383. msecs_to_jiffies(NCI_DATA_TIMEOUT));
  384. if (r == NCI_STATUS_OK && resp)
  385. *resp = conn_info->rx_skb;
  386. return r;
  387. }
  388. EXPORT_SYMBOL(nci_nfcc_loopback);
  389. static int nci_open_device(struct nci_dev *ndev)
  390. {
  391. int rc = 0;
  392. mutex_lock(&ndev->req_lock);
  393. if (test_bit(NCI_UP, &ndev->flags)) {
  394. rc = -EALREADY;
  395. goto done;
  396. }
  397. if (ndev->ops->open(ndev)) {
  398. rc = -EIO;
  399. goto done;
  400. }
  401. atomic_set(&ndev->cmd_cnt, 1);
  402. set_bit(NCI_INIT, &ndev->flags);
  403. if (ndev->ops->init)
  404. rc = ndev->ops->init(ndev);
  405. if (!rc) {
  406. rc = __nci_request(ndev, nci_reset_req, 0,
  407. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  408. }
  409. if (!rc && ndev->ops->setup) {
  410. rc = ndev->ops->setup(ndev);
  411. }
  412. if (!rc) {
  413. rc = __nci_request(ndev, nci_init_req, 0,
  414. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  415. }
  416. if (!rc && ndev->ops->post_setup)
  417. rc = ndev->ops->post_setup(ndev);
  418. if (!rc) {
  419. rc = __nci_request(ndev, nci_init_complete_req, 0,
  420. msecs_to_jiffies(NCI_INIT_TIMEOUT));
  421. }
  422. clear_bit(NCI_INIT, &ndev->flags);
  423. if (!rc) {
  424. set_bit(NCI_UP, &ndev->flags);
  425. nci_clear_target_list(ndev);
  426. atomic_set(&ndev->state, NCI_IDLE);
  427. } else {
  428. /* Init failed, cleanup */
  429. skb_queue_purge(&ndev->cmd_q);
  430. skb_queue_purge(&ndev->rx_q);
  431. skb_queue_purge(&ndev->tx_q);
  432. ndev->ops->close(ndev);
  433. ndev->flags = 0;
  434. }
  435. done:
  436. mutex_unlock(&ndev->req_lock);
  437. return rc;
  438. }
  439. static int nci_close_device(struct nci_dev *ndev)
  440. {
  441. nci_req_cancel(ndev, ENODEV);
  442. mutex_lock(&ndev->req_lock);
  443. if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
  444. del_timer_sync(&ndev->cmd_timer);
  445. del_timer_sync(&ndev->data_timer);
  446. mutex_unlock(&ndev->req_lock);
  447. return 0;
  448. }
  449. /* Drop RX and TX queues */
  450. skb_queue_purge(&ndev->rx_q);
  451. skb_queue_purge(&ndev->tx_q);
  452. /* Flush RX and TX wq */
  453. flush_workqueue(ndev->rx_wq);
  454. flush_workqueue(ndev->tx_wq);
  455. /* Reset device */
  456. skb_queue_purge(&ndev->cmd_q);
  457. atomic_set(&ndev->cmd_cnt, 1);
  458. set_bit(NCI_INIT, &ndev->flags);
  459. __nci_request(ndev, nci_reset_req, 0,
  460. msecs_to_jiffies(NCI_RESET_TIMEOUT));
  461. /* After this point our queues are empty
  462. * and no works are scheduled.
  463. */
  464. ndev->ops->close(ndev);
  465. clear_bit(NCI_INIT, &ndev->flags);
  466. del_timer_sync(&ndev->cmd_timer);
  467. /* Flush cmd wq */
  468. flush_workqueue(ndev->cmd_wq);
  469. /* Clear flags */
  470. ndev->flags = 0;
  471. mutex_unlock(&ndev->req_lock);
  472. return 0;
  473. }
  474. /* NCI command timer function */
  475. static void nci_cmd_timer(unsigned long arg)
  476. {
  477. struct nci_dev *ndev = (void *) arg;
  478. atomic_set(&ndev->cmd_cnt, 1);
  479. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  480. }
  481. /* NCI data exchange timer function */
  482. static void nci_data_timer(unsigned long arg)
  483. {
  484. struct nci_dev *ndev = (void *) arg;
  485. set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  486. queue_work(ndev->rx_wq, &ndev->rx_work);
  487. }
  488. static int nci_dev_up(struct nfc_dev *nfc_dev)
  489. {
  490. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  491. return nci_open_device(ndev);
  492. }
  493. static int nci_dev_down(struct nfc_dev *nfc_dev)
  494. {
  495. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  496. return nci_close_device(ndev);
  497. }
  498. int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
  499. {
  500. struct nci_set_config_param param;
  501. if (!val || !len)
  502. return 0;
  503. param.id = id;
  504. param.len = len;
  505. param.val = val;
  506. return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  507. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  508. }
  509. EXPORT_SYMBOL(nci_set_config);
  510. static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
  511. {
  512. struct nci_nfcee_discover_cmd cmd;
  513. __u8 action = opt;
  514. cmd.discovery_action = action;
  515. nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
  516. }
  517. int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
  518. {
  519. return __nci_request(ndev, nci_nfcee_discover_req, action,
  520. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  521. }
  522. EXPORT_SYMBOL(nci_nfcee_discover);
  523. static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
  524. {
  525. struct nci_nfcee_mode_set_cmd *cmd =
  526. (struct nci_nfcee_mode_set_cmd *)opt;
  527. nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
  528. sizeof(struct nci_nfcee_mode_set_cmd), cmd);
  529. }
  530. int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
  531. {
  532. struct nci_nfcee_mode_set_cmd cmd;
  533. cmd.nfcee_id = nfcee_id;
  534. cmd.nfcee_mode = nfcee_mode;
  535. return __nci_request(ndev, nci_nfcee_mode_set_req,
  536. (unsigned long)&cmd,
  537. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  538. }
  539. EXPORT_SYMBOL(nci_nfcee_mode_set);
  540. static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
  541. {
  542. struct core_conn_create_data *data =
  543. (struct core_conn_create_data *)opt;
  544. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
  545. }
  546. int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
  547. u8 number_destination_params,
  548. size_t params_len,
  549. struct core_conn_create_dest_spec_params *params)
  550. {
  551. int r;
  552. struct nci_core_conn_create_cmd *cmd;
  553. struct core_conn_create_data data;
  554. data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
  555. cmd = kzalloc(data.length, GFP_KERNEL);
  556. if (!cmd)
  557. return -ENOMEM;
  558. cmd->destination_type = destination_type;
  559. cmd->number_destination_params = number_destination_params;
  560. data.cmd = cmd;
  561. if (params) {
  562. memcpy(cmd->params, params, params_len);
  563. if (params->length > 0)
  564. memcpy(&ndev->cur_params,
  565. &params->value[DEST_SPEC_PARAMS_ID_INDEX],
  566. sizeof(struct dest_spec_params));
  567. else
  568. ndev->cur_params.id = 0;
  569. } else {
  570. ndev->cur_params.id = 0;
  571. }
  572. ndev->cur_dest_type = destination_type;
  573. r = __nci_request(ndev, nci_core_conn_create_req, (unsigned long)&data,
  574. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  575. kfree(cmd);
  576. return r;
  577. }
  578. EXPORT_SYMBOL(nci_core_conn_create);
  579. static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
  580. {
  581. __u8 conn_id = opt;
  582. nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
  583. }
  584. int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
  585. {
  586. ndev->cur_conn_id = conn_id;
  587. return __nci_request(ndev, nci_core_conn_close_req, conn_id,
  588. msecs_to_jiffies(NCI_CMD_TIMEOUT));
  589. }
  590. EXPORT_SYMBOL(nci_core_conn_close);
  591. static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
  592. {
  593. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  594. struct nci_set_config_param param;
  595. int rc;
  596. param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
  597. if ((param.val == NULL) || (param.len == 0))
  598. return 0;
  599. if (param.len > NFC_MAX_GT_LEN)
  600. return -EINVAL;
  601. param.id = NCI_PN_ATR_REQ_GEN_BYTES;
  602. rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  603. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  604. if (rc)
  605. return rc;
  606. param.id = NCI_LN_ATR_RES_GEN_BYTES;
  607. return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
  608. msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
  609. }
  610. static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
  611. {
  612. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  613. int rc;
  614. __u8 val;
  615. val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
  616. rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
  617. if (rc)
  618. return rc;
  619. val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
  620. rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
  621. if (rc)
  622. return rc;
  623. val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
  624. return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
  625. }
  626. static int nci_start_poll(struct nfc_dev *nfc_dev,
  627. __u32 im_protocols, __u32 tm_protocols)
  628. {
  629. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  630. struct nci_rf_discover_param param;
  631. int rc;
  632. if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
  633. (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
  634. pr_err("unable to start poll, since poll is already active\n");
  635. return -EBUSY;
  636. }
  637. if (ndev->target_active_prot) {
  638. pr_err("there is an active target\n");
  639. return -EBUSY;
  640. }
  641. if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
  642. (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
  643. pr_debug("target active or w4 select, implicitly deactivate\n");
  644. rc = nci_request(ndev, nci_rf_deactivate_req,
  645. NCI_DEACTIVATE_TYPE_IDLE_MODE,
  646. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  647. if (rc)
  648. return -EBUSY;
  649. }
  650. if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
  651. rc = nci_set_local_general_bytes(nfc_dev);
  652. if (rc) {
  653. pr_err("failed to set local general bytes\n");
  654. return rc;
  655. }
  656. }
  657. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  658. rc = nci_set_listen_parameters(nfc_dev);
  659. if (rc)
  660. pr_err("failed to set listen parameters\n");
  661. }
  662. param.im_protocols = im_protocols;
  663. param.tm_protocols = tm_protocols;
  664. rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
  665. msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
  666. if (!rc)
  667. ndev->poll_prots = im_protocols;
  668. return rc;
  669. }
  670. static void nci_stop_poll(struct nfc_dev *nfc_dev)
  671. {
  672. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  673. if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
  674. (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
  675. pr_err("unable to stop poll, since poll is not active\n");
  676. return;
  677. }
  678. nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
  679. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  680. }
  681. static int nci_activate_target(struct nfc_dev *nfc_dev,
  682. struct nfc_target *target, __u32 protocol)
  683. {
  684. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  685. struct nci_rf_discover_select_param param;
  686. struct nfc_target *nci_target = NULL;
  687. int i;
  688. int rc = 0;
  689. pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
  690. if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
  691. (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
  692. pr_err("there is no available target to activate\n");
  693. return -EINVAL;
  694. }
  695. if (ndev->target_active_prot) {
  696. pr_err("there is already an active target\n");
  697. return -EBUSY;
  698. }
  699. for (i = 0; i < ndev->n_targets; i++) {
  700. if (ndev->targets[i].idx == target->idx) {
  701. nci_target = &ndev->targets[i];
  702. break;
  703. }
  704. }
  705. if (!nci_target) {
  706. pr_err("unable to find the selected target\n");
  707. return -EINVAL;
  708. }
  709. if (!(nci_target->supported_protocols & (1 << protocol))) {
  710. pr_err("target does not support the requested protocol 0x%x\n",
  711. protocol);
  712. return -EINVAL;
  713. }
  714. if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
  715. param.rf_discovery_id = nci_target->logical_idx;
  716. if (protocol == NFC_PROTO_JEWEL)
  717. param.rf_protocol = NCI_RF_PROTOCOL_T1T;
  718. else if (protocol == NFC_PROTO_MIFARE)
  719. param.rf_protocol = NCI_RF_PROTOCOL_T2T;
  720. else if (protocol == NFC_PROTO_FELICA)
  721. param.rf_protocol = NCI_RF_PROTOCOL_T3T;
  722. else if (protocol == NFC_PROTO_ISO14443 ||
  723. protocol == NFC_PROTO_ISO14443_B)
  724. param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
  725. else
  726. param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
  727. rc = nci_request(ndev, nci_rf_discover_select_req,
  728. (unsigned long)&param,
  729. msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
  730. }
  731. if (!rc)
  732. ndev->target_active_prot = protocol;
  733. return rc;
  734. }
  735. static void nci_deactivate_target(struct nfc_dev *nfc_dev,
  736. struct nfc_target *target,
  737. __u8 mode)
  738. {
  739. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  740. u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
  741. pr_debug("entry\n");
  742. if (!ndev->target_active_prot) {
  743. pr_err("unable to deactivate target, no active target\n");
  744. return;
  745. }
  746. ndev->target_active_prot = 0;
  747. switch (mode) {
  748. case NFC_TARGET_MODE_SLEEP:
  749. nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
  750. break;
  751. }
  752. if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
  753. nci_request(ndev, nci_rf_deactivate_req, nci_mode,
  754. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  755. }
  756. }
  757. static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  758. __u8 comm_mode, __u8 *gb, size_t gb_len)
  759. {
  760. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  761. int rc;
  762. pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
  763. rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
  764. if (rc)
  765. return rc;
  766. rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
  767. ndev->remote_gb_len);
  768. if (!rc)
  769. rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
  770. NFC_RF_INITIATOR);
  771. return rc;
  772. }
  773. static int nci_dep_link_down(struct nfc_dev *nfc_dev)
  774. {
  775. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  776. int rc;
  777. pr_debug("entry\n");
  778. if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
  779. nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
  780. } else {
  781. if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
  782. atomic_read(&ndev->state) == NCI_DISCOVERY) {
  783. nci_request(ndev, nci_rf_deactivate_req, 0,
  784. msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
  785. }
  786. rc = nfc_tm_deactivated(nfc_dev);
  787. if (rc)
  788. pr_err("error when signaling tm deactivation\n");
  789. }
  790. return 0;
  791. }
  792. static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
  793. struct sk_buff *skb,
  794. data_exchange_cb_t cb, void *cb_context)
  795. {
  796. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  797. int rc;
  798. struct nci_conn_info *conn_info;
  799. conn_info = ndev->rf_conn_info;
  800. if (!conn_info)
  801. return -EPROTO;
  802. pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
  803. if (!ndev->target_active_prot) {
  804. pr_err("unable to exchange data, no active target\n");
  805. return -EINVAL;
  806. }
  807. if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  808. return -EBUSY;
  809. /* store cb and context to be used on receiving data */
  810. conn_info->data_exchange_cb = cb;
  811. conn_info->data_exchange_cb_context = cb_context;
  812. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  813. if (rc)
  814. clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
  815. return rc;
  816. }
  817. static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  818. {
  819. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  820. int rc;
  821. rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
  822. if (rc)
  823. pr_err("unable to send data\n");
  824. return rc;
  825. }
  826. static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  827. {
  828. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  829. if (ndev->ops->enable_se)
  830. return ndev->ops->enable_se(ndev, se_idx);
  831. return 0;
  832. }
  833. static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
  834. {
  835. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  836. if (ndev->ops->disable_se)
  837. return ndev->ops->disable_se(ndev, se_idx);
  838. return 0;
  839. }
  840. static int nci_discover_se(struct nfc_dev *nfc_dev)
  841. {
  842. int r;
  843. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  844. if (ndev->ops->discover_se) {
  845. r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
  846. if (r != NCI_STATUS_OK)
  847. return -EPROTO;
  848. return ndev->ops->discover_se(ndev);
  849. }
  850. return 0;
  851. }
  852. static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
  853. u8 *apdu, size_t apdu_length,
  854. se_io_cb_t cb, void *cb_context)
  855. {
  856. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  857. if (ndev->ops->se_io)
  858. return ndev->ops->se_io(ndev, se_idx, apdu,
  859. apdu_length, cb, cb_context);
  860. return 0;
  861. }
  862. static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
  863. {
  864. struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
  865. if (!ndev->ops->fw_download)
  866. return -ENOTSUPP;
  867. return ndev->ops->fw_download(ndev, firmware_name);
  868. }
  869. static struct nfc_ops nci_nfc_ops = {
  870. .dev_up = nci_dev_up,
  871. .dev_down = nci_dev_down,
  872. .start_poll = nci_start_poll,
  873. .stop_poll = nci_stop_poll,
  874. .dep_link_up = nci_dep_link_up,
  875. .dep_link_down = nci_dep_link_down,
  876. .activate_target = nci_activate_target,
  877. .deactivate_target = nci_deactivate_target,
  878. .im_transceive = nci_transceive,
  879. .tm_send = nci_tm_send,
  880. .enable_se = nci_enable_se,
  881. .disable_se = nci_disable_se,
  882. .discover_se = nci_discover_se,
  883. .se_io = nci_se_io,
  884. .fw_download = nci_fw_download,
  885. };
  886. /* ---- Interface to NCI drivers ---- */
  887. /**
  888. * nci_allocate_device - allocate a new nci device
  889. *
  890. * @ops: device operations
  891. * @supported_protocols: NFC protocols supported by the device
  892. */
  893. struct nci_dev *nci_allocate_device(struct nci_ops *ops,
  894. __u32 supported_protocols,
  895. int tx_headroom, int tx_tailroom)
  896. {
  897. struct nci_dev *ndev;
  898. pr_debug("supported_protocols 0x%x\n", supported_protocols);
  899. if (!ops->open || !ops->close || !ops->send)
  900. return NULL;
  901. if (!supported_protocols)
  902. return NULL;
  903. ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
  904. if (!ndev)
  905. return NULL;
  906. ndev->ops = ops;
  907. if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
  908. pr_err("Too many proprietary commands: %zd\n",
  909. ops->n_prop_ops);
  910. ops->prop_ops = NULL;
  911. ops->n_prop_ops = 0;
  912. }
  913. ndev->tx_headroom = tx_headroom;
  914. ndev->tx_tailroom = tx_tailroom;
  915. init_completion(&ndev->req_completion);
  916. ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
  917. supported_protocols,
  918. tx_headroom + NCI_DATA_HDR_SIZE,
  919. tx_tailroom);
  920. if (!ndev->nfc_dev)
  921. goto free_nci;
  922. ndev->hci_dev = nci_hci_allocate(ndev);
  923. if (!ndev->hci_dev)
  924. goto free_nfc;
  925. nfc_set_drvdata(ndev->nfc_dev, ndev);
  926. return ndev;
  927. free_nfc:
  928. nfc_free_device(ndev->nfc_dev);
  929. free_nci:
  930. kfree(ndev);
  931. return NULL;
  932. }
  933. EXPORT_SYMBOL(nci_allocate_device);
  934. /**
  935. * nci_free_device - deallocate nci device
  936. *
  937. * @ndev: The nci device to deallocate
  938. */
  939. void nci_free_device(struct nci_dev *ndev)
  940. {
  941. nfc_free_device(ndev->nfc_dev);
  942. kfree(ndev);
  943. }
  944. EXPORT_SYMBOL(nci_free_device);
  945. /**
  946. * nci_register_device - register a nci device in the nfc subsystem
  947. *
  948. * @dev: The nci device to register
  949. */
  950. int nci_register_device(struct nci_dev *ndev)
  951. {
  952. int rc;
  953. struct device *dev = &ndev->nfc_dev->dev;
  954. char name[32];
  955. ndev->flags = 0;
  956. INIT_WORK(&ndev->cmd_work, nci_cmd_work);
  957. snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
  958. ndev->cmd_wq = create_singlethread_workqueue(name);
  959. if (!ndev->cmd_wq) {
  960. rc = -ENOMEM;
  961. goto exit;
  962. }
  963. INIT_WORK(&ndev->rx_work, nci_rx_work);
  964. snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
  965. ndev->rx_wq = create_singlethread_workqueue(name);
  966. if (!ndev->rx_wq) {
  967. rc = -ENOMEM;
  968. goto destroy_cmd_wq_exit;
  969. }
  970. INIT_WORK(&ndev->tx_work, nci_tx_work);
  971. snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
  972. ndev->tx_wq = create_singlethread_workqueue(name);
  973. if (!ndev->tx_wq) {
  974. rc = -ENOMEM;
  975. goto destroy_rx_wq_exit;
  976. }
  977. skb_queue_head_init(&ndev->cmd_q);
  978. skb_queue_head_init(&ndev->rx_q);
  979. skb_queue_head_init(&ndev->tx_q);
  980. setup_timer(&ndev->cmd_timer, nci_cmd_timer,
  981. (unsigned long) ndev);
  982. setup_timer(&ndev->data_timer, nci_data_timer,
  983. (unsigned long) ndev);
  984. mutex_init(&ndev->req_lock);
  985. INIT_LIST_HEAD(&ndev->conn_info_list);
  986. rc = nfc_register_device(ndev->nfc_dev);
  987. if (rc)
  988. goto destroy_rx_wq_exit;
  989. goto exit;
  990. destroy_rx_wq_exit:
  991. destroy_workqueue(ndev->rx_wq);
  992. destroy_cmd_wq_exit:
  993. destroy_workqueue(ndev->cmd_wq);
  994. exit:
  995. return rc;
  996. }
  997. EXPORT_SYMBOL(nci_register_device);
  998. /**
  999. * nci_unregister_device - unregister a nci device in the nfc subsystem
  1000. *
  1001. * @dev: The nci device to unregister
  1002. */
  1003. void nci_unregister_device(struct nci_dev *ndev)
  1004. {
  1005. struct nci_conn_info *conn_info, *n;
  1006. nci_close_device(ndev);
  1007. destroy_workqueue(ndev->cmd_wq);
  1008. destroy_workqueue(ndev->rx_wq);
  1009. destroy_workqueue(ndev->tx_wq);
  1010. list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
  1011. list_del(&conn_info->list);
  1012. /* conn_info is allocated with devm_kzalloc */
  1013. }
  1014. nfc_unregister_device(ndev->nfc_dev);
  1015. }
  1016. EXPORT_SYMBOL(nci_unregister_device);
  1017. /**
  1018. * nci_recv_frame - receive frame from NCI drivers
  1019. *
  1020. * @ndev: The nci device
  1021. * @skb: The sk_buff to receive
  1022. */
  1023. int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  1024. {
  1025. pr_debug("len %d\n", skb->len);
  1026. if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
  1027. !test_bit(NCI_INIT, &ndev->flags))) {
  1028. kfree_skb(skb);
  1029. return -ENXIO;
  1030. }
  1031. /* Queue frame for rx worker thread */
  1032. skb_queue_tail(&ndev->rx_q, skb);
  1033. queue_work(ndev->rx_wq, &ndev->rx_work);
  1034. return 0;
  1035. }
  1036. EXPORT_SYMBOL(nci_recv_frame);
  1037. int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
  1038. {
  1039. pr_debug("len %d\n", skb->len);
  1040. if (!ndev) {
  1041. kfree_skb(skb);
  1042. return -ENODEV;
  1043. }
  1044. /* Get rid of skb owner, prior to sending to the driver. */
  1045. skb_orphan(skb);
  1046. /* Send copy to sniffer */
  1047. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  1048. RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
  1049. return ndev->ops->send(ndev, skb);
  1050. }
  1051. EXPORT_SYMBOL(nci_send_frame);
  1052. /* Send NCI command */
  1053. int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
  1054. {
  1055. struct nci_ctrl_hdr *hdr;
  1056. struct sk_buff *skb;
  1057. pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
  1058. skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
  1059. if (!skb) {
  1060. pr_err("no memory for command\n");
  1061. return -ENOMEM;
  1062. }
  1063. hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
  1064. hdr->gid = nci_opcode_gid(opcode);
  1065. hdr->oid = nci_opcode_oid(opcode);
  1066. hdr->plen = plen;
  1067. nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
  1068. nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
  1069. if (plen)
  1070. skb_put_data(skb, payload, plen);
  1071. skb_queue_tail(&ndev->cmd_q, skb);
  1072. queue_work(ndev->cmd_wq, &ndev->cmd_work);
  1073. return 0;
  1074. }
  1075. EXPORT_SYMBOL(nci_send_cmd);
  1076. /* Proprietary commands API */
  1077. static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
  1078. size_t n_ops,
  1079. __u16 opcode)
  1080. {
  1081. size_t i;
  1082. struct nci_driver_ops *op;
  1083. if (!ops || !n_ops)
  1084. return NULL;
  1085. for (i = 0; i < n_ops; i++) {
  1086. op = &ops[i];
  1087. if (op->opcode == opcode)
  1088. return op;
  1089. }
  1090. return NULL;
  1091. }
  1092. static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
  1093. struct sk_buff *skb, struct nci_driver_ops *ops,
  1094. size_t n_ops)
  1095. {
  1096. struct nci_driver_ops *op;
  1097. op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
  1098. if (!op || !op->rsp)
  1099. return -ENOTSUPP;
  1100. return op->rsp(ndev, skb);
  1101. }
  1102. static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
  1103. struct sk_buff *skb, struct nci_driver_ops *ops,
  1104. size_t n_ops)
  1105. {
  1106. struct nci_driver_ops *op;
  1107. op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
  1108. if (!op || !op->ntf)
  1109. return -ENOTSUPP;
  1110. return op->ntf(ndev, skb);
  1111. }
  1112. int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
  1113. struct sk_buff *skb)
  1114. {
  1115. return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
  1116. ndev->ops->n_prop_ops);
  1117. }
  1118. int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
  1119. struct sk_buff *skb)
  1120. {
  1121. return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
  1122. ndev->ops->n_prop_ops);
  1123. }
  1124. int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
  1125. struct sk_buff *skb)
  1126. {
  1127. return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
  1128. ndev->ops->n_core_ops);
  1129. }
  1130. int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
  1131. struct sk_buff *skb)
  1132. {
  1133. return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
  1134. ndev->ops->n_core_ops);
  1135. }
  1136. /* ---- NCI TX Data worker thread ---- */
  1137. static void nci_tx_work(struct work_struct *work)
  1138. {
  1139. struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
  1140. struct nci_conn_info *conn_info;
  1141. struct sk_buff *skb;
  1142. conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
  1143. if (!conn_info)
  1144. return;
  1145. pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
  1146. /* Send queued tx data */
  1147. while (atomic_read(&conn_info->credits_cnt)) {
  1148. skb = skb_dequeue(&ndev->tx_q);
  1149. if (!skb)
  1150. return;
  1151. /* Check if data flow control is used */
  1152. if (atomic_read(&conn_info->credits_cnt) !=
  1153. NCI_DATA_FLOW_CONTROL_NOT_USED)
  1154. atomic_dec(&conn_info->credits_cnt);
  1155. pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
  1156. nci_pbf(skb->data),
  1157. nci_conn_id(skb->data),
  1158. nci_plen(skb->data));
  1159. nci_send_frame(ndev, skb);
  1160. mod_timer(&ndev->data_timer,
  1161. jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
  1162. }
  1163. }
  1164. /* ----- NCI RX worker thread (data & control) ----- */
  1165. static void nci_rx_work(struct work_struct *work)
  1166. {
  1167. struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
  1168. struct sk_buff *skb;
  1169. while ((skb = skb_dequeue(&ndev->rx_q))) {
  1170. /* Send copy to sniffer */
  1171. nfc_send_to_raw_sock(ndev->nfc_dev, skb,
  1172. RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
  1173. /* Process frame */
  1174. switch (nci_mt(skb->data)) {
  1175. case NCI_MT_RSP_PKT:
  1176. nci_rsp_packet(ndev, skb);
  1177. break;
  1178. case NCI_MT_NTF_PKT:
  1179. nci_ntf_packet(ndev, skb);
  1180. break;
  1181. case NCI_MT_DATA_PKT:
  1182. nci_rx_data_packet(ndev, skb);
  1183. break;
  1184. default:
  1185. pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
  1186. kfree_skb(skb);
  1187. break;
  1188. }
  1189. }
  1190. /* check if a data exchange timout has occurred */
  1191. if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
  1192. /* complete the data exchange transaction, if exists */
  1193. if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
  1194. nci_data_exchange_complete(ndev, NULL,
  1195. ndev->cur_conn_id,
  1196. -ETIMEDOUT);
  1197. clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
  1198. }
  1199. }
  1200. /* ----- NCI TX CMD worker thread ----- */
  1201. static void nci_cmd_work(struct work_struct *work)
  1202. {
  1203. struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
  1204. struct sk_buff *skb;
  1205. pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
  1206. /* Send queued command */
  1207. if (atomic_read(&ndev->cmd_cnt)) {
  1208. skb = skb_dequeue(&ndev->cmd_q);
  1209. if (!skb)
  1210. return;
  1211. atomic_dec(&ndev->cmd_cnt);
  1212. pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
  1213. nci_pbf(skb->data),
  1214. nci_opcode_gid(nci_opcode(skb->data)),
  1215. nci_opcode_oid(nci_opcode(skb->data)),
  1216. nci_plen(skb->data));
  1217. nci_send_frame(ndev, skb);
  1218. mod_timer(&ndev->cmd_timer,
  1219. jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
  1220. }
  1221. }
  1222. MODULE_LICENSE("GPL");