1 // SPDX-License-Identifier: GPL-2.0+
3 * f_thor.c -- USB TIZEN THOR Downloader gadget function
5 * Copyright (C) 2013 Samsung Electronics
9 * git://review.tizen.org/kernel/u-boot
12 * Copyright (C) 2009 Samsung Electronics
23 #include <linux/usb/ch9.h>
24 #include <linux/usb/gadget.h>
25 #include <linux/usb/composite.h>
26 #include <linux/usb/cdc.h>
32 static void thor_tx_data(unsigned char *data, int len);
33 static void thor_set_dma(void *addr, int len);
34 static int thor_rx_data(void);
36 static struct f_thor *thor_func;
37 static inline struct f_thor *func_to_thor(struct usb_function *f)
39 return container_of(f, struct f_thor, usb_function);
42 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf,
43 sizeof(struct rsp_box));
44 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf,
45 sizeof(struct rqt_box));
47 /* ********************************************************** */
48 /* THOR protocol - transmission handling */
49 /* ********************************************************** */
50 DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1);
51 static unsigned long long int thor_file_size;
52 static int alt_setting_num;
54 static void send_rsp(const struct rsp_box *rsp)
56 memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
57 thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box));
59 debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
62 static void send_data_rsp(s32 ack, s32 count)
64 ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp,
65 sizeof(struct data_rsp_box));
70 memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
71 thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box));
73 debug("-DATA RSP: %d, %d\n", ack, count);
76 static int process_rqt_info(const struct rqt_box *rqt)
78 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
79 memset(rsp, 0, sizeof(struct rsp_box));
82 rsp->rsp_data = rqt->rqt_data;
84 switch (rqt->rqt_data) {
85 case RQT_INFO_VER_PROTOCOL:
86 rsp->int_data[0] = VER_PROTOCOL_MAJOR;
87 rsp->int_data[1] = VER_PROTOCOL_MINOR;
90 snprintf(rsp->str_data[0], sizeof(rsp->str_data[0]),
93 case RQT_INIT_VER_BOOT:
94 sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION);
96 case RQT_INIT_VER_KERNEL:
97 sprintf(rsp->str_data[0], "%s", "k unknown");
99 case RQT_INIT_VER_PLATFORM:
100 sprintf(rsp->str_data[0], "%s", "p unknown");
102 case RQT_INIT_VER_CSC:
103 sprintf(rsp->str_data[0], "%s", "c unknown");
113 static int process_rqt_cmd(const struct rqt_box *rqt)
115 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
116 memset(rsp, 0, sizeof(struct rsp_box));
119 rsp->rsp_data = rqt->rqt_data;
121 switch (rqt->rqt_data) {
123 debug("TARGET RESET\n");
127 #ifdef CONFIG_THOR_RESET_OFF
130 run_command("reset", 0);
132 case RQT_CMD_POWEROFF:
133 case RQT_CMD_EFSCLEAR:
136 printf("Command not supported -> cmd: %d\n", rqt->rqt_data);
143 static long long int download_head(unsigned long long total,
144 unsigned int packet_size,
148 long long int rcv_cnt = 0, left_to_rcv, ret_rcv;
149 struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num);
150 void *transfer_buffer = dfu_get_buf(dfu_entity);
151 void *buf = transfer_buffer;
152 int usb_pkt_cnt = 0, ret;
155 * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on
157 * The packet response is sent on the purpose after successful data
158 * chunk write. There is a room for improvement when asynchronous write
161 while (total - rcv_cnt >= packet_size) {
162 thor_set_dma(buf, packet_size);
164 ret_rcv = thor_rx_data();
168 debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt,
171 if ((rcv_cnt % THOR_STORE_UNIT_SIZE) == 0) {
172 ret = dfu_write(dfu_get_entity(alt_setting_num),
173 transfer_buffer, THOR_STORE_UNIT_SIZE,
176 pr_err("DFU write failed [%d] cnt: %d",
180 buf = transfer_buffer;
182 send_data_rsp(0, ++usb_pkt_cnt);
185 /* Calculate the amount of data to arrive from PC (in bytes) */
186 left_to_rcv = total - rcv_cnt;
189 * Calculate number of data already received. but not yet stored
190 * on the medium (they are smaller than THOR_STORE_UNIT_SIZE)
192 *left = left_to_rcv + buf - transfer_buffer;
193 debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__,
194 *left, left_to_rcv, buf);
197 thor_set_dma(buf, packet_size);
198 ret_rcv = thor_rx_data();
202 send_data_rsp(0, ++usb_pkt_cnt);
205 debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
210 static int download_tail(long long int left, int cnt)
212 struct dfu_entity *dfu_entity;
213 void *transfer_buffer;
216 debug("%s: left: %llu cnt: %d\n", __func__, left, cnt);
218 dfu_entity = dfu_get_entity(alt_setting_num);
220 pr_err("Alt setting: %d entity not found!\n", alt_setting_num);
224 transfer_buffer = dfu_get_buf(dfu_entity);
225 if (!transfer_buffer) {
226 pr_err("Transfer buffer not allocated!");
231 ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++);
233 pr_err("DFU write failed [%d]: left: %llu", ret, left);
239 * To store last "packet" or write file from buffer to filesystem
240 * DFU storage backend requires dfu_flush
242 * This also frees memory malloc'ed by dfu_get_buf(), so no explicit
243 * need fo call dfu_free_buf() is needed.
245 ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt);
247 pr_err("DFU flush failed!");
252 static long long int process_rqt_download(const struct rqt_box *rqt)
254 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
255 static long long int left, ret_head;
256 int file_type, ret = 0;
259 memset(rsp, 0, sizeof(struct rsp_box));
261 rsp->rsp_data = rqt->rqt_data;
263 switch (rqt->rqt_data) {
265 thor_file_size = (unsigned long long int)rqt->int_data[0] +
266 (((unsigned long long int)rqt->int_data[1])
268 debug("INIT: total %llu bytes\n", thor_file_size);
270 case RQT_DL_FILE_INFO:
271 file_type = rqt->int_data[0];
272 if (file_type == FILE_TYPE_PIT) {
273 puts("PIT table file - not supported\n");
274 rsp->ack = -ENOTSUPP;
279 thor_file_size = (unsigned long long int)rqt->int_data[1] +
280 (((unsigned long long int)rqt->int_data[2])
282 memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
283 f_name[F_NAME_BUF_SIZE] = '\0';
285 debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
286 f_name, 0, thor_file_size, file_type);
288 rsp->int_data[0] = THOR_PACKET_SIZE;
290 alt_setting_num = dfu_get_alt(f_name);
291 if (alt_setting_num < 0) {
292 pr_err("Alt setting [%d] to write not found!",
298 case RQT_DL_FILE_START:
300 ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
307 case RQT_DL_FILE_END:
308 debug("DL FILE_END\n");
309 rsp->ack = download_tail(left, cnt);
318 pr_err("Operation not supported: %d", rqt->rqt_data);
326 static int process_data(void)
328 ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
331 memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
333 debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
337 ret = process_rqt_info(rqt);
340 ret = process_rqt_cmd(rqt);
343 ret = (int) process_rqt_download(rqt);
346 puts("RQT: UPLOAD not supported!\n");
349 pr_err("unknown request (%d)", rqt->rqt);
355 /* ********************************************************** */
356 /* THOR USB Function */
357 /* ********************************************************** */
359 static inline struct usb_endpoint_descriptor *
360 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
361 struct usb_endpoint_descriptor *fs)
363 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
368 static struct usb_interface_descriptor thor_downloader_intf_data = {
369 .bLength = sizeof(thor_downloader_intf_data),
370 .bDescriptorType = USB_DT_INTERFACE,
373 .bInterfaceClass = USB_CLASS_CDC_DATA,
376 static struct usb_endpoint_descriptor fs_in_desc = {
377 .bLength = USB_DT_ENDPOINT_SIZE,
378 .bDescriptorType = USB_DT_ENDPOINT,
380 .bEndpointAddress = USB_DIR_IN,
381 .bmAttributes = USB_ENDPOINT_XFER_BULK,
384 static struct usb_endpoint_descriptor fs_out_desc = {
385 .bLength = USB_DT_ENDPOINT_SIZE,
386 .bDescriptorType = USB_DT_ENDPOINT,
388 .bEndpointAddress = USB_DIR_OUT,
389 .bmAttributes = USB_ENDPOINT_XFER_BULK,
392 /* CDC configuration */
393 static struct usb_interface_descriptor thor_downloader_intf_int = {
394 .bLength = sizeof(thor_downloader_intf_int),
395 .bDescriptorType = USB_DT_INTERFACE,
398 .bInterfaceClass = USB_CLASS_COMM,
399 /* 0x02 Abstract Line Control Model */
400 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
401 /* 0x01 Common AT commands */
402 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
405 static struct usb_cdc_header_desc thor_downloader_cdc_header = {
406 .bLength = sizeof(thor_downloader_cdc_header),
407 .bDescriptorType = 0x24, /* CS_INTERFACE */
408 .bDescriptorSubType = 0x00,
412 static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = {
413 .bLength = sizeof(thor_downloader_cdc_call),
414 .bDescriptorType = 0x24, /* CS_INTERFACE */
415 .bDescriptorSubType = 0x01,
416 .bmCapabilities = 0x00,
417 .bDataInterface = 0x01,
420 static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = {
421 .bLength = sizeof(thor_downloader_cdc_abstract),
422 .bDescriptorType = 0x24, /* CS_INTERFACE */
423 .bDescriptorSubType = 0x02,
424 .bmCapabilities = 0x00,
427 static struct usb_cdc_union_desc thor_downloader_cdc_union = {
428 .bLength = sizeof(thor_downloader_cdc_union),
429 .bDescriptorType = 0x24, /* CS_INTERFACE */
430 .bDescriptorSubType = USB_CDC_UNION_TYPE,
433 static struct usb_endpoint_descriptor fs_int_desc = {
434 .bLength = USB_DT_ENDPOINT_SIZE,
435 .bDescriptorType = USB_DT_ENDPOINT,
437 .bEndpointAddress = 3 | USB_DIR_IN,
438 .bmAttributes = USB_ENDPOINT_XFER_INT,
439 .wMaxPacketSize = __constant_cpu_to_le16(16),
444 static struct usb_interface_assoc_descriptor
445 thor_iad_descriptor = {
446 .bLength = sizeof(thor_iad_descriptor),
447 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
449 .bFirstInterface = 0,
450 .bInterfaceCount = 2, /* control + data */
451 .bFunctionClass = USB_CLASS_COMM,
452 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
453 .bFunctionProtocol = USB_CDC_PROTO_NONE,
456 static struct usb_endpoint_descriptor hs_in_desc = {
457 .bLength = USB_DT_ENDPOINT_SIZE,
458 .bDescriptorType = USB_DT_ENDPOINT,
460 .bmAttributes = USB_ENDPOINT_XFER_BULK,
461 .wMaxPacketSize = __constant_cpu_to_le16(512),
464 static struct usb_endpoint_descriptor hs_out_desc = {
465 .bLength = USB_DT_ENDPOINT_SIZE,
466 .bDescriptorType = USB_DT_ENDPOINT,
468 .bmAttributes = USB_ENDPOINT_XFER_BULK,
469 .wMaxPacketSize = __constant_cpu_to_le16(512),
472 static struct usb_endpoint_descriptor hs_int_desc = {
473 .bLength = USB_DT_ENDPOINT_SIZE,
474 .bDescriptorType = USB_DT_ENDPOINT,
476 .bmAttributes = USB_ENDPOINT_XFER_INT,
477 .wMaxPacketSize = __constant_cpu_to_le16(16),
483 * This attribute vendor descriptor is necessary for correct operation with
484 * Windows version of THOR download program
486 * It prevents windows driver from sending zero lenght packet (ZLP) after
487 * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
489 static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = {
490 .bLength = sizeof(thor_downloader_cdc_av),
491 .bDescriptorType = 0x24,
492 .bDescriptorSubType = 0x80,
498 static const struct usb_descriptor_header *hs_thor_downloader_function[] = {
499 (struct usb_descriptor_header *)&thor_iad_descriptor,
501 (struct usb_descriptor_header *)&thor_downloader_intf_int,
502 (struct usb_descriptor_header *)&thor_downloader_cdc_header,
503 (struct usb_descriptor_header *)&thor_downloader_cdc_call,
504 (struct usb_descriptor_header *)&thor_downloader_cdc_abstract,
505 (struct usb_descriptor_header *)&thor_downloader_cdc_union,
506 (struct usb_descriptor_header *)&hs_int_desc,
508 (struct usb_descriptor_header *)&thor_downloader_intf_data,
509 (struct usb_descriptor_header *)&thor_downloader_cdc_av,
510 (struct usb_descriptor_header *)&hs_in_desc,
511 (struct usb_descriptor_header *)&hs_out_desc,
515 /*-------------------------------------------------------------------------*/
516 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
518 struct usb_request *req;
520 req = usb_ep_alloc_request(ep, 0);
524 req->length = length;
525 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
527 usb_ep_free_request(ep, req);
534 static int thor_rx_data(void)
536 struct thor_dev *dev = thor_func->dev;
537 int data_to_rx, tmp, status;
539 data_to_rx = dev->out_req->length;
542 dev->out_req->length = data_to_rx;
543 debug("dev->out_req->length:%d dev->rxdata:%d\n",
544 dev->out_req->length, dev->rxdata);
546 status = usb_ep_queue(dev->out_ep, dev->out_req, 0);
548 pr_err("kill %s: resubmit %d bytes --> %d",
549 dev->out_ep->name, dev->out_req->length, status);
550 usb_ep_set_halt(dev->out_ep);
554 while (!dev->rxdata) {
555 usb_gadget_handle_interrupts(0);
560 data_to_rx -= dev->out_req->actual;
561 } while (data_to_rx);
566 static void thor_tx_data(unsigned char *data, int len)
568 struct thor_dev *dev = thor_func->dev;
569 unsigned char *ptr = dev->in_req->buf;
573 memcpy(ptr, data, len);
575 dev->in_req->length = len;
577 debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
578 dev->in_req->length, sizeof(data));
580 status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
582 pr_err("kill %s: resubmit %d bytes --> %d",
583 dev->in_ep->name, dev->in_req->length, status);
584 usb_ep_set_halt(dev->in_ep);
587 /* Wait until tx interrupt received */
589 usb_gadget_handle_interrupts(0);
594 static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req)
596 struct thor_dev *dev = thor_func->dev;
597 int status = req->status;
599 debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req);
602 if (ep == dev->out_ep)
609 /* this endpoint is normally active while we're configured */
610 case -ECONNABORTED: /* hardware forced ep reset */
611 case -ECONNRESET: /* request dequeued */
612 case -ESHUTDOWN: /* disconnect from host */
613 case -EREMOTEIO: /* short read */
615 pr_err("ERROR:%d", status);
619 debug("%s complete --> %d, %d/%d\n", ep->name,
620 status, req->actual, req->length);
623 static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
625 if (req->status || req->actual != req->length)
626 debug("setup complete --> %d, %d/%d\n",
627 req->status, req->actual, req->length);
631 thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
633 struct thor_dev *dev = thor_func->dev;
634 struct usb_request *req = dev->req;
635 struct usb_gadget *gadget = dev->gadget;
638 u16 len = le16_to_cpu(ctrl->wLength);
640 debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
641 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
644 switch (ctrl->bRequest) {
645 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
648 case USB_CDC_REQ_SET_LINE_CODING:
650 /* Line Coding set done = configuration done */
651 thor_func->dev->configuration_done = 1;
655 pr_err("thor_setup: unknown request: %d", ctrl->bRequest);
660 req->zero = value < len;
661 value = usb_ep_queue(gadget->ep0, req, 0);
663 debug("%s: ep_queue: %d\n", __func__, value);
671 /* Specific to the THOR protocol */
672 static void thor_set_dma(void *addr, int len)
674 struct thor_dev *dev = thor_func->dev;
676 debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
677 debug("addr:%p, len:%d\n", addr, len);
679 dev->out_req->buf = addr;
680 dev->out_req->length = len;
685 struct thor_dev *dev = thor_func->dev;
687 /* Wait for a device enumeration and configuration settings */
688 debug("THOR enumeration/configuration setting....\n");
689 while (!dev->configuration_done)
690 usb_gadget_handle_interrupts(0);
692 thor_set_dma(thor_rx_data_buf, strlen("THOR"));
693 /* detect the download request from Host PC */
694 if (thor_rx_data() < 0) {
695 printf("%s: Data not received!\n", __func__);
699 if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
700 puts("Download request from the Host PC\n");
701 udelay(30 * 1000); /* 30 ms */
703 strcpy((char *)thor_tx_data_buf, "ROHT");
704 thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
706 puts("Wrong reply information\n");
713 int thor_handle(void)
717 /* receive the data from Host PC */
719 thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
720 ret = thor_rx_data();
723 ret = process_data();
724 #ifdef CONFIG_THOR_RESET_OFF
725 if (ret == RESET_DONE)
731 printf("%s: No data received!\n", __func__);
739 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
743 usb_ep_free_request(ep, req);
746 static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
748 struct usb_gadget *gadget = c->cdev->gadget;
749 struct f_thor *f_thor = func_to_thor(f);
750 struct thor_dev *dev;
755 dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev));
759 memset(dev, 0, sizeof(*dev));
760 dev->gadget = gadget;
763 debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
765 debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev);
768 /* preallocate control response and buffer */
769 dev->req = usb_ep_alloc_request(gadget->ep0, 0);
774 dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
776 if (!dev->req->buf) {
781 dev->req->complete = thor_setup_complete;
783 /* DYNAMIC interface numbers assignments */
784 status = usb_interface_id(c, f);
789 thor_downloader_intf_int.bInterfaceNumber = status;
790 thor_downloader_cdc_union.bMasterInterface0 = status;
792 status = usb_interface_id(c, f);
797 thor_downloader_intf_data.bInterfaceNumber = status;
798 thor_downloader_cdc_union.bSlaveInterface0 = status;
800 /* allocate instance-specific endpoints */
801 ep = usb_ep_autoconfig(gadget, &fs_in_desc);
807 if (gadget_is_dualspeed(gadget)) {
808 hs_in_desc.bEndpointAddress =
809 fs_in_desc.bEndpointAddress;
812 dev->in_ep = ep; /* Store IN EP for enabling @ setup */
813 ep->driver_data = dev;
815 ep = usb_ep_autoconfig(gadget, &fs_out_desc);
821 if (gadget_is_dualspeed(gadget))
822 hs_out_desc.bEndpointAddress =
823 fs_out_desc.bEndpointAddress;
825 dev->out_ep = ep; /* Store OUT EP for enabling @ setup */
826 ep->driver_data = dev;
828 ep = usb_ep_autoconfig(gadget, &fs_int_desc);
835 ep->driver_data = dev;
837 if (gadget_is_dualspeed(gadget)) {
838 hs_int_desc.bEndpointAddress =
839 fs_int_desc.bEndpointAddress;
841 f->hs_descriptors = (struct usb_descriptor_header **)
842 &hs_thor_downloader_function;
844 if (!f->hs_descriptors)
848 debug("%s: out_ep:%p out_req:%p\n", __func__,
849 dev->out_ep, dev->out_req);
855 free_ep_req(gadget->ep0, dev->req);
860 static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
862 struct f_thor *f_thor = func_to_thor(f);
863 struct thor_dev *dev = f_thor->dev;
865 free_ep_req(dev->gadget->ep0, dev->req);
867 memset(thor_func, 0, sizeof(*thor_func));
871 static void thor_func_disable(struct usb_function *f)
873 struct f_thor *f_thor = func_to_thor(f);
874 struct thor_dev *dev = f_thor->dev;
876 debug("%s:\n", __func__);
878 /* Avoid freeing memory when ep is still claimed */
879 if (dev->in_ep->driver_data) {
880 usb_ep_disable(dev->in_ep);
881 free_ep_req(dev->in_ep, dev->in_req);
882 dev->in_ep->driver_data = NULL;
885 if (dev->out_ep->driver_data) {
886 usb_ep_disable(dev->out_ep);
887 usb_ep_free_request(dev->out_ep, dev->out_req);
888 dev->out_ep->driver_data = NULL;
891 if (dev->int_ep->driver_data) {
892 usb_ep_disable(dev->int_ep);
893 dev->int_ep->driver_data = NULL;
897 static int thor_eps_setup(struct usb_function *f)
899 struct usb_composite_dev *cdev = f->config->cdev;
900 struct usb_gadget *gadget = cdev->gadget;
901 struct thor_dev *dev = thor_func->dev;
902 struct usb_endpoint_descriptor *d;
903 struct usb_request *req;
908 d = ep_desc(gadget, &hs_in_desc, &fs_in_desc);
909 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
911 result = usb_ep_enable(ep, d);
915 ep->driver_data = cdev; /* claim */
916 req = alloc_ep_req(ep, THOR_PACKET_SIZE);
919 goto err_disable_in_ep;
922 memset(req->buf, 0, req->length);
923 req->complete = thor_rx_tx_complete;
926 d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
927 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
929 result = usb_ep_enable(ep, d);
931 goto err_free_in_req;
933 ep->driver_data = cdev; /* claim */
934 req = usb_ep_alloc_request(ep, 0);
937 goto err_disable_out_ep;
940 req->complete = thor_rx_tx_complete;
944 ep->driver_data = cdev; /* claim */
949 usb_ep_disable(dev->out_ep);
952 free_ep_req(dev->in_ep, dev->in_req);
956 usb_ep_disable(dev->in_ep);
962 static int thor_func_set_alt(struct usb_function *f,
963 unsigned intf, unsigned alt)
965 struct thor_dev *dev = thor_func->dev;
968 debug("%s: func: %s intf: %d alt: %d\n",
969 __func__, f->name, intf, alt);
973 debug("ACM INTR interface\n");
976 debug("Communication Data interface\n");
977 result = thor_eps_setup(f);
979 pr_err("%s: EPs setup failed!", __func__);
980 dev->configuration_done = 1;
987 static int thor_func_init(struct usb_configuration *c)
989 struct f_thor *f_thor;
992 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
994 f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
998 memset(f_thor, 0, sizeof(*f_thor));
1000 f_thor->usb_function.name = "f_thor";
1001 f_thor->usb_function.bind = thor_func_bind;
1002 f_thor->usb_function.unbind = thor_unbind;
1003 f_thor->usb_function.setup = thor_func_setup;
1004 f_thor->usb_function.set_alt = thor_func_set_alt;
1005 f_thor->usb_function.disable = thor_func_disable;
1007 status = usb_add_function(c, &f_thor->usb_function);
1014 int thor_add(struct usb_configuration *c)
1016 debug("%s:\n", __func__);
1017 return thor_func_init(c);
1020 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);