Ignore:
Timestamp:
03/30/09 17:39:47 (3 years ago)
Author:
alonbl
Message:

FreeBSD patches with libusb support

By Andrew Thompson.
http://www.opensc-project.org/pipermail/opensc-devel/2009-March/011997.html

---

Hi,

Here is a patch to add libusb support to ifd/sys-bsd.c, this will be
needed for FreeBSD 8.0 onwards. I have verified with unifdef that the
source is unchanged when ENABLE_LIBUSB isnt defined.

 http://people.freebsd.org/~thompsa/patch-src_ifd_sys-bsd.c

cheers,
Andrew

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/ifd/sys-bsd.c

    r1137 r1138  
    1414#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) 
    1515#include <sys/types.h> 
     16#ifndef ENABLE_LIBUSB 
    1617#if defined(__DragonFly__) 
    1718#include <bus/usb/usb.h> 
     
    1920#include <dev/usb/usb.h> 
    2021#endif 
     22#endif /* !ENABLE_LIBUSB */ 
    2123#include <sys/stat.h> 
    2224#include <sys/ioctl.h> 
     
    3133#include <errno.h> 
    3234#include <openct/driver.h> 
     35#ifdef ENABLE_LIBUSB 
     36#include <usb.h> 
     37#endif 
    3338 
    3439#include "usb-descriptors.h" 
     40 
     41#ifdef ENABLE_LIBUSB 
     42struct usb_dev_handle *devices[128]; 
     43#endif 
    3544 
    3645/* 
     
    4655} 
    4756 
     57#ifndef ENABLE_LIBUSB 
    4858typedef struct ep { 
    4959        int ep_fd; 
     
    91101        } 
    92102} 
     103#endif /* !ENABLE_LIBUSB */ 
    93104 
    94105int ifd_sysdep_usb_bulk(ifd_device_t * dev, int ep, void *buffer, size_t len, 
     
    104115                 direction); 
    105116        if (direction) { 
     117#ifdef ENABLE_LIBUSB 
     118                if ((bytes_to_process = 
     119                     usb_bulk_read(devices[dev->fd], ep, buffer, len, 
     120                                   timeout)) < 0) { 
     121                        ifd_debug(6, "ifd_sysdep_usb_bulk: read failed: %s", 
     122                                  strerror(errno)); 
     123                        ct_error("usb_bulk read failed: %s", strerror(errno)); 
     124                        return IFD_ERROR_COMM_ERROR; 
     125                } 
     126#else 
    106127                int one = 1; 
    107128 
     
    124145                        return IFD_ERROR_COMM_ERROR; 
    125146                } 
     147#endif /* ENABLE_LIBUSB */ 
    126148                ct_debug("ifd_sysdep_usb_bulk: read %d bytes", 
    127149                         bytes_to_process); 
    128150                return bytes_to_process; 
    129151        } else { 
     152#ifndef ENABLE_LIBUSB 
    130153                if (open_ep(dev->name, 0, endpoint, O_WRONLY | O_NONBLOCK)) { 
    131154                        ct_debug("ifd_sysdep_usb_bulk: opening endpoint failed"); 
    132155                        return -1; 
    133156                } 
     157#endif /* !ENABLE_LIBUSB */ 
    134158 
    135159                bytes_to_process = len; 
    136160                if ((bytes_processed = 
     161#ifdef ENABLE_LIBUSB 
     162                     usb_bulk_write(devices[dev->fd], ep, buffer, 
     163                                    bytes_to_process, timeout) 
     164#else 
    137165                     write(interfaces[0][endpoint].ep_fd, buffer, 
    138                            bytes_to_process)) != bytes_to_process) { 
     166                           bytes_to_process) 
     167#endif /* ENABLE_LIBUSB */ 
     168                            ) != bytes_to_process) { 
    139169                        ifd_debug(6, "ifd_sysdep_usb_bulk: write failed: %s", 
    140170                                  strerror(errno)); 
     
    167197{ 
    168198        ifd_usb_capture_t *cap; 
     199#ifndef ENABLE_LIBUSB 
    169200        int direction = 
    170201            (ep & IFD_USB_ENDPOINT_DIR_MASK) == IFD_USB_ENDPOINT_IN ? 1 : 0; 
    171202        int endpoint = (ep & ~IFD_USB_ENDPOINT_DIR_MASK); 
     203#endif /* !ENABLE_LIBUSB */ 
    172204 
    173205        if (!(cap = (ifd_usb_capture_t *) calloc(1, sizeof(*cap) + maxpacket))) { 
     
    179211        cap->maxpacket = maxpacket; 
    180212 
     213#ifndef ENABLE_LIBUSB 
    181214        if (!interfaces[0][endpoint].ep_fd) { 
    182215                if (open_ep(dev->name, 0, endpoint, O_RDONLY | O_NONBLOCK)) { 
     
    186219                } 
    187220        } 
     221#endif /* !ENABLE_LIBUSB */ 
    188222        *capret = cap; 
    189223        return 0; 
     
    199233                           void *buffer, size_t len, long timeout) 
    200234{ 
     235        int bytes_to_process = 0; 
     236#ifdef ENABLE_LIBUSB 
     237        if ((bytes_to_process = 
     238             usb_interrupt_read(devices[dev->fd], cap->endpoint, buffer, len, 
     239                                timeout)) < 0) { 
     240                ifd_debug(6, 
     241                          "ifd_sysdep_usb_capture: usb_interrupt_read failed: %s", 
     242                          strerror(errno)); 
     243                ct_error("usb_bulk read failed: %s", strerror(errno)); 
     244                return IFD_ERROR_COMM_ERROR; 
     245        } 
     246#else 
    201247        struct timeval begin; 
    202         int bytes_to_process = 0; 
    203248        int direction = 
    204249            (cap->endpoint & IFD_USB_ENDPOINT_DIR_MASK) == 
     
    227272                } 
    228273        } while (!bytes_to_process); 
     274#endif /* ENABLE_LIBUSB */ 
    229275        ct_debug("ifd_sysdep_usb_capture: read buffer[%d]=%s", bytes_to_process, 
    230276                 ct_hexdump(buffer, bytes_to_process)); 
     
    234280int ifd_sysdep_usb_end_capture(ifd_device_t * dev, ifd_usb_capture_t * cap) 
    235281{ 
     282#ifndef ENABLE_LIBUSB 
    236283        int direction = 
    237284            (cap->endpoint & IFD_USB_ENDPOINT_DIR_MASK) == 
     
    239286        int endpoint = (cap->endpoint & ~IFD_USB_ENDPOINT_DIR_MASK); 
    240287        close_ep(0, endpoint); 
     288#endif /* !ENABLE_LIBUSB */ 
    241289        if (cap) 
    242290                free(cap); 
     
    252300                           long timeout) 
    253301{ 
     302        int rc, val; 
     303 
     304#ifdef ENABLE_LIBUSB 
     305        ct_debug("ifd_sysdep_usb_control: dev->fd=%d handle=0x%x", dev->fd, 
     306                 devices[dev->fd]); 
     307        if ((rc = 
     308             usb_control_msg(devices[dev->fd], requesttype, request, value, 
     309                             index, data, len, timeout)) < 0) { 
     310                ifd_debug(1, "usb_control_msg failed: %d", rc); 
     311                ct_error("usb_control_msg failed: %s(%d)", 
     312                         strerror(errno), errno); 
     313                return IFD_ERROR_COMM_ERROR; 
     314        } 
     315 
     316        ct_debug("ifd_sysdep_usb_control: return rc=%d", rc); 
     317        return rc; 
     318#else 
    254319        struct usb_ctl_request ctrl; 
    255         int rc, val; 
    256320 
    257321        ifd_debug(1, "BSD: ifd_sysdep_usb_control(0x%x)", request); 
     
    294358                          ct_hexdump(ctrl.ucr_data, ctrl.ucr_actlen)); 
    295359        return ctrl.ucr_actlen; 
     360#endif /* ENABLE_LIBUSB */ 
    296361} 
    297362 
    298363int ifd_sysdep_usb_set_configuration(ifd_device_t * dev, int config) 
    299364{ 
    300         int value, rc; 
     365        int rc; 
     366#ifdef ENABLE_LIBUSB 
     367        if ((rc = usb_set_configuration(devices[dev->fd], config)) < 0) { 
     368                ifd_debug(1, "usb_set_configuration failed: %d", rc); 
     369#else 
     370        int value; 
    301371        value = config; 
    302372        if ((rc = ioctl(dev->fd, USB_SET_CONFIG, &value)) < 0) { 
    303373                ifd_debug(1, "USB_SET_CONFIG failed: %d", rc); 
     374#endif /* ENABLE_LIBUSB */ 
    304375                ct_error("usb_set_configuration failed: %s(%d)", 
    305376                         strerror(errno), errno); 
     
    312383{ 
    313384        int rc; 
     385#ifdef ENABLE_LIBUSB 
     386        if ((rc = usb_set_altinterface(devices[dev->fd], alt)) < 0) { 
     387                ifd_debug(1, "usb_set_altinterface failed: %d", rc); 
     388#else 
    314389        struct usb_alt_interface { 
    315390                int uai_config_index; 
     
    323398        if ((rc = ioctl(dev->fd, USB_SET_ALTINTERFACE, &value)) < 0) { 
    324399                ifd_debug(1, "USB_SET_ALTINTERFACE failed: %d", rc); 
     400#endif /* ENABLE_LIBUSB */ 
    325401                ct_error("usb_set_interface failed: %s(%d)", 
    326402                         strerror(errno), errno); 
     
    332408int ifd_sysdep_usb_claim_interface(ifd_device_t * dev, int interface) 
    333409{ 
     410#ifdef ENABLE_LIBUSB 
     411        int rc; 
     412 
     413        ct_debug("ifd_sysdep_usb_claim_interface: interface=%d", interface); 
     414        if ((rc = usb_claim_interface(devices[dev->fd], interface)) < 0) { 
     415                ifd_debug(1, "usb_clain_interface failed: %d", rc); 
     416                ct_error("usb_release_interface failed: %s(%d)", 
     417                         strerror(errno), errno); 
     418                return IFD_ERROR_COMM_ERROR; 
     419        } 
     420#else 
    334421        ct_debug 
    335422            ("ifd_sysdep_usb_claim_interface: interface=%d (not yet implemented)", 
    336423             interface); 
     424#endif /* ENABLE_LIBUSB */ 
    337425        return 0; 
    338426} 
     
    340428int ifd_sysdep_usb_release_interface(ifd_device_t * dev, int interface) 
    341429{ 
     430#ifdef ENABLE_LIBUSB 
     431        int rc; 
     432 
     433        ct_debug("ifd_sysdep_usb_release_interface: interface=%d", interface); 
     434        if ((rc = usb_release_interface(devices[dev->fd], interface)) < 0) { 
     435                ifd_debug(1, "usb_release_interface failed: %d", rc); 
     436                ct_error("usb_release_interface failed: %s(%d)", 
     437                         strerror(errno), errno); 
     438                return IFD_ERROR_COMM_ERROR; 
     439        } 
     440#else 
    342441        ct_debug 
    343442            ("ifd_sysdep_usb_release_interface: interface=%d (not yet implemented)", 
    344443             interface); 
     444#endif /* ENABLE_LIBUSB */ 
    345445        return 0; 
    346446} 
     
    348448int ifd_sysdep_usb_open(const char *device) 
    349449{ 
     450#ifdef ENABLE_LIBUSB 
     451        struct usb_bus *bus; 
     452        struct usb_device *dev; 
     453 
     454        ct_debug("ifd_sysdep_usb_open: name=%s", device); 
     455        ct_debug("ifd_sysdep_usb_open: usb_init()"); 
     456        usb_init(); 
     457        ct_debug("ifd_sysdep_usb_open: usb_find_busses()"); 
     458        usb_find_busses(); 
     459        ct_debug("ifd_sysdep_usb_open: usb_find_devices()"); 
     460        usb_find_devices(); 
     461 
     462        ct_debug("ifd_sysdep_usb_open: walk devices"); 
     463        for (bus = usb_busses; bus; bus = bus->next) { 
     464                for (dev = bus->devices; dev; dev = dev->next) { 
     465                        int i; 
     466 
     467                        if (strcmp(dev->filename, device) != 0) 
     468                                continue; 
     469 
     470                        ct_debug 
     471                            ("ifd_sysdep_usb_open: found match name=%s device=%s", 
     472                             device, dev->filename); 
     473                        for (i = 0; i < 128; i++) { 
     474                                if (devices[i] == NULL) { 
     475                                        devices[i] = usb_open(dev); 
     476                                        ct_debug 
     477                                            ("ifd_sysdep_usb_open: usb_open index=%d handle=0x%x", 
     478                                             i, devices[i]); 
     479                                        return i; 
     480                                } 
     481                        } 
     482                } 
     483        } 
     484        return -1; 
     485#else 
    350486#ifdef __OpenBSD__ 
    351487        char path[256]; 
     
    357493        return open(device, O_RDWR); 
    358494#endif /* __OpenBSD__ */ 
     495#endif /* ENABLE_LIBUSB */ 
    359496} 
    360497 
     
    370507int ifd_scan_usb(void) 
    371508{ 
     509#ifdef ENABLE_LIBUSB 
     510        struct usb_bus *bus; 
     511        struct usb_device *dev; 
     512        ifd_devid_t id; 
     513 
     514        usb_init(); 
     515        usb_find_busses(); 
     516        usb_find_devices(); 
     517 
     518        id.type = IFD_DEVICE_TYPE_USB; 
     519        id.num = 2; 
     520        for (bus = usb_busses; bus; bus = bus->next) { 
     521                for (dev = bus->devices; dev; dev = dev->next) { 
     522                        const char *driver; 
     523                        char typedev[PATH_MAX]; 
     524 
     525                        id.val[0] = dev->descriptor.idVendor; 
     526                        id.val[1] = dev->descriptor.idProduct; 
     527 
     528                        /* FIXME: if we don't find a driver with vendor/product 
     529                         * then check for the interface type (ccid) and use 
     530                         * driver ccid... */ 
     531 
     532                        if (!(driver = ifd_driver_for_id(&id))) 
     533                                continue; 
     534 
     535                        snprintf(typedev, sizeof(typedev), 
     536                                 "usb:%s", dev->filename); 
     537 
     538                        ifd_spawn_handler(driver, typedev, -1); 
     539                } 
     540        } 
     541#else 
    372542        int i, controller_fd; 
    373543        char controller_devname[10]; 
     
    433603                close(controller_fd); 
    434604        } 
     605#endif /* ENABLE_LIBUSB */ 
    435606        return 0; 
    436607} 
Note: See TracChangeset for help on using the changeset viewer.