Skip to content

Commit 9009943

Browse files
Eric Lapuyadelinvjw
authored andcommitted
NFC: Cache the core NFC active target pointer instead of its index
The NFC Core now caches the active nfc target pointer, thereby avoiding the need to lookup the target table for each invocation of a driver ops. Consequently, pn533, HCI and NCI now directly receive an nfc_target pointer instead of a target index. Cc: Ilan Elias <ilane@ti.com> Signed-off-by: Eric Lapuyade <eric.lapuyade@intel.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com> Signed-off-by: John W. Linville <linville@tuxdriver.com>
1 parent 536acc0 commit 9009943

5 files changed

Lines changed: 99 additions & 79 deletions

File tree

‎drivers/nfc/pn533.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,8 +1194,8 @@ static int pn533_activate_target_nfcdep(struct pn533 *dev)
11941194
return rc;
11951195
}
11961196

1197-
static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
1198-
u32 protocol)
1197+
static int pn533_activate_target(struct nfc_dev *nfc_dev,
1198+
struct nfc_target *target, u32 protocol)
11991199
{
12001200
struct pn533 *dev = nfc_get_drvdata(nfc_dev);
12011201
int rc;
@@ -1243,7 +1243,8 @@ static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
12431243
return 0;
12441244
}
12451245

1246-
static void pn533_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
1246+
static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1247+
struct nfc_target *target)
12471248
{
12481249
struct pn533 *dev = nfc_get_drvdata(nfc_dev);
12491250
u8 tg;
@@ -1351,7 +1352,7 @@ static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
13511352
return 0;
13521353
}
13531354

1354-
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, int target_idx,
1355+
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
13551356
u8 comm_mode, u8* gb, size_t gb_len)
13561357
{
13571358
struct pn533 *dev = nfc_get_drvdata(nfc_dev);
@@ -1552,10 +1553,9 @@ static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
15521553
return 0;
15531554
}
15541555

1555-
static int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
1556-
struct sk_buff *skb,
1557-
data_exchange_cb_t cb,
1558-
void *cb_context)
1556+
static int pn533_data_exchange(struct nfc_dev *nfc_dev,
1557+
struct nfc_target *target, struct sk_buff *skb,
1558+
data_exchange_cb_t cb, void *cb_context)
15591559
{
15601560
struct pn533 *dev = nfc_get_drvdata(nfc_dev);
15611561
struct pn533_frame *out_frame, *in_frame;

‎include/net/nfc/nfc.h‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,28 @@ struct nfc_dev;
4848
typedef void (*data_exchange_cb_t)(void *context, struct sk_buff *skb,
4949
int err);
5050

51+
struct nfc_target;
52+
5153
struct nfc_ops {
5254
int (*dev_up)(struct nfc_dev *dev);
5355
int (*dev_down)(struct nfc_dev *dev);
5456
int (*start_poll)(struct nfc_dev *dev, u32 protocols);
5557
void (*stop_poll)(struct nfc_dev *dev);
56-
int (*dep_link_up)(struct nfc_dev *dev, int target_idx, u8 comm_mode,
57-
u8 *gb, size_t gb_len);
58+
int (*dep_link_up)(struct nfc_dev *dev, struct nfc_target *target,
59+
u8 comm_mode, u8 *gb, size_t gb_len);
5860
int (*dep_link_down)(struct nfc_dev *dev);
59-
int (*activate_target)(struct nfc_dev *dev, u32 target_idx,
61+
int (*activate_target)(struct nfc_dev *dev, struct nfc_target *target,
6062
u32 protocol);
61-
void (*deactivate_target)(struct nfc_dev *dev, u32 target_idx);
62-
int (*data_exchange)(struct nfc_dev *dev, u32 target_idx,
63+
void (*deactivate_target)(struct nfc_dev *dev,
64+
struct nfc_target *target);
65+
int (*data_exchange)(struct nfc_dev *dev, struct nfc_target *target,
6366
struct sk_buff *skb, data_exchange_cb_t cb,
6467
void *cb_context);
65-
int (*check_presence)(struct nfc_dev *dev, u32 target_idx);
68+
int (*check_presence)(struct nfc_dev *dev, struct nfc_target *target);
6669
};
6770

6871
#define NFC_TARGET_IDX_ANY -1
6972
#define NFC_MAX_GT_LEN 48
70-
#define NFC_TARGET_IDX_NONE 0xffffffff
7173

7274
struct nfc_target {
7375
u32 idx;
@@ -99,7 +101,7 @@ struct nfc_dev {
99101
struct device dev;
100102
bool dev_up;
101103
bool polling;
102-
u32 activated_target_idx;
104+
struct nfc_target *active_target;
103105
bool dep_link_up;
104106
u32 dep_rf_mode;
105107
struct nfc_genl_data genl_data;

‎net/nfc/core.c‎

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int nfc_dev_down(struct nfc_dev *dev)
9797
goto error;
9898
}
9999

100-
if (dev->polling || dev->activated_target_idx != NFC_TARGET_IDX_NONE) {
100+
if (dev->polling || dev->active_target) {
101101
rc = -EBUSY;
102102
goto error;
103103
}
@@ -183,11 +183,27 @@ int nfc_stop_poll(struct nfc_dev *dev)
183183
return rc;
184184
}
185185

186+
static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
187+
{
188+
int i;
189+
190+
if (dev->n_targets == 0)
191+
return NULL;
192+
193+
for (i = 0; i < dev->n_targets ; i++) {
194+
if (dev->targets[i].idx == target_idx)
195+
return &dev->targets[i];
196+
}
197+
198+
return NULL;
199+
}
200+
186201
int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
187202
{
188203
int rc = 0;
189204
u8 *gb;
190205
size_t gb_len;
206+
struct nfc_target *target;
191207

192208
pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
193209

@@ -212,9 +228,15 @@ int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
212228
goto error;
213229
}
214230

215-
rc = dev->ops->dep_link_up(dev, target_index, comm_mode, gb, gb_len);
231+
target = nfc_find_target(dev, target_index);
232+
if (target == NULL) {
233+
rc = -ENOTCONN;
234+
goto error;
235+
}
236+
237+
rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
216238
if (!rc)
217-
dev->activated_target_idx = target_index;
239+
dev->active_target = target;
218240

219241
error:
220242
device_unlock(&dev->dev);
@@ -250,7 +272,7 @@ int nfc_dep_link_down(struct nfc_dev *dev)
250272
rc = dev->ops->dep_link_down(dev);
251273
if (!rc) {
252274
dev->dep_link_up = false;
253-
dev->activated_target_idx = NFC_TARGET_IDX_NONE;
275+
dev->active_target = NULL;
254276
nfc_llcp_mac_is_down(dev);
255277
nfc_genl_dep_link_down_event(dev);
256278
}
@@ -282,6 +304,7 @@ EXPORT_SYMBOL(nfc_dep_link_is_up);
282304
int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
283305
{
284306
int rc;
307+
struct nfc_target *target;
285308

286309
pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
287310
dev_name(&dev->dev), target_idx, protocol);
@@ -293,9 +316,20 @@ int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
293316
goto error;
294317
}
295318

296-
rc = dev->ops->activate_target(dev, target_idx, protocol);
319+
if (dev->active_target) {
320+
rc = -EBUSY;
321+
goto error;
322+
}
323+
324+
target = nfc_find_target(dev, target_idx);
325+
if (target == NULL) {
326+
rc = -ENOTCONN;
327+
goto error;
328+
}
329+
330+
rc = dev->ops->activate_target(dev, target, protocol);
297331
if (!rc) {
298-
dev->activated_target_idx = target_idx;
332+
dev->active_target = target;
299333

300334
if (dev->ops->check_presence)
301335
mod_timer(&dev->check_pres_timer, jiffies +
@@ -327,11 +361,21 @@ int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
327361
goto error;
328362
}
329363

364+
if (dev->active_target == NULL) {
365+
rc = -ENOTCONN;
366+
goto error;
367+
}
368+
369+
if (dev->active_target->idx != target_idx) {
370+
rc = -ENOTCONN;
371+
goto error;
372+
}
373+
330374
if (dev->ops->check_presence)
331375
del_timer_sync(&dev->check_pres_timer);
332376

333-
dev->ops->deactivate_target(dev, target_idx);
334-
dev->activated_target_idx = NFC_TARGET_IDX_NONE;
377+
dev->ops->deactivate_target(dev, dev->active_target);
378+
dev->active_target = NULL;
335379

336380
error:
337381
device_unlock(&dev->dev);
@@ -365,13 +409,13 @@ int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
365409
goto error;
366410
}
367411

368-
if (dev->activated_target_idx == NFC_TARGET_IDX_NONE) {
412+
if (dev->active_target == NULL) {
369413
rc = -ENOTCONN;
370414
kfree_skb(skb);
371415
goto error;
372416
}
373417

374-
if (target_idx != dev->activated_target_idx) {
418+
if (dev->active_target->idx != target_idx) {
375419
rc = -EADDRNOTAVAIL;
376420
kfree_skb(skb);
377421
goto error;
@@ -380,7 +424,8 @@ int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
380424
if (dev->ops->check_presence)
381425
del_timer_sync(&dev->check_pres_timer);
382426

383-
rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context);
427+
rc = dev->ops->data_exchange(dev, dev->active_target, skb, cb,
428+
cb_context);
384429

385430
if (!rc && dev->ops->check_presence)
386431
mod_timer(&dev->check_pres_timer, jiffies +
@@ -514,7 +559,7 @@ int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
514559

515560
dev->targets_generation++;
516561
dev->n_targets--;
517-
dev->activated_target_idx = NFC_TARGET_IDX_NONE;
562+
dev->active_target = NULL;
518563

519564
if (dev->n_targets) {
520565
memcpy(&dev->targets[i], &dev->targets[i + 1],
@@ -556,15 +601,14 @@ static void nfc_check_pres_work(struct work_struct *work)
556601

557602
device_lock(&dev->dev);
558603

559-
if (dev->activated_target_idx != NFC_TARGET_IDX_NONE &&
560-
timer_pending(&dev->check_pres_timer) == 0) {
561-
rc = dev->ops->check_presence(dev, dev->activated_target_idx);
604+
if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
605+
rc = dev->ops->check_presence(dev, dev->active_target);
562606
if (!rc) {
563607
mod_timer(&dev->check_pres_timer, jiffies +
564608
msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
565609
} else {
566-
nfc_target_lost(dev, dev->activated_target_idx);
567-
dev->activated_target_idx = NFC_TARGET_IDX_NONE;
610+
nfc_target_lost(dev, dev->active_target->idx);
611+
dev->active_target = NULL;
568612
}
569613
}
570614

@@ -643,8 +687,6 @@ struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
643687
/* first generation must not be 0 */
644688
dev->targets_generation = 1;
645689

646-
dev->activated_target_idx = NFC_TARGET_IDX_NONE;
647-
648690
if (ops->check_presence) {
649691
char name[32];
650692
init_timer(&dev->check_pres_timer);
@@ -662,7 +704,6 @@ struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
662704
}
663705
}
664706

665-
666707
return dev;
667708
}
668709
EXPORT_SYMBOL(nfc_allocate_device);

‎net/nfc/hci/core.c‎

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -520,50 +520,26 @@ static void hci_stop_poll(struct nfc_dev *nfc_dev)
520520
}
521521
}
522522

523-
static struct nfc_target *hci_find_target(struct nfc_hci_dev *hdev,
524-
u32 target_idx)
523+
static int hci_activate_target(struct nfc_dev *nfc_dev,
524+
struct nfc_target *target, u32 protocol)
525525
{
526-
int i;
527-
if (hdev->poll_started == false || hdev->targets == NULL)
528-
return NULL;
529-
530-
for (i = 0; i < hdev->target_count; i++) {
531-
if (hdev->targets[i].idx == target_idx)
532-
return &hdev->targets[i];
533-
}
534-
535-
return NULL;
536-
}
537-
538-
static int hci_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
539-
u32 protocol)
540-
{
541-
struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
542-
543-
if (hci_find_target(hdev, target_idx) == NULL)
544-
return -ENOMEDIUM;
545-
546526
return 0;
547527
}
548528

549-
static void hci_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
529+
static void hci_deactivate_target(struct nfc_dev *nfc_dev,
530+
struct nfc_target *target)
550531
{
551532
}
552533

553-
static int hci_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
534+
static int hci_data_exchange(struct nfc_dev *nfc_dev, struct nfc_target *target,
554535
struct sk_buff *skb, data_exchange_cb_t cb,
555536
void *cb_context)
556537
{
557538
struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
558539
int r;
559-
struct nfc_target *target;
560540
struct sk_buff *res_skb = NULL;
561541

562-
pr_debug("target_idx=%d\n", target_idx);
563-
564-
target = hci_find_target(hdev, target_idx);
565-
if (target == NULL)
566-
return -ENOMEDIUM;
542+
pr_debug("target_idx=%d\n", target->idx);
567543

568544
switch (target->hci_reader_gate) {
569545
case NFC_HCI_RF_READER_A_GATE:

0 commit comments

Comments
 (0)