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