pn533.c 52 KB

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