/*
* Gadget Driver for iAP
*
* Copyright (C) 2016
* Author:
*
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#define IAP_BULK_BUFFER_SIZE 4096
/* number of tx requests to allocate */
#define TX_REQ_MAX 4
#define IAP_GET_DEVICE_STATE _IOW('z', 1, int)
static const char iap_shortname[] = "zjinnova_iap";
struct iap_dev {
struct usb_function function;
struct usb_composite_dev *cdev;
spinlock_t lock;
struct usb_ep *ep_in;
struct usb_ep *ep_out;
int online;
int error;
atomic_t read_excl;
atomic_t write_excl;
atomic_t open_excl;
struct list_head tx_idle;
wait_queue_head_t read_wq;
wait_queue_head_t write_wq;
struct usb_request *rx_req;
int rx_done;
struct work_struct work;
struct miscdevice *misc_device;
int sw_online;
};
static struct usb_interface_descriptor iap_interface_desc = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 2,
.bInterfaceClass = 0xFF,
.bInterfaceSubClass = 0xF0,
.bInterfaceProtocol = 0,
};
static struct usb_endpoint_descriptor iap_highspeed_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(512),
};
static struct usb_endpoint_descriptor iap_highspeed_out_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(512),
};
static struct usb_endpoint_descriptor iap_fullspeed_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};
static struct usb_endpoint_descriptor iap_fullspeed_out_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};
static struct usb_descriptor_header *fs_iap_descs[] = {
(struct usb_descriptor_header *) &iap_interface_desc,
(struct usb_descriptor_header *) &iap_fullspeed_in_desc,
(struct usb_descriptor_header *) &iap_fullspeed_out_desc,
NULL,
};
static struct usb_descriptor_header *hs_iap_descs[] = {
(struct usb_descriptor_header *) &iap_interface_desc,
(struct usb_descriptor_header *) &iap_highspeed_in_desc,
(struct usb_descriptor_header *) &iap_highspeed_out_desc,
NULL,
};
static struct usb_string iap_string_defs[] = {
[0].s = "iAP Interface",
{ } /* end of list */
};
static struct usb_gadget_strings iap_string_table = {
.language = 0x0409, /* en-us */
.strings = iap_string_defs,
};
static struct usb_gadget_strings *iap_strings[] = {
&iap_string_table,
NULL,
};
/* temporary variable used between iap_open() and iap_gadget_bind() */
static struct iap_dev *_iap_dev;
static inline struct iap_dev *func_to_iap(struct usb_function *f)
{
return container_of(f, struct iap_dev, function);
}
static struct usb_request *iap_request_new(struct usb_ep *ep, int buffer_size)
{