Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/****************************************************************************
* drivers/usbdev/usbdev_storage.c
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Mass storage class device. Bulk-only with SCSI subclass.
*
* References:
* "Universal Serial Bus Mass Storage Class, Specification Overview,"
* Revision 1.2, USB Implementer's Forum, June 23, 2003.
*
* "Universal Serial Bus Mass Storage Class, Bulk-Only Transport,"
* Revision 1.0, USB Implementer's Forum, September 31, 1999.
*
* "SCSI Primary Commands - 3 (SPC-3)," American National Standard
* for Information Technology, May 4, 2005
*
* "SCSI Primary Commands - 4 (SPC-4)," American National Standard
* for Information Technology, July 19, 2008
*
* "SCSI Block Commands -2 (SBC-2)," American National Standard
* for Information Technology, November 13, 2004
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <queue.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/fs.h>
#include <nuttx/usb.h>
#include <nuttx/usb_bulk.h>
#include <nuttx/usbdev.h>
#include <nuttx/usbdev_trace.h>
#include "usbdev_storage.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/* The internal version of the class driver */
struct usbstrg_driver_s
{
struct usbdevclass_driver_s drvr;
FAR struct usbstrg_dev_s *dev;
};
/* This is what is allocated */
struct usbstrg_alloc_s
{
struct usbstrg_dev_s dev;
struct usbstrg_driver_s drvr;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Class Driver Support *****************************************************/
static void usbstrg_ep0incomplete(FAR struct usbdev_ep_s *ep,
FAR struct usbdev_req_s *req);
static struct usbdev_req_s *usbstrg_allocreq(FAR struct usbdev_ep_s *ep,
uint16 len);
static void usbstrg_freereq(FAR struct usbdev_ep_s *ep,
FAR struct usbdev_req_s *req);
static int usbstrg_mkstrdesc(ubyte id, struct usb_strdesc_s *strdesc);
#ifdef CONFIG_USBDEV_DUALSPEED
static sint16 usbstrg_mkcfgdesc(ubyte *buf, ubyte speed);
#else
static sint16 usbstrg_mkcfgdesc(ubyte *buf);
#endif
/* Class Driver Operations (most at interrupt level) ************************/
static int usbstrg_bind(FAR struct usbdev_s *dev,
FAR struct usbdevclass_driver_s *driver);
static void usbstrg_unbind(FAR struct usbdev_s *dev);
static int usbstrg_setup(FAR struct usbdev_s *dev,
FAR const struct usb_ctrlreq_s *ctrl);
static void usbstrg_disconnect(FAR struct usbdev_s *dev);
/* Initialization/Uninitialization ******************************************/
static void usbstrg_lununinitialize(struct usbstrg_lun_s *lun);
/****************************************************************************
* Private Data
****************************************************************************/
/* Driver operations ********************************************************/
static struct usbdevclass_driverops_s g_driverops =
{
usbstrg_bind, /* bind */
usbstrg_unbind, /* unbind */
usbstrg_setup, /* setup */
usbstrg_disconnect, /* disconnect */
NULL, /* suspend */
NULL /* resume */
};
/* Descriptors **************************************************************/
/* Device descriptor */
static const struct usb_devdesc_s g_devdesc =
{
USB_SIZEOF_DEVDESC, /* len */
USB_DESC_TYPE_DEVICE, /* type */
{LSBYTE(0x0200), MSBYTE(0x0200)}, /* usb */
USB_CLASS_PER_INTERFACE, /* class */
0, /* subclass */
0, /* protocol */
CONFIG_USBSTRG_EP0MAXPACKET, /* maxpacketsize */
{ LSBYTE(CONFIG_USBSTRG_VENDORID), /* vendor */
MSBYTE(CONFIG_USBSTRG_VENDORID) },
{ LSBYTE(CONFIG_USBSTRG_PRODUCTID), /* product */
MSBYTE(CONFIG_USBSTRG_PRODUCTID) },
{ LSBYTE(CONFIG_USBSTRG_VERSIONNO), /* device */
MSBYTE(CONFIG_USBSTRG_VERSIONNO) },
USBSTRG_MANUFACTURERSTRID, /* imfgr */
USBSTRG_PRODUCTSTRID, /* iproduct */
USBSTRG_SERIALSTRID, /* serno */
USBSTRG_NCONFIGS /* nconfigs */
};
/* Configuration descriptor */
static const struct usb_cfgdesc_s g_cfgdesc =
{
USB_SIZEOF_CFGDESC, /* len */
USB_DESC_TYPE_CONFIG, /* type */
{0, 0}, /* totallen -- to be provided */
USBSTRG_NINTERFACES, /* ninterfaces */
USBSTRG_CONFIGID, /* cfgvalue */
USBSTRG_CONFIGSTRID, /* icfg */
USB_CONFIG_ATTR_ONE|SELFPOWERED|REMOTEWAKEUP, /* attr */
(CONFIG_USBDEV_MAXPOWER + 1) / 2 /* mxpower */
};
/* Single interface descriptor */
static const struct usb_ifdesc_s g_ifdesc =
{
USB_SIZEOF_IFDESC, /* len */
USB_DESC_TYPE_INTERFACE, /* type */
0, /* ifno */
0, /* alt */
USBSTRG_NENDPOINTS, /* neps */
USB_CLASS_MASS_STORAGE, /* class */
SUBSTRG_SUBCLASS_SCSI, /* subclass */
USBSTRG_PROTO_BULKONLY, /* protocol */
USBSTRG_CONFIGSTRID /* iif */
};
/* Endpoint descriptors */
static const struct usb_epdesc_s g_fsepbulkoutdesc =
{
USB_SIZEOF_EPDESC, /* len */
USB_DESC_TYPE_ENDPOINT, /* type */
USBSTRG_EPOUTBULK_ADDR, /* addr */
USBSTRG_EPOUTBULK_ATTR, /* attr */
{ LSBYTE(USBSTRG_FSBULKMAXPACKET), /* maxpacket */
MSBYTE(USBSTRG_FSBULKMAXPACKET) },
0 /* interval */
};
static const struct usb_epdesc_s g_fsepbulkindesc =
{
USB_SIZEOF_EPDESC, /* len */
USB_DESC_TYPE_ENDPOINT, /* type */
USBSTRG_EPINBULK_ADDR, /* addr */
USBSTRG_EPINBULK_ATTR, /* attr */
{ LSBYTE(USBSTRG_FSBULKMAXPACKET), /* maxpacket */
MSBYTE(USBSTRG_FSBULKMAXPACKET) },
0 /* interval */
};
#ifdef CONFIG_USBDEV_DUALSPEED
static const struct usb_qualdesc_s g_qualdesc =
{
USB_SIZEOF_QUALDESC, /* len */
USB_DESC_TYPE_DEVICEQUALIFIER, /* type */
{LSBYTE(0x0200), MSBYTE(0x0200) }, /* USB */
USB_CLASS_PER_INTERFACE, /* class */
0, /* subclass */
0, /* protocol */
CONFIG_USBSTRG_EP0MAXPACKET, /* mxpacketsize */
USBSTRG_NCONFIGS, /* nconfigs */
0, /* reserved */
};
static const struct usb_epdesc_s g_hsepbulkoutdesc =
{
USB_SIZEOF_EPDESC, /* len */
USB_DESC_TYPE_ENDPOINT, /* type */
USBSTRG_EPOUTBULK_ADDR, /* addr */
USBSTRG_EPOUTBULK_ATTR, /* attr */
{ LSBYTE(USBSTRG_HSBULKMAXPACKET), /* maxpacket */
MSBYTE(USBSTRG_HSBULKMAXPACKET) },
0 /* interval */
};
static const struct usb_epdesc_s g_hsepbulkindesc =
{
USB_SIZEOF_EPDESC, /* len */
USB_DESC_TYPE_ENDPOINT, /* type */
USBSTRG_EPINBULK_ADDR, /* addr */
USBSTRG_EPINBULK_ATTR, /* attr */
{ LSBYTE(USBSTRG_HSBULKMAXPACKET), /* maxpacket */
MSBYTE(USBSTRG_HSBULKMAXPACKET) },
0 /* interval */
};
#endif
/****************************************************************************
* Public Data
****************************************************************************/
/* String *******************************************************************/
const char g_vendorstr[] = CONFIG_USBSTRG_VENDORSTR;
const char g_productstr[] = CONFIG_USBSTRG_PRODUCTSTR;
const char g_serialstr[] = CONFIG_USBSTRG_SERIALSTR;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Class Driver Support
****************************************************************************/
/****************************************************************************
* Name: usbstrg_ep0incomplete
*
* Description:
* Handle completion of EP0 control operations
*
****************************************************************************/
static void usbstrg_ep0incomplete(FAR struct usbdev_ep_s *ep,
FAR struct usbdev_req_s *req)
{
if (req->result || req->xfrd != req->len)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_REQRESULT), (uint16)-req->result);
}
}
/****************************************************************************
* Name: usbstrg_allocreq
*
* Description:
* Allocate a request instance along with its buffer
*
****************************************************************************/
static struct usbdev_req_s *usbstrg_allocreq(FAR struct usbdev_ep_s *ep, uint16 len)
{
FAR struct usbdev_req_s *req;
req = EP_ALLOCREQ(ep);
if (req != NULL)
{
req->len = len;
req->buf = EP_ALLOCBUFFER(ep, len);
if (!req->buf)
{
EP_FREEREQ(ep, req);
req = NULL;
}
}
return req;
}
/****************************************************************************
* Name: usbstrg_freereq
*
* Description:
* Free a request instance along with its buffer
*
****************************************************************************/
static void usbstrg_freereq(FAR struct usbdev_ep_s *ep, struct usbdev_req_s *req)
{
if (ep != NULL && req != NULL)
{
if (req->buf != NULL)
{
EP_FREEBUFFER(ep, req->buf);
}
EP_FREEREQ(ep, req);
}
}
/****************************************************************************
* Name: usbstrg_mkstrdesc
*
* Description:
* Construct a string descriptor
*
****************************************************************************/
static int usbstrg_mkstrdesc(ubyte id, struct usb_strdesc_s *strdesc)
{
const char *str;
int len;
int ndata;
int i;
switch (id)
{
case 0:
{
/* Descriptor 0 is the language id */
strdesc->len = 4;
strdesc->type = USB_DESC_TYPE_STRING;
strdesc->data[0] = LSBYTE(USBSTRG_STR_LANGUAGE);
strdesc->data[1] = MSBYTE(USBSTRG_STR_LANGUAGE);
return 4;
}
case USBSTRG_MANUFACTURERSTRID:
str = g_vendorstr;
break;
case USBSTRG_PRODUCTSTRID:
str = g_productstr;
break;
case USBSTRG_SERIALSTRID:
str = g_serialstr;
break;
case USBSTRG_CONFIGSTRID:
str = CONFIG_USBSTRG_CONFIGSTR;
break;
default:
return -EINVAL;
}
/* The string is utf16-le. The poor man's utf-8 to utf16-le
* conversion below will only handle 7-bit en-us ascii
*/
len = strlen(str);
for (i = 0, ndata = 0; i < len; i++, ndata += 2)
{
strdesc->data[ndata] = str[i];
strdesc->data[ndata+1] = 0;
}
strdesc->len = ndata+2;
strdesc->type = USB_DESC_TYPE_STRING;
return strdesc->len;
}
/****************************************************************************
* Name: usbstrg_mkcfgdesc
*
* Description:
* Construct the configuration descriptor
*
****************************************************************************/
#ifdef CONFIG_USBDEV_DUALSPEED
static sint16 usbstrg_mkcfgdesc(ubyte *buf, ubyte speed)
#else
static sint16 usbstrg_mkcfgdesc(ubyte *buf)
#endif
{
FAR struct usb_cfgdesc_s *cfgdesc = (struct usb_cfgdesc_s*)buf;
#ifdef CONFIG_USBDEV_DUALSPEED
FAR struct usb_epdesc_s *epdesc;
boolean hispeed = (speed == USB_SPEED_HIGH);
uint16 bulkmxpacket;
#endif
uint16 totallen;
/* This is the total length of the configuration (not necessarily the
* size that we will be sending now.
*/
totallen = USB_SIZEOF_CFGDESC + USB_SIZEOF_IFDESC + USBSTRG_NENDPOINTS * USB_SIZEOF_EPDESC;
/* Configuration descriptor -- Copy the canned descriptor and fill in the
* type (we'll also need to update the size below
*/
memcpy(cfgdesc, &g_cfgdesc, USB_SIZEOF_CFGDESC);
buf += USB_SIZEOF_CFGDESC;
/* Copy the canned interface descriptor */
memcpy(buf, &g_ifdesc, USB_SIZEOF_IFDESC);
buf += USB_SIZEOF_IFDESC;
/* Make the two endpoint configurations */
#ifdef CONFIG_USBDEV_DUALSPEED
/* Check for switches between high and full speed */
hispeed = (speed == USB_SPEED_HIGH);
if (type == USB_DESC_TYPE_OTHERSPEEDCONFIG)
{
hispeed = !hispeed;
}
bulkmxpacket = USBSTRG_BULKMAXPACKET(hispeed);
epdesc = USBSTRG_EPBULKINDESC(hispeed);
memcpy(buf, epdesc, USB_SIZEOF_EPDESC);
buf += USB_SIZEOF_EPDESC;
epdesc = USBSTRG_EPBULKOUTDESC(hispeed);
memcpy(buf, epdesc, USB_SIZEOF_EPDESC);
#else
memcpy(buf, &g_fsepbulkoutdesc, USB_SIZEOF_EPDESC);
buf += USB_SIZEOF_EPDESC;
memcpy(buf, &g_fsepbulkindesc, USB_SIZEOF_EPDESC);
#endif
/* Finally, fill in the total size of the configuration descriptor */
cfgdesc->totallen[0] = LSBYTE(totallen);
cfgdesc->totallen[1] = MSBYTE(totallen);
return totallen;
}
/****************************************************************************
* Class Driver Interfaces
****************************************************************************/
/****************************************************************************
* Name: usbstrg_bind
*
* Description:
* Invoked when the driver is bound to a USB device driver
*
****************************************************************************/
static int usbstrg_bind(FAR struct usbdev_s *dev, FAR struct usbdevclass_driver_s *driver)
{
FAR struct usbstrg_dev_s *priv = ((struct usbstrg_driver_s*)driver)->dev;
FAR struct usbstrg_req_s *reqcontainer;
irqstate_t flags;
int ret;
int i;
usbtrace(TRACE_CLASSBIND, 0);
/* Bind the structures */
priv->usbdev = dev;
dev->ep0->private = priv;
/* The configured EP0 size should match the reported EP0 size. We could
* easily adapt to the reported EP0 size, but then we could not use the
* const, canned descriptors.
*/
DEBUGASSERT(CONFIG_USBSTRG_EP0MAXPACKET == dev->ep0->maxpacket);
/* Preallocate control request */
priv->ctrlreq = usbstrg_allocreq(dev->ep0, USBSTRG_MXDESCLEN);
if (priv->ctrlreq == NULL)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_ALLOCCTRLREQ), 0);
ret = -ENOMEM;
goto errout;
}
priv->ctrlreq->callback = usbstrg_ep0incomplete;
/* Pre-allocate all endpoints... the endpoints will not be functional
* until the SET CONFIGURATION request is processed in usbstrg_setconfig.
* This is done here because there may be calls to malloc and the SET
* CONFIGURATION processing probably occurrs within interrupt handling
* logic where malloc calls will fail.
*/
/* Pre-allocate the IN bulk endpoint */
priv->epbulkin = DEV_ALLOCEP(dev, USBSTRG_EPINBULK_ADDR, TRUE, USB_EP_ATTR_XFER_BULK);
if (!priv->epbulkin)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_EPBULKINALLOCFAIL), 0);
ret = -ENODEV;
goto errout;
}
priv->epbulkin->private = priv;
/* Pre-allocate the OUT bulk endpoint */
priv->epbulkout = DEV_ALLOCEP(dev, USBSTRG_EPOUTBULK_ADDR, FALSE, USB_EP_ATTR_XFER_BULK);
if (!priv->epbulkout)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_EPBULKOUTALLOCFAIL), 0);
ret = -ENODEV;
goto errout;
}
priv->epbulkout->private = priv;
/* Pre-allocate read requests */
for (i = 0; i < CONFIG_USBSTRG_NRDREQS; i++)
{
reqcontainer = &priv->rdreqs[i];
reqcontainer->req = usbstrg_allocreq(priv->epbulkout, CONFIG_USBSTRG_BULKOUTREQLEN);
if (reqcontainer->req == NULL)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_RDALLOCREQ), (uint16)-ret);
ret = -ENOMEM;
goto errout;
}
reqcontainer->req->private = reqcontainer;
reqcontainer->req->callback = usbstrg_rdcomplete;
}
/* Pre-allocate write request containers and put in a free list */
for (i = 0; i < CONFIG_USBSTRG_NWRREQS; i++)
{
reqcontainer = &priv->wrreqs[i];
reqcontainer->req = usbstrg_allocreq(priv->epbulkin, CONFIG_USBSTRG_BULKINREQLEN);
if (reqcontainer->req == NULL)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_WRALLOCREQ), (uint16)-ret);
ret = -ENOMEM;
goto errout;
}
reqcontainer->req->private = reqcontainer;
reqcontainer->req->callback = usbstrg_wrcomplete;
flags = irqsave();
sq_addlast((sq_entry_t*)reqcontainer, &priv->wrreqlist);
irqrestore(flags);
}
/* Report if we are selfpowered */
#ifdef CONFIG_USBDEV_SELFPOWERED
DEV_SETSELFPOWERED(dev);
#endif
return OK;
errout:
usbstrg_unbind(dev);
return ret;
}
/****************************************************************************
* Name: usbstrg_unbind
*
* Description:
* Invoked when the driver is unbound from a USB device driver
*
****************************************************************************/
static void usbstrg_unbind(FAR struct usbdev_s *dev)
{
FAR struct usbstrg_dev_s *priv;
FAR struct usbstrg_req_s *reqcontainer;
irqstate_t flags;
int i;
usbtrace(TRACE_CLASSUNBIND, 0);
#ifdef CONFIG_DEBUG
if (!dev || !dev->ep0)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_UNBINDINVALIDARGS), 0);
return;
}
#endif
/* Extract reference to private data */
priv = (FAR struct usbstrg_dev_s *)dev->ep0->private;
#ifdef CONFIG_DEBUG
if (!priv)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_EP0NOTBOUND1), 0);
return;
}
#endif
/* The worker thread should have already been stopped by the
* driver un-initialize logic.
*/
DEBUGASSERT(priv->thstate == USBSTRG_STATE_TERMINATED);
/* Make sure that we are not already unbound */
if (priv != NULL)
{
/* Make sure that the endpoints have been unconfigured. If
* we were terminated gracefully, then the configuration should
* already have been reset. If not, then calling usbstrg_resetconfig
* should cause the endpoints to immediately terminate all
* transfers and return the requests to us (with result == -ESHUTDOWN)
*/
usbstrg_resetconfig(priv);
up_mdelay(50);
/* Free the bulk IN endpoint */
if (priv->epbulkin)
{
DEV_FREEEP(dev, priv->epbulkin);
priv->epbulkin = NULL;
}
/* Free the pre-allocated control request */
if (priv->ctrlreq != NULL)
{
usbstrg_freereq(dev->ep0, priv->ctrlreq);
priv->ctrlreq = NULL;
}
/* Free pre-allocated read requests (which should all have
* been returned to the free list at this time -- we don't check)
*/
for (i = 0; i < CONFIG_USBSTRG_NRDREQS; i++)
{
reqcontainer = &priv->rdreqs[i];
if (reqcontainer->req)
{
usbstrg_freereq(priv->epbulkout, reqcontainer->req);
reqcontainer->req = NULL;
}
}
/* Free the bulk OUT endpoint */
if (priv->epbulkout)
{
DEV_FREEEP(dev, priv->epbulkout);
priv->epbulkout = NULL;
}
/* Free write requests that are not in use (which should be all
* of them
*/
flags = irqsave();
while (!sq_empty(&priv->wrreqlist))
{
reqcontainer = (struct usbstrg_req_s *)sq_remfirst(&priv->wrreqlist);
if (reqcontainer->req != NULL)
{
usbstrg_freereq(priv->epbulkin, reqcontainer->req);
}
}
irqrestore(flags);
}
}
/****************************************************************************
* Name: usbstrg_setup
*
* Description:
* Invoked for ep0 control requests. This function probably executes
* in the context of an interrupt handler.
*
****************************************************************************/
static int usbstrg_setup(FAR struct usbdev_s *dev, const struct usb_ctrlreq_s *ctrl)
{
FAR struct usbstrg_dev_s *priv;
FAR struct usbdev_req_s *ctrlreq;
uint16 value;
uint16 index;
uint16 len;
int ret = -EOPNOTSUPP;
#ifdef CONFIG_DEBUG
if (!dev || !dev->ep0 || !ctrl)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_SETUPINVALIDARGS), 0);
return -EIO;
}
#endif
/* Extract reference to private data */
usbtrace(TRACE_CLASSSETUP, ctrl->req);
priv = (FAR struct usbstrg_dev_s *)dev->ep0->private;
#ifdef CONFIG_DEBUG
if (!priv || !priv->ctrlreq)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_EP0NOTBOUND2), 0);
return -ENODEV;
}
#endif
ctrlreq = priv->ctrlreq;
/* Extract the little-endian 16-bit values to host order */
value = GETUINT16(ctrl->value);
index = GETUINT16(ctrl->index);
len = GETUINT16(ctrl->len);
uvdbg("type=%02x req=%02x value=%04x index=%04x len=%04x\n",
ctrl->type, ctrl->req, value, index, len);
if ((ctrl->type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
{
/**********************************************************************
* Standard Requests
**********************************************************************/
switch (ctrl->req)
{
case USB_REQ_GETDESCRIPTOR:
{
/* The value field specifies the descriptor type in the MS byte and the
* descriptor index in the LS byte (order is little endian)
*/
switch (ctrl->value[1])
{
case USB_DESC_TYPE_DEVICE:
{
ret = USB_SIZEOF_DEVDESC;
memcpy(ctrlreq->buf, &g_devdesc, ret);
}
break;
#ifdef CONFIG_USBDEV_DUALSPEED
case USB_DESC_TYPE_DEVICEQUALIFIER:
{
ret = USB_SIZEOF_QUALDESC;
memcpy(ctrlreq->buf, &g_qualdesc, ret);
}
break;
case USB_DESC_TYPE_OTHERSPEEDCONFIG:
#endif /* CONFIG_USBDEV_DUALSPEED */
case USB_DESC_TYPE_CONFIG:
{
#ifdef CONFIG_USBDEV_DUALSPEED
ret = usbstrg_mkcfgdesc(ctrlreq->buf, dev->speed, len);
#else
ret = usbstrg_mkcfgdesc(ctrlreq->buf);
#endif
}
break;
case USB_DESC_TYPE_STRING:
{
/* index == language code. */
ret = usbstrg_mkstrdesc(ctrl->value[0], (struct usb_strdesc_s *)ctrlreq->buf);
}
break;
default:
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_GETUNKNOWNDESC), value);
}
break;
}
}
break;
case USB_REQ_SETCONFIGURATION:
{
if (ctrl->type == 0)
{
/* Signal the worker thread to instantiate the new configuration */
priv->theventset |= USBSTRG_EVENT_CFGCHANGE;
priv->thvalue = value;
pthread_cond_signal(&priv->cond);
/* Return here... the response will be provided later by the
* worker thread.
*/
return OK;
}
}
break;
case USB_REQ_GETCONFIGURATION:
{
if (ctrl->type == USB_DIR_IN)
{
ctrlreq->buf[0] = priv->config;
ret = 1;
}
}
break;
case USB_REQ_SETINTERFACE:
{
if (ctrl->type == USB_REQ_RECIPIENT_INTERFACE)
{
if (priv->config == USBSTRG_CONFIGID &&
index == USBSTRG_INTERFACEID &&
value == USBSTRG_ALTINTERFACEID)
{
/* Signal to instantiate the interface change */
priv->theventset |= USBSTRG_EVENT_IFCHANGE;
pthread_cond_signal(&priv->cond);
/* Return here... the response will be provided later by the
* worker thread.
*/
return OK;
}
}
}
break;
case USB_REQ_GETINTERFACE:
{
if (ctrl->type == (USB_DIR_IN|USB_REQ_RECIPIENT_INTERFACE) &&
priv->config == USBSTRG_CONFIGIDNONE)
{
if (index != USBSTRG_INTERFACEID)
{
ret = -EDOM;
}
else
{
ctrlreq->buf[0] = USBSTRG_ALTINTERFACEID;
ret = 1;
}
}
}
break;
default:
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_UNSUPPORTEDSTDREQ), ctrl->req);
break;
}
}
else
{
/**********************************************************************
* Bulk-Only Mass Storage Class Requests
**********************************************************************/
/* Verify that we are configured */
if (!priv->config)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_NOTCONFIGURED), 0);
return ret;
}
switch (ctrl->req)
{
case USBSTRG_REQ_MSRESET: /* Reset mass storage device and interface */
{
if (ctrl->type == USBSTRG_TYPE_SETUPOUT && value == 0 && len == 0)
{
/* Only one interface is supported */
if (index != USBSTRG_CONFIGID)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_MSRESETNDX), index);
ret = -EDOM;
}
else
{
/* Signal to stop the current operation and reinitialize state */
priv->theventset |= USBSTRG_EVENT_RESET;
pthread_cond_signal(&priv->cond);
/* Return here... the response will be provided later by the
* worker thread.
*/
return OK;
}
}
}
break;
case USBSTRG_REQ_GETMAXLUN: /* Return number LUNs supported */
{
if (ctrl->type != USBSTRG_TYPE_SETUPIN && value == 0)
{
/* Only one interface is supported */
if (index != USBSTRG_CONFIGID)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_GETMAXLUNNDX), index);
ret = -EDOM;
}
else
{
ctrlreq->buf[0] = priv->nluns - 1;
ret = 1;
}
}
}
break;
default:
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_BADREQUEST), index);
break;
}
}
/* Respond to the setup command if data was returned. On an error return
* value (ret < 0), the USB driver will stall EP0.
*/
if (ret >= 0)
{
ctrlreq->len = min(len, ret);
ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
ret = EP_SUBMIT(dev->ep0, ctrlreq);
if (ret < 0)
{
usbtrace(TRACE_CLSERROR(USBSTRG_TRACEERR_EPRESPQ), (uint16)-ret);
#if 0 /* Not necessary */
ctrlreq->result = OK;
usbstrg_ep0incomplete(dev->ep0, ctrlreq);
#endif
}
}
return ret;
}
/****************************************************************************
* Name: usbstrg_disconnect
*
* Description:
* Invoked after all transfers have been stopped, when the host is
* disconnected. This function is probably called from the context of an
* interrupt handler.
*
****************************************************************************/