59 static inline struct f_rudolf *func_to_rudolf(
struct usb_function *f)
61 return container_of(f,
struct f_rudolf,
function);
67 static struct usb_interface_descriptor rudolf_intf = {
68 .bLength =
sizeof rudolf_intf,
69 .bDescriptorType = USB_DT_INTERFACE,
70 .bInterfaceNumber = 0,
71 .bAlternateSetting = 0,
73 .bInterfaceClass = USB_CLASS_HID,
74 .bInterfaceSubClass = 0,
75 .bInterfaceProtocol = 0,
79 static struct hid_descriptor hs_hid_rudolf_desc = {
80 .bLength =
sizeof hs_hid_rudolf_desc,
81 .bDescriptorType = HID_DT_HID,
82 .bcdHID = cpu_to_le16(0x0110),
84 .bNumDescriptors = 0x01,
85 .desc[0].bDescriptorType = 0x22,
86 .desc[0].wDescriptorLength =
sizeof hs_hid_report_descriptor,
91 static struct hid_descriptor fs_hid_rudolf_desc = {
92 .bLength =
sizeof fs_hid_rudolf_desc,
93 .bDescriptorType = HID_DT_HID,
94 .bcdHID = cpu_to_le16(0x0110),
96 .bNumDescriptors = 0x01,
97 .desc[0].bDescriptorType = 0x22,
98 .desc[0].wDescriptorLength =
sizeof fs_hid_report_descriptor,
104 static struct usb_endpoint_descriptor rudolf_out_fs_desc = {
105 .bLength = USB_DT_ENDPOINT_SIZE,
106 .bDescriptorType = USB_DT_ENDPOINT,
107 .bEndpointAddress = USB_DIR_OUT,
108 .bmAttributes = USB_ENDPOINT_XFER_INT,
109 .wMaxPacketSize = cpu_to_le16(64),
113 static struct usb_endpoint_descriptor rudolf_in_fs_desc = {
114 .bLength = USB_DT_ENDPOINT_SIZE,
115 .bDescriptorType = USB_DT_ENDPOINT,
116 .bEndpointAddress = USB_DIR_IN,
117 .bmAttributes = USB_ENDPOINT_XFER_INT,
118 .wMaxPacketSize = cpu_to_le16(64),
124 static struct usb_endpoint_descriptor rudolf_in_hs_desc = {
125 .bLength = USB_DT_ENDPOINT_SIZE,
126 .bDescriptorType = USB_DT_ENDPOINT,
127 .bEndpointAddress = USB_DIR_IN,
128 .bmAttributes = USB_ENDPOINT_XFER_INT,
129 .wMaxPacketSize = cpu_to_le16(1024),
137 static struct usb_endpoint_descriptor rudolf_out_hs_desc = {
138 .bLength = USB_DT_ENDPOINT_SIZE,
139 .bDescriptorType = USB_DT_ENDPOINT,
140 .bEndpointAddress = USB_DIR_OUT,
141 .bmAttributes = USB_ENDPOINT_XFER_INT,
142 .wMaxPacketSize = cpu_to_le16(1024),
150 static struct usb_descriptor_header *hs_rudolf_descs[] = {
151 (
struct usb_descriptor_header *) &rudolf_intf,
152 (
struct usb_descriptor_header *) &hs_hid_rudolf_desc,
153 (
struct usb_descriptor_header *) &rudolf_in_hs_desc,
154 (
struct usb_descriptor_header *) &rudolf_out_hs_desc,
158 static struct usb_descriptor_header *fs_rudolf_descs[] = {
159 (
struct usb_descriptor_header *) &rudolf_intf,
160 (
struct usb_descriptor_header *) &fs_hid_rudolf_desc,
161 (
struct usb_descriptor_header *) &rudolf_in_fs_desc,
162 (
struct usb_descriptor_header *) &rudolf_out_fs_desc,
168 static struct usb_string strings_rudolf[] = {
169 [0].s =
"Xfer data to and from EV3 brick",
173 static struct usb_gadget_strings stringtab_rudolf = {
175 .strings = strings_rudolf,
178 static struct usb_gadget_strings *rudolf_strings[] = {
186 f_rudolf_bind(
struct usb_configuration *c,
struct usb_function *f)
188 struct usb_composite_dev *cdev = c->cdev;
189 struct f_rudolf *rudolf = func_to_rudolf(f);
196 rudolf_intf.bInterfaceNumber = id;
199 rudolf->
in_ep = usb_ep_autoconfig(cdev->gadget, &rudolf_in_fs_desc);
200 if (!rudolf->
in_ep) {
202 ERROR(cdev,
"%s: can't autoconfigure on %s\n",
203 f->name, cdev->gadget->name);
206 rudolf->
in_ep->driver_data = cdev;
208 rudolf->
out_ep = usb_ep_autoconfig(cdev->gadget, &rudolf_out_fs_desc);
211 rudolf->
out_ep->driver_data = cdev;
215 if (gadget_is_dualspeed(c->cdev->gadget)) {
217 rudolf_in_hs_desc.bEndpointAddress =
218 rudolf_in_fs_desc.bEndpointAddress;
219 rudolf_out_hs_desc.bEndpointAddress =
220 rudolf_out_fs_desc.bEndpointAddress;
221 f->hs_descriptors = hs_rudolf_descs;
222 f->descriptors = fs_rudolf_descs;
225 DBG(cdev,
"%s speed %s: IN/%s, OUT/%s\n",
226 gadget_is_dualspeed(c->cdev->gadget) ?
"dual" :
"full",
227 f->name, rudolf->
in_ep->name, rudolf->
out_ep->name);
232 f_rudolf_unbind(
struct usb_configuration *c,
struct usb_function *f)
234 kfree(func_to_rudolf(f));
237 static void usb_req_arm(
struct usb_ep *ep,
struct usb_request *req)
252 status = usb_ep_queue(ep, req, GFP_ATOMIC);
259 static int read_data_from_host(
struct usb_request *req)
264 int from_host_length = 0;
268 for (i = 0; i < req->actual; i++, buf++)
275 return (from_host_length);
278 static void write_data_to_the_host(
struct usb_ep *ep,
struct usb_request *req)
285 printk(
"WR to HOST req->length = %d\r\n", req->length);
295 for (i = 0; i < req->length; i++)
300 static void rudolf_complete(
struct usb_ep *ep,
struct usb_request *req)
302 struct f_rudolf *rudolf = ep->driver_data;
303 int status = req->status;
313 printk(
"Rudolf_complete OUT\n");
317 usb_req_arm(ep, req);
323 printk(
"Rudolf_complete IN\n");
331 printk(
"IN_IN_IN - READY ?????\n");
339 printk(
"IN_IN_IN - PENDING settes to BUSY\n");
343 write_data_to_the_host(ep, req);
344 usb_req_arm(ep, req);
349 printk(
"IN_IN_IN - BUSY settes to READY\n");
358 printk(
"IN_IN_IN - IDLE\n");
379 read_data_from_host(req);
398 static int rudolf_start_ep(
struct f_rudolf *rudolf,
bool is_in)
401 struct usb_request *req;
409 req->complete = rudolf_complete;
412 printk(
"UsbSpeed.Speed = %d\n\r",
UsbSpeed.Speed);
418 printk(
"rudolf_start_ep FULL\n\r");
421 (*pUsbSpeed).Speed = FULL_SPEED;
428 printk(
"rudolf_start_ep HIGH\n\r");
431 (*pUsbSpeed).Speed = HIGH_SPEED;
442 printk(
"req->length = %d ***** Rudolf_Start_Ep_in\n\r", req->length);
451 printk(
"***** Rudolf_Start_Ep_out\n");
455 status = usb_ep_queue(ep, req, GFP_ATOMIC);
458 struct usb_composite_dev *cdev;
460 cdev = rudolf->
function.config->cdev;
461 ERROR(cdev,
"start %s %s --> %d\n",
462 is_in ?
"IN" :
"OUT",
471 static void disable_rudolf(
struct f_rudolf *rudolf)
473 struct usb_composite_dev *cdev;
475 cdev = rudolf->
function.config->cdev;
477 VDBG(cdev,
"%s disabled\n", rudolf->
function.name);
481 enable_rudolf(
struct usb_composite_dev *cdev,
struct f_rudolf *rudolf)
484 const struct usb_endpoint_descriptor *ep_in, *ep_out;
487 ep_in = ep_choose(cdev->gadget, &rudolf_in_hs_desc, &rudolf_in_fs_desc);
489 ep_out = ep_choose(cdev->gadget, &rudolf_out_hs_desc, &rudolf_out_fs_desc);
493 result = usb_ep_enable(ep, ep_in);
498 ep->driver_data = rudolf;
500 result = rudolf_start_ep(rudolf,
true);
506 ep->driver_data =
NULL;
512 result = usb_ep_enable(ep, ep_out);
517 ep->driver_data = rudolf;
519 result = rudolf_start_ep(rudolf,
false);
523 ep->driver_data =
NULL;
527 DBG(cdev,
"%s enabled\n", rudolf->
function.name);
532 static int f_rudolf_set_alt(
struct usb_function *f,
533 unsigned intf,
unsigned alt)
535 struct f_rudolf *rudolf = func_to_rudolf(f);
536 struct usb_composite_dev *cdev = f->config->cdev;
539 if (rudolf->
in_ep->driver_data)
540 disable_rudolf(rudolf);
542 return enable_rudolf(cdev, rudolf);
545 static void f_rudolf_disable(
struct usb_function *f)
547 struct f_rudolf *rudolf = func_to_rudolf(f);
549 disable_rudolf(rudolf);
554 static int rudolf_bind_config(
struct usb_configuration *c)
559 rudolf = kzalloc(
sizeof *rudolf, GFP_KERNEL);
563 rudolf->
function.name =
"rudolf xfer";
564 rudolf->
function.descriptors = hs_rudolf_descs;
565 rudolf->
function.bind = f_rudolf_bind;
566 rudolf->
function.unbind = f_rudolf_unbind;
567 rudolf->
function.set_alt = f_rudolf_set_alt;
568 rudolf->
function.disable = f_rudolf_disable;
577 static int rudolf_setup(
struct usb_configuration *c,
578 const struct usb_ctrlrequest *ctrl)
580 struct usb_request *req = c->cdev->req;
581 int value = -EOPNOTSUPP;
582 u16 w_index = le16_to_cpu(ctrl->wIndex);
583 u16 w_value = le16_to_cpu(ctrl->wValue);
584 u16 w_length = le16_to_cpu(ctrl->wLength);
591 switch (ctrl->bRequest) {
604 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
606 if (w_value || w_index)
609 if (w_length > req->length)
614 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
616 if (w_value || w_index)
619 if (w_length > req->length)
627 "unknown control req%02x.%02x v%04x i%04x l%d\n",
628 ctrl->bRequestType, ctrl->bRequest,
629 w_value, w_index, w_length);
633 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
636 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
637 | USB_REQ_GET_DESCRIPTOR):
638 switch (w_value >> 8) {
642 length = min_t(
unsigned short, length,
643 sizeof hs_hid_report_descriptor);
644 memcpy(req->buf, hs_hid_report_descriptor, length);
670 VDBG(c->cdev,
"source/sink req%02x.%02x v%04x i%04x l%d\n",
671 ctrl->bRequestType, ctrl->bRequest,
672 w_value, w_index, w_length);
675 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
677 ERROR(c->cdev,
"source/sinkc response, err %d\n",
688 static struct usb_configuration rudolf_driver = {
689 .label =
"rudolf driver",
690 .strings = rudolf_strings,
691 .bind = rudolf_bind_config,
692 .setup = rudolf_setup,
693 .bConfigurationValue = 1,
694 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
710 strings_rudolf[0].id = id;
712 rudolf_intf.iInterface = id;
713 rudolf_driver.iConfiguration = 1;
717 rudolf_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
720 if (gadget_is_otg(cdev->gadget)) {
721 rudolf_driver.descriptors = otg_desc;
722 rudolf_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
int usb_string_id(struct usb_composite_dev *cdev)
struct usb_request * save_in_req
struct usb_ep * save_in_ep
struct usb_function function
int rudolf_add(struct usb_composite_dev *cdev, bool autoresume)
int usb_add_function(struct usb_configuration *config, struct usb_function *function)
void disable_endpoints(struct usb_composite_dev *cdev, struct usb_ep *in, struct usb_ep *out)
void free_ep_req(struct usb_ep *ep, struct usb_request *req)
struct usb_request * alloc_ep_req(struct usb_ep *ep)
int usb_interface_id(struct usb_configuration *config, struct usb_function *function)
int usb_add_config(struct usb_composite_dev *cdev, struct usb_configuration *config)
char usb_char_buffer_out[MAX_EP_SIZE]
char usb_char_buffer_in[MAX_EP_SIZE]