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