#include "aiousb.h" #include //memcpy #include //this should be for debugging only void aiousb_close_device(int device_index) { usb_release_interface(device_array[device_index].hdev, 0); usb_close(device_array[device_index].hdev); device_array[device_index].hdev = NULL; } int aiousb_ctr_read(int device_index, int block_index, int ctr_index, __u16 *read_value) { int ctr_blocks; int value; if (device_array[device_index].hdev == NULL) return -1; if (device_array[device_index].ctr_blocks <= block_index) return -2; value = (ctr_index << 8) | block_index; return usb_control_msg(device_array[device_index].hdev, 0xC0, 0x20, value, 0, (char *) read_value, 2, 5000); } int aiousb_ctr_read_mode_load(int device_index, int block_index, int ctr_index, int mode, int load_value, __u16 *read_value) { int ctr_blocks; int value; int status; char read[2]; if (device_array[device_index].hdev == NULL) return -1; aiousb_query_device_info(device_index, NULL, &ctr_blocks); if (ctr_blocks <= block_index) return -2; value = (ctr_index << 6) | 0x30 | mode << 1; value = value << 8; value |= block_index; status = usb_control_msg(device_array[device_index].hdev, 0x40, 0x23, value, load_value, read, 2, 1000); if (read_value != NULL) memcpy(read_value, read, 2); return status; } int aiousb_ctr_mode(int device_index, int block_index, int ctr_index, int mode) { int ctr_blocks; int value; if (device_array[device_index].hdev == NULL) return -1; aiousb_query_device_info(device_index, NULL, &ctr_blocks); if (ctr_blocks <= block_index) return -2; value = (ctr_index << 6) | 0x30 | mode << 1; value = value << 8; value |= block_index; return usb_control_msg(device_array[device_index].hdev, 0x40, 0x21, value, 0, NULL, 0, 1000); } int aiousb_dio_read(int device_index, void *data) { int bytes; if (device_array[device_index].hdev == NULL) return - 1; aiousb_query_device_info(device_index, &bytes, NULL); if (bytes == 0) return -2; return usb_control_msg(device_array[device_index].hdev, 0xC0, 0x11, 0, 0, (char *)data, 0x004, 5000); } int aiousb_dio_write(int device_index, void *data) { int bytes; if (device_array[device_index].hdev == NULL) return -1; aiousb_query_device_info(device_index, &bytes, NULL); if (bytes == 0) return -2; return usb_control_msg(device_array[device_index].hdev, 0x40, 0x10, 0, 0, (char *)data, 0x004, 5000); } int aiousb_dio_configure(int device_index, int tristate, unsigned char config_byte, void *data) { int bytes; unsigned char toBoard[6]; unsigned char *pData; pData = (unsigned char *)data; toBoard[0] = pData[0]; toBoard[1] = pData[1]; toBoard[2] = pData[2]; toBoard[3] = pData[3]; toBoard[4] = config_byte; toBoard[5] = 0; if (device_array[device_index].hdev == NULL) return -1; aiousb_query_device_info(device_index, &bytes, NULL); if (bytes == 0) return -2; return usb_control_msg(device_array[device_index].hdev, 0x40, 0x12, ((tristate) ? 1 : 0), 0, (char *)toBoard, 0x6, 1000); } int aiousb_query_device_info (int device_index, int *dio_bytes, int *counter_blocks) { int bytes, counters; switch (device_array[device_index].product_id) { case 0: return -1; break; case USB_DIO_32_ID_DEV: bytes = 4; counters = 3; break; case USB_DA12_8A_REVA_DEV: case USB_DA12_8A_DEV: bytes = 0; counters = 0; break; case USB_IIRO_16_DEV: bytes = 4; counters = 0; break; case USB_CTR_15_DEV: bytes = 0; counters = 5; break; case USB_IIRO4_2SM_DEV: bytes = 2; counters = 0; break; default: return -1; break; } if (dio_bytes != NULL) *dio_bytes = bytes; if (counter_blocks != NULL) *counter_blocks = counters; return 0; } int aiousb_get_devices() { int num_found = 0; int count; struct usb_bus *busses, *bus; struct usb_device *dev; for (count = 0; count < MAX_USB_DEVICES; count++) { device_array[count].hdev = NULL; device_array[count].product_id = 0; device_array[count].pdev = NULL; } usb_init(); usb_find_busses(); usb_find_devices(); busses = usb_get_busses(); for (bus = busses; bus; bus = bus -> next) for (dev = bus -> devices; dev; dev= dev -> next) { if (dev -> descriptor.idVendor == ACCES_VENDOR_ID) { //switch on the product ID. This way we ignore any devices that don't //have their firmware uploaded yet. switch (dev -> descriptor.idProduct) { case USB_DIO_32_ID_DEV: case USB_DA12_8A_REVA_DEV: case USB_DA12_8A_DEV: case USB_IIRO_16_DEV: case USB_CTR_15_DEV: case USB_IIRO4_2SM_DEV: device_array[num_found].product_id = dev->descriptor.idProduct; device_array[num_found].pdev = dev; aiousb_query_device_info(num_found, &(device_array[num_found].dio_bytes), &(device_array[num_found].ctr_blocks)); num_found++; break; }; } } return num_found; } int aiousb_open_device(int device_index) { int status; if (device_array[device_index].product_id == 0) return -3; device_array[device_index].hdev = usb_open(device_array[device_index].pdev); if (device_array[device_index].hdev == NULL) return -1; return 0; }