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