2727#include "nxos/drivers/aic.h"
2828#include "nxos/util.h"
2929
30+ #include <lego/usb.h>
31+
3032#include "usb_ch9.h"
3133#include "usb_common_desc.h"
3234
8688
8789#define USB_WVALUE_INDEX 0xFF
8890
91+ /**
92+ * Indices for string descriptors
93+ */
94+ enum {
95+ STRING_DESC_LANGID ,
96+ STRING_DESC_MFG ,
97+ STRING_DESC_PRODUCT ,
98+ };
99+
89100/* The following definitions are 'raw' USB setup packets. They are all
90101 * standard responses to various setup requests by the USB host. These
91102 * packets are all constant, and mostly boilerplate. Don't be too
@@ -110,8 +121,8 @@ static const pbdrv_usb_dev_desc_t pbdrv_usb_nxt_device_descriptor = {
110121 .idVendor = 0x0694 , /* Vendor ID : LEGO */
111122 .idProduct = 0x0002 , /* Product ID : NXT */
112123 .bcdDevice = 0x0200 , /* Product revision: 2.0.0. */
113- .iManufacturer = 1 ,
114- .iProduct = 2 ,
124+ .iManufacturer = STRING_DESC_MFG ,
125+ .iProduct = STRING_DESC_PRODUCT ,
115126 .iSerialNumber = 0 , // TODO: implement a serial number
116127 .bNumConfigurations = 1 ,
117128};
@@ -185,45 +196,6 @@ static const pbdrv_usb_nxt_conf_t pbdrv_usb_nxt_full_config = {
185196 },
186197};
187198
188- static const uint8_t pbdrv_usb_nxt_string_desc [] = {
189- 4 , USB_DESC_TYPE_STR , /* Descriptor length and type. */
190- 0x09 , 0x04 , /* Supported language ID (US English). */
191- };
192-
193- static const uint8_t pbdrv_usb_lego_str [] = {
194- 10 , USB_DESC_TYPE_STR ,
195- 'L' , 0 ,
196- 'E' , 0 ,
197- 'G' , 0 ,
198- 'O' , 0 ,
199- };
200-
201- static const uint8_t pbdrv_usb_nxt_str [] = {
202- 30 , USB_DESC_TYPE_STR ,
203- 'N' , 0 ,
204- 'X' , 0 ,
205- 'T' , 0 ,
206- ' ' , 0 ,
207- '+' , 0 ,
208- ' ' , 0 ,
209- 'P' , 0 ,
210- 'y' , 0 ,
211- 'b' , 0 ,
212- 'r' , 0 ,
213- 'i' , 0 ,
214- 'c' , 0 ,
215- 'k' , 0 ,
216- 's' , 0 ,
217- };
218-
219- /* Internal lookup table mapping string descriptors to their indices
220- * in the USB string descriptor table.
221- */
222- static const uint8_t * pbdrv_usb_nxt_strings [] = {
223- pbdrv_usb_lego_str ,
224- pbdrv_usb_nxt_str ,
225- };
226-
227199typedef enum {
228200 USB_UNINITIALIZED ,
229201 USB_READY ,
@@ -495,17 +467,30 @@ static void pbdrv_usb_handle_std_request(pbdrv_usb_nxt_setup_packet_t *packet) {
495467 break ;
496468
497469 case USB_DESC_TYPE_STR : /* String or language info. */
498- if ((packet -> value & USB_WVALUE_INDEX ) == 0 ) {
499- pbdrv_usb_nxt_write_data (0 , pbdrv_usb_nxt_string_desc ,
500- MIN (pbdrv_usb_nxt_string_desc [0 ], packet -> length ));
470+ {
471+ const void * desc = 0 ;
472+ switch (index ) {
473+ case STRING_DESC_LANGID :
474+ desc = & pbdrv_usb_str_desc_langid ;
475+ size = sizeof (pbdrv_usb_str_desc_langid .s );
476+ break ;
477+ case STRING_DESC_MFG :
478+ desc = & pbdrv_usb_str_desc_mfg ;
479+ size = sizeof (pbdrv_usb_str_desc_mfg .s );
480+ break ;
481+ case STRING_DESC_PRODUCT :
482+ desc = & pbdrv_usb_str_desc_prod ;
483+ size = sizeof (pbdrv_usb_str_desc_prod .s );
484+ break ;
485+ }
486+
487+ if (desc ) {
488+ pbdrv_usb_nxt_write_data (0 , desc , MIN (size , packet -> length ));
501489 } else {
502- /* The host wants a specific string. */
503- /* TODO: This should check if the requested string exists. */
504- pbdrv_usb_nxt_write_data (0 , pbdrv_usb_nxt_strings [index - 1 ],
505- MIN (pbdrv_usb_nxt_strings [index - 1 ][0 ],
506- packet -> length ));
490+ pbdrv_usb_nxt_send_stall (0 );
507491 }
508- break ;
492+ }
493+ break ;
509494
510495 case USB_DESC_TYPE_DEVICE_QUALIFIER : /* Device qualifier descriptor. */
511496 size = pbdrv_usb_nxt_dev_qualifier_desc .bLength ;
0 commit comments