pn533.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  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. #define SONY_VENDOR_ID 0x054c
  37. #define PASORI_PRODUCT_ID 0x02e1
  38. #define PN533_DEVICE_STD 0x1
  39. #define PN533_DEVICE_PASORI 0x2
  40. #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
  41. NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
  42. NFC_PROTO_NFC_DEP_MASK |\
  43. NFC_PROTO_ISO14443_B_MASK)
  44. #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
  45. NFC_PROTO_MIFARE_MASK | \
  46. NFC_PROTO_FELICA_MASK | \
  47. NFC_PROTO_ISO14443_MASK | \
  48. NFC_PROTO_NFC_DEP_MASK)
  49. static const struct usb_device_id pn533_table[] = {
  50. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  51. .idVendor = PN533_VENDOR_ID,
  52. .idProduct = PN533_PRODUCT_ID,
  53. .driver_info = PN533_DEVICE_STD,
  54. },
  55. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  56. .idVendor = SCM_VENDOR_ID,
  57. .idProduct = SCL3711_PRODUCT_ID,
  58. .driver_info = PN533_DEVICE_STD,
  59. },
  60. { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  61. .idVendor = SONY_VENDOR_ID,
  62. .idProduct = PASORI_PRODUCT_ID,
  63. .driver_info = PN533_DEVICE_PASORI,
  64. },
  65. { }
  66. };
  67. MODULE_DEVICE_TABLE(usb, pn533_table);
  68. /* How much time we spend listening for initiators */
  69. #define PN533_LISTEN_TIME 2
  70. /* frame definitions */
  71. #define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
  72. 254 (DATA)
  73. 2 (DCS, postamble) */
  74. #define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
  75. + 2) /* data[0] TFI, data[1] CC */
  76. #define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
  77. /*
  78. * Max extended frame payload len, excluding TFI and CC
  79. * which are already in PN533_FRAME_HEADER_LEN.
  80. */
  81. #define PN533_FRAME_MAX_PAYLOAD_LEN 263
  82. #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
  83. PN533_FRAME_TAIL_LEN)
  84. #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
  85. #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
  86. #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
  87. /* start of frame */
  88. #define PN533_SOF 0x00FF
  89. /* frame identifier: in/out/error */
  90. #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
  91. #define PN533_DIR_OUT 0xD4
  92. #define PN533_DIR_IN 0xD5
  93. /* PN533 Commands */
  94. #define PN533_FRAME_CMD(f) (f->data[1])
  95. #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
  96. #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
  97. #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
  98. #define PN533_CMD_RF_CONFIGURATION 0x32
  99. #define PN533_CMD_IN_DATA_EXCHANGE 0x40
  100. #define PN533_CMD_IN_COMM_THRU 0x42
  101. #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
  102. #define PN533_CMD_IN_ATR 0x50
  103. #define PN533_CMD_IN_RELEASE 0x52
  104. #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
  105. #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
  106. #define PN533_CMD_TG_GET_DATA 0x86
  107. #define PN533_CMD_TG_SET_DATA 0x8e
  108. #define PN533_CMD_UNDEF 0xff
  109. #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
  110. /* PN533 Return codes */
  111. #define PN533_CMD_RET_MASK 0x3F
  112. #define PN533_CMD_MI_MASK 0x40
  113. #define PN533_CMD_RET_SUCCESS 0x00
  114. struct pn533;
  115. typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
  116. u8 *params, int params_len);
  117. typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
  118. struct sk_buff *resp);
  119. /* structs for pn533 commands */
  120. /* PN533_CMD_GET_FIRMWARE_VERSION */
  121. struct pn533_fw_version {
  122. u8 ic;
  123. u8 ver;
  124. u8 rev;
  125. u8 support;
  126. };
  127. /* PN533_CMD_RF_CONFIGURATION */
  128. #define PN533_CFGITEM_TIMING 0x02
  129. #define PN533_CFGITEM_MAX_RETRIES 0x05
  130. #define PN533_CFGITEM_PASORI 0x82
  131. #define PN533_CONFIG_TIMING_102 0xb
  132. #define PN533_CONFIG_TIMING_204 0xc
  133. #define PN533_CONFIG_TIMING_409 0xd
  134. #define PN533_CONFIG_TIMING_819 0xe
  135. #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
  136. #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
  137. struct pn533_config_max_retries {
  138. u8 mx_rty_atr;
  139. u8 mx_rty_psl;
  140. u8 mx_rty_passive_act;
  141. } __packed;
  142. struct pn533_config_timing {
  143. u8 rfu;
  144. u8 atr_res_timeout;
  145. u8 dep_timeout;
  146. } __packed;
  147. /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
  148. /* felica commands opcode */
  149. #define PN533_FELICA_OPC_SENSF_REQ 0
  150. #define PN533_FELICA_OPC_SENSF_RES 1
  151. /* felica SENSF_REQ parameters */
  152. #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
  153. #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
  154. #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
  155. #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
  156. /* type B initiator_data values */
  157. #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
  158. #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
  159. #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
  160. union pn533_cmd_poll_initdata {
  161. struct {
  162. u8 afi;
  163. u8 polling_method;
  164. } __packed type_b;
  165. struct {
  166. u8 opcode;
  167. __be16 sc;
  168. u8 rc;
  169. u8 tsn;
  170. } __packed felica;
  171. };
  172. /* Poll modulations */
  173. enum {
  174. PN533_POLL_MOD_106KBPS_A,
  175. PN533_POLL_MOD_212KBPS_FELICA,
  176. PN533_POLL_MOD_424KBPS_FELICA,
  177. PN533_POLL_MOD_106KBPS_JEWEL,
  178. PN533_POLL_MOD_847KBPS_B,
  179. PN533_LISTEN_MOD,
  180. __PN533_POLL_MOD_AFTER_LAST,
  181. };
  182. #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
  183. struct pn533_poll_modulations {
  184. struct {
  185. u8 maxtg;
  186. u8 brty;
  187. union pn533_cmd_poll_initdata initiator_data;
  188. } __packed data;
  189. u8 len;
  190. };
  191. const struct pn533_poll_modulations poll_mod[] = {
  192. [PN533_POLL_MOD_106KBPS_A] = {
  193. .data = {
  194. .maxtg = 1,
  195. .brty = 0,
  196. },
  197. .len = 2,
  198. },
  199. [PN533_POLL_MOD_212KBPS_FELICA] = {
  200. .data = {
  201. .maxtg = 1,
  202. .brty = 1,
  203. .initiator_data.felica = {
  204. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  205. .sc = PN533_FELICA_SENSF_SC_ALL,
  206. .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
  207. .tsn = 0,
  208. },
  209. },
  210. .len = 7,
  211. },
  212. [PN533_POLL_MOD_424KBPS_FELICA] = {
  213. .data = {
  214. .maxtg = 1,
  215. .brty = 2,
  216. .initiator_data.felica = {
  217. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  218. .sc = PN533_FELICA_SENSF_SC_ALL,
  219. .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
  220. .tsn = 0,
  221. },
  222. },
  223. .len = 7,
  224. },
  225. [PN533_POLL_MOD_106KBPS_JEWEL] = {
  226. .data = {
  227. .maxtg = 1,
  228. .brty = 4,
  229. },
  230. .len = 2,
  231. },
  232. [PN533_POLL_MOD_847KBPS_B] = {
  233. .data = {
  234. .maxtg = 1,
  235. .brty = 8,
  236. .initiator_data.type_b = {
  237. .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
  238. .polling_method =
  239. PN533_TYPE_B_POLL_METHOD_TIMESLOT,
  240. },
  241. },
  242. .len = 3,
  243. },
  244. [PN533_LISTEN_MOD] = {
  245. .len = 0,
  246. },
  247. };
  248. /* PN533_CMD_IN_ATR */
  249. struct pn533_cmd_activate_param {
  250. u8 tg;
  251. u8 next;
  252. } __packed;
  253. struct pn533_cmd_activate_response {
  254. u8 status;
  255. u8 nfcid3t[10];
  256. u8 didt;
  257. u8 bst;
  258. u8 brt;
  259. u8 to;
  260. u8 ppt;
  261. /* optional */
  262. u8 gt[];
  263. } __packed;
  264. /* PN533_CMD_IN_JUMP_FOR_DEP */
  265. struct pn533_cmd_jump_dep {
  266. u8 active;
  267. u8 baud;
  268. u8 next;
  269. u8 data[];
  270. } __packed;
  271. struct pn533_cmd_jump_dep_response {
  272. u8 status;
  273. u8 tg;
  274. u8 nfcid3t[10];
  275. u8 didt;
  276. u8 bst;
  277. u8 brt;
  278. u8 to;
  279. u8 ppt;
  280. /* optional */
  281. u8 gt[];
  282. } __packed;
  283. /* PN533_TG_INIT_AS_TARGET */
  284. #define PN533_INIT_TARGET_PASSIVE 0x1
  285. #define PN533_INIT_TARGET_DEP 0x2
  286. #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
  287. #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
  288. #define PN533_INIT_TARGET_RESP_DEP 0x4
  289. struct pn533_cmd_init_target {
  290. u8 mode;
  291. u8 mifare[6];
  292. u8 felica[18];
  293. u8 nfcid3[10];
  294. u8 gb_len;
  295. u8 gb[];
  296. } __packed;
  297. struct pn533_cmd_init_target_response {
  298. u8 mode;
  299. u8 cmd[];
  300. } __packed;
  301. struct pn533 {
  302. struct usb_device *udev;
  303. struct usb_interface *interface;
  304. struct nfc_dev *nfc_dev;
  305. struct urb *out_urb;
  306. struct pn533_frame *out_frame;
  307. struct urb *in_urb;
  308. struct pn533_frame *in_frame;
  309. struct sk_buff_head resp_q;
  310. struct workqueue_struct *wq;
  311. struct work_struct cmd_work;
  312. struct work_struct cmd_complete_work;
  313. struct work_struct poll_work;
  314. struct work_struct mi_work;
  315. struct work_struct tg_work;
  316. struct timer_list listen_timer;
  317. struct pn533_frame *wq_in_frame;
  318. int wq_in_error;
  319. int cancel_listen;
  320. pn533_cmd_complete_t cmd_complete;
  321. void *cmd_complete_arg;
  322. struct mutex cmd_lock;
  323. u8 cmd;
  324. struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
  325. u8 poll_mod_count;
  326. u8 poll_mod_curr;
  327. u32 poll_protocols;
  328. u32 listen_protocols;
  329. u8 *gb;
  330. size_t gb_len;
  331. u8 tgt_available_prots;
  332. u8 tgt_active_prot;
  333. u8 tgt_mode;
  334. u32 device_type;
  335. struct list_head cmd_queue;
  336. u8 cmd_pending;
  337. };
  338. struct pn533_cmd {
  339. struct list_head queue;
  340. struct pn533_frame *out_frame;
  341. struct pn533_frame *in_frame;
  342. int in_frame_len;
  343. u8 cmd_code;
  344. struct sk_buff *req;
  345. struct sk_buff *resp;
  346. pn533_cmd_complete_t cmd_complete;
  347. void *arg;
  348. };
  349. struct pn533_frame {
  350. u8 preamble;
  351. __be16 start_frame;
  352. u8 datalen;
  353. u8 datalen_checksum;
  354. u8 data[];
  355. } __packed;
  356. /* The rule: value + checksum = 0 */
  357. static inline u8 pn533_checksum(u8 value)
  358. {
  359. return ~value + 1;
  360. }
  361. /* The rule: sum(data elements) + checksum = 0 */
  362. static u8 pn533_data_checksum(u8 *data, int datalen)
  363. {
  364. u8 sum = 0;
  365. int i;
  366. for (i = 0; i < datalen; i++)
  367. sum += data[i];
  368. return pn533_checksum(sum);
  369. }
  370. /**
  371. * pn533_tx_frame_ack - create a ack frame
  372. * @frame: The frame to be set as ack
  373. *
  374. * Ack is different type of standard frame. As a standard frame, it has
  375. * preamble and start_frame. However the checksum of this frame must fail,
  376. * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
  377. * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
  378. * After datalen_checksum field, the postamble is placed.
  379. */
  380. static void pn533_tx_frame_ack(struct pn533_frame *frame)
  381. {
  382. frame->preamble = 0;
  383. frame->start_frame = cpu_to_be16(PN533_SOF);
  384. frame->datalen = 0;
  385. frame->datalen_checksum = 0xFF;
  386. /* data[0] is used as postamble */
  387. frame->data[0] = 0;
  388. }
  389. static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
  390. {
  391. frame->preamble = 0;
  392. frame->start_frame = cpu_to_be16(PN533_SOF);
  393. PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
  394. PN533_FRAME_CMD(frame) = cmd;
  395. frame->datalen = 2;
  396. }
  397. static void pn533_tx_frame_finish(struct pn533_frame *frame)
  398. {
  399. frame->datalen_checksum = pn533_checksum(frame->datalen);
  400. PN533_FRAME_CHECKSUM(frame) =
  401. pn533_data_checksum(frame->data, frame->datalen);
  402. PN533_FRAME_POSTAMBLE(frame) = 0;
  403. }
  404. static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
  405. {
  406. u8 checksum;
  407. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  408. return false;
  409. checksum = pn533_checksum(frame->datalen);
  410. if (checksum != frame->datalen_checksum)
  411. return false;
  412. checksum = pn533_data_checksum(frame->data, frame->datalen);
  413. if (checksum != PN533_FRAME_CHECKSUM(frame))
  414. return false;
  415. return true;
  416. }
  417. static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
  418. {
  419. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  420. return false;
  421. if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
  422. return false;
  423. return true;
  424. }
  425. static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
  426. {
  427. return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
  428. }
  429. static void pn533_wq_cmd_complete(struct work_struct *work)
  430. {
  431. struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
  432. struct pn533_frame *in_frame;
  433. int rc;
  434. in_frame = dev->wq_in_frame;
  435. if (dev->wq_in_error)
  436. rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
  437. dev->wq_in_error);
  438. else
  439. rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
  440. PN533_FRAME_CMD_PARAMS_PTR(in_frame),
  441. PN533_FRAME_CMD_PARAMS_LEN(in_frame));
  442. if (rc != -EINPROGRESS)
  443. queue_work(dev->wq, &dev->cmd_work);
  444. }
  445. static void pn533_recv_response(struct urb *urb)
  446. {
  447. struct pn533 *dev = urb->context;
  448. struct pn533_frame *in_frame;
  449. dev->wq_in_frame = NULL;
  450. switch (urb->status) {
  451. case 0:
  452. /* success */
  453. break;
  454. case -ECONNRESET:
  455. case -ENOENT:
  456. case -ESHUTDOWN:
  457. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  458. " status: %d", urb->status);
  459. dev->wq_in_error = urb->status;
  460. goto sched_wq;
  461. default:
  462. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  463. " %d", urb->status);
  464. dev->wq_in_error = urb->status;
  465. goto sched_wq;
  466. }
  467. in_frame = dev->in_urb->transfer_buffer;
  468. if (!pn533_rx_frame_is_valid(in_frame)) {
  469. nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
  470. dev->wq_in_error = -EIO;
  471. goto sched_wq;
  472. }
  473. if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
  474. nfc_dev_err(&dev->interface->dev, "The received frame is not "
  475. "response to the last command");
  476. dev->wq_in_error = -EIO;
  477. goto sched_wq;
  478. }
  479. nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
  480. dev->wq_in_error = 0;
  481. dev->wq_in_frame = in_frame;
  482. sched_wq:
  483. queue_work(dev->wq, &dev->cmd_complete_work);
  484. }
  485. static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
  486. {
  487. dev->in_urb->complete = pn533_recv_response;
  488. return usb_submit_urb(dev->in_urb, flags);
  489. }
  490. static void pn533_recv_ack(struct urb *urb)
  491. {
  492. struct pn533 *dev = urb->context;
  493. struct pn533_frame *in_frame;
  494. int rc;
  495. switch (urb->status) {
  496. case 0:
  497. /* success */
  498. break;
  499. case -ECONNRESET:
  500. case -ENOENT:
  501. case -ESHUTDOWN:
  502. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  503. " status: %d", urb->status);
  504. dev->wq_in_error = urb->status;
  505. goto sched_wq;
  506. default:
  507. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  508. " %d", urb->status);
  509. dev->wq_in_error = urb->status;
  510. goto sched_wq;
  511. }
  512. in_frame = dev->in_urb->transfer_buffer;
  513. if (!pn533_rx_frame_is_ack(in_frame)) {
  514. nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
  515. dev->wq_in_error = -EIO;
  516. goto sched_wq;
  517. }
  518. nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
  519. rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
  520. if (rc) {
  521. nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
  522. " result %d", rc);
  523. dev->wq_in_error = rc;
  524. goto sched_wq;
  525. }
  526. return;
  527. sched_wq:
  528. dev->wq_in_frame = NULL;
  529. queue_work(dev->wq, &dev->cmd_complete_work);
  530. }
  531. static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
  532. {
  533. dev->in_urb->complete = pn533_recv_ack;
  534. return usb_submit_urb(dev->in_urb, flags);
  535. }
  536. static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
  537. {
  538. int rc;
  539. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  540. pn533_tx_frame_ack(dev->out_frame);
  541. dev->out_urb->transfer_buffer = dev->out_frame;
  542. dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
  543. rc = usb_submit_urb(dev->out_urb, flags);
  544. return rc;
  545. }
  546. static int __pn533_send_cmd_frame_async(struct pn533 *dev,
  547. struct pn533_frame *out_frame,
  548. struct pn533_frame *in_frame,
  549. int in_frame_len,
  550. pn533_cmd_complete_t cmd_complete,
  551. void *arg)
  552. {
  553. int rc;
  554. nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
  555. PN533_FRAME_CMD(out_frame));
  556. dev->cmd = PN533_FRAME_CMD(out_frame);
  557. dev->cmd_complete = cmd_complete;
  558. dev->cmd_complete_arg = arg;
  559. dev->out_urb->transfer_buffer = out_frame;
  560. dev->out_urb->transfer_buffer_length =
  561. PN533_FRAME_SIZE(out_frame);
  562. dev->in_urb->transfer_buffer = in_frame;
  563. dev->in_urb->transfer_buffer_length = in_frame_len;
  564. rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
  565. if (rc)
  566. return rc;
  567. rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
  568. if (rc)
  569. goto error;
  570. return 0;
  571. error:
  572. usb_unlink_urb(dev->out_urb);
  573. return rc;
  574. }
  575. static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
  576. {
  577. struct pn533_frame *frame;
  578. /* payload is already there, just update datalen */
  579. int payload_len = skb->len;
  580. skb_push(skb, PN533_FRAME_HEADER_LEN);
  581. skb_put(skb, PN533_FRAME_TAIL_LEN);
  582. frame = (struct pn533_frame *)skb->data;
  583. pn533_tx_frame_init(frame, cmd_code);
  584. frame->datalen += payload_len;
  585. pn533_tx_frame_finish(frame);
  586. }
  587. struct pn533_send_async_complete_arg {
  588. pn533_send_async_complete_t complete_cb;
  589. void *complete_cb_context;
  590. struct sk_buff *resp;
  591. struct sk_buff *req;
  592. };
  593. static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
  594. int params_len)
  595. {
  596. struct pn533_send_async_complete_arg *arg = _arg;
  597. struct sk_buff *req = arg->req;
  598. struct sk_buff *resp = arg->resp;
  599. struct pn533_frame *frame = (struct pn533_frame *)resp->data;
  600. int rc;
  601. dev_kfree_skb(req);
  602. if (params_len < 0) {
  603. nfc_dev_err(&dev->interface->dev,
  604. "Error %d when starting as a target",
  605. params_len);
  606. arg->complete_cb(dev, arg->complete_cb_context,
  607. ERR_PTR(params_len));
  608. rc = params_len;
  609. dev_kfree_skb(resp);
  610. goto out;
  611. }
  612. skb_put(resp, PN533_FRAME_SIZE(frame));
  613. skb_pull(resp, PN533_FRAME_HEADER_LEN);
  614. skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
  615. rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
  616. out:
  617. kfree(arg);
  618. return rc;
  619. }
  620. static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
  621. struct sk_buff *req, struct sk_buff *resp,
  622. int resp_len,
  623. pn533_send_async_complete_t complete_cb,
  624. void *complete_cb_context)
  625. {
  626. struct pn533_cmd *cmd;
  627. struct pn533_send_async_complete_arg *arg;
  628. int rc = 0;
  629. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  630. arg = kzalloc(sizeof(arg), GFP_KERNEL);
  631. if (!arg)
  632. return -ENOMEM;
  633. arg->complete_cb = complete_cb;
  634. arg->complete_cb_context = complete_cb_context;
  635. arg->resp = resp;
  636. arg->req = req;
  637. pn533_build_cmd_frame(cmd_code, req);
  638. mutex_lock(&dev->cmd_lock);
  639. if (!dev->cmd_pending) {
  640. rc = __pn533_send_cmd_frame_async(dev,
  641. (struct pn533_frame *)req->data,
  642. (struct pn533_frame *)resp->data,
  643. resp_len, pn533_send_async_complete,
  644. arg);
  645. if (rc)
  646. goto error;
  647. dev->cmd_pending = 1;
  648. goto unlock;
  649. }
  650. nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
  651. cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
  652. if (!cmd) {
  653. rc = -ENOMEM;
  654. goto error;
  655. }
  656. INIT_LIST_HEAD(&cmd->queue);
  657. cmd->cmd_code = cmd_code;
  658. cmd->req = req;
  659. cmd->resp = resp;
  660. cmd->arg = arg;
  661. list_add_tail(&cmd->queue, &dev->cmd_queue);
  662. goto unlock;
  663. error:
  664. kfree(arg);
  665. unlock:
  666. mutex_unlock(&dev->cmd_lock);
  667. return rc;
  668. }
  669. static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
  670. struct sk_buff *req,
  671. pn533_send_async_complete_t complete_cb,
  672. void *complete_cb_context)
  673. {
  674. struct sk_buff *resp;
  675. int rc;
  676. int resp_len = PN533_FRAME_HEADER_LEN +
  677. PN533_FRAME_MAX_PAYLOAD_LEN +
  678. PN533_FRAME_TAIL_LEN;
  679. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  680. resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
  681. if (!resp)
  682. return -ENOMEM;
  683. rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
  684. complete_cb_context);
  685. if (rc)
  686. dev_kfree_skb(resp);
  687. return rc;
  688. }
  689. static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
  690. struct sk_buff *req,
  691. pn533_send_async_complete_t complete_cb,
  692. void *complete_cb_context)
  693. {
  694. struct sk_buff *resp;
  695. int rc;
  696. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  697. resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
  698. if (!resp)
  699. return -ENOMEM;
  700. rc = __pn533_send_async(dev, cmd_code, req, resp,
  701. PN533_NORMAL_FRAME_MAX_LEN,
  702. complete_cb, complete_cb_context);
  703. if (rc)
  704. dev_kfree_skb(resp);
  705. return rc;
  706. }
  707. static void pn533_wq_cmd(struct work_struct *work)
  708. {
  709. struct pn533 *dev = container_of(work, struct pn533, cmd_work);
  710. struct pn533_cmd *cmd;
  711. mutex_lock(&dev->cmd_lock);
  712. if (list_empty(&dev->cmd_queue)) {
  713. dev->cmd_pending = 0;
  714. mutex_unlock(&dev->cmd_lock);
  715. return;
  716. }
  717. cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
  718. list_del(&cmd->queue);
  719. mutex_unlock(&dev->cmd_lock);
  720. if (cmd->cmd_code != PN533_CMD_UNDEF)
  721. __pn533_send_cmd_frame_async(dev,
  722. (struct pn533_frame *)cmd->req->data,
  723. (struct pn533_frame *)cmd->resp->data,
  724. PN533_NORMAL_FRAME_MAX_LEN,
  725. pn533_send_async_complete,
  726. cmd->arg);
  727. else
  728. __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
  729. cmd->in_frame_len,
  730. cmd->cmd_complete, cmd->arg);
  731. kfree(cmd);
  732. }
  733. static int pn533_send_cmd_frame_async(struct pn533 *dev,
  734. struct pn533_frame *out_frame,
  735. struct pn533_frame *in_frame,
  736. int in_frame_len,
  737. pn533_cmd_complete_t cmd_complete,
  738. void *arg)
  739. {
  740. struct pn533_cmd *cmd;
  741. int rc = 0;
  742. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  743. mutex_lock(&dev->cmd_lock);
  744. if (!dev->cmd_pending) {
  745. rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
  746. in_frame_len, cmd_complete,
  747. arg);
  748. if (!rc)
  749. dev->cmd_pending = 1;
  750. goto unlock;
  751. }
  752. nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
  753. cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
  754. if (!cmd) {
  755. rc = -ENOMEM;
  756. goto unlock;
  757. }
  758. INIT_LIST_HEAD(&cmd->queue);
  759. cmd->out_frame = out_frame;
  760. cmd->in_frame = in_frame;
  761. cmd->in_frame_len = in_frame_len;
  762. cmd->cmd_code = PN533_CMD_UNDEF;
  763. cmd->cmd_complete = cmd_complete;
  764. cmd->arg = arg;
  765. list_add_tail(&cmd->queue, &dev->cmd_queue);
  766. unlock:
  767. mutex_unlock(&dev->cmd_lock);
  768. return rc;
  769. }
  770. struct pn533_sync_cmd_response {
  771. int rc;
  772. struct sk_buff *resp;
  773. struct completion done;
  774. };
  775. static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
  776. u8 *params, int params_len)
  777. {
  778. struct pn533_sync_cmd_response *arg = _arg;
  779. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  780. arg->rc = 0;
  781. if (params_len < 0) /* error */
  782. arg->rc = params_len;
  783. complete(&arg->done);
  784. return 0;
  785. }
  786. static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
  787. struct sk_buff *resp)
  788. {
  789. struct pn533_sync_cmd_response *arg = _arg;
  790. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  791. arg->resp = resp;
  792. complete(&arg->done);
  793. return 0;
  794. }
  795. /* pn533_send_cmd_sync
  796. *
  797. * Please note the req parameter is freed inside the function to
  798. * limit a number of return value interpretations by the caller.
  799. *
  800. * 1. negative in case of error during TX path -> req should be freed
  801. *
  802. * 2. negative in case of error during RX path -> req should not be freed
  803. * as it's been already freed at the begining of RX path by
  804. * async_complete_cb.
  805. *
  806. * 3. valid pointer in case of succesfult RX path
  807. *
  808. * A caller has to check a return value with IS_ERR macro. If the test pass,
  809. * the returned pointer is valid.
  810. *
  811. * */
  812. static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
  813. struct sk_buff *req)
  814. {
  815. int rc;
  816. struct pn533_sync_cmd_response arg;
  817. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  818. init_completion(&arg.done);
  819. rc = pn533_send_cmd_async(dev, cmd_code, req,
  820. pn533_send_sync_complete, &arg);
  821. if (rc) {
  822. dev_kfree_skb(req);
  823. return ERR_PTR(rc);
  824. }
  825. wait_for_completion(&arg.done);
  826. return arg.resp;
  827. }
  828. static int pn533_send_cmd_frame_sync(struct pn533 *dev,
  829. struct pn533_frame *out_frame,
  830. struct pn533_frame *in_frame,
  831. int in_frame_len)
  832. {
  833. int rc;
  834. struct pn533_sync_cmd_response arg;
  835. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  836. init_completion(&arg.done);
  837. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
  838. pn533_sync_cmd_complete, &arg);
  839. if (rc)
  840. return rc;
  841. wait_for_completion(&arg.done);
  842. return arg.rc;
  843. }
  844. static void pn533_send_complete(struct urb *urb)
  845. {
  846. struct pn533 *dev = urb->context;
  847. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  848. switch (urb->status) {
  849. case 0:
  850. /* success */
  851. break;
  852. case -ECONNRESET:
  853. case -ENOENT:
  854. case -ESHUTDOWN:
  855. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  856. " status: %d", urb->status);
  857. break;
  858. default:
  859. nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
  860. " %d", urb->status);
  861. }
  862. }
  863. struct pn533_target_type_a {
  864. __be16 sens_res;
  865. u8 sel_res;
  866. u8 nfcid_len;
  867. u8 nfcid_data[];
  868. } __packed;
  869. #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
  870. #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
  871. #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
  872. #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
  873. #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
  874. #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
  875. #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
  876. #define PN533_TYPE_A_SEL_PROT_MIFARE 0
  877. #define PN533_TYPE_A_SEL_PROT_ISO14443 1
  878. #define PN533_TYPE_A_SEL_PROT_DEP 2
  879. #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
  880. static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
  881. int target_data_len)
  882. {
  883. u8 ssd;
  884. u8 platconf;
  885. if (target_data_len < sizeof(struct pn533_target_type_a))
  886. return false;
  887. /* The lenght check of nfcid[] and ats[] are not being performed because
  888. the values are not being used */
  889. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  890. ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
  891. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
  892. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  893. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  894. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  895. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  896. return false;
  897. /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
  898. if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
  899. return false;
  900. return true;
  901. }
  902. static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
  903. int tgt_data_len)
  904. {
  905. struct pn533_target_type_a *tgt_type_a;
  906. tgt_type_a = (struct pn533_target_type_a *) tgt_data;
  907. if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
  908. return -EPROTO;
  909. switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
  910. case PN533_TYPE_A_SEL_PROT_MIFARE:
  911. nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
  912. break;
  913. case PN533_TYPE_A_SEL_PROT_ISO14443:
  914. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  915. break;
  916. case PN533_TYPE_A_SEL_PROT_DEP:
  917. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  918. break;
  919. case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
  920. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
  921. NFC_PROTO_NFC_DEP_MASK;
  922. break;
  923. }
  924. nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
  925. nfc_tgt->sel_res = tgt_type_a->sel_res;
  926. nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
  927. memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
  928. return 0;
  929. }
  930. struct pn533_target_felica {
  931. u8 pol_res;
  932. u8 opcode;
  933. u8 nfcid2[8];
  934. u8 pad[8];
  935. /* optional */
  936. u8 syst_code[];
  937. } __packed;
  938. #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
  939. #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
  940. static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
  941. int target_data_len)
  942. {
  943. if (target_data_len < sizeof(struct pn533_target_felica))
  944. return false;
  945. if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
  946. return false;
  947. return true;
  948. }
  949. static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
  950. int tgt_data_len)
  951. {
  952. struct pn533_target_felica *tgt_felica;
  953. tgt_felica = (struct pn533_target_felica *) tgt_data;
  954. if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
  955. return -EPROTO;
  956. if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
  957. tgt_felica->nfcid2[1] ==
  958. PN533_FELICA_SENSF_NFCID2_DEP_B2)
  959. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  960. else
  961. nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
  962. memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
  963. nfc_tgt->sensf_res_len = 9;
  964. return 0;
  965. }
  966. struct pn533_target_jewel {
  967. __be16 sens_res;
  968. u8 jewelid[4];
  969. } __packed;
  970. static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
  971. int target_data_len)
  972. {
  973. u8 ssd;
  974. u8 platconf;
  975. if (target_data_len < sizeof(struct pn533_target_jewel))
  976. return false;
  977. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  978. ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
  979. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
  980. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  981. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  982. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  983. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  984. return false;
  985. return true;
  986. }
  987. static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
  988. int tgt_data_len)
  989. {
  990. struct pn533_target_jewel *tgt_jewel;
  991. tgt_jewel = (struct pn533_target_jewel *) tgt_data;
  992. if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
  993. return -EPROTO;
  994. nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
  995. nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
  996. nfc_tgt->nfcid1_len = 4;
  997. memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
  998. return 0;
  999. }
  1000. struct pn533_type_b_prot_info {
  1001. u8 bitrate;
  1002. u8 fsci_type;
  1003. u8 fwi_adc_fo;
  1004. } __packed;
  1005. #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
  1006. #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
  1007. #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
  1008. struct pn533_type_b_sens_res {
  1009. u8 opcode;
  1010. u8 nfcid[4];
  1011. u8 appdata[4];
  1012. struct pn533_type_b_prot_info prot_info;
  1013. } __packed;
  1014. #define PN533_TYPE_B_OPC_SENSB_RES 0x50
  1015. struct pn533_target_type_b {
  1016. struct pn533_type_b_sens_res sensb_res;
  1017. u8 attrib_res_len;
  1018. u8 attrib_res[];
  1019. } __packed;
  1020. static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
  1021. int target_data_len)
  1022. {
  1023. if (target_data_len < sizeof(struct pn533_target_type_b))
  1024. return false;
  1025. if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
  1026. return false;
  1027. if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
  1028. PN533_TYPE_B_PROT_TYPE_RFU_MASK)
  1029. return false;
  1030. return true;
  1031. }
  1032. static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
  1033. int tgt_data_len)
  1034. {
  1035. struct pn533_target_type_b *tgt_type_b;
  1036. tgt_type_b = (struct pn533_target_type_b *) tgt_data;
  1037. if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
  1038. return -EPROTO;
  1039. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
  1040. return 0;
  1041. }
  1042. struct pn533_poll_response {
  1043. u8 nbtg;
  1044. u8 tg;
  1045. u8 target_data[];
  1046. } __packed;
  1047. static int pn533_target_found(struct pn533 *dev,
  1048. struct pn533_poll_response *resp, int resp_len)
  1049. {
  1050. int target_data_len;
  1051. struct nfc_target nfc_tgt;
  1052. int rc;
  1053. nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
  1054. dev->poll_mod_curr);
  1055. if (resp->tg != 1)
  1056. return -EPROTO;
  1057. memset(&nfc_tgt, 0, sizeof(struct nfc_target));
  1058. target_data_len = resp_len - sizeof(struct pn533_poll_response);
  1059. switch (dev->poll_mod_curr) {
  1060. case PN533_POLL_MOD_106KBPS_A:
  1061. rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
  1062. target_data_len);
  1063. break;
  1064. case PN533_POLL_MOD_212KBPS_FELICA:
  1065. case PN533_POLL_MOD_424KBPS_FELICA:
  1066. rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
  1067. target_data_len);
  1068. break;
  1069. case PN533_POLL_MOD_106KBPS_JEWEL:
  1070. rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
  1071. target_data_len);
  1072. break;
  1073. case PN533_POLL_MOD_847KBPS_B:
  1074. rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
  1075. target_data_len);
  1076. break;
  1077. default:
  1078. nfc_dev_err(&dev->interface->dev, "Unknown current poll"
  1079. " modulation");
  1080. return -EPROTO;
  1081. }
  1082. if (rc)
  1083. return rc;
  1084. if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
  1085. nfc_dev_dbg(&dev->interface->dev, "The target found does not"
  1086. " have the desired protocol");
  1087. return -EAGAIN;
  1088. }
  1089. nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
  1090. "0x%x", nfc_tgt.supported_protocols);
  1091. dev->tgt_available_prots = nfc_tgt.supported_protocols;
  1092. nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
  1093. return 0;
  1094. }
  1095. static inline void pn533_poll_next_mod(struct pn533 *dev)
  1096. {
  1097. dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
  1098. }
  1099. static void pn533_poll_reset_mod_list(struct pn533 *dev)
  1100. {
  1101. dev->poll_mod_count = 0;
  1102. }
  1103. static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
  1104. {
  1105. dev->poll_mod_active[dev->poll_mod_count] =
  1106. (struct pn533_poll_modulations *) &poll_mod[mod_index];
  1107. dev->poll_mod_count++;
  1108. }
  1109. static void pn533_poll_create_mod_list(struct pn533 *dev,
  1110. u32 im_protocols, u32 tm_protocols)
  1111. {
  1112. pn533_poll_reset_mod_list(dev);
  1113. if (im_protocols & NFC_PROTO_MIFARE_MASK
  1114. || im_protocols & NFC_PROTO_ISO14443_MASK
  1115. || im_protocols & NFC_PROTO_NFC_DEP_MASK)
  1116. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
  1117. if (im_protocols & NFC_PROTO_FELICA_MASK
  1118. || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
  1119. pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
  1120. pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
  1121. }
  1122. if (im_protocols & NFC_PROTO_JEWEL_MASK)
  1123. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
  1124. if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
  1125. pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
  1126. if (tm_protocols)
  1127. pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
  1128. }
  1129. static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
  1130. {
  1131. struct pn533_poll_response *resp;
  1132. int rc;
  1133. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1134. resp = (struct pn533_poll_response *) params;
  1135. if (resp->nbtg) {
  1136. rc = pn533_target_found(dev, resp, params_len);
  1137. /* We must stop the poll after a valid target found */
  1138. if (rc == 0) {
  1139. pn533_poll_reset_mod_list(dev);
  1140. return 0;
  1141. }
  1142. }
  1143. return -EAGAIN;
  1144. }
  1145. static int pn533_init_target_frame(struct pn533_frame *frame,
  1146. u8 *gb, size_t gb_len)
  1147. {
  1148. struct pn533_cmd_init_target *cmd;
  1149. size_t cmd_len;
  1150. u8 felica_params[18] = {0x1, 0xfe, /* DEP */
  1151. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
  1152. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  1153. 0xff, 0xff}; /* System code */
  1154. u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
  1155. 0x0, 0x0, 0x0,
  1156. 0x40}; /* SEL_RES for DEP */
  1157. cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
  1158. cmd = kzalloc(cmd_len, GFP_KERNEL);
  1159. if (cmd == NULL)
  1160. return -ENOMEM;
  1161. pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
  1162. /* DEP support only */
  1163. cmd->mode |= PN533_INIT_TARGET_DEP;
  1164. /* Felica params */
  1165. memcpy(cmd->felica, felica_params, 18);
  1166. get_random_bytes(cmd->felica + 2, 6);
  1167. /* NFCID3 */
  1168. memset(cmd->nfcid3, 0, 10);
  1169. memcpy(cmd->nfcid3, cmd->felica, 8);
  1170. /* MIFARE params */
  1171. memcpy(cmd->mifare, mifare_params, 6);
  1172. /* General bytes */
  1173. cmd->gb_len = gb_len;
  1174. memcpy(cmd->gb, gb, gb_len);
  1175. /* Len Tk */
  1176. cmd->gb[gb_len] = 0;
  1177. memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
  1178. frame->datalen += cmd_len;
  1179. pn533_tx_frame_finish(frame);
  1180. kfree(cmd);
  1181. return 0;
  1182. }
  1183. #define PN533_CMD_DATAEXCH_HEAD_LEN 1
  1184. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  1185. static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
  1186. u8 *params, int params_len)
  1187. {
  1188. struct sk_buff *skb_resp = arg;
  1189. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  1190. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1191. if (params_len < 0) {
  1192. nfc_dev_err(&dev->interface->dev,
  1193. "Error %d when starting as a target",
  1194. params_len);
  1195. return params_len;
  1196. }
  1197. if (params_len > 0 && params[0] != 0) {
  1198. nfc_tm_deactivated(dev->nfc_dev);
  1199. dev->tgt_mode = 0;
  1200. kfree_skb(skb_resp);
  1201. return 0;
  1202. }
  1203. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  1204. skb_pull(skb_resp,
  1205. PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
  1206. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
  1207. return nfc_tm_data_received(dev->nfc_dev, skb_resp);
  1208. }
  1209. static void pn533_wq_tg_get_data(struct work_struct *work)
  1210. {
  1211. struct pn533 *dev = container_of(work, struct pn533, tg_work);
  1212. struct pn533_frame *in_frame;
  1213. struct sk_buff *skb_resp;
  1214. size_t skb_resp_len;
  1215. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1216. skb_resp_len = PN533_FRAME_HEADER_LEN +
  1217. PN533_CMD_DATAEXCH_HEAD_LEN +
  1218. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1219. PN533_FRAME_TAIL_LEN;
  1220. skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
  1221. if (!skb_resp)
  1222. return;
  1223. in_frame = (struct pn533_frame *)skb_resp->data;
  1224. pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
  1225. pn533_tx_frame_finish(dev->out_frame);
  1226. pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
  1227. skb_resp_len,
  1228. pn533_tm_get_data_complete,
  1229. skb_resp);
  1230. return;
  1231. }
  1232. #define ATR_REQ_GB_OFFSET 17
  1233. static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
  1234. {
  1235. struct pn533_cmd_init_target_response *resp;
  1236. u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
  1237. size_t gb_len;
  1238. int rc;
  1239. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1240. if (params_len < 0) {
  1241. nfc_dev_err(&dev->interface->dev,
  1242. "Error %d when starting as a target",
  1243. params_len);
  1244. return params_len;
  1245. }
  1246. if (params_len < ATR_REQ_GB_OFFSET + 1)
  1247. return -EINVAL;
  1248. resp = (struct pn533_cmd_init_target_response *) params;
  1249. nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
  1250. resp->mode, params_len);
  1251. frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
  1252. if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
  1253. comm_mode = NFC_COMM_ACTIVE;
  1254. /* Again, only DEP */
  1255. if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
  1256. return -EOPNOTSUPP;
  1257. gb = resp->cmd + ATR_REQ_GB_OFFSET;
  1258. gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
  1259. rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
  1260. comm_mode, gb, gb_len);
  1261. if (rc < 0) {
  1262. nfc_dev_err(&dev->interface->dev,
  1263. "Error when signaling target activation");
  1264. return rc;
  1265. }
  1266. dev->tgt_mode = 1;
  1267. queue_work(dev->wq, &dev->tg_work);
  1268. return 0;
  1269. }
  1270. static void pn533_listen_mode_timer(unsigned long data)
  1271. {
  1272. struct pn533 *dev = (struct pn533 *) data;
  1273. nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
  1274. /* An ack will cancel the last issued command (poll) */
  1275. pn533_send_ack(dev, GFP_ATOMIC);
  1276. dev->cancel_listen = 1;
  1277. pn533_poll_next_mod(dev);
  1278. queue_work(dev->wq, &dev->poll_work);
  1279. }
  1280. static int pn533_poll_complete(struct pn533 *dev, void *arg,
  1281. u8 *params, int params_len)
  1282. {
  1283. struct pn533_poll_modulations *cur_mod;
  1284. int rc;
  1285. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1286. if (params_len == -ENOENT) {
  1287. if (dev->poll_mod_count != 0)
  1288. return 0;
  1289. nfc_dev_err(&dev->interface->dev,
  1290. "Polling operation has been stopped");
  1291. goto stop_poll;
  1292. }
  1293. if (params_len < 0) {
  1294. nfc_dev_err(&dev->interface->dev,
  1295. "Error %d when running poll", params_len);
  1296. goto stop_poll;
  1297. }
  1298. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1299. if (cur_mod->len == 0) {
  1300. del_timer(&dev->listen_timer);
  1301. return pn533_init_target_complete(dev, params, params_len);
  1302. } else {
  1303. rc = pn533_start_poll_complete(dev, params, params_len);
  1304. if (!rc)
  1305. return rc;
  1306. }
  1307. pn533_poll_next_mod(dev);
  1308. queue_work(dev->wq, &dev->poll_work);
  1309. return 0;
  1310. stop_poll:
  1311. pn533_poll_reset_mod_list(dev);
  1312. dev->poll_protocols = 0;
  1313. return 0;
  1314. }
  1315. static void pn533_build_poll_frame(struct pn533 *dev,
  1316. struct pn533_frame *frame,
  1317. struct pn533_poll_modulations *mod)
  1318. {
  1319. nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
  1320. if (mod->len == 0) {
  1321. /* Listen mode */
  1322. pn533_init_target_frame(frame, dev->gb, dev->gb_len);
  1323. } else {
  1324. /* Polling mode */
  1325. pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
  1326. memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
  1327. frame->datalen += mod->len;
  1328. pn533_tx_frame_finish(frame);
  1329. }
  1330. }
  1331. static int pn533_send_poll_frame(struct pn533 *dev)
  1332. {
  1333. struct pn533_poll_modulations *cur_mod;
  1334. int rc;
  1335. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1336. pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
  1337. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1338. PN533_NORMAL_FRAME_MAX_LEN,
  1339. pn533_poll_complete,
  1340. NULL);
  1341. if (rc)
  1342. nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
  1343. return rc;
  1344. }
  1345. static void pn533_wq_poll(struct work_struct *work)
  1346. {
  1347. struct pn533 *dev = container_of(work, struct pn533, poll_work);
  1348. struct pn533_poll_modulations *cur_mod;
  1349. int rc;
  1350. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1351. nfc_dev_dbg(&dev->interface->dev,
  1352. "%s cancel_listen %d modulation len %d",
  1353. __func__, dev->cancel_listen, cur_mod->len);
  1354. if (dev->cancel_listen == 1) {
  1355. dev->cancel_listen = 0;
  1356. usb_kill_urb(dev->in_urb);
  1357. }
  1358. rc = pn533_send_poll_frame(dev);
  1359. if (rc)
  1360. return;
  1361. if (cur_mod->len == 0 && dev->poll_mod_count > 1)
  1362. mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
  1363. return;
  1364. }
  1365. static int pn533_start_poll(struct nfc_dev *nfc_dev,
  1366. u32 im_protocols, u32 tm_protocols)
  1367. {
  1368. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1369. nfc_dev_dbg(&dev->interface->dev,
  1370. "%s: im protocols 0x%x tm protocols 0x%x",
  1371. __func__, im_protocols, tm_protocols);
  1372. if (dev->tgt_active_prot) {
  1373. nfc_dev_err(&dev->interface->dev,
  1374. "Cannot poll with a target already activated");
  1375. return -EBUSY;
  1376. }
  1377. if (dev->tgt_mode) {
  1378. nfc_dev_err(&dev->interface->dev,
  1379. "Cannot poll while already being activated");
  1380. return -EBUSY;
  1381. }
  1382. if (tm_protocols) {
  1383. dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
  1384. if (dev->gb == NULL)
  1385. tm_protocols = 0;
  1386. }
  1387. dev->poll_mod_curr = 0;
  1388. pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
  1389. dev->poll_protocols = im_protocols;
  1390. dev->listen_protocols = tm_protocols;
  1391. return pn533_send_poll_frame(dev);
  1392. }
  1393. static void pn533_stop_poll(struct nfc_dev *nfc_dev)
  1394. {
  1395. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1396. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1397. del_timer(&dev->listen_timer);
  1398. if (!dev->poll_mod_count) {
  1399. nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
  1400. " running");
  1401. return;
  1402. }
  1403. /* An ack will cancel the last issued command (poll) */
  1404. pn533_send_ack(dev, GFP_KERNEL);
  1405. /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
  1406. usb_kill_urb(dev->in_urb);
  1407. pn533_poll_reset_mod_list(dev);
  1408. }
  1409. static int pn533_activate_target_nfcdep(struct pn533 *dev)
  1410. {
  1411. struct pn533_cmd_activate_param param;
  1412. struct pn533_cmd_activate_response *resp;
  1413. u16 gt_len;
  1414. int rc;
  1415. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1416. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
  1417. param.tg = 1;
  1418. param.next = 0;
  1419. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
  1420. sizeof(struct pn533_cmd_activate_param));
  1421. dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
  1422. pn533_tx_frame_finish(dev->out_frame);
  1423. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1424. PN533_NORMAL_FRAME_MAX_LEN);
  1425. if (rc)
  1426. return rc;
  1427. resp = (struct pn533_cmd_activate_response *)
  1428. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1429. rc = resp->status & PN533_CMD_RET_MASK;
  1430. if (rc != PN533_CMD_RET_SUCCESS)
  1431. return -EIO;
  1432. /* ATR_RES general bytes are located at offset 16 */
  1433. gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
  1434. rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
  1435. return rc;
  1436. }
  1437. static int pn533_activate_target(struct nfc_dev *nfc_dev,
  1438. struct nfc_target *target, u32 protocol)
  1439. {
  1440. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1441. int rc;
  1442. nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
  1443. protocol);
  1444. if (dev->poll_mod_count) {
  1445. nfc_dev_err(&dev->interface->dev, "Cannot activate while"
  1446. " polling");
  1447. return -EBUSY;
  1448. }
  1449. if (dev->tgt_active_prot) {
  1450. nfc_dev_err(&dev->interface->dev, "There is already an active"
  1451. " target");
  1452. return -EBUSY;
  1453. }
  1454. if (!dev->tgt_available_prots) {
  1455. nfc_dev_err(&dev->interface->dev, "There is no available target"
  1456. " to activate");
  1457. return -EINVAL;
  1458. }
  1459. if (!(dev->tgt_available_prots & (1 << protocol))) {
  1460. nfc_dev_err(&dev->interface->dev, "The target does not support"
  1461. " the requested protocol %u", protocol);
  1462. return -EINVAL;
  1463. }
  1464. if (protocol == NFC_PROTO_NFC_DEP) {
  1465. rc = pn533_activate_target_nfcdep(dev);
  1466. if (rc) {
  1467. nfc_dev_err(&dev->interface->dev, "Error %d when"
  1468. " activating target with"
  1469. " NFC_DEP protocol", rc);
  1470. return rc;
  1471. }
  1472. }
  1473. dev->tgt_active_prot = protocol;
  1474. dev->tgt_available_prots = 0;
  1475. return 0;
  1476. }
  1477. static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
  1478. struct nfc_target *target)
  1479. {
  1480. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1481. u8 tg;
  1482. u8 status;
  1483. int rc;
  1484. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1485. if (!dev->tgt_active_prot) {
  1486. nfc_dev_err(&dev->interface->dev, "There is no active target");
  1487. return;
  1488. }
  1489. dev->tgt_active_prot = 0;
  1490. skb_queue_purge(&dev->resp_q);
  1491. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
  1492. tg = 1;
  1493. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
  1494. dev->out_frame->datalen += sizeof(u8);
  1495. pn533_tx_frame_finish(dev->out_frame);
  1496. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1497. PN533_NORMAL_FRAME_MAX_LEN);
  1498. if (rc) {
  1499. nfc_dev_err(&dev->interface->dev, "Error when sending release"
  1500. " command to the controller");
  1501. return;
  1502. }
  1503. status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
  1504. rc = status & PN533_CMD_RET_MASK;
  1505. if (rc != PN533_CMD_RET_SUCCESS)
  1506. nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
  1507. " the target", rc);
  1508. return;
  1509. }
  1510. static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
  1511. u8 *params, int params_len)
  1512. {
  1513. struct pn533_cmd_jump_dep_response *resp;
  1514. struct nfc_target nfc_target;
  1515. u8 target_gt_len;
  1516. int rc;
  1517. struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
  1518. u8 active = cmd->active;
  1519. kfree(arg);
  1520. if (params_len == -ENOENT) {
  1521. nfc_dev_dbg(&dev->interface->dev, "");
  1522. return 0;
  1523. }
  1524. if (params_len < 0) {
  1525. nfc_dev_err(&dev->interface->dev,
  1526. "Error %d when bringing DEP link up",
  1527. params_len);
  1528. return 0;
  1529. }
  1530. if (dev->tgt_available_prots &&
  1531. !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
  1532. nfc_dev_err(&dev->interface->dev,
  1533. "The target does not support DEP");
  1534. return -EINVAL;
  1535. }
  1536. resp = (struct pn533_cmd_jump_dep_response *) params;
  1537. rc = resp->status & PN533_CMD_RET_MASK;
  1538. if (rc != PN533_CMD_RET_SUCCESS) {
  1539. nfc_dev_err(&dev->interface->dev,
  1540. "Bringing DEP link up failed %d", rc);
  1541. return 0;
  1542. }
  1543. if (!dev->tgt_available_prots) {
  1544. nfc_dev_dbg(&dev->interface->dev, "Creating new target");
  1545. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1546. nfc_target.nfcid1_len = 10;
  1547. memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
  1548. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1549. if (rc)
  1550. return 0;
  1551. dev->tgt_available_prots = 0;
  1552. }
  1553. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1554. /* ATR_RES general bytes are located at offset 17 */
  1555. target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
  1556. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1557. resp->gt, target_gt_len);
  1558. if (rc == 0)
  1559. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1560. dev->nfc_dev->targets[0].idx,
  1561. !active, NFC_RF_INITIATOR);
  1562. return 0;
  1563. }
  1564. static int pn533_mod_to_baud(struct pn533 *dev)
  1565. {
  1566. switch (dev->poll_mod_curr) {
  1567. case PN533_POLL_MOD_106KBPS_A:
  1568. return 0;
  1569. case PN533_POLL_MOD_212KBPS_FELICA:
  1570. return 1;
  1571. case PN533_POLL_MOD_424KBPS_FELICA:
  1572. return 2;
  1573. default:
  1574. return -EINVAL;
  1575. }
  1576. }
  1577. #define PASSIVE_DATA_LEN 5
  1578. static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  1579. u8 comm_mode, u8* gb, size_t gb_len)
  1580. {
  1581. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1582. struct pn533_cmd_jump_dep *cmd;
  1583. u8 cmd_len, *data_ptr;
  1584. u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
  1585. int rc, baud;
  1586. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1587. if (dev->poll_mod_count) {
  1588. nfc_dev_err(&dev->interface->dev,
  1589. "Cannot bring the DEP link up while polling");
  1590. return -EBUSY;
  1591. }
  1592. if (dev->tgt_active_prot) {
  1593. nfc_dev_err(&dev->interface->dev,
  1594. "There is already an active target");
  1595. return -EBUSY;
  1596. }
  1597. baud = pn533_mod_to_baud(dev);
  1598. if (baud < 0) {
  1599. nfc_dev_err(&dev->interface->dev,
  1600. "Invalid curr modulation %d", dev->poll_mod_curr);
  1601. return baud;
  1602. }
  1603. cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
  1604. if (comm_mode == NFC_COMM_PASSIVE)
  1605. cmd_len += PASSIVE_DATA_LEN;
  1606. cmd = kzalloc(cmd_len, GFP_KERNEL);
  1607. if (cmd == NULL)
  1608. return -ENOMEM;
  1609. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
  1610. cmd->active = !comm_mode;
  1611. cmd->next = 0;
  1612. cmd->baud = baud;
  1613. data_ptr = cmd->data;
  1614. if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
  1615. memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
  1616. cmd->next |= 1;
  1617. data_ptr += PASSIVE_DATA_LEN;
  1618. }
  1619. if (gb != NULL && gb_len > 0) {
  1620. cmd->next |= 4; /* We have some Gi */
  1621. memcpy(data_ptr, gb, gb_len);
  1622. } else {
  1623. cmd->next = 0;
  1624. }
  1625. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
  1626. dev->out_frame->datalen += cmd_len;
  1627. pn533_tx_frame_finish(dev->out_frame);
  1628. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1629. PN533_NORMAL_FRAME_MAX_LEN,
  1630. pn533_in_dep_link_up_complete, cmd);
  1631. if (rc < 0)
  1632. kfree(cmd);
  1633. return rc;
  1634. }
  1635. static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
  1636. {
  1637. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1638. pn533_poll_reset_mod_list(dev);
  1639. if (dev->tgt_mode || dev->tgt_active_prot) {
  1640. pn533_send_ack(dev, GFP_KERNEL);
  1641. usb_kill_urb(dev->in_urb);
  1642. }
  1643. dev->tgt_active_prot = 0;
  1644. dev->tgt_mode = 0;
  1645. skb_queue_purge(&dev->resp_q);
  1646. return 0;
  1647. }
  1648. static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
  1649. bool target)
  1650. {
  1651. int payload_len = skb->len;
  1652. struct pn533_frame *out_frame;
  1653. u8 tg;
  1654. nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
  1655. payload_len);
  1656. if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  1657. /* TODO: Implement support to multi-part data exchange */
  1658. nfc_dev_err(&dev->interface->dev, "Data length greater than the"
  1659. " max allowed: %d",
  1660. PN533_CMD_DATAEXCH_DATA_MAXLEN);
  1661. return -ENOSYS;
  1662. }
  1663. skb_push(skb, PN533_FRAME_HEADER_LEN);
  1664. if (target == true) {
  1665. switch (dev->device_type) {
  1666. case PN533_DEVICE_PASORI:
  1667. if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
  1668. out_frame = (struct pn533_frame *) skb->data;
  1669. pn533_tx_frame_init(out_frame,
  1670. PN533_CMD_IN_COMM_THRU);
  1671. break;
  1672. }
  1673. default:
  1674. skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
  1675. out_frame = (struct pn533_frame *) skb->data;
  1676. pn533_tx_frame_init(out_frame,
  1677. PN533_CMD_IN_DATA_EXCHANGE);
  1678. tg = 1;
  1679. memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
  1680. &tg, sizeof(u8));
  1681. out_frame->datalen += sizeof(u8);
  1682. break;
  1683. }
  1684. } else {
  1685. skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
  1686. out_frame = (struct pn533_frame *) skb->data;
  1687. pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
  1688. }
  1689. /* The data is already in the out_frame, just update the datalen */
  1690. out_frame->datalen += payload_len;
  1691. pn533_tx_frame_finish(out_frame);
  1692. skb_put(skb, PN533_FRAME_TAIL_LEN);
  1693. return 0;
  1694. }
  1695. struct pn533_data_exchange_arg {
  1696. struct sk_buff *skb_resp;
  1697. struct sk_buff *skb_out;
  1698. data_exchange_cb_t cb;
  1699. void *cb_context;
  1700. };
  1701. static struct sk_buff *pn533_build_response(struct pn533 *dev)
  1702. {
  1703. struct sk_buff *skb, *tmp, *t;
  1704. unsigned int skb_len = 0, tmp_len = 0;
  1705. nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1706. if (skb_queue_empty(&dev->resp_q))
  1707. return NULL;
  1708. if (skb_queue_len(&dev->resp_q) == 1) {
  1709. skb = skb_dequeue(&dev->resp_q);
  1710. goto out;
  1711. }
  1712. skb_queue_walk_safe(&dev->resp_q, tmp, t)
  1713. skb_len += tmp->len;
  1714. nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
  1715. __func__, skb_len);
  1716. skb = alloc_skb(skb_len, GFP_KERNEL);
  1717. if (skb == NULL)
  1718. goto out;
  1719. skb_put(skb, skb_len);
  1720. skb_queue_walk_safe(&dev->resp_q, tmp, t) {
  1721. memcpy(skb->data + tmp_len, tmp->data, tmp->len);
  1722. tmp_len += tmp->len;
  1723. }
  1724. out:
  1725. skb_queue_purge(&dev->resp_q);
  1726. return skb;
  1727. }
  1728. static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
  1729. u8 *params, int params_len)
  1730. {
  1731. struct pn533_data_exchange_arg *arg = _arg;
  1732. struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
  1733. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  1734. int err = 0;
  1735. u8 status;
  1736. u8 cmd_ret;
  1737. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1738. dev_kfree_skb(arg->skb_out);
  1739. if (params_len < 0) { /* error */
  1740. err = params_len;
  1741. goto error;
  1742. }
  1743. status = params[0];
  1744. cmd_ret = status & PN533_CMD_RET_MASK;
  1745. if (cmd_ret != PN533_CMD_RET_SUCCESS) {
  1746. nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
  1747. " exchanging data", cmd_ret);
  1748. err = -EIO;
  1749. goto error;
  1750. }
  1751. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  1752. skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
  1753. skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
  1754. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
  1755. skb_queue_tail(&dev->resp_q, skb_resp);
  1756. if (status & PN533_CMD_MI_MASK) {
  1757. queue_work(dev->wq, &dev->mi_work);
  1758. return -EINPROGRESS;
  1759. }
  1760. skb = pn533_build_response(dev);
  1761. if (skb == NULL)
  1762. goto error;
  1763. arg->cb(arg->cb_context, skb, 0);
  1764. kfree(arg);
  1765. return 0;
  1766. error:
  1767. skb_queue_purge(&dev->resp_q);
  1768. dev_kfree_skb(skb_resp);
  1769. arg->cb(arg->cb_context, NULL, err);
  1770. kfree(arg);
  1771. return 0;
  1772. }
  1773. static int pn533_transceive(struct nfc_dev *nfc_dev,
  1774. struct nfc_target *target, struct sk_buff *skb,
  1775. data_exchange_cb_t cb, void *cb_context)
  1776. {
  1777. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1778. struct pn533_frame *out_frame, *in_frame;
  1779. struct pn533_data_exchange_arg *arg;
  1780. struct sk_buff *skb_resp;
  1781. int skb_resp_len;
  1782. int rc;
  1783. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1784. if (!dev->tgt_active_prot) {
  1785. nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
  1786. " there is no active target");
  1787. rc = -EINVAL;
  1788. goto error;
  1789. }
  1790. rc = pn533_build_tx_frame(dev, skb, true);
  1791. if (rc)
  1792. goto error;
  1793. skb_resp_len = PN533_FRAME_HEADER_LEN +
  1794. PN533_CMD_DATAEXCH_HEAD_LEN +
  1795. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1796. PN533_FRAME_TAIL_LEN;
  1797. skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
  1798. if (!skb_resp) {
  1799. rc = -ENOMEM;
  1800. goto error;
  1801. }
  1802. in_frame = (struct pn533_frame *) skb_resp->data;
  1803. out_frame = (struct pn533_frame *) skb->data;
  1804. arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
  1805. if (!arg) {
  1806. rc = -ENOMEM;
  1807. goto free_skb_resp;
  1808. }
  1809. arg->skb_resp = skb_resp;
  1810. arg->skb_out = skb;
  1811. arg->cb = cb;
  1812. arg->cb_context = cb_context;
  1813. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
  1814. pn533_data_exchange_complete, arg);
  1815. if (rc) {
  1816. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1817. " perform data_exchange", rc);
  1818. goto free_arg;
  1819. }
  1820. return 0;
  1821. free_arg:
  1822. kfree(arg);
  1823. free_skb_resp:
  1824. kfree_skb(skb_resp);
  1825. error:
  1826. kfree_skb(skb);
  1827. return rc;
  1828. }
  1829. static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
  1830. u8 *params, int params_len)
  1831. {
  1832. struct sk_buff *skb_out = arg;
  1833. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1834. dev_kfree_skb(skb_out);
  1835. if (params_len < 0) {
  1836. nfc_dev_err(&dev->interface->dev,
  1837. "Error %d when sending data",
  1838. params_len);
  1839. return params_len;
  1840. }
  1841. if (params_len > 0 && params[0] != 0) {
  1842. nfc_tm_deactivated(dev->nfc_dev);
  1843. dev->tgt_mode = 0;
  1844. return 0;
  1845. }
  1846. queue_work(dev->wq, &dev->tg_work);
  1847. return 0;
  1848. }
  1849. static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  1850. {
  1851. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1852. struct pn533_frame *out_frame;
  1853. int rc;
  1854. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1855. rc = pn533_build_tx_frame(dev, skb, false);
  1856. if (rc)
  1857. goto error;
  1858. out_frame = (struct pn533_frame *) skb->data;
  1859. rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
  1860. PN533_NORMAL_FRAME_MAX_LEN,
  1861. pn533_tm_send_complete, skb);
  1862. if (rc) {
  1863. nfc_dev_err(&dev->interface->dev,
  1864. "Error %d when trying to send data", rc);
  1865. goto error;
  1866. }
  1867. return 0;
  1868. error:
  1869. kfree_skb(skb);
  1870. return rc;
  1871. }
  1872. static void pn533_wq_mi_recv(struct work_struct *work)
  1873. {
  1874. struct pn533 *dev = container_of(work, struct pn533, mi_work);
  1875. struct sk_buff *skb_cmd;
  1876. struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
  1877. struct pn533_frame *out_frame, *in_frame;
  1878. struct sk_buff *skb_resp;
  1879. int skb_resp_len;
  1880. int rc;
  1881. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1882. /* This is a zero payload size skb */
  1883. skb_cmd = alloc_skb(PN533_FRAME_HEADER_LEN +
  1884. PN533_CMD_DATAEXCH_HEAD_LEN +
  1885. PN533_FRAME_TAIL_LEN,
  1886. GFP_KERNEL);
  1887. if (skb_cmd == NULL)
  1888. goto error_cmd;
  1889. skb_reserve(skb_cmd,
  1890. PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
  1891. rc = pn533_build_tx_frame(dev, skb_cmd, true);
  1892. if (rc)
  1893. goto error_frame;
  1894. skb_resp_len = PN533_FRAME_HEADER_LEN +
  1895. PN533_CMD_DATAEXCH_HEAD_LEN +
  1896. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1897. PN533_FRAME_TAIL_LEN;
  1898. skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
  1899. if (!skb_resp) {
  1900. rc = -ENOMEM;
  1901. goto error_frame;
  1902. }
  1903. in_frame = (struct pn533_frame *) skb_resp->data;
  1904. out_frame = (struct pn533_frame *) skb_cmd->data;
  1905. arg->skb_resp = skb_resp;
  1906. arg->skb_out = skb_cmd;
  1907. rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
  1908. skb_resp_len,
  1909. pn533_data_exchange_complete,
  1910. dev->cmd_complete_arg);
  1911. if (!rc)
  1912. return;
  1913. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1914. " perform data_exchange", rc);
  1915. kfree_skb(skb_resp);
  1916. error_frame:
  1917. kfree_skb(skb_cmd);
  1918. error_cmd:
  1919. pn533_send_ack(dev, GFP_KERNEL);
  1920. kfree(arg);
  1921. queue_work(dev->wq, &dev->cmd_work);
  1922. }
  1923. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  1924. u8 cfgdata_len)
  1925. {
  1926. int rc;
  1927. u8 *params;
  1928. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1929. pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
  1930. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1931. params[0] = cfgitem;
  1932. memcpy(&params[1], cfgdata, cfgdata_len);
  1933. dev->out_frame->datalen += (1 + cfgdata_len);
  1934. pn533_tx_frame_finish(dev->out_frame);
  1935. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1936. PN533_NORMAL_FRAME_MAX_LEN);
  1937. return rc;
  1938. }
  1939. static int pn533_fw_reset(struct pn533 *dev)
  1940. {
  1941. int rc;
  1942. u8 *params;
  1943. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1944. pn533_tx_frame_init(dev->out_frame, 0x18);
  1945. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1946. params[0] = 0x1;
  1947. dev->out_frame->datalen += 1;
  1948. pn533_tx_frame_finish(dev->out_frame);
  1949. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1950. PN533_NORMAL_FRAME_MAX_LEN);
  1951. return 0;
  1952. }
  1953. static struct nfc_ops pn533_nfc_ops = {
  1954. .dev_up = NULL,
  1955. .dev_down = NULL,
  1956. .dep_link_up = pn533_dep_link_up,
  1957. .dep_link_down = pn533_dep_link_down,
  1958. .start_poll = pn533_start_poll,
  1959. .stop_poll = pn533_stop_poll,
  1960. .activate_target = pn533_activate_target,
  1961. .deactivate_target = pn533_deactivate_target,
  1962. .im_transceive = pn533_transceive,
  1963. .tm_send = pn533_tm_send,
  1964. };
  1965. static int pn533_setup(struct pn533 *dev)
  1966. {
  1967. struct pn533_config_max_retries max_retries;
  1968. struct pn533_config_timing timing;
  1969. u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
  1970. int rc;
  1971. switch (dev->device_type) {
  1972. case PN533_DEVICE_STD:
  1973. max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
  1974. max_retries.mx_rty_psl = 2;
  1975. max_retries.mx_rty_passive_act =
  1976. PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1977. timing.rfu = PN533_CONFIG_TIMING_102;
  1978. timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
  1979. timing.dep_timeout = PN533_CONFIG_TIMING_409;
  1980. break;
  1981. case PN533_DEVICE_PASORI:
  1982. max_retries.mx_rty_atr = 0x2;
  1983. max_retries.mx_rty_psl = 0x1;
  1984. max_retries.mx_rty_passive_act =
  1985. PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1986. timing.rfu = PN533_CONFIG_TIMING_102;
  1987. timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
  1988. timing.dep_timeout = PN533_CONFIG_TIMING_204;
  1989. break;
  1990. default:
  1991. nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
  1992. dev->device_type);
  1993. return -EINVAL;
  1994. }
  1995. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  1996. (u8 *)&max_retries, sizeof(max_retries));
  1997. if (rc) {
  1998. nfc_dev_err(&dev->interface->dev,
  1999. "Error on setting MAX_RETRIES config");
  2000. return rc;
  2001. }
  2002. rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
  2003. (u8 *)&timing, sizeof(timing));
  2004. if (rc) {
  2005. nfc_dev_err(&dev->interface->dev,
  2006. "Error on setting RF timings");
  2007. return rc;
  2008. }
  2009. switch (dev->device_type) {
  2010. case PN533_DEVICE_STD:
  2011. break;
  2012. case PN533_DEVICE_PASORI:
  2013. pn533_fw_reset(dev);
  2014. rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
  2015. pasori_cfg, 3);
  2016. if (rc) {
  2017. nfc_dev_err(&dev->interface->dev,
  2018. "Error while settings PASORI config");
  2019. return rc;
  2020. }
  2021. pn533_fw_reset(dev);
  2022. break;
  2023. }
  2024. return 0;
  2025. }
  2026. static int pn533_probe(struct usb_interface *interface,
  2027. const struct usb_device_id *id)
  2028. {
  2029. struct pn533_fw_version *fw_ver;
  2030. struct pn533 *dev;
  2031. struct usb_host_interface *iface_desc;
  2032. struct usb_endpoint_descriptor *endpoint;
  2033. int in_endpoint = 0;
  2034. int out_endpoint = 0;
  2035. int rc = -ENOMEM;
  2036. int i;
  2037. u32 protocols;
  2038. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2039. if (!dev)
  2040. return -ENOMEM;
  2041. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  2042. dev->interface = interface;
  2043. mutex_init(&dev->cmd_lock);
  2044. iface_desc = interface->cur_altsetting;
  2045. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  2046. endpoint = &iface_desc->endpoint[i].desc;
  2047. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
  2048. in_endpoint = endpoint->bEndpointAddress;
  2049. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
  2050. out_endpoint = endpoint->bEndpointAddress;
  2051. }
  2052. if (!in_endpoint || !out_endpoint) {
  2053. nfc_dev_err(&interface->dev, "Could not find bulk-in or"
  2054. " bulk-out endpoint");
  2055. rc = -ENODEV;
  2056. goto error;
  2057. }
  2058. dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
  2059. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  2060. dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
  2061. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  2062. if (!dev->in_frame || !dev->out_frame ||
  2063. !dev->in_urb || !dev->out_urb)
  2064. goto error;
  2065. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  2066. usb_rcvbulkpipe(dev->udev, in_endpoint),
  2067. NULL, 0, NULL, dev);
  2068. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  2069. usb_sndbulkpipe(dev->udev, out_endpoint),
  2070. NULL, 0,
  2071. pn533_send_complete, dev);
  2072. INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
  2073. INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
  2074. INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
  2075. INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
  2076. INIT_WORK(&dev->poll_work, pn533_wq_poll);
  2077. dev->wq = alloc_ordered_workqueue("pn533", 0);
  2078. if (dev->wq == NULL)
  2079. goto error;
  2080. init_timer(&dev->listen_timer);
  2081. dev->listen_timer.data = (unsigned long) dev;
  2082. dev->listen_timer.function = pn533_listen_mode_timer;
  2083. skb_queue_head_init(&dev->resp_q);
  2084. INIT_LIST_HEAD(&dev->cmd_queue);
  2085. usb_set_intfdata(interface, dev);
  2086. pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
  2087. pn533_tx_frame_finish(dev->out_frame);
  2088. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  2089. PN533_NORMAL_FRAME_MAX_LEN);
  2090. if (rc)
  2091. goto destroy_wq;
  2092. fw_ver = (struct pn533_fw_version *)
  2093. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  2094. nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
  2095. " attached", fw_ver->ver, fw_ver->rev);
  2096. dev->device_type = id->driver_info;
  2097. switch (dev->device_type) {
  2098. case PN533_DEVICE_STD:
  2099. protocols = PN533_ALL_PROTOCOLS;
  2100. break;
  2101. case PN533_DEVICE_PASORI:
  2102. protocols = PN533_NO_TYPE_B_PROTOCOLS;
  2103. break;
  2104. default:
  2105. nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
  2106. dev->device_type);
  2107. rc = -EINVAL;
  2108. goto destroy_wq;
  2109. }
  2110. dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
  2111. PN533_FRAME_HEADER_LEN +
  2112. PN533_CMD_DATAEXCH_HEAD_LEN,
  2113. PN533_FRAME_TAIL_LEN);
  2114. if (!dev->nfc_dev)
  2115. goto destroy_wq;
  2116. nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
  2117. nfc_set_drvdata(dev->nfc_dev, dev);
  2118. rc = nfc_register_device(dev->nfc_dev);
  2119. if (rc)
  2120. goto free_nfc_dev;
  2121. rc = pn533_setup(dev);
  2122. if (rc)
  2123. goto unregister_nfc_dev;
  2124. return 0;
  2125. unregister_nfc_dev:
  2126. nfc_unregister_device(dev->nfc_dev);
  2127. free_nfc_dev:
  2128. nfc_free_device(dev->nfc_dev);
  2129. destroy_wq:
  2130. destroy_workqueue(dev->wq);
  2131. error:
  2132. kfree(dev->in_frame);
  2133. usb_free_urb(dev->in_urb);
  2134. kfree(dev->out_frame);
  2135. usb_free_urb(dev->out_urb);
  2136. kfree(dev);
  2137. return rc;
  2138. }
  2139. static void pn533_disconnect(struct usb_interface *interface)
  2140. {
  2141. struct pn533 *dev;
  2142. struct pn533_cmd *cmd, *n;
  2143. dev = usb_get_intfdata(interface);
  2144. usb_set_intfdata(interface, NULL);
  2145. nfc_unregister_device(dev->nfc_dev);
  2146. nfc_free_device(dev->nfc_dev);
  2147. usb_kill_urb(dev->in_urb);
  2148. usb_kill_urb(dev->out_urb);
  2149. destroy_workqueue(dev->wq);
  2150. skb_queue_purge(&dev->resp_q);
  2151. del_timer(&dev->listen_timer);
  2152. list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
  2153. list_del(&cmd->queue);
  2154. kfree(cmd);
  2155. }
  2156. kfree(dev->in_frame);
  2157. usb_free_urb(dev->in_urb);
  2158. kfree(dev->out_frame);
  2159. usb_free_urb(dev->out_urb);
  2160. kfree(dev);
  2161. nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
  2162. }
  2163. static struct usb_driver pn533_driver = {
  2164. .name = "pn533",
  2165. .probe = pn533_probe,
  2166. .disconnect = pn533_disconnect,
  2167. .id_table = pn533_table,
  2168. };
  2169. module_usb_driver(pn533_driver);
  2170. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
  2171. " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  2172. MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
  2173. MODULE_VERSION(VERSION);
  2174. MODULE_LICENSE("GPL");