scd: Improve code reability of ccid-driver.c

* scd/ccid-driver.c (my_npth_unprotect, my_npth_protect): New.
Replace all direct uses by these wrappers.
This commit is contained in:
Werner Koch 2024-03-07 13:29:56 +01:00
parent 1682ca9f01
commit 09431d1762
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
1 changed files with 49 additions and 96 deletions

View File

@ -298,6 +298,23 @@ static int send_escape_cmd (ccid_driver_t handle, const unsigned char *data,
size_t resultmax, size_t *resultlen); size_t resultmax, size_t *resultlen);
static void
my_npth_unprotect (void)
{
#ifdef USE_NPTH
npth_unprotect ();
#endif
}
static void
my_npth_protect (void)
{
#ifdef USE_NPTH
npth_protect ();
#endif
}
static int static int
map_libusb_error (int usberr) map_libusb_error (int usberr)
{ {
@ -984,31 +1001,23 @@ get_escaped_usb_string (libusb_device_handle *idev, int idx,
/* First get the list of supported languages and use the first one. /* First get the list of supported languages and use the first one.
If we do don't find it we try to use English. Note that this is If we do don't find it we try to use English. Note that this is
all in a 2 bute Unicode encoding using little endian. */ all in a 2 bute Unicode encoding using little endian. */
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
rc = libusb_control_transfer (idev, LIBUSB_ENDPOINT_IN, rc = libusb_control_transfer (idev, LIBUSB_ENDPOINT_IN,
LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_REQUEST_GET_DESCRIPTOR,
(LIBUSB_DT_STRING << 8), 0, (LIBUSB_DT_STRING << 8), 0,
buf, sizeof buf, 1000 /* ms timeout */); buf, sizeof buf, 1000 /* ms timeout */);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
if (rc < 4) if (rc < 4)
langid = 0x0409; /* English. */ langid = 0x0409; /* English. */
else else
langid = (buf[3] << 8) | buf[2]; langid = (buf[3] << 8) | buf[2];
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
rc = libusb_control_transfer (idev, LIBUSB_ENDPOINT_IN, rc = libusb_control_transfer (idev, LIBUSB_ENDPOINT_IN,
LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_REQUEST_GET_DESCRIPTOR,
(LIBUSB_DT_STRING << 8) + idx, langid, (LIBUSB_DT_STRING << 8) + idx, langid,
buf, sizeof buf, 1000 /* ms timeout */); buf, sizeof buf, 1000 /* ms timeout */);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
if (rc < 2 || buf[1] != LIBUSB_DT_STRING) if (rc < 2 || buf[1] != LIBUSB_DT_STRING)
return NULL; /* Error or not a string. */ return NULL; /* Error or not a string. */
len = buf[0]; len = buf[0];
@ -1345,13 +1354,9 @@ ccid_vendor_specific_setup (ccid_driver_t handle)
{ {
if (handle->id_vendor == VENDOR_SCM && handle->id_product == SCM_SPR532) if (handle->id_vendor == VENDOR_SCM && handle->id_product == SCM_SPR532)
{ {
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
libusb_clear_halt (handle->idev, handle->ep_intr); libusb_clear_halt (handle->idev, handle->ep_intr);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
} }
return 0; return 0;
} }
@ -1660,13 +1665,9 @@ ccid_usb_thread (void *arg)
while (ccid_usb_thread_is_alive) while (ccid_usb_thread_is_alive)
{ {
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
libusb_handle_events_completed (ctx, NULL); libusb_handle_events_completed (ctx, NULL);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
} }
return NULL; return NULL;
@ -1776,29 +1777,21 @@ ccid_open_usb_reader (const char *spec_reader_name,
goto leave; goto leave;
} }
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
if (!(opt.compat_flags & COMPAT_CCID_NO_AUTO_DETACH)) if (!(opt.compat_flags & COMPAT_CCID_NO_AUTO_DETACH))
{ {
rc = libusb_set_auto_detach_kernel_driver (idev, 1); rc = libusb_set_auto_detach_kernel_driver (idev, 1);
if (rc) if (rc)
{ {
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
DEBUGOUT_1 ("note: set_auto_detach_kernel_driver failed: %d\n", rc); DEBUGOUT_1 ("note: set_auto_detach_kernel_driver failed: %d\n", rc);
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
} }
} }
rc = libusb_claim_interface (idev, ifc_no); rc = libusb_claim_interface (idev, ifc_no);
if (rc) if (rc)
{ {
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc); DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
rc = map_libusb_error (rc); rc = map_libusb_error (rc);
goto leave; goto leave;
@ -1811,17 +1804,13 @@ ccid_open_usb_reader (const char *spec_reader_name,
rc = libusb_set_interface_alt_setting (idev, ifc_no, set_no); rc = libusb_set_interface_alt_setting (idev, ifc_no, set_no);
if (rc) if (rc)
{ {
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
DEBUGOUT_1 ("usb_set_interface_alt_setting failed: %d\n", rc); DEBUGOUT_1 ("usb_set_interface_alt_setting failed: %d\n", rc);
rc = map_libusb_error (rc); rc = map_libusb_error (rc);
goto leave; goto leave;
} }
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
/* Perform any vendor specific intialization. */ /* Perform any vendor specific intialization. */
rc = ccid_vendor_specific_init (*handle); rc = ccid_vendor_specific_init (*handle);
@ -1957,13 +1946,9 @@ do_close_reader (ccid_driver_t handle)
while (!handle->powered_off) while (!handle->powered_off)
{ {
DEBUGOUT ("libusb_handle_events_completed\n"); DEBUGOUT ("libusb_handle_events_completed\n");
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
libusb_handle_events_completed (NULL, &handle->powered_off); libusb_handle_events_completed (NULL, &handle->powered_off);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
} }
} }
@ -2094,15 +2079,11 @@ bulk_out (ccid_driver_t handle, unsigned char *msg, size_t msglen,
} }
} }
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
rc = libusb_bulk_transfer (handle->idev, handle->ep_bulk_out, rc = libusb_bulk_transfer (handle->idev, handle->ep_bulk_out,
msg, msglen, &transferred, msg, msglen, &transferred,
5000 /* ms timeout */); 5000 /* ms timeout */);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
if (rc == 0 && transferred == msglen) if (rc == 0 && transferred == msglen)
return 0; return 0;
@ -2142,14 +2123,10 @@ bulk_in (ccid_driver_t handle, unsigned char *buffer, size_t length,
memset (buffer, 0, length); memset (buffer, 0, length);
retry: retry:
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
rc = libusb_bulk_transfer (handle->idev, handle->ep_bulk_in, rc = libusb_bulk_transfer (handle->idev, handle->ep_bulk_in,
buffer, length, &msglen, bwi*timeout); buffer, length, &msglen, bwi*timeout);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
if (rc) if (rc)
{ {
DEBUGOUT_1 ("usb_bulk_read error: %s\n", libusb_error_name (rc)); DEBUGOUT_1 ("usb_bulk_read error: %s\n", libusb_error_name (rc));
@ -2298,9 +2275,7 @@ abort_cmd (ccid_driver_t handle, int seqno, int init)
/* Send the abort command to the control pipe. Note that we don't /* Send the abort command to the control pipe. Note that we don't
need to keep track of sent abort commands because there should need to keep track of sent abort commands because there should
never be another thread using the same slot concurrently. */ never be another thread using the same slot concurrently. */
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
rc = libusb_control_transfer (handle->idev, rc = libusb_control_transfer (handle->idev,
0x21,/* bmRequestType: host-to-device, 0x21,/* bmRequestType: host-to-device,
class specific, to interface. */ class specific, to interface. */
@ -2309,9 +2284,7 @@ abort_cmd (ccid_driver_t handle, int seqno, int init)
handle->ifc_no, handle->ifc_no,
dummybuf, 0, dummybuf, 0,
1000 /* ms timeout */); 1000 /* ms timeout */);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
if (rc) if (rc)
{ {
DEBUGOUT_1 ("usb_control_msg error: %s\n", libusb_error_name (rc)); DEBUGOUT_1 ("usb_control_msg error: %s\n", libusb_error_name (rc));
@ -2337,15 +2310,11 @@ abort_cmd (ccid_driver_t handle, int seqno, int init)
msglen = 10; msglen = 10;
set_msg_len (msg, 0); set_msg_len (msg, 0);
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
rc = libusb_bulk_transfer (handle->idev, handle->ep_bulk_out, rc = libusb_bulk_transfer (handle->idev, handle->ep_bulk_out,
msg, msglen, &transferred, msg, msglen, &transferred,
init? 100: 5000 /* ms timeout */); init? 100: 5000 /* ms timeout */);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
if (rc == 0 && transferred == msglen) if (rc == 0 && transferred == msglen)
rc = 0; rc = 0;
else if (rc) else if (rc)
@ -2355,15 +2324,11 @@ abort_cmd (ccid_driver_t handle, int seqno, int init)
if (rc) if (rc)
return map_libusb_error (rc); return map_libusb_error (rc);
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
rc = libusb_bulk_transfer (handle->idev, handle->ep_bulk_in, rc = libusb_bulk_transfer (handle->idev, handle->ep_bulk_in,
msg, sizeof msg, &msglen, msg, sizeof msg, &msglen,
init? 100: 5000 /*ms timeout*/); init? 100: 5000 /*ms timeout*/);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
if (rc) if (rc)
{ {
DEBUGOUT_1 ("usb_bulk_read error in abort_cmd: %s\n", DEBUGOUT_1 ("usb_bulk_read error in abort_cmd: %s\n",
@ -2577,14 +2542,10 @@ ccid_slot_status (ccid_driver_t handle, int *statusbits, int on_wire)
if (!retries) if (!retries)
{ {
DEBUGOUT ("USB: CALLING USB_CLEAR_HALT\n"); DEBUGOUT ("USB: CALLING USB_CLEAR_HALT\n");
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
libusb_clear_halt (handle->idev, handle->ep_bulk_in); libusb_clear_halt (handle->idev, handle->ep_bulk_in);
libusb_clear_halt (handle->idev, handle->ep_bulk_out); libusb_clear_halt (handle->idev, handle->ep_bulk_out);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
} }
else else
DEBUGOUT ("USB: RETRYING bulk_in AGAIN\n"); DEBUGOUT ("USB: RETRYING bulk_in AGAIN\n");
@ -3353,13 +3314,9 @@ ccid_transceive (ccid_driver_t handle,
if (tpdulen < 4) if (tpdulen < 4)
{ {
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
libusb_clear_halt (handle->idev, handle->ep_bulk_in); libusb_clear_halt (handle->idev, handle->ep_bulk_in);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
return CCID_DRIVER_ERR_ABORTED; return CCID_DRIVER_ERR_ABORTED;
} }
@ -3811,13 +3768,9 @@ ccid_transceive_secure (ccid_driver_t handle,
if (tpdulen < 4) if (tpdulen < 4)
{ {
#ifdef USE_NPTH my_npth_unprotect ();
npth_unprotect ();
#endif
libusb_clear_halt (handle->idev, handle->ep_bulk_in); libusb_clear_halt (handle->idev, handle->ep_bulk_in);
#ifdef USE_NPTH my_npth_protect ();
npth_protect ();
#endif
return CCID_DRIVER_ERR_ABORTED; return CCID_DRIVER_ERR_ABORTED;
} }
if (debug_level > 1) if (debug_level > 1)