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>
26 #include <linux/delay.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
29 #include <linux/usb/composite.h>
30 #include <linux/usb/cdc.h>
36 static void thor_tx_data(unsigned char *data, int len);
37 static void thor_set_dma(void *addr, int len);
38 static int thor_rx_data(void);
40 static struct f_thor *thor_func;
41 static inline struct f_thor *func_to_thor(struct usb_function *f)
43 return container_of(f, struct f_thor, usb_function);
46 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf,
47 sizeof(struct rsp_box));
48 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf,
49 sizeof(struct rqt_box));
51 /* ********************************************************** */
52 /* THOR protocol - transmission handling */
53 /* ********************************************************** */
54 DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1);
55 static unsigned long long int thor_file_size;
56 static int alt_setting_num;
58 static void send_rsp(const struct rsp_box *rsp)
60 memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
61 thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box));
63 debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
66 static void send_data_rsp(s32 ack, s32 count)
68 ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp,
69 sizeof(struct data_rsp_box));
74 memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
75 thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box));
77 debug("-DATA RSP: %d, %d\n", ack, count);
80 static int process_rqt_info(const struct rqt_box *rqt)
82 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
83 memset(rsp, 0, sizeof(struct rsp_box));
86 rsp->rsp_data = rqt->rqt_data;
88 switch (rqt->rqt_data) {
89 case RQT_INFO_VER_PROTOCOL:
90 rsp->int_data[0] = VER_PROTOCOL_MAJOR;
91 rsp->int_data[1] = VER_PROTOCOL_MINOR;
94 snprintf(rsp->str_data[0], sizeof(rsp->str_data[0]),
97 case RQT_INIT_VER_BOOT:
98 sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION);
100 case RQT_INIT_VER_KERNEL:
101 sprintf(rsp->str_data[0], "%s", "k unknown");
103 case RQT_INIT_VER_PLATFORM:
104 sprintf(rsp->str_data[0], "%s", "p unknown");
106 case RQT_INIT_VER_CSC:
107 sprintf(rsp->str_data[0], "%s", "c unknown");
117 static int process_rqt_cmd(const struct rqt_box *rqt)
119 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
120 memset(rsp, 0, sizeof(struct rsp_box));
123 rsp->rsp_data = rqt->rqt_data;
125 switch (rqt->rqt_data) {
127 debug("TARGET RESET\n");
131 #ifdef CONFIG_THOR_RESET_OFF
134 run_command("reset", 0);
136 case RQT_CMD_POWEROFF:
137 case RQT_CMD_EFSCLEAR:
140 printf("Command not supported -> cmd: %d\n", rqt->rqt_data);
147 static long long int download_head(unsigned long long total,
148 unsigned int packet_size,
152 long long int rcv_cnt = 0, left_to_rcv, ret_rcv;
153 struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num);
154 void *transfer_buffer = dfu_get_buf(dfu_entity);
155 void *buf = transfer_buffer;
156 int usb_pkt_cnt = 0, ret;
159 * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on
161 * The packet response is sent on the purpose after successful data
162 * chunk write. There is a room for improvement when asynchronous write
165 while (total - rcv_cnt >= packet_size) {
166 thor_set_dma(buf, packet_size);
168 ret_rcv = thor_rx_data();
172 debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt,
175 if ((rcv_cnt % THOR_STORE_UNIT_SIZE) == 0) {
176 ret = dfu_write(dfu_get_entity(alt_setting_num),
177 transfer_buffer, THOR_STORE_UNIT_SIZE,
180 pr_err("DFU write failed [%d] cnt: %d\n",
184 buf = transfer_buffer;
186 send_data_rsp(0, ++usb_pkt_cnt);
189 /* Calculate the amount of data to arrive from PC (in bytes) */
190 left_to_rcv = total - rcv_cnt;
193 * Calculate number of data already received. but not yet stored
194 * on the medium (they are smaller than THOR_STORE_UNIT_SIZE)
196 *left = left_to_rcv + buf - transfer_buffer;
197 debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__,
198 *left, left_to_rcv, buf);
201 thor_set_dma(buf, packet_size);
202 ret_rcv = thor_rx_data();
206 send_data_rsp(0, ++usb_pkt_cnt);
209 debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
214 static int download_tail(long long int left, int cnt)
216 struct dfu_entity *dfu_entity;
217 void *transfer_buffer;
220 debug("%s: left: %llu cnt: %d\n", __func__, left, cnt);
222 dfu_entity = dfu_get_entity(alt_setting_num);
224 pr_err("Alt setting: %d entity not found!\n", alt_setting_num);
228 transfer_buffer = dfu_get_buf(dfu_entity);
229 if (!transfer_buffer) {
230 pr_err("Transfer buffer not allocated!\n");
235 ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++);
237 pr_err("DFU write failed[%d]: left: %llu\n", ret, left);
243 * To store last "packet" or write file from buffer to filesystem
244 * DFU storage backend requires dfu_flush
246 * This also frees memory malloc'ed by dfu_get_buf(), so no explicit
247 * need fo call dfu_free_buf() is needed.
249 ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt);
251 pr_err("DFU flush failed!\n");
256 static long long int process_rqt_download(const struct rqt_box *rqt)
258 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
259 static long long int left, ret_head;
260 int file_type, ret = 0;
263 memset(rsp, 0, sizeof(struct rsp_box));
265 rsp->rsp_data = rqt->rqt_data;
267 switch (rqt->rqt_data) {
269 thor_file_size = (unsigned long long int)rqt->int_data[0] +
270 (((unsigned long long int)rqt->int_data[1])
272 debug("INIT: total %llu bytes\n", thor_file_size);
274 case RQT_DL_FILE_INFO:
275 file_type = rqt->int_data[0];
276 if (file_type == FILE_TYPE_PIT) {
277 puts("PIT table file - not supported\n");
278 rsp->ack = -ENOTSUPP;
283 thor_file_size = (unsigned long long int)rqt->int_data[1] +
284 (((unsigned long long int)rqt->int_data[2])
286 memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
287 f_name[F_NAME_BUF_SIZE] = '\0';
289 debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
290 f_name, 0, thor_file_size, file_type);
292 rsp->int_data[0] = THOR_PACKET_SIZE;
294 alt_setting_num = dfu_get_alt(f_name);
295 if (alt_setting_num < 0) {
296 pr_err("Alt setting [%d] to write not found!\n",
302 case RQT_DL_FILE_START:
304 ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
311 case RQT_DL_FILE_END:
312 debug("DL FILE_END\n");
313 rsp->ack = download_tail(left, cnt);
322 pr_err("Operation not supported: %d\n", rqt->rqt_data);
330 static int process_data(void)
332 ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
335 memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
337 debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
341 ret = process_rqt_info(rqt);
344 ret = process_rqt_cmd(rqt);
347 ret = (int) process_rqt_download(rqt);
350 puts("RQT: UPLOAD not supported!\n");
353 pr_err("unknown request (%d)\n", rqt->rqt);
359 /* ********************************************************** */
360 /* THOR USB Function */
361 /* ********************************************************** */
363 static inline struct usb_endpoint_descriptor *
364 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
365 struct usb_endpoint_descriptor *fs)
367 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
372 static struct usb_interface_descriptor thor_downloader_intf_data = {
373 .bLength = sizeof(thor_downloader_intf_data),
374 .bDescriptorType = USB_DT_INTERFACE,
377 .bInterfaceClass = USB_CLASS_CDC_DATA,
380 static struct usb_endpoint_descriptor fs_in_desc = {
381 .bLength = USB_DT_ENDPOINT_SIZE,
382 .bDescriptorType = USB_DT_ENDPOINT,
384 .bEndpointAddress = USB_DIR_IN,
385 .bmAttributes = USB_ENDPOINT_XFER_BULK,
388 static struct usb_endpoint_descriptor fs_out_desc = {
389 .bLength = USB_DT_ENDPOINT_SIZE,
390 .bDescriptorType = USB_DT_ENDPOINT,
392 .bEndpointAddress = USB_DIR_OUT,
393 .bmAttributes = USB_ENDPOINT_XFER_BULK,
396 /* CDC configuration */
397 static struct usb_interface_descriptor thor_downloader_intf_int = {
398 .bLength = sizeof(thor_downloader_intf_int),
399 .bDescriptorType = USB_DT_INTERFACE,
402 .bInterfaceClass = USB_CLASS_COMM,
403 /* 0x02 Abstract Line Control Model */
404 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
405 /* 0x01 Common AT commands */
406 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
409 static struct usb_cdc_header_desc thor_downloader_cdc_header = {
410 .bLength = sizeof(thor_downloader_cdc_header),
411 .bDescriptorType = 0x24, /* CS_INTERFACE */
412 .bDescriptorSubType = 0x00,
416 static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = {
417 .bLength = sizeof(thor_downloader_cdc_call),
418 .bDescriptorType = 0x24, /* CS_INTERFACE */
419 .bDescriptorSubType = 0x01,
420 .bmCapabilities = 0x00,
421 .bDataInterface = 0x01,
424 static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = {
425 .bLength = sizeof(thor_downloader_cdc_abstract),
426 .bDescriptorType = 0x24, /* CS_INTERFACE */
427 .bDescriptorSubType = 0x02,
428 .bmCapabilities = 0x00,
431 static struct usb_cdc_union_desc thor_downloader_cdc_union = {
432 .bLength = sizeof(thor_downloader_cdc_union),
433 .bDescriptorType = 0x24, /* CS_INTERFACE */
434 .bDescriptorSubType = USB_CDC_UNION_TYPE,
437 static struct usb_endpoint_descriptor fs_int_desc = {
438 .bLength = USB_DT_ENDPOINT_SIZE,
439 .bDescriptorType = USB_DT_ENDPOINT,
441 .bEndpointAddress = 3 | USB_DIR_IN,
442 .bmAttributes = USB_ENDPOINT_XFER_INT,
443 .wMaxPacketSize = __constant_cpu_to_le16(16),
448 static struct usb_interface_assoc_descriptor
449 thor_iad_descriptor = {
450 .bLength = sizeof(thor_iad_descriptor),
451 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
453 .bFirstInterface = 0,
454 .bInterfaceCount = 2, /* control + data */
455 .bFunctionClass = USB_CLASS_COMM,
456 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
457 .bFunctionProtocol = USB_CDC_PROTO_NONE,
460 static struct usb_endpoint_descriptor hs_in_desc = {
461 .bLength = USB_DT_ENDPOINT_SIZE,
462 .bDescriptorType = USB_DT_ENDPOINT,
464 .bmAttributes = USB_ENDPOINT_XFER_BULK,
465 .wMaxPacketSize = __constant_cpu_to_le16(512),
468 static struct usb_endpoint_descriptor hs_out_desc = {
469 .bLength = USB_DT_ENDPOINT_SIZE,
470 .bDescriptorType = USB_DT_ENDPOINT,
472 .bmAttributes = USB_ENDPOINT_XFER_BULK,
473 .wMaxPacketSize = __constant_cpu_to_le16(512),
476 static struct usb_endpoint_descriptor hs_int_desc = {
477 .bLength = USB_DT_ENDPOINT_SIZE,
478 .bDescriptorType = USB_DT_ENDPOINT,
480 .bmAttributes = USB_ENDPOINT_XFER_INT,
481 .wMaxPacketSize = __constant_cpu_to_le16(16),
487 * This attribute vendor descriptor is necessary for correct operation with
488 * Windows version of THOR download program
490 * It prevents windows driver from sending zero lenght packet (ZLP) after
491 * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
493 static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = {
494 .bLength = sizeof(thor_downloader_cdc_av),
495 .bDescriptorType = 0x24,
496 .bDescriptorSubType = 0x80,
502 static const struct usb_descriptor_header *hs_thor_downloader_function[] = {
503 (struct usb_descriptor_header *)&thor_iad_descriptor,
505 (struct usb_descriptor_header *)&thor_downloader_intf_int,
506 (struct usb_descriptor_header *)&thor_downloader_cdc_header,
507 (struct usb_descriptor_header *)&thor_downloader_cdc_call,
508 (struct usb_descriptor_header *)&thor_downloader_cdc_abstract,
509 (struct usb_descriptor_header *)&thor_downloader_cdc_union,
510 (struct usb_descriptor_header *)&hs_int_desc,
512 (struct usb_descriptor_header *)&thor_downloader_intf_data,
513 (struct usb_descriptor_header *)&thor_downloader_cdc_av,
514 (struct usb_descriptor_header *)&hs_in_desc,
515 (struct usb_descriptor_header *)&hs_out_desc,
519 /*-------------------------------------------------------------------------*/
520 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
522 struct usb_request *req;
524 req = usb_ep_alloc_request(ep, 0);
528 req->length = length;
529 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
531 usb_ep_free_request(ep, req);
538 static int thor_rx_data(void)
540 struct thor_dev *dev = thor_func->dev;
541 int data_to_rx, tmp, status;
543 data_to_rx = dev->out_req->length;
546 dev->out_req->length = data_to_rx;
547 debug("dev->out_req->length:%d dev->rxdata:%d\n",
548 dev->out_req->length, dev->rxdata);
550 status = usb_ep_queue(dev->out_ep, dev->out_req, 0);
552 pr_err("kill %s: resubmit %d bytes --> %d\n",
553 dev->out_ep->name, dev->out_req->length, status);
554 usb_ep_set_halt(dev->out_ep);
558 while (!dev->rxdata) {
559 usb_gadget_handle_interrupts(0);
564 data_to_rx -= dev->out_req->actual;
565 } while (data_to_rx);
570 static void thor_tx_data(unsigned char *data, int len)
572 struct thor_dev *dev = thor_func->dev;
573 unsigned char *ptr = dev->in_req->buf;
577 memcpy(ptr, data, len);
579 dev->in_req->length = len;
581 debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
582 dev->in_req->length, sizeof(data));
584 status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
586 pr_err("kill %s: resubmit %d bytes --> %d\n",
587 dev->in_ep->name, dev->in_req->length, status);
588 usb_ep_set_halt(dev->in_ep);
591 /* Wait until tx interrupt received */
593 usb_gadget_handle_interrupts(0);
598 static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req)
600 struct thor_dev *dev = thor_func->dev;
601 int status = req->status;
603 debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req);
606 if (ep == dev->out_ep)
613 /* this endpoint is normally active while we're configured */
614 case -ECONNABORTED: /* hardware forced ep reset */
615 case -ECONNRESET: /* request dequeued */
616 case -ESHUTDOWN: /* disconnect from host */
617 case -EREMOTEIO: /* short read */
619 pr_err("ERROR:%d\n", status);
623 debug("%s complete --> %d, %d/%d\n", ep->name,
624 status, req->actual, req->length);
627 static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
629 if (req->status || req->actual != req->length)
630 debug("setup complete --> %d, %d/%d\n",
631 req->status, req->actual, req->length);
635 thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
637 struct thor_dev *dev = thor_func->dev;
638 struct usb_request *req = dev->req;
639 struct usb_gadget *gadget = dev->gadget;
642 u16 len = le16_to_cpu(ctrl->wLength);
644 debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
645 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
648 switch (ctrl->bRequest) {
649 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
652 case USB_CDC_REQ_SET_LINE_CODING:
654 /* Line Coding set done = configuration done */
655 thor_func->dev->configuration_done = 1;
659 pr_err("thor_setup: unknown request: %d\n", ctrl->bRequest);
664 req->zero = value < len;
665 value = usb_ep_queue(gadget->ep0, req, 0);
667 debug("%s: ep_queue: %d\n", __func__, value);
675 /* Specific to the THOR protocol */
676 static void thor_set_dma(void *addr, int len)
678 struct thor_dev *dev = thor_func->dev;
680 debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
681 debug("addr:%p, len:%d\n", addr, len);
683 dev->out_req->buf = addr;
684 dev->out_req->length = len;
689 struct thor_dev *dev = thor_func->dev;
691 /* Wait for a device enumeration and configuration settings */
692 debug("THOR enumeration/configuration setting....\n");
693 while (!dev->configuration_done)
694 usb_gadget_handle_interrupts(0);
696 thor_set_dma(thor_rx_data_buf, strlen("THOR"));
697 /* detect the download request from Host PC */
698 if (thor_rx_data() < 0) {
699 printf("%s: Data not received!\n", __func__);
703 if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
704 puts("Download request from the Host PC\n");
705 udelay(30 * 1000); /* 30 ms */
707 strcpy((char *)thor_tx_data_buf, "ROHT");
708 thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
710 puts("Wrong reply information\n");
717 int thor_handle(void)
721 /* receive the data from Host PC */
723 thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
724 ret = thor_rx_data();
727 ret = process_data();
728 #ifdef CONFIG_THOR_RESET_OFF
729 if (ret == RESET_DONE)
735 printf("%s: No data received!\n", __func__);
743 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
747 usb_ep_free_request(ep, req);
750 static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
752 struct usb_gadget *gadget = c->cdev->gadget;
753 struct f_thor *f_thor = func_to_thor(f);
754 struct thor_dev *dev;
759 dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev));
763 memset(dev, 0, sizeof(*dev));
764 dev->gadget = gadget;
767 debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
769 debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev);
772 /* preallocate control response and buffer */
773 dev->req = usb_ep_alloc_request(gadget->ep0, 0);
778 dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
780 if (!dev->req->buf) {
785 dev->req->complete = thor_setup_complete;
787 /* DYNAMIC interface numbers assignments */
788 status = usb_interface_id(c, f);
793 thor_downloader_intf_int.bInterfaceNumber = status;
794 thor_downloader_cdc_union.bMasterInterface0 = status;
796 status = usb_interface_id(c, f);
801 thor_downloader_intf_data.bInterfaceNumber = status;
802 thor_downloader_cdc_union.bSlaveInterface0 = status;
804 /* allocate instance-specific endpoints */
805 ep = usb_ep_autoconfig(gadget, &fs_in_desc);
811 if (gadget_is_dualspeed(gadget)) {
812 hs_in_desc.bEndpointAddress =
813 fs_in_desc.bEndpointAddress;
816 dev->in_ep = ep; /* Store IN EP for enabling @ setup */
817 ep->driver_data = dev;
819 ep = usb_ep_autoconfig(gadget, &fs_out_desc);
825 if (gadget_is_dualspeed(gadget))
826 hs_out_desc.bEndpointAddress =
827 fs_out_desc.bEndpointAddress;
829 dev->out_ep = ep; /* Store OUT EP for enabling @ setup */
830 ep->driver_data = dev;
832 ep = usb_ep_autoconfig(gadget, &fs_int_desc);
839 ep->driver_data = dev;
841 if (gadget_is_dualspeed(gadget)) {
842 hs_int_desc.bEndpointAddress =
843 fs_int_desc.bEndpointAddress;
845 f->hs_descriptors = (struct usb_descriptor_header **)
846 &hs_thor_downloader_function;
848 if (!f->hs_descriptors)
852 debug("%s: out_ep:%p out_req:%p\n", __func__,
853 dev->out_ep, dev->out_req);
859 free_ep_req(gadget->ep0, dev->req);
864 static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
866 struct f_thor *f_thor = func_to_thor(f);
867 struct thor_dev *dev = f_thor->dev;
869 free_ep_req(dev->gadget->ep0, dev->req);
871 memset(thor_func, 0, sizeof(*thor_func));
875 static void thor_func_disable(struct usb_function *f)
877 struct f_thor *f_thor = func_to_thor(f);
878 struct thor_dev *dev = f_thor->dev;
880 debug("%s:\n", __func__);
882 /* Avoid freeing memory when ep is still claimed */
883 if (dev->in_ep->driver_data) {
884 usb_ep_disable(dev->in_ep);
885 free_ep_req(dev->in_ep, dev->in_req);
886 dev->in_ep->driver_data = NULL;
889 if (dev->out_ep->driver_data) {
890 usb_ep_disable(dev->out_ep);
891 usb_ep_free_request(dev->out_ep, dev->out_req);
892 dev->out_ep->driver_data = NULL;
895 if (dev->int_ep->driver_data) {
896 usb_ep_disable(dev->int_ep);
897 dev->int_ep->driver_data = NULL;
901 static int thor_eps_setup(struct usb_function *f)
903 struct usb_composite_dev *cdev = f->config->cdev;
904 struct usb_gadget *gadget = cdev->gadget;
905 struct thor_dev *dev = thor_func->dev;
906 struct usb_endpoint_descriptor *d;
907 struct usb_request *req;
912 d = ep_desc(gadget, &hs_in_desc, &fs_in_desc);
913 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
915 result = usb_ep_enable(ep, d);
919 ep->driver_data = cdev; /* claim */
920 req = alloc_ep_req(ep, THOR_PACKET_SIZE);
923 goto err_disable_in_ep;
926 memset(req->buf, 0, req->length);
927 req->complete = thor_rx_tx_complete;
930 d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
931 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
933 result = usb_ep_enable(ep, d);
935 goto err_free_in_req;
937 ep->driver_data = cdev; /* claim */
938 req = usb_ep_alloc_request(ep, 0);
941 goto err_disable_out_ep;
944 req->complete = thor_rx_tx_complete;
948 d = ep_desc(gadget, &hs_int_desc, &fs_int_desc);
949 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
951 result = usb_ep_enable(ep, d);
955 ep->driver_data = cdev; /* claim */
960 usb_ep_disable(dev->out_ep);
963 free_ep_req(dev->in_ep, dev->in_req);
967 usb_ep_disable(dev->in_ep);
973 static int thor_func_set_alt(struct usb_function *f,
974 unsigned intf, unsigned alt)
976 struct thor_dev *dev = thor_func->dev;
979 debug("%s: func: %s intf: %d alt: %d\n",
980 __func__, f->name, intf, alt);
984 debug("ACM INTR interface\n");
987 debug("Communication Data interface\n");
988 result = thor_eps_setup(f);
990 pr_err("%s: EPs setup failed!\n", __func__);
991 dev->configuration_done = 1;
998 static int thor_func_init(struct usb_configuration *c)
1000 struct f_thor *f_thor;
1003 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
1005 f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
1009 memset(f_thor, 0, sizeof(*f_thor));
1011 f_thor->usb_function.name = "f_thor";
1012 f_thor->usb_function.bind = thor_func_bind;
1013 f_thor->usb_function.unbind = thor_unbind;
1014 f_thor->usb_function.setup = thor_func_setup;
1015 f_thor->usb_function.set_alt = thor_func_set_alt;
1016 f_thor->usb_function.disable = thor_func_disable;
1018 status = usb_add_function(c, &f_thor->usb_function);
1025 int thor_add(struct usb_configuration *c)
1027 debug("%s:\n", __func__);
1028 return thor_func_init(c);
1031 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);