mass.c
17.2 KB
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
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2011-12-12 Yi Qiu first version
*/
#include <rtthread.h>
#include <drivers/usb_host.h>
#include "mass.h"
#ifdef RT_USBH_MSTORAGE
extern rt_err_t rt_udisk_run(struct uhintf* intf);
extern rt_err_t rt_udisk_stop(struct uhintf* intf);
static struct uclass_driver storage_driver;
/**
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
*
* @param intf the interface instance.
* @param max_lun the buffer to save max_lun.
*
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t _pipe_check(struct uhintf* intf, upipe_t pipe)
{
struct uinstance* device;
rt_err_t ret;
ustor_t stor;
int size = 0;
struct ustorage_csw csw;
if(intf == RT_NULL || pipe == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
/* get usb device instance from the interface instance */
device = intf->device;
/* get storage instance from the interface instance */
stor = (ustor_t)intf->user_data;
/* check pipe status */
if(pipe->status == UPIPE_STATUS_OK) return RT_EOK;
if(pipe->status == UPIPE_STATUS_ERROR)
{
rt_kprintf("pipe status error\n");
return -RT_EIO;
}
if(pipe->status == UPIPE_STATUS_STALL)
{
/* clear the pipe stall status */
ret = rt_usbh_clear_feature(device, pipe->ep.bEndpointAddress,
USB_FEATURE_ENDPOINT_HALT);
if(ret != RT_EOK) return ret;
}
rt_thread_delay(50);
rt_kprintf("pipes1 0x%x, 0x%x\n", stor->pipe_in, stor->pipe_out);
stor->pipe_in->status = UPIPE_STATUS_OK;
RT_DEBUG_LOG(RT_DEBUG_USB, ("clean storage in pipe stall\n"));
/* it should receive csw after clear the stall feature */
size = rt_usb_hcd_pipe_xfer(stor->pipe_in->inst->hcd,
stor->pipe_in, &csw, SIZEOF_CSW, 100);
if(size != SIZEOF_CSW)
{
rt_kprintf("receive the csw after stall failed\n");
return -RT_EIO;
}
return -RT_ERROR;
}
/**
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
*
* @param intf the interface instance.
* @param max_lun the buffer to save max_lun.
*
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usb_bulk_only_xfer(struct uhintf* intf,
ustorage_cbw_t cmd, rt_uint8_t* buffer, int timeout)
{
rt_size_t size;
rt_err_t ret;
upipe_t pipe;
struct ustorage_csw csw;
ustor_t stor;
RT_ASSERT(cmd != RT_NULL);
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
/* get storage instance from the interface instance */
stor = (ustor_t)intf->user_data;
do
{
/* send the cbw */
size = rt_usb_hcd_pipe_xfer(stor->pipe_out->inst->hcd, stor->pipe_out,
cmd, SIZEOF_CBW, timeout);
if(size != SIZEOF_CBW)
{
rt_kprintf("CBW size error\n");
return -RT_EIO;
}
if(cmd->xfer_len != 0)
{
pipe = (cmd->dflags == CBWFLAGS_DIR_IN) ? stor->pipe_in :
stor->pipe_out;
size = rt_usb_hcd_pipe_xfer(pipe->inst->hcd, pipe, (void*)buffer,
cmd->xfer_len, timeout);
if(size != cmd->xfer_len)
{
rt_kprintf("request size %d, transfer size %d\n",
cmd->xfer_len, size);
break;
}
}
/* receive the csw */
size = rt_usb_hcd_pipe_xfer(stor->pipe_in->inst->hcd, stor->pipe_in,
&csw, SIZEOF_CSW, timeout);
if(size != SIZEOF_CSW)
{
rt_kprintf("csw size error\n");
return -RT_EIO;
}
}while(0);
/* check in pipes status */
ret = _pipe_check(intf, stor->pipe_in);
if(ret != RT_EOK)
{
rt_kprintf("in pipe error\n");
return ret;
}
/* check out pipes status */
ret = _pipe_check(intf, stor->pipe_out);
if(ret != RT_EOK)
{
rt_kprintf("out pipe error\n");
return ret;
}
/* check csw status */
if(csw.signature != CSW_SIGNATURE || csw.tag != CBW_TAG_VALUE)
{
rt_kprintf("csw signature error\n");
return -RT_EIO;
}
if(csw.status != 0)
{
//rt_kprintf("csw status error:%d\n",csw.status);
return -RT_ERROR;
}
return RT_EOK;
}
/**
* This function will do USBREQ_GET_MAX_LUN request for the usb interface instance.
*
* @param intf the interface instance.
* @param max_lun the buffer to save max_lun.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_storage_get_max_lun(struct uhintf* intf, rt_uint8_t* max_lun)
{
struct uinstance* device;
struct urequest setup;
int timeout = USB_TIMEOUT_BASIC;
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
/* parameter check */
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_get_max_lun\n"));
/* get usb device instance from the interface instance */
device = intf->device;
/* construct the request */
setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
USB_REQ_TYPE_INTERFACE;
setup.bRequest = USBREQ_GET_MAX_LUN;
setup.wValue = intf->intf_desc->bInterfaceNumber;
setup.wIndex = 0;
setup.wLength = 1;
/* do control transfer request */
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
{
return -RT_EIO;
}
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, max_lun, 1, timeout) != 1)
{
return -RT_EIO;
}
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) != 0)
{
return -RT_EIO;
}
return RT_EOK;
}
/**
* This function will do USBREQ_MASS_STORAGE_RESET request for the usb interface instance.
*
* @param intf the interface instance.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_storage_reset(struct uhintf* intf)
{
struct urequest setup;
struct uinstance* device;
int timeout = USB_TIMEOUT_BASIC;
/* parameter check */
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_reset\n"));
/* get usb device instance from the interface instance */
device = intf->device;
/* construct the request */
setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
USB_REQ_TYPE_INTERFACE;
setup.bRequest = USBREQ_MASS_STORAGE_RESET;
setup.wIndex = intf->intf_desc->bInterfaceNumber;
setup.wLength = 0;
setup.wValue = 0;
if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
{
return -RT_EIO;
}
if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
{
return -RT_EIO;
}
return RT_EOK;
}
/**
* This function will execute SCSI_READ_10 command to read data from the usb device.
*
* @param intf the interface instance.
* @param buffer the data buffer to save read data
* @param sector the start sector address to read.
* @param sector the sector count to read.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_storage_read10(struct uhintf* intf, rt_uint8_t *buffer,
rt_uint32_t sector, rt_size_t count, int timeout)
{
struct ustorage_cbw cmd;
/* parameter check */
if(intf == RT_NULL)
{
rt_kprintf("interface is not available\n");
return -RT_EIO;
}
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_read10\n"));
/* construct the command block wrapper */
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
cmd.signature = CBW_SIGNATURE;
cmd.tag = CBW_TAG_VALUE;
cmd.xfer_len = SECTOR_SIZE * count;
cmd.dflags = CBWFLAGS_DIR_IN;
cmd.lun = 0;
cmd.cb_len = 10;
cmd.cb[0] = SCSI_READ_10;
cmd.cb[1] = 0;
cmd.cb[2] = (rt_uint8_t)(sector >> 24);
cmd.cb[3] = (rt_uint8_t)(sector >> 16);
cmd.cb[4] = (rt_uint8_t)(sector >> 8);
cmd.cb[5] = (rt_uint8_t)sector;
cmd.cb[6] = 0;
cmd.cb[7] = (count & 0xff00) >> 8;
cmd.cb[8] = (rt_uint8_t) count & 0xff;
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
}
/**
* This function will execute SCSI_WRITE_10 command to write data to the usb device.
*
* @param intf the interface instance.
* @param buffer the data buffer to save write data
* @param sector the start sector address to write.
* @param sector the sector count to write.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_storage_write10(struct uhintf* intf, rt_uint8_t *buffer,
rt_uint32_t sector, rt_size_t count, int timeout)
{
struct ustorage_cbw cmd;
/* parameter check */
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_write10\n"));
/* construct the command block wrapper */
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
cmd.signature = CBW_SIGNATURE;
cmd.tag = CBW_TAG_VALUE;
cmd.xfer_len = SECTOR_SIZE * count;
cmd.dflags = CBWFLAGS_DIR_OUT;
cmd.lun = 0;
cmd.cb_len = 10;
cmd.cb[0] = SCSI_WRITE_10;
cmd.cb[1] = 0;
cmd.cb[2] = (rt_uint8_t)(sector >> 24);
cmd.cb[3] = (rt_uint8_t)(sector >> 16);
cmd.cb[4] = (rt_uint8_t)(sector >> 8);
cmd.cb[5] = (rt_uint8_t)sector;
cmd.cb[6] = 0;
cmd.cb[7] = (count & 0xff00) >> 8;
cmd.cb[8] = (rt_uint8_t) count & 0xff;
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
}
/**
* This function will execute SCSI_REQUEST_SENSE command to get sense data.
*
* @param intf the interface instance.
* @param buffer the data buffer to save sense data
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_storage_request_sense(struct uhintf* intf, rt_uint8_t* buffer)
{
struct ustorage_cbw cmd;
int timeout = USB_TIMEOUT_LONG;
/* parameter check */
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_request_sense\n"));
/* construct the command block wrapper */
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
cmd.signature = CBW_SIGNATURE;
cmd.tag = CBW_TAG_VALUE;
cmd.xfer_len = 18;
cmd.dflags = CBWFLAGS_DIR_IN;
cmd.lun = 0;
cmd.cb_len = 6;
cmd.cb[0] = SCSI_REQUEST_SENSE;
cmd.cb[4] = 18;
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
}
/**
* This function will execute SCSI_TEST_UNIT_READY command to get unit ready status.
*
* @param intf the interface instance.
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_storage_test_unit_ready(struct uhintf* intf)
{
struct ustorage_cbw cmd;
int timeout = USB_TIMEOUT_LONG;
/* parameter check */
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_test_unit_ready\n"));
/* construct the command block wrapper */
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
cmd.signature = CBW_SIGNATURE;
cmd.tag = CBW_TAG_VALUE;
cmd.xfer_len = 0;
cmd.dflags = CBWFLAGS_DIR_OUT;
cmd.lun = 0;
cmd.cb_len = 12;
cmd.cb[0] = SCSI_TEST_UNIT_READY;
return rt_usb_bulk_only_xfer(intf, &cmd, RT_NULL, timeout);
}
/**
* This function will execute SCSI_INQUIRY_CMD command to get inquiry data.
*
* @param intf the interface instance.
* @param buffer the data buffer to save inquiry data
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_storage_inquiry(struct uhintf* intf, rt_uint8_t* buffer)
{
struct ustorage_cbw cmd;
int timeout = USB_TIMEOUT_LONG;
/* parameter check */
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_inquiry\n"));
/* construct the command block wrapper */
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
cmd.signature = CBW_SIGNATURE;
cmd.tag = CBW_TAG_VALUE;
cmd.xfer_len = 36;
cmd.dflags = CBWFLAGS_DIR_IN;
cmd.lun = 0;
cmd.cb_len = 6;//12
cmd.cb[0] = SCSI_INQUIRY_CMD;
cmd.cb[4] = 36;
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
}
/**
* This function will execute SCSI_READ_CAPACITY command to get capacity data.
*
* @param intf the interface instance.
* @param buffer the data buffer to save capacity data
*
* @return the error code, RT_EOK on successfully.
*/
rt_err_t rt_usbh_storage_get_capacity(struct uhintf* intf, rt_uint8_t* buffer)
{
struct ustorage_cbw cmd;
int timeout = USB_TIMEOUT_LONG;
/* parameter check */
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_get_capacity\n"));
/* construct the command block wrapper */
rt_memset(&cmd, 0, sizeof(struct ustorage_cbw));
cmd.signature = CBW_SIGNATURE;
cmd.tag = CBW_TAG_VALUE;
cmd.xfer_len = 8;
cmd.dflags = CBWFLAGS_DIR_IN;
cmd.lun = 0;
cmd.cb_len = 12;
cmd.cb[0] = SCSI_READ_CAPACITY;
return rt_usb_bulk_only_xfer(intf, &cmd, buffer, timeout);
}
/**
* This function will run mass storage class driver when usb device is detected
* and identified as a mass storage class device, it will continue to do the enumulate
* process.
*
* @param arg the argument.
*
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usbh_storage_enable(void* arg)
{
int i = 0;
rt_err_t ret;
ustor_t stor;
struct uhintf* intf = (struct uhintf*)arg;
/* parameter check */
if(intf == RT_NULL)
{
rt_kprintf("the interface is not available\n");
return -RT_EIO;
}
RT_DEBUG_LOG(RT_DEBUG_USB, ("subclass %d, protocal %d\n",
intf->intf_desc->bInterfaceSubClass,
intf->intf_desc->bInterfaceProtocol));
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_run\n"));
/* only support SCSI subclass and bulk only protocal */
stor = rt_malloc(sizeof(struct ustor));
RT_ASSERT(stor != RT_NULL);
/* initilize the data structure */
rt_memset(stor, 0, sizeof(struct ustor));
intf->user_data = (void*)stor;
for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
{
uep_desc_t ep_desc;
/* get endpoint descriptor from interface descriptor */
rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
if(ep_desc == RT_NULL)
{
rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
return -RT_ERROR;
}
/* the endpoint type of mass storage class should be BULK */
if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
continue;
/* allocate pipes according to the endpoint type */
if(ep_desc->bEndpointAddress & USB_DIR_IN)
{
/* alloc an in pipe for the storage instance */
stor->pipe_in = rt_usb_instance_find_pipe(intf->device,ep_desc->bEndpointAddress);
}
else
{
/* alloc an output pipe for the storage instance */
stor->pipe_out = rt_usb_instance_find_pipe(intf->device,ep_desc->bEndpointAddress);
}
}
/* check pipes infomation */
if(stor->pipe_in == RT_NULL || stor->pipe_out == RT_NULL)
{
rt_kprintf("pipe error, unsupported device\n");
return -RT_ERROR;
}
/* should implement as callback */
ret = rt_udisk_run(intf);
if(ret != RT_EOK) return ret;
return RT_EOK;
}
/**
* This function will be invoked when usb device plug out is detected and it would clean
* and release all mass storage class related resources.
*
* @param arg the argument.
*
* @return the error code, RT_EOK on successfully.
*/
static rt_err_t rt_usbh_storage_disable(void* arg)
{
ustor_t stor;
struct uhintf* intf = (struct uhintf*)arg;
/* parameter check */
RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->user_data != RT_NULL);
RT_ASSERT(intf->device != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_storage_stop\n"));
/* get storage instance from interface instance */
stor = (ustor_t)intf->user_data;
rt_udisk_stop(intf);
/* free storage instance */
if(stor != RT_NULL) rt_free(stor);
return RT_EOK;
}
/**
* This function will register mass storage class driver to the usb class driver manager.
* and it should be invoked in the usb system initialization.
*
* @return the error code, RT_EOK on successfully.
*/
ucd_t rt_usbh_class_driver_storage(void)
{
storage_driver.class_code = USB_CLASS_MASS_STORAGE;
storage_driver.enable = rt_usbh_storage_enable;
storage_driver.disable = rt_usbh_storage_disable;
return &storage_driver;
}
#endif