1 | /* CrO2 datassette emulator
|
---|
2 | * Copyright 2013, Adrien Destugues <pulkomandy@pulkomandy.tk>
|
---|
3 | *
|
---|
4 | * Distributed under the terms of the MIT licence.
|
---|
5 | *
|
---|
6 | * Handles device communication through libusb
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include "device_bekit.h"
|
---|
10 | #include "k5.h"
|
---|
11 |
|
---|
12 | #include <typeinfo>
|
---|
13 | #include <USBKit.h>
|
---|
14 |
|
---|
15 |
|
---|
16 | // Gets the device instance. Throws an error message if something bad happens.
|
---|
17 | Device& Device::getDevice() throw(const char*)
|
---|
18 | {
|
---|
19 | if (instance == NULL) {
|
---|
20 | instance = new HaikuDevice();
|
---|
21 | }
|
---|
22 |
|
---|
23 | return *instance;
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | HaikuDevice::HaikuDevice() throw(const char*)
|
---|
28 | {
|
---|
29 | // At this point, either we have found a device and handle is pointing to it,
|
---|
30 | // or we failed and handle is NULL.
|
---|
31 | if (!handle)
|
---|
32 | throw "Device not found. Is the USB cable plugged correctly?";
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | HaikuDevice::~HaikuDevice()
|
---|
37 | {
|
---|
38 | delete handle;
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | int HaikuDevice::read(uint8_t* buffer, size_t max)
|
---|
43 | {
|
---|
44 | return handle->ControlTransfer(USB_REQTYPE_VENDOR | USB_REQTYPE_ENDPOINT_IN,
|
---|
45 | PSCMD_GET, 0,0, max, (char*)buffer);
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | int HaikuDevice::write(const uint8_t* buffer, size_t size, int blktype)
|
---|
51 | {
|
---|
52 | int rqtype = (size == 0) ? USB_REQTYPE_ENDPOINT_IN:USB_REQTYPE_ENDPOINT_OUT;
|
---|
53 | return handle->ControlTransfer(USB_REQTYPE_VENDOR | rqtype,
|
---|
54 | PSCMD_PUT, blktype,0 /*checksum*/, size, (char*)buffer);
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | uint8_t HaikuDevice::getStatus()
|
---|
60 | {
|
---|
61 | uint8_t status;
|
---|
62 | handle->ControlTransfer(USB_REQTYPE_VENDOR | USB_REQTYPE_ENDPOINT_IN,
|
---|
63 | PSCMD_STATUS, 0,0, 1, (char*)&status);
|
---|
64 | // TODO handle errors (return value)
|
---|
65 | return status;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | status_t HaikuDevice::DeviceAdded(BUSBDevice* device)
|
---|
70 | {
|
---|
71 | if (handle != NULL) {
|
---|
72 | // We already have a device !
|
---|
73 | return B_ERROR;
|
---|
74 | }
|
---|
75 |
|
---|
76 | // Check if the device matches what we can handle
|
---|
77 |
|
---|
78 | if (device->VendorID() != vid && device->ProductID() != pid)
|
---|
79 | return B_ERROR;
|
---|
80 |
|
---|
81 | // Since we use V-USB shared vid/pid pair, we also have to check the
|
---|
82 | // manufacturer and product strings to actually identify the device amongst
|
---|
83 | // other users of the pair.
|
---|
84 | if (strcmp(device->ManufacturerString(), vendor) != 0)
|
---|
85 | return B_ERROR;
|
---|
86 |
|
---|
87 | if (strcmp(device->ProductString(), product) != 0)
|
---|
88 | return B_ERROR;
|
---|
89 |
|
---|
90 | // Ok, we have found our device !
|
---|
91 | handle = device;
|
---|
92 | return B_OK;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | void HaikuDevice::DeviceRemoved(BUSBDevice* device)
|
---|
97 | {
|
---|
98 | // This is only called for devices we accepted in DeviceAdded. We accept
|
---|
99 | // only one, so we can safely remove it.
|
---|
100 | handle = NULL;
|
---|
101 | }
|
---|
102 |
|
---|