USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Usb.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 /* USB functions */
18 
19 #include "avrpins.h"
20 #include "max3421e.h"
21 #include "usbhost.h"
22 #include "Usb.h"
23 
24 #if defined(ARDUINO) && ARDUINO >=100
25 #include "Arduino.h"
26 #else
27 #include <WProgram.h>
28 #endif
29 
30 static uint8_t usb_error = 0;
31 static uint8_t usb_task_state;
32 
33 /* constructor */
34 USB::USB() : bmHubPre(0) {
35  usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; //set up state machine
36  init();
37 }
38 
39 /* Initialize data structures */
40 void USB::init() {
41  //devConfigIndex = 0;
42  bmHubPre = 0;
43 }
44 
45 uint8_t USB::getUsbTaskState(void) {
46  return ( usb_task_state);
47 }
48 
49 void USB::setUsbTaskState(uint8_t state) {
50  usb_task_state = state;
51 }
52 
53 EpInfo* USB::getEpInfoEntry(uint8_t addr, uint8_t ep) {
54  UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
55 
56  if (!p || !p->epinfo)
57  return NULL;
58 
59  EpInfo *pep = p->epinfo;
60 
61  for (uint8_t i = 0; i < p->epcount; i++) {
62  if ((pep)->epAddr == ep)
63  return pep;
64 
65  pep++;
66  }
67  return NULL;
68 }
69 
70 /* set device table entry */
71 
72 /* each device is different and has different number of endpoints. This function plugs endpoint record structure, defined in application, to devtable */
73 uint8_t USB::setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo* eprecord_ptr) {
74  if (!eprecord_ptr)
76 
77  UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
78 
79  if (!p)
81 
82  p->address = addr;
83  p->epinfo = eprecord_ptr;
84  p->epcount = epcount;
85 
86  return 0;
87 }
88 
89 uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t &nak_limit) {
90  UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
91 
92  if (!p)
94 
95  if (!p->epinfo)
97 
98  *ppep = getEpInfoEntry(addr, ep);
99 
100  if (!*ppep)
102 
103  nak_limit = (0x0001UL << (((*ppep)->bmNakPower > USB_NAK_MAX_POWER) ? USB_NAK_MAX_POWER : (*ppep)->bmNakPower));
104  nak_limit--;
105  /*
106  USBTRACE2("\r\nAddress: ", addr);
107  USBTRACE2(" EP: ", ep);
108  USBTRACE2(" NAK Power: ",(*ppep)->bmNakPower);
109  USBTRACE2(" NAK Limit: ", nak_limit);
110  USBTRACE("\r\n");
111  */
112  regWr(rPERADDR, addr); //set peripheral address
113 
114  uint8_t mode = regRd(rMODE);
115 
116  //Serial.print("\r\nMode: ");
117  //Serial.println( mode, HEX);
118  //Serial.print("\r\nLS: ");
119  //Serial.println(p->lowspeed, HEX);
120 
121 
122 
123  // Set bmLOWSPEED and bmHUBPRE in case of low-speed device, reset them otherwise
124  regWr(rMODE, (p->lowspeed) ? mode | bmLOWSPEED | bmHubPre : mode & ~(bmHUBPRE | bmLOWSPEED));
125 
126  return 0;
127 }
128 
129 /* Control transfer. Sets address, endpoint, fills control packet with necessary data, dispatches control packet, and initiates bulk IN transfer, */
130 /* depending on request. Actual requests are defined as inlines */
131 /* return codes: */
132 /* 00 = success */
133 
134 /* 01-0f = non-zero HRSLT */
135 uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
136  uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr, USBReadParser *p) {
137  bool direction = false; //request direction, IN or OUT
138  uint8_t rcode;
139  SETUP_PKT setup_pkt;
140 
141  EpInfo *pep = NULL;
142  uint16_t nak_limit = 0;
143 
144  rcode = SetAddress(addr, ep, &pep, nak_limit);
145 
146  if (rcode)
147  return rcode;
148 
149  direction = ((bmReqType & 0x80) > 0);
150 
151  /* fill in setup packet */
152  setup_pkt.ReqType_u.bmRequestType = bmReqType;
153  setup_pkt.bRequest = bRequest;
154  setup_pkt.wVal_u.wValueLo = wValLo;
155  setup_pkt.wVal_u.wValueHi = wValHi;
156  setup_pkt.wIndex = wInd;
157  setup_pkt.wLength = total;
158 
159  bytesWr(rSUDFIFO, 8, (uint8_t*) & setup_pkt); //transfer to setup packet FIFO
160 
161  rcode = dispatchPkt(tokSETUP, ep, nak_limit); //dispatch packet
162 
163  if (rcode) //return HRSLT if not zero
164  return ( rcode);
165 
166  if (dataptr != NULL) //data stage, if present
167  {
168  if (direction) //IN transfer
169  {
170  uint16_t left = total;
171 
172  pep->bmRcvToggle = 1; //bmRCVTOG1;
173 
174  while (left) {
175  // Bytes read into buffer
176  uint16_t read = nbytes;
177  //uint16_t read = (left<nbytes) ? left : nbytes;
178 
179  rcode = InTransfer(pep, nak_limit, &read, dataptr);
180  if (rcode == hrTOGERR) {
181  // yes, we flip it wrong here so that next time it is actually correct!
182  pep->bmRcvToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
183  continue;
184  }
185 
186  if (rcode)
187  return rcode;
188 
189  // Invoke callback function if inTransfer completed successfully and callback function pointer is specified
190  if (!rcode && p)
191  ((USBReadParser*)p)->Parse(read, dataptr, total - left);
192 
193  left -= read;
194 
195  if (read < nbytes)
196  break;
197  }
198  } else //OUT transfer
199  {
200  pep->bmSndToggle = 1; //bmSNDTOG1;
201  rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
202  }
203  if (rcode) //return error
204  return ( rcode);
205  }
206  // Status stage
207  return dispatchPkt((direction) ? tokOUTHS : tokINHS, ep, nak_limit); //GET if direction
208 }
209 
210 /* IN transfer to arbitrary endpoint. Assumes PERADDR is set. Handles multiple packets if necessary. Transfers 'nbytes' bytes. */
211 /* Keep sending INs and writes data to memory area pointed by 'data' */
212 
213 /* rcode 0 if no errors. rcode 01-0f is relayed from dispatchPkt(). Rcode f0 means RCVDAVIRQ error,
214  fe USB xfer timeout */
215 uint8_t USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data) {
216  EpInfo *pep = NULL;
217  uint16_t nak_limit = 0;
218 
219  uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
220 
221  if (rcode) {
222  //printf("SetAddress Failed");
223  return rcode;
224  }
225  return InTransfer(pep, nak_limit, nbytesptr, data);
226 }
227 
228 uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t* data) {
229  uint8_t rcode = 0;
230  uint8_t pktsize;
231 
232  uint16_t nbytes = *nbytesptr;
233  //printf("Requesting %i bytes ", nbytes);
234  uint8_t maxpktsize = pep->maxPktSize;
235 
236  *nbytesptr = 0;
237  regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); //set toggle value
238 
239  while (1) // use a 'return' to exit this loop
240  {
241  rcode = dispatchPkt(tokIN, pep->epAddr, nak_limit); //IN packet to EP-'endpoint'. Function takes care of NAKS.
242  if (rcode == hrTOGERR) {
243  // yes, we flip it wrong here so that next time it is actually correct!
244  pep->bmRcvToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
245  regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); //set toggle value
246  continue;
247  }
248  if (rcode) {
249  //printf(">>>>>>>> Problem! dispatchPkt %2.2x\r\n", rcode);
250  break; //should be 0, indicating ACK. Else return error code.
251  }
252  /* check for RCVDAVIRQ and generate error if not present */
253  /* the only case when absence of RCVDAVIRQ makes sense is when toggle error occurred. Need to add handling for that */
254  if ((regRd(rHIRQ) & bmRCVDAVIRQ) == 0) {
255  //printf(">>>>>>>> Problem! NO RCVDAVIRQ!\r\n");
256  rcode = 0xf0; //receive error
257  break;
258  }
259  pktsize = regRd(rRCVBC); //number of received bytes
260  //printf("Got %i bytes \r\n", pktsize);
261  // This would be OK, but...
262  //assert(pktsize <= nbytes);
263  if (pktsize > nbytes) {
264  // This can happen. Use of assert on Arduino locks up the Arduino.
265  // So I will trim the value, and hope for the best.
266  //printf(">>>>>>>> Problem! Wanted %i bytes but got %i.\r\n", nbytes, pktsize);
267  pktsize = nbytes;
268  }
269 
270  int16_t mem_left = (int16_t)nbytes - *((int16_t*)nbytesptr);
271 
272  if (mem_left < 0)
273  mem_left = 0;
274 
275  data = bytesRd(rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data);
276 
277  regWr(rHIRQ, bmRCVDAVIRQ); // Clear the IRQ & free the buffer
278  *nbytesptr += pktsize; // add this packet's byte count to total transfer length
279 
280  /* The transfer is complete under two conditions: */
281  /* 1. The device sent a short packet (L.T. maxPacketSize) */
282  /* 2. 'nbytes' have been transferred. */
283  if ((pktsize < maxpktsize) || (*nbytesptr >= nbytes)) // have we transferred 'nbytes' bytes?
284  {
285  // Save toggle value
286  pep->bmRcvToggle = ((regRd(rHRSL) & bmRCVTOGRD)) ? 1 : 0;
287  //printf("\r\n");
288  rcode = 0;
289  break;
290  } // if
291  } //while( 1 )
292  return ( rcode);
293 }
294 
295 /* OUT transfer to arbitrary endpoint. Handles multiple packets if necessary. Transfers 'nbytes' bytes. */
296 /* Handles NAK bug per Maxim Application Note 4000 for single buffer transfer */
297 
298 /* rcode 0 if no errors. rcode 01-0f is relayed from HRSL */
299 uint8_t USB::outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* data) {
300  EpInfo *pep = NULL;
301  uint16_t nak_limit = 0;
302 
303  uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
304 
305  if (rcode)
306  return rcode;
307 
308  return OutTransfer(pep, nak_limit, nbytes, data);
309 }
310 
311 uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) {
312  uint8_t rcode = hrSUCCESS, retry_count;
313  uint8_t *data_p = data; //local copy of the data pointer
314  uint16_t bytes_tosend, nak_count;
315  uint16_t bytes_left = nbytes;
316 
317  uint8_t maxpktsize = pep->maxPktSize;
318 
319  if (maxpktsize < 1 || maxpktsize > 64)
321 
322  unsigned long timeout = millis() + USB_XFER_TIMEOUT;
323 
324  regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); //set toggle value
325 
326  while (bytes_left) {
327  retry_count = 0;
328  nak_count = 0;
329  bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
330  bytesWr(rSNDFIFO, bytes_tosend, data_p); //filling output FIFO
331  regWr(rSNDBC, bytes_tosend); //set number of bytes
332  regWr(rHXFR, (tokOUT | pep->epAddr)); //dispatch packet
333  while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); //wait for the completion IRQ
334  regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ
335  rcode = (regRd(rHRSL) & 0x0f);
336 
337  while (rcode && (timeout > millis())) {
338  switch (rcode) {
339  case hrNAK:
340  nak_count++;
341  if (nak_limit && (nak_count == nak_limit))
342  goto breakout;
343  //return ( rcode);
344  break;
345  case hrTIMEOUT:
346  retry_count++;
347  if (retry_count == USB_RETRY_LIMIT)
348  goto breakout;
349  //return ( rcode);
350  break;
351  case hrTOGERR:
352  // yes, we flip it wrong here so that next time it is actually correct!
353  pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
354  regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); //set toggle value
355  break;
356  default:
357  goto breakout;
358  }//switch( rcode
359 
360  /* process NAK according to Host out NAK bug */
361  regWr(rSNDBC, 0);
362  regWr(rSNDFIFO, *data_p);
363  regWr(rSNDBC, bytes_tosend);
364  regWr(rHXFR, (tokOUT | pep->epAddr)); //dispatch packet
365  while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); //wait for the completion IRQ
366  regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ
367  rcode = (regRd(rHRSL) & 0x0f);
368  }//while( rcode && ....
369  bytes_left -= bytes_tosend;
370  data_p += bytes_tosend;
371  }//while( bytes_left...
372 breakout:
373 
374  pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 1 : 0; //bmSNDTOG1 : bmSNDTOG0; //update toggle
375  return ( rcode); //should be 0 in all cases
376 }
377 /* dispatch USB packet. Assumes peripheral address is set and relevant buffer is loaded/empty */
378 /* If NAK, tries to re-send up to nak_limit times */
379 /* If nak_limit == 0, do not count NAKs, exit after timeout */
380 /* If bus timeout, re-sends up to USB_RETRY_LIMIT times */
381 
382 /* return codes 0x00-0x0f are HRSLT( 0x00 being success ), 0xff means timeout */
383 uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
384  unsigned long timeout = millis() + USB_XFER_TIMEOUT;
385  uint8_t tmpdata;
386  uint8_t rcode = hrSUCCESS;
387  uint8_t retry_count = 0;
388  uint16_t nak_count = 0;
389 
390  while (timeout > millis()) {
391  regWr(rHXFR, (token | ep)); //launch the transfer
393 
394  while (timeout > millis()) //wait for transfer completion
395  {
396  tmpdata = regRd(rHIRQ);
397 
398  if (tmpdata & bmHXFRDNIRQ) {
399  regWr(rHIRQ, bmHXFRDNIRQ); //clear the interrupt
400  rcode = 0x00;
401  break;
402  }//if( tmpdata & bmHXFRDNIRQ
403 
404  }//while ( millis() < timeout
405 
406  //if (rcode != 0x00) //exit if timeout
407  // return ( rcode);
408 
409  rcode = (regRd(rHRSL) & 0x0f); //analyze transfer result
410 
411  switch (rcode) {
412  case hrNAK:
413  nak_count++;
414  if (nak_limit && (nak_count == nak_limit))
415  return (rcode);
416  break;
417  case hrTIMEOUT:
418  retry_count++;
419  if (retry_count == USB_RETRY_LIMIT)
420  return (rcode);
421  break;
422  default:
423  return (rcode);
424  }//switch( rcode
425 
426  }//while( timeout > millis()
427  return ( rcode);
428 }
429 
430 /* USB main task. Performs enumeration/cleanup */
431 void USB::Task(void) //USB state machine
432 {
433  uint8_t rcode;
434  uint8_t tmpdata;
435  static unsigned long delay = 0;
436  //USB_DEVICE_DESCRIPTOR buf;
437  bool lowspeed = false;
438 
439  MAX3421E::Task();
440 
441  tmpdata = getVbusState();
442 
443  /* modify USB task state if Vbus changed */
444  switch (tmpdata) {
445  case SE1: //illegal state
446  usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL;
447  lowspeed = false;
448  break;
449  case SE0: //disconnected
450  if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED)
451  usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
452  lowspeed = false;
453  break;
454  case LSHOST:
455 
456  lowspeed = true;
457  //intentional fallthrough
458  case FSHOST: //attached
459  if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED) {
460  delay = millis() + USB_SETTLE_DELAY;
461  usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE;
462  }
463  break;
464  }// switch( tmpdata
465 
466  for (uint8_t i = 0; i < USB_NUMDEVICES; i++)
467  if (devConfig[i])
468  rcode = devConfig[i]->Poll();
469 
470  switch (usb_task_state) {
472  init();
473 
474  for (uint8_t i = 0; i < USB_NUMDEVICES; i++)
475  if (devConfig[i])
476  rcode = devConfig[i]->Release();
477 
478  usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE;
479  break;
480  case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: //just sit here
481  break;
482  case USB_DETACHED_SUBSTATE_ILLEGAL: //just sit here
483  break;
484  case USB_ATTACHED_SUBSTATE_SETTLE: //settle time for just attached device
485  if (delay < millis())
486  usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
487  break;
489  regWr(rHCTL, bmBUSRST); //issue bus reset
491  break;
493  if ((regRd(rHCTL) & bmBUSRST) == 0) {
494  tmpdata = regRd(rMODE) | bmSOFKAENAB; //start SOF generation
495  regWr(rMODE, tmpdata);
496  usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF;
497  //delay = millis() + 20; //20ms wait after reset per USB spec
498  }
499  break;
500  case USB_ATTACHED_SUBSTATE_WAIT_SOF: //todo: change check order
501  if (regRd(rHIRQ) & bmFRAMEIRQ) {
502  //when first SOF received _and_ 20ms has passed we can continue
503  /*
504  if (delay < millis()) //20ms passed
505  usb_task_state = USB_STATE_CONFIGURING;
506  */
507  usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET;
508  delay = millis() + 20;
509  }
510  break;
512  if (delay < millis()) usb_task_state = USB_STATE_CONFIGURING;
513  break;
515 
516  //Serial.print("\r\nConf.LS: ");
517  //Serial.println(lowspeed, HEX);
518 
519  rcode = Configuring(0, 0, lowspeed);
520 
521  if (rcode) {
523  usb_error = rcode;
524  usb_task_state = USB_STATE_ERROR;
525  }
526  } else
527  usb_task_state = USB_STATE_RUNNING;
528  break;
529  case USB_STATE_RUNNING:
530  break;
531  case USB_STATE_ERROR:
532  //MAX3421E::Init();
533  break;
534  } // switch( usb_task_state )
535 }
536 
537 uint8_t USB::DefaultAddressing(uint8_t parent, uint8_t port, bool lowspeed) {
538  //uint8_t buf[12];
539  uint8_t rcode;
540  UsbDevice *p0 = NULL, *p = NULL;
541 
542  // Get pointer to pseudo device with address 0 assigned
543  p0 = addrPool.GetUsbDevicePtr(0);
544 
545  if (!p0)
547 
548  if (!p0->epinfo)
550 
551  p0->lowspeed = (lowspeed) ? true : false;
552 
553  // Allocate new address according to device class
554  uint8_t bAddress = addrPool.AllocAddress(parent, false, port);
555 
556  if (!bAddress)
558 
559  p = addrPool.GetUsbDevicePtr(bAddress);
560 
561  if (!p)
563 
564  p->lowspeed = lowspeed;
565 
566  // Assign new address to the device
567  rcode = setAddr(0, 0, bAddress);
568 
569  if (rcode) {
570  addrPool.FreeAddress(bAddress);
571  bAddress = 0;
572  return rcode;
573  }
574  return 0;
575 };
576 
577 uint8_t USB::AttemptConfig(uint8_t driver, uint8_t parent, uint8_t port, bool lowspeed) {
578  uint8_t rcode = 0;
579  //printf("AttemptConfig: parent = %i, port = %i\r\n", parent, port);
580 
581  rcode = devConfig[driver]->ConfigureDevice(parent, port, lowspeed);
583  if (parent == 0) {
584  // Send a bus reset on the root interface.
585  regWr(rHCTL, bmBUSRST); //issue bus reset
586  delay(102); // delay 102ms, compensate for clock inaccuracy.
587  } else {
588  // reset parent port
589  devConfig[parent]->ResetHubPort(port);
590  }
591  }
592  rcode = devConfig[driver]->Init(parent, port, lowspeed);
593  return rcode;
594 }
595 
596 /*
597  * This is broken. We need to enumerate differently.
598  * It causes major problems with several devices if detected in an unexpected order.
599  *
600  *
601  * Oleg - I wouldn't do anything before the newly connected device is considered sane.
602  * i.e.(delays are not indicated for brevity):
603  * 1. reset
604  * 2. GetDevDescr();
605  * 3a. If ACK, continue with allocating address, addressing, etc.
606  * 3b. Else reset again, count resets, stop at some number (5?).
607  * 4. When max.number of resets is reached, toggle power/fail
608  * If desired, this could be modified by performing two resets with GetDevDescr() in the middle - however, from my experience, if a device answers to GDD()
609  * it doesn't need to be reset again
610  * New steps proposal:
611  * 1: get address pool instance. exit on fail
612  * 2: pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf). exit on fail.
613  * 3: bus reset, 100ms delay
614  * 4: set address
615  * 5: pUsb->setEpInfoEntry(bAddress, 1, epInfo), exit on fail
616  * 6: while (configurations) {
617  * for(each configuration) {
618  * for (each driver) {
619  * 6a: Ask device if it likes configuration. Returns 0 on OK.
620  * If successful, the driver configured device.
621  * The driver now owns the endpoints, and takes over managing them.
622  * The following will need codes:
623  * Everything went well, instance consumed, exit with success.
624  * Instance already in use, ignore it, try next driver.
625  * Not a supported device, ignore it, try next driver.
626  * Not a supported configuration for this device, ignore it, try next driver.
627  * Could not configure device, fatal, exit with fail.
628  * }
629  * }
630  * }
631  * 7: for(each driver) {
632  * 7a: Ask device if it knows this VID/PID. Acts exactly like 6a, but using VID/PID
633  * 8: if we get here, no driver likes the device plugged in, so exit failure.
634  *
635  */
636 uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
637  //uint8_t bAddress = 0;
638  //printf("Configuring: parent = %i, port = %i\r\n", parent, port);
639  uint8_t devConfigIndex;
640  uint8_t rcode = 0;
641  uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
642  UsbDevice *p = NULL;
643  EpInfo *oldep_ptr = NULL;
644  EpInfo epInfo;
645 
646  epInfo.epAddr = 0;
647  epInfo.maxPktSize = 8;
648  epInfo.epAttribs = 0;
649  epInfo.bmNakPower = USB_NAK_MAX_POWER;
650 
651  delay(2000);
652  AddressPool &addrPool = GetAddressPool();
653  // Get pointer to pseudo device with address 0 assigned
654  p = addrPool.GetUsbDevicePtr(0);
655  if (!p) {
656  //printf("Configuring error: USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL\r\n");
658  }
659 
660  // Save old pointer to EP_RECORD of address 0
661  oldep_ptr = p->epinfo;
662 
663  // Temporary assign new pointer to epInfo to p->epinfo in order to
664  // avoid toggle inconsistence
665 
666  p->epinfo = &epInfo;
667 
668  p->lowspeed = lowspeed;
669  // Get device descriptor
670  rcode = getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
671 
672  // Restore p->epinfo
673  p->epinfo = oldep_ptr;
674 
675  if (rcode) {
676  //printf("Configuring error: Can't get USB_DEVICE_DESCRIPTOR\r\n");
677  return rcode;
678  }
679 
680  // to-do?
681  // Allocate new address according to device class
682  //bAddress = addrPool.AllocAddress(parent, false, port);
683 
684  //if (!bAddress)
685  // return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
686 
687  uint16_t vid = (uint16_t)((USB_DEVICE_DESCRIPTOR*)buf)->idVendor;
688  uint16_t pid = (uint16_t)((USB_DEVICE_DESCRIPTOR*)buf)->idProduct;
689  uint8_t klass = ((USB_DEVICE_DESCRIPTOR*)buf)->bDeviceClass;
690 
691  // Attempt to configure if VID/PID or device class matches with a driver
692  for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) {
693  if (!devConfig[devConfigIndex]) continue; // no driver
694  if (devConfig[devConfigIndex]->GetAddress()) continue; // consumed
695  if (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass)) {
696  rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
698  break;
699  }
700  }
701 
702  if (devConfigIndex < USB_NUMDEVICES) {
703  return rcode;
704  }
705 
706 
707  // blindly attempt to configure
708  for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) {
709  if (!devConfig[devConfigIndex]) continue;
710  if (devConfig[devConfigIndex]->GetAddress()) continue; // consumed
711  if (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass)) continue; // If this is true it means it must have returned USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED above
712  rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
713 
714  //printf("ERROR ENUMERATING %2.2x\r\n", rcode);
716  // in case of an error dev_index should be reset to 0
717  // in order to start from the very beginning the
718  // next time the program gets here
719  //if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE)
720  // devConfigIndex = 0;
721  return rcode;
722  }
723  }
724  // if we get here that means that the device class is not supported by any of registered classes
725  rcode = DefaultAddressing(parent, port, lowspeed);
726 
727  return rcode;
728 }
729 
730 uint8_t USB::ReleaseDevice(uint8_t addr) {
731  if (!addr)
732  return 0;
733 
734  for (uint8_t i = 0; i < USB_NUMDEVICES; i++) {
735  if(!devConfig[i]) continue;
736  if (devConfig[i]->GetAddress() == addr)
737  return devConfig[i]->Release();
738  }
739  return 0;
740 }
741 
742 #if 1
743 //get device descriptor
744 
745 uint8_t USB::getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr) {
746  return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, NULL));
747 }
748 //get configuration descriptor
749 
750 uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
751  return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, NULL));
752 }
753 
754 uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser *p) {
755  const uint8_t bufSize = 64;
756  uint8_t buf[bufSize];
757 
758  uint8_t ret = getConfDescr(addr, ep, 8, conf, buf);
759 
760  if (ret)
761  return ret;
762 
763  uint16_t total = ((USB_CONFIGURATION_DESCRIPTOR*)buf)->wTotalLength;
764 
765  //USBTRACE2("\r\ntotal conf.size:", total);
766 
767  return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p));
768 }
769 
770 //get string descriptor
771 
772 uint8_t USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) {
773  return ( ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, NULL));
774 }
775 //set address
776 
777 uint8_t USB::setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr) {
778  return ( ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
779 }
780 //set configuration
781 
782 uint8_t USB::setConf(uint8_t addr, uint8_t ep, uint8_t conf_value) {
783  return ( ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
784 }
785 
786 #endif // defined(USB_METHODS_INLINE)
787