2007-11-30 00:13:00 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2008-04-18 16:42:50 +00:00
|
|
|
* $Id$
|
2007-11-30 00:13:00 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Christian Gmeiner
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2008-03-02 20:45:33 +00:00
|
|
|
#include "string.h"
|
2007-11-30 00:13:00 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "usb_core.h"
|
|
|
|
#include "usb_drv.h"
|
2008-03-02 20:45:33 +00:00
|
|
|
#include "kernel.h"
|
2008-05-03 07:10:21 +00:00
|
|
|
#include "usb_serial.h"
|
2007-11-30 00:13:00 +00:00
|
|
|
|
|
|
|
//#define LOGF_ENABLE
|
|
|
|
#include "logf.h"
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
#ifdef USB_SERIAL
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
/* serial interface */
|
2008-04-26 19:02:16 +00:00
|
|
|
static struct usb_interface_descriptor __attribute__((aligned(2)))
|
2008-03-06 21:25:09 +00:00
|
|
|
interface_descriptor =
|
|
|
|
{
|
|
|
|
.bLength = sizeof(struct usb_interface_descriptor),
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bAlternateSetting = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_CDC_DATA,
|
|
|
|
.bInterfaceSubClass = 0,
|
|
|
|
.bInterfaceProtocol = 0,
|
|
|
|
.iInterface = 0
|
|
|
|
};
|
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor __attribute__((aligned(2))) endpoint_descriptor =
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
|
|
|
.bLength = sizeof(struct usb_endpoint_descriptor),
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = 0,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 0,
|
|
|
|
.bInterval = 0
|
|
|
|
};
|
|
|
|
|
2008-03-02 22:12:51 +00:00
|
|
|
#define BUFFER_SIZE 512 /* Max 16k because of controller limitations */
|
2008-04-20 16:51:09 +00:00
|
|
|
#if CONFIG_CPU == IMX31L
|
2008-04-18 16:42:50 +00:00
|
|
|
static unsigned char send_buffer[BUFFER_SIZE]
|
|
|
|
USBDEVBSS_ATTR __attribute__((aligned(32)));
|
2008-04-20 16:51:09 +00:00
|
|
|
static unsigned char receive_buffer[32]
|
2008-04-18 16:42:50 +00:00
|
|
|
USBDEVBSS_ATTR __attribute__((aligned(32)));
|
2008-04-20 16:51:09 +00:00
|
|
|
#else
|
|
|
|
static unsigned char send_buffer[BUFFER_SIZE] __attribute__((aligned(32)));
|
|
|
|
static unsigned char receive_buffer[32] __attribute__((aligned(32)));
|
|
|
|
#endif
|
|
|
|
|
2008-03-02 20:45:33 +00:00
|
|
|
static bool busy_sending = false;
|
|
|
|
static int buffer_start;
|
|
|
|
static int buffer_length;
|
|
|
|
static bool active = false;
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static int usb_endpoint;
|
|
|
|
static int usb_interface;
|
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
static struct mutex sendlock SHAREDBSS_ATTR;
|
2007-11-30 00:13:00 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static void sendout(void)
|
|
|
|
{
|
|
|
|
if(buffer_start+buffer_length > BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
/* Buffer wraps. Only send the first part */
|
|
|
|
usb_drv_send_nonblocking(usb_endpoint, &send_buffer[buffer_start],
|
|
|
|
(BUFFER_SIZE - buffer_start));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Send everything */
|
|
|
|
usb_drv_send_nonblocking(usb_endpoint, &send_buffer[buffer_start],
|
|
|
|
buffer_length);
|
|
|
|
}
|
|
|
|
busy_sending=true;
|
|
|
|
}
|
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
int usb_serial_set_first_endpoint(int endpoint)
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
2008-04-26 19:02:16 +00:00
|
|
|
usb_endpoint = endpoint;
|
|
|
|
return endpoint + 1;
|
|
|
|
}
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
int usb_serial_set_first_interface(int interface)
|
|
|
|
{
|
|
|
|
usb_interface = interface;
|
|
|
|
return interface + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int usb_serial_get_config_descriptor(unsigned char *dest,int max_packet_size)
|
|
|
|
{
|
|
|
|
unsigned char *orig_dest = dest;
|
|
|
|
|
|
|
|
endpoint_descriptor.wMaxPacketSize=max_packet_size;
|
|
|
|
interface_descriptor.bInterfaceNumber=usb_interface;
|
2008-03-06 21:25:09 +00:00
|
|
|
|
|
|
|
memcpy(dest,&interface_descriptor,sizeof(struct usb_interface_descriptor));
|
|
|
|
dest+=sizeof(struct usb_interface_descriptor);
|
|
|
|
|
2008-05-06 20:48:50 +00:00
|
|
|
endpoint_descriptor.bEndpointAddress = usb_endpoint | USB_DIR_IN;
|
2008-03-06 21:25:09 +00:00
|
|
|
memcpy(dest,&endpoint_descriptor,sizeof(struct usb_endpoint_descriptor));
|
|
|
|
dest+=sizeof(struct usb_endpoint_descriptor);
|
|
|
|
|
2008-05-06 20:48:50 +00:00
|
|
|
endpoint_descriptor.bEndpointAddress = usb_endpoint | USB_DIR_OUT;
|
2008-03-06 21:25:09 +00:00
|
|
|
memcpy(dest,&endpoint_descriptor,sizeof(struct usb_endpoint_descriptor));
|
2008-04-26 19:02:16 +00:00
|
|
|
dest+=sizeof(struct usb_endpoint_descriptor);
|
|
|
|
|
|
|
|
return (dest - orig_dest);
|
2008-03-06 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
void usb_serial_init_connection(void)
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
|
|
|
/* prime rx endpoint */
|
2008-04-18 16:42:50 +00:00
|
|
|
usb_drv_recv(usb_endpoint, receive_buffer, sizeof receive_buffer);
|
2008-03-06 21:25:09 +00:00
|
|
|
|
|
|
|
/* we come here too after a bus reset, so reset some data */
|
|
|
|
mutex_lock(&sendlock);
|
|
|
|
busy_sending = false;
|
|
|
|
if(buffer_length>0)
|
|
|
|
{
|
|
|
|
sendout();
|
|
|
|
}
|
|
|
|
mutex_unlock(&sendlock);
|
|
|
|
}
|
|
|
|
|
2007-11-30 00:13:00 +00:00
|
|
|
/* called by usb_code_init() */
|
|
|
|
void usb_serial_init(void)
|
|
|
|
{
|
|
|
|
logf("serial: init");
|
2008-03-02 20:45:33 +00:00
|
|
|
busy_sending = false;
|
|
|
|
buffer_start = 0;
|
|
|
|
buffer_length = 0;
|
|
|
|
active = true;
|
|
|
|
mutex_init(&sendlock);
|
|
|
|
}
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
void usb_serial_disconnect(void)
|
2008-03-02 20:45:33 +00:00
|
|
|
{
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_serial_send(unsigned char *data,int length)
|
|
|
|
{
|
|
|
|
if(!active)
|
|
|
|
return;
|
2008-03-02 23:34:30 +00:00
|
|
|
if(length<=0)
|
|
|
|
return;
|
2008-03-02 20:45:33 +00:00
|
|
|
mutex_lock(&sendlock);
|
|
|
|
if(buffer_start+buffer_length > BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
/* current buffer wraps, so new data can't */
|
|
|
|
int available_space = BUFFER_SIZE - buffer_length;
|
|
|
|
length=MIN(length,available_space);
|
2008-03-06 21:25:09 +00:00
|
|
|
memcpy(&send_buffer[(buffer_start+buffer_length)%BUFFER_SIZE],
|
|
|
|
data,length);
|
2008-03-02 20:45:33 +00:00
|
|
|
buffer_length+=length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* current buffer doesn't wrap, so new data might */
|
2008-03-06 21:25:09 +00:00
|
|
|
int available_end_space = (BUFFER_SIZE - (buffer_start+buffer_length));
|
2008-03-02 20:45:33 +00:00
|
|
|
int first_chunk = MIN(length,available_end_space);
|
|
|
|
memcpy(&send_buffer[buffer_start + buffer_length],data,first_chunk);
|
|
|
|
length-=first_chunk;
|
|
|
|
buffer_length+=first_chunk;
|
|
|
|
if(length>0)
|
|
|
|
{
|
|
|
|
/* wrap */
|
|
|
|
memcpy(&send_buffer[0],&data[first_chunk],MIN(length,buffer_start));
|
|
|
|
buffer_length+=MIN(length,buffer_start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(busy_sending)
|
|
|
|
{
|
|
|
|
/* Do nothing. The transfer completion handler will pick it up */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sendout();
|
|
|
|
}
|
|
|
|
mutex_unlock(&sendlock);
|
2007-11-30 00:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called by usb_core_transfer_complete() */
|
2008-04-26 19:02:16 +00:00
|
|
|
void usb_serial_transfer_complete(int ep,bool in, int status, int length)
|
2007-11-30 00:13:00 +00:00
|
|
|
{
|
2008-04-26 19:02:16 +00:00
|
|
|
(void)ep;
|
2008-02-20 22:54:26 +00:00
|
|
|
switch (in) {
|
|
|
|
case false:
|
2008-03-02 20:45:33 +00:00
|
|
|
logf("serial: %s", receive_buffer);
|
|
|
|
/* Data received. TODO : Do something with it ? */
|
2008-04-18 16:42:50 +00:00
|
|
|
usb_drv_recv(usb_endpoint, receive_buffer, sizeof receive_buffer);
|
2007-11-30 00:13:00 +00:00
|
|
|
break;
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
case true:
|
2008-03-02 20:45:33 +00:00
|
|
|
mutex_lock(&sendlock);
|
|
|
|
/* Data sent out. Update circular buffer */
|
|
|
|
if(status == 0)
|
|
|
|
{
|
|
|
|
buffer_start = (buffer_start + length)%BUFFER_SIZE;
|
|
|
|
buffer_length -= length;
|
|
|
|
}
|
|
|
|
busy_sending = false;
|
|
|
|
|
2008-03-02 23:34:30 +00:00
|
|
|
if(buffer_length>0)
|
2008-03-02 20:45:33 +00:00
|
|
|
{
|
|
|
|
sendout();
|
|
|
|
}
|
|
|
|
mutex_unlock(&sendlock);
|
2007-11-30 00:13:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called by usb_core_control_request() */
|
|
|
|
bool usb_serial_control_request(struct usb_ctrlrequest* req)
|
|
|
|
{
|
|
|
|
bool handled = false;
|
|
|
|
switch (req->bRequest) {
|
|
|
|
default:
|
|
|
|
logf("serial: unhandeld req %d", req->bRequest);
|
|
|
|
}
|
|
|
|
return handled;
|
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
|
|
|
|
#endif /*USB_SERIAL*/
|