pn533.c 48 KB

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