1
0
mirror of git://projects.qi-hardware.com/iris.git synced 2024-11-05 06:43:44 +02:00
iris/source/usb-mass-storage.ccp

774 lines
22 KiB
Plaintext
Raw Normal View History

2010-09-02 00:27:14 +03:00
#pypp 0
// Iris: micro-kernel for a capability-based operating system.
// source/usb-mass-storage.ccp: USB mass storage device driver.
// Copyright 2009-2010 Bas Wijnen <wijnen@debian.org>
//
// 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 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "iris.hh"
#include "devices.hh"
#define ARCH
#include "arch.hh"
#if 0
States and expected interrupts:
IDLE: after reset or csw.
IN interrupt: csw received, do nothing.
OUT interrupt: cbw; handle
-> IDLE (no data; csw sent)
-> CSW (data sent in one packet)
-> TX (more than one packet to send)
-> RX (receive packets)
TX: transmitting data.
IN interrupt: host received data; send more.
-> TX (more to send)
-> CSW (last data has now been sent)
RX: receiving data.
OUT interrupt: host sent data; handle.
-> RX (more to receive)
-> IDLE (done receiving; send csw)
CSW: waiting to transmit csw.
IN interrupt: TX is done; send csw
-> IDLE
#endif
extern "C":
void *memset (char *s, int c, unsigned long n):
Iris::debug ("memset called: %x %x->%x\n", s, n, c)
for unsigned i = 0; i < n; ++i:
s[i] = c
return s
class Udc:
typedef unsigned char u8
typedef unsigned short u16
typedef unsigned int u32
typedef u8 string
// The ugly stuff is because pypp doesn't support __attribute__.
/**/struct Setup {
u8 request_type;
u8 request;
u16 value;
u16 index;
u16 length;
} __attribute__ ((packed))
/**/struct Device {
static u8 const Type = 1;
u8 length;
u8 type;
u16 usb_version;
u8 dev_class;
u8 subclass;
u8 protocol;
u8 max_packet_size0;
u16 vendor;
u16 product;
u16 dev_version;
string s_manufacturer;
string s_product;
string s_serial;
u8 num_configurations;
} __attribute__ ((packed))
/**/struct Configuration {
static u8 const Type = 2;
u8 length;
u8 type;
u16 total_length;
u8 num_interfaces;
u8 configuration_value;
u8 configuration;
u8 attributes;
u8 max_power;
} __attribute__ ((packed))
/**/struct Interface {
static u8 const Type = 4;
u8 length;
u8 type;
u8 interface;
u8 alternate;
u8 num_endpoints;
u8 iface_class;
u8 subclass;
u8 protocol;
string name;
} __attribute__ ((packed))
/**/struct Endpoint {
static u8 const Type = 5;
u8 length;
u8 type;
u8 address;
u8 attributes;
u16 max_packet_size;
u8 interval;
} __attribute__ ((packed))
/**/struct Device_Qualifier {
static u8 const Type = 6;
u8 length;
u8 type;
u16 version;
u8 dev_class;
u8 subclass;
u8 protocol;
u8 max_packet_size0;
u8 num_configurations;
u8 reserved;
} __attribute__ ((packed))
/**/struct Langs {
static u8 const Type = 3;
u8 length;
u8 type;
u8 lang;
} __attribute__ ((packed))
template <unsigned size> struct String {
static u8 const Type = 3;
u8 length;
u8 type;
u16 data[size];
} __attribute__ ((packed))
/**/struct CBW {
u32 sig;
u32 tag;
u32 length;
u8 flags;
u8 lun;
u8 size;
u8 data[16];
enum Code {
TEST_UNIT_READY = 0x00,
2010-09-09 22:48:35 +03:00
REQUEST_SENSE = 0x03,
FORMAT_UNIT = 0x04,
INQUIRY = 0x12,
RESERVE6 = 0x16,
RELEASE6 = 0x17,
SEND_DIAGNOSTIC = 0x1d,
2010-09-02 00:27:14 +03:00
READ_CAPACITY = 0x25,
2010-09-09 22:48:35 +03:00
READ10 = 0x28,
WRITE10 = 0x2a,
RESERVE10 = 0x56,
RELEASE10 = 0x57
2010-09-02 00:27:14 +03:00
};
} __attribute__ ((packed))
static unsigned const max_packet_size0 = 64
static unsigned const max_packet_size_bulk = 64
enum Requests:
GET_STATUS = 0
CLEAR_FEATURE = 1
SET_FEATURE = 3
SET_ADDRESS = 5
GET_DESCRIPTOR = 6
SET_DESCRIPTOR = 7
GET_CONFIGURATION = 8
SET_CONFIGURATION = 9
GET_INTERFACE = 10
SET_INTERFACE = 11
SYNCH_FRAME = 12
enum Storage_requests:
BULK_ONLY_RESET = 0xff
GET_MAX_LUN = 0xfe
enum Request_types:
STANDARD_TO_DEVICE = 0
CLASS_TO_DEVICE = 0x20
VENDOR_TO_DEVICE = 0x40
STANDARD_TO_INTERFACE = 1
CLASS_TO_INTERFACE = 0x21
VENDOR_TO_INTERFACE = 0x41
STANDARD_TO_ENDPOINT = 2
CLASS_TO_ENDPOINT = 0x22
VENDOR_TO_ENDPOINT = 0x42
STANDARD_FROM_DEVICE = 0x80
CLASS_FROM_DEVICE = 0xa0
VENDOR_FROM_DEVICE = 0xc0
STANDARD_FROM_INTERFACE = 0x81
CLASS_FROM_INTERFACE = 0xa1
VENDOR_FROM_INTERFACE = 0xc1
STANDARD_FROM_ENDPOINT = 0x82
CLASS_FROM_ENDPOINT = 0xa2
VENDOR_FROM_ENDPOINT = 0xc2
enum Endpoint_types:
CONTROL = 0
ISOCHRONOUS = 1
BULK = 2
INTERRUPT = 3
enum Endpoint_features:
ENDPOINT_HALT = 0
/**/struct my_config {
Configuration config;
Interface interface;
Endpoint endpoint[2];
} __attribute__ ((packed))
static Device device_descriptor
//static Device_Qualifier device_qualifier_descriptor
static my_config config_descriptor; //, other_config_descriptor
static String <1> s_langs
static String <6> s_manufacturer
static String <16> s_product
static String <12> s_serial
char configuration
unsigned get_descriptor (unsigned type, unsigned idx, unsigned len)
unsigned handle_setup (Setup *s)
void irq_usb ()
void irq_in0 ()
void irq_out ()
void send_csw ()
unsigned big_endian (unsigned src)
bool handle_interrupt (bool usb, bool in)
void stall (unsigned error)
bool stalling[3]
2010-09-02 00:27:14 +03:00
unsigned residue
unsigned status
unsigned tag
2010-09-02 00:27:14 +03:00
unsigned block_bits
Iris::WBlock block
2010-09-09 22:48:35 +03:00
Iris::Page buffer_page
// A random address to map the buffer.
static unsigned const buffer = 0x15000
2010-09-02 00:27:14 +03:00
public:
void init (Iris::WBlock b)
void log (unsigned c)
void interrupt ()
void send (unsigned ep, char const *data, unsigned length, unsigned maxlength)
void send_padded (char const *data, unsigned length, unsigned maxlength)
2010-09-02 00:27:14 +03:00
Udc::Device Udc::device_descriptor
Udc::my_config Udc::config_descriptor
Udc::String <1> Udc::s_langs
Udc::String <6> Udc::s_manufacturer
Udc::String <16> Udc::s_product
Udc::String <12> Udc::s_serial
void Udc::init (Iris::WBlock b):
block = b
block_bits = block.get_align_bits ()
2010-09-09 22:48:35 +03:00
// Set up the buffer page.
buffer_page = Iris::my_memory.create_page ()
buffer_page.set_flags (Iris::Page::PAYING)
Iris::my_memory.map (buffer_page, buffer)
2010-09-02 00:27:14 +03:00
// Initialize the globals. My method of compiling doesn't handle global constructors.
device_descriptor = (Device){ sizeof (Device), Device::Type, 0x200, 0, 0, 0, max_packet_size0, 0xfffe, 0x0002, 0x100, 1, 2, 3, 1 }
config_descriptor = (my_config){
(Configuration){ sizeof (Configuration), Configuration::Type, sizeof (my_config), 1, 1, 0, 0xc0, 30 },
(Interface){ sizeof (Interface), Interface::Type, 0, 0, 2, 0x8, 0x6, 0x50, 0 }, {
(Endpoint){ sizeof (Endpoint), Endpoint::Type, 1, BULK, max_packet_size_bulk, 0 },
(Endpoint){ sizeof (Endpoint), Endpoint::Type, 0x82, BULK, max_packet_size_bulk, 0 }
}
}
s_langs = (String <1>){ sizeof (String <1>), String <1>::Type, { 0x0409 } }
s_manufacturer = (String <6>){ sizeof (String <6>), String <6>::Type, { 's', 'h', 'e', 'v', 'e', 'k' } }
s_product = (String <16>){ sizeof (String <16>), String <16>::Type, { 'I', 'r', 'i', 's', ' ', 'o', 'n', ' ', 'N', 'a', 'n', 'o', 'N', 'o', 't', 'e' } }
s_serial = (String <12>){ sizeof (String <12>), String <12>::Type, { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B' } }
cpm_start_udc ()
// Disconnect from the bus and don't try to get high-speed.
UDC_POWER = 0
UDC_TESTMODE = 0
configuration = 0
2010-09-09 22:48:35 +03:00
// Set max packet sizes.
UDC_INDEX = 1
UDC_OUTMAXP = max_packet_size_bulk
UDC_OUTCSR = UDC_OUTCSR_CDT | UDC_OUTCSR_FF
UDC_OUTCSR = UDC_OUTCSR_CDT | UDC_OUTCSR_FF
UDC_INDEX = 2
UDC_INMAXP = max_packet_size_bulk
UDC_INCSRH = UDC_INCSRH_MODE
UDC_INCSR = UDC_INCSR_CDT | UDC_INCSR_FF
UDC_INCSR = UDC_INCSR_CDT | UDC_INCSR_FF
2010-09-02 00:27:14 +03:00
// exit suspend mode by reading the interrupt register.
unsigned i = UDC_INTRUSB
// reset all pending endpoint interrupts.
i = UDC_INTRIN
i = UDC_INTROUT
// enable interrupt on bus reset.
UDC_INTRUSBE = UDC_INTR_RESET
2010-09-09 22:48:35 +03:00
// enable interrupts on endpoint 0 and in endpoint 2
UDC_INTRINE = 1 << 0 | 1 << 2
// and on out endpoint 1.
UDC_INTROUTE = 1 << 1
2010-09-02 00:27:14 +03:00
// Wait a while.
Iris::sleep (HZ / 10)
// Connect to the host.
UDC_POWER = UDC_POWER_SOFTCONN
// Initialize cbw state
status = 0
residue = 0
void Udc::send (unsigned ep, char const *data, unsigned length, unsigned maxlength):
2010-09-02 00:27:14 +03:00
if maxlength < length:
length = maxlength
unsigned i
for i = 0; (length - i & ~3) > 0 && i < length; i += 4:
UDC_FIFO (ep) = ((unsigned *)data)[i / 4]
2010-09-09 22:48:35 +03:00
//kdebug_num (((unsigned *)data)[i / 4], 8)
//kdebug (" ")
2010-09-02 00:27:14 +03:00
for ; i < length; ++i:
UDC_FIFO8 (ep) = data[i]
2010-09-09 22:48:35 +03:00
//kdebug_num (data[i], 2)
//kdebug (" ")
2010-09-02 00:27:14 +03:00
void Udc::send_padded (char const *data, unsigned length, unsigned maxlength):
2010-09-09 22:48:35 +03:00
UDC_INDEX = 2
if UDC_INCSR & UDC_INCSR_INPKTRDY:
Iris::panic (0, "sending padded not possible because a packet is already waiting.\n")
2010-09-02 00:27:14 +03:00
unsigned len = length < maxlength ? length : maxlength
residue = maxlength - len
len = (len + 3) & ~3
send (2, data, len, maxlength)
2010-09-02 00:27:14 +03:00
while len + 3 < maxlength:
UDC_FIFO (2) = 0
2010-09-02 00:27:14 +03:00
len += 4
2010-09-09 22:48:35 +03:00
//kdebug_char ('-')
2010-09-02 00:27:14 +03:00
while len < maxlength:
UDC_FIFO8 (2) = 0
2010-09-02 00:27:14 +03:00
++len
2010-09-09 22:48:35 +03:00
//kdebug_char ('.')
UDC_INCSR |= UDC_INCSR_INPKTRDY
while true:
Iris::register_interrupt (IRQ_UDC)
Iris::wait_for_interrupt (IRQ_UDC)
2010-09-09 22:48:35 +03:00
//kdebug ("interrupt pad\t")
unsigned usb = UDC_INTRUSB
unsigned in = UDC_INTRIN
unsigned out = UDC_INTROUT
if usb & 4 || in & 1:
2010-09-09 22:48:35 +03:00
//kdebug ("general interrupt pad\t")
if !handle_interrupt (usb & 4, in & 1):
return
if out & 2:
Iris::panic (0, "out interrupt while waiting for in")
if in & 4:
break
2010-09-09 22:48:35 +03:00
//kdebug ("done interrupt pad\n")
2010-09-02 00:27:14 +03:00
unsigned Udc::get_descriptor (unsigned type, unsigned idx, unsigned len):
switch type:
case Configuration::Type:
if idx != 0:
return false
2010-09-09 22:48:35 +03:00
//Iris::debug ("get config descriptor\n")
send (0, reinterpret_cast <char const *> (&config_descriptor), sizeof (config_descriptor), len)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
case Device::Type:
if idx != 0:
return false
2010-09-09 22:48:35 +03:00
//Iris::debug ("get device descriptor\n")
send (0, reinterpret_cast <char const *> (&device_descriptor), sizeof (device_descriptor), len)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
case Device_Qualifier::Type:
//if idx != 0:
// return false
//send (0, reinterpret_cast <char const *> (&device_qualifier_descriptor), sizeof (device_qualifier_descriptor), len)
//return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
//break
return ~0
// The 6 is an arbitrary number, except that String <6> is instantiated already.
case String <6>::Type:
switch idx:
case 0:
2010-09-09 22:48:35 +03:00
//Iris::debug ("get language descriptor\n")
send (0, reinterpret_cast <char const *> (&s_langs), sizeof (s_langs), len)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
case 1:
2010-09-09 22:48:35 +03:00
//Iris::debug ("get manufacturer descriptor\n")
send (0, reinterpret_cast <char const *> (&s_manufacturer), sizeof (s_manufacturer), len)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
case 2:
2010-09-09 22:48:35 +03:00
//Iris::debug ("get product descriptor\n")
send (0, reinterpret_cast <char const *> (&s_product), sizeof (s_product), len)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
case 3:
2010-09-09 22:48:35 +03:00
//Iris::debug ("get serial descriptor\n")
send (0, reinterpret_cast <char const *> (&s_serial), sizeof (s_serial), len)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
default:
return ~0
default:
return ~0
unsigned Udc::handle_setup (Setup *s):
switch s->request_type:
case STANDARD_TO_DEVICE:
2010-09-09 22:48:35 +03:00
UDC_INDEX = 0
UDC_CSR0 = UDC_CSR0_DATAEND | UDC_CSR0_SVDOUTPKTRDY
2010-09-02 00:27:14 +03:00
switch s->request:
case SET_ADDRESS:
UDC_FADDR = s->value
2010-09-09 22:48:35 +03:00
//Iris::debug ("set address %x\n", s->value)
2010-09-02 00:27:14 +03:00
return 0
case SET_CONFIGURATION:
if s->value >= 2:
return ~0
configuration = s->value
2010-09-09 22:48:35 +03:00
//Iris::debug ("set configuration %x\n", s->value)
2010-09-02 00:27:14 +03:00
return 0
case SET_INTERFACE:
if s->value != 0:
return ~0
2010-09-09 22:48:35 +03:00
//Iris::debug ("set interface %x\n", s->value)
2010-09-02 00:27:14 +03:00
return 0
default:
return ~0
case STANDARD_FROM_DEVICE:
2010-09-09 22:48:35 +03:00
UDC_INDEX = 0
UDC_CSR0 = UDC_CSR0_DATAEND | UDC_CSR0_SVDOUTPKTRDY
2010-09-02 00:27:14 +03:00
switch s->request:
case GET_STATUS:
2010-09-09 22:48:35 +03:00
//Iris::debug ("get status\t")
send (0, "\0\0", 2, s->length)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
case GET_DESCRIPTOR:
return get_descriptor ((s->value >> 8) & 0xff, s->value & 0xff, s->length)
case GET_CONFIGURATION:
2010-09-09 22:48:35 +03:00
//Iris::debug ("get configuration\t")
send (0, &configuration, 1, s->length)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
case GET_INTERFACE:
2010-09-09 22:48:35 +03:00
//Iris::debug ("get interface\t")
send (0, "\0", 1, s->length)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
default:
return ~0
case STANDARD_TO_ENDPOINT:
switch s->request:
case CLEAR_FEATURE:
switch s->value:
case ENDPOINT_HALT:
switch s->index:
case 0x82:
2010-09-09 22:48:35 +03:00
//Iris::debug ("in ep halt reset\n")
2010-09-02 00:27:14 +03:00
UDC_INDEX = 2
2010-09-09 22:48:35 +03:00
UDC_INCSR &= ~UDC_INCSR_SENDSTALL
stalling[2] = false
2010-09-09 22:48:35 +03:00
break
2010-09-02 00:27:14 +03:00
case 1:
2010-09-09 22:48:35 +03:00
//Iris::debug ("out ep halt reset\n")
2010-09-02 00:27:14 +03:00
UDC_INDEX = 1
2010-09-09 22:48:35 +03:00
UDC_OUTCSR &= ~UDC_OUTCSR_SENDSTALL
stalling[1] = false
2010-09-09 22:48:35 +03:00
break
2010-09-02 00:27:14 +03:00
default:
return ~0
2010-09-09 22:48:35 +03:00
UDC_INDEX = 0
UDC_CSR0 = UDC_CSR0_DATAEND | UDC_CSR0_SVDOUTPKTRDY
return 0
2010-09-02 00:27:14 +03:00
default:
return ~0
default:
return ~0
case CLASS_FROM_INTERFACE:
2010-09-09 22:48:35 +03:00
UDC_INDEX = 0
UDC_CSR0 = UDC_CSR0_DATAEND | UDC_CSR0_SVDOUTPKTRDY
2010-09-02 00:27:14 +03:00
switch s->request:
case GET_MAX_LUN:
2010-09-09 22:48:35 +03:00
//Iris::debug ("get max lun\t")
send (0, "\0", 1, s->length)
return UDC_CSR0_INPKTRDY | UDC_CSR0_DATAEND
2010-09-02 00:27:14 +03:00
default:
return ~0
case CLASS_TO_INTERFACE:
2010-09-09 22:48:35 +03:00
UDC_INDEX = 0
UDC_CSR0 = UDC_CSR0_DATAEND | UDC_CSR0_SVDOUTPKTRDY
2010-09-02 00:27:14 +03:00
switch s->request:
case BULK_ONLY_RESET:
2010-09-09 22:48:35 +03:00
//Iris::debug ("bulk reset\n")
2010-09-02 00:27:14 +03:00
return 0
default:
return ~0
default:
Iris::debug ("request: %x %x %x %x %x\n", s->request_type, s->request, s->index, s->length, s->value)
return ~0
void Udc::irq_usb ():
// Reset.
// enable interrupts on endpoint 0 and in endpoint 2
UDC_INTRINE = 1 << 0 | 1 << 2
// and on out endpoint 1.
UDC_INTROUTE = 1 << 1
UDC_INDEX = 1
// Do this twice to flush a double-buffered fifo completely.
2010-09-09 22:48:35 +03:00
UDC_OUTMAXP = max_packet_size_bulk
2010-09-02 00:27:14 +03:00
UDC_OUTCSR |= UDC_OUTCSR_CDT | UDC_OUTCSR_FF
UDC_OUTCSR |= UDC_OUTCSR_CDT | UDC_OUTCSR_FF
UDC_INDEX = 2
2010-09-09 22:48:35 +03:00
UDC_INMAXP = max_packet_size_bulk
2010-09-02 00:27:14 +03:00
UDC_INCSR |= UDC_INCSR_CDT
2010-09-09 22:48:35 +03:00
//Iris::debug ("usb reset\n")
2010-09-02 00:27:14 +03:00
void Udc::irq_in0 ():
// Interrupt on endpoint 0.
UDC_INDEX = 0
2010-09-02 00:27:14 +03:00
unsigned csr = UDC_CSR0
if csr & UDC_CSR0_SENTSTALL:
2010-09-09 22:48:35 +03:00
UDC_CSR0 = 0
//Iris::debug ("stall done\t")
2010-09-02 00:27:14 +03:00
if csr & UDC_CSR0_SETUPEND:
2010-09-09 22:48:35 +03:00
UDC_CSR0 = UDC_CSR0_SVDSETUPEND
//Iris::debug ("setup aborted\t")
2010-09-02 00:27:14 +03:00
if !(csr & UDC_CSR0_OUTPKTRDY):
2010-09-09 22:48:35 +03:00
//Iris::debug ("no packet 0: %x\n", csr)
2010-09-02 00:27:14 +03:00
return
union { unsigned d[2]; Setup s; } packet
packet.d[0] = UDC_FIFO (0)
packet.d[1] = UDC_FIFO (0)
if !(packet.s.request_type & 0x80) && packet.s.length > 0:
// More data will follow; unsupported.
2010-09-09 22:48:35 +03:00
//Iris::debug ("packet on ep0 too long\n")
UDC_CSR0 = UDC_CSR0_SENDSTALL
2010-09-02 00:27:14 +03:00
return
UDC_INDEX = 1
UDC_OUTCSR |= UDC_OUTCSR_CDT
UDC_INDEX = 2
UDC_INCSR |= UDC_INCSR_CDT
2010-09-09 22:48:35 +03:00
unsigned ret = handle_setup (&packet.s)
2010-09-02 00:27:14 +03:00
UDC_INDEX = 0
if ret == ~0:
Iris::debug ("failed setup: %x %x %x %x %x\n", packet.s.request_type, packet.s.request, packet.s.index, packet.s.length, packet.s.value)
2010-09-09 22:48:35 +03:00
UDC_CSR0 = UDC_CSR0_SENDSTALL
2010-09-02 00:27:14 +03:00
return
2010-09-09 22:48:35 +03:00
if ret:
UDC_CSR0 = ret
//kdebug ("done in0\n")
2010-09-02 00:27:14 +03:00
void Udc::send_csw ():
UDC_INDEX = 2
UDC_FIFO (2) = 0x53425355
UDC_FIFO (2) = tag
2010-09-02 00:27:14 +03:00
UDC_FIFO (2) = residue
UDC_FIFO8 (2) = status
UDC_INCSR |= UDC_INCSR_INPKTRDY
status = 0
residue = 0
while true:
Iris::register_interrupt (IRQ_UDC)
Iris::wait_for_interrupt (IRQ_UDC)
unsigned usb = UDC_INTRUSB
unsigned in = UDC_INTRIN
if usb & 4 || in & 1:
if !handle_interrupt (usb & 4, in & 1):
return
if in & 4:
break
unsigned out = UDC_INTROUT
if out & 2:
Iris::panic (0, "out interrupt while waiting for in after csw")
2010-09-09 22:48:35 +03:00
//kdebug ("sent csw\n")
void Udc::stall (unsigned error):
unsigned index = UDC_INDEX
2010-09-09 22:48:35 +03:00
if stalling[index]:
Iris::debug ("already stalling!\n")
if index == 1:
UDC_OUTCSR |= UDC_OUTCSR_SENDSTALL
else:
UDC_INCSR |= UDC_INCSR_SENDSTALL
stalling[index] = true
while stalling[index]:
2010-09-09 22:48:35 +03:00
//kdebug ("stalling\t")
Iris::register_interrupt (IRQ_UDC)
Iris::wait_for_interrupt (IRQ_UDC)
2010-09-09 22:48:35 +03:00
//kdebug ("stalling interrupt\n")
unsigned usb = UDC_INTRUSB
unsigned in = UDC_INTRIN
2010-09-09 22:48:35 +03:00
if in & 4:
//kdebug ("stall has been sent to in endpoint\n")
UDC_INDEX = 2
UDC_INCSR &= ~UDC_INCSR_SENTSTALL
//Iris::debug ("csr: %x\n", UDC_INCSR)
if usb & 4 || in & 1:
2010-09-09 22:48:35 +03:00
//kdebug ("stuff\n")
if !handle_interrupt (usb & 4, in & 1):
return
unsigned out = UDC_INTROUT
if out & 2:
2010-09-09 22:48:35 +03:00
//kdebug ("stall has been sent to out endpoint\n")
UDC_INDEX = 1
UDC_OUTCSR &= ~UDC_OUTCSR_SENTSTALL
UDC_INDEX = index
if index == 2:
status = error
send_csw ()
2010-09-02 00:27:14 +03:00
unsigned Udc::big_endian (unsigned src):
return src >> 24 | src >> 8 & 0xff00 | src << 8 & 0xff0000 | src << 24
void Udc::irq_out ():
UDC_INDEX = 1
unsigned csr = UDC_OUTCSR
unsigned size = UDC_OUTCOUNT
if !(csr & UDC_OUTCSR_OUTPKTRDY):
// No packet, just a notification.
2010-09-09 22:48:35 +03:00
//kdebug ("no packet\n")
return
if csr & UDC_OUTCSR_SENDSTALL:
// When stalling, do nothing else.
2010-09-09 22:48:35 +03:00
//kdebug ("not responding to out during stall\n")
UDC_OUTCSR = csr & ~UDC_OUTCSR_SENTSTALL
return
// expect a new cbw.
if size != 31:
Iris::debug ("count %d != 31\n", size)
stall (2)
return
union Cbw:
unsigned u[8]
2010-09-09 22:48:35 +03:00
char b[32]
CBW cbw
Cbw cbw
2010-09-09 22:48:35 +03:00
for unsigned i = 0; i < 7; ++i:
cbw.u[i] = UDC_FIFO (1)
2010-09-09 22:48:35 +03:00
for unsigned i = 28; i < 31; ++i:
cbw.b[i] = UDC_FIFO8 (1)
UDC_OUTCSR = csr & ~UDC_OUTCSR_OUTPKTRDY
tag = cbw.cbw.tag
if cbw.cbw.sig != 0x43425355 || cbw.cbw.lun != 0 || cbw.cbw.size == 0 || cbw.cbw.size > 16:
Iris::debug ("sig %x lun %d size %d\n", cbw.cbw.sig, cbw.cbw.lun, cbw.cbw.size)
stall (2)
return
2010-09-09 22:48:35 +03:00
//kdebug ("bulk cbw\t")
2010-09-02 00:27:14 +03:00
UDC_INDEX = 2
bool to_host = cbw.cbw.flags & 0x80
switch cbw.cbw.data[0]:
2010-09-09 22:48:35 +03:00
case CBW::TEST_UNIT_READY:
if to_host || cbw.cbw.length != 0:
stall (2)
2010-09-09 22:48:35 +03:00
return
//Iris::debug ("sending ready response\t")
send_csw ()
break
2010-09-09 22:48:35 +03:00
case CBW::REQUEST_SENSE:
//Iris::debug ("sense requested\t")
send_padded ("\xf0\x00\x05\x00\x00\x00\x00\x00", 8, cbw.cbw.length)
send_csw ()
break
case CBW::FORMAT_UNIT:
Iris::panic (0, "FORMAT_UNIT isn't implemented")
case CBW::INQUIRY:
if !to_host:
stall (2)
2010-09-02 00:27:14 +03:00
return
2010-09-09 22:48:35 +03:00
//Iris::debug ("sending inquiry response\t")
send_padded ("\x00\x00\x04\x02\x1f\x00\x00\x00shevek iris usb stick \x00\x00\x04\x02", 36, cbw.cbw.length)
2010-09-02 00:27:14 +03:00
send_csw ()
break
2010-09-09 22:48:35 +03:00
case CBW::RESERVE6:
Iris::panic (0, "RESERVE6 isn't implemented")
case CBW::RELEASE6:
Iris::panic (0, "RELEASE6 isn't implemented")
case CBW::SEND_DIAGNOSTIC:
Iris::panic (0, "SEND_DIAGNOSTIC isn't implemented")
2010-09-02 00:27:14 +03:00
case CBW::READ_CAPACITY:
if !to_host:
stall (2)
2010-09-09 22:48:35 +03:00
return
2010-09-02 00:27:14 +03:00
unsigned capacity[2]
capacity[0] = big_endian ((block.get_size ().value () >> block_bits) - 1)
capacity[1] = big_endian (1 << block_bits)
2010-09-09 22:48:35 +03:00
//Iris::debug ("sending capacity: %x * %x\t", capacity[0], capacity[1])
send_padded ((char *)capacity, 8, cbw.cbw.length)
send_csw ()
break
2010-09-09 22:48:35 +03:00
case CBW::READ10:
unsigned lba = cbw.cbw.data[2] << 24 | cbw.cbw.data[3] << 16 | cbw.cbw.data[4] << 8 | cbw.cbw.data[5]
unsigned blocks = cbw.cbw.data[7] << 8 | cbw.cbw.data[8]
for unsigned i = 0; i < blocks; ++i:
//Iris::debug ("reading block %d:", lba + i)
// read block lba + i.
buffer_page.set_flags (Iris::Page::FRAME)
block.get_block (lba << block_bits, 1 << block_bits, 0, buffer_page)
for unsigned p = 0; p < 1 << block_bits; p += max_packet_size_bulk:
//Iris::debug (" %d", p)
UDC_INDEX = 2
for unsigned t = 0; t < max_packet_size_bulk; t += 4:
UDC_FIFO (2) = ((unsigned *)buffer)[(p + t) >> 2]
UDC_INCSR |= UDC_INCSR_INPKTRDY
//Iris::debug ("\n")
while true:
Iris::register_interrupt (IRQ_UDC)
Iris::wait_for_interrupt (IRQ_UDC)
//kdebug ("interrupt read10\t")
unsigned usb = UDC_INTRUSB
unsigned in = UDC_INTRIN
unsigned out = UDC_INTROUT
if usb & 4 || in & 1:
//kdebug ("general interrupt read10\t")
if !handle_interrupt (usb & 4, in & 1):
return
if out & 2:
Iris::panic (0, "out interrupt while waiting for in")
if in & 4:
break
send_csw ()
break
2010-09-09 22:48:35 +03:00
case CBW::WRITE10:
Iris::panic (0, "WRITE10 isn't implemented")
case CBW::RESERVE10:
Iris::panic (0, "RESERVE10 isn't implemented")
case CBW::RELEASE10:
Iris::panic (0, "RELEASE10 isn't implemented")
2010-09-02 00:27:14 +03:00
default:
Iris::debug ("cbw:")
for unsigned i = 0; i < cbw.cbw.size; ++i:
kdebug_char (' ')
kdebug_num (cbw.cbw.data[i], 2)
Iris::debug ("\n")
residue = cbw.cbw.length
stall (1)
2010-09-09 22:48:35 +03:00
return
// TODO.
2010-09-02 00:27:14 +03:00
bool Udc::handle_interrupt (bool usb, bool in):
if usb:
2010-09-09 22:48:35 +03:00
//Iris::debug ("usb\t")
// reset.
irq_usb ()
return false
if in:
2010-09-09 22:48:35 +03:00
//Iris::debug ("control\t")
// control request
irq_in0 ()
return true
2010-09-02 00:27:14 +03:00
void Udc::interrupt ():
2010-09-09 22:48:35 +03:00
//Iris::debug ("interrupt\n")
2010-09-02 00:27:14 +03:00
while true:
unsigned usb = UDC_INTRUSB
unsigned in = UDC_INTRIN
2010-09-09 22:48:35 +03:00
bool action = false
2010-09-02 00:27:14 +03:00
if in & 4:
2010-09-09 22:48:35 +03:00
Iris::panic (0, "data request during idle\n")
if usb & 4 || in & 1:
handle_interrupt (usb & 4, in & 1)
action = true
unsigned out = UDC_INTROUT
2010-09-02 00:27:14 +03:00
if out & 2:
irq_out ()
2010-09-09 22:48:35 +03:00
action = true
if !action:
// No more interrupts to handle; this is normal, because we're looping until this happens.
//Iris::debug ("irq done\n")
return
2010-09-02 00:27:14 +03:00
Iris::Num start ():
map_udc ()
map_gpio ()
map_cpm ()
Udc udc
Iris::WBlock nand = Iris::my_parent.get_capability <Iris::WBlock> ()
udc.init (nand)
while true:
Iris::register_interrupt (IRQ_UDC)
Iris::wait ()
udc.interrupt ()