Keyboard firmwares for Atmel AVR and Cortex-M
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

adk.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Circuits At Home, LTD
  11. Web : http://www.circuitsathome.com
  12. e-mail : [email protected]
  13. */
  14. /* Google ADK interface */
  15. #include "adk.h"
  16. const uint8_t ADK::epDataInIndex = 1;
  17. const uint8_t ADK::epDataOutIndex = 2;
  18. ADK::ADK(USB *p, const char* manufacturer,
  19. const char* model,
  20. const char* description,
  21. const char* version,
  22. const char* uri,
  23. const char* serial) :
  24. /* ADK ID Strings */
  25. manufacturer(manufacturer),
  26. model(model),
  27. description(description),
  28. version(version),
  29. uri(uri),
  30. serial(serial),
  31. pUsb(p), //pointer to USB class instance - mandatory
  32. bAddress(0), //device address - mandatory
  33. bConfNum(0), //configuration number
  34. bNumEP(1), //if config descriptor needs to be parsed
  35. ready(false) {
  36. // initialize endpoint data structures
  37. for(uint8_t i = 0; i < ADK_MAX_ENDPOINTS; i++) {
  38. epInfo[i].epAddr = 0;
  39. epInfo[i].maxPktSize = (i) ? 0 : 8;
  40. epInfo[i].epAttribs = 0;
  41. epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
  42. }//for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++...
  43. // register in USB subsystem
  44. if(pUsb) {
  45. pUsb->RegisterDeviceClass(this); //set devConfig[] entry
  46. }
  47. }
  48. uint8_t ADK::ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) {
  49. return Init(parent, port, lowspeed); // Just call Init. Yes, really!
  50. }
  51. /* Connection initialization of an Android phone */
  52. uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) {
  53. uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
  54. USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
  55. uint8_t rcode;
  56. uint8_t num_of_conf; // number of configurations
  57. UsbDevice *p = NULL;
  58. EpInfo *oldep_ptr = NULL;
  59. // get memory address of USB device address pool
  60. AddressPool &addrPool = pUsb->GetAddressPool();
  61. USBTRACE("\r\nADK Init");
  62. // check if address has already been assigned to an instance
  63. if(bAddress) {
  64. USBTRACE("\r\nAddress in use");
  65. return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
  66. }
  67. // Get pointer to pseudo device with address 0 assigned
  68. p = addrPool.GetUsbDevicePtr(0);
  69. if(!p) {
  70. USBTRACE("\r\nAddress not found");
  71. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  72. }
  73. if(!p->epinfo) {
  74. USBTRACE("epinfo is null\r\n");
  75. return USB_ERROR_EPINFO_IS_NULL;
  76. }
  77. // Save old pointer to EP_RECORD of address 0
  78. oldep_ptr = p->epinfo;
  79. // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
  80. p->epinfo = epInfo;
  81. p->lowspeed = lowspeed;
  82. // Get device descriptor
  83. rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
  84. // Restore p->epinfo
  85. p->epinfo = oldep_ptr;
  86. if(rcode) {
  87. goto FailGetDevDescr;
  88. }
  89. // Allocate new address according to device class
  90. bAddress = addrPool.AllocAddress(parent, false, port);
  91. // Extract Max Packet Size from device descriptor
  92. epInfo[0].maxPktSize = udd->bMaxPacketSize0;
  93. // Assign new address to the device
  94. rcode = pUsb->setAddr(0, 0, bAddress);
  95. if(rcode) {
  96. p->lowspeed = false;
  97. addrPool.FreeAddress(bAddress);
  98. bAddress = 0;
  99. //USBTRACE2("setAddr:",rcode);
  100. return rcode;
  101. }//if (rcode...
  102. //USBTRACE2("\r\nAddr:", bAddress);
  103. // Spec says you should wait at least 200ms.
  104. //delay(300);
  105. p->lowspeed = false;
  106. //get pointer to assigned address record
  107. p = addrPool.GetUsbDevicePtr(bAddress);
  108. if(!p) {
  109. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  110. }
  111. p->lowspeed = lowspeed;
  112. // Assign epInfo to epinfo pointer - only EP0 is known
  113. rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
  114. if(rcode) {
  115. goto FailSetDevTblEntry;
  116. }
  117. //check if ADK device is already in accessory mode; if yes, configure and exit
  118. if(udd->idVendor == ADK_VID &&
  119. (udd->idProduct == ADK_PID || udd->idProduct == ADB_PID)) {
  120. USBTRACE("\r\nAcc.mode device detected");
  121. /* go through configurations, find first bulk-IN, bulk-OUT EP, fill epInfo and quit */
  122. num_of_conf = udd->bNumConfigurations;
  123. //USBTRACE2("\r\nNC:",num_of_conf);
  124. for(uint8_t i = 0; i < num_of_conf; i++) {
  125. ConfigDescParser < 0, 0, 0, 0 > confDescrParser(this);
  126. delay(1);
  127. rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
  128. #if defined(XOOM)
  129. //added by Jaylen Scott Vanorden
  130. if(rcode) {
  131. USBTRACE2("\r\nGot 1st bad code for config: ", rcode);
  132. // Try once more
  133. rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
  134. }
  135. #endif
  136. if(rcode) {
  137. goto FailGetConfDescr;
  138. }
  139. if(bNumEP > 2) {
  140. break;
  141. }
  142. } // for (uint8_t i=0; i<num_of_conf; i++...
  143. if(bNumEP == 3) {
  144. // Assign epInfo to epinfo pointer - this time all 3 endpoins
  145. rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo);
  146. if(rcode) {
  147. goto FailSetDevTblEntry;
  148. }
  149. }
  150. // Set Configuration Value
  151. rcode = pUsb->setConf(bAddress, 0, bConfNum);
  152. if(rcode) {
  153. goto FailSetConfDescr;
  154. }
  155. /* print endpoint structure */
  156. /*
  157. USBTRACE("\r\nEndpoint Structure:");
  158. USBTRACE("\r\nEP0:");
  159. USBTRACE2("\r\nAddr: ", epInfo[0].epAddr);
  160. USBTRACE2("\r\nMax.pkt.size: ", epInfo[0].maxPktSize);
  161. USBTRACE2("\r\nAttr: ", epInfo[0].epAttribs);
  162. USBTRACE("\r\nEpout:");
  163. USBTRACE2("\r\nAddr: ", epInfo[epDataOutIndex].epAddr);
  164. USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataOutIndex].maxPktSize);
  165. USBTRACE2("\r\nAttr: ", epInfo[epDataOutIndex].epAttribs);
  166. USBTRACE("\r\nEpin:");
  167. USBTRACE2("\r\nAddr: ", epInfo[epDataInIndex].epAddr);
  168. USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataInIndex].maxPktSize);
  169. USBTRACE2("\r\nAttr: ", epInfo[epDataInIndex].epAttribs);
  170. */
  171. USBTRACE("\r\nConfiguration successful");
  172. ready = true;
  173. return 0; //successful configuration
  174. }//if( buf->idVendor == ADK_VID...
  175. //probe device - get accessory protocol revision
  176. {
  177. uint16_t adkproto = -1;
  178. delay(1);
  179. rcode = getProto((uint8_t*) & adkproto);
  180. #if defined(XOOM)
  181. //added by Jaylen Scott Vanorden
  182. if(rcode) {
  183. USBTRACE2("\r\nGot 1st bad code for proto: ", rcode);
  184. // Try once more
  185. rcode = getProto((uint8_t*) & adkproto);
  186. }
  187. #endif
  188. if(rcode) {
  189. goto FailGetProto; //init fails
  190. }
  191. USBTRACE2("\r\nADK protocol rev. ", adkproto);
  192. }
  193. delay(100);
  194. //sending ID strings
  195. sendStr(ACCESSORY_STRING_MANUFACTURER, manufacturer);
  196. delay(10);
  197. sendStr(ACCESSORY_STRING_MODEL, model);
  198. delay(10);
  199. sendStr(ACCESSORY_STRING_DESCRIPTION, description);
  200. delay(10);
  201. sendStr(ACCESSORY_STRING_VERSION, version);
  202. delay(10);
  203. sendStr(ACCESSORY_STRING_URI, uri);
  204. delay(10);
  205. sendStr(ACCESSORY_STRING_SERIAL, serial);
  206. delay(100);
  207. //switch to accessory mode
  208. //the Android phone will reset
  209. rcode = switchAcc();
  210. if(rcode) {
  211. goto FailSwAcc; //init fails
  212. }
  213. rcode = USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET;
  214. delay(100); // Give Android a chance to do its reset. This is a guess, and possibly could be lower.
  215. goto SwAttempt; //switch to accessory mode attempted
  216. /* diagnostic messages */
  217. FailGetDevDescr:
  218. #ifdef DEBUG_USB_HOST
  219. NotifyFailGetDevDescr(rcode);
  220. goto Fail;
  221. #endif
  222. FailSetDevTblEntry:
  223. #ifdef DEBUG_USB_HOST
  224. NotifyFailSetDevTblEntry(rcode);
  225. goto Fail;
  226. #endif
  227. FailGetConfDescr:
  228. #ifdef DEBUG_USB_HOST
  229. NotifyFailGetConfDescr(rcode);
  230. goto Fail;
  231. #endif
  232. FailSetConfDescr:
  233. #ifdef DEBUG_USB_HOST
  234. NotifyFailSetConfDescr(rcode);
  235. goto Fail;
  236. #endif
  237. FailGetProto:
  238. #ifdef DEBUG_USB_HOST
  239. USBTRACE("\r\ngetProto:");
  240. goto Fail;
  241. #endif
  242. FailSwAcc:
  243. #ifdef DEBUG_USB_HOST
  244. USBTRACE("\r\nswAcc:");
  245. goto Fail;
  246. #endif
  247. //FailOnInit:
  248. // USBTRACE("OnInit:");
  249. // goto Fail;
  250. //
  251. SwAttempt:
  252. #ifdef DEBUG_USB_HOST
  253. USBTRACE("\r\nAccessory mode switch attempt");
  254. Fail:
  255. #endif
  256. //USBTRACE2("\r\nADK Init Failed, error code: ", rcode);
  257. //NotifyFail(rcode);
  258. Release();
  259. return rcode;
  260. }
  261. /* Extracts bulk-IN and bulk-OUT endpoint information from config descriptor */
  262. void ADK::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
  263. //ErrorMessage<uint8_t>(PSTR("Conf.Val"), conf);
  264. //ErrorMessage<uint8_t>(PSTR("Iface Num"), iface);
  265. //ErrorMessage<uint8_t>(PSTR("Alt.Set"), alt);
  266. //added by Yuuichi Akagawa
  267. if(bNumEP == 3) {
  268. return;
  269. }
  270. bConfNum = conf;
  271. if((pep->bmAttributes & 0x02) == 2) {
  272. uint8_t index = ((pep->bEndpointAddress & 0x80) == 0x80) ? epDataInIndex : epDataOutIndex;
  273. // Fill in the endpoint info structure
  274. epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
  275. epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
  276. bNumEP++;
  277. //PrintEndpointDescriptor(pep);
  278. }
  279. }
  280. /* Performs a cleanup after failed Init() attempt */
  281. uint8_t ADK::Release() {
  282. pUsb->GetAddressPool().FreeAddress(bAddress);
  283. bNumEP = 1; //must have to be reset to 1
  284. bAddress = 0;
  285. ready = false;
  286. return 0;
  287. }
  288. uint8_t ADK::RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr) {
  289. //USBTRACE2("\r\nAddr: ", bAddress );
  290. //USBTRACE2("\r\nEP: ",epInfo[epDataInIndex].epAddr);
  291. return pUsb->inTransfer(bAddress, epInfo[epDataInIndex].epAddr, bytes_rcvd, dataptr);
  292. }
  293. uint8_t ADK::SndData(uint16_t nbytes, uint8_t *dataptr) {
  294. return pUsb->outTransfer(bAddress, epInfo[epDataOutIndex].epAddr, nbytes, dataptr);
  295. }
  296. void ADK::PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr) {
  297. Notify(PSTR("Endpoint descriptor:"), 0x80);
  298. Notify(PSTR("\r\nLength:\t\t"), 0x80);
  299. D_PrintHex<uint8_t > (ep_ptr->bLength, 0x80);
  300. Notify(PSTR("\r\nType:\t\t"), 0x80);
  301. D_PrintHex<uint8_t > (ep_ptr->bDescriptorType, 0x80);
  302. Notify(PSTR("\r\nAddress:\t"), 0x80);
  303. D_PrintHex<uint8_t > (ep_ptr->bEndpointAddress, 0x80);
  304. Notify(PSTR("\r\nAttributes:\t"), 0x80);
  305. D_PrintHex<uint8_t > (ep_ptr->bmAttributes, 0x80);
  306. Notify(PSTR("\r\nMaxPktSize:\t"), 0x80);
  307. D_PrintHex<uint16_t > (ep_ptr->wMaxPacketSize, 0x80);
  308. Notify(PSTR("\r\nPoll Intrv:\t"), 0x80);
  309. D_PrintHex<uint8_t > (ep_ptr->bInterval, 0x80);
  310. Notify(PSTR("\r\n"), 0x80);
  311. }