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
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-11-30 00:13:00 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2009-04-18 20:04:52 +00:00
|
|
|
#include "usb_class_driver.h"
|
2007-11-30 00:13:00 +00:00
|
|
|
//#define LOGF_ENABLE
|
|
|
|
#include "logf.h"
|
|
|
|
|
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)))
|
2009-04-18 21:32:41 +00:00
|
|
|
interface_descriptor =
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
|
|
|
.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
|
|
|
|
2009-04-18 21:32:41 +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
|
|
|
|
};
|
|
|
|
|
2009-02-21 16:36:54 +00:00
|
|
|
#define BUFFER_SIZE 512
|
2009-02-16 21:45:40 +00:00
|
|
|
static unsigned char send_buffer[BUFFER_SIZE]
|
|
|
|
USB_DEVBSS_ATTR __attribute__((aligned(32)));
|
|
|
|
static unsigned char receive_buffer[32]
|
|
|
|
USB_DEVBSS_ATTR __attribute__((aligned(32)));
|
2008-04-20 16:51:09 +00:00
|
|
|
|
2009-02-21 00:28:40 +00:00
|
|
|
static void sendout(void);
|
|
|
|
|
2008-03-02 20:45:33 +00:00
|
|
|
static int buffer_start;
|
2009-02-20 15:20:31 +00:00
|
|
|
/* The number of bytes to transfer that haven't been given to the USB stack yet */
|
2008-03-02 20:45:33 +00:00
|
|
|
static int buffer_length;
|
2009-02-20 15:20:31 +00:00
|
|
|
/* The number of bytes to transfer that have been given to the USB stack */
|
|
|
|
static int buffer_transitlength;
|
2008-03-02 20:45:33 +00:00
|
|
|
static bool active = false;
|
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
static int ep_in, ep_out;
|
2008-03-06 21:25:09 +00:00
|
|
|
static int usb_interface;
|
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
int usb_serial_request_endpoints(struct usb_class_driver *drv)
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
2009-05-16 15:30:09 +00:00
|
|
|
ep_in = usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN, drv);
|
2008-10-03 22:43:16 +00:00
|
|
|
if (ep_in < 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-05-16 15:30:09 +00:00
|
|
|
ep_out = usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT,
|
|
|
|
drv);
|
2008-10-03 22:43:16 +00:00
|
|
|
if (ep_out < 0) {
|
|
|
|
usb_core_release_endpoint(ep_in);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-04-26 19:02:16 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2009-04-18 21:32:41 +00:00
|
|
|
int usb_serial_get_config_descriptor(unsigned char *dest, int max_packet_size)
|
2008-04-26 19:02:16 +00:00
|
|
|
{
|
|
|
|
unsigned char *orig_dest = dest;
|
|
|
|
|
2009-04-18 20:04:52 +00:00
|
|
|
interface_descriptor.bInterfaceNumber = usb_interface;
|
2009-05-16 15:30:09 +00:00
|
|
|
PACK_DATA(dest, interface_descriptor);
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2009-04-18 20:04:52 +00:00
|
|
|
endpoint_descriptor.wMaxPacketSize = max_packet_size;
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
endpoint_descriptor.bEndpointAddress = ep_in;
|
2009-05-16 15:30:09 +00:00
|
|
|
PACK_DATA(dest, endpoint_descriptor);
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
endpoint_descriptor.bEndpointAddress = ep_out;
|
2009-05-16 15:30:09 +00:00
|
|
|
PACK_DATA(dest, endpoint_descriptor);
|
2008-04-26 19:02:16 +00:00
|
|
|
|
|
|
|
return (dest - orig_dest);
|
2008-03-06 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
2009-02-20 15:20:31 +00:00
|
|
|
/* called by usb_core_control_request() */
|
2009-04-18 20:40:50 +00:00
|
|
|
bool usb_serial_control_request(struct usb_ctrlrequest* req, unsigned char* dest)
|
2009-02-20 15:20:31 +00:00
|
|
|
{
|
|
|
|
bool handled = false;
|
2009-04-18 20:40:50 +00:00
|
|
|
|
|
|
|
(void)dest;
|
2009-02-20 15:20:31 +00:00
|
|
|
switch (req->bRequest) {
|
|
|
|
default:
|
|
|
|
logf("serial: unhandeld req %d", req->bRequest);
|
|
|
|
}
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
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-10-03 22:43:16 +00:00
|
|
|
usb_drv_recv(ep_out, 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 */
|
2009-02-21 00:28:40 +00:00
|
|
|
buffer_transitlength = 0;
|
2008-03-06 21:25:09 +00:00
|
|
|
if(buffer_length>0)
|
|
|
|
{
|
|
|
|
sendout();
|
|
|
|
}
|
2009-02-16 21:45:40 +00:00
|
|
|
active=true;
|
2008-03-06 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
buffer_start = 0;
|
|
|
|
buffer_length = 0;
|
2009-02-21 00:28:40 +00:00
|
|
|
buffer_transitlength = 0;
|
2008-03-02 20:45:33 +00:00
|
|
|
active = true;
|
|
|
|
}
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
void usb_serial_disconnect(void)
|
2008-03-02 20:45:33 +00:00
|
|
|
{
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
2009-02-20 15:20:31 +00:00
|
|
|
static void sendout(void)
|
|
|
|
{
|
2009-04-19 19:25:59 +00:00
|
|
|
buffer_transitlength = MIN(buffer_length,BUFFER_SIZE-buffer_start);
|
|
|
|
if(buffer_transitlength > 0)
|
2009-02-20 15:20:31 +00:00
|
|
|
{
|
2009-04-19 19:25:59 +00:00
|
|
|
buffer_length -= buffer_transitlength;
|
2009-02-21 00:28:40 +00:00
|
|
|
usb_drv_send_nonblocking(ep_in, &send_buffer[buffer_start],
|
2009-04-19 19:25:59 +00:00
|
|
|
buffer_transitlength);
|
2009-02-21 00:28:40 +00:00
|
|
|
}
|
2009-02-20 15:20:31 +00:00
|
|
|
}
|
|
|
|
|
2008-03-02 20:45:33 +00:00
|
|
|
void usb_serial_send(unsigned char *data,int length)
|
|
|
|
{
|
2009-04-19 19:25:59 +00:00
|
|
|
int freestart, available_end_space, i;
|
|
|
|
|
|
|
|
if (!active||length<=0)
|
2008-03-02 23:34:30 +00:00
|
|
|
return;
|
2009-04-19 19:25:59 +00:00
|
|
|
|
|
|
|
i=buffer_start+buffer_length+buffer_transitlength;
|
|
|
|
freestart=i%BUFFER_SIZE;
|
|
|
|
available_end_space=BUFFER_SIZE-i;
|
|
|
|
|
|
|
|
if (0>=available_end_space)
|
2009-04-18 21:32:41 +00:00
|
|
|
{
|
2009-04-19 19:25:59 +00:00
|
|
|
/* current buffer wraps, so new data can't wrap */
|
|
|
|
int available_space = BUFFER_SIZE -
|
|
|
|
(buffer_length + buffer_transitlength);
|
|
|
|
|
|
|
|
length = MIN(length,available_space);
|
|
|
|
memcpy(&send_buffer[freestart],data,length);
|
2008-03-02 20:45:33 +00:00
|
|
|
buffer_length+=length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* current buffer doesn't wrap, so new data might */
|
|
|
|
int first_chunk = MIN(length,available_end_space);
|
2009-04-19 19:25:59 +00:00
|
|
|
|
2009-02-20 15:20:31 +00:00
|
|
|
memcpy(&send_buffer[freestart],data,first_chunk);
|
2008-03-02 20:45:33 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2009-04-19 19:25:59 +00:00
|
|
|
|
|
|
|
if (buffer_transitlength==0)
|
2008-03-02 20:45:33 +00:00
|
|
|
sendout();
|
2009-04-19 19:25:59 +00:00
|
|
|
/* else do nothing. The transfer completion handler will pick it up */
|
2007-11-30 00:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called by usb_core_transfer_complete() */
|
2008-10-03 22:43:16 +00:00
|
|
|
void usb_serial_transfer_complete(int ep,int dir, int status, int length)
|
2007-11-30 00:13:00 +00:00
|
|
|
{
|
2008-04-26 19:02:16 +00:00
|
|
|
(void)ep;
|
2009-04-19 19:25:59 +00:00
|
|
|
(void)length;
|
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
switch (dir) {
|
|
|
|
case USB_DIR_OUT:
|
2008-03-02 20:45:33 +00:00
|
|
|
logf("serial: %s", receive_buffer);
|
|
|
|
/* Data received. TODO : Do something with it ? */
|
2009-02-20 15:20:31 +00:00
|
|
|
|
|
|
|
/* Get the next bit */
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_drv_recv(ep_out, receive_buffer, sizeof receive_buffer);
|
2007-11-30 00:13:00 +00:00
|
|
|
break;
|
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
case USB_DIR_IN:
|
2008-03-02 20:45:33 +00:00
|
|
|
/* Data sent out. Update circular buffer */
|
|
|
|
if(status == 0)
|
|
|
|
{
|
2009-04-19 19:25:59 +00:00
|
|
|
/* TODO: Handle (length != buffer_transitlength) */
|
|
|
|
|
|
|
|
buffer_start=(buffer_start+buffer_transitlength)%BUFFER_SIZE;
|
2009-02-21 00:28:40 +00:00
|
|
|
buffer_transitlength = 0;
|
2008-03-02 20:45:33 +00:00
|
|
|
}
|
|
|
|
|
2008-03-02 23:34:30 +00:00
|
|
|
if(buffer_length>0)
|
2008-03-02 20:45:33 +00:00
|
|
|
sendout();
|
2007-11-30 00:13:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|