xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_rockusb.c (revision 9d5c314b35defd1c678227deba13c3b4dfd4a0f3)
1 /*
2  * Copyright 2017 Rockchip Electronics Co., Ltd
3  * Frank Wang <frank.wang@rock-chips.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <asm/io.h>
9 #include <asm/arch/boot_mode.h>
10 #include <asm/arch/chip_info.h>
11 #include <write_keybox.h>
12 #include <linux/mtd/mtd.h>
13 
14 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION
15 #include <asm/arch/vendor.h>
16 #endif
17 
18 #include <rockusb.h>
19 
20 #define ROCKUSB_INTERFACE_CLASS	0xff
21 #define ROCKUSB_INTERFACE_SUB_CLASS	0x06
22 #define ROCKUSB_INTERFACE_PROTOCOL	0x05
23 
24 #define ROCKCHIP_FLASH_BLOCK_SIZE	1024
25 #define ROCKCHIP_FLASH_PAGE_SIZE	4
26 
27 static struct usb_interface_descriptor rkusb_intf_desc = {
28 	.bLength		= USB_DT_INTERFACE_SIZE,
29 	.bDescriptorType	= USB_DT_INTERFACE,
30 	.bInterfaceNumber	= 0x00,
31 	.bAlternateSetting	= 0x00,
32 	.bNumEndpoints		= 0x02,
33 	.bInterfaceClass	= ROCKUSB_INTERFACE_CLASS,
34 	.bInterfaceSubClass	= ROCKUSB_INTERFACE_SUB_CLASS,
35 	.bInterfaceProtocol	= ROCKUSB_INTERFACE_PROTOCOL,
36 };
37 
38 static struct usb_descriptor_header *rkusb_fs_function[] = {
39 	(struct usb_descriptor_header *)&rkusb_intf_desc,
40 	(struct usb_descriptor_header *)&fsg_fs_bulk_in_desc,
41 	(struct usb_descriptor_header *)&fsg_fs_bulk_out_desc,
42 	NULL,
43 };
44 
45 static struct usb_descriptor_header *rkusb_hs_function[] = {
46 	(struct usb_descriptor_header *)&rkusb_intf_desc,
47 	(struct usb_descriptor_header *)&fsg_hs_bulk_in_desc,
48 	(struct usb_descriptor_header *)&fsg_hs_bulk_out_desc,
49 	NULL,
50 };
51 
52 static struct usb_descriptor_header *rkusb_ss_function[] = {
53 	(struct usb_descriptor_header *)&rkusb_intf_desc,
54 	(struct usb_descriptor_header *)&fsg_ss_bulk_in_desc,
55 	(struct usb_descriptor_header *)&fsg_ss_bulk_in_comp_desc,
56 	(struct usb_descriptor_header *)&fsg_ss_bulk_out_desc,
57 	(struct usb_descriptor_header *)&fsg_ss_bulk_out_comp_desc,
58 	NULL,
59 };
60 
61 struct rk_flash_info {
62 	u32	flash_size;
63 	u16	block_size;
64 	u8	page_size;
65 	u8	ecc_bits;
66 	u8	access_time;
67 	u8	manufacturer;
68 	u8	flash_mask;
69 } __packed;
70 
71 static int rkusb_rst_code; /* The subcode in reset command (0xFF) */
72 
73 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
74 {
75 	if (IS_RKUSB_UMS_DNL(name)) {
76 		/* Fix to Rockchip's VID and PID */
77 		dev->idVendor  = __constant_cpu_to_le16(0x2207);
78 		dev->idProduct = __constant_cpu_to_le16(CONFIG_ROCKUSB_G_DNL_PID);
79 
80 		/* Enumerate as a loader device */
81 #if defined(CONFIG_SUPPORT_USBPLUG)
82 		dev->bcdUSB = cpu_to_le16(0x0200);
83 #else
84 		dev->bcdUSB = cpu_to_le16(0x0201);
85 #endif
86 	} else if (!strncmp(name, "usb_dnl_fastboot", 16)) {
87 		/* Fix to Google's VID and PID */
88 		dev->idVendor  = __constant_cpu_to_le16(0x18d1);
89 		dev->idProduct = __constant_cpu_to_le16(0xd00d);
90 	} else if (!strncmp(name, "usb_dnl_dfu", 11)) {
91 		/* Fix to Rockchip's VID and PID for DFU */
92 		dev->idVendor  = cpu_to_le16(0x2207);
93 		dev->idProduct = cpu_to_le16(0x0107);
94 	}
95 
96 	return 0;
97 }
98 
99 __maybe_unused
100 static inline void dump_cbw(struct fsg_bulk_cb_wrap *cbw)
101 {
102 	assert(!cbw);
103 
104 	debug("%s:\n", __func__);
105 	debug("Signature %x\n", cbw->Signature);
106 	debug("Tag %x\n", cbw->Tag);
107 	debug("DataTransferLength %x\n", cbw->DataTransferLength);
108 	debug("Flags %x\n", cbw->Flags);
109 	debug("LUN %x\n", cbw->Lun);
110 	debug("Length %x\n", cbw->Length);
111 	debug("OptionCode %x\n", cbw->CDB[0]);
112 	debug("SubCode %x\n", cbw->CDB[1]);
113 	debug("SectorAddr %x\n", get_unaligned_be32(&cbw->CDB[2]));
114 	debug("BlkSectors %x\n\n", get_unaligned_be16(&cbw->CDB[7]));
115 }
116 
117 static int rkusb_check_lun(struct fsg_common *common)
118 {
119 	struct fsg_lun *curlun;
120 
121 	/* Check the LUN */
122 	if (common->lun >= 0 && common->lun < common->nluns) {
123 		curlun = &common->luns[common->lun];
124 		if (common->cmnd[0] != SC_REQUEST_SENSE) {
125 			curlun->sense_data = SS_NO_SENSE;
126 			curlun->info_valid = 0;
127 		}
128 	} else {
129 		curlun = NULL;
130 		common->bad_lun_okay = 0;
131 
132 		/*
133 		 * INQUIRY and REQUEST SENSE commands are explicitly allowed
134 		 * to use unsupported LUNs; all others may not.
135 		 */
136 		if (common->cmnd[0] != SC_INQUIRY &&
137 		    common->cmnd[0] != SC_REQUEST_SENSE) {
138 			debug("unsupported LUN %d\n", common->lun);
139 			return -EINVAL;
140 		}
141 	}
142 
143 	return 0;
144 }
145 
146 static void __do_reset(struct usb_ep *ep, struct usb_request *req)
147 {
148 	u32 boot_flag = BOOT_NORMAL;
149 
150 	if (rkusb_rst_code == 0x03)
151 		boot_flag = BOOT_BROM_DOWNLOAD;
152 
153 	rkusb_rst_code = 0; /* restore to default */
154 	writel(boot_flag, (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG);
155 
156 	do_reset(NULL, 0, 0, NULL);
157 }
158 
159 static int rkusb_do_reset(struct fsg_common *common,
160 			  struct fsg_buffhd *bh)
161 {
162 	common->data_size_from_cmnd = common->cmnd[4];
163 	common->residue = 0;
164 	bh->inreq->complete = __do_reset;
165 	bh->state = BUF_STATE_EMPTY;
166 
167 	rkusb_rst_code = !common->cmnd[1] ? 0xff : common->cmnd[1];
168 	return 0;
169 }
170 
171 static int rkusb_do_test_unit_ready(struct fsg_common *common,
172 				    struct fsg_buffhd *bh)
173 {
174 	common->residue = 0x06 << 24; /* Max block xfer support from host */
175 	common->data_dir = DATA_DIR_NONE;
176 	bh->state = BUF_STATE_EMPTY;
177 
178 	return 0;
179 }
180 
181 static int rkusb_do_read_flash_id(struct fsg_common *common,
182 				  struct fsg_buffhd *bh)
183 {
184 	u8 *buf = (u8 *)bh->buf;
185 	u32 len = 5;
186 	enum if_type type = ums[common->lun].block_dev.if_type;
187 
188 	if (type == IF_TYPE_MMC)
189 		memcpy((void *)&buf[0], "EMMC ", 5);
190 	else if (type == IF_TYPE_RKNAND)
191 		memcpy((void *)&buf[0], "NAND ", 5);
192 	else
193 		memcpy((void *)&buf[0], "UNKN ", 5); /* unknown */
194 
195 	/* Set data xfer size */
196 	common->residue = common->data_size_from_cmnd = len;
197 	common->data_size = len;
198 
199 	return len;
200 }
201 
202 static int rkusb_do_test_bad_block(struct fsg_common *common,
203 				   struct fsg_buffhd *bh)
204 {
205 	u8 *buf = (u8 *)bh->buf;
206 	u32 len = 64;
207 
208 	memset((void *)&buf[0], 0, len);
209 
210 	/* Set data xfer size */
211 	common->residue = common->data_size_from_cmnd = len;
212 	common->data_size = len;
213 
214 	return len;
215 }
216 
217 static int rkusb_do_read_flash_info(struct fsg_common *common,
218 				    struct fsg_buffhd *bh)
219 {
220 	struct blk_desc *desc = &ums[common->lun].block_dev;
221 	u8 *buf = (u8 *)bh->buf;
222 	u32 len = sizeof(struct rk_flash_info);
223 	struct rk_flash_info finfo = {
224 		.block_size = ROCKCHIP_FLASH_BLOCK_SIZE,
225 		.ecc_bits = 0,
226 		.page_size = ROCKCHIP_FLASH_PAGE_SIZE,
227 		.access_time = 40,
228 		.manufacturer = 0,
229 		.flash_mask = 0
230 	};
231 
232 	finfo.flash_size = (u32)desc->lba;
233 
234 	if (desc->if_type == IF_TYPE_MTD &&
235 	    (desc->devnum == BLK_MTD_NAND ||
236 	    desc->devnum == BLK_MTD_SPI_NAND)) {
237 		struct mtd_info *mtd = (struct mtd_info *)desc->bdev->priv;
238 
239 		if (mtd) {
240 			finfo.block_size = mtd->erasesize >> 9;
241 			finfo.page_size = mtd->writesize >> 9;
242 		}
243 	}
244 
245 	if (desc->if_type == IF_TYPE_MTD && desc->devnum == BLK_MTD_SPI_NOR) {
246 		/* RV1126/RK3308 mtd spinor keep the former upgrade mode */
247 #if !defined(CONFIG_ROCKCHIP_RV1126) && !defined(CONFIG_ROCKCHIP_RK3308)
248 		finfo.block_size = 0x100; /* Aligned to 128KB */
249 #else
250 		finfo.block_size = ROCKCHIP_FLASH_BLOCK_SIZE;
251 #endif
252 	}
253 
254 	debug("Flash info: block_size= %x page_size= %x\n", finfo.block_size,
255 	      finfo.page_size);
256 
257 	if (finfo.flash_size)
258 		finfo.flash_mask = 1;
259 
260 	memset((void *)&buf[0], 0, len);
261 	memcpy((void *)&buf[0], (void *)&finfo, len);
262 
263 	/* Set data xfer size */
264 	common->residue = common->data_size_from_cmnd = len;
265         /* legacy upgrade_tool does not set correct transfer size */
266 	common->data_size = len;
267 
268 	return len;
269 }
270 
271 static int rkusb_do_get_chip_info(struct fsg_common *common,
272 				  struct fsg_buffhd *bh)
273 {
274 	u8 *buf = (u8 *)bh->buf;
275 	u32 len = common->data_size;
276 	u32 chip_info[4];
277 
278 	memset((void *)chip_info, 0, sizeof(chip_info));
279 	rockchip_rockusb_get_chip_info(chip_info);
280 
281 	memset((void *)&buf[0], 0, len);
282 	memcpy((void *)&buf[0], (void *)chip_info, len);
283 
284 	/* Set data xfer size */
285 	common->residue = common->data_size_from_cmnd = len;
286 
287 	return len;
288 }
289 
290 static int rkusb_do_lba_erase(struct fsg_common *common,
291 			      struct fsg_buffhd *bh)
292 {
293 	struct fsg_lun *curlun = &common->luns[common->lun];
294 	u32 lba, amount;
295 	loff_t file_offset;
296 	int rc;
297 
298 	lba = get_unaligned_be32(&common->cmnd[2]);
299 	if (lba >= curlun->num_sectors) {
300 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
301 		rc = -EINVAL;
302 		goto out;
303 	}
304 
305 	file_offset = ((loff_t) lba) << 9;
306 	amount = get_unaligned_be16(&common->cmnd[7]) << 9;
307 	if (unlikely(amount == 0)) {
308 		curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
309 		rc = -EIO;
310 		goto out;
311 	}
312 
313 	/* Perform the erase */
314 	rc = ums[common->lun].erase_sector(&ums[common->lun],
315 			       file_offset / SECTOR_SIZE,
316 			       amount / SECTOR_SIZE);
317 	if (!rc) {
318 		curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
319 		rc = -EIO;
320 	}
321 
322 out:
323 	common->data_dir = DATA_DIR_NONE;
324 	bh->state = BUF_STATE_EMPTY;
325 
326 	return rc;
327 }
328 
329 static int rkusb_do_erase_force(struct fsg_common *common,
330 				struct fsg_buffhd *bh)
331 {
332 	struct blk_desc *desc = &ums[common->lun].block_dev;
333 	struct fsg_lun *curlun = &common->luns[common->lun];
334 	u16 block_size = ROCKCHIP_FLASH_BLOCK_SIZE;
335 	u32 lba, amount;
336 	loff_t file_offset;
337 	int rc;
338 
339 	lba = get_unaligned_be32(&common->cmnd[2]);
340 	if (lba >= curlun->num_sectors) {
341 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
342 		rc = -EINVAL;
343 		goto out;
344 	}
345 
346 	if (desc->if_type == IF_TYPE_MTD &&
347 	    (desc->devnum == BLK_MTD_NAND ||
348 	    desc->devnum == BLK_MTD_SPI_NAND)) {
349 		struct mtd_info *mtd = (struct mtd_info *)desc->bdev->priv;
350 
351 		if (mtd)
352 			block_size = mtd->erasesize >> 9;
353 	}
354 
355 	file_offset = ((loff_t)lba) * block_size;
356 	amount = get_unaligned_be16(&common->cmnd[7]) * block_size;
357 
358 	debug("%s lba= %x, nsec= %x\n", __func__, lba,
359 	      (u32)get_unaligned_be16(&common->cmnd[7]));
360 
361 	if (unlikely(amount == 0)) {
362 		curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
363 		rc = -EIO;
364 		goto out;
365 	}
366 
367 	/* Perform the erase */
368 	rc = ums[common->lun].erase_sector(&ums[common->lun],
369 					   file_offset,
370 					   amount);
371 	if (!rc) {
372 		curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
373 		rc = -EIO;
374 	}
375 
376 out:
377 	common->data_dir = DATA_DIR_NONE;
378 	bh->state = BUF_STATE_EMPTY;
379 
380 	return rc;
381 }
382 
383 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION
384 static int rkusb_do_vs_write(struct fsg_common *common)
385 {
386 	struct fsg_lun		*curlun = &common->luns[common->lun];
387 	u16			type = get_unaligned_be16(&common->cmnd[4]);
388 	struct vendor_item	*vhead;
389 	struct fsg_buffhd	*bh;
390 	void			*data;
391 	int			rc;
392 
393 	if (common->data_size >= (u32)65536) {
394 		/* _MUST_ small than 64K */
395 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
396 		return -EINVAL;
397 	}
398 
399 	common->residue         = common->data_size;
400 	common->usb_amount_left = common->data_size;
401 
402 	/* Carry out the file writes */
403 	if (unlikely(common->data_size == 0))
404 		return -EIO; /* No data to write */
405 
406 	for (;;) {
407 		if (common->usb_amount_left > 0) {
408 			/* Wait for the next buffer to become available */
409 			bh = common->next_buffhd_to_fill;
410 			if (bh->state != BUF_STATE_EMPTY)
411 				goto wait;
412 
413 			/* Request the next buffer */
414 			common->usb_amount_left      -= common->data_size;
415 			bh->outreq->length	     = common->data_size;
416 			bh->bulk_out_intended_length = common->data_size;
417 			bh->outreq->short_not_ok     = 1;
418 
419 			START_TRANSFER_OR(common, bulk_out, bh->outreq,
420 					  &bh->outreq_busy, &bh->state)
421 				/*
422 				 * Don't know what to do if
423 				 * common->fsg is NULL
424 				 */
425 				return -EIO;
426 			common->next_buffhd_to_fill = bh->next;
427 		} else {
428 			/* Then, wait for the data to become available */
429 			bh = common->next_buffhd_to_drain;
430 			if (bh->state != BUF_STATE_FULL)
431 				goto wait;
432 
433 			common->next_buffhd_to_drain = bh->next;
434 			bh->state = BUF_STATE_EMPTY;
435 
436 			/* Did something go wrong with the transfer? */
437 			if (bh->outreq->status != 0) {
438 				curlun->sense_data = SS_COMMUNICATION_FAILURE;
439 				curlun->info_valid = 1;
440 				break;
441 			}
442 
443 			/* Perform the write */
444 			vhead = (struct vendor_item *)bh->buf;
445 			data  = bh->buf + sizeof(struct vendor_item);
446 
447 			if (!type) {
448 				/* Vendor storage */
449 				rc = vendor_storage_write(vhead->id,
450 							  (char __user *)data,
451 							  vhead->size);
452 				if (rc < 0) {
453 					curlun->sense_data = SS_WRITE_ERROR;
454 					return -EIO;
455 				}
456 			} else {
457 				/* RPMB */
458 				rc =
459 				write_keybox_to_secure_storage((u8 *)data,
460 							       vhead->size);
461 				if (rc < 0) {
462 					curlun->sense_data = SS_WRITE_ERROR;
463 					return -EIO;
464 				}
465 			}
466 
467 			common->residue -= common->data_size;
468 
469 			/* Did the host decide to stop early? */
470 			if (bh->outreq->actual != bh->outreq->length)
471 				common->short_packet_received = 1;
472 			break; /* Command done */
473 		}
474 wait:
475 		/* Wait for something to happen */
476 		rc = sleep_thread(common);
477 		if (rc)
478 			return rc;
479 	}
480 
481 	return -EIO; /* No default reply */
482 }
483 
484 static int rkusb_do_vs_read(struct fsg_common *common)
485 {
486 	struct fsg_lun		*curlun = &common->luns[common->lun];
487 	u16			type = get_unaligned_be16(&common->cmnd[4]);
488 	struct vendor_item	*vhead;
489 	struct fsg_buffhd	*bh;
490 	void			*data;
491 	int			rc;
492 
493 	if (common->data_size >= (u32)65536) {
494 		/* _MUST_ small than 64K */
495 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
496 		return -EINVAL;
497 	}
498 
499 	common->residue         = common->data_size;
500 	common->usb_amount_left = common->data_size;
501 
502 	/* Carry out the file reads */
503 	if (unlikely(common->data_size == 0))
504 		return -EIO; /* No default reply */
505 
506 	for (;;) {
507 		/* Wait for the next buffer to become available */
508 		bh = common->next_buffhd_to_fill;
509 		while (bh->state != BUF_STATE_EMPTY) {
510 			rc = sleep_thread(common);
511 			if (rc)
512 				return rc;
513 		}
514 
515 		memset(bh->buf, 0, FSG_BUFLEN);
516 		vhead = (struct vendor_item *)bh->buf;
517 		data  = bh->buf + sizeof(struct vendor_item);
518 		vhead->id = get_unaligned_be16(&common->cmnd[2]);
519 
520 		if (!type) {
521 			/* Vendor storage */
522 			rc = vendor_storage_read(vhead->id,
523 						 (char __user *)data,
524 						 common->data_size);
525 			if (!rc) {
526 				curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
527 				return -EIO;
528 			}
529 			vhead->size = rc;
530 		} else {
531 			/* RPMB */
532 			rc =
533 			read_raw_data_from_secure_storage((u8 *)data,
534 							  common->data_size);
535 			if (!rc) {
536 				curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
537 				return -EIO;
538 			}
539 			vhead->size = rc;
540 		}
541 
542 		common->residue   -= common->data_size;
543 		bh->inreq->length = common->data_size;
544 		bh->state         = BUF_STATE_FULL;
545 
546 		break; /* No more left to read */
547 	}
548 
549 	return -EIO; /* No default reply */
550 }
551 #endif
552 
553 static int rkusb_do_read_capacity(struct fsg_common *common,
554 				  struct fsg_buffhd *bh)
555 {
556 	u8 *buf = (u8 *)bh->buf;
557 	u32 len = common->data_size;
558 	enum if_type type = ums[common->lun].block_dev.if_type;
559 	int devnum = ums[common->lun].block_dev.devnum;
560 
561 	/*
562 	 * bit[0]: Direct LBA, 0: Disabled;
563 	 * bit[1]: Vendor Storage API, 0: Disabed (default);
564 	 * bit[2]: First 4M Access, 0: Disabled;
565 	 * bit[3]: Read LBA On, 0: Disabed (default);
566 	 * bit[4]: New Vendor Storage API, 0: Disabed;
567 	 * bit[5:63}: Reserved.
568 	 */
569 	memset((void *)&buf[0], 0, len);
570 	if (type == IF_TYPE_MMC || type == IF_TYPE_SD)
571 		buf[0] = BIT(0) | BIT(2) | BIT(4);
572 	else
573 		buf[0] = BIT(0) | BIT(4);
574 
575 	if (type == IF_TYPE_MTD &&
576 	    (devnum == BLK_MTD_NAND ||
577 	    devnum == BLK_MTD_SPI_NAND))
578 		buf[0] |= (1 << 6);
579 
580 #if !defined(CONFIG_ROCKCHIP_RV1126) && !defined(CONFIG_ROCKCHIP_RK3308)
581 	if (type == IF_TYPE_MTD && devnum == BLK_MTD_SPI_NOR)
582 		buf[0] |= (1 << 6);
583 #endif
584 
585 #if defined(CONFIG_ROCKCHIP_RK3568)
586 	buf[1] = BIT(0);
587 #endif
588 	/* Set data xfer size */
589 	common->residue = len;
590 	common->data_size_from_cmnd = len;
591 
592 	return len;
593 }
594 
595 static void rkusb_fixup_cbwcb(struct fsg_common *common,
596 			      struct fsg_buffhd *bh)
597 {
598 	struct usb_request      *req = bh->outreq;
599 	struct fsg_bulk_cb_wrap *cbw = req->buf;
600 
601 	/* FIXME cbw.DataTransferLength was not set by Upgrade Tool */
602 	common->data_size = le32_to_cpu(cbw->DataTransferLength);
603 	if (common->data_size == 0) {
604 		common->data_size =
605 		get_unaligned_be16(&common->cmnd[7]) << 9;
606 		printf("Trasfer Length NOT set, please use new version tool\n");
607 		debug("%s %d, cmnd1 %x\n", __func__,
608 		      get_unaligned_be16(&common->cmnd[7]),
609 		      get_unaligned_be16(&common->cmnd[1]));
610 	}
611 	if (cbw->Flags & USB_BULK_IN_FLAG)
612 		common->data_dir = DATA_DIR_TO_HOST;
613 	else
614 		common->data_dir = DATA_DIR_FROM_HOST;
615 
616 	/* Not support */
617 	common->cmnd[1] = 0;
618 }
619 
620 static int rkusb_cmd_process(struct fsg_common *common,
621 			     struct fsg_buffhd *bh, int *reply)
622 {
623 	struct usb_request	*req = bh->outreq;
624 	struct fsg_bulk_cb_wrap	*cbw = req->buf;
625 	int rc;
626 
627 	dump_cbw(cbw);
628 
629 	if (rkusb_check_lun(common)) {
630 		*reply = -EINVAL;
631 		return RKUSB_RC_ERROR;
632 	}
633 
634 	switch (common->cmnd[0]) {
635 	case RKUSB_TEST_UNIT_READY:
636 		*reply = rkusb_do_test_unit_ready(common, bh);
637 		rc = RKUSB_RC_FINISHED;
638 		break;
639 
640 	case RKUSB_READ_FLASH_ID:
641 		*reply = rkusb_do_read_flash_id(common, bh);
642 		rc = RKUSB_RC_FINISHED;
643 		break;
644 
645 	case RKUSB_TEST_BAD_BLOCK:
646 		*reply = rkusb_do_test_bad_block(common, bh);
647 		rc = RKUSB_RC_FINISHED;
648 		break;
649 
650 	case RKUSB_ERASE_10_FORCE:
651 		*reply = rkusb_do_erase_force(common, bh);
652 		rc = RKUSB_RC_FINISHED;
653 		break;
654 
655 	case RKUSB_LBA_READ_10:
656 		rkusb_fixup_cbwcb(common, bh);
657 		common->cmnd[0] = SC_READ_10;
658 		common->cmnd[1] = 0; /* Not support */
659 		rc = RKUSB_RC_CONTINUE;
660 		break;
661 
662 	case RKUSB_LBA_WRITE_10:
663 		rkusb_fixup_cbwcb(common, bh);
664 		common->cmnd[0] = SC_WRITE_10;
665 		common->cmnd[1] = 0; /* Not support */
666 		rc = RKUSB_RC_CONTINUE;
667 		break;
668 
669 	case RKUSB_READ_FLASH_INFO:
670 		*reply = rkusb_do_read_flash_info(common, bh);
671 		rc = RKUSB_RC_FINISHED;
672 		break;
673 
674 	case RKUSB_GET_CHIP_VER:
675 		*reply = rkusb_do_get_chip_info(common, bh);
676 		rc = RKUSB_RC_FINISHED;
677 		break;
678 
679 	case RKUSB_LBA_ERASE:
680 		*reply = rkusb_do_lba_erase(common, bh);
681 		rc = RKUSB_RC_FINISHED;
682 		break;
683 
684 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION
685 	case RKUSB_VS_WRITE:
686 		*reply = rkusb_do_vs_write(common);
687 		rc = RKUSB_RC_FINISHED;
688 		break;
689 
690 	case RKUSB_VS_READ:
691 		*reply = rkusb_do_vs_read(common);
692 		rc = RKUSB_RC_FINISHED;
693 		break;
694 #endif
695 
696 	case RKUSB_READ_CAPACITY:
697 		*reply = rkusb_do_read_capacity(common, bh);
698 		rc = RKUSB_RC_FINISHED;
699 		break;
700 
701 	case RKUSB_RESET:
702 		*reply = rkusb_do_reset(common, bh);
703 		rc = RKUSB_RC_FINISHED;
704 		break;
705 
706 	case RKUSB_READ_10:
707 	case RKUSB_WRITE_10:
708 		printf("CMD Not support, pls use new version Tool\n");
709 	case RKUSB_SET_DEVICE_ID:
710 	case RKUSB_ERASE_10:
711 	case RKUSB_WRITE_SPARE:
712 	case RKUSB_READ_SPARE:
713 	case RKUSB_GET_VERSION:
714 	case RKUSB_ERASE_SYS_DISK:
715 	case RKUSB_SDRAM_READ_10:
716 	case RKUSB_SDRAM_WRITE_10:
717 	case RKUSB_SDRAM_EXECUTE:
718 	case RKUSB_LOW_FORMAT:
719 	case RKUSB_SET_RESET_FLAG:
720 	case RKUSB_SPI_READ_10:
721 	case RKUSB_SPI_WRITE_10:
722 	case RKUSB_SESSION:
723 		/* Fall through */
724 	default:
725 		rc = RKUSB_RC_UNKNOWN_CMND;
726 		break;
727 	}
728 
729 	return rc;
730 }
731 
732 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add);
733