@@ -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+
186201int 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
219241error :
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);
282304int 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
336380error :
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}
668709EXPORT_SYMBOL (nfc_allocate_device );
0 commit comments