pn533.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. * Copyright (C) 2012-2013 Tieto Poland
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/usb.h>
  23. #include <linux/nfc.h>
  24. #include <linux/netdevice.h>
  25. #include <net/nfc/nfc.h>
  26. #define VERSION "0.2"
  27. #define PN533_VENDOR_ID 0x4CC
  28. #define PN533_PRODUCT_ID 0x2533
  29. #define SCM_VENDOR_ID 0x4E6
  30. #define SCL3711_PRODUCT_ID 0x5591
  31. #define SONY_VENDOR_ID 0x054c
  32. #define PASORI_PRODUCT_ID 0x02e1
  33. #define ACS_VENDOR_ID 0x072f
  34. #define ACR122U_PRODUCT_ID 0x2200
  35. #define PN533_DEVICE_STD 0x1
  36. #define PN533_DEVICE_PASORI 0x2
  37. #define PN533_DEVICE_ACR122U 0x3
  38. #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
  39. NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
  40. NFC_PROTO_NFC_DEP_MASK |\
  41. NFC_PROTO_ISO14443_B_MASK)
  42. #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
  43. NFC_PROTO_MIFARE_MASK | \
  44. NFC_PROTO_FELICA_MASK | \
  45. NFC_PROTO_ISO14443_MASK | \
  46. NFC_PROTO_NFC_DEP_MASK)
  47. static const struct usb_device_id pn533_table[] = {
  48. { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID),
  49. .driver_info = PN533_DEVICE_STD },
  50. { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID),
  51. .driver_info = PN533_DEVICE_STD },
  52. { USB_DEVICE(SONY_VENDOR_ID, PASORI_PRODUCT_ID),
  53. .driver_info = PN533_DEVICE_PASORI },
  54. { USB_DEVICE(ACS_VENDOR_ID, ACR122U_PRODUCT_ID),
  55. .driver_info = PN533_DEVICE_ACR122U },
  56. { }
  57. };
  58. MODULE_DEVICE_TABLE(usb, pn533_table);
  59. /* How much time we spend listening for initiators */
  60. #define PN533_LISTEN_TIME 2
  61. /* Delay between each poll frame (ms) */
  62. #define PN533_POLL_INTERVAL 10
  63. /* Standard pn533 frame definitions (standard and extended)*/
  64. #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
  65. + 2) /* data[0] TFI, data[1] CC */
  66. #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
  67. #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
  68. + 2) /* data[0] TFI, data[1] CC */
  69. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  70. #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */
  71. /*
  72. * Max extended frame payload len, excluding TFI and CC
  73. * which are already in PN533_FRAME_HEADER_LEN.
  74. */
  75. #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
  76. #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
  77. Postamble (1) */
  78. #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
  79. #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
  80. /* Half start code (3), LEN (4) should be 0xffff for extended frame */
  81. #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
  82. && (hdr)->datalen_checksum == 0xFF)
  83. #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
  84. /* start of frame */
  85. #define PN533_STD_FRAME_SOF 0x00FF
  86. /* standard frame identifier: in/out/error */
  87. #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
  88. #define PN533_STD_FRAME_DIR_OUT 0xD4
  89. #define PN533_STD_FRAME_DIR_IN 0xD5
  90. /* ACS ACR122 pn533 frame definitions */
  91. #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
  92. + 2)
  93. #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
  94. #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
  95. + 2)
  96. #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
  97. #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
  98. /* CCID messages types */
  99. #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
  100. #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
  101. #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
  102. /* PN533 Commands */
  103. #define PN533_FRAME_CMD(f) (f->data[1])
  104. #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
  105. #define PN533_CMD_RF_CONFIGURATION 0x32
  106. #define PN533_CMD_IN_DATA_EXCHANGE 0x40
  107. #define PN533_CMD_IN_COMM_THRU 0x42
  108. #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
  109. #define PN533_CMD_IN_ATR 0x50
  110. #define PN533_CMD_IN_RELEASE 0x52
  111. #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
  112. #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
  113. #define PN533_CMD_TG_GET_DATA 0x86
  114. #define PN533_CMD_TG_SET_DATA 0x8e
  115. #define PN533_CMD_TG_SET_META_DATA 0x94
  116. #define PN533_CMD_UNDEF 0xff
  117. #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
  118. /* PN533 Return codes */
  119. #define PN533_CMD_RET_MASK 0x3F
  120. #define PN533_CMD_MI_MASK 0x40
  121. #define PN533_CMD_RET_SUCCESS 0x00
  122. struct pn533;
  123. typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
  124. struct sk_buff *resp);
  125. /* structs for pn533 commands */
  126. /* PN533_CMD_GET_FIRMWARE_VERSION */
  127. struct pn533_fw_version {
  128. u8 ic;
  129. u8 ver;
  130. u8 rev;
  131. u8 support;
  132. };
  133. /* PN533_CMD_RF_CONFIGURATION */
  134. #define PN533_CFGITEM_RF_FIELD 0x01
  135. #define PN533_CFGITEM_TIMING 0x02
  136. #define PN533_CFGITEM_MAX_RETRIES 0x05
  137. #define PN533_CFGITEM_PASORI 0x82
  138. #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
  139. #define PN533_CFGITEM_RF_FIELD_ON 0x1
  140. #define PN533_CFGITEM_RF_FIELD_OFF 0x0
  141. #define PN533_CONFIG_TIMING_102 0xb
  142. #define PN533_CONFIG_TIMING_204 0xc
  143. #define PN533_CONFIG_TIMING_409 0xd
  144. #define PN533_CONFIG_TIMING_819 0xe
  145. #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
  146. #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
  147. struct pn533_config_max_retries {
  148. u8 mx_rty_atr;
  149. u8 mx_rty_psl;
  150. u8 mx_rty_passive_act;
  151. } __packed;
  152. struct pn533_config_timing {
  153. u8 rfu;
  154. u8 atr_res_timeout;
  155. u8 dep_timeout;
  156. } __packed;
  157. /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
  158. /* felica commands opcode */
  159. #define PN533_FELICA_OPC_SENSF_REQ 0
  160. #define PN533_FELICA_OPC_SENSF_RES 1
  161. /* felica SENSF_REQ parameters */
  162. #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
  163. #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
  164. #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
  165. #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
  166. /* type B initiator_data values */
  167. #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
  168. #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
  169. #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
  170. union pn533_cmd_poll_initdata {
  171. struct {
  172. u8 afi;
  173. u8 polling_method;
  174. } __packed type_b;
  175. struct {
  176. u8 opcode;
  177. __be16 sc;
  178. u8 rc;
  179. u8 tsn;
  180. } __packed felica;
  181. };
  182. /* Poll modulations */
  183. enum {
  184. PN533_POLL_MOD_106KBPS_A,
  185. PN533_POLL_MOD_212KBPS_FELICA,
  186. PN533_POLL_MOD_424KBPS_FELICA,
  187. PN533_POLL_MOD_106KBPS_JEWEL,
  188. PN533_POLL_MOD_847KBPS_B,
  189. PN533_LISTEN_MOD,
  190. __PN533_POLL_MOD_AFTER_LAST,
  191. };
  192. #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
  193. struct pn533_poll_modulations {
  194. struct {
  195. u8 maxtg;
  196. u8 brty;
  197. union pn533_cmd_poll_initdata initiator_data;
  198. } __packed data;
  199. u8 len;
  200. };
  201. static const struct pn533_poll_modulations poll_mod[] = {
  202. [PN533_POLL_MOD_106KBPS_A] = {
  203. .data = {
  204. .maxtg = 1,
  205. .brty = 0,
  206. },
  207. .len = 2,
  208. },
  209. [PN533_POLL_MOD_212KBPS_FELICA] = {
  210. .data = {
  211. .maxtg = 1,
  212. .brty = 1,
  213. .initiator_data.felica = {
  214. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  215. .sc = PN533_FELICA_SENSF_SC_ALL,
  216. .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
  217. .tsn = 0x03,
  218. },
  219. },
  220. .len = 7,
  221. },
  222. [PN533_POLL_MOD_424KBPS_FELICA] = {
  223. .data = {
  224. .maxtg = 1,
  225. .brty = 2,
  226. .initiator_data.felica = {
  227. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  228. .sc = PN533_FELICA_SENSF_SC_ALL,
  229. .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
  230. .tsn = 0x03,
  231. },
  232. },
  233. .len = 7,
  234. },
  235. [PN533_POLL_MOD_106KBPS_JEWEL] = {
  236. .data = {
  237. .maxtg = 1,
  238. .brty = 4,
  239. },
  240. .len = 2,
  241. },
  242. [PN533_POLL_MOD_847KBPS_B] = {
  243. .data = {
  244. .maxtg = 1,
  245. .brty = 8,
  246. .initiator_data.type_b = {
  247. .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
  248. .polling_method =
  249. PN533_TYPE_B_POLL_METHOD_TIMESLOT,
  250. },
  251. },
  252. .len = 3,
  253. },
  254. [PN533_LISTEN_MOD] = {
  255. .len = 0,
  256. },
  257. };
  258. /* PN533_CMD_IN_ATR */
  259. struct pn533_cmd_activate_response {
  260. u8 status;
  261. u8 nfcid3t[10];
  262. u8 didt;
  263. u8 bst;
  264. u8 brt;
  265. u8 to;
  266. u8 ppt;
  267. /* optional */
  268. u8 gt[];
  269. } __packed;
  270. struct pn533_cmd_jump_dep_response {
  271. u8 status;
  272. u8 tg;
  273. u8 nfcid3t[10];
  274. u8 didt;
  275. u8 bst;
  276. u8 brt;
  277. u8 to;
  278. u8 ppt;
  279. /* optional */
  280. u8 gt[];
  281. } __packed;
  282. /* PN533_TG_INIT_AS_TARGET */
  283. #define PN533_INIT_TARGET_PASSIVE 0x1
  284. #define PN533_INIT_TARGET_DEP 0x2
  285. #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
  286. #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
  287. #define PN533_INIT_TARGET_RESP_DEP 0x4
  288. enum pn533_protocol_type {
  289. PN533_PROTO_REQ_ACK_RESP = 0,
  290. PN533_PROTO_REQ_RESP
  291. };
  292. struct pn533 {
  293. struct usb_device *udev;
  294. struct usb_interface *interface;
  295. struct nfc_dev *nfc_dev;
  296. u32 device_type;
  297. enum pn533_protocol_type protocol_type;
  298. struct urb *out_urb;
  299. struct urb *in_urb;
  300. struct sk_buff_head resp_q;
  301. struct sk_buff_head fragment_skb;
  302. struct workqueue_struct *wq;
  303. struct work_struct cmd_work;
  304. struct work_struct cmd_complete_work;
  305. struct delayed_work poll_work;
  306. struct work_struct mi_rx_work;
  307. struct work_struct mi_tx_work;
  308. struct work_struct mi_tm_rx_work;
  309. struct work_struct mi_tm_tx_work;
  310. struct work_struct tg_work;
  311. struct work_struct rf_work;
  312. struct list_head cmd_queue;
  313. struct pn533_cmd *cmd;
  314. u8 cmd_pending;
  315. struct mutex cmd_lock; /* protects cmd queue */
  316. void *cmd_complete_mi_arg;
  317. void *cmd_complete_dep_arg;
  318. struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
  319. u8 poll_mod_count;
  320. u8 poll_mod_curr;
  321. u8 poll_dep;
  322. u32 poll_protocols;
  323. u32 listen_protocols;
  324. struct timer_list listen_timer;
  325. int cancel_listen;
  326. u8 *gb;
  327. size_t gb_len;
  328. u8 tgt_available_prots;
  329. u8 tgt_active_prot;
  330. u8 tgt_mode;
  331. struct pn533_frame_ops *ops;
  332. };
  333. struct pn533_cmd {
  334. struct list_head queue;
  335. u8 code;
  336. int status;
  337. struct sk_buff *req;
  338. struct sk_buff *resp;
  339. int resp_len;
  340. pn533_send_async_complete_t complete_cb;
  341. void *complete_cb_context;
  342. };
  343. struct pn533_std_frame {
  344. u8 preamble;
  345. __be16 start_frame;
  346. u8 datalen;
  347. u8 datalen_checksum;
  348. u8 data[];
  349. } __packed;
  350. struct pn533_ext_frame { /* Extended Information frame */
  351. u8 preamble;
  352. __be16 start_frame;
  353. __be16 eif_flag; /* fixed to 0xFFFF */
  354. __be16 datalen;
  355. u8 datalen_checksum;
  356. u8 data[];
  357. } __packed;
  358. struct pn533_frame_ops {
  359. void (*tx_frame_init)(void *frame, u8 cmd_code);
  360. void (*tx_frame_finish)(void *frame);
  361. void (*tx_update_payload_len)(void *frame, int len);
  362. int tx_header_len;
  363. int tx_tail_len;
  364. bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
  365. int (*rx_frame_size)(void *frame);
  366. int rx_header_len;
  367. int rx_tail_len;
  368. int max_payload_len;
  369. u8 (*get_cmd_code)(void *frame);
  370. };
  371. struct pn533_acr122_ccid_hdr {
  372. u8 type;
  373. u32 datalen;
  374. u8 slot;
  375. u8 seq;
  376. u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
  377. byte for reposnse msg */
  378. u8 data[]; /* payload */
  379. } __packed;
  380. struct pn533_acr122_apdu_hdr {
  381. u8 class;
  382. u8 ins;
  383. u8 p1;
  384. u8 p2;
  385. } __packed;
  386. struct pn533_acr122_tx_frame {
  387. struct pn533_acr122_ccid_hdr ccid;
  388. struct pn533_acr122_apdu_hdr apdu;
  389. u8 datalen;
  390. u8 data[]; /* pn533 frame: TFI ... */
  391. } __packed;
  392. struct pn533_acr122_rx_frame {
  393. struct pn533_acr122_ccid_hdr ccid;
  394. u8 data[]; /* pn533 frame : TFI ... */
  395. } __packed;
  396. static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
  397. {
  398. struct pn533_acr122_tx_frame *frame = _frame;
  399. frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
  400. frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
  401. sizeof(datalen) */
  402. frame->ccid.slot = 0;
  403. frame->ccid.seq = 0;
  404. frame->ccid.params[0] = 0;
  405. frame->ccid.params[1] = 0;
  406. frame->ccid.params[2] = 0;
  407. frame->data[0] = PN533_STD_FRAME_DIR_OUT;
  408. frame->data[1] = cmd_code;
  409. frame->datalen = 2; /* data[0] + data[1] */
  410. frame->apdu.class = 0xFF;
  411. frame->apdu.ins = 0;
  412. frame->apdu.p1 = 0;
  413. frame->apdu.p2 = 0;
  414. }
  415. static void pn533_acr122_tx_frame_finish(void *_frame)
  416. {
  417. struct pn533_acr122_tx_frame *frame = _frame;
  418. frame->ccid.datalen += frame->datalen;
  419. }
  420. static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
  421. {
  422. struct pn533_acr122_tx_frame *frame = _frame;
  423. frame->datalen += len;
  424. }
  425. static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
  426. {
  427. struct pn533_acr122_rx_frame *frame = _frame;
  428. if (frame->ccid.type != 0x83)
  429. return false;
  430. if (!frame->ccid.datalen)
  431. return false;
  432. if (frame->data[frame->ccid.datalen - 2] == 0x63)
  433. return false;
  434. return true;
  435. }
  436. static int pn533_acr122_rx_frame_size(void *frame)
  437. {
  438. struct pn533_acr122_rx_frame *f = frame;
  439. /* f->ccid.datalen already includes tail length */
  440. return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
  441. }
  442. static u8 pn533_acr122_get_cmd_code(void *frame)
  443. {
  444. struct pn533_acr122_rx_frame *f = frame;
  445. return PN533_FRAME_CMD(f);
  446. }
  447. static struct pn533_frame_ops pn533_acr122_frame_ops = {
  448. .tx_frame_init = pn533_acr122_tx_frame_init,
  449. .tx_frame_finish = pn533_acr122_tx_frame_finish,
  450. .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
  451. .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
  452. .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
  453. .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
  454. .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
  455. .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
  456. .rx_frame_size = pn533_acr122_rx_frame_size,
  457. .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
  458. .get_cmd_code = pn533_acr122_get_cmd_code,
  459. };
  460. /* The rule: value(high byte) + value(low byte) + checksum = 0 */
  461. static inline u8 pn533_ext_checksum(u16 value)
  462. {
  463. return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
  464. }
  465. /* The rule: value + checksum = 0 */
  466. static inline u8 pn533_std_checksum(u8 value)
  467. {
  468. return ~value + 1;
  469. }
  470. /* The rule: sum(data elements) + checksum = 0 */
  471. static u8 pn533_std_data_checksum(u8 *data, int datalen)
  472. {
  473. u8 sum = 0;
  474. int i;
  475. for (i = 0; i < datalen; i++)
  476. sum += data[i];
  477. return pn533_std_checksum(sum);
  478. }
  479. static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
  480. {
  481. struct pn533_std_frame *frame = _frame;
  482. frame->preamble = 0;
  483. frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
  484. PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
  485. PN533_FRAME_CMD(frame) = cmd_code;
  486. frame->datalen = 2;
  487. }
  488. static void pn533_std_tx_frame_finish(void *_frame)
  489. {
  490. struct pn533_std_frame *frame = _frame;
  491. frame->datalen_checksum = pn533_std_checksum(frame->datalen);
  492. PN533_STD_FRAME_CHECKSUM(frame) =
  493. pn533_std_data_checksum(frame->data, frame->datalen);
  494. PN533_STD_FRAME_POSTAMBLE(frame) = 0;
  495. }
  496. static void pn533_std_tx_update_payload_len(void *_frame, int len)
  497. {
  498. struct pn533_std_frame *frame = _frame;
  499. frame->datalen += len;
  500. }
  501. static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
  502. {
  503. u8 checksum;
  504. struct pn533_std_frame *stdf = _frame;
  505. if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
  506. return false;
  507. if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
  508. /* Standard frame code */
  509. dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
  510. checksum = pn533_std_checksum(stdf->datalen);
  511. if (checksum != stdf->datalen_checksum)
  512. return false;
  513. checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
  514. if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
  515. return false;
  516. } else {
  517. /* Extended */
  518. struct pn533_ext_frame *eif = _frame;
  519. dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
  520. checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
  521. if (checksum != eif->datalen_checksum)
  522. return false;
  523. /* check data checksum */
  524. checksum = pn533_std_data_checksum(eif->data,
  525. be16_to_cpu(eif->datalen));
  526. if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
  527. return false;
  528. }
  529. return true;
  530. }
  531. static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
  532. {
  533. if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
  534. return false;
  535. if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
  536. return false;
  537. return true;
  538. }
  539. static inline int pn533_std_rx_frame_size(void *frame)
  540. {
  541. struct pn533_std_frame *f = frame;
  542. /* check for Extended Information frame */
  543. if (PN533_STD_IS_EXTENDED(f)) {
  544. struct pn533_ext_frame *eif = frame;
  545. return sizeof(struct pn533_ext_frame)
  546. + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
  547. }
  548. return sizeof(struct pn533_std_frame) + f->datalen +
  549. PN533_STD_FRAME_TAIL_LEN;
  550. }
  551. static u8 pn533_std_get_cmd_code(void *frame)
  552. {
  553. struct pn533_std_frame *f = frame;
  554. struct pn533_ext_frame *eif = frame;
  555. if (PN533_STD_IS_EXTENDED(f))
  556. return PN533_FRAME_CMD(eif);
  557. else
  558. return PN533_FRAME_CMD(f);
  559. }
  560. static struct pn533_frame_ops pn533_std_frame_ops = {
  561. .tx_frame_init = pn533_std_tx_frame_init,
  562. .tx_frame_finish = pn533_std_tx_frame_finish,
  563. .tx_update_payload_len = pn533_std_tx_update_payload_len,
  564. .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
  565. .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
  566. .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
  567. .rx_frame_size = pn533_std_rx_frame_size,
  568. .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
  569. .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
  570. .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
  571. .get_cmd_code = pn533_std_get_cmd_code,
  572. };
  573. static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
  574. {
  575. return (dev->ops->get_cmd_code(frame) ==
  576. PN533_CMD_RESPONSE(dev->cmd->code));
  577. }
  578. static void pn533_recv_response(struct urb *urb)
  579. {
  580. struct pn533 *dev = urb->context;
  581. struct pn533_cmd *cmd = dev->cmd;
  582. u8 *in_frame;
  583. cmd->status = urb->status;
  584. switch (urb->status) {
  585. case 0:
  586. break; /* success */
  587. case -ECONNRESET:
  588. case -ENOENT:
  589. dev_dbg(&dev->interface->dev,
  590. "The urb has been canceled (status %d)\n",
  591. urb->status);
  592. goto sched_wq;
  593. case -ESHUTDOWN:
  594. default:
  595. nfc_err(&dev->interface->dev,
  596. "Urb failure (status %d)\n", urb->status);
  597. goto sched_wq;
  598. }
  599. in_frame = dev->in_urb->transfer_buffer;
  600. dev_dbg(&dev->interface->dev, "Received a frame\n");
  601. print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
  602. dev->ops->rx_frame_size(in_frame), false);
  603. if (!dev->ops->rx_is_frame_valid(in_frame, dev)) {
  604. nfc_err(&dev->interface->dev, "Received an invalid frame\n");
  605. cmd->status = -EIO;
  606. goto sched_wq;
  607. }
  608. if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
  609. nfc_err(&dev->interface->dev,
  610. "It it not the response to the last command\n");
  611. cmd->status = -EIO;
  612. goto sched_wq;
  613. }
  614. sched_wq:
  615. queue_work(dev->wq, &dev->cmd_complete_work);
  616. }
  617. static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
  618. {
  619. dev->in_urb->complete = pn533_recv_response;
  620. return usb_submit_urb(dev->in_urb, flags);
  621. }
  622. static void pn533_recv_ack(struct urb *urb)
  623. {
  624. struct pn533 *dev = urb->context;
  625. struct pn533_cmd *cmd = dev->cmd;
  626. struct pn533_std_frame *in_frame;
  627. int rc;
  628. cmd->status = urb->status;
  629. switch (urb->status) {
  630. case 0:
  631. break; /* success */
  632. case -ECONNRESET:
  633. case -ENOENT:
  634. dev_dbg(&dev->interface->dev,
  635. "The urb has been stopped (status %d)\n",
  636. urb->status);
  637. goto sched_wq;
  638. case -ESHUTDOWN:
  639. default:
  640. nfc_err(&dev->interface->dev,
  641. "Urb failure (status %d)\n", urb->status);
  642. goto sched_wq;
  643. }
  644. in_frame = dev->in_urb->transfer_buffer;
  645. if (!pn533_std_rx_frame_is_ack(in_frame)) {
  646. nfc_err(&dev->interface->dev, "Received an invalid ack\n");
  647. cmd->status = -EIO;
  648. goto sched_wq;
  649. }
  650. rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
  651. if (rc) {
  652. nfc_err(&dev->interface->dev,
  653. "usb_submit_urb failed with result %d\n", rc);
  654. cmd->status = rc;
  655. goto sched_wq;
  656. }
  657. return;
  658. sched_wq:
  659. queue_work(dev->wq, &dev->cmd_complete_work);
  660. }
  661. static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
  662. {
  663. dev->in_urb->complete = pn533_recv_ack;
  664. return usb_submit_urb(dev->in_urb, flags);
  665. }
  666. static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
  667. {
  668. u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
  669. /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
  670. int rc;
  671. dev->out_urb->transfer_buffer = ack;
  672. dev->out_urb->transfer_buffer_length = sizeof(ack);
  673. rc = usb_submit_urb(dev->out_urb, flags);
  674. return rc;
  675. }
  676. static int __pn533_send_frame_async(struct pn533 *dev,
  677. struct sk_buff *out,
  678. struct sk_buff *in,
  679. int in_len)
  680. {
  681. int rc;
  682. dev->out_urb->transfer_buffer = out->data;
  683. dev->out_urb->transfer_buffer_length = out->len;
  684. dev->in_urb->transfer_buffer = in->data;
  685. dev->in_urb->transfer_buffer_length = in_len;
  686. print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
  687. out->data, out->len, false);
  688. rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
  689. if (rc)
  690. return rc;
  691. if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
  692. /* request for response for sent packet directly */
  693. rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
  694. if (rc)
  695. goto error;
  696. } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
  697. /* request for ACK if that's the case */
  698. rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
  699. if (rc)
  700. goto error;
  701. }
  702. return 0;
  703. error:
  704. usb_unlink_urb(dev->out_urb);
  705. return rc;
  706. }
  707. static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
  708. struct sk_buff *skb)
  709. {
  710. /* payload is already there, just update datalen */
  711. int payload_len = skb->len;
  712. struct pn533_frame_ops *ops = dev->ops;
  713. skb_push(skb, ops->tx_header_len);
  714. skb_put(skb, ops->tx_tail_len);
  715. ops->tx_frame_init(skb->data, cmd_code);
  716. ops->tx_update_payload_len(skb->data, payload_len);
  717. ops->tx_frame_finish(skb->data);
  718. }
  719. static int pn533_send_async_complete(struct pn533 *dev)
  720. {
  721. struct pn533_cmd *cmd = dev->cmd;
  722. int status = cmd->status;
  723. struct sk_buff *req = cmd->req;
  724. struct sk_buff *resp = cmd->resp;
  725. int rc;
  726. dev_kfree_skb(req);
  727. if (status < 0) {
  728. rc = cmd->complete_cb(dev, cmd->complete_cb_context,
  729. ERR_PTR(status));
  730. dev_kfree_skb(resp);
  731. goto done;
  732. }
  733. skb_put(resp, dev->ops->rx_frame_size(resp->data));
  734. skb_pull(resp, dev->ops->rx_header_len);
  735. skb_trim(resp, resp->len - dev->ops->rx_tail_len);
  736. rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
  737. done:
  738. kfree(cmd);
  739. dev->cmd = NULL;
  740. return rc;
  741. }
  742. static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
  743. struct sk_buff *req, struct sk_buff *resp,
  744. int resp_len,
  745. pn533_send_async_complete_t complete_cb,
  746. void *complete_cb_context)
  747. {
  748. struct pn533_cmd *cmd;
  749. int rc = 0;
  750. dev_dbg(&dev->interface->dev, "Sending command 0x%x\n", cmd_code);
  751. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  752. if (!cmd)
  753. return -ENOMEM;
  754. cmd->code = cmd_code;
  755. cmd->req = req;
  756. cmd->resp = resp;
  757. cmd->resp_len = resp_len;
  758. cmd->complete_cb = complete_cb;
  759. cmd->complete_cb_context = complete_cb_context;
  760. pn533_build_cmd_frame(dev, cmd_code, req);
  761. mutex_lock(&dev->cmd_lock);
  762. if (!dev->cmd_pending) {
  763. rc = __pn533_send_frame_async(dev, req, resp, resp_len);
  764. if (rc)
  765. goto error;
  766. dev->cmd_pending = 1;
  767. dev->cmd = cmd;
  768. goto unlock;
  769. }
  770. dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x\n",
  771. __func__, cmd_code);
  772. INIT_LIST_HEAD(&cmd->queue);
  773. list_add_tail(&cmd->queue, &dev->cmd_queue);
  774. goto unlock;
  775. error:
  776. kfree(cmd);
  777. unlock:
  778. mutex_unlock(&dev->cmd_lock);
  779. return rc;
  780. }
  781. static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
  782. struct sk_buff *req,
  783. pn533_send_async_complete_t complete_cb,
  784. void *complete_cb_context)
  785. {
  786. struct sk_buff *resp;
  787. int rc;
  788. int resp_len = dev->ops->rx_header_len +
  789. dev->ops->max_payload_len +
  790. dev->ops->rx_tail_len;
  791. resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
  792. if (!resp)
  793. return -ENOMEM;
  794. rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
  795. complete_cb_context);
  796. if (rc)
  797. dev_kfree_skb(resp);
  798. return rc;
  799. }
  800. static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
  801. struct sk_buff *req,
  802. pn533_send_async_complete_t complete_cb,
  803. void *complete_cb_context)
  804. {
  805. struct sk_buff *resp;
  806. int rc;
  807. int resp_len = dev->ops->rx_header_len +
  808. dev->ops->max_payload_len +
  809. dev->ops->rx_tail_len;
  810. resp = alloc_skb(resp_len, GFP_KERNEL);
  811. if (!resp)
  812. return -ENOMEM;
  813. rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
  814. complete_cb_context);
  815. if (rc)
  816. dev_kfree_skb(resp);
  817. return rc;
  818. }
  819. /*
  820. * pn533_send_cmd_direct_async
  821. *
  822. * The function sends a piority cmd directly to the chip omiting the cmd
  823. * queue. It's intended to be used by chaining mechanism of received responses
  824. * where the host has to request every single chunk of data before scheduling
  825. * next cmd from the queue.
  826. */
  827. static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
  828. struct sk_buff *req,
  829. pn533_send_async_complete_t complete_cb,
  830. void *complete_cb_context)
  831. {
  832. struct sk_buff *resp;
  833. struct pn533_cmd *cmd;
  834. int rc;
  835. int resp_len = dev->ops->rx_header_len +
  836. dev->ops->max_payload_len +
  837. dev->ops->rx_tail_len;
  838. resp = alloc_skb(resp_len, GFP_KERNEL);
  839. if (!resp)
  840. return -ENOMEM;
  841. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  842. if (!cmd) {
  843. dev_kfree_skb(resp);
  844. return -ENOMEM;
  845. }
  846. cmd->code = cmd_code;
  847. cmd->req = req;
  848. cmd->resp = resp;
  849. cmd->resp_len = resp_len;
  850. cmd->complete_cb = complete_cb;
  851. cmd->complete_cb_context = complete_cb_context;
  852. pn533_build_cmd_frame(dev, cmd_code, req);
  853. rc = __pn533_send_frame_async(dev, req, resp, resp_len);
  854. if (rc < 0) {
  855. dev_kfree_skb(resp);
  856. kfree(cmd);
  857. } else {
  858. dev->cmd = cmd;
  859. }
  860. return rc;
  861. }
  862. static void pn533_wq_cmd_complete(struct work_struct *work)
  863. {
  864. struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
  865. int rc;
  866. rc = pn533_send_async_complete(dev);
  867. if (rc != -EINPROGRESS)
  868. queue_work(dev->wq, &dev->cmd_work);
  869. }
  870. static void pn533_wq_cmd(struct work_struct *work)
  871. {
  872. struct pn533 *dev = container_of(work, struct pn533, cmd_work);
  873. struct pn533_cmd *cmd;
  874. int rc;
  875. mutex_lock(&dev->cmd_lock);
  876. if (list_empty(&dev->cmd_queue)) {
  877. dev->cmd_pending = 0;
  878. mutex_unlock(&dev->cmd_lock);
  879. return;
  880. }
  881. cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
  882. list_del(&cmd->queue);
  883. mutex_unlock(&dev->cmd_lock);
  884. rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
  885. if (rc < 0) {
  886. dev_kfree_skb(cmd->req);
  887. dev_kfree_skb(cmd->resp);
  888. kfree(cmd);
  889. return;
  890. }
  891. dev->cmd = cmd;
  892. }
  893. struct pn533_sync_cmd_response {
  894. struct sk_buff *resp;
  895. struct completion done;
  896. };
  897. static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
  898. struct sk_buff *resp)
  899. {
  900. struct pn533_sync_cmd_response *arg = _arg;
  901. arg->resp = resp;
  902. complete(&arg->done);
  903. return 0;
  904. }
  905. /* pn533_send_cmd_sync
  906. *
  907. * Please note the req parameter is freed inside the function to
  908. * limit a number of return value interpretations by the caller.
  909. *
  910. * 1. negative in case of error during TX path -> req should be freed
  911. *
  912. * 2. negative in case of error during RX path -> req should not be freed
  913. * as it's been already freed at the begining of RX path by
  914. * async_complete_cb.
  915. *
  916. * 3. valid pointer in case of succesfult RX path
  917. *
  918. * A caller has to check a return value with IS_ERR macro. If the test pass,
  919. * the returned pointer is valid.
  920. *
  921. * */
  922. static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
  923. struct sk_buff *req)
  924. {
  925. int rc;
  926. struct pn533_sync_cmd_response arg;
  927. init_completion(&arg.done);
  928. rc = pn533_send_cmd_async(dev, cmd_code, req,
  929. pn533_send_sync_complete, &arg);
  930. if (rc) {
  931. dev_kfree_skb(req);
  932. return ERR_PTR(rc);
  933. }
  934. wait_for_completion(&arg.done);
  935. return arg.resp;
  936. }
  937. static void pn533_send_complete(struct urb *urb)
  938. {
  939. struct pn533 *dev = urb->context;
  940. switch (urb->status) {
  941. case 0:
  942. break; /* success */
  943. case -ECONNRESET:
  944. case -ENOENT:
  945. dev_dbg(&dev->interface->dev,
  946. "The urb has been stopped (status %d)\n",
  947. urb->status);
  948. break;
  949. case -ESHUTDOWN:
  950. default:
  951. nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
  952. urb->status);
  953. }
  954. }
  955. static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
  956. {
  957. /* ACR122U does not support any command which aborts last
  958. * issued command i.e. as ACK for standard PN533. Additionally,
  959. * it behaves stange, sending broken or incorrect responses,
  960. * when we cancel urb before the chip will send response.
  961. */
  962. if (dev->device_type == PN533_DEVICE_ACR122U)
  963. return;
  964. /* An ack will cancel the last issued command */
  965. pn533_send_ack(dev, flags);
  966. /* cancel the urb request */
  967. usb_kill_urb(dev->in_urb);
  968. }
  969. static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
  970. {
  971. struct sk_buff *skb;
  972. skb = alloc_skb(dev->ops->tx_header_len +
  973. size +
  974. dev->ops->tx_tail_len, GFP_KERNEL);
  975. if (skb)
  976. skb_reserve(skb, dev->ops->tx_header_len);
  977. return skb;
  978. }
  979. struct pn533_target_type_a {
  980. __be16 sens_res;
  981. u8 sel_res;
  982. u8 nfcid_len;
  983. u8 nfcid_data[];
  984. } __packed;
  985. #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
  986. #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
  987. #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
  988. #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
  989. #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
  990. #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
  991. #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
  992. #define PN533_TYPE_A_SEL_PROT_MIFARE 0
  993. #define PN533_TYPE_A_SEL_PROT_ISO14443 1
  994. #define PN533_TYPE_A_SEL_PROT_DEP 2
  995. #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
  996. static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
  997. int target_data_len)
  998. {
  999. u8 ssd;
  1000. u8 platconf;
  1001. if (target_data_len < sizeof(struct pn533_target_type_a))
  1002. return false;
  1003. /* The lenght check of nfcid[] and ats[] are not being performed because
  1004. the values are not being used */
  1005. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  1006. ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
  1007. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
  1008. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  1009. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  1010. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  1011. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  1012. return false;
  1013. /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
  1014. if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
  1015. return false;
  1016. return true;
  1017. }
  1018. static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
  1019. int tgt_data_len)
  1020. {
  1021. struct pn533_target_type_a *tgt_type_a;
  1022. tgt_type_a = (struct pn533_target_type_a *)tgt_data;
  1023. if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
  1024. return -EPROTO;
  1025. switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
  1026. case PN533_TYPE_A_SEL_PROT_MIFARE:
  1027. nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
  1028. break;
  1029. case PN533_TYPE_A_SEL_PROT_ISO14443:
  1030. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  1031. break;
  1032. case PN533_TYPE_A_SEL_PROT_DEP:
  1033. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1034. break;
  1035. case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
  1036. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
  1037. NFC_PROTO_NFC_DEP_MASK;
  1038. break;
  1039. }
  1040. nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
  1041. nfc_tgt->sel_res = tgt_type_a->sel_res;
  1042. nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
  1043. memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
  1044. return 0;
  1045. }
  1046. struct pn533_target_felica {
  1047. u8 pol_res;
  1048. u8 opcode;
  1049. u8 nfcid2[NFC_NFCID2_MAXSIZE];
  1050. u8 pad[8];
  1051. /* optional */
  1052. u8 syst_code[];
  1053. } __packed;
  1054. #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
  1055. #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
  1056. static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
  1057. int target_data_len)
  1058. {
  1059. if (target_data_len < sizeof(struct pn533_target_felica))
  1060. return false;
  1061. if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
  1062. return false;
  1063. return true;
  1064. }
  1065. static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
  1066. int tgt_data_len)
  1067. {
  1068. struct pn533_target_felica *tgt_felica;
  1069. tgt_felica = (struct pn533_target_felica *)tgt_data;
  1070. if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
  1071. return -EPROTO;
  1072. if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
  1073. (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
  1074. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1075. else
  1076. nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
  1077. memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
  1078. nfc_tgt->sensf_res_len = 9;
  1079. memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
  1080. nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
  1081. return 0;
  1082. }
  1083. struct pn533_target_jewel {
  1084. __be16 sens_res;
  1085. u8 jewelid[4];
  1086. } __packed;
  1087. static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
  1088. int target_data_len)
  1089. {
  1090. u8 ssd;
  1091. u8 platconf;
  1092. if (target_data_len < sizeof(struct pn533_target_jewel))
  1093. return false;
  1094. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  1095. ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
  1096. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
  1097. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  1098. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  1099. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  1100. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  1101. return false;
  1102. return true;
  1103. }
  1104. static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
  1105. int tgt_data_len)
  1106. {
  1107. struct pn533_target_jewel *tgt_jewel;
  1108. tgt_jewel = (struct pn533_target_jewel *)tgt_data;
  1109. if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
  1110. return -EPROTO;
  1111. nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
  1112. nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
  1113. nfc_tgt->nfcid1_len = 4;
  1114. memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
  1115. return 0;
  1116. }
  1117. struct pn533_type_b_prot_info {
  1118. u8 bitrate;
  1119. u8 fsci_type;
  1120. u8 fwi_adc_fo;
  1121. } __packed;
  1122. #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
  1123. #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
  1124. #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
  1125. struct pn533_type_b_sens_res {
  1126. u8 opcode;
  1127. u8 nfcid[4];
  1128. u8 appdata[4];
  1129. struct pn533_type_b_prot_info prot_info;
  1130. } __packed;
  1131. #define PN533_TYPE_B_OPC_SENSB_RES 0x50
  1132. struct pn533_target_type_b {
  1133. struct pn533_type_b_sens_res sensb_res;
  1134. u8 attrib_res_len;
  1135. u8 attrib_res[];
  1136. } __packed;
  1137. static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
  1138. int target_data_len)
  1139. {
  1140. if (target_data_len < sizeof(struct pn533_target_type_b))
  1141. return false;
  1142. if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
  1143. return false;
  1144. if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
  1145. PN533_TYPE_B_PROT_TYPE_RFU_MASK)
  1146. return false;
  1147. return true;
  1148. }
  1149. static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
  1150. int tgt_data_len)
  1151. {
  1152. struct pn533_target_type_b *tgt_type_b;
  1153. tgt_type_b = (struct pn533_target_type_b *)tgt_data;
  1154. if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
  1155. return -EPROTO;
  1156. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
  1157. return 0;
  1158. }
  1159. static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
  1160. int tgdata_len)
  1161. {
  1162. struct nfc_target nfc_tgt;
  1163. int rc;
  1164. dev_dbg(&dev->interface->dev, "%s: modulation=%d\n",
  1165. __func__, dev->poll_mod_curr);
  1166. if (tg != 1)
  1167. return -EPROTO;
  1168. memset(&nfc_tgt, 0, sizeof(struct nfc_target));
  1169. switch (dev->poll_mod_curr) {
  1170. case PN533_POLL_MOD_106KBPS_A:
  1171. rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
  1172. break;
  1173. case PN533_POLL_MOD_212KBPS_FELICA:
  1174. case PN533_POLL_MOD_424KBPS_FELICA:
  1175. rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
  1176. break;
  1177. case PN533_POLL_MOD_106KBPS_JEWEL:
  1178. rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
  1179. break;
  1180. case PN533_POLL_MOD_847KBPS_B:
  1181. rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
  1182. break;
  1183. default:
  1184. nfc_err(&dev->interface->dev,
  1185. "Unknown current poll modulation\n");
  1186. return -EPROTO;
  1187. }
  1188. if (rc)
  1189. return rc;
  1190. if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
  1191. dev_dbg(&dev->interface->dev,
  1192. "The Tg found doesn't have the desired protocol\n");
  1193. return -EAGAIN;
  1194. }
  1195. dev_dbg(&dev->interface->dev,
  1196. "Target found - supported protocols: 0x%x\n",
  1197. nfc_tgt.supported_protocols);
  1198. dev->tgt_available_prots = nfc_tgt.supported_protocols;
  1199. nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
  1200. return 0;
  1201. }
  1202. static inline void pn533_poll_next_mod(struct pn533 *dev)
  1203. {
  1204. dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
  1205. }
  1206. static void pn533_poll_reset_mod_list(struct pn533 *dev)
  1207. {
  1208. dev->poll_mod_count = 0;
  1209. }
  1210. static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
  1211. {
  1212. dev->poll_mod_active[dev->poll_mod_count] =
  1213. (struct pn533_poll_modulations *)&poll_mod[mod_index];
  1214. dev->poll_mod_count++;
  1215. }
  1216. static void pn533_poll_create_mod_list(struct pn533 *dev,
  1217. u32 im_protocols, u32 tm_protocols)
  1218. {
  1219. pn533_poll_reset_mod_list(dev);
  1220. if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
  1221. (im_protocols & NFC_PROTO_ISO14443_MASK) ||
  1222. (im_protocols & NFC_PROTO_NFC_DEP_MASK))
  1223. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
  1224. if (im_protocols & NFC_PROTO_FELICA_MASK ||
  1225. im_protocols & NFC_PROTO_NFC_DEP_MASK) {
  1226. pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
  1227. pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
  1228. }
  1229. if (im_protocols & NFC_PROTO_JEWEL_MASK)
  1230. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
  1231. if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
  1232. pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
  1233. if (tm_protocols)
  1234. pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
  1235. }
  1236. static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
  1237. {
  1238. u8 nbtg, tg, *tgdata;
  1239. int rc, tgdata_len;
  1240. /* Toggle the DEP polling */
  1241. dev->poll_dep = 1;
  1242. nbtg = resp->data[0];
  1243. tg = resp->data[1];
  1244. tgdata = &resp->data[2];
  1245. tgdata_len = resp->len - 2; /* nbtg + tg */
  1246. if (nbtg) {
  1247. rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
  1248. /* We must stop the poll after a valid target found */
  1249. if (rc == 0) {
  1250. pn533_poll_reset_mod_list(dev);
  1251. return 0;
  1252. }
  1253. }
  1254. return -EAGAIN;
  1255. }
  1256. static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
  1257. {
  1258. struct sk_buff *skb;
  1259. u8 *felica, *nfcid3, *gb;
  1260. u8 *gbytes = dev->gb;
  1261. size_t gbytes_len = dev->gb_len;
  1262. u8 felica_params[18] = {0x1, 0xfe, /* DEP */
  1263. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
  1264. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  1265. 0xff, 0xff}; /* System code */
  1266. u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
  1267. 0x0, 0x0, 0x0,
  1268. 0x40}; /* SEL_RES for DEP */
  1269. unsigned int skb_len = 36 + /* mode (1), mifare (6),
  1270. felica (18), nfcid3 (10), gb_len (1) */
  1271. gbytes_len +
  1272. 1; /* len Tk*/
  1273. skb = pn533_alloc_skb(dev, skb_len);
  1274. if (!skb)
  1275. return NULL;
  1276. /* DEP support only */
  1277. *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
  1278. /* MIFARE params */
  1279. memcpy(skb_put(skb, 6), mifare_params, 6);
  1280. /* Felica params */
  1281. felica = skb_put(skb, 18);
  1282. memcpy(felica, felica_params, 18);
  1283. get_random_bytes(felica + 2, 6);
  1284. /* NFCID3 */
  1285. nfcid3 = skb_put(skb, 10);
  1286. memset(nfcid3, 0, 10);
  1287. memcpy(nfcid3, felica, 8);
  1288. /* General bytes */
  1289. *skb_put(skb, 1) = gbytes_len;
  1290. gb = skb_put(skb, gbytes_len);
  1291. memcpy(gb, gbytes, gbytes_len);
  1292. /* Len Tk */
  1293. *skb_put(skb, 1) = 0;
  1294. return skb;
  1295. }
  1296. #define PN533_CMD_DATAEXCH_HEAD_LEN 1
  1297. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  1298. static void pn533_wq_tm_mi_recv(struct work_struct *work);
  1299. static struct sk_buff *pn533_build_response(struct pn533 *dev);
  1300. static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
  1301. struct sk_buff *resp)
  1302. {
  1303. struct sk_buff *skb;
  1304. u8 status, ret, mi;
  1305. int rc;
  1306. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1307. if (IS_ERR(resp)) {
  1308. skb_queue_purge(&dev->resp_q);
  1309. return PTR_ERR(resp);
  1310. }
  1311. status = resp->data[0];
  1312. ret = status & PN533_CMD_RET_MASK;
  1313. mi = status & PN533_CMD_MI_MASK;
  1314. skb_pull(resp, sizeof(status));
  1315. if (ret != PN533_CMD_RET_SUCCESS) {
  1316. rc = -EIO;
  1317. goto error;
  1318. }
  1319. skb_queue_tail(&dev->resp_q, resp);
  1320. if (mi) {
  1321. queue_work(dev->wq, &dev->mi_tm_rx_work);
  1322. return -EINPROGRESS;
  1323. }
  1324. skb = pn533_build_response(dev);
  1325. if (!skb) {
  1326. rc = -EIO;
  1327. goto error;
  1328. }
  1329. return nfc_tm_data_received(dev->nfc_dev, skb);
  1330. error:
  1331. nfc_tm_deactivated(dev->nfc_dev);
  1332. dev->tgt_mode = 0;
  1333. skb_queue_purge(&dev->resp_q);
  1334. dev_kfree_skb(resp);
  1335. return rc;
  1336. }
  1337. static void pn533_wq_tm_mi_recv(struct work_struct *work)
  1338. {
  1339. struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
  1340. struct sk_buff *skb;
  1341. int rc;
  1342. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1343. skb = pn533_alloc_skb(dev, 0);
  1344. if (!skb)
  1345. return;
  1346. rc = pn533_send_cmd_direct_async(dev,
  1347. PN533_CMD_TG_GET_DATA,
  1348. skb,
  1349. pn533_tm_get_data_complete,
  1350. NULL);
  1351. if (rc < 0)
  1352. dev_kfree_skb(skb);
  1353. return;
  1354. }
  1355. static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
  1356. struct sk_buff *resp);
  1357. static void pn533_wq_tm_mi_send(struct work_struct *work)
  1358. {
  1359. struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
  1360. struct sk_buff *skb;
  1361. int rc;
  1362. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1363. /* Grab the first skb in the queue */
  1364. skb = skb_dequeue(&dev->fragment_skb);
  1365. if (skb == NULL) { /* No more data */
  1366. /* Reset the queue for future use */
  1367. skb_queue_head_init(&dev->fragment_skb);
  1368. goto error;
  1369. }
  1370. /* last entry - remove MI bit */
  1371. if (skb_queue_len(&dev->fragment_skb) == 0) {
  1372. rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
  1373. skb, pn533_tm_send_complete, NULL);
  1374. } else
  1375. rc = pn533_send_cmd_direct_async(dev,
  1376. PN533_CMD_TG_SET_META_DATA,
  1377. skb, pn533_tm_send_complete, NULL);
  1378. if (rc == 0) /* success */
  1379. return;
  1380. dev_err(&dev->interface->dev,
  1381. "Error %d when trying to perform set meta data_exchange", rc);
  1382. dev_kfree_skb(skb);
  1383. error:
  1384. pn533_send_ack(dev, GFP_KERNEL);
  1385. queue_work(dev->wq, &dev->cmd_work);
  1386. }
  1387. static void pn533_wq_tg_get_data(struct work_struct *work)
  1388. {
  1389. struct pn533 *dev = container_of(work, struct pn533, tg_work);
  1390. struct sk_buff *skb;
  1391. int rc;
  1392. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1393. skb = pn533_alloc_skb(dev, 0);
  1394. if (!skb)
  1395. return;
  1396. rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
  1397. pn533_tm_get_data_complete, NULL);
  1398. if (rc < 0)
  1399. dev_kfree_skb(skb);
  1400. return;
  1401. }
  1402. #define ATR_REQ_GB_OFFSET 17
  1403. static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
  1404. {
  1405. u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
  1406. size_t gb_len;
  1407. int rc;
  1408. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1409. if (resp->len < ATR_REQ_GB_OFFSET + 1)
  1410. return -EINVAL;
  1411. mode = resp->data[0];
  1412. cmd = &resp->data[1];
  1413. dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
  1414. mode, resp->len);
  1415. if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
  1416. PN533_INIT_TARGET_RESP_ACTIVE)
  1417. comm_mode = NFC_COMM_ACTIVE;
  1418. if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
  1419. return -EOPNOTSUPP;
  1420. gb = cmd + ATR_REQ_GB_OFFSET;
  1421. gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
  1422. rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
  1423. comm_mode, gb, gb_len);
  1424. if (rc < 0) {
  1425. nfc_err(&dev->interface->dev,
  1426. "Error when signaling target activation\n");
  1427. return rc;
  1428. }
  1429. dev->tgt_mode = 1;
  1430. queue_work(dev->wq, &dev->tg_work);
  1431. return 0;
  1432. }
  1433. static void pn533_listen_mode_timer(unsigned long data)
  1434. {
  1435. struct pn533 *dev = (struct pn533 *)data;
  1436. dev_dbg(&dev->interface->dev, "Listen mode timeout\n");
  1437. dev->cancel_listen = 1;
  1438. pn533_poll_next_mod(dev);
  1439. queue_delayed_work(dev->wq, &dev->poll_work,
  1440. msecs_to_jiffies(PN533_POLL_INTERVAL));
  1441. }
  1442. static int pn533_rf_complete(struct pn533 *dev, void *arg,
  1443. struct sk_buff *resp)
  1444. {
  1445. int rc = 0;
  1446. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1447. if (IS_ERR(resp)) {
  1448. rc = PTR_ERR(resp);
  1449. nfc_err(&dev->interface->dev, "RF setting error %d\n", rc);
  1450. return rc;
  1451. }
  1452. queue_delayed_work(dev->wq, &dev->poll_work,
  1453. msecs_to_jiffies(PN533_POLL_INTERVAL));
  1454. dev_kfree_skb(resp);
  1455. return rc;
  1456. }
  1457. static void pn533_wq_rf(struct work_struct *work)
  1458. {
  1459. struct pn533 *dev = container_of(work, struct pn533, rf_work);
  1460. struct sk_buff *skb;
  1461. int rc;
  1462. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1463. skb = pn533_alloc_skb(dev, 2);
  1464. if (!skb)
  1465. return;
  1466. *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
  1467. *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
  1468. rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
  1469. pn533_rf_complete, NULL);
  1470. if (rc < 0) {
  1471. dev_kfree_skb(skb);
  1472. nfc_err(&dev->interface->dev, "RF setting error %d\n", rc);
  1473. }
  1474. return;
  1475. }
  1476. static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
  1477. struct sk_buff *resp)
  1478. {
  1479. struct pn533_cmd_jump_dep_response *rsp;
  1480. struct nfc_target nfc_target;
  1481. u8 target_gt_len;
  1482. int rc;
  1483. if (IS_ERR(resp))
  1484. return PTR_ERR(resp);
  1485. rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
  1486. rc = rsp->status & PN533_CMD_RET_MASK;
  1487. if (rc != PN533_CMD_RET_SUCCESS) {
  1488. /* Not target found, turn radio off */
  1489. queue_work(dev->wq, &dev->rf_work);
  1490. dev_kfree_skb(resp);
  1491. return 0;
  1492. }
  1493. dev_dbg(&dev->interface->dev, "Creating new target");
  1494. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1495. nfc_target.nfcid1_len = 10;
  1496. memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
  1497. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1498. if (rc)
  1499. goto error;
  1500. dev->tgt_available_prots = 0;
  1501. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1502. /* ATR_RES general bytes are located at offset 17 */
  1503. target_gt_len = resp->len - 17;
  1504. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1505. rsp->gt, target_gt_len);
  1506. if (!rc) {
  1507. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1508. dev->nfc_dev->targets[0].idx,
  1509. 0, NFC_RF_INITIATOR);
  1510. if (!rc)
  1511. pn533_poll_reset_mod_list(dev);
  1512. }
  1513. error:
  1514. dev_kfree_skb(resp);
  1515. return rc;
  1516. }
  1517. #define PASSIVE_DATA_LEN 5
  1518. static int pn533_poll_dep(struct nfc_dev *nfc_dev)
  1519. {
  1520. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1521. struct sk_buff *skb;
  1522. int rc, skb_len;
  1523. u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
  1524. u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
  1525. dev_dbg(&dev->interface->dev, "%s", __func__);
  1526. if (!dev->gb) {
  1527. dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
  1528. if (!dev->gb || !dev->gb_len) {
  1529. dev->poll_dep = 0;
  1530. queue_work(dev->wq, &dev->rf_work);
  1531. }
  1532. }
  1533. skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
  1534. skb_len += PASSIVE_DATA_LEN;
  1535. /* NFCID3 */
  1536. skb_len += NFC_NFCID3_MAXSIZE;
  1537. nfcid3[0] = 0x1;
  1538. nfcid3[1] = 0xfe;
  1539. get_random_bytes(nfcid3 + 2, 6);
  1540. skb = pn533_alloc_skb(dev, skb_len);
  1541. if (!skb)
  1542. return -ENOMEM;
  1543. *skb_put(skb, 1) = 0x01; /* Active */
  1544. *skb_put(skb, 1) = 0x02; /* 424 kbps */
  1545. next = skb_put(skb, 1); /* Next */
  1546. *next = 0;
  1547. /* Copy passive data */
  1548. memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
  1549. *next |= 1;
  1550. /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
  1551. memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
  1552. NFC_NFCID3_MAXSIZE);
  1553. *next |= 2;
  1554. memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
  1555. *next |= 4; /* We have some Gi */
  1556. rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
  1557. pn533_poll_dep_complete, NULL);
  1558. if (rc < 0)
  1559. dev_kfree_skb(skb);
  1560. return rc;
  1561. }
  1562. static int pn533_poll_complete(struct pn533 *dev, void *arg,
  1563. struct sk_buff *resp)
  1564. {
  1565. struct pn533_poll_modulations *cur_mod;
  1566. int rc;
  1567. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1568. if (IS_ERR(resp)) {
  1569. rc = PTR_ERR(resp);
  1570. nfc_err(&dev->interface->dev, "%s Poll complete error %d\n",
  1571. __func__, rc);
  1572. if (rc == -ENOENT) {
  1573. if (dev->poll_mod_count != 0)
  1574. return rc;
  1575. else
  1576. goto stop_poll;
  1577. } else if (rc < 0) {
  1578. nfc_err(&dev->interface->dev,
  1579. "Error %d when running poll\n", rc);
  1580. goto stop_poll;
  1581. }
  1582. }
  1583. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1584. if (cur_mod->len == 0) { /* Target mode */
  1585. del_timer(&dev->listen_timer);
  1586. rc = pn533_init_target_complete(dev, resp);
  1587. goto done;
  1588. }
  1589. /* Initiator mode */
  1590. rc = pn533_start_poll_complete(dev, resp);
  1591. if (!rc)
  1592. goto done;
  1593. if (!dev->poll_mod_count) {
  1594. dev_dbg(&dev->interface->dev, "Polling has been stopped\n");
  1595. goto done;
  1596. }
  1597. pn533_poll_next_mod(dev);
  1598. /* Not target found, turn radio off */
  1599. queue_work(dev->wq, &dev->rf_work);
  1600. done:
  1601. dev_kfree_skb(resp);
  1602. return rc;
  1603. stop_poll:
  1604. nfc_err(&dev->interface->dev, "Polling operation has been stopped\n");
  1605. pn533_poll_reset_mod_list(dev);
  1606. dev->poll_protocols = 0;
  1607. return rc;
  1608. }
  1609. static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
  1610. struct pn533_poll_modulations *mod)
  1611. {
  1612. struct sk_buff *skb;
  1613. skb = pn533_alloc_skb(dev, mod->len);
  1614. if (!skb)
  1615. return NULL;
  1616. memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
  1617. return skb;
  1618. }
  1619. static int pn533_send_poll_frame(struct pn533 *dev)
  1620. {
  1621. struct pn533_poll_modulations *mod;
  1622. struct sk_buff *skb;
  1623. int rc;
  1624. u8 cmd_code;
  1625. mod = dev->poll_mod_active[dev->poll_mod_curr];
  1626. dev_dbg(&dev->interface->dev, "%s mod len %d\n",
  1627. __func__, mod->len);
  1628. if (dev->poll_dep) {
  1629. dev->poll_dep = 0;
  1630. return pn533_poll_dep(dev->nfc_dev);
  1631. }
  1632. if (mod->len == 0) { /* Listen mode */
  1633. cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
  1634. skb = pn533_alloc_poll_tg_frame(dev);
  1635. } else { /* Polling mode */
  1636. cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
  1637. skb = pn533_alloc_poll_in_frame(dev, mod);
  1638. }
  1639. if (!skb) {
  1640. nfc_err(&dev->interface->dev, "Failed to allocate skb\n");
  1641. return -ENOMEM;
  1642. }
  1643. rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
  1644. NULL);
  1645. if (rc < 0) {
  1646. dev_kfree_skb(skb);
  1647. nfc_err(&dev->interface->dev, "Polling loop error %d\n", rc);
  1648. }
  1649. return rc;
  1650. }
  1651. static void pn533_wq_poll(struct work_struct *work)
  1652. {
  1653. struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
  1654. struct pn533_poll_modulations *cur_mod;
  1655. int rc;
  1656. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1657. dev_dbg(&dev->interface->dev,
  1658. "%s cancel_listen %d modulation len %d\n",
  1659. __func__, dev->cancel_listen, cur_mod->len);
  1660. if (dev->cancel_listen == 1) {
  1661. dev->cancel_listen = 0;
  1662. pn533_abort_cmd(dev, GFP_ATOMIC);
  1663. }
  1664. rc = pn533_send_poll_frame(dev);
  1665. if (rc)
  1666. return;
  1667. if (cur_mod->len == 0 && dev->poll_mod_count > 1)
  1668. mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
  1669. return;
  1670. }
  1671. static int pn533_start_poll(struct nfc_dev *nfc_dev,
  1672. u32 im_protocols, u32 tm_protocols)
  1673. {
  1674. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1675. struct pn533_poll_modulations *cur_mod;
  1676. u8 rand_mod;
  1677. int rc;
  1678. dev_dbg(&dev->interface->dev,
  1679. "%s: im protocols 0x%x tm protocols 0x%x\n",
  1680. __func__, im_protocols, tm_protocols);
  1681. if (dev->tgt_active_prot) {
  1682. nfc_err(&dev->interface->dev,
  1683. "Cannot poll with a target already activated\n");
  1684. return -EBUSY;
  1685. }
  1686. if (dev->tgt_mode) {
  1687. nfc_err(&dev->interface->dev,
  1688. "Cannot poll while already being activated\n");
  1689. return -EBUSY;
  1690. }
  1691. if (tm_protocols) {
  1692. dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
  1693. if (dev->gb == NULL)
  1694. tm_protocols = 0;
  1695. }
  1696. pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
  1697. dev->poll_protocols = im_protocols;
  1698. dev->listen_protocols = tm_protocols;
  1699. /* Do not always start polling from the same modulation */
  1700. get_random_bytes(&rand_mod, sizeof(rand_mod));
  1701. rand_mod %= dev->poll_mod_count;
  1702. dev->poll_mod_curr = rand_mod;
  1703. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1704. rc = pn533_send_poll_frame(dev);
  1705. /* Start listen timer */
  1706. if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
  1707. mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
  1708. return rc;
  1709. }
  1710. static void pn533_stop_poll(struct nfc_dev *nfc_dev)
  1711. {
  1712. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1713. del_timer(&dev->listen_timer);
  1714. if (!dev->poll_mod_count) {
  1715. dev_dbg(&dev->interface->dev,
  1716. "Polling operation was not running\n");
  1717. return;
  1718. }
  1719. pn533_abort_cmd(dev, GFP_KERNEL);
  1720. flush_delayed_work(&dev->poll_work);
  1721. pn533_poll_reset_mod_list(dev);
  1722. }
  1723. static int pn533_activate_target_nfcdep(struct pn533 *dev)
  1724. {
  1725. struct pn533_cmd_activate_response *rsp;
  1726. u16 gt_len;
  1727. int rc;
  1728. struct sk_buff *skb;
  1729. struct sk_buff *resp;
  1730. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1731. skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
  1732. if (!skb)
  1733. return -ENOMEM;
  1734. *skb_put(skb, sizeof(u8)) = 1; /* TG */
  1735. *skb_put(skb, sizeof(u8)) = 0; /* Next */
  1736. resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
  1737. if (IS_ERR(resp))
  1738. return PTR_ERR(resp);
  1739. rsp = (struct pn533_cmd_activate_response *)resp->data;
  1740. rc = rsp->status & PN533_CMD_RET_MASK;
  1741. if (rc != PN533_CMD_RET_SUCCESS) {
  1742. nfc_err(&dev->interface->dev,
  1743. "Target activation failed (error 0x%x)\n", rc);
  1744. dev_kfree_skb(resp);
  1745. return -EIO;
  1746. }
  1747. /* ATR_RES general bytes are located at offset 16 */
  1748. gt_len = resp->len - 16;
  1749. rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
  1750. dev_kfree_skb(resp);
  1751. return rc;
  1752. }
  1753. static int pn533_activate_target(struct nfc_dev *nfc_dev,
  1754. struct nfc_target *target, u32 protocol)
  1755. {
  1756. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1757. int rc;
  1758. dev_dbg(&dev->interface->dev, "%s: protocol=%u\n", __func__, protocol);
  1759. if (dev->poll_mod_count) {
  1760. nfc_err(&dev->interface->dev,
  1761. "Cannot activate while polling\n");
  1762. return -EBUSY;
  1763. }
  1764. if (dev->tgt_active_prot) {
  1765. nfc_err(&dev->interface->dev,
  1766. "There is already an active target\n");
  1767. return -EBUSY;
  1768. }
  1769. if (!dev->tgt_available_prots) {
  1770. nfc_err(&dev->interface->dev,
  1771. "There is no available target to activate\n");
  1772. return -EINVAL;
  1773. }
  1774. if (!(dev->tgt_available_prots & (1 << protocol))) {
  1775. nfc_err(&dev->interface->dev,
  1776. "Target doesn't support requested proto %u\n",
  1777. protocol);
  1778. return -EINVAL;
  1779. }
  1780. if (protocol == NFC_PROTO_NFC_DEP) {
  1781. rc = pn533_activate_target_nfcdep(dev);
  1782. if (rc) {
  1783. nfc_err(&dev->interface->dev,
  1784. "Activating target with DEP failed %d\n", rc);
  1785. return rc;
  1786. }
  1787. }
  1788. dev->tgt_active_prot = protocol;
  1789. dev->tgt_available_prots = 0;
  1790. return 0;
  1791. }
  1792. static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
  1793. struct nfc_target *target)
  1794. {
  1795. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1796. struct sk_buff *skb;
  1797. struct sk_buff *resp;
  1798. int rc;
  1799. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1800. if (!dev->tgt_active_prot) {
  1801. nfc_err(&dev->interface->dev, "There is no active target\n");
  1802. return;
  1803. }
  1804. dev->tgt_active_prot = 0;
  1805. skb_queue_purge(&dev->resp_q);
  1806. skb = pn533_alloc_skb(dev, sizeof(u8));
  1807. if (!skb)
  1808. return;
  1809. *skb_put(skb, 1) = 1; /* TG*/
  1810. resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
  1811. if (IS_ERR(resp))
  1812. return;
  1813. rc = resp->data[0] & PN533_CMD_RET_MASK;
  1814. if (rc != PN533_CMD_RET_SUCCESS)
  1815. nfc_err(&dev->interface->dev,
  1816. "Error 0x%x when releasing the target\n", rc);
  1817. dev_kfree_skb(resp);
  1818. return;
  1819. }
  1820. static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
  1821. struct sk_buff *resp)
  1822. {
  1823. struct pn533_cmd_jump_dep_response *rsp;
  1824. u8 target_gt_len;
  1825. int rc;
  1826. u8 active = *(u8 *)arg;
  1827. kfree(arg);
  1828. if (IS_ERR(resp))
  1829. return PTR_ERR(resp);
  1830. if (dev->tgt_available_prots &&
  1831. !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
  1832. nfc_err(&dev->interface->dev,
  1833. "The target does not support DEP\n");
  1834. rc = -EINVAL;
  1835. goto error;
  1836. }
  1837. rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
  1838. rc = rsp->status & PN533_CMD_RET_MASK;
  1839. if (rc != PN533_CMD_RET_SUCCESS) {
  1840. nfc_err(&dev->interface->dev,
  1841. "Bringing DEP link up failed (error 0x%x)\n", rc);
  1842. goto error;
  1843. }
  1844. if (!dev->tgt_available_prots) {
  1845. struct nfc_target nfc_target;
  1846. dev_dbg(&dev->interface->dev, "Creating new target\n");
  1847. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1848. nfc_target.nfcid1_len = 10;
  1849. memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
  1850. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1851. if (rc)
  1852. goto error;
  1853. dev->tgt_available_prots = 0;
  1854. }
  1855. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1856. /* ATR_RES general bytes are located at offset 17 */
  1857. target_gt_len = resp->len - 17;
  1858. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1859. rsp->gt, target_gt_len);
  1860. if (rc == 0)
  1861. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1862. dev->nfc_dev->targets[0].idx,
  1863. !active, NFC_RF_INITIATOR);
  1864. error:
  1865. dev_kfree_skb(resp);
  1866. return rc;
  1867. }
  1868. static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
  1869. static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  1870. u8 comm_mode, u8 *gb, size_t gb_len)
  1871. {
  1872. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1873. struct sk_buff *skb;
  1874. int rc, skb_len;
  1875. u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
  1876. u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
  1877. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1878. if (dev->poll_mod_count) {
  1879. nfc_err(&dev->interface->dev,
  1880. "Cannot bring the DEP link up while polling\n");
  1881. return -EBUSY;
  1882. }
  1883. if (dev->tgt_active_prot) {
  1884. nfc_err(&dev->interface->dev,
  1885. "There is already an active target\n");
  1886. return -EBUSY;
  1887. }
  1888. skb_len = 3 + gb_len; /* ActPass + BR + Next */
  1889. skb_len += PASSIVE_DATA_LEN;
  1890. /* NFCID3 */
  1891. skb_len += NFC_NFCID3_MAXSIZE;
  1892. if (target && !target->nfcid2_len) {
  1893. nfcid3[0] = 0x1;
  1894. nfcid3[1] = 0xfe;
  1895. get_random_bytes(nfcid3 + 2, 6);
  1896. }
  1897. skb = pn533_alloc_skb(dev, skb_len);
  1898. if (!skb)
  1899. return -ENOMEM;
  1900. *skb_put(skb, 1) = !comm_mode; /* ActPass */
  1901. *skb_put(skb, 1) = 0x02; /* 424 kbps */
  1902. next = skb_put(skb, 1); /* Next */
  1903. *next = 0;
  1904. /* Copy passive data */
  1905. memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
  1906. *next |= 1;
  1907. /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
  1908. if (target && target->nfcid2_len)
  1909. memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
  1910. target->nfcid2_len);
  1911. else
  1912. memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
  1913. NFC_NFCID3_MAXSIZE);
  1914. *next |= 2;
  1915. if (gb != NULL && gb_len > 0) {
  1916. memcpy(skb_put(skb, gb_len), gb, gb_len);
  1917. *next |= 4; /* We have some Gi */
  1918. } else {
  1919. *next = 0;
  1920. }
  1921. arg = kmalloc(sizeof(*arg), GFP_KERNEL);
  1922. if (!arg) {
  1923. dev_kfree_skb(skb);
  1924. return -ENOMEM;
  1925. }
  1926. *arg = !comm_mode;
  1927. pn533_rf_field(dev->nfc_dev, 0);
  1928. rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
  1929. pn533_in_dep_link_up_complete, arg);
  1930. if (rc < 0) {
  1931. dev_kfree_skb(skb);
  1932. kfree(arg);
  1933. }
  1934. return rc;
  1935. }
  1936. static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
  1937. {
  1938. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1939. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1940. pn533_poll_reset_mod_list(dev);
  1941. if (dev->tgt_mode || dev->tgt_active_prot)
  1942. pn533_abort_cmd(dev, GFP_KERNEL);
  1943. dev->tgt_active_prot = 0;
  1944. dev->tgt_mode = 0;
  1945. skb_queue_purge(&dev->resp_q);
  1946. return 0;
  1947. }
  1948. struct pn533_data_exchange_arg {
  1949. data_exchange_cb_t cb;
  1950. void *cb_context;
  1951. };
  1952. static struct sk_buff *pn533_build_response(struct pn533 *dev)
  1953. {
  1954. struct sk_buff *skb, *tmp, *t;
  1955. unsigned int skb_len = 0, tmp_len = 0;
  1956. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1957. if (skb_queue_empty(&dev->resp_q))
  1958. return NULL;
  1959. if (skb_queue_len(&dev->resp_q) == 1) {
  1960. skb = skb_dequeue(&dev->resp_q);
  1961. goto out;
  1962. }
  1963. skb_queue_walk_safe(&dev->resp_q, tmp, t)
  1964. skb_len += tmp->len;
  1965. dev_dbg(&dev->interface->dev, "%s total length %d\n",
  1966. __func__, skb_len);
  1967. skb = alloc_skb(skb_len, GFP_KERNEL);
  1968. if (skb == NULL)
  1969. goto out;
  1970. skb_put(skb, skb_len);
  1971. skb_queue_walk_safe(&dev->resp_q, tmp, t) {
  1972. memcpy(skb->data + tmp_len, tmp->data, tmp->len);
  1973. tmp_len += tmp->len;
  1974. }
  1975. out:
  1976. skb_queue_purge(&dev->resp_q);
  1977. return skb;
  1978. }
  1979. static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
  1980. struct sk_buff *resp)
  1981. {
  1982. struct pn533_data_exchange_arg *arg = _arg;
  1983. struct sk_buff *skb;
  1984. int rc = 0;
  1985. u8 status, ret, mi;
  1986. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1987. if (IS_ERR(resp)) {
  1988. rc = PTR_ERR(resp);
  1989. goto _error;
  1990. }
  1991. status = resp->data[0];
  1992. ret = status & PN533_CMD_RET_MASK;
  1993. mi = status & PN533_CMD_MI_MASK;
  1994. skb_pull(resp, sizeof(status));
  1995. if (ret != PN533_CMD_RET_SUCCESS) {
  1996. nfc_err(&dev->interface->dev,
  1997. "Exchanging data failed (error 0x%x)\n", ret);
  1998. rc = -EIO;
  1999. goto error;
  2000. }
  2001. skb_queue_tail(&dev->resp_q, resp);
  2002. if (mi) {
  2003. dev->cmd_complete_mi_arg = arg;
  2004. queue_work(dev->wq, &dev->mi_rx_work);
  2005. return -EINPROGRESS;
  2006. }
  2007. /* Prepare for the next round */
  2008. if (skb_queue_len(&dev->fragment_skb) > 0) {
  2009. dev->cmd_complete_dep_arg = arg;
  2010. queue_work(dev->wq, &dev->mi_tx_work);
  2011. return -EINPROGRESS;
  2012. }
  2013. skb = pn533_build_response(dev);
  2014. if (!skb) {
  2015. rc = -ENOMEM;
  2016. goto error;
  2017. }
  2018. arg->cb(arg->cb_context, skb, 0);
  2019. kfree(arg);
  2020. return 0;
  2021. error:
  2022. dev_kfree_skb(resp);
  2023. _error:
  2024. skb_queue_purge(&dev->resp_q);
  2025. arg->cb(arg->cb_context, NULL, rc);
  2026. kfree(arg);
  2027. return rc;
  2028. }
  2029. /* Split the Tx skb into small chunks */
  2030. static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
  2031. {
  2032. struct sk_buff *frag;
  2033. int frag_size;
  2034. do {
  2035. /* Remaining size */
  2036. if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
  2037. frag_size = PN533_CMD_DATAFRAME_MAXLEN;
  2038. else
  2039. frag_size = skb->len;
  2040. /* Allocate and reserve */
  2041. frag = pn533_alloc_skb(dev, frag_size);
  2042. if (!frag) {
  2043. skb_queue_purge(&dev->fragment_skb);
  2044. break;
  2045. }
  2046. if (!dev->tgt_mode) {
  2047. /* Reserve the TG/MI byte */
  2048. skb_reserve(frag, 1);
  2049. /* MI + TG */
  2050. if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
  2051. *skb_push(frag, sizeof(u8)) =
  2052. (PN533_CMD_MI_MASK | 1);
  2053. else
  2054. *skb_push(frag, sizeof(u8)) = 1; /* TG */
  2055. }
  2056. memcpy(skb_put(frag, frag_size), skb->data, frag_size);
  2057. /* Reduce the size of incoming buffer */
  2058. skb_pull(skb, frag_size);
  2059. /* Add this to skb_queue */
  2060. skb_queue_tail(&dev->fragment_skb, frag);
  2061. } while (skb->len > 0);
  2062. dev_kfree_skb(skb);
  2063. return skb_queue_len(&dev->fragment_skb);
  2064. }
  2065. static int pn533_transceive(struct nfc_dev *nfc_dev,
  2066. struct nfc_target *target, struct sk_buff *skb,
  2067. data_exchange_cb_t cb, void *cb_context)
  2068. {
  2069. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  2070. struct pn533_data_exchange_arg *arg = NULL;
  2071. int rc;
  2072. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  2073. if (!dev->tgt_active_prot) {
  2074. nfc_err(&dev->interface->dev,
  2075. "Can't exchange data if there is no active target\n");
  2076. rc = -EINVAL;
  2077. goto error;
  2078. }
  2079. arg = kmalloc(sizeof(*arg), GFP_KERNEL);
  2080. if (!arg) {
  2081. rc = -ENOMEM;
  2082. goto error;
  2083. }
  2084. arg->cb = cb;
  2085. arg->cb_context = cb_context;
  2086. switch (dev->device_type) {
  2087. case PN533_DEVICE_PASORI:
  2088. if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
  2089. rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
  2090. skb,
  2091. pn533_data_exchange_complete,
  2092. arg);
  2093. break;
  2094. }
  2095. default:
  2096. /* jumbo frame ? */
  2097. if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  2098. rc = pn533_fill_fragment_skbs(dev, skb);
  2099. if (rc <= 0)
  2100. goto error;
  2101. skb = skb_dequeue(&dev->fragment_skb);
  2102. if (!skb) {
  2103. rc = -EIO;
  2104. goto error;
  2105. }
  2106. } else {
  2107. *skb_push(skb, sizeof(u8)) = 1; /* TG */
  2108. }
  2109. rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
  2110. skb, pn533_data_exchange_complete,
  2111. arg);
  2112. break;
  2113. }
  2114. if (rc < 0) /* rc from send_async */
  2115. goto error;
  2116. return 0;
  2117. error:
  2118. kfree(arg);
  2119. dev_kfree_skb(skb);
  2120. return rc;
  2121. }
  2122. static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
  2123. struct sk_buff *resp)
  2124. {
  2125. u8 status;
  2126. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  2127. if (IS_ERR(resp))
  2128. return PTR_ERR(resp);
  2129. status = resp->data[0];
  2130. /* Prepare for the next round */
  2131. if (skb_queue_len(&dev->fragment_skb) > 0) {
  2132. queue_work(dev->wq, &dev->mi_tm_tx_work);
  2133. return -EINPROGRESS;
  2134. }
  2135. dev_kfree_skb(resp);
  2136. if (status != 0) {
  2137. nfc_tm_deactivated(dev->nfc_dev);
  2138. dev->tgt_mode = 0;
  2139. return 0;
  2140. }
  2141. queue_work(dev->wq, &dev->tg_work);
  2142. return 0;
  2143. }
  2144. static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  2145. {
  2146. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  2147. int rc;
  2148. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  2149. /* let's split in multiple chunks if size's too big */
  2150. if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  2151. rc = pn533_fill_fragment_skbs(dev, skb);
  2152. if (rc <= 0)
  2153. goto error;
  2154. /* get the first skb */
  2155. skb = skb_dequeue(&dev->fragment_skb);
  2156. if (!skb) {
  2157. rc = -EIO;
  2158. goto error;
  2159. }
  2160. rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
  2161. pn533_tm_send_complete, NULL);
  2162. } else {
  2163. /* Send th skb */
  2164. rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
  2165. pn533_tm_send_complete, NULL);
  2166. }
  2167. error:
  2168. if (rc < 0) {
  2169. dev_kfree_skb(skb);
  2170. skb_queue_purge(&dev->fragment_skb);
  2171. }
  2172. return rc;
  2173. }
  2174. static void pn533_wq_mi_recv(struct work_struct *work)
  2175. {
  2176. struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
  2177. struct sk_buff *skb;
  2178. int rc;
  2179. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  2180. skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
  2181. if (!skb)
  2182. goto error;
  2183. switch (dev->device_type) {
  2184. case PN533_DEVICE_PASORI:
  2185. if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
  2186. rc = pn533_send_cmd_direct_async(dev,
  2187. PN533_CMD_IN_COMM_THRU,
  2188. skb,
  2189. pn533_data_exchange_complete,
  2190. dev->cmd_complete_mi_arg);
  2191. break;
  2192. }
  2193. default:
  2194. *skb_put(skb, sizeof(u8)) = 1; /*TG*/
  2195. rc = pn533_send_cmd_direct_async(dev,
  2196. PN533_CMD_IN_DATA_EXCHANGE,
  2197. skb,
  2198. pn533_data_exchange_complete,
  2199. dev->cmd_complete_mi_arg);
  2200. break;
  2201. }
  2202. if (rc == 0) /* success */
  2203. return;
  2204. nfc_err(&dev->interface->dev,
  2205. "Error %d when trying to perform data_exchange\n", rc);
  2206. dev_kfree_skb(skb);
  2207. kfree(dev->cmd_complete_mi_arg);
  2208. error:
  2209. pn533_send_ack(dev, GFP_KERNEL);
  2210. queue_work(dev->wq, &dev->cmd_work);
  2211. }
  2212. static void pn533_wq_mi_send(struct work_struct *work)
  2213. {
  2214. struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
  2215. struct sk_buff *skb;
  2216. int rc;
  2217. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  2218. /* Grab the first skb in the queue */
  2219. skb = skb_dequeue(&dev->fragment_skb);
  2220. if (skb == NULL) { /* No more data */
  2221. /* Reset the queue for future use */
  2222. skb_queue_head_init(&dev->fragment_skb);
  2223. goto error;
  2224. }
  2225. switch (dev->device_type) {
  2226. case PN533_DEVICE_PASORI:
  2227. if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
  2228. rc = -EIO;
  2229. break;
  2230. }
  2231. rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
  2232. skb,
  2233. pn533_data_exchange_complete,
  2234. dev->cmd_complete_dep_arg);
  2235. break;
  2236. default:
  2237. /* Still some fragments? */
  2238. rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE,
  2239. skb,
  2240. pn533_data_exchange_complete,
  2241. dev->cmd_complete_dep_arg);
  2242. break;
  2243. }
  2244. if (rc == 0) /* success */
  2245. return;
  2246. nfc_err(&dev->interface->dev,
  2247. "Error %d when trying to perform data_exchange\n", rc);
  2248. dev_kfree_skb(skb);
  2249. kfree(dev->cmd_complete_dep_arg);
  2250. error:
  2251. pn533_send_ack(dev, GFP_KERNEL);
  2252. queue_work(dev->wq, &dev->cmd_work);
  2253. }
  2254. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  2255. u8 cfgdata_len)
  2256. {
  2257. struct sk_buff *skb;
  2258. struct sk_buff *resp;
  2259. int skb_len;
  2260. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  2261. skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
  2262. skb = pn533_alloc_skb(dev, skb_len);
  2263. if (!skb)
  2264. return -ENOMEM;
  2265. *skb_put(skb, sizeof(cfgitem)) = cfgitem;
  2266. memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
  2267. resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
  2268. if (IS_ERR(resp))
  2269. return PTR_ERR(resp);
  2270. dev_kfree_skb(resp);
  2271. return 0;
  2272. }
  2273. static int pn533_get_firmware_version(struct pn533 *dev,
  2274. struct pn533_fw_version *fv)
  2275. {
  2276. struct sk_buff *skb;
  2277. struct sk_buff *resp;
  2278. skb = pn533_alloc_skb(dev, 0);
  2279. if (!skb)
  2280. return -ENOMEM;
  2281. resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
  2282. if (IS_ERR(resp))
  2283. return PTR_ERR(resp);
  2284. fv->ic = resp->data[0];
  2285. fv->ver = resp->data[1];
  2286. fv->rev = resp->data[2];
  2287. fv->support = resp->data[3];
  2288. dev_kfree_skb(resp);
  2289. return 0;
  2290. }
  2291. static int pn533_pasori_fw_reset(struct pn533 *dev)
  2292. {
  2293. struct sk_buff *skb;
  2294. struct sk_buff *resp;
  2295. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  2296. skb = pn533_alloc_skb(dev, sizeof(u8));
  2297. if (!skb)
  2298. return -ENOMEM;
  2299. *skb_put(skb, sizeof(u8)) = 0x1;
  2300. resp = pn533_send_cmd_sync(dev, 0x18, skb);
  2301. if (IS_ERR(resp))
  2302. return PTR_ERR(resp);
  2303. dev_kfree_skb(resp);
  2304. return 0;
  2305. }
  2306. struct pn533_acr122_poweron_rdr_arg {
  2307. int rc;
  2308. struct completion done;
  2309. };
  2310. static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
  2311. {
  2312. struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
  2313. dev_dbg(&urb->dev->dev, "%s\n", __func__);
  2314. print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
  2315. urb->transfer_buffer, urb->transfer_buffer_length,
  2316. false);
  2317. arg->rc = urb->status;
  2318. complete(&arg->done);
  2319. }
  2320. static int pn533_acr122_poweron_rdr(struct pn533 *dev)
  2321. {
  2322. /* Power on th reader (CCID cmd) */
  2323. u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
  2324. 0, 0, 0, 0, 0, 0, 3, 0, 0};
  2325. u8 buf[255];
  2326. int rc;
  2327. void *cntx;
  2328. struct pn533_acr122_poweron_rdr_arg arg;
  2329. dev_dbg(&dev->interface->dev, "%s\n", __func__);
  2330. init_completion(&arg.done);
  2331. cntx = dev->in_urb->context; /* backup context */
  2332. dev->in_urb->transfer_buffer = buf;
  2333. dev->in_urb->transfer_buffer_length = 255;
  2334. dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
  2335. dev->in_urb->context = &arg;
  2336. dev->out_urb->transfer_buffer = cmd;
  2337. dev->out_urb->transfer_buffer_length = sizeof(cmd);
  2338. print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
  2339. cmd, sizeof(cmd), false);
  2340. rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
  2341. if (rc) {
  2342. nfc_err(&dev->interface->dev,
  2343. "Reader power on cmd error %d\n", rc);
  2344. return rc;
  2345. }
  2346. rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
  2347. if (rc) {
  2348. nfc_err(&dev->interface->dev,
  2349. "Can't submit reader poweron cmd response %d\n", rc);
  2350. return rc;
  2351. }
  2352. wait_for_completion(&arg.done);
  2353. dev->in_urb->context = cntx; /* restore context */
  2354. return arg.rc;
  2355. }
  2356. static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
  2357. {
  2358. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  2359. u8 rf_field = !!rf;
  2360. int rc;
  2361. rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
  2362. rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
  2363. (u8 *)&rf_field, 1);
  2364. if (rc) {
  2365. nfc_err(&dev->interface->dev, "Error on setting RF field\n");
  2366. return rc;
  2367. }
  2368. return rc;
  2369. }
  2370. static int pn533_dev_up(struct nfc_dev *nfc_dev)
  2371. {
  2372. return pn533_rf_field(nfc_dev, 1);
  2373. }
  2374. static int pn533_dev_down(struct nfc_dev *nfc_dev)
  2375. {
  2376. return pn533_rf_field(nfc_dev, 0);
  2377. }
  2378. static struct nfc_ops pn533_nfc_ops = {
  2379. .dev_up = pn533_dev_up,
  2380. .dev_down = pn533_dev_down,
  2381. .dep_link_up = pn533_dep_link_up,
  2382. .dep_link_down = pn533_dep_link_down,
  2383. .start_poll = pn533_start_poll,
  2384. .stop_poll = pn533_stop_poll,
  2385. .activate_target = pn533_activate_target,
  2386. .deactivate_target = pn533_deactivate_target,
  2387. .im_transceive = pn533_transceive,
  2388. .tm_send = pn533_tm_send,
  2389. };
  2390. static int pn533_setup(struct pn533 *dev)
  2391. {
  2392. struct pn533_config_max_retries max_retries;
  2393. struct pn533_config_timing timing;
  2394. u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
  2395. int rc;
  2396. switch (dev->device_type) {
  2397. case PN533_DEVICE_STD:
  2398. case PN533_DEVICE_PASORI:
  2399. case PN533_DEVICE_ACR122U:
  2400. max_retries.mx_rty_atr = 0x2;
  2401. max_retries.mx_rty_psl = 0x1;
  2402. max_retries.mx_rty_passive_act =
  2403. PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  2404. timing.rfu = PN533_CONFIG_TIMING_102;
  2405. timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
  2406. timing.dep_timeout = PN533_CONFIG_TIMING_204;
  2407. break;
  2408. default:
  2409. nfc_err(&dev->interface->dev, "Unknown device type %d\n",
  2410. dev->device_type);
  2411. return -EINVAL;
  2412. }
  2413. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  2414. (u8 *)&max_retries, sizeof(max_retries));
  2415. if (rc) {
  2416. nfc_err(&dev->interface->dev,
  2417. "Error on setting MAX_RETRIES config\n");
  2418. return rc;
  2419. }
  2420. rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
  2421. (u8 *)&timing, sizeof(timing));
  2422. if (rc) {
  2423. nfc_err(&dev->interface->dev, "Error on setting RF timings\n");
  2424. return rc;
  2425. }
  2426. switch (dev->device_type) {
  2427. case PN533_DEVICE_STD:
  2428. break;
  2429. case PN533_DEVICE_PASORI:
  2430. pn533_pasori_fw_reset(dev);
  2431. rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
  2432. pasori_cfg, 3);
  2433. if (rc) {
  2434. nfc_err(&dev->interface->dev,
  2435. "Error while settings PASORI config\n");
  2436. return rc;
  2437. }
  2438. pn533_pasori_fw_reset(dev);
  2439. break;
  2440. }
  2441. return 0;
  2442. }
  2443. static int pn533_probe(struct usb_interface *interface,
  2444. const struct usb_device_id *id)
  2445. {
  2446. struct pn533_fw_version fw_ver;
  2447. struct pn533 *dev;
  2448. struct usb_host_interface *iface_desc;
  2449. struct usb_endpoint_descriptor *endpoint;
  2450. int in_endpoint = 0;
  2451. int out_endpoint = 0;
  2452. int rc = -ENOMEM;
  2453. int i;
  2454. u32 protocols;
  2455. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2456. if (!dev)
  2457. return -ENOMEM;
  2458. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  2459. dev->interface = interface;
  2460. mutex_init(&dev->cmd_lock);
  2461. iface_desc = interface->cur_altsetting;
  2462. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  2463. endpoint = &iface_desc->endpoint[i].desc;
  2464. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
  2465. in_endpoint = endpoint->bEndpointAddress;
  2466. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
  2467. out_endpoint = endpoint->bEndpointAddress;
  2468. }
  2469. if (!in_endpoint || !out_endpoint) {
  2470. nfc_err(&interface->dev,
  2471. "Could not find bulk-in or bulk-out endpoint\n");
  2472. rc = -ENODEV;
  2473. goto error;
  2474. }
  2475. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  2476. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  2477. if (!dev->in_urb || !dev->out_urb)
  2478. goto error;
  2479. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  2480. usb_rcvbulkpipe(dev->udev, in_endpoint),
  2481. NULL, 0, NULL, dev);
  2482. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  2483. usb_sndbulkpipe(dev->udev, out_endpoint),
  2484. NULL, 0, pn533_send_complete, dev);
  2485. INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
  2486. INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
  2487. INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv);
  2488. INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send);
  2489. INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
  2490. INIT_WORK(&dev->mi_tm_rx_work, pn533_wq_tm_mi_recv);
  2491. INIT_WORK(&dev->mi_tm_tx_work, pn533_wq_tm_mi_send);
  2492. INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll);
  2493. INIT_WORK(&dev->rf_work, pn533_wq_rf);
  2494. dev->wq = alloc_ordered_workqueue("pn533", 0);
  2495. if (dev->wq == NULL)
  2496. goto error;
  2497. init_timer(&dev->listen_timer);
  2498. dev->listen_timer.data = (unsigned long) dev;
  2499. dev->listen_timer.function = pn533_listen_mode_timer;
  2500. skb_queue_head_init(&dev->resp_q);
  2501. skb_queue_head_init(&dev->fragment_skb);
  2502. INIT_LIST_HEAD(&dev->cmd_queue);
  2503. usb_set_intfdata(interface, dev);
  2504. dev->ops = &pn533_std_frame_ops;
  2505. dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
  2506. dev->device_type = id->driver_info;
  2507. switch (dev->device_type) {
  2508. case PN533_DEVICE_STD:
  2509. protocols = PN533_ALL_PROTOCOLS;
  2510. break;
  2511. case PN533_DEVICE_PASORI:
  2512. protocols = PN533_NO_TYPE_B_PROTOCOLS;
  2513. break;
  2514. case PN533_DEVICE_ACR122U:
  2515. protocols = PN533_NO_TYPE_B_PROTOCOLS;
  2516. dev->ops = &pn533_acr122_frame_ops;
  2517. dev->protocol_type = PN533_PROTO_REQ_RESP,
  2518. rc = pn533_acr122_poweron_rdr(dev);
  2519. if (rc < 0) {
  2520. nfc_err(&dev->interface->dev,
  2521. "Couldn't poweron the reader (error %d)\n", rc);
  2522. goto destroy_wq;
  2523. }
  2524. break;
  2525. default:
  2526. nfc_err(&dev->interface->dev, "Unknown device type %d\n",
  2527. dev->device_type);
  2528. rc = -EINVAL;
  2529. goto destroy_wq;
  2530. }
  2531. memset(&fw_ver, 0, sizeof(fw_ver));
  2532. rc = pn533_get_firmware_version(dev, &fw_ver);
  2533. if (rc < 0)
  2534. goto destroy_wq;
  2535. nfc_info(&dev->interface->dev,
  2536. "NXP PN5%02X firmware ver %d.%d now attached\n",
  2537. fw_ver.ic, fw_ver.ver, fw_ver.rev);
  2538. dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
  2539. dev->ops->tx_header_len +
  2540. PN533_CMD_DATAEXCH_HEAD_LEN,
  2541. dev->ops->tx_tail_len);
  2542. if (!dev->nfc_dev) {
  2543. rc = -ENOMEM;
  2544. goto destroy_wq;
  2545. }
  2546. nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
  2547. nfc_set_drvdata(dev->nfc_dev, dev);
  2548. rc = nfc_register_device(dev->nfc_dev);
  2549. if (rc)
  2550. goto free_nfc_dev;
  2551. rc = pn533_setup(dev);
  2552. if (rc)
  2553. goto unregister_nfc_dev;
  2554. return 0;
  2555. unregister_nfc_dev:
  2556. nfc_unregister_device(dev->nfc_dev);
  2557. free_nfc_dev:
  2558. nfc_free_device(dev->nfc_dev);
  2559. destroy_wq:
  2560. destroy_workqueue(dev->wq);
  2561. error:
  2562. usb_free_urb(dev->in_urb);
  2563. usb_free_urb(dev->out_urb);
  2564. usb_put_dev(dev->udev);
  2565. kfree(dev);
  2566. return rc;
  2567. }
  2568. static void pn533_disconnect(struct usb_interface *interface)
  2569. {
  2570. struct pn533 *dev;
  2571. struct pn533_cmd *cmd, *n;
  2572. dev = usb_get_intfdata(interface);
  2573. usb_set_intfdata(interface, NULL);
  2574. nfc_unregister_device(dev->nfc_dev);
  2575. nfc_free_device(dev->nfc_dev);
  2576. usb_kill_urb(dev->in_urb);
  2577. usb_kill_urb(dev->out_urb);
  2578. flush_delayed_work(&dev->poll_work);
  2579. destroy_workqueue(dev->wq);
  2580. skb_queue_purge(&dev->resp_q);
  2581. del_timer(&dev->listen_timer);
  2582. list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
  2583. list_del(&cmd->queue);
  2584. kfree(cmd);
  2585. }
  2586. usb_free_urb(dev->in_urb);
  2587. usb_free_urb(dev->out_urb);
  2588. kfree(dev);
  2589. nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
  2590. }
  2591. static struct usb_driver pn533_driver = {
  2592. .name = "pn533",
  2593. .probe = pn533_probe,
  2594. .disconnect = pn533_disconnect,
  2595. .id_table = pn533_table,
  2596. };
  2597. module_usb_driver(pn533_driver);
  2598. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  2599. MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  2600. MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
  2601. MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
  2602. MODULE_VERSION(VERSION);
  2603. MODULE_LICENSE("GPL");