pn533.c 62 KB

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