libusb20.c (184610) | libusb20.c (185087) |
---|---|
1/* $FreeBSD: head/lib/libusb20/libusb20.c 184610 2008-11-04 02:31:03Z alfred $ */ | 1/* $FreeBSD: head/lib/libusb20/libusb20.c 185087 2008-11-19 08:56:35Z alfred $ */ |
2/*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. --- 535 unchanged lines hidden (view full) --- 545 546 xfer = pdev->pTransfer + x; 547 548 xfer->pdev = pdev; 549 xfer->trIndex = x; 550 xfer->callback = &dummy_callback; 551 } 552 | 2/*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. --- 535 unchanged lines hidden (view full) --- 545 546 xfer = pdev->pTransfer + x; 547 548 xfer->pdev = pdev; 549 xfer->trIndex = x; 550 xfer->callback = &dummy_callback; 551 } 552 |
553 /* set "nTransfer" early */ 554 pdev->nTransfer = nTransferMax; 555 |
|
553 error = (pdev->beMethods->open_device) (pdev, nTransferMax); 554 555 if (error) { 556 if (pdev->pTransfer != NULL) { 557 free(pdev->pTransfer); 558 pdev->pTransfer = NULL; 559 } 560 pdev->file = -1; 561 pdev->file_ctrl = -1; 562 pdev->nTransfer = 0; 563 } else { 564 pdev->is_opened = 1; | 556 error = (pdev->beMethods->open_device) (pdev, nTransferMax); 557 558 if (error) { 559 if (pdev->pTransfer != NULL) { 560 free(pdev->pTransfer); 561 pdev->pTransfer = NULL; 562 } 563 pdev->file = -1; 564 pdev->file_ctrl = -1; 565 pdev->nTransfer = 0; 566 } else { 567 pdev->is_opened = 1; |
565 pdev->nTransfer = nTransferMax; | |
566 } 567 return (error); 568} 569 570int 571libusb20_dev_release_interface(struct libusb20_device *pdev, uint8_t ifaceIndex) 572{ 573 int error; --- 68 unchanged lines hidden (view full) --- 642 643 error = (pdev->methods->do_request_sync) (pdev, 644 setup, data, pactlen, timeout, flags); 645 return (error); 646} 647 648int 649libusb20_dev_req_string_sync(struct libusb20_device *pdev, | 568 } 569 return (error); 570} 571 572int 573libusb20_dev_release_interface(struct libusb20_device *pdev, uint8_t ifaceIndex) 574{ 575 int error; --- 68 unchanged lines hidden (view full) --- 644 645 error = (pdev->methods->do_request_sync) (pdev, 646 setup, data, pactlen, timeout, flags); 647 return (error); 648} 649 650int 651libusb20_dev_req_string_sync(struct libusb20_device *pdev, |
650 uint8_t index, uint16_t langid, void *ptr, uint16_t len) | 652 uint8_t str_index, uint16_t langid, void *ptr, uint16_t len) |
651{ 652 struct LIBUSB20_CONTROL_SETUP_DECODED req; 653 int error; 654 655 if (len < 4) { 656 /* invalid length */ 657 return (LIBUSB20_ERROR_INVALID_PARAM); 658 } 659 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req); 660 661 /* 662 * We need to read the USB string in two steps else some USB 663 * devices will complain. 664 */ 665 req.bmRequestType = 666 LIBUSB20_REQUEST_TYPE_STANDARD | 667 LIBUSB20_RECIPIENT_DEVICE | 668 LIBUSB20_ENDPOINT_IN; 669 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR; | 653{ 654 struct LIBUSB20_CONTROL_SETUP_DECODED req; 655 int error; 656 657 if (len < 4) { 658 /* invalid length */ 659 return (LIBUSB20_ERROR_INVALID_PARAM); 660 } 661 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req); 662 663 /* 664 * We need to read the USB string in two steps else some USB 665 * devices will complain. 666 */ 667 req.bmRequestType = 668 LIBUSB20_REQUEST_TYPE_STANDARD | 669 LIBUSB20_RECIPIENT_DEVICE | 670 LIBUSB20_ENDPOINT_IN; 671 req.bRequest = LIBUSB20_REQUEST_GET_DESCRIPTOR; |
670 req.wValue = (LIBUSB20_DT_STRING << 8) | index; | 672 req.wValue = (LIBUSB20_DT_STRING << 8) | str_index; |
671 req.wIndex = langid; 672 req.wLength = 4; /* bytes */ 673 674 error = libusb20_dev_request_sync(pdev, &req, 675 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK); 676 if (error) { 677 return (error); 678 } --- 11 unchanged lines hidden (view full) --- 690 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) { 691 return (LIBUSB20_ERROR_OTHER); 692 } 693 return (0); /* success */ 694} 695 696int 697libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev, | 673 req.wIndex = langid; 674 req.wLength = 4; /* bytes */ 675 676 error = libusb20_dev_request_sync(pdev, &req, 677 ptr, NULL, 1000, LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK); 678 if (error) { 679 return (error); 680 } --- 11 unchanged lines hidden (view full) --- 692 if (((uint8_t *)ptr)[1] != LIBUSB20_DT_STRING) { 693 return (LIBUSB20_ERROR_OTHER); 694 } 695 return (0); /* success */ 696} 697 698int 699libusb20_dev_req_string_simple_sync(struct libusb20_device *pdev, |
698 uint8_t index, void *ptr, uint16_t len) | 700 uint8_t str_index, void *ptr, uint16_t len) |
699{ 700 char *buf; 701 int error; 702 uint16_t langid; 703 uint16_t n; 704 uint16_t i; 705 uint16_t c; 706 uint8_t temp[255]; 707 uint8_t swap; 708 709 /* the following code derives from the FreeBSD USB kernel */ 710 711 if ((len < 1) || (ptr == NULL)) { 712 /* too short buffer */ 713 return (LIBUSB20_ERROR_INVALID_PARAM); 714 } | 701{ 702 char *buf; 703 int error; 704 uint16_t langid; 705 uint16_t n; 706 uint16_t i; 707 uint16_t c; 708 uint8_t temp[255]; 709 uint8_t swap; 710 711 /* the following code derives from the FreeBSD USB kernel */ 712 713 if ((len < 1) || (ptr == NULL)) { 714 /* too short buffer */ 715 return (LIBUSB20_ERROR_INVALID_PARAM); 716 } |
715 /* 716 * Make sure that there is sensible contents in the buffer in case 717 * of an error: 718 */ 719 *(uint8_t *)ptr = 0; 720 | |
721 error = libusb20_dev_req_string_sync(pdev, 722 0, 0, temp, sizeof(temp)); | 717 error = libusb20_dev_req_string_sync(pdev, 718 0, 0, temp, sizeof(temp)); |
723 if (error < 0) | 719 if (error < 0) { 720 *(uint8_t *)ptr = 0; /* zero terminate */ |
724 return (error); | 721 return (error); |
725 | 722 } |
726 langid = temp[2] | (temp[3] << 8); 727 | 723 langid = temp[2] | (temp[3] << 8); 724 |
728 error = libusb20_dev_req_string_sync(pdev, index, | 725 error = libusb20_dev_req_string_sync(pdev, str_index, |
729 langid, temp, sizeof(temp)); | 726 langid, temp, sizeof(temp)); |
730 if (error < 0) | 727 if (error < 0) { 728 *(uint8_t *)ptr = 0; /* zero terminate */ |
731 return (error); | 729 return (error); |
732 | 730 } |
733 if (temp[0] < 2) { 734 /* string length is too short */ | 731 if (temp[0] < 2) { 732 /* string length is too short */ |
733 *(uint8_t *)ptr = 0; /* zero terminate */ |
|
735 return (LIBUSB20_ERROR_OTHER); 736 } 737 /* reserve one byte for terminating zero */ 738 len--; 739 740 /* find maximum length */ 741 n = (temp[0] / 2) - 1; 742 if (n > len) { --- 14 unchanged lines hidden (view full) --- 757 /* Little Endian, default */ 758 *buf = c; 759 swap = 1; 760 } else if (((c & 0x00ff) == 0) && (swap & 2)) { 761 /* Big Endian */ 762 *buf = c >> 8; 763 swap = 2; 764 } else { | 734 return (LIBUSB20_ERROR_OTHER); 735 } 736 /* reserve one byte for terminating zero */ 737 len--; 738 739 /* find maximum length */ 740 n = (temp[0] / 2) - 1; 741 if (n > len) { --- 14 unchanged lines hidden (view full) --- 756 /* Little Endian, default */ 757 *buf = c; 758 swap = 1; 759 } else if (((c & 0x00ff) == 0) && (swap & 2)) { 760 /* Big Endian */ 761 *buf = c >> 8; 762 swap = 2; 763 } else { |
765 *buf = '.'; | 764 /* skip invalid character */ 765 continue; |
766 } 767 /* 768 * Filter by default - we don't allow greater and less than 769 * signs because they might confuse the dmesg printouts! 770 */ 771 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) { | 766 } 767 /* 768 * Filter by default - we don't allow greater and less than 769 * signs because they might confuse the dmesg printouts! 770 */ 771 if ((*buf == '<') || (*buf == '>') || (!isprint(*buf))) { |
772 *buf = '.'; | 772 /* skip invalid character */ 773 continue; |
773 } 774 buf++; 775 } 776 *buf = 0; /* zero terminate string */ 777 778 return (0); 779} 780 --- 50 unchanged lines hidden (view full) --- 831 pdev->methods = &libusb20_dummy_methods; 832 return (pdev); 833} 834 835uint8_t 836libusb20_dev_get_config_index(struct libusb20_device *pdev) 837{ 838 int error; | 774 } 775 buf++; 776 } 777 *buf = 0; /* zero terminate string */ 778 779 return (0); 780} 781 --- 50 unchanged lines hidden (view full) --- 832 pdev->methods = &libusb20_dummy_methods; 833 return (pdev); 834} 835 836uint8_t 837libusb20_dev_get_config_index(struct libusb20_device *pdev) 838{ 839 int error; |
839 uint8_t index; | 840 uint8_t cfg_index; |
840 uint8_t do_close; 841 842 if (!pdev->is_opened) { 843 error = libusb20_dev_open(pdev, 0); 844 if (error == 0) { 845 do_close = 1; 846 } else { 847 do_close = 0; 848 } 849 } else { 850 do_close = 0; 851 } 852 | 841 uint8_t do_close; 842 843 if (!pdev->is_opened) { 844 error = libusb20_dev_open(pdev, 0); 845 if (error == 0) { 846 do_close = 1; 847 } else { 848 do_close = 0; 849 } 850 } else { 851 do_close = 0; 852 } 853 |
853 error = (pdev->methods->get_config_index) (pdev, &index); | 854 error = (pdev->methods->get_config_index) (pdev, &cfg_index); |
854 if (error) { | 855 if (error) { |
855 index = 0 - 1; /* current config index */ | 856 cfg_index = 0 - 1; /* current config index */ |
856 } 857 if (do_close) { 858 if (libusb20_dev_close(pdev)) { 859 /* ignore */ 860 } 861 } | 857 } 858 if (do_close) { 859 if (libusb20_dev_close(pdev)) { 860 /* ignore */ 861 } 862 } |
862 return (index); | 863 return (cfg_index); |
863} 864 865uint8_t 866libusb20_dev_get_mode(struct libusb20_device *pdev) 867{ 868 return (pdev->usb_mode); 869} 870 --- 11 unchanged lines hidden (view full) --- 882 883 error = (pdev->methods->process) (pdev); 884 return (error); 885} 886 887void 888libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout) 889{ | 864} 865 866uint8_t 867libusb20_dev_get_mode(struct libusb20_device *pdev) 868{ 869 return (pdev->usb_mode); 870} 871 --- 11 unchanged lines hidden (view full) --- 883 884 error = (pdev->methods->process) (pdev); 885 return (error); 886} 887 888void 889libusb20_dev_wait_process(struct libusb20_device *pdev, int timeout) 890{ |
890 struct pollfd pfd[2]; | 891 struct pollfd pfd[1]; |
891 892 if (!pdev->is_opened) { 893 return; 894 } 895 pfd[0].fd = pdev->file; 896 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 897 pfd[0].revents = 0; | 892 893 if (!pdev->is_opened) { 894 return; 895 } 896 pfd[0].fd = pdev->file; 897 pfd[0].events = (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 898 pfd[0].revents = 0; |
898 pfd[1].fd = 0; /* standard input */ 899 pfd[1].events = (POLLIN | POLLRDNORM); 900 pfd[1].revents = 0; | |
901 | 899 |
902 if (poll(pfd, 2, timeout)) { | 900 if (poll(pfd, 1, timeout)) { |
903 /* ignore any error */ 904 } 905 return; 906} 907 908void 909libusb20_dev_free(struct libusb20_device *pdev) 910{ --- 155 unchanged lines hidden (view full) --- 1066 mode = &a; 1067 return ((pbe->methods->bus_get_perm) (pbe, bus, mode)); 1068} 1069 1070/* USB backend operations */ 1071 1072int 1073libusb20_be_get_dev_quirk(struct libusb20_backend *pbe, | 901 /* ignore any error */ 902 } 903 return; 904} 905 906void 907libusb20_dev_free(struct libusb20_device *pdev) 908{ --- 155 unchanged lines hidden (view full) --- 1064 mode = &a; 1065 return ((pbe->methods->bus_get_perm) (pbe, bus, mode)); 1066} 1067 1068/* USB backend operations */ 1069 1070int 1071libusb20_be_get_dev_quirk(struct libusb20_backend *pbe, |
1074 uint16_t index, struct libusb20_quirk *pq) | 1072 uint16_t quirk_index, struct libusb20_quirk *pq) |
1075{ | 1073{ |
1076 return ((pbe->methods->root_get_dev_quirk) (pbe, index, pq)); | 1074 return ((pbe->methods->root_get_dev_quirk) (pbe, quirk_index, pq)); |
1077} 1078 1079int 1080libusb20_be_get_quirk_name(struct libusb20_backend *pbe, | 1075} 1076 1077int 1078libusb20_be_get_quirk_name(struct libusb20_backend *pbe, |
1081 uint16_t index, struct libusb20_quirk *pq) | 1079 uint16_t quirk_index, struct libusb20_quirk *pq) |
1082{ | 1080{ |
1083 return ((pbe->methods->root_get_quirk_name) (pbe, index, pq)); | 1081 return ((pbe->methods->root_get_quirk_name) (pbe, quirk_index, pq)); |
1084} 1085 1086int 1087libusb20_be_add_dev_quirk(struct libusb20_backend *pbe, 1088 struct libusb20_quirk *pq) 1089{ 1090 return ((pbe->methods->root_add_dev_quirk) (pbe, pq)); 1091} --- 154 unchanged lines hidden --- | 1082} 1083 1084int 1085libusb20_be_add_dev_quirk(struct libusb20_backend *pbe, 1086 struct libusb20_quirk *pq) 1087{ 1088 return ((pbe->methods->root_add_dev_quirk) (pbe, pq)); 1089} --- 154 unchanged lines hidden --- |