1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749 |
- #include "libusbi.h"
- #include "version.h"
- #ifdef __ANDROID__
- #include <android/log.h>
- #endif
- #include <stdio.h>
- #include <string.h>
- #ifdef HAVE_SYSLOG
- #include <syslog.h>
- #endif
- static const struct libusb_version libusb_version_internal =
- { LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO,
- LIBUSB_RC, "http://libusb.info" };
- static struct timespec timestamp_origin;
- #if defined(ENABLE_LOGGING) && !defined(USE_SYSTEM_LOGGING_FACILITY)
- static libusb_log_cb log_handler;
- #endif
- struct libusb_context *usbi_default_context;
- struct libusb_context *usbi_fallback_context;
- static int default_context_refcnt;
- static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
- static struct usbi_option default_context_options[LIBUSB_OPTION_MAX];
- usbi_mutex_static_t active_contexts_lock = USBI_MUTEX_INITIALIZER;
- struct list_head active_contexts_list;
-
- #define DISCOVERED_DEVICES_SIZE_STEP 16
- static struct discovered_devs *discovered_devs_alloc(void)
- {
- struct discovered_devs *ret =
- malloc(sizeof(*ret) + (sizeof(void *) * DISCOVERED_DEVICES_SIZE_STEP));
- if (ret) {
- ret->len = 0;
- ret->capacity = DISCOVERED_DEVICES_SIZE_STEP;
- }
- return ret;
- }
- static void discovered_devs_free(struct discovered_devs *discdevs)
- {
- size_t i;
- for (i = 0; i < discdevs->len; i++)
- libusb_unref_device(discdevs->devices[i]);
- free(discdevs);
- }
- struct discovered_devs *discovered_devs_append(
- struct discovered_devs *discdevs, struct libusb_device *dev)
- {
- size_t len = discdevs->len;
- size_t capacity;
- struct discovered_devs *new_discdevs;
-
- if (len < discdevs->capacity) {
- discdevs->devices[len] = libusb_ref_device(dev);
- discdevs->len++;
- return discdevs;
- }
-
- usbi_dbg(DEVICE_CTX(dev), "need to increase capacity");
- capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP;
-
- new_discdevs = realloc(discdevs,
- sizeof(*discdevs) + (sizeof(void *) * capacity));
- if (!new_discdevs) {
- discovered_devs_free(discdevs);
- return NULL;
- }
- discdevs = new_discdevs;
- discdevs->capacity = capacity;
- discdevs->devices[len] = libusb_ref_device(dev);
- discdevs->len++;
- return discdevs;
- }
- struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
- unsigned long session_id)
- {
- size_t priv_size = usbi_backend.device_priv_size;
- struct libusb_device *dev = calloc(1, PTR_ALIGN(sizeof(*dev)) + priv_size);
- if (!dev)
- return NULL;
- usbi_atomic_store(&dev->refcnt, 1);
- dev->ctx = ctx;
- dev->session_data = session_id;
- dev->speed = LIBUSB_SPEED_UNKNOWN;
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
- usbi_connect_device(dev);
- return dev;
- }
- void usbi_connect_device(struct libusb_device *dev)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev);
- usbi_atomic_store(&dev->attached, 1);
- usbi_mutex_lock(&dev->ctx->usb_devs_lock);
- list_add(&dev->list, &dev->ctx->usb_devs);
- usbi_mutex_unlock(&dev->ctx->usb_devs_lock);
- usbi_hotplug_notification(ctx, dev, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED);
- }
- void usbi_disconnect_device(struct libusb_device *dev)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev);
- usbi_atomic_store(&dev->attached, 0);
- usbi_mutex_lock(&ctx->usb_devs_lock);
- list_del(&dev->list);
- usbi_mutex_unlock(&ctx->usb_devs_lock);
- usbi_hotplug_notification(ctx, dev, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT);
- }
- int usbi_sanitize_device(struct libusb_device *dev)
- {
- uint8_t num_configurations;
- if (dev->device_descriptor.bLength != LIBUSB_DT_DEVICE_SIZE ||
- dev->device_descriptor.bDescriptorType != LIBUSB_DT_DEVICE) {
- usbi_err(DEVICE_CTX(dev), "invalid device descriptor");
- return LIBUSB_ERROR_IO;
- }
- num_configurations = dev->device_descriptor.bNumConfigurations;
- if (num_configurations > USB_MAXCONFIG) {
- usbi_err(DEVICE_CTX(dev), "too many configurations");
- return LIBUSB_ERROR_IO;
- } else if (0 == num_configurations) {
- usbi_dbg(DEVICE_CTX(dev), "zero configurations, maybe an unauthorized device");
- }
- return 0;
- }
- struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
- unsigned long session_id)
- {
- struct libusb_device *dev;
- struct libusb_device *ret = NULL;
- usbi_mutex_lock(&ctx->usb_devs_lock);
- for_each_device(ctx, dev) {
- if (dev->session_data == session_id) {
- ret = libusb_ref_device(dev);
- break;
- }
- }
- usbi_mutex_unlock(&ctx->usb_devs_lock);
- return ret;
- }
- ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
- libusb_device ***list)
- {
- struct discovered_devs *discdevs = discovered_devs_alloc();
- struct libusb_device **ret;
- int r = 0;
- ssize_t i, len;
- usbi_dbg(ctx, " ");
- if (!discdevs)
- return LIBUSB_ERROR_NO_MEM;
- ctx = usbi_get_context(ctx);
- if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
-
- struct libusb_device *dev;
- if (usbi_backend.hotplug_poll)
- usbi_backend.hotplug_poll();
- usbi_mutex_lock(&ctx->usb_devs_lock);
- for_each_device(ctx, dev) {
- discdevs = discovered_devs_append(discdevs, dev);
- if (!discdevs) {
- r = LIBUSB_ERROR_NO_MEM;
- break;
- }
- }
- usbi_mutex_unlock(&ctx->usb_devs_lock);
- } else {
-
- r = usbi_backend.get_device_list(ctx, &discdevs);
- }
- if (r < 0) {
- len = r;
- goto out;
- }
-
- len = (ssize_t)discdevs->len;
- ret = calloc((size_t)len + 1, sizeof(struct libusb_device *));
- if (!ret) {
- len = LIBUSB_ERROR_NO_MEM;
- goto out;
- }
- ret[len] = NULL;
- for (i = 0; i < len; i++) {
- struct libusb_device *dev = discdevs->devices[i];
- ret[i] = libusb_ref_device(dev);
- }
- *list = ret;
- out:
- if (discdevs)
- discovered_devs_free(discdevs);
- return len;
- }
- void API_EXPORTED libusb_free_device_list(libusb_device **list,
- int unref_devices)
- {
- if (!list)
- return;
- if (unref_devices) {
- int i = 0;
- struct libusb_device *dev;
- while ((dev = list[i++]) != NULL)
- libusb_unref_device(dev);
- }
- free(list);
- }
- uint8_t API_EXPORTED libusb_get_bus_number(libusb_device *dev)
- {
- return dev->bus_number;
- }
- uint8_t API_EXPORTED libusb_get_port_number(libusb_device *dev)
- {
- return dev->port_number;
- }
- int API_EXPORTED libusb_get_port_numbers(libusb_device *dev,
- uint8_t *port_numbers, int port_numbers_len)
- {
- int i = port_numbers_len;
- struct libusb_context *ctx = DEVICE_CTX(dev);
- if (port_numbers_len <= 0)
- return LIBUSB_ERROR_INVALID_PARAM;
-
- while((dev) && (dev->port_number != 0)) {
- if (--i < 0) {
- usbi_warn(ctx, "port numbers array is too small");
- return LIBUSB_ERROR_OVERFLOW;
- }
- port_numbers[i] = dev->port_number;
- dev = dev->parent_dev;
- }
- if (i < port_numbers_len)
- memmove(port_numbers, &port_numbers[i], port_numbers_len - i);
- return port_numbers_len - i;
- }
- int API_EXPORTED libusb_get_port_path(libusb_context *ctx, libusb_device *dev,
- uint8_t *port_numbers, uint8_t port_numbers_len)
- {
- UNUSED(ctx);
- return libusb_get_port_numbers(dev, port_numbers, port_numbers_len);
- }
- DEFAULT_VISIBILITY
- libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev)
- {
- return dev->parent_dev;
- }
- uint8_t API_EXPORTED libusb_get_device_address(libusb_device *dev)
- {
- return dev->device_address;
- }
- int API_EXPORTED libusb_get_device_speed(libusb_device *dev)
- {
- return dev->speed;
- }
- static const struct libusb_endpoint_descriptor *find_endpoint(
- struct libusb_config_descriptor *config, unsigned char endpoint)
- {
- int iface_idx;
- for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) {
- const struct libusb_interface *iface = &config->interface[iface_idx];
- int altsetting_idx;
- for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting;
- altsetting_idx++) {
- const struct libusb_interface_descriptor *altsetting
- = &iface->altsetting[altsetting_idx];
- int ep_idx;
- for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; ep_idx++) {
- const struct libusb_endpoint_descriptor *ep =
- &altsetting->endpoint[ep_idx];
- if (ep->bEndpointAddress == endpoint)
- return ep;
- }
- }
- }
- return NULL;
- }
- int API_EXPORTED libusb_get_max_packet_size(libusb_device *dev,
- unsigned char endpoint)
- {
- struct libusb_config_descriptor *config;
- const struct libusb_endpoint_descriptor *ep;
- int r;
- r = libusb_get_active_config_descriptor(dev, &config);
- if (r < 0) {
- usbi_err(DEVICE_CTX(dev),
- "could not retrieve active config descriptor");
- return LIBUSB_ERROR_OTHER;
- }
- ep = find_endpoint(config, endpoint);
- if (!ep) {
- r = LIBUSB_ERROR_NOT_FOUND;
- goto out;
- }
- r = ep->wMaxPacketSize;
- out:
- libusb_free_config_descriptor(config);
- return r;
- }
- int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev,
- unsigned char endpoint)
- {
- struct libusb_config_descriptor *config;
- const struct libusb_endpoint_descriptor *ep;
- struct libusb_ss_endpoint_companion_descriptor *ss_ep_cmp;
- enum libusb_endpoint_transfer_type ep_type;
- uint16_t val;
- int r;
- int speed;
- r = libusb_get_active_config_descriptor(dev, &config);
- if (r < 0) {
- usbi_err(DEVICE_CTX(dev),
- "could not retrieve active config descriptor");
- return LIBUSB_ERROR_OTHER;
- }
- ep = find_endpoint(config, endpoint);
- if (!ep) {
- r = LIBUSB_ERROR_NOT_FOUND;
- goto out;
- }
- speed = libusb_get_device_speed(dev);
- if (speed >= LIBUSB_SPEED_SUPER) {
- r = libusb_get_ss_endpoint_companion_descriptor(dev->ctx, ep, &ss_ep_cmp);
- if (r == LIBUSB_SUCCESS) {
- r = ss_ep_cmp->wBytesPerInterval;
- libusb_free_ss_endpoint_companion_descriptor(ss_ep_cmp);
- }
- }
-
- if (speed < LIBUSB_SPEED_SUPER || r < 0) {
- val = ep->wMaxPacketSize;
- ep_type = (enum libusb_endpoint_transfer_type) (ep->bmAttributes & 0x3);
- r = val & 0x07ff;
- if (ep_type == LIBUSB_ENDPOINT_TRANSFER_TYPE_ISOCHRONOUS
- || ep_type == LIBUSB_ENDPOINT_TRANSFER_TYPE_INTERRUPT)
- r *= (1 + ((val >> 11) & 3));
- }
- out:
- libusb_free_config_descriptor(config);
- return r;
- }
- DEFAULT_VISIBILITY
- libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev)
- {
- long refcnt;
- refcnt = usbi_atomic_inc(&dev->refcnt);
- assert(refcnt >= 2);
- return dev;
- }
- void API_EXPORTED libusb_unref_device(libusb_device *dev)
- {
- long refcnt;
- if (!dev)
- return;
- refcnt = usbi_atomic_dec(&dev->refcnt);
- assert(refcnt >= 0);
- if (refcnt == 0) {
- usbi_dbg(DEVICE_CTX(dev), "destroy device %d.%d", dev->bus_number, dev->device_address);
- libusb_unref_device(dev->parent_dev);
- if (usbi_backend.destroy_device)
- usbi_backend.destroy_device(dev);
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
-
- usbi_disconnect_device(dev);
- }
- free(dev);
- }
- }
- int API_EXPORTED libusb_wrap_sys_device(libusb_context *ctx, intptr_t sys_dev,
- libusb_device_handle **dev_handle)
- {
- struct libusb_device_handle *_dev_handle;
- size_t priv_size = usbi_backend.device_handle_priv_size;
- int r;
- usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR, (uintptr_t)sys_dev);
- ctx = usbi_get_context(ctx);
- if (!usbi_backend.wrap_sys_device)
- return LIBUSB_ERROR_NOT_SUPPORTED;
- _dev_handle = calloc(1, PTR_ALIGN(sizeof(*_dev_handle)) + priv_size);
- if (!_dev_handle)
- return LIBUSB_ERROR_NO_MEM;
- usbi_mutex_init(&_dev_handle->lock);
- r = usbi_backend.wrap_sys_device(ctx, _dev_handle, sys_dev);
- if (r < 0) {
- usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR " returns %d", (uintptr_t)sys_dev, r);
- usbi_mutex_destroy(&_dev_handle->lock);
- free(_dev_handle);
- return r;
- }
- usbi_mutex_lock(&ctx->open_devs_lock);
- list_add(&_dev_handle->list, &ctx->open_devs);
- usbi_mutex_unlock(&ctx->open_devs_lock);
- *dev_handle = _dev_handle;
- return 0;
- }
- int API_EXPORTED libusb_open(libusb_device *dev,
- libusb_device_handle **dev_handle)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev);
- struct libusb_device_handle *_dev_handle;
- size_t priv_size = usbi_backend.device_handle_priv_size;
- int r;
- usbi_dbg(DEVICE_CTX(dev), "open %d.%d", dev->bus_number, dev->device_address);
- if (!usbi_atomic_load(&dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- _dev_handle = calloc(1, PTR_ALIGN(sizeof(*_dev_handle)) + priv_size);
- if (!_dev_handle)
- return LIBUSB_ERROR_NO_MEM;
- usbi_mutex_init(&_dev_handle->lock);
- _dev_handle->dev = libusb_ref_device(dev);
- r = usbi_backend.open(_dev_handle);
- if (r < 0) {
- usbi_dbg(DEVICE_CTX(dev), "open %d.%d returns %d", dev->bus_number, dev->device_address, r);
- libusb_unref_device(dev);
- usbi_mutex_destroy(&_dev_handle->lock);
- free(_dev_handle);
- return r;
- }
- usbi_mutex_lock(&ctx->open_devs_lock);
- list_add(&_dev_handle->list, &ctx->open_devs);
- usbi_mutex_unlock(&ctx->open_devs_lock);
- *dev_handle = _dev_handle;
- return 0;
- }
- DEFAULT_VISIBILITY
- libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
- libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
- {
- struct libusb_device **devs;
- struct libusb_device *found = NULL;
- struct libusb_device *dev;
- struct libusb_device_handle *dev_handle = NULL;
- size_t i = 0;
- int r;
- if (libusb_get_device_list(ctx, &devs) < 0)
- return NULL;
- while ((dev = devs[i++]) != NULL) {
- struct libusb_device_descriptor desc;
- r = libusb_get_device_descriptor(dev, &desc);
- if (r < 0)
- goto out;
- if (desc.idVendor == vendor_id && desc.idProduct == product_id) {
- found = dev;
- break;
- }
- }
- if (found) {
- r = libusb_open(found, &dev_handle);
- if (r < 0)
- dev_handle = NULL;
- }
- out:
- libusb_free_device_list(devs, 1);
- return dev_handle;
- }
- static void do_close(struct libusb_context *ctx,
- struct libusb_device_handle *dev_handle)
- {
- struct usbi_transfer *itransfer;
- struct usbi_transfer *tmp;
-
- usbi_mutex_lock(&ctx->flying_transfers_lock);
-
- for_each_transfer_safe(ctx, itransfer, tmp) {
- struct libusb_transfer *transfer =
- USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- if (transfer->dev_handle != dev_handle)
- continue;
- usbi_mutex_lock(&itransfer->lock);
- if (!(itransfer->state_flags & USBI_TRANSFER_DEVICE_DISAPPEARED)) {
- usbi_err(ctx, "Device handle closed while transfer was still being processed, but the device is still connected as far as we know");
- if (itransfer->state_flags & USBI_TRANSFER_CANCELLING)
- usbi_warn(ctx, "A cancellation for an in-flight transfer hasn't completed but closing the device handle");
- else
- usbi_err(ctx, "A cancellation hasn't even been scheduled on the transfer for which the device is closing");
- }
- usbi_mutex_unlock(&itransfer->lock);
-
- list_del(&itransfer->list);
- transfer->dev_handle = NULL;
-
- usbi_dbg(ctx, "Removed transfer %p from the in-flight list because device handle %p closed",
- transfer, dev_handle);
- }
- usbi_mutex_unlock(&ctx->flying_transfers_lock);
- usbi_mutex_lock(&ctx->open_devs_lock);
- list_del(&dev_handle->list);
- usbi_mutex_unlock(&ctx->open_devs_lock);
- usbi_backend.close(dev_handle);
- libusb_unref_device(dev_handle->dev);
- usbi_mutex_destroy(&dev_handle->lock);
- free(dev_handle);
- }
- void API_EXPORTED libusb_close(libusb_device_handle *dev_handle)
- {
- struct libusb_context *ctx;
- unsigned int event_flags;
- int handling_events;
- if (!dev_handle)
- return;
- ctx = HANDLE_CTX(dev_handle);
- usbi_dbg(ctx, " ");
- handling_events = usbi_handling_events(ctx);
-
- if (!handling_events) {
-
- usbi_mutex_lock(&ctx->event_data_lock);
- event_flags = ctx->event_flags;
- if (!ctx->device_close++)
- ctx->event_flags |= USBI_EVENT_DEVICE_CLOSE;
- if (!event_flags)
- usbi_signal_event(&ctx->event);
- usbi_mutex_unlock(&ctx->event_data_lock);
-
- libusb_lock_events(ctx);
- }
-
- do_close(ctx, dev_handle);
- if (!handling_events) {
-
- usbi_mutex_lock(&ctx->event_data_lock);
- if (!--ctx->device_close)
- ctx->event_flags &= ~USBI_EVENT_DEVICE_CLOSE;
- if (!ctx->event_flags)
- usbi_clear_event(&ctx->event);
- usbi_mutex_unlock(&ctx->event_data_lock);
-
- libusb_unlock_events(ctx);
- }
- }
- DEFAULT_VISIBILITY
- libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle)
- {
- return dev_handle->dev;
- }
- int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev_handle,
- int *config)
- {
- int r = LIBUSB_ERROR_NOT_SUPPORTED;
- uint8_t tmp = 0;
- struct libusb_context *ctx = HANDLE_CTX(dev_handle);
- usbi_dbg(ctx, " ");
- if (usbi_backend.get_configuration)
- r = usbi_backend.get_configuration(dev_handle, &tmp);
- if (r == LIBUSB_ERROR_NOT_SUPPORTED) {
- usbi_dbg(ctx, "falling back to control message");
- r = libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN,
- LIBUSB_REQUEST_GET_CONFIGURATION, 0, 0, &tmp, 1, 1000);
- if (r == 1) {
- r = 0;
- } else if (r == 0) {
- usbi_err(ctx, "zero bytes returned in ctrl transfer?");
- r = LIBUSB_ERROR_IO;
- } else {
- usbi_dbg(ctx, "control failed, error %d", r);
- }
- }
- if (r == 0) {
- usbi_dbg(ctx, "active config %u", tmp);
- *config = (int)tmp;
- }
- return r;
- }
- int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev_handle,
- int configuration)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), "configuration %d", configuration);
- if (configuration < -1 || configuration > (int)UINT8_MAX)
- return LIBUSB_ERROR_INVALID_PARAM;
- return usbi_backend.set_configuration(dev_handle, configuration);
- }
- int API_EXPORTED libusb_claim_interface(libusb_device_handle *dev_handle,
- int interface_number)
- {
- int r = 0;
- usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number);
- if (interface_number < 0 || interface_number >= USB_MAXINTERFACES)
- return LIBUSB_ERROR_INVALID_PARAM;
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- usbi_mutex_lock(&dev_handle->lock);
- if (dev_handle->claimed_interfaces & (1U << interface_number))
- goto out;
- r = usbi_backend.claim_interface(dev_handle, (uint8_t)interface_number);
- if (r == 0)
- dev_handle->claimed_interfaces |= 1U << interface_number;
- out:
- usbi_mutex_unlock(&dev_handle->lock);
- return r;
- }
- int API_EXPORTED libusb_release_interface(libusb_device_handle *dev_handle,
- int interface_number)
- {
- int r;
- usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number);
- if (interface_number < 0 || interface_number >= USB_MAXINTERFACES)
- return LIBUSB_ERROR_INVALID_PARAM;
- usbi_mutex_lock(&dev_handle->lock);
- if (!(dev_handle->claimed_interfaces & (1U << interface_number))) {
- r = LIBUSB_ERROR_NOT_FOUND;
- goto out;
- }
- r = usbi_backend.release_interface(dev_handle, (uint8_t)interface_number);
- if (r == 0)
- dev_handle->claimed_interfaces &= ~(1U << interface_number);
- out:
- usbi_mutex_unlock(&dev_handle->lock);
- return r;
- }
- int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev_handle,
- int interface_number, int alternate_setting)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), "interface %d altsetting %d",
- interface_number, alternate_setting);
- if (interface_number < 0 || interface_number >= USB_MAXINTERFACES)
- return LIBUSB_ERROR_INVALID_PARAM;
- if (alternate_setting < 0 || alternate_setting > (int)UINT8_MAX)
- return LIBUSB_ERROR_INVALID_PARAM;
- if (!usbi_atomic_load(&dev_handle->dev->attached)) {
- usbi_mutex_unlock(&dev_handle->lock);
- return LIBUSB_ERROR_NO_DEVICE;
- }
- usbi_mutex_lock(&dev_handle->lock);
- if (!(dev_handle->claimed_interfaces & (1U << interface_number))) {
- usbi_mutex_unlock(&dev_handle->lock);
- return LIBUSB_ERROR_NOT_FOUND;
- }
- usbi_mutex_unlock(&dev_handle->lock);
- return usbi_backend.set_interface_altsetting(dev_handle,
- (uint8_t)interface_number, (uint8_t)alternate_setting);
- }
- int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev_handle,
- unsigned char endpoint)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), "endpoint 0x%x", endpoint);
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- return usbi_backend.clear_halt(dev_handle, endpoint);
- }
- int API_EXPORTED libusb_reset_device(libusb_device_handle *dev_handle)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), " ");
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend.reset_device)
- return usbi_backend.reset_device(dev_handle);
- else
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- int API_EXPORTED libusb_alloc_streams(libusb_device_handle *dev_handle,
- uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), "streams %u eps %d", (unsigned)num_streams, num_endpoints);
- if (!num_streams || !endpoints || num_endpoints <= 0)
- return LIBUSB_ERROR_INVALID_PARAM;
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend.alloc_streams)
- return usbi_backend.alloc_streams(dev_handle, num_streams, endpoints,
- num_endpoints);
- else
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- int API_EXPORTED libusb_free_streams(libusb_device_handle *dev_handle,
- unsigned char *endpoints, int num_endpoints)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), "eps %d", num_endpoints);
- if (!endpoints || num_endpoints <= 0)
- return LIBUSB_ERROR_INVALID_PARAM;
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend.free_streams)
- return usbi_backend.free_streams(dev_handle, endpoints,
- num_endpoints);
- else
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- DEFAULT_VISIBILITY
- unsigned char * LIBUSB_CALL libusb_dev_mem_alloc(libusb_device_handle *dev_handle,
- size_t length)
- {
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return NULL;
- if (usbi_backend.dev_mem_alloc)
- return usbi_backend.dev_mem_alloc(dev_handle, length);
- else
- return NULL;
- }
- int API_EXPORTED libusb_dev_mem_free(libusb_device_handle *dev_handle,
- unsigned char *buffer, size_t length)
- {
- if (usbi_backend.dev_mem_free)
- return usbi_backend.dev_mem_free(dev_handle, buffer, length);
- else
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev_handle,
- int interface_number)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number);
- if (interface_number < 0 || interface_number >= USB_MAXINTERFACES)
- return LIBUSB_ERROR_INVALID_PARAM;
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend.kernel_driver_active)
- return usbi_backend.kernel_driver_active(dev_handle, (uint8_t)interface_number);
- else
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- int API_EXPORTED libusb_detach_kernel_driver(libusb_device_handle *dev_handle,
- int interface_number)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number);
- if (interface_number < 0 || interface_number >= USB_MAXINTERFACES)
- return LIBUSB_ERROR_INVALID_PARAM;
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend.detach_kernel_driver)
- return usbi_backend.detach_kernel_driver(dev_handle, (uint8_t)interface_number);
- else
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev_handle,
- int interface_number)
- {
- usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number);
- if (interface_number < 0 || interface_number >= USB_MAXINTERFACES)
- return LIBUSB_ERROR_INVALID_PARAM;
- if (!usbi_atomic_load(&dev_handle->dev->attached))
- return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend.attach_kernel_driver)
- return usbi_backend.attach_kernel_driver(dev_handle, (uint8_t)interface_number);
- else
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- int API_EXPORTED libusb_set_auto_detach_kernel_driver(
- libusb_device_handle *dev_handle, int enable)
- {
- if (!(usbi_backend.caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER))
- return LIBUSB_ERROR_NOT_SUPPORTED;
- dev_handle->auto_detach_kernel_driver = enable;
- return LIBUSB_SUCCESS;
- }
- void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
- {
- #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
- ctx = usbi_get_context(ctx);
- if (!ctx->debug_fixed) {
- level = CLAMP(level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG);
- ctx->debug = (enum libusb_log_level)level;
- }
- #else
- UNUSED(ctx);
- UNUSED(level);
- #endif
- }
- void API_EXPORTED libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb,
- int mode)
- {
- #if defined(ENABLE_LOGGING) && (!defined(ENABLE_DEBUG_LOGGING) || !defined(USE_SYSTEM_LOGGING_FACILITY))
- #if !defined(USE_SYSTEM_LOGGING_FACILITY)
- if (mode & LIBUSB_LOG_CB_GLOBAL)
- log_handler = cb;
- #endif
- #if !defined(ENABLE_DEBUG_LOGGING)
- if (mode & LIBUSB_LOG_CB_CONTEXT) {
- ctx = usbi_get_context(ctx);
- ctx->log_handler = cb;
- }
- #else
- UNUSED(ctx);
- #endif
- #else
- UNUSED(ctx);
- UNUSED(cb);
- UNUSED(mode);
- #endif
- }
- int API_EXPORTED libusb_set_option(libusb_context *ctx,
- enum libusb_option option, ...)
- {
- int arg = 0, r = LIBUSB_SUCCESS;
- va_list ap;
- va_start(ap, option);
- if (LIBUSB_OPTION_LOG_LEVEL == option) {
- arg = va_arg(ap, int);
- if (arg < LIBUSB_LOG_LEVEL_NONE || arg > LIBUSB_LOG_LEVEL_DEBUG) {
- r = LIBUSB_ERROR_INVALID_PARAM;
- }
- }
- va_end(ap);
- if (LIBUSB_SUCCESS != r) {
- return r;
- }
- if (option >= LIBUSB_OPTION_MAX) {
- return LIBUSB_ERROR_INVALID_PARAM;
- }
- if (NULL == ctx) {
- usbi_mutex_static_lock(&default_context_lock);
- default_context_options[option].is_set = 1;
- if (LIBUSB_OPTION_LOG_LEVEL == option) {
- default_context_options[option].arg.ival = arg;
- }
- usbi_mutex_static_unlock(&default_context_lock);
- }
- ctx = usbi_get_context(ctx);
- if (NULL == ctx) {
- return LIBUSB_SUCCESS;
- }
- switch (option) {
- case LIBUSB_OPTION_LOG_LEVEL:
- #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
- if (!ctx->debug_fixed)
- ctx->debug = (enum libusb_log_level)arg;
- #endif
- break;
-
- case LIBUSB_OPTION_USE_USBDK:
- case LIBUSB_OPTION_NO_DEVICE_DISCOVERY:
- if (usbi_backend.set_option)
- return usbi_backend.set_option(ctx, option, ap);
- return LIBUSB_ERROR_NOT_SUPPORTED;
- break;
- case LIBUSB_OPTION_MAX:
- default:
- return LIBUSB_ERROR_INVALID_PARAM;
- }
- return LIBUSB_SUCCESS;;
- }
- #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
- static enum libusb_log_level get_env_debug_level(void)
- {
- const char *dbg = getenv("LIBUSB_DEBUG");
- enum libusb_log_level level;
- if (dbg) {
- int dbg_level = atoi(dbg);
- dbg_level = CLAMP(dbg_level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG);
- level = (enum libusb_log_level)dbg_level;
- } else {
- level = LIBUSB_LOG_LEVEL_NONE;
- }
- return level;
- }
- #endif
- int API_EXPORTED libusb_init(libusb_context **ctx)
- {
- size_t priv_size = usbi_backend.context_priv_size;
- struct libusb_context *_ctx;
- int r;
- usbi_mutex_static_lock(&default_context_lock);
- if (!ctx && default_context_refcnt > 0) {
- usbi_dbg(usbi_default_context, "reusing default context");
- default_context_refcnt++;
- usbi_mutex_static_unlock(&default_context_lock);
- return 0;
- }
-
- if (!active_contexts_list.next) {
- list_init(&active_contexts_list);
- usbi_get_monotonic_time(×tamp_origin);
- }
- _ctx = calloc(1, PTR_ALIGN(sizeof(*_ctx)) + priv_size);
- if (!_ctx) {
- usbi_mutex_static_unlock(&default_context_lock);
- return LIBUSB_ERROR_NO_MEM;
- }
- #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
- if (NULL == ctx && default_context_options[LIBUSB_OPTION_LOG_LEVEL].is_set) {
- _ctx->debug = default_context_options[LIBUSB_OPTION_LOG_LEVEL].arg.ival;
- } else {
- _ctx->debug = get_env_debug_level();
- }
- if (_ctx->debug != LIBUSB_LOG_LEVEL_NONE)
- _ctx->debug_fixed = 1;
- #endif
- usbi_mutex_init(&_ctx->usb_devs_lock);
- usbi_mutex_init(&_ctx->open_devs_lock);
- list_init(&_ctx->usb_devs);
- list_init(&_ctx->open_devs);
-
- for (enum libusb_option option = 0 ; option < LIBUSB_OPTION_MAX ; option++) {
- if (LIBUSB_OPTION_LOG_LEVEL == option || !default_context_options[option].is_set) {
- continue;
- }
- r = libusb_set_option(_ctx, option);
- if (LIBUSB_SUCCESS != r)
- goto err_free_ctx;
- }
-
- if (!ctx) {
- usbi_default_context = _ctx;
- default_context_refcnt = 1;
- usbi_dbg(usbi_default_context, "created default context");
- }
- usbi_dbg(_ctx, "libusb v%u.%u.%u.%u%s", libusb_version_internal.major, libusb_version_internal.minor,
- libusb_version_internal.micro, libusb_version_internal.nano, libusb_version_internal.rc);
- r = usbi_io_init(_ctx);
- if (r < 0)
- goto err_free_ctx;
- usbi_mutex_static_lock(&active_contexts_lock);
- list_add(&_ctx->list, &active_contexts_list);
- usbi_mutex_static_unlock(&active_contexts_lock);
- if (usbi_backend.init) {
- r = usbi_backend.init(_ctx);
- if (r)
- goto err_io_exit;
- }
-
- usbi_hotplug_init(_ctx);
- if (ctx) {
- *ctx = _ctx;
- if (!usbi_fallback_context) {
- usbi_fallback_context = _ctx;
- usbi_warn(usbi_fallback_context, "installing new context as implicit default");
- }
- }
- usbi_mutex_static_unlock(&default_context_lock);
- return 0;
- err_io_exit:
- usbi_mutex_static_lock(&active_contexts_lock);
- list_del(&_ctx->list);
- usbi_mutex_static_unlock(&active_contexts_lock);
- usbi_hotplug_exit(_ctx);
- usbi_io_exit(_ctx);
- err_free_ctx:
- if (!ctx) {
-
- usbi_default_context = NULL;
- default_context_refcnt = 0;
- }
- usbi_mutex_destroy(&_ctx->open_devs_lock);
- usbi_mutex_destroy(&_ctx->usb_devs_lock);
- free(_ctx);
- usbi_mutex_static_unlock(&default_context_lock);
- return r;
- }
- void API_EXPORTED libusb_exit(libusb_context *ctx)
- {
- struct libusb_context *_ctx;
- struct libusb_device *dev;
- usbi_mutex_static_lock(&default_context_lock);
-
- if (!ctx) {
- if (!usbi_default_context) {
- usbi_dbg(ctx, "no default context, not initialized?");
- usbi_mutex_static_unlock(&default_context_lock);
- return;
- }
- if (--default_context_refcnt > 0) {
- usbi_dbg(ctx, "not destroying default context");
- usbi_mutex_static_unlock(&default_context_lock);
- return;
- }
- usbi_dbg(ctx, "destroying default context");
- _ctx = usbi_default_context;
- } else {
- usbi_dbg(ctx, " ");
- _ctx = ctx;
- }
- usbi_mutex_static_lock(&active_contexts_lock);
- list_del(&_ctx->list);
- usbi_mutex_static_unlock(&active_contexts_lock);
- if (usbi_backend.exit)
- usbi_backend.exit(_ctx);
- if (!ctx)
- usbi_default_context = NULL;
- if (ctx == usbi_fallback_context)
- usbi_fallback_context = NULL;
- usbi_mutex_static_unlock(&default_context_lock);
-
- usbi_hotplug_exit(_ctx);
- usbi_io_exit(_ctx);
- for_each_device(_ctx, dev) {
- usbi_warn(_ctx, "device %d.%d still referenced",
- dev->bus_number, dev->device_address);
- DEVICE_CTX(dev) = NULL;
- }
- if (!list_empty(&_ctx->open_devs))
- usbi_warn(_ctx, "application left some devices open");
- usbi_mutex_destroy(&_ctx->open_devs_lock);
- usbi_mutex_destroy(&_ctx->usb_devs_lock);
- free(_ctx);
- }
- int API_EXPORTED libusb_has_capability(uint32_t capability)
- {
- switch (capability) {
- case LIBUSB_CAP_HAS_CAPABILITY:
- return 1;
- case LIBUSB_CAP_HAS_HOTPLUG:
- return !(usbi_backend.get_device_list);
- case LIBUSB_CAP_HAS_HID_ACCESS:
- return (usbi_backend.caps & USBI_CAP_HAS_HID_ACCESS);
- case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
- return (usbi_backend.caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
- }
- return 0;
- }
- #ifdef ENABLE_LOGGING
- #ifdef LIBUSB_PRINTF_WIN32
- int usbi_snprintf(char *str, size_t size, const char *format, ...)
- {
- va_list args;
- int ret;
- va_start(args, format);
- ret = usbi_vsnprintf(str, size, format, args);
- va_end(args);
- return ret;
- }
- int usbi_vsnprintf(char *str, size_t size, const char *format, va_list args)
- {
- int ret;
- ret = _vsnprintf(str, size, format, args);
- if (ret < 0 || ret == (int)size) {
-
- str[size - 1] = '\0';
- if (ret < 0)
- ret = _vsnprintf(NULL, 0, format, args);
- }
- return ret;
- }
- #endif
- static void log_str(enum libusb_log_level level, const char *str)
- {
- #if defined(USE_SYSTEM_LOGGING_FACILITY)
- #if defined(__ANDROID__)
- int priority;
- switch (level) {
- case LIBUSB_LOG_LEVEL_NONE: return;
- case LIBUSB_LOG_LEVEL_ERROR: priority = ANDROID_LOG_ERROR; break;
- case LIBUSB_LOG_LEVEL_WARNING: priority = ANDROID_LOG_WARN; break;
- case LIBUSB_LOG_LEVEL_INFO: priority = ANDROID_LOG_INFO; break;
- case LIBUSB_LOG_LEVEL_DEBUG: priority = ANDROID_LOG_DEBUG; break;
- default: priority = ANDROID_LOG_UNKNOWN;
- }
- __android_log_write(priority, "libusb", str);
- #elif defined(_WIN32)
- UNUSED(level);
- OutputDebugStringA(str);
- #elif defined(HAVE_SYSLOG)
- int syslog_level;
- switch (level) {
- case LIBUSB_LOG_LEVEL_NONE: return;
- case LIBUSB_LOG_LEVEL_ERROR: syslog_level = LOG_ERR; break;
- case LIBUSB_LOG_LEVEL_WARNING: syslog_level = LOG_WARNING; break;
- case LIBUSB_LOG_LEVEL_INFO: syslog_level = LOG_INFO; break;
- case LIBUSB_LOG_LEVEL_DEBUG: syslog_level = LOG_DEBUG; break;
- default: syslog_level = LOG_INFO;
- }
- syslog(syslog_level, "%s", str);
- #else
- #warning System logging is not supported on this platform. Logging to stderr will be used instead.
- UNUSED(level);
- fputs(str, stderr);
- #endif
- #else
-
- if (log_handler)
- log_handler(NULL, level, str);
- else
- fputs(str, stderr);
- #endif
- }
- static void log_v(struct libusb_context *ctx, enum libusb_log_level level,
- const char *function, const char *format, va_list args)
- {
- const char *prefix;
- char buf[USBI_MAX_LOG_LEN];
- int global_debug, header_len, text_len;
- static int has_debug_header_been_displayed = 0;
- #ifdef ENABLE_DEBUG_LOGGING
- global_debug = 1;
- UNUSED(ctx);
- #else
- enum libusb_log_level ctx_level;
- ctx = ctx ? ctx : usbi_default_context;
- ctx = ctx ? ctx : usbi_fallback_context;
- if (ctx)
- ctx_level = ctx->debug;
- else
- ctx_level = get_env_debug_level();
- if (ctx_level < level)
- return;
- global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG);
- #endif
- switch (level) {
- case LIBUSB_LOG_LEVEL_NONE:
- return;
- case LIBUSB_LOG_LEVEL_ERROR:
- prefix = "error";
- break;
- case LIBUSB_LOG_LEVEL_WARNING:
- prefix = "warning";
- break;
- case LIBUSB_LOG_LEVEL_INFO:
- prefix = "info";
- break;
- case LIBUSB_LOG_LEVEL_DEBUG:
- prefix = "debug";
- break;
- default:
- prefix = "unknown";
- break;
- }
- if (global_debug) {
- struct timespec timestamp;
- if (!has_debug_header_been_displayed) {
- has_debug_header_been_displayed = 1;
- log_str(LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
- log_str(LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------" USBI_LOG_LINE_END);
- }
- usbi_get_monotonic_time(×tamp);
- TIMESPEC_SUB(×tamp, ×tamp_origin, ×tamp);
- header_len = snprintf(buf, sizeof(buf),
- "[%2ld.%06ld] [%08x] libusb: %s [%s] ",
- (long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000L), usbi_get_tid(), prefix, function);
- } else {
- header_len = snprintf(buf, sizeof(buf),
- "libusb: %s [%s] ", prefix, function);
- }
- if (header_len < 0 || header_len >= (int)sizeof(buf)) {
-
- header_len = 0;
- }
- text_len = vsnprintf(buf + header_len, sizeof(buf) - (size_t)header_len,
- format, args);
- if (text_len < 0 || text_len + header_len >= (int)sizeof(buf)) {
-
- text_len = (int)sizeof(buf) - header_len;
- }
- if (header_len + text_len + (int)sizeof(USBI_LOG_LINE_END) >= (int)sizeof(buf)) {
-
- text_len -= (header_len + text_len + (int)sizeof(USBI_LOG_LINE_END)) - (int)sizeof(buf);
- }
- strcpy(buf + header_len + text_len, USBI_LOG_LINE_END);
- log_str(level, buf);
-
- #ifndef ENABLE_DEBUG_LOGGING
- if (ctx && ctx->log_handler)
- ctx->log_handler(ctx, level, buf);
- #endif
- }
- void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
- const char *function, const char *format, ...)
- {
- va_list args;
- va_start(args, format);
- log_v(ctx, level, function, format, args);
- va_end(args);
- }
- #endif
- DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_error_name(int error_code)
- {
- switch (error_code) {
- case LIBUSB_ERROR_IO:
- return "LIBUSB_ERROR_IO";
- case LIBUSB_ERROR_INVALID_PARAM:
- return "LIBUSB_ERROR_INVALID_PARAM";
- case LIBUSB_ERROR_ACCESS:
- return "LIBUSB_ERROR_ACCESS";
- case LIBUSB_ERROR_NO_DEVICE:
- return "LIBUSB_ERROR_NO_DEVICE";
- case LIBUSB_ERROR_NOT_FOUND:
- return "LIBUSB_ERROR_NOT_FOUND";
- case LIBUSB_ERROR_BUSY:
- return "LIBUSB_ERROR_BUSY";
- case LIBUSB_ERROR_TIMEOUT:
- return "LIBUSB_ERROR_TIMEOUT";
- case LIBUSB_ERROR_OVERFLOW:
- return "LIBUSB_ERROR_OVERFLOW";
- case LIBUSB_ERROR_PIPE:
- return "LIBUSB_ERROR_PIPE";
- case LIBUSB_ERROR_INTERRUPTED:
- return "LIBUSB_ERROR_INTERRUPTED";
- case LIBUSB_ERROR_NO_MEM:
- return "LIBUSB_ERROR_NO_MEM";
- case LIBUSB_ERROR_NOT_SUPPORTED:
- return "LIBUSB_ERROR_NOT_SUPPORTED";
- case LIBUSB_ERROR_OTHER:
- return "LIBUSB_ERROR_OTHER";
- case LIBUSB_TRANSFER_ERROR:
- return "LIBUSB_TRANSFER_ERROR";
- case LIBUSB_TRANSFER_TIMED_OUT:
- return "LIBUSB_TRANSFER_TIMED_OUT";
- case LIBUSB_TRANSFER_CANCELLED:
- return "LIBUSB_TRANSFER_CANCELLED";
- case LIBUSB_TRANSFER_STALL:
- return "LIBUSB_TRANSFER_STALL";
- case LIBUSB_TRANSFER_NO_DEVICE:
- return "LIBUSB_TRANSFER_NO_DEVICE";
- case LIBUSB_TRANSFER_OVERFLOW:
- return "LIBUSB_TRANSFER_OVERFLOW";
- case 0:
- return "LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLETED";
- default:
- return "**UNKNOWN**";
- }
- }
- DEFAULT_VISIBILITY
- const struct libusb_version * LIBUSB_CALL libusb_get_version(void)
- {
- return &libusb_version_internal;
- }
|