pn533.c 57 KB

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