source: thomson/elec/CrO2/software/device_bekit.cpp@ 6bd7144

main
Last change on this file since 6bd7144 was 6bd7144, checked in by Adrien Destugues <pulkomandy@…>, 11 years ago

More work on Haiku port

git-svn-id: svn://localhost/thomson@29 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

  • Property mode set to 100644
File size: 3.0 KB
Line 
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
15class DeviceScanner: public BUSBRoster
16{
17 public:
18 DeviceScanner(uint32_t vid, uint32_t pid, const char* vendor, const char* product);
19
20 // BUSBRoster
21 status_t DeviceAdded(BUSBDevice* device);
22 void DeviceRemoved(BUSBDevice* device);
23
24 BUSBDevice* handle;
25 private:
26 uint32_t vid, pid;
27 const char* vendor, *product;
28};
29
30// Gets the device instance. Throws an error message if something bad happens.
31Device& Device::getDevice() throw(const char*)
32{
33 if (instance == NULL) {
34 DeviceScanner* scanner = new DeviceScanner(vid, pid, vendor, product);
35
36 while(scanner->handle == NULL); // FIXME don't hog CPU, and timeout
37 //throw "Device not found. Is the USB cable plugged correctly?";
38
39 // We have our device, don't need the roster anymore.
40 delete scanner;
41
42 instance = new HaikuDevice(scanner->handle);
43 }
44
45 return *instance;
46}
47
48
49HaikuDevice::HaikuDevice(BUSBDevice* handle) throw(const char*)
50{
51 this->handle = handle;
52}
53
54
55HaikuDevice::~HaikuDevice()
56{
57 delete handle;
58}
59
60
61int HaikuDevice::read(uint8_t* buffer, size_t max)
62{
63 return handle->ControlTransfer(USB_REQTYPE_VENDOR | USB_REQTYPE_ENDPOINT_IN,
64 PSCMD_GET, 0,0, max, (char*)buffer);
65
66}
67
68
69int HaikuDevice::write(const uint8_t* buffer, size_t size, int blktype)
70{
71 int rqtype = (size == 0) ? USB_REQTYPE_ENDPOINT_IN:USB_REQTYPE_ENDPOINT_OUT;
72 return handle->ControlTransfer(USB_REQTYPE_VENDOR | rqtype,
73 PSCMD_PUT, blktype,0 /*checksum*/, size, (char*)buffer);
74
75}
76
77
78uint8_t HaikuDevice::getStatus()
79{
80 uint8_t status;
81 handle->ControlTransfer(USB_REQTYPE_VENDOR | USB_REQTYPE_ENDPOINT_IN,
82 PSCMD_STATUS, 0,0, 1, (char*)&status);
83 // TODO handle errors (return value)
84 return status;
85}
86
87
88DeviceScanner::DeviceScanner(uint32_t vid, uint32_t pid, const char* vendor, const char* product)
89 : vid(vid)
90 , pid(pid)
91 , vendor(vendor)
92 , product(product)
93{
94}
95
96
97status_t DeviceScanner::DeviceAdded(BUSBDevice* device)
98{
99 if (handle != NULL) {
100 // We already have a device !
101 return B_ERROR;
102 }
103
104 // Check if the device matches what we can handle
105
106 if (device->VendorID() != vid && device->ProductID() != pid)
107 return B_ERROR;
108
109 // Since we use V-USB shared vid/pid pair, we also have to check the
110 // manufacturer and product strings to actually identify the device amongst
111 // other users of the pair.
112 if (strcmp(device->ManufacturerString(), vendor) != 0)
113 return B_ERROR;
114
115 if (strcmp(device->ProductString(), product) != 0)
116 return B_ERROR;
117
118 // Ok, we have found our device !
119 handle = device;
120 return B_OK;
121}
122
123
124void DeviceScanner::DeviceRemoved(BUSBDevice* device)
125{
126 // This is only called for devices we accepted in DeviceAdded. We accept
127 // only one, so we can safely remove it.
128 handle = NULL;
129}
130
Note: See TracBrowser for help on using the repository browser.