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
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/gadget.h>
27 #include <linux/usb/composite.h>
28 #include <linux/usb/cdc.h>
34 static void thor_tx_data(unsigned char *data, int len);
35 static void thor_set_dma(void *addr, int len);
36 static int thor_rx_data(void);
38 static struct f_thor *thor_func;
39 static inline struct f_thor *func_to_thor(struct usb_function *f)
41 return container_of(f, struct f_thor, usb_function);
44 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_tx_data_buf,
45 sizeof(struct rsp_box));
46 DEFINE_CACHE_ALIGN_BUFFER(unsigned char, thor_rx_data_buf,
47 sizeof(struct rqt_box));
49 /* ********************************************************** */
50 /* THOR protocol - transmission handling */
51 /* ********************************************************** */
52 DEFINE_CACHE_ALIGN_BUFFER(char, f_name, F_NAME_BUF_SIZE + 1);
53 static unsigned long long int thor_file_size;
54 static int alt_setting_num;
56 static void send_rsp(const struct rsp_box *rsp)
58 memcpy(thor_tx_data_buf, rsp, sizeof(struct rsp_box));
59 thor_tx_data(thor_tx_data_buf, sizeof(struct rsp_box));
61 debug("-RSP: %d, %d\n", rsp->rsp, rsp->rsp_data);
64 static void send_data_rsp(s32 ack, s32 count)
66 ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box, rsp,
67 sizeof(struct data_rsp_box));
72 memcpy(thor_tx_data_buf, rsp, sizeof(struct data_rsp_box));
73 thor_tx_data(thor_tx_data_buf, sizeof(struct data_rsp_box));
75 debug("-DATA RSP: %d, %d\n", ack, count);
78 static int process_rqt_info(const struct rqt_box *rqt)
80 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
81 memset(rsp, 0, sizeof(struct rsp_box));
84 rsp->rsp_data = rqt->rqt_data;
86 switch (rqt->rqt_data) {
87 case RQT_INFO_VER_PROTOCOL:
88 rsp->int_data[0] = VER_PROTOCOL_MAJOR;
89 rsp->int_data[1] = VER_PROTOCOL_MINOR;
92 snprintf(rsp->str_data[0], sizeof(rsp->str_data[0]),
95 case RQT_INIT_VER_BOOT:
96 sprintf(rsp->str_data[0], "%s", U_BOOT_VERSION);
98 case RQT_INIT_VER_KERNEL:
99 sprintf(rsp->str_data[0], "%s", "k unknown");
101 case RQT_INIT_VER_PLATFORM:
102 sprintf(rsp->str_data[0], "%s", "p unknown");
104 case RQT_INIT_VER_CSC:
105 sprintf(rsp->str_data[0], "%s", "c unknown");
115 static int process_rqt_cmd(const struct rqt_box *rqt)
117 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
118 memset(rsp, 0, sizeof(struct rsp_box));
121 rsp->rsp_data = rqt->rqt_data;
123 switch (rqt->rqt_data) {
125 debug("TARGET RESET\n");
129 #ifdef CONFIG_THOR_RESET_OFF
132 run_command("reset", 0);
134 case RQT_CMD_POWEROFF:
135 case RQT_CMD_EFSCLEAR:
138 printf("Command not supported -> cmd: %d\n", rqt->rqt_data);
145 static long long int download_head(unsigned long long total,
146 unsigned int packet_size,
150 long long int rcv_cnt = 0, left_to_rcv, ret_rcv;
151 struct dfu_entity *dfu_entity = dfu_get_entity(alt_setting_num);
152 void *transfer_buffer = dfu_get_buf(dfu_entity);
153 void *buf = transfer_buffer;
154 int usb_pkt_cnt = 0, ret;
157 * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on
159 * The packet response is sent on the purpose after successful data
160 * chunk write. There is a room for improvement when asynchronous write
163 while (total - rcv_cnt >= packet_size) {
164 thor_set_dma(buf, packet_size);
166 ret_rcv = thor_rx_data();
170 debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt,
173 if ((rcv_cnt % THOR_STORE_UNIT_SIZE) == 0) {
174 ret = dfu_write(dfu_get_entity(alt_setting_num),
175 transfer_buffer, THOR_STORE_UNIT_SIZE,
178 pr_err("DFU write failed [%d] cnt: %d\n",
182 buf = transfer_buffer;
184 send_data_rsp(0, ++usb_pkt_cnt);
187 /* Calculate the amount of data to arrive from PC (in bytes) */
188 left_to_rcv = total - rcv_cnt;
191 * Calculate number of data already received. but not yet stored
192 * on the medium (they are smaller than THOR_STORE_UNIT_SIZE)
194 *left = left_to_rcv + buf - transfer_buffer;
195 debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__,
196 *left, left_to_rcv, buf);
199 thor_set_dma(buf, packet_size);
200 ret_rcv = thor_rx_data();
204 send_data_rsp(0, ++usb_pkt_cnt);
207 debug("%s: %llu total: %llu cnt: %d\n", __func__, rcv_cnt, total, *cnt);
212 static int download_tail(long long int left, int cnt)
214 struct dfu_entity *dfu_entity;
215 void *transfer_buffer;
218 debug("%s: left: %llu cnt: %d\n", __func__, left, cnt);
220 dfu_entity = dfu_get_entity(alt_setting_num);
222 pr_err("Alt setting: %d entity not found!\n", alt_setting_num);
226 transfer_buffer = dfu_get_buf(dfu_entity);
227 if (!transfer_buffer) {
228 pr_err("Transfer buffer not allocated!\n");
233 ret = dfu_write(dfu_entity, transfer_buffer, left, cnt++);
235 pr_err("DFU write failed[%d]: left: %llu\n", ret, left);
241 * To store last "packet" or write file from buffer to filesystem
242 * DFU storage backend requires dfu_flush
244 * This also frees memory malloc'ed by dfu_get_buf(), so no explicit
245 * need fo call dfu_free_buf() is needed.
247 ret = dfu_flush(dfu_entity, transfer_buffer, 0, cnt);
249 pr_err("DFU flush failed!\n");
254 static long long int process_rqt_download(const struct rqt_box *rqt)
256 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box, rsp, sizeof(struct rsp_box));
257 static long long int left, ret_head;
258 int file_type, ret = 0;
261 memset(rsp, 0, sizeof(struct rsp_box));
263 rsp->rsp_data = rqt->rqt_data;
265 switch (rqt->rqt_data) {
267 thor_file_size = (unsigned long long int)rqt->int_data[0] +
268 (((unsigned long long int)rqt->int_data[1])
270 debug("INIT: total %llu bytes\n", thor_file_size);
272 case RQT_DL_FILE_INFO:
273 file_type = rqt->int_data[0];
274 if (file_type == FILE_TYPE_PIT) {
275 puts("PIT table file - not supported\n");
276 rsp->ack = -ENOTSUPP;
281 thor_file_size = (unsigned long long int)rqt->int_data[1] +
282 (((unsigned long long int)rqt->int_data[2])
284 memcpy(f_name, rqt->str_data[0], F_NAME_BUF_SIZE);
285 f_name[F_NAME_BUF_SIZE] = '\0';
287 debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
288 f_name, 0, thor_file_size, file_type);
290 rsp->int_data[0] = THOR_PACKET_SIZE;
292 alt_setting_num = dfu_get_alt(f_name);
293 if (alt_setting_num < 0) {
294 pr_err("Alt setting [%d] to write not found!\n",
300 case RQT_DL_FILE_START:
302 ret_head = download_head(thor_file_size, THOR_PACKET_SIZE,
309 case RQT_DL_FILE_END:
310 debug("DL FILE_END\n");
311 rsp->ack = download_tail(left, cnt);
320 pr_err("Operation not supported: %d\n", rqt->rqt_data);
328 static int process_data(void)
330 ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box, rqt, sizeof(struct rqt_box));
333 memcpy(rqt, thor_rx_data_buf, sizeof(struct rqt_box));
335 debug("+RQT: %d, %d\n", rqt->rqt, rqt->rqt_data);
339 ret = process_rqt_info(rqt);
342 ret = process_rqt_cmd(rqt);
345 ret = (int) process_rqt_download(rqt);
348 puts("RQT: UPLOAD not supported!\n");
351 pr_err("unknown request (%d)\n", rqt->rqt);
357 /* ********************************************************** */
358 /* THOR USB Function */
359 /* ********************************************************** */
361 static inline struct usb_endpoint_descriptor *
362 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
363 struct usb_endpoint_descriptor *fs)
365 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
370 static struct usb_interface_descriptor thor_downloader_intf_data = {
371 .bLength = sizeof(thor_downloader_intf_data),
372 .bDescriptorType = USB_DT_INTERFACE,
375 .bInterfaceClass = USB_CLASS_CDC_DATA,
378 static struct usb_endpoint_descriptor fs_in_desc = {
379 .bLength = USB_DT_ENDPOINT_SIZE,
380 .bDescriptorType = USB_DT_ENDPOINT,
382 .bEndpointAddress = USB_DIR_IN,
383 .bmAttributes = USB_ENDPOINT_XFER_BULK,
386 static struct usb_endpoint_descriptor fs_out_desc = {
387 .bLength = USB_DT_ENDPOINT_SIZE,
388 .bDescriptorType = USB_DT_ENDPOINT,
390 .bEndpointAddress = USB_DIR_OUT,
391 .bmAttributes = USB_ENDPOINT_XFER_BULK,
394 /* CDC configuration */
395 static struct usb_interface_descriptor thor_downloader_intf_int = {
396 .bLength = sizeof(thor_downloader_intf_int),
397 .bDescriptorType = USB_DT_INTERFACE,
400 .bInterfaceClass = USB_CLASS_COMM,
401 /* 0x02 Abstract Line Control Model */
402 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
403 /* 0x01 Common AT commands */
404 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
407 static struct usb_cdc_header_desc thor_downloader_cdc_header = {
408 .bLength = sizeof(thor_downloader_cdc_header),
409 .bDescriptorType = 0x24, /* CS_INTERFACE */
410 .bDescriptorSubType = 0x00,
414 static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call = {
415 .bLength = sizeof(thor_downloader_cdc_call),
416 .bDescriptorType = 0x24, /* CS_INTERFACE */
417 .bDescriptorSubType = 0x01,
418 .bmCapabilities = 0x00,
419 .bDataInterface = 0x01,
422 static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract = {
423 .bLength = sizeof(thor_downloader_cdc_abstract),
424 .bDescriptorType = 0x24, /* CS_INTERFACE */
425 .bDescriptorSubType = 0x02,
426 .bmCapabilities = 0x00,
429 static struct usb_cdc_union_desc thor_downloader_cdc_union = {
430 .bLength = sizeof(thor_downloader_cdc_union),
431 .bDescriptorType = 0x24, /* CS_INTERFACE */
432 .bDescriptorSubType = USB_CDC_UNION_TYPE,
435 static struct usb_endpoint_descriptor fs_int_desc = {
436 .bLength = USB_DT_ENDPOINT_SIZE,
437 .bDescriptorType = USB_DT_ENDPOINT,
439 .bEndpointAddress = 3 | USB_DIR_IN,
440 .bmAttributes = USB_ENDPOINT_XFER_INT,
441 .wMaxPacketSize = __constant_cpu_to_le16(16),
446 static struct usb_interface_assoc_descriptor
447 thor_iad_descriptor = {
448 .bLength = sizeof(thor_iad_descriptor),
449 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
451 .bFirstInterface = 0,
452 .bInterfaceCount = 2, /* control + data */
453 .bFunctionClass = USB_CLASS_COMM,
454 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
455 .bFunctionProtocol = USB_CDC_PROTO_NONE,
458 static struct usb_endpoint_descriptor hs_in_desc = {
459 .bLength = USB_DT_ENDPOINT_SIZE,
460 .bDescriptorType = USB_DT_ENDPOINT,
462 .bmAttributes = USB_ENDPOINT_XFER_BULK,
463 .wMaxPacketSize = __constant_cpu_to_le16(512),
466 static struct usb_endpoint_descriptor hs_out_desc = {
467 .bLength = USB_DT_ENDPOINT_SIZE,
468 .bDescriptorType = USB_DT_ENDPOINT,
470 .bmAttributes = USB_ENDPOINT_XFER_BULK,
471 .wMaxPacketSize = __constant_cpu_to_le16(512),
474 static struct usb_endpoint_descriptor hs_int_desc = {
475 .bLength = USB_DT_ENDPOINT_SIZE,
476 .bDescriptorType = USB_DT_ENDPOINT,
478 .bmAttributes = USB_ENDPOINT_XFER_INT,
479 .wMaxPacketSize = __constant_cpu_to_le16(16),
485 * This attribute vendor descriptor is necessary for correct operation with
486 * Windows version of THOR download program
488 * It prevents windows driver from sending zero lenght packet (ZLP) after
489 * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
491 static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av = {
492 .bLength = sizeof(thor_downloader_cdc_av),
493 .bDescriptorType = 0x24,
494 .bDescriptorSubType = 0x80,
500 static const struct usb_descriptor_header *hs_thor_downloader_function[] = {
501 (struct usb_descriptor_header *)&thor_iad_descriptor,
503 (struct usb_descriptor_header *)&thor_downloader_intf_int,
504 (struct usb_descriptor_header *)&thor_downloader_cdc_header,
505 (struct usb_descriptor_header *)&thor_downloader_cdc_call,
506 (struct usb_descriptor_header *)&thor_downloader_cdc_abstract,
507 (struct usb_descriptor_header *)&thor_downloader_cdc_union,
508 (struct usb_descriptor_header *)&hs_int_desc,
510 (struct usb_descriptor_header *)&thor_downloader_intf_data,
511 (struct usb_descriptor_header *)&thor_downloader_cdc_av,
512 (struct usb_descriptor_header *)&hs_in_desc,
513 (struct usb_descriptor_header *)&hs_out_desc,
517 /*-------------------------------------------------------------------------*/
518 static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
520 struct usb_request *req;
522 req = usb_ep_alloc_request(ep, 0);
526 req->length = length;
527 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length);
529 usb_ep_free_request(ep, req);
536 static int thor_rx_data(void)
538 struct thor_dev *dev = thor_func->dev;
539 int data_to_rx, tmp, status;
541 data_to_rx = dev->out_req->length;
544 dev->out_req->length = data_to_rx;
545 debug("dev->out_req->length:%d dev->rxdata:%d\n",
546 dev->out_req->length, dev->rxdata);
548 status = usb_ep_queue(dev->out_ep, dev->out_req, 0);
550 pr_err("kill %s: resubmit %d bytes --> %d\n",
551 dev->out_ep->name, dev->out_req->length, status);
552 usb_ep_set_halt(dev->out_ep);
556 while (!dev->rxdata) {
557 usb_gadget_handle_interrupts(0);
562 data_to_rx -= dev->out_req->actual;
563 } while (data_to_rx);
568 static void thor_tx_data(unsigned char *data, int len)
570 struct thor_dev *dev = thor_func->dev;
571 unsigned char *ptr = dev->in_req->buf;
575 memcpy(ptr, data, len);
577 dev->in_req->length = len;
579 debug("%s: dev->in_req->length:%d to_cpy:%zd\n", __func__,
580 dev->in_req->length, sizeof(data));
582 status = usb_ep_queue(dev->in_ep, dev->in_req, 0);
584 pr_err("kill %s: resubmit %d bytes --> %d\n",
585 dev->in_ep->name, dev->in_req->length, status);
586 usb_ep_set_halt(dev->in_ep);
589 /* Wait until tx interrupt received */
591 usb_gadget_handle_interrupts(0);
596 static void thor_rx_tx_complete(struct usb_ep *ep, struct usb_request *req)
598 struct thor_dev *dev = thor_func->dev;
599 int status = req->status;
601 debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__, ep, req);
604 if (ep == dev->out_ep)
611 /* this endpoint is normally active while we're configured */
612 case -ECONNABORTED: /* hardware forced ep reset */
613 case -ECONNRESET: /* request dequeued */
614 case -ESHUTDOWN: /* disconnect from host */
615 case -EREMOTEIO: /* short read */
617 pr_err("ERROR:%d\n", status);
621 debug("%s complete --> %d, %d/%d\n", ep->name,
622 status, req->actual, req->length);
625 static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
627 if (req->status || req->actual != req->length)
628 debug("setup complete --> %d, %d/%d\n",
629 req->status, req->actual, req->length);
633 thor_func_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
635 struct thor_dev *dev = thor_func->dev;
636 struct usb_request *req = dev->req;
637 struct usb_gadget *gadget = dev->gadget;
640 u16 len = le16_to_cpu(ctrl->wLength);
642 debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
643 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, ctrl->wIndex,
646 switch (ctrl->bRequest) {
647 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
650 case USB_CDC_REQ_SET_LINE_CODING:
652 /* Line Coding set done = configuration done */
653 thor_func->dev->configuration_done = 1;
657 pr_err("thor_setup: unknown request: %d\n", ctrl->bRequest);
662 req->zero = value < len;
663 value = usb_ep_queue(gadget->ep0, req, 0);
665 debug("%s: ep_queue: %d\n", __func__, value);
673 /* Specific to the THOR protocol */
674 static void thor_set_dma(void *addr, int len)
676 struct thor_dev *dev = thor_func->dev;
678 debug("in_req:%p, out_req:%p\n", dev->in_req, dev->out_req);
679 debug("addr:%p, len:%d\n", addr, len);
681 dev->out_req->buf = addr;
682 dev->out_req->length = len;
687 struct thor_dev *dev = thor_func->dev;
689 /* Wait for a device enumeration and configuration settings */
690 debug("THOR enumeration/configuration setting....\n");
691 while (!dev->configuration_done)
692 usb_gadget_handle_interrupts(0);
694 thor_set_dma(thor_rx_data_buf, strlen("THOR"));
695 /* detect the download request from Host PC */
696 if (thor_rx_data() < 0) {
697 printf("%s: Data not received!\n", __func__);
701 if (!strncmp((char *)thor_rx_data_buf, "THOR", strlen("THOR"))) {
702 puts("Download request from the Host PC\n");
703 udelay(30 * 1000); /* 30 ms */
705 strcpy((char *)thor_tx_data_buf, "ROHT");
706 thor_tx_data(thor_tx_data_buf, strlen("ROHT"));
708 puts("Wrong reply information\n");
715 int thor_handle(void)
719 /* receive the data from Host PC */
721 thor_set_dma(thor_rx_data_buf, sizeof(struct rqt_box));
722 ret = thor_rx_data();
725 ret = process_data();
726 #ifdef CONFIG_THOR_RESET_OFF
727 if (ret == RESET_DONE)
733 printf("%s: No data received!\n", __func__);
741 static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
745 usb_ep_free_request(ep, req);
748 static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
750 struct usb_gadget *gadget = c->cdev->gadget;
751 struct f_thor *f_thor = func_to_thor(f);
752 struct thor_dev *dev;
757 dev = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*dev));
761 memset(dev, 0, sizeof(*dev));
762 dev->gadget = gadget;
765 debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
767 debug("f_thor: 0x%p thor: 0x%p\n", f_thor, dev);
770 /* preallocate control response and buffer */
771 dev->req = usb_ep_alloc_request(gadget->ep0, 0);
776 dev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
778 if (!dev->req->buf) {
783 dev->req->complete = thor_setup_complete;
785 /* DYNAMIC interface numbers assignments */
786 status = usb_interface_id(c, f);
791 thor_downloader_intf_int.bInterfaceNumber = status;
792 thor_downloader_cdc_union.bMasterInterface0 = status;
794 status = usb_interface_id(c, f);
799 thor_downloader_intf_data.bInterfaceNumber = status;
800 thor_downloader_cdc_union.bSlaveInterface0 = status;
802 /* allocate instance-specific endpoints */
803 ep = usb_ep_autoconfig(gadget, &fs_in_desc);
809 if (gadget_is_dualspeed(gadget)) {
810 hs_in_desc.bEndpointAddress =
811 fs_in_desc.bEndpointAddress;
814 dev->in_ep = ep; /* Store IN EP for enabling @ setup */
815 ep->driver_data = dev;
817 ep = usb_ep_autoconfig(gadget, &fs_out_desc);
823 if (gadget_is_dualspeed(gadget))
824 hs_out_desc.bEndpointAddress =
825 fs_out_desc.bEndpointAddress;
827 dev->out_ep = ep; /* Store OUT EP for enabling @ setup */
828 ep->driver_data = dev;
830 ep = usb_ep_autoconfig(gadget, &fs_int_desc);
837 ep->driver_data = dev;
839 if (gadget_is_dualspeed(gadget)) {
840 hs_int_desc.bEndpointAddress =
841 fs_int_desc.bEndpointAddress;
843 f->hs_descriptors = (struct usb_descriptor_header **)
844 &hs_thor_downloader_function;
846 if (!f->hs_descriptors)
850 debug("%s: out_ep:%p out_req:%p\n", __func__,
851 dev->out_ep, dev->out_req);
857 free_ep_req(gadget->ep0, dev->req);
862 static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
864 struct f_thor *f_thor = func_to_thor(f);
865 struct thor_dev *dev = f_thor->dev;
867 free_ep_req(dev->gadget->ep0, dev->req);
869 memset(thor_func, 0, sizeof(*thor_func));
873 static void thor_func_disable(struct usb_function *f)
875 struct f_thor *f_thor = func_to_thor(f);
876 struct thor_dev *dev = f_thor->dev;
878 debug("%s:\n", __func__);
880 /* Avoid freeing memory when ep is still claimed */
881 if (dev->in_ep->driver_data) {
882 usb_ep_disable(dev->in_ep);
883 free_ep_req(dev->in_ep, dev->in_req);
884 dev->in_ep->driver_data = NULL;
887 if (dev->out_ep->driver_data) {
888 usb_ep_disable(dev->out_ep);
889 usb_ep_free_request(dev->out_ep, dev->out_req);
890 dev->out_ep->driver_data = NULL;
893 if (dev->int_ep->driver_data) {
894 usb_ep_disable(dev->int_ep);
895 dev->int_ep->driver_data = NULL;
899 static int thor_eps_setup(struct usb_function *f)
901 struct usb_composite_dev *cdev = f->config->cdev;
902 struct usb_gadget *gadget = cdev->gadget;
903 struct thor_dev *dev = thor_func->dev;
904 struct usb_endpoint_descriptor *d;
905 struct usb_request *req;
910 d = ep_desc(gadget, &hs_in_desc, &fs_in_desc);
911 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
913 result = usb_ep_enable(ep, d);
917 ep->driver_data = cdev; /* claim */
918 req = alloc_ep_req(ep, THOR_PACKET_SIZE);
921 goto err_disable_in_ep;
924 memset(req->buf, 0, req->length);
925 req->complete = thor_rx_tx_complete;
928 d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
929 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
931 result = usb_ep_enable(ep, d);
933 goto err_free_in_req;
935 ep->driver_data = cdev; /* claim */
936 req = usb_ep_alloc_request(ep, 0);
939 goto err_disable_out_ep;
942 req->complete = thor_rx_tx_complete;
946 d = ep_desc(gadget, &hs_int_desc, &fs_int_desc);
947 debug("(d)bEndpointAddress: 0x%x\n", d->bEndpointAddress);
949 result = usb_ep_enable(ep, d);
953 ep->driver_data = cdev; /* claim */
958 usb_ep_disable(dev->out_ep);
961 free_ep_req(dev->in_ep, dev->in_req);
965 usb_ep_disable(dev->in_ep);
971 static int thor_func_set_alt(struct usb_function *f,
972 unsigned intf, unsigned alt)
974 struct thor_dev *dev = thor_func->dev;
977 debug("%s: func: %s intf: %d alt: %d\n",
978 __func__, f->name, intf, alt);
982 debug("ACM INTR interface\n");
985 debug("Communication Data interface\n");
986 result = thor_eps_setup(f);
988 pr_err("%s: EPs setup failed!\n", __func__);
989 dev->configuration_done = 1;
996 static int thor_func_init(struct usb_configuration *c)
998 struct f_thor *f_thor;
1001 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
1003 f_thor = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_thor));
1007 memset(f_thor, 0, sizeof(*f_thor));
1009 f_thor->usb_function.name = "f_thor";
1010 f_thor->usb_function.bind = thor_func_bind;
1011 f_thor->usb_function.unbind = thor_unbind;
1012 f_thor->usb_function.setup = thor_func_setup;
1013 f_thor->usb_function.set_alt = thor_func_set_alt;
1014 f_thor->usb_function.disable = thor_func_disable;
1016 status = usb_add_function(c, &f_thor->usb_function);
1023 int thor_add(struct usb_configuration *c)
1025 debug("%s:\n", __func__);
1026 return thor_func_init(c);
1029 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor, thor_add);