?? ozerocdoff.c
字號:
fprintf(outlog, "Found attached driver: %s\n", buffer); } ret = usb_detach_kernel_driver_np(usb_hd, 0); if (ret != 0) { if(!quiet) fprintf(errlog, "ERROR: unsuccessful kernel driver detaching\n"); (void)usb_close(usb_hd); exit(5); } } else { /* hopefully no default kernel driver yet running, send only a warning */ if(!quiet) fprintf(errlog, "WARNING: No kernel driver attached found\n"); } ret = usb_claim_interface(usb_hd, 0); if (ret != 0) { if(!quiet) fprintf(errlog, "ERROR: could not claim interface\n"); (void)usb_close(usb_hd); exit(6); } /* findout the Message and Response Endpoint */ int MessageEndpoint=search_message_endp(usb_dev); int ResponseEndpoint=search_response_endp(usb_dev); if (MessageEndpoint == 0) { if(!quiet) fprintf(errlog, "ERROR: could not detremine Endpoint addresses\n"); (void)usb_close(usb_hd); exit(6); } if (debug) { fprintf(outlog,"MessageEndpoint: 0x%x\n", MessageEndpoint); fprintf(outlog,"ResponseEndpoint: 0x%x \n", ResponseEndpoint); } /* preparation to send rezero string */ (void)usb_clear_halt(usb_hd, MessageEndpoint); //ResponseEndpoint ret = usb_set_altinterface(usb_hd, 0); if (ret != 0) { if (warno) { if(!quiet) fprintf(errlog, "WARNING: unsuccessful set alternative interface\n"); } else { if(!quiet) fprintf(errlog, "ERROR: unsuccessful set alternative interface\n"); (void)usb_release_interface(usb_hd, 0); (void)usb_close(usb_hd); exit(7); } } /* usb mass storage setting time (needed by some WWAN modems) */ sleep(1); if (!test) { /* send rezero string */ ret = usb_bulk_write(usb_hd, MessageEndpoint, (char *)MessageContent, sizeof(MessageContent), 1000); if (ret < 0) { if (warno) { if(!quiet) fprintf(errlog, "WARNING: unsuccessful write rezero string\n"); } else { if(!quiet) fprintf(errlog, "ERROR: unsuccessful write rezero string\n"); (void)usb_release_interface(usb_hd, 0); (void)usb_close(usb_hd); exit(8); } } else { if (debug) fprintf(outlog, "Have successfully send ZERO-CD disabling command\n"); } /* needed by some WWAN modems) */ if (ResponseEndpoint != 0) { ret = usb_bulk_read(usb_hd, ResponseEndpoint, buffer, BUFFER_SIZE, 1000); if (ret < 0 ) { if(!quiet) fprintf(errlog, "WARNING: unsuccessful read response message\n"); } } } /* close device */ ret = usb_close(usb_hd); if (ret < 0) { if (warno) { if(!quiet) fprintf(errlog, "WARNING: closeing failed\n"); } else { if(!quiet) fprintf(errlog, "ERROR: closeing failed\n"); (void)usb_release_interface(usb_hd, 0); exit(9); } } /* search again to check result. Note that still the search is needed by some WWAN modems! */ for (count = 5; (count>0) && (usb_dev != NULL); count--) { sleep(1); (void)usb_find_devices(); usb_dev = search_devices(TARGET_VENDOR, TargetProduct, ""); } if (usb_dev != NULL) { if (warno) { if(!quiet) fprintf(errlog, "WARNING: Zero-CD device still found\n"); } else { if(!quiet) fprintf(errlog, "ERROR: Zero-CD device still found\n"); (void)usb_release_interface(usb_hd, 0); exit(10); } } else { if (debug) fprintf(outlog, "Checked successfully ZERO-CD disabled\n"); else if(!quiet) fprintf(outlog, "Successfully ZERO-CD disabled\n"); } /* relese device */ (void)usb_release_interface(usb_hd, 0); return 0;}/** * release_usb_device * need for the signal handler to ensure proper usb port close and release * * input param : just a dumy paramter * return : nothting */void release_usb_device(int param) { (void)usb_release_interface(usb_hd, 0); (void)usb_close(usb_hd);}/** * strrcut * string help function, which cuts the string and returns the * right boundary last 'n' characters from the given string 's' or * an empty string, if the given string length is shorter than 'n' * * input s : const string * input n : number of charaacters, which should be left on the * right side of the given string * return : the result (shorten) strinf or empty string */const char *strrcut(const char *s, int n) { if (strlen(s)>=n) { return &(s[strlen(s)-n]); } else { return ""; }}/** * strrcmp * string help function, which is used to compare just the right * part of the given two strings. All other part is just cut away or * not be used for the comparision * * input s1 : const 1st string * input s2 : const 2nd string * return : the strcmp return value of the shorted given strings */int strrcmp(const char *s1, const char *s2) { if (strlen(s1)<=strlen(s2)) { return strcmp(s1,strrcut(s2,strlen(s1))); } else { return strcmp(strrcut(s1,strlen(s2)),s2); }}/** search_devices * search function of the vendor and device id. Optional the name can * be used to compare the right part of found string of the usblib. * * input vendor : USB vendor id * input product : USB product id * input name : optional right part of the searched filename or just an empty string * return : the found usb device structure, which matches the search */struct usb_device* search_devices(int vendor, int product, const char * name) { struct usb_bus *bus; for (bus = usb_get_busses(); bus; bus = bus->next) { struct usb_device *dev; for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) { /* check number of Endpoints, class, subclass and protocol */ if (debug) { fprintf(outlog, "Endpoints: %d (2)\n", dev->config[0].interface[0].altsetting[0].bNumEndpoints); fprintf(outlog, "Class: 0x%x (0x08)\n", dev->config[0].interface[0].altsetting[0].bInterfaceClass); fprintf(outlog, "SubClass: 0x%x (0x06)\n", dev->config[0].interface[0].altsetting[0].bInterfaceSubClass); fprintf(outlog, "Protocol: 0x%x (0x50)\n", dev->config[0].interface[0].altsetting[0].bInterfaceProtocol); } if ( (dev->config[0].interface[0].altsetting[0].bNumEndpoints == 2) && (dev->config[0].interface[0].altsetting[0].bInterfaceClass == 0x08) && (dev->config[0].interface[0].altsetting[0].bInterfaceSubClass == 0x06) && (dev->config[0].interface[0].altsetting[0].bInterfaceProtocol == 0x50) ) { if (debug) { fprintf(outlog, "Found file name: '%s'\n", dev->filename); fprintf(outlog, "Required last chars of file name: '%s'\n", name); } if (strrcmp(name, dev->filename) == 0) { if (debug) fprintf(outlog, "UMS interface found\n"); return dev; } else { if (debug) fprintf(outlog, "UMS interface found, but its wrong\n"); } } } } } return NULL;}/** * search_message_endp * looks inside the USB descriptor stuff for the needed endpoint address. * It searches explicit for an bulk output interface and returns the address * or even zero * * input dev : usb device structure * return : endpoint address of 0, if it is no bulk output endpoint */int search_message_endp(struct usb_device *dev) { if ( ((dev->config[0].interface[0].altsetting[0].endpoint[0].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) &&((dev->config[0].interface[0].altsetting[0].endpoint[0].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)) { return dev->config[0].interface[0].altsetting[0].endpoint[0].bEndpointAddress; } else if ( ((dev->config[0].interface[0].altsetting[0].endpoint[1].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) &&((dev->config[0].interface[0].altsetting[0].endpoint[1].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)) { return dev->config[0].interface[0].altsetting[0].endpoint[1].bEndpointAddress; } return 0;}/** * search_response_endp * looks inside the USB descriptor stuff for the needed endpoint address. * It searches explicit for an bulk input interface and returns the address * or even zero * * input dev : usb device structure * return : endpoint address of 0, if it is no bulk input endpoint */int search_response_endp(struct usb_device *dev) { if ( ((dev->config[0].interface[0].altsetting[0].endpoint[0].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) &&((dev->config[0].interface[0].altsetting[0].endpoint[0].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)) { return dev->config[0].interface[0].altsetting[0].endpoint[0].bEndpointAddress; } else if ( ((dev->config[0].interface[0].altsetting[0].endpoint[1].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) &&((dev->config[0].interface[0].altsetting[0].endpoint[1].bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)) { return dev->config[0].interface[0].altsetting[0].endpoint[1].bEndpointAddress; } return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -