pn533.c 47 KB

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