USB Host Shield 2.0
cdcprolific.cpp
Go to the documentation of this file.
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 "cdcprolific.h"
18 
20 ACM(p, pasync),
21 wPLType(0) {
22 }
23 
24 uint8_t PL2303::Init(uint8_t parent, uint8_t port, bool lowspeed) {
25  const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
26 
27  uint8_t buf[constBufSize];
28  USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
29  uint8_t rcode;
30  UsbDevice *p = NULL;
31  EpInfo *oldep_ptr = NULL;
32  uint8_t num_of_conf; // number of configurations
33 
34  AddressPool &addrPool = pUsb->GetAddressPool();
35 
36  USBTRACE("PL Init\r\n");
37 
38  if(bAddress)
40 
41  // Get pointer to pseudo device with address 0 assigned
42  p = addrPool.GetUsbDevicePtr(0);
43 
44  if(!p)
46 
47  if(!p->epinfo) {
48  USBTRACE("epinfo\r\n");
50  }
51 
52  // Save old pointer to EP_RECORD of address 0
53  oldep_ptr = p->epinfo;
54 
55  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
56  p->epinfo = epInfo;
57 
58  p->lowspeed = lowspeed;
59 
60  // Get device descriptor
61  rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
62 
63  // Restore p->epinfo
64  p->epinfo = oldep_ptr;
65 
66  if(rcode)
67  goto FailGetDevDescr;
68 
69  if(udd->idVendor != PL_VID && udd->idProduct != PL_PID)
71 
72  // Save type of PL chip
73  wPLType = udd->bcdDevice;
74 
75  // Allocate new address according to device class
76  bAddress = addrPool.AllocAddress(parent, false, port);
77 
78  if(!bAddress)
80 
81  // Extract Max Packet Size from the device descriptor
83 
84  // Assign new address to the device
85  rcode = pUsb->setAddr(0, 0, bAddress);
86 
87  if(rcode) {
88  p->lowspeed = false;
89  addrPool.FreeAddress(bAddress);
90  bAddress = 0;
91  USBTRACE2("setAddr:", rcode);
92  return rcode;
93  }
94 
95  USBTRACE2("Addr:", bAddress);
96 
97  p->lowspeed = false;
98 
99  p = addrPool.GetUsbDevicePtr(bAddress);
100 
101  if(!p)
103 
104  p->lowspeed = lowspeed;
105 
106  num_of_conf = udd->bNumConfigurations;
107 
108  // Assign epInfo to epinfo pointer
109  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
110 
111  if(rcode)
112  goto FailSetDevTblEntry;
113 
114  USBTRACE2("NC:", num_of_conf);
115 
116  for(uint8_t i = 0; i < num_of_conf; i++) {
119 
120  rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
121 
122  if(rcode)
123  goto FailGetConfDescr;
124 
125  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
126 
127  if(rcode)
128  goto FailGetConfDescr;
129 
130  if(bNumEP > 1)
131  break;
132  } // for
133 
134  if(bNumEP < 2)
136 
137  // Assign epInfo to epinfo pointer
139 
140  USBTRACE2("Conf:", bConfNum);
141 
142  // Set Configuration Value
143  rcode = pUsb->setConf(bAddress, 0, bConfNum);
144 
145  if(rcode)
146  goto FailSetConfDescr;
147 
148  rcode = pAsync->OnInit(this);
149 
150  if(rcode)
151  goto FailOnInit;
152 
153  USBTRACE("PL configured\r\n");
154 
155  //bPollEnable = true;
156  ready = true;
157  return 0;
158 
159 FailGetDevDescr:
160 #ifdef DEBUG_USB_HOST
162  goto Fail;
163 #endif
164 
165 FailSetDevTblEntry:
166 #ifdef DEBUG_USB_HOST
168  goto Fail;
169 #endif
170 
171 FailGetConfDescr:
172 #ifdef DEBUG_USB_HOST
174  goto Fail;
175 #endif
176 
177 FailSetConfDescr:
178 #ifdef DEBUG_USB_HOST
180  goto Fail;
181 #endif
182 
183 FailOnInit:
184 #ifdef DEBUG_USB_HOST
185  USBTRACE("OnInit:");
186 #endif
187 
188 #ifdef DEBUG_USB_HOST
189 Fail:
190  NotifyFail(rcode);
191 #endif
192  Release();
193  return rcode;
194 }
195 
196 //uint8_t PL::Poll()
197 //{
198 // uint8_t rcode = 0;
199 //
200 // //if (!bPollEnable)
201 // // return 0;
202 //
203 // //if (qNextPollTime <= millis())
204 // //{
205 // // USB_HOST_SERIAL.println(bAddress, HEX);
206 //
207 // // qNextPollTime = millis() + 100;
208 // //}
209 // return rcode;
210 //}
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition: Usb.cpp:769
EpInfo * epinfo
Definition: address.h:76
bool lowspeed
Definition: address.h:79
#define USB_ERROR_EPINFO_IS_NULL
Definition: UsbCore.h:83
virtual uint8_t OnInit(ACM *pacm)
Definition: cdcacm.h:131
uint16_t bcdDevice
Definition: usb_ch9.h:108
#define PL_PID
Definition: cdcprolific.h:23
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: cdcprolific.cpp:24
PL2303(USB *pusb, CDCAsyncOper *pasync)
Definition: cdcprolific.cpp:19
uint8_t bMaxPacketSize0
Definition: usb_ch9.h:105
volatile bool ready
Definition: cdcacm.h:178
#define NotifyFail(...)
Definition: message.h:55
USB * pUsb
Definition: cdcacm.h:169
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:808
uint8_t bNumEP
Definition: cdcacm.h:175
EpInfo epInfo[ACM_MAX_ENDPOINTS]
Definition: cdcacm.h:181
#define NotifyFailGetDevDescr(...)
Definition: message.h:50
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:64
virtual void FreeAddress(uint8_t addr)=0
virtual UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
CDCAsyncOper * pAsync
Definition: cdcacm.h:170
#define USBTRACE2(s, r)
Definition: macros.h:77
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:799
#define NotifyFailGetConfDescr(...)
Definition: message.h:52
Definition: address.h:32
#define PL_VID
Definition: cdcprolific.h:22
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
uint8_t bAddress
Definition: cdcacm.h:171
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition: UsbCore.h:85
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:82
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition: UsbCore.h:77
uint16_t idProduct
Definition: usb_ch9.h:107
uint8_t Release()
Definition: cdcacm.cpp:257
uint8_t bConfNum
Definition: cdcacm.h:172
uint8_t bNumConfigurations
Definition: usb_ch9.h:112
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:80
uint8_t maxPktSize
Definition: address.h:34
AddressPool & GetAddressPool()
Definition: UsbCore.h:213
Definition: UsbCore.h:197
Definition: cdcacm.h:163
#define NotifyFailSetConfDescr(...)
Definition: message.h:53
#define USBTRACE(s)
Definition: macros.h:75
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:764
#define NotifyFailSetDevTblEntry(...)
Definition: message.h:51