[e017851] | 1 | /* CrO2 datassette emulator
|
---|
| 2 | * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
|
---|
| 3 | *
|
---|
| 4 | * Distributed under the terms of the MIT licence.
|
---|
| 5 | *
|
---|
| 6 | * Handles device communication through libusb
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 | #include "device_libusb.h"
|
---|
| 10 | #include "k5.h"
|
---|
| 11 |
|
---|
| 12 | #include <typeinfo>
|
---|
| 13 |
|
---|
| 14 | bool Device::initOnce = false;
|
---|
| 15 | Device* Device::instance = NULL;
|
---|
| 16 |
|
---|
| 17 | const uint32_t Device::vid = 0x16C0;
|
---|
| 18 | const uint32_t Device::pid = 0x05DC;
|
---|
| 19 | const char* Device::vendor = "pulkomandy.ath.cx";
|
---|
| 20 | const char* Device::product = "CrO2";
|
---|
| 21 |
|
---|
| 22 | /* These are the vendor specific commands implemented by our USB device */
|
---|
| 23 | #define PSCMD_CONFIG 0
|
---|
| 24 | #define PSCMD_GET 1
|
---|
| 25 | #define PSCMD_PUT 2
|
---|
| 26 | #define PSCMD_STATUS 3
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | // Gets the device instance. Throws an error message if something bad happens.
|
---|
| 30 | Device& Device::getDevice() throw(const char*)
|
---|
| 31 | {
|
---|
| 32 | if (instance == NULL)
|
---|
| 33 | instance = new LibUsbDevice();
|
---|
| 34 |
|
---|
| 35 | return *instance;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | /* USB device lookup by VID and PID, then Vendor and Product strings, as we use
|
---|
| 40 | * V-USB shared ID. */
|
---|
| 41 | static int usbGetStringAscii(usb_dev_handle *dev, int index, int langid,
|
---|
| 42 | char *buf, int buflen)
|
---|
| 43 | {
|
---|
| 44 | char buffer[256];
|
---|
| 45 | int rval, i;
|
---|
| 46 |
|
---|
| 47 | if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
|
---|
| 48 | (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0)
|
---|
| 49 | return rval;
|
---|
| 50 | if(buffer[1] != USB_DT_STRING)
|
---|
| 51 | return 0;
|
---|
| 52 | if((unsigned char)buffer[0] < rval)
|
---|
| 53 | rval = (unsigned char)buffer[0];
|
---|
| 54 | rval /= 2;
|
---|
| 55 | /* lossy conversion to ISO Latin1 */
|
---|
| 56 | for(i=1;i<rval;i++){
|
---|
| 57 | if(i > buflen) /* destination buffer overflow */
|
---|
| 58 | break;
|
---|
| 59 | buf[i-1] = buffer[2 * i];
|
---|
| 60 | if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
|
---|
| 61 | buf[i-1] = '?';
|
---|
| 62 | }
|
---|
| 63 | buf[i-1] = 0;
|
---|
| 64 | return i-1;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | LibUsbDevice::LibUsbDevice() throw(const char*)
|
---|
| 68 | {
|
---|
| 69 | handle = NULL;
|
---|
| 70 |
|
---|
| 71 | if (!initOnce)
|
---|
| 72 | {
|
---|
| 73 | initOnce = true;
|
---|
| 74 | usb_init();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | usb_find_busses();
|
---|
| 78 | usb_find_devices();
|
---|
| 79 |
|
---|
| 80 | for(struct usb_bus* bus=usb_get_busses(); bus; bus=bus->next)
|
---|
| 81 | {
|
---|
| 82 | for(struct usb_device* dev=bus->devices; dev; dev=dev->next)
|
---|
| 83 | {
|
---|
| 84 | if(dev->descriptor.idVendor != vid || dev->descriptor.idProduct != pid)
|
---|
| 85 | continue;
|
---|
| 86 |
|
---|
| 87 | // Found device with correct VID and PID. Now try to match the
|
---|
| 88 | // vendor and product strings
|
---|
| 89 | char string[256];
|
---|
| 90 | int len;
|
---|
| 91 | handle = usb_open(dev);
|
---|
| 92 | /* we need to open the device in order to query strings */
|
---|
| 93 | if(!handle){
|
---|
| 94 | continue;
|
---|
| 95 | }
|
---|
| 96 | /* now check whether the names match: */
|
---|
| 97 | len = usbGetStringAscii(handle, dev->descriptor.iManufacturer,
|
---|
| 98 | 0x0409, string, sizeof(string));
|
---|
| 99 | if(len >= 0)
|
---|
| 100 | {
|
---|
| 101 | if(strcmp(string, vendor) == 0){
|
---|
| 102 | len = usbGetStringAscii(handle, dev->descriptor.iProduct,
|
---|
| 103 | 0x0409, string, sizeof(string));
|
---|
| 104 | if(len >= 0){
|
---|
| 105 | if(strcmp(string, product) == 0)
|
---|
| 106 | break;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | usb_close(handle);
|
---|
| 111 | handle = NULL;
|
---|
| 112 | }
|
---|
| 113 | if(handle)
|
---|
| 114 | break;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | // At this point, either we have found a device and handle is pointing to it,
|
---|
| 118 | // or we failed and handle is NULL.
|
---|
| 119 | if (!handle)
|
---|
| 120 | throw "Device not found. Is the USB cable plugged correctly?";
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | LibUsbDevice::~LibUsbDevice()
|
---|
| 125 | {
|
---|
| 126 | usb_close(handle);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | int LibUsbDevice::read(uint8_t* buffer, size_t max)
|
---|
| 131 | {
|
---|
| 132 | return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
|
---|
| 133 | PSCMD_GET, 0,0, (char*)buffer, max, 5000);
|
---|
| 134 |
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | int LibUsbDevice::write(const uint8_t* buffer, size_t size, int blktype)
|
---|
| 139 | {
|
---|
| 140 | int rqtype = (size == 0) ? USB_ENDPOINT_IN:USB_ENDPOINT_OUT;
|
---|
| 141 | return usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | rqtype,
|
---|
| 142 | PSCMD_PUT, blktype,0 /*checksum*/, (char*)buffer, size, 5000);
|
---|
| 143 |
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | uint8_t LibUsbDevice::getStatus()
|
---|
| 148 | {
|
---|
| 149 | uint8_t status;
|
---|
| 150 | usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
|
---|
| 151 | PSCMD_STATUS, 0,0, (char*)&status, 1, 5000);
|
---|
| 152 | // TODO handle errors (return value)
|
---|
| 153 | return status;
|
---|
| 154 | }
|
---|