pn533.c 51 KB

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