xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_rockusb.c (revision 5a94b26492fd3ad20c580976e18e101b67d14e6e)
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 				/* Vendor storage */
452 				rc = vendor_storage_write(vhead->id,
453 							  (char __user *)data,
454 							  vhead->size);
455 				if (rc < 0) {
456 					curlun->sense_data = SS_WRITE_ERROR;
457 					return -EIO;
458 				}
459 			} else if (type == 1) {
460 				/* RPMB */
461 				rc =
462 				write_keybox_to_secure_storage((u8 *)data,
463 							       vhead->size);
464 				if (rc < 0) {
465 					curlun->sense_data = SS_WRITE_ERROR;
466 					return -EIO;
467 				}
468 			} else if (type == 2) {
469 				/* security storage */
470 #ifdef CONFIG_RK_AVB_LIBAVB_USER
471 				debug("%s call rk_avb_write_perm_attr %d, %d\n",
472 				      __func__, vhead->id, vhead->size);
473 				rc = rk_avb_write_perm_attr(vhead->id,
474 							    (char __user *)data,
475 							    vhead->size);
476 				if (rc < 0) {
477 					curlun->sense_data = SS_WRITE_ERROR;
478 					return -EIO;
479 				}
480 #else
481 				printf("Please enable CONFIG_RK_AVB_LIBAVB_USER\n");
482 #endif
483 			} else if (type == 3) {
484 				/* efuse or otp*/
485 #ifdef CONFIG_OPTEE_CLIENT
486 				if (memcmp(data, "TAEK", 4) == 0) {
487 					if (vhead->size - 8 != 32) {
488 						printf("check ta encryption key size fail!\n");
489 						curlun->sense_data = SS_WRITE_ERROR;
490 						return -EIO;
491 					}
492 					if (trusty_write_ta_encryption_key((uint32_t *)(data + 8), 8) != 0) {
493 						printf("trusty_write_ta_encryption_key error!");
494 						curlun->sense_data = SS_WRITE_ERROR;
495 						return -EIO;
496 					}
497 				} else {
498 					printf("Unknown tag\n");
499 					curlun->sense_data = SS_WRITE_ERROR;
500 					return -EIO;
501 				}
502 #else
503 				printf("Please enable CONFIG_OPTEE_CLIENT\n");
504 #endif
505 			} else {
506 				return -EINVAL;
507 			}
508 
509 			common->residue -= common->data_size;
510 
511 			/* Did the host decide to stop early? */
512 			if (bh->outreq->actual != bh->outreq->length)
513 				common->short_packet_received = 1;
514 			break; /* Command done */
515 		}
516 wait:
517 		/* Wait for something to happen */
518 		rc = sleep_thread(common);
519 		if (rc)
520 			return rc;
521 	}
522 
523 	return -EIO; /* No default reply */
524 }
525 
526 static int rkusb_do_vs_read(struct fsg_common *common)
527 {
528 	struct fsg_lun		*curlun = &common->luns[common->lun];
529 	u16			type = get_unaligned_be16(&common->cmnd[4]);
530 	struct vendor_item	*vhead;
531 	struct fsg_buffhd	*bh;
532 	void			*data;
533 	int			rc;
534 
535 	if (common->data_size >= (u32)65536) {
536 		/* _MUST_ small than 64K */
537 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
538 		return -EINVAL;
539 	}
540 
541 	common->residue         = common->data_size;
542 	common->usb_amount_left = common->data_size;
543 
544 	/* Carry out the file reads */
545 	if (unlikely(common->data_size == 0))
546 		return -EIO; /* No default reply */
547 
548 	for (;;) {
549 		/* Wait for the next buffer to become available */
550 		bh = common->next_buffhd_to_fill;
551 		while (bh->state != BUF_STATE_EMPTY) {
552 			rc = sleep_thread(common);
553 			if (rc)
554 				return rc;
555 		}
556 
557 		memset(bh->buf, 0, FSG_BUFLEN);
558 		vhead = (struct vendor_item *)bh->buf;
559 		data  = bh->buf + sizeof(struct vendor_item);
560 		vhead->id = get_unaligned_be16(&common->cmnd[2]);
561 
562 		if (!type) {
563 			/* Vendor storage */
564 			rc = vendor_storage_read(vhead->id,
565 						 (char __user *)data,
566 						 common->data_size);
567 			if (!rc) {
568 				curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
569 				return -EIO;
570 			}
571 			vhead->size = rc;
572 		} else if (type == 1) {
573 			/* RPMB */
574 			rc =
575 			read_raw_data_from_secure_storage((u8 *)data,
576 							  common->data_size);
577 			if (!rc) {
578 				curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
579 				return -EIO;
580 			}
581 			vhead->size = rc;
582 		} else if (type == 2) {
583 			/* security storage */
584 #ifdef CONFIG_RK_AVB_LIBAVB_USER
585 			rc = rk_avb_read_perm_attr(vhead->id,
586 						   (char __user *)data,
587 						   vhead->size);
588 			if (rc < 0)
589 				return -EIO;
590 			vhead->size = rc;
591 #else
592 			printf("Please enable CONFIG_RK_AVB_LIBAVB_USER!\n");
593 #endif
594 		} else {
595 			return -EINVAL;
596 		}
597 
598 		common->residue   -= common->data_size;
599 		bh->inreq->length = common->data_size;
600 		bh->state         = BUF_STATE_FULL;
601 
602 		break; /* No more left to read */
603 	}
604 
605 	return -EIO; /* No default reply */
606 }
607 #endif
608 
609 static int rkusb_do_get_storage_info(struct fsg_common *common,
610 				     struct fsg_buffhd *bh)
611 {
612 	enum if_type type = ums[common->lun].block_dev.if_type;
613 	int devnum = ums[common->lun].block_dev.devnum;
614 	u32 media = BOOT_TYPE_UNKNOWN;
615 	u32 len = common->data_size;
616 	u8 *buf = (u8 *)bh->buf;
617 
618 	if (len > 4)
619 		len = 4;
620 
621 	switch (type) {
622 	case IF_TYPE_MMC:
623 		media = BOOT_TYPE_EMMC;
624 		break;
625 
626 	case IF_TYPE_SD:
627 		media = BOOT_TYPE_SD0;
628 		break;
629 
630 	case IF_TYPE_MTD:
631 		if (devnum == BLK_MTD_SPI_NAND)
632 			media = BOOT_TYPE_MTD_BLK_SPI_NAND;
633 		else if (devnum == BLK_MTD_NAND)
634 			media = BOOT_TYPE_NAND;
635 		else
636 			media = BOOT_TYPE_MTD_BLK_SPI_NOR;
637 		break;
638 
639 	case IF_TYPE_SCSI:
640 		media = BOOT_TYPE_SATA;
641 		break;
642 
643 	case IF_TYPE_RKNAND:
644 		media = BOOT_TYPE_NAND;
645 		break;
646 
647 	case IF_TYPE_NVME:
648 		media = BOOT_TYPE_PCIE;
649 		break;
650 
651 	default:
652 		break;
653 	}
654 
655 	memcpy((void *)&buf[0], (void *)&media, len);
656 	common->residue = len;
657 	common->data_size_from_cmnd = len;
658 
659 	return len;
660 }
661 
662 static int rkusb_do_read_capacity(struct fsg_common *common,
663 				  struct fsg_buffhd *bh)
664 {
665 	u8 *buf = (u8 *)bh->buf;
666 	u32 len = common->data_size;
667 	enum if_type type = ums[common->lun].block_dev.if_type;
668 	int devnum = ums[common->lun].block_dev.devnum;
669 
670 	/*
671 	 * bit[0]: Direct LBA, 0: Disabled;
672 	 * bit[1]: Vendor Storage API, 0: Disabed (default);
673 	 * bit[2]: First 4M Access, 0: Disabled;
674 	 * bit[3]: Read LBA On, 0: Disabed (default);
675 	 * bit[4]: New Vendor Storage API, 0: Disabed;
676 	 * bit[5]: Read uart data from ram
677 	 * bit[6]: Read IDB config
678 	 * bit[7]: Read SecureMode
679 	 * bit[8]: New IDB feature
680 	 * bit[9]: Get storage media info
681 	 * bit[10:63}: Reserved.
682 	 */
683 	memset((void *)&buf[0], 0, len);
684 	if (type == IF_TYPE_MMC || type == IF_TYPE_SD || type == IF_TYPE_NVME)
685 		buf[0] = BIT(0) | BIT(2) | BIT(4);
686 	else
687 		buf[0] = BIT(0) | BIT(4);
688 
689 	if (type == IF_TYPE_MTD &&
690 	    (devnum == BLK_MTD_NAND ||
691 	    devnum == BLK_MTD_SPI_NAND))
692 		buf[0] |= (1 << 6);
693 
694 #if !defined(CONFIG_ROCKCHIP_RV1126) && !defined(CONFIG_ROCKCHIP_RK3308)
695 	if (type == IF_TYPE_MTD && devnum == BLK_MTD_SPI_NOR)
696 		buf[0] |= (1 << 6);
697 #endif
698 
699 #if defined(CONFIG_ROCKCHIP_NEW_IDB)
700 	buf[1] = BIT(0);
701 #endif
702 	buf[1] |= BIT(1);
703 
704 	/* Set data xfer size */
705 	common->residue = len;
706 	common->data_size_from_cmnd = len;
707 
708 	return len;
709 }
710 
711 static void rkusb_fixup_cbwcb(struct fsg_common *common,
712 			      struct fsg_buffhd *bh)
713 {
714 	struct usb_request      *req = bh->outreq;
715 	struct fsg_bulk_cb_wrap *cbw = req->buf;
716 
717 	/* FIXME cbw.DataTransferLength was not set by Upgrade Tool */
718 	common->data_size = le32_to_cpu(cbw->DataTransferLength);
719 	if (common->data_size == 0) {
720 		common->data_size =
721 		get_unaligned_be16(&common->cmnd[7]) << 9;
722 		printf("Trasfer Length NOT set, please use new version tool\n");
723 		debug("%s %d, cmnd1 %x\n", __func__,
724 		      get_unaligned_be16(&common->cmnd[7]),
725 		      get_unaligned_be16(&common->cmnd[1]));
726 	}
727 	if (cbw->Flags & USB_BULK_IN_FLAG)
728 		common->data_dir = DATA_DIR_TO_HOST;
729 	else
730 		common->data_dir = DATA_DIR_FROM_HOST;
731 
732 	/* Not support */
733 	common->cmnd[1] = 0;
734 }
735 
736 static int rkusb_cmd_process(struct fsg_common *common,
737 			     struct fsg_buffhd *bh, int *reply)
738 {
739 	struct usb_request	*req = bh->outreq;
740 	struct fsg_bulk_cb_wrap	*cbw = req->buf;
741 	int rc;
742 
743 	dump_cbw(cbw);
744 
745 	if (rkusb_check_lun(common)) {
746 		*reply = -EINVAL;
747 		return RKUSB_RC_ERROR;
748 	}
749 
750 	switch (common->cmnd[0]) {
751 	case RKUSB_TEST_UNIT_READY:
752 		*reply = rkusb_do_test_unit_ready(common, bh);
753 		rc = RKUSB_RC_FINISHED;
754 		break;
755 
756 	case RKUSB_READ_FLASH_ID:
757 		*reply = rkusb_do_read_flash_id(common, bh);
758 		rc = RKUSB_RC_FINISHED;
759 		break;
760 
761 	case RKUSB_TEST_BAD_BLOCK:
762 		*reply = rkusb_do_test_bad_block(common, bh);
763 		rc = RKUSB_RC_FINISHED;
764 		break;
765 
766 	case RKUSB_ERASE_10_FORCE:
767 		*reply = rkusb_do_erase_force(common, bh);
768 		rc = RKUSB_RC_FINISHED;
769 		break;
770 
771 	case RKUSB_LBA_READ_10:
772 		rkusb_fixup_cbwcb(common, bh);
773 		common->cmnd[0] = SC_READ_10;
774 		common->cmnd[1] = 0; /* Not support */
775 		rc = RKUSB_RC_CONTINUE;
776 		break;
777 
778 	case RKUSB_LBA_WRITE_10:
779 		rkusb_fixup_cbwcb(common, bh);
780 		common->cmnd[0] = SC_WRITE_10;
781 		common->cmnd[1] = 0; /* Not support */
782 		rc = RKUSB_RC_CONTINUE;
783 		break;
784 
785 	case RKUSB_READ_FLASH_INFO:
786 		*reply = rkusb_do_read_flash_info(common, bh);
787 		rc = RKUSB_RC_FINISHED;
788 		break;
789 
790 	case RKUSB_GET_CHIP_VER:
791 		*reply = rkusb_do_get_chip_info(common, bh);
792 		rc = RKUSB_RC_FINISHED;
793 		break;
794 
795 	case RKUSB_LBA_ERASE:
796 		*reply = rkusb_do_lba_erase(common, bh);
797 		rc = RKUSB_RC_FINISHED;
798 		break;
799 
800 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION
801 	case RKUSB_VS_WRITE:
802 		*reply = rkusb_do_vs_write(common);
803 		rc = RKUSB_RC_FINISHED;
804 		break;
805 
806 	case RKUSB_VS_READ:
807 		*reply = rkusb_do_vs_read(common);
808 		rc = RKUSB_RC_FINISHED;
809 		break;
810 #endif
811 	case RKUSB_GET_STORAGE_MEDIA:
812 		*reply = rkusb_do_get_storage_info(common, bh);
813 		rc = RKUSB_RC_FINISHED;
814 		break;
815 
816 	case RKUSB_READ_CAPACITY:
817 		*reply = rkusb_do_read_capacity(common, bh);
818 		rc = RKUSB_RC_FINISHED;
819 		break;
820 
821 	case RKUSB_RESET:
822 		*reply = rkusb_do_reset(common, bh);
823 		rc = RKUSB_RC_FINISHED;
824 		break;
825 
826 	case RKUSB_READ_10:
827 	case RKUSB_WRITE_10:
828 		printf("CMD Not support, pls use new version Tool\n");
829 	case RKUSB_SET_DEVICE_ID:
830 	case RKUSB_ERASE_10:
831 	case RKUSB_WRITE_SPARE:
832 	case RKUSB_READ_SPARE:
833 	case RKUSB_GET_VERSION:
834 	case RKUSB_ERASE_SYS_DISK:
835 	case RKUSB_SDRAM_READ_10:
836 	case RKUSB_SDRAM_WRITE_10:
837 	case RKUSB_SDRAM_EXECUTE:
838 	case RKUSB_LOW_FORMAT:
839 	case RKUSB_SET_RESET_FLAG:
840 	case RKUSB_SPI_READ_10:
841 	case RKUSB_SPI_WRITE_10:
842 	case RKUSB_SESSION:
843 		/* Fall through */
844 	default:
845 		rc = RKUSB_RC_UNKNOWN_CMND;
846 		break;
847 	}
848 
849 	return rc;
850 }
851 
852 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add);
853