usb: getting string descriptors, minor improvements
[quarnos.git] / resources / usb.h
blobce1eff56a0eef735f28db34c78ba8c6f3c641597
1 /* Quarn OS
3 * USB bus
5 * Copyright (C) 2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef _USB_H_
24 #define _USB_H_
26 #include "pci.h"
27 #include "bus.h"
28 #include "usb_hc.h"
29 #include "device.h"
30 #include "did.h"
32 namespace resources {
33 class uhci;
35 class usb : public bus {
36 private:
37 int dev_count;
39 pci_did pciaddr;
41 p<usb_hc> host;
42 public:
43 struct usb_setup_data {
44 u8 bmRequestType;
45 u8 bRequest;
46 u16 wValue;
47 u16 wIndex;
48 u16 wLength;
50 public:
51 enum usb_pid {
52 pid_setup = 0x2d,
53 pid_in = 0x69,
54 pid_out = 0xe1
56 private:
57 struct device_descriptor {
58 u8 bLength;
59 u8 bDescriptorType;
60 u16 bcdUSB;
61 u8 bDeviceClass;
62 u8 bDeviceSubClass;
63 u8 bDeviceProtocol;
64 u8 bMaxPacketSize0;
65 u16 idVendor;
66 u16 idProduct;
67 u16 bcdDevice;
68 u8 iManufacturer;
69 u8 iProduct;
70 u8 iSerialNumber;
71 u8 bNumConfigurations;
72 } __attribute__((packed));
74 struct configuration_descriptor {
75 u8 bLength;
76 u8 bDescriptorType;
77 u16 wTotalLength;
78 u8 bNumInterfaces;
79 u8 bConfigurationValue;
80 u8 iConfiguration;
81 u8 bmAttrubutes;
82 u8 bMaxPower;
83 } __attribute__((packed));
84 public:
85 struct interface_descriptor {
86 u8 bLength;
87 u8 bDescriptorType;
88 u8 bInterfaceNumber;
89 u8 bAlternateSetting;
90 u8 bNumEndpoints;
91 u8 bInterfaceClass;
92 u8 bInterfaceSubClass;
93 u8 bInterfaceProtocol;
94 u8 iInterface;
95 } __attribute__((packed));
96 private:
97 struct endpoint_descriptor {
98 u8 bLength;
99 u8 bDescriptorType;
100 u8 bEndpointAddress;
101 u8 bmAttributes;
102 u16 wMaxPacketSize;
103 u8 bInterval;
104 } __attribute__((packed));
106 void device_connected(int);
107 string get_dev_string(int, int);
108 public:
109 // bool initialize();
110 // bool type_added(manes::type_name);
111 p<usb_hc> get_host() {
112 return host;
115 void set_conf(int, int);
117 int get_count();
119 bool init_bus(p<device>);
121 static void register_type();
124 class usb_did : public did {
125 public:
126 usb_did() {}
127 virtual ~usb_did() {}
129 private:
130 int address;
131 p<usb::interface_descriptor> intf;
133 int control_endp;
134 int read_endp;
135 int write_endp;
137 int configuration;
138 public:
139 usb_did(p<bus> b, int addr, p<usb::interface_descriptor> i) : did(b), address(addr), intf(i) { }
141 virtual void set_endps(int c, int r, int w) {
142 control_endp = c;
143 read_endp = r;
144 write_endp = w;
147 virtual void set_conf(int c) {
148 configuration = c;
151 virtual int get_class() const {
152 return intf->bInterfaceClass;
155 virtual int get_subclass() const {
156 return intf->bInterfaceSubClass;
159 virtual int get_protocol() const {
160 return intf->bInterfaceProtocol;
162 void accepted() {
163 bus_id.cast<usb>()->set_conf(address, configuration);
166 virtual void bulk_read(buffer &buf) {
167 bus_id.cast<usb>()->get_host()->bulk_transfer(usb::pid_in, buf.get_address(), buf.get_size(), address, read_endp);
170 virtual void bulk_write(const buffer &buf) {
171 bus_id.cast<usb>()->get_host()->bulk_transfer(usb::pid_out, buf.get_address(), buf.get_size(), address, write_endp);
174 virtual void control(const buffer &buf) {
175 bus_id.cast<usb>()->get_host()->control_transfer(usb::pid_setup, buf.get_address(), buf.get_size(), 0, address);
178 virtual buffer control(const buffer &buf, int size) {
179 void *addr = bus_id.cast<usb>()->get_host()->control_transfer(usb::pid_setup, buf.get_address(), buf.get_size(), size, address);
180 return buffer(addr, size);
185 #endif