Advertisement
Guest User

recfsusb2n.libusb.patch

a guest
Jun 17th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.73 KB | None | 0 0
  1. diff -uNr recfsusb2n.orig/src/Makefile recfsusb2n/src/Makefile
  2. --- recfsusb2n.orig/src/Makefile 2021-06-17 16:06:14.000000000 +0900
  3. +++ recfsusb2n/src/Makefile 2021-06-17 16:21:31.000000000 +0900
  4. @@ -1,7 +1,7 @@
  5. CC = gcc
  6. -CFLAGS = -O2 -Wall
  7. -OBJS = main.o utils.o message.o osdepend.o usbdevfile.o usbops.o tsthread.o em287x.o em287x_usb.o tc90522.o mxl136.o tda20142.o tssplitter_lite.o
  8. -LIBS = -lpthread
  9. +CFLAGS := -O2 -g -Wall `pkg-config --cflags libusb-1.0` $(CFLAGS)
  10. +OBJS = main.o utils.o message.o osdepend.o tsthread.o em287x.o em287x_usb.o tc90522.o mxl136.o tda20142.o tssplitter_lite.o
  11. +LIBS = -lpthread `pkg-config --libs libusb-1.0` $(LDFLAGS)
  12. TARGET = recfsusb2n
  13. CONF = $(TARGET).conf
  14. PREFIX = /usr/local
  15. diff -uNr recfsusb2n.orig/src/Makefile.dep recfsusb2n/src/Makefile.dep
  16. --- recfsusb2n.orig/src/Makefile.dep 2021-06-17 16:06:14.000000000 +0900
  17. +++ recfsusb2n/src/Makefile.dep 2021-06-17 16:06:40.000000000 +0900
  18. @@ -1,17 +1,14 @@
  19. -main.o: main.c usbdevfile.h types_u.h em287x.h tc90522.h mxl136.h \
  20. - tda20142.h tsthread.h utils.h verstr.h message.h
  21. -utils.o: utils.c utils.h message.h decoder.h tssplitter_lite.h
  22. +main.o: main.c em287x.h types_u.h tc90522.h mxl136.h tda20142.h \
  23. + tsthread.h utils.h verstr.h message.h
  24. +utils.o: utils.c decoder.h utils.h message.h tssplitter_lite.h channel.h
  25. message.o: message.c message.h
  26. osdepend.o: osdepend.c osdepend.h types_u.h
  27. -usbdevfile.o: usbdevfile.c usbdevfile.h types_u.h message.h
  28. -usbops.o: usbops.c usbops.h types_u.h message.h
  29. -tsthread.o: tsthread.c usbops.h types_u.h osdepend.h tsbuff.h tsthread.h \
  30. - message.h
  31. -em287x.o: em287x.c usbops.h types_u.h osdepend.h em287x.h em287x_usb.h \
  32. +tsthread.o: tsthread.c osdepend.h types_u.h tsbuff.h tsthread.h message.h
  33. +em287x.o: em287x.c osdepend.h types_u.h em287x.h em287x_usb.h \
  34. em287x_priv.h message.h
  35. em287x_usb.o: em287x_usb.c osdepend.h types_u.h em287x_usb.h \
  36. em287x_priv.h message.h
  37. tc90522.o: tc90522.c osdepend.h types_u.h tc90522.h message.h
  38. mxl136.o: mxl136.c osdepend.h types_u.h mxl136.h message.h
  39. tda20142.o: tda20142.c osdepend.h types_u.h tda20142.h message.h
  40. -tssplitter_lite.o: tssplitter_lite.c tssplitter_lite.h
  41. +tssplitter_lite.o: tssplitter_lite.c decoder.h tssplitter_lite.h
  42. diff -uNr recfsusb2n.orig/src/em287x.c recfsusb2n/src/em287x.c
  43. --- recfsusb2n.orig/src/em287x.c 2021-06-17 16:06:14.000000000 +0900
  44. +++ recfsusb2n/src/em287x.c 2021-06-17 16:06:40.000000000 +0900
  45. @@ -6,9 +6,9 @@
  46.  
  47. #include <errno.h>
  48. #include <string.h>
  49. -#include <malloc.h>
  50. +#include <stdlib.h>
  51.  
  52. -#include "usbops.h"
  53. +#include <libusb.h>
  54. #include "osdepend.h"
  55. #include "em287x.h"
  56. #include "em287x_usb.h"
  57. @@ -209,7 +209,7 @@
  58. if(( ret = writeReg(s, 0x13, 0x08 ) )) goto err1;
  59.  
  60. if(( ret = writeReg(s, 0x0c, 0x10 ) )) goto err1;
  61. - if(( ret = usb_setinterface(s->fd, 0, 1) )) {
  62. + if(( ret = libusb_set_interface_alt_setting(s->fd, 0, 1) )) {
  63. warn_info(ret,"failed"); goto err1;
  64. }
  65. if(( ret = writeReg(s, 0x5f, 0x80 ) )) goto err1;
  66. @@ -224,7 +224,7 @@
  67. {
  68. int ret;
  69.  
  70. - if(( ret = usb_setinterface(s->fd, 0, 0) )) {
  71. + if(( ret = libusb_set_interface_alt_setting(s->fd, 0, 0) )) {
  72. warn_info(ret,"failed"); goto err1;
  73. }
  74.  
  75. @@ -265,7 +265,7 @@
  76. pusbep->startstopFunc = em287x_startstopStream;
  77. st->chip_id = 0;
  78. st->fd = pusbep->fd;
  79. - if(( ret = usb_claim(st->fd, 0) )) {
  80. + if(( ret = libusb_claim_interface(st->fd, 0) )) {
  81. warn_info(ret,"failed");
  82. return -5;
  83. }
  84. @@ -309,7 +309,7 @@
  85. warn_info(r,"failed");
  86. ret = (ret << 8) | (r & 0xFF);
  87. }
  88. - if((r = usb_release(s->fd, 0))) {
  89. + if((r = libusb_release_interface(s->fd, 0))) {
  90. warn_info(r,"failed");
  91. ret = (ret << 8) | (r & 0xFF);
  92. }
  93. @@ -349,4 +349,4 @@
  94.  
  95.  
  96.  
  97. -/*EOF*/
  98. \ No newline at end of file
  99. +/*EOF*/
  100. diff -uNr recfsusb2n.orig/src/em287x_usb.c recfsusb2n/src/em287x_usb.c
  101. --- recfsusb2n.orig/src/em287x_usb.c 2021-06-17 16:06:14.000000000 +0900
  102. +++ recfsusb2n/src/em287x_usb.c 2021-06-17 16:06:40.000000000 +0900
  103. @@ -7,21 +7,20 @@
  104. #include <stddef.h>
  105. #include <errno.h>
  106. #include <sys/ioctl.h>
  107. -#include <linux/usb/ch9.h>
  108. -#include <linux/usbdevice_fs.h>
  109. +#include <libusb.h>
  110.  
  111. #include "osdepend.h"
  112. #include "em287x_usb.h"
  113. #include "em287x_priv.h"
  114. #include "message.h"
  115.  
  116. -int em287x_ctrl(const int fd, const uint16_t reg, const uint16_t size, uint8_t* const data, const unsigned mode)
  117. +int em287x_ctrl(libusb_device_handle* fd, const uint16_t reg, const uint16_t size, uint8_t* const data, const unsigned mode)
  118. {
  119. int ret;
  120. - struct usbdevfs_ctrltransfer usb_cmd = { USB_TYPE_VENDOR|USB_RECIP_DEVICE, 0, 0, reg, size, USB_TIMEOUT, data};
  121. - usb_cmd.bRequestType |= (mode & 0x1) ? USB_DIR_OUT : USB_DIR_IN;
  122. - usb_cmd.bRequest = (mode >> 8) & 0xFF;
  123. - if(ioctl(fd, USBDEVFS_CONTROL, &usb_cmd) < 0) {
  124. + uint8_t bmRequestType = LIBUSB_REQUEST_TYPE_VENDOR| LIBUSB_RECIPIENT_DEVICE;
  125. + bmRequestType |= (mode & 0x1) ? LIBUSB_ENDPOINT_OUT : LIBUSB_ENDPOINT_IN;
  126. + uint8_t bRequest = (mode >> 8) & 0xFF;
  127. + if(libusb_control_transfer(fd, bmRequestType, bRequest, 0, reg, data, size, USB_TIMEOUT) < 0) {
  128. ret = errno;
  129. warn_info(ret,"%02X_%02X", (mode >> 8) & 0xFF, reg);
  130. return -ret;
  131. diff -uNr recfsusb2n.orig/src/em287x_usb.h recfsusb2n/src/em287x_usb.h
  132. --- recfsusb2n.orig/src/em287x_usb.h 2021-06-17 16:06:14.000000000 +0900
  133. +++ recfsusb2n/src/em287x_usb.h 2021-06-17 16:06:40.000000000 +0900
  134. @@ -8,6 +8,6 @@
  135.  
  136. #define USB_TIMEOUT 400 //# USB ctrl timeout (msec)
  137.  
  138. -int em287x_ctrl(const int fd, const uint16_t reg, const uint16_t size, uint8_t* const data, const unsigned mode);
  139. +int em287x_ctrl(libusb_device_handle* fd, const uint16_t reg, const uint16_t size, uint8_t* const data, const unsigned mode);
  140.  
  141. /*EOF*/
  142. diff -uNr recfsusb2n.orig/src/main.c recfsusb2n/src/main.c
  143. --- recfsusb2n.orig/src/main.c 2021-06-17 16:06:14.000000000 +0900
  144. +++ recfsusb2n/src/main.c 2021-06-17 16:06:40.000000000 +0900
  145. @@ -10,7 +10,7 @@
  146. #include <errno.h>
  147. #include <unistd.h>
  148.  
  149. -#include "usbdevfile.h"
  150. +#include <libusb.h>
  151. #include "em287x.h"
  152. #include "tc90522.h"
  153. #include "mxl136.h"
  154. @@ -26,21 +26,6 @@
  155. static unsigned char volatile caughtSignal = 0;
  156. static void sighandler(int arg) { caughtSignal = 1; }
  157.  
  158. -static int check_usbdevId(const unsigned int* desc)
  159. -{
  160. - //if(0xeb1a == desc[0] && 0x8178 == desc[1]) return 0;
  161. - if(0x0511 == desc[0]) {
  162. - switch( desc[1] ) {
  163. - //case 0x0029:
  164. - //case 0x003b:
  165. - case 0x0045:
  166. - return 0;
  167. - }
  168. - }
  169. - return 1; //# not match
  170. -}
  171. -
  172. -
  173. int main(int argc, char **argv)
  174. {
  175. int ret = 0, i, j;
  176. @@ -58,14 +43,16 @@
  177. msg("recfsusb2n " PRG_VER " " PRG_BUILT " " PRG_TIMESTAMP ", (GPL) 2016 trinity19683\n");
  178. parseOption(argc, argv, &args);
  179.  
  180. - usbep.fd = usbdevfile_alloc(check_usbdevId, &args.devfile);
  181. + libusb_init(NULL);
  182. + usbep.fd = libusb_open_device_with_vid_pid(NULL, 0x0511, 0x0045);
  183. if(0 <= usbep.fd) {
  184. - msg("# ISDB-T/S TV Tuner FSUSB2N: '%s', freq= %u kHz, duration= %u sec.\n", args.devfile, args.freq, args.recsec);
  185. - free(args.devfile);
  186. + msg("# ISDB-T/S TV Tuner FSUSB2N: freq= %u kHz, duration= %u sec.\n", args.freq, args.recsec);
  187. }else{
  188. warn_msg(0,"No device can be used.");
  189. ret = 40; goto end1;
  190. }
  191. + libusb_claim_interface(usbep.fd, 0);
  192. +
  193. u_gettime(&time_begin);
  194. if(em287x_create(&devst, &usbep) != 0) {
  195. ret = 41; goto end3;
  196. @@ -259,9 +246,7 @@
  197. end3:
  198. em287x_destroy(devst);
  199. //end2:
  200. - if(close(usbep.fd) == -1) {
  201. - warn_msg(errno,"failed to close usbdevfile");
  202. - }
  203. + libusb_close(usbep.fd);
  204. end1:
  205. if(0 == ret) {
  206. u_difftime(&time_begin, &time_end, &i, &j);
  207. diff -uNr recfsusb2n.orig/src/mxl136.c recfsusb2n/src/mxl136.c
  208. --- recfsusb2n.orig/src/mxl136.c 2021-06-17 16:06:14.000000000 +0900
  209. +++ recfsusb2n/src/mxl136.c 2021-06-17 16:06:40.000000000 +0900
  210. @@ -7,7 +7,7 @@
  211. #include <stdint.h>
  212. #include <errno.h>
  213. #include <string.h>
  214. -#include <malloc.h>
  215. +#include <stdlib.h>
  216.  
  217. #include "osdepend.h"
  218. #include "mxl136.h"
  219. @@ -183,4 +183,4 @@
  220. }
  221.  
  222.  
  223. -/*EOF*/
  224. \ No newline at end of file
  225. +/*EOF*/
  226. diff -uNr recfsusb2n.orig/src/osdepend.c recfsusb2n/src/osdepend.c
  227. --- recfsusb2n.orig/src/osdepend.c 2021-06-17 16:06:14.000000000 +0900
  228. +++ recfsusb2n/src/osdepend.c 2021-06-17 16:06:40.000000000 +0900
  229. @@ -5,7 +5,7 @@
  230. */
  231.  
  232. #include <pthread.h>
  233. -#include <malloc.h>
  234. +#include <stdlib.h>
  235. #include <errno.h>
  236. #include <unistd.h>
  237. #include "osdepend.h"
  238. @@ -57,4 +57,4 @@
  239. return 0;
  240. }
  241.  
  242. -/*EOF*/
  243. \ No newline at end of file
  244. +/*EOF*/
  245. diff -uNr recfsusb2n.orig/src/tc90522.c recfsusb2n/src/tc90522.c
  246. --- recfsusb2n.orig/src/tc90522.c 2021-06-17 16:06:14.000000000 +0900
  247. +++ recfsusb2n/src/tc90522.c 2021-06-17 16:06:40.000000000 +0900
  248. @@ -7,7 +7,7 @@
  249. #include <stdint.h>
  250. #include <errno.h>
  251. #include <string.h>
  252. -#include <malloc.h>
  253. +#include <stdlib.h>
  254.  
  255. #include "osdepend.h"
  256. #include "tc90522.h"
  257. @@ -640,4 +640,4 @@
  258. }
  259.  
  260.  
  261. -/*EOF*/
  262. \ No newline at end of file
  263. +/*EOF*/
  264. diff -uNr recfsusb2n.orig/src/tda20142.c recfsusb2n/src/tda20142.c
  265. --- recfsusb2n.orig/src/tda20142.c 2021-06-17 16:06:14.000000000 +0900
  266. +++ recfsusb2n/src/tda20142.c 2021-06-17 16:06:40.000000000 +0900
  267. @@ -7,7 +7,7 @@
  268. #include <stdint.h>
  269. #include <errno.h>
  270. #include <string.h>
  271. -#include <malloc.h>
  272. +#include <stdlib.h>
  273.  
  274. #include "osdepend.h"
  275. #include "tda20142.h"
  276. @@ -408,4 +408,4 @@
  277. }
  278.  
  279.  
  280. -/*EOF*/
  281. \ No newline at end of file
  282. +/*EOF*/
  283. diff -uNr recfsusb2n.orig/src/tssplitter_lite.h recfsusb2n/src/tssplitter_lite.h
  284. --- recfsusb2n.orig/src/tssplitter_lite.h 2021-06-17 16:06:14.000000000 +0900
  285. +++ recfsusb2n/src/tssplitter_lite.h 2021-06-17 16:06:40.000000000 +0900
  286. @@ -21,6 +21,7 @@
  287.  
  288. #define __STDC_FORMAT_MACROS
  289. #include <inttypes.h>
  290. +#include <sys/types.h>
  291. #include <unistd.h>
  292.  
  293. #define LENGTH_PACKET (188)
  294. diff -uNr recfsusb2n.orig/src/tsthread.c recfsusb2n/src/tsthread.c
  295. --- recfsusb2n.orig/src/tsthread.c 2021-06-17 16:06:14.000000000 +0900
  296. +++ recfsusb2n/src/tsthread.c 2021-06-17 16:06:40.000000000 +0900
  297. @@ -6,12 +6,11 @@
  298.  
  299. #include <errno.h>
  300. #include <sys/ioctl.h>
  301. -#include <linux/usbdevice_fs.h>
  302. #include <string.h>
  303. #include <poll.h>
  304. #include <pthread.h>
  305.  
  306. -#include "usbops.h"
  307. +#include <libusb.h>
  308. #include "osdepend.h"
  309. #include "tsbuff.h"
  310. #include "tsthread.h"
  311. @@ -36,37 +35,42 @@
  312. int buff_pop;
  313. };
  314.  
  315. +static void cb_iso(struct libusb_transfer* urbp);
  316. +static void cb_bulk(struct libusb_transfer* urbp);
  317.  
  318. -static int submitNextURB(struct tsthread_param* const ps, struct usbdevfs_urb* const urbp)
  319. +static int submitNextURB(struct tsthread_param* const ps, struct libusb_transfer* const urbp)
  320. {
  321. - //# isochronous URB request
  322. int ret = 0, j;
  323.  
  324. - memset(urbp, 0, sizeof(struct usbdevfs_urb) + sizeof(struct usbdevfs_iso_packet_desc) * NUM_ISOC_PACKET );
  325. - urbp->type = (ps->pUSB->endpoint & 0x100) ? USBDEVFS_URB_TYPE_ISO : USBDEVFS_URB_TYPE_BULK;
  326. + urbp->dev_handle = ps->pUSB->fd;
  327. + urbp->flags = 0;
  328. urbp->endpoint = ps->pUSB->endpoint & 0xFF;
  329. - urbp->usercontext = &(ps->actual_length[ps->buff_push]);
  330. + urbp->type = (ps->pUSB->endpoint & 0x100) ? LIBUSB_TRANSFER_TYPE_ISOCHRONOUS : LIBUSB_TRANSFER_TYPE_BULK ;
  331. + urbp->timeout = 4000;
  332. + urbp->user_data = &(ps->actual_length[ps->buff_push]);
  333. urbp->buffer = ps->buffer + (ps->buff_push * ps->buff_unitSize);
  334.  
  335. - if(USBDEVFS_URB_TYPE_BULK == urbp->type) {
  336. - urbp->buffer_length = ps->buff_unitSize;
  337. + if(LIBUSB_TRANSFER_TYPE_BULK == urbp->type) {
  338. + urbp->length = ps->buff_unitSize;
  339. ps->actual_length[ps->buff_push] = -2;
  340. + urbp->callback = &cb_bulk;
  341. }else{
  342. - urbp->number_of_packets = NUM_ISOC_PACKET;
  343. - urbp->flags = USBDEVFS_URB_ISO_ASAP;
  344. + urbp->num_iso_packets = NUM_ISOC_PACKET;
  345. + urbp->flags = 0;
  346. + urbp->callback = &cb_iso;
  347. for(j = 0; j < NUM_ISOC_PACKET; j++ ) {
  348. - urbp->iso_frame_desc[j].length = ps->buff_unitSize;
  349. + urbp->iso_packet_desc[j].length = ps->buff_unitSize;
  350. ps->actual_length[ps->buff_push + j] = -2;
  351. }
  352. }
  353.  
  354. - if(ioctl(ps->pUSB->fd, USBDEVFS_SUBMITURB, urbp) < 0) {
  355. + if(libusb_submit_transfer(urbp) < 0) {
  356. ret = -errno;
  357. warn_info(errno,"submitURB failed");
  358. urbp->buffer = NULL;
  359. }else{
  360. int next_index = ps->buff_push;
  361. - if(USBDEVFS_URB_TYPE_BULK == urbp->type) {
  362. + if(LIBUSB_TRANSFER_TYPE_BULK == urbp->type) {
  363. next_index ++;
  364. }else{
  365. next_index += NUM_ISOC_PACKET;
  366. @@ -76,67 +80,48 @@
  367. return ret;
  368. }
  369.  
  370. -static int reapURB(struct tsthread_param* const ps)
  371. +static void cb_iso(struct libusb_transfer* urbp)
  372. {
  373. - struct usbdevfs_urb *urbp;
  374. - int i = 0, j, ret;
  375. -
  376. - for(;;) {
  377. - if((ret = ioctl(ps->pUSB->fd, USBDEVFS_REAPURBNDELAY, (void *)(&urbp)) ) < 0) {
  378. - if(EAGAIN == errno || EWOULDBLOCK == errno) {
  379. - //# no more data to be received
  380. - ret = -EAGAIN;
  381. - }else{
  382. - warn_info(errno,"reapURB_ndelay failed");
  383. - ret = -errno;
  384. - }
  385. - }
  386. - if(0 > ret) break;
  387. -
  388. - //# received
  389. - int* const pLen = urbp->usercontext;
  390. - if(USBDEVFS_URB_TYPE_BULK == urbp->type) {
  391. - if(0 == urbp->status) {
  392. - //# success
  393. - int actual_length = urbp->actual_length;
  394. - if(ps->buff_unitSize < actual_length) {
  395. - warn_info(actual_length,"reapURB overflow");
  396. - actual_length = ps->buff_unitSize;
  397. - }
  398. - pLen[0] = actual_length;
  399. - }else{
  400. - //# failed
  401. - pLen[0] = 0;
  402. - warn_info(urbp->status,"reapURB");
  403. - }
  404. + if (urbp->status != LIBUSB_TRANSFER_COMPLETED) {
  405. + return;
  406. + }
  407. +
  408. + int j;
  409. + int* const pLen = urbp->user_data;
  410. + const int numPkts = urbp->num_iso_packets;
  411. + for(j = 0; j < numPkts; j++ ) {
  412. + struct libusb_iso_packet_descriptor* const isocDesc = &(urbp->iso_packet_desc[j]);
  413. + if(0 == isocDesc->status) {
  414. + //# success
  415. + int actual_length = isocDesc->actual_length;
  416. + pLen[j] = actual_length;
  417. }else{
  418. - const int numPkts = urbp->number_of_packets;
  419. - for(j = 0; j < numPkts; j++ ) {
  420. - struct usbdevfs_iso_packet_desc* const isocDesc = &(urbp->iso_frame_desc[j]);
  421. - if(0 == isocDesc->status) {
  422. - //# success
  423. - int actual_length = isocDesc->actual_length;
  424. - if(ps->buff_unitSize < actual_length) {
  425. - warn_info(actual_length,"reapURB overflow");
  426. - actual_length = ps->buff_unitSize;
  427. - }
  428. - pLen[j] = actual_length;
  429. - }else{
  430. - //# failed
  431. - pLen[j] = 0;
  432. - warn_info(isocDesc->status,"reapURB");
  433. - }
  434. - }
  435. + //# failed
  436. + pLen[j] = 0;
  437. + warn_info(isocDesc->status,"reapURB");
  438. }
  439. - //# mark this URB unused
  440. - urbp->buffer = NULL;
  441. - i++;
  442. }
  443. +
  444. + urbp->buffer = NULL;
  445. +}
  446.  
  447. - if(-EAGAIN != ret) {
  448. - return ret;
  449. +static void cb_bulk(struct libusb_transfer* urbp)
  450. +{
  451. + if (urbp->status != LIBUSB_TRANSFER_COMPLETED) {
  452. + return;
  453. }
  454. - return i;
  455. +
  456. + int* const pLen = urbp->user_data;
  457. + if(0 == urbp->status) {
  458. + //# success
  459. + int actual_length = urbp->actual_length;
  460. + pLen[0] = actual_length;
  461. + }else{
  462. + //# failed
  463. + pLen[0] = 0;
  464. + warn_info(urbp->status,"reapURB");
  465. + }
  466. + urbp->buffer = NULL;
  467. }
  468.  
  469. /* TS thread function issues URB requests. */
  470. @@ -144,32 +129,28 @@
  471. {
  472. struct tsthread_param* const ps = param;
  473. int ret, i;
  474. - const unsigned UrbSize = ROUNDUP( sizeof(struct usbdevfs_urb) + sizeof(struct usbdevfs_iso_packet_desc) * NUM_ISOC_PACKET ,0xF);
  475. - void* ptrUrb;
  476. + struct libusb_transfer* pUrb[TS_MaxNumIO];
  477.  
  478. - ptrUrb = uHeapAlloc(UrbSize * TS_MaxNumIO);
  479. - if(! ptrUrb) {
  480. - warn_info(errno,"failed to allocate");
  481. - return;
  482. + for (i = 0; i < TS_MaxNumIO; i++) {
  483. + pUrb[i] = libusb_alloc_transfer(NUM_ISOC_PACKET);
  484. + if(!pUrb[i]) {
  485. + warn_info(errno,"failed to allocate");
  486. + return;
  487. + }
  488. + pUrb[i]->buffer = NULL;
  489. }
  490.  
  491. ps->buff_push = 0;
  492. - for(i = 0; i < TS_MaxNumIO; i++) {
  493. - struct usbdevfs_urb* const pUrb = ptrUrb + UrbSize * i;
  494. - //# mark this URB unused
  495. - pUrb->buffer = NULL;
  496. - }
  497.  
  498. for(;;) {
  499. int num = TS_MaxNumIO;
  500. for(i = 0; i < TS_MaxNumIO; i++) {
  501. - struct usbdevfs_urb* const pUrb = ptrUrb + UrbSize * i;
  502. - if(! pUrb->buffer) {
  503. + if(! pUrb[i]->buffer) {
  504. if(ps->flags & 0x01) {
  505. num--;
  506. }else{
  507. //# continue to issue a new URB request
  508. - submitNextURB(ps, pUrb);
  509. + submitNextURB(ps, pUrb[i]);
  510. }
  511. }
  512. }
  513. @@ -179,33 +160,23 @@
  514. }
  515. if(ps->flags & 0x02) {
  516. //# canceled
  517. - reapURB(ps);
  518. for(i = 0; i < TS_MaxNumIO; i++) {
  519. - struct usbdevfs_urb* const pUrb = ptrUrb + UrbSize * i;
  520. - if(! pUrb->buffer) continue;
  521. - if(ioctl(ps->pUSB->fd, USBDEVFS_DISCARDURB, pUrb) < 0) {
  522. + if(! pUrb[i]->buffer) continue;
  523. + if(libusb_cancel_transfer(pUrb[i]) < 0) {
  524. warn_info(errno,"failed to discard URB");
  525. }
  526. }
  527. + libusb_handle_events(NULL);
  528. break;
  529. }
  530. -
  531. - struct pollfd pfd;
  532. - pfd.fd = ps->pUSB->fd;
  533. - pfd.events = POLLOUT | POLLERR;
  534. - ret = poll(&pfd, 1, TS_PollTimeout);
  535. - if(0 > ret) {
  536. - warn_info(errno,"poll failed");
  537. - break;
  538. - }else{
  539. - if(0 == ret) {
  540. - //# timeout
  541. - if(!(ps->flags & 0x01)) dmsg("poll timeout");
  542. - }
  543. - if(reapURB(ps) < 0) break;
  544. + ret = libusb_handle_events(NULL);
  545. + if (ret == 1) {
  546. + warn_info(errno, "libusb_handle_events timeout");
  547. }
  548. }
  549. - uHeapFree(ptrUrb);
  550. + for (i = 0; i < TS_MaxNumIO; i++)
  551. + if (pUrb[i])
  552. + libusb_free_transfer(pUrb[i]);
  553. ps->flags |= 0x01; //# continue = F
  554. }
  555.  
  556. @@ -324,19 +295,6 @@
  557. return ps->actual_length[j];
  558. }
  559.  
  560. -int tsthread_wait(const tsthread_ptr tptr, const int timeout)
  561. -{
  562. - struct tsthread_param* const ps = tptr;
  563. - int ret;
  564. - struct pollfd pfd;
  565. - pfd.fd = ps->pUSB->fd;
  566. - pfd.events = POLLOUT | POLLERR;
  567. - ret = poll(&pfd, 1, timeout);
  568. - if(0 > ret) {
  569. - warn_info(errno,"poll failed");
  570. - }
  571. - return ret;
  572. -}
  573.  
  574.  
  575. /*EOF*/
  576. diff -uNr recfsusb2n.orig/src/types_u.h recfsusb2n/src/types_u.h
  577. --- recfsusb2n.orig/src/types_u.h 2021-06-17 16:06:14.000000000 +0900
  578. +++ recfsusb2n/src/types_u.h 2021-06-17 16:06:40.000000000 +0900
  579. @@ -4,8 +4,9 @@
  580. 2016-01-22
  581. */
  582. #pragma once
  583. +#include <libusb.h>
  584.  
  585. -typedef int HANDLE;
  586. +typedef libusb_device_handle* HANDLE;
  587. typedef void* PMUTEX;
  588.  
  589. struct usb_endpoint_st {
  590. @@ -22,4 +23,4 @@
  591. unsigned char addr;
  592. };
  593.  
  594. -/*EOF*/
  595. \ No newline at end of file
  596. +/*EOF*/
  597. diff -uNr recfsusb2n.orig/src/usbdevfile.c recfsusb2n/src/usbdevfile.c
  598. --- recfsusb2n.orig/src/usbdevfile.c 2021-06-17 16:06:14.000000000 +0900
  599. +++ recfsusb2n/src/usbdevfile.c 1970-01-01 09:00:00.000000000 +0900
  600. @@ -1,154 +0,0 @@
  601. -/* fsusb2i (c) 2015 trinity19683
  602. - USB device file (Linux OSes)
  603. - usbdevfile.c
  604. - 2015-12-28
  605. -*/
  606. -
  607. -#include <errno.h>
  608. -#include <unistd.h>
  609. -#include <string.h>
  610. -#include <sys/file.h>
  611. -#include <linux/usb/ch9.h>
  612. -#include <dirent.h>
  613. -
  614. -#include "usbdevfile.h"
  615. -#include "message.h"
  616. -
  617. -#define PATH_SEPARATOR '/'
  618. -
  619. -const char* BASE_DIR_UDEV = "/dev/bus/usb";
  620. -
  621. -static int usb_getdesc(const char *devfile, struct usb_device_descriptor* desc)
  622. -{
  623. - int f;
  624. -
  625. - f = open(devfile, O_RDONLY);
  626. - if(-1 == f) {
  627. - warn_msg(errno,"can't open usbdevfile to read '%s'", devfile);
  628. - return errno;
  629. - }
  630. - memset(desc, 0, sizeof(struct usb_device_descriptor));
  631. - ssize_t rlen = read(f, desc, sizeof(struct usb_device_descriptor));
  632. - if(-1 == rlen) {
  633. - warn_msg(errno,"can't read usbdevfile '%s'", devfile);
  634. - return errno;
  635. - }
  636. - if(close(f) == -1) {
  637. - warn_msg(errno,"can't close usbdevfile '%s'", devfile);
  638. - return errno;
  639. - }
  640. -
  641. - return 0;
  642. -}
  643. -
  644. -static int path_strcat(char* const path, const int lpos, const char* const fname, const size_t maxlen)
  645. -{
  646. - int len = strlen(fname);
  647. - if(lpos + len >= maxlen -2) {
  648. - //# buffer overflow
  649. - return -1;
  650. - }
  651. - path[lpos] = PATH_SEPARATOR;
  652. - memcpy(&path[lpos +1], fname, 1 + len);
  653. - return lpos + 1 + len;
  654. -}
  655. -
  656. -static HANDLE usb_open(const char *devfile)
  657. -{
  658. - HANDLE fd;
  659. -
  660. - if((fd = open(devfile, O_RDWR)) == -1) {
  661. - warn_msg(errno,"can't open usbdevfile to read/write '%s'", devfile);
  662. - return -errno;
  663. - }
  664. - if(flock(fd, LOCK_EX | LOCK_NB) < 0) {
  665. - warn_msg(errno,"share violation. usbdevfile '%s'", devfile);
  666. - if(close(fd) == -1) {
  667. - warn_msg(errno,"failed to close usbdevfile '%s'", devfile);
  668. - //return -errno;
  669. - }
  670. - return -errno;
  671. - }
  672. - return fd;
  673. -}
  674. -
  675. -HANDLE usbdevfile_alloc(int (*check_func)(const unsigned int*), char** const pdevfile)
  676. -{
  677. -#define MAX_DEPTH_DIR 2
  678. - DIR *dp[MAX_DEPTH_DIR];
  679. - int depth, dirpath_len;
  680. - char dirpath[PATH_MAX];
  681. -
  682. - if(NULL == pdevfile) {
  683. - return -EINVAL;
  684. - }
  685. - if(NULL != *pdevfile) {
  686. - return usb_open(*pdevfile);
  687. - }
  688. -
  689. - strcpy(dirpath, BASE_DIR_UDEV);
  690. - dirpath_len = strlen(BASE_DIR_UDEV);
  691. - dp[0] = NULL;
  692. - depth = 0;
  693. - while(0 <= depth) {
  694. - if(NULL == dp[depth]) {
  695. - dp[depth] = opendir(dirpath);
  696. - }
  697. - if(NULL == dp[depth]) {
  698. - warn_info(0,"failed to opendir '%s'", dirpath);
  699. - depth--;
  700. - continue;
  701. - }
  702. - struct dirent *dentry;
  703. - while((dentry = readdir(dp[depth])) != NULL) {
  704. - if(DT_CHR == dentry->d_type) {
  705. - //# found USB devfile
  706. - const int r = path_strcat(dirpath, dirpath_len, dentry->d_name, sizeof(dirpath));
  707. - if(0 > r)
  708. - goto endcheck_usbdevfile; //# buffer overflow check
  709. -
  710. - struct usb_device_descriptor desc;
  711. - if(usb_getdesc(dirpath, &desc) != 0)
  712. - goto endcheck_usbdevfile;
  713. - unsigned int tmpbuf[2];
  714. - tmpbuf[0] = desc.idVendor;
  715. - tmpbuf[1] = desc.idProduct;
  716. - if(check_func(tmpbuf) != 0)
  717. - goto endcheck_usbdevfile;
  718. - HANDLE fd = usb_open(dirpath);
  719. - if(0 <= fd) {
  720. - //# usb_open success
  721. - *pdevfile = strdup(dirpath);
  722. - return fd;
  723. - }
  724. -endcheck_usbdevfile:
  725. - dirpath[dirpath_len] = 0;
  726. -
  727. - }else if(MAX_DEPTH_DIR -1 > depth && DT_DIR == dentry->d_type) {
  728. - if(strcmp(dentry->d_name,".") == 0 || strcmp(dentry->d_name,"..") == 0) {
  729. - //# self or parent directory, skip
  730. - continue;
  731. - }
  732. - const int len = path_strcat(dirpath, dirpath_len, dentry->d_name, sizeof(dirpath));
  733. - if(0 <= len) { //# buffer overflow check
  734. - dirpath_len = len;
  735. - dp[++depth] = NULL;
  736. - break;
  737. - }
  738. - }
  739. - }
  740. - if(NULL == dentry) {
  741. - //# end of directory stream
  742. - closedir(dp[depth]);
  743. - char* const ptr = strrchr(dirpath, PATH_SEPARATOR);
  744. - if(NULL != ptr) {
  745. - *ptr = 0;
  746. - dirpath_len = (int)(ptr - dirpath);
  747. - }
  748. - depth--;
  749. - }
  750. - }
  751. - return -1;
  752. -}
  753. -
  754. -/*EOF*/
  755. diff -uNr recfsusb2n.orig/src/usbdevfile.h recfsusb2n/src/usbdevfile.h
  756. --- recfsusb2n.orig/src/usbdevfile.h 2021-06-17 16:06:14.000000000 +0900
  757. +++ recfsusb2n/src/usbdevfile.h 1970-01-01 09:00:00.000000000 +0900
  758. @@ -1,11 +0,0 @@
  759. -/* fsusb2i (c) 2015 trinity19683
  760. - USB device file
  761. - usbdevfile.h
  762. - 2015-12-09
  763. -*/
  764. -#pragma once
  765. -#include "types_u.h"
  766. -
  767. -HANDLE usbdevfile_alloc(int (*check_func)(const unsigned int*), char** const pdevfile);
  768. -
  769. -/*EOF*/
  770. diff -uNr recfsusb2n.orig/src/usbops.c recfsusb2n/src/usbops.c
  771. --- recfsusb2n.orig/src/usbops.c 2021-06-17 16:06:14.000000000 +0900
  772. +++ recfsusb2n/src/usbops.c 1970-01-01 09:00:00.000000000 +0900
  773. @@ -1,67 +0,0 @@
  774. -/* SunPTV-USB (c) 2016 trinity19683
  775. - USB operations (Linux OSes)
  776. - usbops.c
  777. - 2016-01-11
  778. -*/
  779. -
  780. -#include <errno.h>
  781. -#include <sys/ioctl.h>
  782. -#include <linux/usbdevice_fs.h>
  783. -#include "usbops.h"
  784. -#include "message.h"
  785. -
  786. -int usb_reset(HANDLE fd)
  787. -{
  788. - if(ioctl(fd, USBDEVFS_RESET, 0) < 0) {
  789. - warn_info(errno,"failed");
  790. - return errno;
  791. - }
  792. - return 0;
  793. -}
  794. -
  795. -int usb_claim(HANDLE fd, unsigned int interface)
  796. -{
  797. - if(ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interface) < 0) {
  798. - if(errno == EBUSY) {
  799. - //# BUSY
  800. - warn_msg(0,"USB interface%u is busy", interface);
  801. - }else{
  802. - warn_info(errno,"failed");
  803. - }
  804. - return errno;
  805. - }
  806. - return 0;
  807. -}
  808. -
  809. -int usb_release(HANDLE fd, unsigned int interface)
  810. -{
  811. - if(ioctl(fd, USBDEVFS_RELEASEINTERFACE, &interface) < 0) {
  812. - warn_info(errno,"failed");
  813. - return errno;
  814. - }
  815. - return 0;
  816. -}
  817. -
  818. -int usb_setconfiguration(HANDLE fd, unsigned int confignum)
  819. -{
  820. - return ioctl(fd, USBDEVFS_SETCONFIGURATION, &confignum);
  821. -}
  822. -
  823. -int usb_setinterface(HANDLE fd, const unsigned int interface, const unsigned int altsetting)
  824. -{
  825. - struct usbdevfs_setinterface setintf;
  826. - setintf.interface = interface;
  827. - setintf.altsetting = altsetting;
  828. - return ioctl(fd, USBDEVFS_SETINTERFACE, &setintf);
  829. -}
  830. -
  831. -int usb_clearhalt(HANDLE fd, unsigned int endpoint)
  832. -{
  833. - if(ioctl(fd, USBDEVFS_CLEAR_HALT, &endpoint) < 0) {
  834. - warn_info(errno,"EP=%02X failed", endpoint);
  835. - return -errno;
  836. - }
  837. - return 0;
  838. -}
  839. -
  840. -/*EOF*/
  841. diff -uNr recfsusb2n.orig/src/usbops.h recfsusb2n/src/usbops.h
  842. --- recfsusb2n.orig/src/usbops.h 2021-06-17 16:06:14.000000000 +0900
  843. +++ recfsusb2n/src/usbops.h 1970-01-01 09:00:00.000000000 +0900
  844. @@ -1,16 +0,0 @@
  845. -/* SunPTV-USB (c) 2016 trinity19683
  846. - USB operations
  847. - usbops.h
  848. - 2016-01-11
  849. -*/
  850. -#pragma once
  851. -#include "types_u.h"
  852. -
  853. -int usb_reset(HANDLE);
  854. -int usb_claim(HANDLE, unsigned int interface);
  855. -int usb_release(HANDLE, unsigned int interface);
  856. -int usb_setconfiguration(HANDLE, unsigned int confignum);
  857. -int usb_setinterface(HANDLE, const unsigned int interface, const unsigned int altsetting);
  858. -int usb_clearhalt(HANDLE, unsigned int endpoint);
  859. -
  860. -/*EOF*/
  861. diff -uNr recfsusb2n.orig/src/utils.c recfsusb2n/src/utils.c
  862. --- recfsusb2n.orig/src/utils.c 2021-06-17 16:06:14.000000000 +0900
  863. +++ recfsusb2n/src/utils.c 2021-06-17 16:12:55.000000000 +0900
  864. @@ -38,7 +38,7 @@
  865. #ifdef STD_B25
  866. msg(" [--b25]");
  867. #endif
  868. - msg(" [--dev devfile] [--tsid n] [--sid n1,n2,...] channel recsec destfile\n");
  869. + msg(" [--tsid n] [--sid n1,n2,...] channel recsec destfile\n");
  870. exit(1);
  871. }
  872.  
  873. @@ -51,7 +51,6 @@
  874. for(;;) {
  875. int option_index = 0;
  876. static struct option long_options[] = {
  877. - {"dev", required_argument, NULL, 'd'},
  878. #ifdef STD_B25
  879. {"b25", no_argument, NULL, 'b'},
  880. {"B25", no_argument, NULL, 'b'},
  881. @@ -69,10 +68,6 @@
  882. case 'v': //# verbose
  883. args->flags |= 0x1;
  884. break;
  885. - case 'd': //# specify devfile (usbdevfs)
  886. - if( optarg )
  887. - args->devfile = strdup(optarg);
  888. - break;
  889. #ifdef STD_B25
  890. case 'b': //# enable descrambling (STD-B25)
  891. args->flags |= 0x1000;
  892. @@ -536,10 +531,21 @@
  893. char *p, buf[256];
  894. ssize_t len;
  895.  
  896. +#ifdef __linux__
  897. if((len = readlink("/proc/self/exe", buf, sizeof(buf) - 8)) == -1)
  898. return 2;
  899. buf[len] = '\0';
  900. strcat(buf, ".conf");
  901. +#else
  902. +#ifdef __APPLE__
  903. + uint32_t bufsize = 256;
  904. + _NSGetExecutablePath(buf, &bufsize);
  905. + strcat(buf, ".conf");
  906. +
  907. +#else
  908. + strcpy(buf, "recfsusb2n.conf");
  909. +#endif
  910. +#endif
  911.  
  912. fp = fopen(buf, "r");
  913. if(fp == NULL) {
  914. @@ -593,4 +599,4 @@
  915. return 0;
  916. }
  917.  
  918. -/*EOF*/
  919. \ No newline at end of file
  920. +/*EOF*/
  921. diff -uNr recfsusb2n.orig/src/utils.h recfsusb2n/src/utils.h
  922. --- recfsusb2n.orig/src/utils.h 2021-06-17 16:06:14.000000000 +0900
  923. +++ recfsusb2n/src/utils.h 2021-06-17 16:06:40.000000000 +0900
  924. @@ -15,7 +15,6 @@
  925. unsigned int recsec;
  926. unsigned int splitter;
  927. char sid_list[32];
  928. - char* devfile;
  929. char* destfile;
  930. };
  931.  
  932.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement