]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/cdc_XR21B1411.cpp
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / cdc_XR21B1411.cpp
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9
10 Contact information
11 -------------------
12
13 Circuits At Home, LTD
14 Web      :  http://www.circuitsathome.com
15 e-mail   :  support@circuitsathome.com
16  */
17 #include "cdc_XR21B1411.h"
18
19 XR21B1411::XR21B1411(USB *p, CDCAsyncOper *pasync) :
20 ACM(p, pasync) {
21         // Is this needed??
22         _enhanced_status = enhanced_features(); // Set up features
23 }
24
25 uint8_t XR21B1411::Init(uint8_t parent, uint8_t port, bool lowspeed) {
26         const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
27
28         uint8_t buf[constBufSize];
29         USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
30
31         uint8_t rcode;
32         UsbDevice *p = NULL;
33         EpInfo *oldep_ptr = NULL;
34         uint8_t num_of_conf; // number of configurations
35
36         AddressPool &addrPool = pUsb->GetAddressPool();
37
38         USBTRACE("XR Init\r\n");
39
40         if(bAddress)
41                 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
42
43         // Get pointer to pseudo device with address 0 assigned
44         p = addrPool.GetUsbDevicePtr(0);
45
46         if(!p)
47                 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
48
49         if(!p->epinfo) {
50                 USBTRACE("epinfo\r\n");
51                 return USB_ERROR_EPINFO_IS_NULL;
52         }
53
54         // Save old pointer to EP_RECORD of address 0
55         oldep_ptr = p->epinfo;
56
57         // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
58         p->epinfo = epInfo;
59
60         p->lowspeed = lowspeed;
61
62         // Get device descriptor
63         rcode = pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf);
64
65         // Restore p->epinfo
66         p->epinfo = oldep_ptr;
67
68         if(rcode)
69                 goto FailGetDevDescr;
70
71         // Allocate new address according to device class
72         bAddress = addrPool.AllocAddress(parent, false, port);
73
74         if(!bAddress)
75                 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
76
77         // Extract Max Packet Size from the device descriptor
78         epInfo[0].maxPktSize = udd->bMaxPacketSize0;
79
80         // Assign new address to the device
81         rcode = pUsb->setAddr(0, 0, bAddress);
82
83         if(rcode) {
84                 p->lowspeed = false;
85                 addrPool.FreeAddress(bAddress);
86                 bAddress = 0;
87                 USBTRACE2("setAddr:", rcode);
88                 return rcode;
89         }
90
91         USBTRACE2("Addr:", bAddress);
92
93         p->lowspeed = false;
94
95         p = addrPool.GetUsbDevicePtr(bAddress);
96
97         if(!p)
98                 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
99
100         p->lowspeed = lowspeed;
101
102         num_of_conf = udd->bNumConfigurations;
103
104         if((((udd->idVendor != 0x2890U) || (udd->idProduct != 0x0201U)) && ((udd->idVendor != 0x04e2U) || (udd->idProduct != 0x1411U))))
105                 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
106
107         // Assign epInfo to epinfo pointer
108         rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
109
110         if(rcode)
111                 goto FailSetDevTblEntry;
112
113         USBTRACE2("NC:", num_of_conf);
114
115         for(uint8_t i = 0; i < num_of_conf; i++) {
116                 ConfigDescParser< USB_CLASS_COM_AND_CDC_CTRL,
117                         CDC_SUBCLASS_ACM,
118                         CDC_PROTOCOL_ITU_T_V_250,
119                         CP_MASK_COMPARE_CLASS |
120                         CP_MASK_COMPARE_SUBCLASS |
121                         CP_MASK_COMPARE_PROTOCOL > CdcControlParser(this);
122
123                 ConfigDescParser<USB_CLASS_CDC_DATA, 0, 0,
124                         CP_MASK_COMPARE_CLASS> CdcDataParser(this);
125
126                 rcode = pUsb->getConfDescr(bAddress, 0, i, &CdcControlParser);
127
128                 if(rcode)
129                         goto FailGetConfDescr;
130
131                 rcode = pUsb->getConfDescr(bAddress, 0, i, &CdcDataParser);
132
133                 if(rcode)
134                         goto FailGetConfDescr;
135
136                 if(bNumEP > 1)
137                         break;
138         } // for
139
140         if(bNumEP < 4)
141                 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
142
143         // Assign epInfo to epinfo pointer
144         rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
145
146         USBTRACE2("Conf:", bConfNum);
147
148         // Set Configuration Value
149         rcode = pUsb->setConf(bAddress, 0, bConfNum);
150
151         if(rcode)
152                 goto FailSetConfDescr;
153
154         // Set up features status
155         _enhanced_status = enhanced_features();
156         half_duplex(false);
157         autoflowRTS(false);
158         autoflowDSR(false);
159         autoflowXON(false);
160         wide(false); // Always false, because this is only available in custom mode.
161
162         rcode = pAsync->OnInit(this);
163
164         if(rcode)
165                 goto FailOnInit;
166
167         USBTRACE("XR configured\r\n");
168
169         ready = true;
170
171         //bPollEnable = true;
172
173         //USBTRACE("Poll enabled\r\n");
174         return 0;
175
176 FailGetDevDescr:
177 #ifdef DEBUG_USB_HOST
178         NotifyFailGetDevDescr();
179         goto Fail;
180 #endif
181
182 FailSetDevTblEntry:
183 #ifdef DEBUG_USB_HOST
184         NotifyFailSetDevTblEntry();
185         goto Fail;
186 #endif
187
188 FailGetConfDescr:
189 #ifdef DEBUG_USB_HOST
190         NotifyFailGetConfDescr();
191         goto Fail;
192 #endif
193
194 FailSetConfDescr:
195 #ifdef DEBUG_USB_HOST
196         NotifyFailSetConfDescr();
197         goto Fail;
198 #endif
199
200 FailOnInit:
201 #ifdef DEBUG_USB_HOST
202         USBTRACE("OnInit:");
203 #endif
204
205 #ifdef DEBUG_USB_HOST
206 Fail:
207         NotifyFail(rcode);
208 #endif
209         Release();
210         return rcode;
211 }