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