1 /* 2 * f_thor.c -- USB TIZEN THOR Downloader gadget function 3 * 4 * Copyright (C) 2013 Samsung Electronics 5 * Lukasz Majewski <l.majewski@samsung.com> 6 * 7 * Based on code from: 8 * git://review.tizen.org/kernel/u-boot 9 * 10 * Developed by: 11 * Copyright (C) 2009 Samsung Electronics 12 * Minkyu Kang <mk7.kang@samsung.com> 13 * Sanghee Kim <sh0130.kim@samsung.com> 14 * 15 * SPDX-License-Identifier: GPL-2.0+ 16 */ 17 18 #include <errno.h> 19 #include <common.h> 20 #include <malloc.h> 21 #include <version.h> 22 #include <linux/usb/ch9.h> 23 #include <linux/usb/gadget.h> 24 #include <linux/usb/composite.h> 25 #include <linux/usb/cdc.h> 26 #include <g_dnl.h> 27 #include <dfu.h> 28 29 #include "f_thor.h" 30 31 static void thor_tx_data(unsigned char *data, int len); 32 static void thor_set_dma(void *addr, int len); 33 static int thor_rx_data(void); 34 35 static struct f_thor *thor_func; 36 static inline struct f_thor *func_to_thor(struct usb_function *f) 37 { 38 return container_of(f, struct f_thor, usb_function); 39 } 40 41 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf, 42 sizeof(struct rsp_box)); 43 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf, 44 sizeof(struct rqt_box)); 45 46 /* ********************************************************** */ 47 /* THOR protocol - transmission handling */ 48 /* ********************************************************** */ 49 DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE); 50 static unsigned long long int thor_file_size; 51 static int alt_setting_num; 52 53 static void send_rsp(const struct rsp_box *rsp) 54 { 55 memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box)); 56 thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box)); 57 58 debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data); 59 } 60 61 static void send_data_rsp(s32 ack, s32 count) 62 { 63 ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp, 64 sizeof(struct data_rsp_box)); 65 66 rsp->ack = ack; 67 rsp->count = count; 68 69 memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box)); 70 thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box)); 71 72 debug("-DATA RSP: %d, %d\n", ack, count); 73 } 74 75 static int process_rqt_info(const struct rqt_box *rqt) 76 { 77 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); 78 memset(rsp, 0, sizeof(struct rsp_box)); 79 80 rsp->rsp = rqt->rqt; 81 rsp->rsp_data = rqt->rqt_data; 82 83 switch (rqt->rqt_data) { 84 case RQT_INFO_VER_PROTOCOL: 85 rsp->int_data[0] = VER_PROTOCOL_MAJOR; 86 rsp->int_data[1] = VER_PROTOCOL_MINOR; 87 break; 88 case RQT_INIT_VER_HW: 89 snprintf(rsp->str_data[0], sizeof(rsp->str_data[0]), 90 "%x", checkboard()); 91 break; 92 case RQT_INIT_VER_BOOT: 93 sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION); 94 break; 95 case RQT_INIT_VER_KERNEL: 96 sprintf(rsp->str_data[0], "%s", "k unknown"); 97 break; 98 case RQT_INIT_VER_PLATFORM: 99 sprintf(rsp->str_data[0], "%s", "p unknown"); 100 break; 101 case RQT_INIT_VER_CSC: 102 sprintf(rsp->str_data[0], "%s", "c unknown"); 103 break; 104 default: 105 return -EINVAL; 106 } 107 108 send_rsp(rsp); 109 return true; 110 } 111 112 static int process_rqt_cmd(const struct rqt_box *rqt) 113 { 114 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); 115 memset(rsp, 0, sizeof(struct rsp_box)); 116 117 rsp->rsp = rqt->rqt; 118 rsp->rsp_data = rqt->rqt_data; 119 120 switch (rqt->rqt_data) { 121 case RQT_CMD_REBOOT: 122 debug("TARGET RESET\n"); 123 send_rsp(rsp); 124 g_dnl_unregister(); 125 dfu_free_entities(); 126 #ifdef CONFIG_THOR_RESET_OFF 127 return RESET_DONE; 128 #endif 129 run_command("reset", 0); 130 break; 131 case RQT_CMD_POWEROFF: 132 case RQT_CMD_EFSCLEAR: 133 send_rsp(rsp); 134 default: 135 printf("Command not supported -> cmd: %d\n", rqt->rqt_data); 136 return -EINVAL; 137 } 138 139 return true; 140 } 141 142 static long long int download_head(unsigned long long total, 143 unsigned int packet_size, 144 long long int *left, 145 int *cnt) 146 { 147 long long int rcv_cnt = 0, left_to_rcv, ret_rcv; 148 struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num); 149 void *transfer_buffer = dfu_get_buf(dfu_entity); 150 void *buf = transfer_buffer; 151 int usb_pkt_cnt = 0, ret; 152 153 /* 154 * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on 155 * the medium. 156 * The packet response is sent on the purpose after successful data 157 * chunk write. There is a room for improvement when asynchronous write 158 * is performed. 159 */ 160 while (total - rcv_cnt >= packet_size) { 161 thor_set_dma(buf, packet_size); 162 buf += packet_size; 163 ret_rcv = thor_rx_data(); 164 if (ret_rcv < 0) 165 return ret_rcv; 166 rcv_cnt += ret_rcv; 167 debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt, 168 rcv_cnt, *cnt); 169 170 if ((rcv_cnt % THOR_STORE_UNIT_SIZE) == 0) { 171 ret = dfu_write(dfu_get_entity(alt_setting_num), 172 transfer_buffer, THOR_STORE_UNIT_SIZE, 173 (*cnt)++); 174 if (ret) { 175 error("DFU write failed [%d] cnt: %d", 176 ret, *cnt); 177 return ret; 178 } 179 buf = transfer_buffer; 180 } 181 send_data_rsp(0, ++usb_pkt_cnt); 182 } 183 184 /* Calculate the amount of data to arrive from PC (in bytes) */ 185 left_to_rcv = total - rcv_cnt; 186 187 /* 188 * Calculate number of data already received. but not yet stored 189 * on the medium (they are smaller than THOR_STORE_UNIT_SIZE) 190 */ 191 *left = left_to_rcv + buf - transfer_buffer; 192 debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__, 193 *left, left_to_rcv, buf); 194 195 if (left_to_rcv) { 196 thor_set_dma(buf, packet_size); 197 ret_rcv = thor_rx_data(); 198 if (ret_rcv < 0) 199 return ret_rcv; 200 rcv_cnt += ret_rcv; 201 send_data_rsp(0, ++usb_pkt_cnt); 202 } 203 204 debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt); 205 206 return rcv_cnt; 207 } 208 209 static int download_tail(long long int left, int cnt) 210 { 211 struct dfu_entity *dfu_entity; 212 void *transfer_buffer; 213 int ret; 214 215 debug("%s: left: %llu cnt: %d\n", __func__, left, cnt); 216 217 dfu_entity = dfu_get_entity(alt_setting_num); 218 if (!dfu_entity) { 219 error("Alt setting: %d entity not found!\n", alt_setting_num); 220 return -ENOENT; 221 } 222 223 transfer_buffer = dfu_get_buf(dfu_entity); 224 if (!transfer_buffer) { 225 error("Transfer buffer not allocated!"); 226 return -ENXIO; 227 } 228 229 if (left) { 230 ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++); 231 if (ret) { 232 error("DFU write failed [%d]: left: %llu", ret, left); 233 return ret; 234 } 235 } 236 237 /* 238 * To store last "packet" or write file from buffer to filesystem 239 * DFU storage backend requires dfu_flush 240 * 241 * This also frees memory malloc'ed by dfu_get_buf(), so no explicit 242 * need fo call dfu_free_buf() is needed. 243 */ 244 ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt); 245 if (ret) 246 error("DFU flush failed!"); 247 248 return ret; 249 } 250 251 static long long int process_rqt_download(const struct rqt_box *rqt) 252 { 253 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box)); 254 static long long int left, ret_head; 255 int file_type, ret = 0; 256 static int cnt; 257 258 memset(rsp, 0, sizeof(struct rsp_box)); 259 rsp->rsp = rqt->rqt; 260 rsp->rsp_data = rqt->rqt_data; 261 262 switch (rqt->rqt_data) { 263 case RQT_DL_INIT: 264 thor_file_size = rqt->int_data[0]; 265 debug("INIT: total %d bytes\n", rqt->int_data[0]); 266 break; 267 case RQT_DL_FILE_INFO: 268 file_type = rqt->int_data[0]; 269 if (file_type == FILE_TYPE_PIT) { 270 puts("PIT table file - not supported\n"); 271 rsp->ack = -ENOTSUPP; 272 ret = rsp->ack; 273 break; 274 } 275 276 thor_file_size = rqt->int_data[1]; 277 memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE); 278 279 debug("INFO: name(%s, %d), size(%llu), type(%d)\n", 280 f_name, 0, thor_file_size, file_type); 281 282 rsp->int_data[0] = THOR_PACKET_SIZE; 283 284 alt_setting_num = dfu_get_alt(f_name); 285 if (alt_setting_num < 0) { 286 error("Alt setting [%d] to write not found!", 287 alt_setting_num); 288 rsp->ack = -ENODEV; 289 ret = rsp->ack; 290 } 291 break; 292 case RQT_DL_FILE_START: 293 send_rsp(rsp); 294 ret_head = download_head(thor_file_size, THOR_PACKET_SIZE, 295 &left, &cnt); 296 if (ret_head < 0) { 297 left = 0; 298 cnt = 0; 299 } 300 return ret_head; 301 case RQT_DL_FILE_END: 302 debug("DL FILE_END\n"); 303 rsp->ack = download_tail(left, cnt); 304 ret = rsp->ack; 305 left = 0; 306 cnt = 0; 307 break; 308 case RQT_DL_EXIT: 309 debug("DL EXIT\n"); 310 break; 311 default: 312 error("Operation not supported: %d", rqt->rqt_data); 313 ret = -ENOTSUPP; 314 } 315 316 send_rsp(rsp); 317 return ret; 318 } 319 320 static int process_data(void) 321 { 322 ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box)); 323 int ret = -EINVAL; 324 325 memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box)); 326 327 debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data); 328 329 switch (rqt->rqt) { 330 case RQT_INFO: 331 ret = process_rqt_info(rqt); 332 break; 333 case RQT_CMD: 334 ret = process_rqt_cmd(rqt); 335 break; 336 case RQT_DL: 337 ret = (int) process_rqt_download(rqt); 338 break; 339 case RQT_UL: 340 puts("RQT: UPLOAD not supported!\n"); 341 break; 342 default: 343 error("unknown request (%d)", rqt->rqt); 344 } 345 346 return ret; 347 } 348 349 /* ********************************************************** */ 350 /* THOR USB Function */ 351 /* ********************************************************** */ 352 353 static inline struct usb_endpoint_descriptor * 354 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, 355 struct usb_endpoint_descriptor *fs) 356 { 357 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 358 return hs; 359 return fs; 360 } 361 362 static struct usb_interface_descriptor thor_downloader_intf_data = { 363 .bLength = sizeof(thor_downloader_intf_data), 364 .bDescriptorType = USB_DT_INTERFACE, 365 366 .bNumEndpoints = 2, 367 .bInterfaceClass = USB_CLASS_CDC_DATA, 368 }; 369 370 static struct usb_endpoint_descriptor fs_in_desc = { 371 .bLength = USB_DT_ENDPOINT_SIZE, 372 .bDescriptorType = USB_DT_ENDPOINT, 373 374 .bEndpointAddress = USB_DIR_IN, 375 .bmAttributes = USB_ENDPOINT_XFER_BULK, 376 }; 377 378 static struct usb_endpoint_descriptor fs_out_desc = { 379 .bLength = USB_DT_ENDPOINT_SIZE, 380 .bDescriptorType = USB_DT_ENDPOINT, 381 382 .bEndpointAddress = USB_DIR_OUT, 383 .bmAttributes = USB_ENDPOINT_XFER_BULK, 384 }; 385 386 /* CDC configuration */ 387 static struct usb_interface_descriptor thor_downloader_intf_int = { 388 .bLength = sizeof(thor_downloader_intf_int), 389 .bDescriptorType = USB_DT_INTERFACE, 390 391 .bNumEndpoints = 1, 392 .bInterfaceClass = USB_CLASS_COMM, 393 /* 0x02 Abstract Line Control Model */ 394 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, 395 /* 0x01 Common AT commands */ 396 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, 397 }; 398 399 static struct usb_cdc_header_desc thor_downloader_cdc_header = { 400 .bLength = sizeof(thor_downloader_cdc_header), 401 .bDescriptorType = 0x24, /* CS_INTERFACE */ 402 .bDescriptorSubType = 0x00, 403 .bcdCDC = 0x0110, 404 }; 405 406 static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = { 407 .bLength = sizeof(thor_downloader_cdc_call), 408 .bDescriptorType = 0x24, /* CS_INTERFACE */ 409 .bDescriptorSubType = 0x01, 410 .bmCapabilities = 0x00, 411 .bDataInterface = 0x01, 412 }; 413 414 static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = { 415 .bLength = sizeof(thor_downloader_cdc_abstract), 416 .bDescriptorType = 0x24, /* CS_INTERFACE */ 417 .bDescriptorSubType = 0x02, 418 .bmCapabilities = 0x00, 419 }; 420 421 static struct usb_cdc_union_desc thor_downloader_cdc_union = { 422 .bLength = sizeof(thor_downloader_cdc_union), 423 .bDescriptorType = 0x24, /* CS_INTERFACE */ 424 .bDescriptorSubType = USB_CDC_UNION_TYPE, 425 }; 426 427 static struct usb_endpoint_descriptor fs_int_desc = { 428 .bLength = USB_DT_ENDPOINT_SIZE, 429 .bDescriptorType = USB_DT_ENDPOINT, 430 431 .bEndpointAddress = 3 | USB_DIR_IN, 432 .bmAttributes = USB_ENDPOINT_XFER_INT, 433 .wMaxPacketSize = __constant_cpu_to_le16(16), 434 435 .bInterval = 0x9, 436 }; 437 438 static struct usb_interface_assoc_descriptor 439 thor_iad_descriptor = { 440 .bLength = sizeof(thor_iad_descriptor), 441 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, 442 443 .bFirstInterface = 0, 444 .bInterfaceCount = 2, /* control + data */ 445 .bFunctionClass = USB_CLASS_COMM, 446 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM, 447 .bFunctionProtocol = USB_CDC_PROTO_NONE, 448 }; 449 450 static struct usb_endpoint_descriptor hs_in_desc = { 451 .bLength = USB_DT_ENDPOINT_SIZE, 452 .bDescriptorType = USB_DT_ENDPOINT, 453 454 .bmAttributes = USB_ENDPOINT_XFER_BULK, 455 .wMaxPacketSize = __constant_cpu_to_le16(512), 456 }; 457 458 static struct usb_endpoint_descriptor hs_out_desc = { 459 .bLength = USB_DT_ENDPOINT_SIZE, 460 .bDescriptorType = USB_DT_ENDPOINT, 461 462 .bmAttributes = USB_ENDPOINT_XFER_BULK, 463 .wMaxPacketSize = __constant_cpu_to_le16(512), 464 }; 465 466 static struct usb_endpoint_descriptor hs_int_desc = { 467 .bLength = USB_DT_ENDPOINT_SIZE, 468 .bDescriptorType = USB_DT_ENDPOINT, 469 470 .bmAttributes = USB_ENDPOINT_XFER_INT, 471 .wMaxPacketSize = __constant_cpu_to_le16(16), 472 473 .bInterval = 0x9, 474 }; 475 476 /* 477 * This attribute vendor descriptor is necessary for correct operation with 478 * Windows version of THOR download program 479 * 480 * It prevents windows driver from sending zero lenght packet (ZLP) after 481 * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb 482 */ 483 static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = { 484 .bLength = sizeof(thor_downloader_cdc_av), 485 .bDescriptorType = 0x24, 486 .bDescriptorSubType = 0x80, 487 .DAUType = 0x0002, 488 .DAULength = 0x0001, 489 .DAUValue = 0x00, 490 }; 491 492 static const struct usb_descriptor_header *hs_thor_downloader_function[] = { 493 (struct usb_descriptor_header *)&thor_iad_descriptor, 494 495 (struct usb_descriptor_header *)&thor_downloader_intf_int, 496 (struct usb_descriptor_header *)&thor_downloader_cdc_header, 497 (struct usb_descriptor_header *)&thor_downloader_cdc_call, 498 (struct usb_descriptor_header *)&thor_downloader_cdc_abstract, 499 (struct usb_descriptor_header *)&thor_downloader_cdc_union, 500 (struct usb_descriptor_header *)&hs_int_desc, 501 502 (struct usb_descriptor_header *)&thor_downloader_intf_data, 503 (struct usb_descriptor_header *)&thor_downloader_cdc_av, 504 (struct usb_descriptor_header *)&hs_in_desc, 505 (struct usb_descriptor_header *)&hs_out_desc, 506 NULL, 507 }; 508 509 /*-------------------------------------------------------------------------*/ 510 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) 511 { 512 struct usb_request *req; 513 514 req = usb_ep_alloc_request(ep, 0); 515 if (!req) 516 return req; 517 518 req->length = length; 519 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length); 520 if (!req->buf) { 521 usb_ep_free_request(ep, req); 522 req = NULL; 523 } 524 525 return req; 526 } 527 528 static int thor_rx_data(void) 529 { 530 struct thor_dev *dev = thor_func->dev; 531 int data_to_rx, tmp, status; 532 533 data_to_rx = dev->out_req->length; 534 tmp = data_to_rx; 535 do { 536 dev->out_req->length = data_to_rx; 537 debug("dev->out_req->length:%d dev->rxdata:%d\n", 538 dev->out_req->length, dev->rxdata); 539 540 status = usb_ep_queue(dev->out_ep, dev->out_req, 0); 541 if (status) { 542 error("kill %s: resubmit %d bytes --> %d", 543 dev->out_ep->name, dev->out_req->length, status); 544 usb_ep_set_halt(dev->out_ep); 545 return -EAGAIN; 546 } 547 548 while (!dev->rxdata) { 549 usb_gadget_handle_interrupts(0); 550 if (ctrlc()) 551 return -1; 552 } 553 dev->rxdata = 0; 554 data_to_rx -= dev->out_req->actual; 555 } while (data_to_rx); 556 557 return tmp; 558 } 559 560 static void thor_tx_data(unsigned char *data, int len) 561 { 562 struct thor_dev *dev = thor_func->dev; 563 unsigned char *ptr = dev->in_req->buf; 564 int status; 565 566 memset(ptr, 0, len); 567 memcpy(ptr, data, len); 568 569 dev->in_req->length = len; 570 571 debug("%s: dev->in_req->length:%d to_cpy:%d\n", __func__, 572 dev->in_req->length, sizeof(data)); 573 574 status = usb_ep_queue(dev->in_ep, dev->in_req, 0); 575 if (status) { 576 error("kill %s: resubmit %d bytes --> %d", 577 dev->in_ep->name, dev->in_req->length, status); 578 usb_ep_set_halt(dev->in_ep); 579 } 580 581 /* Wait until tx interrupt received */ 582 while (!dev->txdata) 583 usb_gadget_handle_interrupts(0); 584 585 dev->txdata = 0; 586 } 587 588 static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req) 589 { 590 struct thor_dev *dev = thor_func->dev; 591 int status = req->status; 592 593 debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req); 594 switch (status) { 595 case 0: 596 if (ep == dev->out_ep) 597 dev->rxdata = 1; 598 else 599 dev->txdata = 1; 600 601 break; 602 603 /* this endpoint is normally active while we're configured */ 604 case -ECONNABORTED: /* hardware forced ep reset */ 605 case -ECONNRESET: /* request dequeued */ 606 case -ESHUTDOWN: /* disconnect from host */ 607 case -EREMOTEIO: /* short read */ 608 case -EOVERFLOW: 609 error("ERROR:%d", status); 610 break; 611 } 612 613 debug("%s complete --> %d, %d/%d\n", ep->name, 614 status, req->actual, req->length); 615 } 616 617 static struct usb_request *thor_start_ep(struct usb_ep *ep) 618 { 619 struct usb_request *req; 620 621 req = alloc_ep_req(ep, THOR_PACKET_SIZE); 622 debug("%s: ep:%p req:%p\n", __func__, ep, req); 623 624 if (!req) 625 return NULL; 626 627 memset(req->buf, 0, req->length); 628 req->complete = thor_rx_tx_complete; 629 630 return req; 631 } 632 633 static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req) 634 { 635 if (req->status || req->actual != req->length) 636 debug("setup complete --> %d, %d/%d\n", 637 req->status, req->actual, req->length); 638 } 639 640 static int 641 thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 642 { 643 struct thor_dev *dev = thor_func->dev; 644 struct usb_request *req = dev->req; 645 struct usb_gadget *gadget = dev->gadget; 646 int value = 0; 647 648 u16 len = le16_to_cpu(ctrl->wLength); 649 650 debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n", 651 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex, 652 ctrl->wLength); 653 654 switch (ctrl->bRequest) { 655 case USB_CDC_REQ_SET_CONTROL_LINE_STATE: 656 value = 0; 657 break; 658 case USB_CDC_REQ_SET_LINE_CODING: 659 value = len; 660 /* Line Coding set done = configuration done */ 661 thor_func->dev->configuration_done = 1; 662 break; 663 664 default: 665 error("thor_setup: unknown request: %d", ctrl->bRequest); 666 } 667 668 if (value >= 0) { 669 req->length = value; 670 req->zero = value < len; 671 value = usb_ep_queue(gadget->ep0, req, 0); 672 if (value < 0) { 673 debug("%s: ep_queue: %d\n", __func__, value); 674 req->status = 0; 675 } 676 } 677 678 return value; 679 } 680 681 /* Specific to the THOR protocol */ 682 static void thor_set_dma(void *addr, int len) 683 { 684 struct thor_dev *dev = thor_func->dev; 685 686 debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req); 687 debug("addr:%p, len:%d\n", addr, len); 688 689 dev->out_req->buf = addr; 690 dev->out_req->length = len; 691 } 692 693 int thor_init(void) 694 { 695 struct thor_dev *dev = thor_func->dev; 696 697 /* Wait for a device enumeration and configuration settings */ 698 debug("THOR enumeration/configuration setting....\n"); 699 while (!dev->configuration_done) 700 usb_gadget_handle_interrupts(0); 701 702 thor_set_dma(thor_rx_data_buf, strlen("THOR")); 703 /* detect the download request from Host PC */ 704 if (thor_rx_data() < 0) { 705 printf("%s: Data not received!\n", __func__); 706 return -1; 707 } 708 709 if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) { 710 puts("Download request from the Host PC\n"); 711 udelay(30 * 1000); /* 30 ms */ 712 713 strcpy((char *)thor_tx_data_buf, "ROHT"); 714 thor_tx_data(thor_tx_data_buf, strlen("ROHT")); 715 } else { 716 puts("Wrong reply information\n"); 717 return -1; 718 } 719 720 return 0; 721 } 722 723 int thor_handle(void) 724 { 725 int ret; 726 727 /* receive the data from Host PC */ 728 while (1) { 729 thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box)); 730 ret = thor_rx_data(); 731 732 if (ret > 0) { 733 ret = process_data(); 734 #ifdef CONFIG_THOR_RESET_OFF 735 if (ret == RESET_DONE) 736 break; 737 #endif 738 if (ret < 0) 739 return ret; 740 } else { 741 printf("%s: No data received!\n", __func__); 742 break; 743 } 744 } 745 746 return 0; 747 } 748 749 static int thor_func_bind(struct usb_configuration *c, struct usb_function *f) 750 { 751 struct usb_gadget *gadget = c->cdev->gadget; 752 struct f_thor *f_thor = func_to_thor(f); 753 struct thor_dev *dev; 754 struct usb_ep *ep; 755 int status; 756 757 thor_func = f_thor; 758 dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev)); 759 if (!dev) 760 return -ENOMEM; 761 762 memset(dev, 0, sizeof(*dev)); 763 dev->gadget = gadget; 764 f_thor->dev = dev; 765 766 debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n", 767 __func__, c, f); 768 debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev); 769 770 /* EP0 */ 771 /* preallocate control response and buffer */ 772 dev->req = usb_ep_alloc_request(gadget->ep0, 0); 773 if (!dev->req) { 774 status = -ENOMEM; 775 goto fail; 776 } 777 dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, 778 THOR_PACKET_SIZE); 779 if (!dev->req->buf) { 780 status = -ENOMEM; 781 goto fail; 782 } 783 784 dev->req->complete = thor_setup_complete; 785 786 /* DYNAMIC interface numbers assignments */ 787 status = usb_interface_id(c, f); 788 789 if (status < 0) 790 goto fail; 791 792 thor_downloader_intf_int.bInterfaceNumber = status; 793 thor_downloader_cdc_union.bMasterInterface0 = status; 794 795 status = usb_interface_id(c, f); 796 797 if (status < 0) 798 goto fail; 799 800 thor_downloader_intf_data.bInterfaceNumber = status; 801 thor_downloader_cdc_union.bSlaveInterface0 = status; 802 803 /* allocate instance-specific endpoints */ 804 ep = usb_ep_autoconfig(gadget, &fs_in_desc); 805 if (!ep) { 806 status = -ENODEV; 807 goto fail; 808 } 809 810 if (gadget_is_dualspeed(gadget)) { 811 hs_in_desc.bEndpointAddress = 812 fs_in_desc.bEndpointAddress; 813 } 814 815 dev->in_ep = ep; /* Store IN EP for enabling @ setup */ 816 ep->driver_data = dev; 817 818 ep = usb_ep_autoconfig(gadget, &fs_out_desc); 819 if (!ep) { 820 status = -ENODEV; 821 goto fail; 822 } 823 824 if (gadget_is_dualspeed(gadget)) 825 hs_out_desc.bEndpointAddress = 826 fs_out_desc.bEndpointAddress; 827 828 dev->out_ep = ep; /* Store OUT EP for enabling @ setup */ 829 ep->driver_data = dev; 830 831 ep = usb_ep_autoconfig(gadget, &fs_int_desc); 832 if (!ep) { 833 status = -ENODEV; 834 goto fail; 835 } 836 837 dev->int_ep = ep; 838 ep->driver_data = dev; 839 840 if (gadget_is_dualspeed(gadget)) { 841 hs_int_desc.bEndpointAddress = 842 fs_int_desc.bEndpointAddress; 843 844 f->hs_descriptors = (struct usb_descriptor_header **) 845 &hs_thor_downloader_function; 846 847 if (!f->hs_descriptors) 848 goto fail; 849 } 850 851 debug("%s: out_ep:%p out_req:%p\n", __func__, 852 dev->out_ep, dev->out_req); 853 854 return 0; 855 856 fail: 857 free(dev); 858 return status; 859 } 860 861 static void free_ep_req(struct usb_ep *ep, struct usb_request *req) 862 { 863 free(req->buf); 864 usb_ep_free_request(ep, req); 865 } 866 867 static void thor_unbind(struct usb_configuration *c, struct usb_function *f) 868 { 869 struct f_thor *f_thor = func_to_thor(f); 870 struct thor_dev *dev = f_thor->dev; 871 872 free(dev); 873 memset(thor_func, 0, sizeof(*thor_func)); 874 thor_func = NULL; 875 } 876 877 static void thor_func_disable(struct usb_function *f) 878 { 879 struct f_thor *f_thor = func_to_thor(f); 880 struct thor_dev *dev = f_thor->dev; 881 882 debug("%s:\n", __func__); 883 884 /* Avoid freeing memory when ep is still claimed */ 885 if (dev->in_ep->driver_data) { 886 free_ep_req(dev->in_ep, dev->in_req); 887 usb_ep_disable(dev->in_ep); 888 dev->in_ep->driver_data = NULL; 889 } 890 891 if (dev->out_ep->driver_data) { 892 dev->out_req->buf = NULL; 893 usb_ep_free_request(dev->out_ep, dev->out_req); 894 usb_ep_disable(dev->out_ep); 895 dev->out_ep->driver_data = NULL; 896 } 897 898 if (dev->int_ep->driver_data) { 899 usb_ep_disable(dev->int_ep); 900 dev->int_ep->driver_data = NULL; 901 } 902 } 903 904 static int thor_eps_setup(struct usb_function *f) 905 { 906 struct usb_composite_dev *cdev = f->config->cdev; 907 struct usb_gadget *gadget = cdev->gadget; 908 struct thor_dev *dev = thor_func->dev; 909 struct usb_endpoint_descriptor *d; 910 struct usb_request *req; 911 struct usb_ep *ep; 912 int result; 913 914 ep = dev->in_ep; 915 d = ep_desc(gadget, &hs_in_desc, &fs_in_desc); 916 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress); 917 918 result = usb_ep_enable(ep, d); 919 if (result) 920 goto exit; 921 922 ep->driver_data = cdev; /* claim */ 923 req = thor_start_ep(ep); 924 if (!req) { 925 usb_ep_disable(ep); 926 result = -EIO; 927 goto exit; 928 } 929 930 dev->in_req = req; 931 ep = dev->out_ep; 932 d = ep_desc(gadget, &hs_out_desc, &fs_out_desc); 933 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress); 934 935 result = usb_ep_enable(ep, d); 936 if (result) 937 goto exit; 938 939 ep->driver_data = cdev; /* claim */ 940 req = thor_start_ep(ep); 941 if (!req) { 942 usb_ep_disable(ep); 943 result = -EIO; 944 goto exit; 945 } 946 947 dev->out_req = req; 948 /* ACM control EP */ 949 ep = dev->int_ep; 950 ep->driver_data = cdev; /* claim */ 951 952 exit: 953 return result; 954 } 955 956 static int thor_func_set_alt(struct usb_function *f, 957 unsigned intf, unsigned alt) 958 { 959 struct thor_dev *dev = thor_func->dev; 960 int result; 961 962 debug("%s: func: %s intf: %d alt: %d\n", 963 __func__, f->name, intf, alt); 964 965 switch (intf) { 966 case 0: 967 debug("ACM INTR interface\n"); 968 break; 969 case 1: 970 debug("Communication Data interface\n"); 971 result = thor_eps_setup(f); 972 if (result) 973 error("%s: EPs setup failed!", __func__); 974 dev->configuration_done = 1; 975 break; 976 } 977 978 return 0; 979 } 980 981 static int thor_func_init(struct usb_configuration *c) 982 { 983 struct f_thor *f_thor; 984 int status; 985 986 debug("%s: cdev: 0x%p\n", __func__, c->cdev); 987 988 f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor)); 989 if (!f_thor) 990 return -ENOMEM; 991 992 memset(f_thor, 0, sizeof(*f_thor)); 993 994 f_thor->usb_function.name = "f_thor"; 995 f_thor->usb_function.bind = thor_func_bind; 996 f_thor->usb_function.unbind = thor_unbind; 997 f_thor->usb_function.setup = thor_func_setup; 998 f_thor->usb_function.set_alt = thor_func_set_alt; 999 f_thor->usb_function.disable = thor_func_disable; 1000 1001 status = usb_add_function(c, &f_thor->usb_function); 1002 if (status) 1003 free(f_thor); 1004 1005 return status; 1006 } 1007 1008 int thor_add(struct usb_configuration *c) 1009 { 1010 debug("%s:\n", __func__); 1011 return thor_func_init(c); 1012 } 1013 1014 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add); 1015