xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_rockusb.c (revision 965eda410b8d28439dc1ba4f76061880d72978fd)
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 <rockusb.h>
9 
10 #define ROCKUSB_INTERFACE_CLASS	0xff
11 #define ROCKUSB_INTERFACE_SUB_CLASS	0x06
12 #define ROCKUSB_INTERFACE_PROTOCOL	0x05
13 
14 static struct usb_interface_descriptor rkusb_intf_desc = {
15 	.bLength		= USB_DT_INTERFACE_SIZE,
16 	.bDescriptorType	= USB_DT_INTERFACE,
17 	.bInterfaceNumber	= 0x00,
18 	.bAlternateSetting	= 0x00,
19 	.bNumEndpoints		= 0x02,
20 	.bInterfaceClass	= ROCKUSB_INTERFACE_CLASS,
21 	.bInterfaceSubClass	= ROCKUSB_INTERFACE_SUB_CLASS,
22 	.bInterfaceProtocol	= ROCKUSB_INTERFACE_PROTOCOL,
23 };
24 
25 static struct usb_descriptor_header *rkusb_fs_function[] = {
26 	(struct usb_descriptor_header *)&rkusb_intf_desc,
27 	(struct usb_descriptor_header *)&fsg_fs_bulk_in_desc,
28 	(struct usb_descriptor_header *)&fsg_fs_bulk_out_desc,
29 	NULL,
30 };
31 
32 static struct usb_descriptor_header *rkusb_hs_function[] = {
33 	(struct usb_descriptor_header *)&rkusb_intf_desc,
34 	(struct usb_descriptor_header *)&fsg_hs_bulk_in_desc,
35 	(struct usb_descriptor_header *)&fsg_hs_bulk_out_desc,
36 	NULL,
37 };
38 
39 struct rk_flash_info {
40 	u32	flash_size;
41 	u16	block_size;
42 	u8	page_size;
43 	u8	ecc_bits;
44 	u8	access_time;
45 	u8	manufacturer;
46 	u8	flash_mask;
47 } __packed;
48 
49 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
50 {
51 	/* Enumerate as a loader device */
52 	if (IS_RKUSB_UMS_DNL(name))
53 		dev->bcdUSB = cpu_to_le16(0x0201);
54 
55 	return 0;
56 }
57 
58 __maybe_unused
59 static inline void dump_cbw(struct fsg_bulk_cb_wrap *cbw)
60 {
61 	assert(!cbw);
62 
63 	debug("%s:\n", __func__);
64 	debug("Signature %x\n", cbw->Signature);
65 	debug("Tag %x\n", cbw->Tag);
66 	debug("DataTransferLength %x\n", cbw->DataTransferLength);
67 	debug("Flags %x\n", cbw->Flags);
68 	debug("LUN %x\n", cbw->Lun);
69 	debug("Length %x\n", cbw->Length);
70 	debug("OptionCode %x\n", cbw->CDB[0]);
71 	debug("SubCode %x\n", cbw->CDB[1]);
72 	debug("SectorAddr %x\n", get_unaligned_be32(&cbw->CDB[2]));
73 	debug("BlkSectors %x\n\n", get_unaligned_be16(&cbw->CDB[7]));
74 }
75 
76 static int rkusb_check_lun(struct fsg_common *common)
77 {
78 	struct fsg_lun *curlun;
79 
80 	/* Check the LUN */
81 	if (common->lun >= 0 && common->lun < common->nluns) {
82 		curlun = &common->luns[common->lun];
83 		if (common->cmnd[0] != SC_REQUEST_SENSE) {
84 			curlun->sense_data = SS_NO_SENSE;
85 			curlun->info_valid = 0;
86 		}
87 	} else {
88 		curlun = NULL;
89 		common->bad_lun_okay = 0;
90 
91 		/*
92 		 * INQUIRY and REQUEST SENSE commands are explicitly allowed
93 		 * to use unsupported LUNs; all others may not.
94 		 */
95 		if (common->cmnd[0] != SC_INQUIRY &&
96 		    common->cmnd[0] != SC_REQUEST_SENSE) {
97 			debug("unsupported LUN %d\n", common->lun);
98 			return -EINVAL;
99 		}
100 	}
101 
102 	return 0;
103 }
104 
105 static void __do_reset(struct usb_ep *ep, struct usb_request *req)
106 {
107 	do_reset(NULL, 0, 0, NULL);
108 }
109 
110 static int rkusb_do_reset(struct fsg_common *common,
111 			  struct fsg_buffhd *bh)
112 {
113 	common->data_size_from_cmnd = common->cmnd[4];
114 	common->residue = 0;
115 	bh->inreq->complete = __do_reset;
116 	bh->state = BUF_STATE_EMPTY;
117 
118 	return 0;
119 }
120 
121 static int rkusb_do_test_unit_ready(struct fsg_common *common,
122 				    struct fsg_buffhd *bh)
123 {
124 	common->residue = 0x06 << 24; /* Max block xfer support from host */
125 	common->data_dir = DATA_DIR_NONE;
126 	bh->state = BUF_STATE_EMPTY;
127 
128 	return 0;
129 }
130 
131 static int rkusb_do_read_flash_id(struct fsg_common *common,
132 				  struct fsg_buffhd *bh)
133 {
134 	u8 *buf = (u8 *)bh->buf;
135 	u32 len = common->data_size;
136 
137 	memcpy((void *)&buf[0], "EMMC ", 5);
138 
139 	/* Set data xfer size */
140 	common->residue = common->data_size_from_cmnd = len;
141 
142 	return len;
143 }
144 
145 static int rkusb_do_test_bad_block(struct fsg_common *common,
146 				   struct fsg_buffhd *bh)
147 {
148 	u8 *buf = (u8 *)bh->buf;
149 	u32 len = common->data_size;
150 
151 	memset((void *)&buf[0], 0, len);
152 
153 	/* Set data xfer size */
154 	common->residue = common->data_size_from_cmnd = len;
155 
156 	return len;
157 }
158 
159 static int rkusb_do_read_flash_info(struct fsg_common *common,
160 				    struct fsg_buffhd *bh)
161 {
162 	u8 *buf = (u8 *)bh->buf;
163 	u32 len = common->data_size;
164 	struct rk_flash_info finfo = {
165 		.block_size = 1024,
166 		.ecc_bits = 0,
167 		.page_size = 4,
168 		.access_time = 40,
169 		.manufacturer = 0,
170 		.flash_mask = 0
171 	};
172 
173 	finfo.flash_size = (u32)ums[common->lun].block_dev.lba;
174 	if (finfo.flash_size)
175 		finfo.flash_mask = 1;
176 
177 	memset((void *)&buf[0], 0, len);
178 	memcpy((void *)&buf[0], (void *)&finfo, len);
179 
180 	/* Set data xfer size */
181 	common->residue = common->data_size_from_cmnd = len;
182 
183 	return len;
184 }
185 
186 static int rkusb_do_lba_erase(struct fsg_common *common,
187 			      struct fsg_buffhd *bh)
188 {
189 	struct fsg_lun *curlun = &common->luns[common->lun];
190 	u32 lba, amount;
191 	loff_t file_offset;
192 	int rc;
193 
194 	lba = get_unaligned_be32(&common->cmnd[2]);
195 	if (lba >= curlun->num_sectors) {
196 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
197 		rc = -EINVAL;
198 		goto out;
199 	}
200 
201 	file_offset = ((loff_t) lba) << 9;
202 	amount = get_unaligned_be16(&common->cmnd[7]) << 9;
203 	if (unlikely(amount == 0)) {
204 		curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
205 		rc = -EIO;
206 		goto out;
207 	}
208 
209 	/* Perform the erase */
210 	rc = ums[common->lun].erase_sector(&ums[common->lun],
211 			       file_offset / SECTOR_SIZE,
212 			       amount / SECTOR_SIZE);
213 	if (!rc) {
214 		curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
215 		rc = -EIO;
216 	}
217 
218 out:
219 	common->data_dir = DATA_DIR_NONE;
220 	bh->state = BUF_STATE_EMPTY;
221 
222 	return rc;
223 }
224 
225 static int rkusb_do_read_capacity(struct fsg_common *common,
226 				    struct fsg_buffhd *bh)
227 {
228 	u8 *buf = (u8 *)bh->buf;
229 	u32 len = common->data_size;
230 
231 	/*
232 	 * bit[0]: Direct LBA, 0: Disabled;
233 	 * bit[1:63}: Reserved.
234 	 */
235 	memset((void *)&buf[0], 0, len);
236 
237 	/* Set data xfer size */
238 	common->residue = common->data_size_from_cmnd = len;
239 
240 	return len;
241 }
242 
243 static int rkusb_cmd_process(struct fsg_common *common,
244 			     struct fsg_buffhd *bh, int *reply)
245 {
246 	struct usb_request	*req = bh->outreq;
247 	struct fsg_bulk_cb_wrap	*cbw = req->buf;
248 	int rc;
249 
250 	dump_cbw(cbw);
251 
252 	if (rkusb_check_lun(common)) {
253 		*reply = -EINVAL;
254 		return RKUSB_RC_ERROR;
255 	}
256 
257 	switch (common->cmnd[0]) {
258 	case RKUSB_TEST_UNIT_READY:
259 		*reply = rkusb_do_test_unit_ready(common, bh);
260 		rc = RKUSB_RC_FINISHED;
261 		break;
262 
263 	case RKUSB_READ_FLASH_ID:
264 		*reply = rkusb_do_read_flash_id(common, bh);
265 		rc = RKUSB_RC_FINISHED;
266 		break;
267 
268 	case RKUSB_TEST_BAD_BLOCK:
269 		*reply = rkusb_do_test_bad_block(common, bh);
270 		rc = RKUSB_RC_FINISHED;
271 		break;
272 
273 	case RKUSB_LBA_READ_10:
274 		common->cmnd[0] = SC_READ_10;
275 		common->cmnd[1] = 0; /* Not support */
276 		rc = RKUSB_RC_CONTINUE;
277 		break;
278 
279 	case RKUSB_LBA_WRITE_10:
280 		common->cmnd[0] = SC_WRITE_10;
281 		common->cmnd[1] = 0; /* Not support */
282 		rc = RKUSB_RC_CONTINUE;
283 		break;
284 
285 	case RKUSB_READ_FLASH_INFO:
286 		*reply = rkusb_do_read_flash_info(common, bh);
287 		rc = RKUSB_RC_FINISHED;
288 		break;
289 
290 	case RKUSB_LBA_ERASE:
291 		*reply = rkusb_do_lba_erase(common, bh);
292 		rc = RKUSB_RC_FINISHED;
293 		break;
294 
295 	case RKUSB_READ_CAPACITY:
296 		*reply = rkusb_do_read_capacity(common, bh);
297 		rc = RKUSB_RC_FINISHED;
298 		break;
299 
300 	case RKUSB_RESET:
301 		*reply = rkusb_do_reset(common, bh);
302 		rc = RKUSB_RC_FINISHED;
303 		break;
304 
305 	case RKUSB_SET_DEVICE_ID:
306 	case RKUSB_READ_10:
307 	case RKUSB_WRITE_10:
308 	case RKUSB_ERASE_10:
309 	case RKUSB_WRITE_SPARE:
310 	case RKUSB_READ_SPARE:
311 	case RKUSB_ERASE_10_FORCE:
312 	case RKUSB_GET_VERSION:
313 	case RKUSB_ERASE_SYS_DISK:
314 	case RKUSB_SDRAM_READ_10:
315 	case RKUSB_SDRAM_WRITE_10:
316 	case RKUSB_SDRAM_EXECUTE:
317 	case RKUSB_GET_CHIP_VER:
318 	case RKUSB_LOW_FORMAT:
319 	case RKUSB_SET_RESET_FLAG:
320 	case RKUSB_SPI_READ_10:
321 	case RKUSB_SPI_WRITE_10:
322 	case RKUSB_SESSION:
323 		/* Fall through */
324 	default:
325 		rc = RKUSB_RC_UNKNOWN_CMND;
326 		break;
327 	}
328 
329 	return rc;
330 }
331 
332 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add);
333