Keyboard firmwares for Atmel AVR and Cortex-M
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

USBDevice.cpp 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #include "stdint.h"
  19. #include "USBEndpoints.h"
  20. #include "USBDevice.h"
  21. #include "USBDescriptor.h"
  22. //#define DEBUG
  23. /* Device status */
  24. #define DEVICE_STATUS_SELF_POWERED (1U<<0)
  25. #define DEVICE_STATUS_REMOTE_WAKEUP (1U<<1)
  26. /* Endpoint status */
  27. #define ENDPOINT_STATUS_HALT (1U<<0)
  28. /* Standard feature selectors */
  29. #define DEVICE_REMOTE_WAKEUP (1)
  30. #define ENDPOINT_HALT (0)
  31. /* Macro to convert wIndex endpoint number to physical endpoint number */
  32. #define WINDEX_TO_PHYSICAL(endpoint) (((endpoint & 0x0f) << 1) + \
  33. ((endpoint & 0x80) ? 1 : 0))
  34. bool USBDevice::requestGetDescriptor(void)
  35. {
  36. bool success = false;
  37. #ifdef DEBUG
  38. printf("get descr: type: %d\r\n", DESCRIPTOR_TYPE(transfer.setup.wValue));
  39. #endif
  40. switch (DESCRIPTOR_TYPE(transfer.setup.wValue))
  41. {
  42. case DEVICE_DESCRIPTOR:
  43. if (deviceDesc() != NULL)
  44. {
  45. if ((deviceDesc()[0] == DEVICE_DESCRIPTOR_LENGTH) \
  46. && (deviceDesc()[1] == DEVICE_DESCRIPTOR))
  47. {
  48. #ifdef DEBUG
  49. printf("device descr\r\n");
  50. #endif
  51. transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;
  52. transfer.ptr = deviceDesc();
  53. transfer.direction = DEVICE_TO_HOST;
  54. success = true;
  55. }
  56. }
  57. break;
  58. case CONFIGURATION_DESCRIPTOR:
  59. if (configurationDesc() != NULL)
  60. {
  61. if ((configurationDesc()[0] == CONFIGURATION_DESCRIPTOR_LENGTH) \
  62. && (configurationDesc()[1] == CONFIGURATION_DESCRIPTOR))
  63. {
  64. #ifdef DEBUG
  65. printf("conf descr request\r\n");
  66. #endif
  67. /* Get wTotalLength */
  68. transfer.remaining = configurationDesc()[2] \
  69. | (configurationDesc()[3] << 8);
  70. transfer.ptr = configurationDesc();
  71. transfer.direction = DEVICE_TO_HOST;
  72. success = true;
  73. }
  74. }
  75. break;
  76. case STRING_DESCRIPTOR:
  77. #ifdef DEBUG
  78. printf("str descriptor\r\n");
  79. #endif
  80. switch (DESCRIPTOR_INDEX(transfer.setup.wValue))
  81. {
  82. case STRING_OFFSET_LANGID:
  83. #ifdef DEBUG
  84. printf("1\r\n");
  85. #endif
  86. transfer.remaining = stringLangidDesc()[0];
  87. transfer.ptr = stringLangidDesc();
  88. transfer.direction = DEVICE_TO_HOST;
  89. success = true;
  90. break;
  91. case STRING_OFFSET_IMANUFACTURER:
  92. #ifdef DEBUG
  93. printf("2\r\n");
  94. #endif
  95. transfer.remaining = stringImanufacturerDesc()[0];
  96. transfer.ptr = stringImanufacturerDesc();
  97. transfer.direction = DEVICE_TO_HOST;
  98. success = true;
  99. break;
  100. case STRING_OFFSET_IPRODUCT:
  101. #ifdef DEBUG
  102. printf("3\r\n");
  103. #endif
  104. transfer.remaining = stringIproductDesc()[0];
  105. transfer.ptr = stringIproductDesc();
  106. transfer.direction = DEVICE_TO_HOST;
  107. success = true;
  108. break;
  109. case STRING_OFFSET_ISERIAL:
  110. #ifdef DEBUG
  111. printf("4\r\n");
  112. #endif
  113. transfer.remaining = stringIserialDesc()[0];
  114. transfer.ptr = stringIserialDesc();
  115. transfer.direction = DEVICE_TO_HOST;
  116. success = true;
  117. break;
  118. case STRING_OFFSET_ICONFIGURATION:
  119. #ifdef DEBUG
  120. printf("5\r\n");
  121. #endif
  122. transfer.remaining = stringIConfigurationDesc()[0];
  123. transfer.ptr = stringIConfigurationDesc();
  124. transfer.direction = DEVICE_TO_HOST;
  125. success = true;
  126. break;
  127. case STRING_OFFSET_IINTERFACE:
  128. #ifdef DEBUG
  129. printf("6\r\n");
  130. #endif
  131. transfer.remaining = stringIinterfaceDesc()[0];
  132. transfer.ptr = stringIinterfaceDesc();
  133. transfer.direction = DEVICE_TO_HOST;
  134. success = true;
  135. break;
  136. }
  137. break;
  138. case INTERFACE_DESCRIPTOR:
  139. #ifdef DEBUG
  140. printf("interface descr\r\n");
  141. #endif
  142. case ENDPOINT_DESCRIPTOR:
  143. #ifdef DEBUG
  144. printf("endpoint descr\r\n");
  145. #endif
  146. /* TODO: Support is optional, not implemented here */
  147. break;
  148. default:
  149. #ifdef DEBUG
  150. printf("ERROR\r\n");
  151. #endif
  152. break;
  153. }
  154. return success;
  155. }
  156. void USBDevice::decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet)
  157. {
  158. /* Fill in the elements of a SETUP_PACKET structure from raw data */
  159. packet->bmRequestType.dataTransferDirection = (data[0] & 0x80) >> 7;
  160. packet->bmRequestType.Type = (data[0] & 0x60) >> 5;
  161. packet->bmRequestType.Recipient = data[0] & 0x1f;
  162. packet->bRequest = data[1];
  163. packet->wValue = (data[2] | (uint16_t)data[3] << 8);
  164. packet->wIndex = (data[4] | (uint16_t)data[5] << 8);
  165. packet->wLength = (data[6] | (uint16_t)data[7] << 8);
  166. }
  167. bool USBDevice::controlOut(void)
  168. {
  169. /* Control transfer data OUT stage */
  170. uint8_t buffer[MAX_PACKET_SIZE_EP0];
  171. uint32_t packetSize;
  172. /* Check we should be transferring data OUT */
  173. if (transfer.direction != HOST_TO_DEVICE)
  174. {
  175. #if defined(TARGET_KL25Z) | defined(TARGET_KL43Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D5M) | defined(TARGET_K64F) | defined(TARGET_K22F) | defined(TARGET_TEENSY3_1)
  176. /*
  177. * We seem to have a pending device-to-host transfer. The host must have
  178. * sent a new control request without waiting for us to finish processing
  179. * the previous one. This appears to happen when we're connected to certain
  180. * USB 3.0 host chip set. Do a zeor-length send to tell the host we're not
  181. * ready for the new request - that'll make it resend - and then just
  182. * pretend we were successful here so that the pending transfer can finish.
  183. */
  184. uint8_t buf[1] = { 0 };
  185. EP0write(buf, 0);
  186. /* execute our pending ttransfer */
  187. controlIn();
  188. /* indicate success */
  189. return true;
  190. #else
  191. /* for other platforms, count on the HAL to handle this case */
  192. return false;
  193. #endif
  194. }
  195. /* Read from endpoint */
  196. packetSize = EP0getReadResult(buffer);
  197. /* Check if transfer size is valid */
  198. if (packetSize > transfer.remaining)
  199. {
  200. /* Too big */
  201. return false;
  202. }
  203. /* Update transfer */
  204. transfer.ptr += packetSize;
  205. transfer.remaining -= packetSize;
  206. /* Check if transfer has completed */
  207. if (transfer.remaining == 0)
  208. {
  209. /* Transfer completed */
  210. if (transfer.notify)
  211. {
  212. /* Notify class layer. */
  213. USBCallback_requestCompleted(buffer, packetSize);
  214. transfer.notify = false;
  215. }
  216. /* Status stage */
  217. EP0write(NULL, 0);
  218. }
  219. else
  220. {
  221. EP0read();
  222. }
  223. return true;
  224. }
  225. bool USBDevice::controlIn(void)
  226. {
  227. /* Control transfer data IN stage */
  228. uint32_t packetSize;
  229. /* Check if transfer has completed (status stage transactions */
  230. /* also have transfer.remaining == 0) */
  231. if (transfer.remaining == 0)
  232. {
  233. if (transfer.zlp)
  234. {
  235. /* Send zero length packet */
  236. EP0write(NULL, 0);
  237. transfer.zlp = false;
  238. }
  239. /* Transfer completed */
  240. if (transfer.notify)
  241. {
  242. /* Notify class layer. */
  243. USBCallback_requestCompleted(NULL, 0);
  244. transfer.notify = false;
  245. }
  246. EP0read();
  247. EP0readStage();
  248. /* Completed */
  249. return true;
  250. }
  251. /* Check we should be transferring data IN */
  252. if (transfer.direction != DEVICE_TO_HOST)
  253. {
  254. return false;
  255. }
  256. packetSize = transfer.remaining;
  257. if (packetSize > MAX_PACKET_SIZE_EP0)
  258. {
  259. packetSize = MAX_PACKET_SIZE_EP0;
  260. }
  261. /* Write to endpoint */
  262. EP0write(transfer.ptr, packetSize);
  263. /* Update transfer */
  264. transfer.ptr += packetSize;
  265. transfer.remaining -= packetSize;
  266. return true;
  267. }
  268. bool USBDevice::requestSetAddress(void)
  269. {
  270. /* Set the device address */
  271. setAddress(transfer.setup.wValue);
  272. if (transfer.setup.wValue == 0)
  273. {
  274. device.state = DEFAULT;
  275. }
  276. else
  277. {
  278. device.state = ADDRESS;
  279. }
  280. return true;
  281. }
  282. bool USBDevice::requestSetConfiguration(void)
  283. {
  284. device.configuration = transfer.setup.wValue;
  285. /* Set the device configuration */
  286. if (device.configuration == 0)
  287. {
  288. /* Not configured */
  289. unconfigureDevice();
  290. device.state = ADDRESS;
  291. }
  292. else
  293. {
  294. if (USBCallback_setConfiguration(device.configuration))
  295. {
  296. /* Valid configuration */
  297. configureDevice();
  298. device.state = CONFIGURED;
  299. }
  300. else
  301. {
  302. return false;
  303. }
  304. }
  305. return true;
  306. }
  307. bool USBDevice::requestGetConfiguration(void)
  308. {
  309. /* Send the device configuration */
  310. transfer.ptr = &device.configuration;
  311. transfer.remaining = sizeof(device.configuration);
  312. transfer.direction = DEVICE_TO_HOST;
  313. return true;
  314. }
  315. bool USBDevice::requestGetInterface(void)
  316. {
  317. /* Return the selected alternate setting for an interface */
  318. if (device.state != CONFIGURED)
  319. {
  320. return false;
  321. }
  322. /* Send the alternate setting */
  323. transfer.setup.wIndex = currentInterface;
  324. transfer.ptr = &currentAlternate;
  325. transfer.remaining = sizeof(currentAlternate);
  326. transfer.direction = DEVICE_TO_HOST;
  327. return true;
  328. }
  329. bool USBDevice::requestSetInterface(void)
  330. {
  331. bool success = false;
  332. if(USBCallback_setInterface(transfer.setup.wIndex, transfer.setup.wValue))
  333. {
  334. success = true;
  335. currentInterface = transfer.setup.wIndex;
  336. currentAlternate = transfer.setup.wValue;
  337. }
  338. return success;
  339. }
  340. bool USBDevice::requestSetFeature()
  341. {
  342. bool success = false;
  343. if (device.state != CONFIGURED)
  344. {
  345. /* Endpoint or interface must be zero */
  346. if (transfer.setup.wIndex != 0)
  347. {
  348. return false;
  349. }
  350. }
  351. switch (transfer.setup.bmRequestType.Recipient)
  352. {
  353. case DEVICE_RECIPIENT:
  354. /* TODO: Remote wakeup feature not supported */
  355. break;
  356. case ENDPOINT_RECIPIENT:
  357. if (transfer.setup.wValue == ENDPOINT_HALT)
  358. {
  359. /* TODO: We should check that the endpoint number is valid */
  360. stallEndpoint(
  361. WINDEX_TO_PHYSICAL(transfer.setup.wIndex));
  362. success = true;
  363. }
  364. break;
  365. default:
  366. break;
  367. }
  368. return success;
  369. }
  370. bool USBDevice::requestClearFeature()
  371. {
  372. bool success = false;
  373. if (device.state != CONFIGURED)
  374. {
  375. /* Endpoint or interface must be zero */
  376. if (transfer.setup.wIndex != 0)
  377. {
  378. return false;
  379. }
  380. }
  381. switch (transfer.setup.bmRequestType.Recipient)
  382. {
  383. case DEVICE_RECIPIENT:
  384. /* TODO: Remote wakeup feature not supported */
  385. break;
  386. case ENDPOINT_RECIPIENT:
  387. /* TODO: We should check that the endpoint number is valid */
  388. if (transfer.setup.wValue == ENDPOINT_HALT)
  389. {
  390. unstallEndpoint( WINDEX_TO_PHYSICAL(transfer.setup.wIndex));
  391. success = true;
  392. }
  393. break;
  394. default:
  395. break;
  396. }
  397. return success;
  398. }
  399. bool USBDevice::requestGetStatus(void)
  400. {
  401. static uint16_t status;
  402. bool success = false;
  403. if (device.state != CONFIGURED)
  404. {
  405. /* Endpoint or interface must be zero */
  406. if (transfer.setup.wIndex != 0)
  407. {
  408. return false;
  409. }
  410. }
  411. switch (transfer.setup.bmRequestType.Recipient)
  412. {
  413. case DEVICE_RECIPIENT:
  414. /* TODO: Currently only supports self powered devices */
  415. status = DEVICE_STATUS_SELF_POWERED;
  416. success = true;
  417. break;
  418. case INTERFACE_RECIPIENT:
  419. status = 0;
  420. success = true;
  421. break;
  422. case ENDPOINT_RECIPIENT:
  423. /* TODO: We should check that the endpoint number is valid */
  424. if (getEndpointStallState(
  425. WINDEX_TO_PHYSICAL(transfer.setup.wIndex)))
  426. {
  427. status = ENDPOINT_STATUS_HALT;
  428. }
  429. else
  430. {
  431. status = 0;
  432. }
  433. success = true;
  434. break;
  435. default:
  436. break;
  437. }
  438. if (success)
  439. {
  440. /* Send the status */
  441. transfer.ptr = (uint8_t *)&status; /* Assumes little endian */
  442. transfer.remaining = sizeof(status);
  443. transfer.direction = DEVICE_TO_HOST;
  444. }
  445. return success;
  446. }
  447. bool USBDevice::requestSetup(void)
  448. {
  449. bool success = false;
  450. /* Process standard requests */
  451. if ((transfer.setup.bmRequestType.Type == STANDARD_TYPE))
  452. {
  453. switch (transfer.setup.bRequest)
  454. {
  455. case GET_STATUS:
  456. success = requestGetStatus();
  457. break;
  458. case CLEAR_FEATURE:
  459. success = requestClearFeature();
  460. break;
  461. case SET_FEATURE:
  462. success = requestSetFeature();
  463. break;
  464. case SET_ADDRESS:
  465. success = requestSetAddress();
  466. break;
  467. case GET_DESCRIPTOR:
  468. success = requestGetDescriptor();
  469. break;
  470. case SET_DESCRIPTOR:
  471. /* TODO: Support is optional, not implemented here */
  472. success = false;
  473. break;
  474. case GET_CONFIGURATION:
  475. success = requestGetConfiguration();
  476. break;
  477. case SET_CONFIGURATION:
  478. success = requestSetConfiguration();
  479. break;
  480. case GET_INTERFACE:
  481. success = requestGetInterface();
  482. break;
  483. case SET_INTERFACE:
  484. success = requestSetInterface();
  485. break;
  486. default:
  487. break;
  488. }
  489. }
  490. return success;
  491. }
  492. bool USBDevice::controlSetup(void)
  493. {
  494. bool success = false;
  495. /* Control transfer setup stage */
  496. uint8_t buffer[MAX_PACKET_SIZE_EP0];
  497. EP0setup(buffer);
  498. /* Initialise control transfer state */
  499. decodeSetupPacket(buffer, &transfer.setup);
  500. transfer.ptr = NULL;
  501. transfer.remaining = 0;
  502. transfer.direction = 0;
  503. transfer.zlp = false;
  504. transfer.notify = false;
  505. #ifdef DEBUG
  506. printf("dataTransferDirection: %d\r\nType: %d\r\nRecipient: %d\r\nbRequest: %d\r\nwValue: %d\r\nwIndex: %d\r\nwLength: %d\r\n",transfer.setup.bmRequestType.dataTransferDirection,
  507. transfer.setup.bmRequestType.Type,
  508. transfer.setup.bmRequestType.Recipient,
  509. transfer.setup.bRequest,
  510. transfer.setup.wValue,
  511. transfer.setup.wIndex,
  512. transfer.setup.wLength);
  513. #endif
  514. /* Class / vendor specific */
  515. success = USBCallback_request();
  516. if (!success)
  517. {
  518. /* Standard requests */
  519. if (!requestSetup())
  520. {
  521. #ifdef DEBUG
  522. printf("fail!!!!\r\n");
  523. #endif
  524. return false;
  525. }
  526. }
  527. /* Check transfer size and direction */
  528. if (transfer.setup.wLength>0)
  529. {
  530. if (transfer.setup.bmRequestType.dataTransferDirection \
  531. == DEVICE_TO_HOST)
  532. {
  533. /* IN data stage is required */
  534. if (transfer.direction != DEVICE_TO_HOST)
  535. {
  536. return false;
  537. }
  538. /* Transfer must be less than or equal to the size */
  539. /* requested by the host */
  540. if (transfer.remaining > transfer.setup.wLength)
  541. {
  542. transfer.remaining = transfer.setup.wLength;
  543. }
  544. }
  545. else
  546. {
  547. /* OUT data stage is required */
  548. if (transfer.direction != HOST_TO_DEVICE)
  549. {
  550. return false;
  551. }
  552. /* Transfer must be equal to the size requested by the host */
  553. if (transfer.remaining != transfer.setup.wLength)
  554. {
  555. return false;
  556. }
  557. }
  558. }
  559. else
  560. {
  561. /* No data stage; transfer size must be zero */
  562. if (transfer.remaining != 0)
  563. {
  564. return false;
  565. }
  566. }
  567. /* Data or status stage if applicable */
  568. if (transfer.setup.wLength>0)
  569. {
  570. if (transfer.setup.bmRequestType.dataTransferDirection \
  571. == DEVICE_TO_HOST)
  572. {
  573. /* Check if we'll need to send a zero length packet at */
  574. /* the end of this transfer */
  575. if (transfer.setup.wLength > transfer.remaining)
  576. {
  577. /* Device wishes to transfer less than host requested */
  578. if ((transfer.remaining % MAX_PACKET_SIZE_EP0) == 0)
  579. {
  580. /* Transfer is a multiple of EP0 max packet size */
  581. transfer.zlp = true;
  582. }
  583. }
  584. /* IN stage */
  585. controlIn();
  586. }
  587. else
  588. {
  589. /* OUT stage */
  590. EP0read();
  591. }
  592. }
  593. else
  594. {
  595. /* Status stage */
  596. EP0write(NULL, 0);
  597. }
  598. return true;
  599. }
  600. void USBDevice::busReset(void)
  601. {
  602. device.state = DEFAULT;
  603. device.configuration = 0;
  604. device.suspended = false;
  605. /* Call class / vendor specific busReset function */
  606. USBCallback_busReset();
  607. }
  608. void USBDevice::EP0setupCallback(void)
  609. {
  610. /* Endpoint 0 setup event */
  611. if (!controlSetup())
  612. {
  613. /* Protocol stall */
  614. EP0stall();
  615. }
  616. /* Return true if an OUT data stage is expected */
  617. }
  618. void USBDevice::EP0out(void)
  619. {
  620. /* Endpoint 0 OUT data event */
  621. if (!controlOut())
  622. {
  623. /* Protocol stall; this will stall both endpoints */
  624. EP0stall();
  625. }
  626. }
  627. void USBDevice::EP0in(void)
  628. {
  629. #ifdef DEBUG
  630. printf("EP0IN\r\n");
  631. #endif
  632. /* Endpoint 0 IN data event */
  633. if (!controlIn())
  634. {
  635. /* Protocol stall; this will stall both endpoints */
  636. EP0stall();
  637. }
  638. }
  639. bool USBDevice::configured(void)
  640. {
  641. /* Returns true if device is in the CONFIGURED state */
  642. return (device.state == CONFIGURED);
  643. }
  644. void USBDevice::connect(bool blocking)
  645. {
  646. /* Connect device */
  647. USBHAL::connect();
  648. if (blocking) {
  649. /* Block if not configured */
  650. while (!configured());
  651. }
  652. }
  653. void USBDevice::disconnect(void)
  654. {
  655. /* Disconnect device */
  656. USBHAL::disconnect();
  657. /* Set initial device state */
  658. device.state = POWERED;
  659. device.configuration = 0;
  660. device.suspended = false;
  661. }
  662. CONTROL_TRANSFER * USBDevice::getTransferPtr(void)
  663. {
  664. return &transfer;
  665. }
  666. bool USBDevice::addEndpoint(uint8_t endpoint, uint32_t maxPacket)
  667. {
  668. return realiseEndpoint(endpoint, maxPacket, 0);
  669. }
  670. bool USBDevice::addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket)
  671. {
  672. /* For interrupt endpoints only */
  673. return realiseEndpoint(endpoint, maxPacket, RATE_FEEDBACK_MODE);
  674. }
  675. uint8_t * USBDevice::findDescriptor(uint8_t descriptorType)
  676. {
  677. /* Find a descriptor within the list of descriptors */
  678. /* following a configuration descriptor. */
  679. uint16_t wTotalLength;
  680. uint8_t *ptr;
  681. if (configurationDesc() == NULL)
  682. {
  683. return NULL;
  684. }
  685. /* Check this is a configuration descriptor */
  686. if ((configurationDesc()[0] != CONFIGURATION_DESCRIPTOR_LENGTH) \
  687. || (configurationDesc()[1] != CONFIGURATION_DESCRIPTOR))
  688. {
  689. return NULL;
  690. }
  691. wTotalLength = configurationDesc()[2] | (configurationDesc()[3] << 8);
  692. /* Check there are some more descriptors to follow */
  693. if (wTotalLength <= (CONFIGURATION_DESCRIPTOR_LENGTH+2))
  694. /* +2 is for bLength and bDescriptorType of next descriptor */
  695. {
  696. return NULL;
  697. }
  698. /* Start at first descriptor after the configuration descriptor */
  699. ptr = &(configurationDesc()[CONFIGURATION_DESCRIPTOR_LENGTH]);
  700. do {
  701. if (ptr[1] /* bDescriptorType */ == descriptorType)
  702. {
  703. /* Found */
  704. return ptr;
  705. }
  706. /* Skip to next descriptor */
  707. ptr += ptr[0]; /* bLength */
  708. } while (ptr < (configurationDesc() + wTotalLength));
  709. /* Reached end of the descriptors - not found */
  710. return NULL;
  711. }
  712. void USBDevice::connectStateChanged(unsigned int connected)
  713. {
  714. }
  715. void USBDevice::suspendStateChanged(unsigned int suspended)
  716. {
  717. }
  718. USBDevice::USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release){
  719. VENDOR_ID = vendor_id;
  720. PRODUCT_ID = product_id;
  721. PRODUCT_RELEASE = product_release;
  722. /* Set initial device state */
  723. device.state = POWERED;
  724. device.configuration = 0;
  725. device.suspended = false;
  726. };
  727. bool USBDevice::readStart(uint8_t endpoint, uint32_t maxSize)
  728. {
  729. return endpointRead(endpoint, maxSize) == EP_PENDING;
  730. }
  731. bool USBDevice::write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize)
  732. {
  733. EP_STATUS result;
  734. if (size > maxSize)
  735. {
  736. return false;
  737. }
  738. if(!configured()) {
  739. return false;
  740. }
  741. /* Send report */
  742. result = endpointWrite(endpoint, buffer, size);
  743. if (result != EP_PENDING)
  744. {
  745. return false;
  746. }
  747. /* Wait for completion */
  748. do {
  749. result = endpointWriteResult(endpoint);
  750. } while ((result == EP_PENDING) && configured());
  751. return (result == EP_COMPLETED);
  752. }
  753. bool USBDevice::writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize)
  754. {
  755. EP_STATUS result;
  756. if (size > maxSize)
  757. {
  758. return false;
  759. }
  760. if(!configured()) {
  761. return false;
  762. }
  763. /* Send report */
  764. result = endpointWrite(endpoint, buffer, size);
  765. if (result != EP_PENDING)
  766. {
  767. return false;
  768. }
  769. result = endpointWriteResult(endpoint);
  770. return (result == EP_COMPLETED);
  771. }
  772. bool USBDevice::readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize)
  773. {
  774. EP_STATUS result;
  775. if(!configured()) {
  776. return false;
  777. }
  778. /* Wait for completion */
  779. do {
  780. result = endpointReadResult(endpoint, buffer, size);
  781. } while ((result == EP_PENDING) && configured());
  782. return (result == EP_COMPLETED);
  783. }
  784. bool USBDevice::readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize)
  785. {
  786. EP_STATUS result;
  787. if(!configured()) {
  788. return false;
  789. }
  790. result = endpointReadResult(endpoint, buffer, size);
  791. return (result == EP_COMPLETED);
  792. }
  793. uint8_t * USBDevice::deviceDesc() {
  794. static uint8_t deviceDescriptor[] = {
  795. DEVICE_DESCRIPTOR_LENGTH, /* bLength */
  796. DEVICE_DESCRIPTOR, /* bDescriptorType */
  797. LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */
  798. MSB(USB_VERSION_2_0), /* bcdUSB (MSB) */
  799. 0x00, /* bDeviceClass */
  800. 0x00, /* bDeviceSubClass */
  801. 0x00, /* bDeviceprotocol */
  802. MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */
  803. (uint8_t)(LSB(VENDOR_ID)), /* idVendor (LSB) */
  804. (uint8_t)(MSB(VENDOR_ID)), /* idVendor (MSB) */
  805. (uint8_t)(LSB(PRODUCT_ID)), /* idProduct (LSB) */
  806. (uint8_t)(MSB(PRODUCT_ID)), /* idProduct (MSB) */
  807. (uint8_t)(LSB(PRODUCT_RELEASE)), /* bcdDevice (LSB) */
  808. (uint8_t)(MSB(PRODUCT_RELEASE)), /* bcdDevice (MSB) */
  809. STRING_OFFSET_IMANUFACTURER, /* iManufacturer */
  810. STRING_OFFSET_IPRODUCT, /* iProduct */
  811. STRING_OFFSET_ISERIAL, /* iSerialNumber */
  812. 0x01 /* bNumConfigurations */
  813. };
  814. return deviceDescriptor;
  815. }
  816. uint8_t * USBDevice::stringLangidDesc() {
  817. static uint8_t stringLangidDescriptor[] = {
  818. 0x04, /*bLength*/
  819. STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
  820. 0x09,0x04, /*bString Lang ID - 0x0409 - English*/
  821. };
  822. return stringLangidDescriptor;
  823. }
  824. uint8_t * USBDevice::stringImanufacturerDesc() {
  825. static uint8_t stringImanufacturerDescriptor[] = {
  826. 0x12, /*bLength*/
  827. STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
  828. 'm',0,'b',0,'e',0,'d',0,'.',0,'o',0,'r',0,'g',0, /*bString iManufacturer - mbed.org*/
  829. };
  830. return stringImanufacturerDescriptor;
  831. }
  832. uint8_t * USBDevice::stringIserialDesc() {
  833. static uint8_t stringIserialDescriptor[] = {
  834. 0x16, /*bLength*/
  835. STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
  836. '0',0,'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,'7',0,'8',0,'9',0, /*bString iSerial - 0123456789*/
  837. };
  838. return stringIserialDescriptor;
  839. }
  840. uint8_t * USBDevice::stringIConfigurationDesc() {
  841. static uint8_t stringIconfigurationDescriptor[] = {
  842. 0x06, /*bLength*/
  843. STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
  844. '0',0,'1',0, /*bString iConfiguration - 01*/
  845. };
  846. return stringIconfigurationDescriptor;
  847. }
  848. uint8_t * USBDevice::stringIinterfaceDesc() {
  849. static uint8_t stringIinterfaceDescriptor[] = {
  850. 0x08, /*bLength*/
  851. STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
  852. 'U',0,'S',0,'B',0, /*bString iInterface - USB*/
  853. };
  854. return stringIinterfaceDescriptor;
  855. }
  856. uint8_t * USBDevice::stringIproductDesc() {
  857. static uint8_t stringIproductDescriptor[] = {
  858. 0x16, /*bLength*/
  859. STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
  860. 'U',0,'S',0,'B',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 /*bString iProduct - USB DEVICE*/
  861. };
  862. return stringIproductDescriptor;
  863. }