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