C   14

logitech-mouse.c

Guest on 14th May 2021 12:40:43 AM

  1. /* This file is generated with usbsnoop2libusb.pl from a usbsnoop log file. */
  2. /* Latest version of the script should be in http://iki.fi/lindi/usb/usbsnoop2libusb.pl */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <signal.h>
  8. #include <ctype.h>
  9. #include <usb.h>
  10. #if 0
  11.  #include <linux/usbdevice_fs.h>
  12.  #define LIBUSB_AUGMENT
  13.  #include "libusb_augment.h"
  14. #endif
  15.  
  16. struct usb_dev_handle *devh;
  17.  
  18. void release_usb_device(int dummy) {
  19.     int ret;
  20.     ret = usb_release_interface(devh, 0);
  21.     if (!ret)
  22.     printf("failed to release interface: %d\n", ret);
  23.     usb_close(devh);
  24.     if (!ret)
  25.     printf("failed to close interface: %d\n", ret);
  26.     exit(1);
  27. }
  28.  
  29. void list_devices() {
  30.     struct usb_bus *bus;
  31.     for (bus = usb_get_busses(); bus; bus = bus->next) {
  32.     struct usb_device *dev;
  33.    
  34.     for (dev = bus->devices; dev; dev = dev->next)
  35.         printf("0x%04x 0x%04x\n",
  36.            dev->descriptor.idVendor,
  37.            dev->descriptor.idProduct);
  38.     }
  39. }    
  40.  
  41. struct usb_device *find_device(int vendor, int product) {
  42.     struct usb_bus *bus;
  43.    
  44.     for (bus = usb_get_busses(); bus; bus = bus->next) {
  45.     struct usb_device *dev;
  46.    
  47.     for (dev = bus->devices; dev; dev = dev->next) {
  48.         if (dev->descriptor.idVendor == vendor
  49.         && dev->descriptor.idProduct == product)
  50.         return dev;
  51.     }
  52.     }
  53.     return NULL;
  54. }
  55.  
  56. void print_bytes(char *bytes, int len) {
  57.     int i;
  58.     if (len > 0) {
  59.     for (i=0; i<len; i++) {
  60.         printf("%02x ", (int)((unsigned char)bytes[i]));
  61.     }
  62.     printf("\"");
  63.         for (i=0; i<len; i++) {
  64.         printf("%c", isprint(bytes[i]) ? bytes[i] : '.');
  65.         }
  66.         printf("\"");
  67.     }
  68. }
  69.  
  70.  
  71. int main(int argc, char **argv) {
  72.     int ret, vendor, product;
  73.     struct usb_device *dev;
  74.     char buf[65535];
  75. #if 0
  76.     usb_urb *isourb;
  77.     struct timeval isotv;
  78.     char isobuf[32768];
  79. #endif
  80.  
  81.     usb_init();
  82.     usb_set_debug(255);
  83.     usb_find_busses();
  84.     usb_find_devices();
  85.  
  86.     if (argc!=3) {
  87.     printf("usage: %s vendorID productID\n", argv[0]);
  88.     printf("ID numbers of currently attached devices:\n");
  89.     list_devices();
  90.     exit(1);
  91.     }
  92.     vendor = strtol(argv[1], NULL, 16);
  93.     product = strtol(argv[2], NULL, 16);
  94.     if (vendor <= 1 || product <= 1) {
  95.     printf("invalid vendor or product id\n");
  96.     exit(1);
  97.     }
  98.     dev = find_device(vendor, product);
  99.     assert(dev);
  100.  
  101.     devh = usb_open(dev);
  102.     assert(devh);
  103.    
  104.     signal(SIGTERM, release_usb_device);
  105.  
  106.     ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
  107.     printf("usb_get_driver_np returned %d\n", ret);
  108.     if (ret == 0) {
  109.     printf("interface 0 already claimed by driver \"%s\", attempting to detach it\n", buf);
  110.     ret = usb_detach_kernel_driver_np(devh, 0);
  111.     printf("usb_detach_kernel_driver_np returned %d\n", ret);
  112.     }
  113.     ret = usb_claim_interface(devh, 0);
  114.     if (ret != 0) {
  115.     printf("claim failed with error %d\n", ret);
  116.         exit(1);
  117.     }
  118.    
  119.     ret = usb_set_altinterface(devh, 0);
  120.     assert(ret >= 0);
  121.  
  122. ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
  123. printf("1 get descriptor returned %d, bytes: ", ret);
  124. print_bytes(buf, ret);
  125. printf("\n");
  126. usleep(1*1000);
  127. ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
  128. printf("2 get descriptor returned %d, bytes: ", ret);
  129. print_bytes(buf, ret);
  130. printf("\n");
  131. usleep(1*1000);
  132. ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000022);
  133. printf("3 get descriptor returned %d, bytes: ", ret);
  134. print_bytes(buf, ret);
  135. printf("\n");
  136. usleep(2*1000);
  137. ret = usb_release_interface(devh, 0);
  138. if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
  139. ret = usb_set_configuration(devh, 0x0000001);
  140. printf("4 set configuration returned %d\n", ret);
  141. ret = usb_claim_interface(devh, 0);
  142. if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
  143. ret = usb_set_altinterface(devh, 0);
  144. printf("4 set alternate setting returned %d\n", ret);
  145. usleep(25*1000);
  146. ret = usb_control_msg(devh, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 0x000000a, 0x0000000, 0x0000000, buf, 0x0000000, 1000);
  147. printf("5 control msg returned %d", ret);
  148. printf("\n");
  149. usleep(1*1000);
  150. ret = usb_get_descriptor(devh, 0x0000022, 0x0000000, buf, 0x0000074);
  151. printf("6 get descriptor returned %d, bytes: ", ret);
  152. print_bytes(buf, ret);
  153. printf("\n");
  154. usleep(4*1000);
  155. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  156. printf("7 interrupt read returned %d, bytes: ", ret);
  157. print_bytes(buf, ret);
  158. printf("\n");
  159. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  160. printf("8 interrupt read returned %d, bytes: ", ret);
  161. print_bytes(buf, ret);
  162. printf("\n");
  163. usleep(2500*1000);
  164. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  165. printf("9 interrupt read returned %d, bytes: ", ret);
  166. print_bytes(buf, ret);
  167. printf("\n");
  168. usleep(104*1000);
  169. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  170. printf("10 interrupt read returned %d, bytes: ", ret);
  171. print_bytes(buf, ret);
  172. printf("\n");
  173. usleep(2500*1000);
  174. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  175. printf("11 interrupt read returned %d, bytes: ", ret);
  176. print_bytes(buf, ret);
  177. printf("\n");
  178. usleep(16*1000);
  179. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  180. printf("12 interrupt read returned %d, bytes: ", ret);
  181. print_bytes(buf, ret);
  182. printf("\n");
  183. usleep(8*1000);
  184. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  185. printf("13 interrupt read returned %d, bytes: ", ret);
  186. print_bytes(buf, ret);
  187. printf("\n");
  188. usleep(8*1000);
  189. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  190. printf("14 interrupt read returned %d, bytes: ", ret);
  191. print_bytes(buf, ret);
  192. printf("\n");
  193. usleep(8*1000);
  194. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  195. printf("15 interrupt read returned %d, bytes: ", ret);
  196. print_bytes(buf, ret);
  197. printf("\n");
  198. usleep(8*1000);
  199. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  200. printf("16 interrupt read returned %d, bytes: ", ret);
  201. print_bytes(buf, ret);
  202. printf("\n");
  203. usleep(8*1000);
  204. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  205. printf("17 interrupt read returned %d, bytes: ", ret);
  206. print_bytes(buf, ret);
  207. printf("\n");
  208. usleep(8*1000);
  209. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  210. printf("18 interrupt read returned %d, bytes: ", ret);
  211. print_bytes(buf, ret);
  212. printf("\n");
  213. usleep(8*1000);
  214. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  215. printf("19 interrupt read returned %d, bytes: ", ret);
  216. print_bytes(buf, ret);
  217. printf("\n");
  218. usleep(8*1000);
  219. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  220. printf("20 interrupt read returned %d, bytes: ", ret);
  221. print_bytes(buf, ret);
  222. printf("\n");
  223. usleep(8*1000);
  224. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  225. printf("21 interrupt read returned %d, bytes: ", ret);
  226. print_bytes(buf, ret);
  227. printf("\n");
  228. usleep(8*1000);
  229. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  230. printf("22 interrupt read returned %d, bytes: ", ret);
  231. print_bytes(buf, ret);
  232. printf("\n");
  233. usleep(8*1000);
  234. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  235. printf("23 interrupt read returned %d, bytes: ", ret);
  236. print_bytes(buf, ret);
  237. printf("\n");
  238. usleep(8*1000);
  239. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  240. printf("24 interrupt read returned %d, bytes: ", ret);
  241. print_bytes(buf, ret);
  242. printf("\n");
  243. usleep(8*1000);
  244. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  245. printf("25 interrupt read returned %d, bytes: ", ret);
  246. print_bytes(buf, ret);
  247. printf("\n");
  248. usleep(8*1000);
  249. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  250. printf("26 interrupt read returned %d, bytes: ", ret);
  251. print_bytes(buf, ret);
  252. printf("\n");
  253. usleep(8*1000);
  254. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  255. printf("27 interrupt read returned %d, bytes: ", ret);
  256. print_bytes(buf, ret);
  257. printf("\n");
  258. usleep(8*1000);
  259. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  260. printf("28 interrupt read returned %d, bytes: ", ret);
  261. print_bytes(buf, ret);
  262. printf("\n");
  263. usleep(8*1000);
  264. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  265. printf("29 interrupt read returned %d, bytes: ", ret);
  266. print_bytes(buf, ret);
  267. printf("\n");
  268. usleep(8*1000);
  269. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  270. printf("30 interrupt read returned %d, bytes: ", ret);
  271. print_bytes(buf, ret);
  272. printf("\n");
  273. usleep(8*1000);
  274. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  275. printf("31 interrupt read returned %d, bytes: ", ret);
  276. print_bytes(buf, ret);
  277. printf("\n");
  278. usleep(8*1000);
  279. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  280. printf("32 interrupt read returned %d, bytes: ", ret);
  281. print_bytes(buf, ret);
  282. printf("\n");
  283. usleep(8*1000);
  284. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  285. printf("33 interrupt read returned %d, bytes: ", ret);
  286. print_bytes(buf, ret);
  287. printf("\n");
  288. usleep(8*1000);
  289. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  290. printf("34 interrupt read returned %d, bytes: ", ret);
  291. print_bytes(buf, ret);
  292. printf("\n");
  293. usleep(8*1000);
  294. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  295. printf("35 interrupt read returned %d, bytes: ", ret);
  296. print_bytes(buf, ret);
  297. printf("\n");
  298. usleep(8*1000);
  299. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  300. printf("36 interrupt read returned %d, bytes: ", ret);
  301. print_bytes(buf, ret);
  302. printf("\n");
  303. usleep(56*1000);
  304. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  305. printf("37 interrupt read returned %d, bytes: ", ret);
  306. print_bytes(buf, ret);
  307. printf("\n");
  308. usleep(8*1000);
  309. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  310. printf("38 interrupt read returned %d, bytes: ", ret);
  311. print_bytes(buf, ret);
  312. printf("\n");
  313. usleep(8*1000);
  314. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  315. printf("39 interrupt read returned %d, bytes: ", ret);
  316. print_bytes(buf, ret);
  317. printf("\n");
  318. usleep(8*1000);
  319. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  320. printf("40 interrupt read returned %d, bytes: ", ret);
  321. print_bytes(buf, ret);
  322. printf("\n");
  323. usleep(8*1000);
  324. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  325. printf("41 interrupt read returned %d, bytes: ", ret);
  326. print_bytes(buf, ret);
  327. printf("\n");
  328. usleep(8*1000);
  329. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  330. printf("42 interrupt read returned %d, bytes: ", ret);
  331. print_bytes(buf, ret);
  332. printf("\n");
  333. usleep(8*1000);
  334. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  335. printf("43 interrupt read returned %d, bytes: ", ret);
  336. print_bytes(buf, ret);
  337. printf("\n");
  338. usleep(8*1000);
  339. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  340. printf("44 interrupt read returned %d, bytes: ", ret);
  341. print_bytes(buf, ret);
  342. printf("\n");
  343. usleep(2500*1000);
  344. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  345. printf("45 interrupt read returned %d, bytes: ", ret);
  346. print_bytes(buf, ret);
  347. printf("\n");
  348. usleep(8*1000);
  349. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  350. printf("46 interrupt read returned %d, bytes: ", ret);
  351. print_bytes(buf, ret);
  352. printf("\n");
  353. usleep(8*1000);
  354. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  355. printf("47 interrupt read returned %d, bytes: ", ret);
  356. print_bytes(buf, ret);
  357. printf("\n");
  358. usleep(8*1000);
  359. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  360. printf("48 interrupt read returned %d, bytes: ", ret);
  361. print_bytes(buf, ret);
  362. printf("\n");
  363. usleep(784*1000);
  364. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  365. printf("49 interrupt read returned %d, bytes: ", ret);
  366. print_bytes(buf, ret);
  367. printf("\n");
  368. usleep(8*1000);
  369. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  370. printf("50 interrupt read returned %d, bytes: ", ret);
  371. print_bytes(buf, ret);
  372. printf("\n");
  373. usleep(1369*1000);
  374. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  375. printf("51 interrupt read returned %d, bytes: ", ret);
  376. print_bytes(buf, ret);
  377. printf("\n");
  378. usleep(136*1000);
  379. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  380. printf("52 interrupt read returned %d, bytes: ", ret);
  381. print_bytes(buf, ret);
  382. printf("\n");
  383. usleep(1744*1000);
  384. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  385. printf("53 interrupt read returned %d, bytes: ", ret);
  386. print_bytes(buf, ret);
  387. printf("\n");
  388. usleep(2500*1000);
  389. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  390. printf("54 interrupt read returned %d, bytes: ", ret);
  391. print_bytes(buf, ret);
  392. printf("\n");
  393. usleep(136*1000);
  394. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  395. printf("55 interrupt read returned %d, bytes: ", ret);
  396. print_bytes(buf, ret);
  397. printf("\n");
  398. usleep(1345*1000);
  399. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  400. printf("56 interrupt read returned %d, bytes: ", ret);
  401. print_bytes(buf, ret);
  402. printf("\n");
  403. usleep(144*1000);
  404. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  405. printf("57 interrupt read returned %d, bytes: ", ret);
  406. print_bytes(buf, ret);
  407. printf("\n");
  408. usleep(696*1000);
  409. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  410. printf("58 interrupt read returned %d, bytes: ", ret);
  411. print_bytes(buf, ret);
  412. printf("\n");
  413. usleep(40*1000);
  414. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  415. printf("59 interrupt read returned %d, bytes: ", ret);
  416. print_bytes(buf, ret);
  417. printf("\n");
  418. usleep(104*1000);
  419. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  420. printf("60 interrupt read returned %d, bytes: ", ret);
  421. print_bytes(buf, ret);
  422. printf("\n");
  423. usleep(648*1000);
  424. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  425. printf("61 interrupt read returned %d, bytes: ", ret);
  426. print_bytes(buf, ret);
  427. printf("\n");
  428. usleep(32*1000);
  429. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  430. printf("62 interrupt read returned %d, bytes: ", ret);
  431. print_bytes(buf, ret);
  432. printf("\n");
  433. usleep(32*1000);
  434. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  435. printf("63 interrupt read returned %d, bytes: ", ret);
  436. print_bytes(buf, ret);
  437. printf("\n");
  438. usleep(32*1000);
  439. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  440. printf("64 interrupt read returned %d, bytes: ", ret);
  441. print_bytes(buf, ret);
  442. printf("\n");
  443. usleep(48*1000);
  444. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  445. printf("65 interrupt read returned %d, bytes: ", ret);
  446. print_bytes(buf, ret);
  447. printf("\n");
  448. usleep(56*1000);
  449. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  450. printf("66 interrupt read returned %d, bytes: ", ret);
  451. print_bytes(buf, ret);
  452. printf("\n");
  453. usleep(1153*1000);
  454. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  455. printf("67 interrupt read returned %d, bytes: ", ret);
  456. print_bytes(buf, ret);
  457. printf("\n");
  458. usleep(200*1000);
  459. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  460. printf("68 interrupt read returned %d, bytes: ", ret);
  461. print_bytes(buf, ret);
  462. printf("\n");
  463. usleep(680*1000);
  464. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  465. printf("69 interrupt read returned %d, bytes: ", ret);
  466. print_bytes(buf, ret);
  467. printf("\n");
  468. usleep(192*1000);
  469. ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000004, 1000);
  470. printf("70 interrupt read returned %d, bytes: ", ret);
  471. print_bytes(buf, ret);
  472. printf("\n");
  473. usleep(2500*1000);
  474. printf("71 abort pipe not implemented yet :(\n");
  475.     ret = usb_release_interface(devh, 0);
  476.     assert(ret == 0);
  477.     ret = usb_close(devh);
  478.     assert(ret == 0);
  479.     return 0;
  480. }

Raw Paste


Login or Register to edit or fork this paste. It's free.