1 // SPDX-License-Identifier: GPL-2.0+
3 * f_thor.c -- USB TIZEN THOR Downloader gadget function
5 * Copyright (C) 2013 Samsung Electronics
6 * Lukasz Majewski <l.majewski@samsung.com>
9 * git://review.tizen.org/kernel/u-boot
12 * Copyright (C) 2009 Samsung Electronics
13 * Minkyu Kang <mk7.kang@samsung.com>
14 * Sanghee Kim <sh0130.kim@samsung.com>
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 struct usb_request *thor_start_ep(struct usb_ep *ep)
625 struct usb_request *req;
627 req = alloc_ep_req(ep, THOR_PACKET_SIZE);
628 debug("%s: ep:%p req:%p\n", __func__, ep, req);
633 memset(req->buf, 0, req->length);
634 req->complete = thor_rx_tx_complete;
639 static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
641 if (req->status || req->actual != req->length)
642 debug("setup complete --> %d, %d/%d\n",
643 req->status, req->actual, req->length);
647 thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
649 struct thor_dev *dev = thor_func->dev;
650 struct usb_request *req = dev->req;
651 struct usb_gadget *gadget = dev->gadget;
654 u16 len = le16_to_cpu(ctrl->wLength);
656 debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
657 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
660 switch (ctrl->bRequest) {
661 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
664 case USB_CDC_REQ_SET_LINE_CODING:
666 /* Line Coding set done = configuration done */
667 thor_func->dev->configuration_done = 1;
671 pr_err("thor_setup: unknown request: %d", ctrl->bRequest);
676 req->zero = value < len;
677 value = usb_ep_queue(gadget->ep0, req, 0);
679 debug("%s: ep_queue: %d\n", __func__, value);
687 /* Specific to the THOR protocol */
688 static void thor_set_dma(void *addr, int len)
690 struct thor_dev *dev = thor_func->dev;
692 debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
693 debug("addr:%p, len:%d\n", addr, len);
695 dev->out_req->buf = addr;
696 dev->out_req->length = len;
701 struct thor_dev *dev = thor_func->dev;
703 /* Wait for a device enumeration and configuration settings */
704 debug("THOR enumeration/configuration setting....\n");
705 while (!dev->configuration_done)
706 usb_gadget_handle_interrupts(0);
708 thor_set_dma(thor_rx_data_buf, strlen("THOR"));
709 /* detect the download request from Host PC */
710 if (thor_rx_data() < 0) {
711 printf("%s: Data not received!\n", __func__);
715 if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
716 puts("Download request from the Host PC\n");
717 udelay(30 * 1000); /* 30 ms */
719 strcpy((char *)thor_tx_data_buf, "ROHT");
720 thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
722 puts("Wrong reply information\n");
729 int thor_handle(void)
733 /* receive the data from Host PC */
735 thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
736 ret = thor_rx_data();
739 ret = process_data();
740 #ifdef CONFIG_THOR_RESET_OFF
741 if (ret == RESET_DONE)
747 printf("%s: No data received!\n", __func__);
755 static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
757 struct usb_gadget *gadget = c->cdev->gadget;
758 struct f_thor *f_thor = func_to_thor(f);
759 struct thor_dev *dev;
764 dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev));
768 memset(dev, 0, sizeof(*dev));
769 dev->gadget = gadget;
772 debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
774 debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev);
777 /* preallocate control response and buffer */
778 dev->req = usb_ep_alloc_request(gadget->ep0, 0);
783 dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
785 if (!dev->req->buf) {
790 dev->req->complete = thor_setup_complete;
792 /* DYNAMIC interface numbers assignments */
793 status = usb_interface_id(c, f);
798 thor_downloader_intf_int.bInterfaceNumber = status;
799 thor_downloader_cdc_union.bMasterInterface0 = status;
801 status = usb_interface_id(c, f);
806 thor_downloader_intf_data.bInterfaceNumber = status;
807 thor_downloader_cdc_union.bSlaveInterface0 = status;
809 /* allocate instance-specific endpoints */
810 ep = usb_ep_autoconfig(gadget, &fs_in_desc);
816 if (gadget_is_dualspeed(gadget)) {
817 hs_in_desc.bEndpointAddress =
818 fs_in_desc.bEndpointAddress;
821 dev->in_ep = ep; /* Store IN EP for enabling @ setup */
822 ep->driver_data = dev;
824 ep = usb_ep_autoconfig(gadget, &fs_out_desc);
830 if (gadget_is_dualspeed(gadget))
831 hs_out_desc.bEndpointAddress =
832 fs_out_desc.bEndpointAddress;
834 dev->out_ep = ep; /* Store OUT EP for enabling @ setup */
835 ep->driver_data = dev;
837 ep = usb_ep_autoconfig(gadget, &fs_int_desc);
844 ep->driver_data = dev;
846 if (gadget_is_dualspeed(gadget)) {
847 hs_int_desc.bEndpointAddress =
848 fs_int_desc.bEndpointAddress;
850 f->hs_descriptors = (struct usb_descriptor_header **)
851 &hs_thor_downloader_function;
853 if (!f->hs_descriptors)
857 debug("%s: out_ep:%p out_req:%p\n", __func__,
858 dev->out_ep, dev->out_req);
867 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
870 usb_ep_free_request(ep, req);
873 static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
875 struct f_thor *f_thor = func_to_thor(f);
876 struct thor_dev *dev = f_thor->dev;
879 memset(thor_func, 0, sizeof(*thor_func));
883 static void thor_func_disable(struct usb_function *f)
885 struct f_thor *f_thor = func_to_thor(f);
886 struct thor_dev *dev = f_thor->dev;
888 debug("%s:\n", __func__);
890 /* Avoid freeing memory when ep is still claimed */
891 if (dev->in_ep->driver_data) {
892 free_ep_req(dev->in_ep, dev->in_req);
893 usb_ep_disable(dev->in_ep);
894 dev->in_ep->driver_data = NULL;
897 if (dev->out_ep->driver_data) {
898 free(dev->out_req->buf);
899 dev->out_req->buf = NULL;
900 usb_ep_free_request(dev->out_ep, dev->out_req);
901 usb_ep_disable(dev->out_ep);
902 dev->out_ep->driver_data = NULL;
905 if (dev->int_ep->driver_data) {
906 usb_ep_disable(dev->int_ep);
907 dev->int_ep->driver_data = NULL;
911 static int thor_eps_setup(struct usb_function *f)
913 struct usb_composite_dev *cdev = f->config->cdev;
914 struct usb_gadget *gadget = cdev->gadget;
915 struct thor_dev *dev = thor_func->dev;
916 struct usb_endpoint_descriptor *d;
917 struct usb_request *req;
922 d = ep_desc(gadget, &hs_in_desc, &fs_in_desc);
923 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
925 result = usb_ep_enable(ep, d);
929 ep->driver_data = cdev; /* claim */
930 req = thor_start_ep(ep);
939 d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
940 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
942 result = usb_ep_enable(ep, d);
946 ep->driver_data = cdev; /* claim */
947 req = thor_start_ep(ep);
957 ep->driver_data = cdev; /* claim */
963 static int thor_func_set_alt(struct usb_function *f,
964 unsigned intf, unsigned alt)
966 struct thor_dev *dev = thor_func->dev;
969 debug("%s: func: %s intf: %d alt: %d\n",
970 __func__, f->name, intf, alt);
974 debug("ACM INTR interface\n");
977 debug("Communication Data interface\n");
978 result = thor_eps_setup(f);
980 pr_err("%s: EPs setup failed!", __func__);
981 dev->configuration_done = 1;
988 static int thor_func_init(struct usb_configuration *c)
990 struct f_thor *f_thor;
993 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
995 f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
999 memset(f_thor, 0, sizeof(*f_thor));
1001 f_thor->usb_function.name = "f_thor";
1002 f_thor->usb_function.bind = thor_func_bind;
1003 f_thor->usb_function.unbind = thor_unbind;
1004 f_thor->usb_function.setup = thor_func_setup;
1005 f_thor->usb_function.set_alt = thor_func_set_alt;
1006 f_thor->usb_function.disable = thor_func_disable;
1008 status = usb_add_function(c, &f_thor->usb_function);
1015 int thor_add(struct usb_configuration *c)
1017 debug("%s:\n", __func__);
1018 return thor_func_init(c);
1021 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);