2 * Copyright 2017 Intel Corporation.
3 * The source code, information and material ("Material") contained herein is
4 * owned by Intel Corporation or its suppliers or licensors, and title to such
5 * Material remains with Intel Corporation or its suppliers or licensors.
6 * The Material contains proprietary information of Intel or its suppliers and
7 * licensors. The Material is protected by worldwide copyright laws and treaty
9 * No part of the Material may be used, copied, reproduced, modified, published,
10 * uploaded, posted, transmitted, distributed or disclosed in any way without
11 * Intel's prior express written permission. No license under any patent,
12 * copyright or other intellectual property rights in the Material is granted to
13 * or conferred upon you, either expressly, by implication, inducement, estoppel
15 * Any license under such intellectual property rights must be express and
16 * approved by Intel in writing.
23 #include <sys/types.h>
28 #include <sys/timeb.h>
31 #include <sys/ioctl.h>
40 #define USB_ENDPOINT_IN 0x81
41 #define USB_ENDPOINT_OUT 0x01
42 #define USB_TIMEOUT 10000
43 #define USB_MAX_PACKET_SIZE 1024 * 1024 * 10
48 static int usb_write(void *f, const void *data, size_t size)
52 if (ss > USB_MAX_PACKET_SIZE)
53 ss = USB_MAX_PACKET_SIZE;
54 if (libusb_bulk_transfer(f, USB_ENDPOINT_OUT, (unsigned char *) data, ss, &bt,
57 data = (char *) data + bt;
63 static int usb_read(void *f, void *data, size_t size)
67 if (ss > USB_MAX_PACKET_SIZE)
68 ss = USB_MAX_PACKET_SIZE;
69 if (libusb_bulk_transfer(f, USB_ENDPOINT_IN, data, ss, &bt, USB_TIMEOUT))
71 data = (char *) data + bt;
77 void *usblink_open(const char *path)
80 libusb_device_handle *h = NULL;
83 rc = usb_find_device(0, (char *) path, 0, (void **) &dev,
84 DEFAULT_OPEN_VID, DEFAULT_OPEN_PID);
88 rc = libusb_open(dev, &h);
90 libusb_unref_device(dev);
94 libusb_unref_device(dev);
95 rc = libusb_claim_interface(h, 0);
103 void usblink_close(void *f)
105 libusb_release_interface(f, 0);
109 void usblink_resetall()
111 libusb_device **devs;
113 struct libusb_device_descriptor desc;
114 libusb_device_handle *h;
116 int rc, iters = 0, cnt_bootrom = 0, cnt_runtime = 0, cnt_after = 0;
118 if ((rc = libusb_get_device_list(NULL, &devs)) < 0)
121 while ((dev = devs[i++]) != NULL) {
122 if (libusb_get_device_descriptor(dev, &desc) < 0)
124 if (desc.idVendor == DEFAULT_VID &&
125 desc.idProduct == DEFAULT_PID)
127 // If Runtime device found, reset it
128 if (desc.idVendor == DEFAULT_OPEN_VID &&
129 desc.idProduct == DEFAULT_OPEN_PID) {
131 rc = libusb_open(dev, &h);
134 rc = libusb_claim_interface(h, 0);
139 PRINT_DEBUG(stderr, "Found stale device, resetting\n");
140 usblink_resetmyriad(h);
144 // If some devices needed reset
147 // Wait until all devices re-enumerate, or timeout occurs
148 while((cnt_after < cnt_bootrom + cnt_runtime) && (iters < ITERATIONS)){
149 usleep(SLEEP_MS*1000);
151 if ((rc = libusb_get_device_list(NULL, &devs)) < 0)
154 while ((dev = devs[i++]) != NULL) {
155 if ((rc = libusb_get_device_descriptor(dev, &desc)) < 0)
157 if (desc.idVendor == DEFAULT_VID &&
158 desc.idProduct == DEFAULT_PID)
164 libusb_free_device_list(devs, 1);
167 int usblink_setdata(void *f, const char *name, const void *data,
168 unsigned int length, int host_ready)
171 memset(&header, 0, sizeof(header));
172 header.cmd = USB_LINK_HOST_SET_DATA;
173 header.hostready = host_ready;
174 strcpy(header.name, name);
175 header.dataLength = length;
176 if (usb_write(f, &header, sizeof(header)))
179 unsigned int operation_permit = 0xFFFF;
180 if (usb_read(f, &operation_permit, sizeof(operation_permit)))
183 if (operation_permit != 0xABCD)
185 int rc = usb_write(f, data, length);
189 int usblink_getdata(void *f, const char *name, void *data, unsigned int length,
190 unsigned int offset, int host_ready)
193 memset(&header, 0, sizeof(header));
194 header.cmd = USB_LINK_HOST_GET_DATA;
195 header.hostready = host_ready;
196 strcpy(header.name, name);
197 header.dataLength = length;
198 header.offset = offset;
199 if (usb_write(f, &header, sizeof(header)))
202 unsigned int operation_permit = 0xFFFF;
203 if (usb_read(f, &operation_permit, sizeof(operation_permit)))
206 if (operation_permit != 0xABCD)
208 return usb_read(f, data, length);
211 int usblink_resetmyriad(void *f)
214 memset(&header, 0, sizeof(header));
215 header.cmd = USB_LINK_RESET_REQUEST;
216 if (usb_write(f, &header, sizeof(header)))
221 int usblink_getmyriadstatus(void *f, myriadStatus_t* myriad_state)
224 memset(&header, 0, sizeof(header));
225 header.cmd = USB_LINK_GET_MYRIAD_STATUS;
226 if (usb_write(f, &header, sizeof(header)))
228 return usb_read(f, myriad_state, sizeof(*myriad_state));