pn533.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  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. int out_maxlen;
  303. struct pn533_frame *out_frame;
  304. struct urb *in_urb;
  305. int in_maxlen;
  306. struct pn533_frame *in_frame;
  307. struct sk_buff_head resp_q;
  308. struct workqueue_struct *wq;
  309. struct work_struct cmd_work;
  310. struct work_struct cmd_complete_work;
  311. struct work_struct poll_work;
  312. struct work_struct mi_work;
  313. struct work_struct tg_work;
  314. struct timer_list listen_timer;
  315. struct pn533_frame *wq_in_frame;
  316. int wq_in_error;
  317. int cancel_listen;
  318. pn533_cmd_complete_t cmd_complete;
  319. void *cmd_complete_arg;
  320. struct mutex cmd_lock;
  321. u8 cmd;
  322. struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
  323. u8 poll_mod_count;
  324. u8 poll_mod_curr;
  325. u32 poll_protocols;
  326. u32 listen_protocols;
  327. u8 *gb;
  328. size_t gb_len;
  329. u8 tgt_available_prots;
  330. u8 tgt_active_prot;
  331. u8 tgt_mode;
  332. u32 device_type;
  333. struct list_head cmd_queue;
  334. u8 cmd_pending;
  335. };
  336. struct pn533_cmd {
  337. struct list_head queue;
  338. struct pn533_frame *out_frame;
  339. struct pn533_frame *in_frame;
  340. int in_frame_len;
  341. pn533_cmd_complete_t cmd_complete;
  342. void *arg;
  343. gfp_t flags;
  344. };
  345. struct pn533_frame {
  346. u8 preamble;
  347. __be16 start_frame;
  348. u8 datalen;
  349. u8 datalen_checksum;
  350. u8 data[];
  351. } __packed;
  352. /* The rule: value + checksum = 0 */
  353. static inline u8 pn533_checksum(u8 value)
  354. {
  355. return ~value + 1;
  356. }
  357. /* The rule: sum(data elements) + checksum = 0 */
  358. static u8 pn533_data_checksum(u8 *data, int datalen)
  359. {
  360. u8 sum = 0;
  361. int i;
  362. for (i = 0; i < datalen; i++)
  363. sum += data[i];
  364. return pn533_checksum(sum);
  365. }
  366. /**
  367. * pn533_tx_frame_ack - create a ack frame
  368. * @frame: The frame to be set as ack
  369. *
  370. * Ack is different type of standard frame. As a standard frame, it has
  371. * preamble and start_frame. However the checksum of this frame must fail,
  372. * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
  373. * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
  374. * After datalen_checksum field, the postamble is placed.
  375. */
  376. static void pn533_tx_frame_ack(struct pn533_frame *frame)
  377. {
  378. frame->preamble = 0;
  379. frame->start_frame = cpu_to_be16(PN533_SOF);
  380. frame->datalen = 0;
  381. frame->datalen_checksum = 0xFF;
  382. /* data[0] is used as postamble */
  383. frame->data[0] = 0;
  384. }
  385. static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
  386. {
  387. frame->preamble = 0;
  388. frame->start_frame = cpu_to_be16(PN533_SOF);
  389. PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
  390. PN533_FRAME_CMD(frame) = cmd;
  391. frame->datalen = 2;
  392. }
  393. static void pn533_tx_frame_finish(struct pn533_frame *frame)
  394. {
  395. frame->datalen_checksum = pn533_checksum(frame->datalen);
  396. PN533_FRAME_CHECKSUM(frame) =
  397. pn533_data_checksum(frame->data, frame->datalen);
  398. PN533_FRAME_POSTAMBLE(frame) = 0;
  399. }
  400. static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
  401. {
  402. u8 checksum;
  403. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  404. return false;
  405. checksum = pn533_checksum(frame->datalen);
  406. if (checksum != frame->datalen_checksum)
  407. return false;
  408. checksum = pn533_data_checksum(frame->data, frame->datalen);
  409. if (checksum != PN533_FRAME_CHECKSUM(frame))
  410. return false;
  411. return true;
  412. }
  413. static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
  414. {
  415. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  416. return false;
  417. if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
  418. return false;
  419. return true;
  420. }
  421. static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
  422. {
  423. return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
  424. }
  425. static void pn533_wq_cmd_complete(struct work_struct *work)
  426. {
  427. struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
  428. struct pn533_frame *in_frame;
  429. int rc;
  430. in_frame = dev->wq_in_frame;
  431. if (dev->wq_in_error)
  432. rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
  433. dev->wq_in_error);
  434. else
  435. rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
  436. PN533_FRAME_CMD_PARAMS_PTR(in_frame),
  437. PN533_FRAME_CMD_PARAMS_LEN(in_frame));
  438. if (rc != -EINPROGRESS)
  439. queue_work(dev->wq, &dev->cmd_work);
  440. }
  441. static void pn533_recv_response(struct urb *urb)
  442. {
  443. struct pn533 *dev = urb->context;
  444. struct pn533_frame *in_frame;
  445. dev->wq_in_frame = NULL;
  446. switch (urb->status) {
  447. case 0:
  448. /* success */
  449. break;
  450. case -ECONNRESET:
  451. case -ENOENT:
  452. case -ESHUTDOWN:
  453. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  454. " status: %d", urb->status);
  455. dev->wq_in_error = urb->status;
  456. goto sched_wq;
  457. default:
  458. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  459. " %d", urb->status);
  460. dev->wq_in_error = urb->status;
  461. goto sched_wq;
  462. }
  463. in_frame = dev->in_urb->transfer_buffer;
  464. if (!pn533_rx_frame_is_valid(in_frame)) {
  465. nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
  466. dev->wq_in_error = -EIO;
  467. goto sched_wq;
  468. }
  469. if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
  470. nfc_dev_err(&dev->interface->dev, "The received frame is not "
  471. "response to the last command");
  472. dev->wq_in_error = -EIO;
  473. goto sched_wq;
  474. }
  475. nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
  476. dev->wq_in_error = 0;
  477. dev->wq_in_frame = in_frame;
  478. sched_wq:
  479. queue_work(dev->wq, &dev->cmd_complete_work);
  480. }
  481. static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
  482. {
  483. dev->in_urb->complete = pn533_recv_response;
  484. return usb_submit_urb(dev->in_urb, flags);
  485. }
  486. static void pn533_recv_ack(struct urb *urb)
  487. {
  488. struct pn533 *dev = urb->context;
  489. struct pn533_frame *in_frame;
  490. int rc;
  491. switch (urb->status) {
  492. case 0:
  493. /* success */
  494. break;
  495. case -ECONNRESET:
  496. case -ENOENT:
  497. case -ESHUTDOWN:
  498. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  499. " status: %d", urb->status);
  500. dev->wq_in_error = urb->status;
  501. goto sched_wq;
  502. default:
  503. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  504. " %d", urb->status);
  505. dev->wq_in_error = urb->status;
  506. goto sched_wq;
  507. }
  508. in_frame = dev->in_urb->transfer_buffer;
  509. if (!pn533_rx_frame_is_ack(in_frame)) {
  510. nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
  511. dev->wq_in_error = -EIO;
  512. goto sched_wq;
  513. }
  514. nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
  515. rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
  516. if (rc) {
  517. nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
  518. " result %d", rc);
  519. dev->wq_in_error = rc;
  520. goto sched_wq;
  521. }
  522. return;
  523. sched_wq:
  524. dev->wq_in_frame = NULL;
  525. queue_work(dev->wq, &dev->cmd_complete_work);
  526. }
  527. static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
  528. {
  529. dev->in_urb->complete = pn533_recv_ack;
  530. return usb_submit_urb(dev->in_urb, flags);
  531. }
  532. static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
  533. {
  534. int rc;
  535. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  536. pn533_tx_frame_ack(dev->out_frame);
  537. dev->out_urb->transfer_buffer = dev->out_frame;
  538. dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
  539. rc = usb_submit_urb(dev->out_urb, flags);
  540. return rc;
  541. }
  542. static int __pn533_send_cmd_frame_async(struct pn533 *dev,
  543. struct pn533_frame *out_frame,
  544. struct pn533_frame *in_frame,
  545. int in_frame_len,
  546. pn533_cmd_complete_t cmd_complete,
  547. void *arg, gfp_t flags)
  548. {
  549. int rc;
  550. nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
  551. PN533_FRAME_CMD(out_frame));
  552. dev->cmd = PN533_FRAME_CMD(out_frame);
  553. dev->cmd_complete = cmd_complete;
  554. dev->cmd_complete_arg = arg;
  555. dev->out_urb->transfer_buffer = out_frame;
  556. dev->out_urb->transfer_buffer_length =
  557. PN533_FRAME_SIZE(out_frame);
  558. dev->in_urb->transfer_buffer = in_frame;
  559. dev->in_urb->transfer_buffer_length = in_frame_len;
  560. rc = usb_submit_urb(dev->out_urb, flags);
  561. if (rc)
  562. return rc;
  563. rc = pn533_submit_urb_for_ack(dev, flags);
  564. if (rc)
  565. goto error;
  566. return 0;
  567. error:
  568. usb_unlink_urb(dev->out_urb);
  569. return rc;
  570. }
  571. static void pn533_wq_cmd(struct work_struct *work)
  572. {
  573. struct pn533 *dev = container_of(work, struct pn533, cmd_work);
  574. struct pn533_cmd *cmd;
  575. mutex_lock(&dev->cmd_lock);
  576. if (list_empty(&dev->cmd_queue)) {
  577. dev->cmd_pending = 0;
  578. mutex_unlock(&dev->cmd_lock);
  579. return;
  580. }
  581. cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
  582. list_del(&cmd->queue);
  583. mutex_unlock(&dev->cmd_lock);
  584. __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
  585. cmd->in_frame_len, cmd->cmd_complete,
  586. cmd->arg, cmd->flags);
  587. kfree(cmd);
  588. }
  589. static int pn533_send_cmd_frame_async(struct pn533 *dev,
  590. struct pn533_frame *out_frame,
  591. struct pn533_frame *in_frame,
  592. int in_frame_len,
  593. pn533_cmd_complete_t cmd_complete,
  594. void *arg, gfp_t flags)
  595. {
  596. struct pn533_cmd *cmd;
  597. int rc = 0;
  598. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  599. mutex_lock(&dev->cmd_lock);
  600. if (!dev->cmd_pending) {
  601. rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
  602. in_frame_len, cmd_complete,
  603. arg, flags);
  604. if (!rc)
  605. dev->cmd_pending = 1;
  606. goto unlock;
  607. }
  608. nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
  609. cmd = kzalloc(sizeof(struct pn533_cmd), flags);
  610. if (!cmd) {
  611. rc = -ENOMEM;
  612. goto unlock;
  613. }
  614. INIT_LIST_HEAD(&cmd->queue);
  615. cmd->out_frame = out_frame;
  616. cmd->in_frame = in_frame;
  617. cmd->in_frame_len = in_frame_len;
  618. cmd->cmd_complete = cmd_complete;
  619. cmd->arg = arg;
  620. cmd->flags = flags;
  621. list_add_tail(&cmd->queue, &dev->cmd_queue);
  622. unlock:
  623. mutex_unlock(&dev->cmd_lock);
  624. return rc;
  625. }
  626. struct pn533_sync_cmd_response {
  627. int rc;
  628. struct completion done;
  629. };
  630. static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
  631. u8 *params, int params_len)
  632. {
  633. struct pn533_sync_cmd_response *arg = _arg;
  634. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  635. arg->rc = 0;
  636. if (params_len < 0) /* error */
  637. arg->rc = params_len;
  638. complete(&arg->done);
  639. return 0;
  640. }
  641. static int pn533_send_cmd_frame_sync(struct pn533 *dev,
  642. struct pn533_frame *out_frame,
  643. struct pn533_frame *in_frame,
  644. int in_frame_len)
  645. {
  646. int rc;
  647. struct pn533_sync_cmd_response arg;
  648. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  649. init_completion(&arg.done);
  650. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
  651. pn533_sync_cmd_complete, &arg, GFP_KERNEL);
  652. if (rc)
  653. return rc;
  654. wait_for_completion(&arg.done);
  655. return arg.rc;
  656. }
  657. static void pn533_send_complete(struct urb *urb)
  658. {
  659. struct pn533 *dev = urb->context;
  660. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  661. switch (urb->status) {
  662. case 0:
  663. /* success */
  664. break;
  665. case -ECONNRESET:
  666. case -ENOENT:
  667. case -ESHUTDOWN:
  668. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  669. " status: %d", urb->status);
  670. break;
  671. default:
  672. nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
  673. " %d", urb->status);
  674. }
  675. }
  676. struct pn533_target_type_a {
  677. __be16 sens_res;
  678. u8 sel_res;
  679. u8 nfcid_len;
  680. u8 nfcid_data[];
  681. } __packed;
  682. #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
  683. #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
  684. #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
  685. #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
  686. #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
  687. #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
  688. #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
  689. #define PN533_TYPE_A_SEL_PROT_MIFARE 0
  690. #define PN533_TYPE_A_SEL_PROT_ISO14443 1
  691. #define PN533_TYPE_A_SEL_PROT_DEP 2
  692. #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
  693. static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
  694. int target_data_len)
  695. {
  696. u8 ssd;
  697. u8 platconf;
  698. if (target_data_len < sizeof(struct pn533_target_type_a))
  699. return false;
  700. /* The lenght check of nfcid[] and ats[] are not being performed because
  701. the values are not being used */
  702. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  703. ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
  704. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
  705. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  706. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  707. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  708. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  709. return false;
  710. /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
  711. if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
  712. return false;
  713. return true;
  714. }
  715. static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
  716. int tgt_data_len)
  717. {
  718. struct pn533_target_type_a *tgt_type_a;
  719. tgt_type_a = (struct pn533_target_type_a *) tgt_data;
  720. if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
  721. return -EPROTO;
  722. switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
  723. case PN533_TYPE_A_SEL_PROT_MIFARE:
  724. nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
  725. break;
  726. case PN533_TYPE_A_SEL_PROT_ISO14443:
  727. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  728. break;
  729. case PN533_TYPE_A_SEL_PROT_DEP:
  730. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  731. break;
  732. case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
  733. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
  734. NFC_PROTO_NFC_DEP_MASK;
  735. break;
  736. }
  737. nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
  738. nfc_tgt->sel_res = tgt_type_a->sel_res;
  739. nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
  740. memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
  741. return 0;
  742. }
  743. struct pn533_target_felica {
  744. u8 pol_res;
  745. u8 opcode;
  746. u8 nfcid2[8];
  747. u8 pad[8];
  748. /* optional */
  749. u8 syst_code[];
  750. } __packed;
  751. #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
  752. #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
  753. static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
  754. int target_data_len)
  755. {
  756. if (target_data_len < sizeof(struct pn533_target_felica))
  757. return false;
  758. if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
  759. return false;
  760. return true;
  761. }
  762. static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
  763. int tgt_data_len)
  764. {
  765. struct pn533_target_felica *tgt_felica;
  766. tgt_felica = (struct pn533_target_felica *) tgt_data;
  767. if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
  768. return -EPROTO;
  769. if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
  770. tgt_felica->nfcid2[1] ==
  771. PN533_FELICA_SENSF_NFCID2_DEP_B2)
  772. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  773. else
  774. nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
  775. memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
  776. nfc_tgt->sensf_res_len = 9;
  777. return 0;
  778. }
  779. struct pn533_target_jewel {
  780. __be16 sens_res;
  781. u8 jewelid[4];
  782. } __packed;
  783. static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
  784. int target_data_len)
  785. {
  786. u8 ssd;
  787. u8 platconf;
  788. if (target_data_len < sizeof(struct pn533_target_jewel))
  789. return false;
  790. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  791. ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
  792. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
  793. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  794. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  795. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  796. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  797. return false;
  798. return true;
  799. }
  800. static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
  801. int tgt_data_len)
  802. {
  803. struct pn533_target_jewel *tgt_jewel;
  804. tgt_jewel = (struct pn533_target_jewel *) tgt_data;
  805. if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
  806. return -EPROTO;
  807. nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
  808. nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
  809. nfc_tgt->nfcid1_len = 4;
  810. memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
  811. return 0;
  812. }
  813. struct pn533_type_b_prot_info {
  814. u8 bitrate;
  815. u8 fsci_type;
  816. u8 fwi_adc_fo;
  817. } __packed;
  818. #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
  819. #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
  820. #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
  821. struct pn533_type_b_sens_res {
  822. u8 opcode;
  823. u8 nfcid[4];
  824. u8 appdata[4];
  825. struct pn533_type_b_prot_info prot_info;
  826. } __packed;
  827. #define PN533_TYPE_B_OPC_SENSB_RES 0x50
  828. struct pn533_target_type_b {
  829. struct pn533_type_b_sens_res sensb_res;
  830. u8 attrib_res_len;
  831. u8 attrib_res[];
  832. } __packed;
  833. static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
  834. int target_data_len)
  835. {
  836. if (target_data_len < sizeof(struct pn533_target_type_b))
  837. return false;
  838. if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
  839. return false;
  840. if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
  841. PN533_TYPE_B_PROT_TYPE_RFU_MASK)
  842. return false;
  843. return true;
  844. }
  845. static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
  846. int tgt_data_len)
  847. {
  848. struct pn533_target_type_b *tgt_type_b;
  849. tgt_type_b = (struct pn533_target_type_b *) tgt_data;
  850. if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
  851. return -EPROTO;
  852. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
  853. return 0;
  854. }
  855. struct pn533_poll_response {
  856. u8 nbtg;
  857. u8 tg;
  858. u8 target_data[];
  859. } __packed;
  860. static int pn533_target_found(struct pn533 *dev,
  861. struct pn533_poll_response *resp, int resp_len)
  862. {
  863. int target_data_len;
  864. struct nfc_target nfc_tgt;
  865. int rc;
  866. nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
  867. dev->poll_mod_curr);
  868. if (resp->tg != 1)
  869. return -EPROTO;
  870. memset(&nfc_tgt, 0, sizeof(struct nfc_target));
  871. target_data_len = resp_len - sizeof(struct pn533_poll_response);
  872. switch (dev->poll_mod_curr) {
  873. case PN533_POLL_MOD_106KBPS_A:
  874. rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
  875. target_data_len);
  876. break;
  877. case PN533_POLL_MOD_212KBPS_FELICA:
  878. case PN533_POLL_MOD_424KBPS_FELICA:
  879. rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
  880. target_data_len);
  881. break;
  882. case PN533_POLL_MOD_106KBPS_JEWEL:
  883. rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
  884. target_data_len);
  885. break;
  886. case PN533_POLL_MOD_847KBPS_B:
  887. rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
  888. target_data_len);
  889. break;
  890. default:
  891. nfc_dev_err(&dev->interface->dev, "Unknown current poll"
  892. " modulation");
  893. return -EPROTO;
  894. }
  895. if (rc)
  896. return rc;
  897. if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
  898. nfc_dev_dbg(&dev->interface->dev, "The target found does not"
  899. " have the desired protocol");
  900. return -EAGAIN;
  901. }
  902. nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
  903. "0x%x", nfc_tgt.supported_protocols);
  904. dev->tgt_available_prots = nfc_tgt.supported_protocols;
  905. nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
  906. return 0;
  907. }
  908. static inline void pn533_poll_next_mod(struct pn533 *dev)
  909. {
  910. dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
  911. }
  912. static void pn533_poll_reset_mod_list(struct pn533 *dev)
  913. {
  914. dev->poll_mod_count = 0;
  915. }
  916. static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
  917. {
  918. dev->poll_mod_active[dev->poll_mod_count] =
  919. (struct pn533_poll_modulations *) &poll_mod[mod_index];
  920. dev->poll_mod_count++;
  921. }
  922. static void pn533_poll_create_mod_list(struct pn533 *dev,
  923. u32 im_protocols, u32 tm_protocols)
  924. {
  925. pn533_poll_reset_mod_list(dev);
  926. if (im_protocols & NFC_PROTO_MIFARE_MASK
  927. || im_protocols & NFC_PROTO_ISO14443_MASK
  928. || im_protocols & NFC_PROTO_NFC_DEP_MASK)
  929. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
  930. if (im_protocols & NFC_PROTO_FELICA_MASK
  931. || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
  932. pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
  933. pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
  934. }
  935. if (im_protocols & NFC_PROTO_JEWEL_MASK)
  936. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
  937. if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
  938. pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
  939. if (tm_protocols)
  940. pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
  941. }
  942. static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
  943. {
  944. struct pn533_poll_response *resp;
  945. int rc;
  946. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  947. resp = (struct pn533_poll_response *) params;
  948. if (resp->nbtg) {
  949. rc = pn533_target_found(dev, resp, params_len);
  950. /* We must stop the poll after a valid target found */
  951. if (rc == 0) {
  952. pn533_poll_reset_mod_list(dev);
  953. return 0;
  954. }
  955. }
  956. return -EAGAIN;
  957. }
  958. static int pn533_init_target_frame(struct pn533_frame *frame,
  959. u8 *gb, size_t gb_len)
  960. {
  961. struct pn533_cmd_init_target *cmd;
  962. size_t cmd_len;
  963. u8 felica_params[18] = {0x1, 0xfe, /* DEP */
  964. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
  965. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  966. 0xff, 0xff}; /* System code */
  967. u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
  968. 0x0, 0x0, 0x0,
  969. 0x40}; /* SEL_RES for DEP */
  970. cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
  971. cmd = kzalloc(cmd_len, GFP_KERNEL);
  972. if (cmd == NULL)
  973. return -ENOMEM;
  974. pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
  975. /* DEP support only */
  976. cmd->mode |= PN533_INIT_TARGET_DEP;
  977. /* Felica params */
  978. memcpy(cmd->felica, felica_params, 18);
  979. get_random_bytes(cmd->felica + 2, 6);
  980. /* NFCID3 */
  981. memset(cmd->nfcid3, 0, 10);
  982. memcpy(cmd->nfcid3, cmd->felica, 8);
  983. /* MIFARE params */
  984. memcpy(cmd->mifare, mifare_params, 6);
  985. /* General bytes */
  986. cmd->gb_len = gb_len;
  987. memcpy(cmd->gb, gb, gb_len);
  988. /* Len Tk */
  989. cmd->gb[gb_len] = 0;
  990. memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
  991. frame->datalen += cmd_len;
  992. pn533_tx_frame_finish(frame);
  993. kfree(cmd);
  994. return 0;
  995. }
  996. #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
  997. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  998. static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
  999. u8 *params, int params_len)
  1000. {
  1001. struct sk_buff *skb_resp = arg;
  1002. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  1003. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1004. if (params_len < 0) {
  1005. nfc_dev_err(&dev->interface->dev,
  1006. "Error %d when starting as a target",
  1007. params_len);
  1008. return params_len;
  1009. }
  1010. if (params_len > 0 && params[0] != 0) {
  1011. nfc_tm_deactivated(dev->nfc_dev);
  1012. dev->tgt_mode = 0;
  1013. kfree_skb(skb_resp);
  1014. return 0;
  1015. }
  1016. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  1017. skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
  1018. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
  1019. return nfc_tm_data_received(dev->nfc_dev, skb_resp);
  1020. }
  1021. static void pn533_wq_tg_get_data(struct work_struct *work)
  1022. {
  1023. struct pn533 *dev = container_of(work, struct pn533, tg_work);
  1024. struct pn533_frame *in_frame;
  1025. struct sk_buff *skb_resp;
  1026. size_t skb_resp_len;
  1027. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1028. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  1029. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1030. PN533_FRAME_TAIL_SIZE;
  1031. skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
  1032. if (!skb_resp)
  1033. return;
  1034. in_frame = (struct pn533_frame *)skb_resp->data;
  1035. pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
  1036. pn533_tx_frame_finish(dev->out_frame);
  1037. pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
  1038. skb_resp_len,
  1039. pn533_tm_get_data_complete,
  1040. skb_resp, GFP_KERNEL);
  1041. return;
  1042. }
  1043. #define ATR_REQ_GB_OFFSET 17
  1044. static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
  1045. {
  1046. struct pn533_cmd_init_target_response *resp;
  1047. u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
  1048. size_t gb_len;
  1049. int rc;
  1050. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1051. if (params_len < 0) {
  1052. nfc_dev_err(&dev->interface->dev,
  1053. "Error %d when starting as a target",
  1054. params_len);
  1055. return params_len;
  1056. }
  1057. if (params_len < ATR_REQ_GB_OFFSET + 1)
  1058. return -EINVAL;
  1059. resp = (struct pn533_cmd_init_target_response *) params;
  1060. nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
  1061. resp->mode, params_len);
  1062. frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
  1063. if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
  1064. comm_mode = NFC_COMM_ACTIVE;
  1065. /* Again, only DEP */
  1066. if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
  1067. return -EOPNOTSUPP;
  1068. gb = resp->cmd + ATR_REQ_GB_OFFSET;
  1069. gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
  1070. rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
  1071. comm_mode, gb, gb_len);
  1072. if (rc < 0) {
  1073. nfc_dev_err(&dev->interface->dev,
  1074. "Error when signaling target activation");
  1075. return rc;
  1076. }
  1077. dev->tgt_mode = 1;
  1078. queue_work(dev->wq, &dev->tg_work);
  1079. return 0;
  1080. }
  1081. static void pn533_listen_mode_timer(unsigned long data)
  1082. {
  1083. struct pn533 *dev = (struct pn533 *) data;
  1084. nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
  1085. /* An ack will cancel the last issued command (poll) */
  1086. pn533_send_ack(dev, GFP_ATOMIC);
  1087. dev->cancel_listen = 1;
  1088. pn533_poll_next_mod(dev);
  1089. queue_work(dev->wq, &dev->poll_work);
  1090. }
  1091. static int pn533_poll_complete(struct pn533 *dev, void *arg,
  1092. u8 *params, int params_len)
  1093. {
  1094. struct pn533_poll_modulations *cur_mod;
  1095. int rc;
  1096. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1097. if (params_len == -ENOENT) {
  1098. if (dev->poll_mod_count != 0)
  1099. return 0;
  1100. nfc_dev_err(&dev->interface->dev,
  1101. "Polling operation has been stopped");
  1102. goto stop_poll;
  1103. }
  1104. if (params_len < 0) {
  1105. nfc_dev_err(&dev->interface->dev,
  1106. "Error %d when running poll", params_len);
  1107. goto stop_poll;
  1108. }
  1109. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1110. if (cur_mod->len == 0) {
  1111. del_timer(&dev->listen_timer);
  1112. return pn533_init_target_complete(dev, params, params_len);
  1113. } else {
  1114. rc = pn533_start_poll_complete(dev, params, params_len);
  1115. if (!rc)
  1116. return rc;
  1117. }
  1118. pn533_poll_next_mod(dev);
  1119. queue_work(dev->wq, &dev->poll_work);
  1120. return 0;
  1121. stop_poll:
  1122. pn533_poll_reset_mod_list(dev);
  1123. dev->poll_protocols = 0;
  1124. return 0;
  1125. }
  1126. static void pn533_build_poll_frame(struct pn533 *dev,
  1127. struct pn533_frame *frame,
  1128. struct pn533_poll_modulations *mod)
  1129. {
  1130. nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
  1131. if (mod->len == 0) {
  1132. /* Listen mode */
  1133. pn533_init_target_frame(frame, dev->gb, dev->gb_len);
  1134. } else {
  1135. /* Polling mode */
  1136. pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
  1137. memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
  1138. frame->datalen += mod->len;
  1139. pn533_tx_frame_finish(frame);
  1140. }
  1141. }
  1142. static int pn533_send_poll_frame(struct pn533 *dev)
  1143. {
  1144. struct pn533_poll_modulations *cur_mod;
  1145. int rc;
  1146. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1147. pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
  1148. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1149. dev->in_maxlen, pn533_poll_complete,
  1150. NULL, GFP_KERNEL);
  1151. if (rc)
  1152. nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
  1153. return rc;
  1154. }
  1155. static void pn533_wq_poll(struct work_struct *work)
  1156. {
  1157. struct pn533 *dev = container_of(work, struct pn533, poll_work);
  1158. struct pn533_poll_modulations *cur_mod;
  1159. int rc;
  1160. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1161. nfc_dev_dbg(&dev->interface->dev,
  1162. "%s cancel_listen %d modulation len %d",
  1163. __func__, dev->cancel_listen, cur_mod->len);
  1164. if (dev->cancel_listen == 1) {
  1165. dev->cancel_listen = 0;
  1166. usb_kill_urb(dev->in_urb);
  1167. }
  1168. rc = pn533_send_poll_frame(dev);
  1169. if (rc)
  1170. return;
  1171. if (cur_mod->len == 0 && dev->poll_mod_count > 1)
  1172. mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
  1173. return;
  1174. }
  1175. static int pn533_start_poll(struct nfc_dev *nfc_dev,
  1176. u32 im_protocols, u32 tm_protocols)
  1177. {
  1178. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1179. nfc_dev_dbg(&dev->interface->dev,
  1180. "%s: im protocols 0x%x tm protocols 0x%x",
  1181. __func__, im_protocols, tm_protocols);
  1182. if (dev->tgt_active_prot) {
  1183. nfc_dev_err(&dev->interface->dev,
  1184. "Cannot poll with a target already activated");
  1185. return -EBUSY;
  1186. }
  1187. if (dev->tgt_mode) {
  1188. nfc_dev_err(&dev->interface->dev,
  1189. "Cannot poll while already being activated");
  1190. return -EBUSY;
  1191. }
  1192. if (tm_protocols) {
  1193. dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
  1194. if (dev->gb == NULL)
  1195. tm_protocols = 0;
  1196. }
  1197. dev->poll_mod_curr = 0;
  1198. pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
  1199. dev->poll_protocols = im_protocols;
  1200. dev->listen_protocols = tm_protocols;
  1201. return pn533_send_poll_frame(dev);
  1202. }
  1203. static void pn533_stop_poll(struct nfc_dev *nfc_dev)
  1204. {
  1205. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1206. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1207. del_timer(&dev->listen_timer);
  1208. if (!dev->poll_mod_count) {
  1209. nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
  1210. " running");
  1211. return;
  1212. }
  1213. /* An ack will cancel the last issued command (poll) */
  1214. pn533_send_ack(dev, GFP_KERNEL);
  1215. /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
  1216. usb_kill_urb(dev->in_urb);
  1217. pn533_poll_reset_mod_list(dev);
  1218. }
  1219. static int pn533_activate_target_nfcdep(struct pn533 *dev)
  1220. {
  1221. struct pn533_cmd_activate_param param;
  1222. struct pn533_cmd_activate_response *resp;
  1223. u16 gt_len;
  1224. int rc;
  1225. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1226. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
  1227. param.tg = 1;
  1228. param.next = 0;
  1229. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
  1230. sizeof(struct pn533_cmd_activate_param));
  1231. dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
  1232. pn533_tx_frame_finish(dev->out_frame);
  1233. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1234. dev->in_maxlen);
  1235. if (rc)
  1236. return rc;
  1237. resp = (struct pn533_cmd_activate_response *)
  1238. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1239. rc = resp->status & PN533_CMD_RET_MASK;
  1240. if (rc != PN533_CMD_RET_SUCCESS)
  1241. return -EIO;
  1242. /* ATR_RES general bytes are located at offset 16 */
  1243. gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
  1244. rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
  1245. return rc;
  1246. }
  1247. static int pn533_activate_target(struct nfc_dev *nfc_dev,
  1248. struct nfc_target *target, u32 protocol)
  1249. {
  1250. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1251. int rc;
  1252. nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
  1253. protocol);
  1254. if (dev->poll_mod_count) {
  1255. nfc_dev_err(&dev->interface->dev, "Cannot activate while"
  1256. " polling");
  1257. return -EBUSY;
  1258. }
  1259. if (dev->tgt_active_prot) {
  1260. nfc_dev_err(&dev->interface->dev, "There is already an active"
  1261. " target");
  1262. return -EBUSY;
  1263. }
  1264. if (!dev->tgt_available_prots) {
  1265. nfc_dev_err(&dev->interface->dev, "There is no available target"
  1266. " to activate");
  1267. return -EINVAL;
  1268. }
  1269. if (!(dev->tgt_available_prots & (1 << protocol))) {
  1270. nfc_dev_err(&dev->interface->dev, "The target does not support"
  1271. " the requested protocol %u", protocol);
  1272. return -EINVAL;
  1273. }
  1274. if (protocol == NFC_PROTO_NFC_DEP) {
  1275. rc = pn533_activate_target_nfcdep(dev);
  1276. if (rc) {
  1277. nfc_dev_err(&dev->interface->dev, "Error %d when"
  1278. " activating target with"
  1279. " NFC_DEP protocol", rc);
  1280. return rc;
  1281. }
  1282. }
  1283. dev->tgt_active_prot = protocol;
  1284. dev->tgt_available_prots = 0;
  1285. return 0;
  1286. }
  1287. static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
  1288. struct nfc_target *target)
  1289. {
  1290. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1291. u8 tg;
  1292. u8 status;
  1293. int rc;
  1294. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1295. if (!dev->tgt_active_prot) {
  1296. nfc_dev_err(&dev->interface->dev, "There is no active target");
  1297. return;
  1298. }
  1299. dev->tgt_active_prot = 0;
  1300. skb_queue_purge(&dev->resp_q);
  1301. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
  1302. tg = 1;
  1303. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
  1304. dev->out_frame->datalen += sizeof(u8);
  1305. pn533_tx_frame_finish(dev->out_frame);
  1306. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1307. dev->in_maxlen);
  1308. if (rc) {
  1309. nfc_dev_err(&dev->interface->dev, "Error when sending release"
  1310. " command to the controller");
  1311. return;
  1312. }
  1313. status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
  1314. rc = status & PN533_CMD_RET_MASK;
  1315. if (rc != PN533_CMD_RET_SUCCESS)
  1316. nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
  1317. " the target", rc);
  1318. return;
  1319. }
  1320. static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
  1321. u8 *params, int params_len)
  1322. {
  1323. struct pn533_cmd_jump_dep_response *resp;
  1324. struct nfc_target nfc_target;
  1325. u8 target_gt_len;
  1326. int rc;
  1327. struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
  1328. u8 active = cmd->active;
  1329. kfree(arg);
  1330. if (params_len == -ENOENT) {
  1331. nfc_dev_dbg(&dev->interface->dev, "");
  1332. return 0;
  1333. }
  1334. if (params_len < 0) {
  1335. nfc_dev_err(&dev->interface->dev,
  1336. "Error %d when bringing DEP link up",
  1337. params_len);
  1338. return 0;
  1339. }
  1340. if (dev->tgt_available_prots &&
  1341. !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
  1342. nfc_dev_err(&dev->interface->dev,
  1343. "The target does not support DEP");
  1344. return -EINVAL;
  1345. }
  1346. resp = (struct pn533_cmd_jump_dep_response *) params;
  1347. rc = resp->status & PN533_CMD_RET_MASK;
  1348. if (rc != PN533_CMD_RET_SUCCESS) {
  1349. nfc_dev_err(&dev->interface->dev,
  1350. "Bringing DEP link up failed %d", rc);
  1351. return 0;
  1352. }
  1353. if (!dev->tgt_available_prots) {
  1354. nfc_dev_dbg(&dev->interface->dev, "Creating new target");
  1355. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1356. nfc_target.nfcid1_len = 10;
  1357. memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
  1358. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1359. if (rc)
  1360. return 0;
  1361. dev->tgt_available_prots = 0;
  1362. }
  1363. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1364. /* ATR_RES general bytes are located at offset 17 */
  1365. target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
  1366. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1367. resp->gt, target_gt_len);
  1368. if (rc == 0)
  1369. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1370. dev->nfc_dev->targets[0].idx,
  1371. !active, NFC_RF_INITIATOR);
  1372. return 0;
  1373. }
  1374. static int pn533_mod_to_baud(struct pn533 *dev)
  1375. {
  1376. switch (dev->poll_mod_curr) {
  1377. case PN533_POLL_MOD_106KBPS_A:
  1378. return 0;
  1379. case PN533_POLL_MOD_212KBPS_FELICA:
  1380. return 1;
  1381. case PN533_POLL_MOD_424KBPS_FELICA:
  1382. return 2;
  1383. default:
  1384. return -EINVAL;
  1385. }
  1386. }
  1387. #define PASSIVE_DATA_LEN 5
  1388. static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  1389. u8 comm_mode, u8* gb, size_t gb_len)
  1390. {
  1391. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1392. struct pn533_cmd_jump_dep *cmd;
  1393. u8 cmd_len, *data_ptr;
  1394. u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
  1395. int rc, baud;
  1396. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1397. if (dev->poll_mod_count) {
  1398. nfc_dev_err(&dev->interface->dev,
  1399. "Cannot bring the DEP link up while polling");
  1400. return -EBUSY;
  1401. }
  1402. if (dev->tgt_active_prot) {
  1403. nfc_dev_err(&dev->interface->dev,
  1404. "There is already an active target");
  1405. return -EBUSY;
  1406. }
  1407. baud = pn533_mod_to_baud(dev);
  1408. if (baud < 0) {
  1409. nfc_dev_err(&dev->interface->dev,
  1410. "Invalid curr modulation %d", dev->poll_mod_curr);
  1411. return baud;
  1412. }
  1413. cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
  1414. if (comm_mode == NFC_COMM_PASSIVE)
  1415. cmd_len += PASSIVE_DATA_LEN;
  1416. cmd = kzalloc(cmd_len, GFP_KERNEL);
  1417. if (cmd == NULL)
  1418. return -ENOMEM;
  1419. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
  1420. cmd->active = !comm_mode;
  1421. cmd->next = 0;
  1422. cmd->baud = baud;
  1423. data_ptr = cmd->data;
  1424. if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
  1425. memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
  1426. cmd->next |= 1;
  1427. data_ptr += PASSIVE_DATA_LEN;
  1428. }
  1429. if (gb != NULL && gb_len > 0) {
  1430. cmd->next |= 4; /* We have some Gi */
  1431. memcpy(data_ptr, gb, gb_len);
  1432. } else {
  1433. cmd->next = 0;
  1434. }
  1435. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
  1436. dev->out_frame->datalen += cmd_len;
  1437. pn533_tx_frame_finish(dev->out_frame);
  1438. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1439. dev->in_maxlen, pn533_in_dep_link_up_complete,
  1440. cmd, 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. dev->in_maxlen, pn533_tm_send_complete,
  1670. skb, GFP_KERNEL);
  1671. if (rc) {
  1672. nfc_dev_err(&dev->interface->dev,
  1673. "Error %d when trying to send data", rc);
  1674. goto error;
  1675. }
  1676. return 0;
  1677. error:
  1678. kfree_skb(skb);
  1679. return rc;
  1680. }
  1681. static void pn533_wq_mi_recv(struct work_struct *work)
  1682. {
  1683. struct pn533 *dev = container_of(work, struct pn533, mi_work);
  1684. struct sk_buff *skb_cmd;
  1685. struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
  1686. struct pn533_frame *out_frame, *in_frame;
  1687. struct sk_buff *skb_resp;
  1688. int skb_resp_len;
  1689. int rc;
  1690. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1691. /* This is a zero payload size skb */
  1692. skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE,
  1693. GFP_KERNEL);
  1694. if (skb_cmd == NULL)
  1695. goto error_cmd;
  1696. skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
  1697. rc = pn533_build_tx_frame(dev, skb_cmd, true);
  1698. if (rc)
  1699. goto error_frame;
  1700. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  1701. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1702. PN533_FRAME_TAIL_SIZE;
  1703. skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
  1704. if (!skb_resp) {
  1705. rc = -ENOMEM;
  1706. goto error_frame;
  1707. }
  1708. in_frame = (struct pn533_frame *) skb_resp->data;
  1709. out_frame = (struct pn533_frame *) skb_cmd->data;
  1710. arg->skb_resp = skb_resp;
  1711. arg->skb_out = skb_cmd;
  1712. rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
  1713. skb_resp_len,
  1714. pn533_data_exchange_complete,
  1715. dev->cmd_complete_arg, GFP_KERNEL);
  1716. if (!rc)
  1717. return;
  1718. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1719. " perform data_exchange", rc);
  1720. kfree_skb(skb_resp);
  1721. error_frame:
  1722. kfree_skb(skb_cmd);
  1723. error_cmd:
  1724. pn533_send_ack(dev, GFP_KERNEL);
  1725. kfree(arg);
  1726. queue_work(dev->wq, &dev->cmd_work);
  1727. }
  1728. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  1729. u8 cfgdata_len)
  1730. {
  1731. int rc;
  1732. u8 *params;
  1733. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1734. pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
  1735. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1736. params[0] = cfgitem;
  1737. memcpy(&params[1], cfgdata, cfgdata_len);
  1738. dev->out_frame->datalen += (1 + cfgdata_len);
  1739. pn533_tx_frame_finish(dev->out_frame);
  1740. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1741. dev->in_maxlen);
  1742. return rc;
  1743. }
  1744. static int pn533_fw_reset(struct pn533 *dev)
  1745. {
  1746. int rc;
  1747. u8 *params;
  1748. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1749. pn533_tx_frame_init(dev->out_frame, 0x18);
  1750. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1751. params[0] = 0x1;
  1752. dev->out_frame->datalen += 1;
  1753. pn533_tx_frame_finish(dev->out_frame);
  1754. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1755. dev->in_maxlen);
  1756. return rc;
  1757. }
  1758. static struct nfc_ops pn533_nfc_ops = {
  1759. .dev_up = NULL,
  1760. .dev_down = NULL,
  1761. .dep_link_up = pn533_dep_link_up,
  1762. .dep_link_down = pn533_dep_link_down,
  1763. .start_poll = pn533_start_poll,
  1764. .stop_poll = pn533_stop_poll,
  1765. .activate_target = pn533_activate_target,
  1766. .deactivate_target = pn533_deactivate_target,
  1767. .im_transceive = pn533_transceive,
  1768. .tm_send = pn533_tm_send,
  1769. };
  1770. static int pn533_setup(struct pn533 *dev)
  1771. {
  1772. struct pn533_config_max_retries max_retries;
  1773. struct pn533_config_timing timing;
  1774. u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
  1775. int rc;
  1776. switch (dev->device_type) {
  1777. case PN533_DEVICE_STD:
  1778. max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
  1779. max_retries.mx_rty_psl = 2;
  1780. max_retries.mx_rty_passive_act =
  1781. PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1782. timing.rfu = PN533_CONFIG_TIMING_102;
  1783. timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
  1784. timing.dep_timeout = PN533_CONFIG_TIMING_409;
  1785. break;
  1786. case PN533_DEVICE_PASORI:
  1787. max_retries.mx_rty_atr = 0x2;
  1788. max_retries.mx_rty_psl = 0x1;
  1789. max_retries.mx_rty_passive_act =
  1790. PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1791. timing.rfu = PN533_CONFIG_TIMING_102;
  1792. timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
  1793. timing.dep_timeout = PN533_CONFIG_TIMING_204;
  1794. break;
  1795. default:
  1796. nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
  1797. dev->device_type);
  1798. return -EINVAL;
  1799. }
  1800. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  1801. (u8 *)&max_retries, sizeof(max_retries));
  1802. if (rc) {
  1803. nfc_dev_err(&dev->interface->dev,
  1804. "Error on setting MAX_RETRIES config");
  1805. return rc;
  1806. }
  1807. rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
  1808. (u8 *)&timing, sizeof(timing));
  1809. if (rc) {
  1810. nfc_dev_err(&dev->interface->dev,
  1811. "Error on setting RF timings");
  1812. return rc;
  1813. }
  1814. switch (dev->device_type) {
  1815. case PN533_DEVICE_STD:
  1816. break;
  1817. case PN533_DEVICE_PASORI:
  1818. pn533_fw_reset(dev);
  1819. rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
  1820. pasori_cfg, 3);
  1821. if (rc) {
  1822. nfc_dev_err(&dev->interface->dev,
  1823. "Error while settings PASORI config");
  1824. return rc;
  1825. }
  1826. pn533_fw_reset(dev);
  1827. break;
  1828. }
  1829. return 0;
  1830. }
  1831. static int pn533_probe(struct usb_interface *interface,
  1832. const struct usb_device_id *id)
  1833. {
  1834. struct pn533_fw_version *fw_ver;
  1835. struct pn533 *dev;
  1836. struct usb_host_interface *iface_desc;
  1837. struct usb_endpoint_descriptor *endpoint;
  1838. int in_endpoint = 0;
  1839. int out_endpoint = 0;
  1840. int rc = -ENOMEM;
  1841. int i;
  1842. u32 protocols;
  1843. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1844. if (!dev)
  1845. return -ENOMEM;
  1846. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  1847. dev->interface = interface;
  1848. mutex_init(&dev->cmd_lock);
  1849. iface_desc = interface->cur_altsetting;
  1850. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1851. endpoint = &iface_desc->endpoint[i].desc;
  1852. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1853. dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
  1854. in_endpoint = endpoint->bEndpointAddress;
  1855. }
  1856. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
  1857. dev->out_maxlen =
  1858. le16_to_cpu(endpoint->wMaxPacketSize);
  1859. out_endpoint = endpoint->bEndpointAddress;
  1860. }
  1861. }
  1862. if (!in_endpoint || !out_endpoint) {
  1863. nfc_dev_err(&interface->dev, "Could not find bulk-in or"
  1864. " bulk-out endpoint");
  1865. rc = -ENODEV;
  1866. goto error;
  1867. }
  1868. dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
  1869. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1870. dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
  1871. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  1872. if (!dev->in_frame || !dev->out_frame ||
  1873. !dev->in_urb || !dev->out_urb)
  1874. goto error;
  1875. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  1876. usb_rcvbulkpipe(dev->udev, in_endpoint),
  1877. NULL, 0, NULL, dev);
  1878. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  1879. usb_sndbulkpipe(dev->udev, out_endpoint),
  1880. NULL, 0,
  1881. pn533_send_complete, dev);
  1882. INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
  1883. INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
  1884. INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
  1885. INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
  1886. INIT_WORK(&dev->poll_work, pn533_wq_poll);
  1887. dev->wq = alloc_ordered_workqueue("pn533", 0);
  1888. if (dev->wq == NULL)
  1889. goto error;
  1890. init_timer(&dev->listen_timer);
  1891. dev->listen_timer.data = (unsigned long) dev;
  1892. dev->listen_timer.function = pn533_listen_mode_timer;
  1893. skb_queue_head_init(&dev->resp_q);
  1894. INIT_LIST_HEAD(&dev->cmd_queue);
  1895. usb_set_intfdata(interface, dev);
  1896. pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
  1897. pn533_tx_frame_finish(dev->out_frame);
  1898. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1899. dev->in_maxlen);
  1900. if (rc)
  1901. goto destroy_wq;
  1902. fw_ver = (struct pn533_fw_version *)
  1903. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1904. nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
  1905. " attached", fw_ver->ver, fw_ver->rev);
  1906. dev->device_type = id->driver_info;
  1907. switch (dev->device_type) {
  1908. case PN533_DEVICE_STD:
  1909. protocols = PN533_ALL_PROTOCOLS;
  1910. break;
  1911. case PN533_DEVICE_PASORI:
  1912. protocols = PN533_NO_TYPE_B_PROTOCOLS;
  1913. break;
  1914. default:
  1915. nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
  1916. dev->device_type);
  1917. rc = -EINVAL;
  1918. goto destroy_wq;
  1919. }
  1920. dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
  1921. PN533_CMD_DATAEXCH_HEAD_LEN,
  1922. PN533_FRAME_TAIL_SIZE);
  1923. if (!dev->nfc_dev)
  1924. goto destroy_wq;
  1925. nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
  1926. nfc_set_drvdata(dev->nfc_dev, dev);
  1927. rc = nfc_register_device(dev->nfc_dev);
  1928. if (rc)
  1929. goto free_nfc_dev;
  1930. rc = pn533_setup(dev);
  1931. if (rc)
  1932. goto unregister_nfc_dev;
  1933. return 0;
  1934. unregister_nfc_dev:
  1935. nfc_unregister_device(dev->nfc_dev);
  1936. free_nfc_dev:
  1937. nfc_free_device(dev->nfc_dev);
  1938. destroy_wq:
  1939. destroy_workqueue(dev->wq);
  1940. error:
  1941. kfree(dev->in_frame);
  1942. usb_free_urb(dev->in_urb);
  1943. kfree(dev->out_frame);
  1944. usb_free_urb(dev->out_urb);
  1945. kfree(dev);
  1946. return rc;
  1947. }
  1948. static void pn533_disconnect(struct usb_interface *interface)
  1949. {
  1950. struct pn533 *dev;
  1951. struct pn533_cmd *cmd, *n;
  1952. dev = usb_get_intfdata(interface);
  1953. usb_set_intfdata(interface, NULL);
  1954. nfc_unregister_device(dev->nfc_dev);
  1955. nfc_free_device(dev->nfc_dev);
  1956. usb_kill_urb(dev->in_urb);
  1957. usb_kill_urb(dev->out_urb);
  1958. destroy_workqueue(dev->wq);
  1959. skb_queue_purge(&dev->resp_q);
  1960. del_timer(&dev->listen_timer);
  1961. list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
  1962. list_del(&cmd->queue);
  1963. kfree(cmd);
  1964. }
  1965. kfree(dev->in_frame);
  1966. usb_free_urb(dev->in_urb);
  1967. kfree(dev->out_frame);
  1968. usb_free_urb(dev->out_urb);
  1969. kfree(dev);
  1970. nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
  1971. }
  1972. static struct usb_driver pn533_driver = {
  1973. .name = "pn533",
  1974. .probe = pn533_probe,
  1975. .disconnect = pn533_disconnect,
  1976. .id_table = pn533_table,
  1977. };
  1978. module_usb_driver(pn533_driver);
  1979. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
  1980. " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  1981. MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
  1982. MODULE_VERSION(VERSION);
  1983. MODULE_LICENSE("GPL");