pn533.c 45 KB

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