2 * f_thor.c -- USB TIZEN THOR Downloader gadget function
4 * Copyright (C) 2013 Samsung Electronics
5 * Lukasz Majewski <l.majewski@samsung.com>
8 * git://review.tizen.org/kernel/u-boot
11 * Copyright (C) 2009 Samsung Electronics
12 * Minkyu Kang <mk7.kang@samsung.com>
13 * Sanghee Kim <sh0130.kim@samsung.com>
15 * SPDX-License-Identifier: GPL-2.0+
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/usb/composite.h>
25 #include <linux/usb/cdc.h>
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);
35 static struct f_thor
*thor_func
;
36 static inline struct f_thor
*func_to_thor(struct usb_function
*f
)
38 return container_of(f
, struct f_thor
, usb_function
);
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
));
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
;
53 static void send_rsp(const struct rsp_box
*rsp
)
55 memcpy(thor_tx_data_buf
, rsp
, sizeof(struct rsp_box
));
56 thor_tx_data(thor_tx_data_buf
, sizeof(struct rsp_box
));
58 debug("-RSP: %d, %d\n", rsp
->rsp
, rsp
->rsp_data
);
61 static void send_data_rsp(s32 ack
, s32 count
)
63 ALLOC_CACHE_ALIGN_BUFFER(struct data_rsp_box
, rsp
,
64 sizeof(struct data_rsp_box
));
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
));
72 debug("-DATA RSP: %d, %d\n", ack
, count
);
75 static int process_rqt_info(const struct rqt_box
*rqt
)
77 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box
, rsp
, sizeof(struct rsp_box
));
78 memset(rsp
, 0, sizeof(struct rsp_box
));
81 rsp
->rsp_data
= rqt
->rqt_data
;
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
;
89 snprintf(rsp
->str_data
[0], sizeof(rsp
->str_data
[0]),
92 case RQT_INIT_VER_BOOT
:
93 sprintf(rsp
->str_data
[0], "%s", U_BOOT_VERSION
);
95 case RQT_INIT_VER_KERNEL
:
96 sprintf(rsp
->str_data
[0], "%s", "k unknown");
98 case RQT_INIT_VER_PLATFORM
:
99 sprintf(rsp
->str_data
[0], "%s", "p unknown");
101 case RQT_INIT_VER_CSC
:
102 sprintf(rsp
->str_data
[0], "%s", "c unknown");
112 static int process_rqt_cmd(const struct rqt_box
*rqt
)
114 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box
, rsp
, sizeof(struct rsp_box
));
115 memset(rsp
, 0, sizeof(struct rsp_box
));
118 rsp
->rsp_data
= rqt
->rqt_data
;
120 switch (rqt
->rqt_data
) {
122 debug("TARGET RESET\n");
126 #ifdef CONFIG_THOR_RESET_OFF
129 run_command("reset", 0);
131 case RQT_CMD_POWEROFF
:
132 case RQT_CMD_EFSCLEAR
:
135 printf("Command not supported -> cmd: %d\n", rqt
->rqt_data
);
142 static long long int download_head(unsigned long long total
,
143 unsigned int packet_size
,
147 long long int rcv_cnt
= 0, left_to_rcv
, ret_rcv
;
148 struct dfu_entity
*dfu_entity
= dfu_get_entity(alt_setting_num
);
149 void *transfer_buffer
= dfu_get_buf(dfu_entity
);
150 void *buf
= transfer_buffer
;
151 int usb_pkt_cnt
= 0, ret
;
154 * Files smaller than THOR_STORE_UNIT_SIZE (now 32 MiB) are stored on
156 * The packet response is sent on the purpose after successful data
157 * chunk write. There is a room for improvement when asynchronous write
160 while (total
- rcv_cnt
>= packet_size
) {
161 thor_set_dma(buf
, packet_size
);
163 ret_rcv
= thor_rx_data();
167 debug("%d: RCV data count: %llu cnt: %d\n", usb_pkt_cnt
,
170 if ((rcv_cnt
% THOR_STORE_UNIT_SIZE
) == 0) {
171 ret
= dfu_write(dfu_get_entity(alt_setting_num
),
172 transfer_buffer
, THOR_STORE_UNIT_SIZE
,
175 error("DFU write failed [%d] cnt: %d",
179 buf
= transfer_buffer
;
181 send_data_rsp(0, ++usb_pkt_cnt
);
184 /* Calculate the amount of data to arrive from PC (in bytes) */
185 left_to_rcv
= total
- rcv_cnt
;
188 * Calculate number of data already received. but not yet stored
189 * on the medium (they are smaller than THOR_STORE_UNIT_SIZE)
191 *left
= left_to_rcv
+ buf
- transfer_buffer
;
192 debug("%s: left: %llu left_to_rcv: %llu buf: 0x%p\n", __func__
,
193 *left
, left_to_rcv
, buf
);
196 thor_set_dma(buf
, packet_size
);
197 ret_rcv
= thor_rx_data();
201 send_data_rsp(0, ++usb_pkt_cnt
);
204 debug("%s: %llu total: %llu cnt: %d\n", __func__
, rcv_cnt
, total
, *cnt
);
209 static int download_tail(long long int left
, int cnt
)
211 struct dfu_entity
*dfu_entity
;
212 void *transfer_buffer
;
215 debug("%s: left: %llu cnt: %d\n", __func__
, left
, cnt
);
217 dfu_entity
= dfu_get_entity(alt_setting_num
);
219 error("Alt setting: %d entity not found!\n", alt_setting_num
);
223 transfer_buffer
= dfu_get_buf(dfu_entity
);
224 if (!transfer_buffer
) {
225 error("Transfer buffer not allocated!");
230 ret
= dfu_write(dfu_entity
, transfer_buffer
, left
, cnt
++);
232 error("DFU write failed [%d]: left: %llu", ret
, left
);
238 * To store last "packet" or write file from buffer to filesystem
239 * DFU storage backend requires dfu_flush
241 * This also frees memory malloc'ed by dfu_get_buf(), so no explicit
242 * need fo call dfu_free_buf() is needed.
244 ret
= dfu_flush(dfu_entity
, transfer_buffer
, 0, cnt
);
246 error("DFU flush failed!");
251 static long long int process_rqt_download(const struct rqt_box
*rqt
)
253 ALLOC_CACHE_ALIGN_BUFFER(struct rsp_box
, rsp
, sizeof(struct rsp_box
));
254 static long long int left
, ret_head
;
255 int file_type
, ret
= 0;
258 memset(rsp
, 0, sizeof(struct rsp_box
));
260 rsp
->rsp_data
= rqt
->rqt_data
;
262 switch (rqt
->rqt_data
) {
264 thor_file_size
= rqt
->int_data
[0];
265 debug("INIT: total %d bytes\n", rqt
->int_data
[0]);
267 case RQT_DL_FILE_INFO
:
268 file_type
= rqt
->int_data
[0];
269 if (file_type
== FILE_TYPE_PIT
) {
270 puts("PIT table file - not supported\n");
271 rsp
->ack
= -ENOTSUPP
;
276 thor_file_size
= rqt
->int_data
[1];
277 memcpy(f_name
, rqt
->str_data
[0], F_NAME_BUF_SIZE
);
279 debug("INFO: name(%s, %d), size(%llu), type(%d)\n",
280 f_name
, 0, thor_file_size
, file_type
);
282 rsp
->int_data
[0] = THOR_PACKET_SIZE
;
284 alt_setting_num
= dfu_get_alt(f_name
);
285 if (alt_setting_num
< 0) {
286 error("Alt setting [%d] to write not found!",
292 case RQT_DL_FILE_START
:
294 ret_head
= download_head(thor_file_size
, THOR_PACKET_SIZE
,
301 case RQT_DL_FILE_END
:
302 debug("DL FILE_END\n");
303 rsp
->ack
= download_tail(left
, cnt
);
312 error("Operation not supported: %d", rqt
->rqt_data
);
320 static int process_data(void)
322 ALLOC_CACHE_ALIGN_BUFFER(struct rqt_box
, rqt
, sizeof(struct rqt_box
));
325 memcpy(rqt
, thor_rx_data_buf
, sizeof(struct rqt_box
));
327 debug("+RQT: %d, %d\n", rqt
->rqt
, rqt
->rqt_data
);
331 ret
= process_rqt_info(rqt
);
334 ret
= process_rqt_cmd(rqt
);
337 ret
= (int) process_rqt_download(rqt
);
340 puts("RQT: UPLOAD not supported!\n");
343 error("unknown request (%d)", rqt
->rqt
);
349 /* ********************************************************** */
350 /* THOR USB Function */
351 /* ********************************************************** */
353 static inline struct usb_endpoint_descriptor
*
354 ep_desc(struct usb_gadget
*g
, struct usb_endpoint_descriptor
*hs
,
355 struct usb_endpoint_descriptor
*fs
)
357 if (gadget_is_dualspeed(g
) && g
->speed
== USB_SPEED_HIGH
)
362 static struct usb_interface_descriptor thor_downloader_intf_data
= {
363 .bLength
= sizeof(thor_downloader_intf_data
),
364 .bDescriptorType
= USB_DT_INTERFACE
,
367 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
370 static struct usb_endpoint_descriptor fs_in_desc
= {
371 .bLength
= USB_DT_ENDPOINT_SIZE
,
372 .bDescriptorType
= USB_DT_ENDPOINT
,
374 .bEndpointAddress
= USB_DIR_IN
,
375 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
378 static struct usb_endpoint_descriptor fs_out_desc
= {
379 .bLength
= USB_DT_ENDPOINT_SIZE
,
380 .bDescriptorType
= USB_DT_ENDPOINT
,
382 .bEndpointAddress
= USB_DIR_OUT
,
383 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
386 /* CDC configuration */
387 static struct usb_interface_descriptor thor_downloader_intf_int
= {
388 .bLength
= sizeof(thor_downloader_intf_int
),
389 .bDescriptorType
= USB_DT_INTERFACE
,
392 .bInterfaceClass
= USB_CLASS_COMM
,
393 /* 0x02 Abstract Line Control Model */
394 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ACM
,
395 /* 0x01 Common AT commands */
396 .bInterfaceProtocol
= USB_CDC_ACM_PROTO_AT_V25TER
,
399 static struct usb_cdc_header_desc thor_downloader_cdc_header
= {
400 .bLength
= sizeof(thor_downloader_cdc_header
),
401 .bDescriptorType
= 0x24, /* CS_INTERFACE */
402 .bDescriptorSubType
= 0x00,
406 static struct usb_cdc_call_mgmt_descriptor thor_downloader_cdc_call
= {
407 .bLength
= sizeof(thor_downloader_cdc_call
),
408 .bDescriptorType
= 0x24, /* CS_INTERFACE */
409 .bDescriptorSubType
= 0x01,
410 .bmCapabilities
= 0x00,
411 .bDataInterface
= 0x01,
414 static struct usb_cdc_acm_descriptor thor_downloader_cdc_abstract
= {
415 .bLength
= sizeof(thor_downloader_cdc_abstract
),
416 .bDescriptorType
= 0x24, /* CS_INTERFACE */
417 .bDescriptorSubType
= 0x02,
418 .bmCapabilities
= 0x00,
421 static struct usb_cdc_union_desc thor_downloader_cdc_union
= {
422 .bLength
= sizeof(thor_downloader_cdc_union
),
423 .bDescriptorType
= 0x24, /* CS_INTERFACE */
424 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
427 static struct usb_endpoint_descriptor fs_int_desc
= {
428 .bLength
= USB_DT_ENDPOINT_SIZE
,
429 .bDescriptorType
= USB_DT_ENDPOINT
,
431 .bEndpointAddress
= 3 | USB_DIR_IN
,
432 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
433 .wMaxPacketSize
= __constant_cpu_to_le16(16),
438 static struct usb_interface_assoc_descriptor
439 thor_iad_descriptor
= {
440 .bLength
= sizeof(thor_iad_descriptor
),
441 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
443 .bFirstInterface
= 0,
444 .bInterfaceCount
= 2, /* control + data */
445 .bFunctionClass
= USB_CLASS_COMM
,
446 .bFunctionSubClass
= USB_CDC_SUBCLASS_ACM
,
447 .bFunctionProtocol
= USB_CDC_PROTO_NONE
,
450 static struct usb_endpoint_descriptor hs_in_desc
= {
451 .bLength
= USB_DT_ENDPOINT_SIZE
,
452 .bDescriptorType
= USB_DT_ENDPOINT
,
454 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
455 .wMaxPacketSize
= __constant_cpu_to_le16(512),
458 static struct usb_endpoint_descriptor hs_out_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_int_desc
= {
467 .bLength
= USB_DT_ENDPOINT_SIZE
,
468 .bDescriptorType
= USB_DT_ENDPOINT
,
470 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
471 .wMaxPacketSize
= __constant_cpu_to_le16(16),
477 * This attribute vendor descriptor is necessary for correct operation with
478 * Windows version of THOR download program
480 * It prevents windows driver from sending zero lenght packet (ZLP) after
481 * each THOR_PACKET_SIZE. This assures consistent behaviour with libusb
483 static struct usb_cdc_attribute_vendor_descriptor thor_downloader_cdc_av
= {
484 .bLength
= sizeof(thor_downloader_cdc_av
),
485 .bDescriptorType
= 0x24,
486 .bDescriptorSubType
= 0x80,
492 static const struct usb_descriptor_header
*hs_thor_downloader_function
[] = {
493 (struct usb_descriptor_header
*)&thor_iad_descriptor
,
495 (struct usb_descriptor_header
*)&thor_downloader_intf_int
,
496 (struct usb_descriptor_header
*)&thor_downloader_cdc_header
,
497 (struct usb_descriptor_header
*)&thor_downloader_cdc_call
,
498 (struct usb_descriptor_header
*)&thor_downloader_cdc_abstract
,
499 (struct usb_descriptor_header
*)&thor_downloader_cdc_union
,
500 (struct usb_descriptor_header
*)&hs_int_desc
,
502 (struct usb_descriptor_header
*)&thor_downloader_intf_data
,
503 (struct usb_descriptor_header
*)&thor_downloader_cdc_av
,
504 (struct usb_descriptor_header
*)&hs_in_desc
,
505 (struct usb_descriptor_header
*)&hs_out_desc
,
509 /*-------------------------------------------------------------------------*/
510 static struct usb_request
*alloc_ep_req(struct usb_ep
*ep
, unsigned length
)
512 struct usb_request
*req
;
514 req
= usb_ep_alloc_request(ep
, 0);
518 req
->length
= length
;
519 req
->buf
= memalign(CONFIG_SYS_CACHELINE_SIZE
, length
);
521 usb_ep_free_request(ep
, req
);
528 static int thor_rx_data(void)
530 struct thor_dev
*dev
= thor_func
->dev
;
531 int data_to_rx
, tmp
, status
;
533 data_to_rx
= dev
->out_req
->length
;
536 dev
->out_req
->length
= data_to_rx
;
537 debug("dev->out_req->length:%d dev->rxdata:%d\n",
538 dev
->out_req
->length
, dev
->rxdata
);
540 status
= usb_ep_queue(dev
->out_ep
, dev
->out_req
, 0);
542 error("kill %s: resubmit %d bytes --> %d",
543 dev
->out_ep
->name
, dev
->out_req
->length
, status
);
544 usb_ep_set_halt(dev
->out_ep
);
548 while (!dev
->rxdata
) {
549 usb_gadget_handle_interrupts(0);
554 data_to_rx
-= dev
->out_req
->actual
;
555 } while (data_to_rx
);
560 static void thor_tx_data(unsigned char *data
, int len
)
562 struct thor_dev
*dev
= thor_func
->dev
;
563 unsigned char *ptr
= dev
->in_req
->buf
;
567 memcpy(ptr
, data
, len
);
569 dev
->in_req
->length
= len
;
571 debug("%s: dev->in_req->length:%d to_cpy:%d\n", __func__
,
572 dev
->in_req
->length
, sizeof(data
));
574 status
= usb_ep_queue(dev
->in_ep
, dev
->in_req
, 0);
576 error("kill %s: resubmit %d bytes --> %d",
577 dev
->in_ep
->name
, dev
->in_req
->length
, status
);
578 usb_ep_set_halt(dev
->in_ep
);
581 /* Wait until tx interrupt received */
583 usb_gadget_handle_interrupts(0);
588 static void thor_rx_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
590 struct thor_dev
*dev
= thor_func
->dev
;
591 int status
= req
->status
;
593 debug("%s: ep_ptr:%p, req_ptr:%p\n", __func__
, ep
, req
);
596 if (ep
== dev
->out_ep
)
603 /* this endpoint is normally active while we're configured */
604 case -ECONNABORTED
: /* hardware forced ep reset */
605 case -ECONNRESET
: /* request dequeued */
606 case -ESHUTDOWN
: /* disconnect from host */
607 case -EREMOTEIO
: /* short read */
609 error("ERROR:%d", status
);
613 debug("%s complete --> %d, %d/%d\n", ep
->name
,
614 status
, req
->actual
, req
->length
);
617 static struct usb_request
*thor_start_ep(struct usb_ep
*ep
)
619 struct usb_request
*req
;
621 req
= alloc_ep_req(ep
, THOR_PACKET_SIZE
);
622 debug("%s: ep:%p req:%p\n", __func__
, ep
, req
);
627 memset(req
->buf
, 0, req
->length
);
628 req
->complete
= thor_rx_tx_complete
;
633 static void thor_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
635 if (req
->status
|| req
->actual
!= req
->length
)
636 debug("setup complete --> %d, %d/%d\n",
637 req
->status
, req
->actual
, req
->length
);
641 thor_func_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
643 struct thor_dev
*dev
= thor_func
->dev
;
644 struct usb_request
*req
= dev
->req
;
645 struct usb_gadget
*gadget
= dev
->gadget
;
648 u16 len
= le16_to_cpu(ctrl
->wLength
);
650 debug("Req_Type: 0x%x Req: 0x%x wValue: 0x%x wIndex: 0x%x wLen: 0x%x\n",
651 ctrl
->bRequestType
, ctrl
->bRequest
, ctrl
->wValue
, ctrl
->wIndex
,
654 switch (ctrl
->bRequest
) {
655 case USB_CDC_REQ_SET_CONTROL_LINE_STATE
:
658 case USB_CDC_REQ_SET_LINE_CODING
:
660 /* Line Coding set done = configuration done */
661 thor_func
->dev
->configuration_done
= 1;
665 error("thor_setup: unknown request: %d", ctrl
->bRequest
);
670 req
->zero
= value
< len
;
671 value
= usb_ep_queue(gadget
->ep0
, req
, 0);
673 debug("%s: ep_queue: %d\n", __func__
, value
);
681 /* Specific to the THOR protocol */
682 static void thor_set_dma(void *addr
, int len
)
684 struct thor_dev
*dev
= thor_func
->dev
;
686 debug("in_req:%p, out_req:%p\n", dev
->in_req
, dev
->out_req
);
687 debug("addr:%p, len:%d\n", addr
, len
);
689 dev
->out_req
->buf
= addr
;
690 dev
->out_req
->length
= len
;
695 struct thor_dev
*dev
= thor_func
->dev
;
697 /* Wait for a device enumeration and configuration settings */
698 debug("THOR enumeration/configuration setting....\n");
699 while (!dev
->configuration_done
)
700 usb_gadget_handle_interrupts(0);
702 thor_set_dma(thor_rx_data_buf
, strlen("THOR"));
703 /* detect the download request from Host PC */
704 if (thor_rx_data() < 0) {
705 printf("%s: Data not received!\n", __func__
);
709 if (!strncmp((char *)thor_rx_data_buf
, "THOR", strlen("THOR"))) {
710 puts("Download request from the Host PC\n");
711 udelay(30 * 1000); /* 30 ms */
713 strcpy((char *)thor_tx_data_buf
, "ROHT");
714 thor_tx_data(thor_tx_data_buf
, strlen("ROHT"));
716 puts("Wrong reply information\n");
723 int thor_handle(void)
727 /* receive the data from Host PC */
729 thor_set_dma(thor_rx_data_buf
, sizeof(struct rqt_box
));
730 ret
= thor_rx_data();
733 ret
= process_data();
734 #ifdef CONFIG_THOR_RESET_OFF
735 if (ret
== RESET_DONE
)
741 printf("%s: No data received!\n", __func__
);
749 static int thor_func_bind(struct usb_configuration
*c
, struct usb_function
*f
)
751 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
752 struct f_thor
*f_thor
= func_to_thor(f
);
753 struct thor_dev
*dev
;
758 dev
= memalign(CONFIG_SYS_CACHELINE_SIZE
, sizeof(*dev
));
762 memset(dev
, 0, sizeof(*dev
));
763 dev
->gadget
= gadget
;
766 debug("%s: usb_configuration: 0x%p usb_function: 0x%p\n",
768 debug("f_thor: 0x%p thor: 0x%p\n", f_thor
, dev
);
771 /* preallocate control response and buffer */
772 dev
->req
= usb_ep_alloc_request(gadget
->ep0
, 0);
777 dev
->req
->buf
= memalign(CONFIG_SYS_CACHELINE_SIZE
,
779 if (!dev
->req
->buf
) {
784 dev
->req
->complete
= thor_setup_complete
;
786 /* DYNAMIC interface numbers assignments */
787 status
= usb_interface_id(c
, f
);
792 thor_downloader_intf_int
.bInterfaceNumber
= status
;
793 thor_downloader_cdc_union
.bMasterInterface0
= status
;
795 status
= usb_interface_id(c
, f
);
800 thor_downloader_intf_data
.bInterfaceNumber
= status
;
801 thor_downloader_cdc_union
.bSlaveInterface0
= status
;
803 /* allocate instance-specific endpoints */
804 ep
= usb_ep_autoconfig(gadget
, &fs_in_desc
);
810 if (gadget_is_dualspeed(gadget
)) {
811 hs_in_desc
.bEndpointAddress
=
812 fs_in_desc
.bEndpointAddress
;
815 dev
->in_ep
= ep
; /* Store IN EP for enabling @ setup */
816 ep
->driver_data
= dev
;
818 ep
= usb_ep_autoconfig(gadget
, &fs_out_desc
);
824 if (gadget_is_dualspeed(gadget
))
825 hs_out_desc
.bEndpointAddress
=
826 fs_out_desc
.bEndpointAddress
;
828 dev
->out_ep
= ep
; /* Store OUT EP for enabling @ setup */
829 ep
->driver_data
= dev
;
831 ep
= usb_ep_autoconfig(gadget
, &fs_int_desc
);
838 ep
->driver_data
= dev
;
840 if (gadget_is_dualspeed(gadget
)) {
841 hs_int_desc
.bEndpointAddress
=
842 fs_int_desc
.bEndpointAddress
;
844 f
->hs_descriptors
= (struct usb_descriptor_header
**)
845 &hs_thor_downloader_function
;
847 if (!f
->hs_descriptors
)
851 debug("%s: out_ep:%p out_req:%p\n", __func__
,
852 dev
->out_ep
, dev
->out_req
);
861 static void free_ep_req(struct usb_ep
*ep
, struct usb_request
*req
)
864 usb_ep_free_request(ep
, req
);
867 static void thor_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
869 struct f_thor
*f_thor
= func_to_thor(f
);
870 struct thor_dev
*dev
= f_thor
->dev
;
873 memset(thor_func
, 0, sizeof(*thor_func
));
877 static void thor_func_disable(struct usb_function
*f
)
879 struct f_thor
*f_thor
= func_to_thor(f
);
880 struct thor_dev
*dev
= f_thor
->dev
;
882 debug("%s:\n", __func__
);
884 /* Avoid freeing memory when ep is still claimed */
885 if (dev
->in_ep
->driver_data
) {
886 free_ep_req(dev
->in_ep
, dev
->in_req
);
887 usb_ep_disable(dev
->in_ep
);
888 dev
->in_ep
->driver_data
= NULL
;
891 if (dev
->out_ep
->driver_data
) {
892 dev
->out_req
->buf
= NULL
;
893 usb_ep_free_request(dev
->out_ep
, dev
->out_req
);
894 usb_ep_disable(dev
->out_ep
);
895 dev
->out_ep
->driver_data
= NULL
;
898 if (dev
->int_ep
->driver_data
) {
899 usb_ep_disable(dev
->int_ep
);
900 dev
->int_ep
->driver_data
= NULL
;
904 static int thor_eps_setup(struct usb_function
*f
)
906 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
907 struct usb_gadget
*gadget
= cdev
->gadget
;
908 struct thor_dev
*dev
= thor_func
->dev
;
909 struct usb_endpoint_descriptor
*d
;
910 struct usb_request
*req
;
915 d
= ep_desc(gadget
, &hs_in_desc
, &fs_in_desc
);
916 debug("(d)bEndpointAddress: 0x%x\n", d
->bEndpointAddress
);
918 result
= usb_ep_enable(ep
, d
);
922 ep
->driver_data
= cdev
; /* claim */
923 req
= thor_start_ep(ep
);
932 d
= ep_desc(gadget
, &hs_out_desc
, &fs_out_desc
);
933 debug("(d)bEndpointAddress: 0x%x\n", d
->bEndpointAddress
);
935 result
= usb_ep_enable(ep
, d
);
939 ep
->driver_data
= cdev
; /* claim */
940 req
= thor_start_ep(ep
);
950 ep
->driver_data
= cdev
; /* claim */
956 static int thor_func_set_alt(struct usb_function
*f
,
957 unsigned intf
, unsigned alt
)
959 struct thor_dev
*dev
= thor_func
->dev
;
962 debug("%s: func: %s intf: %d alt: %d\n",
963 __func__
, f
->name
, intf
, alt
);
967 debug("ACM INTR interface\n");
970 debug("Communication Data interface\n");
971 result
= thor_eps_setup(f
);
973 error("%s: EPs setup failed!", __func__
);
974 dev
->configuration_done
= 1;
981 static int thor_func_init(struct usb_configuration
*c
)
983 struct f_thor
*f_thor
;
986 debug("%s: cdev: 0x%p\n", __func__
, c
->cdev
);
988 f_thor
= memalign(CONFIG_SYS_CACHELINE_SIZE
, sizeof(*f_thor
));
992 memset(f_thor
, 0, sizeof(*f_thor
));
994 f_thor
->usb_function
.name
= "f_thor";
995 f_thor
->usb_function
.bind
= thor_func_bind
;
996 f_thor
->usb_function
.unbind
= thor_unbind
;
997 f_thor
->usb_function
.setup
= thor_func_setup
;
998 f_thor
->usb_function
.set_alt
= thor_func_set_alt
;
999 f_thor
->usb_function
.disable
= thor_func_disable
;
1001 status
= usb_add_function(c
, &f_thor
->usb_function
);
1008 int thor_add(struct usb_configuration
*c
)
1010 debug("%s:\n", __func__
);
1011 return thor_func_init(c
);
1014 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_thor
, thor_add
);