pn533.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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. #include <linux/device.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/usb.h>
  28. #include <linux/nfc.h>
  29. #include <linux/netdevice.h>
  30. #include <net/nfc/nfc.h>
  31. #define VERSION "0.1"
  32. #define PN533_VENDOR_ID 0x4CC
  33. #define PN533_PRODUCT_ID 0x2533
  34. #define SCM_VENDOR_ID 0x4E6
  35. #define SCL3711_PRODUCT_ID 0x5591
  36. static const struct usb_device_id pn533_table[] = {
  37. { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID) },
  38. { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID) },
  39. { }
  40. };
  41. MODULE_DEVICE_TABLE(usb, pn533_table);
  42. /* frame definitions */
  43. #define PN533_FRAME_TAIL_SIZE 2
  44. #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
  45. PN533_FRAME_TAIL_SIZE)
  46. #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
  47. #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
  48. #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
  49. /* start of frame */
  50. #define PN533_SOF 0x00FF
  51. /* frame identifier: in/out/error */
  52. #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
  53. #define PN533_DIR_OUT 0xD4
  54. #define PN533_DIR_IN 0xD5
  55. /* PN533 Commands */
  56. #define PN533_FRAME_CMD(f) (f->data[1])
  57. #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
  58. #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
  59. #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
  60. #define PN533_CMD_RF_CONFIGURATION 0x32
  61. #define PN533_CMD_IN_DATA_EXCHANGE 0x40
  62. #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
  63. #define PN533_CMD_IN_ATR 0x50
  64. #define PN533_CMD_IN_RELEASE 0x52
  65. #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
  66. #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
  67. /* PN533 Return codes */
  68. #define PN533_CMD_RET_MASK 0x3F
  69. #define PN533_CMD_MI_MASK 0x40
  70. #define PN533_CMD_RET_SUCCESS 0x00
  71. struct pn533;
  72. typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
  73. u8 *params, int params_len);
  74. /* structs for pn533 commands */
  75. /* PN533_CMD_GET_FIRMWARE_VERSION */
  76. struct pn533_fw_version {
  77. u8 ic;
  78. u8 ver;
  79. u8 rev;
  80. u8 support;
  81. };
  82. /* PN533_CMD_RF_CONFIGURATION */
  83. #define PN533_CFGITEM_MAX_RETRIES 0x05
  84. #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
  85. #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
  86. struct pn533_config_max_retries {
  87. u8 mx_rty_atr;
  88. u8 mx_rty_psl;
  89. u8 mx_rty_passive_act;
  90. } __packed;
  91. /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
  92. /* felica commands opcode */
  93. #define PN533_FELICA_OPC_SENSF_REQ 0
  94. #define PN533_FELICA_OPC_SENSF_RES 1
  95. /* felica SENSF_REQ parameters */
  96. #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
  97. #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
  98. #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
  99. #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
  100. /* type B initiator_data values */
  101. #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
  102. #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
  103. #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
  104. union pn533_cmd_poll_initdata {
  105. struct {
  106. u8 afi;
  107. u8 polling_method;
  108. } __packed type_b;
  109. struct {
  110. u8 opcode;
  111. __be16 sc;
  112. u8 rc;
  113. u8 tsn;
  114. } __packed felica;
  115. };
  116. /* Poll modulations */
  117. enum {
  118. PN533_POLL_MOD_106KBPS_A,
  119. PN533_POLL_MOD_212KBPS_FELICA,
  120. PN533_POLL_MOD_424KBPS_FELICA,
  121. PN533_POLL_MOD_106KBPS_JEWEL,
  122. PN533_POLL_MOD_847KBPS_B,
  123. __PN533_POLL_MOD_AFTER_LAST,
  124. };
  125. #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
  126. struct pn533_poll_modulations {
  127. struct {
  128. u8 maxtg;
  129. u8 brty;
  130. union pn533_cmd_poll_initdata initiator_data;
  131. } __packed data;
  132. u8 len;
  133. };
  134. const struct pn533_poll_modulations poll_mod[] = {
  135. [PN533_POLL_MOD_106KBPS_A] = {
  136. .data = {
  137. .maxtg = 1,
  138. .brty = 0,
  139. },
  140. .len = 2,
  141. },
  142. [PN533_POLL_MOD_212KBPS_FELICA] = {
  143. .data = {
  144. .maxtg = 1,
  145. .brty = 1,
  146. .initiator_data.felica = {
  147. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  148. .sc = PN533_FELICA_SENSF_SC_ALL,
  149. .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
  150. .tsn = 0,
  151. },
  152. },
  153. .len = 7,
  154. },
  155. [PN533_POLL_MOD_424KBPS_FELICA] = {
  156. .data = {
  157. .maxtg = 1,
  158. .brty = 2,
  159. .initiator_data.felica = {
  160. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  161. .sc = PN533_FELICA_SENSF_SC_ALL,
  162. .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
  163. .tsn = 0,
  164. },
  165. },
  166. .len = 7,
  167. },
  168. [PN533_POLL_MOD_106KBPS_JEWEL] = {
  169. .data = {
  170. .maxtg = 1,
  171. .brty = 4,
  172. },
  173. .len = 2,
  174. },
  175. [PN533_POLL_MOD_847KBPS_B] = {
  176. .data = {
  177. .maxtg = 1,
  178. .brty = 8,
  179. .initiator_data.type_b = {
  180. .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
  181. .polling_method =
  182. PN533_TYPE_B_POLL_METHOD_TIMESLOT,
  183. },
  184. },
  185. .len = 3,
  186. },
  187. };
  188. /* PN533_CMD_IN_ATR */
  189. struct pn533_cmd_activate_param {
  190. u8 tg;
  191. u8 next;
  192. } __packed;
  193. struct pn533_cmd_activate_response {
  194. u8 status;
  195. u8 nfcid3t[10];
  196. u8 didt;
  197. u8 bst;
  198. u8 brt;
  199. u8 to;
  200. u8 ppt;
  201. /* optional */
  202. u8 gt[];
  203. } __packed;
  204. /* PN533_CMD_IN_JUMP_FOR_DEP */
  205. struct pn533_cmd_jump_dep {
  206. u8 active;
  207. u8 baud;
  208. u8 next;
  209. u8 gt[];
  210. } __packed;
  211. struct pn533_cmd_jump_dep_response {
  212. u8 status;
  213. u8 tg;
  214. u8 nfcid3t[10];
  215. u8 didt;
  216. u8 bst;
  217. u8 brt;
  218. u8 to;
  219. u8 ppt;
  220. /* optional */
  221. u8 gt[];
  222. } __packed;
  223. struct pn533 {
  224. struct usb_device *udev;
  225. struct usb_interface *interface;
  226. struct nfc_dev *nfc_dev;
  227. struct urb *out_urb;
  228. int out_maxlen;
  229. struct pn533_frame *out_frame;
  230. struct urb *in_urb;
  231. int in_maxlen;
  232. struct pn533_frame *in_frame;
  233. struct tasklet_struct tasklet;
  234. struct pn533_frame *tklt_in_frame;
  235. int tklt_in_error;
  236. pn533_cmd_complete_t cmd_complete;
  237. void *cmd_complete_arg;
  238. struct semaphore cmd_lock;
  239. u8 cmd;
  240. struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
  241. u8 poll_mod_count;
  242. u8 poll_mod_curr;
  243. u32 poll_protocols;
  244. u8 tgt_available_prots;
  245. u8 tgt_active_prot;
  246. };
  247. struct pn533_frame {
  248. u8 preamble;
  249. __be16 start_frame;
  250. u8 datalen;
  251. u8 datalen_checksum;
  252. u8 data[];
  253. } __packed;
  254. /* The rule: value + checksum = 0 */
  255. static inline u8 pn533_checksum(u8 value)
  256. {
  257. return ~value + 1;
  258. }
  259. /* The rule: sum(data elements) + checksum = 0 */
  260. static u8 pn533_data_checksum(u8 *data, int datalen)
  261. {
  262. u8 sum = 0;
  263. int i;
  264. for (i = 0; i < datalen; i++)
  265. sum += data[i];
  266. return pn533_checksum(sum);
  267. }
  268. /**
  269. * pn533_tx_frame_ack - create a ack frame
  270. * @frame: The frame to be set as ack
  271. *
  272. * Ack is different type of standard frame. As a standard frame, it has
  273. * preamble and start_frame. However the checksum of this frame must fail,
  274. * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
  275. * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
  276. * After datalen_checksum field, the postamble is placed.
  277. */
  278. static void pn533_tx_frame_ack(struct pn533_frame *frame)
  279. {
  280. frame->preamble = 0;
  281. frame->start_frame = cpu_to_be16(PN533_SOF);
  282. frame->datalen = 0;
  283. frame->datalen_checksum = 0xFF;
  284. /* data[0] is used as postamble */
  285. frame->data[0] = 0;
  286. }
  287. static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
  288. {
  289. frame->preamble = 0;
  290. frame->start_frame = cpu_to_be16(PN533_SOF);
  291. PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
  292. PN533_FRAME_CMD(frame) = cmd;
  293. frame->datalen = 2;
  294. }
  295. static void pn533_tx_frame_finish(struct pn533_frame *frame)
  296. {
  297. frame->datalen_checksum = pn533_checksum(frame->datalen);
  298. PN533_FRAME_CHECKSUM(frame) =
  299. pn533_data_checksum(frame->data, frame->datalen);
  300. PN533_FRAME_POSTAMBLE(frame) = 0;
  301. }
  302. static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
  303. {
  304. u8 checksum;
  305. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  306. return false;
  307. checksum = pn533_checksum(frame->datalen);
  308. if (checksum != frame->datalen_checksum)
  309. return false;
  310. checksum = pn533_data_checksum(frame->data, frame->datalen);
  311. if (checksum != PN533_FRAME_CHECKSUM(frame))
  312. return false;
  313. return true;
  314. }
  315. static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
  316. {
  317. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  318. return false;
  319. if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
  320. return false;
  321. return true;
  322. }
  323. static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
  324. {
  325. return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
  326. }
  327. static void pn533_tasklet_cmd_complete(unsigned long arg)
  328. {
  329. struct pn533 *dev = (struct pn533 *) arg;
  330. struct pn533_frame *in_frame = dev->tklt_in_frame;
  331. int rc;
  332. if (dev->tklt_in_error)
  333. rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
  334. dev->tklt_in_error);
  335. else
  336. rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
  337. PN533_FRAME_CMD_PARAMS_PTR(in_frame),
  338. PN533_FRAME_CMD_PARAMS_LEN(in_frame));
  339. if (rc != -EINPROGRESS)
  340. up(&dev->cmd_lock);
  341. }
  342. static void pn533_recv_response(struct urb *urb)
  343. {
  344. struct pn533 *dev = urb->context;
  345. struct pn533_frame *in_frame;
  346. dev->tklt_in_frame = NULL;
  347. switch (urb->status) {
  348. case 0:
  349. /* success */
  350. break;
  351. case -ECONNRESET:
  352. case -ENOENT:
  353. case -ESHUTDOWN:
  354. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  355. " status: %d", urb->status);
  356. dev->tklt_in_error = urb->status;
  357. goto sched_tasklet;
  358. default:
  359. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  360. " %d", urb->status);
  361. dev->tklt_in_error = urb->status;
  362. goto sched_tasklet;
  363. }
  364. in_frame = dev->in_urb->transfer_buffer;
  365. if (!pn533_rx_frame_is_valid(in_frame)) {
  366. nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
  367. dev->tklt_in_error = -EIO;
  368. goto sched_tasklet;
  369. }
  370. if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
  371. nfc_dev_err(&dev->interface->dev, "The received frame is not "
  372. "response to the last command");
  373. dev->tklt_in_error = -EIO;
  374. goto sched_tasklet;
  375. }
  376. nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
  377. dev->tklt_in_error = 0;
  378. dev->tklt_in_frame = in_frame;
  379. sched_tasklet:
  380. tasklet_schedule(&dev->tasklet);
  381. }
  382. static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
  383. {
  384. dev->in_urb->complete = pn533_recv_response;
  385. return usb_submit_urb(dev->in_urb, flags);
  386. }
  387. static void pn533_recv_ack(struct urb *urb)
  388. {
  389. struct pn533 *dev = urb->context;
  390. struct pn533_frame *in_frame;
  391. int rc;
  392. switch (urb->status) {
  393. case 0:
  394. /* success */
  395. break;
  396. case -ECONNRESET:
  397. case -ENOENT:
  398. case -ESHUTDOWN:
  399. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  400. " status: %d", urb->status);
  401. dev->tklt_in_error = urb->status;
  402. goto sched_tasklet;
  403. default:
  404. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  405. " %d", urb->status);
  406. dev->tklt_in_error = urb->status;
  407. goto sched_tasklet;
  408. }
  409. in_frame = dev->in_urb->transfer_buffer;
  410. if (!pn533_rx_frame_is_ack(in_frame)) {
  411. nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
  412. dev->tklt_in_error = -EIO;
  413. goto sched_tasklet;
  414. }
  415. nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
  416. rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
  417. if (rc) {
  418. nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
  419. " result %d", rc);
  420. dev->tklt_in_error = rc;
  421. goto sched_tasklet;
  422. }
  423. return;
  424. sched_tasklet:
  425. dev->tklt_in_frame = NULL;
  426. tasklet_schedule(&dev->tasklet);
  427. }
  428. static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
  429. {
  430. dev->in_urb->complete = pn533_recv_ack;
  431. return usb_submit_urb(dev->in_urb, flags);
  432. }
  433. static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
  434. {
  435. int rc;
  436. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  437. pn533_tx_frame_ack(dev->out_frame);
  438. dev->out_urb->transfer_buffer = dev->out_frame;
  439. dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
  440. rc = usb_submit_urb(dev->out_urb, flags);
  441. return rc;
  442. }
  443. static int __pn533_send_cmd_frame_async(struct pn533 *dev,
  444. struct pn533_frame *out_frame,
  445. struct pn533_frame *in_frame,
  446. int in_frame_len,
  447. pn533_cmd_complete_t cmd_complete,
  448. void *arg, gfp_t flags)
  449. {
  450. int rc;
  451. nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
  452. PN533_FRAME_CMD(out_frame));
  453. dev->cmd = PN533_FRAME_CMD(out_frame);
  454. dev->cmd_complete = cmd_complete;
  455. dev->cmd_complete_arg = arg;
  456. dev->out_urb->transfer_buffer = out_frame;
  457. dev->out_urb->transfer_buffer_length =
  458. PN533_FRAME_SIZE(out_frame);
  459. dev->in_urb->transfer_buffer = in_frame;
  460. dev->in_urb->transfer_buffer_length = in_frame_len;
  461. rc = usb_submit_urb(dev->out_urb, flags);
  462. if (rc)
  463. return rc;
  464. rc = pn533_submit_urb_for_ack(dev, flags);
  465. if (rc)
  466. goto error;
  467. return 0;
  468. error:
  469. usb_unlink_urb(dev->out_urb);
  470. return rc;
  471. }
  472. static int pn533_send_cmd_frame_async(struct pn533 *dev,
  473. struct pn533_frame *out_frame,
  474. struct pn533_frame *in_frame,
  475. int in_frame_len,
  476. pn533_cmd_complete_t cmd_complete,
  477. void *arg, gfp_t flags)
  478. {
  479. int rc;
  480. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  481. if (down_trylock(&dev->cmd_lock))
  482. return -EBUSY;
  483. rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
  484. in_frame_len, cmd_complete, arg, flags);
  485. if (rc)
  486. goto error;
  487. return 0;
  488. error:
  489. up(&dev->cmd_lock);
  490. return rc;
  491. }
  492. struct pn533_sync_cmd_response {
  493. int rc;
  494. struct completion done;
  495. };
  496. static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
  497. u8 *params, int params_len)
  498. {
  499. struct pn533_sync_cmd_response *arg = _arg;
  500. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  501. arg->rc = 0;
  502. if (params_len < 0) /* error */
  503. arg->rc = params_len;
  504. complete(&arg->done);
  505. return 0;
  506. }
  507. static int pn533_send_cmd_frame_sync(struct pn533 *dev,
  508. struct pn533_frame *out_frame,
  509. struct pn533_frame *in_frame,
  510. int in_frame_len)
  511. {
  512. int rc;
  513. struct pn533_sync_cmd_response arg;
  514. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  515. init_completion(&arg.done);
  516. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
  517. pn533_sync_cmd_complete, &arg, GFP_KERNEL);
  518. if (rc)
  519. return rc;
  520. wait_for_completion(&arg.done);
  521. return arg.rc;
  522. }
  523. static void pn533_send_complete(struct urb *urb)
  524. {
  525. struct pn533 *dev = urb->context;
  526. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  527. switch (urb->status) {
  528. case 0:
  529. /* success */
  530. break;
  531. case -ECONNRESET:
  532. case -ENOENT:
  533. case -ESHUTDOWN:
  534. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  535. " status: %d", urb->status);
  536. break;
  537. default:
  538. nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
  539. " %d", urb->status);
  540. }
  541. }
  542. struct pn533_target_type_a {
  543. __be16 sens_res;
  544. u8 sel_res;
  545. u8 nfcid_len;
  546. u8 nfcid_data[];
  547. } __packed;
  548. #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
  549. #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
  550. #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
  551. #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
  552. #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
  553. #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
  554. #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
  555. #define PN533_TYPE_A_SEL_PROT_MIFARE 0
  556. #define PN533_TYPE_A_SEL_PROT_ISO14443 1
  557. #define PN533_TYPE_A_SEL_PROT_DEP 2
  558. #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
  559. static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
  560. int target_data_len)
  561. {
  562. u8 ssd;
  563. u8 platconf;
  564. if (target_data_len < sizeof(struct pn533_target_type_a))
  565. return false;
  566. /* The lenght check of nfcid[] and ats[] are not being performed because
  567. the values are not being used */
  568. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  569. ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
  570. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
  571. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  572. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  573. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  574. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  575. return false;
  576. /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
  577. if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
  578. return false;
  579. return true;
  580. }
  581. static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
  582. int tgt_data_len)
  583. {
  584. struct pn533_target_type_a *tgt_type_a;
  585. tgt_type_a = (struct pn533_target_type_a *) tgt_data;
  586. if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
  587. return -EPROTO;
  588. switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
  589. case PN533_TYPE_A_SEL_PROT_MIFARE:
  590. nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
  591. break;
  592. case PN533_TYPE_A_SEL_PROT_ISO14443:
  593. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  594. break;
  595. case PN533_TYPE_A_SEL_PROT_DEP:
  596. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  597. break;
  598. case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
  599. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
  600. NFC_PROTO_NFC_DEP_MASK;
  601. break;
  602. }
  603. nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
  604. nfc_tgt->sel_res = tgt_type_a->sel_res;
  605. return 0;
  606. }
  607. struct pn533_target_felica {
  608. u8 pol_res;
  609. u8 opcode;
  610. u8 nfcid2[8];
  611. u8 pad[8];
  612. /* optional */
  613. u8 syst_code[];
  614. } __packed;
  615. #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
  616. #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
  617. static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
  618. int target_data_len)
  619. {
  620. if (target_data_len < sizeof(struct pn533_target_felica))
  621. return false;
  622. if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
  623. return false;
  624. return true;
  625. }
  626. static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
  627. int tgt_data_len)
  628. {
  629. struct pn533_target_felica *tgt_felica;
  630. tgt_felica = (struct pn533_target_felica *) tgt_data;
  631. if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
  632. return -EPROTO;
  633. if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
  634. tgt_felica->nfcid2[1] ==
  635. PN533_FELICA_SENSF_NFCID2_DEP_B2)
  636. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  637. else
  638. nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
  639. return 0;
  640. }
  641. struct pn533_target_jewel {
  642. __be16 sens_res;
  643. u8 jewelid[4];
  644. } __packed;
  645. static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
  646. int target_data_len)
  647. {
  648. u8 ssd;
  649. u8 platconf;
  650. if (target_data_len < sizeof(struct pn533_target_jewel))
  651. return false;
  652. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  653. ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
  654. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
  655. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  656. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  657. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  658. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  659. return false;
  660. return true;
  661. }
  662. static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
  663. int tgt_data_len)
  664. {
  665. struct pn533_target_jewel *tgt_jewel;
  666. tgt_jewel = (struct pn533_target_jewel *) tgt_data;
  667. if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
  668. return -EPROTO;
  669. nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
  670. nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
  671. return 0;
  672. }
  673. struct pn533_type_b_prot_info {
  674. u8 bitrate;
  675. u8 fsci_type;
  676. u8 fwi_adc_fo;
  677. } __packed;
  678. #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
  679. #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
  680. #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
  681. struct pn533_type_b_sens_res {
  682. u8 opcode;
  683. u8 nfcid[4];
  684. u8 appdata[4];
  685. struct pn533_type_b_prot_info prot_info;
  686. } __packed;
  687. #define PN533_TYPE_B_OPC_SENSB_RES 0x50
  688. struct pn533_target_type_b {
  689. struct pn533_type_b_sens_res sensb_res;
  690. u8 attrib_res_len;
  691. u8 attrib_res[];
  692. } __packed;
  693. static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
  694. int target_data_len)
  695. {
  696. if (target_data_len < sizeof(struct pn533_target_type_b))
  697. return false;
  698. if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
  699. return false;
  700. if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
  701. PN533_TYPE_B_PROT_TYPE_RFU_MASK)
  702. return false;
  703. return true;
  704. }
  705. static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
  706. int tgt_data_len)
  707. {
  708. struct pn533_target_type_b *tgt_type_b;
  709. tgt_type_b = (struct pn533_target_type_b *) tgt_data;
  710. if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
  711. return -EPROTO;
  712. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  713. return 0;
  714. }
  715. struct pn533_poll_response {
  716. u8 nbtg;
  717. u8 tg;
  718. u8 target_data[];
  719. } __packed;
  720. static int pn533_target_found(struct pn533 *dev,
  721. struct pn533_poll_response *resp, int resp_len)
  722. {
  723. int target_data_len;
  724. struct nfc_target nfc_tgt;
  725. int rc;
  726. nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
  727. dev->poll_mod_curr);
  728. if (resp->tg != 1)
  729. return -EPROTO;
  730. target_data_len = resp_len - sizeof(struct pn533_poll_response);
  731. switch (dev->poll_mod_curr) {
  732. case PN533_POLL_MOD_106KBPS_A:
  733. rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
  734. target_data_len);
  735. break;
  736. case PN533_POLL_MOD_212KBPS_FELICA:
  737. case PN533_POLL_MOD_424KBPS_FELICA:
  738. rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
  739. target_data_len);
  740. break;
  741. case PN533_POLL_MOD_106KBPS_JEWEL:
  742. rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
  743. target_data_len);
  744. break;
  745. case PN533_POLL_MOD_847KBPS_B:
  746. rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
  747. target_data_len);
  748. break;
  749. default:
  750. nfc_dev_err(&dev->interface->dev, "Unknown current poll"
  751. " modulation");
  752. return -EPROTO;
  753. }
  754. if (rc)
  755. return rc;
  756. if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
  757. nfc_dev_dbg(&dev->interface->dev, "The target found does not"
  758. " have the desired protocol");
  759. return -EAGAIN;
  760. }
  761. nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
  762. "0x%x", nfc_tgt.supported_protocols);
  763. dev->tgt_available_prots = nfc_tgt.supported_protocols;
  764. nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
  765. return 0;
  766. }
  767. static void pn533_poll_reset_mod_list(struct pn533 *dev)
  768. {
  769. dev->poll_mod_count = 0;
  770. }
  771. static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
  772. {
  773. dev->poll_mod_active[dev->poll_mod_count] =
  774. (struct pn533_poll_modulations *) &poll_mod[mod_index];
  775. dev->poll_mod_count++;
  776. }
  777. static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
  778. {
  779. pn533_poll_reset_mod_list(dev);
  780. if (protocols & NFC_PROTO_MIFARE_MASK
  781. || protocols & NFC_PROTO_ISO14443_MASK
  782. || protocols & NFC_PROTO_NFC_DEP_MASK)
  783. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
  784. if (protocols & NFC_PROTO_FELICA_MASK
  785. || protocols & NFC_PROTO_NFC_DEP_MASK) {
  786. pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
  787. pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
  788. }
  789. if (protocols & NFC_PROTO_JEWEL_MASK)
  790. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
  791. if (protocols & NFC_PROTO_ISO14443_MASK)
  792. pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
  793. }
  794. static void pn533_start_poll_frame(struct pn533_frame *frame,
  795. struct pn533_poll_modulations *mod)
  796. {
  797. pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
  798. memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
  799. frame->datalen += mod->len;
  800. pn533_tx_frame_finish(frame);
  801. }
  802. static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
  803. u8 *params, int params_len)
  804. {
  805. struct pn533_poll_response *resp;
  806. struct pn533_poll_modulations *next_mod;
  807. int rc;
  808. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  809. if (params_len == -ENOENT) {
  810. nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
  811. " stopped");
  812. goto stop_poll;
  813. }
  814. if (params_len < 0) {
  815. nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
  816. params_len);
  817. goto stop_poll;
  818. }
  819. resp = (struct pn533_poll_response *) params;
  820. if (resp->nbtg) {
  821. rc = pn533_target_found(dev, resp, params_len);
  822. /* We must stop the poll after a valid target found */
  823. if (rc == 0)
  824. goto stop_poll;
  825. if (rc != -EAGAIN)
  826. nfc_dev_err(&dev->interface->dev, "The target found is"
  827. " not valid - continuing to poll");
  828. }
  829. dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
  830. next_mod = dev->poll_mod_active[dev->poll_mod_curr];
  831. nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
  832. dev->poll_mod_curr);
  833. pn533_start_poll_frame(dev->out_frame, next_mod);
  834. /* Don't need to down the semaphore again */
  835. rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  836. dev->in_maxlen, pn533_start_poll_complete,
  837. NULL, GFP_ATOMIC);
  838. if (rc == -EPERM) {
  839. nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
  840. " because poll has been stopped");
  841. goto stop_poll;
  842. }
  843. if (rc) {
  844. nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
  845. " next modulation", rc);
  846. goto stop_poll;
  847. }
  848. /* Inform caller function to do not up the semaphore */
  849. return -EINPROGRESS;
  850. stop_poll:
  851. pn533_poll_reset_mod_list(dev);
  852. dev->poll_protocols = 0;
  853. return 0;
  854. }
  855. static int pn533_start_poll(struct nfc_dev *nfc_dev, u32 protocols)
  856. {
  857. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  858. struct pn533_poll_modulations *start_mod;
  859. int rc;
  860. nfc_dev_dbg(&dev->interface->dev, "%s - protocols=0x%x", __func__,
  861. protocols);
  862. if (dev->poll_mod_count) {
  863. nfc_dev_err(&dev->interface->dev, "Polling operation already"
  864. " active");
  865. return -EBUSY;
  866. }
  867. if (dev->tgt_active_prot) {
  868. nfc_dev_err(&dev->interface->dev, "Cannot poll with a target"
  869. " already activated");
  870. return -EBUSY;
  871. }
  872. pn533_poll_create_mod_list(dev, protocols);
  873. if (!dev->poll_mod_count) {
  874. nfc_dev_err(&dev->interface->dev, "No valid protocols"
  875. " specified");
  876. rc = -EINVAL;
  877. goto error;
  878. }
  879. nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
  880. dev->poll_mod_count);
  881. dev->poll_mod_curr = 0;
  882. start_mod = dev->poll_mod_active[dev->poll_mod_curr];
  883. pn533_start_poll_frame(dev->out_frame, start_mod);
  884. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  885. dev->in_maxlen, pn533_start_poll_complete,
  886. NULL, GFP_KERNEL);
  887. if (rc) {
  888. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  889. " start poll", rc);
  890. goto error;
  891. }
  892. dev->poll_protocols = protocols;
  893. return 0;
  894. error:
  895. pn533_poll_reset_mod_list(dev);
  896. return rc;
  897. }
  898. static void pn533_stop_poll(struct nfc_dev *nfc_dev)
  899. {
  900. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  901. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  902. if (!dev->poll_mod_count) {
  903. nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
  904. " running");
  905. return;
  906. }
  907. /* An ack will cancel the last issued command (poll) */
  908. pn533_send_ack(dev, GFP_KERNEL);
  909. /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
  910. usb_kill_urb(dev->in_urb);
  911. }
  912. static int pn533_activate_target_nfcdep(struct pn533 *dev)
  913. {
  914. struct pn533_cmd_activate_param param;
  915. struct pn533_cmd_activate_response *resp;
  916. u16 gt_len;
  917. int rc;
  918. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  919. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
  920. param.tg = 1;
  921. param.next = 0;
  922. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
  923. sizeof(struct pn533_cmd_activate_param));
  924. dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
  925. pn533_tx_frame_finish(dev->out_frame);
  926. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  927. dev->in_maxlen);
  928. if (rc)
  929. return rc;
  930. resp = (struct pn533_cmd_activate_response *)
  931. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  932. rc = resp->status & PN533_CMD_RET_MASK;
  933. if (rc != PN533_CMD_RET_SUCCESS)
  934. return -EIO;
  935. /* ATR_RES general bytes are located at offset 16 */
  936. gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
  937. rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
  938. return rc;
  939. }
  940. static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
  941. u32 protocol)
  942. {
  943. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  944. int rc;
  945. nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
  946. protocol);
  947. if (dev->poll_mod_count) {
  948. nfc_dev_err(&dev->interface->dev, "Cannot activate while"
  949. " polling");
  950. return -EBUSY;
  951. }
  952. if (dev->tgt_active_prot) {
  953. nfc_dev_err(&dev->interface->dev, "There is already an active"
  954. " target");
  955. return -EBUSY;
  956. }
  957. if (!dev->tgt_available_prots) {
  958. nfc_dev_err(&dev->interface->dev, "There is no available target"
  959. " to activate");
  960. return -EINVAL;
  961. }
  962. if (!(dev->tgt_available_prots & (1 << protocol))) {
  963. nfc_dev_err(&dev->interface->dev, "The target does not support"
  964. " the requested protocol %u", protocol);
  965. return -EINVAL;
  966. }
  967. if (protocol == NFC_PROTO_NFC_DEP) {
  968. rc = pn533_activate_target_nfcdep(dev);
  969. if (rc) {
  970. nfc_dev_err(&dev->interface->dev, "Error %d when"
  971. " activating target with"
  972. " NFC_DEP protocol", rc);
  973. return rc;
  974. }
  975. }
  976. dev->tgt_active_prot = protocol;
  977. dev->tgt_available_prots = 0;
  978. return 0;
  979. }
  980. static void pn533_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
  981. {
  982. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  983. u8 tg;
  984. u8 status;
  985. int rc;
  986. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  987. if (!dev->tgt_active_prot) {
  988. nfc_dev_err(&dev->interface->dev, "There is no active target");
  989. return;
  990. }
  991. dev->tgt_active_prot = 0;
  992. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
  993. tg = 1;
  994. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
  995. dev->out_frame->datalen += sizeof(u8);
  996. pn533_tx_frame_finish(dev->out_frame);
  997. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  998. dev->in_maxlen);
  999. if (rc) {
  1000. nfc_dev_err(&dev->interface->dev, "Error when sending release"
  1001. " command to the controller");
  1002. return;
  1003. }
  1004. status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
  1005. rc = status & PN533_CMD_RET_MASK;
  1006. if (rc != PN533_CMD_RET_SUCCESS)
  1007. nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
  1008. " the target", rc);
  1009. return;
  1010. }
  1011. static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
  1012. u8 *params, int params_len)
  1013. {
  1014. struct pn533_cmd_jump_dep *cmd;
  1015. struct pn533_cmd_jump_dep_response *resp;
  1016. struct nfc_target nfc_target;
  1017. u8 target_gt_len;
  1018. int rc;
  1019. if (params_len == -ENOENT) {
  1020. nfc_dev_dbg(&dev->interface->dev, "");
  1021. return 0;
  1022. }
  1023. if (params_len < 0) {
  1024. nfc_dev_err(&dev->interface->dev,
  1025. "Error %d when bringing DEP link up",
  1026. params_len);
  1027. return 0;
  1028. }
  1029. if (dev->tgt_available_prots &&
  1030. !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
  1031. nfc_dev_err(&dev->interface->dev,
  1032. "The target does not support DEP");
  1033. return -EINVAL;
  1034. }
  1035. resp = (struct pn533_cmd_jump_dep_response *) params;
  1036. cmd = (struct pn533_cmd_jump_dep *) arg;
  1037. rc = resp->status & PN533_CMD_RET_MASK;
  1038. if (rc != PN533_CMD_RET_SUCCESS) {
  1039. nfc_dev_err(&dev->interface->dev,
  1040. "Bringing DEP link up failed %d", rc);
  1041. return 0;
  1042. }
  1043. if (!dev->tgt_available_prots) {
  1044. nfc_dev_dbg(&dev->interface->dev, "Creating new target");
  1045. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1046. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1047. if (rc)
  1048. return 0;
  1049. dev->tgt_available_prots = 0;
  1050. }
  1051. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1052. /* ATR_RES general bytes are located at offset 17 */
  1053. target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
  1054. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1055. resp->gt, target_gt_len);
  1056. if (rc == 0)
  1057. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1058. dev->nfc_dev->targets[0].idx,
  1059. !cmd->active, NFC_RF_INITIATOR);
  1060. return 0;
  1061. }
  1062. static int pn533_dep_link_up(struct nfc_dev *nfc_dev, int target_idx,
  1063. u8 comm_mode, u8 rf_mode)
  1064. {
  1065. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1066. struct pn533_cmd_jump_dep *cmd;
  1067. u8 cmd_len, local_gt_len, *local_gt;
  1068. int rc;
  1069. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1070. if (rf_mode == NFC_RF_TARGET) {
  1071. nfc_dev_err(&dev->interface->dev, "Target mode not supported");
  1072. return -EOPNOTSUPP;
  1073. }
  1074. if (dev->poll_mod_count) {
  1075. nfc_dev_err(&dev->interface->dev,
  1076. "Cannot bring the DEP link up while polling");
  1077. return -EBUSY;
  1078. }
  1079. if (dev->tgt_active_prot) {
  1080. nfc_dev_err(&dev->interface->dev,
  1081. "There is already an active target");
  1082. return -EBUSY;
  1083. }
  1084. local_gt = nfc_get_local_general_bytes(dev->nfc_dev, &local_gt_len);
  1085. if (local_gt_len > NFC_MAX_GT_LEN)
  1086. return -EINVAL;
  1087. cmd_len = sizeof(struct pn533_cmd_jump_dep) + local_gt_len;
  1088. cmd = kzalloc(cmd_len, GFP_KERNEL);
  1089. if (cmd == NULL)
  1090. return -ENOMEM;
  1091. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
  1092. cmd->active = !comm_mode;
  1093. cmd->baud = 0;
  1094. if (local_gt != NULL) {
  1095. cmd->next = 4; /* We have some Gi */
  1096. memcpy(cmd->gt, local_gt, local_gt_len);
  1097. } else {
  1098. cmd->next = 0;
  1099. }
  1100. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
  1101. dev->out_frame->datalen += cmd_len;
  1102. pn533_tx_frame_finish(dev->out_frame);
  1103. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1104. dev->in_maxlen, pn533_in_dep_link_up_complete,
  1105. cmd, GFP_KERNEL);
  1106. if (rc)
  1107. goto out;
  1108. out:
  1109. kfree(cmd);
  1110. return rc;
  1111. }
  1112. static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
  1113. {
  1114. pn533_deactivate_target(nfc_dev, 0);
  1115. return 0;
  1116. }
  1117. #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
  1118. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  1119. static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
  1120. {
  1121. int payload_len = skb->len;
  1122. struct pn533_frame *out_frame;
  1123. u8 tg;
  1124. nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
  1125. payload_len);
  1126. if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  1127. /* TODO: Implement support to multi-part data exchange */
  1128. nfc_dev_err(&dev->interface->dev, "Data length greater than the"
  1129. " max allowed: %d",
  1130. PN533_CMD_DATAEXCH_DATA_MAXLEN);
  1131. return -ENOSYS;
  1132. }
  1133. skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
  1134. out_frame = (struct pn533_frame *) skb->data;
  1135. pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
  1136. tg = 1;
  1137. memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
  1138. out_frame->datalen += sizeof(u8);
  1139. /* The data is already in the out_frame, just update the datalen */
  1140. out_frame->datalen += payload_len;
  1141. pn533_tx_frame_finish(out_frame);
  1142. skb_put(skb, PN533_FRAME_TAIL_SIZE);
  1143. return 0;
  1144. }
  1145. struct pn533_data_exchange_arg {
  1146. struct sk_buff *skb_resp;
  1147. struct sk_buff *skb_out;
  1148. data_exchange_cb_t cb;
  1149. void *cb_context;
  1150. };
  1151. static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
  1152. u8 *params, int params_len)
  1153. {
  1154. struct pn533_data_exchange_arg *arg = _arg;
  1155. struct sk_buff *skb_resp = arg->skb_resp;
  1156. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  1157. int err = 0;
  1158. u8 status;
  1159. u8 cmd_ret;
  1160. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1161. dev_kfree_skb_irq(arg->skb_out);
  1162. if (params_len < 0) { /* error */
  1163. err = params_len;
  1164. goto error;
  1165. }
  1166. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  1167. status = params[0];
  1168. cmd_ret = status & PN533_CMD_RET_MASK;
  1169. if (cmd_ret != PN533_CMD_RET_SUCCESS) {
  1170. nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
  1171. " exchanging data", cmd_ret);
  1172. err = -EIO;
  1173. goto error;
  1174. }
  1175. if (status & PN533_CMD_MI_MASK) {
  1176. /* TODO: Implement support to multi-part data exchange */
  1177. nfc_dev_err(&dev->interface->dev, "Multi-part message not yet"
  1178. " supported");
  1179. /* Prevent the other messages from controller */
  1180. pn533_send_ack(dev, GFP_ATOMIC);
  1181. err = -ENOSYS;
  1182. goto error;
  1183. }
  1184. skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
  1185. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
  1186. arg->cb(arg->cb_context, skb_resp, 0);
  1187. kfree(arg);
  1188. return 0;
  1189. error:
  1190. dev_kfree_skb_irq(skb_resp);
  1191. arg->cb(arg->cb_context, NULL, err);
  1192. kfree(arg);
  1193. return 0;
  1194. }
  1195. static int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
  1196. struct sk_buff *skb,
  1197. data_exchange_cb_t cb,
  1198. void *cb_context)
  1199. {
  1200. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1201. struct pn533_frame *out_frame, *in_frame;
  1202. struct pn533_data_exchange_arg *arg;
  1203. struct sk_buff *skb_resp;
  1204. int skb_resp_len;
  1205. int rc;
  1206. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1207. if (!dev->tgt_active_prot) {
  1208. nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
  1209. " there is no active target");
  1210. rc = -EINVAL;
  1211. goto error;
  1212. }
  1213. rc = pn533_data_exchange_tx_frame(dev, skb);
  1214. if (rc)
  1215. goto error;
  1216. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  1217. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1218. PN533_FRAME_TAIL_SIZE;
  1219. skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
  1220. if (!skb_resp) {
  1221. rc = -ENOMEM;
  1222. goto error;
  1223. }
  1224. in_frame = (struct pn533_frame *) skb_resp->data;
  1225. out_frame = (struct pn533_frame *) skb->data;
  1226. arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
  1227. if (!arg) {
  1228. rc = -ENOMEM;
  1229. goto free_skb_resp;
  1230. }
  1231. arg->skb_resp = skb_resp;
  1232. arg->skb_out = skb;
  1233. arg->cb = cb;
  1234. arg->cb_context = cb_context;
  1235. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
  1236. pn533_data_exchange_complete, arg,
  1237. GFP_KERNEL);
  1238. if (rc) {
  1239. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1240. " perform data_exchange", rc);
  1241. goto free_arg;
  1242. }
  1243. return 0;
  1244. free_arg:
  1245. kfree(arg);
  1246. free_skb_resp:
  1247. kfree_skb(skb_resp);
  1248. error:
  1249. kfree_skb(skb);
  1250. return rc;
  1251. }
  1252. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  1253. u8 cfgdata_len)
  1254. {
  1255. int rc;
  1256. u8 *params;
  1257. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1258. pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
  1259. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1260. params[0] = cfgitem;
  1261. memcpy(&params[1], cfgdata, cfgdata_len);
  1262. dev->out_frame->datalen += (1 + cfgdata_len);
  1263. pn533_tx_frame_finish(dev->out_frame);
  1264. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1265. dev->in_maxlen);
  1266. return rc;
  1267. }
  1268. struct nfc_ops pn533_nfc_ops = {
  1269. .dev_up = NULL,
  1270. .dev_down = NULL,
  1271. .dep_link_up = pn533_dep_link_up,
  1272. .dep_link_down = pn533_dep_link_down,
  1273. .start_poll = pn533_start_poll,
  1274. .stop_poll = pn533_stop_poll,
  1275. .activate_target = pn533_activate_target,
  1276. .deactivate_target = pn533_deactivate_target,
  1277. .data_exchange = pn533_data_exchange,
  1278. };
  1279. static int pn533_probe(struct usb_interface *interface,
  1280. const struct usb_device_id *id)
  1281. {
  1282. struct pn533_fw_version *fw_ver;
  1283. struct pn533 *dev;
  1284. struct usb_host_interface *iface_desc;
  1285. struct usb_endpoint_descriptor *endpoint;
  1286. struct pn533_config_max_retries max_retries;
  1287. int in_endpoint = 0;
  1288. int out_endpoint = 0;
  1289. int rc = -ENOMEM;
  1290. int i;
  1291. u32 protocols;
  1292. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1293. if (!dev)
  1294. return -ENOMEM;
  1295. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  1296. dev->interface = interface;
  1297. sema_init(&dev->cmd_lock, 1);
  1298. iface_desc = interface->cur_altsetting;
  1299. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1300. endpoint = &iface_desc->endpoint[i].desc;
  1301. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1302. dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
  1303. in_endpoint = endpoint->bEndpointAddress;
  1304. }
  1305. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
  1306. dev->out_maxlen =
  1307. le16_to_cpu(endpoint->wMaxPacketSize);
  1308. out_endpoint = endpoint->bEndpointAddress;
  1309. }
  1310. }
  1311. if (!in_endpoint || !out_endpoint) {
  1312. nfc_dev_err(&interface->dev, "Could not find bulk-in or"
  1313. " bulk-out endpoint");
  1314. rc = -ENODEV;
  1315. goto error;
  1316. }
  1317. dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
  1318. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1319. dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
  1320. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  1321. if (!dev->in_frame || !dev->out_frame ||
  1322. !dev->in_urb || !dev->out_urb)
  1323. goto error;
  1324. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  1325. usb_rcvbulkpipe(dev->udev, in_endpoint),
  1326. NULL, 0, NULL, dev);
  1327. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  1328. usb_sndbulkpipe(dev->udev, out_endpoint),
  1329. NULL, 0,
  1330. pn533_send_complete, dev);
  1331. tasklet_init(&dev->tasklet, pn533_tasklet_cmd_complete, (ulong)dev);
  1332. usb_set_intfdata(interface, dev);
  1333. pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
  1334. pn533_tx_frame_finish(dev->out_frame);
  1335. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1336. dev->in_maxlen);
  1337. if (rc)
  1338. goto kill_tasklet;
  1339. fw_ver = (struct pn533_fw_version *)
  1340. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1341. nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
  1342. " attached", fw_ver->ver, fw_ver->rev);
  1343. protocols = NFC_PROTO_JEWEL_MASK
  1344. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  1345. | NFC_PROTO_ISO14443_MASK
  1346. | NFC_PROTO_NFC_DEP_MASK;
  1347. dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
  1348. PN533_CMD_DATAEXCH_HEAD_LEN,
  1349. PN533_FRAME_TAIL_SIZE);
  1350. if (!dev->nfc_dev)
  1351. goto kill_tasklet;
  1352. nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
  1353. nfc_set_drvdata(dev->nfc_dev, dev);
  1354. rc = nfc_register_device(dev->nfc_dev);
  1355. if (rc)
  1356. goto free_nfc_dev;
  1357. max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
  1358. max_retries.mx_rty_psl = 2;
  1359. max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1360. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  1361. (u8 *) &max_retries, sizeof(max_retries));
  1362. if (rc) {
  1363. nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
  1364. " config");
  1365. goto free_nfc_dev;
  1366. }
  1367. return 0;
  1368. free_nfc_dev:
  1369. nfc_free_device(dev->nfc_dev);
  1370. kill_tasklet:
  1371. tasklet_kill(&dev->tasklet);
  1372. error:
  1373. kfree(dev->in_frame);
  1374. usb_free_urb(dev->in_urb);
  1375. kfree(dev->out_frame);
  1376. usb_free_urb(dev->out_urb);
  1377. kfree(dev);
  1378. return rc;
  1379. }
  1380. static void pn533_disconnect(struct usb_interface *interface)
  1381. {
  1382. struct pn533 *dev;
  1383. dev = usb_get_intfdata(interface);
  1384. usb_set_intfdata(interface, NULL);
  1385. nfc_unregister_device(dev->nfc_dev);
  1386. nfc_free_device(dev->nfc_dev);
  1387. usb_kill_urb(dev->in_urb);
  1388. usb_kill_urb(dev->out_urb);
  1389. tasklet_kill(&dev->tasklet);
  1390. kfree(dev->in_frame);
  1391. usb_free_urb(dev->in_urb);
  1392. kfree(dev->out_frame);
  1393. usb_free_urb(dev->out_urb);
  1394. kfree(dev);
  1395. nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
  1396. }
  1397. static struct usb_driver pn533_driver = {
  1398. .name = "pn533",
  1399. .probe = pn533_probe,
  1400. .disconnect = pn533_disconnect,
  1401. .id_table = pn533_table,
  1402. };
  1403. module_usb_driver(pn533_driver);
  1404. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
  1405. " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  1406. MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
  1407. MODULE_VERSION(VERSION);
  1408. MODULE_LICENSE("GPL");