pn533.c 59 KB

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