1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
Description: Migrate to libusb 1.0
From 519acbb908c45e2354f38bab93a73ae382a91ff1
Author: Nicolas Schodet
Forwareded: no
Bug-Debian: https://bugs.debian.org/810460
Reviewed-By: Petter Reinholdtsen <pere@debian.org>
Last-Update: 2021-10-07
---
Makefile | 16 +++----
src/t2n.cc | 3 +-
src/usbmisc.cc | 141 +++++++++++++++++++++++++++++++++++++--------------------
src/usbmisc.h | 13 +++---
src/usbnxt.cc | 53 ++--------------------
src/usbscan.c | 54 +++++++++++++---------
6 files changed, 143 insertions(+), 137 deletions(-)
Index: t2n-git/Makefile
===================================================================
--- t2n-git.orig/Makefile 2024-04-15 18:01:34.469399798 +0200
+++ t2n-git/Makefile 2024-04-15 18:03:02.062353707 +0200
@@ -21,17 +21,19 @@
OBJDIR=./obj$(ARCHI)
SRCDIR=./src
-all: dynamic static
+all: dynamic
dynamic : $(OBJDIR) $(OBJDIR)/$(MAIN) $(OBJDIR)/usbscan
-static : $(OBJDIR)/$(MAIN).static
+
+USB_CFLAGS = $(shell pkg-config libusb-1.0 --cflags)
+USB_LIBS = $(shell pkg-config libusb-1.0 --libs)
CC=g++
LK=g++
-CFLAGS+= -I$(SRCDIR) -I$(OBJDIR) -g -Wall -fPIE -Wno-int-to-pointer-cast
-LDFLAGS+=
+override CFLAGS+= -I$(SRCDIR) -I$(OBJDIR) -g -Wall -fPIE -Wno-int-to-pointer-cast $(USB_CFLAGS)
+override LDFLAGS+=
-LIBS=-lusb -lstdc++
+LIBS=$(USB_LIBS)
OBJS=$(OBJDIR)/$(MAIN).o \
$(OBJDIR)/usbmisc.o \
@@ -46,9 +48,6 @@
$(OBJDIR)/$(MAIN) : $(OBJS)
$(LK) $(LDFLAGS) $(OBJS) $(LIBS) -o $(OBJDIR)/$(MAIN)
-$(OBJDIR)/$(MAIN).static : $(OBJS)
- $(LK) -static $(LDFLAGS) $(OBJS) $(LIBS) -o $(OBJDIR)/$(MAIN).static
-
$(OBJDIR)/%.o : $(SRCDIR)/%.cc
$(CC) -c $(CFLAGS) $(SRCDIR)/$*.cc -o $(OBJDIR)/$*.o
@@ -70,12 +69,11 @@
distclean:
rm -rf $(OBJDIR)
-distrib: all static
+distrib: all
ddir=t2n-`$(OBJDIR)/$(MAIN) -version`-$(ARCHI) ; \
echo $$ddir ; \
if [ ! -d $$ddir ] ; then mkdir $$ddir ; fi ; \
cp $(OBJDIR)/$(MAIN) $$ddir ; strip $(OBJDIR)/$(MAIN) ; \
- cp $(OBJDIR)/$(MAIN).static $$ddir ; strip $(OBJDIR)/$(MAIN).static ; \
# cp -r hotplug $$ddir ; \
# cp -r udev $$ddir ; \
tar zcvf $$ddir.tgz $$ddir ; \
Index: t2n-git/src/t2n.cc
===================================================================
--- t2n-git.orig/src/t2n.cc 2024-04-15 18:01:34.469399798 +0200
+++ t2n-git/src/t2n.cc 2024-04-15 18:01:34.465399755 +0200
@@ -30,11 +30,12 @@
#include <string.h>
#include <stdio.h>
+#include <libusb.h>
+
#include "version.h"
#include "errors.h"
#include "ezargs.h"
#include "errormng.h"
-#include "usb.h"
#include "usbmisc.h"
#include "usbnxt.h"
Index: t2n-git/src/usbmisc.cc
===================================================================
--- t2n-git.orig/src/usbmisc.cc 2024-04-15 18:01:34.469399798 +0200
+++ t2n-git/src/usbmisc.cc 2024-04-15 18:01:34.465399755 +0200
@@ -31,9 +31,10 @@
#include <string.h>
#include <stdio.h>
#include <ctype.h>
+#include <libusb.h>
+
#include "errors.h"
#include "errormng.h"
-#include "usb.h"
#include "usbmisc.h"
//toujours vrai ???
@@ -54,47 +55,51 @@
_bbuff = NULL;
}
-//extern int print_device(struct usb_device *dev, int level);
-
int UsbHandle::init(u_int16_t vendor, u_int16_t product){
+ int err;
_dev = UsbHandle::_find_usb_dev(vendor, product);
if(_dev == NULL) {
- _usbmisc_error.set("no usb device found (vendor=0x%04x product=0x%04x)",
- vendor, product
- );
return -1;
}
-//print_device(_dev, 0);
-
- _handle = usb_open(_dev);
- if (!_handle) {
- _usbmisc_error.set("fails to open device handle (vendor=0x%04x product=0x%04x)",
- vendor, product
+ err = libusb_open(_dev, &_handle);
+ if(err < 0){
+ _usbmisc_error.set(
+ "fails to open device handle, %s (vendor=0x%04x product=0x%04x)",
+ libusb_strerror(err), vendor, product
);
return -1;
}
- //reset ?
- usb_reset(_handle);
- int err;
+ err = libusb_reset_device(_handle);
+ if(err < 0){
+ _usbmisc_error.set(
+ "fails to reset device, %s (vendor=0x%04x product=0x%04x)",
+ libusb_strerror(err), vendor, product
+ );
+ return -1;
+ }
+
//set config
- err = usb_set_configuration(_handle, USB_CONFIG);
+ err = libusb_set_configuration(_handle, USB_CONFIG);
if(err != 0){
-//printf( "usb_strerror = %s\n", usb_strerror( ));
_usbmisc_error.set(
- "fails to set config, errno=%d, usberr=\"%s\" (cf=%d vendor=0x%04x product=0x%04x)",
- err, usb_strerror(), USB_CONFIG, vendor, product
+ "fails to set config, %s (cf=%d vendor=0x%04x product=0x%04x)",
+ libusb_strerror(err), USB_CONFIG, vendor, product
);
+ libusb_close(_handle);
+ _handle = NULL;
return -1;
}
//demande d'interface ...
- err = usb_claim_interface(_handle, USB_INTERFACE);
+ err = libusb_claim_interface(_handle, USB_INTERFACE);
if(err != 0){
_usbmisc_error.set(
- "fails to optain usb interface (erno=%d id=%d vendor=0x%04x product=0x%04x)",
- err, USB_INTERFACE, vendor, product
+ "fails to obtain usb interface, %s (id=%d vendor=0x%04x product=0x%04x)",
+ libusb_strerror(err), USB_INTERFACE, vendor, product
);
+ libusb_close(_handle);
+ _handle = NULL;
return -1;
}
return 0;
@@ -102,34 +107,55 @@
UsbHandle::~UsbHandle(){
//fermeture propre ...
- if(_handle) usb_close(_handle);
+ if(_handle){
+ libusb_release_interface(_handle, USB_INTERFACE);
+ libusb_close(_handle);
+ }
+ if(_dev) libusb_unref_device(_dev);
}
-struct usb_device* UsbHandle::_find_usb_dev(
+libusb_device* UsbHandle::_find_usb_dev(
u_int16_t vendor,
u_int16_t product
){
- struct usb_bus *busses, *bus;
- struct usb_device *devs, *d;
+ libusb_device **list;
u_int16_t v, p;
+ int ret;
- usb_init();
- usb_find_busses();
- usb_find_devices();
- busses = usb_get_busses();
- for(bus = busses; bus; bus = bus->next){
- Verbose(2, "(usb scan) bus dirname=%s\n", bus->dirname);
- devs = bus->devices;
- for(d=devs; d; d=d->next){
- v = d->descriptor.idVendor;
- p = d->descriptor.idProduct;
- Verbose(2, "(usb scan) dev filename=%s, vendor=0x%04x, product=0x%04x\n",
- d->filename, v, p
+ ret = libusb_init(NULL);
+ if(ret < 0){
+ _usbmisc_error.set("libusb_init error: %s\n", libusb_strerror(ret));
+ return NULL;
+ }
+
+ ssize_t cnt = libusb_get_device_list(NULL, &list);
+ if(cnt < 0){
+ _usbmisc_error.set("libusb_get_device_list: %s\n",
+ libusb_strerror(cnt));
+ return NULL;
+ }
+ for(ssize_t i = 0; i < cnt; i++){
+ libusb_device *dev = list[i];
+ struct libusb_device_descriptor desc;
+ int ret = libusb_get_device_descriptor(dev, &desc);
+ if (ret == 0){
+ v = desc.idVendor;
+ p = desc.idProduct;
+ Verbose(2, "(usb scan) vendor=0x%04x, product=0x%04x\n",
+ v, p
);
- if((v == vendor) && (p==product)) return d;
+ if((v == vendor) && (p==product)){
+ libusb_ref_device(dev);
+ libusb_free_device_list(list, 1);
+ return dev;
+ }
}
}
+ libusb_free_device_list(list, 1);
+ _usbmisc_error.set("no usb device found (vendor=0x%04x product=0x%04x)",
+ vendor, product
+ );
return NULL;
}
@@ -235,6 +261,24 @@
fprintf(os,"\n");
}
+int UsbBulkBuff::transfer(unsigned int ep, unsigned char *data, int len, unsigned int to)
+{
+ int transfered;
+ int ret;
+ int total = 0;
+
+ do{
+ ret = libusb_bulk_transfer(_devh, ep, data, len, &transfered, to);
+ if(ret < 0)
+ return ret;
+ total += transfered;
+ data += transfered;
+ len -= transfered;
+ } while(len && !(ep & 0x80));
+
+ return total;
+}
+
int UsbBulkBuff::write_read(
const char* vprolog
){
@@ -244,24 +288,22 @@
return 0;
}
if(Verbose() >= 3){
- Verbose(3,"UsbBulkBuff::usb_bulk_write(0x%02x, 0x%02x, 0x%x, %d, %d)\n",
+ Verbose(3,"UsbBulkBuff::write(0x%02x, 0x%02x, 0x%x, %d, %d)\n",
_devh, _write_ep, _write_buff.data(), _write_buff.len(), _write_to);
_write_buff.dump(stderr);
}
- r = usb_bulk_write(_devh, _write_ep, _write_buff.data(), _write_buff.len(), _write_to);
- Verbose(2, "%s usb_bulk_write %d bytes on %d bytes\n",
+ r = transfer(_write_ep, (unsigned char *)_write_buff.data(), _write_buff.len(), _write_to);
+ Verbose(2, "%s write %d bytes on %d bytes\n",
(vprolog)?vprolog:"", r, _write_buff.len());
- if(r != _write_buff.len()) {
- _usbmisc_error.set("usb_bulk_write failed (%d bytes instead of %d)\n",
- r, _write_buff.len()
- );
+ if(r < 0) {
+ _usbmisc_error.set("write failed, %s\n", libusb_strerror(r));
return -1;
}
- r = usb_bulk_read(_devh, _read_ep, _read_buff.data(), _read_buff.maxsz(), _read_to);
- Verbose(2, "%s usb_bulk_read -> %d bytes\n",
+ r = transfer(_read_ep, (unsigned char *)_read_buff.data(), _read_buff.maxsz(), _read_to);
+ Verbose(2, "%s read -> %d bytes\n",
(vprolog)?vprolog:"", r);
if(r < 0) {
- _usbmisc_error.set("usb_bulk_read failed\n");
+ _usbmisc_error.set("read failed, %s\n", libusb_strerror(r));
return -1;
}
_read_buff.set_len(r);
@@ -319,7 +361,6 @@
return 0;
BUFF_ERROR :
- printf( "usb_strerror = %s\n", usb_strerror( ));
_usbmisc_error.set("internal buffer error");
return -1;
}
Index: t2n-git/src/usbmisc.h
===================================================================
--- t2n-git.orig/src/usbmisc.h 2024-04-15 18:01:34.469399798 +0200
+++ t2n-git/src/usbmisc.h 2024-04-15 18:01:34.469399798 +0200
@@ -71,11 +71,11 @@
class UsbBulkBuff;
class UsbHandle {
- struct usb_device* _dev;
- struct usb_dev_handle* _handle;
+ libusb_device* _dev;
+ libusb_device_handle* _handle;
UsbBulkBuff* _bbuff;
- static struct usb_device* _find_usb_dev(
+ libusb_device* _find_usb_dev(
u_int16_t vendor, u_int16_t product
);
@@ -153,7 +153,7 @@
class UsbBulkBuff {
- struct usb_dev_handle* _devh;
+ libusb_device_handle* _devh;
unsigned int _write_ep;
unsigned int _write_to;
unsigned int _read_ep;
@@ -162,13 +162,14 @@
Buff _read_buff;
Buff _write_buff;
void _init();
+ int transfer(unsigned int ep, unsigned char *buf, int len, unsigned int to);
friend class UsbHandle;
protected:
//Cration, initialisations
UsbBulkBuff();
- inline void init(struct usb_dev_handle* dh, int wep, int wto, int rep, int rto);
+ inline void init(libusb_device_handle* dh, int wep, int wto, int rep, int rto);
//reset des buffers
void reset();
@@ -201,7 +202,7 @@
reset();
}
-inline void UsbBulkBuff::init(struct usb_dev_handle* dh, int wep, int wto, int rep, int rto){
+inline void UsbBulkBuff::init(libusb_device_handle* dh, int wep, int wto, int rep, int rto){
_devh = dh;
_write_ep = wep;
_write_to = wto;
Index: t2n-git/src/usbnxt.cc
===================================================================
--- t2n-git.orig/src/usbnxt.cc 2024-04-15 18:01:34.469399798 +0200
+++ t2n-git/src/usbnxt.cc 2024-04-15 18:01:34.469399798 +0200
@@ -31,9 +31,11 @@
#include <stdio.h>
#include <libgen.h>
#include <errno.h>
+#include <unistd.h>
+#include <libusb.h>
+
#include "errors.h"
#include "errormng.h"
-#include "usb.h"
#include "usbmisc.h"
#include "usbnxt.h"
@@ -570,7 +572,6 @@
);
if (res) return _fails(fmsg);
if (status){
- printf( "usb_strerror = %s\n", usb_strerror( ));
_usbnxt_error.set(
"upload: can't initiate upload (reply=0x%02x,status=0x%02x,error=0x%04x)",
reply, status, res);
@@ -629,51 +630,3 @@
return 0;
}
-
-/*
-int nxt_battery(usb_dev_handle* dev){
- int r;
- buffer com;
- buffer res;
- char reply_c;
- char command_c;
- char status_c;
- unsigned int mV;
-
- com[0] = DIRECT_COMMAND;
- com[1] = GET_BATTERY_LEVEL;
-
- r = do_nxt_command(dev, &com[0], 2, &res[0]);
-
- // retour : byte, byte, byte, short (le)
- if (r != 5) {
- fprintf(stderr, "nxt_battery: bad response (%d bytes)\n", r);
- return -1;
- }
- reply_c = res[0];
- command_c = res[1];
- status_c = res[2];
- mV = ((unsigned int)res[3] << 8) & (unsigned int)res[4];
-
- return (int)mV;
-}
-
-int main(){
- struct usb_dev_handle *nxt;
-
- nxt = find_nxt();
- if(nxt) {
- printf ("TROUV !!!\n");
- } else {
- printf ("pas trouv ...\n");
- return 1;
- }
- usb_reset(nxt);
-
-CleanReturn:
-
- if(nxt) {
- usb_close(nxt);
- }
-}
-*/
Index: t2n-git/src/usbscan.c
===================================================================
--- t2n-git.orig/src/usbscan.c 2024-04-15 18:01:34.469399798 +0200
+++ t2n-git/src/usbscan.c 2024-04-15 18:01:34.469399798 +0200
@@ -26,40 +26,52 @@
----------------------------------------------------------------------------
test de scan sur un port usb ...
--------------------------------------------------------------------------*/
-#include "usb.h"
#include <string.h>
#include <stdio.h>
+#include <libusb.h>
// USB Gnrique : recherche d'un device particulier
-struct usb_device* find_usb_dev(
+struct libusb_device *find_usb_dev(
u_int16_t vendor,
u_int16_t product
){
- struct usb_bus *busses, *bus;
- struct usb_device *devs, *d;
+ libusb_device **list;
u_int16_t v, p;
+ int ret;
- usb_init();
- usb_find_busses();
- usb_find_devices();
- busses = usb_get_busses();
- for(bus = busses; bus; bus = bus->next){
- printf("bus dirname=%s\n", bus->dirname);
- devs = bus->devices;
- for(d=devs; d; d=d->next){
- v = d->descriptor.idVendor;
- p = d->descriptor.idProduct;
- printf(" dev filename=%s, vendor=0x%04x, product=0x%04x\n",
- d->filename, v, p
- );
- if((v == vendor) && (p==product)) return d;
+ ret = libusb_init(NULL);
+ if(ret){
+ fprintf(stderr, "libusb_init: %s\n", libusb_strerror(ret));
+ return NULL;
+ }
+ ssize_t cnt = libusb_get_device_list(NULL, &list);
+ if(cnt < 0){
+ fprintf(stderr, "libusb_get_device_list: %s\n", libusb_strerror(cnt));
+ return NULL;
+ }
+ for(ssize_t i = 0; i < cnt; i++){
+ libusb_device *dev = list[i];
+ struct libusb_device_descriptor desc;
+ int ret = libusb_get_device_descriptor(dev, &desc);
+ if (ret == 0){
+ v = desc.idVendor;
+ p = desc.idProduct;
+ printf(" dev vendor=0x%04x, product=0x%04x\n", v, p);
+ if((v == vendor) && (p==product)){
+ libusb_ref_device(dev);
+ libusb_free_device_list(list, 1);
+ return dev;
+ }
}
}
+ libusb_free_device_list(list, 1);
return NULL;
}
int main(){
- struct usb_device *d;
- d = find_usb_dev(-1,-1);
- return 0;
+ libusb_device *dev;
+ dev = find_usb_dev(-1, -1);
+ if(dev)
+ libusb_unref_device(dev);
+ return dev ? 0 : 1;
}
|