1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
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
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
18 /* Google ADK interface */
22 const uint8_t ADK::epDataInIndex = 1;
23 const uint8_t ADK::epDataOutIndex = 2;
25 ADK::ADK(USB *p, const char* manufacturer,
27 const char* description,
33 manufacturer(manufacturer),
35 description(description),
39 pUsb(p), //pointer to USB class instance - mandatory
40 bAddress(0), //device address - mandatory
41 bConfNum(0), //configuration number
42 bNumEP(1), //if config descriptor needs to be parsed
44 // initialize endpoint data structures
45 for(uint8_t i = 0; i < ADK_MAX_ENDPOINTS; i++) {
47 epInfo[i].maxPktSize = (i) ? 0 : 8;
48 epInfo[i].epAttribs = 0;
49 epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
50 }//for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++...
52 // register in USB subsystem
54 pUsb->RegisterDeviceClass(this); //set devConfig[] entry
58 uint8_t ADK::ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) {
59 return Init(parent, port, lowspeed); // Just call Init. Yes, really!
62 /* Connection initialization of an Android phone */
63 uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) {
64 uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
65 USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
67 uint8_t num_of_conf; // number of configurations
69 EpInfo *oldep_ptr = NULL;
71 // get memory address of USB device address pool
72 AddressPool &addrPool = pUsb->GetAddressPool();
74 USBTRACE("\r\nADK Init");
76 // check if address has already been assigned to an instance
78 USBTRACE("\r\nAddress in use");
79 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
82 // Get pointer to pseudo device with address 0 assigned
83 p = addrPool.GetUsbDevicePtr(0);
86 USBTRACE("\r\nAddress not found");
87 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
91 USBTRACE("epinfo is null\r\n");
92 return USB_ERROR_EPINFO_IS_NULL;
95 // Save old pointer to EP_RECORD of address 0
96 oldep_ptr = p->epinfo;
98 // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
101 p->lowspeed = lowspeed;
103 // Get device descriptor
104 rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
107 p->epinfo = oldep_ptr;
110 goto FailGetDevDescr;
113 // Allocate new address according to device class
114 bAddress = addrPool.AllocAddress(parent, false, port);
116 // Extract Max Packet Size from device descriptor
117 epInfo[0].maxPktSize = udd->bMaxPacketSize0;
119 // Assign new address to the device
120 rcode = pUsb->setAddr(0, 0, bAddress);
123 addrPool.FreeAddress(bAddress);
125 //USBTRACE2("setAddr:",rcode);
129 //USBTRACE2("\r\nAddr:", bAddress);
130 // Spec says you should wait at least 200ms.
135 //get pointer to assigned address record
136 p = addrPool.GetUsbDevicePtr(bAddress);
138 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
141 p->lowspeed = lowspeed;
143 // Assign epInfo to epinfo pointer - only EP0 is known
144 rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
146 goto FailSetDevTblEntry;
149 //check if ADK device is already in accessory mode; if yes, configure and exit
150 if(udd->idVendor == ADK_VID &&
151 (udd->idProduct == ADK_PID || udd->idProduct == ADB_PID)) {
152 USBTRACE("\r\nAcc.mode device detected");
153 /* go through configurations, find first bulk-IN, bulk-OUT EP, fill epInfo and quit */
154 num_of_conf = udd->bNumConfigurations;
156 //USBTRACE2("\r\nNC:",num_of_conf);
157 for(uint8_t i = 0; i < num_of_conf; i++) {
158 ConfigDescParser < 0, 0, 0, 0 > confDescrParser(this);
160 rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
162 //added by Jaylen Scott Vanorden
164 USBTRACE2("\r\nGot 1st bad code for config: ", rcode);
166 rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
170 goto FailGetConfDescr;
175 } // for (uint8_t i=0; i<num_of_conf; i++...
178 // Assign epInfo to epinfo pointer - this time all 3 endpoins
179 rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo);
181 goto FailSetDevTblEntry;
185 // Set Configuration Value
186 rcode = pUsb->setConf(bAddress, 0, bConfNum);
188 goto FailSetConfDescr;
190 /* print endpoint structure */
192 USBTRACE("\r\nEndpoint Structure:");
193 USBTRACE("\r\nEP0:");
194 USBTRACE2("\r\nAddr: ", epInfo[0].epAddr);
195 USBTRACE2("\r\nMax.pkt.size: ", epInfo[0].maxPktSize);
196 USBTRACE2("\r\nAttr: ", epInfo[0].epAttribs);
197 USBTRACE("\r\nEpout:");
198 USBTRACE2("\r\nAddr: ", epInfo[epDataOutIndex].epAddr);
199 USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataOutIndex].maxPktSize);
200 USBTRACE2("\r\nAttr: ", epInfo[epDataOutIndex].epAttribs);
201 USBTRACE("\r\nEpin:");
202 USBTRACE2("\r\nAddr: ", epInfo[epDataInIndex].epAddr);
203 USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataInIndex].maxPktSize);
204 USBTRACE2("\r\nAttr: ", epInfo[epDataInIndex].epAttribs);
207 USBTRACE("\r\nConfiguration successful");
209 return 0; //successful configuration
210 }//if( buf->idVendor == ADK_VID...
212 //probe device - get accessory protocol revision
214 uint16_t adkproto = -1;
216 rcode = getProto((uint8_t*) & adkproto);
218 //added by Jaylen Scott Vanorden
220 USBTRACE2("\r\nGot 1st bad code for proto: ", rcode);
222 rcode = getProto((uint8_t*) & adkproto);
226 goto FailGetProto; //init fails
228 USBTRACE2("\r\nADK protocol rev. ", adkproto);
234 sendStr(ACCESSORY_STRING_MANUFACTURER, manufacturer);
236 sendStr(ACCESSORY_STRING_MODEL, model);
238 sendStr(ACCESSORY_STRING_DESCRIPTION, description);
240 sendStr(ACCESSORY_STRING_VERSION, version);
242 sendStr(ACCESSORY_STRING_URI, uri);
244 sendStr(ACCESSORY_STRING_SERIAL, serial);
248 //switch to accessory mode
249 //the Android phone will reset
252 goto FailSwAcc; //init fails
254 rcode = USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET;
255 delay(100); // Give Android a chance to do its reset. This is a guess, and possibly could be lower.
256 goto SwAttempt; //switch to accessory mode attempted
258 /* diagnostic messages */
260 #ifdef DEBUG_USB_HOST
261 NotifyFailGetDevDescr(rcode);
266 #ifdef DEBUG_USB_HOST
267 NotifyFailSetDevTblEntry(rcode);
272 #ifdef DEBUG_USB_HOST
273 NotifyFailGetConfDescr(rcode);
278 #ifdef DEBUG_USB_HOST
279 NotifyFailSetConfDescr(rcode);
284 #ifdef DEBUG_USB_HOST
285 USBTRACE("\r\ngetProto:");
290 #ifdef DEBUG_USB_HOST
291 USBTRACE("\r\nswAcc:");
296 // USBTRACE("OnInit:");
300 #ifdef DEBUG_USB_HOST
301 USBTRACE("\r\nAccessory mode switch attempt");
304 //USBTRACE2("\r\nADK Init Failed, error code: ", rcode);
310 /* Extracts bulk-IN and bulk-OUT endpoint information from config descriptor */
311 void ADK::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
312 //ErrorMessage<uint8_t>(PSTR("Conf.Val"), conf);
313 //ErrorMessage<uint8_t>(PSTR("Iface Num"), iface);
314 //ErrorMessage<uint8_t>(PSTR("Alt.Set"), alt);
316 //added by Yuuichi Akagawa
323 if((pep->bmAttributes & 0x02) == 2) {
324 uint8_t index = ((pep->bEndpointAddress & 0x80) == 0x80) ? epDataInIndex : epDataOutIndex;
325 // Fill in the endpoint info structure
326 epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
327 epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
331 //PrintEndpointDescriptor(pep);
335 /* Performs a cleanup after failed Init() attempt */
336 uint8_t ADK::Release() {
337 pUsb->GetAddressPool().FreeAddress(bAddress);
339 bNumEP = 1; //must have to be reset to 1
346 uint8_t ADK::RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr) {
347 //USBTRACE2("\r\nAddr: ", bAddress );
348 //USBTRACE2("\r\nEP: ",epInfo[epDataInIndex].epAddr);
349 return pUsb->inTransfer(bAddress, epInfo[epDataInIndex].epAddr, bytes_rcvd, dataptr);
352 uint8_t ADK::SndData(uint16_t nbytes, uint8_t *dataptr) {
353 return pUsb->outTransfer(bAddress, epInfo[epDataOutIndex].epAddr, nbytes, dataptr);
356 void ADK::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr) {
357 Notify(PSTR("Endpoint descriptor:"), 0x80);
358 Notify(PSTR("\r\nLength:\t\t"), 0x80);
359 D_PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
360 Notify(PSTR("\r\nType:\t\t"), 0x80);
361 D_PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
362 Notify(PSTR("\r\nAddress:\t"), 0x80);
363 D_PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
364 Notify(PSTR("\r\nAttributes:\t"), 0x80);
365 D_PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
366 Notify(PSTR("\r\nMaxPktSize:\t"), 0x80);
367 D_PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
368 Notify(PSTR("\r\nPoll Intrv:\t"), 0x80);
369 D_PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
370 Notify(PSTR("\r\n"), 0x80);