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