xref: /openbmc/u-boot/drivers/usb/gadget/f_rockusb.c (revision cbd2fba1)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2017
4  *
5  * Eddie Cai <eddie.cai.linux@gmail.com>
6  */
7 #include <config.h>
8 #include <common.h>
9 #include <errno.h>
10 #include <malloc.h>
11 #include <memalign.h>
12 #include <linux/usb/ch9.h>
13 #include <linux/usb/gadget.h>
14 #include <linux/usb/composite.h>
15 #include <linux/compiler.h>
16 #include <version.h>
17 #include <g_dnl.h>
18 #include <asm/arch/f_rockusb.h>
19 
20 static inline struct f_rockusb *func_to_rockusb(struct usb_function *f)
21 {
22 	return container_of(f, struct f_rockusb, usb_function);
23 }
24 
25 static struct usb_endpoint_descriptor fs_ep_in = {
26 	.bLength            = USB_DT_ENDPOINT_SIZE,
27 	.bDescriptorType    = USB_DT_ENDPOINT,
28 	.bEndpointAddress   = USB_DIR_IN,
29 	.bmAttributes       = USB_ENDPOINT_XFER_BULK,
30 	.wMaxPacketSize     = cpu_to_le16(64),
31 };
32 
33 static struct usb_endpoint_descriptor fs_ep_out = {
34 	.bLength		= USB_DT_ENDPOINT_SIZE,
35 	.bDescriptorType	= USB_DT_ENDPOINT,
36 	.bEndpointAddress	= USB_DIR_OUT,
37 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
38 	.wMaxPacketSize		= cpu_to_le16(64),
39 };
40 
41 static struct usb_endpoint_descriptor hs_ep_in = {
42 	.bLength		= USB_DT_ENDPOINT_SIZE,
43 	.bDescriptorType	= USB_DT_ENDPOINT,
44 	.bEndpointAddress	= USB_DIR_IN,
45 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
46 	.wMaxPacketSize		= cpu_to_le16(512),
47 };
48 
49 static struct usb_endpoint_descriptor hs_ep_out = {
50 	.bLength		= USB_DT_ENDPOINT_SIZE,
51 	.bDescriptorType	= USB_DT_ENDPOINT,
52 	.bEndpointAddress	= USB_DIR_OUT,
53 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
54 	.wMaxPacketSize		= cpu_to_le16(512),
55 };
56 
57 static struct usb_interface_descriptor interface_desc = {
58 	.bLength		= USB_DT_INTERFACE_SIZE,
59 	.bDescriptorType	= USB_DT_INTERFACE,
60 	.bInterfaceNumber	= 0x00,
61 	.bAlternateSetting	= 0x00,
62 	.bNumEndpoints		= 0x02,
63 	.bInterfaceClass	= ROCKUSB_INTERFACE_CLASS,
64 	.bInterfaceSubClass	= ROCKUSB_INTERFACE_SUB_CLASS,
65 	.bInterfaceProtocol	= ROCKUSB_INTERFACE_PROTOCOL,
66 };
67 
68 static struct usb_descriptor_header *rkusb_fs_function[] = {
69 	(struct usb_descriptor_header *)&interface_desc,
70 	(struct usb_descriptor_header *)&fs_ep_in,
71 	(struct usb_descriptor_header *)&fs_ep_out,
72 };
73 
74 static struct usb_descriptor_header *rkusb_hs_function[] = {
75 	(struct usb_descriptor_header *)&interface_desc,
76 	(struct usb_descriptor_header *)&hs_ep_in,
77 	(struct usb_descriptor_header *)&hs_ep_out,
78 	NULL,
79 };
80 
81 static const char rkusb_name[] = "Rockchip Rockusb";
82 
83 static struct usb_string rkusb_string_defs[] = {
84 	[0].s = rkusb_name,
85 	{  }			/* end of list */
86 };
87 
88 static struct usb_gadget_strings stringtab_rkusb = {
89 	.language	= 0x0409,	/* en-us */
90 	.strings	= rkusb_string_defs,
91 };
92 
93 static struct usb_gadget_strings *rkusb_strings[] = {
94 	&stringtab_rkusb,
95 	NULL,
96 };
97 
98 static struct f_rockusb *rockusb_func;
99 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
100 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size);
101 
102 struct f_rockusb *get_rkusb(void)
103 {
104 	struct f_rockusb *f_rkusb = rockusb_func;
105 
106 	if (!f_rkusb) {
107 		f_rkusb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_rkusb));
108 		if (!f_rkusb)
109 			return 0;
110 
111 		rockusb_func = f_rkusb;
112 		memset(f_rkusb, 0, sizeof(*f_rkusb));
113 	}
114 
115 	if (!f_rkusb->buf_head) {
116 		f_rkusb->buf_head = memalign(CONFIG_SYS_CACHELINE_SIZE,
117 					     RKUSB_BUF_SIZE);
118 		if (!f_rkusb->buf_head)
119 			return 0;
120 
121 		f_rkusb->buf = f_rkusb->buf_head;
122 		memset(f_rkusb->buf_head, 0, RKUSB_BUF_SIZE);
123 	}
124 	return f_rkusb;
125 }
126 
127 static struct usb_endpoint_descriptor *rkusb_ep_desc(
128 struct usb_gadget *g,
129 struct usb_endpoint_descriptor *fs,
130 struct usb_endpoint_descriptor *hs)
131 {
132 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
133 		return hs;
134 	return fs;
135 }
136 
137 static void rockusb_complete(struct usb_ep *ep, struct usb_request *req)
138 {
139 	int status = req->status;
140 
141 	if (!status)
142 		return;
143 	debug("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
144 }
145 
146 /* config the rockusb device*/
147 static int rockusb_bind(struct usb_configuration *c, struct usb_function *f)
148 {
149 	int id;
150 	struct usb_gadget *gadget = c->cdev->gadget;
151 	struct f_rockusb *f_rkusb = func_to_rockusb(f);
152 	const char *s;
153 
154 	id = usb_interface_id(c, f);
155 	if (id < 0)
156 		return id;
157 	interface_desc.bInterfaceNumber = id;
158 
159 	id = usb_string_id(c->cdev);
160 	if (id < 0)
161 		return id;
162 
163 	rkusb_string_defs[0].id = id;
164 	interface_desc.iInterface = id;
165 
166 	f_rkusb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
167 	if (!f_rkusb->in_ep)
168 		return -ENODEV;
169 	f_rkusb->in_ep->driver_data = c->cdev;
170 
171 	f_rkusb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
172 	if (!f_rkusb->out_ep)
173 		return -ENODEV;
174 	f_rkusb->out_ep->driver_data = c->cdev;
175 
176 	f->descriptors = rkusb_fs_function;
177 
178 	if (gadget_is_dualspeed(gadget)) {
179 		hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
180 		hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
181 		f->hs_descriptors = rkusb_hs_function;
182 	}
183 
184 	s = env_get("serial#");
185 	if (s)
186 		g_dnl_set_serialnumber((char *)s);
187 
188 	return 0;
189 }
190 
191 static void rockusb_unbind(struct usb_configuration *c, struct usb_function *f)
192 {
193 	/* clear the configuration*/
194 	memset(rockusb_func, 0, sizeof(*rockusb_func));
195 }
196 
197 static void rockusb_disable(struct usb_function *f)
198 {
199 	struct f_rockusb *f_rkusb = func_to_rockusb(f);
200 
201 	usb_ep_disable(f_rkusb->out_ep);
202 	usb_ep_disable(f_rkusb->in_ep);
203 
204 	if (f_rkusb->out_req) {
205 		free(f_rkusb->out_req->buf);
206 		usb_ep_free_request(f_rkusb->out_ep, f_rkusb->out_req);
207 		f_rkusb->out_req = NULL;
208 	}
209 	if (f_rkusb->in_req) {
210 		free(f_rkusb->in_req->buf);
211 		usb_ep_free_request(f_rkusb->in_ep, f_rkusb->in_req);
212 		f_rkusb->in_req = NULL;
213 	}
214 	if (f_rkusb->buf_head) {
215 		free(f_rkusb->buf_head);
216 		f_rkusb->buf_head = NULL;
217 		f_rkusb->buf = NULL;
218 	}
219 }
220 
221 static struct usb_request *rockusb_start_ep(struct usb_ep *ep)
222 {
223 	struct usb_request *req;
224 
225 	req = usb_ep_alloc_request(ep, 0);
226 	if (!req)
227 		return NULL;
228 
229 	req->length = EP_BUFFER_SIZE;
230 	req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
231 	if (!req->buf) {
232 		usb_ep_free_request(ep, req);
233 		return NULL;
234 	}
235 	memset(req->buf, 0, req->length);
236 
237 	return req;
238 }
239 
240 static int rockusb_set_alt(struct usb_function *f, unsigned int interface,
241 			   unsigned int alt)
242 {
243 	int ret;
244 	struct usb_composite_dev *cdev = f->config->cdev;
245 	struct usb_gadget *gadget = cdev->gadget;
246 	struct f_rockusb *f_rkusb = func_to_rockusb(f);
247 	const struct usb_endpoint_descriptor *d;
248 
249 	debug("%s: func: %s intf: %d alt: %d\n",
250 	      __func__, f->name, interface, alt);
251 
252 	d = rkusb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
253 	ret = usb_ep_enable(f_rkusb->out_ep, d);
254 	if (ret) {
255 		printf("failed to enable out ep\n");
256 		return ret;
257 	}
258 
259 	f_rkusb->out_req = rockusb_start_ep(f_rkusb->out_ep);
260 	if (!f_rkusb->out_req) {
261 		printf("failed to alloc out req\n");
262 		ret = -EINVAL;
263 		goto err;
264 	}
265 	f_rkusb->out_req->complete = rx_handler_command;
266 
267 	d = rkusb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
268 	ret = usb_ep_enable(f_rkusb->in_ep, d);
269 	if (ret) {
270 		printf("failed to enable in ep\n");
271 		goto err;
272 	}
273 
274 	f_rkusb->in_req = rockusb_start_ep(f_rkusb->in_ep);
275 	if (!f_rkusb->in_req) {
276 		printf("failed alloc req in\n");
277 		ret = -EINVAL;
278 		goto err;
279 	}
280 	f_rkusb->in_req->complete = rockusb_complete;
281 
282 	ret = usb_ep_queue(f_rkusb->out_ep, f_rkusb->out_req, 0);
283 	if (ret)
284 		goto err;
285 
286 	return 0;
287 err:
288 	rockusb_disable(f);
289 	return ret;
290 }
291 
292 static int rockusb_add(struct usb_configuration *c)
293 {
294 	struct f_rockusb *f_rkusb = get_rkusb();
295 	int status;
296 
297 	debug("%s: cdev: 0x%p\n", __func__, c->cdev);
298 
299 	f_rkusb->usb_function.name = "f_rockusb";
300 	f_rkusb->usb_function.bind = rockusb_bind;
301 	f_rkusb->usb_function.unbind = rockusb_unbind;
302 	f_rkusb->usb_function.set_alt = rockusb_set_alt;
303 	f_rkusb->usb_function.disable = rockusb_disable;
304 	f_rkusb->usb_function.strings = rkusb_strings;
305 
306 	status = usb_add_function(c, &f_rkusb->usb_function);
307 	if (status) {
308 		free(f_rkusb);
309 		rockusb_func = f_rkusb;
310 	}
311 	return status;
312 }
313 
314 void rockusb_dev_init(char *dev_type, int dev_index)
315 {
316 	struct f_rockusb *f_rkusb = get_rkusb();
317 
318 	f_rkusb->dev_type = dev_type;
319 	f_rkusb->dev_index = dev_index;
320 }
321 
322 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_rockusb, rockusb_add);
323 
324 static int rockusb_tx_write(const char *buffer, unsigned int buffer_size)
325 {
326 	struct usb_request *in_req = rockusb_func->in_req;
327 	int ret;
328 
329 	memcpy(in_req->buf, buffer, buffer_size);
330 	in_req->length = buffer_size;
331 	debug("Transferring 0x%x bytes\n", buffer_size);
332 	usb_ep_dequeue(rockusb_func->in_ep, in_req);
333 	ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
334 	if (ret)
335 		printf("Error %d on queue\n", ret);
336 	return 0;
337 }
338 
339 static int rockusb_tx_write_str(const char *buffer)
340 {
341 	return rockusb_tx_write(buffer, strlen(buffer));
342 }
343 
344 #ifdef DEBUG
345 static void printcbw(char *buf)
346 {
347 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
348 				 sizeof(struct fsg_bulk_cb_wrap));
349 
350 	memcpy((char *)cbw, buf, USB_BULK_CB_WRAP_LEN);
351 
352 	debug("cbw: signature:%x\n", cbw->signature);
353 	debug("cbw: tag=%x\n", cbw->tag);
354 	debug("cbw: data_transfer_length=%d\n", cbw->data_transfer_length);
355 	debug("cbw: flags=%x\n", cbw->flags);
356 	debug("cbw: lun=%d\n", cbw->lun);
357 	debug("cbw: length=%d\n", cbw->length);
358 	debug("cbw: ucOperCode=%x\n", cbw->CDB[0]);
359 	debug("cbw: ucReserved=%x\n", cbw->CDB[1]);
360 	debug("cbw: dwAddress:%x %x %x %x\n", cbw->CDB[5], cbw->CDB[4],
361 	      cbw->CDB[3], cbw->CDB[2]);
362 	debug("cbw: ucReserved2=%x\n", cbw->CDB[6]);
363 	debug("cbw: uslength:%x %x\n", cbw->CDB[8], cbw->CDB[7]);
364 }
365 
366 static void printcsw(char *buf)
367 {
368 	ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
369 				 sizeof(struct bulk_cs_wrap));
370 	memcpy((char *)csw, buf, USB_BULK_CS_WRAP_LEN);
371 	debug("csw: signature:%x\n", csw->signature);
372 	debug("csw: tag:%x\n", csw->tag);
373 	debug("csw: residue:%x\n", csw->residue);
374 	debug("csw: status:%x\n", csw->status);
375 }
376 #endif
377 
378 static int rockusb_tx_write_csw(u32 tag, int residue, u8 status, int size)
379 {
380 	ALLOC_CACHE_ALIGN_BUFFER(struct bulk_cs_wrap, csw,
381 				 sizeof(struct bulk_cs_wrap));
382 	csw->signature = cpu_to_le32(USB_BULK_CS_SIG);
383 	csw->tag = tag;
384 	csw->residue = cpu_to_be32(residue);
385 	csw->status = status;
386 #ifdef DEBUG
387 	printcsw((char *)csw);
388 #endif
389 	return rockusb_tx_write((char *)csw, size);
390 }
391 
392 static void tx_handler_send_csw(struct usb_ep *ep, struct usb_request *req)
393 {
394 	struct f_rockusb *f_rkusb = get_rkusb();
395 	int status = req->status;
396 
397 	if (status)
398 		debug("status: %d ep '%s' trans: %d\n",
399 		      status, ep->name, req->actual);
400 
401 	/* Return back to default in_req complete function after sending CSW */
402 	req->complete = rockusb_complete;
403 	rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD, USB_BULK_CS_WRAP_LEN);
404 }
405 
406 static unsigned int rx_bytes_expected(struct usb_ep *ep)
407 {
408 	struct f_rockusb *f_rkusb = get_rkusb();
409 	int rx_remain = f_rkusb->dl_size - f_rkusb->dl_bytes;
410 	unsigned int rem;
411 	unsigned int maxpacket = ep->maxpacket;
412 
413 	if (rx_remain <= 0)
414 		return 0;
415 	else if (rx_remain > EP_BUFFER_SIZE)
416 		return EP_BUFFER_SIZE;
417 
418 	rem = rx_remain % maxpacket;
419 	if (rem > 0)
420 		rx_remain = rx_remain + (maxpacket - rem);
421 
422 	return rx_remain;
423 }
424 
425 /* usb_request complete call back to handle upload image */
426 static void tx_handler_ul_image(struct usb_ep *ep, struct usb_request *req)
427 {
428 	ALLOC_CACHE_ALIGN_BUFFER(char, rbuffer, RKBLOCK_BUF_SIZE);
429 	struct f_rockusb *f_rkusb = get_rkusb();
430 	struct usb_request *in_req = rockusb_func->in_req;
431 	int ret;
432 
433 	/* Print error status of previous transfer */
434 	if (req->status)
435 		debug("status: %d ep '%s' trans: %d len %d\n", req->status,
436 		      ep->name, req->actual, req->length);
437 
438 	/* On transfer complete reset in_req and feedback host with CSW_GOOD */
439 	if (f_rkusb->ul_bytes >= f_rkusb->ul_size) {
440 		in_req->length = 0;
441 		in_req->complete = rockusb_complete;
442 
443 		rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
444 				     USB_BULK_CS_WRAP_LEN);
445 		return;
446 	}
447 
448 	/* Proceed with current chunk */
449 	unsigned int transfer_size = f_rkusb->ul_size - f_rkusb->ul_bytes;
450 
451 	if (transfer_size > RKBLOCK_BUF_SIZE)
452 		transfer_size = RKBLOCK_BUF_SIZE;
453 	/* Read at least one block */
454 	unsigned int blkcount = (transfer_size + f_rkusb->desc->blksz - 1) /
455 				f_rkusb->desc->blksz;
456 
457 	debug("ul %x bytes, %x blks, read lba %x, ul_size:%x, ul_bytes:%x, ",
458 	      transfer_size, blkcount, f_rkusb->lba,
459 	      f_rkusb->ul_size, f_rkusb->ul_bytes);
460 
461 	int blks = blk_dread(f_rkusb->desc, f_rkusb->lba, blkcount, rbuffer);
462 
463 	if (blks != blkcount) {
464 		printf("failed reading from device %s: %d\n",
465 		       f_rkusb->dev_type, f_rkusb->dev_index);
466 		rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
467 				     USB_BULK_CS_WRAP_LEN);
468 		return;
469 	}
470 	f_rkusb->lba += blkcount;
471 	f_rkusb->ul_bytes += transfer_size;
472 
473 	/* Proceed with USB request */
474 	memcpy(in_req->buf, rbuffer, transfer_size);
475 	in_req->length = transfer_size;
476 	in_req->complete = tx_handler_ul_image;
477 	debug("Uploading 0x%x bytes\n", transfer_size);
478 	usb_ep_dequeue(rockusb_func->in_ep, in_req);
479 	ret = usb_ep_queue(rockusb_func->in_ep, in_req, 0);
480 	if (ret)
481 		printf("Error %d on queue\n", ret);
482 }
483 
484 /* usb_request complete call back to handle down load image */
485 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
486 {
487 	struct f_rockusb *f_rkusb = get_rkusb();
488 	unsigned int transfer_size = 0;
489 	const unsigned char *buffer = req->buf;
490 	unsigned int buffer_size = req->actual;
491 
492 	transfer_size = f_rkusb->dl_size - f_rkusb->dl_bytes;
493 
494 	if (req->status != 0) {
495 		printf("Bad status: %d\n", req->status);
496 		rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
497 				     USB_BULK_CS_WRAP_LEN);
498 		return;
499 	}
500 
501 	if (buffer_size < transfer_size)
502 		transfer_size = buffer_size;
503 
504 	memcpy((void *)f_rkusb->buf, buffer, transfer_size);
505 	f_rkusb->dl_bytes += transfer_size;
506 	int blks = 0, blkcnt = transfer_size  / f_rkusb->desc->blksz;
507 
508 	debug("dl %x bytes, %x blks, write lba %x, dl_size:%x, dl_bytes:%x, ",
509 	      transfer_size, blkcnt, f_rkusb->lba, f_rkusb->dl_size,
510 	      f_rkusb->dl_bytes);
511 	blks = blk_dwrite(f_rkusb->desc, f_rkusb->lba, blkcnt, f_rkusb->buf);
512 	if (blks != blkcnt) {
513 		printf("failed writing to device %s: %d\n", f_rkusb->dev_type,
514 		       f_rkusb->dev_index);
515 		rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
516 				     USB_BULK_CS_WRAP_LEN);
517 		return;
518 	}
519 	f_rkusb->lba += blkcnt;
520 
521 	/* Check if transfer is done */
522 	if (f_rkusb->dl_bytes >= f_rkusb->dl_size) {
523 		req->complete = rx_handler_command;
524 		req->length = EP_BUFFER_SIZE;
525 		f_rkusb->buf = f_rkusb->buf_head;
526 		debug("transfer 0x%x bytes done\n", f_rkusb->dl_size);
527 		f_rkusb->dl_size = 0;
528 		rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_GOOD,
529 				     USB_BULK_CS_WRAP_LEN);
530 	} else {
531 		req->length = rx_bytes_expected(ep);
532 		if (f_rkusb->buf == f_rkusb->buf_head)
533 			f_rkusb->buf = f_rkusb->buf_head + EP_BUFFER_SIZE;
534 		else
535 			f_rkusb->buf = f_rkusb->buf_head;
536 
537 		debug("remain %x bytes, %lx sectors\n", req->length,
538 		      req->length / f_rkusb->desc->blksz);
539 	}
540 
541 	req->actual = 0;
542 	usb_ep_queue(ep, req, 0);
543 }
544 
545 static void cb_test_unit_ready(struct usb_ep *ep, struct usb_request *req)
546 {
547 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
548 				 sizeof(struct fsg_bulk_cb_wrap));
549 
550 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
551 
552 	rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
553 			     CSW_GOOD, USB_BULK_CS_WRAP_LEN);
554 }
555 
556 static void cb_read_storage_id(struct usb_ep *ep, struct usb_request *req)
557 {
558 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
559 				 sizeof(struct fsg_bulk_cb_wrap));
560 	struct f_rockusb *f_rkusb = get_rkusb();
561 	char emmc_id[] = "EMMC ";
562 
563 	printf("read storage id\n");
564 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
565 
566 	/* Prepare for sending subsequent CSW_GOOD */
567 	f_rkusb->tag = cbw->tag;
568 	f_rkusb->in_req->complete = tx_handler_send_csw;
569 
570 	rockusb_tx_write_str(emmc_id);
571 }
572 
573 int __weak rk_get_bootrom_chip_version(unsigned int *chip_info, int size)
574 {
575 	return 0;
576 }
577 
578 static void cb_get_chip_version(struct usb_ep *ep, struct usb_request *req)
579 {
580 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
581 				 sizeof(struct fsg_bulk_cb_wrap));
582 	struct f_rockusb *f_rkusb = get_rkusb();
583 	unsigned int chip_info[4], i;
584 
585 	memset(chip_info, 0, sizeof(chip_info));
586 	rk_get_bootrom_chip_version(chip_info, 4);
587 
588 	/*
589 	 * Chip Version is a string saved in BOOTROM address space Little Endian
590 	 *
591 	 * Ex for rk3288: 0x33323041 0x32303134 0x30383133 0x56323030
592 	 * which brings:  320A20140813V200
593 	 *
594 	 * Note that memory version do invert MSB/LSB so printing the char
595 	 * buffer will show: A02341023180002V
596 	 */
597 	printf("read chip version: ");
598 	for (i = 0; i < 4; i++) {
599 		printf("%c%c%c%c",
600 		       (chip_info[i] >> 24) & 0xFF,
601 		       (chip_info[i] >> 16) & 0xFF,
602 		       (chip_info[i] >>  8) & 0xFF,
603 		       (chip_info[i] >>  0) & 0xFF);
604 	}
605 	printf("\n");
606 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
607 
608 	/* Prepare for sending subsequent CSW_GOOD */
609 	f_rkusb->tag = cbw->tag;
610 	f_rkusb->in_req->complete = tx_handler_send_csw;
611 
612 	rockusb_tx_write((char *)chip_info, sizeof(chip_info));
613 }
614 
615 static void cb_read_lba(struct usb_ep *ep, struct usb_request *req)
616 {
617 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
618 				 sizeof(struct fsg_bulk_cb_wrap));
619 	struct f_rockusb *f_rkusb = get_rkusb();
620 	int sector_count;
621 
622 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
623 	sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
624 	f_rkusb->tag = cbw->tag;
625 
626 	if (!f_rkusb->desc) {
627 		char *type = f_rkusb->dev_type;
628 		int index = f_rkusb->dev_index;
629 
630 		f_rkusb->desc = blk_get_dev(type, index);
631 		if (!f_rkusb->desc ||
632 		    f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
633 			printf("invalid device \"%s\", %d\n", type, index);
634 			rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
635 					     USB_BULK_CS_WRAP_LEN);
636 			return;
637 		}
638 	}
639 
640 	f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
641 	f_rkusb->ul_size = sector_count * f_rkusb->desc->blksz;
642 	f_rkusb->ul_bytes = 0;
643 
644 	debug("require read %x bytes, %x sectors from lba %x\n",
645 	      f_rkusb->ul_size, sector_count, f_rkusb->lba);
646 
647 	if (f_rkusb->ul_size == 0)  {
648 		rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
649 				     CSW_FAIL, USB_BULK_CS_WRAP_LEN);
650 		return;
651 	}
652 
653 	/* Start right now sending first chunk */
654 	tx_handler_ul_image(ep, req);
655 }
656 
657 static void cb_write_lba(struct usb_ep *ep, struct usb_request *req)
658 {
659 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
660 				 sizeof(struct fsg_bulk_cb_wrap));
661 	struct f_rockusb *f_rkusb = get_rkusb();
662 	int sector_count;
663 
664 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
665 	sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
666 	f_rkusb->tag = cbw->tag;
667 
668 	if (!f_rkusb->desc) {
669 		char *type = f_rkusb->dev_type;
670 		int index = f_rkusb->dev_index;
671 
672 		f_rkusb->desc = blk_get_dev(type, index);
673 		if (!f_rkusb->desc ||
674 		    f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
675 			printf("invalid device \"%s\", %d\n", type, index);
676 			rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
677 					     USB_BULK_CS_WRAP_LEN);
678 			return;
679 		}
680 	}
681 
682 	f_rkusb->lba = get_unaligned_be32(&cbw->CDB[2]);
683 	f_rkusb->dl_size = sector_count * f_rkusb->desc->blksz;
684 	f_rkusb->dl_bytes = 0;
685 
686 	debug("require write %x bytes, %x sectors to lba %x\n",
687 	      f_rkusb->dl_size, sector_count, f_rkusb->lba);
688 
689 	if (f_rkusb->dl_size == 0)  {
690 		rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length,
691 				     CSW_FAIL, USB_BULK_CS_WRAP_LEN);
692 	} else {
693 		req->complete = rx_handler_dl_image;
694 		req->length = rx_bytes_expected(ep);
695 	}
696 }
697 
698 static void cb_erase_lba(struct usb_ep *ep, struct usb_request *req)
699 {
700 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
701 				 sizeof(struct fsg_bulk_cb_wrap));
702 	struct f_rockusb *f_rkusb = get_rkusb();
703 	int sector_count, lba, blks;
704 
705 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
706 	sector_count = (int)get_unaligned_be16(&cbw->CDB[7]);
707 	f_rkusb->tag = cbw->tag;
708 
709 	if (!f_rkusb->desc) {
710 		char *type = f_rkusb->dev_type;
711 		int index = f_rkusb->dev_index;
712 
713 		f_rkusb->desc = blk_get_dev(type, index);
714 		if (!f_rkusb->desc ||
715 		    f_rkusb->desc->type == DEV_TYPE_UNKNOWN) {
716 			printf("invalid device \"%s\", %d\n", type, index);
717 			rockusb_tx_write_csw(f_rkusb->tag, 0, CSW_FAIL,
718 					     USB_BULK_CS_WRAP_LEN);
719 			return;
720 		}
721 	}
722 
723 	lba = get_unaligned_be32(&cbw->CDB[2]);
724 
725 	debug("require erase %x sectors from lba %x\n",
726 	      sector_count, lba);
727 
728 	blks = blk_derase(f_rkusb->desc, lba, sector_count);
729 	if (blks != sector_count) {
730 		printf("failed erasing device %s: %d\n", f_rkusb->dev_type,
731 		       f_rkusb->dev_index);
732 		rockusb_tx_write_csw(f_rkusb->tag,
733 				     cbw->data_transfer_length, CSW_FAIL,
734 				     USB_BULK_CS_WRAP_LEN);
735 		return;
736 	}
737 
738 	rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
739 			     USB_BULK_CS_WRAP_LEN);
740 }
741 
742 void __weak rkusb_set_reboot_flag(int flag)
743 {
744 	struct f_rockusb *f_rkusb = get_rkusb();
745 
746 	printf("rockkusb set reboot flag: %d\n", f_rkusb->reboot_flag);
747 }
748 
749 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
750 {
751 	struct f_rockusb *f_rkusb = get_rkusb();
752 
753 	rkusb_set_reboot_flag(f_rkusb->reboot_flag);
754 	do_reset(NULL, 0, 0, NULL);
755 }
756 
757 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
758 {
759 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
760 				 sizeof(struct fsg_bulk_cb_wrap));
761 	struct f_rockusb *f_rkusb = get_rkusb();
762 
763 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
764 	f_rkusb->reboot_flag = cbw->CDB[1];
765 	rockusb_func->in_req->complete = compl_do_reset;
766 	rockusb_tx_write_csw(cbw->tag, cbw->data_transfer_length, CSW_GOOD,
767 			     USB_BULK_CS_WRAP_LEN);
768 }
769 
770 static void cb_not_support(struct usb_ep *ep, struct usb_request *req)
771 {
772 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
773 				 sizeof(struct fsg_bulk_cb_wrap));
774 
775 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
776 	printf("Rockusb command %x not support yet\n", cbw->CDB[0]);
777 	rockusb_tx_write_csw(cbw->tag, 0, CSW_FAIL, USB_BULK_CS_WRAP_LEN);
778 }
779 
780 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
781 	{
782 		.cmd = K_FW_TEST_UNIT_READY,
783 		.cb = cb_test_unit_ready,
784 	},
785 	{
786 		.cmd = K_FW_READ_FLASH_ID,
787 		.cb = cb_read_storage_id,
788 	},
789 	{
790 		.cmd = K_FW_SET_DEVICE_ID,
791 		.cb = cb_not_support,
792 	},
793 	{
794 		.cmd = K_FW_TEST_BAD_BLOCK,
795 		.cb = cb_not_support,
796 	},
797 	{
798 		.cmd = K_FW_READ_10,
799 		.cb = cb_not_support,
800 	},
801 	{
802 		.cmd = K_FW_WRITE_10,
803 		.cb = cb_not_support,
804 	},
805 	{
806 		.cmd = K_FW_ERASE_10,
807 		.cb = cb_not_support,
808 	},
809 	{
810 		.cmd = K_FW_WRITE_SPARE,
811 		.cb = cb_not_support,
812 	},
813 	{
814 		.cmd = K_FW_READ_SPARE,
815 		.cb = cb_not_support,
816 	},
817 	{
818 		.cmd = K_FW_ERASE_10_FORCE,
819 		.cb = cb_not_support,
820 	},
821 	{
822 		.cmd = K_FW_GET_VERSION,
823 		.cb = cb_not_support,
824 	},
825 	{
826 		.cmd = K_FW_LBA_READ_10,
827 		.cb = cb_read_lba,
828 	},
829 	{
830 		.cmd = K_FW_LBA_WRITE_10,
831 		.cb = cb_write_lba,
832 	},
833 	{
834 		.cmd = K_FW_ERASE_SYS_DISK,
835 		.cb = cb_not_support,
836 	},
837 	{
838 		.cmd = K_FW_SDRAM_READ_10,
839 		.cb = cb_not_support,
840 	},
841 	{
842 		.cmd = K_FW_SDRAM_WRITE_10,
843 		.cb = cb_not_support,
844 	},
845 	{
846 		.cmd = K_FW_SDRAM_EXECUTE,
847 		.cb = cb_not_support,
848 	},
849 	{
850 		.cmd = K_FW_READ_FLASH_INFO,
851 		.cb = cb_not_support,
852 	},
853 	{
854 		.cmd = K_FW_GET_CHIP_VER,
855 		.cb = cb_get_chip_version,
856 	},
857 	{
858 		.cmd = K_FW_LOW_FORMAT,
859 		.cb = cb_not_support,
860 	},
861 	{
862 		.cmd = K_FW_SET_RESET_FLAG,
863 		.cb = cb_not_support,
864 	},
865 	{
866 		.cmd = K_FW_SPI_READ_10,
867 		.cb = cb_not_support,
868 	},
869 	{
870 		.cmd = K_FW_SPI_WRITE_10,
871 		.cb = cb_not_support,
872 	},
873 	{
874 		.cmd = K_FW_LBA_ERASE_10,
875 		.cb = cb_erase_lba,
876 	},
877 	{
878 		.cmd = K_FW_SESSION,
879 		.cb = cb_not_support,
880 	},
881 	{
882 		.cmd = K_FW_RESET,
883 		.cb = cb_reboot,
884 	},
885 };
886 
887 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
888 {
889 	void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
890 
891 	ALLOC_CACHE_ALIGN_BUFFER(struct fsg_bulk_cb_wrap, cbw,
892 				 sizeof(struct fsg_bulk_cb_wrap));
893 	char *cmdbuf = req->buf;
894 	int i;
895 
896 	if (req->status || req->length == 0)
897 		return;
898 
899 	memcpy((char *)cbw, req->buf, USB_BULK_CB_WRAP_LEN);
900 #ifdef DEBUG
901 	printcbw(req->buf);
902 #endif
903 
904 	for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
905 		if (cmd_dispatch_info[i].cmd == cbw->CDB[0]) {
906 			func_cb = cmd_dispatch_info[i].cb;
907 			break;
908 		}
909 	}
910 
911 	if (!func_cb) {
912 		printf("unknown command: %s\n", (char *)req->buf);
913 		rockusb_tx_write_str("FAILunknown command");
914 	} else {
915 		if (req->actual < req->length) {
916 			u8 *buf = (u8 *)req->buf;
917 
918 			buf[req->actual] = 0;
919 			func_cb(ep, req);
920 		} else {
921 			puts("buffer overflow\n");
922 			rockusb_tx_write_str("FAILbuffer overflow");
923 		}
924 	}
925 
926 	*cmdbuf = '\0';
927 	req->actual = 0;
928 	usb_ep_queue(ep, req, 0);
929 }
930